blob: 3fde2f9dfb25e9466eb7ecea5b60f09749e06145 [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>
Tony Lindgren5b83b222015-05-21 15:51:52 -070046#include <linux/pm_wakeirq.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010047#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010048
49/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070050#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053052#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010053#define OMAP_HSMMC_BLK 0x0104
54#define OMAP_HSMMC_ARG 0x0108
55#define OMAP_HSMMC_CMD 0x010C
56#define OMAP_HSMMC_RSP10 0x0110
57#define OMAP_HSMMC_RSP32 0x0114
58#define OMAP_HSMMC_RSP54 0x0118
59#define OMAP_HSMMC_RSP76 0x011C
60#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020061#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010062#define OMAP_HSMMC_HCTL 0x0128
63#define OMAP_HSMMC_SYSCTL 0x012C
64#define OMAP_HSMMC_STAT 0x0130
65#define OMAP_HSMMC_IE 0x0134
66#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053067#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define OMAP_HSMMC_CAPA 0x0140
69
70#define VS18 (1 << 26)
71#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053072#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010073#define SDVS18 (0x5 << 9)
74#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080075#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010076#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010077#define SDVSCLR 0xFFFFF1FF
78#define SDVSDET 0x00000400
79#define AUTOIDLE 0x1
80#define SDBP (1 << 8)
81#define DTO 0xe
82#define ICE 0x1
83#define ICS 0x2
84#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053085#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010086#define CLKD_MASK 0x0000FFC0
87#define CLKD_SHIFT 6
88#define DTO_MASK 0x000F0000
89#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053091#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define DP_SELECT (1 << 21)
93#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053094#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010095#define MSBS (1 << 5)
96#define BCE (1 << 1)
97#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053098#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020099#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +0530100#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200101#define CLKEXTFREE (1 << 16)
102#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200103#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100105#define STAT_CLEAR 0xFFFFFFFF
106#define INIT_STREAM_CMD 0x00000000
107#define DUAL_VOLT_OCR_BIT 7
108#define SRC (1 << 25)
109#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700110#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100111
Andreas Fenkartf9459012014-05-29 10:28:03 +0200112/* PSTATE */
113#define DLEV_DAT(x) (1 << (20 + (x)))
114
Venkatraman Sa7e96872012-11-19 22:00:01 +0530115/* Interrupt masks for IE and ISE register */
116#define CC_EN (1 << 0)
117#define TC_EN (1 << 1)
118#define BWR_EN (1 << 4)
119#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200120#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530121#define ERR_EN (1 << 15)
122#define CTO_EN (1 << 16)
123#define CCRC_EN (1 << 17)
124#define CEB_EN (1 << 18)
125#define CIE_EN (1 << 19)
126#define DTO_EN (1 << 20)
127#define DCRC_EN (1 << 21)
128#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530129#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530130#define CERR_EN (1 << 28)
131#define BADA_EN (1 << 29)
132
Balaji T Ka2e77152014-01-21 19:54:42 +0530133#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530134 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
135 BRR_EN | BWR_EN | TC_EN | CC_EN)
136
Balaji T Ka2e77152014-01-21 19:54:42 +0530137#define CNI (1 << 7)
138#define ACIE (1 << 4)
139#define ACEB (1 << 3)
140#define ACCE (1 << 2)
141#define ACTO (1 << 1)
142#define ACNE (1 << 0)
143
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530144#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530145#define MMC_TIMEOUT_MS 20 /* 20 mSec */
146#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400147#define OMAP_MMC_MIN_CLOCK 400000
148#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530149#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100150
Balaji T Ke99448f2014-02-19 20:26:40 +0530151#define VDD_1V8 1800000 /* 180000 uV */
152#define VDD_3V0 3000000 /* 300000 uV */
153#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
154
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100155/*
156 * One controller can have multiple slots, like on some omap boards using
157 * omap.c controller driver. Luckily this is not currently done on any known
158 * omap_hsmmc.c device.
159 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100160#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100161
162/*
163 * MMC Host controller read/write API's
164 */
165#define OMAP_HSMMC_READ(base, reg) \
166 __raw_readl((base) + OMAP_HSMMC_##reg)
167
168#define OMAP_HSMMC_WRITE(base, reg, val) \
169 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
170
Per Forlin9782aff2011-07-01 18:55:23 +0200171struct omap_hsmmc_next {
172 unsigned int dma_len;
173 s32 cookie;
174};
175
Denis Karpov70a33412009-09-22 16:44:59 -0700176struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 struct device *dev;
178 struct mmc_host *mmc;
179 struct mmc_request *mrq;
180 struct mmc_command *cmd;
181 struct mmc_data *data;
182 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100183 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800184 /*
185 * vcc == configured supply
186 * vcc_aux == optional
187 * - MMC1, supply for DAT4..DAT7
188 * - MMC2/MMC2, external level shifter voltage supply, for
189 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
190 */
191 struct regulator *vcc;
192 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530193 struct regulator *pbias;
194 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100195 void __iomem *base;
196 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700197 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100198 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200199 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700201 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100202 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530203 u32 con;
204 u32 hctl;
205 u32 sysctl;
206 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100207 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200208 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100209 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100210 struct dma_chan *tx_chan;
211 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200212 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700213 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700214 int protect_card;
215 int reqs_blocked;
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 */
Per Forlin9782aff2011-07-01 18:55:23 +0200221 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100222 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100223
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100224 /* return MMC cover switch state, can be NULL if not supported.
225 *
226 * possible return values:
227 * 0 - closed
228 * 1 - open
229 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100230 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100231
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100232 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100233};
234
Nishanth Menon59445b12014-02-13 23:45:48 -0600235struct omap_mmc_of_data {
236 u32 reg_offset;
237 u8 controller_flags;
238};
239
Balaji T Kbf129e12014-01-21 19:54:42 +0530240static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
241
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100242static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800243{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530244 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800245
NeilBrown41afa3142015-01-13 08:23:18 +1300246 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800247}
248
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100249static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800250{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530251 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800252
NeilBrown41afa3142015-01-13 08:23:18 +1300253 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800254}
255
Adrian Hunterb702b102010-02-15 10:03:35 -0800256#ifdef CONFIG_REGULATOR
257
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100258static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800259{
260 struct omap_hsmmc_host *host =
261 platform_get_drvdata(to_platform_device(dev));
262 int ret = 0;
263
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200264 if (mmc_pdata(host)->set_power)
265 return mmc_pdata(host)->set_power(dev, power_on, vdd);
266
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800267 /*
268 * If we don't see a Vcc regulator, assume it's a fixed
269 * voltage always-on regulator.
270 */
271 if (!host->vcc)
272 return 0;
273
Andreas Fenkart326119c2014-11-08 15:33:14 +0100274 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100275 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800276
Balaji T Ke99448f2014-02-19 20:26:40 +0530277 if (host->pbias) {
278 if (host->pbias_enabled == 1) {
279 ret = regulator_disable(host->pbias);
280 if (!ret)
281 host->pbias_enabled = 0;
282 }
283 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
284 }
285
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800286 /*
287 * Assume Vcc regulator is used only to power the card ... OMAP
288 * VDDS is used to power the pins, optionally with a transceiver to
289 * support cards using voltages other than VDDS (1.8V nominal). When a
290 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
291 *
292 * In some cases this regulator won't support enable/disable;
293 * e.g. it's a fixed rail for a WLAN chip.
294 *
295 * In other cases vcc_aux switches interface power. Example, for
296 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
297 * chips/cards need an interface voltage rail too.
298 */
299 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530300 if (host->vcc)
301 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800302 /* Enable interface voltage rail, if needed */
303 if (ret == 0 && host->vcc_aux) {
304 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530305 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400306 ret = mmc_regulator_set_ocr(host->mmc,
307 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800308 }
309 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400310 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800311 if (host->vcc_aux)
312 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530313 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400314 /* Then proceed to shut down the local regulator */
315 ret = mmc_regulator_set_ocr(host->mmc,
316 host->vcc, 0);
317 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800318 }
319
Balaji T Ke99448f2014-02-19 20:26:40 +0530320 if (host->pbias) {
321 if (vdd <= VDD_165_195)
322 ret = regulator_set_voltage(host->pbias, VDD_1V8,
323 VDD_1V8);
324 else
325 ret = regulator_set_voltage(host->pbias, VDD_3V0,
326 VDD_3V0);
327 if (ret < 0)
328 goto error_set_power;
329
330 if (host->pbias_enabled == 0) {
331 ret = regulator_enable(host->pbias);
332 if (!ret)
333 host->pbias_enabled = 1;
334 }
335 }
336
Andreas Fenkart326119c2014-11-08 15:33:14 +0100337 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100338 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800339
Balaji T Ke99448f2014-02-19 20:26:40 +0530340error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800341 return ret;
342}
343
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800344static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
345{
kishore kadiyala64be9782010-10-01 16:35:28 -0700346 int ocr_value = 0;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530347 int ret;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800348
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200349 if (mmc_pdata(host)->set_power)
350 return 0;
351
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530352 host->vcc = devm_regulator_get_optional(host->dev, "vmmc");
353 if (IS_ERR(host->vcc)) {
354 ret = PTR_ERR(host->vcc);
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530355 if (ret != -ENODEV)
356 return ret;
Kishon Vijay Abraham I7d607f92015-08-27 14:43:53 +0530357 dev_dbg(host->dev, "unable to get vmmc regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530358 PTR_ERR(host->vcc));
359 host->vcc = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360 } else {
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530361 ocr_value = mmc_regulator_get_ocrmask(host->vcc);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100362 if (!mmc_pdata(host)->ocr_mask) {
363 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700364 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100365 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530366 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100367 mmc_pdata(host)->ocr_mask);
368 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700369 return -EINVAL;
370 }
371 }
Balaji T K987fd492014-02-19 20:26:40 +0530372 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800373
Balaji T K987fd492014-02-19 20:26:40 +0530374 /* Allow an aux regulator */
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530375 host->vcc_aux = devm_regulator_get_optional(host->dev, "vmmc_aux");
376 if (IS_ERR(host->vcc_aux)) {
377 ret = PTR_ERR(host->vcc_aux);
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530378 if (ret != -ENODEV)
379 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530380 dev_dbg(host->dev, "unable to get vmmc_aux regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530381 PTR_ERR(host->vcc_aux));
382 host->vcc_aux = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530383 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800384
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530385 host->pbias = devm_regulator_get_optional(host->dev, "pbias");
386 if (IS_ERR(host->pbias)) {
387 ret = PTR_ERR(host->pbias);
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530388 if (ret != -ENODEV)
389 return ret;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530390 dev_dbg(host->dev, "unable to get pbias regulator %ld\n",
Kishon Vijay Abraham Ic299dc32015-08-27 14:43:55 +0530391 PTR_ERR(host->pbias));
392 host->pbias = NULL;
Kishon Vijay Abraham I6a9b2ff2015-08-27 14:43:54 +0530393 }
Balaji T Ke99448f2014-02-19 20:26:40 +0530394
Balaji T K987fd492014-02-19 20:26:40 +0530395 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100396 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530397 return 0;
398 /*
399 * To disable boot_on regulator, enable regulator
400 * to increase usecount and then disable it.
401 */
402 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
403 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100404 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300405
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200406 omap_hsmmc_set_power(host->dev, 1, vdd);
407 omap_hsmmc_set_power(host->dev, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800408 }
409
410 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800411}
412
Adrian Hunterb702b102010-02-15 10:03:35 -0800413static inline int omap_hsmmc_have_reg(void)
414{
415 return 1;
416}
417
418#else
419
Andreas Fenkartf7f0f032015-07-07 20:38:43 +0200420static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
421{
422 return 0;
423}
424
Adrian Hunterb702b102010-02-15 10:03:35 -0800425static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
426{
427 return -EINVAL;
428}
429
Adrian Hunterb702b102010-02-15 10:03:35 -0800430static inline int omap_hsmmc_have_reg(void)
431{
432 return 0;
433}
434
435#endif
436
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100437static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300438
439static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
440 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100441 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800442{
443 int ret;
444
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100445 if (gpio_is_valid(pdata->gpio_cod)) {
446 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800447 if (ret)
448 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100449
450 host->get_cover_state = omap_hsmmc_get_cover_state;
451 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100452 } else if (gpio_is_valid(pdata->gpio_cd)) {
453 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100454 if (ret)
455 return ret;
456
457 host->card_detect = omap_hsmmc_card_detect;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100458 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800459
Andreas Fenkart326119c2014-11-08 15:33:14 +0100460 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300461 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800462 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300463 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100464 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800465
466 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800467}
468
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100469/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300470 * Start clock to the card
471 */
472static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
473{
474 OMAP_HSMMC_WRITE(host->base, SYSCTL,
475 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
476}
477
478/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100479 * Stop clock to the card
480 */
Denis Karpov70a33412009-09-22 16:44:59 -0700481static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100482{
483 OMAP_HSMMC_WRITE(host->base, SYSCTL,
484 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
485 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900486 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100487}
488
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700489static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
490 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700491{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200492 u32 irq_mask = INT_EN_MASK;
493 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700494
495 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200496 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700497
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700498 /* Disable timeout for erases */
499 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530500 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700501
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200502 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700503 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
504 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200505
506 /* latch pending CIRQ, but don't signal MMC core */
507 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
508 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700509 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200510 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700511}
512
513static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
514{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200515 u32 irq_mask = 0;
516 unsigned long flags;
517
518 spin_lock_irqsave(&host->irq_lock, flags);
519 /* no transfer running but need to keep cirq if enabled */
520 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
521 irq_mask |= CIRQ_EN;
522 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
523 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700524 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200525 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700526}
527
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300528/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530529static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300530{
531 u16 dsor = 0;
532
533 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530534 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530535 if (dsor > CLKD_MAX)
536 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300537 }
538
539 return dsor;
540}
541
Andy Shevchenko5934df22011-05-06 12:14:06 +0300542static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
543{
544 struct mmc_ios *ios = &host->mmc->ios;
545 unsigned long regval;
546 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530547 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300548
Venkatraman S8986d312012-08-07 19:10:38 +0530549 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300550
551 omap_hsmmc_stop_clock(host);
552
553 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
554 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530555 clkdiv = calc_divisor(host, ios);
556 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300557 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
558 OMAP_HSMMC_WRITE(host->base, SYSCTL,
559 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
560
561 /* Wait till the ICS bit is set */
562 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
563 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
564 && time_before(jiffies, timeout))
565 cpu_relax();
566
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530567 /*
568 * Enable High-Speed Support
569 * Pre-Requisites
570 * - Controller should support High-Speed-Enable Bit
571 * - Controller should not be using DDR Mode
572 * - Controller should advertise that it supports High Speed
573 * in capabilities register
574 * - MMC/SD clock coming out of controller > 25MHz
575 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100576 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900577 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100578 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530579 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
580 regval = OMAP_HSMMC_READ(host->base, HCTL);
581 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
582 regval |= HSPE;
583 else
584 regval &= ~HSPE;
585
586 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
587 }
588
Andy Shevchenko5934df22011-05-06 12:14:06 +0300589 omap_hsmmc_start_clock(host);
590}
591
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400592static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
593{
594 struct mmc_ios *ios = &host->mmc->ios;
595 u32 con;
596
597 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100598 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
599 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530600 con |= DDR; /* configure in DDR mode */
601 else
602 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400603 switch (ios->bus_width) {
604 case MMC_BUS_WIDTH_8:
605 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
606 break;
607 case MMC_BUS_WIDTH_4:
608 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
609 OMAP_HSMMC_WRITE(host->base, HCTL,
610 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
611 break;
612 case MMC_BUS_WIDTH_1:
613 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
614 OMAP_HSMMC_WRITE(host->base, HCTL,
615 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
616 break;
617 }
618}
619
620static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
621{
622 struct mmc_ios *ios = &host->mmc->ios;
623 u32 con;
624
625 con = OMAP_HSMMC_READ(host->base, CON);
626 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
627 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
628 else
629 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
630}
631
Denis Karpov11dd62a2009-09-22 16:44:43 -0700632#ifdef CONFIG_PM
633
634/*
635 * Restore the MMC host context, if it was lost as result of a
636 * power state change.
637 */
Denis Karpov70a33412009-09-22 16:44:59 -0700638static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700639{
640 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400641 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700642 unsigned long timeout;
643
Tony Lindgren0a82e062013-10-21 00:25:19 +0530644 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
645 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
646 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
647 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
648 return 0;
649
650 host->context_loss++;
651
Balaji T Kc2200ef2012-03-07 09:55:30 -0500652 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700653 if (host->power_mode != MMC_POWER_OFF &&
654 (1 << ios->vdd) <= MMC_VDD_23_24)
655 hctl = SDVS18;
656 else
657 hctl = SDVS30;
658 capa = VS30 | VS18;
659 } else {
660 hctl = SDVS18;
661 capa = VS18;
662 }
663
Balaji T K5a52b082014-05-29 10:28:02 +0200664 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
665 hctl |= IWE;
666
Denis Karpov11dd62a2009-09-22 16:44:43 -0700667 OMAP_HSMMC_WRITE(host->base, HCTL,
668 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
669
670 OMAP_HSMMC_WRITE(host->base, CAPA,
671 OMAP_HSMMC_READ(host->base, CAPA) | capa);
672
673 OMAP_HSMMC_WRITE(host->base, HCTL,
674 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
675
676 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
677 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
678 && time_before(jiffies, timeout))
679 ;
680
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200681 OMAP_HSMMC_WRITE(host->base, ISE, 0);
682 OMAP_HSMMC_WRITE(host->base, IE, 0);
683 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700684
685 /* Do not initialize card-specific things if the power is off */
686 if (host->power_mode == MMC_POWER_OFF)
687 goto out;
688
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400689 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700690
Andy Shevchenko5934df22011-05-06 12:14:06 +0300691 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700692
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400693 omap_hsmmc_set_bus_mode(host);
694
Denis Karpov11dd62a2009-09-22 16:44:43 -0700695out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530696 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
697 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700698 return 0;
699}
700
701/*
702 * Save the MMC host context (store the number of power state changes so far).
703 */
Denis Karpov70a33412009-09-22 16:44:59 -0700704static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700705{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530706 host->con = OMAP_HSMMC_READ(host->base, CON);
707 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
708 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
709 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700710}
711
712#else
713
Denis Karpov70a33412009-09-22 16:44:59 -0700714static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700715{
716 return 0;
717}
718
Denis Karpov70a33412009-09-22 16:44:59 -0700719static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700720{
721}
722
723#endif
724
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725/*
726 * Send init stream sequence to card
727 * before sending IDLE command
728 */
Denis Karpov70a33412009-09-22 16:44:59 -0700729static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730{
731 int reg = 0;
732 unsigned long timeout;
733
Adrian Hunterb62f6222009-09-22 16:45:01 -0700734 if (host->protect_card)
735 return;
736
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100737 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700738
739 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740 OMAP_HSMMC_WRITE(host->base, CON,
741 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
742 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
743
744 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530745 while ((reg != CC_EN) && time_before(jiffies, timeout))
746 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747
748 OMAP_HSMMC_WRITE(host->base, CON,
749 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700750
751 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
752 OMAP_HSMMC_READ(host->base, STAT);
753
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100754 enable_irq(host->irq);
755}
756
757static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700758int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759{
760 int r = 1;
761
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100762 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100763 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764 return r;
765}
766
767static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700768omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769 char *buf)
770{
771 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700772 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773
Denis Karpov70a33412009-09-22 16:44:59 -0700774 return sprintf(buf, "%s\n",
775 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776}
777
Denis Karpov70a33412009-09-22 16:44:59 -0700778static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100779
780static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700781omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100782 char *buf)
783{
784 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700785 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786
Andreas Fenkart326119c2014-11-08 15:33:14 +0100787 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788}
789
Denis Karpov70a33412009-09-22 16:44:59 -0700790static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100791
792/*
793 * Configure the response type and send the cmd.
794 */
795static void
Denis Karpov70a33412009-09-22 16:44:59 -0700796omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100797 struct mmc_data *data)
798{
799 int cmdreg = 0, resptype = 0, cmdtype = 0;
800
Venkatraman S8986d312012-08-07 19:10:38 +0530801 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
803 host->cmd = cmd;
804
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700805 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100806
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200807 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808 if (cmd->flags & MMC_RSP_PRESENT) {
809 if (cmd->flags & MMC_RSP_136)
810 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200811 else if (cmd->flags & MMC_RSP_BUSY) {
812 resptype = 3;
813 host->response_busy = 1;
814 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815 resptype = 2;
816 }
817
818 /*
819 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
820 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
821 * a val of 0x3, rest 0x0.
822 */
823 if (cmd == host->mrq->stop)
824 cmdtype = 0x3;
825
826 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
827
Balaji T Ka2e77152014-01-21 19:54:42 +0530828 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
829 host->mrq->sbc) {
830 cmdreg |= ACEN_ACMD23;
831 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
832 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 if (data) {
834 cmdreg |= DP_SELECT | MSBS | BCE;
835 if (data->flags & MMC_DATA_READ)
836 cmdreg |= DDIR;
837 else
838 cmdreg &= ~(DDIR);
839 }
840
841 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530842 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843
Adrian Hunterb4175772010-05-26 14:42:06 -0700844 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700845
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100846 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
847 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
848}
849
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200850static int
Denis Karpov70a33412009-09-22 16:44:59 -0700851omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200852{
853 if (data->flags & MMC_DATA_WRITE)
854 return DMA_TO_DEVICE;
855 else
856 return DMA_FROM_DEVICE;
857}
858
Russell Kingc5c98922012-04-13 12:14:39 +0100859static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
860 struct mmc_data *data)
861{
862 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
863}
864
Adrian Hunterb4175772010-05-26 14:42:06 -0700865static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
866{
867 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530868 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700869
Venkatraman S31463b12012-04-09 12:08:34 +0530870 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700871 host->req_in_progress = 0;
872 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530873 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700874
875 omap_hsmmc_disable_irq(host);
876 /* Do not complete the request if DMA is still in progress */
877 if (mrq->data && host->use_dma && dma_ch != -1)
878 return;
879 host->mrq = NULL;
880 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100881 pm_runtime_mark_last_busy(host->dev);
882 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700883}
884
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100885/*
886 * Notify the transfer complete to MMC core
887 */
888static void
Denis Karpov70a33412009-09-22 16:44:59 -0700889omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100890{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200891 if (!data) {
892 struct mmc_request *mrq = host->mrq;
893
Adrian Hunter23050102009-09-22 16:44:57 -0700894 /* TC before CC from CMD6 - don't know why, but it happens */
895 if (host->cmd && host->cmd->opcode == 6 &&
896 host->response_busy) {
897 host->response_busy = 0;
898 return;
899 }
900
Adrian Hunterb4175772010-05-26 14:42:06 -0700901 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200902 return;
903 }
904
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100905 host->data = NULL;
906
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907 if (!data->error)
908 data->bytes_xfered += data->blocks * (data->blksz);
909 else
910 data->bytes_xfered = 0;
911
Balaji T Kbf129e12014-01-21 19:54:42 +0530912 if (data->stop && (data->error || !host->mrq->sbc))
913 omap_hsmmc_start_command(host, data->stop, NULL);
914 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700915 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100916}
917
918/*
919 * Notify the core about command completion
920 */
921static void
Denis Karpov70a33412009-09-22 16:44:59 -0700922omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100923{
Balaji T Kbf129e12014-01-21 19:54:42 +0530924 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530925 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530926 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530927 omap_hsmmc_start_dma_transfer(host);
928 omap_hsmmc_start_command(host, host->mrq->cmd,
929 host->mrq->data);
930 return;
931 }
932
Balaji T K2177fa92014-05-09 22:16:52 +0530933 host->cmd = NULL;
934
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935 if (cmd->flags & MMC_RSP_PRESENT) {
936 if (cmd->flags & MMC_RSP_136) {
937 /* response type 2 */
938 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
939 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
940 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
941 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
942 } else {
943 /* response types 1, 1b, 3, 4, 5, 6 */
944 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
945 }
946 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700947 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530948 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100949}
950
951/*
952 * DMA clean up for command errors
953 */
Denis Karpov70a33412009-09-22 16:44:59 -0700954static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955{
Adrian Hunterb4175772010-05-26 14:42:06 -0700956 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530957 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700958
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200959 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100960
Venkatraman S31463b12012-04-09 12:08:34 +0530961 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700962 dma_ch = host->dma_ch;
963 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530964 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700965
966 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100967 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
968
969 dmaengine_terminate_all(chan);
970 dma_unmap_sg(chan->device->dev,
971 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700972 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100973
Per Forlin053bf342011-11-07 21:55:11 +0530974 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100975 }
976 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977}
978
979/*
980 * Readable error output
981 */
982#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300983static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984{
985 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700986 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300987 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
988 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
989 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
990 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100991 };
992 char res[256];
993 char *buf = res;
994 int len, i;
995
996 len = sprintf(buf, "MMC IRQ 0x%x :", status);
997 buf += len;
998
Denis Karpov70a33412009-09-22 16:44:59 -0700999 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001000 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001001 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001002 buf += len;
1003 }
1004
Venkatraman S8986d312012-08-07 19:10:38 +05301005 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001006}
Adrian Hunter699b9582011-05-06 12:14:01 +03001007#else
1008static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1009 u32 status)
1010{
1011}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001012#endif /* CONFIG_MMC_DEBUG */
1013
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001014/*
1015 * MMC controller internal state machines reset
1016 *
1017 * Used to reset command or data internal state machines, using respectively
1018 * SRC or SRD bit of SYSCTL register
1019 * Can be called from interrupt context
1020 */
Denis Karpov70a33412009-09-22 16:44:59 -07001021static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1022 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001023{
1024 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301025 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001026
1027 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1028 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1029
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001030 /*
1031 * OMAP4 ES2 and greater has an updated reset logic.
1032 * Monitor a 0->1 transition first
1033 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001034 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001035 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001036 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301037 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001038 }
1039 i = 0;
1040
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001041 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1042 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301043 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001044
1045 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1046 dev_err(mmc_dev(host->mmc),
1047 "Timeout waiting on controller reset in %s\n",
1048 __func__);
1049}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001050
Balaji T K25e18972012-11-19 21:59:55 +05301051static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1052 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301053{
Balaji T K25e18972012-11-19 21:59:55 +05301054 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301055 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301056 if (host->cmd)
1057 host->cmd->error = err;
1058 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301059
1060 if (host->data) {
1061 omap_hsmmc_reset_controller_fsm(host, SRD);
1062 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301063 } else if (host->mrq && host->mrq->cmd)
1064 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301065}
1066
Adrian Hunterb4175772010-05-26 14:42:06 -07001067static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001068{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001069 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001070 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301071 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001072
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001073 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301074 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001075
Venkatraman Sa7e96872012-11-19 22:00:01 +05301076 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001077 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001078
Venkatraman Sa7e96872012-11-19 22:00:01 +05301079 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301080 end_cmd = 1;
Kishon Vijay Abraham I408806f2015-06-16 16:07:17 +05301081 if (host->data || host->response_busy) {
1082 end_trans = !end_cmd;
1083 host->response_busy = 0;
1084 }
Venkatraman Sa7e96872012-11-19 22:00:01 +05301085 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301086 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Vignesh R5027cd12015-06-16 16:07:18 +05301087 else if (status & (CCRC_EN | DCRC_EN | DEB_EN | CEB_EN |
1088 BADA_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301089 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1090
Balaji T Ka2e77152014-01-21 19:54:42 +05301091 if (status & ACE_EN) {
1092 u32 ac12;
1093 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1094 if (!(ac12 & ACNE) && host->mrq->sbc) {
1095 end_cmd = 1;
1096 if (ac12 & ACTO)
1097 error = -ETIMEDOUT;
1098 else if (ac12 & (ACCE | ACEB | ACIE))
1099 error = -EILSEQ;
1100 host->mrq->sbc->error = error;
1101 hsmmc_command_incomplete(host, error, end_cmd);
1102 }
1103 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1104 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001105 }
1106
Francesco Lavra7472bab2013-06-29 08:25:12 +02001107 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301108 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001109 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301110 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001111 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001112}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001113
Adrian Hunterb4175772010-05-26 14:42:06 -07001114/*
1115 * MMC controller IRQ handler
1116 */
1117static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1118{
1119 struct omap_hsmmc_host *host = dev_id;
1120 int status;
1121
1122 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001123 while (status & (INT_EN_MASK | CIRQ_EN)) {
1124 if (host->req_in_progress)
1125 omap_hsmmc_do_irq(host, status);
1126
1127 if (status & CIRQ_EN)
1128 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301129
Adrian Hunterb4175772010-05-26 14:42:06 -07001130 /* Flush posted write */
1131 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301132 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001133
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001134 return IRQ_HANDLED;
1135}
1136
Denis Karpov70a33412009-09-22 16:44:59 -07001137static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001138{
1139 unsigned long i;
1140
1141 OMAP_HSMMC_WRITE(host->base, HCTL,
1142 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1143 for (i = 0; i < loops_per_jiffy; i++) {
1144 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1145 break;
1146 cpu_relax();
1147 }
1148}
1149
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001150/*
David Brownelleb250822009-02-17 14:49:01 -08001151 * Switch MMC interface voltage ... only relevant for MMC1.
1152 *
1153 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1154 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1155 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001156 */
Denis Karpov70a33412009-09-22 16:44:59 -07001157static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158{
1159 u32 reg_val = 0;
1160 int ret;
1161
1162 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301163 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301164 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301165 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001166
1167 /* Turn the power off */
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001168 ret = omap_hsmmc_set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001169
1170 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001171 if (!ret)
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001172 ret = omap_hsmmc_set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301173 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301174 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301175 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001176
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001177 if (ret != 0)
1178 goto err;
1179
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001180 OMAP_HSMMC_WRITE(host->base, HCTL,
1181 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1182 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001183
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184 /*
1185 * If a MMC dual voltage card is detected, the set_ios fn calls
1186 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001187 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001188 *
David Brownelleb250822009-02-17 14:49:01 -08001189 * Cope with a bit of slop in the range ... per data sheets:
1190 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1191 * but recommended values are 1.71V to 1.89V
1192 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1193 * but recommended values are 2.7V to 3.3V
1194 *
1195 * Board setup code shouldn't permit anything very out-of-range.
1196 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1197 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001198 */
David Brownelleb250822009-02-17 14:49:01 -08001199 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001200 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001201 else
1202 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001203
1204 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001205 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001206
1207 return 0;
1208err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301209 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001210 return ret;
1211}
1212
Adrian Hunterb62f6222009-09-22 16:45:01 -07001213/* Protect the card while the cover is open */
1214static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1215{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001216 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001217 return;
1218
1219 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001220 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001221 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301222 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001223 "card is now accessible\n",
1224 mmc_hostname(host->mmc));
1225 host->protect_card = 0;
1226 }
1227 } else {
1228 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301229 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001230 "card is now inaccessible\n",
1231 mmc_hostname(host->mmc));
1232 host->protect_card = 1;
1233 }
1234 }
1235}
1236
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001237/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001238 * irq handler when (cell-phone) cover is mounted/removed
1239 */
1240static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1241{
1242 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001243
1244 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1245
Andreas Fenkart11227d12015-03-03 13:28:17 +01001246 omap_hsmmc_protect_card(host);
1247 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001248 return IRQ_HANDLED;
1249}
1250
Russell Kingc5c98922012-04-13 12:14:39 +01001251static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001252{
Russell Kingc5c98922012-04-13 12:14:39 +01001253 struct omap_hsmmc_host *host = param;
1254 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001255 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001256 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001257
Russell Kingc5c98922012-04-13 12:14:39 +01001258 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001259 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001260 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001261 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001262 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001263
Adrian Hunter770d7432011-05-06 12:14:11 +03001264 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001265 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001266 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001267 dma_unmap_sg(chan->device->dev,
1268 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001269 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001270
1271 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001273 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001274
1275 /* If DMA has finished after TC, complete the request */
1276 if (!req_in_progress) {
1277 struct mmc_request *mrq = host->mrq;
1278
1279 host->mrq = NULL;
1280 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001281 pm_runtime_mark_last_busy(host->dev);
1282 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001283 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001284}
1285
Per Forlin9782aff2011-07-01 18:55:23 +02001286static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1287 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001288 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001289 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001290{
1291 int dma_len;
1292
1293 if (!next && data->host_cookie &&
1294 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301295 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001296 " host->next_data.cookie %d\n",
1297 __func__, data->host_cookie, host->next_data.cookie);
1298 data->host_cookie = 0;
1299 }
1300
1301 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001302 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001303 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001304 omap_hsmmc_get_dma_dir(host, data));
1305
1306 } else {
1307 dma_len = host->next_data.dma_len;
1308 host->next_data.dma_len = 0;
1309 }
1310
1311
1312 if (dma_len == 0)
1313 return -EINVAL;
1314
1315 if (next) {
1316 next->dma_len = dma_len;
1317 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1318 } else
1319 host->dma_len = dma_len;
1320
1321 return 0;
1322}
1323
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001325 * Routine to configure and start DMA for the MMC card
1326 */
Balaji T K9d025332014-01-21 19:54:42 +05301327static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001328 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001329{
Russell King26b88522012-04-13 12:27:37 +01001330 struct dma_slave_config cfg;
1331 struct dma_async_tx_descriptor *tx;
1332 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001334 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001336 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001337 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001338 struct scatterlist *sgl;
1339
1340 sgl = data->sg + i;
1341 if (sgl->length % data->blksz)
1342 return -EINVAL;
1343 }
1344 if ((data->blksz % 4) != 0)
1345 /* REVISIT: The MMC buffer increments only when MSB is written.
1346 * Return error for blksz which is non multiple of four.
1347 */
1348 return -EINVAL;
1349
Adrian Hunterb4175772010-05-26 14:42:06 -07001350 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001351
Russell Kingc5c98922012-04-13 12:14:39 +01001352 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001353
Russell King26b88522012-04-13 12:27:37 +01001354 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1355 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1356 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1357 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1358 cfg.src_maxburst = data->blksz / 4;
1359 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001360
Russell King26b88522012-04-13 12:27:37 +01001361 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001362 if (ret)
1363 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001364
Russell King26b88522012-04-13 12:27:37 +01001365 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1366 if (ret)
1367 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001368
Russell King26b88522012-04-13 12:27:37 +01001369 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1370 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1371 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1372 if (!tx) {
1373 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1374 /* FIXME: cleanup */
1375 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001376 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001377
Russell King26b88522012-04-13 12:27:37 +01001378 tx->callback = omap_hsmmc_dma_callback;
1379 tx->callback_param = host;
1380
1381 /* Does not fail */
1382 dmaengine_submit(tx);
1383
1384 host->dma_ch = 1;
1385
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001386 return 0;
1387}
1388
Denis Karpov70a33412009-09-22 16:44:59 -07001389static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001390 unsigned int timeout_ns,
1391 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001392{
1393 unsigned int timeout, cycle_ns;
1394 uint32_t reg, clkd, dto = 0;
1395
1396 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1397 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1398 if (clkd == 0)
1399 clkd = 1;
1400
Balaji T K6e3076c2014-01-21 19:54:42 +05301401 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001402 timeout = timeout_ns / cycle_ns;
1403 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404 if (timeout) {
1405 while ((timeout & 0x80000000) == 0) {
1406 dto += 1;
1407 timeout <<= 1;
1408 }
1409 dto = 31 - dto;
1410 timeout <<= 1;
1411 if (timeout && dto)
1412 dto += 1;
1413 if (dto >= 13)
1414 dto -= 13;
1415 else
1416 dto = 0;
1417 if (dto > 14)
1418 dto = 14;
1419 }
1420
1421 reg &= ~DTO_MASK;
1422 reg |= dto << DTO_SHIFT;
1423 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1424}
1425
Balaji T K9d025332014-01-21 19:54:42 +05301426static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1427{
1428 struct mmc_request *req = host->mrq;
1429 struct dma_chan *chan;
1430
1431 if (!req->data)
1432 return;
1433 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1434 | (req->data->blocks << 16));
1435 set_data_timeout(host, req->data->timeout_ns,
1436 req->data->timeout_clks);
1437 chan = omap_hsmmc_get_dma_chan(host, req->data);
1438 dma_async_issue_pending(chan);
1439}
1440
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441/*
1442 * Configure block length for MMC/SD cards and initiate the transfer.
1443 */
1444static int
Denis Karpov70a33412009-09-22 16:44:59 -07001445omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001446{
1447 int ret;
1448 host->data = req->data;
1449
1450 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001452 /*
1453 * Set an arbitrary 100ms data timeout for commands with
1454 * busy signal.
1455 */
1456 if (req->cmd->flags & MMC_RSP_BUSY)
1457 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001458 return 0;
1459 }
1460
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301462 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301464 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001465 return ret;
1466 }
1467 }
1468 return 0;
1469}
1470
Per Forlin9782aff2011-07-01 18:55:23 +02001471static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1472 int err)
1473{
1474 struct omap_hsmmc_host *host = mmc_priv(mmc);
1475 struct mmc_data *data = mrq->data;
1476
Russell King26b88522012-04-13 12:27:37 +01001477 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001478 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001479
Russell King26b88522012-04-13 12:27:37 +01001480 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1481 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001482 data->host_cookie = 0;
1483 }
1484}
1485
1486static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1487 bool is_first_req)
1488{
1489 struct omap_hsmmc_host *host = mmc_priv(mmc);
1490
1491 if (mrq->data->host_cookie) {
1492 mrq->data->host_cookie = 0;
1493 return ;
1494 }
1495
Russell Kingc5c98922012-04-13 12:14:39 +01001496 if (host->use_dma) {
1497 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001498
Per Forlin9782aff2011-07-01 18:55:23 +02001499 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001500 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001501 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001502 }
Per Forlin9782aff2011-07-01 18:55:23 +02001503}
1504
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505/*
1506 * Request function. for read/write operation
1507 */
Denis Karpov70a33412009-09-22 16:44:59 -07001508static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001509{
Denis Karpov70a33412009-09-22 16:44:59 -07001510 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001511 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001512
Adrian Hunterb4175772010-05-26 14:42:06 -07001513 BUG_ON(host->req_in_progress);
1514 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001515 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001516 if (host->protect_card) {
1517 if (host->reqs_blocked < 3) {
1518 /*
1519 * Ensure the controller is left in a consistent
1520 * state by resetting the command and data state
1521 * machines.
1522 */
1523 omap_hsmmc_reset_controller_fsm(host, SRD);
1524 omap_hsmmc_reset_controller_fsm(host, SRC);
1525 host->reqs_blocked += 1;
1526 }
1527 req->cmd->error = -EBADF;
1528 if (req->data)
1529 req->data->error = -EBADF;
1530 req->cmd->retries = 0;
1531 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001532 pm_runtime_mark_last_busy(host->dev);
1533 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001534 return;
1535 } else if (host->reqs_blocked)
1536 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001537 WARN_ON(host->mrq != NULL);
1538 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301539 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001540 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001541 if (err) {
1542 req->cmd->error = err;
1543 if (req->data)
1544 req->data->error = err;
1545 host->mrq = NULL;
1546 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001547 pm_runtime_mark_last_busy(host->dev);
1548 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001549 return;
1550 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301551 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301552 omap_hsmmc_start_command(host, req->sbc, NULL);
1553 return;
1554 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001555
Balaji T K9d025332014-01-21 19:54:42 +05301556 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001557 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001558}
1559
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001560/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001561static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562{
Denis Karpov70a33412009-09-22 16:44:59 -07001563 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001564 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001565
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301566 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001567
Adrian Huntera3621462009-09-22 16:44:42 -07001568 if (ios->power_mode != host->power_mode) {
1569 switch (ios->power_mode) {
1570 case MMC_POWER_OFF:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001571 omap_hsmmc_set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001572 break;
1573 case MMC_POWER_UP:
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02001574 omap_hsmmc_set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001575 break;
1576 case MMC_POWER_ON:
1577 do_send_init_stream = 1;
1578 break;
1579 }
1580 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001581 }
1582
Denis Karpovdd498ef2009-09-22 16:44:49 -07001583 /* FIXME: set registers based only on changes to ios */
1584
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001585 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001586
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301587 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001588 /* Only MMC1 can interface at 3V without some flavor
1589 * of external transceiver; but they all handle 1.8V.
1590 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001591 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301592 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593 /*
1594 * The mmc_select_voltage fn of the core does
1595 * not seem to set the power_mode to
1596 * MMC_POWER_UP upon recalculating the voltage.
1597 * vdd 1.8v.
1598 */
Denis Karpov70a33412009-09-22 16:44:59 -07001599 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1600 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001601 "Switch operation failed\n");
1602 }
1603 }
1604
Andy Shevchenko5934df22011-05-06 12:14:06 +03001605 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001606
Adrian Huntera3621462009-09-22 16:44:42 -07001607 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001608 send_init_stream(host);
1609
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001610 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001611
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301612 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001613}
1614
1615static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1616{
Denis Karpov70a33412009-09-22 16:44:59 -07001617 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001618
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001619 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001620 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001621 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001622}
1623
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001624static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1625{
1626 struct omap_hsmmc_host *host = mmc_priv(mmc);
1627
Andreas Fenkart326119c2014-11-08 15:33:14 +01001628 if (mmc_pdata(host)->init_card)
1629 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001630}
1631
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001632static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1633{
1634 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001635 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001636 unsigned long flags;
1637
1638 spin_lock_irqsave(&host->irq_lock, flags);
1639
Balaji T K5a52b082014-05-29 10:28:02 +02001640 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001641 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1642 if (enable) {
1643 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1644 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001645 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001646 } else {
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 }
Balaji T K5a52b082014-05-29 10:28:02 +02001651 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001652 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1653
1654 /*
1655 * if enable, piggy back detection on current request
1656 * but always disable immediately
1657 */
1658 if (!host->req_in_progress || !enable)
1659 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1660
1661 /* flush posted write */
1662 OMAP_HSMMC_READ(host->base, IE);
1663
1664 spin_unlock_irqrestore(&host->irq_lock, flags);
1665}
1666
1667static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1668{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001669 int ret;
1670
1671 /*
1672 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1673 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1674 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1675 * with functional clock disabled.
1676 */
1677 if (!host->dev->of_node || !host->wake_irq)
1678 return -ENODEV;
1679
Tony Lindgren5b83b222015-05-21 15:51:52 -07001680 ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001681 if (ret) {
1682 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1683 goto err;
1684 }
1685
1686 /*
1687 * Some omaps don't have wake-up path from deeper idle states
1688 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1689 */
1690 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001691 struct pinctrl *p = devm_pinctrl_get(host->dev);
1692 if (!p) {
1693 ret = -ENODEV;
1694 goto err_free_irq;
1695 }
1696 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1697 dev_info(host->dev, "missing default pinctrl state\n");
1698 devm_pinctrl_put(p);
1699 ret = -EINVAL;
1700 goto err_free_irq;
1701 }
1702
1703 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1704 dev_info(host->dev, "missing idle pinctrl state\n");
1705 devm_pinctrl_put(p);
1706 ret = -EINVAL;
1707 goto err_free_irq;
1708 }
1709 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001710 }
1711
Balaji T K5a52b082014-05-29 10:28:02 +02001712 OMAP_HSMMC_WRITE(host->base, HCTL,
1713 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001714 return 0;
1715
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001716err_free_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07001717 dev_pm_clear_wake_irq(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001718err:
1719 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1720 host->wake_irq = 0;
1721 return ret;
1722}
1723
Denis Karpov70a33412009-09-22 16:44:59 -07001724static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001725{
1726 u32 hctl, capa, value;
1727
1728 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301729 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001730 hctl = SDVS30;
1731 capa = VS30 | VS18;
1732 } else {
1733 hctl = SDVS18;
1734 capa = VS18;
1735 }
1736
1737 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1738 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1739
1740 value = OMAP_HSMMC_READ(host->base, CAPA);
1741 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1742
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001743 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001744 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001745}
1746
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001747static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1748 unsigned int direction, int blk_size)
1749{
1750 /* This controller can't do multiblock reads due to hw bugs */
1751 if (direction == MMC_DATA_READ)
1752 return 1;
1753
1754 return blk_size;
1755}
1756
1757static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001758 .post_req = omap_hsmmc_post_req,
1759 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001760 .request = omap_hsmmc_request,
1761 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001762 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001763 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001764 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001765 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001766};
1767
Denis Karpovd900f712009-09-22 16:44:38 -07001768#ifdef CONFIG_DEBUG_FS
1769
Denis Karpov70a33412009-09-22 16:44:59 -07001770static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001771{
1772 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001773 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001774
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001775 seq_printf(s, "mmc%d:\n", mmc->index);
1776 seq_printf(s, "sdio irq mode\t%s\n",
1777 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1778
1779 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1780 seq_printf(s, "sdio irq \t%s\n",
1781 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1782 : "disabled");
1783 }
1784 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001785
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301786 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001787 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001788 seq_printf(s, "CON:\t\t0x%08x\n",
1789 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001790 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1791 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001792 seq_printf(s, "HCTL:\t\t0x%08x\n",
1793 OMAP_HSMMC_READ(host->base, HCTL));
1794 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1795 OMAP_HSMMC_READ(host->base, SYSCTL));
1796 seq_printf(s, "IE:\t\t0x%08x\n",
1797 OMAP_HSMMC_READ(host->base, IE));
1798 seq_printf(s, "ISE:\t\t0x%08x\n",
1799 OMAP_HSMMC_READ(host->base, ISE));
1800 seq_printf(s, "CAPA:\t\t0x%08x\n",
1801 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001802
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301803 pm_runtime_mark_last_busy(host->dev);
1804 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001805
Denis Karpovd900f712009-09-22 16:44:38 -07001806 return 0;
1807}
1808
Denis Karpov70a33412009-09-22 16:44:59 -07001809static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001810{
Denis Karpov70a33412009-09-22 16:44:59 -07001811 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001812}
1813
1814static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001815 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001816 .read = seq_read,
1817 .llseek = seq_lseek,
1818 .release = single_release,
1819};
1820
Denis Karpov70a33412009-09-22 16:44:59 -07001821static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001822{
1823 if (mmc->debugfs_root)
1824 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1825 mmc, &mmc_regs_fops);
1826}
1827
1828#else
1829
Denis Karpov70a33412009-09-22 16:44:59 -07001830static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001831{
1832}
1833
1834#endif
1835
Rajendra Nayak46856a62012-03-12 20:32:37 +05301836#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001837static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1838 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1839 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1840};
1841
1842static const struct omap_mmc_of_data omap4_mmc_of_data = {
1843 .reg_offset = 0x100,
1844};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001845static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1846 .reg_offset = 0x100,
1847 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1848};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301849
1850static const struct of_device_id omap_mmc_of_match[] = {
1851 {
1852 .compatible = "ti,omap2-hsmmc",
1853 },
1854 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001855 .compatible = "ti,omap3-pre-es3-hsmmc",
1856 .data = &omap3_pre_es3_mmc_of_data,
1857 },
1858 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301859 .compatible = "ti,omap3-hsmmc",
1860 },
1861 {
1862 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001863 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301864 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001865 {
1866 .compatible = "ti,am33xx-hsmmc",
1867 .data = &am33xx_mmc_of_data,
1868 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301869 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001870};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301871MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1872
Andreas Fenkart551434382014-11-08 15:33:09 +01001873static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301874{
Andreas Fenkart551434382014-11-08 15:33:09 +01001875 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301876 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301877
1878 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1879 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301880 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301881
1882 if (of_find_property(np, "ti,dual-volt", NULL))
1883 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1884
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001885 pdata->gpio_cd = -EINVAL;
1886 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001887 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301888
1889 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001890 pdata->nonremovable = true;
1891 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301892 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301893
1894 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001895 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301896
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301897 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001898 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301899
Rajendra Nayak46856a62012-03-12 20:32:37 +05301900 return pdata;
1901}
1902#else
Andreas Fenkart551434382014-11-08 15:33:09 +01001903static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301904 *of_get_hsmmc_pdata(struct device *dev)
1905{
Balaji T K19df45b2014-02-28 19:08:18 +05301906 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301907}
1908#endif
1909
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001910static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001911{
Andreas Fenkart551434382014-11-08 15:33:09 +01001912 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001913 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001914 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001915 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001916 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301917 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001918 dma_cap_mask_t mask;
1919 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06001920 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301921 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301922
1923 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1924 if (match) {
1925 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001926
1927 if (IS_ERR(pdata))
1928 return PTR_ERR(pdata);
1929
Rajendra Nayak46856a62012-03-12 20:32:37 +05301930 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001931 data = match->data;
1932 pdata->reg_offset = data->reg_offset;
1933 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301934 }
1935 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001936
1937 if (pdata == NULL) {
1938 dev_err(&pdev->dev, "Platform Data is missing\n");
1939 return -ENXIO;
1940 }
1941
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001942 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1943 irq = platform_get_irq(pdev, 0);
1944 if (res == NULL || irq < 0)
1945 return -ENXIO;
1946
Balaji T K77fae212014-05-09 22:16:51 +05301947 base = devm_ioremap_resource(&pdev->dev, res);
1948 if (IS_ERR(base))
1949 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001950
Denis Karpov70a33412009-09-22 16:44:59 -07001951 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001952 if (!mmc) {
1953 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001954 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001955 }
1956
NeilBrownfdb9de12015-01-13 08:23:18 +13001957 ret = mmc_of_parse(mmc);
1958 if (ret)
1959 goto err1;
1960
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 host = mmc_priv(mmc);
1962 host->mmc = mmc;
1963 host->pdata = pdata;
1964 host->dev = &pdev->dev;
1965 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001966 host->dma_ch = -1;
1967 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05301968 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05301969 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08001970 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001971 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05301972 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001973
NeilBrown41afa3142015-01-13 08:23:18 +13001974 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001975 if (ret)
1976 goto err_gpio;
1977
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001979
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001980 if (pdev->dev.of_node)
1981 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
1982
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301983 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001984
Daniel Mackd418ed82012-02-19 13:20:33 +01001985 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1986
1987 if (pdata->max_freq > 0)
1988 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13001989 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01001990 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001991
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001992 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001993
Balaji T K96181952014-05-09 22:16:48 +05301994 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995 if (IS_ERR(host->fclk)) {
1996 ret = PTR_ERR(host->fclk);
1997 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001998 goto err1;
1999 }
2000
Paul Walmsley9b682562011-10-06 14:50:35 -06002001 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2002 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 -07002003 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002004 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002005
Tony Lindgren5b83b222015-05-21 15:51:52 -07002006 device_init_wakeup(&pdev->dev, true);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302007 pm_runtime_enable(host->dev);
2008 pm_runtime_get_sync(host->dev);
2009 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2010 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011
Balaji T K92a3aeb2012-02-24 21:14:34 +05302012 omap_hsmmc_context_save(host);
2013
Balaji T K96181952014-05-09 22:16:48 +05302014 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302015 /*
2016 * MMC can still work without debounce clock.
2017 */
2018 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302019 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302020 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302021 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302022 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002023 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002025 /* Since we do only SG emulation, we can have as many segs
2026 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002027 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002028
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002029 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2030 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2031 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2032 mmc->max_seg_size = mmc->max_req_size;
2033
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002034 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002035 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002036
Andreas Fenkart326119c2014-11-08 15:33:14 +01002037 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002038 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002039 mmc->caps |= MMC_CAP_4_BIT_DATA;
2040
Andreas Fenkart326119c2014-11-08 15:33:14 +01002041 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002042 mmc->caps |= MMC_CAP_NONREMOVABLE;
2043
NeilBrownfdb9de12015-01-13 08:23:18 +13002044 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002045
Denis Karpov70a33412009-09-22 16:44:59 -07002046 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302048 if (!pdev->dev.of_node) {
2049 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2050 if (!res) {
2051 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2052 ret = -ENXIO;
2053 goto err_irq;
2054 }
2055 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002056
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302057 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2058 if (!res) {
2059 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2060 ret = -ENXIO;
2061 goto err_irq;
2062 }
2063 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002064 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002065
Russell King26b88522012-04-13 12:27:37 +01002066 dma_cap_zero(mask);
2067 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002068
Matt Porterd272fbf2013-05-10 17:42:34 +05302069 host->rx_chan =
2070 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2071 &rx_req, &pdev->dev, "rx");
2072
Russell King26b88522012-04-13 12:27:37 +01002073 if (!host->rx_chan) {
2074 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002075 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002076 goto err_irq;
2077 }
2078
Matt Porterd272fbf2013-05-10 17:42:34 +05302079 host->tx_chan =
2080 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2081 &tx_req, &pdev->dev, "tx");
2082
Russell King26b88522012-04-13 12:27:37 +01002083 if (!host->tx_chan) {
2084 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002085 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002086 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002087 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002089 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302090 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091 mmc_hostname(mmc), host);
2092 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302093 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002094 goto err_irq;
2095 }
2096
Andreas Fenkartf7f0f032015-07-07 20:38:43 +02002097 if (omap_hsmmc_have_reg()) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002098 ret = omap_hsmmc_reg_get(host);
2099 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002100 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002101 }
2102
Andreas Fenkart326119c2014-11-08 15:33:14 +01002103 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Adrian Hunterb4175772010-05-26 14:42:06 -07002105 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002106
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002107 /*
2108 * For now, only support SDIO interrupt if we have a separate
2109 * wake-up interrupt configured from device tree. This is because
2110 * the wake-up interrupt is needed for idle state and some
2111 * platforms need special quirks. And we don't want to add new
2112 * legacy mux platform init code callbacks any longer as we
2113 * are moving to DT based booting anyways.
2114 */
2115 ret = omap_hsmmc_configure_wake_irq(host);
2116 if (!ret)
2117 mmc->caps |= MMC_CAP_SDIO_IRQ;
2118
Adrian Hunterb62f6222009-09-22 16:45:01 -07002119 omap_hsmmc_protect_card(host);
2120
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121 mmc_add_host(mmc);
2122
Andreas Fenkart326119c2014-11-08 15:33:14 +01002123 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002124 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2125 if (ret < 0)
2126 goto err_slot_name;
2127 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002128 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002129 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002130 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002131 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002132 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002133 }
2134
Denis Karpov70a33412009-09-22 16:44:59 -07002135 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302136 pm_runtime_mark_last_busy(host->dev);
2137 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002138
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002139 return 0;
2140
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141err_slot_name:
2142 mmc_remove_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143err_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07002144 device_init_wakeup(&pdev->dev, false);
Russell Kingc5c98922012-04-13 12:14:39 +01002145 if (host->tx_chan)
2146 dma_release_channel(host->tx_chan);
2147 if (host->rx_chan)
2148 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302149 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002150 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302151 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302152 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002154err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002155 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002157 return ret;
2158}
2159
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002160static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002161{
Denis Karpov70a33412009-09-22 16:44:59 -07002162 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002163
Felipe Balbi927ce942012-03-14 11:18:27 +02002164 pm_runtime_get_sync(host->dev);
2165 mmc_remove_host(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002166
Russell Kingc5c98922012-04-13 12:14:39 +01002167 if (host->tx_chan)
2168 dma_release_channel(host->tx_chan);
2169 if (host->rx_chan)
2170 dma_release_channel(host->rx_chan);
2171
Felipe Balbi927ce942012-03-14 11:18:27 +02002172 pm_runtime_put_sync(host->dev);
2173 pm_runtime_disable(host->dev);
Tony Lindgren5b83b222015-05-21 15:51:52 -07002174 device_init_wakeup(&pdev->dev, false);
Balaji T K96181952014-05-09 22:16:48 +05302175 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302176 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002177
Balaji T K9d1f0282012-10-15 21:35:07 +05302178 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002179
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180 return 0;
2181}
2182
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002183#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002184static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002185{
Felipe Balbi927ce942012-03-14 11:18:27 +02002186 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2187
2188 if (!host)
2189 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002190
Felipe Balbi927ce942012-03-14 11:18:27 +02002191 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002192
2193 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002194 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2195 OMAP_HSMMC_WRITE(host->base, IE, 0);
2196 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002197 OMAP_HSMMC_WRITE(host->base, HCTL,
2198 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2199 }
2200
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302201 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302202 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002203
Eliad Peller31f9d462011-11-22 16:02:17 +02002204 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002205 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206}
2207
2208/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002209static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002210{
Felipe Balbi927ce942012-03-14 11:18:27 +02002211 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2212
2213 if (!host)
2214 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002215
Felipe Balbi927ce942012-03-14 11:18:27 +02002216 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002217
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302218 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302219 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002220
Felipe Balbi927ce942012-03-14 11:18:27 +02002221 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2222 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002223
Felipe Balbi927ce942012-03-14 11:18:27 +02002224 omap_hsmmc_protect_card(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002225 pm_runtime_mark_last_busy(host->dev);
2226 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002227 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002228}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229#endif
2230
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302231static int omap_hsmmc_runtime_suspend(struct device *dev)
2232{
2233 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002234 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002235 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302236
2237 host = platform_get_drvdata(to_platform_device(dev));
2238 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002239 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302240
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002241 spin_lock_irqsave(&host->irq_lock, flags);
2242 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2243 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2244 /* disable sdio irq handling to prevent race */
2245 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2246 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002247
2248 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2249 /*
2250 * dat1 line low, pending sdio irq
2251 * race condition: possible irq handler running on
2252 * multi-core, abort
2253 */
2254 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2255 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2256 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2257 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2258 pm_runtime_mark_last_busy(dev);
2259 ret = -EBUSY;
2260 goto abort;
2261 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002262
Andreas Fenkart97978a42014-05-29 10:28:04 +02002263 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002264 } else {
2265 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002266 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002267
Andreas Fenkartf9459012014-05-29 10:28:03 +02002268abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002269 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002270 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302271}
2272
2273static int omap_hsmmc_runtime_resume(struct device *dev)
2274{
2275 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002276 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302277
2278 host = platform_get_drvdata(to_platform_device(dev));
2279 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002280 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302281
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002282 spin_lock_irqsave(&host->irq_lock, flags);
2283 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2284 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002285
Andreas Fenkart97978a42014-05-29 10:28:04 +02002286 pinctrl_pm_select_default_state(host->dev);
2287
2288 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002289 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2290 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2291 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002292 } else {
2293 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002294 }
2295 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302296 return 0;
2297}
2298
Kevin Hilmana791daa2010-05-26 14:42:07 -07002299static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002300 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302301 .runtime_suspend = omap_hsmmc_runtime_suspend,
2302 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002303};
2304
2305static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002306 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002307 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002308 .driver = {
2309 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002310 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302311 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002312 },
2313};
2314
Felipe Balbib7964502012-03-14 11:18:32 +02002315module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002316MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2317MODULE_LICENSE("GPL");
2318MODULE_ALIAS("platform:" DRIVER_NAME);
2319MODULE_AUTHOR("Texas Instruments Inc");