blob: 7c71dcdcba8bdc4d5e59ff124e9f793f1dbf296d [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) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100631 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530632 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
633 regval = OMAP_HSMMC_READ(host->base, HCTL);
634 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
635 regval |= HSPE;
636 else
637 regval &= ~HSPE;
638
639 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
640 }
641
Andy Shevchenko5934df22011-05-06 12:14:06 +0300642 omap_hsmmc_start_clock(host);
643}
644
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400645static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
646{
647 struct mmc_ios *ios = &host->mmc->ios;
648 u32 con;
649
650 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100651 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
652 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d922012-04-09 12:08:33 +0530653 con |= DDR; /* configure in DDR mode */
654 else
655 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400656 switch (ios->bus_width) {
657 case MMC_BUS_WIDTH_8:
658 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
659 break;
660 case MMC_BUS_WIDTH_4:
661 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
662 OMAP_HSMMC_WRITE(host->base, HCTL,
663 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
664 break;
665 case MMC_BUS_WIDTH_1:
666 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
667 OMAP_HSMMC_WRITE(host->base, HCTL,
668 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
669 break;
670 }
671}
672
673static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
674{
675 struct mmc_ios *ios = &host->mmc->ios;
676 u32 con;
677
678 con = OMAP_HSMMC_READ(host->base, CON);
679 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
680 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
681 else
682 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
683}
684
Denis Karpov11dd62a2009-09-22 16:44:43 -0700685#ifdef CONFIG_PM
686
687/*
688 * Restore the MMC host context, if it was lost as result of a
689 * power state change.
690 */
Denis Karpov70a33412009-09-22 16:44:59 -0700691static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700692{
693 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400694 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700695 unsigned long timeout;
696
Tony Lindgren0a82e062013-10-21 00:25:19 +0530697 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
698 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
699 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
700 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
701 return 0;
702
703 host->context_loss++;
704
Balaji T Kc2200ef2012-03-07 09:55:30 -0500705 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700706 if (host->power_mode != MMC_POWER_OFF &&
707 (1 << ios->vdd) <= MMC_VDD_23_24)
708 hctl = SDVS18;
709 else
710 hctl = SDVS30;
711 capa = VS30 | VS18;
712 } else {
713 hctl = SDVS18;
714 capa = VS18;
715 }
716
Balaji T K5a52b082014-05-29 10:28:02 +0200717 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
718 hctl |= IWE;
719
Denis Karpov11dd62a2009-09-22 16:44:43 -0700720 OMAP_HSMMC_WRITE(host->base, HCTL,
721 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
722
723 OMAP_HSMMC_WRITE(host->base, CAPA,
724 OMAP_HSMMC_READ(host->base, CAPA) | capa);
725
726 OMAP_HSMMC_WRITE(host->base, HCTL,
727 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
728
729 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
730 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
731 && time_before(jiffies, timeout))
732 ;
733
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200734 OMAP_HSMMC_WRITE(host->base, ISE, 0);
735 OMAP_HSMMC_WRITE(host->base, IE, 0);
736 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700737
738 /* Do not initialize card-specific things if the power is off */
739 if (host->power_mode == MMC_POWER_OFF)
740 goto out;
741
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400742 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700743
Andy Shevchenko5934df22011-05-06 12:14:06 +0300744 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700745
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400746 omap_hsmmc_set_bus_mode(host);
747
Denis Karpov11dd62a2009-09-22 16:44:43 -0700748out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530749 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
750 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700751 return 0;
752}
753
754/*
755 * Save the MMC host context (store the number of power state changes so far).
756 */
Denis Karpov70a33412009-09-22 16:44:59 -0700757static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700758{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530759 host->con = OMAP_HSMMC_READ(host->base, CON);
760 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
761 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
762 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700763}
764
765#else
766
Denis Karpov70a33412009-09-22 16:44:59 -0700767static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700768{
769 return 0;
770}
771
Denis Karpov70a33412009-09-22 16:44:59 -0700772static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700773{
774}
775
776#endif
777
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100778/*
779 * Send init stream sequence to card
780 * before sending IDLE command
781 */
Denis Karpov70a33412009-09-22 16:44:59 -0700782static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783{
784 int reg = 0;
785 unsigned long timeout;
786
Adrian Hunterb62f6222009-09-22 16:45:01 -0700787 if (host->protect_card)
788 return;
789
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100790 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700791
792 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100793 OMAP_HSMMC_WRITE(host->base, CON,
794 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
795 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
796
797 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530798 while ((reg != CC_EN) && time_before(jiffies, timeout))
799 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100800
801 OMAP_HSMMC_WRITE(host->base, CON,
802 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700803
804 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
805 OMAP_HSMMC_READ(host->base, STAT);
806
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100807 enable_irq(host->irq);
808}
809
810static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700811int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100812{
813 int r = 1;
814
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100815 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100816 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100817 return r;
818}
819
820static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700821omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100822 char *buf)
823{
824 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700825 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100826
Denis Karpov70a33412009-09-22 16:44:59 -0700827 return sprintf(buf, "%s\n",
828 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100829}
830
Denis Karpov70a33412009-09-22 16:44:59 -0700831static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100832
833static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700834omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100835 char *buf)
836{
837 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700838 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100839
Andreas Fenkart326119c2014-11-08 15:33:14 +0100840 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100841}
842
Denis Karpov70a33412009-09-22 16:44:59 -0700843static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844
845/*
846 * Configure the response type and send the cmd.
847 */
848static void
Denis Karpov70a33412009-09-22 16:44:59 -0700849omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100850 struct mmc_data *data)
851{
852 int cmdreg = 0, resptype = 0, cmdtype = 0;
853
Venkatraman S8986d312012-08-07 19:10:38 +0530854 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100855 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
856 host->cmd = cmd;
857
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700858 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100859
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200860 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100861 if (cmd->flags & MMC_RSP_PRESENT) {
862 if (cmd->flags & MMC_RSP_136)
863 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200864 else if (cmd->flags & MMC_RSP_BUSY) {
865 resptype = 3;
866 host->response_busy = 1;
867 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100868 resptype = 2;
869 }
870
871 /*
872 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
873 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
874 * a val of 0x3, rest 0x0.
875 */
876 if (cmd == host->mrq->stop)
877 cmdtype = 0x3;
878
879 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
880
Balaji T Ka2e77152014-01-21 19:54:42 +0530881 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
882 host->mrq->sbc) {
883 cmdreg |= ACEN_ACMD23;
884 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
885 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100886 if (data) {
887 cmdreg |= DP_SELECT | MSBS | BCE;
888 if (data->flags & MMC_DATA_READ)
889 cmdreg |= DDIR;
890 else
891 cmdreg &= ~(DDIR);
892 }
893
894 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530895 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100896
Adrian Hunterb4175772010-05-26 14:42:06 -0700897 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700898
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100899 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
900 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
901}
902
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200903static int
Denis Karpov70a33412009-09-22 16:44:59 -0700904omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200905{
906 if (data->flags & MMC_DATA_WRITE)
907 return DMA_TO_DEVICE;
908 else
909 return DMA_FROM_DEVICE;
910}
911
Russell Kingc5c98922012-04-13 12:14:39 +0100912static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
913 struct mmc_data *data)
914{
915 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
916}
917
Adrian Hunterb4175772010-05-26 14:42:06 -0700918static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
919{
920 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530921 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700922
Venkatraman S31463b12012-04-09 12:08:34 +0530923 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700924 host->req_in_progress = 0;
925 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530926 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700927
928 omap_hsmmc_disable_irq(host);
929 /* Do not complete the request if DMA is still in progress */
930 if (mrq->data && host->use_dma && dma_ch != -1)
931 return;
932 host->mrq = NULL;
933 mmc_request_done(host->mmc, mrq);
934}
935
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100936/*
937 * Notify the transfer complete to MMC core
938 */
939static void
Denis Karpov70a33412009-09-22 16:44:59 -0700940omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100941{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200942 if (!data) {
943 struct mmc_request *mrq = host->mrq;
944
Adrian Hunter23050102009-09-22 16:44:57 -0700945 /* TC before CC from CMD6 - don't know why, but it happens */
946 if (host->cmd && host->cmd->opcode == 6 &&
947 host->response_busy) {
948 host->response_busy = 0;
949 return;
950 }
951
Adrian Hunterb4175772010-05-26 14:42:06 -0700952 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200953 return;
954 }
955
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100956 host->data = NULL;
957
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958 if (!data->error)
959 data->bytes_xfered += data->blocks * (data->blksz);
960 else
961 data->bytes_xfered = 0;
962
Balaji T Kbf129e12014-01-21 19:54:42 +0530963 if (data->stop && (data->error || !host->mrq->sbc))
964 omap_hsmmc_start_command(host, data->stop, NULL);
965 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700966 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100967}
968
969/*
970 * Notify the core about command completion
971 */
972static void
Denis Karpov70a33412009-09-22 16:44:59 -0700973omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100974{
Balaji T Kbf129e12014-01-21 19:54:42 +0530975 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530976 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530977 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530978 omap_hsmmc_start_dma_transfer(host);
979 omap_hsmmc_start_command(host, host->mrq->cmd,
980 host->mrq->data);
981 return;
982 }
983
Balaji T K2177fa92014-05-09 22:16:52 +0530984 host->cmd = NULL;
985
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 if (cmd->flags & MMC_RSP_PRESENT) {
987 if (cmd->flags & MMC_RSP_136) {
988 /* response type 2 */
989 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
990 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
991 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
992 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
993 } else {
994 /* response types 1, 1b, 3, 4, 5, 6 */
995 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
996 }
997 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700998 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530999 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001000}
1001
1002/*
1003 * DMA clean up for command errors
1004 */
Denis Karpov70a33412009-09-22 16:44:59 -07001005static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001006{
Adrian Hunterb4175772010-05-26 14:42:06 -07001007 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +05301008 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -07001009
Jarkko Lavinen82788ff2008-12-05 12:31:46 +02001010 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001011
Venkatraman S31463b12012-04-09 12:08:34 +05301012 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001013 dma_ch = host->dma_ch;
1014 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +05301015 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -07001016
1017 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001018 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1019
1020 dmaengine_terminate_all(chan);
1021 dma_unmap_sg(chan->device->dev,
1022 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001023 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001024
Per Forlin053bf342011-11-07 21:55:11 +05301025 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001026 }
1027 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001028}
1029
1030/*
1031 * Readable error output
1032 */
1033#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001034static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001035{
1036 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001037 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001038 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1039 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1040 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1041 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001042 };
1043 char res[256];
1044 char *buf = res;
1045 int len, i;
1046
1047 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1048 buf += len;
1049
Denis Karpov70a33412009-09-22 16:44:59 -07001050 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001051 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001052 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053 buf += len;
1054 }
1055
Venkatraman S8986d312012-08-07 19:10:38 +05301056 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001057}
Adrian Hunter699b9582011-05-06 12:14:01 +03001058#else
1059static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1060 u32 status)
1061{
1062}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001063#endif /* CONFIG_MMC_DEBUG */
1064
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001065/*
1066 * MMC controller internal state machines reset
1067 *
1068 * Used to reset command or data internal state machines, using respectively
1069 * SRC or SRD bit of SYSCTL register
1070 * Can be called from interrupt context
1071 */
Denis Karpov70a33412009-09-22 16:44:59 -07001072static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1073 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001074{
1075 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301076 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001077
1078 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1079 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1080
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001081 /*
1082 * OMAP4 ES2 and greater has an updated reset logic.
1083 * Monitor a 0->1 transition first
1084 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001085 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001086 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001087 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301088 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001089 }
1090 i = 0;
1091
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001092 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1093 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301094 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001095
1096 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1097 dev_err(mmc_dev(host->mmc),
1098 "Timeout waiting on controller reset in %s\n",
1099 __func__);
1100}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101
Balaji T K25e18972012-11-19 21:59:55 +05301102static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1103 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301104{
Balaji T K25e18972012-11-19 21:59:55 +05301105 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301106 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301107 if (host->cmd)
1108 host->cmd->error = err;
1109 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301110
1111 if (host->data) {
1112 omap_hsmmc_reset_controller_fsm(host, SRD);
1113 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301114 } else if (host->mrq && host->mrq->cmd)
1115 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301116}
1117
Adrian Hunterb4175772010-05-26 14:42:06 -07001118static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001119{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001120 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001121 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301122 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001123
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001124 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301125 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001126
Venkatraman Sa7e96872012-11-19 22:00:01 +05301127 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001128 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001129
Venkatraman Sa7e96872012-11-19 22:00:01 +05301130 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301131 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301132 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301133 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301134 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301135 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1136
Balaji T Ka2e77152014-01-21 19:54:42 +05301137 if (status & ACE_EN) {
1138 u32 ac12;
1139 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1140 if (!(ac12 & ACNE) && host->mrq->sbc) {
1141 end_cmd = 1;
1142 if (ac12 & ACTO)
1143 error = -ETIMEDOUT;
1144 else if (ac12 & (ACCE | ACEB | ACIE))
1145 error = -EILSEQ;
1146 host->mrq->sbc->error = error;
1147 hsmmc_command_incomplete(host, error, end_cmd);
1148 }
1149 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1150 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301151 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301152 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301153 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001154 }
1155 }
1156
Francesco Lavra7472bab2013-06-29 08:25:12 +02001157 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301158 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001159 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301160 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001161 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001162}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001163
Adrian Hunterb4175772010-05-26 14:42:06 -07001164/*
1165 * MMC controller IRQ handler
1166 */
1167static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1168{
1169 struct omap_hsmmc_host *host = dev_id;
1170 int status;
1171
1172 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001173 while (status & (INT_EN_MASK | CIRQ_EN)) {
1174 if (host->req_in_progress)
1175 omap_hsmmc_do_irq(host, status);
1176
1177 if (status & CIRQ_EN)
1178 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301179
Adrian Hunterb4175772010-05-26 14:42:06 -07001180 /* Flush posted write */
1181 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301182 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001183
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184 return IRQ_HANDLED;
1185}
1186
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001187static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1188{
1189 struct omap_hsmmc_host *host = dev_id;
1190
1191 /* cirq is level triggered, disable to avoid infinite loop */
1192 spin_lock(&host->irq_lock);
1193 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1194 disable_irq_nosync(host->wake_irq);
1195 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1196 }
1197 spin_unlock(&host->irq_lock);
1198 pm_request_resume(host->dev); /* no use counter */
1199
1200 return IRQ_HANDLED;
1201}
1202
Denis Karpov70a33412009-09-22 16:44:59 -07001203static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001204{
1205 unsigned long i;
1206
1207 OMAP_HSMMC_WRITE(host->base, HCTL,
1208 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1209 for (i = 0; i < loops_per_jiffy; i++) {
1210 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1211 break;
1212 cpu_relax();
1213 }
1214}
1215
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001216/*
David Brownelleb250822009-02-17 14:49:01 -08001217 * Switch MMC interface voltage ... only relevant for MMC1.
1218 *
1219 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1220 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1221 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001222 */
Denis Karpov70a33412009-09-22 16:44:59 -07001223static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001224{
1225 u32 reg_val = 0;
1226 int ret;
1227
1228 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301229 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301230 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301231 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001232
1233 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001234 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001235
1236 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001237 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001238 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301239 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301240 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301241 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001242
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001243 if (ret != 0)
1244 goto err;
1245
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001246 OMAP_HSMMC_WRITE(host->base, HCTL,
1247 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1248 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001249
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001250 /*
1251 * If a MMC dual voltage card is detected, the set_ios fn calls
1252 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001253 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001254 *
David Brownelleb250822009-02-17 14:49:01 -08001255 * Cope with a bit of slop in the range ... per data sheets:
1256 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1257 * but recommended values are 1.71V to 1.89V
1258 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1259 * but recommended values are 2.7V to 3.3V
1260 *
1261 * Board setup code shouldn't permit anything very out-of-range.
1262 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1263 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264 */
David Brownelleb250822009-02-17 14:49:01 -08001265 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001266 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001267 else
1268 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001269
1270 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001271 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001272
1273 return 0;
1274err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301275 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001276 return ret;
1277}
1278
Adrian Hunterb62f6222009-09-22 16:45:01 -07001279/* Protect the card while the cover is open */
1280static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1281{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001282 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001283 return;
1284
1285 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001286 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001287 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301288 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001289 "card is now accessible\n",
1290 mmc_hostname(host->mmc));
1291 host->protect_card = 0;
1292 }
1293 } else {
1294 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301295 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001296 "card is now inaccessible\n",
1297 mmc_hostname(host->mmc));
1298 host->protect_card = 1;
1299 }
1300 }
1301}
1302
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001304 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001305 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001306static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001307{
NeilBrown7efab4f2011-12-30 12:35:13 +11001308 struct omap_hsmmc_host *host = dev_id;
Adrian Huntera6b22402009-09-22 16:44:45 -07001309 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001310
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001311 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001312
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001313 if (host->card_detect)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001314 carddetect = host->card_detect(host->dev);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001315 else {
1316 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001317 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001318 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001319
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001320 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001321 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001322 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001323 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324 return IRQ_HANDLED;
1325}
1326
Russell Kingc5c98922012-04-13 12:14:39 +01001327static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001328{
Russell Kingc5c98922012-04-13 12:14:39 +01001329 struct omap_hsmmc_host *host = param;
1330 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001331 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001332 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333
Russell Kingc5c98922012-04-13 12:14:39 +01001334 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001335 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001336 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001337 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001338 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001339
Adrian Hunter770d7432011-05-06 12:14:11 +03001340 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001341 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001342 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001343 dma_unmap_sg(chan->device->dev,
1344 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001345 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001346
1347 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001348 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001349 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001350
1351 /* If DMA has finished after TC, complete the request */
1352 if (!req_in_progress) {
1353 struct mmc_request *mrq = host->mrq;
1354
1355 host->mrq = NULL;
1356 mmc_request_done(host->mmc, mrq);
1357 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358}
1359
Per Forlin9782aff2011-07-01 18:55:23 +02001360static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1361 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001362 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001363 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001364{
1365 int dma_len;
1366
1367 if (!next && data->host_cookie &&
1368 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301369 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001370 " host->next_data.cookie %d\n",
1371 __func__, data->host_cookie, host->next_data.cookie);
1372 data->host_cookie = 0;
1373 }
1374
1375 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001376 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001377 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001378 omap_hsmmc_get_dma_dir(host, data));
1379
1380 } else {
1381 dma_len = host->next_data.dma_len;
1382 host->next_data.dma_len = 0;
1383 }
1384
1385
1386 if (dma_len == 0)
1387 return -EINVAL;
1388
1389 if (next) {
1390 next->dma_len = dma_len;
1391 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1392 } else
1393 host->dma_len = dma_len;
1394
1395 return 0;
1396}
1397
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001398/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001399 * Routine to configure and start DMA for the MMC card
1400 */
Balaji T K9d025332014-01-21 19:54:42 +05301401static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001402 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001403{
Russell King26b88522012-04-13 12:27:37 +01001404 struct dma_slave_config cfg;
1405 struct dma_async_tx_descriptor *tx;
1406 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001407 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001408 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001409
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001410 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001411 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001412 struct scatterlist *sgl;
1413
1414 sgl = data->sg + i;
1415 if (sgl->length % data->blksz)
1416 return -EINVAL;
1417 }
1418 if ((data->blksz % 4) != 0)
1419 /* REVISIT: The MMC buffer increments only when MSB is written.
1420 * Return error for blksz which is non multiple of four.
1421 */
1422 return -EINVAL;
1423
Adrian Hunterb4175772010-05-26 14:42:06 -07001424 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001425
Russell Kingc5c98922012-04-13 12:14:39 +01001426 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001427
Russell King26b88522012-04-13 12:27:37 +01001428 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1429 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1430 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1431 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1432 cfg.src_maxburst = data->blksz / 4;
1433 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001434
Russell King26b88522012-04-13 12:27:37 +01001435 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001436 if (ret)
1437 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001438
Russell King26b88522012-04-13 12:27:37 +01001439 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1440 if (ret)
1441 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001442
Russell King26b88522012-04-13 12:27:37 +01001443 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1444 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1445 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1446 if (!tx) {
1447 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1448 /* FIXME: cleanup */
1449 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001450 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451
Russell King26b88522012-04-13 12:27:37 +01001452 tx->callback = omap_hsmmc_dma_callback;
1453 tx->callback_param = host;
1454
1455 /* Does not fail */
1456 dmaengine_submit(tx);
1457
1458 host->dma_ch = 1;
1459
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001460 return 0;
1461}
1462
Denis Karpov70a33412009-09-22 16:44:59 -07001463static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001464 unsigned int timeout_ns,
1465 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001466{
1467 unsigned int timeout, cycle_ns;
1468 uint32_t reg, clkd, dto = 0;
1469
1470 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1471 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1472 if (clkd == 0)
1473 clkd = 1;
1474
Balaji T K6e3076c2014-01-21 19:54:42 +05301475 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001476 timeout = timeout_ns / cycle_ns;
1477 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001478 if (timeout) {
1479 while ((timeout & 0x80000000) == 0) {
1480 dto += 1;
1481 timeout <<= 1;
1482 }
1483 dto = 31 - dto;
1484 timeout <<= 1;
1485 if (timeout && dto)
1486 dto += 1;
1487 if (dto >= 13)
1488 dto -= 13;
1489 else
1490 dto = 0;
1491 if (dto > 14)
1492 dto = 14;
1493 }
1494
1495 reg &= ~DTO_MASK;
1496 reg |= dto << DTO_SHIFT;
1497 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1498}
1499
Balaji T K9d025332014-01-21 19:54:42 +05301500static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1501{
1502 struct mmc_request *req = host->mrq;
1503 struct dma_chan *chan;
1504
1505 if (!req->data)
1506 return;
1507 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1508 | (req->data->blocks << 16));
1509 set_data_timeout(host, req->data->timeout_ns,
1510 req->data->timeout_clks);
1511 chan = omap_hsmmc_get_dma_chan(host, req->data);
1512 dma_async_issue_pending(chan);
1513}
1514
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001515/*
1516 * Configure block length for MMC/SD cards and initiate the transfer.
1517 */
1518static int
Denis Karpov70a33412009-09-22 16:44:59 -07001519omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520{
1521 int ret;
1522 host->data = req->data;
1523
1524 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001525 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001526 /*
1527 * Set an arbitrary 100ms data timeout for commands with
1528 * busy signal.
1529 */
1530 if (req->cmd->flags & MMC_RSP_BUSY)
1531 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001532 return 0;
1533 }
1534
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001535 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301536 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001537 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301538 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001539 return ret;
1540 }
1541 }
1542 return 0;
1543}
1544
Per Forlin9782aff2011-07-01 18:55:23 +02001545static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1546 int err)
1547{
1548 struct omap_hsmmc_host *host = mmc_priv(mmc);
1549 struct mmc_data *data = mrq->data;
1550
Russell King26b88522012-04-13 12:27:37 +01001551 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001552 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001553
Russell King26b88522012-04-13 12:27:37 +01001554 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1555 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001556 data->host_cookie = 0;
1557 }
1558}
1559
1560static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1561 bool is_first_req)
1562{
1563 struct omap_hsmmc_host *host = mmc_priv(mmc);
1564
1565 if (mrq->data->host_cookie) {
1566 mrq->data->host_cookie = 0;
1567 return ;
1568 }
1569
Russell Kingc5c98922012-04-13 12:14:39 +01001570 if (host->use_dma) {
1571 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001572
Per Forlin9782aff2011-07-01 18:55:23 +02001573 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001574 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001575 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001576 }
Per Forlin9782aff2011-07-01 18:55:23 +02001577}
1578
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001579/*
1580 * Request function. for read/write operation
1581 */
Denis Karpov70a33412009-09-22 16:44:59 -07001582static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001583{
Denis Karpov70a33412009-09-22 16:44:59 -07001584 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001585 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001586
Adrian Hunterb4175772010-05-26 14:42:06 -07001587 BUG_ON(host->req_in_progress);
1588 BUG_ON(host->dma_ch != -1);
1589 if (host->protect_card) {
1590 if (host->reqs_blocked < 3) {
1591 /*
1592 * Ensure the controller is left in a consistent
1593 * state by resetting the command and data state
1594 * machines.
1595 */
1596 omap_hsmmc_reset_controller_fsm(host, SRD);
1597 omap_hsmmc_reset_controller_fsm(host, SRC);
1598 host->reqs_blocked += 1;
1599 }
1600 req->cmd->error = -EBADF;
1601 if (req->data)
1602 req->data->error = -EBADF;
1603 req->cmd->retries = 0;
1604 mmc_request_done(mmc, req);
1605 return;
1606 } else if (host->reqs_blocked)
1607 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001608 WARN_ON(host->mrq != NULL);
1609 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301610 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001611 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001612 if (err) {
1613 req->cmd->error = err;
1614 if (req->data)
1615 req->data->error = err;
1616 host->mrq = NULL;
1617 mmc_request_done(mmc, req);
1618 return;
1619 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301620 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301621 omap_hsmmc_start_command(host, req->sbc, NULL);
1622 return;
1623 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001624
Balaji T K9d025332014-01-21 19:54:42 +05301625 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001626 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001627}
1628
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001629/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001630static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001631{
Denis Karpov70a33412009-09-22 16:44:59 -07001632 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001633 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001634
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301635 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001636
Adrian Huntera3621462009-09-22 16:44:42 -07001637 if (ios->power_mode != host->power_mode) {
1638 switch (ios->power_mode) {
1639 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001640 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001641 break;
1642 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001643 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001644 break;
1645 case MMC_POWER_ON:
1646 do_send_init_stream = 1;
1647 break;
1648 }
1649 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001650 }
1651
Denis Karpovdd498ef2009-09-22 16:44:49 -07001652 /* FIXME: set registers based only on changes to ios */
1653
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001654 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001655
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301656 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001657 /* Only MMC1 can interface at 3V without some flavor
1658 * of external transceiver; but they all handle 1.8V.
1659 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001660 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301661 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001662 /*
1663 * The mmc_select_voltage fn of the core does
1664 * not seem to set the power_mode to
1665 * MMC_POWER_UP upon recalculating the voltage.
1666 * vdd 1.8v.
1667 */
Denis Karpov70a33412009-09-22 16:44:59 -07001668 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1669 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001670 "Switch operation failed\n");
1671 }
1672 }
1673
Andy Shevchenko5934df22011-05-06 12:14:06 +03001674 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001675
Adrian Huntera3621462009-09-22 16:44:42 -07001676 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001677 send_init_stream(host);
1678
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001679 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001680
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301681 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001682}
1683
1684static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1685{
Denis Karpov70a33412009-09-22 16:44:59 -07001686 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001687
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001688 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001689 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001690 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001691}
1692
1693static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1694{
Denis Karpov70a33412009-09-22 16:44:59 -07001695 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001696
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001697 if (!host->get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001698 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001699 return host->get_ro(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001700}
1701
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001702static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1703{
1704 struct omap_hsmmc_host *host = mmc_priv(mmc);
1705
Andreas Fenkart326119c2014-11-08 15:33:14 +01001706 if (mmc_pdata(host)->init_card)
1707 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001708}
1709
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001710static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1711{
1712 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001713 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001714 unsigned long flags;
1715
1716 spin_lock_irqsave(&host->irq_lock, flags);
1717
Balaji T K5a52b082014-05-29 10:28:02 +02001718 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001719 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1720 if (enable) {
1721 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1722 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001723 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001724 } else {
1725 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1726 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001727 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001728 }
Balaji T K5a52b082014-05-29 10:28:02 +02001729 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001730 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1731
1732 /*
1733 * if enable, piggy back detection on current request
1734 * but always disable immediately
1735 */
1736 if (!host->req_in_progress || !enable)
1737 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1738
1739 /* flush posted write */
1740 OMAP_HSMMC_READ(host->base, IE);
1741
1742 spin_unlock_irqrestore(&host->irq_lock, flags);
1743}
1744
1745static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1746{
1747 struct mmc_host *mmc = host->mmc;
1748 int ret;
1749
1750 /*
1751 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1752 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1753 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1754 * with functional clock disabled.
1755 */
1756 if (!host->dev->of_node || !host->wake_irq)
1757 return -ENODEV;
1758
1759 /* Prevent auto-enabling of IRQ */
1760 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1761 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1762 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1763 mmc_hostname(mmc), host);
1764 if (ret) {
1765 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1766 goto err;
1767 }
1768
1769 /*
1770 * Some omaps don't have wake-up path from deeper idle states
1771 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1772 */
1773 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001774 struct pinctrl *p = devm_pinctrl_get(host->dev);
1775 if (!p) {
1776 ret = -ENODEV;
1777 goto err_free_irq;
1778 }
1779 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1780 dev_info(host->dev, "missing default pinctrl state\n");
1781 devm_pinctrl_put(p);
1782 ret = -EINVAL;
1783 goto err_free_irq;
1784 }
1785
1786 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1787 dev_info(host->dev, "missing idle pinctrl state\n");
1788 devm_pinctrl_put(p);
1789 ret = -EINVAL;
1790 goto err_free_irq;
1791 }
1792 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001793 }
1794
Balaji T K5a52b082014-05-29 10:28:02 +02001795 OMAP_HSMMC_WRITE(host->base, HCTL,
1796 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001797 return 0;
1798
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001799err_free_irq:
1800 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001801err:
1802 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1803 host->wake_irq = 0;
1804 return ret;
1805}
1806
Denis Karpov70a33412009-09-22 16:44:59 -07001807static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001808{
1809 u32 hctl, capa, value;
1810
1811 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301812 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001813 hctl = SDVS30;
1814 capa = VS30 | VS18;
1815 } else {
1816 hctl = SDVS18;
1817 capa = VS18;
1818 }
1819
1820 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1821 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1822
1823 value = OMAP_HSMMC_READ(host->base, CAPA);
1824 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1825
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001826 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001827 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001828}
1829
Denis Karpov70a33412009-09-22 16:44:59 -07001830static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001831{
Denis Karpov70a33412009-09-22 16:44:59 -07001832 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001833
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301834 pm_runtime_get_sync(host->dev);
1835
Denis Karpovdd498ef2009-09-22 16:44:49 -07001836 return 0;
1837}
1838
Adrian Hunter907d2e72012-02-29 09:17:21 +02001839static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001840{
Denis Karpov70a33412009-09-22 16:44:59 -07001841 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001842
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301843 pm_runtime_mark_last_busy(host->dev);
1844 pm_runtime_put_autosuspend(host->dev);
1845
Denis Karpovdd498ef2009-09-22 16:44:49 -07001846 return 0;
1847}
1848
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001849static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1850 unsigned int direction, int blk_size)
1851{
1852 /* This controller can't do multiblock reads due to hw bugs */
1853 if (direction == MMC_DATA_READ)
1854 return 1;
1855
1856 return blk_size;
1857}
1858
1859static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001860 .enable = omap_hsmmc_enable_fclk,
1861 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001862 .post_req = omap_hsmmc_post_req,
1863 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001864 .request = omap_hsmmc_request,
1865 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001866 .get_cd = omap_hsmmc_get_cd,
1867 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001868 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001869 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001870};
1871
Denis Karpovd900f712009-09-22 16:44:38 -07001872#ifdef CONFIG_DEBUG_FS
1873
Denis Karpov70a33412009-09-22 16:44:59 -07001874static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001875{
1876 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001877 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001878
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001879 seq_printf(s, "mmc%d:\n", mmc->index);
1880 seq_printf(s, "sdio irq mode\t%s\n",
1881 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1882
1883 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1884 seq_printf(s, "sdio irq \t%s\n",
1885 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1886 : "disabled");
1887 }
1888 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001889
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301890 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001891 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001892 seq_printf(s, "CON:\t\t0x%08x\n",
1893 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001894 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1895 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001896 seq_printf(s, "HCTL:\t\t0x%08x\n",
1897 OMAP_HSMMC_READ(host->base, HCTL));
1898 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1899 OMAP_HSMMC_READ(host->base, SYSCTL));
1900 seq_printf(s, "IE:\t\t0x%08x\n",
1901 OMAP_HSMMC_READ(host->base, IE));
1902 seq_printf(s, "ISE:\t\t0x%08x\n",
1903 OMAP_HSMMC_READ(host->base, ISE));
1904 seq_printf(s, "CAPA:\t\t0x%08x\n",
1905 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001906
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301907 pm_runtime_mark_last_busy(host->dev);
1908 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001909
Denis Karpovd900f712009-09-22 16:44:38 -07001910 return 0;
1911}
1912
Denis Karpov70a33412009-09-22 16:44:59 -07001913static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001914{
Denis Karpov70a33412009-09-22 16:44:59 -07001915 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001916}
1917
1918static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001919 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001920 .read = seq_read,
1921 .llseek = seq_lseek,
1922 .release = single_release,
1923};
1924
Denis Karpov70a33412009-09-22 16:44:59 -07001925static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001926{
1927 if (mmc->debugfs_root)
1928 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1929 mmc, &mmc_regs_fops);
1930}
1931
1932#else
1933
Denis Karpov70a33412009-09-22 16:44:59 -07001934static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001935{
1936}
1937
1938#endif
1939
Rajendra Nayak46856a62012-03-12 20:32:37 +05301940#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001941static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1942 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1943 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1944};
1945
1946static const struct omap_mmc_of_data omap4_mmc_of_data = {
1947 .reg_offset = 0x100,
1948};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001949static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1950 .reg_offset = 0x100,
1951 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1952};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301953
1954static const struct of_device_id omap_mmc_of_match[] = {
1955 {
1956 .compatible = "ti,omap2-hsmmc",
1957 },
1958 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001959 .compatible = "ti,omap3-pre-es3-hsmmc",
1960 .data = &omap3_pre_es3_mmc_of_data,
1961 },
1962 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301963 .compatible = "ti,omap3-hsmmc",
1964 },
1965 {
1966 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001967 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301968 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001969 {
1970 .compatible = "ti,am33xx-hsmmc",
1971 .data = &am33xx_mmc_of_data,
1972 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301973 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001974};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301975MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1976
Andreas Fenkart55143432014-11-08 15:33:09 +01001977static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301978{
Andreas Fenkart55143432014-11-08 15:33:09 +01001979 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301980 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301981 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001982 int cd_gpio, wp_gpio;
1983
1984 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1985 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1986 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1987 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301988
1989 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1990 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301991 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301992
1993 if (of_find_property(np, "ti,dual-volt", NULL))
1994 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1995
Andreas Fenkart326119c2014-11-08 15:33:14 +01001996 pdata->switch_pin = cd_gpio;
1997 pdata->gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301998
1999 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01002000 pdata->nonremovable = true;
2001 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302002 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04002003 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302004 if (bus_width == 4)
Andreas Fenkart326119c2014-11-08 15:33:14 +01002005 pdata->caps |= MMC_CAP_4_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302006 else if (bus_width == 8)
Andreas Fenkart326119c2014-11-08 15:33:14 +01002007 pdata->caps |= MMC_CAP_8_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302008
2009 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002010 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302011
Daniel Mackd8714e82012-10-15 21:35:06 +05302012 if (!of_property_read_u32(np, "max-frequency", &max_freq))
2013 pdata->max_freq = max_freq;
2014
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302015 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002016 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302017
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002018 if (of_find_property(np, "keep-power-in-suspend", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002019 pdata->pm_caps |= MMC_PM_KEEP_POWER;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002020
2021 if (of_find_property(np, "enable-sdio-wakeup", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002022 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002023
Rajendra Nayak46856a62012-03-12 20:32:37 +05302024 return pdata;
2025}
2026#else
Andreas Fenkart55143432014-11-08 15:33:09 +01002027static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05302028 *of_get_hsmmc_pdata(struct device *dev)
2029{
Balaji T K19df45b2014-02-28 19:08:18 +05302030 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302031}
2032#endif
2033
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002034static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002035{
Andreas Fenkart55143432014-11-08 15:33:09 +01002036 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002037 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002038 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002039 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002040 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302041 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002042 dma_cap_mask_t mask;
2043 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002044 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302045 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302046
2047 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2048 if (match) {
2049 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002050
2051 if (IS_ERR(pdata))
2052 return PTR_ERR(pdata);
2053
Rajendra Nayak46856a62012-03-12 20:32:37 +05302054 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002055 data = match->data;
2056 pdata->reg_offset = data->reg_offset;
2057 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302058 }
2059 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002060
2061 if (pdata == NULL) {
2062 dev_err(&pdev->dev, "Platform Data is missing\n");
2063 return -ENXIO;
2064 }
2065
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002066 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2067 irq = platform_get_irq(pdev, 0);
2068 if (res == NULL || irq < 0)
2069 return -ENXIO;
2070
Balaji T K77fae212014-05-09 22:16:51 +05302071 base = devm_ioremap_resource(&pdev->dev, res);
2072 if (IS_ERR(base))
2073 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002074
Denis Karpov70a33412009-09-22 16:44:59 -07002075 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002076 if (!mmc) {
2077 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002078 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002079 }
2080
2081 host = mmc_priv(mmc);
2082 host->mmc = mmc;
2083 host->pdata = pdata;
2084 host->dev = &pdev->dev;
2085 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002086 host->dma_ch = -1;
2087 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302088 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302089 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002090 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002091 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302092 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002094 ret = omap_hsmmc_gpio_init(host, pdata);
2095 if (ret)
2096 goto err_gpio;
2097
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002098 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002099
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002100 if (pdev->dev.of_node)
2101 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2102
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302103 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002104
Daniel Mackd418ed82012-02-19 13:20:33 +01002105 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2106
2107 if (pdata->max_freq > 0)
2108 mmc->f_max = pdata->max_freq;
2109 else
2110 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002111
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002112 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113
Balaji T K96181952014-05-09 22:16:48 +05302114 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002115 if (IS_ERR(host->fclk)) {
2116 ret = PTR_ERR(host->fclk);
2117 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002118 goto err1;
2119 }
2120
Paul Walmsley9b682562011-10-06 14:50:35 -06002121 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2122 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 -07002123 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002124 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002125
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302126 pm_runtime_enable(host->dev);
2127 pm_runtime_get_sync(host->dev);
2128 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2129 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002130
Balaji T K92a3aeb2012-02-24 21:14:34 +05302131 omap_hsmmc_context_save(host);
2132
Balaji T K96181952014-05-09 22:16:48 +05302133 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302134 /*
2135 * MMC can still work without debounce clock.
2136 */
2137 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302138 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302139 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302140 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302141 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002142 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002144 /* Since we do only SG emulation, we can have as many segs
2145 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002146 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002147
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002148 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2149 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2150 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2151 mmc->max_seg_size = mmc->max_req_size;
2152
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002153 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002154 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002155
Andreas Fenkart326119c2014-11-08 15:33:14 +01002156 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002157 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002158 mmc->caps |= MMC_CAP_4_BIT_DATA;
2159
Andreas Fenkart326119c2014-11-08 15:33:14 +01002160 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002161 mmc->caps |= MMC_CAP_NONREMOVABLE;
2162
Andreas Fenkart326119c2014-11-08 15:33:14 +01002163 mmc->pm_caps = mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002164
Denis Karpov70a33412009-09-22 16:44:59 -07002165 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002166
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302167 if (!pdev->dev.of_node) {
2168 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2169 if (!res) {
2170 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2171 ret = -ENXIO;
2172 goto err_irq;
2173 }
2174 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002175
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302176 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2177 if (!res) {
2178 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2179 ret = -ENXIO;
2180 goto err_irq;
2181 }
2182 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002183 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002184
Russell King26b88522012-04-13 12:27:37 +01002185 dma_cap_zero(mask);
2186 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002187
Matt Porterd272fbf2013-05-10 17:42:34 +05302188 host->rx_chan =
2189 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2190 &rx_req, &pdev->dev, "rx");
2191
Russell King26b88522012-04-13 12:27:37 +01002192 if (!host->rx_chan) {
2193 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002194 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002195 goto err_irq;
2196 }
2197
Matt Porterd272fbf2013-05-10 17:42:34 +05302198 host->tx_chan =
2199 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2200 &tx_req, &pdev->dev, "tx");
2201
Russell King26b88522012-04-13 12:27:37 +01002202 if (!host->tx_chan) {
2203 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002204 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002205 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002206 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002207
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302209 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002210 mmc_hostname(mmc), host);
2211 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302212 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002213 goto err_irq;
2214 }
2215
Andreas Fenkart326119c2014-11-08 15:33:14 +01002216 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002217 ret = omap_hsmmc_reg_get(host);
2218 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002219 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002220 host->use_reg = 1;
2221 }
2222
Andreas Fenkart326119c2014-11-08 15:33:14 +01002223 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002224
2225 /* Request IRQ for card detect */
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002226 if (host->card_detect_irq) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302227 ret = devm_request_threaded_irq(&pdev->dev,
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002228 host->card_detect_irq,
Balaji T K9fa0e052014-05-09 22:16:50 +05302229 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002230 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002231 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002232 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302233 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 "Unable to grab MMC CD IRQ\n");
2235 goto err_irq_cd;
2236 }
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002237 host->suspend = omap_hsmmc_suspend_cdirq;
2238 host->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002239 }
2240
Adrian Hunterb4175772010-05-26 14:42:06 -07002241 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002242
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002243 /*
2244 * For now, only support SDIO interrupt if we have a separate
2245 * wake-up interrupt configured from device tree. This is because
2246 * the wake-up interrupt is needed for idle state and some
2247 * platforms need special quirks. And we don't want to add new
2248 * legacy mux platform init code callbacks any longer as we
2249 * are moving to DT based booting anyways.
2250 */
2251 ret = omap_hsmmc_configure_wake_irq(host);
2252 if (!ret)
2253 mmc->caps |= MMC_CAP_SDIO_IRQ;
2254
Adrian Hunterb62f6222009-09-22 16:45:01 -07002255 omap_hsmmc_protect_card(host);
2256
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002257 mmc_add_host(mmc);
2258
Andreas Fenkart326119c2014-11-08 15:33:14 +01002259 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002260 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2261 if (ret < 0)
2262 goto err_slot_name;
2263 }
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002264 if (host->card_detect_irq && host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002265 ret = device_create_file(&mmc->class_dev,
2266 &dev_attr_cover_switch);
2267 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002268 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002269 }
2270
Denis Karpov70a33412009-09-22 16:44:59 -07002271 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302272 pm_runtime_mark_last_busy(host->dev);
2273 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002274
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002275 return 0;
2276
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002277err_slot_name:
2278 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002279err_irq_cd:
2280 if (host->use_reg)
2281 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002282err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002283 if (host->tx_chan)
2284 dma_release_channel(host->tx_chan);
2285 if (host->rx_chan)
2286 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302287 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002288 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302289 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302290 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002291err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002292 omap_hsmmc_gpio_free(host, pdata);
2293err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002294 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002295err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296 return ret;
2297}
2298
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002299static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002300{
Denis Karpov70a33412009-09-22 16:44:59 -07002301 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002302
Felipe Balbi927ce942012-03-14 11:18:27 +02002303 pm_runtime_get_sync(host->dev);
2304 mmc_remove_host(host->mmc);
2305 if (host->use_reg)
2306 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002307
Russell Kingc5c98922012-04-13 12:14:39 +01002308 if (host->tx_chan)
2309 dma_release_channel(host->tx_chan);
2310 if (host->rx_chan)
2311 dma_release_channel(host->rx_chan);
2312
Felipe Balbi927ce942012-03-14 11:18:27 +02002313 pm_runtime_put_sync(host->dev);
2314 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302315 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302316 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002317
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002318 omap_hsmmc_gpio_free(host, host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302319 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002320
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002321 return 0;
2322}
2323
2324#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302325static int omap_hsmmc_prepare(struct device *dev)
2326{
2327 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2328
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002329 if (host->suspend)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01002330 return host->suspend(dev);
Felipe Balbia48ce882012-11-19 21:59:59 +05302331
2332 return 0;
2333}
2334
2335static void omap_hsmmc_complete(struct device *dev)
2336{
2337 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2338
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01002339 if (host->resume)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01002340 host->resume(dev);
Felipe Balbia48ce882012-11-19 21:59:59 +05302341
2342}
2343
Kevin Hilmana791daa2010-05-26 14:42:07 -07002344static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002345{
Felipe Balbi927ce942012-03-14 11:18:27 +02002346 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2347
2348 if (!host)
2349 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002350
Felipe Balbi927ce942012-03-14 11:18:27 +02002351 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002352
2353 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002354 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2355 OMAP_HSMMC_WRITE(host->base, IE, 0);
2356 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002357 OMAP_HSMMC_WRITE(host->base, HCTL,
2358 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2359 }
2360
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002361 /* do not wake up due to sdio irq */
2362 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2363 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2364 disable_irq(host->wake_irq);
2365
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302366 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302367 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002368
Eliad Peller31f9d462011-11-22 16:02:17 +02002369 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002370 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002371}
2372
2373/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002374static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002375{
Felipe Balbi927ce942012-03-14 11:18:27 +02002376 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2377
2378 if (!host)
2379 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002380
Felipe Balbi927ce942012-03-14 11:18:27 +02002381 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002382
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302383 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302384 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002385
Felipe Balbi927ce942012-03-14 11:18:27 +02002386 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2387 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002388
Felipe Balbi927ce942012-03-14 11:18:27 +02002389 omap_hsmmc_protect_card(host);
2390
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002391 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2392 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2393 enable_irq(host->wake_irq);
2394
Felipe Balbi927ce942012-03-14 11:18:27 +02002395 pm_runtime_mark_last_busy(host->dev);
2396 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002397 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002398}
2399
2400#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302401#define omap_hsmmc_prepare NULL
2402#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002403#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302404#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002405#endif
2406
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302407static int omap_hsmmc_runtime_suspend(struct device *dev)
2408{
2409 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002410 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002411 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302412
2413 host = platform_get_drvdata(to_platform_device(dev));
2414 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002415 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302416
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002417 spin_lock_irqsave(&host->irq_lock, flags);
2418 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2419 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2420 /* disable sdio irq handling to prevent race */
2421 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2422 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002423
2424 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2425 /*
2426 * dat1 line low, pending sdio irq
2427 * race condition: possible irq handler running on
2428 * multi-core, abort
2429 */
2430 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2431 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2432 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2433 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2434 pm_runtime_mark_last_busy(dev);
2435 ret = -EBUSY;
2436 goto abort;
2437 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002438
Andreas Fenkart97978a42014-05-29 10:28:04 +02002439 pinctrl_pm_select_idle_state(dev);
2440
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002441 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2442 enable_irq(host->wake_irq);
2443 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002444 } else {
2445 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002446 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002447
Andreas Fenkartf9459012014-05-29 10:28:03 +02002448abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002449 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002450 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302451}
2452
2453static int omap_hsmmc_runtime_resume(struct device *dev)
2454{
2455 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002456 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302457
2458 host = platform_get_drvdata(to_platform_device(dev));
2459 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002460 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302461
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002462 spin_lock_irqsave(&host->irq_lock, flags);
2463 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2464 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2465 /* sdio irq flag can't change while in runtime suspend */
2466 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2467 disable_irq_nosync(host->wake_irq);
2468 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2469 }
2470
Andreas Fenkart97978a42014-05-29 10:28:04 +02002471 pinctrl_pm_select_default_state(host->dev);
2472
2473 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002474 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2475 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2476 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002477 } else {
2478 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002479 }
2480 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302481 return 0;
2482}
2483
Kevin Hilmana791daa2010-05-26 14:42:07 -07002484static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002485 .suspend = omap_hsmmc_suspend,
2486 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302487 .prepare = omap_hsmmc_prepare,
2488 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302489 .runtime_suspend = omap_hsmmc_runtime_suspend,
2490 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002491};
2492
2493static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002494 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002495 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002496 .driver = {
2497 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002498 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302499 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002500 },
2501};
2502
Felipe Balbib7964502012-03-14 11:18:32 +02002503module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002504MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2505MODULE_LICENSE("GPL");
2506MODULE_ALIAS("platform:" DRIVER_NAME);
2507MODULE_AUTHOR("Texas Instruments Inc");