blob: 8a216c92c5a888cf8b56c80d85a89b274a1b5993 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
2 * drivers/mmc/host/omap_hsmmc.c
3 *
4 * Driver for OMAP2430/3430 MMC controller.
5 *
6 * Copyright (C) 2007 Texas Instruments.
7 *
8 * Authors:
9 * Syed Mohammed Khasim <x0khasim@ti.com>
10 * Madhusudhan <madhu.cr@ti.com>
11 * Mohit Jalori <mjalori@ti.com>
12 *
13 * This file is licensed under the terms of the GNU General Public License
14 * version 2. This program is licensed "as is" without any warranty of any
15 * kind, whether express or implied.
16 */
17
18#include <linux/module.h>
19#include <linux/init.h>
Andy Shevchenkoac330f442011-05-10 15:51:54 +030020#include <linux/kernel.h>
Denis Karpovd900f712009-09-22 16:44:38 -070021#include <linux/debugfs.h>
Russell Kingc5c98922012-04-13 12:14:39 +010022#include <linux/dmaengine.h>
Denis Karpovd900f712009-09-22 16:44:38 -070023#include <linux/seq_file.h>
Felipe Balbi031cd032013-07-11 16:09:05 +030024#include <linux/sizes.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010025#include <linux/interrupt.h>
26#include <linux/delay.h>
27#include <linux/dma-mapping.h>
28#include <linux/platform_device.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010029#include <linux/timer.h>
30#include <linux/clk.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053031#include <linux/of.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020032#include <linux/of_irq.h>
Rajendra Nayak46856a62012-03-12 20:32:37 +053033#include <linux/of_gpio.h>
34#include <linux/of_device.h>
Balaji T Kee526d52014-05-09 22:16:53 +053035#include <linux/omap-dmaengine.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010036#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070037#include <linux/mmc/core.h>
Adrian Hunter93caf8e692010-08-11 14:17:48 -070038#include <linux/mmc/mmc.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010039#include <linux/io.h>
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +020040#include <linux/irq.h>
Adrian Hunterdb0fefc2010-02-15 10:03:34 -080041#include <linux/gpio.h>
42#include <linux/regulator/consumer.h>
Daniel Mack46b76032012-10-15 21:35:05 +053043#include <linux/pinctrl/consumer.h>
Balaji T Kfa4aa2d2011-07-01 22:09:35 +053044#include <linux/pm_runtime.h>
Andreas Fenkart551434382014-11-08 15:33:09 +010045#include <linux/platform_data/hsmmc-omap.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010046
47/* OMAP HSMMC Host Controller Registers */
Denis Karpov11dd62a2009-09-22 16:44:43 -070048#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010049#define OMAP_HSMMC_CON 0x002C
Balaji T Ka2e77152014-01-21 19:54:42 +053050#define OMAP_HSMMC_SDMASA 0x0100
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010051#define OMAP_HSMMC_BLK 0x0104
52#define OMAP_HSMMC_ARG 0x0108
53#define OMAP_HSMMC_CMD 0x010C
54#define OMAP_HSMMC_RSP10 0x0110
55#define OMAP_HSMMC_RSP32 0x0114
56#define OMAP_HSMMC_RSP54 0x0118
57#define OMAP_HSMMC_RSP76 0x011C
58#define OMAP_HSMMC_DATA 0x0120
Andreas Fenkartbb0635f2014-05-29 10:28:01 +020059#define OMAP_HSMMC_PSTATE 0x0124
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010060#define OMAP_HSMMC_HCTL 0x0128
61#define OMAP_HSMMC_SYSCTL 0x012C
62#define OMAP_HSMMC_STAT 0x0130
63#define OMAP_HSMMC_IE 0x0134
64#define OMAP_HSMMC_ISE 0x0138
Balaji T Ka2e77152014-01-21 19:54:42 +053065#define OMAP_HSMMC_AC12 0x013C
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010066#define OMAP_HSMMC_CAPA 0x0140
67
68#define VS18 (1 << 26)
69#define VS30 (1 << 25)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053070#define HSS (1 << 21)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010071#define SDVS18 (0x5 << 9)
72#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080073#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010074#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010075#define SDVSCLR 0xFFFFF1FF
76#define SDVSDET 0x00000400
77#define AUTOIDLE 0x1
78#define SDBP (1 << 8)
79#define DTO 0xe
80#define ICE 0x1
81#define ICS 0x2
82#define CEN (1 << 2)
Balaji T Ked164182013-10-21 00:25:21 +053083#define CLKD_MAX 0x3FF /* max clock divisor: 1023 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010084#define CLKD_MASK 0x0000FFC0
85#define CLKD_SHIFT 6
86#define DTO_MASK 0x000F0000
87#define DTO_SHIFT 16
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010088#define INIT_STREAM (1 << 1)
Balaji T Ka2e77152014-01-21 19:54:42 +053089#define ACEN_ACMD23 (2 << 2)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010090#define DP_SELECT (1 << 21)
91#define DDIR (1 << 4)
Venkatraman Sa7e96872012-11-19 22:00:01 +053092#define DMAE 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010093#define MSBS (1 << 5)
94#define BCE (1 << 1)
95#define FOUR_BIT (1 << 1)
Hebbar, Gururajacd587092012-11-19 21:59:58 +053096#define HSPE (1 << 2)
Balaji T K5a52b082014-05-29 10:28:02 +020097#define IWE (1 << 24)
Balaji T K03b5d9242012-04-09 12:08:33 +053098#define DDR (1 << 19)
Balaji T K5a52b082014-05-29 10:28:02 +020099#define CLKEXTFREE (1 << 16)
100#define CTPL (1 << 11)
Jarkko Lavinen73153012008-11-21 16:49:54 +0200101#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100102#define OD 0x1
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103#define STAT_CLEAR 0xFFFFFFFF
104#define INIT_STREAM_CMD 0x00000000
105#define DUAL_VOLT_OCR_BIT 7
106#define SRC (1 << 25)
107#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700108#define SOFTRESET (1 << 1)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100109
Andreas Fenkartf9459012014-05-29 10:28:03 +0200110/* PSTATE */
111#define DLEV_DAT(x) (1 << (20 + (x)))
112
Venkatraman Sa7e96872012-11-19 22:00:01 +0530113/* Interrupt masks for IE and ISE register */
114#define CC_EN (1 << 0)
115#define TC_EN (1 << 1)
116#define BWR_EN (1 << 4)
117#define BRR_EN (1 << 5)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200118#define CIRQ_EN (1 << 8)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530119#define ERR_EN (1 << 15)
120#define CTO_EN (1 << 16)
121#define CCRC_EN (1 << 17)
122#define CEB_EN (1 << 18)
123#define CIE_EN (1 << 19)
124#define DTO_EN (1 << 20)
125#define DCRC_EN (1 << 21)
126#define DEB_EN (1 << 22)
Balaji T Ka2e77152014-01-21 19:54:42 +0530127#define ACE_EN (1 << 24)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530128#define CERR_EN (1 << 28)
129#define BADA_EN (1 << 29)
130
Balaji T Ka2e77152014-01-21 19:54:42 +0530131#define INT_EN_MASK (BADA_EN | CERR_EN | ACE_EN | DEB_EN | DCRC_EN |\
Venkatraman Sa7e96872012-11-19 22:00:01 +0530132 DTO_EN | CIE_EN | CEB_EN | CCRC_EN | CTO_EN | \
133 BRR_EN | BWR_EN | TC_EN | CC_EN)
134
Balaji T Ka2e77152014-01-21 19:54:42 +0530135#define CNI (1 << 7)
136#define ACIE (1 << 4)
137#define ACEB (1 << 3)
138#define ACCE (1 << 2)
139#define ACTO (1 << 1)
140#define ACNE (1 << 0)
141
Balaji T Kfa4aa2d2011-07-01 22:09:35 +0530142#define MMC_AUTOSUSPEND_DELAY 100
Jianpeng Ma1e881782013-10-21 00:25:20 +0530143#define MMC_TIMEOUT_MS 20 /* 20 mSec */
144#define MMC_TIMEOUT_US 20000 /* 20000 micro Sec */
Andy Shevchenko6b206ef2011-07-13 11:16:29 -0400145#define OMAP_MMC_MIN_CLOCK 400000
146#define OMAP_MMC_MAX_CLOCK 52000000
Kishore Kadiyala0005ae72011-02-28 20:48:05 +0530147#define DRIVER_NAME "omap_hsmmc"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100148
Balaji T Ke99448f2014-02-19 20:26:40 +0530149#define VDD_1V8 1800000 /* 180000 uV */
150#define VDD_3V0 3000000 /* 300000 uV */
151#define VDD_165_195 (ffs(MMC_VDD_165_195) - 1)
152
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153/*
154 * One controller can have multiple slots, like on some omap boards using
155 * omap.c controller driver. Luckily this is not currently done on any known
156 * omap_hsmmc.c device.
157 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100158#define mmc_pdata(host) host->pdata
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100159
160/*
161 * MMC Host controller read/write API's
162 */
163#define OMAP_HSMMC_READ(base, reg) \
164 __raw_readl((base) + OMAP_HSMMC_##reg)
165
166#define OMAP_HSMMC_WRITE(base, reg, val) \
167 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
168
Per Forlin9782aff2011-07-01 18:55:23 +0200169struct omap_hsmmc_next {
170 unsigned int dma_len;
171 s32 cookie;
172};
173
Denis Karpov70a33412009-09-22 16:44:59 -0700174struct omap_hsmmc_host {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100175 struct device *dev;
176 struct mmc_host *mmc;
177 struct mmc_request *mrq;
178 struct mmc_command *cmd;
179 struct mmc_data *data;
180 struct clk *fclk;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100181 struct clk *dbclk;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800182 /*
183 * vcc == configured supply
184 * vcc_aux == optional
185 * - MMC1, supply for DAT4..DAT7
186 * - MMC2/MMC2, external level shifter voltage supply, for
187 * chip (SDIO, eMMC, etc) or transceiver (MMC2 only)
188 */
189 struct regulator *vcc;
190 struct regulator *vcc_aux;
Balaji T Ke99448f2014-02-19 20:26:40 +0530191 struct regulator *pbias;
192 bool pbias_enabled;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100193 void __iomem *base;
194 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700195 spinlock_t irq_lock; /* Prevent races with irq handler */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100196 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200197 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100198 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700199 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100200 int suspended;
Tony Lindgren0a82e062013-10-21 00:25:19 +0530201 u32 con;
202 u32 hctl;
203 u32 sysctl;
204 u32 capa;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100205 int irq;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200206 int wake_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100207 int use_dma, dma_ch;
Russell Kingc5c98922012-04-13 12:14:39 +0100208 struct dma_chan *tx_chan;
209 struct dma_chan *rx_chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100210 int slot_id;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200211 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700212 int context_loss;
Adrian Hunterb62f6222009-09-22 16:45:01 -0700213 int protect_card;
214 int reqs_blocked;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800215 int use_reg;
Adrian Hunterb4175772010-05-26 14:42:06 -0700216 int req_in_progress;
Balaji T K6e3076c2014-01-21 19:54:42 +0530217 unsigned long clk_rate;
Balaji T Ka2e77152014-01-21 19:54:42 +0530218 unsigned int flags;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200219#define AUTO_CMD23 (1 << 0) /* Auto CMD23 support */
220#define HSMMC_SDIO_IRQ_ENABLED (1 << 1) /* SDIO irq enabled */
221#define HSMMC_WAKE_IRQ_ENABLED (1 << 2)
Per Forlin9782aff2011-07-01 18:55:23 +0200222 struct omap_hsmmc_next next_data;
Andreas Fenkart551434382014-11-08 15:33:09 +0100223 struct omap_hsmmc_platform_data *pdata;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100224};
225
Nishanth Menon59445b12014-02-13 23:45:48 -0600226struct omap_mmc_of_data {
227 u32 reg_offset;
228 u8 controller_flags;
229};
230
Balaji T Kbf129e12014-01-21 19:54:42 +0530231static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host);
232
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800233static int omap_hsmmc_card_detect(struct device *dev, int slot)
234{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530235 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100236 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800237
238 /* NOTE: assumes card detect signal is active-low */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100239 return !gpio_get_value_cansleep(mmc->switch_pin);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800240}
241
242static int omap_hsmmc_get_wp(struct device *dev, int slot)
243{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530244 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100245 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800246
247 /* NOTE: assumes write protect signal is active-high */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100248 return gpio_get_value_cansleep(mmc->gpio_wp);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800249}
250
251static int omap_hsmmc_get_cover_state(struct device *dev, int slot)
252{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530253 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100254 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800255
256 /* NOTE: assumes card detect signal is active-low */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100257 return !gpio_get_value_cansleep(mmc->switch_pin);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800258}
259
260#ifdef CONFIG_PM
261
262static int omap_hsmmc_suspend_cdirq(struct device *dev, int slot)
263{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530264 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100265 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800266
Andreas Fenkart326119c2014-11-08 15:33:14 +0100267 disable_irq(mmc->card_detect_irq);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800268 return 0;
269}
270
271static int omap_hsmmc_resume_cdirq(struct device *dev, int slot)
272{
Balaji T K9ea28ec2012-10-15 21:35:08 +0530273 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
Andreas Fenkart551434382014-11-08 15:33:09 +0100274 struct omap_hsmmc_platform_data *mmc = host->pdata;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800275
Andreas Fenkart326119c2014-11-08 15:33:14 +0100276 enable_irq(mmc->card_detect_irq);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800277 return 0;
278}
279
280#else
281
282#define omap_hsmmc_suspend_cdirq NULL
283#define omap_hsmmc_resume_cdirq NULL
284
285#endif
286
Adrian Hunterb702b102010-02-15 10:03:35 -0800287#ifdef CONFIG_REGULATOR
288
Rajendra Nayak69b07ec2012-03-07 09:55:30 -0500289static int omap_hsmmc_set_power(struct device *dev, int slot, int power_on,
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800290 int vdd)
291{
292 struct omap_hsmmc_host *host =
293 platform_get_drvdata(to_platform_device(dev));
294 int ret = 0;
295
296 /*
297 * If we don't see a Vcc regulator, assume it's a fixed
298 * voltage always-on regulator.
299 */
300 if (!host->vcc)
301 return 0;
302
Andreas Fenkart326119c2014-11-08 15:33:14 +0100303 if (mmc_pdata(host)->before_set_reg)
304 mmc_pdata(host)->before_set_reg(dev, slot, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800305
Balaji T Ke99448f2014-02-19 20:26:40 +0530306 if (host->pbias) {
307 if (host->pbias_enabled == 1) {
308 ret = regulator_disable(host->pbias);
309 if (!ret)
310 host->pbias_enabled = 0;
311 }
312 regulator_set_voltage(host->pbias, VDD_3V0, VDD_3V0);
313 }
314
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800315 /*
316 * Assume Vcc regulator is used only to power the card ... OMAP
317 * VDDS is used to power the pins, optionally with a transceiver to
318 * support cards using voltages other than VDDS (1.8V nominal). When a
319 * transceiver is used, DAT3..7 are muxed as transceiver control pins.
320 *
321 * In some cases this regulator won't support enable/disable;
322 * e.g. it's a fixed rail for a WLAN chip.
323 *
324 * In other cases vcc_aux switches interface power. Example, for
325 * eMMC cards it represents VccQ. Sometimes transceivers or SDIO
326 * chips/cards need an interface voltage rail too.
327 */
328 if (power_on) {
Balaji T K987fd492014-02-19 20:26:40 +0530329 if (host->vcc)
330 ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800331 /* Enable interface voltage rail, if needed */
332 if (ret == 0 && host->vcc_aux) {
333 ret = regulator_enable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530334 if (ret < 0 && host->vcc)
Linus Walleij99fc5132010-09-29 01:08:27 -0400335 ret = mmc_regulator_set_ocr(host->mmc,
336 host->vcc, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800337 }
338 } else {
Linus Walleij99fc5132010-09-29 01:08:27 -0400339 /* Shut down the rail */
Adrian Hunter6da20c82010-02-15 10:03:34 -0800340 if (host->vcc_aux)
341 ret = regulator_disable(host->vcc_aux);
Balaji T K987fd492014-02-19 20:26:40 +0530342 if (host->vcc) {
Linus Walleij99fc5132010-09-29 01:08:27 -0400343 /* Then proceed to shut down the local regulator */
344 ret = mmc_regulator_set_ocr(host->mmc,
345 host->vcc, 0);
346 }
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800347 }
348
Balaji T Ke99448f2014-02-19 20:26:40 +0530349 if (host->pbias) {
350 if (vdd <= VDD_165_195)
351 ret = regulator_set_voltage(host->pbias, VDD_1V8,
352 VDD_1V8);
353 else
354 ret = regulator_set_voltage(host->pbias, VDD_3V0,
355 VDD_3V0);
356 if (ret < 0)
357 goto error_set_power;
358
359 if (host->pbias_enabled == 0) {
360 ret = regulator_enable(host->pbias);
361 if (!ret)
362 host->pbias_enabled = 1;
363 }
364 }
365
Andreas Fenkart326119c2014-11-08 15:33:14 +0100366 if (mmc_pdata(host)->after_set_reg)
367 mmc_pdata(host)->after_set_reg(dev, slot, power_on, vdd);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800368
Balaji T Ke99448f2014-02-19 20:26:40 +0530369error_set_power:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800370 return ret;
371}
372
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800373static int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
374{
375 struct regulator *reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700376 int ocr_value = 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800377
Balaji T Kf2ddc1d2014-02-19 20:26:40 +0530378 reg = devm_regulator_get(host->dev, "vmmc");
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800379 if (IS_ERR(reg)) {
Balaji T K987fd492014-02-19 20:26:40 +0530380 dev_err(host->dev, "unable to get vmmc regulator %ld\n",
381 PTR_ERR(reg));
NeilBrown1fdc90f2012-08-08 00:06:00 -0400382 return PTR_ERR(reg);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800383 } else {
384 host->vcc = reg;
kishore kadiyala64be9782010-10-01 16:35:28 -0700385 ocr_value = mmc_regulator_get_ocrmask(reg);
Andreas Fenkart326119c2014-11-08 15:33:14 +0100386 if (!mmc_pdata(host)->ocr_mask) {
387 mmc_pdata(host)->ocr_mask = ocr_value;
kishore kadiyala64be9782010-10-01 16:35:28 -0700388 } else {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100389 if (!(mmc_pdata(host)->ocr_mask & ocr_value)) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +0530390 dev_err(host->dev, "ocrmask %x is not supported\n",
Andreas Fenkart326119c2014-11-08 15:33:14 +0100391 mmc_pdata(host)->ocr_mask);
392 mmc_pdata(host)->ocr_mask = 0;
kishore kadiyala64be9782010-10-01 16:35:28 -0700393 return -EINVAL;
394 }
395 }
Balaji T K987fd492014-02-19 20:26:40 +0530396 }
Andreas Fenkart326119c2014-11-08 15:33:14 +0100397 mmc_pdata(host)->set_power = omap_hsmmc_set_power;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800398
Balaji T K987fd492014-02-19 20:26:40 +0530399 /* Allow an aux regulator */
400 reg = devm_regulator_get_optional(host->dev, "vmmc_aux");
401 host->vcc_aux = IS_ERR(reg) ? NULL : reg;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800402
Balaji T Ke99448f2014-02-19 20:26:40 +0530403 reg = devm_regulator_get_optional(host->dev, "pbias");
404 host->pbias = IS_ERR(reg) ? NULL : reg;
405
Balaji T K987fd492014-02-19 20:26:40 +0530406 /* For eMMC do not power off when not in sleep state */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100407 if (mmc_pdata(host)->no_regulator_off_init)
Balaji T K987fd492014-02-19 20:26:40 +0530408 return 0;
409 /*
410 * To disable boot_on regulator, enable regulator
411 * to increase usecount and then disable it.
412 */
413 if ((host->vcc && regulator_is_enabled(host->vcc) > 0) ||
414 (host->vcc_aux && regulator_is_enabled(host->vcc_aux))) {
Andreas Fenkart326119c2014-11-08 15:33:14 +0100415 int vdd = ffs(mmc_pdata(host)->ocr_mask) - 1;
Adrian Huntere840ce12011-05-06 12:14:10 +0300416
Andreas Fenkart326119c2014-11-08 15:33:14 +0100417 mmc_pdata(host)->set_power(host->dev, host->slot_id, 1, vdd);
418 mmc_pdata(host)->set_power(host->dev, host->slot_id, 0, 0);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800419 }
420
421 return 0;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800422}
423
424static void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
425{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100426 mmc_pdata(host)->set_power = NULL;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -0800427}
428
Adrian Hunterb702b102010-02-15 10:03:35 -0800429static inline int omap_hsmmc_have_reg(void)
430{
431 return 1;
432}
433
434#else
435
436static inline int omap_hsmmc_reg_get(struct omap_hsmmc_host *host)
437{
438 return -EINVAL;
439}
440
441static inline void omap_hsmmc_reg_put(struct omap_hsmmc_host *host)
442{
443}
444
445static inline int omap_hsmmc_have_reg(void)
446{
447 return 0;
448}
449
450#endif
451
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100452static int omap_hsmmc_gpio_init(struct omap_hsmmc_host *host,
453 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800454{
455 int ret;
456
Andreas Fenkart326119c2014-11-08 15:33:14 +0100457 if (gpio_is_valid(pdata->switch_pin)) {
458 if (pdata->cover)
459 pdata->get_cover_state =
Adrian Hunterb702b102010-02-15 10:03:35 -0800460 omap_hsmmc_get_cover_state;
461 else
Andreas Fenkart326119c2014-11-08 15:33:14 +0100462 pdata->card_detect = omap_hsmmc_card_detect;
463 pdata->card_detect_irq =
464 gpio_to_irq(pdata->switch_pin);
465 ret = gpio_request(pdata->switch_pin, "mmc_cd");
Adrian Hunterb702b102010-02-15 10:03:35 -0800466 if (ret)
467 return ret;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100468 ret = gpio_direction_input(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800469 if (ret)
470 goto err_free_sp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100471 } else {
472 pdata->switch_pin = -EINVAL;
473 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800474
Andreas Fenkart326119c2014-11-08 15:33:14 +0100475 if (gpio_is_valid(pdata->gpio_wp)) {
476 pdata->get_ro = omap_hsmmc_get_wp;
477 ret = gpio_request(pdata->gpio_wp, "mmc_wp");
Adrian Hunterb702b102010-02-15 10:03:35 -0800478 if (ret)
479 goto err_free_cd;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100480 ret = gpio_direction_input(pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800481 if (ret)
482 goto err_free_wp;
Andreas Fenkart326119c2014-11-08 15:33:14 +0100483 } else {
484 pdata->gpio_wp = -EINVAL;
485 }
Adrian Hunterb702b102010-02-15 10:03:35 -0800486
487 return 0;
488
489err_free_wp:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100490 gpio_free(pdata->gpio_wp);
Adrian Hunterb702b102010-02-15 10:03:35 -0800491err_free_cd:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100492 if (gpio_is_valid(pdata->switch_pin))
Adrian Hunterb702b102010-02-15 10:03:35 -0800493err_free_sp:
Andreas Fenkart326119c2014-11-08 15:33:14 +0100494 gpio_free(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800495 return ret;
496}
497
Andreas Fenkart1e363e32014-11-08 15:33:15 +0100498static void omap_hsmmc_gpio_free(struct omap_hsmmc_host *host,
499 struct omap_hsmmc_platform_data *pdata)
Adrian Hunterb702b102010-02-15 10:03:35 -0800500{
Andreas Fenkart326119c2014-11-08 15:33:14 +0100501 if (gpio_is_valid(pdata->gpio_wp))
502 gpio_free(pdata->gpio_wp);
503 if (gpio_is_valid(pdata->switch_pin))
504 gpio_free(pdata->switch_pin);
Adrian Hunterb702b102010-02-15 10:03:35 -0800505}
506
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100507/*
Andy Shevchenkoe0c7f992011-05-06 12:14:05 +0300508 * Start clock to the card
509 */
510static void omap_hsmmc_start_clock(struct omap_hsmmc_host *host)
511{
512 OMAP_HSMMC_WRITE(host->base, SYSCTL,
513 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
514}
515
516/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100517 * Stop clock to the card
518 */
Denis Karpov70a33412009-09-22 16:44:59 -0700519static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100520{
521 OMAP_HSMMC_WRITE(host->base, SYSCTL,
522 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
523 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
Masanari Iida7122bbb2012-08-05 23:25:40 +0900524 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stopped\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100525}
526
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700527static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
528 struct mmc_command *cmd)
Adrian Hunterb4175772010-05-26 14:42:06 -0700529{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200530 u32 irq_mask = INT_EN_MASK;
531 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700532
533 if (host->use_dma)
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200534 irq_mask &= ~(BRR_EN | BWR_EN);
Adrian Hunterb4175772010-05-26 14:42:06 -0700535
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700536 /* Disable timeout for erases */
537 if (cmd->opcode == MMC_ERASE)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530538 irq_mask &= ~DTO_EN;
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700539
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200540 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700541 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
542 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200543
544 /* latch pending CIRQ, but don't signal MMC core */
545 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
546 irq_mask |= CIRQ_EN;
Adrian Hunterb4175772010-05-26 14:42:06 -0700547 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200548 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700549}
550
551static void omap_hsmmc_disable_irq(struct omap_hsmmc_host *host)
552{
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200553 u32 irq_mask = 0;
554 unsigned long flags;
555
556 spin_lock_irqsave(&host->irq_lock, flags);
557 /* no transfer running but need to keep cirq if enabled */
558 if (host->flags & HSMMC_SDIO_IRQ_ENABLED)
559 irq_mask |= CIRQ_EN;
560 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
561 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
Adrian Hunterb4175772010-05-26 14:42:06 -0700562 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200563 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700564}
565
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300566/* Calculate divisor for the given clock frequency */
Balaji TKd83b6e02011-12-20 15:12:00 +0530567static u16 calc_divisor(struct omap_hsmmc_host *host, struct mmc_ios *ios)
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300568{
569 u16 dsor = 0;
570
571 if (ios->clock) {
Balaji TKd83b6e02011-12-20 15:12:00 +0530572 dsor = DIV_ROUND_UP(clk_get_rate(host->fclk), ios->clock);
Balaji T Ked164182013-10-21 00:25:21 +0530573 if (dsor > CLKD_MAX)
574 dsor = CLKD_MAX;
Andy Shevchenkoac330f442011-05-10 15:51:54 +0300575 }
576
577 return dsor;
578}
579
Andy Shevchenko5934df22011-05-06 12:14:06 +0300580static void omap_hsmmc_set_clock(struct omap_hsmmc_host *host)
581{
582 struct mmc_ios *ios = &host->mmc->ios;
583 unsigned long regval;
584 unsigned long timeout;
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530585 unsigned long clkdiv;
Andy Shevchenko5934df22011-05-06 12:14:06 +0300586
Venkatraman S8986d312012-08-07 19:10:38 +0530587 dev_vdbg(mmc_dev(host->mmc), "Set clock to %uHz\n", ios->clock);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300588
589 omap_hsmmc_stop_clock(host);
590
591 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
592 regval = regval & ~(CLKD_MASK | DTO_MASK);
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530593 clkdiv = calc_divisor(host, ios);
594 regval = regval | (clkdiv << 6) | (DTO << 16);
Andy Shevchenko5934df22011-05-06 12:14:06 +0300595 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
596 OMAP_HSMMC_WRITE(host->base, SYSCTL,
597 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
598
599 /* Wait till the ICS bit is set */
600 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
601 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
602 && time_before(jiffies, timeout))
603 cpu_relax();
604
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530605 /*
606 * Enable High-Speed Support
607 * Pre-Requisites
608 * - Controller should support High-Speed-Enable Bit
609 * - Controller should not be using DDR Mode
610 * - Controller should advertise that it supports High Speed
611 * in capabilities register
612 * - MMC/SD clock coming out of controller > 25MHz
613 */
Andreas Fenkart326119c2014-11-08 15:33:14 +0100614 if ((mmc_pdata(host)->features & HSMMC_HAS_HSPE_SUPPORT) &&
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900615 (ios->timing != MMC_TIMING_MMC_DDR52) &&
Hebbar, Gururajacd587092012-11-19 21:59:58 +0530616 ((OMAP_HSMMC_READ(host->base, CAPA) & HSS) == HSS)) {
617 regval = OMAP_HSMMC_READ(host->base, HCTL);
618 if (clkdiv && (clk_get_rate(host->fclk)/clkdiv) > 25000000)
619 regval |= HSPE;
620 else
621 regval &= ~HSPE;
622
623 OMAP_HSMMC_WRITE(host->base, HCTL, regval);
624 }
625
Andy Shevchenko5934df22011-05-06 12:14:06 +0300626 omap_hsmmc_start_clock(host);
627}
628
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400629static void omap_hsmmc_set_bus_width(struct omap_hsmmc_host *host)
630{
631 struct mmc_ios *ios = &host->mmc->ios;
632 u32 con;
633
634 con = OMAP_HSMMC_READ(host->base, CON);
Seungwon Jeon5438ad92014-03-14 21:12:27 +0900635 if (ios->timing == MMC_TIMING_MMC_DDR52)
Balaji T K03b5d9242012-04-09 12:08:33 +0530636 con |= DDR; /* configure in DDR mode */
637 else
638 con &= ~DDR;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400639 switch (ios->bus_width) {
640 case MMC_BUS_WIDTH_8:
641 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
642 break;
643 case MMC_BUS_WIDTH_4:
644 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
645 OMAP_HSMMC_WRITE(host->base, HCTL,
646 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
647 break;
648 case MMC_BUS_WIDTH_1:
649 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
650 OMAP_HSMMC_WRITE(host->base, HCTL,
651 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
652 break;
653 }
654}
655
656static void omap_hsmmc_set_bus_mode(struct omap_hsmmc_host *host)
657{
658 struct mmc_ios *ios = &host->mmc->ios;
659 u32 con;
660
661 con = OMAP_HSMMC_READ(host->base, CON);
662 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
663 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
664 else
665 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
666}
667
Denis Karpov11dd62a2009-09-22 16:44:43 -0700668#ifdef CONFIG_PM
669
670/*
671 * Restore the MMC host context, if it was lost as result of a
672 * power state change.
673 */
Denis Karpov70a33412009-09-22 16:44:59 -0700674static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700675{
676 struct mmc_ios *ios = &host->mmc->ios;
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400677 u32 hctl, capa;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700678 unsigned long timeout;
679
Tony Lindgren0a82e062013-10-21 00:25:19 +0530680 if (host->con == OMAP_HSMMC_READ(host->base, CON) &&
681 host->hctl == OMAP_HSMMC_READ(host->base, HCTL) &&
682 host->sysctl == OMAP_HSMMC_READ(host->base, SYSCTL) &&
683 host->capa == OMAP_HSMMC_READ(host->base, CAPA))
684 return 0;
685
686 host->context_loss++;
687
Balaji T Kc2200ef2012-03-07 09:55:30 -0500688 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Denis Karpov11dd62a2009-09-22 16:44:43 -0700689 if (host->power_mode != MMC_POWER_OFF &&
690 (1 << ios->vdd) <= MMC_VDD_23_24)
691 hctl = SDVS18;
692 else
693 hctl = SDVS30;
694 capa = VS30 | VS18;
695 } else {
696 hctl = SDVS18;
697 capa = VS18;
698 }
699
Balaji T K5a52b082014-05-29 10:28:02 +0200700 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
701 hctl |= IWE;
702
Denis Karpov11dd62a2009-09-22 16:44:43 -0700703 OMAP_HSMMC_WRITE(host->base, HCTL,
704 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
705
706 OMAP_HSMMC_WRITE(host->base, CAPA,
707 OMAP_HSMMC_READ(host->base, CAPA) | capa);
708
709 OMAP_HSMMC_WRITE(host->base, HCTL,
710 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
711
712 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
713 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
714 && time_before(jiffies, timeout))
715 ;
716
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +0200717 OMAP_HSMMC_WRITE(host->base, ISE, 0);
718 OMAP_HSMMC_WRITE(host->base, IE, 0);
719 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700720
721 /* Do not initialize card-specific things if the power is off */
722 if (host->power_mode == MMC_POWER_OFF)
723 goto out;
724
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400725 omap_hsmmc_set_bus_width(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700726
Andy Shevchenko5934df22011-05-06 12:14:06 +0300727 omap_hsmmc_set_clock(host);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700728
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -0400729 omap_hsmmc_set_bus_mode(host);
730
Denis Karpov11dd62a2009-09-22 16:44:43 -0700731out:
Tony Lindgren0a82e062013-10-21 00:25:19 +0530732 dev_dbg(mmc_dev(host->mmc), "context is restored: restore count %d\n",
733 host->context_loss);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700734 return 0;
735}
736
737/*
738 * Save the MMC host context (store the number of power state changes so far).
739 */
Denis Karpov70a33412009-09-22 16:44:59 -0700740static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700741{
Tony Lindgren0a82e062013-10-21 00:25:19 +0530742 host->con = OMAP_HSMMC_READ(host->base, CON);
743 host->hctl = OMAP_HSMMC_READ(host->base, HCTL);
744 host->sysctl = OMAP_HSMMC_READ(host->base, SYSCTL);
745 host->capa = OMAP_HSMMC_READ(host->base, CAPA);
Denis Karpov11dd62a2009-09-22 16:44:43 -0700746}
747
748#else
749
Denis Karpov70a33412009-09-22 16:44:59 -0700750static int omap_hsmmc_context_restore(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700751{
752 return 0;
753}
754
Denis Karpov70a33412009-09-22 16:44:59 -0700755static void omap_hsmmc_context_save(struct omap_hsmmc_host *host)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700756{
757}
758
759#endif
760
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100761/*
762 * Send init stream sequence to card
763 * before sending IDLE command
764 */
Denis Karpov70a33412009-09-22 16:44:59 -0700765static void send_init_stream(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100766{
767 int reg = 0;
768 unsigned long timeout;
769
Adrian Hunterb62f6222009-09-22 16:45:01 -0700770 if (host->protect_card)
771 return;
772
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773 disable_irq(host->irq);
Adrian Hunterb4175772010-05-26 14:42:06 -0700774
775 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100776 OMAP_HSMMC_WRITE(host->base, CON,
777 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
778 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
779
780 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Venkatraman Sa7e96872012-11-19 22:00:01 +0530781 while ((reg != CC_EN) && time_before(jiffies, timeout))
782 reg = OMAP_HSMMC_READ(host->base, STAT) & CC_EN;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100783
784 OMAP_HSMMC_WRITE(host->base, CON,
785 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700786
787 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
788 OMAP_HSMMC_READ(host->base, STAT);
789
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100790 enable_irq(host->irq);
791}
792
793static inline
Denis Karpov70a33412009-09-22 16:44:59 -0700794int omap_hsmmc_cover_is_closed(struct omap_hsmmc_host *host)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100795{
796 int r = 1;
797
Andreas Fenkart326119c2014-11-08 15:33:14 +0100798 if (mmc_pdata(host)->get_cover_state)
799 r = mmc_pdata(host)->get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100800 return r;
801}
802
803static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700804omap_hsmmc_show_cover_switch(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100805 char *buf)
806{
807 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700808 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100809
Denis Karpov70a33412009-09-22 16:44:59 -0700810 return sprintf(buf, "%s\n",
811 omap_hsmmc_cover_is_closed(host) ? "closed" : "open");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100812}
813
Denis Karpov70a33412009-09-22 16:44:59 -0700814static DEVICE_ATTR(cover_switch, S_IRUGO, omap_hsmmc_show_cover_switch, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100815
816static ssize_t
Denis Karpov70a33412009-09-22 16:44:59 -0700817omap_hsmmc_show_slot_name(struct device *dev, struct device_attribute *attr,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100818 char *buf)
819{
820 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
Denis Karpov70a33412009-09-22 16:44:59 -0700821 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100822
Andreas Fenkart326119c2014-11-08 15:33:14 +0100823 return sprintf(buf, "%s\n", mmc_pdata(host)->name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100824}
825
Denis Karpov70a33412009-09-22 16:44:59 -0700826static DEVICE_ATTR(slot_name, S_IRUGO, omap_hsmmc_show_slot_name, NULL);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827
828/*
829 * Configure the response type and send the cmd.
830 */
831static void
Denis Karpov70a33412009-09-22 16:44:59 -0700832omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100833 struct mmc_data *data)
834{
835 int cmdreg = 0, resptype = 0, cmdtype = 0;
836
Venkatraman S8986d312012-08-07 19:10:38 +0530837 dev_vdbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100838 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
839 host->cmd = cmd;
840
Adrian Hunter93caf8e692010-08-11 14:17:48 -0700841 omap_hsmmc_enable_irq(host, cmd);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100842
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200843 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100844 if (cmd->flags & MMC_RSP_PRESENT) {
845 if (cmd->flags & MMC_RSP_136)
846 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200847 else if (cmd->flags & MMC_RSP_BUSY) {
848 resptype = 3;
849 host->response_busy = 1;
850 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100851 resptype = 2;
852 }
853
854 /*
855 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
856 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
857 * a val of 0x3, rest 0x0.
858 */
859 if (cmd == host->mrq->stop)
860 cmdtype = 0x3;
861
862 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
863
Balaji T Ka2e77152014-01-21 19:54:42 +0530864 if ((host->flags & AUTO_CMD23) && mmc_op_multi(cmd->opcode) &&
865 host->mrq->sbc) {
866 cmdreg |= ACEN_ACMD23;
867 OMAP_HSMMC_WRITE(host->base, SDMASA, host->mrq->sbc->arg);
868 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100869 if (data) {
870 cmdreg |= DP_SELECT | MSBS | BCE;
871 if (data->flags & MMC_DATA_READ)
872 cmdreg |= DDIR;
873 else
874 cmdreg &= ~(DDIR);
875 }
876
877 if (host->use_dma)
Venkatraman Sa7e96872012-11-19 22:00:01 +0530878 cmdreg |= DMAE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100879
Adrian Hunterb4175772010-05-26 14:42:06 -0700880 host->req_in_progress = 1;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700881
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100882 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
883 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
884}
885
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200886static int
Denis Karpov70a33412009-09-22 16:44:59 -0700887omap_hsmmc_get_dma_dir(struct omap_hsmmc_host *host, struct mmc_data *data)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200888{
889 if (data->flags & MMC_DATA_WRITE)
890 return DMA_TO_DEVICE;
891 else
892 return DMA_FROM_DEVICE;
893}
894
Russell Kingc5c98922012-04-13 12:14:39 +0100895static struct dma_chan *omap_hsmmc_get_dma_chan(struct omap_hsmmc_host *host,
896 struct mmc_data *data)
897{
898 return data->flags & MMC_DATA_WRITE ? host->tx_chan : host->rx_chan;
899}
900
Adrian Hunterb4175772010-05-26 14:42:06 -0700901static void omap_hsmmc_request_done(struct omap_hsmmc_host *host, struct mmc_request *mrq)
902{
903 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530904 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700905
Venkatraman S31463b12012-04-09 12:08:34 +0530906 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700907 host->req_in_progress = 0;
908 dma_ch = host->dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530909 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700910
911 omap_hsmmc_disable_irq(host);
912 /* Do not complete the request if DMA is still in progress */
913 if (mrq->data && host->use_dma && dma_ch != -1)
914 return;
915 host->mrq = NULL;
916 mmc_request_done(host->mmc, mrq);
917}
918
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100919/*
920 * Notify the transfer complete to MMC core
921 */
922static void
Denis Karpov70a33412009-09-22 16:44:59 -0700923omap_hsmmc_xfer_done(struct omap_hsmmc_host *host, struct mmc_data *data)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100924{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200925 if (!data) {
926 struct mmc_request *mrq = host->mrq;
927
Adrian Hunter23050102009-09-22 16:44:57 -0700928 /* TC before CC from CMD6 - don't know why, but it happens */
929 if (host->cmd && host->cmd->opcode == 6 &&
930 host->response_busy) {
931 host->response_busy = 0;
932 return;
933 }
934
Adrian Hunterb4175772010-05-26 14:42:06 -0700935 omap_hsmmc_request_done(host, mrq);
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200936 return;
937 }
938
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100939 host->data = NULL;
940
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100941 if (!data->error)
942 data->bytes_xfered += data->blocks * (data->blksz);
943 else
944 data->bytes_xfered = 0;
945
Balaji T Kbf129e12014-01-21 19:54:42 +0530946 if (data->stop && (data->error || !host->mrq->sbc))
947 omap_hsmmc_start_command(host, data->stop, NULL);
948 else
Adrian Hunterb4175772010-05-26 14:42:06 -0700949 omap_hsmmc_request_done(host, data->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100950}
951
952/*
953 * Notify the core about command completion
954 */
955static void
Denis Karpov70a33412009-09-22 16:44:59 -0700956omap_hsmmc_cmd_done(struct omap_hsmmc_host *host, struct mmc_command *cmd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100957{
Balaji T Kbf129e12014-01-21 19:54:42 +0530958 if (host->mrq->sbc && (host->cmd == host->mrq->sbc) &&
Balaji T Ka2e77152014-01-21 19:54:42 +0530959 !host->mrq->sbc->error && !(host->flags & AUTO_CMD23)) {
Balaji T K2177fa92014-05-09 22:16:52 +0530960 host->cmd = NULL;
Balaji T Kbf129e12014-01-21 19:54:42 +0530961 omap_hsmmc_start_dma_transfer(host);
962 omap_hsmmc_start_command(host, host->mrq->cmd,
963 host->mrq->data);
964 return;
965 }
966
Balaji T K2177fa92014-05-09 22:16:52 +0530967 host->cmd = NULL;
968
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100969 if (cmd->flags & MMC_RSP_PRESENT) {
970 if (cmd->flags & MMC_RSP_136) {
971 /* response type 2 */
972 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
973 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
974 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
975 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
976 } else {
977 /* response types 1, 1b, 3, 4, 5, 6 */
978 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
979 }
980 }
Adrian Hunterb4175772010-05-26 14:42:06 -0700981 if ((host->data == NULL && !host->response_busy) || cmd->error)
Balaji T Kd4b2c372014-01-21 19:54:42 +0530982 omap_hsmmc_request_done(host, host->mrq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100983}
984
985/*
986 * DMA clean up for command errors
987 */
Denis Karpov70a33412009-09-22 16:44:59 -0700988static void omap_hsmmc_dma_cleanup(struct omap_hsmmc_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100989{
Adrian Hunterb4175772010-05-26 14:42:06 -0700990 int dma_ch;
Venkatraman S31463b12012-04-09 12:08:34 +0530991 unsigned long flags;
Adrian Hunterb4175772010-05-26 14:42:06 -0700992
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200993 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100994
Venkatraman S31463b12012-04-09 12:08:34 +0530995 spin_lock_irqsave(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700996 dma_ch = host->dma_ch;
997 host->dma_ch = -1;
Venkatraman S31463b12012-04-09 12:08:34 +0530998 spin_unlock_irqrestore(&host->irq_lock, flags);
Adrian Hunterb4175772010-05-26 14:42:06 -0700999
1000 if (host->use_dma && dma_ch != -1) {
Russell Kingc5c98922012-04-13 12:14:39 +01001001 struct dma_chan *chan = omap_hsmmc_get_dma_chan(host, host->data);
1002
1003 dmaengine_terminate_all(chan);
1004 dma_unmap_sg(chan->device->dev,
1005 host->data->sg, host->data->sg_len,
Denis Karpov70a33412009-09-22 16:44:59 -07001006 omap_hsmmc_get_dma_dir(host, host->data));
Russell Kingc5c98922012-04-13 12:14:39 +01001007
Per Forlin053bf342011-11-07 21:55:11 +05301008 host->data->host_cookie = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001009 }
1010 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001011}
1012
1013/*
1014 * Readable error output
1015 */
1016#ifdef CONFIG_MMC_DEBUG
Adrian Hunter699b9582011-05-06 12:14:01 +03001017static void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host, u32 status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001018{
1019 /* --- means reserved bit without definition at documentation */
Denis Karpov70a33412009-09-22 16:44:59 -07001020 static const char *omap_hsmmc_status_bits[] = {
Adrian Hunter699b9582011-05-06 12:14:01 +03001021 "CC" , "TC" , "BGE", "---", "BWR" , "BRR" , "---" , "---" ,
1022 "CIRQ", "OBI" , "---", "---", "---" , "---" , "---" , "ERRI",
1023 "CTO" , "CCRC", "CEB", "CIE", "DTO" , "DCRC", "DEB" , "---" ,
1024 "ACE" , "---" , "---", "---", "CERR", "BADA", "---" , "---"
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001025 };
1026 char res[256];
1027 char *buf = res;
1028 int len, i;
1029
1030 len = sprintf(buf, "MMC IRQ 0x%x :", status);
1031 buf += len;
1032
Denis Karpov70a33412009-09-22 16:44:59 -07001033 for (i = 0; i < ARRAY_SIZE(omap_hsmmc_status_bits); i++)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001034 if (status & (1 << i)) {
Denis Karpov70a33412009-09-22 16:44:59 -07001035 len = sprintf(buf, " %s", omap_hsmmc_status_bits[i]);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001036 buf += len;
1037 }
1038
Venkatraman S8986d312012-08-07 19:10:38 +05301039 dev_vdbg(mmc_dev(host->mmc), "%s\n", res);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001040}
Adrian Hunter699b9582011-05-06 12:14:01 +03001041#else
1042static inline void omap_hsmmc_dbg_report_irq(struct omap_hsmmc_host *host,
1043 u32 status)
1044{
1045}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001046#endif /* CONFIG_MMC_DEBUG */
1047
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001048/*
1049 * MMC controller internal state machines reset
1050 *
1051 * Used to reset command or data internal state machines, using respectively
1052 * SRC or SRD bit of SYSCTL register
1053 * Can be called from interrupt context
1054 */
Denis Karpov70a33412009-09-22 16:44:59 -07001055static inline void omap_hsmmc_reset_controller_fsm(struct omap_hsmmc_host *host,
1056 unsigned long bit)
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001057{
1058 unsigned long i = 0;
Jianpeng Ma1e881782013-10-21 00:25:20 +05301059 unsigned long limit = MMC_TIMEOUT_US;
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001060
1061 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1062 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
1063
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001064 /*
1065 * OMAP4 ES2 and greater has an updated reset logic.
1066 * Monitor a 0->1 transition first
1067 */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001068 if (mmc_pdata(host)->features & HSMMC_HAS_UPDATED_RESET) {
kishore kadiyalab432b4b2010-11-17 22:35:32 -05001069 while ((!(OMAP_HSMMC_READ(host->base, SYSCTL) & bit))
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001070 && (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301071 udelay(1);
Madhusudhan Chikkature07ad64b2010-10-01 16:35:25 -07001072 }
1073 i = 0;
1074
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001075 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
1076 (i++ < limit))
Jianpeng Ma1e881782013-10-21 00:25:20 +05301077 udelay(1);
Jean Pihet3ebf74b2009-02-06 16:42:51 +01001078
1079 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
1080 dev_err(mmc_dev(host->mmc),
1081 "Timeout waiting on controller reset in %s\n",
1082 __func__);
1083}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001084
Balaji T K25e18972012-11-19 21:59:55 +05301085static void hsmmc_command_incomplete(struct omap_hsmmc_host *host,
1086 int err, int end_cmd)
Venkatraman Sae4bf782012-08-09 20:36:07 +05301087{
Balaji T K25e18972012-11-19 21:59:55 +05301088 if (end_cmd) {
Balaji T K94d4f272012-11-19 21:59:56 +05301089 omap_hsmmc_reset_controller_fsm(host, SRC);
Balaji T K25e18972012-11-19 21:59:55 +05301090 if (host->cmd)
1091 host->cmd->error = err;
1092 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301093
1094 if (host->data) {
1095 omap_hsmmc_reset_controller_fsm(host, SRD);
1096 omap_hsmmc_dma_cleanup(host, err);
Balaji T Kdc7745b2012-11-19 21:59:57 +05301097 } else if (host->mrq && host->mrq->cmd)
1098 host->mrq->cmd->error = err;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301099}
1100
Adrian Hunterb4175772010-05-26 14:42:06 -07001101static void omap_hsmmc_do_irq(struct omap_hsmmc_host *host, int status)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001102{
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001103 struct mmc_data *data;
Adrian Hunterb4175772010-05-26 14:42:06 -07001104 int end_cmd = 0, end_trans = 0;
Balaji T Ka2e77152014-01-21 19:54:42 +05301105 int error = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001106
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001107 data = host->data;
Venkatraman S8986d312012-08-07 19:10:38 +05301108 dev_vdbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001109
Venkatraman Sa7e96872012-11-19 22:00:01 +05301110 if (status & ERR_EN) {
Adrian Hunter699b9582011-05-06 12:14:01 +03001111 omap_hsmmc_dbg_report_irq(host, status);
Adrian Hunter4a694dc2009-01-12 16:13:08 +02001112
Venkatraman Sa7e96872012-11-19 22:00:01 +05301113 if (status & (CTO_EN | CCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301114 end_cmd = 1;
Venkatraman Sa7e96872012-11-19 22:00:01 +05301115 if (status & (CTO_EN | DTO_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301116 hsmmc_command_incomplete(host, -ETIMEDOUT, end_cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301117 else if (status & (CCRC_EN | DCRC_EN))
Balaji T K25e18972012-11-19 21:59:55 +05301118 hsmmc_command_incomplete(host, -EILSEQ, end_cmd);
1119
Balaji T Ka2e77152014-01-21 19:54:42 +05301120 if (status & ACE_EN) {
1121 u32 ac12;
1122 ac12 = OMAP_HSMMC_READ(host->base, AC12);
1123 if (!(ac12 & ACNE) && host->mrq->sbc) {
1124 end_cmd = 1;
1125 if (ac12 & ACTO)
1126 error = -ETIMEDOUT;
1127 else if (ac12 & (ACCE | ACEB | ACIE))
1128 error = -EILSEQ;
1129 host->mrq->sbc->error = error;
1130 hsmmc_command_incomplete(host, error, end_cmd);
1131 }
1132 dev_dbg(mmc_dev(host->mmc), "AC12 err: 0x%x\n", ac12);
1133 }
Venkatraman Sae4bf782012-08-09 20:36:07 +05301134 if (host->data || host->response_busy) {
Balaji T K25e18972012-11-19 21:59:55 +05301135 end_trans = !end_cmd;
Venkatraman Sae4bf782012-08-09 20:36:07 +05301136 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001137 }
1138 }
1139
Francesco Lavra7472bab2013-06-29 08:25:12 +02001140 OMAP_HSMMC_WRITE(host->base, STAT, status);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301141 if (end_cmd || ((status & CC_EN) && host->cmd))
Denis Karpov70a33412009-09-22 16:44:59 -07001142 omap_hsmmc_cmd_done(host, host->cmd);
Venkatraman Sa7e96872012-11-19 22:00:01 +05301143 if ((end_trans || (status & TC_EN)) && host->mrq)
Denis Karpov70a33412009-09-22 16:44:59 -07001144 omap_hsmmc_xfer_done(host, data);
Adrian Hunterb4175772010-05-26 14:42:06 -07001145}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001146
Adrian Hunterb4175772010-05-26 14:42:06 -07001147/*
1148 * MMC controller IRQ handler
1149 */
1150static irqreturn_t omap_hsmmc_irq(int irq, void *dev_id)
1151{
1152 struct omap_hsmmc_host *host = dev_id;
1153 int status;
1154
1155 status = OMAP_HSMMC_READ(host->base, STAT);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001156 while (status & (INT_EN_MASK | CIRQ_EN)) {
1157 if (host->req_in_progress)
1158 omap_hsmmc_do_irq(host, status);
1159
1160 if (status & CIRQ_EN)
1161 mmc_signal_sdio_irq(host->mmc);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301162
Adrian Hunterb4175772010-05-26 14:42:06 -07001163 /* Flush posted write */
1164 status = OMAP_HSMMC_READ(host->base, STAT);
Venkatraman S1f6b9fa2012-08-08 15:44:29 +05301165 }
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001166
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001167 return IRQ_HANDLED;
1168}
1169
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001170static irqreturn_t omap_hsmmc_wake_irq(int irq, void *dev_id)
1171{
1172 struct omap_hsmmc_host *host = dev_id;
1173
1174 /* cirq is level triggered, disable to avoid infinite loop */
1175 spin_lock(&host->irq_lock);
1176 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
1177 disable_irq_nosync(host->wake_irq);
1178 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
1179 }
1180 spin_unlock(&host->irq_lock);
1181 pm_request_resume(host->dev); /* no use counter */
1182
1183 return IRQ_HANDLED;
1184}
1185
Denis Karpov70a33412009-09-22 16:44:59 -07001186static void set_sd_bus_power(struct omap_hsmmc_host *host)
Adrian Huntere13bb302009-03-12 17:08:26 +02001187{
1188 unsigned long i;
1189
1190 OMAP_HSMMC_WRITE(host->base, HCTL,
1191 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
1192 for (i = 0; i < loops_per_jiffy; i++) {
1193 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
1194 break;
1195 cpu_relax();
1196 }
1197}
1198
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001199/*
David Brownelleb250822009-02-17 14:49:01 -08001200 * Switch MMC interface voltage ... only relevant for MMC1.
1201 *
1202 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
1203 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
1204 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001205 */
Denis Karpov70a33412009-09-22 16:44:59 -07001206static int omap_hsmmc_switch_opcond(struct omap_hsmmc_host *host, int vdd)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001207{
1208 u32 reg_val = 0;
1209 int ret;
1210
1211 /* Disable the clocks */
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301212 pm_runtime_put_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301213 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301214 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001215
1216 /* Turn the power off */
Andreas Fenkart326119c2014-11-08 15:33:14 +01001217 ret = mmc_pdata(host)->set_power(host->dev, host->slot_id, 0, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001218
1219 /* Turn the power ON with given VDD 1.8 or 3.0v */
Adrian Hunter2bec0892009-09-22 16:45:02 -07001220 if (!ret)
Andreas Fenkart326119c2014-11-08 15:33:14 +01001221 ret = mmc_pdata(host)->set_power(host->dev, host->slot_id, 1,
1222 vdd);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301223 pm_runtime_get_sync(host->dev);
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05301224 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05301225 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07001226
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001227 if (ret != 0)
1228 goto err;
1229
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001230 OMAP_HSMMC_WRITE(host->base, HCTL,
1231 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
1232 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -08001233
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001234 /*
1235 * If a MMC dual voltage card is detected, the set_ios fn calls
1236 * this fn with VDD bit set for 1.8V. Upon card removal from the
Denis Karpov70a33412009-09-22 16:44:59 -07001237 * slot, omap_hsmmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001238 *
David Brownelleb250822009-02-17 14:49:01 -08001239 * Cope with a bit of slop in the range ... per data sheets:
1240 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
1241 * but recommended values are 1.71V to 1.89V
1242 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
1243 * but recommended values are 2.7V to 3.3V
1244 *
1245 * Board setup code shouldn't permit anything very out-of-range.
1246 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
1247 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001248 */
David Brownelleb250822009-02-17 14:49:01 -08001249 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001250 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -08001251 else
1252 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001253
1254 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +02001255 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001256
1257 return 0;
1258err:
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301259 dev_err(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001260 return ret;
1261}
1262
Adrian Hunterb62f6222009-09-22 16:45:01 -07001263/* Protect the card while the cover is open */
1264static void omap_hsmmc_protect_card(struct omap_hsmmc_host *host)
1265{
Andreas Fenkart326119c2014-11-08 15:33:14 +01001266 if (!mmc_pdata(host)->get_cover_state)
Adrian Hunterb62f6222009-09-22 16:45:01 -07001267 return;
1268
1269 host->reqs_blocked = 0;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001270 if (mmc_pdata(host)->get_cover_state(host->dev, host->slot_id)) {
Adrian Hunterb62f6222009-09-22 16:45:01 -07001271 if (host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301272 dev_info(host->dev, "%s: cover is closed, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001273 "card is now accessible\n",
1274 mmc_hostname(host->mmc));
1275 host->protect_card = 0;
1276 }
1277 } else {
1278 if (!host->protect_card) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301279 dev_info(host->dev, "%s: cover is open, "
Adrian Hunterb62f6222009-09-22 16:45:01 -07001280 "card is now inaccessible\n",
1281 mmc_hostname(host->mmc));
1282 host->protect_card = 1;
1283 }
1284 }
1285}
1286
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001287/*
NeilBrown7efab4f2011-12-30 12:35:13 +11001288 * irq handler to notify the core about card insertion/removal
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001289 */
NeilBrown7efab4f2011-12-30 12:35:13 +11001290static irqreturn_t omap_hsmmc_detect(int irq, void *dev_id)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001291{
NeilBrown7efab4f2011-12-30 12:35:13 +11001292 struct omap_hsmmc_host *host = dev_id;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001293 struct omap_hsmmc_platform_data *pdata = host->pdata;
Adrian Huntera6b22402009-09-22 16:44:45 -07001294 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -08001295
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001296 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -07001297
Andreas Fenkart326119c2014-11-08 15:33:14 +01001298 if (pdata->card_detect)
1299 carddetect = pdata->card_detect(host->dev, host->slot_id);
Adrian Hunterb62f6222009-09-22 16:45:01 -07001300 else {
1301 omap_hsmmc_protect_card(host);
Adrian Huntera6b22402009-09-22 16:44:45 -07001302 carddetect = -ENOSYS;
Adrian Hunterb62f6222009-09-22 16:45:01 -07001303 }
Adrian Huntera6b22402009-09-22 16:44:45 -07001304
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001305 if (carddetect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001306 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
Madhusudhan Chikkaturecdeebad2010-04-06 14:34:49 -07001307 else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001308 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001309 return IRQ_HANDLED;
1310}
1311
Russell Kingc5c98922012-04-13 12:14:39 +01001312static void omap_hsmmc_dma_callback(void *param)
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001313{
Russell Kingc5c98922012-04-13 12:14:39 +01001314 struct omap_hsmmc_host *host = param;
1315 struct dma_chan *chan;
Adrian Hunter770d7432011-05-06 12:14:11 +03001316 struct mmc_data *data;
Russell Kingc5c98922012-04-13 12:14:39 +01001317 int req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001318
Russell Kingc5c98922012-04-13 12:14:39 +01001319 spin_lock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001320 if (host->dma_ch < 0) {
Russell Kingc5c98922012-04-13 12:14:39 +01001321 spin_unlock_irq(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001322 return;
Adrian Hunterb4175772010-05-26 14:42:06 -07001323 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001324
Adrian Hunter770d7432011-05-06 12:14:11 +03001325 data = host->mrq->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001326 chan = omap_hsmmc_get_dma_chan(host, data);
Per Forlin9782aff2011-07-01 18:55:23 +02001327 if (!data->host_cookie)
Russell Kingc5c98922012-04-13 12:14:39 +01001328 dma_unmap_sg(chan->device->dev,
1329 data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001330 omap_hsmmc_get_dma_dir(host, data));
Adrian Hunterb4175772010-05-26 14:42:06 -07001331
1332 req_in_progress = host->req_in_progress;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001333 host->dma_ch = -1;
Russell Kingc5c98922012-04-13 12:14:39 +01001334 spin_unlock_irq(&host->irq_lock);
Adrian Hunterb4175772010-05-26 14:42:06 -07001335
1336 /* If DMA has finished after TC, complete the request */
1337 if (!req_in_progress) {
1338 struct mmc_request *mrq = host->mrq;
1339
1340 host->mrq = NULL;
1341 mmc_request_done(host->mmc, mrq);
1342 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001343}
1344
Per Forlin9782aff2011-07-01 18:55:23 +02001345static int omap_hsmmc_pre_dma_transfer(struct omap_hsmmc_host *host,
1346 struct mmc_data *data,
Russell Kingc5c98922012-04-13 12:14:39 +01001347 struct omap_hsmmc_next *next,
Russell King26b88522012-04-13 12:27:37 +01001348 struct dma_chan *chan)
Per Forlin9782aff2011-07-01 18:55:23 +02001349{
1350 int dma_len;
1351
1352 if (!next && data->host_cookie &&
1353 data->host_cookie != host->next_data.cookie) {
Rajendra Nayak2cecdf02012-02-23 17:02:20 +05301354 dev_warn(host->dev, "[%s] invalid cookie: data->host_cookie %d"
Per Forlin9782aff2011-07-01 18:55:23 +02001355 " host->next_data.cookie %d\n",
1356 __func__, data->host_cookie, host->next_data.cookie);
1357 data->host_cookie = 0;
1358 }
1359
1360 /* Check if next job is already prepared */
Dan Carpenterb38313d2014-01-30 15:15:18 +03001361 if (next || data->host_cookie != host->next_data.cookie) {
Russell King26b88522012-04-13 12:27:37 +01001362 dma_len = dma_map_sg(chan->device->dev, data->sg, data->sg_len,
Per Forlin9782aff2011-07-01 18:55:23 +02001363 omap_hsmmc_get_dma_dir(host, data));
1364
1365 } else {
1366 dma_len = host->next_data.dma_len;
1367 host->next_data.dma_len = 0;
1368 }
1369
1370
1371 if (dma_len == 0)
1372 return -EINVAL;
1373
1374 if (next) {
1375 next->dma_len = dma_len;
1376 data->host_cookie = ++next->cookie < 0 ? 1 : next->cookie;
1377 } else
1378 host->dma_len = dma_len;
1379
1380 return 0;
1381}
1382
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001383/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001384 * Routine to configure and start DMA for the MMC card
1385 */
Balaji T K9d025332014-01-21 19:54:42 +05301386static int omap_hsmmc_setup_dma_transfer(struct omap_hsmmc_host *host,
Denis Karpov70a33412009-09-22 16:44:59 -07001387 struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001388{
Russell King26b88522012-04-13 12:27:37 +01001389 struct dma_slave_config cfg;
1390 struct dma_async_tx_descriptor *tx;
1391 int ret = 0, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001392 struct mmc_data *data = req->data;
Russell Kingc5c98922012-04-13 12:14:39 +01001393 struct dma_chan *chan;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001394
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001395 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001396 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001397 struct scatterlist *sgl;
1398
1399 sgl = data->sg + i;
1400 if (sgl->length % data->blksz)
1401 return -EINVAL;
1402 }
1403 if ((data->blksz % 4) != 0)
1404 /* REVISIT: The MMC buffer increments only when MSB is written.
1405 * Return error for blksz which is non multiple of four.
1406 */
1407 return -EINVAL;
1408
Adrian Hunterb4175772010-05-26 14:42:06 -07001409 BUG_ON(host->dma_ch != -1);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001410
Russell Kingc5c98922012-04-13 12:14:39 +01001411 chan = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001412
Russell King26b88522012-04-13 12:27:37 +01001413 cfg.src_addr = host->mapbase + OMAP_HSMMC_DATA;
1414 cfg.dst_addr = host->mapbase + OMAP_HSMMC_DATA;
1415 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1416 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
1417 cfg.src_maxburst = data->blksz / 4;
1418 cfg.dst_maxburst = data->blksz / 4;
Russell Kingc5c98922012-04-13 12:14:39 +01001419
Russell King26b88522012-04-13 12:27:37 +01001420 ret = dmaengine_slave_config(chan, &cfg);
Per Forlin9782aff2011-07-01 18:55:23 +02001421 if (ret)
1422 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001423
Russell King26b88522012-04-13 12:27:37 +01001424 ret = omap_hsmmc_pre_dma_transfer(host, data, NULL, chan);
1425 if (ret)
1426 return ret;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001427
Russell King26b88522012-04-13 12:27:37 +01001428 tx = dmaengine_prep_slave_sg(chan, data->sg, data->sg_len,
1429 data->flags & MMC_DATA_WRITE ? DMA_MEM_TO_DEV : DMA_DEV_TO_MEM,
1430 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1431 if (!tx) {
1432 dev_err(mmc_dev(host->mmc), "prep_slave_sg() failed\n");
1433 /* FIXME: cleanup */
1434 return -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001435 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001436
Russell King26b88522012-04-13 12:27:37 +01001437 tx->callback = omap_hsmmc_dma_callback;
1438 tx->callback_param = host;
1439
1440 /* Does not fail */
1441 dmaengine_submit(tx);
1442
1443 host->dma_ch = 1;
1444
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001445 return 0;
1446}
1447
Denis Karpov70a33412009-09-22 16:44:59 -07001448static void set_data_timeout(struct omap_hsmmc_host *host,
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001449 unsigned int timeout_ns,
1450 unsigned int timeout_clks)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001451{
1452 unsigned int timeout, cycle_ns;
1453 uint32_t reg, clkd, dto = 0;
1454
1455 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
1456 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
1457 if (clkd == 0)
1458 clkd = 1;
1459
Balaji T K6e3076c2014-01-21 19:54:42 +05301460 cycle_ns = 1000000000 / (host->clk_rate / clkd);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001461 timeout = timeout_ns / cycle_ns;
1462 timeout += timeout_clks;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001463 if (timeout) {
1464 while ((timeout & 0x80000000) == 0) {
1465 dto += 1;
1466 timeout <<= 1;
1467 }
1468 dto = 31 - dto;
1469 timeout <<= 1;
1470 if (timeout && dto)
1471 dto += 1;
1472 if (dto >= 13)
1473 dto -= 13;
1474 else
1475 dto = 0;
1476 if (dto > 14)
1477 dto = 14;
1478 }
1479
1480 reg &= ~DTO_MASK;
1481 reg |= dto << DTO_SHIFT;
1482 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
1483}
1484
Balaji T K9d025332014-01-21 19:54:42 +05301485static void omap_hsmmc_start_dma_transfer(struct omap_hsmmc_host *host)
1486{
1487 struct mmc_request *req = host->mrq;
1488 struct dma_chan *chan;
1489
1490 if (!req->data)
1491 return;
1492 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1493 | (req->data->blocks << 16));
1494 set_data_timeout(host, req->data->timeout_ns,
1495 req->data->timeout_clks);
1496 chan = omap_hsmmc_get_dma_chan(host, req->data);
1497 dma_async_issue_pending(chan);
1498}
1499
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001500/*
1501 * Configure block length for MMC/SD cards and initiate the transfer.
1502 */
1503static int
Denis Karpov70a33412009-09-22 16:44:59 -07001504omap_hsmmc_prepare_data(struct omap_hsmmc_host *host, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001505{
1506 int ret;
1507 host->data = req->data;
1508
1509 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001510 OMAP_HSMMC_WRITE(host->base, BLK, 0);
Adrian Huntere2bf08d2009-09-22 16:45:03 -07001511 /*
1512 * Set an arbitrary 100ms data timeout for commands with
1513 * busy signal.
1514 */
1515 if (req->cmd->flags & MMC_RSP_BUSY)
1516 set_data_timeout(host, 100000000U, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001517 return 0;
1518 }
1519
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001520 if (host->use_dma) {
Balaji T K9d025332014-01-21 19:54:42 +05301521 ret = omap_hsmmc_setup_dma_transfer(host, req);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001522 if (ret != 0) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05301523 dev_err(mmc_dev(host->mmc), "MMC start dma failure\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001524 return ret;
1525 }
1526 }
1527 return 0;
1528}
1529
Per Forlin9782aff2011-07-01 18:55:23 +02001530static void omap_hsmmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1531 int err)
1532{
1533 struct omap_hsmmc_host *host = mmc_priv(mmc);
1534 struct mmc_data *data = mrq->data;
1535
Russell King26b88522012-04-13 12:27:37 +01001536 if (host->use_dma && data->host_cookie) {
Russell Kingc5c98922012-04-13 12:14:39 +01001537 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, data);
Russell Kingc5c98922012-04-13 12:14:39 +01001538
Russell King26b88522012-04-13 12:27:37 +01001539 dma_unmap_sg(c->device->dev, data->sg, data->sg_len,
1540 omap_hsmmc_get_dma_dir(host, data));
Per Forlin9782aff2011-07-01 18:55:23 +02001541 data->host_cookie = 0;
1542 }
1543}
1544
1545static void omap_hsmmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
1546 bool is_first_req)
1547{
1548 struct omap_hsmmc_host *host = mmc_priv(mmc);
1549
1550 if (mrq->data->host_cookie) {
1551 mrq->data->host_cookie = 0;
1552 return ;
1553 }
1554
Russell Kingc5c98922012-04-13 12:14:39 +01001555 if (host->use_dma) {
1556 struct dma_chan *c = omap_hsmmc_get_dma_chan(host, mrq->data);
Russell Kingc5c98922012-04-13 12:14:39 +01001557
Per Forlin9782aff2011-07-01 18:55:23 +02001558 if (omap_hsmmc_pre_dma_transfer(host, mrq->data,
Russell King26b88522012-04-13 12:27:37 +01001559 &host->next_data, c))
Per Forlin9782aff2011-07-01 18:55:23 +02001560 mrq->data->host_cookie = 0;
Russell Kingc5c98922012-04-13 12:14:39 +01001561 }
Per Forlin9782aff2011-07-01 18:55:23 +02001562}
1563
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001564/*
1565 * Request function. for read/write operation
1566 */
Denis Karpov70a33412009-09-22 16:44:59 -07001567static void omap_hsmmc_request(struct mmc_host *mmc, struct mmc_request *req)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001568{
Denis Karpov70a33412009-09-22 16:44:59 -07001569 struct omap_hsmmc_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001570 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001571
Adrian Hunterb4175772010-05-26 14:42:06 -07001572 BUG_ON(host->req_in_progress);
1573 BUG_ON(host->dma_ch != -1);
1574 if (host->protect_card) {
1575 if (host->reqs_blocked < 3) {
1576 /*
1577 * Ensure the controller is left in a consistent
1578 * state by resetting the command and data state
1579 * machines.
1580 */
1581 omap_hsmmc_reset_controller_fsm(host, SRD);
1582 omap_hsmmc_reset_controller_fsm(host, SRC);
1583 host->reqs_blocked += 1;
1584 }
1585 req->cmd->error = -EBADF;
1586 if (req->data)
1587 req->data->error = -EBADF;
1588 req->cmd->retries = 0;
1589 mmc_request_done(mmc, req);
1590 return;
1591 } else if (host->reqs_blocked)
1592 host->reqs_blocked = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001593 WARN_ON(host->mrq != NULL);
1594 host->mrq = req;
Balaji T K6e3076c2014-01-21 19:54:42 +05301595 host->clk_rate = clk_get_rate(host->fclk);
Denis Karpov70a33412009-09-22 16:44:59 -07001596 err = omap_hsmmc_prepare_data(host, req);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001597 if (err) {
1598 req->cmd->error = err;
1599 if (req->data)
1600 req->data->error = err;
1601 host->mrq = NULL;
1602 mmc_request_done(mmc, req);
1603 return;
1604 }
Balaji T Ka2e77152014-01-21 19:54:42 +05301605 if (req->sbc && !(host->flags & AUTO_CMD23)) {
Balaji T Kbf129e12014-01-21 19:54:42 +05301606 omap_hsmmc_start_command(host, req->sbc, NULL);
1607 return;
1608 }
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001609
Balaji T K9d025332014-01-21 19:54:42 +05301610 omap_hsmmc_start_dma_transfer(host);
Denis Karpov70a33412009-09-22 16:44:59 -07001611 omap_hsmmc_start_command(host, req->cmd, req->data);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001612}
1613
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001614/* Routine to configure clock values. Exposed API to core */
Denis Karpov70a33412009-09-22 16:44:59 -07001615static void omap_hsmmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001616{
Denis Karpov70a33412009-09-22 16:44:59 -07001617 struct omap_hsmmc_host *host = mmc_priv(mmc);
Adrian Huntera3621462009-09-22 16:44:42 -07001618 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001619
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301620 pm_runtime_get_sync(host->dev);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001621
Adrian Huntera3621462009-09-22 16:44:42 -07001622 if (ios->power_mode != host->power_mode) {
1623 switch (ios->power_mode) {
1624 case MMC_POWER_OFF:
Andreas Fenkart326119c2014-11-08 15:33:14 +01001625 mmc_pdata(host)->set_power(host->dev, host->slot_id,
1626 0, 0);
Adrian Huntera3621462009-09-22 16:44:42 -07001627 break;
1628 case MMC_POWER_UP:
Andreas Fenkart326119c2014-11-08 15:33:14 +01001629 mmc_pdata(host)->set_power(host->dev, host->slot_id,
1630 1, ios->vdd);
Adrian Huntera3621462009-09-22 16:44:42 -07001631 break;
1632 case MMC_POWER_ON:
1633 do_send_init_stream = 1;
1634 break;
1635 }
1636 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001637 }
1638
Denis Karpovdd498ef2009-09-22 16:44:49 -07001639 /* FIXME: set registers based only on changes to ios */
1640
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001641 omap_hsmmc_set_bus_width(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001642
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301643 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
David Brownelleb250822009-02-17 14:49:01 -08001644 /* Only MMC1 can interface at 3V without some flavor
1645 * of external transceiver; but they all handle 1.8V.
1646 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001647 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
Balaji T K2cf171c2014-02-19 20:26:40 +05301648 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001649 /*
1650 * The mmc_select_voltage fn of the core does
1651 * not seem to set the power_mode to
1652 * MMC_POWER_UP upon recalculating the voltage.
1653 * vdd 1.8v.
1654 */
Denis Karpov70a33412009-09-22 16:44:59 -07001655 if (omap_hsmmc_switch_opcond(host, ios->vdd) != 0)
1656 dev_dbg(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001657 "Switch operation failed\n");
1658 }
1659 }
1660
Andy Shevchenko5934df22011-05-06 12:14:06 +03001661 omap_hsmmc_set_clock(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001662
Adrian Huntera3621462009-09-22 16:44:42 -07001663 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001664 send_init_stream(host);
1665
Andy Shevchenko3796fb8a2011-07-13 11:31:15 -04001666 omap_hsmmc_set_bus_mode(host);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001667
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301668 pm_runtime_put_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001669}
1670
1671static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1672{
Denis Karpov70a33412009-09-22 16:44:59 -07001673 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001674
Andreas Fenkart326119c2014-11-08 15:33:14 +01001675 if (!mmc_pdata(host)->card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001676 return -ENOSYS;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001677 return mmc_pdata(host)->card_detect(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001678}
1679
1680static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1681{
Denis Karpov70a33412009-09-22 16:44:59 -07001682 struct omap_hsmmc_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001683
Andreas Fenkart326119c2014-11-08 15:33:14 +01001684 if (!mmc_pdata(host)->get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001685 return -ENOSYS;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001686 return mmc_pdata(host)->get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001687}
1688
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001689static void omap_hsmmc_init_card(struct mmc_host *mmc, struct mmc_card *card)
1690{
1691 struct omap_hsmmc_host *host = mmc_priv(mmc);
1692
Andreas Fenkart326119c2014-11-08 15:33:14 +01001693 if (mmc_pdata(host)->init_card)
1694 mmc_pdata(host)->init_card(card);
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001695}
1696
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001697static void omap_hsmmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
1698{
1699 struct omap_hsmmc_host *host = mmc_priv(mmc);
Balaji T K5a52b082014-05-29 10:28:02 +02001700 u32 irq_mask, con;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001701 unsigned long flags;
1702
1703 spin_lock_irqsave(&host->irq_lock, flags);
1704
Balaji T K5a52b082014-05-29 10:28:02 +02001705 con = OMAP_HSMMC_READ(host->base, CON);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001706 irq_mask = OMAP_HSMMC_READ(host->base, ISE);
1707 if (enable) {
1708 host->flags |= HSMMC_SDIO_IRQ_ENABLED;
1709 irq_mask |= CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001710 con |= CTPL | CLKEXTFREE;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001711 } else {
1712 host->flags &= ~HSMMC_SDIO_IRQ_ENABLED;
1713 irq_mask &= ~CIRQ_EN;
Balaji T K5a52b082014-05-29 10:28:02 +02001714 con &= ~(CTPL | CLKEXTFREE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001715 }
Balaji T K5a52b082014-05-29 10:28:02 +02001716 OMAP_HSMMC_WRITE(host->base, CON, con);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001717 OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
1718
1719 /*
1720 * if enable, piggy back detection on current request
1721 * but always disable immediately
1722 */
1723 if (!host->req_in_progress || !enable)
1724 OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
1725
1726 /* flush posted write */
1727 OMAP_HSMMC_READ(host->base, IE);
1728
1729 spin_unlock_irqrestore(&host->irq_lock, flags);
1730}
1731
1732static int omap_hsmmc_configure_wake_irq(struct omap_hsmmc_host *host)
1733{
1734 struct mmc_host *mmc = host->mmc;
1735 int ret;
1736
1737 /*
1738 * For omaps with wake-up path, wakeirq will be irq from pinctrl and
1739 * for other omaps, wakeirq will be from GPIO (dat line remuxed to
1740 * gpio). wakeirq is needed to detect sdio irq in runtime suspend state
1741 * with functional clock disabled.
1742 */
1743 if (!host->dev->of_node || !host->wake_irq)
1744 return -ENODEV;
1745
1746 /* Prevent auto-enabling of IRQ */
1747 irq_set_status_flags(host->wake_irq, IRQ_NOAUTOEN);
1748 ret = devm_request_irq(host->dev, host->wake_irq, omap_hsmmc_wake_irq,
1749 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
1750 mmc_hostname(mmc), host);
1751 if (ret) {
1752 dev_err(mmc_dev(host->mmc), "Unable to request wake IRQ\n");
1753 goto err;
1754 }
1755
1756 /*
1757 * Some omaps don't have wake-up path from deeper idle states
1758 * and need to remux SDIO DAT1 to GPIO for wake-up from idle.
1759 */
1760 if (host->pdata->controller_flags & OMAP_HSMMC_SWAKEUP_MISSING) {
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001761 struct pinctrl *p = devm_pinctrl_get(host->dev);
1762 if (!p) {
1763 ret = -ENODEV;
1764 goto err_free_irq;
1765 }
1766 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_DEFAULT))) {
1767 dev_info(host->dev, "missing default pinctrl state\n");
1768 devm_pinctrl_put(p);
1769 ret = -EINVAL;
1770 goto err_free_irq;
1771 }
1772
1773 if (IS_ERR(pinctrl_lookup_state(p, PINCTRL_STATE_IDLE))) {
1774 dev_info(host->dev, "missing idle pinctrl state\n");
1775 devm_pinctrl_put(p);
1776 ret = -EINVAL;
1777 goto err_free_irq;
1778 }
1779 devm_pinctrl_put(p);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001780 }
1781
Balaji T K5a52b082014-05-29 10:28:02 +02001782 OMAP_HSMMC_WRITE(host->base, HCTL,
1783 OMAP_HSMMC_READ(host->base, HCTL) | IWE);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001784 return 0;
1785
Andreas Fenkart455e5cd2014-05-29 10:28:05 +02001786err_free_irq:
1787 devm_free_irq(host->dev, host->wake_irq, host);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001788err:
1789 dev_warn(host->dev, "no SDIO IRQ support, falling back to polling\n");
1790 host->wake_irq = 0;
1791 return ret;
1792}
1793
Denis Karpov70a33412009-09-22 16:44:59 -07001794static void omap_hsmmc_conf_bus_power(struct omap_hsmmc_host *host)
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001795{
1796 u32 hctl, capa, value;
1797
1798 /* Only MMC1 supports 3.0V */
Kishore Kadiyala4621d5f2011-02-28 20:48:04 +05301799 if (host->pdata->controller_flags & OMAP_HSMMC_SUPPORTS_DUAL_VOLT) {
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001800 hctl = SDVS30;
1801 capa = VS30 | VS18;
1802 } else {
1803 hctl = SDVS18;
1804 capa = VS18;
1805 }
1806
1807 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1808 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1809
1810 value = OMAP_HSMMC_READ(host->base, CAPA);
1811 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1812
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001813 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001814 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001815}
1816
Denis Karpov70a33412009-09-22 16:44:59 -07001817static int omap_hsmmc_enable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001818{
Denis Karpov70a33412009-09-22 16:44:59 -07001819 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001820
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301821 pm_runtime_get_sync(host->dev);
1822
Denis Karpovdd498ef2009-09-22 16:44:49 -07001823 return 0;
1824}
1825
Adrian Hunter907d2e72012-02-29 09:17:21 +02001826static int omap_hsmmc_disable_fclk(struct mmc_host *mmc)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001827{
Denis Karpov70a33412009-09-22 16:44:59 -07001828 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001829
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301830 pm_runtime_mark_last_busy(host->dev);
1831 pm_runtime_put_autosuspend(host->dev);
1832
Denis Karpovdd498ef2009-09-22 16:44:49 -07001833 return 0;
1834}
1835
Kuninori Morimotoafd8c292014-09-08 23:44:51 -07001836static int omap_hsmmc_multi_io_quirk(struct mmc_card *card,
1837 unsigned int direction, int blk_size)
1838{
1839 /* This controller can't do multiblock reads due to hw bugs */
1840 if (direction == MMC_DATA_READ)
1841 return 1;
1842
1843 return blk_size;
1844}
1845
1846static struct mmc_host_ops omap_hsmmc_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001847 .enable = omap_hsmmc_enable_fclk,
1848 .disable = omap_hsmmc_disable_fclk,
Per Forlin9782aff2011-07-01 18:55:23 +02001849 .post_req = omap_hsmmc_post_req,
1850 .pre_req = omap_hsmmc_pre_req,
Denis Karpov70a33412009-09-22 16:44:59 -07001851 .request = omap_hsmmc_request,
1852 .set_ios = omap_hsmmc_set_ios,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001853 .get_cd = omap_hsmmc_get_cd,
1854 .get_ro = omap_hsmmc_get_ro,
Grazvydas Ignotas48168582010-08-10 18:01:52 -07001855 .init_card = omap_hsmmc_init_card,
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001856 .enable_sdio_irq = omap_hsmmc_enable_sdio_irq,
Denis Karpovdd498ef2009-09-22 16:44:49 -07001857};
1858
Denis Karpovd900f712009-09-22 16:44:38 -07001859#ifdef CONFIG_DEBUG_FS
1860
Denis Karpov70a33412009-09-22 16:44:59 -07001861static int omap_hsmmc_regs_show(struct seq_file *s, void *data)
Denis Karpovd900f712009-09-22 16:44:38 -07001862{
1863 struct mmc_host *mmc = s->private;
Denis Karpov70a33412009-09-22 16:44:59 -07001864 struct omap_hsmmc_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001865
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001866 seq_printf(s, "mmc%d:\n", mmc->index);
1867 seq_printf(s, "sdio irq mode\t%s\n",
1868 (mmc->caps & MMC_CAP_SDIO_IRQ) ? "interrupt" : "polling");
1869
1870 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1871 seq_printf(s, "sdio irq \t%s\n",
1872 (host->flags & HSMMC_SDIO_IRQ_ENABLED) ? "enabled"
1873 : "disabled");
1874 }
1875 seq_printf(s, "ctx_loss:\t%d\n", host->context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001876
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301877 pm_runtime_get_sync(host->dev);
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001878 seq_puts(s, "\nregs:\n");
Denis Karpovd900f712009-09-22 16:44:38 -07001879 seq_printf(s, "CON:\t\t0x%08x\n",
1880 OMAP_HSMMC_READ(host->base, CON));
Andreas Fenkartbb0635f2014-05-29 10:28:01 +02001881 seq_printf(s, "PSTATE:\t\t0x%08x\n",
1882 OMAP_HSMMC_READ(host->base, PSTATE));
Denis Karpovd900f712009-09-22 16:44:38 -07001883 seq_printf(s, "HCTL:\t\t0x%08x\n",
1884 OMAP_HSMMC_READ(host->base, HCTL));
1885 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1886 OMAP_HSMMC_READ(host->base, SYSCTL));
1887 seq_printf(s, "IE:\t\t0x%08x\n",
1888 OMAP_HSMMC_READ(host->base, IE));
1889 seq_printf(s, "ISE:\t\t0x%08x\n",
1890 OMAP_HSMMC_READ(host->base, ISE));
1891 seq_printf(s, "CAPA:\t\t0x%08x\n",
1892 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001893
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05301894 pm_runtime_mark_last_busy(host->dev);
1895 pm_runtime_put_autosuspend(host->dev);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001896
Denis Karpovd900f712009-09-22 16:44:38 -07001897 return 0;
1898}
1899
Denis Karpov70a33412009-09-22 16:44:59 -07001900static int omap_hsmmc_regs_open(struct inode *inode, struct file *file)
Denis Karpovd900f712009-09-22 16:44:38 -07001901{
Denis Karpov70a33412009-09-22 16:44:59 -07001902 return single_open(file, omap_hsmmc_regs_show, inode->i_private);
Denis Karpovd900f712009-09-22 16:44:38 -07001903}
1904
1905static const struct file_operations mmc_regs_fops = {
Denis Karpov70a33412009-09-22 16:44:59 -07001906 .open = omap_hsmmc_regs_open,
Denis Karpovd900f712009-09-22 16:44:38 -07001907 .read = seq_read,
1908 .llseek = seq_lseek,
1909 .release = single_release,
1910};
1911
Denis Karpov70a33412009-09-22 16:44:59 -07001912static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001913{
1914 if (mmc->debugfs_root)
1915 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1916 mmc, &mmc_regs_fops);
1917}
1918
1919#else
1920
Denis Karpov70a33412009-09-22 16:44:59 -07001921static void omap_hsmmc_debugfs(struct mmc_host *mmc)
Denis Karpovd900f712009-09-22 16:44:38 -07001922{
1923}
1924
1925#endif
1926
Rajendra Nayak46856a62012-03-12 20:32:37 +05301927#ifdef CONFIG_OF
Nishanth Menon59445b12014-02-13 23:45:48 -06001928static const struct omap_mmc_of_data omap3_pre_es3_mmc_of_data = {
1929 /* See 35xx errata 2.1.1.128 in SPRZ278F */
1930 .controller_flags = OMAP_HSMMC_BROKEN_MULTIBLOCK_READ,
1931};
1932
1933static const struct omap_mmc_of_data omap4_mmc_of_data = {
1934 .reg_offset = 0x100,
1935};
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001936static const struct omap_mmc_of_data am33xx_mmc_of_data = {
1937 .reg_offset = 0x100,
1938 .controller_flags = OMAP_HSMMC_SWAKEUP_MISSING,
1939};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301940
1941static const struct of_device_id omap_mmc_of_match[] = {
1942 {
1943 .compatible = "ti,omap2-hsmmc",
1944 },
1945 {
Nishanth Menon59445b12014-02-13 23:45:48 -06001946 .compatible = "ti,omap3-pre-es3-hsmmc",
1947 .data = &omap3_pre_es3_mmc_of_data,
1948 },
1949 {
Rajendra Nayak46856a62012-03-12 20:32:37 +05301950 .compatible = "ti,omap3-hsmmc",
1951 },
1952 {
1953 .compatible = "ti,omap4-hsmmc",
Nishanth Menon59445b12014-02-13 23:45:48 -06001954 .data = &omap4_mmc_of_data,
Rajendra Nayak46856a62012-03-12 20:32:37 +05301955 },
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02001956 {
1957 .compatible = "ti,am33xx-hsmmc",
1958 .data = &am33xx_mmc_of_data,
1959 },
Rajendra Nayak46856a62012-03-12 20:32:37 +05301960 {},
Chris Ballb6d085f2012-04-10 09:57:36 -04001961};
Rajendra Nayak46856a62012-03-12 20:32:37 +05301962MODULE_DEVICE_TABLE(of, omap_mmc_of_match);
1963
Andreas Fenkart551434382014-11-08 15:33:09 +01001964static struct omap_hsmmc_platform_data *of_get_hsmmc_pdata(struct device *dev)
Rajendra Nayak46856a62012-03-12 20:32:37 +05301965{
Andreas Fenkart551434382014-11-08 15:33:09 +01001966 struct omap_hsmmc_platform_data *pdata;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301967 struct device_node *np = dev->of_node;
Daniel Mackd8714e82012-10-15 21:35:06 +05301968 u32 bus_width, max_freq;
Jan Luebbedc642c22013-01-30 10:07:17 +01001969 int cd_gpio, wp_gpio;
1970
1971 cd_gpio = of_get_named_gpio(np, "cd-gpios", 0);
1972 wp_gpio = of_get_named_gpio(np, "wp-gpios", 0);
1973 if (cd_gpio == -EPROBE_DEFER || wp_gpio == -EPROBE_DEFER)
1974 return ERR_PTR(-EPROBE_DEFER);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301975
1976 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
1977 if (!pdata)
Balaji T K19df45b2014-02-28 19:08:18 +05301978 return ERR_PTR(-ENOMEM); /* out of memory */
Rajendra Nayak46856a62012-03-12 20:32:37 +05301979
1980 if (of_find_property(np, "ti,dual-volt", NULL))
1981 pdata->controller_flags |= OMAP_HSMMC_SUPPORTS_DUAL_VOLT;
1982
1983 /* This driver only supports 1 slot */
1984 pdata->nr_slots = 1;
Andreas Fenkart326119c2014-11-08 15:33:14 +01001985 pdata->switch_pin = cd_gpio;
1986 pdata->gpio_wp = wp_gpio;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301987
1988 if (of_find_property(np, "ti,non-removable", NULL)) {
Andreas Fenkart326119c2014-11-08 15:33:14 +01001989 pdata->nonremovable = true;
1990 pdata->no_regulator_off_init = true;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301991 }
Arnd Bergmann7f217792012-05-13 00:14:24 -04001992 of_property_read_u32(np, "bus-width", &bus_width);
Rajendra Nayak46856a62012-03-12 20:32:37 +05301993 if (bus_width == 4)
Andreas Fenkart326119c2014-11-08 15:33:14 +01001994 pdata->caps |= MMC_CAP_4_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301995 else if (bus_width == 8)
Andreas Fenkart326119c2014-11-08 15:33:14 +01001996 pdata->caps |= MMC_CAP_8_BIT_DATA;
Rajendra Nayak46856a62012-03-12 20:32:37 +05301997
1998 if (of_find_property(np, "ti,needs-special-reset", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01001999 pdata->features |= HSMMC_HAS_UPDATED_RESET;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302000
Daniel Mackd8714e82012-10-15 21:35:06 +05302001 if (!of_property_read_u32(np, "max-frequency", &max_freq))
2002 pdata->max_freq = max_freq;
2003
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302004 if (of_find_property(np, "ti,needs-special-hs-handling", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002005 pdata->features |= HSMMC_HAS_HSPE_SUPPORT;
Hebbar, Gururajacd587092012-11-19 21:59:58 +05302006
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002007 if (of_find_property(np, "keep-power-in-suspend", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002008 pdata->pm_caps |= MMC_PM_KEEP_POWER;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002009
2010 if (of_find_property(np, "enable-sdio-wakeup", NULL))
Andreas Fenkart326119c2014-11-08 15:33:14 +01002011 pdata->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
Daniel Mackc9ae64d2014-02-17 12:36:33 +01002012
Rajendra Nayak46856a62012-03-12 20:32:37 +05302013 return pdata;
2014}
2015#else
Andreas Fenkart551434382014-11-08 15:33:09 +01002016static inline struct omap_hsmmc_platform_data
Rajendra Nayak46856a62012-03-12 20:32:37 +05302017 *of_get_hsmmc_pdata(struct device *dev)
2018{
Balaji T K19df45b2014-02-28 19:08:18 +05302019 return ERR_PTR(-EINVAL);
Rajendra Nayak46856a62012-03-12 20:32:37 +05302020}
2021#endif
2022
Bill Pembertonc3be1ef2012-11-19 13:23:06 -05002023static int omap_hsmmc_probe(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002024{
Andreas Fenkart551434382014-11-08 15:33:09 +01002025 struct omap_hsmmc_platform_data *pdata = pdev->dev.platform_data;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002026 struct mmc_host *mmc;
Denis Karpov70a33412009-09-22 16:44:59 -07002027 struct omap_hsmmc_host *host = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002028 struct resource *res;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002029 int ret, irq;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302030 const struct of_device_id *match;
Russell King26b88522012-04-13 12:27:37 +01002031 dma_cap_mask_t mask;
2032 unsigned tx_req, rx_req;
Nishanth Menon59445b12014-02-13 23:45:48 -06002033 const struct omap_mmc_of_data *data;
Balaji T K77fae212014-05-09 22:16:51 +05302034 void __iomem *base;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302035
2036 match = of_match_device(of_match_ptr(omap_mmc_of_match), &pdev->dev);
2037 if (match) {
2038 pdata = of_get_hsmmc_pdata(&pdev->dev);
Jan Luebbedc642c22013-01-30 10:07:17 +01002039
2040 if (IS_ERR(pdata))
2041 return PTR_ERR(pdata);
2042
Rajendra Nayak46856a62012-03-12 20:32:37 +05302043 if (match->data) {
Nishanth Menon59445b12014-02-13 23:45:48 -06002044 data = match->data;
2045 pdata->reg_offset = data->reg_offset;
2046 pdata->controller_flags |= data->controller_flags;
Rajendra Nayak46856a62012-03-12 20:32:37 +05302047 }
2048 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002049
2050 if (pdata == NULL) {
2051 dev_err(&pdev->dev, "Platform Data is missing\n");
2052 return -ENXIO;
2053 }
2054
2055 if (pdata->nr_slots == 0) {
2056 dev_err(&pdev->dev, "No Slots\n");
2057 return -ENXIO;
2058 }
2059
2060 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2061 irq = platform_get_irq(pdev, 0);
2062 if (res == NULL || irq < 0)
2063 return -ENXIO;
2064
Balaji T K77fae212014-05-09 22:16:51 +05302065 base = devm_ioremap_resource(&pdev->dev, res);
2066 if (IS_ERR(base))
2067 return PTR_ERR(base);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002068
Denis Karpov70a33412009-09-22 16:44:59 -07002069 mmc = mmc_alloc_host(sizeof(struct omap_hsmmc_host), &pdev->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002070 if (!mmc) {
2071 ret = -ENOMEM;
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002072 goto err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002073 }
2074
2075 host = mmc_priv(mmc);
2076 host->mmc = mmc;
2077 host->pdata = pdata;
2078 host->dev = &pdev->dev;
2079 host->use_dma = 1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002080 host->dma_ch = -1;
2081 host->irq = irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002082 host->slot_id = 0;
Balaji T Kfc307df2012-04-02 12:26:47 +05302083 host->mapbase = res->start + pdata->reg_offset;
Balaji T K77fae212014-05-09 22:16:51 +05302084 host->base = base + pdata->reg_offset;
Adrian Hunter6da20c82010-02-15 10:03:34 -08002085 host->power_mode = MMC_POWER_OFF;
Per Forlin9782aff2011-07-01 18:55:23 +02002086 host->next_data.cookie = 1;
Balaji T Ke99448f2014-02-19 20:26:40 +05302087 host->pbias_enabled = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002088
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002089 ret = omap_hsmmc_gpio_init(host, pdata);
2090 if (ret)
2091 goto err_gpio;
2092
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002093 platform_set_drvdata(pdev, host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002094
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002095 if (pdev->dev.of_node)
2096 host->wake_irq = irq_of_parse_and_map(pdev->dev.of_node, 1);
2097
Balaji T K7a8c2ce2011-07-01 22:09:34 +05302098 mmc->ops = &omap_hsmmc_ops;
Denis Karpovdd498ef2009-09-22 16:44:49 -07002099
Daniel Mackd418ed82012-02-19 13:20:33 +01002100 mmc->f_min = OMAP_MMC_MIN_CLOCK;
2101
2102 if (pdata->max_freq > 0)
2103 mmc->f_max = pdata->max_freq;
2104 else
2105 mmc->f_max = OMAP_MMC_MAX_CLOCK;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002106
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07002107 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002108
Balaji T K96181952014-05-09 22:16:48 +05302109 host->fclk = devm_clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002110 if (IS_ERR(host->fclk)) {
2111 ret = PTR_ERR(host->fclk);
2112 host->fclk = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002113 goto err1;
2114 }
2115
Paul Walmsley9b682562011-10-06 14:50:35 -06002116 if (host->pdata->controller_flags & OMAP_HSMMC_BROKEN_MULTIBLOCK_READ) {
2117 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 -07002118 omap_hsmmc_ops.multi_io_quirk = omap_hsmmc_multi_io_quirk;
Paul Walmsley9b682562011-10-06 14:50:35 -06002119 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07002120
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302121 pm_runtime_enable(host->dev);
2122 pm_runtime_get_sync(host->dev);
2123 pm_runtime_set_autosuspend_delay(host->dev, MMC_AUTOSUSPEND_DELAY);
2124 pm_runtime_use_autosuspend(host->dev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002125
Balaji T K92a3aeb2012-02-24 21:14:34 +05302126 omap_hsmmc_context_save(host);
2127
Balaji T K96181952014-05-09 22:16:48 +05302128 host->dbclk = devm_clk_get(&pdev->dev, "mmchsdb_fck");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302129 /*
2130 * MMC can still work without debounce clock.
2131 */
2132 if (IS_ERR(host->dbclk)) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302133 host->dbclk = NULL;
Rajendra Nayak94c18142012-06-27 14:19:54 +05302134 } else if (clk_prepare_enable(host->dbclk) != 0) {
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302135 dev_warn(mmc_dev(host->mmc), "Failed to enable debounce clk\n");
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302136 host->dbclk = NULL;
Adrian Hunter2bec0892009-09-22 16:45:02 -07002137 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002138
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002139 /* Since we do only SG emulation, we can have as many segs
2140 * as we want. */
Martin K. Petersena36274e2010-09-10 01:33:59 -04002141 mmc->max_segs = 1024;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02002142
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002143 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
2144 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
2145 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2146 mmc->max_seg_size = mmc->max_req_size;
2147
Jarkko Lavinen13189e72009-09-22 16:44:53 -07002148 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
Adrian Hunter93caf8e692010-08-11 14:17:48 -07002149 MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002150
Andreas Fenkart326119c2014-11-08 15:33:14 +01002151 mmc->caps |= mmc_pdata(host)->caps;
Sukumar Ghorai3a638332010-09-15 14:49:23 +00002152 if (mmc->caps & MMC_CAP_8_BIT_DATA)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002153 mmc->caps |= MMC_CAP_4_BIT_DATA;
2154
Andreas Fenkart326119c2014-11-08 15:33:14 +01002155 if (mmc_pdata(host)->nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07002156 mmc->caps |= MMC_CAP_NONREMOVABLE;
2157
Andreas Fenkart326119c2014-11-08 15:33:14 +01002158 mmc->pm_caps = mmc_pdata(host)->pm_caps;
Eliad Peller6fdc75d2011-11-22 16:02:18 +02002159
Denis Karpov70a33412009-09-22 16:44:59 -07002160 omap_hsmmc_conf_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002161
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302162 if (!pdev->dev.of_node) {
2163 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
2164 if (!res) {
2165 dev_err(mmc_dev(host->mmc), "cannot get DMA TX channel\n");
2166 ret = -ENXIO;
2167 goto err_irq;
2168 }
2169 tx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002170
Santosh Shilimkar4a29b552013-05-10 17:42:35 +05302171 res = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
2172 if (!res) {
2173 dev_err(mmc_dev(host->mmc), "cannot get DMA RX channel\n");
2174 ret = -ENXIO;
2175 goto err_irq;
2176 }
2177 rx_req = res->start;
Balaji T Kb7bf7732012-03-07 09:55:30 -05002178 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002179
Russell King26b88522012-04-13 12:27:37 +01002180 dma_cap_zero(mask);
2181 dma_cap_set(DMA_SLAVE, mask);
Russell Kingc5c98922012-04-13 12:14:39 +01002182
Matt Porterd272fbf2013-05-10 17:42:34 +05302183 host->rx_chan =
2184 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2185 &rx_req, &pdev->dev, "rx");
2186
Russell King26b88522012-04-13 12:27:37 +01002187 if (!host->rx_chan) {
2188 dev_err(mmc_dev(host->mmc), "unable to obtain RX DMA engine channel %u\n", rx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002189 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002190 goto err_irq;
2191 }
2192
Matt Porterd272fbf2013-05-10 17:42:34 +05302193 host->tx_chan =
2194 dma_request_slave_channel_compat(mask, omap_dma_filter_fn,
2195 &tx_req, &pdev->dev, "tx");
2196
Russell King26b88522012-04-13 12:27:37 +01002197 if (!host->tx_chan) {
2198 dev_err(mmc_dev(host->mmc), "unable to obtain TX DMA engine channel %u\n", tx_req);
Kevin Hilman04e8c7b2012-07-11 17:51:40 +01002199 ret = -ENXIO;
Russell King26b88522012-04-13 12:27:37 +01002200 goto err_irq;
Russell Kingc5c98922012-04-13 12:14:39 +01002201 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002202
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002203 /* Request IRQ for MMC operations */
Balaji T Ke1538ed2014-05-09 22:16:49 +05302204 ret = devm_request_irq(&pdev->dev, host->irq, omap_hsmmc_irq, 0,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002205 mmc_hostname(mmc), host);
2206 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302207 dev_err(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002208 goto err_irq;
2209 }
2210
Andreas Fenkart326119c2014-11-08 15:33:14 +01002211 if (omap_hsmmc_have_reg() && !mmc_pdata(host)->set_power) {
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002212 ret = omap_hsmmc_reg_get(host);
2213 if (ret)
Andreas Fenkartbb09d152014-11-08 15:33:11 +01002214 goto err_irq;
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002215 host->use_reg = 1;
2216 }
2217
Andreas Fenkart326119c2014-11-08 15:33:14 +01002218 mmc->ocr_avail = mmc_pdata(host)->ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002219
2220 /* Request IRQ for card detect */
Andreas Fenkart326119c2014-11-08 15:33:14 +01002221 if ((mmc_pdata(host)->card_detect_irq)) {
Balaji T K9fa0e052014-05-09 22:16:50 +05302222 ret = devm_request_threaded_irq(&pdev->dev,
Andreas Fenkart326119c2014-11-08 15:33:14 +01002223 mmc_pdata(host)->card_detect_irq,
Balaji T K9fa0e052014-05-09 22:16:50 +05302224 NULL, omap_hsmmc_detect,
Ming Leidb35f832012-05-17 10:27:12 +08002225 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
NeilBrown7efab4f2011-12-30 12:35:13 +11002226 mmc_hostname(mmc), host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002227 if (ret) {
Venkatraman Sb1e056a2012-11-19 22:00:00 +05302228 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002229 "Unable to grab MMC CD IRQ\n");
2230 goto err_irq_cd;
2231 }
kishore kadiyala72f2e2c2010-09-24 17:13:20 +00002232 pdata->suspend = omap_hsmmc_suspend_cdirq;
2233 pdata->resume = omap_hsmmc_resume_cdirq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002234 }
2235
Adrian Hunterb4175772010-05-26 14:42:06 -07002236 omap_hsmmc_disable_irq(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002237
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002238 /*
2239 * For now, only support SDIO interrupt if we have a separate
2240 * wake-up interrupt configured from device tree. This is because
2241 * the wake-up interrupt is needed for idle state and some
2242 * platforms need special quirks. And we don't want to add new
2243 * legacy mux platform init code callbacks any longer as we
2244 * are moving to DT based booting anyways.
2245 */
2246 ret = omap_hsmmc_configure_wake_irq(host);
2247 if (!ret)
2248 mmc->caps |= MMC_CAP_SDIO_IRQ;
2249
Adrian Hunterb62f6222009-09-22 16:45:01 -07002250 omap_hsmmc_protect_card(host);
2251
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002252 mmc_add_host(mmc);
2253
Andreas Fenkart326119c2014-11-08 15:33:14 +01002254 if (mmc_pdata(host)->name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002255 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
2256 if (ret < 0)
2257 goto err_slot_name;
2258 }
Andreas Fenkart326119c2014-11-08 15:33:14 +01002259 if (mmc_pdata(host)->card_detect_irq &&
2260 mmc_pdata(host)->get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002261 ret = device_create_file(&mmc->class_dev,
2262 &dev_attr_cover_switch);
2263 if (ret < 0)
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002264 goto err_slot_name;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002265 }
2266
Denis Karpov70a33412009-09-22 16:44:59 -07002267 omap_hsmmc_debugfs(mmc);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302268 pm_runtime_mark_last_busy(host->dev);
2269 pm_runtime_put_autosuspend(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07002270
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002271 return 0;
2272
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002273err_slot_name:
2274 mmc_remove_host(mmc);
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002275err_irq_cd:
2276 if (host->use_reg)
2277 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002278err_irq:
Russell Kingc5c98922012-04-13 12:14:39 +01002279 if (host->tx_chan)
2280 dma_release_channel(host->tx_chan);
2281 if (host->rx_chan)
2282 dma_release_channel(host->rx_chan);
Balaji T Kd59d77e2012-02-24 21:14:33 +05302283 pm_runtime_put_sync(host->dev);
Tony Lindgren37f61902012-03-08 23:41:35 -05002284 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302285 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302286 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002287err1:
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002288 omap_hsmmc_gpio_free(host, pdata);
2289err_gpio:
Adrian Hunterdb0fefc2010-02-15 10:03:34 -08002290 mmc_free_host(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002291err:
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002292 return ret;
2293}
2294
Bill Pemberton6e0ee712012-11-19 13:26:03 -05002295static int omap_hsmmc_remove(struct platform_device *pdev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002296{
Denis Karpov70a33412009-09-22 16:44:59 -07002297 struct omap_hsmmc_host *host = platform_get_drvdata(pdev);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002298
Felipe Balbi927ce942012-03-14 11:18:27 +02002299 pm_runtime_get_sync(host->dev);
2300 mmc_remove_host(host->mmc);
2301 if (host->use_reg)
2302 omap_hsmmc_reg_put(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002303
Russell Kingc5c98922012-04-13 12:14:39 +01002304 if (host->tx_chan)
2305 dma_release_channel(host->tx_chan);
2306 if (host->rx_chan)
2307 dma_release_channel(host->rx_chan);
2308
Felipe Balbi927ce942012-03-14 11:18:27 +02002309 pm_runtime_put_sync(host->dev);
2310 pm_runtime_disable(host->dev);
Balaji T K96181952014-05-09 22:16:48 +05302311 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302312 clk_disable_unprepare(host->dbclk);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002313
Andreas Fenkart1e363e32014-11-08 15:33:15 +01002314 omap_hsmmc_gpio_free(host, host->pdata);
Balaji T K9d1f0282012-10-15 21:35:07 +05302315 mmc_free_host(host->mmc);
Felipe Balbi927ce942012-03-14 11:18:27 +02002316
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002317 return 0;
2318}
2319
2320#ifdef CONFIG_PM
Felipe Balbia48ce882012-11-19 21:59:59 +05302321static int omap_hsmmc_prepare(struct device *dev)
2322{
2323 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2324
2325 if (host->pdata->suspend)
2326 return host->pdata->suspend(dev, host->slot_id);
2327
2328 return 0;
2329}
2330
2331static void omap_hsmmc_complete(struct device *dev)
2332{
2333 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2334
2335 if (host->pdata->resume)
2336 host->pdata->resume(dev, host->slot_id);
2337
2338}
2339
Kevin Hilmana791daa2010-05-26 14:42:07 -07002340static int omap_hsmmc_suspend(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002341{
Felipe Balbi927ce942012-03-14 11:18:27 +02002342 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2343
2344 if (!host)
2345 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002346
Felipe Balbi927ce942012-03-14 11:18:27 +02002347 pm_runtime_get_sync(host->dev);
Felipe Balbi927ce942012-03-14 11:18:27 +02002348
2349 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER)) {
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002350 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2351 OMAP_HSMMC_WRITE(host->base, IE, 0);
2352 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
Felipe Balbi927ce942012-03-14 11:18:27 +02002353 OMAP_HSMMC_WRITE(host->base, HCTL,
2354 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
2355 }
2356
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002357 /* do not wake up due to sdio irq */
2358 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2359 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2360 disable_irq(host->wake_irq);
2361
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302362 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302363 clk_disable_unprepare(host->dbclk);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002364
Eliad Peller31f9d462011-11-22 16:02:17 +02002365 pm_runtime_put_sync(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002366 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002367}
2368
2369/* Routine to resume the MMC device */
Kevin Hilmana791daa2010-05-26 14:42:07 -07002370static int omap_hsmmc_resume(struct device *dev)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002371{
Felipe Balbi927ce942012-03-14 11:18:27 +02002372 struct omap_hsmmc_host *host = dev_get_drvdata(dev);
2373
2374 if (!host)
2375 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002376
Felipe Balbi927ce942012-03-14 11:18:27 +02002377 pm_runtime_get_sync(host->dev);
Denis Karpov11dd62a2009-09-22 16:44:43 -07002378
Rajendra Nayakcd03d9a2012-04-09 12:08:35 +05302379 if (host->dbclk)
Rajendra Nayak94c18142012-06-27 14:19:54 +05302380 clk_prepare_enable(host->dbclk);
Adrian Hunter2bec0892009-09-22 16:45:02 -07002381
Felipe Balbi927ce942012-03-14 11:18:27 +02002382 if (!(host->mmc->pm_flags & MMC_PM_KEEP_POWER))
2383 omap_hsmmc_conf_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01002384
Felipe Balbi927ce942012-03-14 11:18:27 +02002385 omap_hsmmc_protect_card(host);
2386
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002387 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2388 !(host->mmc->pm_flags & MMC_PM_WAKE_SDIO_IRQ))
2389 enable_irq(host->wake_irq);
2390
Felipe Balbi927ce942012-03-14 11:18:27 +02002391 pm_runtime_mark_last_busy(host->dev);
2392 pm_runtime_put_autosuspend(host->dev);
Ulf Hansson3932afd2013-09-25 14:47:06 +02002393 return 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002394}
2395
2396#else
Felipe Balbia48ce882012-11-19 21:59:59 +05302397#define omap_hsmmc_prepare NULL
2398#define omap_hsmmc_complete NULL
Denis Karpov70a33412009-09-22 16:44:59 -07002399#define omap_hsmmc_suspend NULL
Felipe Balbia48ce882012-11-19 21:59:59 +05302400#define omap_hsmmc_resume NULL
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002401#endif
2402
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302403static int omap_hsmmc_runtime_suspend(struct device *dev)
2404{
2405 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002406 unsigned long flags;
Andreas Fenkartf9459012014-05-29 10:28:03 +02002407 int ret = 0;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302408
2409 host = platform_get_drvdata(to_platform_device(dev));
2410 omap_hsmmc_context_save(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002411 dev_dbg(dev, "disabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302412
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002413 spin_lock_irqsave(&host->irq_lock, flags);
2414 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2415 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2416 /* disable sdio irq handling to prevent race */
2417 OMAP_HSMMC_WRITE(host->base, ISE, 0);
2418 OMAP_HSMMC_WRITE(host->base, IE, 0);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002419
2420 if (!(OMAP_HSMMC_READ(host->base, PSTATE) & DLEV_DAT(1))) {
2421 /*
2422 * dat1 line low, pending sdio irq
2423 * race condition: possible irq handler running on
2424 * multi-core, abort
2425 */
2426 dev_dbg(dev, "pending sdio irq, abort suspend\n");
2427 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2428 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2429 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
2430 pm_runtime_mark_last_busy(dev);
2431 ret = -EBUSY;
2432 goto abort;
2433 }
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002434
Andreas Fenkart97978a42014-05-29 10:28:04 +02002435 pinctrl_pm_select_idle_state(dev);
2436
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002437 WARN_ON(host->flags & HSMMC_WAKE_IRQ_ENABLED);
2438 enable_irq(host->wake_irq);
2439 host->flags |= HSMMC_WAKE_IRQ_ENABLED;
Andreas Fenkart97978a42014-05-29 10:28:04 +02002440 } else {
2441 pinctrl_pm_select_idle_state(dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002442 }
Andreas Fenkart97978a42014-05-29 10:28:04 +02002443
Andreas Fenkartf9459012014-05-29 10:28:03 +02002444abort:
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002445 spin_unlock_irqrestore(&host->irq_lock, flags);
Andreas Fenkartf9459012014-05-29 10:28:03 +02002446 return ret;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302447}
2448
2449static int omap_hsmmc_runtime_resume(struct device *dev)
2450{
2451 struct omap_hsmmc_host *host;
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002452 unsigned long flags;
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302453
2454 host = platform_get_drvdata(to_platform_device(dev));
2455 omap_hsmmc_context_restore(host);
Felipe Balbi927ce942012-03-14 11:18:27 +02002456 dev_dbg(dev, "enabled\n");
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302457
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002458 spin_lock_irqsave(&host->irq_lock, flags);
2459 if ((host->mmc->caps & MMC_CAP_SDIO_IRQ) &&
2460 (host->flags & HSMMC_SDIO_IRQ_ENABLED)) {
2461 /* sdio irq flag can't change while in runtime suspend */
2462 if (host->flags & HSMMC_WAKE_IRQ_ENABLED) {
2463 disable_irq_nosync(host->wake_irq);
2464 host->flags &= ~HSMMC_WAKE_IRQ_ENABLED;
2465 }
2466
Andreas Fenkart97978a42014-05-29 10:28:04 +02002467 pinctrl_pm_select_default_state(host->dev);
2468
2469 /* irq lost, if pinmux incorrect */
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002470 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
2471 OMAP_HSMMC_WRITE(host->base, ISE, CIRQ_EN);
2472 OMAP_HSMMC_WRITE(host->base, IE, CIRQ_EN);
Andreas Fenkart97978a42014-05-29 10:28:04 +02002473 } else {
2474 pinctrl_pm_select_default_state(host->dev);
Andreas Fenkart2cd3a2a2014-05-29 10:28:00 +02002475 }
2476 spin_unlock_irqrestore(&host->irq_lock, flags);
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302477 return 0;
2478}
2479
Kevin Hilmana791daa2010-05-26 14:42:07 -07002480static struct dev_pm_ops omap_hsmmc_dev_pm_ops = {
Denis Karpov70a33412009-09-22 16:44:59 -07002481 .suspend = omap_hsmmc_suspend,
2482 .resume = omap_hsmmc_resume,
Felipe Balbia48ce882012-11-19 21:59:59 +05302483 .prepare = omap_hsmmc_prepare,
2484 .complete = omap_hsmmc_complete,
Balaji T Kfa4aa2d2011-07-01 22:09:35 +05302485 .runtime_suspend = omap_hsmmc_runtime_suspend,
2486 .runtime_resume = omap_hsmmc_runtime_resume,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002487};
2488
2489static struct platform_driver omap_hsmmc_driver = {
Felipe Balbiefa25fd2012-03-14 11:18:28 +02002490 .probe = omap_hsmmc_probe,
Bill Pemberton0433c142012-11-19 13:20:26 -05002491 .remove = omap_hsmmc_remove,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002492 .driver = {
2493 .name = DRIVER_NAME,
Kevin Hilmana791daa2010-05-26 14:42:07 -07002494 .pm = &omap_hsmmc_dev_pm_ops,
Rajendra Nayak46856a62012-03-12 20:32:37 +05302495 .of_match_table = of_match_ptr(omap_mmc_of_match),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002496 },
2497};
2498
Felipe Balbib7964502012-03-14 11:18:32 +02002499module_platform_driver(omap_hsmmc_driver);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01002500MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
2501MODULE_LICENSE("GPL");
2502MODULE_ALIAS("platform:" DRIVER_NAME);
2503MODULE_AUTHOR("Texas Instruments Inc");