blob: b2b411da297b06e73441f8dd51c8bae0b004bcc0 [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 Shevchenkoac330f42011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
NeilBrown41afa3142015-01-13 08:23:18 +130039#include <linux/mmc/slot-gpio.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010040#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020041#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053044#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053045#include <linux/pm_runtime.h>
Tony Lindgren5b83b222015-05-21 15:51:52 -070046#include <linux/pm_wakeirq.h>
Andreas Fenkart55143432014-11-08 15:33:09 +010047#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010048
49/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070050#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053052#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010053#define OMAP_HSMMC_BLK 0x0104
54#define OMAP_HSMMC_ARG 0x0108
55#define OMAP_HSMMC_CMD 0x010C
56#define OMAP_HSMMC_RSP10 0x0110
57#define OMAP_HSMMC_RSP32 0x0114
58#define OMAP_HSMMC_RSP54 0x0118
59#define OMAP_HSMMC_RSP76 0x011C
60#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020061#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010062#define OMAP_HSMMC_HCTL 0x0128
63#define OMAP_HSMMC_SYSCTL 0x012C
64#define OMAP_HSMMC_STAT 0x0130
65#define OMAP_HSMMC_IE 0x0134
66#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053067#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010068#define OMAP_HSMMC_CAPA 0x0140
69
70#define VS18 (1 << 26)
71#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053072#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010073#define SDVS18 (0x5 << 9)
74#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080075#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010076#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010077#define SDVSCLR 0xFFFFF1FF
78#define SDVSDET 0x00000400
79#define AUTOIDLE 0x1
80#define SDBP (1 << 8)
81#define DTO 0xe
82#define ICE 0x1
83#define ICS 0x2
84#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053085#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010086#define CLKD_MASK 0x0000FFC0
87#define CLKD_SHIFT 6
88#define DTO_MASK 0x000F0000
89#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053091#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010092#define DP_SELECT (1 << 21)
93#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053094#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010095#define MSBS (1 << 5)
96#define BCE (1 << 1)
97#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053098#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020099#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +0530100#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200101#define CLKEXTFREE (1 << 16)
102#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200103#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100105#define STAT_CLEAR 0xFFFFFFFF
106#define INIT_STREAM_CMD 0x00000000
107#define DUAL_VOLT_OCR_BIT 7
108#define SRC (1 << 25)
109#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700110#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100111
Andreas Fenkartf9459012014-05-29 10:28:03 +0200112/* PSTATE */
113#define DLEV_DAT(x) (1 << (20 + (x)))
114
Venkatraman Sa7e96872012-11-19 22:00:01 +0530115/* Interrupt masks for IE and ISE register */
116#define CC_EN (1 << 0)
117#define TC_EN (1 << 1)
118#define BWR_EN (1 << 4)
119#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200120#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530121#define ERR_EN (1 << 15)
122#define CTO_EN (1 << 16)
123#define CCRC_EN (1 << 17)
124#define CEB_EN (1 << 18)
125#define CIE_EN (1 << 19)
126#define DTO_EN (1 << 20)
127#define DCRC_EN (1 << 21)
128#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530129#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530130#define CERR_EN (1 << 28)
131#define BADA_EN (1 << 29)
132
Balaji T Ka2e77152014-01-21 19:54:42 +0530133#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530134 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
135 BRR_EN | BWR_EN | TC_EN | CC_EN)
136
Balaji T Ka2e77152014-01-21 19:54:42 +0530137#define CNI (1 << 7)
138#define ACIE (1 << 4)
139#define ACEB (1 << 3)
140#define ACCE (1 << 2)
141#define ACTO (1 << 1)
142#define ACNE (1 << 0)
143
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530144#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530145#define MMC_TIMEOUT_MS 20 /* 20 mSec */
146#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400147#define OMAP_MMC_MIN_CLOCK 400000
148#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530149#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100150
Balaji T Ke99448f2014-02-19 20:26:40 +0530151#define VDD_1V8 1800000 /* 180000 uV */
152#define VDD_3V0 3000000 /* 300000 uV */
153#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
154
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100155/*
156 * One controller can have multiple slots, like on some omap boards using
157 * omap.c controller driver. Luckily this is not currently done on any known
158 * omap_hsmmc.c device.
159 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100160#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100161
162/*
163 * MMC Host controller read/write API's
164 */
165#define OMAP_HSMMC_READ(base, reg) \
166 __raw_readl((base) + OMAP_HSMMC_##reg)
167
168#define OMAP_HSMMC_WRITE(base, reg, val) \
169 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
170
Per Forlin9782aff2011-07-01 18:55:23 +0200171struct omap_hsmmc_next {
172 unsigned int dma_len;
173 s32 cookie;
174};
175
Denis Karpov70a33412009-09-22 16:44:59 -0700176struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100177 struct device *dev;
178 struct mmc_host *mmc;
179 struct mmc_request *mrq;
180 struct mmc_command *cmd;
181 struct mmc_data *data;
182 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100183 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800184 /*
185 * vcc == configured supply
186 * vcc_aux == optional
187 * - MMC1, supply for DAT4..DAT7
188 * - MMC2/MMC2, external level shifter voltage supply, for
189 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
190 */
191 struct regulator *vcc;
192 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530193 struct regulator *pbias;
194 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100195 void __iomem *base;
196 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700197 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100198 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200199 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700201 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100202 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530203 u32 con;
204 u32 hctl;
205 u32 sysctl;
206 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100207 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200208 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100209 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100210 struct dma_chan *tx_chan;
211 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200212 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700213 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700214 int protect_card;
215 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800216 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700217 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530218 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530219 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200220#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
221#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
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
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100421static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300422
423static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
424 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100425 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800426{
427 int ret;
428
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100429 if (gpio_is_valid(pdata->gpio_cod)) {
430 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cod, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800431 if (ret)
432 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100433
434 host->get_cover_state = omap_hsmmc_get_cover_state;
435 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
Andreas Fenkartb7a56462015-03-20 15:53:54 +0100436 } else if (gpio_is_valid(pdata->gpio_cd)) {
437 ret = mmc_gpio_request_cd(mmc, pdata->gpio_cd, 0);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100438 if (ret)
439 return ret;
440
441 host->card_detect = omap_hsmmc_card_detect;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100442 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800443
Andreas Fenkart326119c2014-11-08 15:33:14 +0100444 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300445 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800446 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300447 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100448 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800449
450 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800451}
452
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100453/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300454 * Start clock to the card
455 */
456static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
457{
458 OMAP_HSMMC_WRITE(host->base, SYSCTL,
459 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
460}
461
462/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100463 * Stop clock to the card
464 */
Denis Karpov70a33412009-09-22 16:44:59 -0700465static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100466{
467 OMAP_HSMMC_WRITE(host->base, SYSCTL,
468 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
469 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900470 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100471}
472
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700473static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
474 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700475{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200476 u32 irq_mask = INT_EN_MASK;
477 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700478
479 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200480 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700481
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700482 /* Disable timeout for erases */
483 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530484 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700485
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200486 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700487 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
488 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200489
490 /* latch pending CIRQ, but don't signal MMC core */
491 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
492 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700493 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200494 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700495}
496
497static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
498{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200499 u32 irq_mask = 0;
500 unsigned long flags;
501
502 spin_lock_irqsave(&host->irq_lock, flags);
503 /* no transfer running but need to keep cirq if enabled */
504 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
505 irq_mask |= CIRQ_EN;
506 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
507 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700508 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200509 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700510}
511
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300512/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530513static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300514{
515 u16 dsor = 0;
516
517 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530518 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530519 if (dsor > CLKD_MAX)
520 dsor = CLKD_MAX;
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300521 }
522
523 return dsor;
524}
525
Andy Shevchenko5934df22011-05-06 12:14:06 +0300526static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
527{
528 struct mmc_ios *ios = &host->mmc->ios;
529 unsigned long regval;
530 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530531 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300532
Venkatraman S8986d312012-08-07 19:10:38 +0530533 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300534
535 omap_hsmmc_stop_clock(host);
536
537 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
538 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530539 clkdiv = calc_divisor(host, ios);
540 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300541 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
542 OMAP_HSMMC_WRITE(host->base, SYSCTL,
543 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
544
545 /* Wait till the ICS bit is set */
546 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
547 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
548 && time_before(jiffies, timeout))
549 cpu_relax();
550
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530551 /*
552 * Enable High-Speed Support
553 * Pre-Requisites
554 * - Controller should support High-Speed-Enable Bit
555 * - Controller should not be using DDR Mode
556 * - Controller should advertise that it supports High Speed
557 * in capabilities register
558 * - MMC/SD clock coming out of controller > 25MHz
559 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100560 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900561 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100562 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530563 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
564 regval = OMAP_HSMMC_READ(host->base, HCTL);
565 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
566 regval |= HSPE;
567 else
568 regval &= ~HSPE;
569
570 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
571 }
572
Andy Shevchenko5934df22011-05-06 12:14:06 +0300573 omap_hsmmc_start_clock(host);
574}
575
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400576static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
577{
578 struct mmc_ios *ios = &host->mmc->ios;
579 u32 con;
580
581 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100582 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
583 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530584 con |= DDR; /* configure in DDR mode */
585 else
586 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400587 switch (ios->bus_width) {
588 case MMC_BUS_WIDTH_8:
589 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
590 break;
591 case MMC_BUS_WIDTH_4:
592 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
593 OMAP_HSMMC_WRITE(host->base, HCTL,
594 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
595 break;
596 case MMC_BUS_WIDTH_1:
597 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
598 OMAP_HSMMC_WRITE(host->base, HCTL,
599 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
600 break;
601 }
602}
603
604static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
605{
606 struct mmc_ios *ios = &host->mmc->ios;
607 u32 con;
608
609 con = OMAP_HSMMC_READ(host->base, CON);
610 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
611 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
612 else
613 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
614}
615
Denis Karpov11dd62a2009-09-22 16:44:43 -0700616#ifdef CONFIG_PM
617
618/*
619 * Restore the MMC host context, if it was lost as result of a
620 * power state change.
621 */
Denis Karpov70a33412009-09-22 16:44:59 -0700622static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700623{
624 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400625 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700626 unsigned long timeout;
627
Tony Lindgren0a82e062013-10-21 00:25:19 +0530628 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
629 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
630 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
631 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
632 return 0;
633
634 host->context_loss++;
635
Balaji T Kc2200ef2012-03-07 09:55:30 -0500636 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700637 if (host->power_mode != MMC_POWER_OFF &&
638 (1 << ios->vdd) <= MMC_VDD_23_24)
639 hctl = SDVS18;
640 else
641 hctl = SDVS30;
642 capa = VS30 | VS18;
643 } else {
644 hctl = SDVS18;
645 capa = VS18;
646 }
647
Balaji T K5a52b082014-05-29 10:28:02 +0200648 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
649 hctl |= IWE;
650
Denis Karpov11dd62a2009-09-22 16:44:43 -0700651 OMAP_HSMMC_WRITE(host->base, HCTL,
652 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
653
654 OMAP_HSMMC_WRITE(host->base, CAPA,
655 OMAP_HSMMC_READ(host->base, CAPA) | capa);
656
657 OMAP_HSMMC_WRITE(host->base, HCTL,
658 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
659
660 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
661 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
662 && time_before(jiffies, timeout))
663 ;
664
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200665 OMAP_HSMMC_WRITE(host->base, ISE, 0);
666 OMAP_HSMMC_WRITE(host->base, IE, 0);
667 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700668
669 /* Do not initialize card-specific things if the power is off */
670 if (host->power_mode == MMC_POWER_OFF)
671 goto out;
672
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400673 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700674
Andy Shevchenko5934df22011-05-06 12:14:06 +0300675 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700676
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400677 omap_hsmmc_set_bus_mode(host);
678
Denis Karpov11dd62a2009-09-22 16:44:43 -0700679out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530680 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
681 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700682 return 0;
683}
684
685/*
686 * Save the MMC host context (store the number of power state changes so far).
687 */
Denis Karpov70a33412009-09-22 16:44:59 -0700688static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700689{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530690 host->con = OMAP_HSMMC_READ(host->base, CON);
691 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
692 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
693 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700694}
695
696#else
697
Denis Karpov70a33412009-09-22 16:44:59 -0700698static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700699{
700 return 0;
701}
702
Denis Karpov70a33412009-09-22 16:44:59 -0700703static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700704{
705}
706
707#endif
708
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100709/*
710 * Send init stream sequence to card
711 * before sending IDLE command
712 */
Denis Karpov70a33412009-09-22 16:44:59 -0700713static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100714{
715 int reg = 0;
716 unsigned long timeout;
717
Adrian Hunterb62f6222009-09-22 16:45:01 -0700718 if (host->protect_card)
719 return;
720
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100721 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700722
723 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100724 OMAP_HSMMC_WRITE(host->base, CON,
725 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
726 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
727
728 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530729 while ((reg != CC_EN) && time_before(jiffies, timeout))
730 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100731
732 OMAP_HSMMC_WRITE(host->base, CON,
733 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700734
735 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
736 OMAP_HSMMC_READ(host->base, STAT);
737
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100738 enable_irq(host->irq);
739}
740
741static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700742int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100743{
744 int r = 1;
745
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100746 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100747 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100748 return r;
749}
750
751static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700752omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100753 char *buf)
754{
755 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700756 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100757
Denis Karpov70a33412009-09-22 16:44:59 -0700758 return sprintf(buf, "%s\n",
759 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100760}
761
Denis Karpov70a33412009-09-22 16:44:59 -0700762static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100763
764static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700765omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100766 char *buf)
767{
768 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700769 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100770
Andreas Fenkart326119c2014-11-08 15:33:14 +0100771 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100772}
773
Denis Karpov70a33412009-09-22 16:44:59 -0700774static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100775
776/*
777 * Configure the response type and send the cmd.
778 */
779static void
Denis Karpov70a33412009-09-22 16:44:59 -0700780omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781 struct mmc_data *data)
782{
783 int cmdreg = 0, resptype = 0, cmdtype = 0;
784
Venkatraman S8986d312012-08-07 19:10:38 +0530785 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
787 host->cmd = cmd;
788
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700789 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100790
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200791 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100792 if (cmd->flags & MMC_RSP_PRESENT) {
793 if (cmd->flags & MMC_RSP_136)
794 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200795 else if (cmd->flags & MMC_RSP_BUSY) {
796 resptype = 3;
797 host->response_busy = 1;
798 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100799 resptype = 2;
800 }
801
802 /*
803 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
804 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
805 * a val of 0x3, rest 0x0.
806 */
807 if (cmd == host->mrq->stop)
808 cmdtype = 0x3;
809
810 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
811
Balaji T Ka2e77152014-01-21 19:54:42 +0530812 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
813 host->mrq->sbc) {
814 cmdreg |= ACEN_ACMD23;
815 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
816 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100817 if (data) {
818 cmdreg |= DP_SELECT | MSBS | BCE;
819 if (data->flags & MMC_DATA_READ)
820 cmdreg |= DDIR;
821 else
822 cmdreg &= ~(DDIR);
823 }
824
825 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530826 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827
Adrian Hunterb4175772010-05-26 14:42:06 -0700828 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700829
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
831 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
832}
833
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200834static int
Denis Karpov70a33412009-09-22 16:44:59 -0700835omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200836{
837 if (data->flags & MMC_DATA_WRITE)
838 return DMA_TO_DEVICE;
839 else
840 return DMA_FROM_DEVICE;
841}
842
Russell Kingc5c98922012-04-13 12:14:39 +0100843static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
844 struct mmc_data *data)
845{
846 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
847}
848
Adrian Hunterb4175772010-05-26 14:42:06 -0700849static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
850{
851 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530852 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700853
Venkatraman S31463b12012-04-09 12:08:34 +0530854 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700855 host->req_in_progress = 0;
856 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530857 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700858
859 omap_hsmmc_disable_irq(host);
860 /* Do not complete the request if DMA is still in progress */
861 if (mrq->data && host->use_dma && dma_ch != -1)
862 return;
863 host->mrq = NULL;
864 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +1100865 pm_runtime_mark_last_busy(host->dev);
866 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -0700867}
868
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869/*
870 * Notify the transfer complete to MMC core
871 */
872static void
Denis Karpov70a33412009-09-22 16:44:59 -0700873omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100874{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200875 if (!data) {
876 struct mmc_request *mrq = host->mrq;
877
Adrian Hunter23050102009-09-22 16:44:57 -0700878 /* TC before CC from CMD6 - don't know why, but it happens */
879 if (host->cmd && host->cmd->opcode == 6 &&
880 host->response_busy) {
881 host->response_busy = 0;
882 return;
883 }
884
Adrian Hunterb4175772010-05-26 14:42:06 -0700885 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200886 return;
887 }
888
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889 host->data = NULL;
890
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891 if (!data->error)
892 data->bytes_xfered += data->blocks * (data->blksz);
893 else
894 data->bytes_xfered = 0;
895
Balaji T Kbf129e12014-01-21 19:54:42 +0530896 if (data->stop && (data->error || !host->mrq->sbc))
897 omap_hsmmc_start_command(host, data->stop, NULL);
898 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700899 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900}
901
902/*
903 * Notify the core about command completion
904 */
905static void
Denis Karpov70a33412009-09-22 16:44:59 -0700906omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907{
Balaji T Kbf129e12014-01-21 19:54:42 +0530908 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530909 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530910 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530911 omap_hsmmc_start_dma_transfer(host);
912 omap_hsmmc_start_command(host, host->mrq->cmd,
913 host->mrq->data);
914 return;
915 }
916
Balaji T K2177fa92014-05-09 22:16:52 +0530917 host->cmd = NULL;
918
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100919 if (cmd->flags & MMC_RSP_PRESENT) {
920 if (cmd->flags & MMC_RSP_136) {
921 /* response type 2 */
922 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
923 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
924 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
925 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
926 } else {
927 /* response types 1, 1b, 3, 4, 5, 6 */
928 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
929 }
930 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700931 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530932 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100933}
934
935/*
936 * DMA clean up for command errors
937 */
Denis Karpov70a33412009-09-22 16:44:59 -0700938static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939{
Adrian Hunterb4175772010-05-26 14:42:06 -0700940 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530941 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700942
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200943 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944
Venkatraman S31463b12012-04-09 12:08:34 +0530945 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700946 dma_ch = host->dma_ch;
947 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530948 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700949
950 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100951 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
952
953 dmaengine_terminate_all(chan);
954 dma_unmap_sg(chan->device->dev,
955 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700956 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100957
Per Forlin053bf342011-11-07 21:55:11 +0530958 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959 }
960 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961}
962
963/*
964 * Readable error output
965 */
966#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300967static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100968{
969 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700970 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300971 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
972 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
973 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
974 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100975 };
976 char res[256];
977 char *buf = res;
978 int len, i;
979
980 len = sprintf(buf, "MMC IRQ 0x%x :", status);
981 buf += len;
982
Denis Karpov70a33412009-09-22 16:44:59 -0700983 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700985 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 buf += len;
987 }
988
Venkatraman S8986d312012-08-07 19:10:38 +0530989 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100990}
Adrian Hunter699b9582011-05-06 12:14:01 +0300991#else
992static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
993 u32 status)
994{
995}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100996#endif /* CONFIG_MMC_DEBUG */
997
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100998/*
999 * MMC controller internal state machines reset
1000 *
1001 * Used to reset command or data internal state machines, using respectively
1002 * SRC or SRD bit of SYSCTL register
1003 * Can be called from interrupt context
1004 */
Denis Karpov70a33412009-09-22 16:44:59 -07001005static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1006 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001007{
1008 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301009 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001010
1011 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1012 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1013
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001014 /*
1015 * OMAP4 ES2 and greater has an updated reset logic.
1016 * Monitor a 0->1 transition first
1017 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001018 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001019 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001020 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301021 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001022 }
1023 i = 0;
1024
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001025 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1026 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301027 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001028
1029 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1030 dev_err(mmc_dev(host->mmc),
1031 "Timeout waiting on controller reset in %s\n",
1032 __func__);
1033}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001034
Balaji T K25e18972012-11-19 21:59:55 +05301035static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1036 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301037{
Balaji T K25e18972012-11-19 21:59:55 +05301038 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301039 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301040 if (host->cmd)
1041 host->cmd->error = err;
1042 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301043
1044 if (host->data) {
1045 omap_hsmmc_reset_controller_fsm(host, SRD);
1046 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301047 } else if (host->mrq && host->mrq->cmd)
1048 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301049}
1050
Adrian Hunterb4175772010-05-26 14:42:06 -07001051static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001052{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001054 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301055 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001056
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001057 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301058 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059
Venkatraman Sa7e96872012-11-19 22:00:01 +05301060 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001061 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001062
Venkatraman Sa7e96872012-11-19 22:00:01 +05301063 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301064 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301065 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301066 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301067 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301068 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1069
Balaji T Ka2e77152014-01-21 19:54:42 +05301070 if (status & ACE_EN) {
1071 u32 ac12;
1072 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1073 if (!(ac12 & ACNE) && host->mrq->sbc) {
1074 end_cmd = 1;
1075 if (ac12 & ACTO)
1076 error = -ETIMEDOUT;
1077 else if (ac12 & (ACCE | ACEB | ACIE))
1078 error = -EILSEQ;
1079 host->mrq->sbc->error = error;
1080 hsmmc_command_incomplete(host, error, end_cmd);
1081 }
1082 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1083 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301084 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301085 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301086 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087 }
1088 }
1089
Francesco Lavra7472bab2013-06-29 08:25:12 +02001090 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301091 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001092 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301093 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001094 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001095}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096
Adrian Hunterb4175772010-05-26 14:42:06 -07001097/*
1098 * MMC controller IRQ handler
1099 */
1100static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1101{
1102 struct omap_hsmmc_host *host = dev_id;
1103 int status;
1104
1105 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001106 while (status & (INT_EN_MASK | CIRQ_EN)) {
1107 if (host->req_in_progress)
1108 omap_hsmmc_do_irq(host, status);
1109
1110 if (status & CIRQ_EN)
1111 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301112
Adrian Hunterb4175772010-05-26 14:42:06 -07001113 /* Flush posted write */
1114 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301115 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117 return IRQ_HANDLED;
1118}
1119
Denis Karpov70a33412009-09-22 16:44:59 -07001120static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001121{
1122 unsigned long i;
1123
1124 OMAP_HSMMC_WRITE(host->base, HCTL,
1125 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1126 for (i = 0; i < loops_per_jiffy; i++) {
1127 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1128 break;
1129 cpu_relax();
1130 }
1131}
1132
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001133/*
David Brownelleb250822009-02-17 14:49:01 -08001134 * Switch MMC interface voltage ... only relevant for MMC1.
1135 *
1136 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1137 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1138 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001139 */
Denis Karpov70a33412009-09-22 16:44:59 -07001140static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001141{
1142 u32 reg_val = 0;
1143 int ret;
1144
1145 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301146 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301147 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301148 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001149
1150 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001151 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152
1153 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001154 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001155 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301156 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301157 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301158 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001159
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001160 if (ret != 0)
1161 goto err;
1162
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163 OMAP_HSMMC_WRITE(host->base, HCTL,
1164 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1165 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001166
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167 /*
1168 * If a MMC dual voltage card is detected, the set_ios fn calls
1169 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001170 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171 *
David Brownelleb250822009-02-17 14:49:01 -08001172 * Cope with a bit of slop in the range ... per data sheets:
1173 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1174 * but recommended values are 1.71V to 1.89V
1175 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1176 * but recommended values are 2.7V to 3.3V
1177 *
1178 * Board setup code shouldn't permit anything very out-of-range.
1179 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1180 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001181 */
David Brownelleb250822009-02-17 14:49:01 -08001182 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001184 else
1185 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001186
1187 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001188 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001189
1190 return 0;
1191err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301192 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001193 return ret;
1194}
1195
Adrian Hunterb62f6222009-09-22 16:45:01 -07001196/* Protect the card while the cover is open */
1197static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1198{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001199 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001200 return;
1201
1202 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001203 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001204 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301205 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001206 "card is now accessible\n",
1207 mmc_hostname(host->mmc));
1208 host->protect_card = 0;
1209 }
1210 } else {
1211 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301212 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001213 "card is now inaccessible\n",
1214 mmc_hostname(host->mmc));
1215 host->protect_card = 1;
1216 }
1217 }
1218}
1219
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001221 * irq handler when (cell-phone) cover is mounted/removed
1222 */
1223static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1224{
1225 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001226
1227 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1228
Andreas Fenkart11227d12015-03-03 13:28:17 +01001229 omap_hsmmc_protect_card(host);
1230 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001231 return IRQ_HANDLED;
1232}
1233
Russell Kingc5c98922012-04-13 12:14:39 +01001234static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001235{
Russell Kingc5c98922012-04-13 12:14:39 +01001236 struct omap_hsmmc_host *host = param;
1237 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001238 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001239 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001240
Russell Kingc5c98922012-04-13 12:14:39 +01001241 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001242 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001243 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001245 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246
Adrian Hunter770d7432011-05-06 12:14:11 +03001247 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001248 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001249 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001250 dma_unmap_sg(chan->device->dev,
1251 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001252 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001253
1254 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001255 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001256 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001257
1258 /* If DMA has finished after TC, complete the request */
1259 if (!req_in_progress) {
1260 struct mmc_request *mrq = host->mrq;
1261
1262 host->mrq = NULL;
1263 mmc_request_done(host->mmc, mrq);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001264 pm_runtime_mark_last_busy(host->dev);
1265 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001266 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267}
1268
Per Forlin9782aff2011-07-01 18:55:23 +02001269static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1270 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001271 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001272 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001273{
1274 int dma_len;
1275
1276 if (!next && data->host_cookie &&
1277 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301278 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001279 " host->next_data.cookie %d\n",
1280 __func__, data->host_cookie, host->next_data.cookie);
1281 data->host_cookie = 0;
1282 }
1283
1284 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001285 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001286 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001287 omap_hsmmc_get_dma_dir(host, data));
1288
1289 } else {
1290 dma_len = host->next_data.dma_len;
1291 host->next_data.dma_len = 0;
1292 }
1293
1294
1295 if (dma_len == 0)
1296 return -EINVAL;
1297
1298 if (next) {
1299 next->dma_len = dma_len;
1300 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1301 } else
1302 host->dma_len = dma_len;
1303
1304 return 0;
1305}
1306
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001307/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001308 * Routine to configure and start DMA for the MMC card
1309 */
Balaji T K9d025332014-01-21 19:54:42 +05301310static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001311 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001312{
Russell King26b88522012-04-13 12:27:37 +01001313 struct dma_slave_config cfg;
1314 struct dma_async_tx_descriptor *tx;
1315 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001316 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001317 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001318
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001319 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001320 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001321 struct scatterlist *sgl;
1322
1323 sgl = data->sg + i;
1324 if (sgl->length % data->blksz)
1325 return -EINVAL;
1326 }
1327 if ((data->blksz % 4) != 0)
1328 /* REVISIT: The MMC buffer increments only when MSB is written.
1329 * Return error for blksz which is non multiple of four.
1330 */
1331 return -EINVAL;
1332
Adrian Hunterb4175772010-05-26 14:42:06 -07001333 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001334
Russell Kingc5c98922012-04-13 12:14:39 +01001335 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001336
Russell King26b88522012-04-13 12:27:37 +01001337 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1338 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1339 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1340 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1341 cfg.src_maxburst = data->blksz / 4;
1342 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001343
Russell King26b88522012-04-13 12:27:37 +01001344 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001345 if (ret)
1346 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001347
Russell King26b88522012-04-13 12:27:37 +01001348 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1349 if (ret)
1350 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001351
Russell King26b88522012-04-13 12:27:37 +01001352 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1353 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1354 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1355 if (!tx) {
1356 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1357 /* FIXME: cleanup */
1358 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001359 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001360
Russell King26b88522012-04-13 12:27:37 +01001361 tx->callback = omap_hsmmc_dma_callback;
1362 tx->callback_param = host;
1363
1364 /* Does not fail */
1365 dmaengine_submit(tx);
1366
1367 host->dma_ch = 1;
1368
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001369 return 0;
1370}
1371
Denis Karpov70a33412009-09-22 16:44:59 -07001372static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001373 unsigned int timeout_ns,
1374 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001375{
1376 unsigned int timeout, cycle_ns;
1377 uint32_t reg, clkd, dto = 0;
1378
1379 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1380 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1381 if (clkd == 0)
1382 clkd = 1;
1383
Balaji T K6e3076c2014-01-21 19:54:42 +05301384 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001385 timeout = timeout_ns / cycle_ns;
1386 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001387 if (timeout) {
1388 while ((timeout & 0x80000000) == 0) {
1389 dto += 1;
1390 timeout <<= 1;
1391 }
1392 dto = 31 - dto;
1393 timeout <<= 1;
1394 if (timeout && dto)
1395 dto += 1;
1396 if (dto >= 13)
1397 dto -= 13;
1398 else
1399 dto = 0;
1400 if (dto > 14)
1401 dto = 14;
1402 }
1403
1404 reg &= ~DTO_MASK;
1405 reg |= dto << DTO_SHIFT;
1406 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1407}
1408
Balaji T K9d025332014-01-21 19:54:42 +05301409static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1410{
1411 struct mmc_request *req = host->mrq;
1412 struct dma_chan *chan;
1413
1414 if (!req->data)
1415 return;
1416 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1417 | (req->data->blocks << 16));
1418 set_data_timeout(host, req->data->timeout_ns,
1419 req->data->timeout_clks);
1420 chan = omap_hsmmc_get_dma_chan(host, req->data);
1421 dma_async_issue_pending(chan);
1422}
1423
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001424/*
1425 * Configure block length for MMC/SD cards and initiate the transfer.
1426 */
1427static int
Denis Karpov70a33412009-09-22 16:44:59 -07001428omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001429{
1430 int ret;
1431 host->data = req->data;
1432
1433 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001434 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001435 /*
1436 * Set an arbitrary 100ms data timeout for commands with
1437 * busy signal.
1438 */
1439 if (req->cmd->flags & MMC_RSP_BUSY)
1440 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001441 return 0;
1442 }
1443
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001444 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301445 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001446 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301447 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001448 return ret;
1449 }
1450 }
1451 return 0;
1452}
1453
Per Forlin9782aff2011-07-01 18:55:23 +02001454static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1455 int err)
1456{
1457 struct omap_hsmmc_host *host = mmc_priv(mmc);
1458 struct mmc_data *data = mrq->data;
1459
Russell King26b88522012-04-13 12:27:37 +01001460 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001461 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001462
Russell King26b88522012-04-13 12:27:37 +01001463 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1464 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001465 data->host_cookie = 0;
1466 }
1467}
1468
1469static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1470 bool is_first_req)
1471{
1472 struct omap_hsmmc_host *host = mmc_priv(mmc);
1473
1474 if (mrq->data->host_cookie) {
1475 mrq->data->host_cookie = 0;
1476 return ;
1477 }
1478
Russell Kingc5c98922012-04-13 12:14:39 +01001479 if (host->use_dma) {
1480 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001481
Per Forlin9782aff2011-07-01 18:55:23 +02001482 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001483 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001484 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001485 }
Per Forlin9782aff2011-07-01 18:55:23 +02001486}
1487
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001488/*
1489 * Request function. for read/write operation
1490 */
Denis Karpov70a33412009-09-22 16:44:59 -07001491static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001492{
Denis Karpov70a33412009-09-22 16:44:59 -07001493 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001494 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001495
Adrian Hunterb4175772010-05-26 14:42:06 -07001496 BUG_ON(host->req_in_progress);
1497 BUG_ON(host->dma_ch != -1);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001498 pm_runtime_get_sync(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001499 if (host->protect_card) {
1500 if (host->reqs_blocked < 3) {
1501 /*
1502 * Ensure the controller is left in a consistent
1503 * state by resetting the command and data state
1504 * machines.
1505 */
1506 omap_hsmmc_reset_controller_fsm(host, SRD);
1507 omap_hsmmc_reset_controller_fsm(host, SRC);
1508 host->reqs_blocked += 1;
1509 }
1510 req->cmd->error = -EBADF;
1511 if (req->data)
1512 req->data->error = -EBADF;
1513 req->cmd->retries = 0;
1514 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001515 pm_runtime_mark_last_busy(host->dev);
1516 pm_runtime_put_autosuspend(host->dev);
Adrian Hunterb4175772010-05-26 14:42:06 -07001517 return;
1518 } else if (host->reqs_blocked)
1519 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 WARN_ON(host->mrq != NULL);
1521 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301522 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001523 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001524 if (err) {
1525 req->cmd->error = err;
1526 if (req->data)
1527 req->data->error = err;
1528 host->mrq = NULL;
1529 mmc_request_done(mmc, req);
NeilBrownf57ba4c2015-03-26 12:18:23 +11001530 pm_runtime_mark_last_busy(host->dev);
1531 pm_runtime_put_autosuspend(host->dev);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001532 return;
1533 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301534 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301535 omap_hsmmc_start_command(host, req->sbc, NULL);
1536 return;
1537 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001538
Balaji T K9d025332014-01-21 19:54:42 +05301539 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001540 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001541}
1542
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001543/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001544static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001545{
Denis Karpov70a33412009-09-22 16:44:59 -07001546 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001547 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001548
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301549 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001550
Adrian Huntera3621462009-09-22 16:44:42 -07001551 if (ios->power_mode != host->power_mode) {
1552 switch (ios->power_mode) {
1553 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001554 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001555 break;
1556 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001557 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001558 break;
1559 case MMC_POWER_ON:
1560 do_send_init_stream = 1;
1561 break;
1562 }
1563 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001564 }
1565
Denis Karpovdd498ef2009-09-22 16:44:49 -07001566 /* FIXME: set registers based only on changes to ios */
1567
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001568 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301570 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001571 /* Only MMC1 can interface at 3V without some flavor
1572 * of external transceiver; but they all handle 1.8V.
1573 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001574 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301575 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001576 /*
1577 * The mmc_select_voltage fn of the core does
1578 * not seem to set the power_mode to
1579 * MMC_POWER_UP upon recalculating the voltage.
1580 * vdd 1.8v.
1581 */
Denis Karpov70a33412009-09-22 16:44:59 -07001582 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1583 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001584 "Switch operation failed\n");
1585 }
1586 }
1587
Andy Shevchenko5934df22011-05-06 12:14:06 +03001588 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001589
Adrian Huntera3621462009-09-22 16:44:42 -07001590 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001591 send_init_stream(host);
1592
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001593 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001594
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301595 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001596}
1597
1598static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1599{
Denis Karpov70a33412009-09-22 16:44:59 -07001600 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001601
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001602 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001604 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605}
1606
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001607static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1608{
1609 struct omap_hsmmc_host *host = mmc_priv(mmc);
1610
Andreas Fenkart326119c2014-11-08 15:33:14 +01001611 if (mmc_pdata(host)->init_card)
1612 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001613}
1614
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001615static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1616{
1617 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001618 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001619 unsigned long flags;
1620
1621 spin_lock_irqsave(&host->irq_lock, flags);
1622
Balaji T K5a52b082014-05-29 10:28:02 +02001623 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001624 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1625 if (enable) {
1626 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1627 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001628 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001629 } else {
1630 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1631 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001632 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001633 }
Balaji T K5a52b082014-05-29 10:28:02 +02001634 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001635 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1636
1637 /*
1638 * if enable, piggy back detection on current request
1639 * but always disable immediately
1640 */
1641 if (!host->req_in_progress || !enable)
1642 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1643
1644 /* flush posted write */
1645 OMAP_HSMMC_READ(host->base, IE);
1646
1647 spin_unlock_irqrestore(&host->irq_lock, flags);
1648}
1649
1650static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1651{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001652 int ret;
1653
1654 /*
1655 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1656 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1657 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1658 * with functional clock disabled.
1659 */
1660 if (!host->dev->of_node || !host->wake_irq)
1661 return -ENODEV;
1662
Tony Lindgren5b83b222015-05-21 15:51:52 -07001663 ret = dev_pm_set_dedicated_wake_irq(host->dev, host->wake_irq);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001664 if (ret) {
1665 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1666 goto err;
1667 }
1668
1669 /*
1670 * Some omaps don't have wake-up path from deeper idle states
1671 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1672 */
1673 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001674 struct pinctrl *p = devm_pinctrl_get(host->dev);
1675 if (!p) {
1676 ret = -ENODEV;
1677 goto err_free_irq;
1678 }
1679 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1680 dev_info(host->dev, "missing default pinctrl state\n");
1681 devm_pinctrl_put(p);
1682 ret = -EINVAL;
1683 goto err_free_irq;
1684 }
1685
1686 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1687 dev_info(host->dev, "missing idle pinctrl state\n");
1688 devm_pinctrl_put(p);
1689 ret = -EINVAL;
1690 goto err_free_irq;
1691 }
1692 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001693 }
1694
Balaji T K5a52b082014-05-29 10:28:02 +02001695 OMAP_HSMMC_WRITE(host->base, HCTL,
1696 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001697 return 0;
1698
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001699err_free_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07001700 dev_pm_clear_wake_irq(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001701err:
1702 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1703 host->wake_irq = 0;
1704 return ret;
1705}
1706
Denis Karpov70a33412009-09-22 16:44:59 -07001707static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001708{
1709 u32 hctl, capa, value;
1710
1711 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301712 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001713 hctl = SDVS30;
1714 capa = VS30 | VS18;
1715 } else {
1716 hctl = SDVS18;
1717 capa = VS18;
1718 }
1719
1720 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1721 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1722
1723 value = OMAP_HSMMC_READ(host->base, CAPA);
1724 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1725
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001726 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001727 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001728}
1729
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001730static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1731 unsigned int direction, int blk_size)
1732{
1733 /* This controller can't do multiblock reads due to hw bugs */
1734 if (direction == MMC_DATA_READ)
1735 return 1;
1736
1737 return blk_size;
1738}
1739
1740static struct mmc_host_ops omap_hsmmc_ops = {
Per Forlin9782aff2011-07-01 18:55:23 +02001741 .post_req = omap_hsmmc_post_req,
1742 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001743 .request = omap_hsmmc_request,
1744 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001745 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001746 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001747 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001748 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001749};
1750
Denis Karpovd900f712009-09-22 16:44:38 -07001751#ifdef CONFIG_DEBUG_FS
1752
Denis Karpov70a33412009-09-22 16:44:59 -07001753static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001754{
1755 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001756 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001757
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001758 seq_printf(s, "mmc%d:\n", mmc->index);
1759 seq_printf(s, "sdio irq mode\t%s\n",
1760 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1761
1762 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1763 seq_printf(s, "sdio irq \t%s\n",
1764 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1765 : "disabled");
1766 }
1767 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001768
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301769 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001770 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001771 seq_printf(s, "CON:\t\t0x%08x\n",
1772 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001773 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1774 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001775 seq_printf(s, "HCTL:\t\t0x%08x\n",
1776 OMAP_HSMMC_READ(host->base, HCTL));
1777 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1778 OMAP_HSMMC_READ(host->base, SYSCTL));
1779 seq_printf(s, "IE:\t\t0x%08x\n",
1780 OMAP_HSMMC_READ(host->base, IE));
1781 seq_printf(s, "ISE:\t\t0x%08x\n",
1782 OMAP_HSMMC_READ(host->base, ISE));
1783 seq_printf(s, "CAPA:\t\t0x%08x\n",
1784 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001785
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301786 pm_runtime_mark_last_busy(host->dev);
1787 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001788
Denis Karpovd900f712009-09-22 16:44:38 -07001789 return 0;
1790}
1791
Denis Karpov70a33412009-09-22 16:44:59 -07001792static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001793{
Denis Karpov70a33412009-09-22 16:44:59 -07001794 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001795}
1796
1797static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001798 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001799 .read = seq_read,
1800 .llseek = seq_lseek,
1801 .release = single_release,
1802};
1803
Denis Karpov70a33412009-09-22 16:44:59 -07001804static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001805{
1806 if (mmc->debugfs_root)
1807 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1808 mmc, &mmc_regs_fops);
1809}
1810
1811#else
1812
Denis Karpov70a33412009-09-22 16:44:59 -07001813static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001814{
1815}
1816
1817#endif
1818
Rajendra Nayak46856a62012-03-12 20:32:37 +05301819#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001820static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1821 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1822 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1823};
1824
1825static const struct omap_mmc_of_data omap4_mmc_of_data = {
1826 .reg_offset = 0x100,
1827};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001828static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1829 .reg_offset = 0x100,
1830 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1831};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301832
1833static const struct of_device_id omap_mmc_of_match[] = {
1834 {
1835 .compatible = "ti,omap2-hsmmc",
1836 },
1837 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001838 .compatible = "ti,omap3-pre-es3-hsmmc",
1839 .data = &omap3_pre_es3_mmc_of_data,
1840 },
1841 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301842 .compatible = "ti,omap3-hsmmc",
1843 },
1844 {
1845 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001846 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301847 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001848 {
1849 .compatible = "ti,am33xx-hsmmc",
1850 .data = &am33xx_mmc_of_data,
1851 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301852 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001853};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301854MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1855
Andreas Fenkart55143432014-11-08 15:33:09 +01001856static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301857{
Andreas Fenkart55143432014-11-08 15:33:09 +01001858 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301859 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301860
1861 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1862 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301863 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301864
1865 if (of_find_property(np, "ti,dual-volt", NULL))
1866 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1867
Andreas Fenkartb7a56462015-03-20 15:53:54 +01001868 pdata->gpio_cd = -EINVAL;
1869 pdata->gpio_cod = -EINVAL;
NeilBrownfdb9de12015-01-13 08:23:18 +13001870 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301871
1872 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001873 pdata->nonremovable = true;
1874 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301875 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301876
1877 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001878 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301879
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301880 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001881 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301882
Rajendra Nayak46856a62012-03-12 20:32:37 +05301883 return pdata;
1884}
1885#else
Andreas Fenkart55143432014-11-08 15:33:09 +01001886static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301887 *of_get_hsmmc_pdata(struct device *dev)
1888{
Balaji T K19df45b2014-02-28 19:08:18 +05301889 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301890}
1891#endif
1892
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001893static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001894{
Andreas Fenkart55143432014-11-08 15:33:09 +01001895 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001896 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001897 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001898 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001899 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301900 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001901 dma_cap_mask_t mask;
1902 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06001903 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301904 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301905
1906 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1907 if (match) {
1908 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001909
1910 if (IS_ERR(pdata))
1911 return PTR_ERR(pdata);
1912
Rajendra Nayak46856a62012-03-12 20:32:37 +05301913 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001914 data = match->data;
1915 pdata->reg_offset = data->reg_offset;
1916 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301917 }
1918 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001919
1920 if (pdata == NULL) {
1921 dev_err(&pdev->dev, "Platform Data is missing\n");
1922 return -ENXIO;
1923 }
1924
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001925 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1926 irq = platform_get_irq(pdev, 0);
1927 if (res == NULL || irq < 0)
1928 return -ENXIO;
1929
Balaji T K77fae212014-05-09 22:16:51 +05301930 base = devm_ioremap_resource(&pdev->dev, res);
1931 if (IS_ERR(base))
1932 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001933
Denis Karpov70a33412009-09-22 16:44:59 -07001934 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001935 if (!mmc) {
1936 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001937 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001938 }
1939
NeilBrownfdb9de12015-01-13 08:23:18 +13001940 ret = mmc_of_parse(mmc);
1941 if (ret)
1942 goto err1;
1943
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001944 host = mmc_priv(mmc);
1945 host->mmc = mmc;
1946 host->pdata = pdata;
1947 host->dev = &pdev->dev;
1948 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001949 host->dma_ch = -1;
1950 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05301951 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05301952 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08001953 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02001954 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05301955 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001956
NeilBrown41afa3142015-01-13 08:23:18 +13001957 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01001958 if (ret)
1959 goto err_gpio;
1960
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001962
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001963 if (pdev->dev.of_node)
1964 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
1965
Balaji T K7a8c2ce2011-07-01 22:09:34 +05301966 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001967
Daniel Mackd418ed82012-02-19 13:20:33 +01001968 mmc->f_min = OMAP_MMC_MIN_CLOCK;
1969
1970 if (pdata->max_freq > 0)
1971 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13001972 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01001973 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001974
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001975 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001976
Balaji T K96181952014-05-09 22:16:48 +05301977 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001978 if (IS_ERR(host->fclk)) {
1979 ret = PTR_ERR(host->fclk);
1980 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001981 goto err1;
1982 }
1983
Paul Walmsley9b682562011-10-06 14:50:35 -06001984 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
1985 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 -07001986 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06001987 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001988
Tony Lindgren5b83b222015-05-21 15:51:52 -07001989 device_init_wakeup(&pdev->dev, true);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301990 pm_runtime_enable(host->dev);
1991 pm_runtime_get_sync(host->dev);
1992 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
1993 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001994
Balaji T K92a3aeb2012-02-24 21:14:34 +05301995 omap_hsmmc_context_save(host);
1996
Balaji T K96181952014-05-09 22:16:48 +05301997 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301998 /*
1999 * MMC can still work without debounce clock.
2000 */
2001 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302002 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302003 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302004 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302005 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002006 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002007
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002008 /* Since we do only SG emulation, we can have as many segs
2009 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002010 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002011
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002012 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2013 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2014 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2015 mmc->max_seg_size = mmc->max_req_size;
2016
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002017 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002018 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002019
Andreas Fenkart326119c2014-11-08 15:33:14 +01002020 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002021 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022 mmc->caps |= MMC_CAP_4_BIT_DATA;
2023
Andreas Fenkart326119c2014-11-08 15:33:14 +01002024 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002025 mmc->caps |= MMC_CAP_NONREMOVABLE;
2026
NeilBrownfdb9de12015-01-13 08:23:18 +13002027 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002028
Denis Karpov70a33412009-09-22 16:44:59 -07002029 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002030
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302031 if (!pdev->dev.of_node) {
2032 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2033 if (!res) {
2034 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2035 ret = -ENXIO;
2036 goto err_irq;
2037 }
2038 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002039
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302040 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2041 if (!res) {
2042 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2043 ret = -ENXIO;
2044 goto err_irq;
2045 }
2046 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002047 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048
Russell King26b88522012-04-13 12:27:37 +01002049 dma_cap_zero(mask);
2050 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002051
Matt Porterd272fbf2013-05-10 17:42:34 +05302052 host->rx_chan =
2053 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2054 &rx_req, &pdev->dev, "rx");
2055
Russell King26b88522012-04-13 12:27:37 +01002056 if (!host->rx_chan) {
2057 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002058 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002059 goto err_irq;
2060 }
2061
Matt Porterd272fbf2013-05-10 17:42:34 +05302062 host->tx_chan =
2063 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2064 &tx_req, &pdev->dev, "tx");
2065
Russell King26b88522012-04-13 12:27:37 +01002066 if (!host->tx_chan) {
2067 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002068 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002069 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002070 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002071
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002072 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302073 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074 mmc_hostname(mmc), host);
2075 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302076 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077 goto err_irq;
2078 }
2079
Andreas Fenkart326119c2014-11-08 15:33:14 +01002080 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002081 ret = omap_hsmmc_reg_get(host);
2082 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002083 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002084 host->use_reg = 1;
2085 }
2086
Andreas Fenkart326119c2014-11-08 15:33:14 +01002087 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088
Adrian Hunterb4175772010-05-26 14:42:06 -07002089 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002090
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002091 /*
2092 * For now, only support SDIO interrupt if we have a separate
2093 * wake-up interrupt configured from device tree. This is because
2094 * the wake-up interrupt is needed for idle state and some
2095 * platforms need special quirks. And we don't want to add new
2096 * legacy mux platform init code callbacks any longer as we
2097 * are moving to DT based booting anyways.
2098 */
2099 ret = omap_hsmmc_configure_wake_irq(host);
2100 if (!ret)
2101 mmc->caps |= MMC_CAP_SDIO_IRQ;
2102
Adrian Hunterb62f6222009-09-22 16:45:01 -07002103 omap_hsmmc_protect_card(host);
2104
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002105 mmc_add_host(mmc);
2106
Andreas Fenkart326119c2014-11-08 15:33:14 +01002107 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2109 if (ret < 0)
2110 goto err_slot_name;
2111 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002112 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002114 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002116 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002117 }
2118
Denis Karpov70a33412009-09-22 16:44:59 -07002119 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302120 pm_runtime_mark_last_busy(host->dev);
2121 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002122
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002123 return 0;
2124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002125err_slot_name:
2126 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002127 if (host->use_reg)
2128 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002129err_irq:
Tony Lindgren5b83b222015-05-21 15:51:52 -07002130 device_init_wakeup(&pdev->dev, false);
Russell Kingc5c98922012-04-13 12:14:39 +01002131 if (host->tx_chan)
2132 dma_release_channel(host->tx_chan);
2133 if (host->rx_chan)
2134 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302135 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002136 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302137 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302138 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002139err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002140err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002141 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002142err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143 return ret;
2144}
2145
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002146static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002147{
Denis Karpov70a33412009-09-22 16:44:59 -07002148 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002149
Felipe Balbi927ce942012-03-14 11:18:27 +02002150 pm_runtime_get_sync(host->dev);
2151 mmc_remove_host(host->mmc);
2152 if (host->use_reg)
2153 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002154
Russell Kingc5c98922012-04-13 12:14:39 +01002155 if (host->tx_chan)
2156 dma_release_channel(host->tx_chan);
2157 if (host->rx_chan)
2158 dma_release_channel(host->rx_chan);
2159
Felipe Balbi927ce942012-03-14 11:18:27 +02002160 pm_runtime_put_sync(host->dev);
2161 pm_runtime_disable(host->dev);
Tony Lindgren5b83b222015-05-21 15:51:52 -07002162 device_init_wakeup(&pdev->dev, false);
Balaji T K96181952014-05-09 22:16:48 +05302163 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302164 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002165
Balaji T K9d1f0282012-10-15 21:35:07 +05302166 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002167
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002168 return 0;
2169}
2170
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002171#ifdef CONFIG_PM_SLEEP
Kevin Hilmana791daa2010-05-26 14:42:07 -07002172static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002173{
Felipe Balbi927ce942012-03-14 11:18:27 +02002174 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2175
2176 if (!host)
2177 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002178
Felipe Balbi927ce942012-03-14 11:18:27 +02002179 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002180
2181 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002182 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2183 OMAP_HSMMC_WRITE(host->base, IE, 0);
2184 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002185 OMAP_HSMMC_WRITE(host->base, HCTL,
2186 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2187 }
2188
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302189 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302190 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002191
Eliad Peller31f9d462011-11-22 16:02:17 +02002192 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002193 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002194}
2195
2196/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002197static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002198{
Felipe Balbi927ce942012-03-14 11:18:27 +02002199 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2200
2201 if (!host)
2202 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002203
Felipe Balbi927ce942012-03-14 11:18:27 +02002204 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002205
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302206 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302207 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002208
Felipe Balbi927ce942012-03-14 11:18:27 +02002209 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2210 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002211
Felipe Balbi927ce942012-03-14 11:18:27 +02002212 omap_hsmmc_protect_card(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002213 pm_runtime_mark_last_busy(host->dev);
2214 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002215 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002216}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002217#endif
2218
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302219static int omap_hsmmc_runtime_suspend(struct device *dev)
2220{
2221 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002222 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002223 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302224
2225 host = platform_get_drvdata(to_platform_device(dev));
2226 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002227 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302228
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002229 spin_lock_irqsave(&host->irq_lock, flags);
2230 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2231 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2232 /* disable sdio irq handling to prevent race */
2233 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2234 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002235
2236 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2237 /*
2238 * dat1 line low, pending sdio irq
2239 * race condition: possible irq handler running on
2240 * multi-core, abort
2241 */
2242 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2243 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2244 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2245 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2246 pm_runtime_mark_last_busy(dev);
2247 ret = -EBUSY;
2248 goto abort;
2249 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002250
Andreas Fenkart97978a42014-05-29 10:28:04 +02002251 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002252 } else {
2253 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002254 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002255
Andreas Fenkartf9459012014-05-29 10:28:03 +02002256abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002257 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002258 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302259}
2260
2261static int omap_hsmmc_runtime_resume(struct device *dev)
2262{
2263 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002264 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302265
2266 host = platform_get_drvdata(to_platform_device(dev));
2267 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002268 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302269
Andreas Fenkart2cd3a2a2014-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)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002273
Andreas Fenkart97978a42014-05-29 10:28:04 +02002274 pinctrl_pm_select_default_state(host->dev);
2275
2276 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002277 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2278 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2279 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002280 } else {
2281 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002282 }
2283 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302284 return 0;
2285}
2286
Kevin Hilmana791daa2010-05-26 14:42:07 -07002287static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Russ Dill3d3bbfb2015-02-27 13:24:34 +02002288 SET_SYSTEM_SLEEP_PM_OPS(omap_hsmmc_suspend, omap_hsmmc_resume)
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302289 .runtime_suspend = omap_hsmmc_runtime_suspend,
2290 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002291};
2292
2293static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002294 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002295 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296 .driver = {
2297 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002298 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302299 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002300 },
2301};
2302
Felipe Balbib7964502012-03-14 11:18:32 +02002303module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002304MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2305MODULE_LICENSE("GPL");
2306MODULE_ALIAS("platform:" DRIVER_NAME);
2307MODULE_AUTHOR("Texas Instruments Inc");