blob: b4c30448566edc5921fd48665b11a7101cdc1024 [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>
30#include <linux/mmc/host.h>
31#include <linux/mmc/mmc.h>
Doug Anderson01730552014-08-22 19:17:51 +053032#include <linux/mmc/sd.h>
Seungwon Jeon90c21432013-08-31 00:14:05 +090033#include <linux/mmc/sdio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050034#include <linux/mmc/dw_mmc.h>
35#include <linux/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090036#include <linux/regulator/consumer.h>
Thomas Abrahamc91eab42012-09-17 18:16:40 +000037#include <linux/of.h>
Doug Anderson55a6ceb2013-01-11 17:03:53 +000038#include <linux/of_gpio.h>
Zhangfei Gaobf626e52014-01-09 22:35:10 +080039#include <linux/mmc/slot-gpio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050040
41#include "dw_mmc.h"
42
43/* Common flag combinations */
Jaehoon Chung3f7eec62013-05-27 13:47:57 +090044#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
Will Newtonf95f3852011-01-02 01:11:59 -050045 SDMMC_INT_HTO | SDMMC_INT_SBE | \
46 SDMMC_INT_EBE)
47#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
48 SDMMC_INT_RESP_ERR)
49#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
50 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
51#define DW_MCI_SEND_STATUS 1
52#define DW_MCI_RECV_STATUS 2
53#define DW_MCI_DMA_THRESHOLD 16
54
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090055#define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
56#define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
57
Will Newtonf95f3852011-01-02 01:11:59 -050058#ifdef CONFIG_MMC_DW_IDMAC
Joonyoung Shimfc79a4d2013-04-26 15:35:22 +090059#define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
60 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
61 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
62 SDMMC_IDMAC_INT_TI)
63
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000064struct idmac_desc_64addr {
65 u32 des0; /* Control Descriptor */
66
67 u32 des1; /* Reserved */
68
69 u32 des2; /*Buffer sizes */
70#define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
71 ((d)->des2 = ((d)->des2 & 0x03ffe000) | ((s) & 0x1fff))
72
73 u32 des3; /* Reserved */
74
75 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
76 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
77
78 u32 des6; /* Lower 32-bits of Next Descriptor Address */
79 u32 des7; /* Upper 32-bits of Next Descriptor Address */
80};
81
Will Newtonf95f3852011-01-02 01:11:59 -050082struct idmac_desc {
83 u32 des0; /* Control Descriptor */
84#define IDMAC_DES0_DIC BIT(1)
85#define IDMAC_DES0_LD BIT(2)
86#define IDMAC_DES0_FD BIT(3)
87#define IDMAC_DES0_CH BIT(4)
88#define IDMAC_DES0_ER BIT(5)
89#define IDMAC_DES0_CES BIT(30)
90#define IDMAC_DES0_OWN BIT(31)
91
92 u32 des1; /* Buffer sizes */
93#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Shashidhar Hiremath9b7bbe12011-07-29 08:49:50 -040094 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
Will Newtonf95f3852011-01-02 01:11:59 -050095
96 u32 des2; /* buffer 1 physical address */
97
98 u32 des3; /* buffer 2 physical address */
99};
100#endif /* CONFIG_MMC_DW_IDMAC */
101
Sonny Rao3a33a942014-08-04 18:19:50 -0700102static bool dw_mci_reset(struct dw_mci *host);
Sonny Rao536f6b92014-10-16 09:58:05 -0700103static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
Seungwon Jeon31bff452013-08-31 00:14:23 +0900104
Will Newtonf95f3852011-01-02 01:11:59 -0500105#if defined(CONFIG_DEBUG_FS)
106static int dw_mci_req_show(struct seq_file *s, void *v)
107{
108 struct dw_mci_slot *slot = s->private;
109 struct mmc_request *mrq;
110 struct mmc_command *cmd;
111 struct mmc_command *stop;
112 struct mmc_data *data;
113
114 /* Make sure we get a consistent snapshot */
115 spin_lock_bh(&slot->host->lock);
116 mrq = slot->mrq;
117
118 if (mrq) {
119 cmd = mrq->cmd;
120 data = mrq->data;
121 stop = mrq->stop;
122
123 if (cmd)
124 seq_printf(s,
125 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
126 cmd->opcode, cmd->arg, cmd->flags,
127 cmd->resp[0], cmd->resp[1], cmd->resp[2],
128 cmd->resp[2], cmd->error);
129 if (data)
130 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
131 data->bytes_xfered, data->blocks,
132 data->blksz, data->flags, data->error);
133 if (stop)
134 seq_printf(s,
135 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
136 stop->opcode, stop->arg, stop->flags,
137 stop->resp[0], stop->resp[1], stop->resp[2],
138 stop->resp[2], stop->error);
139 }
140
141 spin_unlock_bh(&slot->host->lock);
142
143 return 0;
144}
145
146static int dw_mci_req_open(struct inode *inode, struct file *file)
147{
148 return single_open(file, dw_mci_req_show, inode->i_private);
149}
150
151static const struct file_operations dw_mci_req_fops = {
152 .owner = THIS_MODULE,
153 .open = dw_mci_req_open,
154 .read = seq_read,
155 .llseek = seq_lseek,
156 .release = single_release,
157};
158
159static int dw_mci_regs_show(struct seq_file *s, void *v)
160{
161 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
162 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
163 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
164 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
165 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
166 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
167
168 return 0;
169}
170
171static int dw_mci_regs_open(struct inode *inode, struct file *file)
172{
173 return single_open(file, dw_mci_regs_show, inode->i_private);
174}
175
176static const struct file_operations dw_mci_regs_fops = {
177 .owner = THIS_MODULE,
178 .open = dw_mci_regs_open,
179 .read = seq_read,
180 .llseek = seq_lseek,
181 .release = single_release,
182};
183
184static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
185{
186 struct mmc_host *mmc = slot->mmc;
187 struct dw_mci *host = slot->host;
188 struct dentry *root;
189 struct dentry *node;
190
191 root = mmc->debugfs_root;
192 if (!root)
193 return;
194
195 node = debugfs_create_file("regs", S_IRUSR, root, host,
196 &dw_mci_regs_fops);
197 if (!node)
198 goto err;
199
200 node = debugfs_create_file("req", S_IRUSR, root, slot,
201 &dw_mci_req_fops);
202 if (!node)
203 goto err;
204
205 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
206 if (!node)
207 goto err;
208
209 node = debugfs_create_x32("pending_events", S_IRUSR, root,
210 (u32 *)&host->pending_events);
211 if (!node)
212 goto err;
213
214 node = debugfs_create_x32("completed_events", S_IRUSR, root,
215 (u32 *)&host->completed_events);
216 if (!node)
217 goto err;
218
219 return;
220
221err:
222 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
223}
224#endif /* defined(CONFIG_DEBUG_FS) */
225
Doug Anderson01730552014-08-22 19:17:51 +0530226static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
227
Will Newtonf95f3852011-01-02 01:11:59 -0500228static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
229{
230 struct mmc_data *data;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000231 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson01730552014-08-22 19:17:51 +0530232 struct dw_mci *host = slot->host;
Arnd Bergmanne95baf12012-11-08 14:26:11 +0000233 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Will Newtonf95f3852011-01-02 01:11:59 -0500234 u32 cmdr;
235 cmd->error = -EINPROGRESS;
236
237 cmdr = cmd->opcode;
238
Seungwon Jeon90c21432013-08-31 00:14:05 +0900239 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
240 cmd->opcode == MMC_GO_IDLE_STATE ||
241 cmd->opcode == MMC_GO_INACTIVE_STATE ||
242 (cmd->opcode == SD_IO_RW_DIRECT &&
243 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
Will Newtonf95f3852011-01-02 01:11:59 -0500244 cmdr |= SDMMC_CMD_STOP;
Jaehoon Chung4a1b27a2014-03-03 11:36:44 +0900245 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
246 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500247
Doug Anderson01730552014-08-22 19:17:51 +0530248 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
249 u32 clk_en_a;
250
251 /* Special bit makes CMD11 not die */
252 cmdr |= SDMMC_CMD_VOLT_SWITCH;
253
254 /* Change state to continue to handle CMD11 weirdness */
255 WARN_ON(slot->host->state != STATE_SENDING_CMD);
256 slot->host->state = STATE_SENDING_CMD11;
257
258 /*
259 * We need to disable low power mode (automatic clock stop)
260 * while doing voltage switch so we don't confuse the card,
261 * since stopping the clock is a specific part of the UHS
262 * voltage change dance.
263 *
264 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
265 * unconditionally turned back on in dw_mci_setup_bus() if it's
266 * ever called with a non-zero clock. That shouldn't happen
267 * until the voltage change is all done.
268 */
269 clk_en_a = mci_readl(host, CLKENA);
270 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
271 mci_writel(host, CLKENA, clk_en_a);
272 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
273 SDMMC_CMD_PRV_DAT_WAIT, 0);
274 }
275
Will Newtonf95f3852011-01-02 01:11:59 -0500276 if (cmd->flags & MMC_RSP_PRESENT) {
277 /* We expect a response, so set this bit */
278 cmdr |= SDMMC_CMD_RESP_EXP;
279 if (cmd->flags & MMC_RSP_136)
280 cmdr |= SDMMC_CMD_RESP_LONG;
281 }
282
283 if (cmd->flags & MMC_RSP_CRC)
284 cmdr |= SDMMC_CMD_RESP_CRC;
285
286 data = cmd->data;
287 if (data) {
288 cmdr |= SDMMC_CMD_DAT_EXP;
289 if (data->flags & MMC_DATA_STREAM)
290 cmdr |= SDMMC_CMD_STRM_MODE;
291 if (data->flags & MMC_DATA_WRITE)
292 cmdr |= SDMMC_CMD_DAT_WR;
293 }
294
James Hogancb27a842012-10-16 09:43:08 +0100295 if (drv_data && drv_data->prepare_command)
296 drv_data->prepare_command(slot->host, &cmdr);
Thomas Abraham800d78b2012-09-17 18:16:42 +0000297
Will Newtonf95f3852011-01-02 01:11:59 -0500298 return cmdr;
299}
300
Seungwon Jeon90c21432013-08-31 00:14:05 +0900301static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
302{
303 struct mmc_command *stop;
304 u32 cmdr;
305
306 if (!cmd->data)
307 return 0;
308
309 stop = &host->stop_abort;
310 cmdr = cmd->opcode;
311 memset(stop, 0, sizeof(struct mmc_command));
312
313 if (cmdr == MMC_READ_SINGLE_BLOCK ||
314 cmdr == MMC_READ_MULTIPLE_BLOCK ||
315 cmdr == MMC_WRITE_BLOCK ||
316 cmdr == MMC_WRITE_MULTIPLE_BLOCK) {
317 stop->opcode = MMC_STOP_TRANSMISSION;
318 stop->arg = 0;
319 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
320 } else if (cmdr == SD_IO_RW_EXTENDED) {
321 stop->opcode = SD_IO_RW_DIRECT;
322 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
323 ((cmd->arg >> 28) & 0x7);
324 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
325 } else {
326 return 0;
327 }
328
329 cmdr = stop->opcode | SDMMC_CMD_STOP |
330 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
331
332 return cmdr;
333}
334
Will Newtonf95f3852011-01-02 01:11:59 -0500335static void dw_mci_start_command(struct dw_mci *host,
336 struct mmc_command *cmd, u32 cmd_flags)
337{
338 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000339 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500340 "start command: ARGR=0x%08x CMDR=0x%08x\n",
341 cmd->arg, cmd_flags);
342
343 mci_writel(host, CMDARG, cmd->arg);
344 wmb();
345
346 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
347}
348
Seungwon Jeon90c21432013-08-31 00:14:05 +0900349static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
Will Newtonf95f3852011-01-02 01:11:59 -0500350{
Seungwon Jeon90c21432013-08-31 00:14:05 +0900351 struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
352 dw_mci_start_command(host, stop, host->stop_cmdr);
Will Newtonf95f3852011-01-02 01:11:59 -0500353}
354
355/* DMA interface functions */
356static void dw_mci_stop_dma(struct dw_mci *host)
357{
James Hogan03e8cb52011-06-29 09:28:43 +0100358 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500359 host->dma_ops->stop(host);
360 host->dma_ops->cleanup(host);
Will Newtonf95f3852011-01-02 01:11:59 -0500361 }
Seungwon Jeonaa50f252013-08-31 00:14:38 +0900362
363 /* Data transfer was stopped by the interrupt handler */
364 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -0500365}
366
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900367static int dw_mci_get_dma_dir(struct mmc_data *data)
368{
369 if (data->flags & MMC_DATA_WRITE)
370 return DMA_TO_DEVICE;
371 else
372 return DMA_FROM_DEVICE;
373}
374
Jaehoon Chung9beee912012-02-16 11:19:38 +0900375#ifdef CONFIG_MMC_DW_IDMAC
Will Newtonf95f3852011-01-02 01:11:59 -0500376static void dw_mci_dma_cleanup(struct dw_mci *host)
377{
378 struct mmc_data *data = host->data;
379
380 if (data)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900381 if (!data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000382 dma_unmap_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900383 data->sg,
384 data->sg_len,
385 dw_mci_get_dma_dir(data));
Will Newtonf95f3852011-01-02 01:11:59 -0500386}
387
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900388static void dw_mci_idmac_reset(struct dw_mci *host)
389{
390 u32 bmod = mci_readl(host, BMOD);
391 /* Software reset of DMA */
392 bmod |= SDMMC_IDMAC_SWRESET;
393 mci_writel(host, BMOD, bmod);
394}
395
Will Newtonf95f3852011-01-02 01:11:59 -0500396static void dw_mci_idmac_stop_dma(struct dw_mci *host)
397{
398 u32 temp;
399
400 /* Disable and reset the IDMAC interface */
401 temp = mci_readl(host, CTRL);
402 temp &= ~SDMMC_CTRL_USE_IDMAC;
403 temp |= SDMMC_CTRL_DMA_RESET;
404 mci_writel(host, CTRL, temp);
405
406 /* Stop the IDMAC running */
407 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900408 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900409 temp |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -0500410 mci_writel(host, BMOD, temp);
411}
412
413static void dw_mci_idmac_complete_dma(struct dw_mci *host)
414{
415 struct mmc_data *data = host->data;
416
Thomas Abraham4a909202012-09-17 18:16:35 +0000417 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500418
419 host->dma_ops->cleanup(host);
420
421 /*
422 * If the card was removed, data will be NULL. No point in trying to
423 * send the stop command or waiting for NBUSY in this case.
424 */
425 if (data) {
426 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
427 tasklet_schedule(&host->tasklet);
428 }
429}
430
431static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
432 unsigned int sg_len)
433{
434 int i;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000435 if (host->dma_64bit_address == 1) {
436 struct idmac_desc_64addr *desc = host->sg_cpu;
Will Newtonf95f3852011-01-02 01:11:59 -0500437
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000438 for (i = 0; i < sg_len; i++, desc++) {
439 unsigned int length = sg_dma_len(&data->sg[i]);
440 u64 mem_addr = sg_dma_address(&data->sg[i]);
Will Newtonf95f3852011-01-02 01:11:59 -0500441
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000442 /*
443 * Set the OWN bit and disable interrupts for this
444 * descriptor
445 */
446 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
447 IDMAC_DES0_CH;
448 /* Buffer length */
449 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, length);
Will Newtonf95f3852011-01-02 01:11:59 -0500450
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000451 /* Physical address to DMA to/from */
452 desc->des4 = mem_addr & 0xffffffff;
453 desc->des5 = mem_addr >> 32;
454 }
Will Newtonf95f3852011-01-02 01:11:59 -0500455
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000456 /* Set first descriptor */
457 desc = host->sg_cpu;
458 desc->des0 |= IDMAC_DES0_FD;
459
460 /* Set last descriptor */
461 desc = host->sg_cpu + (i - 1) *
462 sizeof(struct idmac_desc_64addr);
463 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
464 desc->des0 |= IDMAC_DES0_LD;
465
466 } else {
467 struct idmac_desc *desc = host->sg_cpu;
468
469 for (i = 0; i < sg_len; i++, desc++) {
470 unsigned int length = sg_dma_len(&data->sg[i]);
471 u32 mem_addr = sg_dma_address(&data->sg[i]);
472
473 /*
474 * Set the OWN bit and disable interrupts for this
475 * descriptor
476 */
477 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
478 IDMAC_DES0_CH;
479 /* Buffer length */
480 IDMAC_SET_BUFFER1_SIZE(desc, length);
481
482 /* Physical address to DMA to/from */
483 desc->des2 = mem_addr;
484 }
485
486 /* Set first descriptor */
487 desc = host->sg_cpu;
488 desc->des0 |= IDMAC_DES0_FD;
489
490 /* Set last descriptor */
491 desc = host->sg_cpu + (i - 1) * sizeof(struct idmac_desc);
492 desc->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
493 desc->des0 |= IDMAC_DES0_LD;
Will Newtonf95f3852011-01-02 01:11:59 -0500494 }
495
Will Newtonf95f3852011-01-02 01:11:59 -0500496 wmb();
497}
498
499static void dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
500{
501 u32 temp;
502
503 dw_mci_translate_sglist(host, host->data, sg_len);
504
Sonny Rao536f6b92014-10-16 09:58:05 -0700505 /* Make sure to reset DMA in case we did PIO before this */
506 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
507 dw_mci_idmac_reset(host);
508
Will Newtonf95f3852011-01-02 01:11:59 -0500509 /* Select IDMAC interface */
510 temp = mci_readl(host, CTRL);
511 temp |= SDMMC_CTRL_USE_IDMAC;
512 mci_writel(host, CTRL, temp);
513
514 wmb();
515
516 /* Enable the IDMAC */
517 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900518 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
Will Newtonf95f3852011-01-02 01:11:59 -0500519 mci_writel(host, BMOD, temp);
520
521 /* Start it running */
522 mci_writel(host, PLDMND, 1);
523}
524
525static int dw_mci_idmac_init(struct dw_mci *host)
526{
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800527 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500528
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000529 if (host->dma_64bit_address == 1) {
530 struct idmac_desc_64addr *p;
531 /* Number of descriptors in the ring buffer */
532 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
Will Newtonf95f3852011-01-02 01:11:59 -0500533
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000534 /* Forward link the descriptor list */
535 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
536 i++, p++) {
537 p->des6 = (host->sg_dma +
538 (sizeof(struct idmac_desc_64addr) *
539 (i + 1))) & 0xffffffff;
Will Newtonf95f3852011-01-02 01:11:59 -0500540
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000541 p->des7 = (u64)(host->sg_dma +
542 (sizeof(struct idmac_desc_64addr) *
543 (i + 1))) >> 32;
544 /* Initialize reserved and buffer size fields to "0" */
545 p->des1 = 0;
546 p->des2 = 0;
547 p->des3 = 0;
548 }
549
550 /* Set the last descriptor as the end-of-ring descriptor */
551 p->des6 = host->sg_dma & 0xffffffff;
552 p->des7 = (u64)host->sg_dma >> 32;
553 p->des0 = IDMAC_DES0_ER;
554
555 } else {
556 struct idmac_desc *p;
557 /* Number of descriptors in the ring buffer */
558 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
559
560 /* Forward link the descriptor list */
561 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; i++, p++)
562 p->des3 = host->sg_dma + (sizeof(struct idmac_desc) *
563 (i + 1));
564
565 /* Set the last descriptor as the end-of-ring descriptor */
566 p->des3 = host->sg_dma;
567 p->des0 = IDMAC_DES0_ER;
568 }
Will Newtonf95f3852011-01-02 01:11:59 -0500569
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900570 dw_mci_idmac_reset(host);
Seungwon Jeon141a7122012-05-22 13:01:03 +0900571
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000572 if (host->dma_64bit_address == 1) {
573 /* Mask out interrupts - get Tx & Rx complete only */
574 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
575 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
576 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
Will Newtonf95f3852011-01-02 01:11:59 -0500577
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000578 /* Set the descriptor base address */
579 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
580 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
581
582 } else {
583 /* Mask out interrupts - get Tx & Rx complete only */
584 mci_writel(host, IDSTS, IDMAC_INT_CLR);
585 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
586 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
587
588 /* Set the descriptor base address */
589 mci_writel(host, DBADDR, host->sg_dma);
590 }
591
Will Newtonf95f3852011-01-02 01:11:59 -0500592 return 0;
593}
594
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100595static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900596 .init = dw_mci_idmac_init,
597 .start = dw_mci_idmac_start_dma,
598 .stop = dw_mci_idmac_stop_dma,
599 .complete = dw_mci_idmac_complete_dma,
600 .cleanup = dw_mci_dma_cleanup,
601};
602#endif /* CONFIG_MMC_DW_IDMAC */
603
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900604static int dw_mci_pre_dma_transfer(struct dw_mci *host,
605 struct mmc_data *data,
606 bool next)
Will Newtonf95f3852011-01-02 01:11:59 -0500607{
608 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900609 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500610
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900611 if (!next && data->host_cookie)
612 return data->host_cookie;
Will Newtonf95f3852011-01-02 01:11:59 -0500613
614 /*
615 * We don't do DMA on "complex" transfers, i.e. with
616 * non-word-aligned buffers or lengths. Also, we don't bother
617 * with all the DMA setup overhead for short transfers.
618 */
619 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
620 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900621
Will Newtonf95f3852011-01-02 01:11:59 -0500622 if (data->blksz & 3)
623 return -EINVAL;
624
625 for_each_sg(data->sg, sg, data->sg_len, i) {
626 if (sg->offset & 3 || sg->length & 3)
627 return -EINVAL;
628 }
629
Thomas Abraham4a909202012-09-17 18:16:35 +0000630 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900631 data->sg,
632 data->sg_len,
633 dw_mci_get_dma_dir(data));
634 if (sg_len == 0)
635 return -EINVAL;
636
637 if (next)
638 data->host_cookie = sg_len;
639
640 return sg_len;
641}
642
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900643static void dw_mci_pre_req(struct mmc_host *mmc,
644 struct mmc_request *mrq,
645 bool is_first_req)
646{
647 struct dw_mci_slot *slot = mmc_priv(mmc);
648 struct mmc_data *data = mrq->data;
649
650 if (!slot->host->use_dma || !data)
651 return;
652
653 if (data->host_cookie) {
654 data->host_cookie = 0;
655 return;
656 }
657
658 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
659 data->host_cookie = 0;
660}
661
662static void dw_mci_post_req(struct mmc_host *mmc,
663 struct mmc_request *mrq,
664 int err)
665{
666 struct dw_mci_slot *slot = mmc_priv(mmc);
667 struct mmc_data *data = mrq->data;
668
669 if (!slot->host->use_dma || !data)
670 return;
671
672 if (data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000673 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900674 data->sg,
675 data->sg_len,
676 dw_mci_get_dma_dir(data));
677 data->host_cookie = 0;
678}
679
Seungwon Jeon52426892013-08-31 00:13:42 +0900680static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
681{
682#ifdef CONFIG_MMC_DW_IDMAC
683 unsigned int blksz = data->blksz;
684 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
685 u32 fifo_width = 1 << host->data_shift;
686 u32 blksz_depth = blksz / fifo_width, fifoth_val;
687 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
688 int idx = (sizeof(mszs) / sizeof(mszs[0])) - 1;
689
690 tx_wmark = (host->fifo_depth) / 2;
691 tx_wmark_invers = host->fifo_depth - tx_wmark;
692
693 /*
694 * MSIZE is '1',
695 * if blksz is not a multiple of the FIFO width
696 */
697 if (blksz % fifo_width) {
698 msize = 0;
699 rx_wmark = 1;
700 goto done;
701 }
702
703 do {
704 if (!((blksz_depth % mszs[idx]) ||
705 (tx_wmark_invers % mszs[idx]))) {
706 msize = idx;
707 rx_wmark = mszs[idx] - 1;
708 break;
709 }
710 } while (--idx > 0);
711 /*
712 * If idx is '0', it won't be tried
713 * Thus, initial values are uesed
714 */
715done:
716 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
717 mci_writel(host, FIFOTH, fifoth_val);
718#endif
719}
720
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900721static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
722{
723 unsigned int blksz = data->blksz;
724 u32 blksz_depth, fifo_depth;
725 u16 thld_size;
726
727 WARN_ON(!(data->flags & MMC_DATA_READ));
728
729 if (host->timing != MMC_TIMING_MMC_HS200 &&
730 host->timing != MMC_TIMING_UHS_SDR104)
731 goto disable;
732
733 blksz_depth = blksz / (1 << host->data_shift);
734 fifo_depth = host->fifo_depth;
735
736 if (blksz_depth > fifo_depth)
737 goto disable;
738
739 /*
740 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
741 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
742 * Currently just choose blksz.
743 */
744 thld_size = blksz;
745 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
746 return;
747
748disable:
749 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
750}
751
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900752static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
753{
754 int sg_len;
755 u32 temp;
756
757 host->using_dma = 0;
758
759 /* If we don't have a channel, we can't do DMA */
760 if (!host->use_dma)
761 return -ENODEV;
762
763 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900764 if (sg_len < 0) {
765 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900766 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900767 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900768
James Hogan03e8cb52011-06-29 09:28:43 +0100769 host->using_dma = 1;
770
Thomas Abraham4a909202012-09-17 18:16:35 +0000771 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500772 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
773 (unsigned long)host->sg_cpu, (unsigned long)host->sg_dma,
774 sg_len);
775
Seungwon Jeon52426892013-08-31 00:13:42 +0900776 /*
777 * Decide the MSIZE and RX/TX Watermark.
778 * If current block size is same with previous size,
779 * no need to update fifoth.
780 */
781 if (host->prev_blksz != data->blksz)
782 dw_mci_adjust_fifoth(host, data);
783
Will Newtonf95f3852011-01-02 01:11:59 -0500784 /* Enable the DMA interface */
785 temp = mci_readl(host, CTRL);
786 temp |= SDMMC_CTRL_DMA_ENABLE;
787 mci_writel(host, CTRL, temp);
788
789 /* Disable RX/TX IRQs, let DMA handle it */
790 temp = mci_readl(host, INTMASK);
791 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
792 mci_writel(host, INTMASK, temp);
793
794 host->dma_ops->start(host, sg_len);
795
796 return 0;
797}
798
799static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
800{
801 u32 temp;
802
803 data->error = -EINPROGRESS;
804
805 WARN_ON(host->data);
806 host->sg = NULL;
807 host->data = data;
808
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900809 if (data->flags & MMC_DATA_READ) {
James Hogan55c5efbc2011-06-29 09:29:58 +0100810 host->dir_status = DW_MCI_RECV_STATUS;
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900811 dw_mci_ctrl_rd_thld(host, data);
812 } else {
James Hogan55c5efbc2011-06-29 09:29:58 +0100813 host->dir_status = DW_MCI_SEND_STATUS;
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900814 }
James Hogan55c5efbc2011-06-29 09:29:58 +0100815
Will Newtonf95f3852011-01-02 01:11:59 -0500816 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +0900817 int flags = SG_MITER_ATOMIC;
818 if (host->data->flags & MMC_DATA_READ)
819 flags |= SG_MITER_TO_SG;
820 else
821 flags |= SG_MITER_FROM_SG;
822
823 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500824 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +0100825 host->part_buf_start = 0;
826 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500827
James Hoganb40af3a2011-06-24 13:54:06 +0100828 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Will Newtonf95f3852011-01-02 01:11:59 -0500829 temp = mci_readl(host, INTMASK);
830 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
831 mci_writel(host, INTMASK, temp);
832
833 temp = mci_readl(host, CTRL);
834 temp &= ~SDMMC_CTRL_DMA_ENABLE;
835 mci_writel(host, CTRL, temp);
Seungwon Jeon52426892013-08-31 00:13:42 +0900836
837 /*
838 * Use the initial fifoth_val for PIO mode.
839 * If next issued data may be transfered by DMA mode,
840 * prev_blksz should be invalidated.
841 */
842 mci_writel(host, FIFOTH, host->fifoth_val);
843 host->prev_blksz = 0;
844 } else {
845 /*
846 * Keep the current block size.
847 * It will be used to decide whether to update
848 * fifoth register next time.
849 */
850 host->prev_blksz = data->blksz;
Will Newtonf95f3852011-01-02 01:11:59 -0500851 }
852}
853
854static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
855{
856 struct dw_mci *host = slot->host;
857 unsigned long timeout = jiffies + msecs_to_jiffies(500);
858 unsigned int cmd_status = 0;
859
860 mci_writel(host, CMDARG, arg);
861 wmb();
862 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
863
864 while (time_before(jiffies, timeout)) {
865 cmd_status = mci_readl(host, CMD);
866 if (!(cmd_status & SDMMC_CMD_START))
867 return;
868 }
869 dev_err(&slot->mmc->class_dev,
870 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
871 cmd, arg, cmd_status);
872}
873
Abhilash Kesavanab269122012-11-19 10:26:21 +0530874static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -0500875{
876 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +0900877 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -0500878 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -0700879 u32 clk_en_a;
Doug Anderson01730552014-08-22 19:17:51 +0530880 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
881
882 /* We must continue to set bit 28 in CMD until the change is complete */
883 if (host->state == STATE_WAITING_CMD11_DONE)
884 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
Will Newtonf95f3852011-01-02 01:11:59 -0500885
Doug Andersonfdf492a2013-08-31 00:11:43 +0900886 if (!clock) {
887 mci_writel(host, CLKENA, 0);
Doug Anderson01730552014-08-22 19:17:51 +0530888 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Doug Andersonfdf492a2013-08-31 00:11:43 +0900889 } else if (clock != host->current_speed || force_clkinit) {
890 div = host->bus_hz / clock;
891 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -0500892 /*
893 * move the + 1 after the divide to prevent
894 * over-clocking the card.
895 */
Seungwon Jeone4199902012-05-22 13:01:21 +0900896 div += 1;
897
Doug Andersonfdf492a2013-08-31 00:11:43 +0900898 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500899
Doug Andersonfdf492a2013-08-31 00:11:43 +0900900 if ((clock << div) != slot->__clk_old || force_clkinit)
901 dev_info(&slot->mmc->class_dev,
902 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
903 slot->id, host->bus_hz, clock,
904 div ? ((host->bus_hz / div) >> 1) :
905 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -0500906
907 /* disable clock */
908 mci_writel(host, CLKENA, 0);
909 mci_writel(host, CLKSRC, 0);
910
911 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +0530912 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -0500913
914 /* set clock to desired speed */
915 mci_writel(host, CLKDIV, div);
916
917 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +0530918 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -0500919
Doug Anderson9623b5b2012-07-25 08:33:17 -0700920 /* enable clock; only low power if no SDIO */
921 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Addy Ke76756232014-11-04 22:03:09 +0800922 if (!(mci_readl(host, INTMASK) & SDMMC_INT_SDIO(slot->sdio_id)))
Doug Anderson9623b5b2012-07-25 08:33:17 -0700923 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
924 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -0500925
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
Doug Andersonfdf492a2013-08-31 00:11:43 +0900929 /* keep the clock with reflecting clock dividor */
930 slot->__clk_old = clock << div;
Will Newtonf95f3852011-01-02 01:11:59 -0500931 }
932
Doug Andersonfdf492a2013-08-31 00:11:43 +0900933 host->current_speed = clock;
934
Will Newtonf95f3852011-01-02 01:11:59 -0500935 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +0900936 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -0500937}
938
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900939static void __dw_mci_start_request(struct dw_mci *host,
940 struct dw_mci_slot *slot,
941 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -0500942{
943 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -0500944 struct mmc_data *data;
945 u32 cmdflags;
946
947 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -0500948
Will Newtonf95f3852011-01-02 01:11:59 -0500949 host->cur_slot = slot;
950 host->mrq = mrq;
951
952 host->pending_events = 0;
953 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +0900954 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500955 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +0900956 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500957
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900958 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -0500959 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +0900960 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -0500961 mci_writel(host, BYTCNT, data->blksz*data->blocks);
962 mci_writel(host, BLKSIZ, data->blksz);
963 }
964
Will Newtonf95f3852011-01-02 01:11:59 -0500965 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
966
967 /* this is the first command, send the initialization clock */
968 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
969 cmdflags |= SDMMC_CMD_INIT;
970
971 if (data) {
972 dw_mci_submit_data(host, data);
973 wmb();
974 }
975
976 dw_mci_start_command(host, cmd, cmdflags);
977
978 if (mrq->stop)
979 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +0900980 else
981 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -0500982}
983
Seungwon Jeon053b3ce2011-12-22 18:01:29 +0900984static void dw_mci_start_request(struct dw_mci *host,
985 struct dw_mci_slot *slot)
986{
987 struct mmc_request *mrq = slot->mrq;
988 struct mmc_command *cmd;
989
990 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
991 __dw_mci_start_request(host, slot, cmd);
992}
993
James Hogan7456caa2011-06-24 13:55:10 +0100994/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -0500995static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
996 struct mmc_request *mrq)
997{
998 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
999 host->state);
1000
Will Newtonf95f3852011-01-02 01:11:59 -05001001 slot->mrq = mrq;
1002
Doug Anderson01730552014-08-22 19:17:51 +05301003 if (host->state == STATE_WAITING_CMD11_DONE) {
1004 dev_warn(&slot->mmc->class_dev,
1005 "Voltage change didn't complete\n");
1006 /*
1007 * this case isn't expected to happen, so we can
1008 * either crash here or just try to continue on
1009 * in the closest possible state
1010 */
1011 host->state = STATE_IDLE;
1012 }
1013
Will Newtonf95f3852011-01-02 01:11:59 -05001014 if (host->state == STATE_IDLE) {
1015 host->state = STATE_SENDING_CMD;
1016 dw_mci_start_request(host, slot);
1017 } else {
1018 list_add_tail(&slot->queue_node, &host->queue);
1019 }
Will Newtonf95f3852011-01-02 01:11:59 -05001020}
1021
1022static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1023{
1024 struct dw_mci_slot *slot = mmc_priv(mmc);
1025 struct dw_mci *host = slot->host;
1026
1027 WARN_ON(slot->mrq);
1028
James Hogan7456caa2011-06-24 13:55:10 +01001029 /*
1030 * The check for card presence and queueing of the request must be
1031 * atomic, otherwise the card could be removed in between and the
1032 * request wouldn't fail until another card was inserted.
1033 */
1034 spin_lock_bh(&host->lock);
1035
Will Newtonf95f3852011-01-02 01:11:59 -05001036 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
James Hogan7456caa2011-06-24 13:55:10 +01001037 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001038 mrq->cmd->error = -ENOMEDIUM;
1039 mmc_request_done(mmc, mrq);
1040 return;
1041 }
1042
Will Newtonf95f3852011-01-02 01:11:59 -05001043 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001044
1045 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001046}
1047
1048static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1049{
1050 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001051 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001052 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301053 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001054
Will Newtonf95f3852011-01-02 01:11:59 -05001055 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001056 case MMC_BUS_WIDTH_4:
1057 slot->ctype = SDMMC_CTYPE_4BIT;
1058 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001059 case MMC_BUS_WIDTH_8:
1060 slot->ctype = SDMMC_CTYPE_8BIT;
1061 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001062 default:
1063 /* set default 1 bit mode */
1064 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001065 }
1066
Seungwon Jeon3f514292012-01-02 16:00:02 +09001067 regs = mci_readl(slot->host, UHS_REG);
1068
Jaehoon Chung41babf72011-02-24 13:46:11 +09001069 /* DDR mode set */
Seungwon Jeoncab3a802014-03-14 21:12:43 +09001070 if (ios->timing == MMC_TIMING_MMC_DDR52)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001071 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001072 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001073 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001074
1075 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001076 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001077
Doug Andersonfdf492a2013-08-31 00:11:43 +09001078 /*
1079 * Use mirror of ios->clock to prevent race with mmc
1080 * core ios update when finding the minimum.
1081 */
1082 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001083
James Hogancb27a842012-10-16 09:43:08 +01001084 if (drv_data && drv_data->set_ios)
1085 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001086
Jaehoon Chungbf7cb222012-11-08 17:35:29 +09001087 /* Slot specific timing and width adjustment */
1088 dw_mci_setup_bus(slot, false);
1089
Doug Anderson01730552014-08-22 19:17:51 +05301090 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1091 slot->host->state = STATE_IDLE;
1092
Will Newtonf95f3852011-01-02 01:11:59 -05001093 switch (ios->power_mode) {
1094 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301095 if (!IS_ERR(mmc->supply.vmmc)) {
1096 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1097 ios->vdd);
1098 if (ret) {
1099 dev_err(slot->host->dev,
1100 "failed to enable vmmc regulator\n");
1101 /*return, if failed turn on vmmc*/
1102 return;
1103 }
1104 }
1105 if (!IS_ERR(mmc->supply.vqmmc) && !slot->host->vqmmc_enabled) {
1106 ret = regulator_enable(mmc->supply.vqmmc);
1107 if (ret < 0)
1108 dev_err(slot->host->dev,
1109 "failed to enable vqmmc regulator\n");
1110 else
1111 slot->host->vqmmc_enabled = true;
1112 }
Will Newtonf95f3852011-01-02 01:11:59 -05001113 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001114 regs = mci_readl(slot->host, PWREN);
1115 regs |= (1 << slot->id);
1116 mci_writel(slot->host, PWREN, regs);
James Hogane6f34e22013-03-12 10:43:32 +00001117 break;
1118 case MMC_POWER_OFF:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301119 if (!IS_ERR(mmc->supply.vmmc))
1120 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1121
1122 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) {
1123 regulator_disable(mmc->supply.vqmmc);
1124 slot->host->vqmmc_enabled = false;
1125 }
1126
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001127 regs = mci_readl(slot->host, PWREN);
1128 regs &= ~(1 << slot->id);
1129 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001130 break;
1131 default:
1132 break;
1133 }
1134}
1135
Doug Anderson01730552014-08-22 19:17:51 +05301136static int dw_mci_card_busy(struct mmc_host *mmc)
1137{
1138 struct dw_mci_slot *slot = mmc_priv(mmc);
1139 u32 status;
1140
1141 /*
1142 * Check the busy bit which is low when DAT[3:0]
1143 * (the data lines) are 0000
1144 */
1145 status = mci_readl(slot->host, STATUS);
1146
1147 return !!(status & SDMMC_STATUS_BUSY);
1148}
1149
1150static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1151{
1152 struct dw_mci_slot *slot = mmc_priv(mmc);
1153 struct dw_mci *host = slot->host;
1154 u32 uhs;
1155 u32 v18 = SDMMC_UHS_18V << slot->id;
1156 int min_uv, max_uv;
1157 int ret;
1158
1159 /*
1160 * Program the voltage. Note that some instances of dw_mmc may use
1161 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1162 * does no harm but you need to set the regulator directly. Try both.
1163 */
1164 uhs = mci_readl(host, UHS_REG);
1165 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1166 min_uv = 2700000;
1167 max_uv = 3600000;
1168 uhs &= ~v18;
1169 } else {
1170 min_uv = 1700000;
1171 max_uv = 1950000;
1172 uhs |= v18;
1173 }
1174 if (!IS_ERR(mmc->supply.vqmmc)) {
1175 ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
1176
1177 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001178 dev_dbg(&mmc->class_dev,
Doug Anderson01730552014-08-22 19:17:51 +05301179 "Regulator set error %d: %d - %d\n",
1180 ret, min_uv, max_uv);
1181 return ret;
1182 }
1183 }
1184 mci_writel(host, UHS_REG, uhs);
1185
1186 return 0;
1187}
1188
Will Newtonf95f3852011-01-02 01:11:59 -05001189static int dw_mci_get_ro(struct mmc_host *mmc)
1190{
1191 int read_only;
1192 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001193 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001194
1195 /* Use platform get_ro function, else try on board write protect */
Jaehoon Chung26375b52014-08-07 16:37:58 +09001196 if ((slot->quirks & DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT) ||
1197 (slot->host->quirks & DW_MCI_QUIRK_NO_WRITE_PROTECT))
Thomas Abrahamb4967aa2012-09-17 18:16:39 +00001198 read_only = 0;
Jaehoon Chung9795a842014-03-03 11:36:46 +09001199 else if (!IS_ERR_VALUE(gpio_ro))
1200 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001201 else
1202 read_only =
1203 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1204
1205 dev_dbg(&mmc->class_dev, "card is %s\n",
1206 read_only ? "read-only" : "read-write");
1207
1208 return read_only;
1209}
1210
1211static int dw_mci_get_cd(struct mmc_host *mmc)
1212{
1213 int present;
1214 struct dw_mci_slot *slot = mmc_priv(mmc);
1215 struct dw_mci_board *brd = slot->host->pdata;
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001216 struct dw_mci *host = slot->host;
1217 int gpio_cd = mmc_gpio_get_cd(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001218
1219 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001220 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
1221 present = 1;
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001222 else if (!IS_ERR_VALUE(gpio_cd))
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001223 present = gpio_cd;
Will Newtonf95f3852011-01-02 01:11:59 -05001224 else
1225 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1226 == 0 ? 1 : 0;
1227
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001228 spin_lock_bh(&host->lock);
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001229 if (present) {
1230 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001231 dev_dbg(&mmc->class_dev, "card is present\n");
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001232 } else {
1233 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001234 dev_dbg(&mmc->class_dev, "card is not present\n");
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001235 }
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001236 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001237
1238 return present;
1239}
1240
Doug Anderson9623b5b2012-07-25 08:33:17 -07001241/*
1242 * Disable lower power mode.
1243 *
1244 * Low power mode will stop the card clock when idle. According to the
1245 * description of the CLKENA register we should disable low power mode
1246 * for SDIO cards if we need SDIO interrupts to work.
1247 *
1248 * This function is fast if low power mode is already disabled.
1249 */
1250static void dw_mci_disable_low_power(struct dw_mci_slot *slot)
1251{
1252 struct dw_mci *host = slot->host;
1253 u32 clk_en_a;
1254 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1255
1256 clk_en_a = mci_readl(host, CLKENA);
1257
1258 if (clk_en_a & clken_low_pwr) {
1259 mci_writel(host, CLKENA, clk_en_a & ~clken_low_pwr);
1260 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1261 SDMMC_CMD_PRV_DAT_WAIT, 0);
1262 }
1263}
1264
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301265static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1266{
1267 struct dw_mci_slot *slot = mmc_priv(mmc);
1268 struct dw_mci *host = slot->host;
1269 u32 int_mask;
1270
1271 /* Enable/disable Slot Specific SDIO interrupt */
1272 int_mask = mci_readl(host, INTMASK);
1273 if (enb) {
Doug Anderson9623b5b2012-07-25 08:33:17 -07001274 /*
1275 * Turn off low power mode if it was enabled. This is a bit of
1276 * a heavy operation and we disable / enable IRQs a lot, so
1277 * we'll leave low power mode disabled and it will get
1278 * re-enabled again in dw_mci_setup_bus().
1279 */
1280 dw_mci_disable_low_power(slot);
1281
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301282 mci_writel(host, INTMASK,
Addy Ke76756232014-11-04 22:03:09 +08001283 (int_mask | SDMMC_INT_SDIO(slot->sdio_id)));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301284 } else {
1285 mci_writel(host, INTMASK,
Addy Ke76756232014-11-04 22:03:09 +08001286 (int_mask & ~SDMMC_INT_SDIO(slot->sdio_id)));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301287 }
1288}
1289
Seungwon Jeon0976f162013-08-31 00:12:42 +09001290static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1291{
1292 struct dw_mci_slot *slot = mmc_priv(mmc);
1293 struct dw_mci *host = slot->host;
1294 const struct dw_mci_drv_data *drv_data = host->drv_data;
1295 struct dw_mci_tuning_data tuning_data;
1296 int err = -ENOSYS;
1297
1298 if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1299 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8) {
1300 tuning_data.blk_pattern = tuning_blk_pattern_8bit;
1301 tuning_data.blksz = sizeof(tuning_blk_pattern_8bit);
1302 } else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1303 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1304 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1305 } else {
1306 return -EINVAL;
1307 }
1308 } else if (opcode == MMC_SEND_TUNING_BLOCK) {
1309 tuning_data.blk_pattern = tuning_blk_pattern_4bit;
1310 tuning_data.blksz = sizeof(tuning_blk_pattern_4bit);
1311 } else {
1312 dev_err(host->dev,
1313 "Undefined command(%d) for tuning\n", opcode);
1314 return -EINVAL;
1315 }
1316
1317 if (drv_data && drv_data->execute_tuning)
1318 err = drv_data->execute_tuning(slot, opcode, &tuning_data);
1319 return err;
1320}
1321
Will Newtonf95f3852011-01-02 01:11:59 -05001322static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301323 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001324 .pre_req = dw_mci_pre_req,
1325 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301326 .set_ios = dw_mci_set_ios,
1327 .get_ro = dw_mci_get_ro,
1328 .get_cd = dw_mci_get_cd,
1329 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001330 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301331 .card_busy = dw_mci_card_busy,
1332 .start_signal_voltage_switch = dw_mci_switch_voltage,
1333
Will Newtonf95f3852011-01-02 01:11:59 -05001334};
1335
1336static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1337 __releases(&host->lock)
1338 __acquires(&host->lock)
1339{
1340 struct dw_mci_slot *slot;
1341 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1342
1343 WARN_ON(host->cmd || host->data);
1344
1345 host->cur_slot->mrq = NULL;
1346 host->mrq = NULL;
1347 if (!list_empty(&host->queue)) {
1348 slot = list_entry(host->queue.next,
1349 struct dw_mci_slot, queue_node);
1350 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001351 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001352 mmc_hostname(slot->mmc));
1353 host->state = STATE_SENDING_CMD;
1354 dw_mci_start_request(host, slot);
1355 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001356 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301357
1358 if (host->state == STATE_SENDING_CMD11)
1359 host->state = STATE_WAITING_CMD11_DONE;
1360 else
1361 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001362 }
1363
1364 spin_unlock(&host->lock);
1365 mmc_request_done(prev_mmc, mrq);
1366 spin_lock(&host->lock);
1367}
1368
Seungwon Jeone352c812013-08-31 00:14:17 +09001369static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001370{
1371 u32 status = host->cmd_status;
1372
1373 host->cmd_status = 0;
1374
1375 /* Read the response from the card (up to 16 bytes) */
1376 if (cmd->flags & MMC_RSP_PRESENT) {
1377 if (cmd->flags & MMC_RSP_136) {
1378 cmd->resp[3] = mci_readl(host, RESP0);
1379 cmd->resp[2] = mci_readl(host, RESP1);
1380 cmd->resp[1] = mci_readl(host, RESP2);
1381 cmd->resp[0] = mci_readl(host, RESP3);
1382 } else {
1383 cmd->resp[0] = mci_readl(host, RESP0);
1384 cmd->resp[1] = 0;
1385 cmd->resp[2] = 0;
1386 cmd->resp[3] = 0;
1387 }
1388 }
1389
1390 if (status & SDMMC_INT_RTO)
1391 cmd->error = -ETIMEDOUT;
1392 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1393 cmd->error = -EILSEQ;
1394 else if (status & SDMMC_INT_RESP_ERR)
1395 cmd->error = -EIO;
1396 else
1397 cmd->error = 0;
1398
1399 if (cmd->error) {
1400 /* newer ip versions need a delay between retries */
1401 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1402 mdelay(20);
Will Newtonf95f3852011-01-02 01:11:59 -05001403 }
Seungwon Jeone352c812013-08-31 00:14:17 +09001404
1405 return cmd->error;
1406}
1407
1408static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1409{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001410 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001411
1412 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1413 if (status & SDMMC_INT_DRTO) {
1414 data->error = -ETIMEDOUT;
1415 } else if (status & SDMMC_INT_DCRC) {
1416 data->error = -EILSEQ;
1417 } else if (status & SDMMC_INT_EBE) {
1418 if (host->dir_status ==
1419 DW_MCI_SEND_STATUS) {
1420 /*
1421 * No data CRC status was returned.
1422 * The number of bytes transferred
1423 * will be exaggerated in PIO mode.
1424 */
1425 data->bytes_xfered = 0;
1426 data->error = -ETIMEDOUT;
1427 } else if (host->dir_status ==
1428 DW_MCI_RECV_STATUS) {
1429 data->error = -EIO;
1430 }
1431 } else {
1432 /* SDMMC_INT_SBE is included */
1433 data->error = -EIO;
1434 }
1435
Doug Andersone6cc0122014-04-22 16:51:21 -07001436 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001437
1438 /*
1439 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001440 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001441 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001442 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001443 } else {
1444 data->bytes_xfered = data->blocks * data->blksz;
1445 data->error = 0;
1446 }
1447
1448 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001449}
1450
1451static void dw_mci_tasklet_func(unsigned long priv)
1452{
1453 struct dw_mci *host = (struct dw_mci *)priv;
1454 struct mmc_data *data;
1455 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001456 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001457 enum dw_mci_state state;
1458 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001459 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001460
1461 spin_lock(&host->lock);
1462
1463 state = host->state;
1464 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001465 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001466
1467 do {
1468 prev_state = state;
1469
1470 switch (state) {
1471 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301472 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001473 break;
1474
Doug Anderson01730552014-08-22 19:17:51 +05301475 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001476 case STATE_SENDING_CMD:
1477 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1478 &host->pending_events))
1479 break;
1480
1481 cmd = host->cmd;
1482 host->cmd = NULL;
1483 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001484 err = dw_mci_command_complete(host, cmd);
1485 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001486 prev_state = state = STATE_SENDING_CMD;
1487 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001488 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001489 goto unlock;
1490 }
1491
Seungwon Jeone352c812013-08-31 00:14:17 +09001492 if (cmd->data && err) {
Seungwon Jeon71abb132013-08-31 00:13:59 +09001493 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001494 send_stop_abort(host, data);
1495 state = STATE_SENDING_STOP;
1496 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001497 }
1498
Seungwon Jeone352c812013-08-31 00:14:17 +09001499 if (!cmd->data || err) {
1500 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001501 goto unlock;
1502 }
1503
1504 prev_state = state = STATE_SENDING_DATA;
1505 /* fall through */
1506
1507 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001508 /*
1509 * We could get a data error and never a transfer
1510 * complete so we'd better check for it here.
1511 *
1512 * Note that we don't really care if we also got a
1513 * transfer complete; stopping the DMA and sending an
1514 * abort won't hurt.
1515 */
Will Newtonf95f3852011-01-02 01:11:59 -05001516 if (test_and_clear_bit(EVENT_DATA_ERROR,
1517 &host->pending_events)) {
1518 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001519 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001520 state = STATE_DATA_ERROR;
1521 break;
1522 }
1523
1524 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1525 &host->pending_events))
1526 break;
1527
1528 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07001529
1530 /*
1531 * Handle an EVENT_DATA_ERROR that might have shown up
1532 * before the transfer completed. This might not have
1533 * been caught by the check above because the interrupt
1534 * could have gone off between the previous check and
1535 * the check for transfer complete.
1536 *
1537 * Technically this ought not be needed assuming we
1538 * get a DATA_COMPLETE eventually (we'll notice the
1539 * error and end the request), but it shouldn't hurt.
1540 *
1541 * This has the advantage of sending the stop command.
1542 */
1543 if (test_and_clear_bit(EVENT_DATA_ERROR,
1544 &host->pending_events)) {
1545 dw_mci_stop_dma(host);
1546 send_stop_abort(host, data);
1547 state = STATE_DATA_ERROR;
1548 break;
1549 }
Will Newtonf95f3852011-01-02 01:11:59 -05001550 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07001551
Will Newtonf95f3852011-01-02 01:11:59 -05001552 /* fall through */
1553
1554 case STATE_DATA_BUSY:
1555 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
1556 &host->pending_events))
1557 break;
1558
1559 host->data = NULL;
1560 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001561 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001562
Seungwon Jeone352c812013-08-31 00:14:17 +09001563 if (!err) {
1564 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05301565 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09001566 data->stop->error = 0;
1567 dw_mci_request_end(host, mrq);
1568 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05001569 }
Will Newtonf95f3852011-01-02 01:11:59 -05001570
Seungwon Jeon90c21432013-08-31 00:14:05 +09001571 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09001572 if (data->stop)
1573 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001574 } else {
1575 /*
1576 * If we don't have a command complete now we'll
1577 * never get one since we just reset everything;
1578 * better end the request.
1579 *
1580 * If we do have a command complete we'll fall
1581 * through to the SENDING_STOP command and
1582 * everything will be peachy keen.
1583 */
1584 if (!test_bit(EVENT_CMD_COMPLETE,
1585 &host->pending_events)) {
1586 host->cmd = NULL;
1587 dw_mci_request_end(host, mrq);
1588 goto unlock;
1589 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09001590 }
Seungwon Jeone352c812013-08-31 00:14:17 +09001591
1592 /*
1593 * If err has non-zero,
1594 * stop-abort command has been already issued.
1595 */
1596 prev_state = state = STATE_SENDING_STOP;
1597
Will Newtonf95f3852011-01-02 01:11:59 -05001598 /* fall through */
1599
1600 case STATE_SENDING_STOP:
1601 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1602 &host->pending_events))
1603 break;
1604
Seungwon Jeon71abb132013-08-31 00:13:59 +09001605 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09001606 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07001607 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09001608
Will Newtonf95f3852011-01-02 01:11:59 -05001609 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001610 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09001611
Seungwon Jeone352c812013-08-31 00:14:17 +09001612 if (mrq->stop)
1613 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001614 else
1615 host->cmd_status = 0;
1616
Seungwon Jeone352c812013-08-31 00:14:17 +09001617 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001618 goto unlock;
1619
1620 case STATE_DATA_ERROR:
1621 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1622 &host->pending_events))
1623 break;
1624
1625 state = STATE_DATA_BUSY;
1626 break;
1627 }
1628 } while (state != prev_state);
1629
1630 host->state = state;
1631unlock:
1632 spin_unlock(&host->lock);
1633
1634}
1635
James Hogan34b664a2011-06-24 13:57:56 +01001636/* push final bytes to part_buf, only use during push */
1637static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1638{
1639 memcpy((void *)&host->part_buf, buf, cnt);
1640 host->part_buf_count = cnt;
1641}
1642
1643/* append bytes to part_buf, only use during push */
1644static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1645{
1646 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1647 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1648 host->part_buf_count += cnt;
1649 return cnt;
1650}
1651
1652/* pull first bytes from part_buf, only use during pull */
1653static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1654{
1655 cnt = min(cnt, (int)host->part_buf_count);
1656 if (cnt) {
1657 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1658 cnt);
1659 host->part_buf_count -= cnt;
1660 host->part_buf_start += cnt;
1661 }
1662 return cnt;
1663}
1664
1665/* pull final bytes from the part_buf, assuming it's just been filled */
1666static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1667{
1668 memcpy(buf, &host->part_buf, cnt);
1669 host->part_buf_start = cnt;
1670 host->part_buf_count = (1 << host->data_shift) - cnt;
1671}
1672
Will Newtonf95f3852011-01-02 01:11:59 -05001673static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1674{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001675 struct mmc_data *data = host->data;
1676 int init_cnt = cnt;
1677
James Hogan34b664a2011-06-24 13:57:56 +01001678 /* try and push anything in the part_buf */
1679 if (unlikely(host->part_buf_count)) {
1680 int len = dw_mci_push_part_bytes(host, buf, cnt);
1681 buf += len;
1682 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001683 if (host->part_buf_count == 2) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001684 mci_writew(host, DATA(host->data_offset),
1685 host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01001686 host->part_buf_count = 0;
1687 }
1688 }
1689#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1690 if (unlikely((unsigned long)buf & 0x1)) {
1691 while (cnt >= 2) {
1692 u16 aligned_buf[64];
1693 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1694 int items = len >> 1;
1695 int i;
1696 /* memcpy from input buffer into aligned buffer */
1697 memcpy(aligned_buf, buf, len);
1698 buf += len;
1699 cnt -= len;
1700 /* push data from aligned buffer into fifo */
1701 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001702 mci_writew(host, DATA(host->data_offset),
1703 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001704 }
1705 } else
1706#endif
1707 {
1708 u16 *pdata = buf;
1709 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001710 mci_writew(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001711 buf = pdata;
1712 }
1713 /* put anything remaining in the part_buf */
1714 if (cnt) {
1715 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001716 /* Push data if we have reached the expected data length */
1717 if ((data->bytes_xfered + init_cnt) ==
1718 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001719 mci_writew(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001720 host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05001721 }
1722}
1723
1724static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
1725{
James Hogan34b664a2011-06-24 13:57:56 +01001726#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1727 if (unlikely((unsigned long)buf & 0x1)) {
1728 while (cnt >= 2) {
1729 /* pull data from fifo into aligned buffer */
1730 u16 aligned_buf[64];
1731 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1732 int items = len >> 1;
1733 int i;
1734 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001735 aligned_buf[i] = mci_readw(host,
1736 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001737 /* memcpy from aligned buffer into output buffer */
1738 memcpy(buf, aligned_buf, len);
1739 buf += len;
1740 cnt -= len;
1741 }
1742 } else
1743#endif
1744 {
1745 u16 *pdata = buf;
1746 for (; cnt >= 2; cnt -= 2)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001747 *pdata++ = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001748 buf = pdata;
1749 }
1750 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001751 host->part_buf16 = mci_readw(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001752 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001753 }
1754}
1755
1756static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
1757{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001758 struct mmc_data *data = host->data;
1759 int init_cnt = cnt;
1760
James Hogan34b664a2011-06-24 13:57:56 +01001761 /* try and push anything in the part_buf */
1762 if (unlikely(host->part_buf_count)) {
1763 int len = dw_mci_push_part_bytes(host, buf, cnt);
1764 buf += len;
1765 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001766 if (host->part_buf_count == 4) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001767 mci_writel(host, DATA(host->data_offset),
1768 host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01001769 host->part_buf_count = 0;
1770 }
1771 }
1772#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1773 if (unlikely((unsigned long)buf & 0x3)) {
1774 while (cnt >= 4) {
1775 u32 aligned_buf[32];
1776 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1777 int items = len >> 2;
1778 int i;
1779 /* memcpy from input buffer into aligned buffer */
1780 memcpy(aligned_buf, buf, len);
1781 buf += len;
1782 cnt -= len;
1783 /* push data from aligned buffer into fifo */
1784 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001785 mci_writel(host, DATA(host->data_offset),
1786 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001787 }
1788 } else
1789#endif
1790 {
1791 u32 *pdata = buf;
1792 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001793 mci_writel(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001794 buf = pdata;
1795 }
1796 /* put anything remaining in the part_buf */
1797 if (cnt) {
1798 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001799 /* Push data if we have reached the expected data length */
1800 if ((data->bytes_xfered + init_cnt) ==
1801 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001802 mci_writel(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001803 host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05001804 }
1805}
1806
1807static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
1808{
James Hogan34b664a2011-06-24 13:57:56 +01001809#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1810 if (unlikely((unsigned long)buf & 0x3)) {
1811 while (cnt >= 4) {
1812 /* pull data from fifo into aligned buffer */
1813 u32 aligned_buf[32];
1814 int len = min(cnt & -4, (int)sizeof(aligned_buf));
1815 int items = len >> 2;
1816 int i;
1817 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001818 aligned_buf[i] = mci_readl(host,
1819 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001820 /* memcpy from aligned buffer into output buffer */
1821 memcpy(buf, aligned_buf, len);
1822 buf += len;
1823 cnt -= len;
1824 }
1825 } else
1826#endif
1827 {
1828 u32 *pdata = buf;
1829 for (; cnt >= 4; cnt -= 4)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001830 *pdata++ = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001831 buf = pdata;
1832 }
1833 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001834 host->part_buf32 = mci_readl(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001835 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001836 }
1837}
1838
1839static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
1840{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001841 struct mmc_data *data = host->data;
1842 int init_cnt = cnt;
1843
James Hogan34b664a2011-06-24 13:57:56 +01001844 /* try and push anything in the part_buf */
1845 if (unlikely(host->part_buf_count)) {
1846 int len = dw_mci_push_part_bytes(host, buf, cnt);
1847 buf += len;
1848 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09001849
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001850 if (host->part_buf_count == 8) {
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09001851 mci_writeq(host, DATA(host->data_offset),
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001852 host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01001853 host->part_buf_count = 0;
1854 }
1855 }
1856#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1857 if (unlikely((unsigned long)buf & 0x7)) {
1858 while (cnt >= 8) {
1859 u64 aligned_buf[16];
1860 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1861 int items = len >> 3;
1862 int i;
1863 /* memcpy from input buffer into aligned buffer */
1864 memcpy(aligned_buf, buf, len);
1865 buf += len;
1866 cnt -= len;
1867 /* push data from aligned buffer into fifo */
1868 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001869 mci_writeq(host, DATA(host->data_offset),
1870 aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001871 }
1872 } else
1873#endif
1874 {
1875 u64 *pdata = buf;
1876 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001877 mci_writeq(host, DATA(host->data_offset), *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001878 buf = pdata;
1879 }
1880 /* put anything remaining in the part_buf */
1881 if (cnt) {
1882 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001883 /* Push data if we have reached the expected data length */
1884 if ((data->bytes_xfered + init_cnt) ==
1885 (data->blksz * data->blocks))
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001886 mci_writeq(host, DATA(host->data_offset),
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001887 host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05001888 }
1889}
1890
1891static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
1892{
James Hogan34b664a2011-06-24 13:57:56 +01001893#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1894 if (unlikely((unsigned long)buf & 0x7)) {
1895 while (cnt >= 8) {
1896 /* pull data from fifo into aligned buffer */
1897 u64 aligned_buf[16];
1898 int len = min(cnt & -8, (int)sizeof(aligned_buf));
1899 int items = len >> 3;
1900 int i;
1901 for (i = 0; i < items; ++i)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001902 aligned_buf[i] = mci_readq(host,
1903 DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001904 /* memcpy from aligned buffer into output buffer */
1905 memcpy(buf, aligned_buf, len);
1906 buf += len;
1907 cnt -= len;
1908 }
1909 } else
1910#endif
1911 {
1912 u64 *pdata = buf;
1913 for (; cnt >= 8; cnt -= 8)
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001914 *pdata++ = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001915 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05001916 }
James Hogan34b664a2011-06-24 13:57:56 +01001917 if (cnt) {
Jaehoon Chung4e0a5ad2011-10-17 19:36:23 +09001918 host->part_buf = mci_readq(host, DATA(host->data_offset));
James Hogan34b664a2011-06-24 13:57:56 +01001919 dw_mci_pull_final_bytes(host, buf, cnt);
1920 }
1921}
1922
1923static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
1924{
1925 int len;
1926
1927 /* get remaining partial bytes */
1928 len = dw_mci_pull_part_bytes(host, buf, cnt);
1929 if (unlikely(len == cnt))
1930 return;
1931 buf += len;
1932 cnt -= len;
1933
1934 /* get the rest of the data */
1935 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05001936}
1937
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001938static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05001939{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001940 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1941 void *buf;
1942 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001943 struct mmc_data *data = host->data;
1944 int shift = host->data_shift;
1945 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001946 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001947 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05001948
1949 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001950 if (!sg_miter_next(sg_miter))
1951 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05001952
Imre Deak4225fc82013-02-27 17:02:57 -08001953 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001954 buf = sg_miter->addr;
1955 remain = sg_miter->length;
1956 offset = 0;
1957
1958 do {
1959 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
1960 << shift) + host->part_buf_count;
1961 len = min(remain, fcnt);
1962 if (!len)
1963 break;
1964 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04001965 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05001966 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001967 remain -= len;
1968 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05001969
Seungwon Jeone74f3a92012-08-01 09:30:46 +09001970 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001971 status = mci_readl(host, MINTSTS);
1972 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09001973 /* if the RXDR is ready read again */
1974 } while ((status & SDMMC_INT_RXDR) ||
1975 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001976
1977 if (!remain) {
1978 if (!sg_miter_next(sg_miter))
1979 goto done;
1980 sg_miter->consumed = 0;
1981 }
1982 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05001983 return;
1984
1985done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001986 sg_miter_stop(sg_miter);
1987 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05001988 smp_wmb();
1989 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
1990}
1991
1992static void dw_mci_write_data_pio(struct dw_mci *host)
1993{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001994 struct sg_mapping_iter *sg_miter = &host->sg_miter;
1995 void *buf;
1996 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05001997 struct mmc_data *data = host->data;
1998 int shift = host->data_shift;
1999 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002000 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002001 unsigned int fifo_depth = host->fifo_depth;
2002 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002003
2004 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002005 if (!sg_miter_next(sg_miter))
2006 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002007
Imre Deak4225fc82013-02-27 17:02:57 -08002008 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002009 buf = sg_miter->addr;
2010 remain = sg_miter->length;
2011 offset = 0;
2012
2013 do {
2014 fcnt = ((fifo_depth -
2015 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2016 << shift) - host->part_buf_count;
2017 len = min(remain, fcnt);
2018 if (!len)
2019 break;
2020 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002021 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002022 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002023 remain -= len;
2024 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002025
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002026 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002027 status = mci_readl(host, MINTSTS);
2028 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002029 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002030
2031 if (!remain) {
2032 if (!sg_miter_next(sg_miter))
2033 goto done;
2034 sg_miter->consumed = 0;
2035 }
2036 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002037 return;
2038
2039done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002040 sg_miter_stop(sg_miter);
2041 host->sg = NULL;
Will Newtonf95f3852011-01-02 01:11:59 -05002042 smp_wmb();
2043 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2044}
2045
2046static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2047{
2048 if (!host->cmd_status)
2049 host->cmd_status = status;
2050
2051 smp_wmb();
2052
2053 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2054 tasklet_schedule(&host->tasklet);
2055}
2056
Doug Anderson6130e7a2014-10-14 09:33:09 -07002057static void dw_mci_handle_cd(struct dw_mci *host)
2058{
2059 int i;
2060
2061 for (i = 0; i < host->num_slots; i++) {
2062 struct dw_mci_slot *slot = host->slot[i];
2063
2064 if (!slot)
2065 continue;
2066
2067 if (slot->mmc->ops->card_event)
2068 slot->mmc->ops->card_event(slot->mmc);
2069 mmc_detect_change(slot->mmc,
2070 msecs_to_jiffies(host->pdata->detect_delay_ms));
2071 }
2072}
2073
Will Newtonf95f3852011-01-02 01:11:59 -05002074static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2075{
2076 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002077 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302078 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05002079
Markos Chandras1fb5f682013-03-12 10:53:11 +00002080 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2081
Doug Anderson476d79f2013-07-09 13:04:40 -07002082 /*
2083 * DTO fix - version 2.10a and below, and only if internal DMA
2084 * is configured.
2085 */
2086 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2087 if (!pending &&
2088 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2089 pending |= SDMMC_INT_DATA_OVER;
2090 }
2091
Markos Chandras1fb5f682013-03-12 10:53:11 +00002092 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302093 /* Check volt switch first, since it can look like an error */
2094 if ((host->state == STATE_SENDING_CMD11) &&
2095 (pending & SDMMC_INT_VOLT_SWITCH)) {
2096 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2097 pending &= ~SDMMC_INT_VOLT_SWITCH;
2098 dw_mci_cmd_interrupt(host, pending);
2099 }
2100
Will Newtonf95f3852011-01-02 01:11:59 -05002101 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2102 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002103 host->cmd_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05002104 smp_wmb();
2105 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002106 }
2107
2108 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2109 /* if there is an error report DATA_ERROR */
2110 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002111 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05002112 smp_wmb();
2113 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002114 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002115 }
2116
2117 if (pending & SDMMC_INT_DATA_OVER) {
2118 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2119 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002120 host->data_status = pending;
Will Newtonf95f3852011-01-02 01:11:59 -05002121 smp_wmb();
2122 if (host->dir_status == DW_MCI_RECV_STATUS) {
2123 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002124 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002125 }
2126 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2127 tasklet_schedule(&host->tasklet);
2128 }
2129
2130 if (pending & SDMMC_INT_RXDR) {
2131 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002132 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002133 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002134 }
2135
2136 if (pending & SDMMC_INT_TXDR) {
2137 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002138 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002139 dw_mci_write_data_pio(host);
2140 }
2141
2142 if (pending & SDMMC_INT_CMD_DONE) {
2143 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002144 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002145 }
2146
2147 if (pending & SDMMC_INT_CD) {
2148 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002149 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002150 }
2151
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302152 /* Handle SDIO Interrupts */
2153 for (i = 0; i < host->num_slots; i++) {
2154 struct dw_mci_slot *slot = host->slot[i];
Addy Ke76756232014-11-04 22:03:09 +08002155 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2156 mci_writel(host, RINTSTS,
2157 SDMMC_INT_SDIO(slot->sdio_id));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302158 mmc_signal_sdio_irq(slot->mmc);
2159 }
2160 }
2161
Markos Chandras1fb5f682013-03-12 10:53:11 +00002162 }
Will Newtonf95f3852011-01-02 01:11:59 -05002163
2164#ifdef CONFIG_MMC_DW_IDMAC
2165 /* Handle DMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002166 if (host->dma_64bit_address == 1) {
2167 pending = mci_readl(host, IDSTS64);
2168 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2169 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2170 SDMMC_IDMAC_INT_RI);
2171 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
2172 host->dma_ops->complete(host);
2173 }
2174 } else {
2175 pending = mci_readl(host, IDSTS);
2176 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2177 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2178 SDMMC_IDMAC_INT_RI);
2179 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
2180 host->dma_ops->complete(host);
2181 }
Will Newtonf95f3852011-01-02 01:11:59 -05002182 }
2183#endif
2184
2185 return IRQ_HANDLED;
2186}
2187
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002188#ifdef CONFIG_OF
2189/* given a slot id, find out the device node representing that slot */
2190static struct device_node *dw_mci_of_find_slot_node(struct device *dev, u8 slot)
2191{
2192 struct device_node *np;
2193 const __be32 *addr;
2194 int len;
2195
2196 if (!dev || !dev->of_node)
2197 return NULL;
2198
2199 for_each_child_of_node(dev->of_node, np) {
2200 addr = of_get_property(np, "reg", &len);
2201 if (!addr || (len < sizeof(int)))
2202 continue;
2203 if (be32_to_cpup(addr) == slot)
2204 return np;
2205 }
2206 return NULL;
2207}
2208
Doug Andersona70aaa62013-01-11 17:03:50 +00002209static struct dw_mci_of_slot_quirks {
2210 char *quirk;
2211 int id;
2212} of_slot_quirks[] = {
2213 {
2214 .quirk = "disable-wp",
2215 .id = DW_MCI_SLOT_QUIRK_NO_WRITE_PROTECT,
2216 },
2217};
2218
2219static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2220{
2221 struct device_node *np = dw_mci_of_find_slot_node(dev, slot);
2222 int quirks = 0;
2223 int idx;
2224
2225 /* get quirks */
2226 for (idx = 0; idx < ARRAY_SIZE(of_slot_quirks); idx++)
Jaehoon Chung26375b52014-08-07 16:37:58 +09002227 if (of_get_property(np, of_slot_quirks[idx].quirk, NULL)) {
2228 dev_warn(dev, "Slot quirk %s is deprecated\n",
2229 of_slot_quirks[idx].quirk);
Doug Andersona70aaa62013-01-11 17:03:50 +00002230 quirks |= of_slot_quirks[idx].id;
Jaehoon Chung26375b52014-08-07 16:37:58 +09002231 }
Doug Andersona70aaa62013-01-11 17:03:50 +00002232
2233 return quirks;
2234}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002235#else /* CONFIG_OF */
Doug Andersona70aaa62013-01-11 17:03:50 +00002236static int dw_mci_of_get_slot_quirks(struct device *dev, u8 slot)
2237{
2238 return 0;
2239}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002240#endif /* CONFIG_OF */
2241
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002242static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002243{
2244 struct mmc_host *mmc;
2245 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002246 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002247 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002248 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002249
Thomas Abraham4a909202012-09-17 18:16:35 +00002250 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002251 if (!mmc)
2252 return -ENOMEM;
2253
2254 slot = mmc_priv(mmc);
2255 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002256 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002257 slot->mmc = mmc;
2258 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002259 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002260
Doug Andersona70aaa62013-01-11 17:03:50 +00002261 slot->quirks = dw_mci_of_get_slot_quirks(host->dev, slot->id);
2262
Will Newtonf95f3852011-01-02 01:11:59 -05002263 mmc->ops = &dw_mci_ops;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002264 if (of_property_read_u32_array(host->dev->of_node,
2265 "clock-freq-min-max", freq, 2)) {
2266 mmc->f_min = DW_MCI_FREQ_MIN;
2267 mmc->f_max = DW_MCI_FREQ_MAX;
2268 } else {
2269 mmc->f_min = freq[0];
2270 mmc->f_max = freq[1];
2271 }
Will Newtonf95f3852011-01-02 01:11:59 -05002272
Yuvaraj CD51da2242014-08-22 19:17:50 +05302273 /*if there are external regulators, get them*/
2274 ret = mmc_regulator_get_supply(mmc);
2275 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002276 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302277
2278 if (!mmc->ocr_avail)
2279 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002280
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002281 if (host->pdata->caps)
2282 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002283
Abhilash Kesavanab269122012-11-19 10:26:21 +05302284 if (host->pdata->pm_caps)
2285 mmc->pm_caps = host->pdata->pm_caps;
2286
Thomas Abraham800d78b2012-09-17 18:16:42 +00002287 if (host->dev->of_node) {
2288 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2289 if (ctrl_id < 0)
2290 ctrl_id = 0;
2291 } else {
2292 ctrl_id = to_platform_device(host->dev)->id;
2293 }
James Hogancb27a842012-10-16 09:43:08 +01002294 if (drv_data && drv_data->caps)
2295 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002296
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002297 if (host->pdata->caps2)
2298 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002299
Doug Anderson3cf890f2014-08-25 11:19:04 -07002300 ret = mmc_of_parse(mmc);
2301 if (ret)
2302 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002303
Will Newtonf95f3852011-01-02 01:11:59 -05002304 if (host->pdata->blk_settings) {
2305 mmc->max_segs = host->pdata->blk_settings->max_segs;
2306 mmc->max_blk_size = host->pdata->blk_settings->max_blk_size;
2307 mmc->max_blk_count = host->pdata->blk_settings->max_blk_count;
2308 mmc->max_req_size = host->pdata->blk_settings->max_req_size;
2309 mmc->max_seg_size = host->pdata->blk_settings->max_seg_size;
2310 } else {
2311 /* Useful defaults if platform data is unset. */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002312#ifdef CONFIG_MMC_DW_IDMAC
2313 mmc->max_segs = host->ring_size;
2314 mmc->max_blk_size = 65536;
2315 mmc->max_blk_count = host->ring_size;
2316 mmc->max_seg_size = 0x1000;
2317 mmc->max_req_size = mmc->max_seg_size * mmc->max_blk_count;
2318#else
Will Newtonf95f3852011-01-02 01:11:59 -05002319 mmc->max_segs = 64;
2320 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
2321 mmc->max_blk_count = 512;
2322 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
2323 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002324#endif /* CONFIG_MMC_DW_IDMAC */
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002325 }
Will Newtonf95f3852011-01-02 01:11:59 -05002326
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002327 if (dw_mci_get_cd(mmc))
2328 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2329 else
2330 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2331
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002332 ret = mmc_add_host(mmc);
2333 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002334 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002335
2336#if defined(CONFIG_DEBUG_FS)
2337 dw_mci_init_debugfs(slot);
2338#endif
2339
Will Newtonf95f3852011-01-02 01:11:59 -05002340 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002341
Doug Anderson3cf890f2014-08-25 11:19:04 -07002342err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002343 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302344 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002345}
2346
2347static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2348{
Will Newtonf95f3852011-01-02 01:11:59 -05002349 /* Debugfs stuff is cleaned up by mmc core */
2350 mmc_remove_host(slot->mmc);
2351 slot->host->slot[id] = NULL;
2352 mmc_free_host(slot->mmc);
2353}
2354
2355static void dw_mci_init_dma(struct dw_mci *host)
2356{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002357 int addr_config;
2358 /* Check ADDR_CONFIG bit in HCON to find IDMAC address bus width */
2359 addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
2360
2361 if (addr_config == 1) {
2362 /* host supports IDMAC in 64-bit address mode */
2363 host->dma_64bit_address = 1;
2364 dev_info(host->dev, "IDMAC supports 64-bit address mode.\n");
2365 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2366 dma_set_coherent_mask(host->dev, DMA_BIT_MASK(64));
2367 } else {
2368 /* host supports IDMAC in 32-bit address mode */
2369 host->dma_64bit_address = 0;
2370 dev_info(host->dev, "IDMAC supports 32-bit address mode.\n");
2371 }
2372
Will Newtonf95f3852011-01-02 01:11:59 -05002373 /* Alloc memory for sg translation */
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002374 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
Will Newtonf95f3852011-01-02 01:11:59 -05002375 &host->sg_dma, GFP_KERNEL);
2376 if (!host->sg_cpu) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002377 dev_err(host->dev, "%s: could not alloc DMA memory\n",
Will Newtonf95f3852011-01-02 01:11:59 -05002378 __func__);
2379 goto no_dma;
2380 }
2381
2382 /* Determine which DMA interface to use */
2383#ifdef CONFIG_MMC_DW_IDMAC
2384 host->dma_ops = &dw_mci_idmac_ops;
Seungwon Jeon00956ea2012-09-28 19:13:11 +09002385 dev_info(host->dev, "Using internal DMA controller.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002386#endif
2387
2388 if (!host->dma_ops)
2389 goto no_dma;
2390
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002391 if (host->dma_ops->init && host->dma_ops->start &&
2392 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002393 if (host->dma_ops->init(host)) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002394 dev_err(host->dev, "%s: Unable to initialize "
Will Newtonf95f3852011-01-02 01:11:59 -05002395 "DMA Controller.\n", __func__);
2396 goto no_dma;
2397 }
2398 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002399 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002400 goto no_dma;
2401 }
2402
2403 host->use_dma = 1;
2404 return;
2405
2406no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002407 dev_info(host->dev, "Using PIO mode.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002408 host->use_dma = 0;
2409 return;
2410}
2411
Seungwon Jeon31bff452013-08-31 00:14:23 +09002412static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
Will Newtonf95f3852011-01-02 01:11:59 -05002413{
2414 unsigned long timeout = jiffies + msecs_to_jiffies(500);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002415 u32 ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05002416
Seungwon Jeon31bff452013-08-31 00:14:23 +09002417 ctrl = mci_readl(host, CTRL);
2418 ctrl |= reset;
2419 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05002420
2421 /* wait till resets clear */
2422 do {
2423 ctrl = mci_readl(host, CTRL);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002424 if (!(ctrl & reset))
Will Newtonf95f3852011-01-02 01:11:59 -05002425 return true;
2426 } while (time_before(jiffies, timeout));
2427
Seungwon Jeon31bff452013-08-31 00:14:23 +09002428 dev_err(host->dev,
2429 "Timeout resetting block (ctrl reset %#x)\n",
2430 ctrl & reset);
Will Newtonf95f3852011-01-02 01:11:59 -05002431
2432 return false;
2433}
2434
Sonny Rao3a33a942014-08-04 18:19:50 -07002435static bool dw_mci_reset(struct dw_mci *host)
Seungwon Jeon31bff452013-08-31 00:14:23 +09002436{
Sonny Rao3a33a942014-08-04 18:19:50 -07002437 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2438 bool ret = false;
2439
Seungwon Jeon31bff452013-08-31 00:14:23 +09002440 /*
2441 * Reseting generates a block interrupt, hence setting
2442 * the scatter-gather pointer to NULL.
2443 */
2444 if (host->sg) {
2445 sg_miter_stop(&host->sg_miter);
2446 host->sg = NULL;
2447 }
2448
Sonny Rao3a33a942014-08-04 18:19:50 -07002449 if (host->use_dma)
2450 flags |= SDMMC_CTRL_DMA_RESET;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002451
Sonny Rao3a33a942014-08-04 18:19:50 -07002452 if (dw_mci_ctrl_reset(host, flags)) {
2453 /*
2454 * In all cases we clear the RAWINTS register to clear any
2455 * interrupts.
2456 */
2457 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2458
2459 /* if using dma we wait for dma_req to clear */
2460 if (host->use_dma) {
2461 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2462 u32 status;
2463 do {
2464 status = mci_readl(host, STATUS);
2465 if (!(status & SDMMC_STATUS_DMA_REQ))
2466 break;
2467 cpu_relax();
2468 } while (time_before(jiffies, timeout));
2469
2470 if (status & SDMMC_STATUS_DMA_REQ) {
2471 dev_err(host->dev,
2472 "%s: Timeout waiting for dma_req to "
2473 "clear during reset\n", __func__);
2474 goto ciu_out;
2475 }
2476
2477 /* when using DMA next we reset the fifo again */
2478 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2479 goto ciu_out;
2480 }
2481 } else {
2482 /* if the controller reset bit did clear, then set clock regs */
2483 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
2484 dev_err(host->dev, "%s: fifo/dma reset bits didn't "
2485 "clear but ciu was reset, doing clock update\n",
2486 __func__);
2487 goto ciu_out;
2488 }
2489 }
2490
2491#if IS_ENABLED(CONFIG_MMC_DW_IDMAC)
2492 /* It is also recommended that we reset and reprogram idmac */
2493 dw_mci_idmac_reset(host);
2494#endif
2495
2496 ret = true;
2497
2498ciu_out:
2499 /* After a CTRL reset we need to have CIU set clock registers */
2500 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2501
2502 return ret;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002503}
2504
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002505#ifdef CONFIG_OF
2506static struct dw_mci_of_quirks {
2507 char *quirk;
2508 int id;
2509} of_quirks[] = {
2510 {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002511 .quirk = "broken-cd",
2512 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
Jaehoon Chung26375b52014-08-07 16:37:58 +09002513 }, {
2514 .quirk = "disable-wp",
2515 .id = DW_MCI_QUIRK_NO_WRITE_PROTECT,
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002516 },
2517};
2518
2519static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2520{
2521 struct dw_mci_board *pdata;
2522 struct device *dev = host->dev;
2523 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002524 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002525 int idx, ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002526 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002527
2528 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
2529 if (!pdata) {
2530 dev_err(dev, "could not allocate memory for pdata\n");
2531 return ERR_PTR(-ENOMEM);
2532 }
2533
2534 /* find out number of slots supported */
2535 if (of_property_read_u32(dev->of_node, "num-slots",
2536 &pdata->num_slots)) {
2537 dev_info(dev, "num-slots property not found, "
2538 "assuming 1 slot is available\n");
2539 pdata->num_slots = 1;
2540 }
2541
2542 /* get quirks */
2543 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2544 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2545 pdata->quirks |= of_quirks[idx].id;
2546
2547 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
2548 dev_info(dev, "fifo-depth property not found, using "
2549 "value of FIFOTH register as default\n");
2550
2551 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2552
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002553 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2554 pdata->bus_hz = clock_frequency;
2555
James Hogancb27a842012-10-16 09:43:08 +01002556 if (drv_data && drv_data->parse_dt) {
2557 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002558 if (ret)
2559 return ERR_PTR(ret);
2560 }
2561
Seungwon Jeon10b49842013-08-31 00:13:22 +09002562 if (of_find_property(np, "supports-highspeed", NULL))
2563 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
2564
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002565 return pdata;
2566}
2567
2568#else /* CONFIG_OF */
2569static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2570{
2571 return ERR_PTR(-EINVAL);
2572}
2573#endif /* CONFIG_OF */
2574
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302575int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002576{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002577 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302578 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002579 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002580 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002581
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002582 if (!host->pdata) {
2583 host->pdata = dw_mci_parse_dt(host);
2584 if (IS_ERR(host->pdata)) {
2585 dev_err(host->dev, "platform data not available\n");
2586 return -EINVAL;
2587 }
Will Newtonf95f3852011-01-02 01:11:59 -05002588 }
2589
Jaehoon Chung907abd52014-03-03 11:36:43 +09002590 if (host->pdata->num_slots > 1) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002591 dev_err(host->dev,
Jaehoon Chung907abd52014-03-03 11:36:43 +09002592 "Platform data must supply num_slots.\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302593 return -ENODEV;
Will Newtonf95f3852011-01-02 01:11:59 -05002594 }
2595
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002596 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002597 if (IS_ERR(host->biu_clk)) {
2598 dev_dbg(host->dev, "biu clock not available\n");
2599 } else {
2600 ret = clk_prepare_enable(host->biu_clk);
2601 if (ret) {
2602 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002603 return ret;
2604 }
Will Newtonf95f3852011-01-02 01:11:59 -05002605 }
2606
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002607 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002608 if (IS_ERR(host->ciu_clk)) {
2609 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002610 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002611 } else {
2612 ret = clk_prepare_enable(host->ciu_clk);
2613 if (ret) {
2614 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002615 goto err_clk_biu;
2616 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002617
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002618 if (host->pdata->bus_hz) {
2619 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
2620 if (ret)
2621 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09002622 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002623 host->pdata->bus_hz);
2624 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002625 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002626 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002627
Jaehoon Chung612de4c2014-03-03 11:36:42 +09002628 if (!host->bus_hz) {
2629 dev_err(host->dev,
2630 "Platform data must supply bus speed\n");
2631 ret = -ENODEV;
2632 goto err_clk_ciu;
2633 }
2634
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09002635 if (drv_data && drv_data->init) {
2636 ret = drv_data->init(host);
2637 if (ret) {
2638 dev_err(host->dev,
2639 "implementation specific init failed\n");
2640 goto err_clk_ciu;
2641 }
2642 }
2643
James Hogancb27a842012-10-16 09:43:08 +01002644 if (drv_data && drv_data->setup_clock) {
2645 ret = drv_data->setup_clock(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002646 if (ret) {
2647 dev_err(host->dev,
2648 "implementation specific clock setup failed\n");
2649 goto err_clk_ciu;
2650 }
2651 }
2652
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302653 host->quirks = host->pdata->quirks;
Will Newtonf95f3852011-01-02 01:11:59 -05002654
2655 spin_lock_init(&host->lock);
2656 INIT_LIST_HEAD(&host->queue);
2657
Will Newtonf95f3852011-01-02 01:11:59 -05002658 /*
2659 * Get the host data width - this assumes that HCON has been set with
2660 * the correct values.
2661 */
2662 i = (mci_readl(host, HCON) >> 7) & 0x7;
2663 if (!i) {
2664 host->push_data = dw_mci_push_data16;
2665 host->pull_data = dw_mci_pull_data16;
2666 width = 16;
2667 host->data_shift = 1;
2668 } else if (i == 2) {
2669 host->push_data = dw_mci_push_data64;
2670 host->pull_data = dw_mci_pull_data64;
2671 width = 64;
2672 host->data_shift = 3;
2673 } else {
2674 /* Check for a reserved value, and warn if it is */
2675 WARN((i != 1),
2676 "HCON reports a reserved host data width!\n"
2677 "Defaulting to 32-bit access.\n");
2678 host->push_data = dw_mci_push_data32;
2679 host->pull_data = dw_mci_pull_data32;
2680 width = 32;
2681 host->data_shift = 2;
2682 }
2683
2684 /* Reset all blocks */
Sonny Rao3a33a942014-08-04 18:19:50 -07002685 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
Seungwon Jeon141a7122012-05-22 13:01:03 +09002686 return -ENODEV;
2687
2688 host->dma_ops = host->pdata->dma_ops;
2689 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002690
2691 /* Clear the interrupts for the host controller */
2692 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2693 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2694
2695 /* Put in max timeout */
2696 mci_writel(host, TMOUT, 0xFFFFFFFF);
2697
2698 /*
2699 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
2700 * Tx Mark = fifo_size / 2 DMA Size = 8
2701 */
James Hoganb86d8252011-06-24 13:57:18 +01002702 if (!host->pdata->fifo_depth) {
2703 /*
2704 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
2705 * have been overwritten by the bootloader, just like we're
2706 * about to do, so if you know the value for your hardware, you
2707 * should put it in the platform data.
2708 */
2709 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00002710 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01002711 } else {
2712 fifo_size = host->pdata->fifo_depth;
2713 }
2714 host->fifo_depth = fifo_size;
Seungwon Jeon52426892013-08-31 00:13:42 +09002715 host->fifoth_val =
2716 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002717 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05002718
2719 /* disable clock to CIU */
2720 mci_writel(host, CLKENA, 0);
2721 mci_writel(host, CLKSRC, 0);
2722
James Hogan63008762013-03-12 10:43:54 +00002723 /*
2724 * In 2.40a spec, Data offset is changed.
2725 * Need to check the version-id and set data-offset for DATA register.
2726 */
2727 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
2728 dev_info(host->dev, "Version ID is %04x\n", host->verid);
2729
2730 if (host->verid < DW_MMC_240A)
2731 host->data_offset = DATA_OFFSET;
2732 else
2733 host->data_offset = DATA_240A_OFFSET;
2734
Will Newtonf95f3852011-01-02 01:11:59 -05002735 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002736 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
2737 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05002738 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07002739 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05002740
Will Newtonf95f3852011-01-02 01:11:59 -05002741 if (host->pdata->num_slots)
2742 host->num_slots = host->pdata->num_slots;
2743 else
2744 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
2745
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05302746 /*
2747 * Enable interrupts for command done, data over, data empty, card det,
2748 * receive ready and error such as transmit, receive timeout, crc error
2749 */
2750 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2751 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2752 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2753 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2754 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); /* Enable mci interrupt */
2755
2756 dev_info(host->dev, "DW MMC controller at irq %d, "
2757 "%d bit host data width, "
2758 "%u deep fifo\n",
2759 host->irq, width, fifo_size);
2760
Will Newtonf95f3852011-01-02 01:11:59 -05002761 /* We need at least one slot to succeed */
2762 for (i = 0; i < host->num_slots; i++) {
2763 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002764 if (ret)
2765 dev_dbg(host->dev, "slot %d init failed\n", i);
2766 else
2767 init_slots++;
2768 }
2769
2770 if (init_slots) {
2771 dev_info(host->dev, "%d slots initialized\n", init_slots);
2772 } else {
2773 dev_dbg(host->dev, "attempted to initialize %d slots, "
2774 "but failed on all\n", host->num_slots);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002775 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05002776 }
2777
Will Newtonf95f3852011-01-02 01:11:59 -05002778 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
Thomas Abraham4a909202012-09-17 18:16:35 +00002779 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002780
2781 return 0;
2782
Will Newtonf95f3852011-01-02 01:11:59 -05002783err_dmaunmap:
2784 if (host->use_dma && host->dma_ops->exit)
2785 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002786
2787err_clk_ciu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002788 if (!IS_ERR(host->ciu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002789 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002790
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002791err_clk_biu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002792 if (!IS_ERR(host->biu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002793 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002794
Will Newtonf95f3852011-01-02 01:11:59 -05002795 return ret;
2796}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302797EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05002798
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302799void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002800{
Will Newtonf95f3852011-01-02 01:11:59 -05002801 int i;
2802
2803 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2804 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
2805
Will Newtonf95f3852011-01-02 01:11:59 -05002806 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002807 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05002808 if (host->slot[i])
2809 dw_mci_cleanup_slot(host->slot[i], i);
2810 }
2811
2812 /* disable clock to CIU */
2813 mci_writel(host, CLKENA, 0);
2814 mci_writel(host, CLKSRC, 0);
2815
Will Newtonf95f3852011-01-02 01:11:59 -05002816 if (host->use_dma && host->dma_ops->exit)
2817 host->dma_ops->exit(host);
2818
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002819 if (!IS_ERR(host->ciu_clk))
2820 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002821
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002822 if (!IS_ERR(host->biu_clk))
2823 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05002824}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302825EXPORT_SYMBOL(dw_mci_remove);
2826
2827
Will Newtonf95f3852011-01-02 01:11:59 -05002828
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002829#ifdef CONFIG_PM_SLEEP
Will Newtonf95f3852011-01-02 01:11:59 -05002830/*
2831 * TODO: we should probably disable the clock to the card in the suspend path.
2832 */
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302833int dw_mci_suspend(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002834{
Will Newtonf95f3852011-01-02 01:11:59 -05002835 return 0;
2836}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302837EXPORT_SYMBOL(dw_mci_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05002838
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302839int dw_mci_resume(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002840{
2841 int i, ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002842
Sonny Rao3a33a942014-08-04 18:19:50 -07002843 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002844 ret = -ENODEV;
2845 return ret;
2846 }
2847
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04002848 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09002849 host->dma_ops->init(host);
2850
Seungwon Jeon52426892013-08-31 00:13:42 +09002851 /*
2852 * Restore the initial value at FIFOTH register
2853 * And Invalidate the prev_blksz with zero
2854 */
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002855 mci_writel(host, FIFOTH, host->fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +09002856 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002857
Doug Anderson2eb29442013-08-31 00:11:49 +09002858 /* Put in max timeout */
2859 mci_writel(host, TMOUT, 0xFFFFFFFF);
2860
Jaehoon Chunge61cf112011-03-17 20:32:33 +09002861 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2862 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
2863 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
2864 DW_MCI_ERROR_FLAGS | SDMMC_INT_CD);
2865 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
2866
Will Newtonf95f3852011-01-02 01:11:59 -05002867 for (i = 0; i < host->num_slots; i++) {
2868 struct dw_mci_slot *slot = host->slot[i];
2869 if (!slot)
2870 continue;
Abhilash Kesavanab269122012-11-19 10:26:21 +05302871 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
2872 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
2873 dw_mci_setup_bus(slot, true);
2874 }
Will Newtonf95f3852011-01-02 01:11:59 -05002875 }
Will Newtonf95f3852011-01-02 01:11:59 -05002876 return 0;
2877}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302878EXPORT_SYMBOL(dw_mci_resume);
Jaehoon Chung6fe88902011-12-08 19:23:03 +09002879#endif /* CONFIG_PM_SLEEP */
2880
Will Newtonf95f3852011-01-02 01:11:59 -05002881static int __init dw_mci_init(void)
2882{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05302883 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302884 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002885}
2886
2887static void __exit dw_mci_exit(void)
2888{
Will Newtonf95f3852011-01-02 01:11:59 -05002889}
2890
2891module_init(dw_mci_init);
2892module_exit(dw_mci_exit);
2893
2894MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
2895MODULE_AUTHOR("NXP Semiconductor VietNam");
2896MODULE_AUTHOR("Imagination Technologies Ltd");
2897MODULE_LICENSE("GPL v2");