blob: 433ca804d52d298c976b98cc4c7ed4a86aa8c6ed [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>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
NeilBrown41afa3142015-01-13 08:23:18 +130039#include <linux/mmc/slot-gpio.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010040#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020041#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053044#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053045#include <linux/pm_runtime.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010046#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010047
48/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070049#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010050#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053051#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010052#define OMAP_HSMMC_BLK 0x0104
53#define OMAP_HSMMC_ARG 0x0108
54#define OMAP_HSMMC_CMD 0x010C
55#define OMAP_HSMMC_RSP10 0x0110
56#define OMAP_HSMMC_RSP32 0x0114
57#define OMAP_HSMMC_RSP54 0x0118
58#define OMAP_HSMMC_RSP76 0x011C
59#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020060#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010061#define OMAP_HSMMC_HCTL 0x0128
62#define OMAP_HSMMC_SYSCTL 0x012C
63#define OMAP_HSMMC_STAT 0x0130
64#define OMAP_HSMMC_IE 0x0134
65#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053066#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define OMAP_HSMMC_CAPA 0x0140
68
69#define VS18 (1 << 26)
70#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053071#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010072#define SDVS18 (0x5 << 9)
73#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080074#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010075#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010076#define SDVSCLR 0xFFFFF1FF
77#define SDVSDET 0x00000400
78#define AUTOIDLE 0x1
79#define SDBP (1 << 8)
80#define DTO 0xe
81#define ICE 0x1
82#define ICS 0x2
83#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053084#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010085#define CLKD_MASK 0x0000FFC0
86#define CLKD_SHIFT 6
87#define DTO_MASK 0x000F0000
88#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053090#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define DP_SELECT (1 << 21)
92#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053093#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010094#define MSBS (1 << 5)
95#define BCE (1 << 1)
96#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053097#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020098#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +053099#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200100#define CLKEXTFREE (1 << 16)
101#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200102#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define STAT_CLEAR 0xFFFFFFFF
105#define INIT_STREAM_CMD 0x00000000
106#define DUAL_VOLT_OCR_BIT 7
107#define SRC (1 << 25)
108#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700109#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100110
Andreas Fenkartf9459012014-05-29 10:28:03 +0200111/* PSTATE */
112#define DLEV_DAT(x) (1 << (20 + (x)))
113
Venkatraman Sa7e96872012-11-19 22:00:01 +0530114/* Interrupt masks for IE and ISE register */
115#define CC_EN (1 << 0)
116#define TC_EN (1 << 1)
117#define BWR_EN (1 << 4)
118#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200119#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530120#define ERR_EN (1 << 15)
121#define CTO_EN (1 << 16)
122#define CCRC_EN (1 << 17)
123#define CEB_EN (1 << 18)
124#define CIE_EN (1 << 19)
125#define DTO_EN (1 << 20)
126#define DCRC_EN (1 << 21)
127#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530128#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530129#define CERR_EN (1 << 28)
130#define BADA_EN (1 << 29)
131
Balaji T Ka2e77152014-01-21 19:54:42 +0530132#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530133 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
134 BRR_EN | BWR_EN | TC_EN | CC_EN)
135
Balaji T Ka2e77152014-01-21 19:54:42 +0530136#define CNI (1 << 7)
137#define ACIE (1 << 4)
138#define ACEB (1 << 3)
139#define ACCE (1 << 2)
140#define ACTO (1 << 1)
141#define ACNE (1 << 0)
142
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530143#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530144#define MMC_TIMEOUT_MS 20 /* 20 mSec */
145#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400146#define OMAP_MMC_MIN_CLOCK 400000
147#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530148#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149
Balaji T Ke99448f2014-02-19 20:26:40 +0530150#define VDD_1V8 1800000 /* 180000 uV */
151#define VDD_3V0 3000000 /* 300000 uV */
152#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
153
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154/*
155 * One controller can have multiple slots, like on some omap boards using
156 * omap.c controller driver. Luckily this is not currently done on any known
157 * omap_hsmmc.c device.
158 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100159#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160
161/*
162 * MMC Host controller read/write API's
163 */
164#define OMAP_HSMMC_READ(base, reg) \
165 __raw_readl((base) + OMAP_HSMMC_##reg)
166
167#define OMAP_HSMMC_WRITE(base, reg, val) \
168 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
169
Per Forlin9782aff2011-07-01 18:55:23 +0200170struct omap_hsmmc_next {
171 unsigned int dma_len;
172 s32 cookie;
173};
174
Denis Karpov70a33412009-09-22 16:44:59 -0700175struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 struct device *dev;
177 struct mmc_host *mmc;
178 struct mmc_request *mrq;
179 struct mmc_command *cmd;
180 struct mmc_data *data;
181 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100182 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183 /*
184 * vcc == configured supply
185 * vcc_aux == optional
186 * - MMC1, supply for DAT4..DAT7
187 * - MMC2/MMC2, external level shifter voltage supply, for
188 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
189 */
190 struct regulator *vcc;
191 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530192 struct regulator *pbias;
193 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 void __iomem *base;
195 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700196 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200198 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100199 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700200 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100201 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530202 u32 con;
203 u32 hctl;
204 u32 sysctl;
205 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100206 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200207 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100208 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100209 struct dma_chan *tx_chan;
210 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200211 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700212 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700213 int protect_card;
214 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800215 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700216 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530217 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530218 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200219#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
220#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
221#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200222 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100223 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100224
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100225 /* return MMC cover switch state, can be NULL if not supported.
226 *
227 * possible return values:
228 * 0 - closed
229 * 1 - open
230 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100231 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100232
233 /* Card detection IRQs */
234 int card_detect_irq;
235
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100236 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100237};
238
Nishanth Menon59445b12014-02-13 23:45:48 -0600239struct omap_mmc_of_data {
240 u32 reg_offset;
241 u8 controller_flags;
242};
243
Balaji T Kbf129e12014-01-21 19:54:42 +0530244static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
245
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100246static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800247{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530248 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800249
NeilBrown41afa3142015-01-13 08:23:18 +1300250 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800251}
252
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100253static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800254{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530255 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800256
NeilBrown41afa3142015-01-13 08:23:18 +1300257 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258}
259
Adrian Hunterb702b102010-02-15 10:03:35 -0800260#ifdef CONFIG_REGULATOR
261
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100262static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800263{
264 struct omap_hsmmc_host *host =
265 platform_get_drvdata(to_platform_device(dev));
266 int ret = 0;
267
268 /*
269 * If we don't see a Vcc regulator, assume it's a fixed
270 * voltage always-on regulator.
271 */
272 if (!host->vcc)
273 return 0;
274
Andreas Fenkart326119c2014-11-08 15:33:14 +0100275 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100276 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800277
Balaji T Ke99448f2014-02-19 20:26:40 +0530278 if (host->pbias) {
279 if (host->pbias_enabled == 1) {
280 ret = regulator_disable(host->pbias);
281 if (!ret)
282 host->pbias_enabled = 0;
283 }
284 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
285 }
286
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800287 /*
288 * Assume Vcc regulator is used only to power the card ... OMAP
289 * VDDS is used to power the pins, optionally with a transceiver to
290 * support cards using voltages other than VDDS (1.8V nominal). When a
291 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
292 *
293 * In some cases this regulator won't support enable/disable;
294 * e.g. it's a fixed rail for a WLAN chip.
295 *
296 * In other cases vcc_aux switches interface power. Example, for
297 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
298 * chips/cards need an interface voltage rail too.
299 */
300 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530301 if (host->vcc)
302 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800303 /* Enable interface voltage rail, if needed */
304 if (ret == 0 && host->vcc_aux) {
305 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530306 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400307 ret = mmc_regulator_set_ocr(host->mmc,
308 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800309 }
310 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400311 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800312 if (host->vcc_aux)
313 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530314 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400315 /* Then proceed to shut down the local regulator */
316 ret = mmc_regulator_set_ocr(host->mmc,
317 host->vcc, 0);
318 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800319 }
320
Balaji T Ke99448f2014-02-19 20:26:40 +0530321 if (host->pbias) {
322 if (vdd <= VDD_165_195)
323 ret = regulator_set_voltage(host->pbias, VDD_1V8,
324 VDD_1V8);
325 else
326 ret = regulator_set_voltage(host->pbias, VDD_3V0,
327 VDD_3V0);
328 if (ret < 0)
329 goto error_set_power;
330
331 if (host->pbias_enabled == 0) {
332 ret = regulator_enable(host->pbias);
333 if (!ret)
334 host->pbias_enabled = 1;
335 }
336 }
337
Andreas Fenkart326119c2014-11-08 15:33:14 +0100338 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100339 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800340
Balaji T Ke99448f2014-02-19 20:26:40 +0530341error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342 return ret;
343}
344
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800345static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
346{
347 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700348 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800349
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530350 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800351 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530352 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
353 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400354 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800355 } else {
356 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700357 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100358 if (!mmc_pdata(host)->ocr_mask) {
359 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700360 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100361 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530362 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100363 mmc_pdata(host)->ocr_mask);
364 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700365 return -EINVAL;
366 }
367 }
Balaji T K987fd492014-02-19 20:26:40 +0530368 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100369 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370
Balaji T K987fd492014-02-19 20:26:40 +0530371 /* Allow an aux regulator */
372 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
373 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800374
Balaji T Ke99448f2014-02-19 20:26:40 +0530375 reg = devm_regulator_get_optional(host->dev, "pbias");
376 host->pbias = IS_ERR(reg) ? NULL : reg;
377
Balaji T K987fd492014-02-19 20:26:40 +0530378 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100379 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530380 return 0;
381 /*
382 * To disable boot_on regulator, enable regulator
383 * to increase usecount and then disable it.
384 */
385 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
386 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100387 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300388
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100389 mmc_pdata(host)->set_power(host->dev, 1, vdd);
390 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800391 }
392
393 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800394}
395
396static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
397{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100398 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800399}
400
Adrian Hunterb702b102010-02-15 10:03:35 -0800401static inline int omap_hsmmc_have_reg(void)
402{
403 return 1;
404}
405
406#else
407
408static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
409{
410 return -EINVAL;
411}
412
413static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
414{
415}
416
417static inline int omap_hsmmc_have_reg(void)
418{
419 return 0;
420}
421
422#endif
423
NeilBrown41afa3142015-01-13 08:23:18 +1300424static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id);
425
426static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
427 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100428 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800429{
430 int ret;
431
Andreas Fenkart326119c2014-11-08 15:33:14 +0100432 if (gpio_is_valid(pdata->switch_pin)) {
433 if (pdata->cover)
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100434 host->get_cover_state =
435 omap_hsmmc_get_cover_state;
Adrian Hunterb702b102010-02-15 10:03:35 -0800436 else
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100437 host->card_detect = omap_hsmmc_card_detect;
438 host->card_detect_irq =
Andreas Fenkart326119c2014-11-08 15:33:14 +0100439 gpio_to_irq(pdata->switch_pin);
NeilBrown41afa3142015-01-13 08:23:18 +1300440 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_detect);
441 ret = mmc_gpio_request_cd(mmc, pdata->switch_pin, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800442 if (ret)
443 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100444 } else {
445 pdata->switch_pin = -EINVAL;
446 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800447
Andreas Fenkart326119c2014-11-08 15:33:14 +0100448 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300449 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800450 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300451 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100452 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800453
454 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800455}
456
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100457/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300458 * Start clock to the card
459 */
460static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
461{
462 OMAP_HSMMC_WRITE(host->base, SYSCTL,
463 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
464}
465
466/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100467 * Stop clock to the card
468 */
Denis Karpov70a33412009-09-22 16:44:59 -0700469static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100470{
471 OMAP_HSMMC_WRITE(host->base, SYSCTL,
472 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
473 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900474 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100475}
476
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700477static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
478 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700479{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200480 u32 irq_mask = INT_EN_MASK;
481 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700482
483 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200484 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700485
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700486 /* Disable timeout for erases */
487 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530488 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700489
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200490 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700491 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
492 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200493
494 /* latch pending CIRQ, but don't signal MMC core */
495 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
496 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700497 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200498 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700499}
500
501static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
502{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200503 u32 irq_mask = 0;
504 unsigned long flags;
505
506 spin_lock_irqsave(&host->irq_lock, flags);
507 /* no transfer running but need to keep cirq if enabled */
508 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
509 irq_mask |= CIRQ_EN;
510 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
511 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700512 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200513 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700514}
515
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300516/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530517static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300518{
519 u16 dsor = 0;
520
521 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530522 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530523 if (dsor > CLKD_MAX)
524 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300525 }
526
527 return dsor;
528}
529
Andy Shevchenko5934df22011-05-06 12:14:06 +0300530static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
531{
532 struct mmc_ios *ios = &host->mmc->ios;
533 unsigned long regval;
534 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530535 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300536
Venkatraman S8986d312012-08-07 19:10:38 +0530537 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300538
539 omap_hsmmc_stop_clock(host);
540
541 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
542 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530543 clkdiv = calc_divisor(host, ios);
544 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300545 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
546 OMAP_HSMMC_WRITE(host->base, SYSCTL,
547 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
548
549 /* Wait till the ICS bit is set */
550 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
551 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
552 && time_before(jiffies, timeout))
553 cpu_relax();
554
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530555 /*
556 * Enable High-Speed Support
557 * Pre-Requisites
558 * - Controller should support High-Speed-Enable Bit
559 * - Controller should not be using DDR Mode
560 * - Controller should advertise that it supports High Speed
561 * in capabilities register
562 * - MMC/SD clock coming out of controller > 25MHz
563 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100564 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900565 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100566 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530567 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
568 regval = OMAP_HSMMC_READ(host->base, HCTL);
569 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
570 regval |= HSPE;
571 else
572 regval &= ~HSPE;
573
574 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
575 }
576
Andy Shevchenko5934df22011-05-06 12:14:06 +0300577 omap_hsmmc_start_clock(host);
578}
579
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400580static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
581{
582 struct mmc_ios *ios = &host->mmc->ios;
583 u32 con;
584
585 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100586 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
587 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530588 con |= DDR; /* configure in DDR mode */
589 else
590 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400591 switch (ios->bus_width) {
592 case MMC_BUS_WIDTH_8:
593 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
594 break;
595 case MMC_BUS_WIDTH_4:
596 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
597 OMAP_HSMMC_WRITE(host->base, HCTL,
598 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
599 break;
600 case MMC_BUS_WIDTH_1:
601 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
602 OMAP_HSMMC_WRITE(host->base, HCTL,
603 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
604 break;
605 }
606}
607
608static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
609{
610 struct mmc_ios *ios = &host->mmc->ios;
611 u32 con;
612
613 con = OMAP_HSMMC_READ(host->base, CON);
614 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
615 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
616 else
617 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
618}
619
Denis Karpov11dd62a2009-09-22 16:44:43 -0700620#ifdef CONFIG_PM
621
622/*
623 * Restore the MMC host context, if it was lost as result of a
624 * power state change.
625 */
Denis Karpov70a33412009-09-22 16:44:59 -0700626static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700627{
628 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400629 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700630 unsigned long timeout;
631
Tony Lindgren0a82e062013-10-21 00:25:19 +0530632 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
633 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
634 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
635 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
636 return 0;
637
638 host->context_loss++;
639
Balaji T Kc2200ef2012-03-07 09:55:30 -0500640 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700641 if (host->power_mode != MMC_POWER_OFF &&
642 (1 << ios->vdd) <= MMC_VDD_23_24)
643 hctl = SDVS18;
644 else
645 hctl = SDVS30;
646 capa = VS30 | VS18;
647 } else {
648 hctl = SDVS18;
649 capa = VS18;
650 }
651
Balaji T K5a52b082014-05-29 10:28:02 +0200652 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
653 hctl |= IWE;
654
Denis Karpov11dd62a2009-09-22 16:44:43 -0700655 OMAP_HSMMC_WRITE(host->base, HCTL,
656 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
657
658 OMAP_HSMMC_WRITE(host->base, CAPA,
659 OMAP_HSMMC_READ(host->base, CAPA) | capa);
660
661 OMAP_HSMMC_WRITE(host->base, HCTL,
662 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
663
664 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
665 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
666 && time_before(jiffies, timeout))
667 ;
668
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200669 OMAP_HSMMC_WRITE(host->base, ISE, 0);
670 OMAP_HSMMC_WRITE(host->base, IE, 0);
671 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700672
673 /* Do not initialize card-specific things if the power is off */
674 if (host->power_mode == MMC_POWER_OFF)
675 goto out;
676
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400677 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700678
Andy Shevchenko5934df22011-05-06 12:14:06 +0300679 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700680
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400681 omap_hsmmc_set_bus_mode(host);
682
Denis Karpov11dd62a2009-09-22 16:44:43 -0700683out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530684 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
685 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700686 return 0;
687}
688
689/*
690 * Save the MMC host context (store the number of power state changes so far).
691 */
Denis Karpov70a33412009-09-22 16:44:59 -0700692static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700693{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530694 host->con = OMAP_HSMMC_READ(host->base, CON);
695 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
696 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
697 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700698}
699
700#else
701
Denis Karpov70a33412009-09-22 16:44:59 -0700702static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700703{
704 return 0;
705}
706
Denis Karpov70a33412009-09-22 16:44:59 -0700707static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700708{
709}
710
711#endif
712
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100713/*
714 * Send init stream sequence to card
715 * before sending IDLE command
716 */
Denis Karpov70a33412009-09-22 16:44:59 -0700717static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100718{
719 int reg = 0;
720 unsigned long timeout;
721
Adrian Hunterb62f6222009-09-22 16:45:01 -0700722 if (host->protect_card)
723 return;
724
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700726
727 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100728 OMAP_HSMMC_WRITE(host->base, CON,
729 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
730 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
731
732 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530733 while ((reg != CC_EN) && time_before(jiffies, timeout))
734 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100735
736 OMAP_HSMMC_WRITE(host->base, CON,
737 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700738
739 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
740 OMAP_HSMMC_READ(host->base, STAT);
741
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100742 enable_irq(host->irq);
743}
744
745static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700746int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747{
748 int r = 1;
749
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100750 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100751 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100752 return r;
753}
754
755static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700756omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100757 char *buf)
758{
759 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700760 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100761
Denis Karpov70a33412009-09-22 16:44:59 -0700762 return sprintf(buf, "%s\n",
763 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764}
765
Denis Karpov70a33412009-09-22 16:44:59 -0700766static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100767
768static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700769omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100770 char *buf)
771{
772 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700773 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774
Andreas Fenkart326119c2014-11-08 15:33:14 +0100775 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776}
777
Denis Karpov70a33412009-09-22 16:44:59 -0700778static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100779
780/*
781 * Configure the response type and send the cmd.
782 */
783static void
Denis Karpov70a33412009-09-22 16:44:59 -0700784omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100785 struct mmc_data *data)
786{
787 int cmdreg = 0, resptype = 0, cmdtype = 0;
788
Venkatraman S8986d312012-08-07 19:10:38 +0530789 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100790 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
791 host->cmd = cmd;
792
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700793 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100794
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200795 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100796 if (cmd->flags & MMC_RSP_PRESENT) {
797 if (cmd->flags & MMC_RSP_136)
798 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200799 else if (cmd->flags & MMC_RSP_BUSY) {
800 resptype = 3;
801 host->response_busy = 1;
802 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100803 resptype = 2;
804 }
805
806 /*
807 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
808 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
809 * a val of 0x3, rest 0x0.
810 */
811 if (cmd == host->mrq->stop)
812 cmdtype = 0x3;
813
814 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
815
Balaji T Ka2e77152014-01-21 19:54:42 +0530816 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
817 host->mrq->sbc) {
818 cmdreg |= ACEN_ACMD23;
819 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
820 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100821 if (data) {
822 cmdreg |= DP_SELECT | MSBS | BCE;
823 if (data->flags & MMC_DATA_READ)
824 cmdreg |= DDIR;
825 else
826 cmdreg &= ~(DDIR);
827 }
828
829 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530830 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100831
Adrian Hunterb4175772010-05-26 14:42:06 -0700832 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700833
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100834 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
835 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
836}
837
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200838static int
Denis Karpov70a33412009-09-22 16:44:59 -0700839omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200840{
841 if (data->flags & MMC_DATA_WRITE)
842 return DMA_TO_DEVICE;
843 else
844 return DMA_FROM_DEVICE;
845}
846
Russell Kingc5c98922012-04-13 12:14:39 +0100847static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
848 struct mmc_data *data)
849{
850 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
851}
852
Adrian Hunterb4175772010-05-26 14:42:06 -0700853static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
854{
855 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530856 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700857
Venkatraman S31463b12012-04-09 12:08:34 +0530858 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700859 host->req_in_progress = 0;
860 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530861 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700862
863 omap_hsmmc_disable_irq(host);
864 /* Do not complete the request if DMA is still in progress */
865 if (mrq->data && host->use_dma && dma_ch != -1)
866 return;
867 host->mrq = NULL;
868 mmc_request_done(host->mmc, mrq);
869}
870
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100871/*
872 * Notify the transfer complete to MMC core
873 */
874static void
Denis Karpov70a33412009-09-22 16:44:59 -0700875omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100876{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200877 if (!data) {
878 struct mmc_request *mrq = host->mrq;
879
Adrian Hunter23050102009-09-22 16:44:57 -0700880 /* TC before CC from CMD6 - don't know why, but it happens */
881 if (host->cmd && host->cmd->opcode == 6 &&
882 host->response_busy) {
883 host->response_busy = 0;
884 return;
885 }
886
Adrian Hunterb4175772010-05-26 14:42:06 -0700887 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200888 return;
889 }
890
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891 host->data = NULL;
892
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100893 if (!data->error)
894 data->bytes_xfered += data->blocks * (data->blksz);
895 else
896 data->bytes_xfered = 0;
897
Balaji T Kbf129e12014-01-21 19:54:42 +0530898 if (data->stop && (data->error || !host->mrq->sbc))
899 omap_hsmmc_start_command(host, data->stop, NULL);
900 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700901 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902}
903
904/*
905 * Notify the core about command completion
906 */
907static void
Denis Karpov70a33412009-09-22 16:44:59 -0700908omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909{
Balaji T Kbf129e12014-01-21 19:54:42 +0530910 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530911 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530912 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530913 omap_hsmmc_start_dma_transfer(host);
914 omap_hsmmc_start_command(host, host->mrq->cmd,
915 host->mrq->data);
916 return;
917 }
918
Balaji T K2177fa92014-05-09 22:16:52 +0530919 host->cmd = NULL;
920
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100921 if (cmd->flags & MMC_RSP_PRESENT) {
922 if (cmd->flags & MMC_RSP_136) {
923 /* response type 2 */
924 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
925 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
926 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
927 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
928 } else {
929 /* response types 1, 1b, 3, 4, 5, 6 */
930 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
931 }
932 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700933 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530934 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935}
936
937/*
938 * DMA clean up for command errors
939 */
Denis Karpov70a33412009-09-22 16:44:59 -0700940static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100941{
Adrian Hunterb4175772010-05-26 14:42:06 -0700942 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530943 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700944
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200945 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100946
Venkatraman S31463b12012-04-09 12:08:34 +0530947 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700948 dma_ch = host->dma_ch;
949 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530950 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700951
952 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100953 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
954
955 dmaengine_terminate_all(chan);
956 dma_unmap_sg(chan->device->dev,
957 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700958 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100959
Per Forlin053bf342011-11-07 21:55:11 +0530960 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961 }
962 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963}
964
965/*
966 * Readable error output
967 */
968#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300969static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100970{
971 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700972 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300973 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
974 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
975 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
976 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977 };
978 char res[256];
979 char *buf = res;
980 int len, i;
981
982 len = sprintf(buf, "MMC IRQ 0x%x :", status);
983 buf += len;
984
Denis Karpov70a33412009-09-22 16:44:59 -0700985 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700987 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100988 buf += len;
989 }
990
Venkatraman S8986d312012-08-07 19:10:38 +0530991 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100992}
Adrian Hunter699b9582011-05-06 12:14:01 +0300993#else
994static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
995 u32 status)
996{
997}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998#endif /* CONFIG_MMC_DEBUG */
999
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001000/*
1001 * MMC controller internal state machines reset
1002 *
1003 * Used to reset command or data internal state machines, using respectively
1004 * SRC or SRD bit of SYSCTL register
1005 * Can be called from interrupt context
1006 */
Denis Karpov70a33412009-09-22 16:44:59 -07001007static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1008 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001009{
1010 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301011 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001012
1013 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1014 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1015
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001016 /*
1017 * OMAP4 ES2 and greater has an updated reset logic.
1018 * Monitor a 0->1 transition first
1019 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001020 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001021 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001022 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301023 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001024 }
1025 i = 0;
1026
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001027 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1028 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301029 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001030
1031 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1032 dev_err(mmc_dev(host->mmc),
1033 "Timeout waiting on controller reset in %s\n",
1034 __func__);
1035}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001036
Balaji T K25e18972012-11-19 21:59:55 +05301037static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1038 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301039{
Balaji T K25e18972012-11-19 21:59:55 +05301040 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301041 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301042 if (host->cmd)
1043 host->cmd->error = err;
1044 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301045
1046 if (host->data) {
1047 omap_hsmmc_reset_controller_fsm(host, SRD);
1048 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301049 } else if (host->mrq && host->mrq->cmd)
1050 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301051}
1052
Adrian Hunterb4175772010-05-26 14:42:06 -07001053static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001054{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001056 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301057 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001058
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301060 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061
Venkatraman Sa7e96872012-11-19 22:00:01 +05301062 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001063 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001064
Venkatraman Sa7e96872012-11-19 22:00:01 +05301065 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301066 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301067 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301068 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301069 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301070 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1071
Balaji T Ka2e77152014-01-21 19:54:42 +05301072 if (status & ACE_EN) {
1073 u32 ac12;
1074 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1075 if (!(ac12 & ACNE) && host->mrq->sbc) {
1076 end_cmd = 1;
1077 if (ac12 & ACTO)
1078 error = -ETIMEDOUT;
1079 else if (ac12 & (ACCE | ACEB | ACIE))
1080 error = -EILSEQ;
1081 host->mrq->sbc->error = error;
1082 hsmmc_command_incomplete(host, error, end_cmd);
1083 }
1084 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1085 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301086 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301087 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301088 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 }
1090 }
1091
Francesco Lavra7472bab2013-06-29 08:25:12 +02001092 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301093 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001094 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301095 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001096 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001097}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001098
Adrian Hunterb4175772010-05-26 14:42:06 -07001099/*
1100 * MMC controller IRQ handler
1101 */
1102static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1103{
1104 struct omap_hsmmc_host *host = dev_id;
1105 int status;
1106
1107 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001108 while (status & (INT_EN_MASK | CIRQ_EN)) {
1109 if (host->req_in_progress)
1110 omap_hsmmc_do_irq(host, status);
1111
1112 if (status & CIRQ_EN)
1113 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301114
Adrian Hunterb4175772010-05-26 14:42:06 -07001115 /* Flush posted write */
1116 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301117 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119 return IRQ_HANDLED;
1120}
1121
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001122static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1123{
1124 struct omap_hsmmc_host *host = dev_id;
1125
1126 /* cirq is level triggered, disable to avoid infinite loop */
1127 spin_lock(&host->irq_lock);
1128 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1129 disable_irq_nosync(host->wake_irq);
1130 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1131 }
1132 spin_unlock(&host->irq_lock);
1133 pm_request_resume(host->dev); /* no use counter */
1134
1135 return IRQ_HANDLED;
1136}
1137
Denis Karpov70a33412009-09-22 16:44:59 -07001138static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001139{
1140 unsigned long i;
1141
1142 OMAP_HSMMC_WRITE(host->base, HCTL,
1143 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1144 for (i = 0; i < loops_per_jiffy; i++) {
1145 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1146 break;
1147 cpu_relax();
1148 }
1149}
1150
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151/*
David Brownelleb250822009-02-17 14:49:01 -08001152 * Switch MMC interface voltage ... only relevant for MMC1.
1153 *
1154 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1155 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1156 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001157 */
Denis Karpov70a33412009-09-22 16:44:59 -07001158static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159{
1160 u32 reg_val = 0;
1161 int ret;
1162
1163 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301164 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301165 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301166 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167
1168 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001169 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170
1171 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001172 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001173 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301174 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301175 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301176 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178 if (ret != 0)
1179 goto err;
1180
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181 OMAP_HSMMC_WRITE(host->base, HCTL,
1182 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1183 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185 /*
1186 * If a MMC dual voltage card is detected, the set_ios fn calls
1187 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001188 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001189 *
David Brownelleb250822009-02-17 14:49:01 -08001190 * Cope with a bit of slop in the range ... per data sheets:
1191 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1192 * but recommended values are 1.71V to 1.89V
1193 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1194 * but recommended values are 2.7V to 3.3V
1195 *
1196 * Board setup code shouldn't permit anything very out-of-range.
1197 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1198 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199 */
David Brownelleb250822009-02-17 14:49:01 -08001200 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001202 else
1203 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
1205 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001206 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207
1208 return 0;
1209err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301210 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001211 return ret;
1212}
1213
Adrian Hunterb62f6222009-09-22 16:45:01 -07001214/* Protect the card while the cover is open */
1215static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1216{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001217 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001218 return;
1219
1220 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001221 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001222 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301223 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001224 "card is now accessible\n",
1225 mmc_hostname(host->mmc));
1226 host->protect_card = 0;
1227 }
1228 } else {
1229 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301230 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001231 "card is now inaccessible\n",
1232 mmc_hostname(host->mmc));
1233 host->protect_card = 1;
1234 }
1235 }
1236}
1237
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001239 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001241static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001242{
NeilBrown7efab4f2011-12-30 12:35:13 +11001243 struct omap_hsmmc_host *host = dev_id;
Adrian Huntera6b22402009-09-22 16:44:45 -07001244 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001245
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001247
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001248 if (host->card_detect)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001249 carddetect = host->card_detect(host->dev);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001250 else {
1251 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001252 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001253 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001254
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001255 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001256 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001257 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001258 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001259 return IRQ_HANDLED;
1260}
1261
Russell Kingc5c98922012-04-13 12:14:39 +01001262static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001263{
Russell Kingc5c98922012-04-13 12:14:39 +01001264 struct omap_hsmmc_host *host = param;
1265 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001266 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001267 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001268
Russell Kingc5c98922012-04-13 12:14:39 +01001269 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001270 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001271 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001273 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001274
Adrian Hunter770d7432011-05-06 12:14:11 +03001275 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001276 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001277 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001278 dma_unmap_sg(chan->device->dev,
1279 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001280 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001281
1282 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001283 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001284 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001285
1286 /* If DMA has finished after TC, complete the request */
1287 if (!req_in_progress) {
1288 struct mmc_request *mrq = host->mrq;
1289
1290 host->mrq = NULL;
1291 mmc_request_done(host->mmc, mrq);
1292 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001293}
1294
Per Forlin9782aff2011-07-01 18:55:23 +02001295static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1296 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001297 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001298 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001299{
1300 int dma_len;
1301
1302 if (!next && data->host_cookie &&
1303 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301304 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001305 " host->next_data.cookie %d\n",
1306 __func__, data->host_cookie, host->next_data.cookie);
1307 data->host_cookie = 0;
1308 }
1309
1310 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001311 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001312 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001313 omap_hsmmc_get_dma_dir(host, data));
1314
1315 } else {
1316 dma_len = host->next_data.dma_len;
1317 host->next_data.dma_len = 0;
1318 }
1319
1320
1321 if (dma_len == 0)
1322 return -EINVAL;
1323
1324 if (next) {
1325 next->dma_len = dma_len;
1326 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1327 } else
1328 host->dma_len = dma_len;
1329
1330 return 0;
1331}
1332
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334 * Routine to configure and start DMA for the MMC card
1335 */
Balaji T K9d025332014-01-21 19:54:42 +05301336static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001337 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001338{
Russell King26b88522012-04-13 12:27:37 +01001339 struct dma_slave_config cfg;
1340 struct dma_async_tx_descriptor *tx;
1341 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001342 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001343 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001344
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001345 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001346 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001347 struct scatterlist *sgl;
1348
1349 sgl = data->sg + i;
1350 if (sgl->length % data->blksz)
1351 return -EINVAL;
1352 }
1353 if ((data->blksz % 4) != 0)
1354 /* REVISIT: The MMC buffer increments only when MSB is written.
1355 * Return error for blksz which is non multiple of four.
1356 */
1357 return -EINVAL;
1358
Adrian Hunterb4175772010-05-26 14:42:06 -07001359 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001360
Russell Kingc5c98922012-04-13 12:14:39 +01001361 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001362
Russell King26b88522012-04-13 12:27:37 +01001363 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1364 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1365 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1366 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1367 cfg.src_maxburst = data->blksz / 4;
1368 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001369
Russell King26b88522012-04-13 12:27:37 +01001370 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001371 if (ret)
1372 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001373
Russell King26b88522012-04-13 12:27:37 +01001374 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1375 if (ret)
1376 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001377
Russell King26b88522012-04-13 12:27:37 +01001378 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1379 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1380 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1381 if (!tx) {
1382 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1383 /* FIXME: cleanup */
1384 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001385 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001386
Russell King26b88522012-04-13 12:27:37 +01001387 tx->callback = omap_hsmmc_dma_callback;
1388 tx->callback_param = host;
1389
1390 /* Does not fail */
1391 dmaengine_submit(tx);
1392
1393 host->dma_ch = 1;
1394
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001395 return 0;
1396}
1397
Denis Karpov70a33412009-09-22 16:44:59 -07001398static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001399 unsigned int timeout_ns,
1400 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001401{
1402 unsigned int timeout, cycle_ns;
1403 uint32_t reg, clkd, dto = 0;
1404
1405 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1406 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1407 if (clkd == 0)
1408 clkd = 1;
1409
Balaji T K6e3076c2014-01-21 19:54:42 +05301410 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001411 timeout = timeout_ns / cycle_ns;
1412 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001413 if (timeout) {
1414 while ((timeout & 0x80000000) == 0) {
1415 dto += 1;
1416 timeout <<= 1;
1417 }
1418 dto = 31 - dto;
1419 timeout <<= 1;
1420 if (timeout && dto)
1421 dto += 1;
1422 if (dto >= 13)
1423 dto -= 13;
1424 else
1425 dto = 0;
1426 if (dto > 14)
1427 dto = 14;
1428 }
1429
1430 reg &= ~DTO_MASK;
1431 reg |= dto << DTO_SHIFT;
1432 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1433}
1434
Balaji T K9d025332014-01-21 19:54:42 +05301435static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1436{
1437 struct mmc_request *req = host->mrq;
1438 struct dma_chan *chan;
1439
1440 if (!req->data)
1441 return;
1442 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1443 | (req->data->blocks << 16));
1444 set_data_timeout(host, req->data->timeout_ns,
1445 req->data->timeout_clks);
1446 chan = omap_hsmmc_get_dma_chan(host, req->data);
1447 dma_async_issue_pending(chan);
1448}
1449
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450/*
1451 * Configure block length for MMC/SD cards and initiate the transfer.
1452 */
1453static int
Denis Karpov70a33412009-09-22 16:44:59 -07001454omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001455{
1456 int ret;
1457 host->data = req->data;
1458
1459 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001460 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001461 /*
1462 * Set an arbitrary 100ms data timeout for commands with
1463 * busy signal.
1464 */
1465 if (req->cmd->flags & MMC_RSP_BUSY)
1466 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467 return 0;
1468 }
1469
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001470 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301471 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301473 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001474 return ret;
1475 }
1476 }
1477 return 0;
1478}
1479
Per Forlin9782aff2011-07-01 18:55:23 +02001480static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1481 int err)
1482{
1483 struct omap_hsmmc_host *host = mmc_priv(mmc);
1484 struct mmc_data *data = mrq->data;
1485
Russell King26b88522012-04-13 12:27:37 +01001486 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001487 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001488
Russell King26b88522012-04-13 12:27:37 +01001489 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1490 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001491 data->host_cookie = 0;
1492 }
1493}
1494
1495static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1496 bool is_first_req)
1497{
1498 struct omap_hsmmc_host *host = mmc_priv(mmc);
1499
1500 if (mrq->data->host_cookie) {
1501 mrq->data->host_cookie = 0;
1502 return ;
1503 }
1504
Russell Kingc5c98922012-04-13 12:14:39 +01001505 if (host->use_dma) {
1506 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001507
Per Forlin9782aff2011-07-01 18:55:23 +02001508 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001509 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001510 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001511 }
Per Forlin9782aff2011-07-01 18:55:23 +02001512}
1513
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001514/*
1515 * Request function. for read/write operation
1516 */
Denis Karpov70a33412009-09-22 16:44:59 -07001517static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001518{
Denis Karpov70a33412009-09-22 16:44:59 -07001519 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001520 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521
Adrian Hunterb4175772010-05-26 14:42:06 -07001522 BUG_ON(host->req_in_progress);
1523 BUG_ON(host->dma_ch != -1);
1524 if (host->protect_card) {
1525 if (host->reqs_blocked < 3) {
1526 /*
1527 * Ensure the controller is left in a consistent
1528 * state by resetting the command and data state
1529 * machines.
1530 */
1531 omap_hsmmc_reset_controller_fsm(host, SRD);
1532 omap_hsmmc_reset_controller_fsm(host, SRC);
1533 host->reqs_blocked += 1;
1534 }
1535 req->cmd->error = -EBADF;
1536 if (req->data)
1537 req->data->error = -EBADF;
1538 req->cmd->retries = 0;
1539 mmc_request_done(mmc, req);
1540 return;
1541 } else if (host->reqs_blocked)
1542 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001543 WARN_ON(host->mrq != NULL);
1544 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301545 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001546 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001547 if (err) {
1548 req->cmd->error = err;
1549 if (req->data)
1550 req->data->error = err;
1551 host->mrq = NULL;
1552 mmc_request_done(mmc, req);
1553 return;
1554 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301555 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301556 omap_hsmmc_start_command(host, req->sbc, NULL);
1557 return;
1558 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001559
Balaji T K9d025332014-01-21 19:54:42 +05301560 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001561 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562}
1563
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001564/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001565static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001566{
Denis Karpov70a33412009-09-22 16:44:59 -07001567 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001568 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301570 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001571
Adrian Huntera3621462009-09-22 16:44:42 -07001572 if (ios->power_mode != host->power_mode) {
1573 switch (ios->power_mode) {
1574 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001575 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001576 break;
1577 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001578 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001579 break;
1580 case MMC_POWER_ON:
1581 do_send_init_stream = 1;
1582 break;
1583 }
1584 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001585 }
1586
Denis Karpovdd498ef2009-09-22 16:44:49 -07001587 /* FIXME: set registers based only on changes to ios */
1588
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001589 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001590
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301591 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001592 /* Only MMC1 can interface at 3V without some flavor
1593 * of external transceiver; but they all handle 1.8V.
1594 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001595 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301596 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001597 /*
1598 * The mmc_select_voltage fn of the core does
1599 * not seem to set the power_mode to
1600 * MMC_POWER_UP upon recalculating the voltage.
1601 * vdd 1.8v.
1602 */
Denis Karpov70a33412009-09-22 16:44:59 -07001603 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1604 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605 "Switch operation failed\n");
1606 }
1607 }
1608
Andy Shevchenko5934df22011-05-06 12:14:06 +03001609 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001610
Adrian Huntera3621462009-09-22 16:44:42 -07001611 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612 send_init_stream(host);
1613
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001614 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001615
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301616 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001617}
1618
1619static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1620{
Denis Karpov70a33412009-09-22 16:44:59 -07001621 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001622
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001623 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001624 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001625 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001626}
1627
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001628static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1629{
1630 struct omap_hsmmc_host *host = mmc_priv(mmc);
1631
Andreas Fenkart326119c2014-11-08 15:33:14 +01001632 if (mmc_pdata(host)->init_card)
1633 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001634}
1635
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001636static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1637{
1638 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001639 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001640 unsigned long flags;
1641
1642 spin_lock_irqsave(&host->irq_lock, flags);
1643
Balaji T K5a52b082014-05-29 10:28:02 +02001644 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001645 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1646 if (enable) {
1647 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1648 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001649 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001650 } else {
1651 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1652 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001653 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001654 }
Balaji T K5a52b082014-05-29 10:28:02 +02001655 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001656 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1657
1658 /*
1659 * if enable, piggy back detection on current request
1660 * but always disable immediately
1661 */
1662 if (!host->req_in_progress || !enable)
1663 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1664
1665 /* flush posted write */
1666 OMAP_HSMMC_READ(host->base, IE);
1667
1668 spin_unlock_irqrestore(&host->irq_lock, flags);
1669}
1670
1671static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1672{
1673 struct mmc_host *mmc = host->mmc;
1674 int ret;
1675
1676 /*
1677 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1678 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1679 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1680 * with functional clock disabled.
1681 */
1682 if (!host->dev->of_node || !host->wake_irq)
1683 return -ENODEV;
1684
1685 /* Prevent auto-enabling of IRQ */
1686 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1687 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1688 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1689 mmc_hostname(mmc), host);
1690 if (ret) {
1691 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1692 goto err;
1693 }
1694
1695 /*
1696 * Some omaps don't have wake-up path from deeper idle states
1697 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1698 */
1699 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001700 struct pinctrl *p = devm_pinctrl_get(host->dev);
1701 if (!p) {
1702 ret = -ENODEV;
1703 goto err_free_irq;
1704 }
1705 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1706 dev_info(host->dev, "missing default pinctrl state\n");
1707 devm_pinctrl_put(p);
1708 ret = -EINVAL;
1709 goto err_free_irq;
1710 }
1711
1712 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1713 dev_info(host->dev, "missing idle pinctrl state\n");
1714 devm_pinctrl_put(p);
1715 ret = -EINVAL;
1716 goto err_free_irq;
1717 }
1718 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001719 }
1720
Balaji T K5a52b082014-05-29 10:28:02 +02001721 OMAP_HSMMC_WRITE(host->base, HCTL,
1722 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001723 return 0;
1724
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001725err_free_irq:
1726 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001727err:
1728 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1729 host->wake_irq = 0;
1730 return ret;
1731}
1732
Denis Karpov70a33412009-09-22 16:44:59 -07001733static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001734{
1735 u32 hctl, capa, value;
1736
1737 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301738 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001739 hctl = SDVS30;
1740 capa = VS30 | VS18;
1741 } else {
1742 hctl = SDVS18;
1743 capa = VS18;
1744 }
1745
1746 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1747 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1748
1749 value = OMAP_HSMMC_READ(host->base, CAPA);
1750 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1751
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001752 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001753 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001754}
1755
Denis Karpov70a33412009-09-22 16:44:59 -07001756static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001757{
Denis Karpov70a33412009-09-22 16:44:59 -07001758 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001759
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301760 pm_runtime_get_sync(host->dev);
1761
Denis Karpovdd498ef2009-09-22 16:44:49 -07001762 return 0;
1763}
1764
Adrian Hunter907d2e72012-02-29 09:17:21 +02001765static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001766{
Denis Karpov70a33412009-09-22 16:44:59 -07001767 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001768
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301769 pm_runtime_mark_last_busy(host->dev);
1770 pm_runtime_put_autosuspend(host->dev);
1771
Denis Karpovdd498ef2009-09-22 16:44:49 -07001772 return 0;
1773}
1774
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001775static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1776 unsigned int direction, int blk_size)
1777{
1778 /* This controller can't do multiblock reads due to hw bugs */
1779 if (direction == MMC_DATA_READ)
1780 return 1;
1781
1782 return blk_size;
1783}
1784
1785static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001786 .enable = omap_hsmmc_enable_fclk,
1787 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001788 .post_req = omap_hsmmc_post_req,
1789 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001790 .request = omap_hsmmc_request,
1791 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001792 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001793 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001794 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001795 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001796};
1797
Denis Karpovd900f712009-09-22 16:44:38 -07001798#ifdef CONFIG_DEBUG_FS
1799
Denis Karpov70a33412009-09-22 16:44:59 -07001800static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001801{
1802 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001803 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001804
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001805 seq_printf(s, "mmc%d:\n", mmc->index);
1806 seq_printf(s, "sdio irq mode\t%s\n",
1807 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1808
1809 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1810 seq_printf(s, "sdio irq \t%s\n",
1811 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1812 : "disabled");
1813 }
1814 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001815
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301816 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001817 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001818 seq_printf(s, "CON:\t\t0x%08x\n",
1819 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001820 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1821 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001822 seq_printf(s, "HCTL:\t\t0x%08x\n",
1823 OMAP_HSMMC_READ(host->base, HCTL));
1824 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1825 OMAP_HSMMC_READ(host->base, SYSCTL));
1826 seq_printf(s, "IE:\t\t0x%08x\n",
1827 OMAP_HSMMC_READ(host->base, IE));
1828 seq_printf(s, "ISE:\t\t0x%08x\n",
1829 OMAP_HSMMC_READ(host->base, ISE));
1830 seq_printf(s, "CAPA:\t\t0x%08x\n",
1831 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001832
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301833 pm_runtime_mark_last_busy(host->dev);
1834 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001835
Denis Karpovd900f712009-09-22 16:44:38 -07001836 return 0;
1837}
1838
Denis Karpov70a33412009-09-22 16:44:59 -07001839static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001840{
Denis Karpov70a33412009-09-22 16:44:59 -07001841 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001842}
1843
1844static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001845 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001846 .read = seq_read,
1847 .llseek = seq_lseek,
1848 .release = single_release,
1849};
1850
Denis Karpov70a33412009-09-22 16:44:59 -07001851static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001852{
1853 if (mmc->debugfs_root)
1854 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1855 mmc, &mmc_regs_fops);
1856}
1857
1858#else
1859
Denis Karpov70a33412009-09-22 16:44:59 -07001860static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001861{
1862}
1863
1864#endif
1865
Rajendra Nayak46856a62012-03-12 20:32:37 +05301866#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001867static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1868 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1869 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1870};
1871
1872static const struct omap_mmc_of_data omap4_mmc_of_data = {
1873 .reg_offset = 0x100,
1874};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001875static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1876 .reg_offset = 0x100,
1877 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1878};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301879
1880static const struct of_device_id omap_mmc_of_match[] = {
1881 {
1882 .compatible = "ti,omap2-hsmmc",
1883 },
1884 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001885 .compatible = "ti,omap3-pre-es3-hsmmc",
1886 .data = &omap3_pre_es3_mmc_of_data,
1887 },
1888 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301889 .compatible = "ti,omap3-hsmmc",
1890 },
1891 {
1892 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001893 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301894 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001895 {
1896 .compatible = "ti,am33xx-hsmmc",
1897 .data = &am33xx_mmc_of_data,
1898 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301899 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001900};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301901MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1902
Andreas Fenkart551434382014-11-08 15:33:09 +01001903static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301904{
Andreas Fenkart551434382014-11-08 15:33:09 +01001905 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301906 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301907
1908 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1909 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301910 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301911
1912 if (of_find_property(np, "ti,dual-volt", NULL))
1913 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1914
NeilBrownfdb9de12015-01-13 08:23:18 +13001915 pdata->switch_pin = -EINVAL;
1916 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301917
1918 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001919 pdata->nonremovable = true;
1920 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301921 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301922
1923 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001924 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301925
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301926 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001927 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301928
Rajendra Nayak46856a62012-03-12 20:32:37 +05301929 return pdata;
1930}
1931#else
Andreas Fenkart551434382014-11-08 15:33:09 +01001932static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301933 *of_get_hsmmc_pdata(struct device *dev)
1934{
Balaji T K19df45b2014-02-28 19:08:18 +05301935 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301936}
1937#endif
1938
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001939static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001940{
Andreas Fenkart551434382014-11-08 15:33:09 +01001941 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001942 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001943 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001945 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301946 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001947 dma_cap_mask_t mask;
1948 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06001949 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301950 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301951
1952 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1953 if (match) {
1954 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001955
1956 if (IS_ERR(pdata))
1957 return PTR_ERR(pdata);
1958
Rajendra Nayak46856a62012-03-12 20:32:37 +05301959 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001960 data = match->data;
1961 pdata->reg_offset = data->reg_offset;
1962 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301963 }
1964 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001965
1966 if (pdata == NULL) {
1967 dev_err(&pdev->dev, "Platform Data is missing\n");
1968 return -ENXIO;
1969 }
1970
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001971 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1972 irq = platform_get_irq(pdev, 0);
1973 if (res == NULL || irq < 0)
1974 return -ENXIO;
1975
Balaji T K77fae212014-05-09 22:16:51 +05301976 base = devm_ioremap_resource(&pdev->dev, res);
1977 if (IS_ERR(base))
1978 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979
Denis Karpov70a33412009-09-22 16:44:59 -07001980 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 if (!mmc) {
1982 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001983 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001984 }
1985
NeilBrownfdb9de12015-01-13 08:23:18 +13001986 ret = mmc_of_parse(mmc);
1987 if (ret)
1988 goto err1;
1989
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990 host = mmc_priv(mmc);
1991 host->mmc = mmc;
1992 host->pdata = pdata;
1993 host->dev = &pdev->dev;
1994 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995 host->dma_ch = -1;
1996 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05301997 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05301998 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08001999 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002000 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302001 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002002
NeilBrown41afa3142015-01-13 08:23:18 +13002003 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002004 if (ret)
2005 goto err_gpio;
2006
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002007 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002008
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002009 if (pdev->dev.of_node)
2010 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2011
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302012 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002013
Daniel Mackd418ed82012-02-19 13:20:33 +01002014 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2015
2016 if (pdata->max_freq > 0)
2017 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002018 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002019 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002020
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002021 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022
Balaji T K96181952014-05-09 22:16:48 +05302023 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024 if (IS_ERR(host->fclk)) {
2025 ret = PTR_ERR(host->fclk);
2026 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027 goto err1;
2028 }
2029
Paul Walmsley9b682562011-10-06 14:50:35 -06002030 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2031 dev_info(&pdev->dev, "multiblock reads disabled due to 35xx erratum 2.1.1.128; MMC read performance may suffer\n");
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07002032 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002033 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002034
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302035 pm_runtime_enable(host->dev);
2036 pm_runtime_get_sync(host->dev);
2037 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2038 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002039
Balaji T K92a3aeb2012-02-24 21:14:34 +05302040 omap_hsmmc_context_save(host);
2041
Balaji T K96181952014-05-09 22:16:48 +05302042 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302043 /*
2044 * MMC can still work without debounce clock.
2045 */
2046 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302047 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302048 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302049 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302050 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002051 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002053 /* Since we do only SG emulation, we can have as many segs
2054 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002055 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002056
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002057 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2058 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2059 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2060 mmc->max_seg_size = mmc->max_req_size;
2061
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002062 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002063 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064
Andreas Fenkart326119c2014-11-08 15:33:14 +01002065 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002066 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002067 mmc->caps |= MMC_CAP_4_BIT_DATA;
2068
Andreas Fenkart326119c2014-11-08 15:33:14 +01002069 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002070 mmc->caps |= MMC_CAP_NONREMOVABLE;
2071
NeilBrownfdb9de12015-01-13 08:23:18 +13002072 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002073
Denis Karpov70a33412009-09-22 16:44:59 -07002074 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302076 if (!pdev->dev.of_node) {
2077 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2078 if (!res) {
2079 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2080 ret = -ENXIO;
2081 goto err_irq;
2082 }
2083 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002084
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302085 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2086 if (!res) {
2087 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2088 ret = -ENXIO;
2089 goto err_irq;
2090 }
2091 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002092 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093
Russell King26b88522012-04-13 12:27:37 +01002094 dma_cap_zero(mask);
2095 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002096
Matt Porterd272fbf2013-05-10 17:42:34 +05302097 host->rx_chan =
2098 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2099 &rx_req, &pdev->dev, "rx");
2100
Russell King26b88522012-04-13 12:27:37 +01002101 if (!host->rx_chan) {
2102 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002103 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002104 goto err_irq;
2105 }
2106
Matt Porterd272fbf2013-05-10 17:42:34 +05302107 host->tx_chan =
2108 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2109 &tx_req, &pdev->dev, "tx");
2110
Russell King26b88522012-04-13 12:27:37 +01002111 if (!host->tx_chan) {
2112 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002113 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002114 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002115 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302118 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002119 mmc_hostname(mmc), host);
2120 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302121 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002122 goto err_irq;
2123 }
2124
Andreas Fenkart326119c2014-11-08 15:33:14 +01002125 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002126 ret = omap_hsmmc_reg_get(host);
2127 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002128 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002129 host->use_reg = 1;
2130 }
2131
Andreas Fenkart326119c2014-11-08 15:33:14 +01002132 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002133
Adrian Hunterb4175772010-05-26 14:42:06 -07002134 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002135
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002136 /*
2137 * For now, only support SDIO interrupt if we have a separate
2138 * wake-up interrupt configured from device tree. This is because
2139 * the wake-up interrupt is needed for idle state and some
2140 * platforms need special quirks. And we don't want to add new
2141 * legacy mux platform init code callbacks any longer as we
2142 * are moving to DT based booting anyways.
2143 */
2144 ret = omap_hsmmc_configure_wake_irq(host);
2145 if (!ret)
2146 mmc->caps |= MMC_CAP_SDIO_IRQ;
2147
Adrian Hunterb62f6222009-09-22 16:45:01 -07002148 omap_hsmmc_protect_card(host);
2149
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150 mmc_add_host(mmc);
2151
Andreas Fenkart326119c2014-11-08 15:33:14 +01002152 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2154 if (ret < 0)
2155 goto err_slot_name;
2156 }
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002157 if (host->card_detect_irq && host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158 ret = device_create_file(&mmc->class_dev,
2159 &dev_attr_cover_switch);
2160 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002161 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162 }
2163
Denis Karpov70a33412009-09-22 16:44:59 -07002164 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302165 pm_runtime_mark_last_busy(host->dev);
2166 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002167
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002168 return 0;
2169
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002170err_slot_name:
2171 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002172 if (host->use_reg)
2173 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002174err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002175 if (host->tx_chan)
2176 dma_release_channel(host->tx_chan);
2177 if (host->rx_chan)
2178 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302179 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002180 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302181 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302182 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002183err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002184err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002185 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002186err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002187 return ret;
2188}
2189
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002190static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002191{
Denis Karpov70a33412009-09-22 16:44:59 -07002192 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002193
Felipe Balbi927ce942012-03-14 11:18:27 +02002194 pm_runtime_get_sync(host->dev);
2195 mmc_remove_host(host->mmc);
2196 if (host->use_reg)
2197 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002198
Russell Kingc5c98922012-04-13 12:14:39 +01002199 if (host->tx_chan)
2200 dma_release_channel(host->tx_chan);
2201 if (host->rx_chan)
2202 dma_release_channel(host->rx_chan);
2203
Felipe Balbi927ce942012-03-14 11:18:27 +02002204 pm_runtime_put_sync(host->dev);
2205 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302206 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302207 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208
Balaji T K9d1f0282012-10-15 21:35:07 +05302209 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002210
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211 return 0;
2212}
2213
2214#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002215static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216{
Felipe Balbi927ce942012-03-14 11:18:27 +02002217 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2218
2219 if (!host)
2220 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002221
Felipe Balbi927ce942012-03-14 11:18:27 +02002222 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002223
2224 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002225 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2226 OMAP_HSMMC_WRITE(host->base, IE, 0);
2227 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002228 OMAP_HSMMC_WRITE(host->base, HCTL,
2229 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2230 }
2231
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002232 /* do not wake up due to sdio irq */
2233 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2234 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2235 disable_irq(host->wake_irq);
2236
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302237 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302238 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002239
Eliad Peller31f9d462011-11-22 16:02:17 +02002240 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002241 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242}
2243
2244/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002245static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002246{
Felipe Balbi927ce942012-03-14 11:18:27 +02002247 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2248
2249 if (!host)
2250 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002251
Felipe Balbi927ce942012-03-14 11:18:27 +02002252 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002253
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302254 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302255 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002256
Felipe Balbi927ce942012-03-14 11:18:27 +02002257 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2258 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002259
Felipe Balbi927ce942012-03-14 11:18:27 +02002260 omap_hsmmc_protect_card(host);
2261
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002262 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2263 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2264 enable_irq(host->wake_irq);
2265
Felipe Balbi927ce942012-03-14 11:18:27 +02002266 pm_runtime_mark_last_busy(host->dev);
2267 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002268 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269}
2270
2271#else
Denis Karpov70a33412009-09-22 16:44:59 -07002272#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302273#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002274#endif
2275
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302276static int omap_hsmmc_runtime_suspend(struct device *dev)
2277{
2278 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002279 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002280 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302281
2282 host = platform_get_drvdata(to_platform_device(dev));
2283 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002284 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302285
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002286 spin_lock_irqsave(&host->irq_lock, flags);
2287 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2288 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2289 /* disable sdio irq handling to prevent race */
2290 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2291 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002292
2293 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2294 /*
2295 * dat1 line low, pending sdio irq
2296 * race condition: possible irq handler running on
2297 * multi-core, abort
2298 */
2299 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2300 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2301 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2302 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2303 pm_runtime_mark_last_busy(dev);
2304 ret = -EBUSY;
2305 goto abort;
2306 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002307
Andreas Fenkart97978a42014-05-29 10:28:04 +02002308 pinctrl_pm_select_idle_state(dev);
2309
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002310 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2311 enable_irq(host->wake_irq);
2312 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002313 } else {
2314 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002315 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002316
Andreas Fenkartf9459012014-05-29 10:28:03 +02002317abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002318 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002319 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302320}
2321
2322static int omap_hsmmc_runtime_resume(struct device *dev)
2323{
2324 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002325 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302326
2327 host = platform_get_drvdata(to_platform_device(dev));
2328 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002329 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302330
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002331 spin_lock_irqsave(&host->irq_lock, flags);
2332 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2333 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2334 /* sdio irq flag can't change while in runtime suspend */
2335 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2336 disable_irq_nosync(host->wake_irq);
2337 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2338 }
2339
Andreas Fenkart97978a42014-05-29 10:28:04 +02002340 pinctrl_pm_select_default_state(host->dev);
2341
2342 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002343 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2344 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2345 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002346 } else {
2347 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002348 }
2349 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302350 return 0;
2351}
2352
Kevin Hilmana791daa2010-05-26 14:42:07 -07002353static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002354 .suspend = omap_hsmmc_suspend,
2355 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302356 .runtime_suspend = omap_hsmmc_runtime_suspend,
2357 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002358};
2359
2360static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002361 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002362 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002363 .driver = {
2364 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002365 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302366 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002367 },
2368};
2369
Felipe Balbib7964502012-03-14 11:18:32 +02002370module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002371MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2372MODULE_LICENSE("GPL");
2373MODULE_ALIAS("platform:" DRIVER_NAME);
2374MODULE_AUTHOR("Texas Instruments Inc");