blob: ea70285171249da81ca06d30c6dd2d6e59e036c1 [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 Fenkart2cd3a2a52014-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 Fenkart2cd3a2a52014-05-29 10:28:00 +020041#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053044#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053045#include <linux/pm_runtime.h>
Andreas Fenkart55143432014-11-08 15:33:09 +010046#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010047
48/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070049#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010050#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053051#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010052#define OMAP_HSMMC_BLK 0x0104
53#define OMAP_HSMMC_ARG 0x0108
54#define OMAP_HSMMC_CMD 0x010C
55#define OMAP_HSMMC_RSP10 0x0110
56#define OMAP_HSMMC_RSP32 0x0114
57#define OMAP_HSMMC_RSP54 0x0118
58#define OMAP_HSMMC_RSP76 0x011C
59#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020060#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010061#define OMAP_HSMMC_HCTL 0x0128
62#define OMAP_HSMMC_SYSCTL 0x012C
63#define OMAP_HSMMC_STAT 0x0130
64#define OMAP_HSMMC_IE 0x0134
65#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053066#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define OMAP_HSMMC_CAPA 0x0140
68
69#define VS18 (1 << 26)
70#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053071#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010072#define SDVS18 (0x5 << 9)
73#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080074#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010075#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010076#define SDVSCLR 0xFFFFF1FF
77#define SDVSDET 0x00000400
78#define AUTOIDLE 0x1
79#define SDBP (1 << 8)
80#define DTO 0xe
81#define ICE 0x1
82#define ICS 0x2
83#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053084#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010085#define CLKD_MASK 0x0000FFC0
86#define CLKD_SHIFT 6
87#define DTO_MASK 0x000F0000
88#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053090#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define DP_SELECT (1 << 21)
92#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053093#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010094#define MSBS (1 << 5)
95#define BCE (1 << 1)
96#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053097#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020098#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +053099#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200100#define CLKEXTFREE (1 << 16)
101#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200102#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define STAT_CLEAR 0xFFFFFFFF
105#define INIT_STREAM_CMD 0x00000000
106#define DUAL_VOLT_OCR_BIT 7
107#define SRC (1 << 25)
108#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700109#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100110
Andreas Fenkartf9459012014-05-29 10:28:03 +0200111/* PSTATE */
112#define DLEV_DAT(x) (1 << (20 + (x)))
113
Venkatraman Sa7e96872012-11-19 22:00:01 +0530114/* Interrupt masks for IE and ISE register */
115#define CC_EN (1 << 0)
116#define TC_EN (1 << 1)
117#define BWR_EN (1 << 4)
118#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200119#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530120#define ERR_EN (1 << 15)
121#define CTO_EN (1 << 16)
122#define CCRC_EN (1 << 17)
123#define CEB_EN (1 << 18)
124#define CIE_EN (1 << 19)
125#define DTO_EN (1 << 20)
126#define DCRC_EN (1 << 21)
127#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530128#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530129#define CERR_EN (1 << 28)
130#define BADA_EN (1 << 29)
131
Balaji T Ka2e77152014-01-21 19:54:42 +0530132#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530133 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
134 BRR_EN | BWR_EN | TC_EN | CC_EN)
135
Balaji T Ka2e77152014-01-21 19:54:42 +0530136#define CNI (1 << 7)
137#define ACIE (1 << 4)
138#define ACEB (1 << 3)
139#define ACCE (1 << 2)
140#define ACTO (1 << 1)
141#define ACNE (1 << 0)
142
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530143#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530144#define MMC_TIMEOUT_MS 20 /* 20 mSec */
145#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400146#define OMAP_MMC_MIN_CLOCK 400000
147#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530148#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149
Balaji T Ke99448f2014-02-19 20:26:40 +0530150#define VDD_1V8 1800000 /* 180000 uV */
151#define VDD_3V0 3000000 /* 300000 uV */
152#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
153
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154/*
155 * One controller can have multiple slots, like on some omap boards using
156 * omap.c controller driver. Luckily this is not currently done on any known
157 * omap_hsmmc.c device.
158 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100159#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160
161/*
162 * MMC Host controller read/write API's
163 */
164#define OMAP_HSMMC_READ(base, reg) \
165 __raw_readl((base) + OMAP_HSMMC_##reg)
166
167#define OMAP_HSMMC_WRITE(base, reg, val) \
168 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
169
Per Forlin9782aff2011-07-01 18:55:23 +0200170struct omap_hsmmc_next {
171 unsigned int dma_len;
172 s32 cookie;
173};
174
Denis Karpov70a33412009-09-22 16:44:59 -0700175struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 struct device *dev;
177 struct mmc_host *mmc;
178 struct mmc_request *mrq;
179 struct mmc_command *cmd;
180 struct mmc_data *data;
181 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100182 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183 /*
184 * vcc == configured supply
185 * vcc_aux == optional
186 * - MMC1, supply for DAT4..DAT7
187 * - MMC2/MMC2, external level shifter voltage supply, for
188 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
189 */
190 struct regulator *vcc;
191 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530192 struct regulator *pbias;
193 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 void __iomem *base;
195 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700196 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200198 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100199 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700200 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100201 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530202 u32 con;
203 u32 hctl;
204 u32 sysctl;
205 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100206 int irq;
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200207 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100208 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100209 struct dma_chan *tx_chan;
210 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200211 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700212 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700213 int protect_card;
214 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800215 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700216 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530217 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530218 unsigned int flags;
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200219#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
220#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
221#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200222 struct omap_hsmmc_next next_data;
Andreas Fenkart55143432014-11-08 15:33:09 +0100223 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100224
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100225 /* return MMC cover switch state, can be NULL if not supported.
226 *
227 * possible return values:
228 * 0 - closed
229 * 1 - open
230 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100231 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100232
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100233 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100234};
235
Nishanth Menon59445b12014-02-13 23:45:48 -0600236struct omap_mmc_of_data {
237 u32 reg_offset;
238 u8 controller_flags;
239};
240
Balaji T Kbf129e12014-01-21 19:54:42 +0530241static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
242
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100243static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800244{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530245 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246
NeilBrown41afa3142015-01-13 08:23:18 +1300247 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800248}
249
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100250static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800251{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530252 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800253
NeilBrown41afa3142015-01-13 08:23:18 +1300254 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800255}
256
Adrian Hunterb702b102010-02-15 10:03:35 -0800257#ifdef CONFIG_REGULATOR
258
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100259static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800260{
261 struct omap_hsmmc_host *host =
262 platform_get_drvdata(to_platform_device(dev));
263 int ret = 0;
264
265 /*
266 * If we don't see a Vcc regulator, assume it's a fixed
267 * voltage always-on regulator.
268 */
269 if (!host->vcc)
270 return 0;
271
Andreas Fenkart326119c2014-11-08 15:33:14 +0100272 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100273 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800274
Balaji T Ke99448f2014-02-19 20:26:40 +0530275 if (host->pbias) {
276 if (host->pbias_enabled == 1) {
277 ret = regulator_disable(host->pbias);
278 if (!ret)
279 host->pbias_enabled = 0;
280 }
281 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
282 }
283
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800284 /*
285 * Assume Vcc regulator is used only to power the card ... OMAP
286 * VDDS is used to power the pins, optionally with a transceiver to
287 * support cards using voltages other than VDDS (1.8V nominal). When a
288 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
289 *
290 * In some cases this regulator won't support enable/disable;
291 * e.g. it's a fixed rail for a WLAN chip.
292 *
293 * In other cases vcc_aux switches interface power. Example, for
294 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
295 * chips/cards need an interface voltage rail too.
296 */
297 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530298 if (host->vcc)
299 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300 /* Enable interface voltage rail, if needed */
301 if (ret == 0 && host->vcc_aux) {
302 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530303 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400304 ret = mmc_regulator_set_ocr(host->mmc,
305 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800306 }
307 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400308 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800309 if (host->vcc_aux)
310 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530311 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400312 /* Then proceed to shut down the local regulator */
313 ret = mmc_regulator_set_ocr(host->mmc,
314 host->vcc, 0);
315 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800316 }
317
Balaji T Ke99448f2014-02-19 20:26:40 +0530318 if (host->pbias) {
319 if (vdd <= VDD_165_195)
320 ret = regulator_set_voltage(host->pbias, VDD_1V8,
321 VDD_1V8);
322 else
323 ret = regulator_set_voltage(host->pbias, VDD_3V0,
324 VDD_3V0);
325 if (ret < 0)
326 goto error_set_power;
327
328 if (host->pbias_enabled == 0) {
329 ret = regulator_enable(host->pbias);
330 if (!ret)
331 host->pbias_enabled = 1;
332 }
333 }
334
Andreas Fenkart326119c2014-11-08 15:33:14 +0100335 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100336 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800337
Balaji T Ke99448f2014-02-19 20:26:40 +0530338error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800339 return ret;
340}
341
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
343{
344 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700345 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800346
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530347 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800348 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530349 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
350 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400351 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800352 } else {
353 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700354 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100355 if (!mmc_pdata(host)->ocr_mask) {
356 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700357 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100358 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530359 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100360 mmc_pdata(host)->ocr_mask);
361 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700362 return -EINVAL;
363 }
364 }
Balaji T K987fd492014-02-19 20:26:40 +0530365 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100366 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800367
Balaji T K987fd492014-02-19 20:26:40 +0530368 /* Allow an aux regulator */
369 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
370 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800371
Balaji T Ke99448f2014-02-19 20:26:40 +0530372 reg = devm_regulator_get_optional(host->dev, "pbias");
373 host->pbias = IS_ERR(reg) ? NULL : reg;
374
Balaji T K987fd492014-02-19 20:26:40 +0530375 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100376 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530377 return 0;
378 /*
379 * To disable boot_on regulator, enable regulator
380 * to increase usecount and then disable it.
381 */
382 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
383 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100384 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300385
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100386 mmc_pdata(host)->set_power(host->dev, 1, vdd);
387 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800388 }
389
390 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800391}
392
393static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
394{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100395 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800396}
397
Adrian Hunterb702b102010-02-15 10:03:35 -0800398static inline int omap_hsmmc_have_reg(void)
399{
400 return 1;
401}
402
403#else
404
405static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
406{
407 return -EINVAL;
408}
409
410static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
411{
412}
413
414static inline int omap_hsmmc_have_reg(void)
415{
416 return 0;
417}
418
419#endif
420
NeilBrown41afa3142015-01-13 08:23:18 +1300421static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100422static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300423
424static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
425 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100426 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800427{
428 int ret;
429
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100430 if (gpio_is_valid(pdata->gpio_cod)) {
431 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800432 if (ret)
433 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100434
435 host->get_cover_state = omap_hsmmc_get_cover_state;
436 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100437 } else if (gpio_is_valid(pdata->gpio_cd)) {
438 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100439 if (ret)
440 return ret;
441
442 host->card_detect = omap_hsmmc_card_detect;
443 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_detect);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100444 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800445
Andreas Fenkart326119c2014-11-08 15:33:14 +0100446 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300447 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800448 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300449 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100450 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800451
452 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800453}
454
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100455/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300456 * Start clock to the card
457 */
458static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
459{
460 OMAP_HSMMC_WRITE(host->base, SYSCTL,
461 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
462}
463
464/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100465 * Stop clock to the card
466 */
Denis Karpov70a33412009-09-22 16:44:59 -0700467static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100468{
469 OMAP_HSMMC_WRITE(host->base, SYSCTL,
470 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
471 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900472 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100473}
474
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700475static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
476 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700477{
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200478 u32 irq_mask = INT_EN_MASK;
479 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700480
481 if (host->use_dma)
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200482 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700483
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700484 /* Disable timeout for erases */
485 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530486 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700487
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200488 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700489 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
490 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200491
492 /* latch pending CIRQ, but don't signal MMC core */
493 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
494 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700495 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200496 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700497}
498
499static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
500{
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200501 u32 irq_mask = 0;
502 unsigned long flags;
503
504 spin_lock_irqsave(&host->irq_lock, flags);
505 /* no transfer running but need to keep cirq if enabled */
506 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
507 irq_mask |= CIRQ_EN;
508 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
509 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700510 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200511 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700512}
513
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300514/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530515static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300516{
517 u16 dsor = 0;
518
519 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530520 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530521 if (dsor > CLKD_MAX)
522 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300523 }
524
525 return dsor;
526}
527
Andy Shevchenko5934df22011-05-06 12:14:06 +0300528static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
529{
530 struct mmc_ios *ios = &host->mmc->ios;
531 unsigned long regval;
532 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530533 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300534
Venkatraman S8986d312012-08-07 19:10:38 +0530535 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300536
537 omap_hsmmc_stop_clock(host);
538
539 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
540 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530541 clkdiv = calc_divisor(host, ios);
542 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300543 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
544 OMAP_HSMMC_WRITE(host->base, SYSCTL,
545 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
546
547 /* Wait till the ICS bit is set */
548 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
549 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
550 && time_before(jiffies, timeout))
551 cpu_relax();
552
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530553 /*
554 * Enable High-Speed Support
555 * Pre-Requisites
556 * - Controller should support High-Speed-Enable Bit
557 * - Controller should not be using DDR Mode
558 * - Controller should advertise that it supports High Speed
559 * in capabilities register
560 * - MMC/SD clock coming out of controller > 25MHz
561 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100562 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900563 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100564 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530565 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
566 regval = OMAP_HSMMC_READ(host->base, HCTL);
567 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
568 regval |= HSPE;
569 else
570 regval &= ~HSPE;
571
572 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
573 }
574
Andy Shevchenko5934df22011-05-06 12:14:06 +0300575 omap_hsmmc_start_clock(host);
576}
577
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400578static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
579{
580 struct mmc_ios *ios = &host->mmc->ios;
581 u32 con;
582
583 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100584 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
585 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530586 con |= DDR; /* configure in DDR mode */
587 else
588 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400589 switch (ios->bus_width) {
590 case MMC_BUS_WIDTH_8:
591 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
592 break;
593 case MMC_BUS_WIDTH_4:
594 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
595 OMAP_HSMMC_WRITE(host->base, HCTL,
596 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
597 break;
598 case MMC_BUS_WIDTH_1:
599 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
600 OMAP_HSMMC_WRITE(host->base, HCTL,
601 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
602 break;
603 }
604}
605
606static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
607{
608 struct mmc_ios *ios = &host->mmc->ios;
609 u32 con;
610
611 con = OMAP_HSMMC_READ(host->base, CON);
612 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
613 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
614 else
615 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
616}
617
Denis Karpov11dd62a2009-09-22 16:44:43 -0700618#ifdef CONFIG_PM
619
620/*
621 * Restore the MMC host context, if it was lost as result of a
622 * power state change.
623 */
Denis Karpov70a33412009-09-22 16:44:59 -0700624static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700625{
626 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400627 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700628 unsigned long timeout;
629
Tony Lindgren0a82e062013-10-21 00:25:19 +0530630 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
631 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
632 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
633 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
634 return 0;
635
636 host->context_loss++;
637
Balaji T Kc2200ef2012-03-07 09:55:30 -0500638 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700639 if (host->power_mode != MMC_POWER_OFF &&
640 (1 << ios->vdd) <= MMC_VDD_23_24)
641 hctl = SDVS18;
642 else
643 hctl = SDVS30;
644 capa = VS30 | VS18;
645 } else {
646 hctl = SDVS18;
647 capa = VS18;
648 }
649
Balaji T K5a52b082014-05-29 10:28:02 +0200650 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
651 hctl |= IWE;
652
Denis Karpov11dd62a2009-09-22 16:44:43 -0700653 OMAP_HSMMC_WRITE(host->base, HCTL,
654 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
655
656 OMAP_HSMMC_WRITE(host->base, CAPA,
657 OMAP_HSMMC_READ(host->base, CAPA) | capa);
658
659 OMAP_HSMMC_WRITE(host->base, HCTL,
660 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
661
662 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
663 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
664 && time_before(jiffies, timeout))
665 ;
666
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +0200667 OMAP_HSMMC_WRITE(host->base, ISE, 0);
668 OMAP_HSMMC_WRITE(host->base, IE, 0);
669 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700670
671 /* Do not initialize card-specific things if the power is off */
672 if (host->power_mode == MMC_POWER_OFF)
673 goto out;
674
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400675 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700676
Andy Shevchenko5934df22011-05-06 12:14:06 +0300677 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700678
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400679 omap_hsmmc_set_bus_mode(host);
680
Denis Karpov11dd62a2009-09-22 16:44:43 -0700681out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530682 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
683 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700684 return 0;
685}
686
687/*
688 * Save the MMC host context (store the number of power state changes so far).
689 */
Denis Karpov70a33412009-09-22 16:44:59 -0700690static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700691{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530692 host->con = OMAP_HSMMC_READ(host->base, CON);
693 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
694 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
695 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700696}
697
698#else
699
Denis Karpov70a33412009-09-22 16:44:59 -0700700static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700701{
702 return 0;
703}
704
Denis Karpov70a33412009-09-22 16:44:59 -0700705static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700706{
707}
708
709#endif
710
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100711/*
712 * Send init stream sequence to card
713 * before sending IDLE command
714 */
Denis Karpov70a33412009-09-22 16:44:59 -0700715static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100716{
717 int reg = 0;
718 unsigned long timeout;
719
Adrian Hunterb62f6222009-09-22 16:45:01 -0700720 if (host->protect_card)
721 return;
722
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700724
725 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726 OMAP_HSMMC_WRITE(host->base, CON,
727 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
728 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
729
730 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530731 while ((reg != CC_EN) && time_before(jiffies, timeout))
732 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100733
734 OMAP_HSMMC_WRITE(host->base, CON,
735 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700736
737 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
738 OMAP_HSMMC_READ(host->base, STAT);
739
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740 enable_irq(host->irq);
741}
742
743static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700744int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100745{
746 int r = 1;
747
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100748 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100749 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750 return r;
751}
752
753static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700754omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755 char *buf)
756{
757 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700758 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759
Denis Karpov70a33412009-09-22 16:44:59 -0700760 return sprintf(buf, "%s\n",
761 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100762}
763
Denis Karpov70a33412009-09-22 16:44:59 -0700764static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100765
766static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700767omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100768 char *buf)
769{
770 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700771 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100772
Andreas Fenkart326119c2014-11-08 15:33:14 +0100773 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774}
775
Denis Karpov70a33412009-09-22 16:44:59 -0700776static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100777
778/*
779 * Configure the response type and send the cmd.
780 */
781static void
Denis Karpov70a33412009-09-22 16:44:59 -0700782omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783 struct mmc_data *data)
784{
785 int cmdreg = 0, resptype = 0, cmdtype = 0;
786
Venkatraman S8986d312012-08-07 19:10:38 +0530787 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
789 host->cmd = cmd;
790
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700791 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100792
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200793 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100794 if (cmd->flags & MMC_RSP_PRESENT) {
795 if (cmd->flags & MMC_RSP_136)
796 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200797 else if (cmd->flags & MMC_RSP_BUSY) {
798 resptype = 3;
799 host->response_busy = 1;
800 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100801 resptype = 2;
802 }
803
804 /*
805 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
806 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
807 * a val of 0x3, rest 0x0.
808 */
809 if (cmd == host->mrq->stop)
810 cmdtype = 0x3;
811
812 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
813
Balaji T Ka2e77152014-01-21 19:54:42 +0530814 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
815 host->mrq->sbc) {
816 cmdreg |= ACEN_ACMD23;
817 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
818 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100819 if (data) {
820 cmdreg |= DP_SELECT | MSBS | BCE;
821 if (data->flags & MMC_DATA_READ)
822 cmdreg |= DDIR;
823 else
824 cmdreg &= ~(DDIR);
825 }
826
827 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530828 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100829
Adrian Hunterb4175772010-05-26 14:42:06 -0700830 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700831
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100832 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
833 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
834}
835
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200836static int
Denis Karpov70a33412009-09-22 16:44:59 -0700837omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200838{
839 if (data->flags & MMC_DATA_WRITE)
840 return DMA_TO_DEVICE;
841 else
842 return DMA_FROM_DEVICE;
843}
844
Russell Kingc5c98922012-04-13 12:14:39 +0100845static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
846 struct mmc_data *data)
847{
848 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
849}
850
Adrian Hunterb4175772010-05-26 14:42:06 -0700851static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
852{
853 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530854 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700855
Venkatraman S31463b12012-04-09 12:08:34 +0530856 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700857 host->req_in_progress = 0;
858 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530859 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700860
861 omap_hsmmc_disable_irq(host);
862 /* Do not complete the request if DMA is still in progress */
863 if (mrq->data && host->use_dma && dma_ch != -1)
864 return;
865 host->mrq = NULL;
866 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100867 pm_runtime_mark_last_busy(host->dev);
868 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700869}
870
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100871/*
872 * Notify the transfer complete to MMC core
873 */
874static void
Denis Karpov70a33412009-09-22 16:44:59 -0700875omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100876{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200877 if (!data) {
878 struct mmc_request *mrq = host->mrq;
879
Adrian Hunter23050102009-09-22 16:44:57 -0700880 /* TC before CC from CMD6 - don't know why, but it happens */
881 if (host->cmd && host->cmd->opcode == 6 &&
882 host->response_busy) {
883 host->response_busy = 0;
884 return;
885 }
886
Adrian Hunterb4175772010-05-26 14:42:06 -0700887 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200888 return;
889 }
890
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891 host->data = NULL;
892
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100893 if (!data->error)
894 data->bytes_xfered += data->blocks * (data->blksz);
895 else
896 data->bytes_xfered = 0;
897
Balaji T Kbf129e12014-01-21 19:54:42 +0530898 if (data->stop && (data->error || !host->mrq->sbc))
899 omap_hsmmc_start_command(host, data->stop, NULL);
900 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700901 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100902}
903
904/*
905 * Notify the core about command completion
906 */
907static void
Denis Karpov70a33412009-09-22 16:44:59 -0700908omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100909{
Balaji T Kbf129e12014-01-21 19:54:42 +0530910 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530911 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530912 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530913 omap_hsmmc_start_dma_transfer(host);
914 omap_hsmmc_start_command(host, host->mrq->cmd,
915 host->mrq->data);
916 return;
917 }
918
Balaji T K2177fa92014-05-09 22:16:52 +0530919 host->cmd = NULL;
920
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100921 if (cmd->flags & MMC_RSP_PRESENT) {
922 if (cmd->flags & MMC_RSP_136) {
923 /* response type 2 */
924 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
925 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
926 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
927 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
928 } else {
929 /* response types 1, 1b, 3, 4, 5, 6 */
930 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
931 }
932 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700933 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530934 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100935}
936
937/*
938 * DMA clean up for command errors
939 */
Denis Karpov70a33412009-09-22 16:44:59 -0700940static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100941{
Adrian Hunterb4175772010-05-26 14:42:06 -0700942 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530943 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700944
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200945 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100946
Venkatraman S31463b12012-04-09 12:08:34 +0530947 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700948 dma_ch = host->dma_ch;
949 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530950 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700951
952 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100953 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
954
955 dmaengine_terminate_all(chan);
956 dma_unmap_sg(chan->device->dev,
957 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700958 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100959
Per Forlin053bf342011-11-07 21:55:11 +0530960 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961 }
962 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100963}
964
965/*
966 * Readable error output
967 */
968#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300969static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100970{
971 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700972 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300973 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
974 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
975 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
976 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100977 };
978 char res[256];
979 char *buf = res;
980 int len, i;
981
982 len = sprintf(buf, "MMC IRQ 0x%x :", status);
983 buf += len;
984
Denis Karpov70a33412009-09-22 16:44:59 -0700985 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700987 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100988 buf += len;
989 }
990
Venkatraman S8986d312012-08-07 19:10:38 +0530991 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100992}
Adrian Hunter699b9582011-05-06 12:14:01 +0300993#else
994static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
995 u32 status)
996{
997}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998#endif /* CONFIG_MMC_DEBUG */
999
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001000/*
1001 * MMC controller internal state machines reset
1002 *
1003 * Used to reset command or data internal state machines, using respectively
1004 * SRC or SRD bit of SYSCTL register
1005 * Can be called from interrupt context
1006 */
Denis Karpov70a33412009-09-22 16:44:59 -07001007static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1008 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001009{
1010 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301011 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001012
1013 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1014 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1015
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001016 /*
1017 * OMAP4 ES2 and greater has an updated reset logic.
1018 * Monitor a 0->1 transition first
1019 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001020 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001021 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001022 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301023 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001024 }
1025 i = 0;
1026
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001027 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1028 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301029 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001030
1031 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1032 dev_err(mmc_dev(host->mmc),
1033 "Timeout waiting on controller reset in %s\n",
1034 __func__);
1035}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001036
Balaji T K25e18972012-11-19 21:59:55 +05301037static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1038 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301039{
Balaji T K25e18972012-11-19 21:59:55 +05301040 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301041 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301042 if (host->cmd)
1043 host->cmd->error = err;
1044 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301045
1046 if (host->data) {
1047 omap_hsmmc_reset_controller_fsm(host, SRD);
1048 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301049 } else if (host->mrq && host->mrq->cmd)
1050 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301051}
1052
Adrian Hunterb4175772010-05-26 14:42:06 -07001053static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001054{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001056 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301057 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001058
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301060 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061
Venkatraman Sa7e96872012-11-19 22:00:01 +05301062 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001063 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001064
Venkatraman Sa7e96872012-11-19 22:00:01 +05301065 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301066 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301067 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301068 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301069 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301070 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1071
Balaji T Ka2e77152014-01-21 19:54:42 +05301072 if (status & ACE_EN) {
1073 u32 ac12;
1074 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1075 if (!(ac12 & ACNE) && host->mrq->sbc) {
1076 end_cmd = 1;
1077 if (ac12 & ACTO)
1078 error = -ETIMEDOUT;
1079 else if (ac12 & (ACCE | ACEB | ACIE))
1080 error = -EILSEQ;
1081 host->mrq->sbc->error = error;
1082 hsmmc_command_incomplete(host, error, end_cmd);
1083 }
1084 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1085 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301086 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301087 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301088 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001089 }
1090 }
1091
Francesco Lavra7472bab2013-06-29 08:25:12 +02001092 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301093 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001094 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301095 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001096 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001097}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001098
Adrian Hunterb4175772010-05-26 14:42:06 -07001099/*
1100 * MMC controller IRQ handler
1101 */
1102static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1103{
1104 struct omap_hsmmc_host *host = dev_id;
1105 int status;
1106
1107 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001108 while (status & (INT_EN_MASK | CIRQ_EN)) {
1109 if (host->req_in_progress)
1110 omap_hsmmc_do_irq(host, status);
1111
1112 if (status & CIRQ_EN)
1113 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301114
Adrian Hunterb4175772010-05-26 14:42:06 -07001115 /* Flush posted write */
1116 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301117 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001118
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119 return IRQ_HANDLED;
1120}
1121
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001122static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1123{
1124 struct omap_hsmmc_host *host = dev_id;
1125
1126 /* cirq is level triggered, disable to avoid infinite loop */
1127 spin_lock(&host->irq_lock);
1128 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1129 disable_irq_nosync(host->wake_irq);
1130 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1131 }
1132 spin_unlock(&host->irq_lock);
1133 pm_request_resume(host->dev); /* no use counter */
1134
1135 return IRQ_HANDLED;
1136}
1137
Denis Karpov70a33412009-09-22 16:44:59 -07001138static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001139{
1140 unsigned long i;
1141
1142 OMAP_HSMMC_WRITE(host->base, HCTL,
1143 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1144 for (i = 0; i < loops_per_jiffy; i++) {
1145 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1146 break;
1147 cpu_relax();
1148 }
1149}
1150
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151/*
David Brownelleb250822009-02-17 14:49:01 -08001152 * Switch MMC interface voltage ... only relevant for MMC1.
1153 *
1154 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1155 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1156 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001157 */
Denis Karpov70a33412009-09-22 16:44:59 -07001158static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001159{
1160 u32 reg_val = 0;
1161 int ret;
1162
1163 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301164 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301165 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301166 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167
1168 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001169 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170
1171 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001172 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001173 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301174 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301175 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301176 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001177
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001178 if (ret != 0)
1179 goto err;
1180
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181 OMAP_HSMMC_WRITE(host->base, HCTL,
1182 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1183 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001184
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001185 /*
1186 * If a MMC dual voltage card is detected, the set_ios fn calls
1187 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001188 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001189 *
David Brownelleb250822009-02-17 14:49:01 -08001190 * Cope with a bit of slop in the range ... per data sheets:
1191 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1192 * but recommended values are 1.71V to 1.89V
1193 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1194 * but recommended values are 2.7V to 3.3V
1195 *
1196 * Board setup code shouldn't permit anything very out-of-range.
1197 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1198 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199 */
David Brownelleb250822009-02-17 14:49:01 -08001200 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001202 else
1203 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001204
1205 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001206 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207
1208 return 0;
1209err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301210 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001211 return ret;
1212}
1213
Adrian Hunterb62f6222009-09-22 16:45:01 -07001214/* Protect the card while the cover is open */
1215static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1216{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001217 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001218 return;
1219
1220 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001221 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001222 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301223 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001224 "card is now accessible\n",
1225 mmc_hostname(host->mmc));
1226 host->protect_card = 0;
1227 }
1228 } else {
1229 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301230 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001231 "card is now inaccessible\n",
1232 mmc_hostname(host->mmc));
1233 host->protect_card = 1;
1234 }
1235 }
1236}
1237
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001239 * irq handler when (cell-phone) cover is mounted/removed
1240 */
1241static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1242{
1243 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001244
1245 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1246
Andreas Fenkart11227d12015-03-03 13:28:17 +01001247 omap_hsmmc_protect_card(host);
1248 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001249 return IRQ_HANDLED;
1250}
1251
1252/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001253 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001255static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001256{
NeilBrown7efab4f2011-12-30 12:35:13 +11001257 struct omap_hsmmc_host *host = dev_id;
David Brownell249d0fa2009-02-04 14:42:03 -08001258
Andreas Fenkart11227d12015-03-03 13:28:17 +01001259 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001260 return IRQ_HANDLED;
1261}
1262
Russell Kingc5c98922012-04-13 12:14:39 +01001263static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001264{
Russell Kingc5c98922012-04-13 12:14:39 +01001265 struct omap_hsmmc_host *host = param;
1266 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001267 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001268 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269
Russell Kingc5c98922012-04-13 12:14:39 +01001270 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001271 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001272 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001273 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001274 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001275
Adrian Hunter770d7432011-05-06 12:14:11 +03001276 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001277 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001278 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001279 dma_unmap_sg(chan->device->dev,
1280 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001281 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001282
1283 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001284 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001285 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001286
1287 /* If DMA has finished after TC, complete the request */
1288 if (!req_in_progress) {
1289 struct mmc_request *mrq = host->mrq;
1290
1291 host->mrq = NULL;
1292 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001293 pm_runtime_mark_last_busy(host->dev);
1294 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001295 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001296}
1297
Per Forlin9782aff2011-07-01 18:55:23 +02001298static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1299 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001300 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001301 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001302{
1303 int dma_len;
1304
1305 if (!next && data->host_cookie &&
1306 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301307 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001308 " host->next_data.cookie %d\n",
1309 __func__, data->host_cookie, host->next_data.cookie);
1310 data->host_cookie = 0;
1311 }
1312
1313 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001314 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001315 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001316 omap_hsmmc_get_dma_dir(host, data));
1317
1318 } else {
1319 dma_len = host->next_data.dma_len;
1320 host->next_data.dma_len = 0;
1321 }
1322
1323
1324 if (dma_len == 0)
1325 return -EINVAL;
1326
1327 if (next) {
1328 next->dma_len = dma_len;
1329 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1330 } else
1331 host->dma_len = dma_len;
1332
1333 return 0;
1334}
1335
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001336/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337 * Routine to configure and start DMA for the MMC card
1338 */
Balaji T K9d025332014-01-21 19:54:42 +05301339static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001340 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001341{
Russell King26b88522012-04-13 12:27:37 +01001342 struct dma_slave_config cfg;
1343 struct dma_async_tx_descriptor *tx;
1344 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001345 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001346 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001348 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001349 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001350 struct scatterlist *sgl;
1351
1352 sgl = data->sg + i;
1353 if (sgl->length % data->blksz)
1354 return -EINVAL;
1355 }
1356 if ((data->blksz % 4) != 0)
1357 /* REVISIT: The MMC buffer increments only when MSB is written.
1358 * Return error for blksz which is non multiple of four.
1359 */
1360 return -EINVAL;
1361
Adrian Hunterb4175772010-05-26 14:42:06 -07001362 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001363
Russell Kingc5c98922012-04-13 12:14:39 +01001364 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001365
Russell King26b88522012-04-13 12:27:37 +01001366 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1367 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1368 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1369 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1370 cfg.src_maxburst = data->blksz / 4;
1371 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001372
Russell King26b88522012-04-13 12:27:37 +01001373 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001374 if (ret)
1375 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001376
Russell King26b88522012-04-13 12:27:37 +01001377 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1378 if (ret)
1379 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001380
Russell King26b88522012-04-13 12:27:37 +01001381 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1382 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1383 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1384 if (!tx) {
1385 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1386 /* FIXME: cleanup */
1387 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001388 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001389
Russell King26b88522012-04-13 12:27:37 +01001390 tx->callback = omap_hsmmc_dma_callback;
1391 tx->callback_param = host;
1392
1393 /* Does not fail */
1394 dmaengine_submit(tx);
1395
1396 host->dma_ch = 1;
1397
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001398 return 0;
1399}
1400
Denis Karpov70a33412009-09-22 16:44:59 -07001401static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001402 unsigned int timeout_ns,
1403 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001404{
1405 unsigned int timeout, cycle_ns;
1406 uint32_t reg, clkd, dto = 0;
1407
1408 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1409 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1410 if (clkd == 0)
1411 clkd = 1;
1412
Balaji T K6e3076c2014-01-21 19:54:42 +05301413 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001414 timeout = timeout_ns / cycle_ns;
1415 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001416 if (timeout) {
1417 while ((timeout & 0x80000000) == 0) {
1418 dto += 1;
1419 timeout <<= 1;
1420 }
1421 dto = 31 - dto;
1422 timeout <<= 1;
1423 if (timeout && dto)
1424 dto += 1;
1425 if (dto >= 13)
1426 dto -= 13;
1427 else
1428 dto = 0;
1429 if (dto > 14)
1430 dto = 14;
1431 }
1432
1433 reg &= ~DTO_MASK;
1434 reg |= dto << DTO_SHIFT;
1435 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1436}
1437
Balaji T K9d025332014-01-21 19:54:42 +05301438static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1439{
1440 struct mmc_request *req = host->mrq;
1441 struct dma_chan *chan;
1442
1443 if (!req->data)
1444 return;
1445 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1446 | (req->data->blocks << 16));
1447 set_data_timeout(host, req->data->timeout_ns,
1448 req->data->timeout_clks);
1449 chan = omap_hsmmc_get_dma_chan(host, req->data);
1450 dma_async_issue_pending(chan);
1451}
1452
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001453/*
1454 * Configure block length for MMC/SD cards and initiate the transfer.
1455 */
1456static int
Denis Karpov70a33412009-09-22 16:44:59 -07001457omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001458{
1459 int ret;
1460 host->data = req->data;
1461
1462 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001464 /*
1465 * Set an arbitrary 100ms data timeout for commands with
1466 * busy signal.
1467 */
1468 if (req->cmd->flags & MMC_RSP_BUSY)
1469 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001470 return 0;
1471 }
1472
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001473 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301474 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001475 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301476 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001477 return ret;
1478 }
1479 }
1480 return 0;
1481}
1482
Per Forlin9782aff2011-07-01 18:55:23 +02001483static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1484 int err)
1485{
1486 struct omap_hsmmc_host *host = mmc_priv(mmc);
1487 struct mmc_data *data = mrq->data;
1488
Russell King26b88522012-04-13 12:27:37 +01001489 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001490 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001491
Russell King26b88522012-04-13 12:27:37 +01001492 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1493 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001494 data->host_cookie = 0;
1495 }
1496}
1497
1498static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1499 bool is_first_req)
1500{
1501 struct omap_hsmmc_host *host = mmc_priv(mmc);
1502
1503 if (mrq->data->host_cookie) {
1504 mrq->data->host_cookie = 0;
1505 return ;
1506 }
1507
Russell Kingc5c98922012-04-13 12:14:39 +01001508 if (host->use_dma) {
1509 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001510
Per Forlin9782aff2011-07-01 18:55:23 +02001511 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001512 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001513 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001514 }
Per Forlin9782aff2011-07-01 18:55:23 +02001515}
1516
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001517/*
1518 * Request function. for read/write operation
1519 */
Denis Karpov70a33412009-09-22 16:44:59 -07001520static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001521{
Denis Karpov70a33412009-09-22 16:44:59 -07001522 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001523 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001524
Adrian Hunterb4175772010-05-26 14:42:06 -07001525 BUG_ON(host->req_in_progress);
1526 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001527 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001528 if (host->protect_card) {
1529 if (host->reqs_blocked < 3) {
1530 /*
1531 * Ensure the controller is left in a consistent
1532 * state by resetting the command and data state
1533 * machines.
1534 */
1535 omap_hsmmc_reset_controller_fsm(host, SRD);
1536 omap_hsmmc_reset_controller_fsm(host, SRC);
1537 host->reqs_blocked += 1;
1538 }
1539 req->cmd->error = -EBADF;
1540 if (req->data)
1541 req->data->error = -EBADF;
1542 req->cmd->retries = 0;
1543 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001544 pm_runtime_mark_last_busy(host->dev);
1545 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001546 return;
1547 } else if (host->reqs_blocked)
1548 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001549 WARN_ON(host->mrq != NULL);
1550 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301551 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001552 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001553 if (err) {
1554 req->cmd->error = err;
1555 if (req->data)
1556 req->data->error = err;
1557 host->mrq = NULL;
1558 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001559 pm_runtime_mark_last_busy(host->dev);
1560 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001561 return;
1562 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301563 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301564 omap_hsmmc_start_command(host, req->sbc, NULL);
1565 return;
1566 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001567
Balaji T K9d025332014-01-21 19:54:42 +05301568 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001569 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001570}
1571
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001572/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001573static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001574{
Denis Karpov70a33412009-09-22 16:44:59 -07001575 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001576 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001577
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301578 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001579
Adrian Huntera3621462009-09-22 16:44:42 -07001580 if (ios->power_mode != host->power_mode) {
1581 switch (ios->power_mode) {
1582 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001583 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001584 break;
1585 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001586 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001587 break;
1588 case MMC_POWER_ON:
1589 do_send_init_stream = 1;
1590 break;
1591 }
1592 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593 }
1594
Denis Karpovdd498ef2009-09-22 16:44:49 -07001595 /* FIXME: set registers based only on changes to ios */
1596
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001597 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001598
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301599 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001600 /* Only MMC1 can interface at 3V without some flavor
1601 * of external transceiver; but they all handle 1.8V.
1602 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301604 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605 /*
1606 * The mmc_select_voltage fn of the core does
1607 * not seem to set the power_mode to
1608 * MMC_POWER_UP upon recalculating the voltage.
1609 * vdd 1.8v.
1610 */
Denis Karpov70a33412009-09-22 16:44:59 -07001611 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1612 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001613 "Switch operation failed\n");
1614 }
1615 }
1616
Andy Shevchenko5934df22011-05-06 12:14:06 +03001617 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001618
Adrian Huntera3621462009-09-22 16:44:42 -07001619 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001620 send_init_stream(host);
1621
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001622 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001623
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301624 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001625}
1626
1627static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1628{
Denis Karpov70a33412009-09-22 16:44:59 -07001629 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001630
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001631 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001632 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001633 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001634}
1635
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001636static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1637{
1638 struct omap_hsmmc_host *host = mmc_priv(mmc);
1639
Andreas Fenkart326119c2014-11-08 15:33:14 +01001640 if (mmc_pdata(host)->init_card)
1641 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001642}
1643
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001644static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1645{
1646 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001647 u32 irq_mask, con;
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001648 unsigned long flags;
1649
1650 spin_lock_irqsave(&host->irq_lock, flags);
1651
Balaji T K5a52b082014-05-29 10:28:02 +02001652 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001653 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1654 if (enable) {
1655 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1656 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001657 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001658 } else {
1659 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1660 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001661 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001662 }
Balaji T K5a52b082014-05-29 10:28:02 +02001663 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001664 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1665
1666 /*
1667 * if enable, piggy back detection on current request
1668 * but always disable immediately
1669 */
1670 if (!host->req_in_progress || !enable)
1671 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1672
1673 /* flush posted write */
1674 OMAP_HSMMC_READ(host->base, IE);
1675
1676 spin_unlock_irqrestore(&host->irq_lock, flags);
1677}
1678
1679static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1680{
1681 struct mmc_host *mmc = host->mmc;
1682 int ret;
1683
1684 /*
1685 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1686 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1687 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1688 * with functional clock disabled.
1689 */
1690 if (!host->dev->of_node || !host->wake_irq)
1691 return -ENODEV;
1692
1693 /* Prevent auto-enabling of IRQ */
1694 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1695 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1696 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1697 mmc_hostname(mmc), host);
1698 if (ret) {
1699 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1700 goto err;
1701 }
1702
1703 /*
1704 * Some omaps don't have wake-up path from deeper idle states
1705 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1706 */
1707 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001708 struct pinctrl *p = devm_pinctrl_get(host->dev);
1709 if (!p) {
1710 ret = -ENODEV;
1711 goto err_free_irq;
1712 }
1713 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1714 dev_info(host->dev, "missing default pinctrl state\n");
1715 devm_pinctrl_put(p);
1716 ret = -EINVAL;
1717 goto err_free_irq;
1718 }
1719
1720 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1721 dev_info(host->dev, "missing idle pinctrl state\n");
1722 devm_pinctrl_put(p);
1723 ret = -EINVAL;
1724 goto err_free_irq;
1725 }
1726 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001727 }
1728
Balaji T K5a52b082014-05-29 10:28:02 +02001729 OMAP_HSMMC_WRITE(host->base, HCTL,
1730 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001731 return 0;
1732
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001733err_free_irq:
1734 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001735err:
1736 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1737 host->wake_irq = 0;
1738 return ret;
1739}
1740
Denis Karpov70a33412009-09-22 16:44:59 -07001741static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001742{
1743 u32 hctl, capa, value;
1744
1745 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301746 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001747 hctl = SDVS30;
1748 capa = VS30 | VS18;
1749 } else {
1750 hctl = SDVS18;
1751 capa = VS18;
1752 }
1753
1754 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1755 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1756
1757 value = OMAP_HSMMC_READ(host->base, CAPA);
1758 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1759
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001760 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001761 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001762}
1763
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001764static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1765 unsigned int direction, int blk_size)
1766{
1767 /* This controller can't do multiblock reads due to hw bugs */
1768 if (direction == MMC_DATA_READ)
1769 return 1;
1770
1771 return blk_size;
1772}
1773
1774static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001775 .post_req = omap_hsmmc_post_req,
1776 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001777 .request = omap_hsmmc_request,
1778 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001779 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001780 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001781 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001782 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001783};
1784
Denis Karpovd900f712009-09-22 16:44:38 -07001785#ifdef CONFIG_DEBUG_FS
1786
Denis Karpov70a33412009-09-22 16:44:59 -07001787static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001788{
1789 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001790 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001791
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001792 seq_printf(s, "mmc%d:\n", mmc->index);
1793 seq_printf(s, "sdio irq mode\t%s\n",
1794 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1795
1796 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1797 seq_printf(s, "sdio irq \t%s\n",
1798 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1799 : "disabled");
1800 }
1801 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001802
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301803 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001804 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001805 seq_printf(s, "CON:\t\t0x%08x\n",
1806 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001807 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1808 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001809 seq_printf(s, "HCTL:\t\t0x%08x\n",
1810 OMAP_HSMMC_READ(host->base, HCTL));
1811 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1812 OMAP_HSMMC_READ(host->base, SYSCTL));
1813 seq_printf(s, "IE:\t\t0x%08x\n",
1814 OMAP_HSMMC_READ(host->base, IE));
1815 seq_printf(s, "ISE:\t\t0x%08x\n",
1816 OMAP_HSMMC_READ(host->base, ISE));
1817 seq_printf(s, "CAPA:\t\t0x%08x\n",
1818 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001819
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301820 pm_runtime_mark_last_busy(host->dev);
1821 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001822
Denis Karpovd900f712009-09-22 16:44:38 -07001823 return 0;
1824}
1825
Denis Karpov70a33412009-09-22 16:44:59 -07001826static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001827{
Denis Karpov70a33412009-09-22 16:44:59 -07001828 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001829}
1830
1831static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001832 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001833 .read = seq_read,
1834 .llseek = seq_lseek,
1835 .release = single_release,
1836};
1837
Denis Karpov70a33412009-09-22 16:44:59 -07001838static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001839{
1840 if (mmc->debugfs_root)
1841 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1842 mmc, &mmc_regs_fops);
1843}
1844
1845#else
1846
Denis Karpov70a33412009-09-22 16:44:59 -07001847static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001848{
1849}
1850
1851#endif
1852
Rajendra Nayak46856a62012-03-12 20:32:37 +05301853#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001854static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1855 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1856 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1857};
1858
1859static const struct omap_mmc_of_data omap4_mmc_of_data = {
1860 .reg_offset = 0x100,
1861};
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001862static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1863 .reg_offset = 0x100,
1864 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1865};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301866
1867static const struct of_device_id omap_mmc_of_match[] = {
1868 {
1869 .compatible = "ti,omap2-hsmmc",
1870 },
1871 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001872 .compatible = "ti,omap3-pre-es3-hsmmc",
1873 .data = &omap3_pre_es3_mmc_of_data,
1874 },
1875 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301876 .compatible = "ti,omap3-hsmmc",
1877 },
1878 {
1879 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001880 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301881 },
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001882 {
1883 .compatible = "ti,am33xx-hsmmc",
1884 .data = &am33xx_mmc_of_data,
1885 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301886 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001887};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301888MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1889
Andreas Fenkart55143432014-11-08 15:33:09 +01001890static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301891{
Andreas Fenkart55143432014-11-08 15:33:09 +01001892 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301893 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301894
1895 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1896 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301897 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301898
1899 if (of_find_property(np, "ti,dual-volt", NULL))
1900 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1901
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001902 pdata->gpio_cd = -EINVAL;
1903 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001904 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301905
1906 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001907 pdata->nonremovable = true;
1908 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301909 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301910
1911 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001912 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301913
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301914 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001915 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301916
Rajendra Nayak46856a62012-03-12 20:32:37 +05301917 return pdata;
1918}
1919#else
Andreas Fenkart55143432014-11-08 15:33:09 +01001920static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301921 *of_get_hsmmc_pdata(struct device *dev)
1922{
Balaji T K19df45b2014-02-28 19:08:18 +05301923 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301924}
1925#endif
1926
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001927static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001928{
Andreas Fenkart55143432014-11-08 15:33:09 +01001929 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001930 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001931 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001932 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001933 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301934 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001935 dma_cap_mask_t mask;
1936 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06001937 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301938 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301939
1940 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1941 if (match) {
1942 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001943
1944 if (IS_ERR(pdata))
1945 return PTR_ERR(pdata);
1946
Rajendra Nayak46856a62012-03-12 20:32:37 +05301947 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001948 data = match->data;
1949 pdata->reg_offset = data->reg_offset;
1950 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301951 }
1952 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001953
1954 if (pdata == NULL) {
1955 dev_err(&pdev->dev, "Platform Data is missing\n");
1956 return -ENXIO;
1957 }
1958
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001959 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1960 irq = platform_get_irq(pdev, 0);
1961 if (res == NULL || irq < 0)
1962 return -ENXIO;
1963
Balaji T K77fae212014-05-09 22:16:51 +05301964 base = devm_ioremap_resource(&pdev->dev, res);
1965 if (IS_ERR(base))
1966 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001967
Denis Karpov70a33412009-09-22 16:44:59 -07001968 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001969 if (!mmc) {
1970 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001971 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001972 }
1973
NeilBrownfdb9de12015-01-13 08:23:18 +13001974 ret = mmc_of_parse(mmc);
1975 if (ret)
1976 goto err1;
1977
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978 host = mmc_priv(mmc);
1979 host->mmc = mmc;
1980 host->pdata = pdata;
1981 host->dev = &pdev->dev;
1982 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001983 host->dma_ch = -1;
1984 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05301985 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05301986 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08001987 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001988 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05301989 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001990
NeilBrown41afa3142015-01-13 08:23:18 +13001991 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001992 if (ret)
1993 goto err_gpio;
1994
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001995 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001996
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02001997 if (pdev->dev.of_node)
1998 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
1999
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302000 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002001
Daniel Mackd418ed82012-02-19 13:20:33 +01002002 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2003
2004 if (pdata->max_freq > 0)
2005 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002006 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002007 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002008
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002009 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002010
Balaji T K96181952014-05-09 22:16:48 +05302011 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002012 if (IS_ERR(host->fclk)) {
2013 ret = PTR_ERR(host->fclk);
2014 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002015 goto err1;
2016 }
2017
Paul Walmsley9b682562011-10-06 14:50:35 -06002018 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2019 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 -07002020 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002021 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002022
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302023 pm_runtime_enable(host->dev);
2024 pm_runtime_get_sync(host->dev);
2025 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2026 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027
Balaji T K92a3aeb2012-02-24 21:14:34 +05302028 omap_hsmmc_context_save(host);
2029
Balaji T K96181952014-05-09 22:16:48 +05302030 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302031 /*
2032 * MMC can still work without debounce clock.
2033 */
2034 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302035 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302036 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302037 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302038 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002039 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002041 /* Since we do only SG emulation, we can have as many segs
2042 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002043 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002044
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2046 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2047 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2048 mmc->max_seg_size = mmc->max_req_size;
2049
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002050 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002051 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002052
Andreas Fenkart326119c2014-11-08 15:33:14 +01002053 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002054 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002055 mmc->caps |= MMC_CAP_4_BIT_DATA;
2056
Andreas Fenkart326119c2014-11-08 15:33:14 +01002057 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002058 mmc->caps |= MMC_CAP_NONREMOVABLE;
2059
NeilBrownfdb9de12015-01-13 08:23:18 +13002060 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002061
Denis Karpov70a33412009-09-22 16:44:59 -07002062 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002063
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302064 if (!pdev->dev.of_node) {
2065 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2066 if (!res) {
2067 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2068 ret = -ENXIO;
2069 goto err_irq;
2070 }
2071 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002072
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302073 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2074 if (!res) {
2075 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2076 ret = -ENXIO;
2077 goto err_irq;
2078 }
2079 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002080 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002081
Russell King26b88522012-04-13 12:27:37 +01002082 dma_cap_zero(mask);
2083 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002084
Matt Porterd272fbf2013-05-10 17:42:34 +05302085 host->rx_chan =
2086 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2087 &rx_req, &pdev->dev, "rx");
2088
Russell King26b88522012-04-13 12:27:37 +01002089 if (!host->rx_chan) {
2090 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002091 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002092 goto err_irq;
2093 }
2094
Matt Porterd272fbf2013-05-10 17:42:34 +05302095 host->tx_chan =
2096 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2097 &tx_req, &pdev->dev, "tx");
2098
Russell King26b88522012-04-13 12:27:37 +01002099 if (!host->tx_chan) {
2100 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002101 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002102 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002103 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002105 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302106 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002107 mmc_hostname(mmc), host);
2108 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302109 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002110 goto err_irq;
2111 }
2112
Andreas Fenkart326119c2014-11-08 15:33:14 +01002113 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002114 ret = omap_hsmmc_reg_get(host);
2115 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002116 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002117 host->use_reg = 1;
2118 }
2119
Andreas Fenkart326119c2014-11-08 15:33:14 +01002120 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002121
Adrian Hunterb4175772010-05-26 14:42:06 -07002122 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002123
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002124 /*
2125 * For now, only support SDIO interrupt if we have a separate
2126 * wake-up interrupt configured from device tree. This is because
2127 * the wake-up interrupt is needed for idle state and some
2128 * platforms need special quirks. And we don't want to add new
2129 * legacy mux platform init code callbacks any longer as we
2130 * are moving to DT based booting anyways.
2131 */
2132 ret = omap_hsmmc_configure_wake_irq(host);
2133 if (!ret)
2134 mmc->caps |= MMC_CAP_SDIO_IRQ;
2135
Adrian Hunterb62f6222009-09-22 16:45:01 -07002136 omap_hsmmc_protect_card(host);
2137
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138 mmc_add_host(mmc);
2139
Andreas Fenkart326119c2014-11-08 15:33:14 +01002140 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2142 if (ret < 0)
2143 goto err_slot_name;
2144 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002145 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002147 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002148 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002149 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150 }
2151
Denis Karpov70a33412009-09-22 16:44:59 -07002152 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302153 pm_runtime_mark_last_busy(host->dev);
2154 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002155
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156 return 0;
2157
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158err_slot_name:
2159 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002160 if (host->use_reg)
2161 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002162err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002163 if (host->tx_chan)
2164 dma_release_channel(host->tx_chan);
2165 if (host->rx_chan)
2166 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302167 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002168 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302169 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302170 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002172err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002173 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002174err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002175 return ret;
2176}
2177
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002178static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002179{
Denis Karpov70a33412009-09-22 16:44:59 -07002180 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002181
Felipe Balbi927ce942012-03-14 11:18:27 +02002182 pm_runtime_get_sync(host->dev);
2183 mmc_remove_host(host->mmc);
2184 if (host->use_reg)
2185 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002186
Russell Kingc5c98922012-04-13 12:14:39 +01002187 if (host->tx_chan)
2188 dma_release_channel(host->tx_chan);
2189 if (host->rx_chan)
2190 dma_release_channel(host->rx_chan);
2191
Felipe Balbi927ce942012-03-14 11:18:27 +02002192 pm_runtime_put_sync(host->dev);
2193 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302194 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302195 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002196
Balaji T K9d1f0282012-10-15 21:35:07 +05302197 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002198
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002199 return 0;
2200}
2201
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002202#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002203static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002204{
Felipe Balbi927ce942012-03-14 11:18:27 +02002205 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2206
2207 if (!host)
2208 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002209
Felipe Balbi927ce942012-03-14 11:18:27 +02002210 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002211
2212 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002213 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2214 OMAP_HSMMC_WRITE(host->base, IE, 0);
2215 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002216 OMAP_HSMMC_WRITE(host->base, HCTL,
2217 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2218 }
2219
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002220 /* do not wake up due to sdio irq */
2221 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2222 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2223 disable_irq(host->wake_irq);
2224
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302225 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302226 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002227
Eliad Peller31f9d462011-11-22 16:02:17 +02002228 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002229 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002230}
2231
2232/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002233static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234{
Felipe Balbi927ce942012-03-14 11:18:27 +02002235 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2236
2237 if (!host)
2238 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002239
Felipe Balbi927ce942012-03-14 11:18:27 +02002240 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002241
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302242 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302243 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002244
Felipe Balbi927ce942012-03-14 11:18:27 +02002245 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2246 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002247
Felipe Balbi927ce942012-03-14 11:18:27 +02002248 omap_hsmmc_protect_card(host);
2249
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002250 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2251 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2252 enable_irq(host->wake_irq);
2253
Felipe Balbi927ce942012-03-14 11:18:27 +02002254 pm_runtime_mark_last_busy(host->dev);
2255 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002256 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002257}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002258#endif
2259
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302260static int omap_hsmmc_runtime_suspend(struct device *dev)
2261{
2262 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002263 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002264 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302265
2266 host = platform_get_drvdata(to_platform_device(dev));
2267 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002268 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302269
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002270 spin_lock_irqsave(&host->irq_lock, flags);
2271 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2272 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2273 /* disable sdio irq handling to prevent race */
2274 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2275 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002276
2277 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2278 /*
2279 * dat1 line low, pending sdio irq
2280 * race condition: possible irq handler running on
2281 * multi-core, abort
2282 */
2283 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2284 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2285 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2286 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2287 pm_runtime_mark_last_busy(dev);
2288 ret = -EBUSY;
2289 goto abort;
2290 }
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002291
Andreas Fenkart97978a42014-05-29 10:28:04 +02002292 pinctrl_pm_select_idle_state(dev);
2293
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002294 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2295 enable_irq(host->wake_irq);
2296 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002297 } else {
2298 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002299 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002300
Andreas Fenkartf9459012014-05-29 10:28:03 +02002301abort:
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002302 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002303 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302304}
2305
2306static int omap_hsmmc_runtime_resume(struct device *dev)
2307{
2308 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002309 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302310
2311 host = platform_get_drvdata(to_platform_device(dev));
2312 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002313 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302314
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002315 spin_lock_irqsave(&host->irq_lock, flags);
2316 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2317 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2318 /* sdio irq flag can't change while in runtime suspend */
2319 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2320 disable_irq_nosync(host->wake_irq);
2321 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2322 }
2323
Andreas Fenkart97978a42014-05-29 10:28:04 +02002324 pinctrl_pm_select_default_state(host->dev);
2325
2326 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002327 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2328 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2329 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002330 } else {
2331 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a52014-05-29 10:28:00 +02002332 }
2333 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302334 return 0;
2335}
2336
Kevin Hilmana791daa2010-05-26 14:42:07 -07002337static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002338 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302339 .runtime_suspend = omap_hsmmc_runtime_suspend,
2340 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002341};
2342
2343static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002344 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002345 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002346 .driver = {
2347 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002348 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302349 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002350 },
2351};
2352
Felipe Balbib7964502012-03-14 11:18:32 +02002353module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002354MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2355MODULE_LICENSE("GPL");
2356MODULE_ALIAS("platform:" DRIVER_NAME);
2357MODULE_AUTHOR("Texas Instruments Inc");