blob: fda762ebca14cb2074939cee17e45d062dc74c30 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
NeilBrown41afa3142015-01-13 08:23:18 +130039#include <linux/mmc/slot-gpio.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010040#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020041#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080042#include <linux/gpio.h>
43#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053044#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053045#include <linux/pm_runtime.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010046#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010047
48/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070049#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010050#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053051#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010052#define OMAP_HSMMC_BLK 0x0104
53#define OMAP_HSMMC_ARG 0x0108
54#define OMAP_HSMMC_CMD 0x010C
55#define OMAP_HSMMC_RSP10 0x0110
56#define OMAP_HSMMC_RSP32 0x0114
57#define OMAP_HSMMC_RSP54 0x0118
58#define OMAP_HSMMC_RSP76 0x011C
59#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020060#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010061#define OMAP_HSMMC_HCTL 0x0128
62#define OMAP_HSMMC_SYSCTL 0x012C
63#define OMAP_HSMMC_STAT 0x0130
64#define OMAP_HSMMC_IE 0x0134
65#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053066#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010067#define OMAP_HSMMC_CAPA 0x0140
68
69#define VS18 (1 << 26)
70#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053071#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010072#define SDVS18 (0x5 << 9)
73#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080074#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010075#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010076#define SDVSCLR 0xFFFFF1FF
77#define SDVSDET 0x00000400
78#define AUTOIDLE 0x1
79#define SDBP (1 << 8)
80#define DTO 0xe
81#define ICE 0x1
82#define ICS 0x2
83#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053084#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010085#define CLKD_MASK 0x0000FFC0
86#define CLKD_SHIFT 6
87#define DTO_MASK 0x000F0000
88#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010089#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053090#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010091#define DP_SELECT (1 << 21)
92#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053093#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010094#define MSBS (1 << 5)
95#define BCE (1 << 1)
96#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053097#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020098#define IWE (1 << 24)
Balaji T K03b5d9242012-04-09 12:08:33 +053099#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +0200100#define CLKEXTFREE (1 << 16)
101#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200102#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100104#define STAT_CLEAR 0xFFFFFFFF
105#define INIT_STREAM_CMD 0x00000000
106#define DUAL_VOLT_OCR_BIT 7
107#define SRC (1 << 25)
108#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700109#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100110
Andreas Fenkartf9459012014-05-29 10:28:03 +0200111/* PSTATE */
112#define DLEV_DAT(x) (1 << (20 + (x)))
113
Venkatraman Sa7e96872012-11-19 22:00:01 +0530114/* Interrupt masks for IE and ISE register */
115#define CC_EN (1 << 0)
116#define TC_EN (1 << 1)
117#define BWR_EN (1 << 4)
118#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200119#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530120#define ERR_EN (1 << 15)
121#define CTO_EN (1 << 16)
122#define CCRC_EN (1 << 17)
123#define CEB_EN (1 << 18)
124#define CIE_EN (1 << 19)
125#define DTO_EN (1 << 20)
126#define DCRC_EN (1 << 21)
127#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530128#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530129#define CERR_EN (1 << 28)
130#define BADA_EN (1 << 29)
131
Balaji T Ka2e77152014-01-21 19:54:42 +0530132#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530133 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
134 BRR_EN | BWR_EN | TC_EN | CC_EN)
135
Balaji T Ka2e77152014-01-21 19:54:42 +0530136#define CNI (1 << 7)
137#define ACIE (1 << 4)
138#define ACEB (1 << 3)
139#define ACCE (1 << 2)
140#define ACTO (1 << 1)
141#define ACNE (1 << 0)
142
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530143#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530144#define MMC_TIMEOUT_MS 20 /* 20 mSec */
145#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400146#define OMAP_MMC_MIN_CLOCK 400000
147#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530148#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100149
Balaji T Ke99448f2014-02-19 20:26:40 +0530150#define VDD_1V8 1800000 /* 180000 uV */
151#define VDD_3V0 3000000 /* 300000 uV */
152#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
153
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100154/*
155 * One controller can have multiple slots, like on some omap boards using
156 * omap.c controller driver. Luckily this is not currently done on any known
157 * omap_hsmmc.c device.
158 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100159#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100160
161/*
162 * MMC Host controller read/write API's
163 */
164#define OMAP_HSMMC_READ(base, reg) \
165 __raw_readl((base) + OMAP_HSMMC_##reg)
166
167#define OMAP_HSMMC_WRITE(base, reg, val) \
168 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
169
Per Forlin9782aff2011-07-01 18:55:23 +0200170struct omap_hsmmc_next {
171 unsigned int dma_len;
172 s32 cookie;
173};
174
Denis Karpov70a33412009-09-22 16:44:59 -0700175struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100176 struct device *dev;
177 struct mmc_host *mmc;
178 struct mmc_request *mrq;
179 struct mmc_command *cmd;
180 struct mmc_data *data;
181 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100182 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800183 /*
184 * vcc == configured supply
185 * vcc_aux == optional
186 * - MMC1, supply for DAT4..DAT7
187 * - MMC2/MMC2, external level shifter voltage supply, for
188 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
189 */
190 struct regulator *vcc;
191 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530192 struct regulator *pbias;
193 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100194 void __iomem *base;
195 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700196 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100197 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200198 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100199 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700200 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100201 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530202 u32 con;
203 u32 hctl;
204 u32 sysctl;
205 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100206 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200207 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100208 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100209 struct dma_chan *tx_chan;
210 struct dma_chan *rx_chan;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200211 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700212 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700213 int protect_card;
214 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800215 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700216 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530217 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530218 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200219#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
220#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
221#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200222 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100223 struct omap_hsmmc_platform_data *pdata;
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100224
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100225 /* return MMC cover switch state, can be NULL if not supported.
226 *
227 * possible return values:
228 * 0 - closed
229 * 1 - open
230 */
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100231 int (*get_cover_state)(struct device *dev);
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100232
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100233 int (*card_detect)(struct device *dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100234};
235
Nishanth Menon59445b12014-02-13 23:45:48 -0600236struct omap_mmc_of_data {
237 u32 reg_offset;
238 u8 controller_flags;
239};
240
Balaji T Kbf129e12014-01-21 19:54:42 +0530241static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
242
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100243static int omap_hsmmc_card_detect(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800244{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530245 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246
NeilBrown41afa3142015-01-13 08:23:18 +1300247 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800248}
249
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100250static int omap_hsmmc_get_cover_state(struct device *dev)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800251{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530252 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800253
NeilBrown41afa3142015-01-13 08:23:18 +1300254 return mmc_gpio_get_cd(host->mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800255}
256
Adrian Hunterb702b102010-02-15 10:03:35 -0800257#ifdef CONFIG_REGULATOR
258
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100259static int omap_hsmmc_set_power(struct device *dev, int power_on, int vdd)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800260{
261 struct omap_hsmmc_host *host =
262 platform_get_drvdata(to_platform_device(dev));
263 int ret = 0;
264
265 /*
266 * If we don't see a Vcc regulator, assume it's a fixed
267 * voltage always-on regulator.
268 */
269 if (!host->vcc)
270 return 0;
271
Andreas Fenkart326119c2014-11-08 15:33:14 +0100272 if (mmc_pdata(host)->before_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100273 mmc_pdata(host)->before_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800274
Balaji T Ke99448f2014-02-19 20:26:40 +0530275 if (host->pbias) {
276 if (host->pbias_enabled == 1) {
277 ret = regulator_disable(host->pbias);
278 if (!ret)
279 host->pbias_enabled = 0;
280 }
281 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
282 }
283
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800284 /*
285 * Assume Vcc regulator is used only to power the card ... OMAP
286 * VDDS is used to power the pins, optionally with a transceiver to
287 * support cards using voltages other than VDDS (1.8V nominal). When a
288 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
289 *
290 * In some cases this regulator won't support enable/disable;
291 * e.g. it's a fixed rail for a WLAN chip.
292 *
293 * In other cases vcc_aux switches interface power. Example, for
294 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
295 * chips/cards need an interface voltage rail too.
296 */
297 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530298 if (host->vcc)
299 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800300 /* Enable interface voltage rail, if needed */
301 if (ret == 0 && host->vcc_aux) {
302 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530303 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400304 ret = mmc_regulator_set_ocr(host->mmc,
305 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800306 }
307 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400308 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800309 if (host->vcc_aux)
310 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530311 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400312 /* Then proceed to shut down the local regulator */
313 ret = mmc_regulator_set_ocr(host->mmc,
314 host->vcc, 0);
315 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800316 }
317
Balaji T Ke99448f2014-02-19 20:26:40 +0530318 if (host->pbias) {
319 if (vdd <= VDD_165_195)
320 ret = regulator_set_voltage(host->pbias, VDD_1V8,
321 VDD_1V8);
322 else
323 ret = regulator_set_voltage(host->pbias, VDD_3V0,
324 VDD_3V0);
325 if (ret < 0)
326 goto error_set_power;
327
328 if (host->pbias_enabled == 0) {
329 ret = regulator_enable(host->pbias);
330 if (!ret)
331 host->pbias_enabled = 1;
332 }
333 }
334
Andreas Fenkart326119c2014-11-08 15:33:14 +0100335 if (mmc_pdata(host)->after_set_reg)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100336 mmc_pdata(host)->after_set_reg(dev, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800337
Balaji T Ke99448f2014-02-19 20:26:40 +0530338error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800339 return ret;
340}
341
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800342static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
343{
344 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700345 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800346
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530347 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800348 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530349 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
350 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400351 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800352 } else {
353 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700354 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100355 if (!mmc_pdata(host)->ocr_mask) {
356 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700357 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100358 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530359 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100360 mmc_pdata(host)->ocr_mask);
361 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700362 return -EINVAL;
363 }
364 }
Balaji T K987fd492014-02-19 20:26:40 +0530365 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100366 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800367
Balaji T K987fd492014-02-19 20:26:40 +0530368 /* Allow an aux regulator */
369 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
370 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800371
Balaji T Ke99448f2014-02-19 20:26:40 +0530372 reg = devm_regulator_get_optional(host->dev, "pbias");
373 host->pbias = IS_ERR(reg) ? NULL : reg;
374
Balaji T K987fd492014-02-19 20:26:40 +0530375 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100376 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530377 return 0;
378 /*
379 * To disable boot_on regulator, enable regulator
380 * to increase usecount and then disable it.
381 */
382 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
383 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100384 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300385
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100386 mmc_pdata(host)->set_power(host->dev, 1, vdd);
387 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800388 }
389
390 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800391}
392
393static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
394{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100395 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800396}
397
Adrian Hunterb702b102010-02-15 10:03:35 -0800398static inline int omap_hsmmc_have_reg(void)
399{
400 return 1;
401}
402
403#else
404
405static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
406{
407 return -EINVAL;
408}
409
410static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
411{
412}
413
414static inline int omap_hsmmc_have_reg(void)
415{
416 return 0;
417}
418
419#endif
420
NeilBrown41afa3142015-01-13 08:23:18 +1300421static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id);
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100422static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id);
NeilBrown41afa3142015-01-13 08:23:18 +1300423
424static int omap_hsmmc_gpio_init(struct mmc_host *mmc,
425 struct omap_hsmmc_host *host,
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100426 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800427{
428 int ret;
429
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100430 if (pdata->cover && gpio_is_valid(pdata->switch_pin)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300431 ret = mmc_gpio_request_cd(mmc, pdata->switch_pin, 0);
Adrian Hunterb702b102010-02-15 10:03:35 -0800432 if (ret)
433 return ret;
Andreas Fenkartcde592c2015-03-03 13:28:15 +0100434
435 host->get_cover_state = omap_hsmmc_get_cover_state;
436 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_cover_irq);
437 } else if (!pdata->cover && gpio_is_valid(pdata->switch_pin)) {
438 ret = mmc_gpio_request_cd(mmc, pdata->switch_pin, 0);
439 if (ret)
440 return ret;
441
442 host->card_detect = omap_hsmmc_card_detect;
443 mmc_gpio_set_cd_isr(mmc, omap_hsmmc_detect);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100444 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800445
Andreas Fenkart326119c2014-11-08 15:33:14 +0100446 if (gpio_is_valid(pdata->gpio_wp)) {
NeilBrown41afa3142015-01-13 08:23:18 +1300447 ret = mmc_gpio_request_ro(mmc, pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800448 if (ret)
NeilBrown41afa3142015-01-13 08:23:18 +1300449 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100450 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800451
452 return 0;
Adrian Hunterb702b102010-02-15 10:03:35 -0800453}
454
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100455/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300456 * Start clock to the card
457 */
458static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
459{
460 OMAP_HSMMC_WRITE(host->base, SYSCTL,
461 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
462}
463
464/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100465 * Stop clock to the card
466 */
Denis Karpov70a33412009-09-22 16:44:59 -0700467static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100468{
469 OMAP_HSMMC_WRITE(host->base, SYSCTL,
470 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
471 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900472 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100473}
474
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700475static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
476 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700477{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200478 u32 irq_mask = INT_EN_MASK;
479 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700480
481 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200482 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700483
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700484 /* Disable timeout for erases */
485 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530486 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700487
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200488 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700489 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
490 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200491
492 /* latch pending CIRQ, but don't signal MMC core */
493 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
494 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700495 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200496 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700497}
498
499static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
500{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200501 u32 irq_mask = 0;
502 unsigned long flags;
503
504 spin_lock_irqsave(&host->irq_lock, flags);
505 /* no transfer running but need to keep cirq if enabled */
506 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
507 irq_mask |= CIRQ_EN;
508 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
509 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700510 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200511 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700512}
513
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300514/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530515static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300516{
517 u16 dsor = 0;
518
519 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530520 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530521 if (dsor > CLKD_MAX)
522 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300523 }
524
525 return dsor;
526}
527
Andy Shevchenko5934df22011-05-06 12:14:06 +0300528static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
529{
530 struct mmc_ios *ios = &host->mmc->ios;
531 unsigned long regval;
532 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530533 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300534
Venkatraman S8986d312012-08-07 19:10:38 +0530535 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300536
537 omap_hsmmc_stop_clock(host);
538
539 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
540 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530541 clkdiv = calc_divisor(host, ios);
542 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300543 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
544 OMAP_HSMMC_WRITE(host->base, SYSCTL,
545 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
546
547 /* Wait till the ICS bit is set */
548 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
549 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
550 && time_before(jiffies, timeout))
551 cpu_relax();
552
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530553 /*
554 * Enable High-Speed Support
555 * Pre-Requisites
556 * - Controller should support High-Speed-Enable Bit
557 * - Controller should not be using DDR Mode
558 * - Controller should advertise that it supports High Speed
559 * in capabilities register
560 * - MMC/SD clock coming out of controller > 25MHz
561 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100562 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900563 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Ulf Hansson903101a2014-11-25 13:05:13 +0100564 (ios->timing != MMC_TIMING_UHS_DDR50) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530565 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
566 regval = OMAP_HSMMC_READ(host->base, HCTL);
567 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
568 regval |= HSPE;
569 else
570 regval &= ~HSPE;
571
572 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
573 }
574
Andy Shevchenko5934df22011-05-06 12:14:06 +0300575 omap_hsmmc_start_clock(host);
576}
577
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400578static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
579{
580 struct mmc_ios *ios = &host->mmc->ios;
581 u32 con;
582
583 con = OMAP_HSMMC_READ(host->base, CON);
Ulf Hansson903101a2014-11-25 13:05:13 +0100584 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
585 ios->timing == MMC_TIMING_UHS_DDR50)
Balaji T K03b5d9242012-04-09 12:08:33 +0530586 con |= DDR; /* configure in DDR mode */
587 else
588 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400589 switch (ios->bus_width) {
590 case MMC_BUS_WIDTH_8:
591 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
592 break;
593 case MMC_BUS_WIDTH_4:
594 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
595 OMAP_HSMMC_WRITE(host->base, HCTL,
596 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
597 break;
598 case MMC_BUS_WIDTH_1:
599 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
600 OMAP_HSMMC_WRITE(host->base, HCTL,
601 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
602 break;
603 }
604}
605
606static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
607{
608 struct mmc_ios *ios = &host->mmc->ios;
609 u32 con;
610
611 con = OMAP_HSMMC_READ(host->base, CON);
612 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
613 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
614 else
615 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
616}
617
Denis Karpov11dd62a2009-09-22 16:44:43 -0700618#ifdef CONFIG_PM
619
620/*
621 * Restore the MMC host context, if it was lost as result of a
622 * power state change.
623 */
Denis Karpov70a33412009-09-22 16:44:59 -0700624static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700625{
626 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400627 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700628 unsigned long timeout;
629
Tony Lindgren0a82e062013-10-21 00:25:19 +0530630 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
631 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
632 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
633 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
634 return 0;
635
636 host->context_loss++;
637
Balaji T Kc2200ef2012-03-07 09:55:30 -0500638 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700639 if (host->power_mode != MMC_POWER_OFF &&
640 (1 << ios->vdd) <= MMC_VDD_23_24)
641 hctl = SDVS18;
642 else
643 hctl = SDVS30;
644 capa = VS30 | VS18;
645 } else {
646 hctl = SDVS18;
647 capa = VS18;
648 }
649
Balaji T K5a52b082014-05-29 10:28:02 +0200650 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
651 hctl |= IWE;
652
Denis Karpov11dd62a2009-09-22 16:44:43 -0700653 OMAP_HSMMC_WRITE(host->base, HCTL,
654 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
655
656 OMAP_HSMMC_WRITE(host->base, CAPA,
657 OMAP_HSMMC_READ(host->base, CAPA) | capa);
658
659 OMAP_HSMMC_WRITE(host->base, HCTL,
660 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
661
662 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
663 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
664 && time_before(jiffies, timeout))
665 ;
666
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200667 OMAP_HSMMC_WRITE(host->base, ISE, 0);
668 OMAP_HSMMC_WRITE(host->base, IE, 0);
669 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700670
671 /* Do not initialize card-specific things if the power is off */
672 if (host->power_mode == MMC_POWER_OFF)
673 goto out;
674
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400675 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700676
Andy Shevchenko5934df22011-05-06 12:14:06 +0300677 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700678
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400679 omap_hsmmc_set_bus_mode(host);
680
Denis Karpov11dd62a2009-09-22 16:44:43 -0700681out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530682 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
683 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700684 return 0;
685}
686
687/*
688 * Save the MMC host context (store the number of power state changes so far).
689 */
Denis Karpov70a33412009-09-22 16:44:59 -0700690static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700691{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530692 host->con = OMAP_HSMMC_READ(host->base, CON);
693 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
694 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
695 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700696}
697
698#else
699
Denis Karpov70a33412009-09-22 16:44:59 -0700700static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700701{
702 return 0;
703}
704
Denis Karpov70a33412009-09-22 16:44:59 -0700705static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700706{
707}
708
709#endif
710
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100711/*
712 * Send init stream sequence to card
713 * before sending IDLE command
714 */
Denis Karpov70a33412009-09-22 16:44:59 -0700715static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100716{
717 int reg = 0;
718 unsigned long timeout;
719
Adrian Hunterb62f6222009-09-22 16:45:01 -0700720 if (host->protect_card)
721 return;
722
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100723 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700724
725 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100726 OMAP_HSMMC_WRITE(host->base, CON,
727 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
728 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
729
730 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530731 while ((reg != CC_EN) && time_before(jiffies, timeout))
732 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100733
734 OMAP_HSMMC_WRITE(host->base, CON,
735 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700736
737 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
738 OMAP_HSMMC_READ(host->base, STAT);
739
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100740 enable_irq(host->irq);
741}
742
743static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700744int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100745{
746 int r = 1;
747
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +0100748 if (host->get_cover_state)
Andreas Fenkart80412ca2014-11-08 15:33:17 +0100749 r = host->get_cover_state(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100750 return r;
751}
752
753static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700754omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100755 char *buf)
756{
757 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700758 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759
Denis Karpov70a33412009-09-22 16:44:59 -0700760 return sprintf(buf, "%s\n",
761 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100762}
763
Denis Karpov70a33412009-09-22 16:44:59 -0700764static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100765
766static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700767omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100768 char *buf)
769{
770 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700771 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100772
Andreas Fenkart326119c2014-11-08 15:33:14 +0100773 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100774}
775
Denis Karpov70a33412009-09-22 16:44:59 -0700776static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100777
778/*
779 * Configure the response type and send the cmd.
780 */
781static void
Denis Karpov70a33412009-09-22 16:44:59 -0700782omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783 struct mmc_data *data)
784{
785 int cmdreg = 0, resptype = 0, cmdtype = 0;
786
Venkatraman S8986d312012-08-07 19:10:38 +0530787 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100788 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
789 host->cmd = cmd;
790
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700791 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100792
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200793 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100794 if (cmd->flags & MMC_RSP_PRESENT) {
795 if (cmd->flags & MMC_RSP_136)
796 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200797 else if (cmd->flags & MMC_RSP_BUSY) {
798 resptype = 3;
799 host->response_busy = 1;
800 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100801 resptype = 2;
802 }
803
804 /*
805 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
806 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
807 * a val of 0x3, rest 0x0.
808 */
809 if (cmd == host->mrq->stop)
810 cmdtype = 0x3;
811
812 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
813
Balaji T Ka2e77152014-01-21 19:54:42 +0530814 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
815 host->mrq->sbc) {
816 cmdreg |= ACEN_ACMD23;
817 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
818 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100819 if (data) {
820 cmdreg |= DP_SELECT | MSBS | BCE;
821 if (data->flags & MMC_DATA_READ)
822 cmdreg |= DDIR;
823 else
824 cmdreg &= ~(DDIR);
825 }
826
827 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530828 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100829
Adrian Hunterb4175772010-05-26 14:42:06 -0700830 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700831
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100832 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
833 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
834}
835
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200836static int
Denis Karpov70a33412009-09-22 16:44:59 -0700837omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200838{
839 if (data->flags & MMC_DATA_WRITE)
840 return DMA_TO_DEVICE;
841 else
842 return DMA_FROM_DEVICE;
843}
844
Russell Kingc5c98922012-04-13 12:14:39 +0100845static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
846 struct mmc_data *data)
847{
848 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
849}
850
Adrian Hunterb4175772010-05-26 14:42:06 -0700851static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
852{
853 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530854 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700855
Venkatraman S31463b12012-04-09 12:08:34 +0530856 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700857 host->req_in_progress = 0;
858 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530859 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700860
861 omap_hsmmc_disable_irq(host);
862 /* Do not complete the request if DMA is still in progress */
863 if (mrq->data && host->use_dma && dma_ch != -1)
864 return;
865 host->mrq = NULL;
866 mmc_request_done(host->mmc, mrq);
867}
868
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869/*
870 * Notify the transfer complete to MMC core
871 */
872static void
Denis Karpov70a33412009-09-22 16:44:59 -0700873omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100874{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200875 if (!data) {
876 struct mmc_request *mrq = host->mrq;
877
Adrian Hunter23050102009-09-22 16:44:57 -0700878 /* TC before CC from CMD6 - don't know why, but it happens */
879 if (host->cmd && host->cmd->opcode == 6 &&
880 host->response_busy) {
881 host->response_busy = 0;
882 return;
883 }
884
Adrian Hunterb4175772010-05-26 14:42:06 -0700885 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200886 return;
887 }
888
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100889 host->data = NULL;
890
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100891 if (!data->error)
892 data->bytes_xfered += data->blocks * (data->blksz);
893 else
894 data->bytes_xfered = 0;
895
Balaji T Kbf129e12014-01-21 19:54:42 +0530896 if (data->stop && (data->error || !host->mrq->sbc))
897 omap_hsmmc_start_command(host, data->stop, NULL);
898 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700899 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900}
901
902/*
903 * Notify the core about command completion
904 */
905static void
Denis Karpov70a33412009-09-22 16:44:59 -0700906omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100907{
Balaji T Kbf129e12014-01-21 19:54:42 +0530908 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530909 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530910 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530911 omap_hsmmc_start_dma_transfer(host);
912 omap_hsmmc_start_command(host, host->mrq->cmd,
913 host->mrq->data);
914 return;
915 }
916
Balaji T K2177fa92014-05-09 22:16:52 +0530917 host->cmd = NULL;
918
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100919 if (cmd->flags & MMC_RSP_PRESENT) {
920 if (cmd->flags & MMC_RSP_136) {
921 /* response type 2 */
922 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
923 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
924 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
925 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
926 } else {
927 /* response types 1, 1b, 3, 4, 5, 6 */
928 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
929 }
930 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700931 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530932 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100933}
934
935/*
936 * DMA clean up for command errors
937 */
Denis Karpov70a33412009-09-22 16:44:59 -0700938static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939{
Adrian Hunterb4175772010-05-26 14:42:06 -0700940 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530941 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700942
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200943 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100944
Venkatraman S31463b12012-04-09 12:08:34 +0530945 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700946 dma_ch = host->dma_ch;
947 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530948 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700949
950 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +0100951 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
952
953 dmaengine_terminate_all(chan);
954 dma_unmap_sg(chan->device->dev,
955 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -0700956 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +0100957
Per Forlin053bf342011-11-07 21:55:11 +0530958 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100959 }
960 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961}
962
963/*
964 * Readable error output
965 */
966#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +0300967static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100968{
969 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -0700970 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +0300971 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
972 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
973 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
974 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100975 };
976 char res[256];
977 char *buf = res;
978 int len, i;
979
980 len = sprintf(buf, "MMC IRQ 0x%x :", status);
981 buf += len;
982
Denis Karpov70a33412009-09-22 16:44:59 -0700983 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100984 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -0700985 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100986 buf += len;
987 }
988
Venkatraman S8986d312012-08-07 19:10:38 +0530989 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100990}
Adrian Hunter699b9582011-05-06 12:14:01 +0300991#else
992static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
993 u32 status)
994{
995}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100996#endif /* CONFIG_MMC_DEBUG */
997
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100998/*
999 * MMC controller internal state machines reset
1000 *
1001 * Used to reset command or data internal state machines, using respectively
1002 * SRC or SRD bit of SYSCTL register
1003 * Can be called from interrupt context
1004 */
Denis Karpov70a33412009-09-22 16:44:59 -07001005static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1006 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001007{
1008 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301009 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001010
1011 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1012 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1013
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001014 /*
1015 * OMAP4 ES2 and greater has an updated reset logic.
1016 * Monitor a 0->1 transition first
1017 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001018 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001019 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001020 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301021 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001022 }
1023 i = 0;
1024
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001025 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1026 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301027 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001028
1029 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1030 dev_err(mmc_dev(host->mmc),
1031 "Timeout waiting on controller reset in %s\n",
1032 __func__);
1033}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001034
Balaji T K25e18972012-11-19 21:59:55 +05301035static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1036 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301037{
Balaji T K25e18972012-11-19 21:59:55 +05301038 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301039 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301040 if (host->cmd)
1041 host->cmd->error = err;
1042 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301043
1044 if (host->data) {
1045 omap_hsmmc_reset_controller_fsm(host, SRD);
1046 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301047 } else if (host->mrq && host->mrq->cmd)
1048 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301049}
1050
Adrian Hunterb4175772010-05-26 14:42:06 -07001051static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001052{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001053 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001054 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301055 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001056
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001057 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301058 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001059
Venkatraman Sa7e96872012-11-19 22:00:01 +05301060 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001061 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001062
Venkatraman Sa7e96872012-11-19 22:00:01 +05301063 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301064 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301065 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301066 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301067 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301068 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1069
Balaji T Ka2e77152014-01-21 19:54:42 +05301070 if (status & ACE_EN) {
1071 u32 ac12;
1072 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1073 if (!(ac12 & ACNE) && host->mrq->sbc) {
1074 end_cmd = 1;
1075 if (ac12 & ACTO)
1076 error = -ETIMEDOUT;
1077 else if (ac12 & (ACCE | ACEB | ACIE))
1078 error = -EILSEQ;
1079 host->mrq->sbc->error = error;
1080 hsmmc_command_incomplete(host, error, end_cmd);
1081 }
1082 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1083 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301084 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301085 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301086 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001087 }
1088 }
1089
Francesco Lavra7472bab2013-06-29 08:25:12 +02001090 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301091 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001092 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301093 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001094 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001095}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001096
Adrian Hunterb4175772010-05-26 14:42:06 -07001097/*
1098 * MMC controller IRQ handler
1099 */
1100static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1101{
1102 struct omap_hsmmc_host *host = dev_id;
1103 int status;
1104
1105 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001106 while (status & (INT_EN_MASK | CIRQ_EN)) {
1107 if (host->req_in_progress)
1108 omap_hsmmc_do_irq(host, status);
1109
1110 if (status & CIRQ_EN)
1111 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301112
Adrian Hunterb4175772010-05-26 14:42:06 -07001113 /* Flush posted write */
1114 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301115 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001116
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001117 return IRQ_HANDLED;
1118}
1119
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001120static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1121{
1122 struct omap_hsmmc_host *host = dev_id;
1123
1124 /* cirq is level triggered, disable to avoid infinite loop */
1125 spin_lock(&host->irq_lock);
1126 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1127 disable_irq_nosync(host->wake_irq);
1128 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1129 }
1130 spin_unlock(&host->irq_lock);
1131 pm_request_resume(host->dev); /* no use counter */
1132
1133 return IRQ_HANDLED;
1134}
1135
Denis Karpov70a33412009-09-22 16:44:59 -07001136static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001137{
1138 unsigned long i;
1139
1140 OMAP_HSMMC_WRITE(host->base, HCTL,
1141 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1142 for (i = 0; i < loops_per_jiffy; i++) {
1143 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1144 break;
1145 cpu_relax();
1146 }
1147}
1148
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001149/*
David Brownelleb250822009-02-17 14:49:01 -08001150 * Switch MMC interface voltage ... only relevant for MMC1.
1151 *
1152 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1153 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1154 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001155 */
Denis Karpov70a33412009-09-22 16:44:59 -07001156static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001157{
1158 u32 reg_val = 0;
1159 int ret;
1160
1161 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301162 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301163 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301164 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001165
1166 /* Turn the power off */
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001167 ret = mmc_pdata(host)->set_power(host->dev, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001168
1169 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001170 if (!ret)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001171 ret = mmc_pdata(host)->set_power(host->dev, 1, vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301172 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301173 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301174 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001175
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001176 if (ret != 0)
1177 goto err;
1178
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179 OMAP_HSMMC_WRITE(host->base, HCTL,
1180 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1181 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001182
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001183 /*
1184 * If a MMC dual voltage card is detected, the set_ios fn calls
1185 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001186 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001187 *
David Brownelleb250822009-02-17 14:49:01 -08001188 * Cope with a bit of slop in the range ... per data sheets:
1189 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1190 * but recommended values are 1.71V to 1.89V
1191 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1192 * but recommended values are 2.7V to 3.3V
1193 *
1194 * Board setup code shouldn't permit anything very out-of-range.
1195 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1196 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001197 */
David Brownelleb250822009-02-17 14:49:01 -08001198 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001200 else
1201 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001202
1203 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001204 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205
1206 return 0;
1207err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301208 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001209 return ret;
1210}
1211
Adrian Hunterb62f6222009-09-22 16:45:01 -07001212/* Protect the card while the cover is open */
1213static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1214{
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001215 if (!host->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001216 return;
1217
1218 host->reqs_blocked = 0;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001219 if (host->get_cover_state(host->dev)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001220 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301221 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001222 "card is now accessible\n",
1223 mmc_hostname(host->mmc));
1224 host->protect_card = 0;
1225 }
1226 } else {
1227 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301228 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001229 "card is now inaccessible\n",
1230 mmc_hostname(host->mmc));
1231 host->protect_card = 1;
1232 }
1233 }
1234}
1235
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001236/*
Andreas Fenkartcde592c2015-03-03 13:28:15 +01001237 * irq handler when (cell-phone) cover is mounted/removed
1238 */
1239static irqreturn_t omap_hsmmc_cover_irq(int irq, void *dev_id)
1240{
1241 struct omap_hsmmc_host *host = dev_id;
1242 int carddetect;
1243
1244 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
1245
1246 if (host->card_detect) {
1247 carddetect = host->card_detect(host->dev);
1248 } else {
1249 omap_hsmmc_protect_card(host);
1250 carddetect = -ENOSYS;
1251 }
1252
1253 if (carddetect)
1254 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
1255 else
1256 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
1257 return IRQ_HANDLED;
1258}
1259
1260/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001261 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001262 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001263static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001264{
NeilBrown7efab4f2011-12-30 12:35:13 +11001265 struct omap_hsmmc_host *host = dev_id;
Adrian Huntera6b22402009-09-22 16:44:45 -07001266 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001267
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001268 if (host->card_detect)
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001269 carddetect = host->card_detect(host->dev);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001270 else {
1271 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001272 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001273 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001274
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001275 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001276 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001277 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001278 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001279 return IRQ_HANDLED;
1280}
1281
Russell Kingc5c98922012-04-13 12:14:39 +01001282static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001283{
Russell Kingc5c98922012-04-13 12:14:39 +01001284 struct omap_hsmmc_host *host = param;
1285 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001286 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001287 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001288
Russell Kingc5c98922012-04-13 12:14:39 +01001289 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001290 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001291 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001292 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001293 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001294
Adrian Hunter770d7432011-05-06 12:14:11 +03001295 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001296 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001297 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001298 dma_unmap_sg(chan->device->dev,
1299 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001300 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001301
1302 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001303 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001304 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001305
1306 /* If DMA has finished after TC, complete the request */
1307 if (!req_in_progress) {
1308 struct mmc_request *mrq = host->mrq;
1309
1310 host->mrq = NULL;
1311 mmc_request_done(host->mmc, mrq);
1312 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001313}
1314
Per Forlin9782aff2011-07-01 18:55:23 +02001315static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1316 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001317 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001318 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001319{
1320 int dma_len;
1321
1322 if (!next && data->host_cookie &&
1323 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301324 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001325 " host->next_data.cookie %d\n",
1326 __func__, data->host_cookie, host->next_data.cookie);
1327 data->host_cookie = 0;
1328 }
1329
1330 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001331 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001332 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001333 omap_hsmmc_get_dma_dir(host, data));
1334
1335 } else {
1336 dma_len = host->next_data.dma_len;
1337 host->next_data.dma_len = 0;
1338 }
1339
1340
1341 if (dma_len == 0)
1342 return -EINVAL;
1343
1344 if (next) {
1345 next->dma_len = dma_len;
1346 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1347 } else
1348 host->dma_len = dma_len;
1349
1350 return 0;
1351}
1352
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001353/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001354 * Routine to configure and start DMA for the MMC card
1355 */
Balaji T K9d025332014-01-21 19:54:42 +05301356static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001357 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001358{
Russell King26b88522012-04-13 12:27:37 +01001359 struct dma_slave_config cfg;
1360 struct dma_async_tx_descriptor *tx;
1361 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001362 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001363 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001364
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001365 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001366 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001367 struct scatterlist *sgl;
1368
1369 sgl = data->sg + i;
1370 if (sgl->length % data->blksz)
1371 return -EINVAL;
1372 }
1373 if ((data->blksz % 4) != 0)
1374 /* REVISIT: The MMC buffer increments only when MSB is written.
1375 * Return error for blksz which is non multiple of four.
1376 */
1377 return -EINVAL;
1378
Adrian Hunterb4175772010-05-26 14:42:06 -07001379 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001380
Russell Kingc5c98922012-04-13 12:14:39 +01001381 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001382
Russell King26b88522012-04-13 12:27:37 +01001383 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1384 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1385 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1386 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1387 cfg.src_maxburst = data->blksz / 4;
1388 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001389
Russell King26b88522012-04-13 12:27:37 +01001390 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001391 if (ret)
1392 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001393
Russell King26b88522012-04-13 12:27:37 +01001394 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1395 if (ret)
1396 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001397
Russell King26b88522012-04-13 12:27:37 +01001398 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1399 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1400 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1401 if (!tx) {
1402 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1403 /* FIXME: cleanup */
1404 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001405 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001406
Russell King26b88522012-04-13 12:27:37 +01001407 tx->callback = omap_hsmmc_dma_callback;
1408 tx->callback_param = host;
1409
1410 /* Does not fail */
1411 dmaengine_submit(tx);
1412
1413 host->dma_ch = 1;
1414
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001415 return 0;
1416}
1417
Denis Karpov70a33412009-09-22 16:44:59 -07001418static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001419 unsigned int timeout_ns,
1420 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001421{
1422 unsigned int timeout, cycle_ns;
1423 uint32_t reg, clkd, dto = 0;
1424
1425 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1426 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1427 if (clkd == 0)
1428 clkd = 1;
1429
Balaji T K6e3076c2014-01-21 19:54:42 +05301430 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001431 timeout = timeout_ns / cycle_ns;
1432 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001433 if (timeout) {
1434 while ((timeout & 0x80000000) == 0) {
1435 dto += 1;
1436 timeout <<= 1;
1437 }
1438 dto = 31 - dto;
1439 timeout <<= 1;
1440 if (timeout && dto)
1441 dto += 1;
1442 if (dto >= 13)
1443 dto -= 13;
1444 else
1445 dto = 0;
1446 if (dto > 14)
1447 dto = 14;
1448 }
1449
1450 reg &= ~DTO_MASK;
1451 reg |= dto << DTO_SHIFT;
1452 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1453}
1454
Balaji T K9d025332014-01-21 19:54:42 +05301455static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1456{
1457 struct mmc_request *req = host->mrq;
1458 struct dma_chan *chan;
1459
1460 if (!req->data)
1461 return;
1462 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1463 | (req->data->blocks << 16));
1464 set_data_timeout(host, req->data->timeout_ns,
1465 req->data->timeout_clks);
1466 chan = omap_hsmmc_get_dma_chan(host, req->data);
1467 dma_async_issue_pending(chan);
1468}
1469
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001470/*
1471 * Configure block length for MMC/SD cards and initiate the transfer.
1472 */
1473static int
Denis Karpov70a33412009-09-22 16:44:59 -07001474omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001475{
1476 int ret;
1477 host->data = req->data;
1478
1479 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001480 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001481 /*
1482 * Set an arbitrary 100ms data timeout for commands with
1483 * busy signal.
1484 */
1485 if (req->cmd->flags & MMC_RSP_BUSY)
1486 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001487 return 0;
1488 }
1489
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001490 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301491 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001492 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301493 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001494 return ret;
1495 }
1496 }
1497 return 0;
1498}
1499
Per Forlin9782aff2011-07-01 18:55:23 +02001500static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1501 int err)
1502{
1503 struct omap_hsmmc_host *host = mmc_priv(mmc);
1504 struct mmc_data *data = mrq->data;
1505
Russell King26b88522012-04-13 12:27:37 +01001506 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001507 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001508
Russell King26b88522012-04-13 12:27:37 +01001509 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1510 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001511 data->host_cookie = 0;
1512 }
1513}
1514
1515static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1516 bool is_first_req)
1517{
1518 struct omap_hsmmc_host *host = mmc_priv(mmc);
1519
1520 if (mrq->data->host_cookie) {
1521 mrq->data->host_cookie = 0;
1522 return ;
1523 }
1524
Russell Kingc5c98922012-04-13 12:14:39 +01001525 if (host->use_dma) {
1526 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001527
Per Forlin9782aff2011-07-01 18:55:23 +02001528 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001529 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001530 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001531 }
Per Forlin9782aff2011-07-01 18:55:23 +02001532}
1533
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001534/*
1535 * Request function. for read/write operation
1536 */
Denis Karpov70a33412009-09-22 16:44:59 -07001537static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001538{
Denis Karpov70a33412009-09-22 16:44:59 -07001539 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001540 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001541
Adrian Hunterb4175772010-05-26 14:42:06 -07001542 BUG_ON(host->req_in_progress);
1543 BUG_ON(host->dma_ch != -1);
1544 if (host->protect_card) {
1545 if (host->reqs_blocked < 3) {
1546 /*
1547 * Ensure the controller is left in a consistent
1548 * state by resetting the command and data state
1549 * machines.
1550 */
1551 omap_hsmmc_reset_controller_fsm(host, SRD);
1552 omap_hsmmc_reset_controller_fsm(host, SRC);
1553 host->reqs_blocked += 1;
1554 }
1555 req->cmd->error = -EBADF;
1556 if (req->data)
1557 req->data->error = -EBADF;
1558 req->cmd->retries = 0;
1559 mmc_request_done(mmc, req);
1560 return;
1561 } else if (host->reqs_blocked)
1562 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001563 WARN_ON(host->mrq != NULL);
1564 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301565 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001566 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001567 if (err) {
1568 req->cmd->error = err;
1569 if (req->data)
1570 req->data->error = err;
1571 host->mrq = NULL;
1572 mmc_request_done(mmc, req);
1573 return;
1574 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301575 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301576 omap_hsmmc_start_command(host, req->sbc, NULL);
1577 return;
1578 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001579
Balaji T K9d025332014-01-21 19:54:42 +05301580 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001581 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001582}
1583
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001584/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001585static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001586{
Denis Karpov70a33412009-09-22 16:44:59 -07001587 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001588 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001589
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301590 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001591
Adrian Huntera3621462009-09-22 16:44:42 -07001592 if (ios->power_mode != host->power_mode) {
1593 switch (ios->power_mode) {
1594 case MMC_POWER_OFF:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001595 mmc_pdata(host)->set_power(host->dev, 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001596 break;
1597 case MMC_POWER_UP:
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001598 mmc_pdata(host)->set_power(host->dev, 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001599 break;
1600 case MMC_POWER_ON:
1601 do_send_init_stream = 1;
1602 break;
1603 }
1604 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001605 }
1606
Denis Karpovdd498ef2009-09-22 16:44:49 -07001607 /* FIXME: set registers based only on changes to ios */
1608
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001609 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001610
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301611 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001612 /* Only MMC1 can interface at 3V without some flavor
1613 * of external transceiver; but they all handle 1.8V.
1614 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001615 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301616 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001617 /*
1618 * The mmc_select_voltage fn of the core does
1619 * not seem to set the power_mode to
1620 * MMC_POWER_UP upon recalculating the voltage.
1621 * vdd 1.8v.
1622 */
Denis Karpov70a33412009-09-22 16:44:59 -07001623 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1624 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001625 "Switch operation failed\n");
1626 }
1627 }
1628
Andy Shevchenko5934df22011-05-06 12:14:06 +03001629 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001630
Adrian Huntera3621462009-09-22 16:44:42 -07001631 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001632 send_init_stream(host);
1633
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001634 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001635
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301636 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001637}
1638
1639static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1640{
Denis Karpov70a33412009-09-22 16:44:59 -07001641 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001642
Andreas Fenkartb5cd43f2014-11-08 15:33:16 +01001643 if (!host->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001644 return -ENOSYS;
Andreas Fenkart80412ca2014-11-08 15:33:17 +01001645 return host->card_detect(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001646}
1647
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001648static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1649{
1650 struct omap_hsmmc_host *host = mmc_priv(mmc);
1651
Andreas Fenkart326119c2014-11-08 15:33:14 +01001652 if (mmc_pdata(host)->init_card)
1653 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001654}
1655
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001656static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1657{
1658 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001659 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001660 unsigned long flags;
1661
1662 spin_lock_irqsave(&host->irq_lock, flags);
1663
Balaji T K5a52b082014-05-29 10:28:02 +02001664 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001665 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1666 if (enable) {
1667 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1668 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001669 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001670 } else {
1671 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1672 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001673 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001674 }
Balaji T K5a52b082014-05-29 10:28:02 +02001675 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001676 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1677
1678 /*
1679 * if enable, piggy back detection on current request
1680 * but always disable immediately
1681 */
1682 if (!host->req_in_progress || !enable)
1683 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1684
1685 /* flush posted write */
1686 OMAP_HSMMC_READ(host->base, IE);
1687
1688 spin_unlock_irqrestore(&host->irq_lock, flags);
1689}
1690
1691static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1692{
1693 struct mmc_host *mmc = host->mmc;
1694 int ret;
1695
1696 /*
1697 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1698 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1699 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1700 * with functional clock disabled.
1701 */
1702 if (!host->dev->of_node || !host->wake_irq)
1703 return -ENODEV;
1704
1705 /* Prevent auto-enabling of IRQ */
1706 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1707 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1708 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1709 mmc_hostname(mmc), host);
1710 if (ret) {
1711 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1712 goto err;
1713 }
1714
1715 /*
1716 * Some omaps don't have wake-up path from deeper idle states
1717 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1718 */
1719 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001720 struct pinctrl *p = devm_pinctrl_get(host->dev);
1721 if (!p) {
1722 ret = -ENODEV;
1723 goto err_free_irq;
1724 }
1725 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1726 dev_info(host->dev, "missing default pinctrl state\n");
1727 devm_pinctrl_put(p);
1728 ret = -EINVAL;
1729 goto err_free_irq;
1730 }
1731
1732 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1733 dev_info(host->dev, "missing idle pinctrl state\n");
1734 devm_pinctrl_put(p);
1735 ret = -EINVAL;
1736 goto err_free_irq;
1737 }
1738 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001739 }
1740
Balaji T K5a52b082014-05-29 10:28:02 +02001741 OMAP_HSMMC_WRITE(host->base, HCTL,
1742 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001743 return 0;
1744
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001745err_free_irq:
1746 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001747err:
1748 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1749 host->wake_irq = 0;
1750 return ret;
1751}
1752
Denis Karpov70a33412009-09-22 16:44:59 -07001753static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001754{
1755 u32 hctl, capa, value;
1756
1757 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301758 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001759 hctl = SDVS30;
1760 capa = VS30 | VS18;
1761 } else {
1762 hctl = SDVS18;
1763 capa = VS18;
1764 }
1765
1766 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1767 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1768
1769 value = OMAP_HSMMC_READ(host->base, CAPA);
1770 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1771
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001772 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001773 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001774}
1775
Denis Karpov70a33412009-09-22 16:44:59 -07001776static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001777{
Denis Karpov70a33412009-09-22 16:44:59 -07001778 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001779
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301780 pm_runtime_get_sync(host->dev);
1781
Denis Karpovdd498ef2009-09-22 16:44:49 -07001782 return 0;
1783}
1784
Adrian Hunter907d2e72012-02-29 09:17:21 +02001785static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001786{
Denis Karpov70a33412009-09-22 16:44:59 -07001787 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001788
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301789 pm_runtime_mark_last_busy(host->dev);
1790 pm_runtime_put_autosuspend(host->dev);
1791
Denis Karpovdd498ef2009-09-22 16:44:49 -07001792 return 0;
1793}
1794
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001795static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1796 unsigned int direction, int blk_size)
1797{
1798 /* This controller can't do multiblock reads due to hw bugs */
1799 if (direction == MMC_DATA_READ)
1800 return 1;
1801
1802 return blk_size;
1803}
1804
1805static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001806 .enable = omap_hsmmc_enable_fclk,
1807 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001808 .post_req = omap_hsmmc_post_req,
1809 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001810 .request = omap_hsmmc_request,
1811 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001812 .get_cd = omap_hsmmc_get_cd,
Andreas Fenkarta49d8352015-03-03 13:28:14 +01001813 .get_ro = mmc_gpio_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001814 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001815 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001816};
1817
Denis Karpovd900f712009-09-22 16:44:38 -07001818#ifdef CONFIG_DEBUG_FS
1819
Denis Karpov70a33412009-09-22 16:44:59 -07001820static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001821{
1822 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001823 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001824
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001825 seq_printf(s, "mmc%d:\n", mmc->index);
1826 seq_printf(s, "sdio irq mode\t%s\n",
1827 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1828
1829 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1830 seq_printf(s, "sdio irq \t%s\n",
1831 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1832 : "disabled");
1833 }
1834 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001835
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301836 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001837 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001838 seq_printf(s, "CON:\t\t0x%08x\n",
1839 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001840 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1841 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001842 seq_printf(s, "HCTL:\t\t0x%08x\n",
1843 OMAP_HSMMC_READ(host->base, HCTL));
1844 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1845 OMAP_HSMMC_READ(host->base, SYSCTL));
1846 seq_printf(s, "IE:\t\t0x%08x\n",
1847 OMAP_HSMMC_READ(host->base, IE));
1848 seq_printf(s, "ISE:\t\t0x%08x\n",
1849 OMAP_HSMMC_READ(host->base, ISE));
1850 seq_printf(s, "CAPA:\t\t0x%08x\n",
1851 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001852
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301853 pm_runtime_mark_last_busy(host->dev);
1854 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001855
Denis Karpovd900f712009-09-22 16:44:38 -07001856 return 0;
1857}
1858
Denis Karpov70a33412009-09-22 16:44:59 -07001859static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001860{
Denis Karpov70a33412009-09-22 16:44:59 -07001861 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001862}
1863
1864static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001865 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001866 .read = seq_read,
1867 .llseek = seq_lseek,
1868 .release = single_release,
1869};
1870
Denis Karpov70a33412009-09-22 16:44:59 -07001871static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001872{
1873 if (mmc->debugfs_root)
1874 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1875 mmc, &mmc_regs_fops);
1876}
1877
1878#else
1879
Denis Karpov70a33412009-09-22 16:44:59 -07001880static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001881{
1882}
1883
1884#endif
1885
Rajendra Nayak46856a62012-03-12 20:32:37 +05301886#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001887static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1888 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1889 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1890};
1891
1892static const struct omap_mmc_of_data omap4_mmc_of_data = {
1893 .reg_offset = 0x100,
1894};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001895static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1896 .reg_offset = 0x100,
1897 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1898};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301899
1900static const struct of_device_id omap_mmc_of_match[] = {
1901 {
1902 .compatible = "ti,omap2-hsmmc",
1903 },
1904 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001905 .compatible = "ti,omap3-pre-es3-hsmmc",
1906 .data = &omap3_pre_es3_mmc_of_data,
1907 },
1908 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301909 .compatible = "ti,omap3-hsmmc",
1910 },
1911 {
1912 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001913 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301914 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001915 {
1916 .compatible = "ti,am33xx-hsmmc",
1917 .data = &am33xx_mmc_of_data,
1918 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301919 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001920};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301921MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1922
Andreas Fenkart551434382014-11-08 15:33:09 +01001923static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301924{
Andreas Fenkart551434382014-11-08 15:33:09 +01001925 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301926 struct device_node *np = dev->of_node;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301927
1928 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1929 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301930 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301931
1932 if (of_find_property(np, "ti,dual-volt", NULL))
1933 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1934
NeilBrownfdb9de12015-01-13 08:23:18 +13001935 pdata->switch_pin = -EINVAL;
1936 pdata->gpio_wp = -EINVAL;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301937
1938 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001939 pdata->nonremovable = true;
1940 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301941 }
Rajendra Nayak46856a62012-03-12 20:32:37 +05301942
1943 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001944 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301945
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301946 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001947 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05301948
Rajendra Nayak46856a62012-03-12 20:32:37 +05301949 return pdata;
1950}
1951#else
Andreas Fenkart551434382014-11-08 15:33:09 +01001952static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05301953 *of_get_hsmmc_pdata(struct device *dev)
1954{
Balaji T K19df45b2014-02-28 19:08:18 +05301955 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301956}
1957#endif
1958
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05001959static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001960{
Andreas Fenkart551434382014-11-08 15:33:09 +01001961 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001962 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07001963 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001964 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08001965 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301966 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01001967 dma_cap_mask_t mask;
1968 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06001969 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05301970 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301971
1972 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
1973 if (match) {
1974 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01001975
1976 if (IS_ERR(pdata))
1977 return PTR_ERR(pdata);
1978
Rajendra Nayak46856a62012-03-12 20:32:37 +05301979 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06001980 data = match->data;
1981 pdata->reg_offset = data->reg_offset;
1982 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301983 }
1984 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001985
1986 if (pdata == NULL) {
1987 dev_err(&pdev->dev, "Platform Data is missing\n");
1988 return -ENXIO;
1989 }
1990
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001991 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1992 irq = platform_get_irq(pdev, 0);
1993 if (res == NULL || irq < 0)
1994 return -ENXIO;
1995
Balaji T K77fae212014-05-09 22:16:51 +05301996 base = devm_ioremap_resource(&pdev->dev, res);
1997 if (IS_ERR(base))
1998 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001999
Denis Karpov70a33412009-09-22 16:44:59 -07002000 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002001 if (!mmc) {
2002 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002003 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002004 }
2005
NeilBrownfdb9de12015-01-13 08:23:18 +13002006 ret = mmc_of_parse(mmc);
2007 if (ret)
2008 goto err1;
2009
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002010 host = mmc_priv(mmc);
2011 host->mmc = mmc;
2012 host->pdata = pdata;
2013 host->dev = &pdev->dev;
2014 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002015 host->dma_ch = -1;
2016 host->irq = irq;
Balaji T Kfc307df2012-04-02 12:26:47 +05302017 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302018 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002019 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002020 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302021 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002022
NeilBrown41afa3142015-01-13 08:23:18 +13002023 ret = omap_hsmmc_gpio_init(mmc, host, pdata);
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002024 if (ret)
2025 goto err_gpio;
2026
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002027 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002029 if (pdev->dev.of_node)
2030 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2031
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302032 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002033
Daniel Mackd418ed82012-02-19 13:20:33 +01002034 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2035
2036 if (pdata->max_freq > 0)
2037 mmc->f_max = pdata->max_freq;
NeilBrownfdb9de12015-01-13 08:23:18 +13002038 else if (mmc->f_max == 0)
Daniel Mackd418ed82012-02-19 13:20:33 +01002039 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002040
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002041 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002042
Balaji T K96181952014-05-09 22:16:48 +05302043 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002044 if (IS_ERR(host->fclk)) {
2045 ret = PTR_ERR(host->fclk);
2046 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002047 goto err1;
2048 }
2049
Paul Walmsley9b682562011-10-06 14:50:35 -06002050 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2051 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 -07002052 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002053 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002054
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302055 pm_runtime_enable(host->dev);
2056 pm_runtime_get_sync(host->dev);
2057 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2058 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002059
Balaji T K92a3aeb2012-02-24 21:14:34 +05302060 omap_hsmmc_context_save(host);
2061
Balaji T K96181952014-05-09 22:16:48 +05302062 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302063 /*
2064 * MMC can still work without debounce clock.
2065 */
2066 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302067 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302068 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302069 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302070 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002071 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002072
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002073 /* Since we do only SG emulation, we can have as many segs
2074 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002075 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002076
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002077 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2078 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2079 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2080 mmc->max_seg_size = mmc->max_req_size;
2081
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002082 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002083 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002084
Andreas Fenkart326119c2014-11-08 15:33:14 +01002085 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002086 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002087 mmc->caps |= MMC_CAP_4_BIT_DATA;
2088
Andreas Fenkart326119c2014-11-08 15:33:14 +01002089 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002090 mmc->caps |= MMC_CAP_NONREMOVABLE;
2091
NeilBrownfdb9de12015-01-13 08:23:18 +13002092 mmc->pm_caps |= mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002093
Denis Karpov70a33412009-09-22 16:44:59 -07002094 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002095
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302096 if (!pdev->dev.of_node) {
2097 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2098 if (!res) {
2099 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2100 ret = -ENXIO;
2101 goto err_irq;
2102 }
2103 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002104
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302105 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2106 if (!res) {
2107 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2108 ret = -ENXIO;
2109 goto err_irq;
2110 }
2111 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002112 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113
Russell King26b88522012-04-13 12:27:37 +01002114 dma_cap_zero(mask);
2115 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002116
Matt Porterd272fbf2013-05-10 17:42:34 +05302117 host->rx_chan =
2118 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2119 &rx_req, &pdev->dev, "rx");
2120
Russell King26b88522012-04-13 12:27:37 +01002121 if (!host->rx_chan) {
2122 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002123 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002124 goto err_irq;
2125 }
2126
Matt Porterd272fbf2013-05-10 17:42:34 +05302127 host->tx_chan =
2128 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2129 &tx_req, &pdev->dev, "tx");
2130
Russell King26b88522012-04-13 12:27:37 +01002131 if (!host->tx_chan) {
2132 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002133 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002134 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002135 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002136
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002137 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302138 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002139 mmc_hostname(mmc), host);
2140 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302141 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002142 goto err_irq;
2143 }
2144
Andreas Fenkart326119c2014-11-08 15:33:14 +01002145 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002146 ret = omap_hsmmc_reg_get(host);
2147 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002148 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002149 host->use_reg = 1;
2150 }
2151
Andreas Fenkart326119c2014-11-08 15:33:14 +01002152 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153
Adrian Hunterb4175772010-05-26 14:42:06 -07002154 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002155
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002156 /*
2157 * For now, only support SDIO interrupt if we have a separate
2158 * wake-up interrupt configured from device tree. This is because
2159 * the wake-up interrupt is needed for idle state and some
2160 * platforms need special quirks. And we don't want to add new
2161 * legacy mux platform init code callbacks any longer as we
2162 * are moving to DT based booting anyways.
2163 */
2164 ret = omap_hsmmc_configure_wake_irq(host);
2165 if (!ret)
2166 mmc->caps |= MMC_CAP_SDIO_IRQ;
2167
Adrian Hunterb62f6222009-09-22 16:45:01 -07002168 omap_hsmmc_protect_card(host);
2169
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002170 mmc_add_host(mmc);
2171
Andreas Fenkart326119c2014-11-08 15:33:14 +01002172 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002173 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2174 if (ret < 0)
2175 goto err_slot_name;
2176 }
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002177 if (host->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002178 ret = device_create_file(&mmc->class_dev,
Andreas Fenkartcde592c2015-03-03 13:28:15 +01002179 &dev_attr_cover_switch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002180 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002181 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002182 }
2183
Denis Karpov70a33412009-09-22 16:44:59 -07002184 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302185 pm_runtime_mark_last_busy(host->dev);
2186 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002187
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002188 return 0;
2189
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002190err_slot_name:
2191 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002192 if (host->use_reg)
2193 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002194err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002195 if (host->tx_chan)
2196 dma_release_channel(host->tx_chan);
2197 if (host->rx_chan)
2198 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302199 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002200 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302201 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302202 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002203err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002204err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002205 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002206err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002207 return ret;
2208}
2209
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002210static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002211{
Denis Karpov70a33412009-09-22 16:44:59 -07002212 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002213
Felipe Balbi927ce942012-03-14 11:18:27 +02002214 pm_runtime_get_sync(host->dev);
2215 mmc_remove_host(host->mmc);
2216 if (host->use_reg)
2217 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002218
Russell Kingc5c98922012-04-13 12:14:39 +01002219 if (host->tx_chan)
2220 dma_release_channel(host->tx_chan);
2221 if (host->rx_chan)
2222 dma_release_channel(host->rx_chan);
2223
Felipe Balbi927ce942012-03-14 11:18:27 +02002224 pm_runtime_put_sync(host->dev);
2225 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302226 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302227 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002228
Balaji T K9d1f0282012-10-15 21:35:07 +05302229 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002230
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002231 return 0;
2232}
2233
2234#ifdef CONFIG_PM
Kevin Hilmana791daa2010-05-26 14:42:07 -07002235static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002236{
Felipe Balbi927ce942012-03-14 11:18:27 +02002237 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2238
2239 if (!host)
2240 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002241
Felipe Balbi927ce942012-03-14 11:18:27 +02002242 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002243
2244 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002245 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2246 OMAP_HSMMC_WRITE(host->base, IE, 0);
2247 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002248 OMAP_HSMMC_WRITE(host->base, HCTL,
2249 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2250 }
2251
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002252 /* do not wake up due to sdio irq */
2253 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2254 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2255 disable_irq(host->wake_irq);
2256
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302257 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302258 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002259
Eliad Peller31f9d462011-11-22 16:02:17 +02002260 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002261 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002262}
2263
2264/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002265static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002266{
Felipe Balbi927ce942012-03-14 11:18:27 +02002267 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2268
2269 if (!host)
2270 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002271
Felipe Balbi927ce942012-03-14 11:18:27 +02002272 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002273
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302274 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302275 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002276
Felipe Balbi927ce942012-03-14 11:18:27 +02002277 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2278 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002279
Felipe Balbi927ce942012-03-14 11:18:27 +02002280 omap_hsmmc_protect_card(host);
2281
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002282 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2283 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2284 enable_irq(host->wake_irq);
2285
Felipe Balbi927ce942012-03-14 11:18:27 +02002286 pm_runtime_mark_last_busy(host->dev);
2287 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002288 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002289}
2290
2291#else
Denis Karpov70a33412009-09-22 16:44:59 -07002292#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302293#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002294#endif
2295
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302296static int omap_hsmmc_runtime_suspend(struct device *dev)
2297{
2298 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002299 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002300 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302301
2302 host = platform_get_drvdata(to_platform_device(dev));
2303 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002304 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302305
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002306 spin_lock_irqsave(&host->irq_lock, flags);
2307 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2308 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2309 /* disable sdio irq handling to prevent race */
2310 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2311 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002312
2313 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2314 /*
2315 * dat1 line low, pending sdio irq
2316 * race condition: possible irq handler running on
2317 * multi-core, abort
2318 */
2319 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2320 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2321 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2322 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2323 pm_runtime_mark_last_busy(dev);
2324 ret = -EBUSY;
2325 goto abort;
2326 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002327
Andreas Fenkart97978a42014-05-29 10:28:04 +02002328 pinctrl_pm_select_idle_state(dev);
2329
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002330 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2331 enable_irq(host->wake_irq);
2332 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002333 } else {
2334 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002335 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002336
Andreas Fenkartf9459012014-05-29 10:28:03 +02002337abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002338 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002339 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302340}
2341
2342static int omap_hsmmc_runtime_resume(struct device *dev)
2343{
2344 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002345 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302346
2347 host = platform_get_drvdata(to_platform_device(dev));
2348 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002349 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302350
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002351 spin_lock_irqsave(&host->irq_lock, flags);
2352 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2353 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2354 /* sdio irq flag can't change while in runtime suspend */
2355 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2356 disable_irq_nosync(host->wake_irq);
2357 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2358 }
2359
Andreas Fenkart97978a42014-05-29 10:28:04 +02002360 pinctrl_pm_select_default_state(host->dev);
2361
2362 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002363 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2364 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2365 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002366 } else {
2367 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002368 }
2369 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302370 return 0;
2371}
2372
Kevin Hilmana791daa2010-05-26 14:42:07 -07002373static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002374 .suspend = omap_hsmmc_suspend,
2375 .resume = omap_hsmmc_resume,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302376 .runtime_suspend = omap_hsmmc_runtime_suspend,
2377 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002378};
2379
2380static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002381 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002382 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002383 .driver = {
2384 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002385 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302386 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002387 },
2388};
2389
Felipe Balbib7964502012-03-14 11:18:32 +02002390module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002391MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2392MODULE_LICENSE("GPL");
2393MODULE_ALIAS("platform:" DRIVER_NAME);
2394MODULE_AUTHOR("Texas Instruments Inc");