blob: 82b40b85293fd2110ff04a6933a5044ac0f3c3e2 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f42011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
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 Fenkart55143432014-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 K03b5d922012-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;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200210 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700211 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700212 int protect_card;
213 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800214 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700215 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530216 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530217 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200218#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
219#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
220#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200221 struct omap_hsmmc_next next_data;
Andreas Fenkart55143432014-11-08 15:33:09 +0100222 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100223
224 /* To handle board related suspend/resume functionality for MMC */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100225 int (*suspend)(struct device *dev);
226 int (*resume)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100227
228 /* return MMC cover switch state, can be NULL if not supported.
229 *
230 * possible return values:
231 * 0 - closed
232 * 1 - open
233 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100234 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100235
236 /* Card detection IRQs */
237 int card_detect_irq;
238
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100239 int (*card_detect)(struct device *dev);
240 int (*get_ro)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100241
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100242};
243
Nishanth Menon59445b12014-02-13 23:45:48 -0600244struct omap_mmc_of_data {
245 u32 reg_offset;
246 u8 controller_flags;
247};
248
Balaji T Kbf129e12014-01-21 19:54:42 +0530249static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
250
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100251static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800252{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530253 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart55143432014-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
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100260static int omap_hsmmc_get_wp(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800261{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530262 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart55143432014-11-08 15:33:09 +0100263 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800264
265 /* NOTE: assumes write protect signal is active-high */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100266 return gpio_get_value_cansleep(mmc->gpio_wp);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800267}
268
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100269static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800270{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530271 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart55143432014-11-08 15:33:09 +0100272 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800273
274 /* NOTE: assumes card detect signal is active-low */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100275 return !gpio_get_value_cansleep(mmc->switch_pin);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800276}
277
278#ifdef CONFIG_PM
279
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100280static int omap_hsmmc_suspend_cdirq(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800281{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530282 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800283
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100284 disable_irq(host->card_detect_irq);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800285 return 0;
286}
287
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100288static int omap_hsmmc_resume_cdirq(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800289{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530290 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800291
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100292 enable_irq(host->card_detect_irq);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800293 return 0;
294}
295
296#else
297
298#define omap_hsmmc_suspend_cdirq NULL
299#define omap_hsmmc_resume_cdirq NULL
300
301#endif
302
Adrian Hunterb702b102010-02-15 10:03:35 -0800303#ifdef CONFIG_REGULATOR
304
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100305static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800306{
307 struct omap_hsmmc_host *host =
308 platform_get_drvdata(to_platform_device(dev));
309 int ret = 0;
310
311 /*
312 * If we don't see a Vcc regulator, assume it's a fixed
313 * voltage always-on regulator.
314 */
315 if (!host->vcc)
316 return 0;
317
Andreas Fenkart326119c2014-11-08 15:33:14 +0100318 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100319 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800320
Balaji T Ke99448f2014-02-19 20:26:40 +0530321 if (host->pbias) {
322 if (host->pbias_enabled == 1) {
323 ret = regulator_disable(host->pbias);
324 if (!ret)
325 host->pbias_enabled = 0;
326 }
327 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
328 }
329
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800330 /*
331 * Assume Vcc regulator is used only to power the card ... OMAP
332 * VDDS is used to power the pins, optionally with a transceiver to
333 * support cards using voltages other than VDDS (1.8V nominal). When a
334 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
335 *
336 * In some cases this regulator won't support enable/disable;
337 * e.g. it's a fixed rail for a WLAN chip.
338 *
339 * In other cases vcc_aux switches interface power. Example, for
340 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
341 * chips/cards need an interface voltage rail too.
342 */
343 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530344 if (host->vcc)
345 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800346 /* Enable interface voltage rail, if needed */
347 if (ret == 0 && host->vcc_aux) {
348 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530349 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400350 ret = mmc_regulator_set_ocr(host->mmc,
351 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800352 }
353 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400354 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800355 if (host->vcc_aux)
356 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530357 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400358 /* Then proceed to shut down the local regulator */
359 ret = mmc_regulator_set_ocr(host->mmc,
360 host->vcc, 0);
361 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800362 }
363
Balaji T Ke99448f2014-02-19 20:26:40 +0530364 if (host->pbias) {
365 if (vdd <= VDD_165_195)
366 ret = regulator_set_voltage(host->pbias, VDD_1V8,
367 VDD_1V8);
368 else
369 ret = regulator_set_voltage(host->pbias, VDD_3V0,
370 VDD_3V0);
371 if (ret < 0)
372 goto error_set_power;
373
374 if (host->pbias_enabled == 0) {
375 ret = regulator_enable(host->pbias);
376 if (!ret)
377 host->pbias_enabled = 1;
378 }
379 }
380
Andreas Fenkart326119c2014-11-08 15:33:14 +0100381 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100382 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800383
Balaji T Ke99448f2014-02-19 20:26:40 +0530384error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800385 return ret;
386}
387
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800388static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
389{
390 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700391 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800392
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530393 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800394 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530395 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
396 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400397 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800398 } else {
399 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700400 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100401 if (!mmc_pdata(host)->ocr_mask) {
402 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700403 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100404 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530405 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100406 mmc_pdata(host)->ocr_mask);
407 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700408 return -EINVAL;
409 }
410 }
Balaji T K987fd492014-02-19 20:26:40 +0530411 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100412 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800413
Balaji T K987fd492014-02-19 20:26:40 +0530414 /* Allow an aux regulator */
415 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
416 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800417
Balaji T Ke99448f2014-02-19 20:26:40 +0530418 reg = devm_regulator_get_optional(host->dev, "pbias");
419 host->pbias = IS_ERR(reg) ? NULL : reg;
420
Balaji T K987fd492014-02-19 20:26:40 +0530421 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100422 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530423 return 0;
424 /*
425 * To disable boot_on regulator, enable regulator
426 * to increase usecount and then disable it.
427 */
428 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
429 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100430 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300431
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100432 mmc_pdata(host)->set_power(host->dev, 1, vdd);
433 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800434 }
435
436 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800437}
438
439static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
440{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100441 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800442}
443
Adrian Hunterb702b102010-02-15 10:03:35 -0800444static inline int omap_hsmmc_have_reg(void)
445{
446 return 1;
447}
448
449#else
450
451static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
452{
453 return -EINVAL;
454}
455
456static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
457{
458}
459
460static inline int omap_hsmmc_have_reg(void)
461{
462 return 0;
463}
464
465#endif
466
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100467static int omap_hsmmc_gpio_init(struct omap_hsmmc_host *host,
468 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800469{
470 int ret;
471
Andreas Fenkart326119c2014-11-08 15:33:14 +0100472 if (gpio_is_valid(pdata->switch_pin)) {
473 if (pdata->cover)
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100474 host->get_cover_state =
475 omap_hsmmc_get_cover_state;
Adrian Hunterb702b102010-02-15 10:03:35 -0800476 else
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100477 host->card_detect = omap_hsmmc_card_detect;
478 host->card_detect_irq =
Andreas Fenkart326119c2014-11-08 15:33:14 +0100479 gpio_to_irq(pdata->switch_pin);
480 ret = gpio_request(pdata->switch_pin, "mmc_cd");
Adrian Hunterb702b102010-02-15 10:03:35 -0800481 if (ret)
482 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100483 ret = gpio_direction_input(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800484 if (ret)
485 goto err_free_sp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100486 } else {
487 pdata->switch_pin = -EINVAL;
488 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800489
Andreas Fenkart326119c2014-11-08 15:33:14 +0100490 if (gpio_is_valid(pdata->gpio_wp)) {
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100491 host->get_ro = omap_hsmmc_get_wp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100492 ret = gpio_request(pdata->gpio_wp, "mmc_wp");
Adrian Hunterb702b102010-02-15 10:03:35 -0800493 if (ret)
494 goto err_free_cd;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100495 ret = gpio_direction_input(pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800496 if (ret)
497 goto err_free_wp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100498 } else {
499 pdata->gpio_wp = -EINVAL;
500 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800501
502 return 0;
503
504err_free_wp:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100505 gpio_free(pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800506err_free_cd:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100507 if (gpio_is_valid(pdata->switch_pin))
Adrian Hunterb702b102010-02-15 10:03:35 -0800508err_free_sp:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100509 gpio_free(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800510 return ret;
511}
512
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100513static void omap_hsmmc_gpio_free(struct omap_hsmmc_host *host,
514 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800515{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100516 if (gpio_is_valid(pdata->gpio_wp))
517 gpio_free(pdata->gpio_wp);
518 if (gpio_is_valid(pdata->switch_pin))
519 gpio_free(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800520}
521
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100522/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300523 * Start clock to the card
524 */
525static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
526{
527 OMAP_HSMMC_WRITE(host->base, SYSCTL,
528 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
529}
530
531/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100532 * Stop clock to the card
533 */
Denis Karpov70a33412009-09-22 16:44:59 -0700534static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100535{
536 OMAP_HSMMC_WRITE(host->base, SYSCTL,
537 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
538 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900539 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100540}
541
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700542static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
543 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700544{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200545 u32 irq_mask = INT_EN_MASK;
546 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700547
548 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200549 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700550
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700551 /* Disable timeout for erases */
552 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530553 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700554
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200555 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700556 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
557 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200558
559 /* latch pending CIRQ, but don't signal MMC core */
560 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
561 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700562 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200563 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700564}
565
566static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
567{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200568 u32 irq_mask = 0;
569 unsigned long flags;
570
571 spin_lock_irqsave(&host->irq_lock, flags);
572 /* no transfer running but need to keep cirq if enabled */
573 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
574 irq_mask |= CIRQ_EN;
575 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
576 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700577 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200578 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700579}
580
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300581/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530582static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300583{
584 u16 dsor = 0;
585
586 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530587 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530588 if (dsor > CLKD_MAX)
589 dsor = CLKD_MAX;
Andy Shevchenkoac330f42011-05-10 15:51:54 +0300590 }
591
592 return dsor;
593}
594
Andy Shevchenko5934df22011-05-06 12:14:06 +0300595static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
596{
597 struct mmc_ios *ios = &host->mmc->ios;
598 unsigned long regval;
599 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530600 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300601
Venkatraman S8986d312012-08-07 19:10:38 +0530602 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300603
604 omap_hsmmc_stop_clock(host);
605
606 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
607 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530608 clkdiv = calc_divisor(host, ios);
609 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300610 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
611 OMAP_HSMMC_WRITE(host->base, SYSCTL,
612 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
613
614 /* Wait till the ICS bit is set */
615 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
616 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
617 && time_before(jiffies, timeout))
618 cpu_relax();
619
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530620 /*
621 * Enable High-Speed Support
622 * Pre-Requisites
623 * - Controller should support High-Speed-Enable Bit
624 * - Controller should not be using DDR Mode
625 * - Controller should advertise that it supports High Speed
626 * in capabilities register
627 * - MMC/SD clock coming out of controller > 25MHz
628 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100629 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900630 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530631 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
632 regval = OMAP_HSMMC_READ(host->base, HCTL);
633 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
634 regval |= HSPE;
635 else
636 regval &= ~HSPE;
637
638 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
639 }
640
Andy Shevchenko5934df22011-05-06 12:14:06 +0300641 omap_hsmmc_start_clock(host);
642}
643
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400644static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
645{
646 struct mmc_ios *ios = &host->mmc->ios;
647 u32 con;
648
649 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900650 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d922012-04-09 12:08:33 +0530651 con |= DDR; /* configure in DDR mode */
652 else
653 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400654 switch (ios->bus_width) {
655 case MMC_BUS_WIDTH_8:
656 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
657 break;
658 case MMC_BUS_WIDTH_4:
659 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
660 OMAP_HSMMC_WRITE(host->base, HCTL,
661 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
662 break;
663 case MMC_BUS_WIDTH_1:
664 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
665 OMAP_HSMMC_WRITE(host->base, HCTL,
666 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
667 break;
668 }
669}
670
671static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
672{
673 struct mmc_ios *ios = &host->mmc->ios;
674 u32 con;
675
676 con = OMAP_HSMMC_READ(host->base, CON);
677 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
678 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
679 else
680 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
681}
682
Denis Karpov11dd62a2009-09-22 16:44:43 -0700683#ifdef CONFIG_PM
684
685/*
686 * Restore the MMC host context, if it was lost as result of a
687 * power state change.
688 */
Denis Karpov70a33412009-09-22 16:44:59 -0700689static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700690{
691 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400692 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700693 unsigned long timeout;
694
Tony Lindgren0a82e062013-10-21 00:25:19 +0530695 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
696 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
697 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
698 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
699 return 0;
700
701 host->context_loss++;
702
Balaji T Kc2200ef2012-03-07 09:55:30 -0500703 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700704 if (host->power_mode != MMC_POWER_OFF &&
705 (1 << ios->vdd) <= MMC_VDD_23_24)
706 hctl = SDVS18;
707 else
708 hctl = SDVS30;
709 capa = VS30 | VS18;
710 } else {
711 hctl = SDVS18;
712 capa = VS18;
713 }
714
Balaji T K5a52b082014-05-29 10:28:02 +0200715 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
716 hctl |= IWE;
717
Denis Karpov11dd62a2009-09-22 16:44:43 -0700718 OMAP_HSMMC_WRITE(host->base, HCTL,
719 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
720
721 OMAP_HSMMC_WRITE(host->base, CAPA,
722 OMAP_HSMMC_READ(host->base, CAPA) | capa);
723
724 OMAP_HSMMC_WRITE(host->base, HCTL,
725 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
726
727 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
728 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
729 && time_before(jiffies, timeout))
730 ;
731
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200732 OMAP_HSMMC_WRITE(host->base, ISE, 0);
733 OMAP_HSMMC_WRITE(host->base, IE, 0);
734 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700735
736 /* Do not initialize card-specific things if the power is off */
737 if (host->power_mode == MMC_POWER_OFF)
738 goto out;
739
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400740 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700741
Andy Shevchenko5934df22011-05-06 12:14:06 +0300742 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700743
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400744 omap_hsmmc_set_bus_mode(host);
745
Denis Karpov11dd62a2009-09-22 16:44:43 -0700746out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530747 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
748 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700749 return 0;
750}
751
752/*
753 * Save the MMC host context (store the number of power state changes so far).
754 */
Denis Karpov70a33412009-09-22 16:44:59 -0700755static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700756{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530757 host->con = OMAP_HSMMC_READ(host->base, CON);
758 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
759 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
760 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700761}
762
763#else
764
Denis Karpov70a33412009-09-22 16:44:59 -0700765static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700766{
767 return 0;
768}
769
Denis Karpov70a33412009-09-22 16:44:59 -0700770static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700771{
772}
773
774#endif
775
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776/*
777 * Send init stream sequence to card
778 * before sending IDLE command
779 */
Denis Karpov70a33412009-09-22 16:44:59 -0700780static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781{
782 int reg = 0;
783 unsigned long timeout;
784
Adrian Hunterb62f6222009-09-22 16:45:01 -0700785 if (host->protect_card)
786 return;
787
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700789
790 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100791 OMAP_HSMMC_WRITE(host->base, CON,
792 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
793 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
794
795 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530796 while ((reg != CC_EN) && time_before(jiffies, timeout))
797 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100798
799 OMAP_HSMMC_WRITE(host->base, CON,
800 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700801
802 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
803 OMAP_HSMMC_READ(host->base, STAT);
804
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805 enable_irq(host->irq);
806}
807
808static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700809int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100810{
811 int r = 1;
812
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100813 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100814 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815 return r;
816}
817
818static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700819omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100820 char *buf)
821{
822 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700823 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824
Denis Karpov70a33412009-09-22 16:44:59 -0700825 return sprintf(buf, "%s\n",
826 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827}
828
Denis Karpov70a33412009-09-22 16:44:59 -0700829static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100830
831static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700832omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 char *buf)
834{
835 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700836 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100837
Andreas Fenkart326119c2014-11-08 15:33:14 +0100838 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100839}
840
Denis Karpov70a33412009-09-22 16:44:59 -0700841static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100842
843/*
844 * Configure the response type and send the cmd.
845 */
846static void
Denis Karpov70a33412009-09-22 16:44:59 -0700847omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100848 struct mmc_data *data)
849{
850 int cmdreg = 0, resptype = 0, cmdtype = 0;
851
Venkatraman S8986d312012-08-07 19:10:38 +0530852 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100853 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
854 host->cmd = cmd;
855
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700856 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100857
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200858 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859 if (cmd->flags & MMC_RSP_PRESENT) {
860 if (cmd->flags & MMC_RSP_136)
861 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200862 else if (cmd->flags & MMC_RSP_BUSY) {
863 resptype = 3;
864 host->response_busy = 1;
865 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100866 resptype = 2;
867 }
868
869 /*
870 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
871 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
872 * a val of 0x3, rest 0x0.
873 */
874 if (cmd == host->mrq->stop)
875 cmdtype = 0x3;
876
877 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
878
Balaji T Ka2e77152014-01-21 19:54:42 +0530879 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
880 host->mrq->sbc) {
881 cmdreg |= ACEN_ACMD23;
882 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
883 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100884 if (data) {
885 cmdreg |= DP_SELECT | MSBS | BCE;
886 if (data->flags & MMC_DATA_READ)
887 cmdreg |= DDIR;
888 else
889 cmdreg &= ~(DDIR);
890 }
891
892 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530893 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100894
Adrian Hunterb4175772010-05-26 14:42:06 -0700895 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700896
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100897 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
898 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
899}
900
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200901static int
Denis Karpov70a33412009-09-22 16:44:59 -0700902omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200903{
904 if (data->flags & MMC_DATA_WRITE)
905 return DMA_TO_DEVICE;
906 else
907 return DMA_FROM_DEVICE;
908}
909
Russell Kingc5c98922012-04-13 12:14:39 +0100910static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
911 struct mmc_data *data)
912{
913 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
914}
915
Adrian Hunterb4175772010-05-26 14:42:06 -0700916static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
917{
918 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530919 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700920
Venkatraman S31463b12012-04-09 12:08:34 +0530921 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700922 host->req_in_progress = 0;
923 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530924 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700925
926 omap_hsmmc_disable_irq(host);
927 /* Do not complete the request if DMA is still in progress */
928 if (mrq->data && host->use_dma && dma_ch != -1)
929 return;
930 host->mrq = NULL;
931 mmc_request_done(host->mmc, mrq);
932}
933
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100934/*
935 * Notify the transfer complete to MMC core
936 */
937static void
Denis Karpov70a33412009-09-22 16:44:59 -0700938omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200940 if (!data) {
941 struct mmc_request *mrq = host->mrq;
942
Adrian Hunter23050102009-09-22 16:44:57 -0700943 /* TC before CC from CMD6 - don't know why, but it happens */
944 if (host->cmd && host->cmd->opcode == 6 &&
945 host->response_busy) {
946 host->response_busy = 0;
947 return;
948 }
949
Adrian Hunterb4175772010-05-26 14:42:06 -0700950 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200951 return;
952 }
953
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100954 host->data = NULL;
955
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100956 if (!data->error)
957 data->bytes_xfered += data->blocks * (data->blksz);
958 else
959 data->bytes_xfered = 0;
960
Balaji T Kbf129e12014-01-21 19:54:42 +0530961 if (data->stop && (data->error || !host->mrq->sbc))
962 omap_hsmmc_start_command(host, data->stop, NULL);
963 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700964 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100965}
966
967/*
968 * Notify the core about command completion
969 */
970static void
Denis Karpov70a33412009-09-22 16:44:59 -0700971omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100972{
Balaji T Kbf129e12014-01-21 19:54:42 +0530973 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530974 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530975 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530976 omap_hsmmc_start_dma_transfer(host);
977 omap_hsmmc_start_command(host, host->mrq->cmd,
978 host->mrq->data);
979 return;
980 }
981
Balaji T K2177fa92014-05-09 22:16:52 +0530982 host->cmd = NULL;
983
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 if (cmd->flags & MMC_RSP_PRESENT) {
985 if (cmd->flags & MMC_RSP_136) {
986 /* response type 2 */
987 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
988 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
989 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
990 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
991 } else {
992 /* response types 1, 1b, 3, 4, 5, 6 */
993 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
994 }
995 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700996 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530997 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100998}
999
1000/*
1001 * DMA clean up for command errors
1002 */
Denis Karpov70a33412009-09-22 16:44:59 -07001003static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001004{
Adrian Hunterb4175772010-05-26 14:42:06 -07001005 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +05301006 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -07001007
Jarkko Lavinen82788ff2008-12-05 12:31:46 +02001008 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001009
Venkatraman S31463b12012-04-09 12:08:34 +05301010 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001011 dma_ch = host->dma_ch;
1012 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301013 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001014
1015 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001016 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1017
1018 dmaengine_terminate_all(chan);
1019 dma_unmap_sg(chan->device->dev,
1020 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001021 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001022
Per Forlin053bf342011-11-07 21:55:11 +05301023 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001024 }
1025 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026}
1027
1028/*
1029 * Readable error output
1030 */
1031#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001032static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001033{
1034 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001035 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001036 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1037 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1038 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1039 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001040 };
1041 char res[256];
1042 char *buf = res;
1043 int len, i;
1044
1045 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1046 buf += len;
1047
Denis Karpov70a33412009-09-22 16:44:59 -07001048 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001049 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001050 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051 buf += len;
1052 }
1053
Venkatraman S8986d312012-08-07 19:10:38 +05301054 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001055}
Adrian Hunter699b9582011-05-06 12:14:01 +03001056#else
1057static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1058 u32 status)
1059{
1060}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001061#endif /* CONFIG_MMC_DEBUG */
1062
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001063/*
1064 * MMC controller internal state machines reset
1065 *
1066 * Used to reset command or data internal state machines, using respectively
1067 * SRC or SRD bit of SYSCTL register
1068 * Can be called from interrupt context
1069 */
Denis Karpov70a33412009-09-22 16:44:59 -07001070static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1071 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001072{
1073 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301074 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001075
1076 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1077 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1078
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001079 /*
1080 * OMAP4 ES2 and greater has an updated reset logic.
1081 * Monitor a 0->1 transition first
1082 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001083 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001084 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001085 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301086 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001087 }
1088 i = 0;
1089
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001090 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1091 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301092 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001093
1094 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1095 dev_err(mmc_dev(host->mmc),
1096 "Timeout waiting on controller reset in %s\n",
1097 __func__);
1098}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099
Balaji T K25e18972012-11-19 21:59:55 +05301100static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1101 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301102{
Balaji T K25e18972012-11-19 21:59:55 +05301103 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301104 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301105 if (host->cmd)
1106 host->cmd->error = err;
1107 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301108
1109 if (host->data) {
1110 omap_hsmmc_reset_controller_fsm(host, SRD);
1111 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301112 } else if (host->mrq && host->mrq->cmd)
1113 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301114}
1115
Adrian Hunterb4175772010-05-26 14:42:06 -07001116static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001118 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001119 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301120 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001121
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001122 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301123 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001124
Venkatraman Sa7e96872012-11-19 22:00:01 +05301125 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001126 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001127
Venkatraman Sa7e96872012-11-19 22:00:01 +05301128 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301129 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301130 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301131 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301132 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301133 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1134
Balaji T Ka2e77152014-01-21 19:54:42 +05301135 if (status & ACE_EN) {
1136 u32 ac12;
1137 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1138 if (!(ac12 & ACNE) && host->mrq->sbc) {
1139 end_cmd = 1;
1140 if (ac12 & ACTO)
1141 error = -ETIMEDOUT;
1142 else if (ac12 & (ACCE | ACEB | ACIE))
1143 error = -EILSEQ;
1144 host->mrq->sbc->error = error;
1145 hsmmc_command_incomplete(host, error, end_cmd);
1146 }
1147 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1148 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301149 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301150 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301151 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001152 }
1153 }
1154
Francesco Lavra7472bab2013-06-29 08:25:12 +02001155 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301156 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001157 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301158 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001159 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001160}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001161
Adrian Hunterb4175772010-05-26 14:42:06 -07001162/*
1163 * MMC controller IRQ handler
1164 */
1165static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1166{
1167 struct omap_hsmmc_host *host = dev_id;
1168 int status;
1169
1170 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001171 while (status & (INT_EN_MASK | CIRQ_EN)) {
1172 if (host->req_in_progress)
1173 omap_hsmmc_do_irq(host, status);
1174
1175 if (status & CIRQ_EN)
1176 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301177
Adrian Hunterb4175772010-05-26 14:42:06 -07001178 /* Flush posted write */
1179 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301180 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001181
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001182 return IRQ_HANDLED;
1183}
1184
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001185static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1186{
1187 struct omap_hsmmc_host *host = dev_id;
1188
1189 /* cirq is level triggered, disable to avoid infinite loop */
1190 spin_lock(&host->irq_lock);
1191 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1192 disable_irq_nosync(host->wake_irq);
1193 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1194 }
1195 spin_unlock(&host->irq_lock);
1196 pm_request_resume(host->dev); /* no use counter */
1197
1198 return IRQ_HANDLED;
1199}
1200
Denis Karpov70a33412009-09-22 16:44:59 -07001201static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001202{
1203 unsigned long i;
1204
1205 OMAP_HSMMC_WRITE(host->base, HCTL,
1206 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1207 for (i = 0; i < loops_per_jiffy; i++) {
1208 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1209 break;
1210 cpu_relax();
1211 }
1212}
1213
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001214/*
David Brownelleb250822009-02-17 14:49:01 -08001215 * Switch MMC interface voltage ... only relevant for MMC1.
1216 *
1217 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1218 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1219 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001220 */
Denis Karpov70a33412009-09-22 16:44:59 -07001221static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001222{
1223 u32 reg_val = 0;
1224 int ret;
1225
1226 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301227 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301228 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301229 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001230
1231 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001232 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001233
1234 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001235 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001236 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301237 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301238 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301239 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001240
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001241 if (ret != 0)
1242 goto err;
1243
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001244 OMAP_HSMMC_WRITE(host->base, HCTL,
1245 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1246 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001247
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001248 /*
1249 * If a MMC dual voltage card is detected, the set_ios fn calls
1250 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001251 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001252 *
David Brownelleb250822009-02-17 14:49:01 -08001253 * Cope with a bit of slop in the range ... per data sheets:
1254 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1255 * but recommended values are 1.71V to 1.89V
1256 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1257 * but recommended values are 2.7V to 3.3V
1258 *
1259 * Board setup code shouldn't permit anything very out-of-range.
1260 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1261 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001262 */
David Brownelleb250822009-02-17 14:49:01 -08001263 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001265 else
1266 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001267
1268 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001269 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001270
1271 return 0;
1272err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301273 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001274 return ret;
1275}
1276
Adrian Hunterb62f6222009-09-22 16:45:01 -07001277/* Protect the card while the cover is open */
1278static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1279{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001280 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001281 return;
1282
1283 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001284 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001285 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301286 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001287 "card is now accessible\n",
1288 mmc_hostname(host->mmc));
1289 host->protect_card = 0;
1290 }
1291 } else {
1292 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301293 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001294 "card is now inaccessible\n",
1295 mmc_hostname(host->mmc));
1296 host->protect_card = 1;
1297 }
1298 }
1299}
1300
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001301/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001302 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001304static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001305{
NeilBrown7efab4f2011-12-30 12:35:13 +11001306 struct omap_hsmmc_host *host = dev_id;
Adrian Huntera6b22402009-09-22 16:44:45 -07001307 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001308
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001309 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001310
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001311 if (host->card_detect)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001312 carddetect = host->card_detect(host->dev);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001313 else {
1314 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001315 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001316 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001317
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001318 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001319 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001320 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001321 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322 return IRQ_HANDLED;
1323}
1324
Russell Kingc5c98922012-04-13 12:14:39 +01001325static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001326{
Russell Kingc5c98922012-04-13 12:14:39 +01001327 struct omap_hsmmc_host *host = param;
1328 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001329 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001330 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001331
Russell Kingc5c98922012-04-13 12:14:39 +01001332 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001333 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001334 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001335 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001336 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337
Adrian Hunter770d7432011-05-06 12:14:11 +03001338 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001339 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001340 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001341 dma_unmap_sg(chan->device->dev,
1342 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001343 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001344
1345 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001346 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001347 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001348
1349 /* If DMA has finished after TC, complete the request */
1350 if (!req_in_progress) {
1351 struct mmc_request *mrq = host->mrq;
1352
1353 host->mrq = NULL;
1354 mmc_request_done(host->mmc, mrq);
1355 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001356}
1357
Per Forlin9782aff2011-07-01 18:55:23 +02001358static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1359 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001360 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001361 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001362{
1363 int dma_len;
1364
1365 if (!next && data->host_cookie &&
1366 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301367 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001368 " host->next_data.cookie %d\n",
1369 __func__, data->host_cookie, host->next_data.cookie);
1370 data->host_cookie = 0;
1371 }
1372
1373 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001374 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001375 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001376 omap_hsmmc_get_dma_dir(host, data));
1377
1378 } else {
1379 dma_len = host->next_data.dma_len;
1380 host->next_data.dma_len = 0;
1381 }
1382
1383
1384 if (dma_len == 0)
1385 return -EINVAL;
1386
1387 if (next) {
1388 next->dma_len = dma_len;
1389 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1390 } else
1391 host->dma_len = dma_len;
1392
1393 return 0;
1394}
1395
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001396/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397 * Routine to configure and start DMA for the MMC card
1398 */
Balaji T K9d025332014-01-21 19:54:42 +05301399static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001400 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001401{
Russell King26b88522012-04-13 12:27:37 +01001402 struct dma_slave_config cfg;
1403 struct dma_async_tx_descriptor *tx;
1404 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001405 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001406 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001407
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001408 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001409 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001410 struct scatterlist *sgl;
1411
1412 sgl = data->sg + i;
1413 if (sgl->length % data->blksz)
1414 return -EINVAL;
1415 }
1416 if ((data->blksz % 4) != 0)
1417 /* REVISIT: The MMC buffer increments only when MSB is written.
1418 * Return error for blksz which is non multiple of four.
1419 */
1420 return -EINVAL;
1421
Adrian Hunterb4175772010-05-26 14:42:06 -07001422 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001423
Russell Kingc5c98922012-04-13 12:14:39 +01001424 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001425
Russell King26b88522012-04-13 12:27:37 +01001426 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1427 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1428 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1429 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1430 cfg.src_maxburst = data->blksz / 4;
1431 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001432
Russell King26b88522012-04-13 12:27:37 +01001433 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001434 if (ret)
1435 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001436
Russell King26b88522012-04-13 12:27:37 +01001437 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1438 if (ret)
1439 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001440
Russell King26b88522012-04-13 12:27:37 +01001441 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1442 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1443 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1444 if (!tx) {
1445 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1446 /* FIXME: cleanup */
1447 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001448 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001449
Russell King26b88522012-04-13 12:27:37 +01001450 tx->callback = omap_hsmmc_dma_callback;
1451 tx->callback_param = host;
1452
1453 /* Does not fail */
1454 dmaengine_submit(tx);
1455
1456 host->dma_ch = 1;
1457
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001458 return 0;
1459}
1460
Denis Karpov70a33412009-09-22 16:44:59 -07001461static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001462 unsigned int timeout_ns,
1463 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001464{
1465 unsigned int timeout, cycle_ns;
1466 uint32_t reg, clkd, dto = 0;
1467
1468 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1469 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1470 if (clkd == 0)
1471 clkd = 1;
1472
Balaji T K6e3076c2014-01-21 19:54:42 +05301473 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001474 timeout = timeout_ns / cycle_ns;
1475 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001476 if (timeout) {
1477 while ((timeout & 0x80000000) == 0) {
1478 dto += 1;
1479 timeout <<= 1;
1480 }
1481 dto = 31 - dto;
1482 timeout <<= 1;
1483 if (timeout && dto)
1484 dto += 1;
1485 if (dto >= 13)
1486 dto -= 13;
1487 else
1488 dto = 0;
1489 if (dto > 14)
1490 dto = 14;
1491 }
1492
1493 reg &= ~DTO_MASK;
1494 reg |= dto << DTO_SHIFT;
1495 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1496}
1497
Balaji T K9d025332014-01-21 19:54:42 +05301498static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1499{
1500 struct mmc_request *req = host->mrq;
1501 struct dma_chan *chan;
1502
1503 if (!req->data)
1504 return;
1505 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1506 | (req->data->blocks << 16));
1507 set_data_timeout(host, req->data->timeout_ns,
1508 req->data->timeout_clks);
1509 chan = omap_hsmmc_get_dma_chan(host, req->data);
1510 dma_async_issue_pending(chan);
1511}
1512
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001513/*
1514 * Configure block length for MMC/SD cards and initiate the transfer.
1515 */
1516static int
Denis Karpov70a33412009-09-22 16:44:59 -07001517omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001518{
1519 int ret;
1520 host->data = req->data;
1521
1522 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001523 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001524 /*
1525 * Set an arbitrary 100ms data timeout for commands with
1526 * busy signal.
1527 */
1528 if (req->cmd->flags & MMC_RSP_BUSY)
1529 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001530 return 0;
1531 }
1532
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001533 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301534 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001535 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301536 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001537 return ret;
1538 }
1539 }
1540 return 0;
1541}
1542
Per Forlin9782aff2011-07-01 18:55:23 +02001543static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1544 int err)
1545{
1546 struct omap_hsmmc_host *host = mmc_priv(mmc);
1547 struct mmc_data *data = mrq->data;
1548
Russell King26b88522012-04-13 12:27:37 +01001549 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001550 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001551
Russell King26b88522012-04-13 12:27:37 +01001552 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1553 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001554 data->host_cookie = 0;
1555 }
1556}
1557
1558static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1559 bool is_first_req)
1560{
1561 struct omap_hsmmc_host *host = mmc_priv(mmc);
1562
1563 if (mrq->data->host_cookie) {
1564 mrq->data->host_cookie = 0;
1565 return ;
1566 }
1567
Russell Kingc5c98922012-04-13 12:14:39 +01001568 if (host->use_dma) {
1569 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001570
Per Forlin9782aff2011-07-01 18:55:23 +02001571 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001572 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001573 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001574 }
Per Forlin9782aff2011-07-01 18:55:23 +02001575}
1576
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001577/*
1578 * Request function. for read/write operation
1579 */
Denis Karpov70a33412009-09-22 16:44:59 -07001580static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001581{
Denis Karpov70a33412009-09-22 16:44:59 -07001582 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001583 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001584
Adrian Hunterb4175772010-05-26 14:42:06 -07001585 BUG_ON(host->req_in_progress);
1586 BUG_ON(host->dma_ch != -1);
1587 if (host->protect_card) {
1588 if (host->reqs_blocked < 3) {
1589 /*
1590 * Ensure the controller is left in a consistent
1591 * state by resetting the command and data state
1592 * machines.
1593 */
1594 omap_hsmmc_reset_controller_fsm(host, SRD);
1595 omap_hsmmc_reset_controller_fsm(host, SRC);
1596 host->reqs_blocked += 1;
1597 }
1598 req->cmd->error = -EBADF;
1599 if (req->data)
1600 req->data->error = -EBADF;
1601 req->cmd->retries = 0;
1602 mmc_request_done(mmc, req);
1603 return;
1604 } else if (host->reqs_blocked)
1605 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001606 WARN_ON(host->mrq != NULL);
1607 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301608 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001609 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001610 if (err) {
1611 req->cmd->error = err;
1612 if (req->data)
1613 req->data->error = err;
1614 host->mrq = NULL;
1615 mmc_request_done(mmc, req);
1616 return;
1617 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301618 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301619 omap_hsmmc_start_command(host, req->sbc, NULL);
1620 return;
1621 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001622
Balaji T K9d025332014-01-21 19:54:42 +05301623 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001624 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001625}
1626
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001627/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001628static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001629{
Denis Karpov70a33412009-09-22 16:44:59 -07001630 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001631 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001632
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301633 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001634
Adrian Huntera3621462009-09-22 16:44:42 -07001635 if (ios->power_mode != host->power_mode) {
1636 switch (ios->power_mode) {
1637 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001638 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001639 break;
1640 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001641 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001642 break;
1643 case MMC_POWER_ON:
1644 do_send_init_stream = 1;
1645 break;
1646 }
1647 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001648 }
1649
Denis Karpovdd498ef2009-09-22 16:44:49 -07001650 /* FIXME: set registers based only on changes to ios */
1651
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001652 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001653
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301654 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001655 /* Only MMC1 can interface at 3V without some flavor
1656 * of external transceiver; but they all handle 1.8V.
1657 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001658 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301659 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001660 /*
1661 * The mmc_select_voltage fn of the core does
1662 * not seem to set the power_mode to
1663 * MMC_POWER_UP upon recalculating the voltage.
1664 * vdd 1.8v.
1665 */
Denis Karpov70a33412009-09-22 16:44:59 -07001666 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1667 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001668 "Switch operation failed\n");
1669 }
1670 }
1671
Andy Shevchenko5934df22011-05-06 12:14:06 +03001672 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001673
Adrian Huntera3621462009-09-22 16:44:42 -07001674 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001675 send_init_stream(host);
1676
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001677 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001678
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301679 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001680}
1681
1682static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1683{
Denis Karpov70a33412009-09-22 16:44:59 -07001684 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001685
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001686 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001687 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001688 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001689}
1690
1691static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1692{
Denis Karpov70a33412009-09-22 16:44:59 -07001693 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001694
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001695 if (!host->get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001696 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001697 return host->get_ro(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001698}
1699
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001700static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1701{
1702 struct omap_hsmmc_host *host = mmc_priv(mmc);
1703
Andreas Fenkart326119c2014-11-08 15:33:14 +01001704 if (mmc_pdata(host)->init_card)
1705 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001706}
1707
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001708static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1709{
1710 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001711 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001712 unsigned long flags;
1713
1714 spin_lock_irqsave(&host->irq_lock, flags);
1715
Balaji T K5a52b082014-05-29 10:28:02 +02001716 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001717 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1718 if (enable) {
1719 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1720 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001721 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001722 } else {
1723 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1724 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001725 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001726 }
Balaji T K5a52b082014-05-29 10:28:02 +02001727 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001728 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1729
1730 /*
1731 * if enable, piggy back detection on current request
1732 * but always disable immediately
1733 */
1734 if (!host->req_in_progress || !enable)
1735 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1736
1737 /* flush posted write */
1738 OMAP_HSMMC_READ(host->base, IE);
1739
1740 spin_unlock_irqrestore(&host->irq_lock, flags);
1741}
1742
1743static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1744{
1745 struct mmc_host *mmc = host->mmc;
1746 int ret;
1747
1748 /*
1749 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1750 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1751 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1752 * with functional clock disabled.
1753 */
1754 if (!host->dev->of_node || !host->wake_irq)
1755 return -ENODEV;
1756
1757 /* Prevent auto-enabling of IRQ */
1758 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1759 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1760 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1761 mmc_hostname(mmc), host);
1762 if (ret) {
1763 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1764 goto err;
1765 }
1766
1767 /*
1768 * Some omaps don't have wake-up path from deeper idle states
1769 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1770 */
1771 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001772 struct pinctrl *p = devm_pinctrl_get(host->dev);
1773 if (!p) {
1774 ret = -ENODEV;
1775 goto err_free_irq;
1776 }
1777 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1778 dev_info(host->dev, "missing default pinctrl state\n");
1779 devm_pinctrl_put(p);
1780 ret = -EINVAL;
1781 goto err_free_irq;
1782 }
1783
1784 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1785 dev_info(host->dev, "missing idle pinctrl state\n");
1786 devm_pinctrl_put(p);
1787 ret = -EINVAL;
1788 goto err_free_irq;
1789 }
1790 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001791 }
1792
Balaji T K5a52b082014-05-29 10:28:02 +02001793 OMAP_HSMMC_WRITE(host->base, HCTL,
1794 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001795 return 0;
1796
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001797err_free_irq:
1798 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001799err:
1800 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1801 host->wake_irq = 0;
1802 return ret;
1803}
1804
Denis Karpov70a33412009-09-22 16:44:59 -07001805static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001806{
1807 u32 hctl, capa, value;
1808
1809 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301810 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001811 hctl = SDVS30;
1812 capa = VS30 | VS18;
1813 } else {
1814 hctl = SDVS18;
1815 capa = VS18;
1816 }
1817
1818 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1819 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1820
1821 value = OMAP_HSMMC_READ(host->base, CAPA);
1822 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1823
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001824 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001825 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001826}
1827
Denis Karpov70a33412009-09-22 16:44:59 -07001828static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001829{
Denis Karpov70a33412009-09-22 16:44:59 -07001830 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001831
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301832 pm_runtime_get_sync(host->dev);
1833
Denis Karpovdd498ef2009-09-22 16:44:49 -07001834 return 0;
1835}
1836
Adrian Hunter907d2e72012-02-29 09:17:21 +02001837static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001838{
Denis Karpov70a33412009-09-22 16:44:59 -07001839 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001840
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301841 pm_runtime_mark_last_busy(host->dev);
1842 pm_runtime_put_autosuspend(host->dev);
1843
Denis Karpovdd498ef2009-09-22 16:44:49 -07001844 return 0;
1845}
1846
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001847static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1848 unsigned int direction, int blk_size)
1849{
1850 /* This controller can't do multiblock reads due to hw bugs */
1851 if (direction == MMC_DATA_READ)
1852 return 1;
1853
1854 return blk_size;
1855}
1856
1857static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001858 .enable = omap_hsmmc_enable_fclk,
1859 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001860 .post_req = omap_hsmmc_post_req,
1861 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001862 .request = omap_hsmmc_request,
1863 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001864 .get_cd = omap_hsmmc_get_cd,
1865 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001866 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001867 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001868};
1869
Denis Karpovd900f712009-09-22 16:44:38 -07001870#ifdef CONFIG_DEBUG_FS
1871
Denis Karpov70a33412009-09-22 16:44:59 -07001872static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001873{
1874 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001875 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001876
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001877 seq_printf(s, "mmc%d:\n", mmc->index);
1878 seq_printf(s, "sdio irq mode\t%s\n",
1879 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1880
1881 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1882 seq_printf(s, "sdio irq \t%s\n",
1883 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1884 : "disabled");
1885 }
1886 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001887
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301888 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001889 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001890 seq_printf(s, "CON:\t\t0x%08x\n",
1891 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001892 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1893 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001894 seq_printf(s, "HCTL:\t\t0x%08x\n",
1895 OMAP_HSMMC_READ(host->base, HCTL));
1896 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1897 OMAP_HSMMC_READ(host->base, SYSCTL));
1898 seq_printf(s, "IE:\t\t0x%08x\n",
1899 OMAP_HSMMC_READ(host->base, IE));
1900 seq_printf(s, "ISE:\t\t0x%08x\n",
1901 OMAP_HSMMC_READ(host->base, ISE));
1902 seq_printf(s, "CAPA:\t\t0x%08x\n",
1903 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001904
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301905 pm_runtime_mark_last_busy(host->dev);
1906 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001907
Denis Karpovd900f712009-09-22 16:44:38 -07001908 return 0;
1909}
1910
Denis Karpov70a33412009-09-22 16:44:59 -07001911static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001912{
Denis Karpov70a33412009-09-22 16:44:59 -07001913 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001914}
1915
1916static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001917 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001918 .read = seq_read,
1919 .llseek = seq_lseek,
1920 .release = single_release,
1921};
1922
Denis Karpov70a33412009-09-22 16:44:59 -07001923static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001924{
1925 if (mmc->debugfs_root)
1926 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1927 mmc, &mmc_regs_fops);
1928}
1929
1930#else
1931
Denis Karpov70a33412009-09-22 16:44:59 -07001932static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001933{
1934}
1935
1936#endif
1937
Rajendra Nayak46856a62012-03-12 20:32:37 +05301938#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001939static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1940 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1941 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1942};
1943
1944static const struct omap_mmc_of_data omap4_mmc_of_data = {
1945 .reg_offset = 0x100,
1946};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001947static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1948 .reg_offset = 0x100,
1949 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1950};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301951
1952static const struct of_device_id omap_mmc_of_match[] = {
1953 {
1954 .compatible = "ti,omap2-hsmmc",
1955 },
1956 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001957 .compatible = "ti,omap3-pre-es3-hsmmc",
1958 .data = &omap3_pre_es3_mmc_of_data,
1959 },
1960 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301961 .compatible = "ti,omap3-hsmmc",
1962 },
1963 {
1964 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001965 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301966 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001967 {
1968 .compatible = "ti,am33xx-hsmmc",
1969 .data = &am33xx_mmc_of_data,
1970 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301971 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001972};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301973MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1974
Andreas Fenkart55143432014-11-08 15:33:09 +01001975static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301976{
Andreas Fenkart55143432014-11-08 15:33:09 +01001977 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301978 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301979 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001980 int cd_gpio, wp_gpio;
1981
1982 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1983 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1984 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1985 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301986
1987 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1988 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301989 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301990
1991 if (of_find_property(np, "ti,dual-volt", NULL))
1992 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1993
Andreas Fenkart326119c2014-11-08 15:33:14 +01001994 pdata->switch_pin = cd_gpio;
1995 pdata->gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301996
1997 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001998 pdata->nonremovable = true;
1999 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302000 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04002001 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302002 if (bus_width == 4)
Andreas Fenkart326119c2014-11-08 15:33:14 +01002003 pdata->caps |= MMC_CAP_4_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302004 else if (bus_width == 8)
Andreas Fenkart326119c2014-11-08 15:33:14 +01002005 pdata->caps |= MMC_CAP_8_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302006
2007 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002008 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302009
Daniel Mackd8714e82012-10-15 21:35:06 +05302010 if (!of_property_read_u32(np, "max-frequency", &max_freq))
2011 pdata->max_freq = max_freq;
2012
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302013 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002014 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302015
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002016 if (of_find_property(np, "keep-power-in-suspend", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002017 pdata->pm_caps |= MMC_PM_KEEP_POWER;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002018
2019 if (of_find_property(np, "enable-sdio-wakeup", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002020 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002021
Rajendra Nayak46856a62012-03-12 20:32:37 +05302022 return pdata;
2023}
2024#else
Andreas Fenkart55143432014-11-08 15:33:09 +01002025static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05302026 *of_get_hsmmc_pdata(struct device *dev)
2027{
Balaji T K19df45b2014-02-28 19:08:18 +05302028 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302029}
2030#endif
2031
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002032static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002033{
Andreas Fenkart55143432014-11-08 15:33:09 +01002034 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002036 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002038 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302039 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002040 dma_cap_mask_t mask;
2041 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002042 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302043 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302044
2045 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2046 if (match) {
2047 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002048
2049 if (IS_ERR(pdata))
2050 return PTR_ERR(pdata);
2051
Rajendra Nayak46856a62012-03-12 20:32:37 +05302052 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002053 data = match->data;
2054 pdata->reg_offset = data->reg_offset;
2055 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302056 }
2057 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002058
2059 if (pdata == NULL) {
2060 dev_err(&pdev->dev, "Platform Data is missing\n");
2061 return -ENXIO;
2062 }
2063
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002064 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2065 irq = platform_get_irq(pdev, 0);
2066 if (res == NULL || irq < 0)
2067 return -ENXIO;
2068
Balaji T K77fae212014-05-09 22:16:51 +05302069 base = devm_ioremap_resource(&pdev->dev, res);
2070 if (IS_ERR(base))
2071 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002072
Denis Karpov70a33412009-09-22 16:44:59 -07002073 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074 if (!mmc) {
2075 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002076 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077 }
2078
2079 host = mmc_priv(mmc);
2080 host->mmc = mmc;
2081 host->pdata = pdata;
2082 host->dev = &pdev->dev;
2083 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084 host->dma_ch = -1;
2085 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302086 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302087 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002088 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002089 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302090 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002091
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002092 ret = omap_hsmmc_gpio_init(host, pdata);
2093 if (ret)
2094 goto err_gpio;
2095
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002096 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002097
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002098 if (pdev->dev.of_node)
2099 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2100
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302101 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002102
Daniel Mackd418ed82012-02-19 13:20:33 +01002103 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2104
2105 if (pdata->max_freq > 0)
2106 mmc->f_max = pdata->max_freq;
2107 else
2108 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002109
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002110 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111
Balaji T K96181952014-05-09 22:16:48 +05302112 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113 if (IS_ERR(host->fclk)) {
2114 ret = PTR_ERR(host->fclk);
2115 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002116 goto err1;
2117 }
2118
Paul Walmsley9b682562011-10-06 14:50:35 -06002119 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2120 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 -07002121 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002122 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002123
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302124 pm_runtime_enable(host->dev);
2125 pm_runtime_get_sync(host->dev);
2126 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2127 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002128
Balaji T K92a3aeb2012-02-24 21:14:34 +05302129 omap_hsmmc_context_save(host);
2130
Balaji T K96181952014-05-09 22:16:48 +05302131 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302132 /*
2133 * MMC can still work without debounce clock.
2134 */
2135 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302136 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302137 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302138 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302139 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002140 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002141
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002142 /* Since we do only SG emulation, we can have as many segs
2143 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002144 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002145
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002146 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2147 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2148 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2149 mmc->max_seg_size = mmc->max_req_size;
2150
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002151 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002152 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153
Andreas Fenkart326119c2014-11-08 15:33:14 +01002154 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002155 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002156 mmc->caps |= MMC_CAP_4_BIT_DATA;
2157
Andreas Fenkart326119c2014-11-08 15:33:14 +01002158 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002159 mmc->caps |= MMC_CAP_NONREMOVABLE;
2160
Andreas Fenkart326119c2014-11-08 15:33:14 +01002161 mmc->pm_caps = mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002162
Denis Karpov70a33412009-09-22 16:44:59 -07002163 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002164
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302165 if (!pdev->dev.of_node) {
2166 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2167 if (!res) {
2168 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2169 ret = -ENXIO;
2170 goto err_irq;
2171 }
2172 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002173
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302174 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2175 if (!res) {
2176 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2177 ret = -ENXIO;
2178 goto err_irq;
2179 }
2180 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002181 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182
Russell King26b88522012-04-13 12:27:37 +01002183 dma_cap_zero(mask);
2184 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002185
Matt Porterd272fbf2013-05-10 17:42:34 +05302186 host->rx_chan =
2187 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2188 &rx_req, &pdev->dev, "rx");
2189
Russell King26b88522012-04-13 12:27:37 +01002190 if (!host->rx_chan) {
2191 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002192 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002193 goto err_irq;
2194 }
2195
Matt Porterd272fbf2013-05-10 17:42:34 +05302196 host->tx_chan =
2197 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2198 &tx_req, &pdev->dev, "tx");
2199
Russell King26b88522012-04-13 12:27:37 +01002200 if (!host->tx_chan) {
2201 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002202 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002203 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002204 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002205
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302207 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 mmc_hostname(mmc), host);
2209 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302210 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211 goto err_irq;
2212 }
2213
Andreas Fenkart326119c2014-11-08 15:33:14 +01002214 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002215 ret = omap_hsmmc_reg_get(host);
2216 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002217 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002218 host->use_reg = 1;
2219 }
2220
Andreas Fenkart326119c2014-11-08 15:33:14 +01002221 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002222
2223 /* Request IRQ for card detect */
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002224 if (host->card_detect_irq) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302225 ret = devm_request_threaded_irq(&pdev->dev,
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002226 host->card_detect_irq,
Balaji T K9fa0e052014-05-09 22:16:50 +05302227 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002228 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002229 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002230 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302231 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232 "Unable to grab MMC CD IRQ\n");
2233 goto err_irq_cd;
2234 }
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002235 host->suspend = omap_hsmmc_suspend_cdirq;
2236 host->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237 }
2238
Adrian Hunterb4175772010-05-26 14:42:06 -07002239 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002240
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002241 /*
2242 * For now, only support SDIO interrupt if we have a separate
2243 * wake-up interrupt configured from device tree. This is because
2244 * the wake-up interrupt is needed for idle state and some
2245 * platforms need special quirks. And we don't want to add new
2246 * legacy mux platform init code callbacks any longer as we
2247 * are moving to DT based booting anyways.
2248 */
2249 ret = omap_hsmmc_configure_wake_irq(host);
2250 if (!ret)
2251 mmc->caps |= MMC_CAP_SDIO_IRQ;
2252
Adrian Hunterb62f6222009-09-22 16:45:01 -07002253 omap_hsmmc_protect_card(host);
2254
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002255 mmc_add_host(mmc);
2256
Andreas Fenkart326119c2014-11-08 15:33:14 +01002257 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002258 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2259 if (ret < 0)
2260 goto err_slot_name;
2261 }
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002262 if (host->card_detect_irq && host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002263 ret = device_create_file(&mmc->class_dev,
2264 &dev_attr_cover_switch);
2265 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002266 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002267 }
2268
Denis Karpov70a33412009-09-22 16:44:59 -07002269 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302270 pm_runtime_mark_last_busy(host->dev);
2271 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002272
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273 return 0;
2274
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002275err_slot_name:
2276 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002277err_irq_cd:
2278 if (host->use_reg)
2279 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002280err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002281 if (host->tx_chan)
2282 dma_release_channel(host->tx_chan);
2283 if (host->rx_chan)
2284 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302285 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002286 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302287 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302288 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002290 omap_hsmmc_gpio_free(host, pdata);
2291err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002292 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002293err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002294 return ret;
2295}
2296
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002297static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002298{
Denis Karpov70a33412009-09-22 16:44:59 -07002299 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002300
Felipe Balbi927ce942012-03-14 11:18:27 +02002301 pm_runtime_get_sync(host->dev);
2302 mmc_remove_host(host->mmc);
2303 if (host->use_reg)
2304 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002305
Russell Kingc5c98922012-04-13 12:14:39 +01002306 if (host->tx_chan)
2307 dma_release_channel(host->tx_chan);
2308 if (host->rx_chan)
2309 dma_release_channel(host->rx_chan);
2310
Felipe Balbi927ce942012-03-14 11:18:27 +02002311 pm_runtime_put_sync(host->dev);
2312 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302313 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302314 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002315
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002316 omap_hsmmc_gpio_free(host, host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302317 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002318
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002319 return 0;
2320}
2321
2322#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302323static int omap_hsmmc_prepare(struct device *dev)
2324{
2325 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2326
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002327 if (host->suspend)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01002328 return host->suspend(dev);
Felipe Balbia48ce882012-11-19 21:59:59 +05302329
2330 return 0;
2331}
2332
2333static void omap_hsmmc_complete(struct device *dev)
2334{
2335 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2336
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002337 if (host->resume)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01002338 host->resume(dev);
Felipe Balbia48ce882012-11-19 21:59:59 +05302339
2340}
2341
Kevin Hilmana791daa2010-05-26 14:42:07 -07002342static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002343{
Felipe Balbi927ce942012-03-14 11:18:27 +02002344 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2345
2346 if (!host)
2347 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002348
Felipe Balbi927ce942012-03-14 11:18:27 +02002349 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002350
2351 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002352 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2353 OMAP_HSMMC_WRITE(host->base, IE, 0);
2354 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002355 OMAP_HSMMC_WRITE(host->base, HCTL,
2356 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2357 }
2358
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002359 /* do not wake up due to sdio irq */
2360 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2361 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2362 disable_irq(host->wake_irq);
2363
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302364 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302365 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002366
Eliad Peller31f9d462011-11-22 16:02:17 +02002367 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002368 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002369}
2370
2371/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002372static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002373{
Felipe Balbi927ce942012-03-14 11:18:27 +02002374 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2375
2376 if (!host)
2377 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002378
Felipe Balbi927ce942012-03-14 11:18:27 +02002379 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002380
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302381 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302382 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002383
Felipe Balbi927ce942012-03-14 11:18:27 +02002384 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2385 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002386
Felipe Balbi927ce942012-03-14 11:18:27 +02002387 omap_hsmmc_protect_card(host);
2388
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002389 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2390 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2391 enable_irq(host->wake_irq);
2392
Felipe Balbi927ce942012-03-14 11:18:27 +02002393 pm_runtime_mark_last_busy(host->dev);
2394 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002395 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002396}
2397
2398#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302399#define omap_hsmmc_prepare NULL
2400#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002401#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302402#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002403#endif
2404
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302405static int omap_hsmmc_runtime_suspend(struct device *dev)
2406{
2407 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002408 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002409 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302410
2411 host = platform_get_drvdata(to_platform_device(dev));
2412 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002413 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302414
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002415 spin_lock_irqsave(&host->irq_lock, flags);
2416 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2417 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2418 /* disable sdio irq handling to prevent race */
2419 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2420 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002421
2422 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2423 /*
2424 * dat1 line low, pending sdio irq
2425 * race condition: possible irq handler running on
2426 * multi-core, abort
2427 */
2428 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2429 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2430 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2431 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2432 pm_runtime_mark_last_busy(dev);
2433 ret = -EBUSY;
2434 goto abort;
2435 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002436
Andreas Fenkart97978a42014-05-29 10:28:04 +02002437 pinctrl_pm_select_idle_state(dev);
2438
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002439 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2440 enable_irq(host->wake_irq);
2441 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002442 } else {
2443 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002444 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002445
Andreas Fenkartf9459012014-05-29 10:28:03 +02002446abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002447 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002448 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302449}
2450
2451static int omap_hsmmc_runtime_resume(struct device *dev)
2452{
2453 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002454 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302455
2456 host = platform_get_drvdata(to_platform_device(dev));
2457 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002458 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302459
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002460 spin_lock_irqsave(&host->irq_lock, flags);
2461 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2462 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2463 /* sdio irq flag can't change while in runtime suspend */
2464 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2465 disable_irq_nosync(host->wake_irq);
2466 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2467 }
2468
Andreas Fenkart97978a42014-05-29 10:28:04 +02002469 pinctrl_pm_select_default_state(host->dev);
2470
2471 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002472 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2473 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2474 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002475 } else {
2476 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002477 }
2478 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302479 return 0;
2480}
2481
Kevin Hilmana791daa2010-05-26 14:42:07 -07002482static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002483 .suspend = omap_hsmmc_suspend,
2484 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302485 .prepare = omap_hsmmc_prepare,
2486 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302487 .runtime_suspend = omap_hsmmc_runtime_suspend,
2488 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002489};
2490
2491static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002492 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002493 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002494 .driver = {
2495 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002496 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302497 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002498 },
2499};
2500
Felipe Balbib7964502012-03-14 11:18:32 +02002501module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002502MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2503MODULE_LICENSE("GPL");
2504MODULE_ALIAS("platform:" DRIVER_NAME);
2505MODULE_AUTHOR("Texas Instruments Inc");