blob: ae72fda171b403afbc6080ed44e5b16f108b11af [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
NeilBrown41afa3142015-01-13 08:23:18 +130039#include <linux/mmc/slot-gpio.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010040#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020041#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053044#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053045#include <linux/pm_runtime.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010046#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010047
48/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070049#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010050#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053051#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010052#define OMAP_HSMMC_BLK 0x0104
53#define OMAP_HSMMC_ARG 0x0108
54#define OMAP_HSMMC_CMD 0x010C
55#define OMAP_HSMMC_RSP10 0x0110
56#define OMAP_HSMMC_RSP32 0x0114
57#define OMAP_HSMMC_RSP54 0x0118
58#define OMAP_HSMMC_RSP76 0x011C
59#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020060#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010061#define OMAP_HSMMC_HCTL 0x0128
62#define OMAP_HSMMC_SYSCTL 0x012C
63#define OMAP_HSMMC_STAT 0x0130
64#define OMAP_HSMMC_IE 0x0134
65#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053066#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define OMAP_HSMMC_CAPA 0x0140
68
69#define VS18 (1 << 26)
70#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053071#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010072#define SDVS18 (0x5 << 9)
73#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080074#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010075#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010076#define SDVSCLR 0xFFFFF1FF
77#define SDVSDET 0x00000400
78#define AUTOIDLE 0x1
79#define SDBP (1 << 8)
80#define DTO 0xe
81#define ICE 0x1
82#define ICS 0x2
83#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053084#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010085#define CLKD_MASK 0x0000FFC0
86#define CLKD_SHIFT 6
87#define DTO_MASK 0x000F0000
88#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053090#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define DP_SELECT (1 << 21)
92#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053093#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010094#define MSBS (1 << 5)
95#define BCE (1 << 1)
96#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053097#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020098#define IWE (1 << 24)
Balaji T K03b5d922012-04-09 12:08:33 +053099#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200100#define CLKEXTFREE (1 << 16)
101#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200102#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define STAT_CLEAR 0xFFFFFFFF
105#define INIT_STREAM_CMD 0x00000000
106#define DUAL_VOLT_OCR_BIT 7
107#define SRC (1 << 25)
108#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700109#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100110
Andreas Fenkartf9459012014-05-29 10:28:03 +0200111/* PSTATE */
112#define DLEV_DAT(x) (1 << (20 + (x)))
113
Venkatraman Sa7e96872012-11-19 22:00:01 +0530114/* Interrupt masks for IE and ISE register */
115#define CC_EN (1 << 0)
116#define TC_EN (1 << 1)
117#define BWR_EN (1 << 4)
118#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200119#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530120#define ERR_EN (1 << 15)
121#define CTO_EN (1 << 16)
122#define CCRC_EN (1 << 17)
123#define CEB_EN (1 << 18)
124#define CIE_EN (1 << 19)
125#define DTO_EN (1 << 20)
126#define DCRC_EN (1 << 21)
127#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530128#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530129#define CERR_EN (1 << 28)
130#define BADA_EN (1 << 29)
131
Balaji T Ka2e77152014-01-21 19:54:42 +0530132#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530133 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
134 BRR_EN | BWR_EN | TC_EN | CC_EN)
135
Balaji T Ka2e77152014-01-21 19:54:42 +0530136#define CNI (1 << 7)
137#define ACIE (1 << 4)
138#define ACEB (1 << 3)
139#define ACCE (1 << 2)
140#define ACTO (1 << 1)
141#define ACNE (1 << 0)
142
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530143#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530144#define MMC_TIMEOUT_MS 20 /* 20 mSec */
145#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400146#define OMAP_MMC_MIN_CLOCK 400000
147#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530148#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149
Balaji T Ke99448f2014-02-19 20:26:40 +0530150#define VDD_1V8 1800000 /* 180000 uV */
151#define VDD_3V0 3000000 /* 300000 uV */
152#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
153
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154/*
155 * One controller can have multiple slots, like on some omap boards using
156 * omap.c controller driver. Luckily this is not currently done on any known
157 * omap_hsmmc.c device.
158 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100159#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160
161/*
162 * MMC Host controller read/write API's
163 */
164#define OMAP_HSMMC_READ(base, reg) \
165 __raw_readl((base) + OMAP_HSMMC_##reg)
166
167#define OMAP_HSMMC_WRITE(base, reg, val) \
168 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
169
Per Forlin9782aff2011-07-01 18:55:23 +0200170struct omap_hsmmc_next {
171 unsigned int dma_len;
172 s32 cookie;
173};
174
Denis Karpov70a33412009-09-22 16:44:59 -0700175struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 struct device *dev;
177 struct mmc_host *mmc;
178 struct mmc_request *mrq;
179 struct mmc_command *cmd;
180 struct mmc_data *data;
181 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100182 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183 /*
184 * vcc == configured supply
185 * vcc_aux == optional
186 * - MMC1, supply for DAT4..DAT7
187 * - MMC2/MMC2, external level shifter voltage supply, for
188 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
189 */
190 struct regulator *vcc;
191 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530192 struct regulator *pbias;
193 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 void __iomem *base;
195 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700196 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200198 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100199 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700200 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100201 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530202 u32 con;
203 u32 hctl;
204 u32 sysctl;
205 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100206 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200207 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100208 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100209 struct dma_chan *tx_chan;
210 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200211 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700212 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700213 int protect_card;
214 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800215 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700216 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530217 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530218 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200219#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
220#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
221#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200222 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100223 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100224
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100225 /* return MMC cover switch state, can be NULL if not supported.
226 *
227 * possible return values:
228 * 0 - closed
229 * 1 - open
230 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100231 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100232
233 /* Card detection IRQs */
234 int card_detect_irq;
235
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100236 int (*card_detect)(struct device *dev);
237 int (*get_ro)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100238
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100239};
240
Nishanth Menon59445b12014-02-13 23:45:48 -0600241struct omap_mmc_of_data {
242 u32 reg_offset;
243 u8 controller_flags;
244};
245
Balaji T Kbf129e12014-01-21 19:54:42 +0530246static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
247
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100248static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800249{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530250 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800251
NeilBrown41afa3142015-01-13 08:23:18 +1300252 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800253}
254
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100255static int omap_hsmmc_get_wp(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800256{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530257 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258
NeilBrown41afa3142015-01-13 08:23:18 +1300259 return mmc_gpio_get_ro(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800260}
261
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100262static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800263{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530264 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800265
NeilBrown41afa3142015-01-13 08:23:18 +1300266 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800267}
268
Adrian Hunterb702b102010-02-15 10:03:35 -0800269#ifdef CONFIG_REGULATOR
270
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100271static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800272{
273 struct omap_hsmmc_host *host =
274 platform_get_drvdata(to_platform_device(dev));
275 int ret = 0;
276
277 /*
278 * If we don't see a Vcc regulator, assume it's a fixed
279 * voltage always-on regulator.
280 */
281 if (!host->vcc)
282 return 0;
283
Andreas Fenkart326119c2014-11-08 15:33:14 +0100284 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100285 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800286
Balaji T Ke99448f2014-02-19 20:26:40 +0530287 if (host->pbias) {
288 if (host->pbias_enabled == 1) {
289 ret = regulator_disable(host->pbias);
290 if (!ret)
291 host->pbias_enabled = 0;
292 }
293 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
294 }
295
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800296 /*
297 * Assume Vcc regulator is used only to power the card ... OMAP
298 * VDDS is used to power the pins, optionally with a transceiver to
299 * support cards using voltages other than VDDS (1.8V nominal). When a
300 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
301 *
302 * In some cases this regulator won't support enable/disable;
303 * e.g. it's a fixed rail for a WLAN chip.
304 *
305 * In other cases vcc_aux switches interface power. Example, for
306 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
307 * chips/cards need an interface voltage rail too.
308 */
309 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530310 if (host->vcc)
311 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800312 /* Enable interface voltage rail, if needed */
313 if (ret == 0 && host->vcc_aux) {
314 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530315 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400316 ret = mmc_regulator_set_ocr(host->mmc,
317 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800318 }
319 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400320 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800321 if (host->vcc_aux)
322 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530323 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400324 /* Then proceed to shut down the local regulator */
325 ret = mmc_regulator_set_ocr(host->mmc,
326 host->vcc, 0);
327 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800328 }
329
Balaji T Ke99448f2014-02-19 20:26:40 +0530330 if (host->pbias) {
331 if (vdd <= VDD_165_195)
332 ret = regulator_set_voltage(host->pbias, VDD_1V8,
333 VDD_1V8);
334 else
335 ret = regulator_set_voltage(host->pbias, VDD_3V0,
336 VDD_3V0);
337 if (ret < 0)
338 goto error_set_power;
339
340 if (host->pbias_enabled == 0) {
341 ret = regulator_enable(host->pbias);
342 if (!ret)
343 host->pbias_enabled = 1;
344 }
345 }
346
Andreas Fenkart326119c2014-11-08 15:33:14 +0100347 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100348 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800349
Balaji T Ke99448f2014-02-19 20:26:40 +0530350error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800351 return ret;
352}
353
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800354static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
355{
356 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700357 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800358
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530359 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800360 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530361 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
362 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400363 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800364 } else {
365 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700366 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100367 if (!mmc_pdata(host)->ocr_mask) {
368 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700369 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100370 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530371 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100372 mmc_pdata(host)->ocr_mask);
373 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700374 return -EINVAL;
375 }
376 }
Balaji T K987fd492014-02-19 20:26:40 +0530377 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100378 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800379
Balaji T K987fd492014-02-19 20:26:40 +0530380 /* Allow an aux regulator */
381 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
382 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800383
Balaji T Ke99448f2014-02-19 20:26:40 +0530384 reg = devm_regulator_get_optional(host->dev, "pbias");
385 host->pbias = IS_ERR(reg) ? NULL : reg;
386
Balaji T K987fd492014-02-19 20:26:40 +0530387 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100388 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530389 return 0;
390 /*
391 * To disable boot_on regulator, enable regulator
392 * to increase usecount and then disable it.
393 */
394 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
395 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100396 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300397
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100398 mmc_pdata(host)->set_power(host->dev, 1, vdd);
399 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800400 }
401
402 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800403}
404
405static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
406{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100407 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800408}
409
Adrian Hunterb702b102010-02-15 10:03:35 -0800410static inline int omap_hsmmc_have_reg(void)
411{
412 return 1;
413}
414
415#else
416
417static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
418{
419 return -EINVAL;
420}
421
422static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
423{
424}
425
426static inline int omap_hsmmc_have_reg(void)
427{
428 return 0;
429}
430
431#endif
432
NeilBrown41afa3142015-01-13 08:23:18 +1300433static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id);
434
435static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
436 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100437 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800438{
439 int ret;
440
Andreas Fenkart326119c2014-11-08 15:33:14 +0100441 if (gpio_is_valid(pdata->switch_pin)) {
442 if (pdata->cover)
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100443 host->get_cover_state =
444 omap_hsmmc_get_cover_state;
Adrian Hunterb702b102010-02-15 10:03:35 -0800445 else
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100446 host->card_detect = omap_hsmmc_card_detect;
447 host->card_detect_irq =
Andreas Fenkart326119c2014-11-08 15:33:14 +0100448 gpio_to_irq(pdata->switch_pin);
NeilBrown41afa3142015-01-13 08:23:18 +1300449 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_detect);
450 ret = mmc_gpio_request_cd(mmc, pdata->switch_pin, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800451 if (ret)
452 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100453 } else {
454 pdata->switch_pin = -EINVAL;
455 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800456
Andreas Fenkart326119c2014-11-08 15:33:14 +0100457 if (gpio_is_valid(pdata->gpio_wp)) {
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100458 host->get_ro = omap_hsmmc_get_wp;
NeilBrown41afa3142015-01-13 08:23:18 +1300459 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800460 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300461 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100462 } else {
463 pdata->gpio_wp = -EINVAL;
464 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800465
466 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800467}
468
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100469/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300470 * Start clock to the card
471 */
472static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
473{
474 OMAP_HSMMC_WRITE(host->base, SYSCTL,
475 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
476}
477
478/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100479 * Stop clock to the card
480 */
Denis Karpov70a33412009-09-22 16:44:59 -0700481static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100482{
483 OMAP_HSMMC_WRITE(host->base, SYSCTL,
484 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
485 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900486 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100487}
488
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700489static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
490 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700491{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200492 u32 irq_mask = INT_EN_MASK;
493 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700494
495 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200496 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700497
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700498 /* Disable timeout for erases */
499 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530500 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700501
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200502 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700503 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
504 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200505
506 /* latch pending CIRQ, but don't signal MMC core */
507 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
508 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700509 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200510 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700511}
512
513static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
514{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200515 u32 irq_mask = 0;
516 unsigned long flags;
517
518 spin_lock_irqsave(&host->irq_lock, flags);
519 /* no transfer running but need to keep cirq if enabled */
520 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
521 irq_mask |= CIRQ_EN;
522 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
523 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700524 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200525 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700526}
527
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300528/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530529static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300530{
531 u16 dsor = 0;
532
533 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530534 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530535 if (dsor > CLKD_MAX)
536 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300537 }
538
539 return dsor;
540}
541
Andy Shevchenko5934df22011-05-06 12:14:06 +0300542static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
543{
544 struct mmc_ios *ios = &host->mmc->ios;
545 unsigned long regval;
546 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530547 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300548
Venkatraman S8986d312012-08-07 19:10:38 +0530549 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300550
551 omap_hsmmc_stop_clock(host);
552
553 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
554 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530555 clkdiv = calc_divisor(host, ios);
556 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300557 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
558 OMAP_HSMMC_WRITE(host->base, SYSCTL,
559 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
560
561 /* Wait till the ICS bit is set */
562 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
563 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
564 && time_before(jiffies, timeout))
565 cpu_relax();
566
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530567 /*
568 * Enable High-Speed Support
569 * Pre-Requisites
570 * - Controller should support High-Speed-Enable Bit
571 * - Controller should not be using DDR Mode
572 * - Controller should advertise that it supports High Speed
573 * in capabilities register
574 * - MMC/SD clock coming out of controller > 25MHz
575 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100576 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900577 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100578 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530579 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
580 regval = OMAP_HSMMC_READ(host->base, HCTL);
581 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
582 regval |= HSPE;
583 else
584 regval &= ~HSPE;
585
586 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
587 }
588
Andy Shevchenko5934df22011-05-06 12:14:06 +0300589 omap_hsmmc_start_clock(host);
590}
591
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400592static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
593{
594 struct mmc_ios *ios = &host->mmc->ios;
595 u32 con;
596
597 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100598 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
599 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530600 con |= DDR; /* configure in DDR mode */
601 else
602 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400603 switch (ios->bus_width) {
604 case MMC_BUS_WIDTH_8:
605 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
606 break;
607 case MMC_BUS_WIDTH_4:
608 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
609 OMAP_HSMMC_WRITE(host->base, HCTL,
610 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
611 break;
612 case MMC_BUS_WIDTH_1:
613 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
614 OMAP_HSMMC_WRITE(host->base, HCTL,
615 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
616 break;
617 }
618}
619
620static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
621{
622 struct mmc_ios *ios = &host->mmc->ios;
623 u32 con;
624
625 con = OMAP_HSMMC_READ(host->base, CON);
626 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
627 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
628 else
629 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
630}
631
Denis Karpov11dd62a2009-09-22 16:44:43 -0700632#ifdef CONFIG_PM
633
634/*
635 * Restore the MMC host context, if it was lost as result of a
636 * power state change.
637 */
Denis Karpov70a33412009-09-22 16:44:59 -0700638static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700639{
640 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400641 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700642 unsigned long timeout;
643
Tony Lindgren0a82e062013-10-21 00:25:19 +0530644 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
645 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
646 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
647 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
648 return 0;
649
650 host->context_loss++;
651
Balaji T Kc2200ef2012-03-07 09:55:30 -0500652 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700653 if (host->power_mode != MMC_POWER_OFF &&
654 (1 << ios->vdd) <= MMC_VDD_23_24)
655 hctl = SDVS18;
656 else
657 hctl = SDVS30;
658 capa = VS30 | VS18;
659 } else {
660 hctl = SDVS18;
661 capa = VS18;
662 }
663
Balaji T K5a52b082014-05-29 10:28:02 +0200664 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
665 hctl |= IWE;
666
Denis Karpov11dd62a2009-09-22 16:44:43 -0700667 OMAP_HSMMC_WRITE(host->base, HCTL,
668 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
669
670 OMAP_HSMMC_WRITE(host->base, CAPA,
671 OMAP_HSMMC_READ(host->base, CAPA) | capa);
672
673 OMAP_HSMMC_WRITE(host->base, HCTL,
674 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
675
676 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
677 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
678 && time_before(jiffies, timeout))
679 ;
680
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200681 OMAP_HSMMC_WRITE(host->base, ISE, 0);
682 OMAP_HSMMC_WRITE(host->base, IE, 0);
683 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700684
685 /* Do not initialize card-specific things if the power is off */
686 if (host->power_mode == MMC_POWER_OFF)
687 goto out;
688
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400689 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700690
Andy Shevchenko5934df22011-05-06 12:14:06 +0300691 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700692
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400693 omap_hsmmc_set_bus_mode(host);
694
Denis Karpov11dd62a2009-09-22 16:44:43 -0700695out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530696 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
697 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700698 return 0;
699}
700
701/*
702 * Save the MMC host context (store the number of power state changes so far).
703 */
Denis Karpov70a33412009-09-22 16:44:59 -0700704static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700705{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530706 host->con = OMAP_HSMMC_READ(host->base, CON);
707 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
708 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
709 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700710}
711
712#else
713
Denis Karpov70a33412009-09-22 16:44:59 -0700714static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700715{
716 return 0;
717}
718
Denis Karpov70a33412009-09-22 16:44:59 -0700719static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700720{
721}
722
723#endif
724
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100725/*
726 * Send init stream sequence to card
727 * before sending IDLE command
728 */
Denis Karpov70a33412009-09-22 16:44:59 -0700729static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730{
731 int reg = 0;
732 unsigned long timeout;
733
Adrian Hunterb62f6222009-09-22 16:45:01 -0700734 if (host->protect_card)
735 return;
736
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100737 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700738
739 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740 OMAP_HSMMC_WRITE(host->base, CON,
741 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
742 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
743
744 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530745 while ((reg != CC_EN) && time_before(jiffies, timeout))
746 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100747
748 OMAP_HSMMC_WRITE(host->base, CON,
749 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700750
751 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
752 OMAP_HSMMC_READ(host->base, STAT);
753
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100754 enable_irq(host->irq);
755}
756
757static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700758int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759{
760 int r = 1;
761
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100762 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100763 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764 return r;
765}
766
767static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700768omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100769 char *buf)
770{
771 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700772 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773
Denis Karpov70a33412009-09-22 16:44:59 -0700774 return sprintf(buf, "%s\n",
775 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776}
777
Denis Karpov70a33412009-09-22 16:44:59 -0700778static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100779
780static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700781omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100782 char *buf)
783{
784 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700785 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100786
Andreas Fenkart326119c2014-11-08 15:33:14 +0100787 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788}
789
Denis Karpov70a33412009-09-22 16:44:59 -0700790static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100791
792/*
793 * Configure the response type and send the cmd.
794 */
795static void
Denis Karpov70a33412009-09-22 16:44:59 -0700796omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100797 struct mmc_data *data)
798{
799 int cmdreg = 0, resptype = 0, cmdtype = 0;
800
Venkatraman S8986d312012-08-07 19:10:38 +0530801 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100802 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
803 host->cmd = cmd;
804
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700805 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100806
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200807 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100808 if (cmd->flags & MMC_RSP_PRESENT) {
809 if (cmd->flags & MMC_RSP_136)
810 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200811 else if (cmd->flags & MMC_RSP_BUSY) {
812 resptype = 3;
813 host->response_busy = 1;
814 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815 resptype = 2;
816 }
817
818 /*
819 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
820 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
821 * a val of 0x3, rest 0x0.
822 */
823 if (cmd == host->mrq->stop)
824 cmdtype = 0x3;
825
826 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
827
Balaji T Ka2e77152014-01-21 19:54:42 +0530828 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
829 host->mrq->sbc) {
830 cmdreg |= ACEN_ACMD23;
831 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
832 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 if (data) {
834 cmdreg |= DP_SELECT | MSBS | BCE;
835 if (data->flags & MMC_DATA_READ)
836 cmdreg |= DDIR;
837 else
838 cmdreg &= ~(DDIR);
839 }
840
841 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530842 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100843
Adrian Hunterb4175772010-05-26 14:42:06 -0700844 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700845
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100846 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
847 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
848}
849
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200850static int
Denis Karpov70a33412009-09-22 16:44:59 -0700851omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200852{
853 if (data->flags & MMC_DATA_WRITE)
854 return DMA_TO_DEVICE;
855 else
856 return DMA_FROM_DEVICE;
857}
858
Russell Kingc5c98922012-04-13 12:14:39 +0100859static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
860 struct mmc_data *data)
861{
862 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
863}
864
Adrian Hunterb4175772010-05-26 14:42:06 -0700865static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
866{
867 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530868 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700869
Venkatraman S31463b12012-04-09 12:08:34 +0530870 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700871 host->req_in_progress = 0;
872 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530873 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700874
875 omap_hsmmc_disable_irq(host);
876 /* Do not complete the request if DMA is still in progress */
877 if (mrq->data && host->use_dma && dma_ch != -1)
878 return;
879 host->mrq = NULL;
880 mmc_request_done(host->mmc, mrq);
881}
882
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100883/*
884 * Notify the transfer complete to MMC core
885 */
886static void
Denis Karpov70a33412009-09-22 16:44:59 -0700887omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100888{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200889 if (!data) {
890 struct mmc_request *mrq = host->mrq;
891
Adrian Hunter23050102009-09-22 16:44:57 -0700892 /* TC before CC from CMD6 - don't know why, but it happens */
893 if (host->cmd && host->cmd->opcode == 6 &&
894 host->response_busy) {
895 host->response_busy = 0;
896 return;
897 }
898
Adrian Hunterb4175772010-05-26 14:42:06 -0700899 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200900 return;
901 }
902
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100903 host->data = NULL;
904
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100905 if (!data->error)
906 data->bytes_xfered += data->blocks * (data->blksz);
907 else
908 data->bytes_xfered = 0;
909
Balaji T Kbf129e12014-01-21 19:54:42 +0530910 if (data->stop && (data->error || !host->mrq->sbc))
911 omap_hsmmc_start_command(host, data->stop, NULL);
912 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700913 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100914}
915
916/*
917 * Notify the core about command completion
918 */
919static void
Denis Karpov70a33412009-09-22 16:44:59 -0700920omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100921{
Balaji T Kbf129e12014-01-21 19:54:42 +0530922 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530923 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530924 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530925 omap_hsmmc_start_dma_transfer(host);
926 omap_hsmmc_start_command(host, host->mrq->cmd,
927 host->mrq->data);
928 return;
929 }
930
Balaji T K2177fa92014-05-09 22:16:52 +0530931 host->cmd = NULL;
932
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100933 if (cmd->flags & MMC_RSP_PRESENT) {
934 if (cmd->flags & MMC_RSP_136) {
935 /* response type 2 */
936 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
937 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
938 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
939 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
940 } else {
941 /* response types 1, 1b, 3, 4, 5, 6 */
942 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
943 }
944 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700945 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530946 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100947}
948
949/*
950 * DMA clean up for command errors
951 */
Denis Karpov70a33412009-09-22 16:44:59 -0700952static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100953{
Adrian Hunterb4175772010-05-26 14:42:06 -0700954 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530955 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700956
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200957 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958
Venkatraman S31463b12012-04-09 12:08:34 +0530959 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700960 dma_ch = host->dma_ch;
961 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530962 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700963
964 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100965 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
966
967 dmaengine_terminate_all(chan);
968 dma_unmap_sg(chan->device->dev,
969 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700970 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100971
Per Forlin053bf342011-11-07 21:55:11 +0530972 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100973 }
974 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100975}
976
977/*
978 * Readable error output
979 */
980#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300981static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100982{
983 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700984 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300985 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
986 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
987 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
988 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100989 };
990 char res[256];
991 char *buf = res;
992 int len, i;
993
994 len = sprintf(buf, "MMC IRQ 0x%x :", status);
995 buf += len;
996
Denis Karpov70a33412009-09-22 16:44:59 -0700997 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700999 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001000 buf += len;
1001 }
1002
Venkatraman S8986d312012-08-07 19:10:38 +05301003 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001004}
Adrian Hunter699b9582011-05-06 12:14:01 +03001005#else
1006static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1007 u32 status)
1008{
1009}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001010#endif /* CONFIG_MMC_DEBUG */
1011
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001012/*
1013 * MMC controller internal state machines reset
1014 *
1015 * Used to reset command or data internal state machines, using respectively
1016 * SRC or SRD bit of SYSCTL register
1017 * Can be called from interrupt context
1018 */
Denis Karpov70a33412009-09-22 16:44:59 -07001019static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1020 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001021{
1022 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301023 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001024
1025 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1026 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1027
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001028 /*
1029 * OMAP4 ES2 and greater has an updated reset logic.
1030 * Monitor a 0->1 transition first
1031 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001032 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001033 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001034 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301035 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001036 }
1037 i = 0;
1038
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001039 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1040 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301041 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001042
1043 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1044 dev_err(mmc_dev(host->mmc),
1045 "Timeout waiting on controller reset in %s\n",
1046 __func__);
1047}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001048
Balaji T K25e18972012-11-19 21:59:55 +05301049static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1050 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301051{
Balaji T K25e18972012-11-19 21:59:55 +05301052 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301053 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301054 if (host->cmd)
1055 host->cmd->error = err;
1056 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301057
1058 if (host->data) {
1059 omap_hsmmc_reset_controller_fsm(host, SRD);
1060 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301061 } else if (host->mrq && host->mrq->cmd)
1062 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301063}
1064
Adrian Hunterb4175772010-05-26 14:42:06 -07001065static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001066{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001067 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001068 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301069 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001071 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301072 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001073
Venkatraman Sa7e96872012-11-19 22:00:01 +05301074 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001075 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001076
Venkatraman Sa7e96872012-11-19 22:00:01 +05301077 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301078 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301079 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301080 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301081 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301082 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1083
Balaji T Ka2e77152014-01-21 19:54:42 +05301084 if (status & ACE_EN) {
1085 u32 ac12;
1086 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1087 if (!(ac12 & ACNE) && host->mrq->sbc) {
1088 end_cmd = 1;
1089 if (ac12 & ACTO)
1090 error = -ETIMEDOUT;
1091 else if (ac12 & (ACCE | ACEB | ACIE))
1092 error = -EILSEQ;
1093 host->mrq->sbc->error = error;
1094 hsmmc_command_incomplete(host, error, end_cmd);
1095 }
1096 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1097 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301098 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301099 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301100 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101 }
1102 }
1103
Francesco Lavra7472bab2013-06-29 08:25:12 +02001104 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301105 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001106 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301107 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001108 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001109}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001110
Adrian Hunterb4175772010-05-26 14:42:06 -07001111/*
1112 * MMC controller IRQ handler
1113 */
1114static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1115{
1116 struct omap_hsmmc_host *host = dev_id;
1117 int status;
1118
1119 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001120 while (status & (INT_EN_MASK | CIRQ_EN)) {
1121 if (host->req_in_progress)
1122 omap_hsmmc_do_irq(host, status);
1123
1124 if (status & CIRQ_EN)
1125 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301126
Adrian Hunterb4175772010-05-26 14:42:06 -07001127 /* Flush posted write */
1128 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301129 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001130
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001131 return IRQ_HANDLED;
1132}
1133
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001134static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1135{
1136 struct omap_hsmmc_host *host = dev_id;
1137
1138 /* cirq is level triggered, disable to avoid infinite loop */
1139 spin_lock(&host->irq_lock);
1140 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1141 disable_irq_nosync(host->wake_irq);
1142 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1143 }
1144 spin_unlock(&host->irq_lock);
1145 pm_request_resume(host->dev); /* no use counter */
1146
1147 return IRQ_HANDLED;
1148}
1149
Denis Karpov70a33412009-09-22 16:44:59 -07001150static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001151{
1152 unsigned long i;
1153
1154 OMAP_HSMMC_WRITE(host->base, HCTL,
1155 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1156 for (i = 0; i < loops_per_jiffy; i++) {
1157 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1158 break;
1159 cpu_relax();
1160 }
1161}
1162
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163/*
David Brownelleb250822009-02-17 14:49:01 -08001164 * Switch MMC interface voltage ... only relevant for MMC1.
1165 *
1166 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1167 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1168 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001169 */
Denis Karpov70a33412009-09-22 16:44:59 -07001170static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001171{
1172 u32 reg_val = 0;
1173 int ret;
1174
1175 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301176 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301177 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301178 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179
1180 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001181 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182
1183 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001184 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001185 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301186 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301187 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301188 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001189
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001190 if (ret != 0)
1191 goto err;
1192
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001193 OMAP_HSMMC_WRITE(host->base, HCTL,
1194 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1195 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001196
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197 /*
1198 * If a MMC dual voltage card is detected, the set_ios fn calls
1199 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001200 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001201 *
David Brownelleb250822009-02-17 14:49:01 -08001202 * Cope with a bit of slop in the range ... per data sheets:
1203 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1204 * but recommended values are 1.71V to 1.89V
1205 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1206 * but recommended values are 2.7V to 3.3V
1207 *
1208 * Board setup code shouldn't permit anything very out-of-range.
1209 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1210 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001211 */
David Brownelleb250822009-02-17 14:49:01 -08001212 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001213 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001214 else
1215 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216
1217 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001218 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001219
1220 return 0;
1221err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301222 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001223 return ret;
1224}
1225
Adrian Hunterb62f6222009-09-22 16:45:01 -07001226/* Protect the card while the cover is open */
1227static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1228{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001229 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001230 return;
1231
1232 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001233 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001234 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301235 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001236 "card is now accessible\n",
1237 mmc_hostname(host->mmc));
1238 host->protect_card = 0;
1239 }
1240 } else {
1241 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301242 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001243 "card is now inaccessible\n",
1244 mmc_hostname(host->mmc));
1245 host->protect_card = 1;
1246 }
1247 }
1248}
1249
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001250/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001251 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001252 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001253static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254{
NeilBrown7efab4f2011-12-30 12:35:13 +11001255 struct omap_hsmmc_host *host = dev_id;
Adrian Huntera6b22402009-09-22 16:44:45 -07001256 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001257
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001258 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001259
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001260 if (host->card_detect)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001261 carddetect = host->card_detect(host->dev);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001262 else {
1263 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001264 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001265 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001266
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001267 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001268 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001269 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001271 return IRQ_HANDLED;
1272}
1273
Russell Kingc5c98922012-04-13 12:14:39 +01001274static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001275{
Russell Kingc5c98922012-04-13 12:14:39 +01001276 struct omap_hsmmc_host *host = param;
1277 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001278 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001279 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001280
Russell Kingc5c98922012-04-13 12:14:39 +01001281 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001282 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001283 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001284 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001285 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001286
Adrian Hunter770d7432011-05-06 12:14:11 +03001287 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001288 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001289 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001290 dma_unmap_sg(chan->device->dev,
1291 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001292 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001293
1294 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001295 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001296 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001297
1298 /* If DMA has finished after TC, complete the request */
1299 if (!req_in_progress) {
1300 struct mmc_request *mrq = host->mrq;
1301
1302 host->mrq = NULL;
1303 mmc_request_done(host->mmc, mrq);
1304 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001305}
1306
Per Forlin9782aff2011-07-01 18:55:23 +02001307static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1308 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001309 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001310 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001311{
1312 int dma_len;
1313
1314 if (!next && data->host_cookie &&
1315 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301316 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001317 " host->next_data.cookie %d\n",
1318 __func__, data->host_cookie, host->next_data.cookie);
1319 data->host_cookie = 0;
1320 }
1321
1322 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001323 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001324 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001325 omap_hsmmc_get_dma_dir(host, data));
1326
1327 } else {
1328 dma_len = host->next_data.dma_len;
1329 host->next_data.dma_len = 0;
1330 }
1331
1332
1333 if (dma_len == 0)
1334 return -EINVAL;
1335
1336 if (next) {
1337 next->dma_len = dma_len;
1338 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1339 } else
1340 host->dma_len = dma_len;
1341
1342 return 0;
1343}
1344
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001345/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001346 * Routine to configure and start DMA for the MMC card
1347 */
Balaji T K9d025332014-01-21 19:54:42 +05301348static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001349 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001350{
Russell King26b88522012-04-13 12:27:37 +01001351 struct dma_slave_config cfg;
1352 struct dma_async_tx_descriptor *tx;
1353 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001355 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001356
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001357 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001358 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001359 struct scatterlist *sgl;
1360
1361 sgl = data->sg + i;
1362 if (sgl->length % data->blksz)
1363 return -EINVAL;
1364 }
1365 if ((data->blksz % 4) != 0)
1366 /* REVISIT: The MMC buffer increments only when MSB is written.
1367 * Return error for blksz which is non multiple of four.
1368 */
1369 return -EINVAL;
1370
Adrian Hunterb4175772010-05-26 14:42:06 -07001371 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001372
Russell Kingc5c98922012-04-13 12:14:39 +01001373 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001374
Russell King26b88522012-04-13 12:27:37 +01001375 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1376 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1377 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1378 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1379 cfg.src_maxburst = data->blksz / 4;
1380 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001381
Russell King26b88522012-04-13 12:27:37 +01001382 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001383 if (ret)
1384 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001385
Russell King26b88522012-04-13 12:27:37 +01001386 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1387 if (ret)
1388 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001389
Russell King26b88522012-04-13 12:27:37 +01001390 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1391 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1392 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1393 if (!tx) {
1394 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1395 /* FIXME: cleanup */
1396 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001398
Russell King26b88522012-04-13 12:27:37 +01001399 tx->callback = omap_hsmmc_dma_callback;
1400 tx->callback_param = host;
1401
1402 /* Does not fail */
1403 dmaengine_submit(tx);
1404
1405 host->dma_ch = 1;
1406
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001407 return 0;
1408}
1409
Denis Karpov70a33412009-09-22 16:44:59 -07001410static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001411 unsigned int timeout_ns,
1412 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001413{
1414 unsigned int timeout, cycle_ns;
1415 uint32_t reg, clkd, dto = 0;
1416
1417 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1418 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1419 if (clkd == 0)
1420 clkd = 1;
1421
Balaji T K6e3076c2014-01-21 19:54:42 +05301422 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001423 timeout = timeout_ns / cycle_ns;
1424 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001425 if (timeout) {
1426 while ((timeout & 0x80000000) == 0) {
1427 dto += 1;
1428 timeout <<= 1;
1429 }
1430 dto = 31 - dto;
1431 timeout <<= 1;
1432 if (timeout && dto)
1433 dto += 1;
1434 if (dto >= 13)
1435 dto -= 13;
1436 else
1437 dto = 0;
1438 if (dto > 14)
1439 dto = 14;
1440 }
1441
1442 reg &= ~DTO_MASK;
1443 reg |= dto << DTO_SHIFT;
1444 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1445}
1446
Balaji T K9d025332014-01-21 19:54:42 +05301447static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1448{
1449 struct mmc_request *req = host->mrq;
1450 struct dma_chan *chan;
1451
1452 if (!req->data)
1453 return;
1454 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1455 | (req->data->blocks << 16));
1456 set_data_timeout(host, req->data->timeout_ns,
1457 req->data->timeout_clks);
1458 chan = omap_hsmmc_get_dma_chan(host, req->data);
1459 dma_async_issue_pending(chan);
1460}
1461
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001462/*
1463 * Configure block length for MMC/SD cards and initiate the transfer.
1464 */
1465static int
Denis Karpov70a33412009-09-22 16:44:59 -07001466omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001467{
1468 int ret;
1469 host->data = req->data;
1470
1471 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001472 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001473 /*
1474 * Set an arbitrary 100ms data timeout for commands with
1475 * busy signal.
1476 */
1477 if (req->cmd->flags & MMC_RSP_BUSY)
1478 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001479 return 0;
1480 }
1481
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001482 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301483 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001484 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301485 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001486 return ret;
1487 }
1488 }
1489 return 0;
1490}
1491
Per Forlin9782aff2011-07-01 18:55:23 +02001492static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1493 int err)
1494{
1495 struct omap_hsmmc_host *host = mmc_priv(mmc);
1496 struct mmc_data *data = mrq->data;
1497
Russell King26b88522012-04-13 12:27:37 +01001498 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001499 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001500
Russell King26b88522012-04-13 12:27:37 +01001501 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1502 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001503 data->host_cookie = 0;
1504 }
1505}
1506
1507static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1508 bool is_first_req)
1509{
1510 struct omap_hsmmc_host *host = mmc_priv(mmc);
1511
1512 if (mrq->data->host_cookie) {
1513 mrq->data->host_cookie = 0;
1514 return ;
1515 }
1516
Russell Kingc5c98922012-04-13 12:14:39 +01001517 if (host->use_dma) {
1518 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001519
Per Forlin9782aff2011-07-01 18:55:23 +02001520 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001521 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001522 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001523 }
Per Forlin9782aff2011-07-01 18:55:23 +02001524}
1525
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001526/*
1527 * Request function. for read/write operation
1528 */
Denis Karpov70a33412009-09-22 16:44:59 -07001529static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001530{
Denis Karpov70a33412009-09-22 16:44:59 -07001531 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001532 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001533
Adrian Hunterb4175772010-05-26 14:42:06 -07001534 BUG_ON(host->req_in_progress);
1535 BUG_ON(host->dma_ch != -1);
1536 if (host->protect_card) {
1537 if (host->reqs_blocked < 3) {
1538 /*
1539 * Ensure the controller is left in a consistent
1540 * state by resetting the command and data state
1541 * machines.
1542 */
1543 omap_hsmmc_reset_controller_fsm(host, SRD);
1544 omap_hsmmc_reset_controller_fsm(host, SRC);
1545 host->reqs_blocked += 1;
1546 }
1547 req->cmd->error = -EBADF;
1548 if (req->data)
1549 req->data->error = -EBADF;
1550 req->cmd->retries = 0;
1551 mmc_request_done(mmc, req);
1552 return;
1553 } else if (host->reqs_blocked)
1554 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001555 WARN_ON(host->mrq != NULL);
1556 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301557 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001558 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001559 if (err) {
1560 req->cmd->error = err;
1561 if (req->data)
1562 req->data->error = err;
1563 host->mrq = NULL;
1564 mmc_request_done(mmc, req);
1565 return;
1566 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301567 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301568 omap_hsmmc_start_command(host, req->sbc, NULL);
1569 return;
1570 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001571
Balaji T K9d025332014-01-21 19:54:42 +05301572 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001573 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001574}
1575
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001576/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001577static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001578{
Denis Karpov70a33412009-09-22 16:44:59 -07001579 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001580 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001581
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301582 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001583
Adrian Huntera3621462009-09-22 16:44:42 -07001584 if (ios->power_mode != host->power_mode) {
1585 switch (ios->power_mode) {
1586 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001587 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001588 break;
1589 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001590 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001591 break;
1592 case MMC_POWER_ON:
1593 do_send_init_stream = 1;
1594 break;
1595 }
1596 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001597 }
1598
Denis Karpovdd498ef2009-09-22 16:44:49 -07001599 /* FIXME: set registers based only on changes to ios */
1600
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001601 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001602
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301603 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001604 /* Only MMC1 can interface at 3V without some flavor
1605 * of external transceiver; but they all handle 1.8V.
1606 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001607 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301608 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001609 /*
1610 * The mmc_select_voltage fn of the core does
1611 * not seem to set the power_mode to
1612 * MMC_POWER_UP upon recalculating the voltage.
1613 * vdd 1.8v.
1614 */
Denis Karpov70a33412009-09-22 16:44:59 -07001615 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1616 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001617 "Switch operation failed\n");
1618 }
1619 }
1620
Andy Shevchenko5934df22011-05-06 12:14:06 +03001621 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001622
Adrian Huntera3621462009-09-22 16:44:42 -07001623 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001624 send_init_stream(host);
1625
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001626 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001627
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301628 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001629}
1630
1631static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1632{
Denis Karpov70a33412009-09-22 16:44:59 -07001633 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001634
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001635 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001636 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001637 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001638}
1639
1640static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1641{
Denis Karpov70a33412009-09-22 16:44:59 -07001642 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001643
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001644 if (!host->get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001645 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001646 return host->get_ro(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647}
1648
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001649static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1650{
1651 struct omap_hsmmc_host *host = mmc_priv(mmc);
1652
Andreas Fenkart326119c2014-11-08 15:33:14 +01001653 if (mmc_pdata(host)->init_card)
1654 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001655}
1656
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001657static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1658{
1659 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001660 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001661 unsigned long flags;
1662
1663 spin_lock_irqsave(&host->irq_lock, flags);
1664
Balaji T K5a52b082014-05-29 10:28:02 +02001665 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001666 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1667 if (enable) {
1668 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1669 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001670 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001671 } else {
1672 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1673 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001674 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001675 }
Balaji T K5a52b082014-05-29 10:28:02 +02001676 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001677 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1678
1679 /*
1680 * if enable, piggy back detection on current request
1681 * but always disable immediately
1682 */
1683 if (!host->req_in_progress || !enable)
1684 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1685
1686 /* flush posted write */
1687 OMAP_HSMMC_READ(host->base, IE);
1688
1689 spin_unlock_irqrestore(&host->irq_lock, flags);
1690}
1691
1692static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1693{
1694 struct mmc_host *mmc = host->mmc;
1695 int ret;
1696
1697 /*
1698 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1699 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1700 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1701 * with functional clock disabled.
1702 */
1703 if (!host->dev->of_node || !host->wake_irq)
1704 return -ENODEV;
1705
1706 /* Prevent auto-enabling of IRQ */
1707 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1708 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1709 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1710 mmc_hostname(mmc), host);
1711 if (ret) {
1712 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1713 goto err;
1714 }
1715
1716 /*
1717 * Some omaps don't have wake-up path from deeper idle states
1718 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1719 */
1720 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001721 struct pinctrl *p = devm_pinctrl_get(host->dev);
1722 if (!p) {
1723 ret = -ENODEV;
1724 goto err_free_irq;
1725 }
1726 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1727 dev_info(host->dev, "missing default pinctrl state\n");
1728 devm_pinctrl_put(p);
1729 ret = -EINVAL;
1730 goto err_free_irq;
1731 }
1732
1733 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1734 dev_info(host->dev, "missing idle pinctrl state\n");
1735 devm_pinctrl_put(p);
1736 ret = -EINVAL;
1737 goto err_free_irq;
1738 }
1739 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001740 }
1741
Balaji T K5a52b082014-05-29 10:28:02 +02001742 OMAP_HSMMC_WRITE(host->base, HCTL,
1743 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001744 return 0;
1745
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001746err_free_irq:
1747 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001748err:
1749 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1750 host->wake_irq = 0;
1751 return ret;
1752}
1753
Denis Karpov70a33412009-09-22 16:44:59 -07001754static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001755{
1756 u32 hctl, capa, value;
1757
1758 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301759 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001760 hctl = SDVS30;
1761 capa = VS30 | VS18;
1762 } else {
1763 hctl = SDVS18;
1764 capa = VS18;
1765 }
1766
1767 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1768 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1769
1770 value = OMAP_HSMMC_READ(host->base, CAPA);
1771 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1772
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001773 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001774 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001775}
1776
Denis Karpov70a33412009-09-22 16:44:59 -07001777static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001778{
Denis Karpov70a33412009-09-22 16:44:59 -07001779 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001780
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301781 pm_runtime_get_sync(host->dev);
1782
Denis Karpovdd498ef2009-09-22 16:44:49 -07001783 return 0;
1784}
1785
Adrian Hunter907d2e72012-02-29 09:17:21 +02001786static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001787{
Denis Karpov70a33412009-09-22 16:44:59 -07001788 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001789
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301790 pm_runtime_mark_last_busy(host->dev);
1791 pm_runtime_put_autosuspend(host->dev);
1792
Denis Karpovdd498ef2009-09-22 16:44:49 -07001793 return 0;
1794}
1795
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001796static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1797 unsigned int direction, int blk_size)
1798{
1799 /* This controller can't do multiblock reads due to hw bugs */
1800 if (direction == MMC_DATA_READ)
1801 return 1;
1802
1803 return blk_size;
1804}
1805
1806static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001807 .enable = omap_hsmmc_enable_fclk,
1808 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001809 .post_req = omap_hsmmc_post_req,
1810 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001811 .request = omap_hsmmc_request,
1812 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001813 .get_cd = omap_hsmmc_get_cd,
1814 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001815 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001816 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001817};
1818
Denis Karpovd900f712009-09-22 16:44:38 -07001819#ifdef CONFIG_DEBUG_FS
1820
Denis Karpov70a33412009-09-22 16:44:59 -07001821static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001822{
1823 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001824 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001825
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001826 seq_printf(s, "mmc%d:\n", mmc->index);
1827 seq_printf(s, "sdio irq mode\t%s\n",
1828 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1829
1830 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1831 seq_printf(s, "sdio irq \t%s\n",
1832 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1833 : "disabled");
1834 }
1835 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001836
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301837 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001838 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001839 seq_printf(s, "CON:\t\t0x%08x\n",
1840 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001841 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1842 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001843 seq_printf(s, "HCTL:\t\t0x%08x\n",
1844 OMAP_HSMMC_READ(host->base, HCTL));
1845 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1846 OMAP_HSMMC_READ(host->base, SYSCTL));
1847 seq_printf(s, "IE:\t\t0x%08x\n",
1848 OMAP_HSMMC_READ(host->base, IE));
1849 seq_printf(s, "ISE:\t\t0x%08x\n",
1850 OMAP_HSMMC_READ(host->base, ISE));
1851 seq_printf(s, "CAPA:\t\t0x%08x\n",
1852 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001853
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301854 pm_runtime_mark_last_busy(host->dev);
1855 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001856
Denis Karpovd900f712009-09-22 16:44:38 -07001857 return 0;
1858}
1859
Denis Karpov70a33412009-09-22 16:44:59 -07001860static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001861{
Denis Karpov70a33412009-09-22 16:44:59 -07001862 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001863}
1864
1865static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001866 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001867 .read = seq_read,
1868 .llseek = seq_lseek,
1869 .release = single_release,
1870};
1871
Denis Karpov70a33412009-09-22 16:44:59 -07001872static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001873{
1874 if (mmc->debugfs_root)
1875 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1876 mmc, &mmc_regs_fops);
1877}
1878
1879#else
1880
Denis Karpov70a33412009-09-22 16:44:59 -07001881static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001882{
1883}
1884
1885#endif
1886
Rajendra Nayak46856a62012-03-12 20:32:37 +05301887#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001888static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1889 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1890 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1891};
1892
1893static const struct omap_mmc_of_data omap4_mmc_of_data = {
1894 .reg_offset = 0x100,
1895};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001896static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1897 .reg_offset = 0x100,
1898 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1899};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301900
1901static const struct of_device_id omap_mmc_of_match[] = {
1902 {
1903 .compatible = "ti,omap2-hsmmc",
1904 },
1905 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001906 .compatible = "ti,omap3-pre-es3-hsmmc",
1907 .data = &omap3_pre_es3_mmc_of_data,
1908 },
1909 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301910 .compatible = "ti,omap3-hsmmc",
1911 },
1912 {
1913 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001914 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301915 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001916 {
1917 .compatible = "ti,am33xx-hsmmc",
1918 .data = &am33xx_mmc_of_data,
1919 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301920 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001921};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301922MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1923
Andreas Fenkart551434382014-11-08 15:33:09 +01001924static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301925{
Andreas Fenkart551434382014-11-08 15:33:09 +01001926 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301927 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301928
1929 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1930 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301931 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301932
1933 if (of_find_property(np, "ti,dual-volt", NULL))
1934 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1935
NeilBrownfdb9de12015-01-13 08:23:18 +13001936 pdata->switch_pin = -EINVAL;
1937 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301938
1939 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001940 pdata->nonremovable = true;
1941 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301942 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301943
1944 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001945 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301946
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301947 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001948 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301949
Rajendra Nayak46856a62012-03-12 20:32:37 +05301950 return pdata;
1951}
1952#else
Andreas Fenkart551434382014-11-08 15:33:09 +01001953static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301954 *of_get_hsmmc_pdata(struct device *dev)
1955{
Balaji T K19df45b2014-02-28 19:08:18 +05301956 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301957}
1958#endif
1959
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001960static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001961{
Andreas Fenkart551434382014-11-08 15:33:09 +01001962 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001963 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001964 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001965 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001966 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301967 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001968 dma_cap_mask_t mask;
1969 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06001970 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301971 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301972
1973 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1974 if (match) {
1975 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001976
1977 if (IS_ERR(pdata))
1978 return PTR_ERR(pdata);
1979
Rajendra Nayak46856a62012-03-12 20:32:37 +05301980 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001981 data = match->data;
1982 pdata->reg_offset = data->reg_offset;
1983 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301984 }
1985 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001986
1987 if (pdata == NULL) {
1988 dev_err(&pdev->dev, "Platform Data is missing\n");
1989 return -ENXIO;
1990 }
1991
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001992 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1993 irq = platform_get_irq(pdev, 0);
1994 if (res == NULL || irq < 0)
1995 return -ENXIO;
1996
Balaji T K77fae212014-05-09 22:16:51 +05301997 base = devm_ioremap_resource(&pdev->dev, res);
1998 if (IS_ERR(base))
1999 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002000
Denis Karpov70a33412009-09-22 16:44:59 -07002001 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002002 if (!mmc) {
2003 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002004 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002005 }
2006
NeilBrownfdb9de12015-01-13 08:23:18 +13002007 ret = mmc_of_parse(mmc);
2008 if (ret)
2009 goto err1;
2010
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002011 host = mmc_priv(mmc);
2012 host->mmc = mmc;
2013 host->pdata = pdata;
2014 host->dev = &pdev->dev;
2015 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002016 host->dma_ch = -1;
2017 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302018 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302019 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002020 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002021 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302022 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002023
NeilBrown41afa3142015-01-13 08:23:18 +13002024 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002025 if (ret)
2026 goto err_gpio;
2027
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002029
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002030 if (pdev->dev.of_node)
2031 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2032
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302033 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002034
Daniel Mackd418ed82012-02-19 13:20:33 +01002035 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2036
2037 if (pdata->max_freq > 0)
2038 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002039 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002040 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002041
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002042 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002043
Balaji T K96181952014-05-09 22:16:48 +05302044 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002045 if (IS_ERR(host->fclk)) {
2046 ret = PTR_ERR(host->fclk);
2047 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002048 goto err1;
2049 }
2050
Paul Walmsley9b682562011-10-06 14:50:35 -06002051 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2052 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 -07002053 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002054 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002055
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302056 pm_runtime_enable(host->dev);
2057 pm_runtime_get_sync(host->dev);
2058 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2059 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060
Balaji T K92a3aeb2012-02-24 21:14:34 +05302061 omap_hsmmc_context_save(host);
2062
Balaji T K96181952014-05-09 22:16:48 +05302063 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302064 /*
2065 * MMC can still work without debounce clock.
2066 */
2067 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302068 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302069 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302070 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302071 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002072 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002073
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002074 /* Since we do only SG emulation, we can have as many segs
2075 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002076 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002077
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002078 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2079 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2080 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2081 mmc->max_seg_size = mmc->max_req_size;
2082
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002083 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002084 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002085
Andreas Fenkart326119c2014-11-08 15:33:14 +01002086 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002087 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088 mmc->caps |= MMC_CAP_4_BIT_DATA;
2089
Andreas Fenkart326119c2014-11-08 15:33:14 +01002090 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002091 mmc->caps |= MMC_CAP_NONREMOVABLE;
2092
NeilBrownfdb9de12015-01-13 08:23:18 +13002093 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002094
Denis Karpov70a33412009-09-22 16:44:59 -07002095 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002096
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302097 if (!pdev->dev.of_node) {
2098 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2099 if (!res) {
2100 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2101 ret = -ENXIO;
2102 goto err_irq;
2103 }
2104 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002105
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302106 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2107 if (!res) {
2108 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2109 ret = -ENXIO;
2110 goto err_irq;
2111 }
2112 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002113 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002114
Russell King26b88522012-04-13 12:27:37 +01002115 dma_cap_zero(mask);
2116 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002117
Matt Porterd272fbf2013-05-10 17:42:34 +05302118 host->rx_chan =
2119 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2120 &rx_req, &pdev->dev, "rx");
2121
Russell King26b88522012-04-13 12:27:37 +01002122 if (!host->rx_chan) {
2123 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002124 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002125 goto err_irq;
2126 }
2127
Matt Porterd272fbf2013-05-10 17:42:34 +05302128 host->tx_chan =
2129 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2130 &tx_req, &pdev->dev, "tx");
2131
Russell King26b88522012-04-13 12:27:37 +01002132 if (!host->tx_chan) {
2133 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002134 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002135 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002136 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002137
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302139 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002140 mmc_hostname(mmc), host);
2141 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302142 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143 goto err_irq;
2144 }
2145
Andreas Fenkart326119c2014-11-08 15:33:14 +01002146 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002147 ret = omap_hsmmc_reg_get(host);
2148 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002149 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002150 host->use_reg = 1;
2151 }
2152
Andreas Fenkart326119c2014-11-08 15:33:14 +01002153 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002154
Adrian Hunterb4175772010-05-26 14:42:06 -07002155 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002157 /*
2158 * For now, only support SDIO interrupt if we have a separate
2159 * wake-up interrupt configured from device tree. This is because
2160 * the wake-up interrupt is needed for idle state and some
2161 * platforms need special quirks. And we don't want to add new
2162 * legacy mux platform init code callbacks any longer as we
2163 * are moving to DT based booting anyways.
2164 */
2165 ret = omap_hsmmc_configure_wake_irq(host);
2166 if (!ret)
2167 mmc->caps |= MMC_CAP_SDIO_IRQ;
2168
Adrian Hunterb62f6222009-09-22 16:45:01 -07002169 omap_hsmmc_protect_card(host);
2170
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002171 mmc_add_host(mmc);
2172
Andreas Fenkart326119c2014-11-08 15:33:14 +01002173 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002174 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2175 if (ret < 0)
2176 goto err_slot_name;
2177 }
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002178 if (host->card_detect_irq && host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002179 ret = device_create_file(&mmc->class_dev,
2180 &dev_attr_cover_switch);
2181 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002182 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002183 }
2184
Denis Karpov70a33412009-09-22 16:44:59 -07002185 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302186 pm_runtime_mark_last_busy(host->dev);
2187 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002188
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002189 return 0;
2190
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002191err_slot_name:
2192 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002193 if (host->use_reg)
2194 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002195err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002196 if (host->tx_chan)
2197 dma_release_channel(host->tx_chan);
2198 if (host->rx_chan)
2199 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302200 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002201 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302202 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302203 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002204err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002205err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002206 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002207err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 return ret;
2209}
2210
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002211static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002212{
Denis Karpov70a33412009-09-22 16:44:59 -07002213 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002214
Felipe Balbi927ce942012-03-14 11:18:27 +02002215 pm_runtime_get_sync(host->dev);
2216 mmc_remove_host(host->mmc);
2217 if (host->use_reg)
2218 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219
Russell Kingc5c98922012-04-13 12:14:39 +01002220 if (host->tx_chan)
2221 dma_release_channel(host->tx_chan);
2222 if (host->rx_chan)
2223 dma_release_channel(host->rx_chan);
2224
Felipe Balbi927ce942012-03-14 11:18:27 +02002225 pm_runtime_put_sync(host->dev);
2226 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302227 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302228 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229
Balaji T K9d1f0282012-10-15 21:35:07 +05302230 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002231
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232 return 0;
2233}
2234
2235#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002236static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237{
Felipe Balbi927ce942012-03-14 11:18:27 +02002238 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2239
2240 if (!host)
2241 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242
Felipe Balbi927ce942012-03-14 11:18:27 +02002243 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002244
2245 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002246 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2247 OMAP_HSMMC_WRITE(host->base, IE, 0);
2248 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002249 OMAP_HSMMC_WRITE(host->base, HCTL,
2250 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2251 }
2252
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002253 /* do not wake up due to sdio irq */
2254 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2255 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2256 disable_irq(host->wake_irq);
2257
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302258 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302259 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002260
Eliad Peller31f9d462011-11-22 16:02:17 +02002261 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002262 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263}
2264
2265/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002266static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002267{
Felipe Balbi927ce942012-03-14 11:18:27 +02002268 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2269
2270 if (!host)
2271 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002272
Felipe Balbi927ce942012-03-14 11:18:27 +02002273 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002274
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302275 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302276 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002277
Felipe Balbi927ce942012-03-14 11:18:27 +02002278 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2279 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002280
Felipe Balbi927ce942012-03-14 11:18:27 +02002281 omap_hsmmc_protect_card(host);
2282
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002283 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2284 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2285 enable_irq(host->wake_irq);
2286
Felipe Balbi927ce942012-03-14 11:18:27 +02002287 pm_runtime_mark_last_busy(host->dev);
2288 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002289 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002290}
2291
2292#else
Denis Karpov70a33412009-09-22 16:44:59 -07002293#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302294#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002295#endif
2296
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302297static int omap_hsmmc_runtime_suspend(struct device *dev)
2298{
2299 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002300 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002301 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302302
2303 host = platform_get_drvdata(to_platform_device(dev));
2304 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002305 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302306
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002307 spin_lock_irqsave(&host->irq_lock, flags);
2308 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2309 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2310 /* disable sdio irq handling to prevent race */
2311 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2312 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002313
2314 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2315 /*
2316 * dat1 line low, pending sdio irq
2317 * race condition: possible irq handler running on
2318 * multi-core, abort
2319 */
2320 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2321 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2322 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2323 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2324 pm_runtime_mark_last_busy(dev);
2325 ret = -EBUSY;
2326 goto abort;
2327 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002328
Andreas Fenkart97978a42014-05-29 10:28:04 +02002329 pinctrl_pm_select_idle_state(dev);
2330
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002331 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2332 enable_irq(host->wake_irq);
2333 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002334 } else {
2335 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002336 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002337
Andreas Fenkartf9459012014-05-29 10:28:03 +02002338abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002339 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002340 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302341}
2342
2343static int omap_hsmmc_runtime_resume(struct device *dev)
2344{
2345 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002346 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302347
2348 host = platform_get_drvdata(to_platform_device(dev));
2349 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002350 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302351
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002352 spin_lock_irqsave(&host->irq_lock, flags);
2353 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2354 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2355 /* sdio irq flag can't change while in runtime suspend */
2356 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2357 disable_irq_nosync(host->wake_irq);
2358 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2359 }
2360
Andreas Fenkart97978a42014-05-29 10:28:04 +02002361 pinctrl_pm_select_default_state(host->dev);
2362
2363 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002364 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2365 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2366 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002367 } else {
2368 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002369 }
2370 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302371 return 0;
2372}
2373
Kevin Hilmana791daa2010-05-26 14:42:07 -07002374static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002375 .suspend = omap_hsmmc_suspend,
2376 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302377 .runtime_suspend = omap_hsmmc_runtime_suspend,
2378 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002379};
2380
2381static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002382 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002383 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002384 .driver = {
2385 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002386 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302387 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002388 },
2389};
2390
Felipe Balbib7964502012-03-14 11:18:32 +02002391module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002392MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2393MODULE_LICENSE("GPL");
2394MODULE_ALIAS("platform:" DRIVER_NAME);
2395MODULE_AUTHOR("Texas Instruments Inc");