blob: a1b80e515147c73df9471ee4d77fcbff4d9915cc [file] [log] [blame]
Will Newtonf95f3852011-01-02 01:11:59 -05001/*
2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
4 *
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/blkdev.h>
15#include <linux/clk.h>
16#include <linux/debugfs.h>
17#include <linux/device.h>
18#include <linux/dma-mapping.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/ioport.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
Will Newtonf95f3852011-01-02 01:11:59 -050025#include <linux/seq_file.h>
26#include <linux/slab.h>
27#include <linux/stat.h>
28#include <linux/delay.h>
29#include <linux/irq.h>
Doug Andersonb24c8b22014-12-02 15:42:46 -080030#include <linux/mmc/card.h>
Will Newtonf95f3852011-01-02 01:11:59 -050031#include <linux/mmc/host.h>
32#include <linux/mmc/mmc.h>
Doug Anderson01730552014-08-22 19:17:51 +053033#include <linux/mmc/sd.h>
Seungwon Jeon90c21432013-08-31 00:14:05 +090034#include <linux/mmc/sdio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050035#include <linux/mmc/dw_mmc.h>
36#include <linux/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090037#include <linux/regulator/consumer.h>
Thomas Abrahamc91eab42012-09-17 18:16:40 +000038#include <linux/of.h>
Doug Anderson55a6ceb2013-01-11 17:03:53 +000039#include <linux/of_gpio.h>
Zhangfei Gaobf626e52014-01-09 22:35:10 +080040#include <linux/mmc/slot-gpio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050041
42#include "dw_mmc.h"
43
44/* Common flag combinations */
Jaehoon Chung3f7eec62013-05-27 13:47:57 +090045#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
Will Newtonf95f3852011-01-02 01:11:59 -050046 SDMMC_INT_HTO | SDMMC_INT_SBE | \
47 SDMMC_INT_EBE)
48#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
49 SDMMC_INT_RESP_ERR)
50#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
51 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
52#define DW_MCI_SEND_STATUS 1
53#define DW_MCI_RECV_STATUS 2
54#define DW_MCI_DMA_THRESHOLD 16
55
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090056#define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
57#define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
58
Will Newtonf95f3852011-01-02 01:11:59 -050059#ifdef CONFIG_MMC_DW_IDMAC
Joonyoung Shimfc79a4d2013-04-26 15:35:22 +090060#define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
61 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
62 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
63 SDMMC_IDMAC_INT_TI)
64
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000065struct idmac_desc_64addr {
66 u32 des0; /* Control Descriptor */
67
68 u32 des1; /* Reserved */
69
70 u32 des2; /*Buffer sizes */
71#define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
72 ((d)->des2 = ((d)->des2 & 0x03ffe000) | ((s) & 0x1fff))
73
74 u32 des3; /* Reserved */
75
76 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
77 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
78
79 u32 des6; /* Lower 32-bits of Next Descriptor Address */
80 u32 des7; /* Upper 32-bits of Next Descriptor Address */
81};
82
Will Newtonf95f3852011-01-02 01:11:59 -050083struct idmac_desc {
84 u32 des0; /* Control Descriptor */
85#define IDMAC_DES0_DIC BIT(1)
86#define IDMAC_DES0_LD BIT(2)
87#define IDMAC_DES0_FD BIT(3)
88#define IDMAC_DES0_CH BIT(4)
89#define IDMAC_DES0_ER BIT(5)
90#define IDMAC_DES0_CES BIT(30)
91#define IDMAC_DES0_OWN BIT(31)
92
93 u32 des1; /* Buffer sizes */
94#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Shashidhar Hiremath9b7bbe12011-07-29 08:49:50 -040095 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
Will Newtonf95f3852011-01-02 01:11:59 -050096
97 u32 des2; /* buffer 1 physical address */
98
99 u32 des3; /* buffer 2 physical address */
100};
101#endif /* CONFIG_MMC_DW_IDMAC */
102
Sonny Rao3a33a942014-08-04 18:19:50 -0700103static bool dw_mci_reset(struct dw_mci *host);
Sonny Rao536f6b92014-10-16 09:58:05 -0700104static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
Seungwon Jeon31bff452013-08-31 00:14:23 +0900105
Will Newtonf95f3852011-01-02 01:11:59 -0500106#if defined(CONFIG_DEBUG_FS)
107static int dw_mci_req_show(struct seq_file *s, void *v)
108{
109 struct dw_mci_slot *slot = s->private;
110 struct mmc_request *mrq;
111 struct mmc_command *cmd;
112 struct mmc_command *stop;
113 struct mmc_data *data;
114
115 /* Make sure we get a consistent snapshot */
116 spin_lock_bh(&slot->host->lock);
117 mrq = slot->mrq;
118
119 if (mrq) {
120 cmd = mrq->cmd;
121 data = mrq->data;
122 stop = mrq->stop;
123
124 if (cmd)
125 seq_printf(s,
126 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
127 cmd->opcode, cmd->arg, cmd->flags,
128 cmd->resp[0], cmd->resp[1], cmd->resp[2],
129 cmd->resp[2], cmd->error);
130 if (data)
131 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
132 data->bytes_xfered, data->blocks,
133 data->blksz, data->flags, data->error);
134 if (stop)
135 seq_printf(s,
136 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
137 stop->opcode, stop->arg, stop->flags,
138 stop->resp[0], stop->resp[1], stop->resp[2],
139 stop->resp[2], stop->error);
140 }
141
142 spin_unlock_bh(&slot->host->lock);
143
144 return 0;
145}
146
147static int dw_mci_req_open(struct inode *inode, struct file *file)
148{
149 return single_open(file, dw_mci_req_show, inode->i_private);
150}
151
152static const struct file_operations dw_mci_req_fops = {
153 .owner = THIS_MODULE,
154 .open = dw_mci_req_open,
155 .read = seq_read,
156 .llseek = seq_lseek,
157 .release = single_release,
158};
159
160static int dw_mci_regs_show(struct seq_file *s, void *v)
161{
162 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
163 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
164 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
165 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
166 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
167 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
168
169 return 0;
170}
171
172static int dw_mci_regs_open(struct inode *inode, struct file *file)
173{
174 return single_open(file, dw_mci_regs_show, inode->i_private);
175}
176
177static const struct file_operations dw_mci_regs_fops = {
178 .owner = THIS_MODULE,
179 .open = dw_mci_regs_open,
180 .read = seq_read,
181 .llseek = seq_lseek,
182 .release = single_release,
183};
184
185static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
186{
187 struct mmc_host *mmc = slot->mmc;
188 struct dw_mci *host = slot->host;
189 struct dentry *root;
190 struct dentry *node;
191
192 root = mmc->debugfs_root;
193 if (!root)
194 return;
195
196 node = debugfs_create_file("regs", S_IRUSR, root, host,
197 &dw_mci_regs_fops);
198 if (!node)
199 goto err;
200
201 node = debugfs_create_file("req", S_IRUSR, root, slot,
202 &dw_mci_req_fops);
203 if (!node)
204 goto err;
205
206 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
207 if (!node)
208 goto err;
209
210 node = debugfs_create_x32("pending_events", S_IRUSR, root,
211 (u32 *)&host->pending_events);
212 if (!node)
213 goto err;
214
215 node = debugfs_create_x32("completed_events", S_IRUSR, root,
216 (u32 *)&host->completed_events);
217 if (!node)
218 goto err;
219
220 return;
221
222err:
223 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
224}
225#endif /* defined(CONFIG_DEBUG_FS) */
226
Doug Anderson01730552014-08-22 19:17:51 +0530227static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
228
Will Newtonf95f3852011-01-02 01:11:59 -0500229static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
230{
231 struct mmc_data *data;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000232 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson01730552014-08-22 19:17:51 +0530233 struct dw_mci *host = slot->host;
Arnd Bergmanne95baf12012-11-08 14:26:11 +0000234 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Will Newtonf95f3852011-01-02 01:11:59 -0500235 u32 cmdr;
236 cmd->error = -EINPROGRESS;
237
238 cmdr = cmd->opcode;
239
Seungwon Jeon90c21432013-08-31 00:14:05 +0900240 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
241 cmd->opcode == MMC_GO_IDLE_STATE ||
242 cmd->opcode == MMC_GO_INACTIVE_STATE ||
243 (cmd->opcode == SD_IO_RW_DIRECT &&
244 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
Will Newtonf95f3852011-01-02 01:11:59 -0500245 cmdr |= SDMMC_CMD_STOP;
Jaehoon Chung4a1b27a2014-03-03 11:36:44 +0900246 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
247 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500248
Doug Anderson01730552014-08-22 19:17:51 +0530249 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
250 u32 clk_en_a;
251
252 /* Special bit makes CMD11 not die */
253 cmdr |= SDMMC_CMD_VOLT_SWITCH;
254
255 /* Change state to continue to handle CMD11 weirdness */
256 WARN_ON(slot->host->state != STATE_SENDING_CMD);
257 slot->host->state = STATE_SENDING_CMD11;
258
259 /*
260 * We need to disable low power mode (automatic clock stop)
261 * while doing voltage switch so we don't confuse the card,
262 * since stopping the clock is a specific part of the UHS
263 * voltage change dance.
264 *
265 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
266 * unconditionally turned back on in dw_mci_setup_bus() if it's
267 * ever called with a non-zero clock. That shouldn't happen
268 * until the voltage change is all done.
269 */
270 clk_en_a = mci_readl(host, CLKENA);
271 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
272 mci_writel(host, CLKENA, clk_en_a);
273 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
274 SDMMC_CMD_PRV_DAT_WAIT, 0);
275 }
276
Will Newtonf95f3852011-01-02 01:11:59 -0500277 if (cmd->flags & MMC_RSP_PRESENT) {
278 /* We expect a response, so set this bit */
279 cmdr |= SDMMC_CMD_RESP_EXP;
280 if (cmd->flags & MMC_RSP_136)
281 cmdr |= SDMMC_CMD_RESP_LONG;
282 }
283
284 if (cmd->flags & MMC_RSP_CRC)
285 cmdr |= SDMMC_CMD_RESP_CRC;
286
287 data = cmd->data;
288 if (data) {
289 cmdr |= SDMMC_CMD_DAT_EXP;
290 if (data->flags & MMC_DATA_STREAM)
291 cmdr |= SDMMC_CMD_STRM_MODE;
292 if (data->flags & MMC_DATA_WRITE)
293 cmdr |= SDMMC_CMD_DAT_WR;
294 }
295
James Hogancb27a842012-10-16 09:43:08 +0100296 if (drv_data && drv_data->prepare_command)
297 drv_data->prepare_command(slot->host, &cmdr);
Thomas Abraham800d78b2012-09-17 18:16:42 +0000298
Will Newtonf95f3852011-01-02 01:11:59 -0500299 return cmdr;
300}
301
Seungwon Jeon90c21432013-08-31 00:14:05 +0900302static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
303{
304 struct mmc_command *stop;
305 u32 cmdr;
306
307 if (!cmd->data)
308 return 0;
309
310 stop = &host->stop_abort;
311 cmdr = cmd->opcode;
312 memset(stop, 0, sizeof(struct mmc_command));
313
314 if (cmdr == MMC_READ_SINGLE_BLOCK ||
315 cmdr == MMC_READ_MULTIPLE_BLOCK ||
316 cmdr == MMC_WRITE_BLOCK ||
317 cmdr == MMC_WRITE_MULTIPLE_BLOCK) {
318 stop->opcode = MMC_STOP_TRANSMISSION;
319 stop->arg = 0;
320 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
321 } else if (cmdr == SD_IO_RW_EXTENDED) {
322 stop->opcode = SD_IO_RW_DIRECT;
323 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
324 ((cmd->arg >> 28) & 0x7);
325 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
326 } else {
327 return 0;
328 }
329
330 cmdr = stop->opcode | SDMMC_CMD_STOP |
331 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
332
333 return cmdr;
334}
335
Will Newtonf95f3852011-01-02 01:11:59 -0500336static void dw_mci_start_command(struct dw_mci *host,
337 struct mmc_command *cmd, u32 cmd_flags)
338{
339 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000340 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500341 "start command: ARGR=0x%08x CMDR=0x%08x\n",
342 cmd->arg, cmd_flags);
343
344 mci_writel(host, CMDARG, cmd->arg);
345 wmb();
346
347 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
348}
349
Seungwon Jeon90c21432013-08-31 00:14:05 +0900350static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
Will Newtonf95f3852011-01-02 01:11:59 -0500351{
Seungwon Jeon90c21432013-08-31 00:14:05 +0900352 struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
353 dw_mci_start_command(host, stop, host->stop_cmdr);
Will Newtonf95f3852011-01-02 01:11:59 -0500354}
355
356/* DMA interface functions */
357static void dw_mci_stop_dma(struct dw_mci *host)
358{
James Hogan03e8cb52011-06-29 09:28:43 +0100359 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500360 host->dma_ops->stop(host);
361 host->dma_ops->cleanup(host);
Will Newtonf95f3852011-01-02 01:11:59 -0500362 }
Seungwon Jeonaa50f252013-08-31 00:14:38 +0900363
364 /* Data transfer was stopped by the interrupt handler */
365 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -0500366}
367
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900368static int dw_mci_get_dma_dir(struct mmc_data *data)
369{
370 if (data->flags & MMC_DATA_WRITE)
371 return DMA_TO_DEVICE;
372 else
373 return DMA_FROM_DEVICE;
374}
375
Jaehoon Chung9beee912012-02-16 11:19:38 +0900376#ifdef CONFIG_MMC_DW_IDMAC
Will Newtonf95f3852011-01-02 01:11:59 -0500377static void dw_mci_dma_cleanup(struct dw_mci *host)
378{
379 struct mmc_data *data = host->data;
380
381 if (data)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900382 if (!data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000383 dma_unmap_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900384 data->sg,
385 data->sg_len,
386 dw_mci_get_dma_dir(data));
Will Newtonf95f3852011-01-02 01:11:59 -0500387}
388
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900389static void dw_mci_idmac_reset(struct dw_mci *host)
390{
391 u32 bmod = mci_readl(host, BMOD);
392 /* Software reset of DMA */
393 bmod |= SDMMC_IDMAC_SWRESET;
394 mci_writel(host, BMOD, bmod);
395}
396
Will Newtonf95f3852011-01-02 01:11:59 -0500397static void dw_mci_idmac_stop_dma(struct dw_mci *host)
398{
399 u32 temp;
400
401 /* Disable and reset the IDMAC interface */
402 temp = mci_readl(host, CTRL);
403 temp &= ~SDMMC_CTRL_USE_IDMAC;
404 temp |= SDMMC_CTRL_DMA_RESET;
405 mci_writel(host, CTRL, temp);
406
407 /* Stop the IDMAC running */
408 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900409 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900410 temp |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -0500411 mci_writel(host, BMOD, temp);
412}
413
414static void dw_mci_idmac_complete_dma(struct dw_mci *host)
415{
416 struct mmc_data *data = host->data;
417
Thomas Abraham4a909202012-09-17 18:16:35 +0000418 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500419
420 host->dma_ops->cleanup(host);
421
422 /*
423 * If the card was removed, data will be NULL. No point in trying to
424 * send the stop command or waiting for NBUSY in this case.
425 */
426 if (data) {
427 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
428 tasklet_schedule(&host->tasklet);
429 }
430}
431
432static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
433 unsigned int sg_len)
434{
435 int i;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000436 if (host->dma_64bit_address == 1) {
437 struct idmac_desc_64addr *desc = host->sg_cpu;
Will Newtonf95f3852011-01-02 01:11:59 -0500438
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000439 for (i = 0; i < sg_len; i++, desc++) {
440 unsigned int length = sg_dma_len(&data->sg[i]);
441 u64 mem_addr = sg_dma_address(&data->sg[i]);
Will Newtonf95f3852011-01-02 01:11:59 -0500442
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000443 /*
444 * Set the OWN bit and disable interrupts for this
445 * descriptor
446 */
447 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
448 IDMAC_DES0_CH;
449 /* Buffer length */
450 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, length);
Will Newtonf95f3852011-01-02 01:11:59 -0500451
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000452 /* Physical address to DMA to/from */
453 desc->des4 = mem_addr & 0xffffffff;
454 desc->des5 = mem_addr >> 32;
455 }
Will Newtonf95f3852011-01-02 01:11:59 -0500456
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000457 /* Set first descriptor */
458 desc = host->sg_cpu;
459 desc->des0 |= IDMAC_DES0_FD;
460
461 /* Set last descriptor */
462 desc = host->sg_cpu + (i - 1) *
463 sizeof(struct idmac_desc_64addr);
464 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
465 desc->des0 |= IDMAC_DES0_LD;
466
467 } else {
468 struct idmac_desc *desc = host->sg_cpu;
469
470 for (i = 0; i < sg_len; i++, desc++) {
471 unsigned int length = sg_dma_len(&data->sg[i]);
472 u32 mem_addr = sg_dma_address(&data->sg[i]);
473
474 /*
475 * Set the OWN bit and disable interrupts for this
476 * descriptor
477 */
478 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
479 IDMAC_DES0_CH;
480 /* Buffer length */
481 IDMAC_SET_BUFFER1_SIZE(desc, length);
482
483 /* Physical address to DMA to/from */
484 desc->des2 = mem_addr;
485 }
486
487 /* Set first descriptor */
488 desc = host->sg_cpu;
489 desc->des0 |= IDMAC_DES0_FD;
490
491 /* Set last descriptor */
492 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
493 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
494 desc->des0 |= IDMAC_DES0_LD;
Will Newtonf95f3852011-01-02 01:11:59 -0500495 }
496
Will Newtonf95f3852011-01-02 01:11:59 -0500497 wmb();
498}
499
500static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
501{
502 u32 temp;
503
504 dw_mci_translate_sglist(host, host->data, sg_len);
505
Sonny Rao536f6b92014-10-16 09:58:05 -0700506 /* Make sure to reset DMA in case we did PIO before this */
507 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
508 dw_mci_idmac_reset(host);
509
Will Newtonf95f3852011-01-02 01:11:59 -0500510 /* Select IDMAC interface */
511 temp = mci_readl(host, CTRL);
512 temp |= SDMMC_CTRL_USE_IDMAC;
513 mci_writel(host, CTRL, temp);
514
515 wmb();
516
517 /* Enable the IDMAC */
518 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900519 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
Will Newtonf95f3852011-01-02 01:11:59 -0500520 mci_writel(host, BMOD, temp);
521
522 /* Start it running */
523 mci_writel(host, PLDMND, 1);
524}
525
526static int dw_mci_idmac_init(struct dw_mci *host)
527{
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800528 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500529
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000530 if (host->dma_64bit_address == 1) {
531 struct idmac_desc_64addr *p;
532 /* Number of descriptors in the ring buffer */
533 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
Will Newtonf95f3852011-01-02 01:11:59 -0500534
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000535 /* Forward link the descriptor list */
536 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
537 i++, p++) {
538 p->des6 = (host->sg_dma +
539 (sizeof(struct idmac_desc_64addr) *
540 (i + 1))) & 0xffffffff;
Will Newtonf95f3852011-01-02 01:11:59 -0500541
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000542 p->des7 = (u64)(host->sg_dma +
543 (sizeof(struct idmac_desc_64addr) *
544 (i + 1))) >> 32;
545 /* Initialize reserved and buffer size fields to "0" */
546 p->des1 = 0;
547 p->des2 = 0;
548 p->des3 = 0;
549 }
550
551 /* Set the last descriptor as the end-of-ring descriptor */
552 p->des6 = host->sg_dma & 0xffffffff;
553 p->des7 = (u64)host->sg_dma >> 32;
554 p->des0 = IDMAC_DES0_ER;
555
556 } else {
557 struct idmac_desc *p;
558 /* Number of descriptors in the ring buffer */
559 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
560
561 /* Forward link the descriptor list */
562 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
563 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) *
564 (i + 1));
565
566 /* Set the last descriptor as the end-of-ring descriptor */
567 p->des3 = host->sg_dma;
568 p->des0 = IDMAC_DES0_ER;
569 }
Will Newtonf95f3852011-01-02 01:11:59 -0500570
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900571 dw_mci_idmac_reset(host);
Seungwon Jeon141a7122012-05-22 13:01:03 +0900572
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000573 if (host->dma_64bit_address == 1) {
574 /* Mask out interrupts - get Tx & Rx complete only */
575 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
576 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
577 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
Will Newtonf95f3852011-01-02 01:11:59 -0500578
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000579 /* Set the descriptor base address */
580 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
581 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
582
583 } else {
584 /* Mask out interrupts - get Tx & Rx complete only */
585 mci_writel(host, IDSTS, IDMAC_INT_CLR);
586 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
587 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
588
589 /* Set the descriptor base address */
590 mci_writel(host, DBADDR, host->sg_dma);
591 }
592
Will Newtonf95f3852011-01-02 01:11:59 -0500593 return 0;
594}
595
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100596static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900597 .init = dw_mci_idmac_init,
598 .start = dw_mci_idmac_start_dma,
599 .stop = dw_mci_idmac_stop_dma,
600 .complete = dw_mci_idmac_complete_dma,
601 .cleanup = dw_mci_dma_cleanup,
602};
603#endif /* CONFIG_MMC_DW_IDMAC */
604
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900605static int dw_mci_pre_dma_transfer(struct dw_mci *host,
606 struct mmc_data *data,
607 bool next)
Will Newtonf95f3852011-01-02 01:11:59 -0500608{
609 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900610 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500611
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900612 if (!next && data->host_cookie)
613 return data->host_cookie;
Will Newtonf95f3852011-01-02 01:11:59 -0500614
615 /*
616 * We don't do DMA on "complex" transfers, i.e. with
617 * non-word-aligned buffers or lengths. Also, we don't bother
618 * with all the DMA setup overhead for short transfers.
619 */
620 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
621 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900622
Will Newtonf95f3852011-01-02 01:11:59 -0500623 if (data->blksz & 3)
624 return -EINVAL;
625
626 for_each_sg(data->sg, sg, data->sg_len, i) {
627 if (sg->offset & 3 || sg->length & 3)
628 return -EINVAL;
629 }
630
Thomas Abraham4a909202012-09-17 18:16:35 +0000631 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900632 data->sg,
633 data->sg_len,
634 dw_mci_get_dma_dir(data));
635 if (sg_len == 0)
636 return -EINVAL;
637
638 if (next)
639 data->host_cookie = sg_len;
640
641 return sg_len;
642}
643
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900644static void dw_mci_pre_req(struct mmc_host *mmc,
645 struct mmc_request *mrq,
646 bool is_first_req)
647{
648 struct dw_mci_slot *slot = mmc_priv(mmc);
649 struct mmc_data *data = mrq->data;
650
651 if (!slot->host->use_dma || !data)
652 return;
653
654 if (data->host_cookie) {
655 data->host_cookie = 0;
656 return;
657 }
658
659 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
660 data->host_cookie = 0;
661}
662
663static void dw_mci_post_req(struct mmc_host *mmc,
664 struct mmc_request *mrq,
665 int err)
666{
667 struct dw_mci_slot *slot = mmc_priv(mmc);
668 struct mmc_data *data = mrq->data;
669
670 if (!slot->host->use_dma || !data)
671 return;
672
673 if (data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000674 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900675 data->sg,
676 data->sg_len,
677 dw_mci_get_dma_dir(data));
678 data->host_cookie = 0;
679}
680
Seungwon Jeon52426892013-08-31 00:13:42 +0900681static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
682{
683#ifdef CONFIG_MMC_DW_IDMAC
684 unsigned int blksz = data->blksz;
685 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
686 u32 fifo_width = 1 << host->data_shift;
687 u32 blksz_depth = blksz / fifo_width, fifoth_val;
688 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
689 int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
690
691 tx_wmark = (host->fifo_depth) / 2;
692 tx_wmark_invers = host->fifo_depth - tx_wmark;
693
694 /*
695 * MSIZE is '1',
696 * if blksz is not a multiple of the FIFO width
697 */
698 if (blksz % fifo_width) {
699 msize = 0;
700 rx_wmark = 1;
701 goto done;
702 }
703
704 do {
705 if (!((blksz_depth % mszs[idx]) ||
706 (tx_wmark_invers % mszs[idx]))) {
707 msize = idx;
708 rx_wmark = mszs[idx] - 1;
709 break;
710 }
711 } while (--idx > 0);
712 /*
713 * If idx is '0', it won't be tried
714 * Thus, initial values are uesed
715 */
716done:
717 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
718 mci_writel(host, FIFOTH, fifoth_val);
719#endif
720}
721
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900722static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
723{
724 unsigned int blksz = data->blksz;
725 u32 blksz_depth, fifo_depth;
726 u16 thld_size;
727
728 WARN_ON(!(data->flags & MMC_DATA_READ));
729
James Hogan66dfd102014-11-17 17:49:05 +0000730 /*
731 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
732 * in the FIFO region, so we really shouldn't access it).
733 */
734 if (host->verid < DW_MMC_240A)
735 return;
736
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900737 if (host->timing != MMC_TIMING_MMC_HS200 &&
738 host->timing != MMC_TIMING_UHS_SDR104)
739 goto disable;
740
741 blksz_depth = blksz / (1 << host->data_shift);
742 fifo_depth = host->fifo_depth;
743
744 if (blksz_depth > fifo_depth)
745 goto disable;
746
747 /*
748 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
749 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
750 * Currently just choose blksz.
751 */
752 thld_size = blksz;
753 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
754 return;
755
756disable:
757 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
758}
759
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900760static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
761{
Doug Andersonf8c58c12014-12-02 15:42:47 -0800762 unsigned long irqflags;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900763 int sg_len;
764 u32 temp;
765
766 host->using_dma = 0;
767
768 /* If we don't have a channel, we can't do DMA */
769 if (!host->use_dma)
770 return -ENODEV;
771
772 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900773 if (sg_len < 0) {
774 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900775 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900776 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900777
James Hogan03e8cb52011-06-29 09:28:43 +0100778 host->using_dma = 1;
779
Thomas Abraham4a909202012-09-17 18:16:35 +0000780 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500781 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
782 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
783 sg_len);
784
Seungwon Jeon52426892013-08-31 00:13:42 +0900785 /*
786 * Decide the MSIZE and RX/TX Watermark.
787 * If current block size is same with previous size,
788 * no need to update fifoth.
789 */
790 if (host->prev_blksz != data->blksz)
791 dw_mci_adjust_fifoth(host, data);
792
Will Newtonf95f3852011-01-02 01:11:59 -0500793 /* Enable the DMA interface */
794 temp = mci_readl(host, CTRL);
795 temp |= SDMMC_CTRL_DMA_ENABLE;
796 mci_writel(host, CTRL, temp);
797
798 /* Disable RX/TX IRQs, let DMA handle it */
Doug Andersonf8c58c12014-12-02 15:42:47 -0800799 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -0500800 temp = mci_readl(host, INTMASK);
801 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
802 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -0800803 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -0500804
805 host->dma_ops->start(host, sg_len);
806
807 return 0;
808}
809
810static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
811{
Doug Andersonf8c58c12014-12-02 15:42:47 -0800812 unsigned long irqflags;
Will Newtonf95f3852011-01-02 01:11:59 -0500813 u32 temp;
814
815 data->error = -EINPROGRESS;
816
817 WARN_ON(host->data);
818 host->sg = NULL;
819 host->data = data;
820
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900821 if (data->flags & MMC_DATA_READ) {
James Hogan55c5efbc2011-06-29 09:29:58 +0100822 host->dir_status = DW_MCI_RECV_STATUS;
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900823 dw_mci_ctrl_rd_thld(host, data);
824 } else {
James Hogan55c5efbc2011-06-29 09:29:58 +0100825 host->dir_status = DW_MCI_SEND_STATUS;
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900826 }
James Hogan55c5efbc2011-06-29 09:29:58 +0100827
Will Newtonf95f3852011-01-02 01:11:59 -0500828 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +0900829 int flags = SG_MITER_ATOMIC;
830 if (host->data->flags & MMC_DATA_READ)
831 flags |= SG_MITER_TO_SG;
832 else
833 flags |= SG_MITER_FROM_SG;
834
835 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500836 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +0100837 host->part_buf_start = 0;
838 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500839
James Hoganb40af3a2011-06-24 13:54:06 +0100840 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Doug Andersonf8c58c12014-12-02 15:42:47 -0800841
842 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -0500843 temp = mci_readl(host, INTMASK);
844 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
845 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -0800846 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -0500847
848 temp = mci_readl(host, CTRL);
849 temp &= ~SDMMC_CTRL_DMA_ENABLE;
850 mci_writel(host, CTRL, temp);
Seungwon Jeon52426892013-08-31 00:13:42 +0900851
852 /*
853 * Use the initial fifoth_val for PIO mode.
854 * If next issued data may be transfered by DMA mode,
855 * prev_blksz should be invalidated.
856 */
857 mci_writel(host, FIFOTH, host->fifoth_val);
858 host->prev_blksz = 0;
859 } else {
860 /*
861 * Keep the current block size.
862 * It will be used to decide whether to update
863 * fifoth register next time.
864 */
865 host->prev_blksz = data->blksz;
Will Newtonf95f3852011-01-02 01:11:59 -0500866 }
867}
868
869static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
870{
871 struct dw_mci *host = slot->host;
872 unsigned long timeout = jiffies + msecs_to_jiffies(500);
873 unsigned int cmd_status = 0;
874
875 mci_writel(host, CMDARG, arg);
876 wmb();
877 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
878
879 while (time_before(jiffies, timeout)) {
880 cmd_status = mci_readl(host, CMD);
881 if (!(cmd_status & SDMMC_CMD_START))
882 return;
883 }
884 dev_err(&slot->mmc->class_dev,
885 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
886 cmd, arg, cmd_status);
887}
888
Abhilash Kesavanab269122012-11-19 10:26:21 +0530889static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -0500890{
891 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +0900892 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -0500893 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -0700894 u32 clk_en_a;
Doug Anderson01730552014-08-22 19:17:51 +0530895 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
896
897 /* We must continue to set bit 28 in CMD until the change is complete */
898 if (host->state == STATE_WAITING_CMD11_DONE)
899 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
Will Newtonf95f3852011-01-02 01:11:59 -0500900
Doug Andersonfdf492a2013-08-31 00:11:43 +0900901 if (!clock) {
902 mci_writel(host, CLKENA, 0);
Doug Anderson01730552014-08-22 19:17:51 +0530903 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Doug Andersonfdf492a2013-08-31 00:11:43 +0900904 } else if (clock != host->current_speed || force_clkinit) {
905 div = host->bus_hz / clock;
906 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -0500907 /*
908 * move the + 1 after the divide to prevent
909 * over-clocking the card.
910 */
Seungwon Jeone4199902012-05-22 13:01:21 +0900911 div += 1;
912
Doug Andersonfdf492a2013-08-31 00:11:43 +0900913 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500914
Doug Andersonfdf492a2013-08-31 00:11:43 +0900915 if ((clock << div) != slot->__clk_old || force_clkinit)
916 dev_info(&slot->mmc->class_dev,
917 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
918 slot->id, host->bus_hz, clock,
919 div ? ((host->bus_hz / div) >> 1) :
920 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -0500921
922 /* disable clock */
923 mci_writel(host, CLKENA, 0);
924 mci_writel(host, CLKSRC, 0);
925
926 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +0530927 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -0500928
929 /* set clock to desired speed */
930 mci_writel(host, CLKDIV, div);
931
932 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +0530933 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -0500934
Doug Anderson9623b5b2012-07-25 08:33:17 -0700935 /* enable clock; only low power if no SDIO */
936 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Doug Andersonb24c8b22014-12-02 15:42:46 -0800937 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
Doug Anderson9623b5b2012-07-25 08:33:17 -0700938 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
939 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -0500940
941 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +0530942 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -0500943
Doug Andersonfdf492a2013-08-31 00:11:43 +0900944 /* keep the clock with reflecting clock dividor */
945 slot->__clk_old = clock << div;
Will Newtonf95f3852011-01-02 01:11:59 -0500946 }
947
Doug Andersonfdf492a2013-08-31 00:11:43 +0900948 host->current_speed = clock;
949
Will Newtonf95f3852011-01-02 01:11:59 -0500950 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +0900951 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -0500952}
953
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900954static void __dw_mci_start_request(struct dw_mci *host,
955 struct dw_mci_slot *slot,
956 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -0500957{
958 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -0500959 struct mmc_data *data;
960 u32 cmdflags;
961
962 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -0500963
Will Newtonf95f3852011-01-02 01:11:59 -0500964 host->cur_slot = slot;
965 host->mrq = mrq;
966
967 host->pending_events = 0;
968 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +0900969 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500970 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +0900971 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500972
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900973 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -0500974 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +0900975 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -0500976 mci_writel(host, BYTCNT, data->blksz*data->blocks);
977 mci_writel(host, BLKSIZ, data->blksz);
978 }
979
Will Newtonf95f3852011-01-02 01:11:59 -0500980 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
981
982 /* this is the first command, send the initialization clock */
983 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
984 cmdflags |= SDMMC_CMD_INIT;
985
986 if (data) {
987 dw_mci_submit_data(host, data);
988 wmb();
989 }
990
991 dw_mci_start_command(host, cmd, cmdflags);
992
993 if (mrq->stop)
994 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +0900995 else
996 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -0500997}
998
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900999static void dw_mci_start_request(struct dw_mci *host,
1000 struct dw_mci_slot *slot)
1001{
1002 struct mmc_request *mrq = slot->mrq;
1003 struct mmc_command *cmd;
1004
1005 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1006 __dw_mci_start_request(host, slot, cmd);
1007}
1008
James Hogan7456caa2011-06-24 13:55:10 +01001009/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -05001010static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1011 struct mmc_request *mrq)
1012{
1013 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1014 host->state);
1015
Will Newtonf95f3852011-01-02 01:11:59 -05001016 slot->mrq = mrq;
1017
Doug Anderson01730552014-08-22 19:17:51 +05301018 if (host->state == STATE_WAITING_CMD11_DONE) {
1019 dev_warn(&slot->mmc->class_dev,
1020 "Voltage change didn't complete\n");
1021 /*
1022 * this case isn't expected to happen, so we can
1023 * either crash here or just try to continue on
1024 * in the closest possible state
1025 */
1026 host->state = STATE_IDLE;
1027 }
1028
Will Newtonf95f3852011-01-02 01:11:59 -05001029 if (host->state == STATE_IDLE) {
1030 host->state = STATE_SENDING_CMD;
1031 dw_mci_start_request(host, slot);
1032 } else {
1033 list_add_tail(&slot->queue_node, &host->queue);
1034 }
Will Newtonf95f3852011-01-02 01:11:59 -05001035}
1036
1037static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1038{
1039 struct dw_mci_slot *slot = mmc_priv(mmc);
1040 struct dw_mci *host = slot->host;
1041
1042 WARN_ON(slot->mrq);
1043
James Hogan7456caa2011-06-24 13:55:10 +01001044 /*
1045 * The check for card presence and queueing of the request must be
1046 * atomic, otherwise the card could be removed in between and the
1047 * request wouldn't fail until another card was inserted.
1048 */
1049 spin_lock_bh(&host->lock);
1050
Will Newtonf95f3852011-01-02 01:11:59 -05001051 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
James Hogan7456caa2011-06-24 13:55:10 +01001052 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001053 mrq->cmd->error = -ENOMEDIUM;
1054 mmc_request_done(mmc, mrq);
1055 return;
1056 }
1057
Will Newtonf95f3852011-01-02 01:11:59 -05001058 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001059
1060 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001061}
1062
1063static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1064{
1065 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001066 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001067 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301068 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001069
Will Newtonf95f3852011-01-02 01:11:59 -05001070 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001071 case MMC_BUS_WIDTH_4:
1072 slot->ctype = SDMMC_CTYPE_4BIT;
1073 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001074 case MMC_BUS_WIDTH_8:
1075 slot->ctype = SDMMC_CTYPE_8BIT;
1076 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001077 default:
1078 /* set default 1 bit mode */
1079 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001080 }
1081
Seungwon Jeon3f514292012-01-02 16:00:02 +09001082 regs = mci_readl(slot->host, UHS_REG);
1083
Jaehoon Chung41babf72011-02-24 13:46:11 +09001084 /* DDR mode set */
Seungwon Jeoncab3a802014-03-14 21:12:43 +09001085 if (ios->timing == MMC_TIMING_MMC_DDR52)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001086 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001087 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001088 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001089
1090 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001091 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001092
Doug Andersonfdf492a2013-08-31 00:11:43 +09001093 /*
1094 * Use mirror of ios->clock to prevent race with mmc
1095 * core ios update when finding the minimum.
1096 */
1097 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001098
James Hogancb27a842012-10-16 09:43:08 +01001099 if (drv_data && drv_data->set_ios)
1100 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001101
Jaehoon Chungbf7cb222012-11-08 17:35:29 +09001102 /* Slot specific timing and width adjustment */
1103 dw_mci_setup_bus(slot, false);
1104
Doug Anderson01730552014-08-22 19:17:51 +05301105 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1106 slot->host->state = STATE_IDLE;
1107
Will Newtonf95f3852011-01-02 01:11:59 -05001108 switch (ios->power_mode) {
1109 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301110 if (!IS_ERR(mmc->supply.vmmc)) {
1111 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1112 ios->vdd);
1113 if (ret) {
1114 dev_err(slot->host->dev,
1115 "failed to enable vmmc regulator\n");
1116 /*return, if failed turn on vmmc*/
1117 return;
1118 }
1119 }
1120 if (!IS_ERR(mmc->supply.vqmmc) && !slot->host->vqmmc_enabled) {
1121 ret = regulator_enable(mmc->supply.vqmmc);
1122 if (ret < 0)
1123 dev_err(slot->host->dev,
1124 "failed to enable vqmmc regulator\n");
1125 else
1126 slot->host->vqmmc_enabled = true;
1127 }
Will Newtonf95f3852011-01-02 01:11:59 -05001128 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001129 regs = mci_readl(slot->host, PWREN);
1130 regs |= (1 << slot->id);
1131 mci_writel(slot->host, PWREN, regs);
James Hogane6f34e22013-03-12 10:43:32 +00001132 break;
1133 case MMC_POWER_OFF:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301134 if (!IS_ERR(mmc->supply.vmmc))
1135 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1136
1137 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) {
1138 regulator_disable(mmc->supply.vqmmc);
1139 slot->host->vqmmc_enabled = false;
1140 }
1141
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001142 regs = mci_readl(slot->host, PWREN);
1143 regs &= ~(1 << slot->id);
1144 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001145 break;
1146 default:
1147 break;
1148 }
1149}
1150
Doug Anderson01730552014-08-22 19:17:51 +05301151static int dw_mci_card_busy(struct mmc_host *mmc)
1152{
1153 struct dw_mci_slot *slot = mmc_priv(mmc);
1154 u32 status;
1155
1156 /*
1157 * Check the busy bit which is low when DAT[3:0]
1158 * (the data lines) are 0000
1159 */
1160 status = mci_readl(slot->host, STATUS);
1161
1162 return !!(status & SDMMC_STATUS_BUSY);
1163}
1164
1165static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1166{
1167 struct dw_mci_slot *slot = mmc_priv(mmc);
1168 struct dw_mci *host = slot->host;
1169 u32 uhs;
1170 u32 v18 = SDMMC_UHS_18V << slot->id;
1171 int min_uv, max_uv;
1172 int ret;
1173
1174 /*
1175 * Program the voltage. Note that some instances of dw_mmc may use
1176 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1177 * does no harm but you need to set the regulator directly. Try both.
1178 */
1179 uhs = mci_readl(host, UHS_REG);
1180 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1181 min_uv = 2700000;
1182 max_uv = 3600000;
1183 uhs &= ~v18;
1184 } else {
1185 min_uv = 1700000;
1186 max_uv = 1950000;
1187 uhs |= v18;
1188 }
1189 if (!IS_ERR(mmc->supply.vqmmc)) {
1190 ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
1191
1192 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001193 dev_dbg(&mmc->class_dev,
Doug Anderson01730552014-08-22 19:17:51 +05301194 "Regulator set error %d: %d - %d\n",
1195 ret, min_uv, max_uv);
1196 return ret;
1197 }
1198 }
1199 mci_writel(host, UHS_REG, uhs);
1200
1201 return 0;
1202}
1203
Will Newtonf95f3852011-01-02 01:11:59 -05001204static int dw_mci_get_ro(struct mmc_host *mmc)
1205{
1206 int read_only;
1207 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001208 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001209
1210 /* Use platform get_ro function, else try on board write protect */
Jaehoon Chung26375b52014-08-07 16:37:58 +09001211 if ((slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT) ||
1212 (slot->host->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT))
Thomas Abrahamb4967aa2012-09-17 18:16:39 +00001213 read_only = 0;
Jaehoon Chung9795a842014-03-03 11:36:46 +09001214 else if (!IS_ERR_VALUE(gpio_ro))
1215 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001216 else
1217 read_only =
1218 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1219
1220 dev_dbg(&mmc->class_dev, "card is %s\n",
1221 read_only ? "read-only" : "read-write");
1222
1223 return read_only;
1224}
1225
1226static int dw_mci_get_cd(struct mmc_host *mmc)
1227{
1228 int present;
1229 struct dw_mci_slot *slot = mmc_priv(mmc);
1230 struct dw_mci_board *brd = slot->host->pdata;
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001231 struct dw_mci *host = slot->host;
1232 int gpio_cd = mmc_gpio_get_cd(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001233
1234 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001235 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
1236 present = 1;
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001237 else if (!IS_ERR_VALUE(gpio_cd))
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001238 present = gpio_cd;
Will Newtonf95f3852011-01-02 01:11:59 -05001239 else
1240 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1241 == 0 ? 1 : 0;
1242
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001243 spin_lock_bh(&host->lock);
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001244 if (present) {
1245 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001246 dev_dbg(&mmc->class_dev, "card is present\n");
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001247 } else {
1248 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001249 dev_dbg(&mmc->class_dev, "card is not present\n");
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001250 }
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001251 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001252
1253 return present;
1254}
1255
Doug Andersonb24c8b22014-12-02 15:42:46 -08001256static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
Doug Anderson9623b5b2012-07-25 08:33:17 -07001257{
Doug Andersonb24c8b22014-12-02 15:42:46 -08001258 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson9623b5b2012-07-25 08:33:17 -07001259 struct dw_mci *host = slot->host;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001260
Doug Andersonb24c8b22014-12-02 15:42:46 -08001261 /*
1262 * Low power mode will stop the card clock when idle. According to the
1263 * description of the CLKENA register we should disable low power mode
1264 * for SDIO cards if we need SDIO interrupts to work.
1265 */
1266 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1267 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1268 u32 clk_en_a_old;
1269 u32 clk_en_a;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001270
Doug Andersonb24c8b22014-12-02 15:42:46 -08001271 clk_en_a_old = mci_readl(host, CLKENA);
1272
1273 if (card->type == MMC_TYPE_SDIO ||
1274 card->type == MMC_TYPE_SD_COMBO) {
1275 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1276 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1277 } else {
1278 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1279 clk_en_a = clk_en_a_old | clken_low_pwr;
1280 }
1281
1282 if (clk_en_a != clk_en_a_old) {
1283 mci_writel(host, CLKENA, clk_en_a);
1284 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1285 SDMMC_CMD_PRV_DAT_WAIT, 0);
1286 }
Doug Anderson9623b5b2012-07-25 08:33:17 -07001287 }
1288}
1289
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301290static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1291{
1292 struct dw_mci_slot *slot = mmc_priv(mmc);
1293 struct dw_mci *host = slot->host;
Doug Andersonf8c58c12014-12-02 15:42:47 -08001294 unsigned long irqflags;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301295 u32 int_mask;
1296
Doug Andersonf8c58c12014-12-02 15:42:47 -08001297 spin_lock_irqsave(&host->irq_lock, irqflags);
1298
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301299 /* Enable/disable Slot Specific SDIO interrupt */
1300 int_mask = mci_readl(host, INTMASK);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001301 if (enb)
1302 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1303 else
1304 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1305 mci_writel(host, INTMASK, int_mask);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001306
1307 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301308}
1309
Seungwon Jeon0976f162013-08-31 00:12:42 +09001310static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1311{
1312 struct dw_mci_slot *slot = mmc_priv(mmc);
1313 struct dw_mci *host = slot->host;
1314 const struct dw_mci_drv_data *drv_data = host->drv_data;
1315 struct dw_mci_tuning_data tuning_data;
1316 int err = -ENOSYS;
1317
1318 if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1319 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
1320 tuning_data.blk_pattern = tuning_blk_pattern_8bit;
1321 tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
1322 } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1323 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1324 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1325 } else {
1326 return -EINVAL;
1327 }
1328 } else if (opcode == MMC_SEND_TUNING_BLOCK) {
1329 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1330 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1331 } else {
1332 dev_err(host->dev,
1333 "Undefined command(%d) for tuning\n", opcode);
1334 return -EINVAL;
1335 }
1336
1337 if (drv_data && drv_data->execute_tuning)
1338 err = drv_data->execute_tuning(slot, opcode, &tuning_data);
1339 return err;
1340}
1341
Will Newtonf95f3852011-01-02 01:11:59 -05001342static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301343 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001344 .pre_req = dw_mci_pre_req,
1345 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301346 .set_ios = dw_mci_set_ios,
1347 .get_ro = dw_mci_get_ro,
1348 .get_cd = dw_mci_get_cd,
1349 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001350 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301351 .card_busy = dw_mci_card_busy,
1352 .start_signal_voltage_switch = dw_mci_switch_voltage,
Doug Andersonb24c8b22014-12-02 15:42:46 -08001353 .init_card = dw_mci_init_card,
Will Newtonf95f3852011-01-02 01:11:59 -05001354};
1355
1356static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1357 __releases(&host->lock)
1358 __acquires(&host->lock)
1359{
1360 struct dw_mci_slot *slot;
1361 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1362
1363 WARN_ON(host->cmd || host->data);
1364
1365 host->cur_slot->mrq = NULL;
1366 host->mrq = NULL;
1367 if (!list_empty(&host->queue)) {
1368 slot = list_entry(host->queue.next,
1369 struct dw_mci_slot, queue_node);
1370 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001371 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001372 mmc_hostname(slot->mmc));
1373 host->state = STATE_SENDING_CMD;
1374 dw_mci_start_request(host, slot);
1375 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001376 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301377
1378 if (host->state == STATE_SENDING_CMD11)
1379 host->state = STATE_WAITING_CMD11_DONE;
1380 else
1381 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001382 }
1383
1384 spin_unlock(&host->lock);
1385 mmc_request_done(prev_mmc, mrq);
1386 spin_lock(&host->lock);
1387}
1388
Seungwon Jeone352c812013-08-31 00:14:17 +09001389static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001390{
1391 u32 status = host->cmd_status;
1392
1393 host->cmd_status = 0;
1394
1395 /* Read the response from the card (up to 16 bytes) */
1396 if (cmd->flags & MMC_RSP_PRESENT) {
1397 if (cmd->flags & MMC_RSP_136) {
1398 cmd->resp[3] = mci_readl(host, RESP0);
1399 cmd->resp[2] = mci_readl(host, RESP1);
1400 cmd->resp[1] = mci_readl(host, RESP2);
1401 cmd->resp[0] = mci_readl(host, RESP3);
1402 } else {
1403 cmd->resp[0] = mci_readl(host, RESP0);
1404 cmd->resp[1] = 0;
1405 cmd->resp[2] = 0;
1406 cmd->resp[3] = 0;
1407 }
1408 }
1409
1410 if (status & SDMMC_INT_RTO)
1411 cmd->error = -ETIMEDOUT;
1412 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1413 cmd->error = -EILSEQ;
1414 else if (status & SDMMC_INT_RESP_ERR)
1415 cmd->error = -EIO;
1416 else
1417 cmd->error = 0;
1418
1419 if (cmd->error) {
1420 /* newer ip versions need a delay between retries */
1421 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1422 mdelay(20);
Will Newtonf95f3852011-01-02 01:11:59 -05001423 }
Seungwon Jeone352c812013-08-31 00:14:17 +09001424
1425 return cmd->error;
1426}
1427
1428static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1429{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001430 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001431
1432 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1433 if (status & SDMMC_INT_DRTO) {
1434 data->error = -ETIMEDOUT;
1435 } else if (status & SDMMC_INT_DCRC) {
1436 data->error = -EILSEQ;
1437 } else if (status & SDMMC_INT_EBE) {
1438 if (host->dir_status ==
1439 DW_MCI_SEND_STATUS) {
1440 /*
1441 * No data CRC status was returned.
1442 * The number of bytes transferred
1443 * will be exaggerated in PIO mode.
1444 */
1445 data->bytes_xfered = 0;
1446 data->error = -ETIMEDOUT;
1447 } else if (host->dir_status ==
1448 DW_MCI_RECV_STATUS) {
1449 data->error = -EIO;
1450 }
1451 } else {
1452 /* SDMMC_INT_SBE is included */
1453 data->error = -EIO;
1454 }
1455
Doug Andersone6cc0122014-04-22 16:51:21 -07001456 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001457
1458 /*
1459 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001460 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001461 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001462 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001463 } else {
1464 data->bytes_xfered = data->blocks * data->blksz;
1465 data->error = 0;
1466 }
1467
1468 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001469}
1470
1471static void dw_mci_tasklet_func(unsigned long priv)
1472{
1473 struct dw_mci *host = (struct dw_mci *)priv;
1474 struct mmc_data *data;
1475 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001476 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001477 enum dw_mci_state state;
1478 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001479 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001480
1481 spin_lock(&host->lock);
1482
1483 state = host->state;
1484 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001485 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001486
1487 do {
1488 prev_state = state;
1489
1490 switch (state) {
1491 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301492 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001493 break;
1494
Doug Anderson01730552014-08-22 19:17:51 +05301495 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001496 case STATE_SENDING_CMD:
1497 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1498 &host->pending_events))
1499 break;
1500
1501 cmd = host->cmd;
1502 host->cmd = NULL;
1503 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001504 err = dw_mci_command_complete(host, cmd);
1505 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001506 prev_state = state = STATE_SENDING_CMD;
1507 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001508 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001509 goto unlock;
1510 }
1511
Seungwon Jeone352c812013-08-31 00:14:17 +09001512 if (cmd->data && err) {
Seungwon Jeon71abb132013-08-31 00:13:59 +09001513 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001514 send_stop_abort(host, data);
1515 state = STATE_SENDING_STOP;
1516 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001517 }
1518
Seungwon Jeone352c812013-08-31 00:14:17 +09001519 if (!cmd->data || err) {
1520 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001521 goto unlock;
1522 }
1523
1524 prev_state = state = STATE_SENDING_DATA;
1525 /* fall through */
1526
1527 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001528 /*
1529 * We could get a data error and never a transfer
1530 * complete so we'd better check for it here.
1531 *
1532 * Note that we don't really care if we also got a
1533 * transfer complete; stopping the DMA and sending an
1534 * abort won't hurt.
1535 */
Will Newtonf95f3852011-01-02 01:11:59 -05001536 if (test_and_clear_bit(EVENT_DATA_ERROR,
1537 &host->pending_events)) {
1538 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001539 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001540 state = STATE_DATA_ERROR;
1541 break;
1542 }
1543
1544 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1545 &host->pending_events))
1546 break;
1547
1548 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07001549
1550 /*
1551 * Handle an EVENT_DATA_ERROR that might have shown up
1552 * before the transfer completed. This might not have
1553 * been caught by the check above because the interrupt
1554 * could have gone off between the previous check and
1555 * the check for transfer complete.
1556 *
1557 * Technically this ought not be needed assuming we
1558 * get a DATA_COMPLETE eventually (we'll notice the
1559 * error and end the request), but it shouldn't hurt.
1560 *
1561 * This has the advantage of sending the stop command.
1562 */
1563 if (test_and_clear_bit(EVENT_DATA_ERROR,
1564 &host->pending_events)) {
1565 dw_mci_stop_dma(host);
1566 send_stop_abort(host, data);
1567 state = STATE_DATA_ERROR;
1568 break;
1569 }
Will Newtonf95f3852011-01-02 01:11:59 -05001570 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07001571
Will Newtonf95f3852011-01-02 01:11:59 -05001572 /* fall through */
1573
1574 case STATE_DATA_BUSY:
1575 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1576 &host->pending_events))
1577 break;
1578
1579 host->data = NULL;
1580 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001581 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001582
Seungwon Jeone352c812013-08-31 00:14:17 +09001583 if (!err) {
1584 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05301585 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09001586 data->stop->error = 0;
1587 dw_mci_request_end(host, mrq);
1588 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05001589 }
Will Newtonf95f3852011-01-02 01:11:59 -05001590
Seungwon Jeon90c21432013-08-31 00:14:05 +09001591 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09001592 if (data->stop)
1593 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001594 } else {
1595 /*
1596 * If we don't have a command complete now we'll
1597 * never get one since we just reset everything;
1598 * better end the request.
1599 *
1600 * If we do have a command complete we'll fall
1601 * through to the SENDING_STOP command and
1602 * everything will be peachy keen.
1603 */
1604 if (!test_bit(EVENT_CMD_COMPLETE,
1605 &host->pending_events)) {
1606 host->cmd = NULL;
1607 dw_mci_request_end(host, mrq);
1608 goto unlock;
1609 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09001610 }
Seungwon Jeone352c812013-08-31 00:14:17 +09001611
1612 /*
1613 * If err has non-zero,
1614 * stop-abort command has been already issued.
1615 */
1616 prev_state = state = STATE_SENDING_STOP;
1617
Will Newtonf95f3852011-01-02 01:11:59 -05001618 /* fall through */
1619
1620 case STATE_SENDING_STOP:
1621 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1622 &host->pending_events))
1623 break;
1624
Seungwon Jeon71abb132013-08-31 00:13:59 +09001625 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09001626 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07001627 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09001628
Will Newtonf95f3852011-01-02 01:11:59 -05001629 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001630 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09001631
Seungwon Jeone352c812013-08-31 00:14:17 +09001632 if (mrq->stop)
1633 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001634 else
1635 host->cmd_status = 0;
1636
Seungwon Jeone352c812013-08-31 00:14:17 +09001637 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001638 goto unlock;
1639
1640 case STATE_DATA_ERROR:
1641 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1642 &host->pending_events))
1643 break;
1644
1645 state = STATE_DATA_BUSY;
1646 break;
1647 }
1648 } while (state != prev_state);
1649
1650 host->state = state;
1651unlock:
1652 spin_unlock(&host->lock);
1653
1654}
1655
James Hogan34b664a2011-06-24 13:57:56 +01001656/* push final bytes to part_buf, only use during push */
1657static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1658{
1659 memcpy((void *)&host->part_buf, buf, cnt);
1660 host->part_buf_count = cnt;
1661}
1662
1663/* append bytes to part_buf, only use during push */
1664static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1665{
1666 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1667 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1668 host->part_buf_count += cnt;
1669 return cnt;
1670}
1671
1672/* pull first bytes from part_buf, only use during pull */
1673static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1674{
1675 cnt = min(cnt, (int)host->part_buf_count);
1676 if (cnt) {
1677 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1678 cnt);
1679 host->part_buf_count -= cnt;
1680 host->part_buf_start += cnt;
1681 }
1682 return cnt;
1683}
1684
1685/* pull final bytes from the part_buf, assuming it's just been filled */
1686static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1687{
1688 memcpy(buf, &host->part_buf, cnt);
1689 host->part_buf_start = cnt;
1690 host->part_buf_count = (1 << host->data_shift) - cnt;
1691}
1692
Will Newtonf95f3852011-01-02 01:11:59 -05001693static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1694{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001695 struct mmc_data *data = host->data;
1696 int init_cnt = cnt;
1697
James Hogan34b664a2011-06-24 13:57:56 +01001698 /* try and push anything in the part_buf */
1699 if (unlikely(host->part_buf_count)) {
1700 int len = dw_mci_push_part_bytes(host, buf, cnt);
1701 buf += len;
1702 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001703 if (host->part_buf_count == 2) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001704 mci_writew(host, DATA(host->data_offset),
1705 host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01001706 host->part_buf_count = 0;
1707 }
1708 }
1709#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1710 if (unlikely((unsigned long)buf & 0x1)) {
1711 while (cnt >= 2) {
1712 u16 aligned_buf[64];
1713 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1714 int items = len >> 1;
1715 int i;
1716 /* memcpy from input buffer into aligned buffer */
1717 memcpy(aligned_buf, buf, len);
1718 buf += len;
1719 cnt -= len;
1720 /* push data from aligned buffer into fifo */
1721 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001722 mci_writew(host, DATA(host->data_offset),
1723 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001724 }
1725 } else
1726#endif
1727 {
1728 u16 *pdata = buf;
1729 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001730 mci_writew(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001731 buf = pdata;
1732 }
1733 /* put anything remaining in the part_buf */
1734 if (cnt) {
1735 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001736 /* Push data if we have reached the expected data length */
1737 if ((data->bytes_xfered + init_cnt) ==
1738 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001739 mci_writew(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001740 host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05001741 }
1742}
1743
1744static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1745{
James Hogan34b664a2011-06-24 13:57:56 +01001746#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1747 if (unlikely((unsigned long)buf & 0x1)) {
1748 while (cnt >= 2) {
1749 /* pull data from fifo into aligned buffer */
1750 u16 aligned_buf[64];
1751 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1752 int items = len >> 1;
1753 int i;
1754 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001755 aligned_buf[i] = mci_readw(host,
1756 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001757 /* memcpy from aligned buffer into output buffer */
1758 memcpy(buf, aligned_buf, len);
1759 buf += len;
1760 cnt -= len;
1761 }
1762 } else
1763#endif
1764 {
1765 u16 *pdata = buf;
1766 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001767 *pdata++ = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001768 buf = pdata;
1769 }
1770 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001771 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001772 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001773 }
1774}
1775
1776static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1777{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001778 struct mmc_data *data = host->data;
1779 int init_cnt = cnt;
1780
James Hogan34b664a2011-06-24 13:57:56 +01001781 /* try and push anything in the part_buf */
1782 if (unlikely(host->part_buf_count)) {
1783 int len = dw_mci_push_part_bytes(host, buf, cnt);
1784 buf += len;
1785 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001786 if (host->part_buf_count == 4) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001787 mci_writel(host, DATA(host->data_offset),
1788 host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01001789 host->part_buf_count = 0;
1790 }
1791 }
1792#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1793 if (unlikely((unsigned long)buf & 0x3)) {
1794 while (cnt >= 4) {
1795 u32 aligned_buf[32];
1796 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1797 int items = len >> 2;
1798 int i;
1799 /* memcpy from input buffer into aligned buffer */
1800 memcpy(aligned_buf, buf, len);
1801 buf += len;
1802 cnt -= len;
1803 /* push data from aligned buffer into fifo */
1804 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001805 mci_writel(host, DATA(host->data_offset),
1806 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001807 }
1808 } else
1809#endif
1810 {
1811 u32 *pdata = buf;
1812 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001813 mci_writel(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001814 buf = pdata;
1815 }
1816 /* put anything remaining in the part_buf */
1817 if (cnt) {
1818 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001819 /* Push data if we have reached the expected data length */
1820 if ((data->bytes_xfered + init_cnt) ==
1821 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001822 mci_writel(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001823 host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05001824 }
1825}
1826
1827static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1828{
James Hogan34b664a2011-06-24 13:57:56 +01001829#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1830 if (unlikely((unsigned long)buf & 0x3)) {
1831 while (cnt >= 4) {
1832 /* pull data from fifo into aligned buffer */
1833 u32 aligned_buf[32];
1834 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1835 int items = len >> 2;
1836 int i;
1837 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001838 aligned_buf[i] = mci_readl(host,
1839 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001840 /* memcpy from aligned buffer into output buffer */
1841 memcpy(buf, aligned_buf, len);
1842 buf += len;
1843 cnt -= len;
1844 }
1845 } else
1846#endif
1847 {
1848 u32 *pdata = buf;
1849 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001850 *pdata++ = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001851 buf = pdata;
1852 }
1853 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001854 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001855 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001856 }
1857}
1858
1859static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
1860{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001861 struct mmc_data *data = host->data;
1862 int init_cnt = cnt;
1863
James Hogan34b664a2011-06-24 13:57:56 +01001864 /* try and push anything in the part_buf */
1865 if (unlikely(host->part_buf_count)) {
1866 int len = dw_mci_push_part_bytes(host, buf, cnt);
1867 buf += len;
1868 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09001869
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001870 if (host->part_buf_count == 8) {
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09001871 mci_writeq(host, DATA(host->data_offset),
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001872 host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01001873 host->part_buf_count = 0;
1874 }
1875 }
1876#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1877 if (unlikely((unsigned long)buf & 0x7)) {
1878 while (cnt >= 8) {
1879 u64 aligned_buf[16];
1880 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1881 int items = len >> 3;
1882 int i;
1883 /* memcpy from input buffer into aligned buffer */
1884 memcpy(aligned_buf, buf, len);
1885 buf += len;
1886 cnt -= len;
1887 /* push data from aligned buffer into fifo */
1888 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001889 mci_writeq(host, DATA(host->data_offset),
1890 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001891 }
1892 } else
1893#endif
1894 {
1895 u64 *pdata = buf;
1896 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001897 mci_writeq(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001898 buf = pdata;
1899 }
1900 /* put anything remaining in the part_buf */
1901 if (cnt) {
1902 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001903 /* Push data if we have reached the expected data length */
1904 if ((data->bytes_xfered + init_cnt) ==
1905 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001906 mci_writeq(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001907 host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05001908 }
1909}
1910
1911static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
1912{
James Hogan34b664a2011-06-24 13:57:56 +01001913#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1914 if (unlikely((unsigned long)buf & 0x7)) {
1915 while (cnt >= 8) {
1916 /* pull data from fifo into aligned buffer */
1917 u64 aligned_buf[16];
1918 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1919 int items = len >> 3;
1920 int i;
1921 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001922 aligned_buf[i] = mci_readq(host,
1923 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001924 /* memcpy from aligned buffer into output buffer */
1925 memcpy(buf, aligned_buf, len);
1926 buf += len;
1927 cnt -= len;
1928 }
1929 } else
1930#endif
1931 {
1932 u64 *pdata = buf;
1933 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001934 *pdata++ = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001935 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05001936 }
James Hogan34b664a2011-06-24 13:57:56 +01001937 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001938 host->part_buf = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001939 dw_mci_pull_final_bytes(host, buf, cnt);
1940 }
1941}
1942
1943static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
1944{
1945 int len;
1946
1947 /* get remaining partial bytes */
1948 len = dw_mci_pull_part_bytes(host, buf, cnt);
1949 if (unlikely(len == cnt))
1950 return;
1951 buf += len;
1952 cnt -= len;
1953
1954 /* get the rest of the data */
1955 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001956}
1957
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001958static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05001959{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001960 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1961 void *buf;
1962 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001963 struct mmc_data *data = host->data;
1964 int shift = host->data_shift;
1965 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001966 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001967 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05001968
1969 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001970 if (!sg_miter_next(sg_miter))
1971 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05001972
Imre Deak4225fc82013-02-27 17:02:57 -08001973 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001974 buf = sg_miter->addr;
1975 remain = sg_miter->length;
1976 offset = 0;
1977
1978 do {
1979 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
1980 << shift) + host->part_buf_count;
1981 len = min(remain, fcnt);
1982 if (!len)
1983 break;
1984 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001985 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05001986 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001987 remain -= len;
1988 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05001989
Seungwon Jeone74f3a92012-08-01 09:30:46 +09001990 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001991 status = mci_readl(host, MINTSTS);
1992 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001993 /* if the RXDR is ready read again */
1994 } while ((status & SDMMC_INT_RXDR) ||
1995 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001996
1997 if (!remain) {
1998 if (!sg_miter_next(sg_miter))
1999 goto done;
2000 sg_miter->consumed = 0;
2001 }
2002 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002003 return;
2004
2005done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002006 sg_miter_stop(sg_miter);
2007 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05002008 smp_wmb();
2009 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2010}
2011
2012static void dw_mci_write_data_pio(struct dw_mci *host)
2013{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002014 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2015 void *buf;
2016 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002017 struct mmc_data *data = host->data;
2018 int shift = host->data_shift;
2019 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002020 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002021 unsigned int fifo_depth = host->fifo_depth;
2022 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002023
2024 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002025 if (!sg_miter_next(sg_miter))
2026 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002027
Imre Deak4225fc82013-02-27 17:02:57 -08002028 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002029 buf = sg_miter->addr;
2030 remain = sg_miter->length;
2031 offset = 0;
2032
2033 do {
2034 fcnt = ((fifo_depth -
2035 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2036 << shift) - host->part_buf_count;
2037 len = min(remain, fcnt);
2038 if (!len)
2039 break;
2040 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002041 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002042 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002043 remain -= len;
2044 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002045
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002046 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002047 status = mci_readl(host, MINTSTS);
2048 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002049 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002050
2051 if (!remain) {
2052 if (!sg_miter_next(sg_miter))
2053 goto done;
2054 sg_miter->consumed = 0;
2055 }
2056 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002057 return;
2058
2059done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002060 sg_miter_stop(sg_miter);
2061 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05002062 smp_wmb();
2063 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2064}
2065
2066static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2067{
2068 if (!host->cmd_status)
2069 host->cmd_status = status;
2070
2071 smp_wmb();
2072
2073 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2074 tasklet_schedule(&host->tasklet);
2075}
2076
Doug Anderson6130e7a2014-10-14 09:33:09 -07002077static void dw_mci_handle_cd(struct dw_mci *host)
2078{
2079 int i;
2080
2081 for (i = 0; i < host->num_slots; i++) {
2082 struct dw_mci_slot *slot = host->slot[i];
2083
2084 if (!slot)
2085 continue;
2086
2087 if (slot->mmc->ops->card_event)
2088 slot->mmc->ops->card_event(slot->mmc);
2089 mmc_detect_change(slot->mmc,
2090 msecs_to_jiffies(host->pdata->detect_delay_ms));
2091 }
2092}
2093
Will Newtonf95f3852011-01-02 01:11:59 -05002094static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2095{
2096 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002097 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302098 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05002099
Markos Chandras1fb5f682013-03-12 10:53:11 +00002100 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2101
Doug Anderson476d79f2013-07-09 13:04:40 -07002102 /*
2103 * DTO fix - version 2.10a and below, and only if internal DMA
2104 * is configured.
2105 */
2106 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2107 if (!pending &&
2108 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2109 pending |= SDMMC_INT_DATA_OVER;
2110 }
2111
Markos Chandras1fb5f682013-03-12 10:53:11 +00002112 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302113 /* Check volt switch first, since it can look like an error */
2114 if ((host->state == STATE_SENDING_CMD11) &&
2115 (pending & SDMMC_INT_VOLT_SWITCH)) {
2116 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2117 pending &= ~SDMMC_INT_VOLT_SWITCH;
2118 dw_mci_cmd_interrupt(host, pending);
2119 }
2120
Will Newtonf95f3852011-01-02 01:11:59 -05002121 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2122 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002123 host->cmd_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05002124 smp_wmb();
2125 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002126 }
2127
2128 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2129 /* if there is an error report DATA_ERROR */
2130 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002131 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05002132 smp_wmb();
2133 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002134 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002135 }
2136
2137 if (pending & SDMMC_INT_DATA_OVER) {
2138 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2139 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002140 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05002141 smp_wmb();
2142 if (host->dir_status == DW_MCI_RECV_STATUS) {
2143 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002144 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002145 }
2146 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2147 tasklet_schedule(&host->tasklet);
2148 }
2149
2150 if (pending & SDMMC_INT_RXDR) {
2151 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002152 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002153 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002154 }
2155
2156 if (pending & SDMMC_INT_TXDR) {
2157 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002158 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002159 dw_mci_write_data_pio(host);
2160 }
2161
2162 if (pending & SDMMC_INT_CMD_DONE) {
2163 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002164 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002165 }
2166
2167 if (pending & SDMMC_INT_CD) {
2168 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002169 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002170 }
2171
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302172 /* Handle SDIO Interrupts */
2173 for (i = 0; i < host->num_slots; i++) {
2174 struct dw_mci_slot *slot = host->slot[i];
Addy Ke76756232014-11-04 22:03:09 +08002175 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2176 mci_writel(host, RINTSTS,
2177 SDMMC_INT_SDIO(slot->sdio_id));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302178 mmc_signal_sdio_irq(slot->mmc);
2179 }
2180 }
2181
Markos Chandras1fb5f682013-03-12 10:53:11 +00002182 }
Will Newtonf95f3852011-01-02 01:11:59 -05002183
2184#ifdef CONFIG_MMC_DW_IDMAC
2185 /* Handle DMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002186 if (host->dma_64bit_address == 1) {
2187 pending = mci_readl(host, IDSTS64);
2188 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2189 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2190 SDMMC_IDMAC_INT_RI);
2191 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2192 host->dma_ops->complete(host);
2193 }
2194 } else {
2195 pending = mci_readl(host, IDSTS);
2196 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2197 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2198 SDMMC_IDMAC_INT_RI);
2199 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2200 host->dma_ops->complete(host);
2201 }
Will Newtonf95f3852011-01-02 01:11:59 -05002202 }
2203#endif
2204
2205 return IRQ_HANDLED;
2206}
2207
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002208#ifdef CONFIG_OF
2209/* given a slot id, find out the device node representing that slot */
2210static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
2211{
2212 struct device_node *np;
2213 const __be32 *addr;
2214 int len;
2215
2216 if (!dev || !dev->of_node)
2217 return NULL;
2218
2219 for_each_child_of_node(dev->of_node, np) {
2220 addr = of_get_property(np, "reg", &len);
2221 if (!addr || (len < sizeof(int)))
2222 continue;
2223 if (be32_to_cpup(addr) == slot)
2224 return np;
2225 }
2226 return NULL;
2227}
2228
Doug Andersona70aaa62013-01-11 17:03:50 +00002229static struct dw_mci_of_slot_quirks {
2230 char *quirk;
2231 int id;
2232} of_slot_quirks[] = {
2233 {
2234 .quirk = "disable-wp",
2235 .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
2236 },
2237};
2238
2239static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2240{
2241 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
2242 int quirks = 0;
2243 int idx;
2244
2245 /* get quirks */
2246 for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
Jaehoon Chung26375b52014-08-07 16:37:58 +09002247 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL)) {
2248 dev_warn(dev, "Slot quirk %s is deprecated\n",
2249 of_slot_quirks[idx].quirk);
Doug Andersona70aaa62013-01-11 17:03:50 +00002250 quirks |= of_slot_quirks[idx].id;
Jaehoon Chung26375b52014-08-07 16:37:58 +09002251 }
Doug Andersona70aaa62013-01-11 17:03:50 +00002252
2253 return quirks;
2254}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002255#else /* CONFIG_OF */
Doug Andersona70aaa62013-01-11 17:03:50 +00002256static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2257{
2258 return 0;
2259}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002260#endif /* CONFIG_OF */
2261
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002262static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002263{
2264 struct mmc_host *mmc;
2265 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002266 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002267 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002268 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002269
Thomas Abraham4a909202012-09-17 18:16:35 +00002270 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002271 if (!mmc)
2272 return -ENOMEM;
2273
2274 slot = mmc_priv(mmc);
2275 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002276 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002277 slot->mmc = mmc;
2278 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002279 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002280
Doug Andersona70aaa62013-01-11 17:03:50 +00002281 slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
2282
Will Newtonf95f3852011-01-02 01:11:59 -05002283 mmc->ops = &dw_mci_ops;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002284 if (of_property_read_u32_array(host->dev->of_node,
2285 "clock-freq-min-max", freq, 2)) {
2286 mmc->f_min = DW_MCI_FREQ_MIN;
2287 mmc->f_max = DW_MCI_FREQ_MAX;
2288 } else {
2289 mmc->f_min = freq[0];
2290 mmc->f_max = freq[1];
2291 }
Will Newtonf95f3852011-01-02 01:11:59 -05002292
Yuvaraj CD51da2242014-08-22 19:17:50 +05302293 /*if there are external regulators, get them*/
2294 ret = mmc_regulator_get_supply(mmc);
2295 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002296 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302297
2298 if (!mmc->ocr_avail)
2299 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002300
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002301 if (host->pdata->caps)
2302 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002303
Abhilash Kesavanab269122012-11-19 10:26:21 +05302304 if (host->pdata->pm_caps)
2305 mmc->pm_caps = host->pdata->pm_caps;
2306
Thomas Abraham800d78b2012-09-17 18:16:42 +00002307 if (host->dev->of_node) {
2308 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2309 if (ctrl_id < 0)
2310 ctrl_id = 0;
2311 } else {
2312 ctrl_id = to_platform_device(host->dev)->id;
2313 }
James Hogancb27a842012-10-16 09:43:08 +01002314 if (drv_data && drv_data->caps)
2315 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002316
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002317 if (host->pdata->caps2)
2318 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002319
Doug Anderson3cf890f2014-08-25 11:19:04 -07002320 ret = mmc_of_parse(mmc);
2321 if (ret)
2322 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002323
Will Newtonf95f3852011-01-02 01:11:59 -05002324 if (host->pdata->blk_settings) {
2325 mmc->max_segs = host->pdata->blk_settings->max_segs;
2326 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
2327 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
2328 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
2329 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
2330 } else {
2331 /* Useful defaults if platform data is unset. */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002332#ifdef CONFIG_MMC_DW_IDMAC
2333 mmc->max_segs = host->ring_size;
2334 mmc->max_blk_size = 65536;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002335 mmc->max_seg_size = 0x1000;
Seungwon Jeon1a25b1b2014-12-22 17:42:02 +05302336 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2337 mmc->max_blk_count = mmc->max_req_size / 512;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002338#else
Will Newtonf95f3852011-01-02 01:11:59 -05002339 mmc->max_segs = 64;
2340 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
2341 mmc->max_blk_count = 512;
2342 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2343 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002344#endif /* CONFIG_MMC_DW_IDMAC */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002345 }
Will Newtonf95f3852011-01-02 01:11:59 -05002346
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002347 if (dw_mci_get_cd(mmc))
2348 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2349 else
2350 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2351
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002352 ret = mmc_add_host(mmc);
2353 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002354 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002355
2356#if defined(CONFIG_DEBUG_FS)
2357 dw_mci_init_debugfs(slot);
2358#endif
2359
Will Newtonf95f3852011-01-02 01:11:59 -05002360 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002361
Doug Anderson3cf890f2014-08-25 11:19:04 -07002362err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002363 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302364 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002365}
2366
2367static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2368{
Will Newtonf95f3852011-01-02 01:11:59 -05002369 /* Debugfs stuff is cleaned up by mmc core */
2370 mmc_remove_host(slot->mmc);
2371 slot->host->slot[id] = NULL;
2372 mmc_free_host(slot->mmc);
2373}
2374
2375static void dw_mci_init_dma(struct dw_mci *host)
2376{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002377 int addr_config;
2378 /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
2379 addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
2380
2381 if (addr_config == 1) {
2382 /* host supports IDMAC in 64-bit address mode */
2383 host->dma_64bit_address = 1;
2384 dev_info(host->dev, "IDMAC supports 64-bit address mode.\n");
2385 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2386 dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64));
2387 } else {
2388 /* host supports IDMAC in 32-bit address mode */
2389 host->dma_64bit_address = 0;
2390 dev_info(host->dev, "IDMAC supports 32-bit address mode.\n");
2391 }
2392
Will Newtonf95f3852011-01-02 01:11:59 -05002393 /* Alloc memory for sg translation */
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002394 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
Will Newtonf95f3852011-01-02 01:11:59 -05002395 &host->sg_dma, GFP_KERNEL);
2396 if (!host->sg_cpu) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002397 dev_err(host->dev, "%s: could not alloc DMA memory\n",
Will Newtonf95f3852011-01-02 01:11:59 -05002398 __func__);
2399 goto no_dma;
2400 }
2401
2402 /* Determine which DMA interface to use */
2403#ifdef CONFIG_MMC_DW_IDMAC
2404 host->dma_ops = &dw_mci_idmac_ops;
Seungwon Jeon00956ea2012-09-28 19:13:11 +09002405 dev_info(host->dev, "Using internal DMA controller.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002406#endif
2407
2408 if (!host->dma_ops)
2409 goto no_dma;
2410
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002411 if (host->dma_ops->init && host->dma_ops->start &&
2412 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002413 if (host->dma_ops->init(host)) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002414 dev_err(host->dev, "%s: Unable to initialize "
Will Newtonf95f3852011-01-02 01:11:59 -05002415 "DMA Controller.\n", __func__);
2416 goto no_dma;
2417 }
2418 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002419 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002420 goto no_dma;
2421 }
2422
2423 host->use_dma = 1;
2424 return;
2425
2426no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002427 dev_info(host->dev, "Using PIO mode.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002428 host->use_dma = 0;
2429 return;
2430}
2431
Seungwon Jeon31bff452013-08-31 00:14:23 +09002432static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
Will Newtonf95f3852011-01-02 01:11:59 -05002433{
2434 unsigned long timeout = jiffies + msecs_to_jiffies(500);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002435 u32 ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05002436
Seungwon Jeon31bff452013-08-31 00:14:23 +09002437 ctrl = mci_readl(host, CTRL);
2438 ctrl |= reset;
2439 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05002440
2441 /* wait till resets clear */
2442 do {
2443 ctrl = mci_readl(host, CTRL);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002444 if (!(ctrl & reset))
Will Newtonf95f3852011-01-02 01:11:59 -05002445 return true;
2446 } while (time_before(jiffies, timeout));
2447
Seungwon Jeon31bff452013-08-31 00:14:23 +09002448 dev_err(host->dev,
2449 "Timeout resetting block (ctrl reset %#x)\n",
2450 ctrl & reset);
Will Newtonf95f3852011-01-02 01:11:59 -05002451
2452 return false;
2453}
2454
Sonny Rao3a33a942014-08-04 18:19:50 -07002455static bool dw_mci_reset(struct dw_mci *host)
Seungwon Jeon31bff452013-08-31 00:14:23 +09002456{
Sonny Rao3a33a942014-08-04 18:19:50 -07002457 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2458 bool ret = false;
2459
Seungwon Jeon31bff452013-08-31 00:14:23 +09002460 /*
2461 * Reseting generates a block interrupt, hence setting
2462 * the scatter-gather pointer to NULL.
2463 */
2464 if (host->sg) {
2465 sg_miter_stop(&host->sg_miter);
2466 host->sg = NULL;
2467 }
2468
Sonny Rao3a33a942014-08-04 18:19:50 -07002469 if (host->use_dma)
2470 flags |= SDMMC_CTRL_DMA_RESET;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002471
Sonny Rao3a33a942014-08-04 18:19:50 -07002472 if (dw_mci_ctrl_reset(host, flags)) {
2473 /*
2474 * In all cases we clear the RAWINTS register to clear any
2475 * interrupts.
2476 */
2477 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2478
2479 /* if using dma we wait for dma_req to clear */
2480 if (host->use_dma) {
2481 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2482 u32 status;
2483 do {
2484 status = mci_readl(host, STATUS);
2485 if (!(status & SDMMC_STATUS_DMA_REQ))
2486 break;
2487 cpu_relax();
2488 } while (time_before(jiffies, timeout));
2489
2490 if (status & SDMMC_STATUS_DMA_REQ) {
2491 dev_err(host->dev,
2492 "%s: Timeout waiting for dma_req to "
2493 "clear during reset\n", __func__);
2494 goto ciu_out;
2495 }
2496
2497 /* when using DMA next we reset the fifo again */
2498 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2499 goto ciu_out;
2500 }
2501 } else {
2502 /* if the controller reset bit did clear, then set clock regs */
2503 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
2504 dev_err(host->dev, "%s: fifo/dma reset bits didn't "
2505 "clear but ciu was reset, doing clock update\n",
2506 __func__);
2507 goto ciu_out;
2508 }
2509 }
2510
2511#if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
2512 /* It is also recommended that we reset and reprogram idmac */
2513 dw_mci_idmac_reset(host);
2514#endif
2515
2516 ret = true;
2517
2518ciu_out:
2519 /* After a CTRL reset we need to have CIU set clock registers */
2520 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2521
2522 return ret;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002523}
2524
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002525#ifdef CONFIG_OF
2526static struct dw_mci_of_quirks {
2527 char *quirk;
2528 int id;
2529} of_quirks[] = {
2530 {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002531 .quirk = "broken-cd",
2532 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
Jaehoon Chung26375b52014-08-07 16:37:58 +09002533 }, {
2534 .quirk = "disable-wp",
2535 .id = DW_MCI_QUIRK_NO_WRITE_PROTECT,
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002536 },
2537};
2538
2539static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2540{
2541 struct dw_mci_board *pdata;
2542 struct device *dev = host->dev;
2543 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002544 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002545 int idx, ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002546 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002547
2548 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2549 if (!pdata) {
2550 dev_err(dev, "could not allocate memory for pdata\n");
2551 return ERR_PTR(-ENOMEM);
2552 }
2553
2554 /* find out number of slots supported */
2555 if (of_property_read_u32(dev->of_node, "num-slots",
2556 &pdata->num_slots)) {
2557 dev_info(dev, "num-slots property not found, "
2558 "assuming 1 slot is available\n");
2559 pdata->num_slots = 1;
2560 }
2561
2562 /* get quirks */
2563 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2564 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2565 pdata->quirks |= of_quirks[idx].id;
2566
2567 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2568 dev_info(dev, "fifo-depth property not found, using "
2569 "value of FIFOTH register as default\n");
2570
2571 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2572
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002573 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2574 pdata->bus_hz = clock_frequency;
2575
James Hogancb27a842012-10-16 09:43:08 +01002576 if (drv_data && drv_data->parse_dt) {
2577 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002578 if (ret)
2579 return ERR_PTR(ret);
2580 }
2581
Seungwon Jeon10b49842013-08-31 00:13:22 +09002582 if (of_find_property(np, "supports-highspeed", NULL))
2583 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2584
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002585 return pdata;
2586}
2587
2588#else /* CONFIG_OF */
2589static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2590{
2591 return ERR_PTR(-EINVAL);
2592}
2593#endif /* CONFIG_OF */
2594
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302595int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002596{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002597 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302598 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002599 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002600 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002601
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002602 if (!host->pdata) {
2603 host->pdata = dw_mci_parse_dt(host);
2604 if (IS_ERR(host->pdata)) {
2605 dev_err(host->dev, "platform data not available\n");
2606 return -EINVAL;
2607 }
Will Newtonf95f3852011-01-02 01:11:59 -05002608 }
2609
Jaehoon Chung907abd52014-03-03 11:36:43 +09002610 if (host->pdata->num_slots > 1) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002611 dev_err(host->dev,
Jaehoon Chung907abd52014-03-03 11:36:43 +09002612 "Platform data must supply num_slots.\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302613 return -ENODEV;
Will Newtonf95f3852011-01-02 01:11:59 -05002614 }
2615
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002616 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002617 if (IS_ERR(host->biu_clk)) {
2618 dev_dbg(host->dev, "biu clock not available\n");
2619 } else {
2620 ret = clk_prepare_enable(host->biu_clk);
2621 if (ret) {
2622 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002623 return ret;
2624 }
Will Newtonf95f3852011-01-02 01:11:59 -05002625 }
2626
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002627 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002628 if (IS_ERR(host->ciu_clk)) {
2629 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002630 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002631 } else {
2632 ret = clk_prepare_enable(host->ciu_clk);
2633 if (ret) {
2634 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002635 goto err_clk_biu;
2636 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002637
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002638 if (host->pdata->bus_hz) {
2639 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
2640 if (ret)
2641 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09002642 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002643 host->pdata->bus_hz);
2644 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002645 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002646 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002647
Jaehoon Chung612de4c2014-03-03 11:36:42 +09002648 if (!host->bus_hz) {
2649 dev_err(host->dev,
2650 "Platform data must supply bus speed\n");
2651 ret = -ENODEV;
2652 goto err_clk_ciu;
2653 }
2654
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09002655 if (drv_data && drv_data->init) {
2656 ret = drv_data->init(host);
2657 if (ret) {
2658 dev_err(host->dev,
2659 "implementation specific init failed\n");
2660 goto err_clk_ciu;
2661 }
2662 }
2663
James Hogancb27a842012-10-16 09:43:08 +01002664 if (drv_data && drv_data->setup_clock) {
2665 ret = drv_data->setup_clock(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002666 if (ret) {
2667 dev_err(host->dev,
2668 "implementation specific clock setup failed\n");
2669 goto err_clk_ciu;
2670 }
2671 }
2672
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302673 host->quirks = host->pdata->quirks;
Will Newtonf95f3852011-01-02 01:11:59 -05002674
2675 spin_lock_init(&host->lock);
Doug Andersonf8c58c12014-12-02 15:42:47 -08002676 spin_lock_init(&host->irq_lock);
Will Newtonf95f3852011-01-02 01:11:59 -05002677 INIT_LIST_HEAD(&host->queue);
2678
Will Newtonf95f3852011-01-02 01:11:59 -05002679 /*
2680 * Get the host data width - this assumes that HCON has been set with
2681 * the correct values.
2682 */
2683 i = (mci_readl(host, HCON) >> 7) & 0x7;
2684 if (!i) {
2685 host->push_data = dw_mci_push_data16;
2686 host->pull_data = dw_mci_pull_data16;
2687 width = 16;
2688 host->data_shift = 1;
2689 } else if (i == 2) {
2690 host->push_data = dw_mci_push_data64;
2691 host->pull_data = dw_mci_pull_data64;
2692 width = 64;
2693 host->data_shift = 3;
2694 } else {
2695 /* Check for a reserved value, and warn if it is */
2696 WARN((i != 1),
2697 "HCON reports a reserved host data width!\n"
2698 "Defaulting to 32-bit access.\n");
2699 host->push_data = dw_mci_push_data32;
2700 host->pull_data = dw_mci_pull_data32;
2701 width = 32;
2702 host->data_shift = 2;
2703 }
2704
2705 /* Reset all blocks */
Sonny Rao3a33a942014-08-04 18:19:50 -07002706 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
Seungwon Jeon141a7122012-05-22 13:01:03 +09002707 return -ENODEV;
2708
2709 host->dma_ops = host->pdata->dma_ops;
2710 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002711
2712 /* Clear the interrupts for the host controller */
2713 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2714 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2715
2716 /* Put in max timeout */
2717 mci_writel(host, TMOUT, 0xFFFFFFFF);
2718
2719 /*
2720 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2721 * Tx Mark = fifo_size / 2 DMA Size = 8
2722 */
James Hoganb86d8252011-06-24 13:57:18 +01002723 if (!host->pdata->fifo_depth) {
2724 /*
2725 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2726 * have been overwritten by the bootloader, just like we're
2727 * about to do, so if you know the value for your hardware, you
2728 * should put it in the platform data.
2729 */
2730 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00002731 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01002732 } else {
2733 fifo_size = host->pdata->fifo_depth;
2734 }
2735 host->fifo_depth = fifo_size;
Seungwon Jeon52426892013-08-31 00:13:42 +09002736 host->fifoth_val =
2737 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002738 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05002739
2740 /* disable clock to CIU */
2741 mci_writel(host, CLKENA, 0);
2742 mci_writel(host, CLKSRC, 0);
2743
James Hogan63008762013-03-12 10:43:54 +00002744 /*
2745 * In 2.40a spec, Data offset is changed.
2746 * Need to check the version-id and set data-offset for DATA register.
2747 */
2748 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
2749 dev_info(host->dev, "Version ID is %04x\n", host->verid);
2750
2751 if (host->verid < DW_MMC_240A)
2752 host->data_offset = DATA_OFFSET;
2753 else
2754 host->data_offset = DATA_240A_OFFSET;
2755
Will Newtonf95f3852011-01-02 01:11:59 -05002756 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002757 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
2758 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05002759 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07002760 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05002761
Will Newtonf95f3852011-01-02 01:11:59 -05002762 if (host->pdata->num_slots)
2763 host->num_slots = host->pdata->num_slots;
2764 else
2765 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2766
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05302767 /*
2768 * Enable interrupts for command done, data over, data empty, card det,
2769 * receive ready and error such as transmit, receive timeout, crc error
2770 */
2771 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2772 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2773 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2774 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2775 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
2776
2777 dev_info(host->dev, "DW MMC controller at irq %d, "
2778 "%d bit host data width, "
2779 "%u deep fifo\n",
2780 host->irq, width, fifo_size);
2781
Will Newtonf95f3852011-01-02 01:11:59 -05002782 /* We need at least one slot to succeed */
2783 for (i = 0; i < host->num_slots; i++) {
2784 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002785 if (ret)
2786 dev_dbg(host->dev, "slot %d init failed\n", i);
2787 else
2788 init_slots++;
2789 }
2790
2791 if (init_slots) {
2792 dev_info(host->dev, "%d slots initialized\n", init_slots);
2793 } else {
2794 dev_dbg(host->dev, "attempted to initialize %d slots, "
2795 "but failed on all\n", host->num_slots);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002796 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05002797 }
2798
Will Newtonf95f3852011-01-02 01:11:59 -05002799 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
Thomas Abraham4a909202012-09-17 18:16:35 +00002800 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002801
2802 return 0;
2803
Will Newtonf95f3852011-01-02 01:11:59 -05002804err_dmaunmap:
2805 if (host->use_dma && host->dma_ops->exit)
2806 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002807
2808err_clk_ciu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002809 if (!IS_ERR(host->ciu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002810 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002811
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002812err_clk_biu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002813 if (!IS_ERR(host->biu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002814 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002815
Will Newtonf95f3852011-01-02 01:11:59 -05002816 return ret;
2817}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302818EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05002819
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302820void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002821{
Will Newtonf95f3852011-01-02 01:11:59 -05002822 int i;
2823
2824 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2825 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2826
Will Newtonf95f3852011-01-02 01:11:59 -05002827 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002828 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05002829 if (host->slot[i])
2830 dw_mci_cleanup_slot(host->slot[i], i);
2831 }
2832
2833 /* disable clock to CIU */
2834 mci_writel(host, CLKENA, 0);
2835 mci_writel(host, CLKSRC, 0);
2836
Will Newtonf95f3852011-01-02 01:11:59 -05002837 if (host->use_dma && host->dma_ops->exit)
2838 host->dma_ops->exit(host);
2839
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002840 if (!IS_ERR(host->ciu_clk))
2841 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002842
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002843 if (!IS_ERR(host->biu_clk))
2844 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05002845}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302846EXPORT_SYMBOL(dw_mci_remove);
2847
2848
Will Newtonf95f3852011-01-02 01:11:59 -05002849
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002850#ifdef CONFIG_PM_SLEEP
Will Newtonf95f3852011-01-02 01:11:59 -05002851/*
2852 * TODO: we should probably disable the clock to the card in the suspend path.
2853 */
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302854int dw_mci_suspend(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002855{
Will Newtonf95f3852011-01-02 01:11:59 -05002856 return 0;
2857}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302858EXPORT_SYMBOL(dw_mci_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05002859
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302860int dw_mci_resume(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002861{
2862 int i, ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002863
Sonny Rao3a33a942014-08-04 18:19:50 -07002864 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002865 ret = -ENODEV;
2866 return ret;
2867 }
2868
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04002869 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09002870 host->dma_ops->init(host);
2871
Seungwon Jeon52426892013-08-31 00:13:42 +09002872 /*
2873 * Restore the initial value at FIFOTH register
2874 * And Invalidate the prev_blksz with zero
2875 */
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002876 mci_writel(host, FIFOTH, host->fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +09002877 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002878
Doug Anderson2eb29442013-08-31 00:11:49 +09002879 /* Put in max timeout */
2880 mci_writel(host, TMOUT, 0xFFFFFFFF);
2881
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002882 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2883 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2884 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2885 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2886 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2887
Will Newtonf95f3852011-01-02 01:11:59 -05002888 for (i = 0; i < host->num_slots; i++) {
2889 struct dw_mci_slot *slot = host->slot[i];
2890 if (!slot)
2891 continue;
Abhilash Kesavanab269122012-11-19 10:26:21 +05302892 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
2893 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
2894 dw_mci_setup_bus(slot, true);
2895 }
Will Newtonf95f3852011-01-02 01:11:59 -05002896 }
Will Newtonf95f3852011-01-02 01:11:59 -05002897 return 0;
2898}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302899EXPORT_SYMBOL(dw_mci_resume);
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002900#endif /* CONFIG_PM_SLEEP */
2901
Will Newtonf95f3852011-01-02 01:11:59 -05002902static int __init dw_mci_init(void)
2903{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05302904 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302905 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002906}
2907
2908static void __exit dw_mci_exit(void)
2909{
Will Newtonf95f3852011-01-02 01:11:59 -05002910}
2911
2912module_init(dw_mci_init);
2913module_exit(dw_mci_exit);
2914
2915MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2916MODULE_AUTHOR("NXP Semiconductor VietNam");
2917MODULE_AUTHOR("Imagination Technologies Ltd");
2918MODULE_LICENSE("GPL v2");