blob: 291b9e125d462c5f7787fcce7f9a0510f8847bc2 [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>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010039#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020040#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080041#include <linux/gpio.h>
42#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053043#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053044#include <linux/pm_runtime.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010045#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046
47/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053050#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_BLK 0x0104
52#define OMAP_HSMMC_ARG 0x0108
53#define OMAP_HSMMC_CMD 0x010C
54#define OMAP_HSMMC_RSP10 0x0110
55#define OMAP_HSMMC_RSP32 0x0114
56#define OMAP_HSMMC_RSP54 0x0118
57#define OMAP_HSMMC_RSP76 0x011C
58#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020059#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010060#define OMAP_HSMMC_HCTL 0x0128
61#define OMAP_HSMMC_SYSCTL 0x012C
62#define OMAP_HSMMC_STAT 0x0130
63#define OMAP_HSMMC_IE 0x0134
64#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053065#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#define OMAP_HSMMC_CAPA 0x0140
67
68#define VS18 (1 << 26)
69#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053070#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVS18 (0x5 << 9)
72#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080073#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010074#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010075#define SDVSCLR 0xFFFFF1FF
76#define SDVSDET 0x00000400
77#define AUTOIDLE 0x1
78#define SDBP (1 << 8)
79#define DTO 0xe
80#define ICE 0x1
81#define ICS 0x2
82#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053083#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define CLKD_MASK 0x0000FFC0
85#define CLKD_SHIFT 6
86#define DTO_MASK 0x000F0000
87#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010088#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053089#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define DP_SELECT (1 << 21)
91#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053092#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010093#define MSBS (1 << 5)
94#define BCE (1 << 1)
95#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053096#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020097#define IWE (1 << 24)
Balaji T K03b5d9242012-04-09 12:08:33 +053098#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +020099#define CLKEXTFREE (1 << 16)
100#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200101#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100102#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define STAT_CLEAR 0xFFFFFFFF
104#define INIT_STREAM_CMD 0x00000000
105#define DUAL_VOLT_OCR_BIT 7
106#define SRC (1 << 25)
107#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700108#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100109
Andreas Fenkartf9459012014-05-29 10:28:03 +0200110/* PSTATE */
111#define DLEV_DAT(x) (1 << (20 + (x)))
112
Venkatraman Sa7e96872012-11-19 22:00:01 +0530113/* Interrupt masks for IE and ISE register */
114#define CC_EN (1 << 0)
115#define TC_EN (1 << 1)
116#define BWR_EN (1 << 4)
117#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200118#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530119#define ERR_EN (1 << 15)
120#define CTO_EN (1 << 16)
121#define CCRC_EN (1 << 17)
122#define CEB_EN (1 << 18)
123#define CIE_EN (1 << 19)
124#define DTO_EN (1 << 20)
125#define DCRC_EN (1 << 21)
126#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530127#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530128#define CERR_EN (1 << 28)
129#define BADA_EN (1 << 29)
130
Balaji T Ka2e77152014-01-21 19:54:42 +0530131#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530132 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
133 BRR_EN | BWR_EN | TC_EN | CC_EN)
134
Balaji T Ka2e77152014-01-21 19:54:42 +0530135#define CNI (1 << 7)
136#define ACIE (1 << 4)
137#define ACEB (1 << 3)
138#define ACCE (1 << 2)
139#define ACTO (1 << 1)
140#define ACNE (1 << 0)
141
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530142#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530143#define MMC_TIMEOUT_MS 20 /* 20 mSec */
144#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400145#define OMAP_MMC_MIN_CLOCK 400000
146#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530147#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100148
Balaji T Ke99448f2014-02-19 20:26:40 +0530149#define VDD_1V8 1800000 /* 180000 uV */
150#define VDD_3V0 3000000 /* 300000 uV */
151#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
152
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153/*
154 * One controller can have multiple slots, like on some omap boards using
155 * omap.c controller driver. Luckily this is not currently done on any known
156 * omap_hsmmc.c device.
157 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100158#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100159
160/*
161 * MMC Host controller read/write API's
162 */
163#define OMAP_HSMMC_READ(base, reg) \
164 __raw_readl((base) + OMAP_HSMMC_##reg)
165
166#define OMAP_HSMMC_WRITE(base, reg, val) \
167 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
168
Per Forlin9782aff2011-07-01 18:55:23 +0200169struct omap_hsmmc_next {
170 unsigned int dma_len;
171 s32 cookie;
172};
173
Denis Karpov70a33412009-09-22 16:44:59 -0700174struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 struct device *dev;
176 struct mmc_host *mmc;
177 struct mmc_request *mrq;
178 struct mmc_command *cmd;
179 struct mmc_data *data;
180 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100181 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800182 /*
183 * vcc == configured supply
184 * vcc_aux == optional
185 * - MMC1, supply for DAT4..DAT7
186 * - MMC2/MMC2, external level shifter voltage supply, for
187 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
188 */
189 struct regulator *vcc;
190 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530191 struct regulator *pbias;
192 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100193 void __iomem *base;
194 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700195 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100196 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200197 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100198 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700199 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530201 u32 con;
202 u32 hctl;
203 u32 sysctl;
204 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100205 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200206 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100207 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100208 struct dma_chan *tx_chan;
209 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100210 int slot_id;
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;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100224};
225
Nishanth Menon59445b12014-02-13 23:45:48 -0600226struct omap_mmc_of_data {
227 u32 reg_offset;
228 u8 controller_flags;
229};
230
Balaji T Kbf129e12014-01-21 19:54:42 +0530231static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
232
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800233static int omap_hsmmc_card_detect(struct device *dev, int slot)
234{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530235 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100236 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800237
238 /* NOTE: assumes card detect signal is active-low */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100239 return !gpio_get_value_cansleep(mmc->switch_pin);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800240}
241
242static int omap_hsmmc_get_wp(struct device *dev, int slot)
243{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530244 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100245 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246
247 /* NOTE: assumes write protect signal is active-high */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100248 return gpio_get_value_cansleep(mmc->gpio_wp);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800249}
250
251static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
252{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530253 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100254 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800255
256 /* NOTE: assumes card detect signal is active-low */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100257 return !gpio_get_value_cansleep(mmc->switch_pin);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258}
259
260#ifdef CONFIG_PM
261
262static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
263{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530264 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100265 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800266
Andreas Fenkart326119c2014-11-08 15:33:14 +0100267 disable_irq(mmc->card_detect_irq);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800268 return 0;
269}
270
271static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
272{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530273 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100274 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800275
Andreas Fenkart326119c2014-11-08 15:33:14 +0100276 enable_irq(mmc->card_detect_irq);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800277 return 0;
278}
279
280#else
281
282#define omap_hsmmc_suspend_cdirq NULL
283#define omap_hsmmc_resume_cdirq NULL
284
285#endif
286
Adrian Hunterb702b102010-02-15 10:03:35 -0800287#ifdef CONFIG_REGULATOR
288
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500289static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800290 int vdd)
291{
292 struct omap_hsmmc_host *host =
293 platform_get_drvdata(to_platform_device(dev));
294 int ret = 0;
295
296 /*
297 * If we don't see a Vcc regulator, assume it's a fixed
298 * voltage always-on regulator.
299 */
300 if (!host->vcc)
301 return 0;
302
Andreas Fenkart326119c2014-11-08 15:33:14 +0100303 if (mmc_pdata(host)->before_set_reg)
304 mmc_pdata(host)->before_set_reg(dev, slot, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800305
Balaji T Ke99448f2014-02-19 20:26:40 +0530306 if (host->pbias) {
307 if (host->pbias_enabled == 1) {
308 ret = regulator_disable(host->pbias);
309 if (!ret)
310 host->pbias_enabled = 0;
311 }
312 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
313 }
314
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800315 /*
316 * Assume Vcc regulator is used only to power the card ... OMAP
317 * VDDS is used to power the pins, optionally with a transceiver to
318 * support cards using voltages other than VDDS (1.8V nominal). When a
319 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
320 *
321 * In some cases this regulator won't support enable/disable;
322 * e.g. it's a fixed rail for a WLAN chip.
323 *
324 * In other cases vcc_aux switches interface power. Example, for
325 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
326 * chips/cards need an interface voltage rail too.
327 */
328 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530329 if (host->vcc)
330 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800331 /* Enable interface voltage rail, if needed */
332 if (ret == 0 && host->vcc_aux) {
333 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530334 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400335 ret = mmc_regulator_set_ocr(host->mmc,
336 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800337 }
338 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400339 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800340 if (host->vcc_aux)
341 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530342 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400343 /* Then proceed to shut down the local regulator */
344 ret = mmc_regulator_set_ocr(host->mmc,
345 host->vcc, 0);
346 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800347 }
348
Balaji T Ke99448f2014-02-19 20:26:40 +0530349 if (host->pbias) {
350 if (vdd <= VDD_165_195)
351 ret = regulator_set_voltage(host->pbias, VDD_1V8,
352 VDD_1V8);
353 else
354 ret = regulator_set_voltage(host->pbias, VDD_3V0,
355 VDD_3V0);
356 if (ret < 0)
357 goto error_set_power;
358
359 if (host->pbias_enabled == 0) {
360 ret = regulator_enable(host->pbias);
361 if (!ret)
362 host->pbias_enabled = 1;
363 }
364 }
365
Andreas Fenkart326119c2014-11-08 15:33:14 +0100366 if (mmc_pdata(host)->after_set_reg)
367 mmc_pdata(host)->after_set_reg(dev, slot, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800368
Balaji T Ke99448f2014-02-19 20:26:40 +0530369error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370 return ret;
371}
372
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800373static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
374{
375 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700376 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800377
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530378 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800379 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530380 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
381 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400382 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800383 } else {
384 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700385 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100386 if (!mmc_pdata(host)->ocr_mask) {
387 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700388 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100389 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530390 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100391 mmc_pdata(host)->ocr_mask);
392 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700393 return -EINVAL;
394 }
395 }
Balaji T K987fd492014-02-19 20:26:40 +0530396 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100397 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800398
Balaji T K987fd492014-02-19 20:26:40 +0530399 /* Allow an aux regulator */
400 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
401 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800402
Balaji T Ke99448f2014-02-19 20:26:40 +0530403 reg = devm_regulator_get_optional(host->dev, "pbias");
404 host->pbias = IS_ERR(reg) ? NULL : reg;
405
Balaji T K987fd492014-02-19 20:26:40 +0530406 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100407 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530408 return 0;
409 /*
410 * To disable boot_on regulator, enable regulator
411 * to increase usecount and then disable it.
412 */
413 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
414 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100415 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300416
Andreas Fenkart326119c2014-11-08 15:33:14 +0100417 mmc_pdata(host)->set_power(host->dev, host->slot_id, 1, vdd);
418 mmc_pdata(host)->set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800419 }
420
421 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800422}
423
424static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
425{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100426 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800427}
428
Adrian Hunterb702b102010-02-15 10:03:35 -0800429static inline int omap_hsmmc_have_reg(void)
430{
431 return 1;
432}
433
434#else
435
436static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
437{
438 return -EINVAL;
439}
440
441static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
442{
443}
444
445static inline int omap_hsmmc_have_reg(void)
446{
447 return 0;
448}
449
450#endif
451
Andreas Fenkart551434382014-11-08 15:33:09 +0100452static int omap_hsmmc_gpio_init(struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800453{
454 int ret;
455
Andreas Fenkart326119c2014-11-08 15:33:14 +0100456 if (gpio_is_valid(pdata->switch_pin)) {
457 if (pdata->cover)
458 pdata->get_cover_state =
Adrian Hunterb702b102010-02-15 10:03:35 -0800459 omap_hsmmc_get_cover_state;
460 else
Andreas Fenkart326119c2014-11-08 15:33:14 +0100461 pdata->card_detect = omap_hsmmc_card_detect;
462 pdata->card_detect_irq =
463 gpio_to_irq(pdata->switch_pin);
464 ret = gpio_request(pdata->switch_pin, "mmc_cd");
Adrian Hunterb702b102010-02-15 10:03:35 -0800465 if (ret)
466 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100467 ret = gpio_direction_input(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800468 if (ret)
469 goto err_free_sp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100470 } else {
471 pdata->switch_pin = -EINVAL;
472 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800473
Andreas Fenkart326119c2014-11-08 15:33:14 +0100474 if (gpio_is_valid(pdata->gpio_wp)) {
475 pdata->get_ro = omap_hsmmc_get_wp;
476 ret = gpio_request(pdata->gpio_wp, "mmc_wp");
Adrian Hunterb702b102010-02-15 10:03:35 -0800477 if (ret)
478 goto err_free_cd;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100479 ret = gpio_direction_input(pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800480 if (ret)
481 goto err_free_wp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100482 } else {
483 pdata->gpio_wp = -EINVAL;
484 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800485
486 return 0;
487
488err_free_wp:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100489 gpio_free(pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800490err_free_cd:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100491 if (gpio_is_valid(pdata->switch_pin))
Adrian Hunterb702b102010-02-15 10:03:35 -0800492err_free_sp:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100493 gpio_free(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800494 return ret;
495}
496
Andreas Fenkart551434382014-11-08 15:33:09 +0100497static void omap_hsmmc_gpio_free(struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800498{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100499 if (gpio_is_valid(pdata->gpio_wp))
500 gpio_free(pdata->gpio_wp);
501 if (gpio_is_valid(pdata->switch_pin))
502 gpio_free(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800503}
504
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100505/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300506 * Start clock to the card
507 */
508static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
509{
510 OMAP_HSMMC_WRITE(host->base, SYSCTL,
511 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
512}
513
514/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100515 * Stop clock to the card
516 */
Denis Karpov70a33412009-09-22 16:44:59 -0700517static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100518{
519 OMAP_HSMMC_WRITE(host->base, SYSCTL,
520 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
521 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900522 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100523}
524
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700525static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
526 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700527{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200528 u32 irq_mask = INT_EN_MASK;
529 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700530
531 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200532 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700533
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700534 /* Disable timeout for erases */
535 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530536 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700537
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200538 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700539 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
540 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200541
542 /* latch pending CIRQ, but don't signal MMC core */
543 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
544 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700545 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200546 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700547}
548
549static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
550{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200551 u32 irq_mask = 0;
552 unsigned long flags;
553
554 spin_lock_irqsave(&host->irq_lock, flags);
555 /* no transfer running but need to keep cirq if enabled */
556 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
557 irq_mask |= CIRQ_EN;
558 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
559 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700560 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200561 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700562}
563
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300564/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530565static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300566{
567 u16 dsor = 0;
568
569 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530570 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530571 if (dsor > CLKD_MAX)
572 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300573 }
574
575 return dsor;
576}
577
Andy Shevchenko5934df22011-05-06 12:14:06 +0300578static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
579{
580 struct mmc_ios *ios = &host->mmc->ios;
581 unsigned long regval;
582 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530583 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300584
Venkatraman S8986d312012-08-07 19:10:38 +0530585 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300586
587 omap_hsmmc_stop_clock(host);
588
589 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
590 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530591 clkdiv = calc_divisor(host, ios);
592 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300593 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
594 OMAP_HSMMC_WRITE(host->base, SYSCTL,
595 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
596
597 /* Wait till the ICS bit is set */
598 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
599 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
600 && time_before(jiffies, timeout))
601 cpu_relax();
602
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530603 /*
604 * Enable High-Speed Support
605 * Pre-Requisites
606 * - Controller should support High-Speed-Enable Bit
607 * - Controller should not be using DDR Mode
608 * - Controller should advertise that it supports High Speed
609 * in capabilities register
610 * - MMC/SD clock coming out of controller > 25MHz
611 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100612 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900613 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530614 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
615 regval = OMAP_HSMMC_READ(host->base, HCTL);
616 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
617 regval |= HSPE;
618 else
619 regval &= ~HSPE;
620
621 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
622 }
623
Andy Shevchenko5934df22011-05-06 12:14:06 +0300624 omap_hsmmc_start_clock(host);
625}
626
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400627static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
628{
629 struct mmc_ios *ios = &host->mmc->ios;
630 u32 con;
631
632 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900633 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d9242012-04-09 12:08:33 +0530634 con |= DDR; /* configure in DDR mode */
635 else
636 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400637 switch (ios->bus_width) {
638 case MMC_BUS_WIDTH_8:
639 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
640 break;
641 case MMC_BUS_WIDTH_4:
642 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
643 OMAP_HSMMC_WRITE(host->base, HCTL,
644 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
645 break;
646 case MMC_BUS_WIDTH_1:
647 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
648 OMAP_HSMMC_WRITE(host->base, HCTL,
649 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
650 break;
651 }
652}
653
654static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
655{
656 struct mmc_ios *ios = &host->mmc->ios;
657 u32 con;
658
659 con = OMAP_HSMMC_READ(host->base, CON);
660 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
661 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
662 else
663 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
664}
665
Denis Karpov11dd62a2009-09-22 16:44:43 -0700666#ifdef CONFIG_PM
667
668/*
669 * Restore the MMC host context, if it was lost as result of a
670 * power state change.
671 */
Denis Karpov70a33412009-09-22 16:44:59 -0700672static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700673{
674 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400675 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700676 unsigned long timeout;
677
Tony Lindgren0a82e062013-10-21 00:25:19 +0530678 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
679 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
680 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
681 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
682 return 0;
683
684 host->context_loss++;
685
Balaji T Kc2200ef2012-03-07 09:55:30 -0500686 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700687 if (host->power_mode != MMC_POWER_OFF &&
688 (1 << ios->vdd) <= MMC_VDD_23_24)
689 hctl = SDVS18;
690 else
691 hctl = SDVS30;
692 capa = VS30 | VS18;
693 } else {
694 hctl = SDVS18;
695 capa = VS18;
696 }
697
Balaji T K5a52b082014-05-29 10:28:02 +0200698 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
699 hctl |= IWE;
700
Denis Karpov11dd62a2009-09-22 16:44:43 -0700701 OMAP_HSMMC_WRITE(host->base, HCTL,
702 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
703
704 OMAP_HSMMC_WRITE(host->base, CAPA,
705 OMAP_HSMMC_READ(host->base, CAPA) | capa);
706
707 OMAP_HSMMC_WRITE(host->base, HCTL,
708 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
709
710 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
711 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
712 && time_before(jiffies, timeout))
713 ;
714
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200715 OMAP_HSMMC_WRITE(host->base, ISE, 0);
716 OMAP_HSMMC_WRITE(host->base, IE, 0);
717 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700718
719 /* Do not initialize card-specific things if the power is off */
720 if (host->power_mode == MMC_POWER_OFF)
721 goto out;
722
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400723 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700724
Andy Shevchenko5934df22011-05-06 12:14:06 +0300725 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700726
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400727 omap_hsmmc_set_bus_mode(host);
728
Denis Karpov11dd62a2009-09-22 16:44:43 -0700729out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530730 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
731 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700732 return 0;
733}
734
735/*
736 * Save the MMC host context (store the number of power state changes so far).
737 */
Denis Karpov70a33412009-09-22 16:44:59 -0700738static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700739{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530740 host->con = OMAP_HSMMC_READ(host->base, CON);
741 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
742 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
743 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700744}
745
746#else
747
Denis Karpov70a33412009-09-22 16:44:59 -0700748static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700749{
750 return 0;
751}
752
Denis Karpov70a33412009-09-22 16:44:59 -0700753static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700754{
755}
756
757#endif
758
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759/*
760 * Send init stream sequence to card
761 * before sending IDLE command
762 */
Denis Karpov70a33412009-09-22 16:44:59 -0700763static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100764{
765 int reg = 0;
766 unsigned long timeout;
767
Adrian Hunterb62f6222009-09-22 16:45:01 -0700768 if (host->protect_card)
769 return;
770
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100771 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700772
773 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774 OMAP_HSMMC_WRITE(host->base, CON,
775 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
776 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
777
778 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530779 while ((reg != CC_EN) && time_before(jiffies, timeout))
780 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781
782 OMAP_HSMMC_WRITE(host->base, CON,
783 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700784
785 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
786 OMAP_HSMMC_READ(host->base, STAT);
787
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 enable_irq(host->irq);
789}
790
791static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700792int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793{
794 int r = 1;
795
Andreas Fenkart326119c2014-11-08 15:33:14 +0100796 if (mmc_pdata(host)->get_cover_state)
797 r = mmc_pdata(host)->get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798 return r;
799}
800
801static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700802omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100803 char *buf)
804{
805 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700806 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100807
Denis Karpov70a33412009-09-22 16:44:59 -0700808 return sprintf(buf, "%s\n",
809 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100810}
811
Denis Karpov70a33412009-09-22 16:44:59 -0700812static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100813
814static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700815omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100816 char *buf)
817{
818 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700819 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100820
Andreas Fenkart326119c2014-11-08 15:33:14 +0100821 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100822}
823
Denis Karpov70a33412009-09-22 16:44:59 -0700824static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100825
826/*
827 * Configure the response type and send the cmd.
828 */
829static void
Denis Karpov70a33412009-09-22 16:44:59 -0700830omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100831 struct mmc_data *data)
832{
833 int cmdreg = 0, resptype = 0, cmdtype = 0;
834
Venkatraman S8986d312012-08-07 19:10:38 +0530835 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100836 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
837 host->cmd = cmd;
838
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700839 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100840
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200841 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100842 if (cmd->flags & MMC_RSP_PRESENT) {
843 if (cmd->flags & MMC_RSP_136)
844 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200845 else if (cmd->flags & MMC_RSP_BUSY) {
846 resptype = 3;
847 host->response_busy = 1;
848 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100849 resptype = 2;
850 }
851
852 /*
853 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
854 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
855 * a val of 0x3, rest 0x0.
856 */
857 if (cmd == host->mrq->stop)
858 cmdtype = 0x3;
859
860 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
861
Balaji T Ka2e77152014-01-21 19:54:42 +0530862 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
863 host->mrq->sbc) {
864 cmdreg |= ACEN_ACMD23;
865 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
866 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100867 if (data) {
868 cmdreg |= DP_SELECT | MSBS | BCE;
869 if (data->flags & MMC_DATA_READ)
870 cmdreg |= DDIR;
871 else
872 cmdreg &= ~(DDIR);
873 }
874
875 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530876 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100877
Adrian Hunterb4175772010-05-26 14:42:06 -0700878 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700879
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100880 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
881 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
882}
883
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200884static int
Denis Karpov70a33412009-09-22 16:44:59 -0700885omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200886{
887 if (data->flags & MMC_DATA_WRITE)
888 return DMA_TO_DEVICE;
889 else
890 return DMA_FROM_DEVICE;
891}
892
Russell Kingc5c98922012-04-13 12:14:39 +0100893static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
894 struct mmc_data *data)
895{
896 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
897}
898
Adrian Hunterb4175772010-05-26 14:42:06 -0700899static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
900{
901 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530902 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700903
Venkatraman S31463b12012-04-09 12:08:34 +0530904 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700905 host->req_in_progress = 0;
906 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530907 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700908
909 omap_hsmmc_disable_irq(host);
910 /* Do not complete the request if DMA is still in progress */
911 if (mrq->data && host->use_dma && dma_ch != -1)
912 return;
913 host->mrq = NULL;
914 mmc_request_done(host->mmc, mrq);
915}
916
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100917/*
918 * Notify the transfer complete to MMC core
919 */
920static void
Denis Karpov70a33412009-09-22 16:44:59 -0700921omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100922{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200923 if (!data) {
924 struct mmc_request *mrq = host->mrq;
925
Adrian Hunter23050102009-09-22 16:44:57 -0700926 /* TC before CC from CMD6 - don't know why, but it happens */
927 if (host->cmd && host->cmd->opcode == 6 &&
928 host->response_busy) {
929 host->response_busy = 0;
930 return;
931 }
932
Adrian Hunterb4175772010-05-26 14:42:06 -0700933 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200934 return;
935 }
936
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100937 host->data = NULL;
938
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939 if (!data->error)
940 data->bytes_xfered += data->blocks * (data->blksz);
941 else
942 data->bytes_xfered = 0;
943
Balaji T Kbf129e12014-01-21 19:54:42 +0530944 if (data->stop && (data->error || !host->mrq->sbc))
945 omap_hsmmc_start_command(host, data->stop, NULL);
946 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700947 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100948}
949
950/*
951 * Notify the core about command completion
952 */
953static void
Denis Karpov70a33412009-09-22 16:44:59 -0700954omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100955{
Balaji T Kbf129e12014-01-21 19:54:42 +0530956 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530957 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530958 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530959 omap_hsmmc_start_dma_transfer(host);
960 omap_hsmmc_start_command(host, host->mrq->cmd,
961 host->mrq->data);
962 return;
963 }
964
Balaji T K2177fa92014-05-09 22:16:52 +0530965 host->cmd = NULL;
966
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100967 if (cmd->flags & MMC_RSP_PRESENT) {
968 if (cmd->flags & MMC_RSP_136) {
969 /* response type 2 */
970 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
971 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
972 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
973 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
974 } else {
975 /* response types 1, 1b, 3, 4, 5, 6 */
976 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
977 }
978 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700979 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530980 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100981}
982
983/*
984 * DMA clean up for command errors
985 */
Denis Karpov70a33412009-09-22 16:44:59 -0700986static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100987{
Adrian Hunterb4175772010-05-26 14:42:06 -0700988 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530989 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700990
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200991 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100992
Venkatraman S31463b12012-04-09 12:08:34 +0530993 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700994 dma_ch = host->dma_ch;
995 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530996 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700997
998 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100999 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1000
1001 dmaengine_terminate_all(chan);
1002 dma_unmap_sg(chan->device->dev,
1003 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001004 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001005
Per Forlin053bf342011-11-07 21:55:11 +05301006 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001007 }
1008 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001009}
1010
1011/*
1012 * Readable error output
1013 */
1014#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001015static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001016{
1017 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001018 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001019 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1020 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1021 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1022 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001023 };
1024 char res[256];
1025 char *buf = res;
1026 int len, i;
1027
1028 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1029 buf += len;
1030
Denis Karpov70a33412009-09-22 16:44:59 -07001031 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001032 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001033 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001034 buf += len;
1035 }
1036
Venkatraman S8986d312012-08-07 19:10:38 +05301037 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001038}
Adrian Hunter699b9582011-05-06 12:14:01 +03001039#else
1040static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1041 u32 status)
1042{
1043}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001044#endif /* CONFIG_MMC_DEBUG */
1045
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001046/*
1047 * MMC controller internal state machines reset
1048 *
1049 * Used to reset command or data internal state machines, using respectively
1050 * SRC or SRD bit of SYSCTL register
1051 * Can be called from interrupt context
1052 */
Denis Karpov70a33412009-09-22 16:44:59 -07001053static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1054 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001055{
1056 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301057 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001058
1059 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1060 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1061
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001062 /*
1063 * OMAP4 ES2 and greater has an updated reset logic.
1064 * Monitor a 0->1 transition first
1065 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001066 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001067 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001068 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301069 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001070 }
1071 i = 0;
1072
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001073 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1074 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301075 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001076
1077 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1078 dev_err(mmc_dev(host->mmc),
1079 "Timeout waiting on controller reset in %s\n",
1080 __func__);
1081}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001082
Balaji T K25e18972012-11-19 21:59:55 +05301083static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1084 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301085{
Balaji T K25e18972012-11-19 21:59:55 +05301086 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301087 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301088 if (host->cmd)
1089 host->cmd->error = err;
1090 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301091
1092 if (host->data) {
1093 omap_hsmmc_reset_controller_fsm(host, SRD);
1094 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301095 } else if (host->mrq && host->mrq->cmd)
1096 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301097}
1098
Adrian Hunterb4175772010-05-26 14:42:06 -07001099static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001100{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001102 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301103 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001104
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001105 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301106 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001107
Venkatraman Sa7e96872012-11-19 22:00:01 +05301108 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001109 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001110
Venkatraman Sa7e96872012-11-19 22:00:01 +05301111 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301112 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301113 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301114 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301115 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301116 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1117
Balaji T Ka2e77152014-01-21 19:54:42 +05301118 if (status & ACE_EN) {
1119 u32 ac12;
1120 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1121 if (!(ac12 & ACNE) && host->mrq->sbc) {
1122 end_cmd = 1;
1123 if (ac12 & ACTO)
1124 error = -ETIMEDOUT;
1125 else if (ac12 & (ACCE | ACEB | ACIE))
1126 error = -EILSEQ;
1127 host->mrq->sbc->error = error;
1128 hsmmc_command_incomplete(host, error, end_cmd);
1129 }
1130 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1131 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301132 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301133 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301134 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001135 }
1136 }
1137
Francesco Lavra7472bab2013-06-29 08:25:12 +02001138 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301139 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001140 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301141 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001142 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001143}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001144
Adrian Hunterb4175772010-05-26 14:42:06 -07001145/*
1146 * MMC controller IRQ handler
1147 */
1148static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1149{
1150 struct omap_hsmmc_host *host = dev_id;
1151 int status;
1152
1153 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001154 while (status & (INT_EN_MASK | CIRQ_EN)) {
1155 if (host->req_in_progress)
1156 omap_hsmmc_do_irq(host, status);
1157
1158 if (status & CIRQ_EN)
1159 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301160
Adrian Hunterb4175772010-05-26 14:42:06 -07001161 /* Flush posted write */
1162 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301163 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001164
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001165 return IRQ_HANDLED;
1166}
1167
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001168static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1169{
1170 struct omap_hsmmc_host *host = dev_id;
1171
1172 /* cirq is level triggered, disable to avoid infinite loop */
1173 spin_lock(&host->irq_lock);
1174 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1175 disable_irq_nosync(host->wake_irq);
1176 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1177 }
1178 spin_unlock(&host->irq_lock);
1179 pm_request_resume(host->dev); /* no use counter */
1180
1181 return IRQ_HANDLED;
1182}
1183
Denis Karpov70a33412009-09-22 16:44:59 -07001184static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001185{
1186 unsigned long i;
1187
1188 OMAP_HSMMC_WRITE(host->base, HCTL,
1189 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1190 for (i = 0; i < loops_per_jiffy; i++) {
1191 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1192 break;
1193 cpu_relax();
1194 }
1195}
1196
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197/*
David Brownelleb250822009-02-17 14:49:01 -08001198 * Switch MMC interface voltage ... only relevant for MMC1.
1199 *
1200 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1201 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1202 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001203 */
Denis Karpov70a33412009-09-22 16:44:59 -07001204static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205{
1206 u32 reg_val = 0;
1207 int ret;
1208
1209 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301210 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301211 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301212 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001213
1214 /* Turn the power off */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001215 ret = mmc_pdata(host)->set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216
1217 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001218 if (!ret)
Andreas Fenkart326119c2014-11-08 15:33:14 +01001219 ret = mmc_pdata(host)->set_power(host->dev, host->slot_id, 1,
1220 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301221 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301222 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301223 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001224
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001225 if (ret != 0)
1226 goto err;
1227
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001228 OMAP_HSMMC_WRITE(host->base, HCTL,
1229 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1230 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001231
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001232 /*
1233 * If a MMC dual voltage card is detected, the set_ios fn calls
1234 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001235 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001236 *
David Brownelleb250822009-02-17 14:49:01 -08001237 * Cope with a bit of slop in the range ... per data sheets:
1238 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1239 * but recommended values are 1.71V to 1.89V
1240 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1241 * but recommended values are 2.7V to 3.3V
1242 *
1243 * Board setup code shouldn't permit anything very out-of-range.
1244 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1245 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246 */
David Brownelleb250822009-02-17 14:49:01 -08001247 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001248 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001249 else
1250 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001251
1252 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001253 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254
1255 return 0;
1256err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301257 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001258 return ret;
1259}
1260
Adrian Hunterb62f6222009-09-22 16:45:01 -07001261/* Protect the card while the cover is open */
1262static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1263{
Andreas Fenkart326119c2014-11-08 15:33:14 +01001264 if (!mmc_pdata(host)->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001265 return;
1266
1267 host->reqs_blocked = 0;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001268 if (mmc_pdata(host)->get_cover_state(host->dev, host->slot_id)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001269 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301270 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001271 "card is now accessible\n",
1272 mmc_hostname(host->mmc));
1273 host->protect_card = 0;
1274 }
1275 } else {
1276 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301277 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001278 "card is now inaccessible\n",
1279 mmc_hostname(host->mmc));
1280 host->protect_card = 1;
1281 }
1282 }
1283}
1284
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001285/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001286 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001287 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001288static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001289{
NeilBrown7efab4f2011-12-30 12:35:13 +11001290 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001291 struct omap_hsmmc_platform_data *pdata = host->pdata;
Adrian Huntera6b22402009-09-22 16:44:45 -07001292 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001293
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001295
Andreas Fenkart326119c2014-11-08 15:33:14 +01001296 if (pdata->card_detect)
1297 carddetect = pdata->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001298 else {
1299 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001300 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001301 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001302
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001303 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001304 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001305 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001306 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001307 return IRQ_HANDLED;
1308}
1309
Russell Kingc5c98922012-04-13 12:14:39 +01001310static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001311{
Russell Kingc5c98922012-04-13 12:14:39 +01001312 struct omap_hsmmc_host *host = param;
1313 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001314 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001315 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001316
Russell Kingc5c98922012-04-13 12:14:39 +01001317 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001318 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001319 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001320 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001321 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322
Adrian Hunter770d7432011-05-06 12:14:11 +03001323 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001324 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001325 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001326 dma_unmap_sg(chan->device->dev,
1327 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001328 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001329
1330 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001331 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001332 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001333
1334 /* If DMA has finished after TC, complete the request */
1335 if (!req_in_progress) {
1336 struct mmc_request *mrq = host->mrq;
1337
1338 host->mrq = NULL;
1339 mmc_request_done(host->mmc, mrq);
1340 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001341}
1342
Per Forlin9782aff2011-07-01 18:55:23 +02001343static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1344 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001345 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001346 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001347{
1348 int dma_len;
1349
1350 if (!next && data->host_cookie &&
1351 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301352 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001353 " host->next_data.cookie %d\n",
1354 __func__, data->host_cookie, host->next_data.cookie);
1355 data->host_cookie = 0;
1356 }
1357
1358 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001359 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001360 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001361 omap_hsmmc_get_dma_dir(host, data));
1362
1363 } else {
1364 dma_len = host->next_data.dma_len;
1365 host->next_data.dma_len = 0;
1366 }
1367
1368
1369 if (dma_len == 0)
1370 return -EINVAL;
1371
1372 if (next) {
1373 next->dma_len = dma_len;
1374 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1375 } else
1376 host->dma_len = dma_len;
1377
1378 return 0;
1379}
1380
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001381/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001382 * Routine to configure and start DMA for the MMC card
1383 */
Balaji T K9d025332014-01-21 19:54:42 +05301384static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001385 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001386{
Russell King26b88522012-04-13 12:27:37 +01001387 struct dma_slave_config cfg;
1388 struct dma_async_tx_descriptor *tx;
1389 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001390 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001391 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001392
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001393 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001394 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001395 struct scatterlist *sgl;
1396
1397 sgl = data->sg + i;
1398 if (sgl->length % data->blksz)
1399 return -EINVAL;
1400 }
1401 if ((data->blksz % 4) != 0)
1402 /* REVISIT: The MMC buffer increments only when MSB is written.
1403 * Return error for blksz which is non multiple of four.
1404 */
1405 return -EINVAL;
1406
Adrian Hunterb4175772010-05-26 14:42:06 -07001407 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001408
Russell Kingc5c98922012-04-13 12:14:39 +01001409 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001410
Russell King26b88522012-04-13 12:27:37 +01001411 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1412 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1413 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1414 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1415 cfg.src_maxburst = data->blksz / 4;
1416 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001417
Russell King26b88522012-04-13 12:27:37 +01001418 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001419 if (ret)
1420 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421
Russell King26b88522012-04-13 12:27:37 +01001422 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1423 if (ret)
1424 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001425
Russell King26b88522012-04-13 12:27:37 +01001426 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1427 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1428 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1429 if (!tx) {
1430 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1431 /* FIXME: cleanup */
1432 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001433 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001434
Russell King26b88522012-04-13 12:27:37 +01001435 tx->callback = omap_hsmmc_dma_callback;
1436 tx->callback_param = host;
1437
1438 /* Does not fail */
1439 dmaengine_submit(tx);
1440
1441 host->dma_ch = 1;
1442
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001443 return 0;
1444}
1445
Denis Karpov70a33412009-09-22 16:44:59 -07001446static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001447 unsigned int timeout_ns,
1448 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449{
1450 unsigned int timeout, cycle_ns;
1451 uint32_t reg, clkd, dto = 0;
1452
1453 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1454 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1455 if (clkd == 0)
1456 clkd = 1;
1457
Balaji T K6e3076c2014-01-21 19:54:42 +05301458 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001459 timeout = timeout_ns / cycle_ns;
1460 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461 if (timeout) {
1462 while ((timeout & 0x80000000) == 0) {
1463 dto += 1;
1464 timeout <<= 1;
1465 }
1466 dto = 31 - dto;
1467 timeout <<= 1;
1468 if (timeout && dto)
1469 dto += 1;
1470 if (dto >= 13)
1471 dto -= 13;
1472 else
1473 dto = 0;
1474 if (dto > 14)
1475 dto = 14;
1476 }
1477
1478 reg &= ~DTO_MASK;
1479 reg |= dto << DTO_SHIFT;
1480 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1481}
1482
Balaji T K9d025332014-01-21 19:54:42 +05301483static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1484{
1485 struct mmc_request *req = host->mrq;
1486 struct dma_chan *chan;
1487
1488 if (!req->data)
1489 return;
1490 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1491 | (req->data->blocks << 16));
1492 set_data_timeout(host, req->data->timeout_ns,
1493 req->data->timeout_clks);
1494 chan = omap_hsmmc_get_dma_chan(host, req->data);
1495 dma_async_issue_pending(chan);
1496}
1497
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001498/*
1499 * Configure block length for MMC/SD cards and initiate the transfer.
1500 */
1501static int
Denis Karpov70a33412009-09-22 16:44:59 -07001502omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001503{
1504 int ret;
1505 host->data = req->data;
1506
1507 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001508 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001509 /*
1510 * Set an arbitrary 100ms data timeout for commands with
1511 * busy signal.
1512 */
1513 if (req->cmd->flags & MMC_RSP_BUSY)
1514 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515 return 0;
1516 }
1517
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001518 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301519 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301521 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001522 return ret;
1523 }
1524 }
1525 return 0;
1526}
1527
Per Forlin9782aff2011-07-01 18:55:23 +02001528static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1529 int err)
1530{
1531 struct omap_hsmmc_host *host = mmc_priv(mmc);
1532 struct mmc_data *data = mrq->data;
1533
Russell King26b88522012-04-13 12:27:37 +01001534 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001535 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001536
Russell King26b88522012-04-13 12:27:37 +01001537 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1538 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001539 data->host_cookie = 0;
1540 }
1541}
1542
1543static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1544 bool is_first_req)
1545{
1546 struct omap_hsmmc_host *host = mmc_priv(mmc);
1547
1548 if (mrq->data->host_cookie) {
1549 mrq->data->host_cookie = 0;
1550 return ;
1551 }
1552
Russell Kingc5c98922012-04-13 12:14:39 +01001553 if (host->use_dma) {
1554 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001555
Per Forlin9782aff2011-07-01 18:55:23 +02001556 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001557 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001558 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001559 }
Per Forlin9782aff2011-07-01 18:55:23 +02001560}
1561
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001562/*
1563 * Request function. for read/write operation
1564 */
Denis Karpov70a33412009-09-22 16:44:59 -07001565static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001566{
Denis Karpov70a33412009-09-22 16:44:59 -07001567 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001568 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001569
Adrian Hunterb4175772010-05-26 14:42:06 -07001570 BUG_ON(host->req_in_progress);
1571 BUG_ON(host->dma_ch != -1);
1572 if (host->protect_card) {
1573 if (host->reqs_blocked < 3) {
1574 /*
1575 * Ensure the controller is left in a consistent
1576 * state by resetting the command and data state
1577 * machines.
1578 */
1579 omap_hsmmc_reset_controller_fsm(host, SRD);
1580 omap_hsmmc_reset_controller_fsm(host, SRC);
1581 host->reqs_blocked += 1;
1582 }
1583 req->cmd->error = -EBADF;
1584 if (req->data)
1585 req->data->error = -EBADF;
1586 req->cmd->retries = 0;
1587 mmc_request_done(mmc, req);
1588 return;
1589 } else if (host->reqs_blocked)
1590 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001591 WARN_ON(host->mrq != NULL);
1592 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301593 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001594 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001595 if (err) {
1596 req->cmd->error = err;
1597 if (req->data)
1598 req->data->error = err;
1599 host->mrq = NULL;
1600 mmc_request_done(mmc, req);
1601 return;
1602 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301603 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301604 omap_hsmmc_start_command(host, req->sbc, NULL);
1605 return;
1606 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001607
Balaji T K9d025332014-01-21 19:54:42 +05301608 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001609 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001610}
1611
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001613static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001614{
Denis Karpov70a33412009-09-22 16:44:59 -07001615 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001616 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001617
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301618 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001619
Adrian Huntera3621462009-09-22 16:44:42 -07001620 if (ios->power_mode != host->power_mode) {
1621 switch (ios->power_mode) {
1622 case MMC_POWER_OFF:
Andreas Fenkart326119c2014-11-08 15:33:14 +01001623 mmc_pdata(host)->set_power(host->dev, host->slot_id,
1624 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001625 break;
1626 case MMC_POWER_UP:
Andreas Fenkart326119c2014-11-08 15:33:14 +01001627 mmc_pdata(host)->set_power(host->dev, host->slot_id,
1628 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001629 break;
1630 case MMC_POWER_ON:
1631 do_send_init_stream = 1;
1632 break;
1633 }
1634 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001635 }
1636
Denis Karpovdd498ef2009-09-22 16:44:49 -07001637 /* FIXME: set registers based only on changes to ios */
1638
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001639 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001640
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301641 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001642 /* Only MMC1 can interface at 3V without some flavor
1643 * of external transceiver; but they all handle 1.8V.
1644 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001645 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301646 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647 /*
1648 * The mmc_select_voltage fn of the core does
1649 * not seem to set the power_mode to
1650 * MMC_POWER_UP upon recalculating the voltage.
1651 * vdd 1.8v.
1652 */
Denis Karpov70a33412009-09-22 16:44:59 -07001653 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1654 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001655 "Switch operation failed\n");
1656 }
1657 }
1658
Andy Shevchenko5934df22011-05-06 12:14:06 +03001659 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001660
Adrian Huntera3621462009-09-22 16:44:42 -07001661 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001662 send_init_stream(host);
1663
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001664 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001665
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301666 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001667}
1668
1669static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1670{
Denis Karpov70a33412009-09-22 16:44:59 -07001671 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001672
Andreas Fenkart326119c2014-11-08 15:33:14 +01001673 if (!mmc_pdata(host)->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001674 return -ENOSYS;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001675 return mmc_pdata(host)->card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001676}
1677
1678static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1679{
Denis Karpov70a33412009-09-22 16:44:59 -07001680 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001681
Andreas Fenkart326119c2014-11-08 15:33:14 +01001682 if (!mmc_pdata(host)->get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001683 return -ENOSYS;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001684 return mmc_pdata(host)->get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001685}
1686
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001687static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1688{
1689 struct omap_hsmmc_host *host = mmc_priv(mmc);
1690
Andreas Fenkart326119c2014-11-08 15:33:14 +01001691 if (mmc_pdata(host)->init_card)
1692 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001693}
1694
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001695static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1696{
1697 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001698 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001699 unsigned long flags;
1700
1701 spin_lock_irqsave(&host->irq_lock, flags);
1702
Balaji T K5a52b082014-05-29 10:28:02 +02001703 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001704 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1705 if (enable) {
1706 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1707 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001708 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001709 } else {
1710 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1711 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001712 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001713 }
Balaji T K5a52b082014-05-29 10:28:02 +02001714 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001715 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1716
1717 /*
1718 * if enable, piggy back detection on current request
1719 * but always disable immediately
1720 */
1721 if (!host->req_in_progress || !enable)
1722 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1723
1724 /* flush posted write */
1725 OMAP_HSMMC_READ(host->base, IE);
1726
1727 spin_unlock_irqrestore(&host->irq_lock, flags);
1728}
1729
1730static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1731{
1732 struct mmc_host *mmc = host->mmc;
1733 int ret;
1734
1735 /*
1736 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1737 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1738 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1739 * with functional clock disabled.
1740 */
1741 if (!host->dev->of_node || !host->wake_irq)
1742 return -ENODEV;
1743
1744 /* Prevent auto-enabling of IRQ */
1745 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1746 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1747 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1748 mmc_hostname(mmc), host);
1749 if (ret) {
1750 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1751 goto err;
1752 }
1753
1754 /*
1755 * Some omaps don't have wake-up path from deeper idle states
1756 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1757 */
1758 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001759 struct pinctrl *p = devm_pinctrl_get(host->dev);
1760 if (!p) {
1761 ret = -ENODEV;
1762 goto err_free_irq;
1763 }
1764 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1765 dev_info(host->dev, "missing default pinctrl state\n");
1766 devm_pinctrl_put(p);
1767 ret = -EINVAL;
1768 goto err_free_irq;
1769 }
1770
1771 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1772 dev_info(host->dev, "missing idle pinctrl state\n");
1773 devm_pinctrl_put(p);
1774 ret = -EINVAL;
1775 goto err_free_irq;
1776 }
1777 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001778 }
1779
Balaji T K5a52b082014-05-29 10:28:02 +02001780 OMAP_HSMMC_WRITE(host->base, HCTL,
1781 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001782 return 0;
1783
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001784err_free_irq:
1785 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001786err:
1787 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1788 host->wake_irq = 0;
1789 return ret;
1790}
1791
Denis Karpov70a33412009-09-22 16:44:59 -07001792static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001793{
1794 u32 hctl, capa, value;
1795
1796 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301797 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001798 hctl = SDVS30;
1799 capa = VS30 | VS18;
1800 } else {
1801 hctl = SDVS18;
1802 capa = VS18;
1803 }
1804
1805 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1806 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1807
1808 value = OMAP_HSMMC_READ(host->base, CAPA);
1809 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1810
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001811 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001812 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001813}
1814
Denis Karpov70a33412009-09-22 16:44:59 -07001815static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001816{
Denis Karpov70a33412009-09-22 16:44:59 -07001817 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001818
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301819 pm_runtime_get_sync(host->dev);
1820
Denis Karpovdd498ef2009-09-22 16:44:49 -07001821 return 0;
1822}
1823
Adrian Hunter907d2e72012-02-29 09:17:21 +02001824static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001825{
Denis Karpov70a33412009-09-22 16:44:59 -07001826 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001827
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301828 pm_runtime_mark_last_busy(host->dev);
1829 pm_runtime_put_autosuspend(host->dev);
1830
Denis Karpovdd498ef2009-09-22 16:44:49 -07001831 return 0;
1832}
1833
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001834static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1835 unsigned int direction, int blk_size)
1836{
1837 /* This controller can't do multiblock reads due to hw bugs */
1838 if (direction == MMC_DATA_READ)
1839 return 1;
1840
1841 return blk_size;
1842}
1843
1844static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001845 .enable = omap_hsmmc_enable_fclk,
1846 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001847 .post_req = omap_hsmmc_post_req,
1848 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001849 .request = omap_hsmmc_request,
1850 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001851 .get_cd = omap_hsmmc_get_cd,
1852 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001853 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001854 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001855};
1856
Denis Karpovd900f712009-09-22 16:44:38 -07001857#ifdef CONFIG_DEBUG_FS
1858
Denis Karpov70a33412009-09-22 16:44:59 -07001859static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001860{
1861 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001862 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001863
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001864 seq_printf(s, "mmc%d:\n", mmc->index);
1865 seq_printf(s, "sdio irq mode\t%s\n",
1866 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1867
1868 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1869 seq_printf(s, "sdio irq \t%s\n",
1870 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1871 : "disabled");
1872 }
1873 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001874
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301875 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001876 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001877 seq_printf(s, "CON:\t\t0x%08x\n",
1878 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001879 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1880 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001881 seq_printf(s, "HCTL:\t\t0x%08x\n",
1882 OMAP_HSMMC_READ(host->base, HCTL));
1883 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1884 OMAP_HSMMC_READ(host->base, SYSCTL));
1885 seq_printf(s, "IE:\t\t0x%08x\n",
1886 OMAP_HSMMC_READ(host->base, IE));
1887 seq_printf(s, "ISE:\t\t0x%08x\n",
1888 OMAP_HSMMC_READ(host->base, ISE));
1889 seq_printf(s, "CAPA:\t\t0x%08x\n",
1890 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001891
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301892 pm_runtime_mark_last_busy(host->dev);
1893 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001894
Denis Karpovd900f712009-09-22 16:44:38 -07001895 return 0;
1896}
1897
Denis Karpov70a33412009-09-22 16:44:59 -07001898static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001899{
Denis Karpov70a33412009-09-22 16:44:59 -07001900 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001901}
1902
1903static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001904 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001905 .read = seq_read,
1906 .llseek = seq_lseek,
1907 .release = single_release,
1908};
1909
Denis Karpov70a33412009-09-22 16:44:59 -07001910static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001911{
1912 if (mmc->debugfs_root)
1913 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1914 mmc, &mmc_regs_fops);
1915}
1916
1917#else
1918
Denis Karpov70a33412009-09-22 16:44:59 -07001919static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001920{
1921}
1922
1923#endif
1924
Rajendra Nayak46856a62012-03-12 20:32:37 +05301925#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001926static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1927 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1928 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1929};
1930
1931static const struct omap_mmc_of_data omap4_mmc_of_data = {
1932 .reg_offset = 0x100,
1933};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001934static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1935 .reg_offset = 0x100,
1936 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1937};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301938
1939static const struct of_device_id omap_mmc_of_match[] = {
1940 {
1941 .compatible = "ti,omap2-hsmmc",
1942 },
1943 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001944 .compatible = "ti,omap3-pre-es3-hsmmc",
1945 .data = &omap3_pre_es3_mmc_of_data,
1946 },
1947 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301948 .compatible = "ti,omap3-hsmmc",
1949 },
1950 {
1951 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001952 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301953 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001954 {
1955 .compatible = "ti,am33xx-hsmmc",
1956 .data = &am33xx_mmc_of_data,
1957 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301958 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001959};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301960MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1961
Andreas Fenkart551434382014-11-08 15:33:09 +01001962static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301963{
Andreas Fenkart551434382014-11-08 15:33:09 +01001964 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301965 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301966 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001967 int cd_gpio, wp_gpio;
1968
1969 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1970 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1971 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1972 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301973
1974 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1975 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301976 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301977
1978 if (of_find_property(np, "ti,dual-volt", NULL))
1979 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1980
1981 /* This driver only supports 1 slot */
1982 pdata->nr_slots = 1;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001983 pdata->switch_pin = cd_gpio;
1984 pdata->gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301985
1986 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001987 pdata->nonremovable = true;
1988 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301989 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001990 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301991 if (bus_width == 4)
Andreas Fenkart326119c2014-11-08 15:33:14 +01001992 pdata->caps |= MMC_CAP_4_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301993 else if (bus_width == 8)
Andreas Fenkart326119c2014-11-08 15:33:14 +01001994 pdata->caps |= MMC_CAP_8_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301995
1996 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001997 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301998
Daniel Mackd8714e82012-10-15 21:35:06 +05301999 if (!of_property_read_u32(np, "max-frequency", &max_freq))
2000 pdata->max_freq = max_freq;
2001
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302002 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002003 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302004
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002005 if (of_find_property(np, "keep-power-in-suspend", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002006 pdata->pm_caps |= MMC_PM_KEEP_POWER;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002007
2008 if (of_find_property(np, "enable-sdio-wakeup", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002009 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002010
Rajendra Nayak46856a62012-03-12 20:32:37 +05302011 return pdata;
2012}
2013#else
Andreas Fenkart551434382014-11-08 15:33:09 +01002014static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05302015 *of_get_hsmmc_pdata(struct device *dev)
2016{
Balaji T K19df45b2014-02-28 19:08:18 +05302017 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302018}
2019#endif
2020
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002021static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022{
Andreas Fenkart551434382014-11-08 15:33:09 +01002023 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002025 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002027 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302028 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002029 dma_cap_mask_t mask;
2030 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002031 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302032 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302033
2034 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2035 if (match) {
2036 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002037
2038 if (IS_ERR(pdata))
2039 return PTR_ERR(pdata);
2040
Rajendra Nayak46856a62012-03-12 20:32:37 +05302041 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002042 data = match->data;
2043 pdata->reg_offset = data->reg_offset;
2044 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302045 }
2046 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047
2048 if (pdata == NULL) {
2049 dev_err(&pdev->dev, "Platform Data is missing\n");
2050 return -ENXIO;
2051 }
2052
2053 if (pdata->nr_slots == 0) {
2054 dev_err(&pdev->dev, "No Slots\n");
2055 return -ENXIO;
2056 }
2057
2058 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2059 irq = platform_get_irq(pdev, 0);
2060 if (res == NULL || irq < 0)
2061 return -ENXIO;
2062
Balaji T K77fae212014-05-09 22:16:51 +05302063 base = devm_ioremap_resource(&pdev->dev, res);
2064 if (IS_ERR(base))
2065 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002067 ret = omap_hsmmc_gpio_init(pdata);
2068 if (ret)
2069 goto err;
2070
Denis Karpov70a33412009-09-22 16:44:59 -07002071 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002072 if (!mmc) {
2073 ret = -ENOMEM;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002074 goto err_alloc;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002075 }
2076
2077 host = mmc_priv(mmc);
2078 host->mmc = mmc;
2079 host->pdata = pdata;
2080 host->dev = &pdev->dev;
2081 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002082 host->dma_ch = -1;
2083 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05302085 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302086 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002087 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002088 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302089 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002090
2091 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002092
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002093 if (pdev->dev.of_node)
2094 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2095
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302096 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002097
Daniel Mackd418ed82012-02-19 13:20:33 +01002098 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2099
2100 if (pdata->max_freq > 0)
2101 mmc->f_max = pdata->max_freq;
2102 else
2103 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002104
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002105 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002106
Balaji T K96181952014-05-09 22:16:48 +05302107 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108 if (IS_ERR(host->fclk)) {
2109 ret = PTR_ERR(host->fclk);
2110 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111 goto err1;
2112 }
2113
Paul Walmsley9b682562011-10-06 14:50:35 -06002114 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2115 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 -07002116 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002117 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002118
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302119 pm_runtime_enable(host->dev);
2120 pm_runtime_get_sync(host->dev);
2121 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2122 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002123
Balaji T K92a3aeb2012-02-24 21:14:34 +05302124 omap_hsmmc_context_save(host);
2125
Balaji T K96181952014-05-09 22:16:48 +05302126 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302127 /*
2128 * MMC can still work without debounce clock.
2129 */
2130 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302131 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302132 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302133 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302134 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002135 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002136
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002137 /* Since we do only SG emulation, we can have as many segs
2138 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002139 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002140
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2142 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2143 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2144 mmc->max_seg_size = mmc->max_req_size;
2145
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002146 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002147 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002148
Andreas Fenkart326119c2014-11-08 15:33:14 +01002149 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002150 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002151 mmc->caps |= MMC_CAP_4_BIT_DATA;
2152
Andreas Fenkart326119c2014-11-08 15:33:14 +01002153 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002154 mmc->caps |= MMC_CAP_NONREMOVABLE;
2155
Andreas Fenkart326119c2014-11-08 15:33:14 +01002156 mmc->pm_caps = mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002157
Denis Karpov70a33412009-09-22 16:44:59 -07002158 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002159
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302160 if (!pdev->dev.of_node) {
2161 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2162 if (!res) {
2163 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2164 ret = -ENXIO;
2165 goto err_irq;
2166 }
2167 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002168
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302169 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2170 if (!res) {
2171 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2172 ret = -ENXIO;
2173 goto err_irq;
2174 }
2175 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002176 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002177
Russell King26b88522012-04-13 12:27:37 +01002178 dma_cap_zero(mask);
2179 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002180
Matt Porterd272fbf2013-05-10 17:42:34 +05302181 host->rx_chan =
2182 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2183 &rx_req, &pdev->dev, "rx");
2184
Russell King26b88522012-04-13 12:27:37 +01002185 if (!host->rx_chan) {
2186 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002187 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002188 goto err_irq;
2189 }
2190
Matt Porterd272fbf2013-05-10 17:42:34 +05302191 host->tx_chan =
2192 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2193 &tx_req, &pdev->dev, "tx");
2194
Russell King26b88522012-04-13 12:27:37 +01002195 if (!host->tx_chan) {
2196 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002197 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002198 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002199 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002200
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002201 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302202 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002203 mmc_hostname(mmc), host);
2204 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302205 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206 goto err_irq;
2207 }
2208
Andreas Fenkart326119c2014-11-08 15:33:14 +01002209 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002210 ret = omap_hsmmc_reg_get(host);
2211 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002212 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002213 host->use_reg = 1;
2214 }
2215
Andreas Fenkart326119c2014-11-08 15:33:14 +01002216 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002217
2218 /* Request IRQ for card detect */
Andreas Fenkart326119c2014-11-08 15:33:14 +01002219 if ((mmc_pdata(host)->card_detect_irq)) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302220 ret = devm_request_threaded_irq(&pdev->dev,
Andreas Fenkart326119c2014-11-08 15:33:14 +01002221 mmc_pdata(host)->card_detect_irq,
Balaji T K9fa0e052014-05-09 22:16:50 +05302222 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002223 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002224 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002225 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302226 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227 "Unable to grab MMC CD IRQ\n");
2228 goto err_irq_cd;
2229 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002230 pdata->suspend = omap_hsmmc_suspend_cdirq;
2231 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232 }
2233
Adrian Hunterb4175772010-05-26 14:42:06 -07002234 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002235
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002236 /*
2237 * For now, only support SDIO interrupt if we have a separate
2238 * wake-up interrupt configured from device tree. This is because
2239 * the wake-up interrupt is needed for idle state and some
2240 * platforms need special quirks. And we don't want to add new
2241 * legacy mux platform init code callbacks any longer as we
2242 * are moving to DT based booting anyways.
2243 */
2244 ret = omap_hsmmc_configure_wake_irq(host);
2245 if (!ret)
2246 mmc->caps |= MMC_CAP_SDIO_IRQ;
2247
Adrian Hunterb62f6222009-09-22 16:45:01 -07002248 omap_hsmmc_protect_card(host);
2249
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002250 mmc_add_host(mmc);
2251
Andreas Fenkart326119c2014-11-08 15:33:14 +01002252 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002253 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2254 if (ret < 0)
2255 goto err_slot_name;
2256 }
Andreas Fenkart326119c2014-11-08 15:33:14 +01002257 if (mmc_pdata(host)->card_detect_irq &&
2258 mmc_pdata(host)->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002259 ret = device_create_file(&mmc->class_dev,
2260 &dev_attr_cover_switch);
2261 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002262 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263 }
2264
Denis Karpov70a33412009-09-22 16:44:59 -07002265 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302266 pm_runtime_mark_last_busy(host->dev);
2267 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002268
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269 return 0;
2270
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002271err_slot_name:
2272 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002273err_irq_cd:
2274 if (host->use_reg)
2275 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002276err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002277 if (host->tx_chan)
2278 dma_release_channel(host->tx_chan);
2279 if (host->rx_chan)
2280 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302281 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002282 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302283 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302284 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002285err1:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002286 mmc_free_host(mmc);
2287err_alloc:
2288 omap_hsmmc_gpio_free(pdata);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002290 return ret;
2291}
2292
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002293static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002294{
Denis Karpov70a33412009-09-22 16:44:59 -07002295 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296
Felipe Balbi927ce942012-03-14 11:18:27 +02002297 pm_runtime_get_sync(host->dev);
2298 mmc_remove_host(host->mmc);
2299 if (host->use_reg)
2300 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002301
Russell Kingc5c98922012-04-13 12:14:39 +01002302 if (host->tx_chan)
2303 dma_release_channel(host->tx_chan);
2304 if (host->rx_chan)
2305 dma_release_channel(host->rx_chan);
2306
Felipe Balbi927ce942012-03-14 11:18:27 +02002307 pm_runtime_put_sync(host->dev);
2308 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302309 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302310 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002311
Balaji T K9ea28ec2012-10-15 21:35:08 +05302312 omap_hsmmc_gpio_free(host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302313 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002314
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002315 return 0;
2316}
2317
2318#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302319static int omap_hsmmc_prepare(struct device *dev)
2320{
2321 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2322
2323 if (host->pdata->suspend)
2324 return host->pdata->suspend(dev, host->slot_id);
2325
2326 return 0;
2327}
2328
2329static void omap_hsmmc_complete(struct device *dev)
2330{
2331 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2332
2333 if (host->pdata->resume)
2334 host->pdata->resume(dev, host->slot_id);
2335
2336}
2337
Kevin Hilmana791daa2010-05-26 14:42:07 -07002338static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002339{
Felipe Balbi927ce942012-03-14 11:18:27 +02002340 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2341
2342 if (!host)
2343 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002344
Felipe Balbi927ce942012-03-14 11:18:27 +02002345 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002346
2347 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002348 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2349 OMAP_HSMMC_WRITE(host->base, IE, 0);
2350 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002351 OMAP_HSMMC_WRITE(host->base, HCTL,
2352 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2353 }
2354
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002355 /* do not wake up due to sdio irq */
2356 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2357 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2358 disable_irq(host->wake_irq);
2359
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302360 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302361 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002362
Eliad Peller31f9d462011-11-22 16:02:17 +02002363 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002364 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002365}
2366
2367/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002368static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002369{
Felipe Balbi927ce942012-03-14 11:18:27 +02002370 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2371
2372 if (!host)
2373 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002374
Felipe Balbi927ce942012-03-14 11:18:27 +02002375 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002376
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302377 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302378 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002379
Felipe Balbi927ce942012-03-14 11:18:27 +02002380 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2381 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002382
Felipe Balbi927ce942012-03-14 11:18:27 +02002383 omap_hsmmc_protect_card(host);
2384
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002385 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2386 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2387 enable_irq(host->wake_irq);
2388
Felipe Balbi927ce942012-03-14 11:18:27 +02002389 pm_runtime_mark_last_busy(host->dev);
2390 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002391 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002392}
2393
2394#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302395#define omap_hsmmc_prepare NULL
2396#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002397#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302398#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002399#endif
2400
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302401static int omap_hsmmc_runtime_suspend(struct device *dev)
2402{
2403 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002404 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002405 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302406
2407 host = platform_get_drvdata(to_platform_device(dev));
2408 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002409 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302410
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002411 spin_lock_irqsave(&host->irq_lock, flags);
2412 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2413 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2414 /* disable sdio irq handling to prevent race */
2415 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2416 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002417
2418 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2419 /*
2420 * dat1 line low, pending sdio irq
2421 * race condition: possible irq handler running on
2422 * multi-core, abort
2423 */
2424 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2425 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2426 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2427 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2428 pm_runtime_mark_last_busy(dev);
2429 ret = -EBUSY;
2430 goto abort;
2431 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002432
Andreas Fenkart97978a42014-05-29 10:28:04 +02002433 pinctrl_pm_select_idle_state(dev);
2434
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002435 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2436 enable_irq(host->wake_irq);
2437 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002438 } else {
2439 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002440 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002441
Andreas Fenkartf9459012014-05-29 10:28:03 +02002442abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002443 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002444 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302445}
2446
2447static int omap_hsmmc_runtime_resume(struct device *dev)
2448{
2449 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002450 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302451
2452 host = platform_get_drvdata(to_platform_device(dev));
2453 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002454 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302455
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002456 spin_lock_irqsave(&host->irq_lock, flags);
2457 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2458 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2459 /* sdio irq flag can't change while in runtime suspend */
2460 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2461 disable_irq_nosync(host->wake_irq);
2462 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2463 }
2464
Andreas Fenkart97978a42014-05-29 10:28:04 +02002465 pinctrl_pm_select_default_state(host->dev);
2466
2467 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002468 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2469 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2470 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002471 } else {
2472 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002473 }
2474 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302475 return 0;
2476}
2477
Kevin Hilmana791daa2010-05-26 14:42:07 -07002478static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002479 .suspend = omap_hsmmc_suspend,
2480 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302481 .prepare = omap_hsmmc_prepare,
2482 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302483 .runtime_suspend = omap_hsmmc_runtime_suspend,
2484 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002485};
2486
2487static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002488 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002489 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002490 .driver = {
2491 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002492 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302493 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002494 },
2495};
2496
Felipe Balbib7964502012-03-14 11:18:32 +02002497module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002498MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2499MODULE_LICENSE("GPL");
2500MODULE_ALIAS("platform:" DRIVER_NAME);
2501MODULE_AUTHOR("Texas Instruments Inc");