blob: 7fe0315142e6f080f90fde76f5607e436f0289a0 [file] [log] [blame]
Will Newtonf95f3852011-01-02 01:11:59 -05001/*
2 * Synopsys DesignWare Multimedia Card Interface driver
3 * (Based on NXP driver for lpc 31xx)
4 *
5 * Copyright (C) 2009 NXP Semiconductors
6 * Copyright (C) 2009, 2010 Imagination Technologies Ltd.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 */
13
14#include <linux/blkdev.h>
15#include <linux/clk.h>
16#include <linux/debugfs.h>
17#include <linux/device.h>
18#include <linux/dma-mapping.h>
19#include <linux/err.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/ioport.h>
23#include <linux/module.h>
24#include <linux/platform_device.h>
Will Newtonf95f3852011-01-02 01:11:59 -050025#include <linux/seq_file.h>
26#include <linux/slab.h>
27#include <linux/stat.h>
28#include <linux/delay.h>
29#include <linux/irq.h>
Doug Andersonb24c8b22014-12-02 15:42:46 -080030#include <linux/mmc/card.h>
Will Newtonf95f3852011-01-02 01:11:59 -050031#include <linux/mmc/host.h>
32#include <linux/mmc/mmc.h>
Doug Anderson01730552014-08-22 19:17:51 +053033#include <linux/mmc/sd.h>
Seungwon Jeon90c21432013-08-31 00:14:05 +090034#include <linux/mmc/sdio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050035#include <linux/mmc/dw_mmc.h>
36#include <linux/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090037#include <linux/regulator/consumer.h>
Thomas Abrahamc91eab42012-09-17 18:16:40 +000038#include <linux/of.h>
Doug Anderson55a6ceb2013-01-11 17:03:53 +000039#include <linux/of_gpio.h>
Zhangfei Gaobf626e52014-01-09 22:35:10 +080040#include <linux/mmc/slot-gpio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050041
42#include "dw_mmc.h"
43
44/* Common flag combinations */
Jaehoon Chung3f7eec62013-05-27 13:47:57 +090045#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
Will Newtonf95f3852011-01-02 01:11:59 -050046 SDMMC_INT_HTO | SDMMC_INT_SBE | \
47 SDMMC_INT_EBE)
48#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
49 SDMMC_INT_RESP_ERR)
50#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
51 DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_HLE)
52#define DW_MCI_SEND_STATUS 1
53#define DW_MCI_RECV_STATUS 2
54#define DW_MCI_DMA_THRESHOLD 16
55
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090056#define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
57#define DW_MCI_FREQ_MIN 400000 /* unit: HZ */
58
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) \
Ben Dooks6687c422015-03-25 11:27:51 +000071 ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
72 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000073
74 u32 des3; /* Reserved */
75
76 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
77 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
78
79 u32 des6; /* Lower 32-bits of Next Descriptor Address */
80 u32 des7; /* Upper 32-bits of Next Descriptor Address */
81};
82
Will Newtonf95f3852011-01-02 01:11:59 -050083struct idmac_desc {
Ben Dooks6687c422015-03-25 11:27:51 +000084 __le32 des0; /* Control Descriptor */
Will Newtonf95f3852011-01-02 01:11:59 -050085#define IDMAC_DES0_DIC BIT(1)
86#define IDMAC_DES0_LD BIT(2)
87#define IDMAC_DES0_FD BIT(3)
88#define IDMAC_DES0_CH BIT(4)
89#define IDMAC_DES0_ER BIT(5)
90#define IDMAC_DES0_CES BIT(30)
91#define IDMAC_DES0_OWN BIT(31)
92
Ben Dooks6687c422015-03-25 11:27:51 +000093 __le32 des1; /* Buffer sizes */
Will Newtonf95f3852011-01-02 01:11:59 -050094#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Shashidhar Hiremath9b7bbe12011-07-29 08:49:50 -040095 ((d)->des1 = ((d)->des1 & 0x03ffe000) | ((s) & 0x1fff))
Will Newtonf95f3852011-01-02 01:11:59 -050096
Ben Dooks6687c422015-03-25 11:27:51 +000097 __le32 des2; /* buffer 1 physical address */
Will Newtonf95f3852011-01-02 01:11:59 -050098
Ben Dooks6687c422015-03-25 11:27:51 +000099 __le32 des3; /* buffer 2 physical address */
Will Newtonf95f3852011-01-02 01:11:59 -0500100};
Alexey Brodkin5959b322015-06-25 11:25:07 +0300101
102/* Each descriptor can transfer up to 4KB of data in chained mode */
103#define DW_MCI_DESC_DATA_LENGTH 0x1000
Will Newtonf95f3852011-01-02 01:11:59 -0500104
Sonny Rao3a33a942014-08-04 18:19:50 -0700105static bool dw_mci_reset(struct dw_mci *host);
Sonny Rao536f6b92014-10-16 09:58:05 -0700106static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800107static int dw_mci_card_busy(struct mmc_host *mmc);
Seungwon Jeon31bff452013-08-31 00:14:23 +0900108
Will Newtonf95f3852011-01-02 01:11:59 -0500109#if defined(CONFIG_DEBUG_FS)
110static int dw_mci_req_show(struct seq_file *s, void *v)
111{
112 struct dw_mci_slot *slot = s->private;
113 struct mmc_request *mrq;
114 struct mmc_command *cmd;
115 struct mmc_command *stop;
116 struct mmc_data *data;
117
118 /* Make sure we get a consistent snapshot */
119 spin_lock_bh(&slot->host->lock);
120 mrq = slot->mrq;
121
122 if (mrq) {
123 cmd = mrq->cmd;
124 data = mrq->data;
125 stop = mrq->stop;
126
127 if (cmd)
128 seq_printf(s,
129 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
130 cmd->opcode, cmd->arg, cmd->flags,
131 cmd->resp[0], cmd->resp[1], cmd->resp[2],
132 cmd->resp[2], cmd->error);
133 if (data)
134 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
135 data->bytes_xfered, data->blocks,
136 data->blksz, data->flags, data->error);
137 if (stop)
138 seq_printf(s,
139 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
140 stop->opcode, stop->arg, stop->flags,
141 stop->resp[0], stop->resp[1], stop->resp[2],
142 stop->resp[2], stop->error);
143 }
144
145 spin_unlock_bh(&slot->host->lock);
146
147 return 0;
148}
149
150static int dw_mci_req_open(struct inode *inode, struct file *file)
151{
152 return single_open(file, dw_mci_req_show, inode->i_private);
153}
154
155static const struct file_operations dw_mci_req_fops = {
156 .owner = THIS_MODULE,
157 .open = dw_mci_req_open,
158 .read = seq_read,
159 .llseek = seq_lseek,
160 .release = single_release,
161};
162
163static int dw_mci_regs_show(struct seq_file *s, void *v)
164{
165 seq_printf(s, "STATUS:\t0x%08x\n", SDMMC_STATUS);
166 seq_printf(s, "RINTSTS:\t0x%08x\n", SDMMC_RINTSTS);
167 seq_printf(s, "CMD:\t0x%08x\n", SDMMC_CMD);
168 seq_printf(s, "CTRL:\t0x%08x\n", SDMMC_CTRL);
169 seq_printf(s, "INTMASK:\t0x%08x\n", SDMMC_INTMASK);
170 seq_printf(s, "CLKENA:\t0x%08x\n", SDMMC_CLKENA);
171
172 return 0;
173}
174
175static int dw_mci_regs_open(struct inode *inode, struct file *file)
176{
177 return single_open(file, dw_mci_regs_show, inode->i_private);
178}
179
180static const struct file_operations dw_mci_regs_fops = {
181 .owner = THIS_MODULE,
182 .open = dw_mci_regs_open,
183 .read = seq_read,
184 .llseek = seq_lseek,
185 .release = single_release,
186};
187
188static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
189{
190 struct mmc_host *mmc = slot->mmc;
191 struct dw_mci *host = slot->host;
192 struct dentry *root;
193 struct dentry *node;
194
195 root = mmc->debugfs_root;
196 if (!root)
197 return;
198
199 node = debugfs_create_file("regs", S_IRUSR, root, host,
200 &dw_mci_regs_fops);
201 if (!node)
202 goto err;
203
204 node = debugfs_create_file("req", S_IRUSR, root, slot,
205 &dw_mci_req_fops);
206 if (!node)
207 goto err;
208
209 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
210 if (!node)
211 goto err;
212
213 node = debugfs_create_x32("pending_events", S_IRUSR, root,
214 (u32 *)&host->pending_events);
215 if (!node)
216 goto err;
217
218 node = debugfs_create_x32("completed_events", S_IRUSR, root,
219 (u32 *)&host->completed_events);
220 if (!node)
221 goto err;
222
223 return;
224
225err:
226 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
227}
228#endif /* defined(CONFIG_DEBUG_FS) */
229
Doug Anderson01730552014-08-22 19:17:51 +0530230static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
231
Will Newtonf95f3852011-01-02 01:11:59 -0500232static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
233{
234 struct mmc_data *data;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000235 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson01730552014-08-22 19:17:51 +0530236 struct dw_mci *host = slot->host;
Arnd Bergmanne95baf12012-11-08 14:26:11 +0000237 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Will Newtonf95f3852011-01-02 01:11:59 -0500238 u32 cmdr;
Will Newtonf95f3852011-01-02 01:11:59 -0500239
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800240 cmd->error = -EINPROGRESS;
Will Newtonf95f3852011-01-02 01:11:59 -0500241 cmdr = cmd->opcode;
242
Seungwon Jeon90c21432013-08-31 00:14:05 +0900243 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
244 cmd->opcode == MMC_GO_IDLE_STATE ||
245 cmd->opcode == MMC_GO_INACTIVE_STATE ||
246 (cmd->opcode == SD_IO_RW_DIRECT &&
247 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
Will Newtonf95f3852011-01-02 01:11:59 -0500248 cmdr |= SDMMC_CMD_STOP;
Jaehoon Chung4a1b27a2014-03-03 11:36:44 +0900249 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
250 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500251
Doug Anderson01730552014-08-22 19:17:51 +0530252 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
253 u32 clk_en_a;
254
255 /* Special bit makes CMD11 not die */
256 cmdr |= SDMMC_CMD_VOLT_SWITCH;
257
258 /* Change state to continue to handle CMD11 weirdness */
259 WARN_ON(slot->host->state != STATE_SENDING_CMD);
260 slot->host->state = STATE_SENDING_CMD11;
261
262 /*
263 * We need to disable low power mode (automatic clock stop)
264 * while doing voltage switch so we don't confuse the card,
265 * since stopping the clock is a specific part of the UHS
266 * voltage change dance.
267 *
268 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
269 * unconditionally turned back on in dw_mci_setup_bus() if it's
270 * ever called with a non-zero clock. That shouldn't happen
271 * until the voltage change is all done.
272 */
273 clk_en_a = mci_readl(host, CLKENA);
274 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
275 mci_writel(host, CLKENA, clk_en_a);
276 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
277 SDMMC_CMD_PRV_DAT_WAIT, 0);
278 }
279
Will Newtonf95f3852011-01-02 01:11:59 -0500280 if (cmd->flags & MMC_RSP_PRESENT) {
281 /* We expect a response, so set this bit */
282 cmdr |= SDMMC_CMD_RESP_EXP;
283 if (cmd->flags & MMC_RSP_136)
284 cmdr |= SDMMC_CMD_RESP_LONG;
285 }
286
287 if (cmd->flags & MMC_RSP_CRC)
288 cmdr |= SDMMC_CMD_RESP_CRC;
289
290 data = cmd->data;
291 if (data) {
292 cmdr |= SDMMC_CMD_DAT_EXP;
293 if (data->flags & MMC_DATA_STREAM)
294 cmdr |= SDMMC_CMD_STRM_MODE;
295 if (data->flags & MMC_DATA_WRITE)
296 cmdr |= SDMMC_CMD_DAT_WR;
297 }
298
James Hogancb27a842012-10-16 09:43:08 +0100299 if (drv_data && drv_data->prepare_command)
300 drv_data->prepare_command(slot->host, &cmdr);
Thomas Abraham800d78b2012-09-17 18:16:42 +0000301
Will Newtonf95f3852011-01-02 01:11:59 -0500302 return cmdr;
303}
304
Seungwon Jeon90c21432013-08-31 00:14:05 +0900305static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
306{
307 struct mmc_command *stop;
308 u32 cmdr;
309
310 if (!cmd->data)
311 return 0;
312
313 stop = &host->stop_abort;
314 cmdr = cmd->opcode;
315 memset(stop, 0, sizeof(struct mmc_command));
316
317 if (cmdr == MMC_READ_SINGLE_BLOCK ||
318 cmdr == MMC_READ_MULTIPLE_BLOCK ||
319 cmdr == MMC_WRITE_BLOCK ||
Ulf Hansson6c2c6502014-12-01 16:13:39 +0100320 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
321 cmdr == MMC_SEND_TUNING_BLOCK ||
322 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
Seungwon Jeon90c21432013-08-31 00:14:05 +0900323 stop->opcode = MMC_STOP_TRANSMISSION;
324 stop->arg = 0;
325 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
326 } else if (cmdr == SD_IO_RW_EXTENDED) {
327 stop->opcode = SD_IO_RW_DIRECT;
328 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
329 ((cmd->arg >> 28) & 0x7);
330 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
331 } else {
332 return 0;
333 }
334
335 cmdr = stop->opcode | SDMMC_CMD_STOP |
336 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
337
338 return cmdr;
339}
340
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800341static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
342{
343 unsigned long timeout = jiffies + msecs_to_jiffies(500);
344
345 /*
346 * Databook says that before issuing a new data transfer command
347 * we need to check to see if the card is busy. Data transfer commands
348 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
349 *
350 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
351 * expected.
352 */
353 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
354 !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
355 while (mci_readl(host, STATUS) & SDMMC_STATUS_BUSY) {
356 if (time_after(jiffies, timeout)) {
357 /* Command will fail; we'll pass error then */
358 dev_err(host->dev, "Busy; trying anyway\n");
359 break;
360 }
361 udelay(10);
362 }
363 }
364}
365
Will Newtonf95f3852011-01-02 01:11:59 -0500366static void dw_mci_start_command(struct dw_mci *host,
367 struct mmc_command *cmd, u32 cmd_flags)
368{
369 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000370 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500371 "start command: ARGR=0x%08x CMDR=0x%08x\n",
372 cmd->arg, cmd_flags);
373
374 mci_writel(host, CMDARG, cmd->arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800375 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800376 dw_mci_wait_while_busy(host, cmd_flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500377
378 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
379}
380
Seungwon Jeon90c21432013-08-31 00:14:05 +0900381static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
Will Newtonf95f3852011-01-02 01:11:59 -0500382{
Seungwon Jeon90c21432013-08-31 00:14:05 +0900383 struct mmc_command *stop = data->stop ? data->stop : &host->stop_abort;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800384
Seungwon Jeon90c21432013-08-31 00:14:05 +0900385 dw_mci_start_command(host, stop, host->stop_cmdr);
Will Newtonf95f3852011-01-02 01:11:59 -0500386}
387
388/* DMA interface functions */
389static void dw_mci_stop_dma(struct dw_mci *host)
390{
James Hogan03e8cb52011-06-29 09:28:43 +0100391 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500392 host->dma_ops->stop(host);
393 host->dma_ops->cleanup(host);
Will Newtonf95f3852011-01-02 01:11:59 -0500394 }
Seungwon Jeonaa50f252013-08-31 00:14:38 +0900395
396 /* Data transfer was stopped by the interrupt handler */
397 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -0500398}
399
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900400static int dw_mci_get_dma_dir(struct mmc_data *data)
401{
402 if (data->flags & MMC_DATA_WRITE)
403 return DMA_TO_DEVICE;
404 else
405 return DMA_FROM_DEVICE;
406}
407
Will Newtonf95f3852011-01-02 01:11:59 -0500408static void dw_mci_dma_cleanup(struct dw_mci *host)
409{
410 struct mmc_data *data = host->data;
411
412 if (data)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900413 if (!data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000414 dma_unmap_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900415 data->sg,
416 data->sg_len,
417 dw_mci_get_dma_dir(data));
Will Newtonf95f3852011-01-02 01:11:59 -0500418}
419
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900420static void dw_mci_idmac_reset(struct dw_mci *host)
421{
422 u32 bmod = mci_readl(host, BMOD);
423 /* Software reset of DMA */
424 bmod |= SDMMC_IDMAC_SWRESET;
425 mci_writel(host, BMOD, bmod);
426}
427
Will Newtonf95f3852011-01-02 01:11:59 -0500428static void dw_mci_idmac_stop_dma(struct dw_mci *host)
429{
430 u32 temp;
431
432 /* Disable and reset the IDMAC interface */
433 temp = mci_readl(host, CTRL);
434 temp &= ~SDMMC_CTRL_USE_IDMAC;
435 temp |= SDMMC_CTRL_DMA_RESET;
436 mci_writel(host, CTRL, temp);
437
438 /* Stop the IDMAC running */
439 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900440 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900441 temp |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -0500442 mci_writel(host, BMOD, temp);
443}
444
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800445static void dw_mci_dmac_complete_dma(void *arg)
Will Newtonf95f3852011-01-02 01:11:59 -0500446{
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800447 struct dw_mci *host = arg;
Will Newtonf95f3852011-01-02 01:11:59 -0500448 struct mmc_data *data = host->data;
449
Thomas Abraham4a909202012-09-17 18:16:35 +0000450 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500451
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800452 if ((host->use_dma == TRANS_MODE_EDMAC) &&
453 data && (data->flags & MMC_DATA_READ))
454 /* Invalidate cache after read */
455 dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc),
456 data->sg,
457 data->sg_len,
458 DMA_FROM_DEVICE);
459
Will Newtonf95f3852011-01-02 01:11:59 -0500460 host->dma_ops->cleanup(host);
461
462 /*
463 * If the card was removed, data will be NULL. No point in trying to
464 * send the stop command or waiting for NBUSY in this case.
465 */
466 if (data) {
467 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
468 tasklet_schedule(&host->tasklet);
469 }
470}
471
472static void dw_mci_translate_sglist(struct dw_mci *host, struct mmc_data *data,
473 unsigned int sg_len)
474{
Alexey Brodkin5959b322015-06-25 11:25:07 +0300475 unsigned int desc_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500476 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800477
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000478 if (host->dma_64bit_address == 1) {
Alexey Brodkin5959b322015-06-25 11:25:07 +0300479 struct idmac_desc_64addr *desc_first, *desc_last, *desc;
Will Newtonf95f3852011-01-02 01:11:59 -0500480
Alexey Brodkin5959b322015-06-25 11:25:07 +0300481 desc_first = desc_last = desc = host->sg_cpu;
482
483 for (i = 0; i < sg_len; i++) {
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000484 unsigned int length = sg_dma_len(&data->sg[i]);
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800485
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000486 u64 mem_addr = sg_dma_address(&data->sg[i]);
Will Newtonf95f3852011-01-02 01:11:59 -0500487
Alexey Brodkin5959b322015-06-25 11:25:07 +0300488 for ( ; length ; desc++) {
489 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
490 length : DW_MCI_DESC_DATA_LENGTH;
Will Newtonf95f3852011-01-02 01:11:59 -0500491
Alexey Brodkin5959b322015-06-25 11:25:07 +0300492 length -= desc_len;
493
494 /*
495 * Set the OWN bit and disable interrupts
496 * for this descriptor
497 */
498 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
499 IDMAC_DES0_CH;
500
501 /* Buffer length */
502 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
503
504 /* Physical address to DMA to/from */
505 desc->des4 = mem_addr & 0xffffffff;
506 desc->des5 = mem_addr >> 32;
507
508 /* Update physical address for the next desc */
509 mem_addr += desc_len;
510
511 /* Save pointer to the last descriptor */
512 desc_last = desc;
513 }
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000514 }
Will Newtonf95f3852011-01-02 01:11:59 -0500515
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000516 /* Set first descriptor */
Alexey Brodkin5959b322015-06-25 11:25:07 +0300517 desc_first->des0 |= IDMAC_DES0_FD;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000518
519 /* Set last descriptor */
Alexey Brodkin5959b322015-06-25 11:25:07 +0300520 desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
521 desc_last->des0 |= IDMAC_DES0_LD;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000522
523 } else {
Alexey Brodkin5959b322015-06-25 11:25:07 +0300524 struct idmac_desc *desc_first, *desc_last, *desc;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000525
Alexey Brodkin5959b322015-06-25 11:25:07 +0300526 desc_first = desc_last = desc = host->sg_cpu;
527
528 for (i = 0; i < sg_len; i++) {
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000529 unsigned int length = sg_dma_len(&data->sg[i]);
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800530
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000531 u32 mem_addr = sg_dma_address(&data->sg[i]);
532
Alexey Brodkin5959b322015-06-25 11:25:07 +0300533 for ( ; length ; desc++) {
534 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
535 length : DW_MCI_DESC_DATA_LENGTH;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000536
Alexey Brodkin5959b322015-06-25 11:25:07 +0300537 length -= desc_len;
538
539 /*
540 * Set the OWN bit and disable interrupts
541 * for this descriptor
542 */
543 desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
544 IDMAC_DES0_DIC |
545 IDMAC_DES0_CH);
546
547 /* Buffer length */
548 IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
549
550 /* Physical address to DMA to/from */
551 desc->des2 = cpu_to_le32(mem_addr);
552
553 /* Update physical address for the next desc */
554 mem_addr += desc_len;
555
556 /* Save pointer to the last descriptor */
557 desc_last = desc;
558 }
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000559 }
560
561 /* Set first descriptor */
Alexey Brodkin5959b322015-06-25 11:25:07 +0300562 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000563
564 /* Set last descriptor */
Alexey Brodkin5959b322015-06-25 11:25:07 +0300565 desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
566 IDMAC_DES0_DIC));
567 desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
Will Newtonf95f3852011-01-02 01:11:59 -0500568 }
569
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800570 wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -0500571}
572
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800573static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
Will Newtonf95f3852011-01-02 01:11:59 -0500574{
575 u32 temp;
576
577 dw_mci_translate_sglist(host, host->data, sg_len);
578
Sonny Rao536f6b92014-10-16 09:58:05 -0700579 /* Make sure to reset DMA in case we did PIO before this */
580 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
581 dw_mci_idmac_reset(host);
582
Will Newtonf95f3852011-01-02 01:11:59 -0500583 /* Select IDMAC interface */
584 temp = mci_readl(host, CTRL);
585 temp |= SDMMC_CTRL_USE_IDMAC;
586 mci_writel(host, CTRL, temp);
587
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800588 /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -0500589 wmb();
590
591 /* Enable the IDMAC */
592 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900593 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
Will Newtonf95f3852011-01-02 01:11:59 -0500594 mci_writel(host, BMOD, temp);
595
596 /* Start it running */
597 mci_writel(host, PLDMND, 1);
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800598
599 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -0500600}
601
602static int dw_mci_idmac_init(struct dw_mci *host)
603{
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800604 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500605
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000606 if (host->dma_64bit_address == 1) {
607 struct idmac_desc_64addr *p;
608 /* Number of descriptors in the ring buffer */
609 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc_64addr);
Will Newtonf95f3852011-01-02 01:11:59 -0500610
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000611 /* Forward link the descriptor list */
612 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
613 i++, p++) {
614 p->des6 = (host->sg_dma +
615 (sizeof(struct idmac_desc_64addr) *
616 (i + 1))) & 0xffffffff;
Will Newtonf95f3852011-01-02 01:11:59 -0500617
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000618 p->des7 = (u64)(host->sg_dma +
619 (sizeof(struct idmac_desc_64addr) *
620 (i + 1))) >> 32;
621 /* Initialize reserved and buffer size fields to "0" */
622 p->des1 = 0;
623 p->des2 = 0;
624 p->des3 = 0;
625 }
626
627 /* Set the last descriptor as the end-of-ring descriptor */
628 p->des6 = host->sg_dma & 0xffffffff;
629 p->des7 = (u64)host->sg_dma >> 32;
630 p->des0 = IDMAC_DES0_ER;
631
632 } else {
633 struct idmac_desc *p;
634 /* Number of descriptors in the ring buffer */
635 host->ring_size = PAGE_SIZE / sizeof(struct idmac_desc);
636
637 /* Forward link the descriptor list */
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800638 for (i = 0, p = host->sg_cpu;
639 i < host->ring_size - 1;
640 i++, p++) {
Ben Dooks6687c422015-03-25 11:27:51 +0000641 p->des3 = cpu_to_le32(host->sg_dma +
642 (sizeof(struct idmac_desc) * (i + 1)));
Zhangfei Gao4b244722015-04-30 22:16:28 +0800643 p->des1 = 0;
644 }
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000645
646 /* Set the last descriptor as the end-of-ring descriptor */
Ben Dooks6687c422015-03-25 11:27:51 +0000647 p->des3 = cpu_to_le32(host->sg_dma);
648 p->des0 = cpu_to_le32(IDMAC_DES0_ER);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000649 }
Will Newtonf95f3852011-01-02 01:11:59 -0500650
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900651 dw_mci_idmac_reset(host);
Seungwon Jeon141a7122012-05-22 13:01:03 +0900652
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000653 if (host->dma_64bit_address == 1) {
654 /* Mask out interrupts - get Tx & Rx complete only */
655 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
656 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
657 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
Will Newtonf95f3852011-01-02 01:11:59 -0500658
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000659 /* Set the descriptor base address */
660 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
661 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
662
663 } else {
664 /* Mask out interrupts - get Tx & Rx complete only */
665 mci_writel(host, IDSTS, IDMAC_INT_CLR);
666 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
667 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
668
669 /* Set the descriptor base address */
670 mci_writel(host, DBADDR, host->sg_dma);
671 }
672
Will Newtonf95f3852011-01-02 01:11:59 -0500673 return 0;
674}
675
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100676static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900677 .init = dw_mci_idmac_init,
678 .start = dw_mci_idmac_start_dma,
679 .stop = dw_mci_idmac_stop_dma,
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800680 .complete = dw_mci_dmac_complete_dma,
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900681 .cleanup = dw_mci_dma_cleanup,
682};
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800683
684static void dw_mci_edmac_stop_dma(struct dw_mci *host)
685{
686 dmaengine_terminate_all(host->dms->ch);
687}
688
689static int dw_mci_edmac_start_dma(struct dw_mci *host,
690 unsigned int sg_len)
691{
692 struct dma_slave_config cfg;
693 struct dma_async_tx_descriptor *desc = NULL;
694 struct scatterlist *sgl = host->data->sg;
695 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
696 u32 sg_elems = host->data->sg_len;
697 u32 fifoth_val;
698 u32 fifo_offset = host->fifo_reg - host->regs;
699 int ret = 0;
700
701 /* Set external dma config: burst size, burst width */
702 cfg.dst_addr = (dma_addr_t)(host->phy_regs + fifo_offset);
703 cfg.src_addr = cfg.dst_addr;
704 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
705 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
706
707 /* Match burst msize with external dma config */
708 fifoth_val = mci_readl(host, FIFOTH);
709 cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
710 cfg.src_maxburst = cfg.dst_maxburst;
711
712 if (host->data->flags & MMC_DATA_WRITE)
713 cfg.direction = DMA_MEM_TO_DEV;
714 else
715 cfg.direction = DMA_DEV_TO_MEM;
716
717 ret = dmaengine_slave_config(host->dms->ch, &cfg);
718 if (ret) {
719 dev_err(host->dev, "Failed to config edmac.\n");
720 return -EBUSY;
721 }
722
723 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
724 sg_len, cfg.direction,
725 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
726 if (!desc) {
727 dev_err(host->dev, "Can't prepare slave sg.\n");
728 return -EBUSY;
729 }
730
731 /* Set dw_mci_dmac_complete_dma as callback */
732 desc->callback = dw_mci_dmac_complete_dma;
733 desc->callback_param = (void *)host;
734 dmaengine_submit(desc);
735
736 /* Flush cache before write */
737 if (host->data->flags & MMC_DATA_WRITE)
738 dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl,
739 sg_elems, DMA_TO_DEVICE);
740
741 dma_async_issue_pending(host->dms->ch);
742
743 return 0;
744}
745
746static int dw_mci_edmac_init(struct dw_mci *host)
747{
748 /* Request external dma channel */
749 host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
750 if (!host->dms)
751 return -ENOMEM;
752
753 host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
754 if (!host->dms->ch) {
755 dev_err(host->dev,
756 "Failed to get external DMA channel %d\n",
757 host->dms->ch->chan_id);
758 kfree(host->dms);
759 host->dms = NULL;
760 return -ENXIO;
761 }
762
763 return 0;
764}
765
766static void dw_mci_edmac_exit(struct dw_mci *host)
767{
768 if (host->dms) {
769 if (host->dms->ch) {
770 dma_release_channel(host->dms->ch);
771 host->dms->ch = NULL;
772 }
773 kfree(host->dms);
774 host->dms = NULL;
775 }
776}
777
778static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
779 .init = dw_mci_edmac_init,
780 .exit = dw_mci_edmac_exit,
781 .start = dw_mci_edmac_start_dma,
782 .stop = dw_mci_edmac_stop_dma,
783 .complete = dw_mci_dmac_complete_dma,
784 .cleanup = dw_mci_dma_cleanup,
785};
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900786
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900787static int dw_mci_pre_dma_transfer(struct dw_mci *host,
788 struct mmc_data *data,
789 bool next)
Will Newtonf95f3852011-01-02 01:11:59 -0500790{
791 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900792 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500793
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900794 if (!next && data->host_cookie)
795 return data->host_cookie;
Will Newtonf95f3852011-01-02 01:11:59 -0500796
797 /*
798 * We don't do DMA on "complex" transfers, i.e. with
799 * non-word-aligned buffers or lengths. Also, we don't bother
800 * with all the DMA setup overhead for short transfers.
801 */
802 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
803 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900804
Will Newtonf95f3852011-01-02 01:11:59 -0500805 if (data->blksz & 3)
806 return -EINVAL;
807
808 for_each_sg(data->sg, sg, data->sg_len, i) {
809 if (sg->offset & 3 || sg->length & 3)
810 return -EINVAL;
811 }
812
Thomas Abraham4a909202012-09-17 18:16:35 +0000813 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900814 data->sg,
815 data->sg_len,
816 dw_mci_get_dma_dir(data));
817 if (sg_len == 0)
818 return -EINVAL;
819
820 if (next)
821 data->host_cookie = sg_len;
822
823 return sg_len;
824}
825
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900826static void dw_mci_pre_req(struct mmc_host *mmc,
827 struct mmc_request *mrq,
828 bool is_first_req)
829{
830 struct dw_mci_slot *slot = mmc_priv(mmc);
831 struct mmc_data *data = mrq->data;
832
833 if (!slot->host->use_dma || !data)
834 return;
835
836 if (data->host_cookie) {
837 data->host_cookie = 0;
838 return;
839 }
840
841 if (dw_mci_pre_dma_transfer(slot->host, mrq->data, 1) < 0)
842 data->host_cookie = 0;
843}
844
845static void dw_mci_post_req(struct mmc_host *mmc,
846 struct mmc_request *mrq,
847 int err)
848{
849 struct dw_mci_slot *slot = mmc_priv(mmc);
850 struct mmc_data *data = mrq->data;
851
852 if (!slot->host->use_dma || !data)
853 return;
854
855 if (data->host_cookie)
Thomas Abraham4a909202012-09-17 18:16:35 +0000856 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900857 data->sg,
858 data->sg_len,
859 dw_mci_get_dma_dir(data));
860 data->host_cookie = 0;
861}
862
Seungwon Jeon52426892013-08-31 00:13:42 +0900863static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
864{
Seungwon Jeon52426892013-08-31 00:13:42 +0900865 unsigned int blksz = data->blksz;
866 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
867 u32 fifo_width = 1 << host->data_shift;
868 u32 blksz_depth = blksz / fifo_width, fifoth_val;
869 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800870 int idx = ARRAY_SIZE(mszs) - 1;
Seungwon Jeon52426892013-08-31 00:13:42 +0900871
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800872 /* pio should ship this scenario */
873 if (!host->use_dma)
874 return;
875
Seungwon Jeon52426892013-08-31 00:13:42 +0900876 tx_wmark = (host->fifo_depth) / 2;
877 tx_wmark_invers = host->fifo_depth - tx_wmark;
878
879 /*
880 * MSIZE is '1',
881 * if blksz is not a multiple of the FIFO width
882 */
883 if (blksz % fifo_width) {
884 msize = 0;
885 rx_wmark = 1;
886 goto done;
887 }
888
889 do {
890 if (!((blksz_depth % mszs[idx]) ||
891 (tx_wmark_invers % mszs[idx]))) {
892 msize = idx;
893 rx_wmark = mszs[idx] - 1;
894 break;
895 }
896 } while (--idx > 0);
897 /*
898 * If idx is '0', it won't be tried
899 * Thus, initial values are uesed
900 */
901done:
902 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
903 mci_writel(host, FIFOTH, fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +0900904}
905
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900906static void dw_mci_ctrl_rd_thld(struct dw_mci *host, struct mmc_data *data)
907{
908 unsigned int blksz = data->blksz;
909 u32 blksz_depth, fifo_depth;
910 u16 thld_size;
911
912 WARN_ON(!(data->flags & MMC_DATA_READ));
913
James Hogan66dfd102014-11-17 17:49:05 +0000914 /*
915 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
916 * in the FIFO region, so we really shouldn't access it).
917 */
918 if (host->verid < DW_MMC_240A)
919 return;
920
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900921 if (host->timing != MMC_TIMING_MMC_HS200 &&
Jaehoon Chung488b8d62015-03-05 19:45:21 +0900922 host->timing != MMC_TIMING_MMC_HS400 &&
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900923 host->timing != MMC_TIMING_UHS_SDR104)
924 goto disable;
925
926 blksz_depth = blksz / (1 << host->data_shift);
927 fifo_depth = host->fifo_depth;
928
929 if (blksz_depth > fifo_depth)
930 goto disable;
931
932 /*
933 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
934 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
935 * Currently just choose blksz.
936 */
937 thld_size = blksz;
938 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(thld_size, 1));
939 return;
940
941disable:
942 mci_writel(host, CDTHRCTL, SDMMC_SET_RD_THLD(0, 0));
943}
944
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900945static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
946{
Doug Andersonf8c58c12014-12-02 15:42:47 -0800947 unsigned long irqflags;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900948 int sg_len;
949 u32 temp;
950
951 host->using_dma = 0;
952
953 /* If we don't have a channel, we can't do DMA */
954 if (!host->use_dma)
955 return -ENODEV;
956
957 sg_len = dw_mci_pre_dma_transfer(host, data, 0);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900958 if (sg_len < 0) {
959 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900960 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +0900961 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900962
James Hogan03e8cb52011-06-29 09:28:43 +0100963 host->using_dma = 1;
964
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800965 if (host->use_dma == TRANS_MODE_IDMAC)
966 dev_vdbg(host->dev,
967 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
968 (unsigned long)host->sg_cpu,
969 (unsigned long)host->sg_dma,
970 sg_len);
Will Newtonf95f3852011-01-02 01:11:59 -0500971
Seungwon Jeon52426892013-08-31 00:13:42 +0900972 /*
973 * Decide the MSIZE and RX/TX Watermark.
974 * If current block size is same with previous size,
975 * no need to update fifoth.
976 */
977 if (host->prev_blksz != data->blksz)
978 dw_mci_adjust_fifoth(host, data);
979
Will Newtonf95f3852011-01-02 01:11:59 -0500980 /* Enable the DMA interface */
981 temp = mci_readl(host, CTRL);
982 temp |= SDMMC_CTRL_DMA_ENABLE;
983 mci_writel(host, CTRL, temp);
984
985 /* Disable RX/TX IRQs, let DMA handle it */
Doug Andersonf8c58c12014-12-02 15:42:47 -0800986 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -0500987 temp = mci_readl(host, INTMASK);
988 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
989 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -0800990 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -0500991
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800992 if (host->dma_ops->start(host, sg_len)) {
993 /* We can't do DMA */
994 dev_err(host->dev, "%s: failed to start DMA.\n", __func__);
995 return -ENODEV;
996 }
Will Newtonf95f3852011-01-02 01:11:59 -0500997
998 return 0;
999}
1000
1001static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1002{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001003 unsigned long irqflags;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001004 int flags = SG_MITER_ATOMIC;
Will Newtonf95f3852011-01-02 01:11:59 -05001005 u32 temp;
1006
1007 data->error = -EINPROGRESS;
1008
1009 WARN_ON(host->data);
1010 host->sg = NULL;
1011 host->data = data;
1012
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001013 if (data->flags & MMC_DATA_READ) {
James Hogan55c5efbc2011-06-29 09:29:58 +01001014 host->dir_status = DW_MCI_RECV_STATUS;
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001015 dw_mci_ctrl_rd_thld(host, data);
1016 } else {
James Hogan55c5efbc2011-06-29 09:29:58 +01001017 host->dir_status = DW_MCI_SEND_STATUS;
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001018 }
James Hogan55c5efbc2011-06-29 09:29:58 +01001019
Will Newtonf95f3852011-01-02 01:11:59 -05001020 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001021 if (host->data->flags & MMC_DATA_READ)
1022 flags |= SG_MITER_TO_SG;
1023 else
1024 flags |= SG_MITER_FROM_SG;
1025
1026 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001027 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +01001028 host->part_buf_start = 0;
1029 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001030
James Hoganb40af3a2011-06-24 13:54:06 +01001031 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001032
1033 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001034 temp = mci_readl(host, INTMASK);
1035 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1036 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001037 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001038
1039 temp = mci_readl(host, CTRL);
1040 temp &= ~SDMMC_CTRL_DMA_ENABLE;
1041 mci_writel(host, CTRL, temp);
Seungwon Jeon52426892013-08-31 00:13:42 +09001042
1043 /*
1044 * Use the initial fifoth_val for PIO mode.
1045 * If next issued data may be transfered by DMA mode,
1046 * prev_blksz should be invalidated.
1047 */
1048 mci_writel(host, FIFOTH, host->fifoth_val);
1049 host->prev_blksz = 0;
1050 } else {
1051 /*
1052 * Keep the current block size.
1053 * It will be used to decide whether to update
1054 * fifoth register next time.
1055 */
1056 host->prev_blksz = data->blksz;
Will Newtonf95f3852011-01-02 01:11:59 -05001057 }
1058}
1059
1060static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
1061{
1062 struct dw_mci *host = slot->host;
1063 unsigned long timeout = jiffies + msecs_to_jiffies(500);
1064 unsigned int cmd_status = 0;
1065
1066 mci_writel(host, CMDARG, arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001067 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -08001068 dw_mci_wait_while_busy(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001069 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
1070
1071 while (time_before(jiffies, timeout)) {
1072 cmd_status = mci_readl(host, CMD);
1073 if (!(cmd_status & SDMMC_CMD_START))
1074 return;
1075 }
1076 dev_err(&slot->mmc->class_dev,
1077 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
1078 cmd, arg, cmd_status);
1079}
1080
Abhilash Kesavanab269122012-11-19 10:26:21 +05301081static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -05001082{
1083 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +09001084 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001085 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001086 u32 clk_en_a;
Doug Anderson01730552014-08-22 19:17:51 +05301087 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
1088
1089 /* We must continue to set bit 28 in CMD until the change is complete */
1090 if (host->state == STATE_WAITING_CMD11_DONE)
1091 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
Will Newtonf95f3852011-01-02 01:11:59 -05001092
Doug Andersonfdf492a2013-08-31 00:11:43 +09001093 if (!clock) {
1094 mci_writel(host, CLKENA, 0);
Doug Anderson01730552014-08-22 19:17:51 +05301095 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Doug Andersonfdf492a2013-08-31 00:11:43 +09001096 } else if (clock != host->current_speed || force_clkinit) {
1097 div = host->bus_hz / clock;
1098 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -05001099 /*
1100 * move the + 1 after the divide to prevent
1101 * over-clocking the card.
1102 */
Seungwon Jeone4199902012-05-22 13:01:21 +09001103 div += 1;
1104
Doug Andersonfdf492a2013-08-31 00:11:43 +09001105 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001106
Doug Andersonfdf492a2013-08-31 00:11:43 +09001107 if ((clock << div) != slot->__clk_old || force_clkinit)
1108 dev_info(&slot->mmc->class_dev,
1109 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1110 slot->id, host->bus_hz, clock,
1111 div ? ((host->bus_hz / div) >> 1) :
1112 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -05001113
1114 /* disable clock */
1115 mci_writel(host, CLKENA, 0);
1116 mci_writel(host, CLKSRC, 0);
1117
1118 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301119 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001120
1121 /* set clock to desired speed */
1122 mci_writel(host, CLKDIV, div);
1123
1124 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301125 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001126
Doug Anderson9623b5b2012-07-25 08:33:17 -07001127 /* enable clock; only low power if no SDIO */
1128 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Doug Andersonb24c8b22014-12-02 15:42:46 -08001129 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
Doug Anderson9623b5b2012-07-25 08:33:17 -07001130 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1131 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -05001132
1133 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301134 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001135
Doug Andersonfdf492a2013-08-31 00:11:43 +09001136 /* keep the clock with reflecting clock dividor */
1137 slot->__clk_old = clock << div;
Will Newtonf95f3852011-01-02 01:11:59 -05001138 }
1139
Doug Andersonfdf492a2013-08-31 00:11:43 +09001140 host->current_speed = clock;
1141
Will Newtonf95f3852011-01-02 01:11:59 -05001142 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +09001143 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -05001144}
1145
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001146static void __dw_mci_start_request(struct dw_mci *host,
1147 struct dw_mci_slot *slot,
1148 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001149{
1150 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001151 struct mmc_data *data;
1152 u32 cmdflags;
1153
1154 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001155
Will Newtonf95f3852011-01-02 01:11:59 -05001156 host->cur_slot = slot;
1157 host->mrq = mrq;
1158
1159 host->pending_events = 0;
1160 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001161 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001162 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001163 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001164
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001165 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -05001166 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +09001167 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -05001168 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1169 mci_writel(host, BLKSIZ, data->blksz);
1170 }
1171
Will Newtonf95f3852011-01-02 01:11:59 -05001172 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1173
1174 /* this is the first command, send the initialization clock */
1175 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1176 cmdflags |= SDMMC_CMD_INIT;
1177
1178 if (data) {
1179 dw_mci_submit_data(host, data);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001180 wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05001181 }
1182
1183 dw_mci_start_command(host, cmd, cmdflags);
1184
Doug Anderson5c935162015-03-09 16:18:21 -07001185 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
Doug Anderson49ba0302015-04-03 11:13:07 -07001186 unsigned long irqflags;
1187
Doug Anderson5c935162015-03-09 16:18:21 -07001188 /*
Doug Anderson8886a6f2015-04-03 11:13:05 -07001189 * Databook says to fail after 2ms w/ no response, but evidence
1190 * shows that sometimes the cmd11 interrupt takes over 130ms.
1191 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1192 * is just about to roll over.
Doug Anderson49ba0302015-04-03 11:13:07 -07001193 *
1194 * We do this whole thing under spinlock and only if the
1195 * command hasn't already completed (indicating the the irq
1196 * already ran so we don't want the timeout).
Doug Anderson5c935162015-03-09 16:18:21 -07001197 */
Doug Anderson49ba0302015-04-03 11:13:07 -07001198 spin_lock_irqsave(&host->irq_lock, irqflags);
1199 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1200 mod_timer(&host->cmd11_timer,
1201 jiffies + msecs_to_jiffies(500) + 1);
1202 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Doug Anderson5c935162015-03-09 16:18:21 -07001203 }
1204
Will Newtonf95f3852011-01-02 01:11:59 -05001205 if (mrq->stop)
1206 host->stop_cmdr = dw_mci_prepare_command(slot->mmc, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001207 else
1208 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001209}
1210
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001211static void dw_mci_start_request(struct dw_mci *host,
1212 struct dw_mci_slot *slot)
1213{
1214 struct mmc_request *mrq = slot->mrq;
1215 struct mmc_command *cmd;
1216
1217 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1218 __dw_mci_start_request(host, slot, cmd);
1219}
1220
James Hogan7456caa2011-06-24 13:55:10 +01001221/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -05001222static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1223 struct mmc_request *mrq)
1224{
1225 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1226 host->state);
1227
Will Newtonf95f3852011-01-02 01:11:59 -05001228 slot->mrq = mrq;
1229
Doug Anderson01730552014-08-22 19:17:51 +05301230 if (host->state == STATE_WAITING_CMD11_DONE) {
1231 dev_warn(&slot->mmc->class_dev,
1232 "Voltage change didn't complete\n");
1233 /*
1234 * this case isn't expected to happen, so we can
1235 * either crash here or just try to continue on
1236 * in the closest possible state
1237 */
1238 host->state = STATE_IDLE;
1239 }
1240
Will Newtonf95f3852011-01-02 01:11:59 -05001241 if (host->state == STATE_IDLE) {
1242 host->state = STATE_SENDING_CMD;
1243 dw_mci_start_request(host, slot);
1244 } else {
1245 list_add_tail(&slot->queue_node, &host->queue);
1246 }
Will Newtonf95f3852011-01-02 01:11:59 -05001247}
1248
1249static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1250{
1251 struct dw_mci_slot *slot = mmc_priv(mmc);
1252 struct dw_mci *host = slot->host;
1253
1254 WARN_ON(slot->mrq);
1255
James Hogan7456caa2011-06-24 13:55:10 +01001256 /*
1257 * The check for card presence and queueing of the request must be
1258 * atomic, otherwise the card could be removed in between and the
1259 * request wouldn't fail until another card was inserted.
1260 */
1261 spin_lock_bh(&host->lock);
1262
Will Newtonf95f3852011-01-02 01:11:59 -05001263 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
James Hogan7456caa2011-06-24 13:55:10 +01001264 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001265 mrq->cmd->error = -ENOMEDIUM;
1266 mmc_request_done(mmc, mrq);
1267 return;
1268 }
1269
Will Newtonf95f3852011-01-02 01:11:59 -05001270 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001271
1272 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001273}
1274
1275static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1276{
1277 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001278 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001279 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301280 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001281
Will Newtonf95f3852011-01-02 01:11:59 -05001282 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001283 case MMC_BUS_WIDTH_4:
1284 slot->ctype = SDMMC_CTYPE_4BIT;
1285 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001286 case MMC_BUS_WIDTH_8:
1287 slot->ctype = SDMMC_CTYPE_8BIT;
1288 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001289 default:
1290 /* set default 1 bit mode */
1291 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001292 }
1293
Seungwon Jeon3f514292012-01-02 16:00:02 +09001294 regs = mci_readl(slot->host, UHS_REG);
1295
Jaehoon Chung41babf72011-02-24 13:46:11 +09001296 /* DDR mode set */
Seungwon Jeon80113132015-01-29 08:11:57 +05301297 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
1298 ios->timing == MMC_TIMING_MMC_HS400)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001299 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001300 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001301 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001302
1303 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001304 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001305
Doug Andersonfdf492a2013-08-31 00:11:43 +09001306 /*
1307 * Use mirror of ios->clock to prevent race with mmc
1308 * core ios update when finding the minimum.
1309 */
1310 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001311
James Hogancb27a842012-10-16 09:43:08 +01001312 if (drv_data && drv_data->set_ios)
1313 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001314
Will Newtonf95f3852011-01-02 01:11:59 -05001315 switch (ios->power_mode) {
1316 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301317 if (!IS_ERR(mmc->supply.vmmc)) {
1318 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1319 ios->vdd);
1320 if (ret) {
1321 dev_err(slot->host->dev,
1322 "failed to enable vmmc regulator\n");
1323 /*return, if failed turn on vmmc*/
1324 return;
1325 }
1326 }
Doug Anderson29d0d162015-01-13 15:58:44 -08001327 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1328 regs = mci_readl(slot->host, PWREN);
1329 regs |= (1 << slot->id);
1330 mci_writel(slot->host, PWREN, regs);
1331 break;
1332 case MMC_POWER_ON:
Doug Andersond1f1dd82015-02-20 10:57:19 -08001333 if (!slot->host->vqmmc_enabled) {
1334 if (!IS_ERR(mmc->supply.vqmmc)) {
1335 ret = regulator_enable(mmc->supply.vqmmc);
1336 if (ret < 0)
1337 dev_err(slot->host->dev,
1338 "failed to enable vqmmc\n");
1339 else
1340 slot->host->vqmmc_enabled = true;
1341
1342 } else {
1343 /* Keep track so we don't reset again */
Yuvaraj CD51da2242014-08-22 19:17:50 +05301344 slot->host->vqmmc_enabled = true;
Doug Andersond1f1dd82015-02-20 10:57:19 -08001345 }
1346
1347 /* Reset our state machine after powering on */
1348 dw_mci_ctrl_reset(slot->host,
1349 SDMMC_CTRL_ALL_RESET_FLAGS);
Yuvaraj CD51da2242014-08-22 19:17:50 +05301350 }
Doug Anderson655babb2015-02-20 10:57:18 -08001351
1352 /* Adjust clock / bus width after power is up */
1353 dw_mci_setup_bus(slot, false);
1354
James Hogane6f34e22013-03-12 10:43:32 +00001355 break;
1356 case MMC_POWER_OFF:
Doug Anderson655babb2015-02-20 10:57:18 -08001357 /* Turn clock off before power goes down */
1358 dw_mci_setup_bus(slot, false);
1359
Yuvaraj CD51da2242014-08-22 19:17:50 +05301360 if (!IS_ERR(mmc->supply.vmmc))
1361 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1362
Doug Andersond1f1dd82015-02-20 10:57:19 -08001363 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
Yuvaraj CD51da2242014-08-22 19:17:50 +05301364 regulator_disable(mmc->supply.vqmmc);
Doug Andersond1f1dd82015-02-20 10:57:19 -08001365 slot->host->vqmmc_enabled = false;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301366
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001367 regs = mci_readl(slot->host, PWREN);
1368 regs &= ~(1 << slot->id);
1369 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001370 break;
1371 default:
1372 break;
1373 }
Doug Anderson655babb2015-02-20 10:57:18 -08001374
1375 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1376 slot->host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001377}
1378
Doug Anderson01730552014-08-22 19:17:51 +05301379static int dw_mci_card_busy(struct mmc_host *mmc)
1380{
1381 struct dw_mci_slot *slot = mmc_priv(mmc);
1382 u32 status;
1383
1384 /*
1385 * Check the busy bit which is low when DAT[3:0]
1386 * (the data lines) are 0000
1387 */
1388 status = mci_readl(slot->host, STATUS);
1389
1390 return !!(status & SDMMC_STATUS_BUSY);
1391}
1392
1393static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1394{
1395 struct dw_mci_slot *slot = mmc_priv(mmc);
1396 struct dw_mci *host = slot->host;
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001397 const struct dw_mci_drv_data *drv_data = host->drv_data;
Doug Anderson01730552014-08-22 19:17:51 +05301398 u32 uhs;
1399 u32 v18 = SDMMC_UHS_18V << slot->id;
Doug Anderson01730552014-08-22 19:17:51 +05301400 int ret;
1401
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001402 if (drv_data && drv_data->switch_voltage)
1403 return drv_data->switch_voltage(mmc, ios);
1404
Doug Anderson01730552014-08-22 19:17:51 +05301405 /*
1406 * Program the voltage. Note that some instances of dw_mmc may use
1407 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1408 * does no harm but you need to set the regulator directly. Try both.
1409 */
1410 uhs = mci_readl(host, UHS_REG);
Douglas Andersone0848f52015-10-12 14:48:26 +02001411 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
Doug Anderson01730552014-08-22 19:17:51 +05301412 uhs &= ~v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001413 else
Doug Anderson01730552014-08-22 19:17:51 +05301414 uhs |= v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001415
Doug Anderson01730552014-08-22 19:17:51 +05301416 if (!IS_ERR(mmc->supply.vqmmc)) {
Douglas Andersone0848f52015-10-12 14:48:26 +02001417 ret = mmc_regulator_set_vqmmc(mmc, ios);
Doug Anderson01730552014-08-22 19:17:51 +05301418
1419 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001420 dev_dbg(&mmc->class_dev,
Douglas Andersone0848f52015-10-12 14:48:26 +02001421 "Regulator set error %d - %s V\n",
1422 ret, uhs & v18 ? "1.8" : "3.3");
Doug Anderson01730552014-08-22 19:17:51 +05301423 return ret;
1424 }
1425 }
1426 mci_writel(host, UHS_REG, uhs);
1427
1428 return 0;
1429}
1430
Will Newtonf95f3852011-01-02 01:11:59 -05001431static int dw_mci_get_ro(struct mmc_host *mmc)
1432{
1433 int read_only;
1434 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001435 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001436
1437 /* Use platform get_ro function, else try on board write protect */
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02001438 if (!IS_ERR_VALUE(gpio_ro))
Jaehoon Chung9795a842014-03-03 11:36:46 +09001439 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001440 else
1441 read_only =
1442 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1443
1444 dev_dbg(&mmc->class_dev, "card is %s\n",
1445 read_only ? "read-only" : "read-write");
1446
1447 return read_only;
1448}
1449
1450static int dw_mci_get_cd(struct mmc_host *mmc)
1451{
1452 int present;
1453 struct dw_mci_slot *slot = mmc_priv(mmc);
1454 struct dw_mci_board *brd = slot->host->pdata;
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001455 struct dw_mci *host = slot->host;
1456 int gpio_cd = mmc_gpio_get_cd(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001457
1458 /* Use platform get_cd function, else try onboard card detect */
Zhangfei Gao4de3bf62015-05-05 16:54:49 +08001459 if ((brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION) ||
1460 (mmc->caps & MMC_CAP_NONREMOVABLE))
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001461 present = 1;
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001462 else if (!IS_ERR_VALUE(gpio_cd))
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001463 present = gpio_cd;
Will Newtonf95f3852011-01-02 01:11:59 -05001464 else
1465 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1466 == 0 ? 1 : 0;
1467
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001468 spin_lock_bh(&host->lock);
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001469 if (present) {
1470 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001471 dev_dbg(&mmc->class_dev, "card is present\n");
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001472 } else {
1473 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001474 dev_dbg(&mmc->class_dev, "card is not present\n");
Zhangfei Gaobf626e52014-01-09 22:35:10 +08001475 }
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001476 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001477
1478 return present;
1479}
1480
Doug Andersonb24c8b22014-12-02 15:42:46 -08001481static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
Doug Anderson9623b5b2012-07-25 08:33:17 -07001482{
Doug Andersonb24c8b22014-12-02 15:42:46 -08001483 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson9623b5b2012-07-25 08:33:17 -07001484 struct dw_mci *host = slot->host;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001485
Doug Andersonb24c8b22014-12-02 15:42:46 -08001486 /*
1487 * Low power mode will stop the card clock when idle. According to the
1488 * description of the CLKENA register we should disable low power mode
1489 * for SDIO cards if we need SDIO interrupts to work.
1490 */
1491 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1492 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1493 u32 clk_en_a_old;
1494 u32 clk_en_a;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001495
Doug Andersonb24c8b22014-12-02 15:42:46 -08001496 clk_en_a_old = mci_readl(host, CLKENA);
1497
1498 if (card->type == MMC_TYPE_SDIO ||
1499 card->type == MMC_TYPE_SD_COMBO) {
1500 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1501 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1502 } else {
1503 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1504 clk_en_a = clk_en_a_old | clken_low_pwr;
1505 }
1506
1507 if (clk_en_a != clk_en_a_old) {
1508 mci_writel(host, CLKENA, clk_en_a);
1509 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1510 SDMMC_CMD_PRV_DAT_WAIT, 0);
1511 }
Doug Anderson9623b5b2012-07-25 08:33:17 -07001512 }
1513}
1514
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301515static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1516{
1517 struct dw_mci_slot *slot = mmc_priv(mmc);
1518 struct dw_mci *host = slot->host;
Doug Andersonf8c58c12014-12-02 15:42:47 -08001519 unsigned long irqflags;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301520 u32 int_mask;
1521
Doug Andersonf8c58c12014-12-02 15:42:47 -08001522 spin_lock_irqsave(&host->irq_lock, irqflags);
1523
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301524 /* Enable/disable Slot Specific SDIO interrupt */
1525 int_mask = mci_readl(host, INTMASK);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001526 if (enb)
1527 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1528 else
1529 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1530 mci_writel(host, INTMASK, int_mask);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001531
1532 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301533}
1534
Seungwon Jeon0976f162013-08-31 00:12:42 +09001535static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1536{
1537 struct dw_mci_slot *slot = mmc_priv(mmc);
1538 struct dw_mci *host = slot->host;
1539 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001540 int err = -EINVAL;
Seungwon Jeon0976f162013-08-31 00:12:42 +09001541
Seungwon Jeon0976f162013-08-31 00:12:42 +09001542 if (drv_data && drv_data->execute_tuning)
Ulf Hansson6c2c6502014-12-01 16:13:39 +01001543 err = drv_data->execute_tuning(slot);
Seungwon Jeon0976f162013-08-31 00:12:42 +09001544 return err;
1545}
1546
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001547static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1548 struct mmc_ios *ios)
Seungwon Jeon80113132015-01-29 08:11:57 +05301549{
1550 struct dw_mci_slot *slot = mmc_priv(mmc);
1551 struct dw_mci *host = slot->host;
1552 const struct dw_mci_drv_data *drv_data = host->drv_data;
1553
1554 if (drv_data && drv_data->prepare_hs400_tuning)
1555 return drv_data->prepare_hs400_tuning(host, ios);
1556
1557 return 0;
1558}
1559
Will Newtonf95f3852011-01-02 01:11:59 -05001560static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301561 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001562 .pre_req = dw_mci_pre_req,
1563 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301564 .set_ios = dw_mci_set_ios,
1565 .get_ro = dw_mci_get_ro,
1566 .get_cd = dw_mci_get_cd,
1567 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001568 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301569 .card_busy = dw_mci_card_busy,
1570 .start_signal_voltage_switch = dw_mci_switch_voltage,
Doug Andersonb24c8b22014-12-02 15:42:46 -08001571 .init_card = dw_mci_init_card,
Seungwon Jeon80113132015-01-29 08:11:57 +05301572 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
Will Newtonf95f3852011-01-02 01:11:59 -05001573};
1574
1575static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1576 __releases(&host->lock)
1577 __acquires(&host->lock)
1578{
1579 struct dw_mci_slot *slot;
1580 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1581
1582 WARN_ON(host->cmd || host->data);
1583
1584 host->cur_slot->mrq = NULL;
1585 host->mrq = NULL;
1586 if (!list_empty(&host->queue)) {
1587 slot = list_entry(host->queue.next,
1588 struct dw_mci_slot, queue_node);
1589 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001590 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001591 mmc_hostname(slot->mmc));
1592 host->state = STATE_SENDING_CMD;
1593 dw_mci_start_request(host, slot);
1594 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001595 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301596
1597 if (host->state == STATE_SENDING_CMD11)
1598 host->state = STATE_WAITING_CMD11_DONE;
1599 else
1600 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001601 }
1602
1603 spin_unlock(&host->lock);
1604 mmc_request_done(prev_mmc, mrq);
1605 spin_lock(&host->lock);
1606}
1607
Seungwon Jeone352c812013-08-31 00:14:17 +09001608static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001609{
1610 u32 status = host->cmd_status;
1611
1612 host->cmd_status = 0;
1613
1614 /* Read the response from the card (up to 16 bytes) */
1615 if (cmd->flags & MMC_RSP_PRESENT) {
1616 if (cmd->flags & MMC_RSP_136) {
1617 cmd->resp[3] = mci_readl(host, RESP0);
1618 cmd->resp[2] = mci_readl(host, RESP1);
1619 cmd->resp[1] = mci_readl(host, RESP2);
1620 cmd->resp[0] = mci_readl(host, RESP3);
1621 } else {
1622 cmd->resp[0] = mci_readl(host, RESP0);
1623 cmd->resp[1] = 0;
1624 cmd->resp[2] = 0;
1625 cmd->resp[3] = 0;
1626 }
1627 }
1628
1629 if (status & SDMMC_INT_RTO)
1630 cmd->error = -ETIMEDOUT;
1631 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1632 cmd->error = -EILSEQ;
1633 else if (status & SDMMC_INT_RESP_ERR)
1634 cmd->error = -EIO;
1635 else
1636 cmd->error = 0;
1637
1638 if (cmd->error) {
1639 /* newer ip versions need a delay between retries */
1640 if (host->quirks & DW_MCI_QUIRK_RETRY_DELAY)
1641 mdelay(20);
Will Newtonf95f3852011-01-02 01:11:59 -05001642 }
Seungwon Jeone352c812013-08-31 00:14:17 +09001643
1644 return cmd->error;
1645}
1646
1647static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1648{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001649 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001650
1651 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1652 if (status & SDMMC_INT_DRTO) {
1653 data->error = -ETIMEDOUT;
1654 } else if (status & SDMMC_INT_DCRC) {
1655 data->error = -EILSEQ;
1656 } else if (status & SDMMC_INT_EBE) {
1657 if (host->dir_status ==
1658 DW_MCI_SEND_STATUS) {
1659 /*
1660 * No data CRC status was returned.
1661 * The number of bytes transferred
1662 * will be exaggerated in PIO mode.
1663 */
1664 data->bytes_xfered = 0;
1665 data->error = -ETIMEDOUT;
1666 } else if (host->dir_status ==
1667 DW_MCI_RECV_STATUS) {
1668 data->error = -EIO;
1669 }
1670 } else {
1671 /* SDMMC_INT_SBE is included */
1672 data->error = -EIO;
1673 }
1674
Doug Andersone6cc0122014-04-22 16:51:21 -07001675 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001676
1677 /*
1678 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001679 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001680 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001681 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001682 } else {
1683 data->bytes_xfered = data->blocks * data->blksz;
1684 data->error = 0;
1685 }
1686
1687 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001688}
1689
Addy Ke57e10482015-08-11 01:27:18 +09001690static void dw_mci_set_drto(struct dw_mci *host)
1691{
1692 unsigned int drto_clks;
1693 unsigned int drto_ms;
1694
1695 drto_clks = mci_readl(host, TMOUT) >> 8;
1696 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1697
1698 /* add a bit spare time */
1699 drto_ms += 10;
1700
1701 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1702}
1703
Will Newtonf95f3852011-01-02 01:11:59 -05001704static void dw_mci_tasklet_func(unsigned long priv)
1705{
1706 struct dw_mci *host = (struct dw_mci *)priv;
1707 struct mmc_data *data;
1708 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001709 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001710 enum dw_mci_state state;
1711 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001712 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001713
1714 spin_lock(&host->lock);
1715
1716 state = host->state;
1717 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001718 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001719
1720 do {
1721 prev_state = state;
1722
1723 switch (state) {
1724 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301725 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001726 break;
1727
Doug Anderson01730552014-08-22 19:17:51 +05301728 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001729 case STATE_SENDING_CMD:
1730 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1731 &host->pending_events))
1732 break;
1733
1734 cmd = host->cmd;
1735 host->cmd = NULL;
1736 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001737 err = dw_mci_command_complete(host, cmd);
1738 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001739 prev_state = state = STATE_SENDING_CMD;
1740 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001741 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001742 goto unlock;
1743 }
1744
Seungwon Jeone352c812013-08-31 00:14:17 +09001745 if (cmd->data && err) {
Seungwon Jeon71abb132013-08-31 00:13:59 +09001746 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001747 send_stop_abort(host, data);
1748 state = STATE_SENDING_STOP;
1749 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001750 }
1751
Seungwon Jeone352c812013-08-31 00:14:17 +09001752 if (!cmd->data || err) {
1753 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001754 goto unlock;
1755 }
1756
1757 prev_state = state = STATE_SENDING_DATA;
1758 /* fall through */
1759
1760 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001761 /*
1762 * We could get a data error and never a transfer
1763 * complete so we'd better check for it here.
1764 *
1765 * Note that we don't really care if we also got a
1766 * transfer complete; stopping the DMA and sending an
1767 * abort won't hurt.
1768 */
Will Newtonf95f3852011-01-02 01:11:59 -05001769 if (test_and_clear_bit(EVENT_DATA_ERROR,
1770 &host->pending_events)) {
1771 dw_mci_stop_dma(host);
addy kebdb9a902015-02-20 10:55:25 +08001772 if (data->stop ||
1773 !(host->data_status & (SDMMC_INT_DRTO |
1774 SDMMC_INT_EBE)))
1775 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001776 state = STATE_DATA_ERROR;
1777 break;
1778 }
1779
1780 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001781 &host->pending_events)) {
1782 /*
1783 * If all data-related interrupts don't come
1784 * within the given time in reading data state.
1785 */
1786 if ((host->quirks & DW_MCI_QUIRK_BROKEN_DTO) &&
1787 (host->dir_status == DW_MCI_RECV_STATUS))
1788 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001789 break;
Addy Ke57e10482015-08-11 01:27:18 +09001790 }
Will Newtonf95f3852011-01-02 01:11:59 -05001791
1792 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07001793
1794 /*
1795 * Handle an EVENT_DATA_ERROR that might have shown up
1796 * before the transfer completed. This might not have
1797 * been caught by the check above because the interrupt
1798 * could have gone off between the previous check and
1799 * the check for transfer complete.
1800 *
1801 * Technically this ought not be needed assuming we
1802 * get a DATA_COMPLETE eventually (we'll notice the
1803 * error and end the request), but it shouldn't hurt.
1804 *
1805 * This has the advantage of sending the stop command.
1806 */
1807 if (test_and_clear_bit(EVENT_DATA_ERROR,
1808 &host->pending_events)) {
1809 dw_mci_stop_dma(host);
addy kebdb9a902015-02-20 10:55:25 +08001810 if (data->stop ||
1811 !(host->data_status & (SDMMC_INT_DRTO |
1812 SDMMC_INT_EBE)))
1813 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001814 state = STATE_DATA_ERROR;
1815 break;
1816 }
Will Newtonf95f3852011-01-02 01:11:59 -05001817 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07001818
Will Newtonf95f3852011-01-02 01:11:59 -05001819 /* fall through */
1820
1821 case STATE_DATA_BUSY:
1822 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001823 &host->pending_events)) {
1824 /*
1825 * If data error interrupt comes but data over
1826 * interrupt doesn't come within the given time.
1827 * in reading data state.
1828 */
1829 if ((host->quirks & DW_MCI_QUIRK_BROKEN_DTO) &&
1830 (host->dir_status == DW_MCI_RECV_STATUS))
1831 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001832 break;
Addy Ke57e10482015-08-11 01:27:18 +09001833 }
Will Newtonf95f3852011-01-02 01:11:59 -05001834
1835 host->data = NULL;
1836 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001837 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001838
Seungwon Jeone352c812013-08-31 00:14:17 +09001839 if (!err) {
1840 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05301841 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09001842 data->stop->error = 0;
1843 dw_mci_request_end(host, mrq);
1844 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05001845 }
Will Newtonf95f3852011-01-02 01:11:59 -05001846
Seungwon Jeon90c21432013-08-31 00:14:05 +09001847 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09001848 if (data->stop)
1849 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001850 } else {
1851 /*
1852 * If we don't have a command complete now we'll
1853 * never get one since we just reset everything;
1854 * better end the request.
1855 *
1856 * If we do have a command complete we'll fall
1857 * through to the SENDING_STOP command and
1858 * everything will be peachy keen.
1859 */
1860 if (!test_bit(EVENT_CMD_COMPLETE,
1861 &host->pending_events)) {
1862 host->cmd = NULL;
1863 dw_mci_request_end(host, mrq);
1864 goto unlock;
1865 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09001866 }
Seungwon Jeone352c812013-08-31 00:14:17 +09001867
1868 /*
1869 * If err has non-zero,
1870 * stop-abort command has been already issued.
1871 */
1872 prev_state = state = STATE_SENDING_STOP;
1873
Will Newtonf95f3852011-01-02 01:11:59 -05001874 /* fall through */
1875
1876 case STATE_SENDING_STOP:
1877 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1878 &host->pending_events))
1879 break;
1880
Seungwon Jeon71abb132013-08-31 00:13:59 +09001881 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09001882 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07001883 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09001884
Will Newtonf95f3852011-01-02 01:11:59 -05001885 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001886 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09001887
Seungwon Jeone352c812013-08-31 00:14:17 +09001888 if (mrq->stop)
1889 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001890 else
1891 host->cmd_status = 0;
1892
Seungwon Jeone352c812013-08-31 00:14:17 +09001893 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001894 goto unlock;
1895
1896 case STATE_DATA_ERROR:
1897 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
1898 &host->pending_events))
1899 break;
1900
1901 state = STATE_DATA_BUSY;
1902 break;
1903 }
1904 } while (state != prev_state);
1905
1906 host->state = state;
1907unlock:
1908 spin_unlock(&host->lock);
1909
1910}
1911
James Hogan34b664a2011-06-24 13:57:56 +01001912/* push final bytes to part_buf, only use during push */
1913static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
1914{
1915 memcpy((void *)&host->part_buf, buf, cnt);
1916 host->part_buf_count = cnt;
1917}
1918
1919/* append bytes to part_buf, only use during push */
1920static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
1921{
1922 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
1923 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
1924 host->part_buf_count += cnt;
1925 return cnt;
1926}
1927
1928/* pull first bytes from part_buf, only use during pull */
1929static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
1930{
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001931 cnt = min_t(int, cnt, host->part_buf_count);
James Hogan34b664a2011-06-24 13:57:56 +01001932 if (cnt) {
1933 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
1934 cnt);
1935 host->part_buf_count -= cnt;
1936 host->part_buf_start += cnt;
1937 }
1938 return cnt;
1939}
1940
1941/* pull final bytes from the part_buf, assuming it's just been filled */
1942static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
1943{
1944 memcpy(buf, &host->part_buf, cnt);
1945 host->part_buf_start = cnt;
1946 host->part_buf_count = (1 << host->data_shift) - cnt;
1947}
1948
Will Newtonf95f3852011-01-02 01:11:59 -05001949static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
1950{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001951 struct mmc_data *data = host->data;
1952 int init_cnt = cnt;
1953
James Hogan34b664a2011-06-24 13:57:56 +01001954 /* try and push anything in the part_buf */
1955 if (unlikely(host->part_buf_count)) {
1956 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001957
James Hogan34b664a2011-06-24 13:57:56 +01001958 buf += len;
1959 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001960 if (host->part_buf_count == 2) {
Ben Dooks76184ac2015-03-25 11:27:52 +00001961 mci_fifo_writew(host->fifo_reg, host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01001962 host->part_buf_count = 0;
1963 }
1964 }
1965#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
1966 if (unlikely((unsigned long)buf & 0x1)) {
1967 while (cnt >= 2) {
1968 u16 aligned_buf[64];
1969 int len = min(cnt & -2, (int)sizeof(aligned_buf));
1970 int items = len >> 1;
1971 int i;
1972 /* memcpy from input buffer into aligned buffer */
1973 memcpy(aligned_buf, buf, len);
1974 buf += len;
1975 cnt -= len;
1976 /* push data from aligned buffer into fifo */
1977 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00001978 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01001979 }
1980 } else
1981#endif
1982 {
1983 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001984
James Hogan34b664a2011-06-24 13:57:56 +01001985 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00001986 mci_fifo_writew(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01001987 buf = pdata;
1988 }
1989 /* put anything remaining in the part_buf */
1990 if (cnt) {
1991 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00001992 /* Push data if we have reached the expected data length */
1993 if ((data->bytes_xfered + init_cnt) ==
1994 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00001995 mci_fifo_writew(host->fifo_reg, host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05001996 }
1997}
1998
1999static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2000{
James Hogan34b664a2011-06-24 13:57:56 +01002001#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2002 if (unlikely((unsigned long)buf & 0x1)) {
2003 while (cnt >= 2) {
2004 /* pull data from fifo into aligned buffer */
2005 u16 aligned_buf[64];
2006 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2007 int items = len >> 1;
2008 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002009
James Hogan34b664a2011-06-24 13:57:56 +01002010 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002011 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002012 /* memcpy from aligned buffer into output buffer */
2013 memcpy(buf, aligned_buf, len);
2014 buf += len;
2015 cnt -= len;
2016 }
2017 } else
2018#endif
2019 {
2020 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002021
James Hogan34b664a2011-06-24 13:57:56 +01002022 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002023 *pdata++ = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002024 buf = pdata;
2025 }
2026 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002027 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002028 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002029 }
2030}
2031
2032static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2033{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002034 struct mmc_data *data = host->data;
2035 int init_cnt = cnt;
2036
James Hogan34b664a2011-06-24 13:57:56 +01002037 /* try and push anything in the part_buf */
2038 if (unlikely(host->part_buf_count)) {
2039 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002040
James Hogan34b664a2011-06-24 13:57:56 +01002041 buf += len;
2042 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002043 if (host->part_buf_count == 4) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002044 mci_fifo_writel(host->fifo_reg, host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01002045 host->part_buf_count = 0;
2046 }
2047 }
2048#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2049 if (unlikely((unsigned long)buf & 0x3)) {
2050 while (cnt >= 4) {
2051 u32 aligned_buf[32];
2052 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2053 int items = len >> 2;
2054 int i;
2055 /* memcpy from input buffer into aligned buffer */
2056 memcpy(aligned_buf, buf, len);
2057 buf += len;
2058 cnt -= len;
2059 /* push data from aligned buffer into fifo */
2060 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002061 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002062 }
2063 } else
2064#endif
2065 {
2066 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002067
James Hogan34b664a2011-06-24 13:57:56 +01002068 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002069 mci_fifo_writel(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002070 buf = pdata;
2071 }
2072 /* put anything remaining in the part_buf */
2073 if (cnt) {
2074 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002075 /* Push data if we have reached the expected data length */
2076 if ((data->bytes_xfered + init_cnt) ==
2077 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002078 mci_fifo_writel(host->fifo_reg, host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05002079 }
2080}
2081
2082static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2083{
James Hogan34b664a2011-06-24 13:57:56 +01002084#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2085 if (unlikely((unsigned long)buf & 0x3)) {
2086 while (cnt >= 4) {
2087 /* pull data from fifo into aligned buffer */
2088 u32 aligned_buf[32];
2089 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2090 int items = len >> 2;
2091 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002092
James Hogan34b664a2011-06-24 13:57:56 +01002093 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002094 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002095 /* memcpy from aligned buffer into output buffer */
2096 memcpy(buf, aligned_buf, len);
2097 buf += len;
2098 cnt -= len;
2099 }
2100 } else
2101#endif
2102 {
2103 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002104
James Hogan34b664a2011-06-24 13:57:56 +01002105 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002106 *pdata++ = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002107 buf = pdata;
2108 }
2109 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002110 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002111 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002112 }
2113}
2114
2115static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2116{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002117 struct mmc_data *data = host->data;
2118 int init_cnt = cnt;
2119
James Hogan34b664a2011-06-24 13:57:56 +01002120 /* try and push anything in the part_buf */
2121 if (unlikely(host->part_buf_count)) {
2122 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002123
James Hogan34b664a2011-06-24 13:57:56 +01002124 buf += len;
2125 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09002126
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002127 if (host->part_buf_count == 8) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002128 mci_fifo_writeq(host->fifo_reg, host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01002129 host->part_buf_count = 0;
2130 }
2131 }
2132#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2133 if (unlikely((unsigned long)buf & 0x7)) {
2134 while (cnt >= 8) {
2135 u64 aligned_buf[16];
2136 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2137 int items = len >> 3;
2138 int i;
2139 /* memcpy from input buffer into aligned buffer */
2140 memcpy(aligned_buf, buf, len);
2141 buf += len;
2142 cnt -= len;
2143 /* push data from aligned buffer into fifo */
2144 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002145 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002146 }
2147 } else
2148#endif
2149 {
2150 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002151
James Hogan34b664a2011-06-24 13:57:56 +01002152 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002153 mci_fifo_writeq(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002154 buf = pdata;
2155 }
2156 /* put anything remaining in the part_buf */
2157 if (cnt) {
2158 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002159 /* Push data if we have reached the expected data length */
2160 if ((data->bytes_xfered + init_cnt) ==
2161 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002162 mci_fifo_writeq(host->fifo_reg, host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05002163 }
2164}
2165
2166static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2167{
James Hogan34b664a2011-06-24 13:57:56 +01002168#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2169 if (unlikely((unsigned long)buf & 0x7)) {
2170 while (cnt >= 8) {
2171 /* pull data from fifo into aligned buffer */
2172 u64 aligned_buf[16];
2173 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2174 int items = len >> 3;
2175 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002176
James Hogan34b664a2011-06-24 13:57:56 +01002177 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002178 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2179
James Hogan34b664a2011-06-24 13:57:56 +01002180 /* memcpy from aligned buffer into output buffer */
2181 memcpy(buf, aligned_buf, len);
2182 buf += len;
2183 cnt -= len;
2184 }
2185 } else
2186#endif
2187 {
2188 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002189
James Hogan34b664a2011-06-24 13:57:56 +01002190 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002191 *pdata++ = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002192 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05002193 }
James Hogan34b664a2011-06-24 13:57:56 +01002194 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002195 host->part_buf = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002196 dw_mci_pull_final_bytes(host, buf, cnt);
2197 }
2198}
2199
2200static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2201{
2202 int len;
2203
2204 /* get remaining partial bytes */
2205 len = dw_mci_pull_part_bytes(host, buf, cnt);
2206 if (unlikely(len == cnt))
2207 return;
2208 buf += len;
2209 cnt -= len;
2210
2211 /* get the rest of the data */
2212 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002213}
2214
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002215static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05002216{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002217 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2218 void *buf;
2219 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002220 struct mmc_data *data = host->data;
2221 int shift = host->data_shift;
2222 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002223 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002224 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002225
2226 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002227 if (!sg_miter_next(sg_miter))
2228 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002229
Imre Deak4225fc82013-02-27 17:02:57 -08002230 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002231 buf = sg_miter->addr;
2232 remain = sg_miter->length;
2233 offset = 0;
2234
2235 do {
2236 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2237 << shift) + host->part_buf_count;
2238 len = min(remain, fcnt);
2239 if (!len)
2240 break;
2241 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002242 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002243 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002244 remain -= len;
2245 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002246
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002247 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002248 status = mci_readl(host, MINTSTS);
2249 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002250 /* if the RXDR is ready read again */
2251 } while ((status & SDMMC_INT_RXDR) ||
2252 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002253
2254 if (!remain) {
2255 if (!sg_miter_next(sg_miter))
2256 goto done;
2257 sg_miter->consumed = 0;
2258 }
2259 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002260 return;
2261
2262done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002263 sg_miter_stop(sg_miter);
2264 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002265 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002266 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2267}
2268
2269static void dw_mci_write_data_pio(struct dw_mci *host)
2270{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002271 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2272 void *buf;
2273 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002274 struct mmc_data *data = host->data;
2275 int shift = host->data_shift;
2276 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002277 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002278 unsigned int fifo_depth = host->fifo_depth;
2279 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002280
2281 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002282 if (!sg_miter_next(sg_miter))
2283 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002284
Imre Deak4225fc82013-02-27 17:02:57 -08002285 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002286 buf = sg_miter->addr;
2287 remain = sg_miter->length;
2288 offset = 0;
2289
2290 do {
2291 fcnt = ((fifo_depth -
2292 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2293 << shift) - host->part_buf_count;
2294 len = min(remain, fcnt);
2295 if (!len)
2296 break;
2297 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002298 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002299 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002300 remain -= len;
2301 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002302
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002303 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002304 status = mci_readl(host, MINTSTS);
2305 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002306 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002307
2308 if (!remain) {
2309 if (!sg_miter_next(sg_miter))
2310 goto done;
2311 sg_miter->consumed = 0;
2312 }
2313 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002314 return;
2315
2316done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002317 sg_miter_stop(sg_miter);
2318 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002319 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002320 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2321}
2322
2323static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2324{
2325 if (!host->cmd_status)
2326 host->cmd_status = status;
2327
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002328 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002329
2330 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2331 tasklet_schedule(&host->tasklet);
2332}
2333
Doug Anderson6130e7a2014-10-14 09:33:09 -07002334static void dw_mci_handle_cd(struct dw_mci *host)
2335{
2336 int i;
2337
2338 for (i = 0; i < host->num_slots; i++) {
2339 struct dw_mci_slot *slot = host->slot[i];
2340
2341 if (!slot)
2342 continue;
2343
2344 if (slot->mmc->ops->card_event)
2345 slot->mmc->ops->card_event(slot->mmc);
2346 mmc_detect_change(slot->mmc,
2347 msecs_to_jiffies(host->pdata->detect_delay_ms));
2348 }
2349}
2350
Will Newtonf95f3852011-01-02 01:11:59 -05002351static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2352{
2353 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002354 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302355 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05002356
Markos Chandras1fb5f682013-03-12 10:53:11 +00002357 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2358
Doug Anderson476d79f2013-07-09 13:04:40 -07002359 /*
2360 * DTO fix - version 2.10a and below, and only if internal DMA
2361 * is configured.
2362 */
2363 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO) {
2364 if (!pending &&
2365 ((mci_readl(host, STATUS) >> 17) & 0x1fff))
2366 pending |= SDMMC_INT_DATA_OVER;
2367 }
2368
Markos Chandras1fb5f682013-03-12 10:53:11 +00002369 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302370 /* Check volt switch first, since it can look like an error */
2371 if ((host->state == STATE_SENDING_CMD11) &&
2372 (pending & SDMMC_INT_VOLT_SWITCH)) {
Doug Anderson49ba0302015-04-03 11:13:07 -07002373 unsigned long irqflags;
Doug Anderson5c935162015-03-09 16:18:21 -07002374
Doug Anderson01730552014-08-22 19:17:51 +05302375 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2376 pending &= ~SDMMC_INT_VOLT_SWITCH;
Doug Anderson49ba0302015-04-03 11:13:07 -07002377
2378 /*
2379 * Hold the lock; we know cmd11_timer can't be kicked
2380 * off after the lock is released, so safe to delete.
2381 */
2382 spin_lock_irqsave(&host->irq_lock, irqflags);
Doug Anderson01730552014-08-22 19:17:51 +05302383 dw_mci_cmd_interrupt(host, pending);
Doug Anderson49ba0302015-04-03 11:13:07 -07002384 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2385
2386 del_timer(&host->cmd11_timer);
Doug Anderson01730552014-08-22 19:17:51 +05302387 }
2388
Will Newtonf95f3852011-01-02 01:11:59 -05002389 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2390 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002391 host->cmd_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002392 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002393 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002394 }
2395
2396 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2397 /* if there is an error report DATA_ERROR */
2398 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002399 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002400 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002401 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002402 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002403 }
2404
2405 if (pending & SDMMC_INT_DATA_OVER) {
Addy Ke57e10482015-08-11 01:27:18 +09002406 if (host->quirks & DW_MCI_QUIRK_BROKEN_DTO)
2407 del_timer(&host->dto_timer);
2408
Will Newtonf95f3852011-01-02 01:11:59 -05002409 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2410 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002411 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002412 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002413 if (host->dir_status == DW_MCI_RECV_STATUS) {
2414 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002415 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002416 }
2417 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2418 tasklet_schedule(&host->tasklet);
2419 }
2420
2421 if (pending & SDMMC_INT_RXDR) {
2422 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002423 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002424 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002425 }
2426
2427 if (pending & SDMMC_INT_TXDR) {
2428 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002429 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002430 dw_mci_write_data_pio(host);
2431 }
2432
2433 if (pending & SDMMC_INT_CMD_DONE) {
2434 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002435 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002436 }
2437
2438 if (pending & SDMMC_INT_CD) {
2439 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002440 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002441 }
2442
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302443 /* Handle SDIO Interrupts */
2444 for (i = 0; i < host->num_slots; i++) {
2445 struct dw_mci_slot *slot = host->slot[i];
Doug Andersoned2540e2015-02-25 10:11:52 -08002446
2447 if (!slot)
2448 continue;
2449
Addy Ke76756232014-11-04 22:03:09 +08002450 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2451 mci_writel(host, RINTSTS,
2452 SDMMC_INT_SDIO(slot->sdio_id));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302453 mmc_signal_sdio_irq(slot->mmc);
2454 }
2455 }
2456
Markos Chandras1fb5f682013-03-12 10:53:11 +00002457 }
Will Newtonf95f3852011-01-02 01:11:59 -05002458
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002459 if (host->use_dma != TRANS_MODE_IDMAC)
2460 return IRQ_HANDLED;
2461
2462 /* Handle IDMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002463 if (host->dma_64bit_address == 1) {
2464 pending = mci_readl(host, IDSTS64);
2465 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2466 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2467 SDMMC_IDMAC_INT_RI);
2468 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002469 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002470 }
2471 } else {
2472 pending = mci_readl(host, IDSTS);
2473 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2474 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2475 SDMMC_IDMAC_INT_RI);
2476 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002477 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002478 }
Will Newtonf95f3852011-01-02 01:11:59 -05002479 }
Will Newtonf95f3852011-01-02 01:11:59 -05002480
2481 return IRQ_HANDLED;
2482}
2483
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002484#ifdef CONFIG_OF
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002485/* given a slot, find out the device node representing that slot */
2486static struct device_node *dw_mci_of_find_slot_node(struct dw_mci_slot *slot)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002487{
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002488 struct device *dev = slot->mmc->parent;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002489 struct device_node *np;
2490 const __be32 *addr;
2491 int len;
2492
2493 if (!dev || !dev->of_node)
2494 return NULL;
2495
2496 for_each_child_of_node(dev->of_node, np) {
2497 addr = of_get_property(np, "reg", &len);
2498 if (!addr || (len < sizeof(int)))
2499 continue;
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002500 if (be32_to_cpup(addr) == slot->id)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002501 return np;
2502 }
2503 return NULL;
2504}
2505
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002506static void dw_mci_slot_of_parse(struct dw_mci_slot *slot)
Doug Andersona70aaa62013-01-11 17:03:50 +00002507{
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002508 struct device_node *np = dw_mci_of_find_slot_node(slot);
Doug Andersona70aaa62013-01-11 17:03:50 +00002509
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002510 if (!np)
2511 return;
Doug Andersona70aaa62013-01-11 17:03:50 +00002512
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002513 if (of_property_read_bool(np, "disable-wp")) {
2514 slot->mmc->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
2515 dev_warn(slot->mmc->parent,
2516 "Slot quirk 'disable-wp' is deprecated\n");
2517 }
Doug Andersona70aaa62013-01-11 17:03:50 +00002518}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002519#else /* CONFIG_OF */
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002520static void dw_mci_slot_of_parse(struct dw_mci_slot *slot)
Doug Andersona70aaa62013-01-11 17:03:50 +00002521{
Doug Andersona70aaa62013-01-11 17:03:50 +00002522}
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002523#endif /* CONFIG_OF */
2524
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002525static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002526{
2527 struct mmc_host *mmc;
2528 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002529 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002530 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002531 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002532
Thomas Abraham4a909202012-09-17 18:16:35 +00002533 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002534 if (!mmc)
2535 return -ENOMEM;
2536
2537 slot = mmc_priv(mmc);
2538 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002539 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002540 slot->mmc = mmc;
2541 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002542 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002543
2544 mmc->ops = &dw_mci_ops;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002545 if (of_property_read_u32_array(host->dev->of_node,
2546 "clock-freq-min-max", freq, 2)) {
2547 mmc->f_min = DW_MCI_FREQ_MIN;
2548 mmc->f_max = DW_MCI_FREQ_MAX;
2549 } else {
2550 mmc->f_min = freq[0];
2551 mmc->f_max = freq[1];
2552 }
Will Newtonf95f3852011-01-02 01:11:59 -05002553
Yuvaraj CD51da2242014-08-22 19:17:50 +05302554 /*if there are external regulators, get them*/
2555 ret = mmc_regulator_get_supply(mmc);
2556 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002557 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302558
2559 if (!mmc->ocr_avail)
2560 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002561
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002562 if (host->pdata->caps)
2563 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002564
Abhilash Kesavanab269122012-11-19 10:26:21 +05302565 if (host->pdata->pm_caps)
2566 mmc->pm_caps = host->pdata->pm_caps;
2567
Thomas Abraham800d78b2012-09-17 18:16:42 +00002568 if (host->dev->of_node) {
2569 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2570 if (ctrl_id < 0)
2571 ctrl_id = 0;
2572 } else {
2573 ctrl_id = to_platform_device(host->dev)->id;
2574 }
James Hogancb27a842012-10-16 09:43:08 +01002575 if (drv_data && drv_data->caps)
2576 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002577
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002578 if (host->pdata->caps2)
2579 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002580
Lars-Peter Clauseneff8f2f2015-05-06 20:31:22 +02002581 dw_mci_slot_of_parse(slot);
2582
Doug Anderson3cf890f2014-08-25 11:19:04 -07002583 ret = mmc_of_parse(mmc);
2584 if (ret)
2585 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002586
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002587 /* Useful defaults if platform data is unset. */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002588 if (host->use_dma == TRANS_MODE_IDMAC) {
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002589 mmc->max_segs = host->ring_size;
2590 mmc->max_blk_size = 65536;
2591 mmc->max_seg_size = 0x1000;
2592 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2593 mmc->max_blk_count = mmc->max_req_size / 512;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002594 } else if (host->use_dma == TRANS_MODE_EDMAC) {
2595 mmc->max_segs = 64;
2596 mmc->max_blk_size = 65536;
2597 mmc->max_blk_count = 65535;
2598 mmc->max_req_size =
2599 mmc->max_blk_size * mmc->max_blk_count;
2600 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002601 } else {
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002602 /* TRANS_MODE_PIO */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002603 mmc->max_segs = 64;
2604 mmc->max_blk_size = 65536; /* BLKSIZ is 16 bits */
2605 mmc->max_blk_count = 512;
2606 mmc->max_req_size = mmc->max_blk_size *
2607 mmc->max_blk_count;
2608 mmc->max_seg_size = mmc->max_req_size;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002609 }
Will Newtonf95f3852011-01-02 01:11:59 -05002610
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002611 if (dw_mci_get_cd(mmc))
2612 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2613 else
2614 clear_bit(DW_MMC_CARD_PRESENT, &slot->flags);
2615
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002616 ret = mmc_add_host(mmc);
2617 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002618 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002619
2620#if defined(CONFIG_DEBUG_FS)
2621 dw_mci_init_debugfs(slot);
2622#endif
2623
Will Newtonf95f3852011-01-02 01:11:59 -05002624 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002625
Doug Anderson3cf890f2014-08-25 11:19:04 -07002626err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002627 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302628 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002629}
2630
2631static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2632{
Will Newtonf95f3852011-01-02 01:11:59 -05002633 /* Debugfs stuff is cleaned up by mmc core */
2634 mmc_remove_host(slot->mmc);
2635 slot->host->slot[id] = NULL;
2636 mmc_free_host(slot->mmc);
2637}
2638
2639static void dw_mci_init_dma(struct dw_mci *host)
2640{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002641 int addr_config;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002642 struct device *dev = host->dev;
2643 struct device_node *np = dev->of_node;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002644
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002645 /*
2646 * Check tansfer mode from HCON[17:16]
2647 * Clear the ambiguous description of dw_mmc databook:
2648 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2649 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2650 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2651 * 2b'11: Non DW DMA Interface -> pio only
2652 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2653 * simpler request/acknowledge handshake mechanism and both of them
2654 * are regarded as external dma master for dw_mmc.
2655 */
2656 host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
2657 if (host->use_dma == DMA_INTERFACE_IDMA) {
2658 host->use_dma = TRANS_MODE_IDMAC;
2659 } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
2660 host->use_dma == DMA_INTERFACE_GDMA) {
2661 host->use_dma = TRANS_MODE_EDMAC;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002662 } else {
Will Newtonf95f3852011-01-02 01:11:59 -05002663 goto no_dma;
2664 }
2665
2666 /* Determine which DMA interface to use */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002667 if (host->use_dma == TRANS_MODE_IDMAC) {
2668 /*
2669 * Check ADDR_CONFIG bit in HCON to find
2670 * IDMAC address bus width
2671 */
2672 addr_config = (mci_readl(host, HCON) >> 27) & 0x01;
Will Newtonf95f3852011-01-02 01:11:59 -05002673
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002674 if (addr_config == 1) {
2675 /* host supports IDMAC in 64-bit address mode */
2676 host->dma_64bit_address = 1;
2677 dev_info(host->dev,
2678 "IDMAC supports 64-bit address mode.\n");
2679 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2680 dma_set_coherent_mask(host->dev,
2681 DMA_BIT_MASK(64));
2682 } else {
2683 /* host supports IDMAC in 32-bit address mode */
2684 host->dma_64bit_address = 0;
2685 dev_info(host->dev,
2686 "IDMAC supports 32-bit address mode.\n");
2687 }
2688
2689 /* Alloc memory for sg translation */
2690 host->sg_cpu = dmam_alloc_coherent(host->dev, PAGE_SIZE,
2691 &host->sg_dma, GFP_KERNEL);
2692 if (!host->sg_cpu) {
2693 dev_err(host->dev,
2694 "%s: could not alloc DMA memory\n",
2695 __func__);
2696 goto no_dma;
2697 }
2698
2699 host->dma_ops = &dw_mci_idmac_ops;
2700 dev_info(host->dev, "Using internal DMA controller.\n");
2701 } else {
2702 /* TRANS_MODE_EDMAC: check dma bindings again */
2703 if ((of_property_count_strings(np, "dma-names") < 0) ||
2704 (!of_find_property(np, "dmas", NULL))) {
2705 goto no_dma;
2706 }
2707 host->dma_ops = &dw_mci_edmac_ops;
2708 dev_info(host->dev, "Using external DMA controller.\n");
2709 }
Will Newtonf95f3852011-01-02 01:11:59 -05002710
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002711 if (host->dma_ops->init && host->dma_ops->start &&
2712 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002713 if (host->dma_ops->init(host)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002714 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2715 __func__);
Will Newtonf95f3852011-01-02 01:11:59 -05002716 goto no_dma;
2717 }
2718 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002719 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002720 goto no_dma;
2721 }
2722
Will Newtonf95f3852011-01-02 01:11:59 -05002723 return;
2724
2725no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002726 dev_info(host->dev, "Using PIO mode.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002727 host->use_dma = TRANS_MODE_PIO;
Will Newtonf95f3852011-01-02 01:11:59 -05002728}
2729
Seungwon Jeon31bff452013-08-31 00:14:23 +09002730static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
Will Newtonf95f3852011-01-02 01:11:59 -05002731{
2732 unsigned long timeout = jiffies + msecs_to_jiffies(500);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002733 u32 ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05002734
Seungwon Jeon31bff452013-08-31 00:14:23 +09002735 ctrl = mci_readl(host, CTRL);
2736 ctrl |= reset;
2737 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05002738
2739 /* wait till resets clear */
2740 do {
2741 ctrl = mci_readl(host, CTRL);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002742 if (!(ctrl & reset))
Will Newtonf95f3852011-01-02 01:11:59 -05002743 return true;
2744 } while (time_before(jiffies, timeout));
2745
Seungwon Jeon31bff452013-08-31 00:14:23 +09002746 dev_err(host->dev,
2747 "Timeout resetting block (ctrl reset %#x)\n",
2748 ctrl & reset);
Will Newtonf95f3852011-01-02 01:11:59 -05002749
2750 return false;
2751}
2752
Sonny Rao3a33a942014-08-04 18:19:50 -07002753static bool dw_mci_reset(struct dw_mci *host)
Seungwon Jeon31bff452013-08-31 00:14:23 +09002754{
Sonny Rao3a33a942014-08-04 18:19:50 -07002755 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2756 bool ret = false;
2757
Seungwon Jeon31bff452013-08-31 00:14:23 +09002758 /*
2759 * Reseting generates a block interrupt, hence setting
2760 * the scatter-gather pointer to NULL.
2761 */
2762 if (host->sg) {
2763 sg_miter_stop(&host->sg_miter);
2764 host->sg = NULL;
2765 }
2766
Sonny Rao3a33a942014-08-04 18:19:50 -07002767 if (host->use_dma)
2768 flags |= SDMMC_CTRL_DMA_RESET;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002769
Sonny Rao3a33a942014-08-04 18:19:50 -07002770 if (dw_mci_ctrl_reset(host, flags)) {
2771 /*
2772 * In all cases we clear the RAWINTS register to clear any
2773 * interrupts.
2774 */
2775 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2776
2777 /* if using dma we wait for dma_req to clear */
2778 if (host->use_dma) {
2779 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2780 u32 status;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002781
Sonny Rao3a33a942014-08-04 18:19:50 -07002782 do {
2783 status = mci_readl(host, STATUS);
2784 if (!(status & SDMMC_STATUS_DMA_REQ))
2785 break;
2786 cpu_relax();
2787 } while (time_before(jiffies, timeout));
2788
2789 if (status & SDMMC_STATUS_DMA_REQ) {
2790 dev_err(host->dev,
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002791 "%s: Timeout waiting for dma_req to clear during reset\n",
2792 __func__);
Sonny Rao3a33a942014-08-04 18:19:50 -07002793 goto ciu_out;
2794 }
2795
2796 /* when using DMA next we reset the fifo again */
2797 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2798 goto ciu_out;
2799 }
2800 } else {
2801 /* if the controller reset bit did clear, then set clock regs */
2802 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002803 dev_err(host->dev,
2804 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
Sonny Rao3a33a942014-08-04 18:19:50 -07002805 __func__);
2806 goto ciu_out;
2807 }
2808 }
2809
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002810 if (host->use_dma == TRANS_MODE_IDMAC)
2811 /* It is also recommended that we reset and reprogram idmac */
2812 dw_mci_idmac_reset(host);
Sonny Rao3a33a942014-08-04 18:19:50 -07002813
2814 ret = true;
2815
2816ciu_out:
2817 /* After a CTRL reset we need to have CIU set clock registers */
2818 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2819
2820 return ret;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002821}
2822
Doug Anderson5c935162015-03-09 16:18:21 -07002823static void dw_mci_cmd11_timer(unsigned long arg)
2824{
2825 struct dw_mci *host = (struct dw_mci *)arg;
2826
Doug Andersonfd674192015-04-03 11:13:06 -07002827 if (host->state != STATE_SENDING_CMD11) {
2828 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2829 return;
2830 }
Doug Anderson5c935162015-03-09 16:18:21 -07002831
2832 host->cmd_status = SDMMC_INT_RTO;
2833 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2834 tasklet_schedule(&host->tasklet);
2835}
2836
Addy Ke57e10482015-08-11 01:27:18 +09002837static void dw_mci_dto_timer(unsigned long arg)
2838{
2839 struct dw_mci *host = (struct dw_mci *)arg;
2840
2841 switch (host->state) {
2842 case STATE_SENDING_DATA:
2843 case STATE_DATA_BUSY:
2844 /*
2845 * If DTO interrupt does NOT come in sending data state,
2846 * we should notify the driver to terminate current transfer
2847 * and report a data timeout to the core.
2848 */
2849 host->data_status = SDMMC_INT_DRTO;
2850 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2851 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2852 tasklet_schedule(&host->tasklet);
2853 break;
2854 default:
2855 break;
2856 }
2857}
2858
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002859#ifdef CONFIG_OF
2860static struct dw_mci_of_quirks {
2861 char *quirk;
2862 int id;
2863} of_quirks[] = {
2864 {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002865 .quirk = "broken-cd",
2866 .id = DW_MCI_QUIRK_BROKEN_CARD_DETECTION,
2867 },
2868};
2869
2870static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2871{
2872 struct dw_mci_board *pdata;
2873 struct device *dev = host->dev;
2874 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002875 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002876 int idx, ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002877 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002878
2879 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +09002880 if (!pdata)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002881 return ERR_PTR(-ENOMEM);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002882
2883 /* find out number of slots supported */
2884 if (of_property_read_u32(dev->of_node, "num-slots",
2885 &pdata->num_slots)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002886 dev_info(dev,
2887 "num-slots property not found, assuming 1 slot is available\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002888 pdata->num_slots = 1;
2889 }
2890
2891 /* get quirks */
2892 for (idx = 0; idx < ARRAY_SIZE(of_quirks); idx++)
2893 if (of_get_property(np, of_quirks[idx].quirk, NULL))
2894 pdata->quirks |= of_quirks[idx].id;
2895
2896 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002897 dev_info(dev,
2898 "fifo-depth property not found, using value of FIFOTH register as default\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002899
2900 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2901
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002902 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2903 pdata->bus_hz = clock_frequency;
2904
James Hogancb27a842012-10-16 09:43:08 +01002905 if (drv_data && drv_data->parse_dt) {
2906 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002907 if (ret)
2908 return ERR_PTR(ret);
2909 }
2910
Jaehoon Chung40a7a462015-08-06 16:23:26 +09002911 if (of_find_property(np, "supports-highspeed", NULL)) {
2912 dev_info(dev, "supports-highspeed property is deprecated.\n");
Seungwon Jeon10b49842013-08-31 00:13:22 +09002913 pdata->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Jaehoon Chung40a7a462015-08-06 16:23:26 +09002914 }
Seungwon Jeon10b49842013-08-31 00:13:22 +09002915
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002916 return pdata;
2917}
2918
2919#else /* CONFIG_OF */
2920static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2921{
2922 return ERR_PTR(-EINVAL);
2923}
2924#endif /* CONFIG_OF */
2925
Doug Andersonfa0c3282015-02-25 10:11:51 -08002926static void dw_mci_enable_cd(struct dw_mci *host)
2927{
2928 struct dw_mci_board *brd = host->pdata;
2929 unsigned long irqflags;
2930 u32 temp;
2931 int i;
2932
2933 /* No need for CD if broken card detection */
2934 if (brd->quirks & DW_MCI_QUIRK_BROKEN_CARD_DETECTION)
2935 return;
2936
2937 /* No need for CD if all slots have a non-error GPIO */
2938 for (i = 0; i < host->num_slots; i++) {
2939 struct dw_mci_slot *slot = host->slot[i];
2940
2941 if (IS_ERR_VALUE(mmc_gpio_get_cd(slot->mmc)))
2942 break;
2943 }
2944 if (i == host->num_slots)
2945 return;
2946
2947 spin_lock_irqsave(&host->irq_lock, irqflags);
2948 temp = mci_readl(host, INTMASK);
2949 temp |= SDMMC_INT_CD;
2950 mci_writel(host, INTMASK, temp);
2951 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2952}
2953
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302954int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05002955{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002956 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302957 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002958 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00002959 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05002960
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002961 if (!host->pdata) {
2962 host->pdata = dw_mci_parse_dt(host);
2963 if (IS_ERR(host->pdata)) {
2964 dev_err(host->dev, "platform data not available\n");
2965 return -EINVAL;
2966 }
Will Newtonf95f3852011-01-02 01:11:59 -05002967 }
2968
Jaehoon Chung9e747b72015-08-06 16:23:24 +09002969 if (host->pdata->num_slots < 1) {
Thomas Abraham4a909202012-09-17 18:16:35 +00002970 dev_err(host->dev,
Jaehoon Chung907abd52014-03-03 11:36:43 +09002971 "Platform data must supply num_slots.\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05302972 return -ENODEV;
Will Newtonf95f3852011-01-02 01:11:59 -05002973 }
2974
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002975 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002976 if (IS_ERR(host->biu_clk)) {
2977 dev_dbg(host->dev, "biu clock not available\n");
2978 } else {
2979 ret = clk_prepare_enable(host->biu_clk);
2980 if (ret) {
2981 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002982 return ret;
2983 }
Will Newtonf95f3852011-01-02 01:11:59 -05002984 }
2985
Seungwon Jeon780f22a2012-11-28 19:26:03 +09002986 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002987 if (IS_ERR(host->ciu_clk)) {
2988 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002989 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002990 } else {
2991 ret = clk_prepare_enable(host->ciu_clk);
2992 if (ret) {
2993 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002994 goto err_clk_biu;
2995 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00002996
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002997 if (host->pdata->bus_hz) {
2998 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
2999 if (ret)
3000 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003001 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003002 host->pdata->bus_hz);
3003 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003004 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003005 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003006
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003007 if (!host->bus_hz) {
3008 dev_err(host->dev,
3009 "Platform data must supply bus speed\n");
3010 ret = -ENODEV;
3011 goto err_clk_ciu;
3012 }
3013
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09003014 if (drv_data && drv_data->init) {
3015 ret = drv_data->init(host);
3016 if (ret) {
3017 dev_err(host->dev,
3018 "implementation specific init failed\n");
3019 goto err_clk_ciu;
3020 }
3021 }
3022
James Hogancb27a842012-10-16 09:43:08 +01003023 if (drv_data && drv_data->setup_clock) {
3024 ret = drv_data->setup_clock(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00003025 if (ret) {
3026 dev_err(host->dev,
3027 "implementation specific clock setup failed\n");
3028 goto err_clk_ciu;
3029 }
3030 }
3031
Doug Anderson5c935162015-03-09 16:18:21 -07003032 setup_timer(&host->cmd11_timer,
3033 dw_mci_cmd11_timer, (unsigned long)host);
3034
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303035 host->quirks = host->pdata->quirks;
Will Newtonf95f3852011-01-02 01:11:59 -05003036
Addy Ke57e10482015-08-11 01:27:18 +09003037 if (host->quirks & DW_MCI_QUIRK_BROKEN_DTO)
3038 setup_timer(&host->dto_timer,
3039 dw_mci_dto_timer, (unsigned long)host);
3040
Will Newtonf95f3852011-01-02 01:11:59 -05003041 spin_lock_init(&host->lock);
Doug Andersonf8c58c12014-12-02 15:42:47 -08003042 spin_lock_init(&host->irq_lock);
Will Newtonf95f3852011-01-02 01:11:59 -05003043 INIT_LIST_HEAD(&host->queue);
3044
Will Newtonf95f3852011-01-02 01:11:59 -05003045 /*
3046 * Get the host data width - this assumes that HCON has been set with
3047 * the correct values.
3048 */
3049 i = (mci_readl(host, HCON) >> 7) & 0x7;
3050 if (!i) {
3051 host->push_data = dw_mci_push_data16;
3052 host->pull_data = dw_mci_pull_data16;
3053 width = 16;
3054 host->data_shift = 1;
3055 } else if (i == 2) {
3056 host->push_data = dw_mci_push_data64;
3057 host->pull_data = dw_mci_pull_data64;
3058 width = 64;
3059 host->data_shift = 3;
3060 } else {
3061 /* Check for a reserved value, and warn if it is */
3062 WARN((i != 1),
3063 "HCON reports a reserved host data width!\n"
3064 "Defaulting to 32-bit access.\n");
3065 host->push_data = dw_mci_push_data32;
3066 host->pull_data = dw_mci_pull_data32;
3067 width = 32;
3068 host->data_shift = 2;
3069 }
3070
3071 /* Reset all blocks */
Sonny Rao3a33a942014-08-04 18:19:50 -07003072 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS))
Seungwon Jeon141a7122012-05-22 13:01:03 +09003073 return -ENODEV;
3074
3075 host->dma_ops = host->pdata->dma_ops;
3076 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05003077
3078 /* Clear the interrupts for the host controller */
3079 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3080 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3081
3082 /* Put in max timeout */
3083 mci_writel(host, TMOUT, 0xFFFFFFFF);
3084
3085 /*
3086 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3087 * Tx Mark = fifo_size / 2 DMA Size = 8
3088 */
James Hoganb86d8252011-06-24 13:57:18 +01003089 if (!host->pdata->fifo_depth) {
3090 /*
3091 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3092 * have been overwritten by the bootloader, just like we're
3093 * about to do, so if you know the value for your hardware, you
3094 * should put it in the platform data.
3095 */
3096 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00003097 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01003098 } else {
3099 fifo_size = host->pdata->fifo_depth;
3100 }
3101 host->fifo_depth = fifo_size;
Seungwon Jeon52426892013-08-31 00:13:42 +09003102 host->fifoth_val =
3103 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003104 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05003105
3106 /* disable clock to CIU */
3107 mci_writel(host, CLKENA, 0);
3108 mci_writel(host, CLKSRC, 0);
3109
James Hogan63008762013-03-12 10:43:54 +00003110 /*
3111 * In 2.40a spec, Data offset is changed.
3112 * Need to check the version-id and set data-offset for DATA register.
3113 */
3114 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3115 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3116
3117 if (host->verid < DW_MMC_240A)
Ben Dooks76184ac2015-03-25 11:27:52 +00003118 host->fifo_reg = host->regs + DATA_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003119 else
Ben Dooks76184ac2015-03-25 11:27:52 +00003120 host->fifo_reg = host->regs + DATA_240A_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003121
Will Newtonf95f3852011-01-02 01:11:59 -05003122 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003123 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3124 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05003125 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07003126 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003127
Will Newtonf95f3852011-01-02 01:11:59 -05003128 if (host->pdata->num_slots)
3129 host->num_slots = host->pdata->num_slots;
3130 else
3131 host->num_slots = ((mci_readl(host, HCON) >> 1) & 0x1F) + 1;
3132
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303133 /*
Doug Andersonfa0c3282015-02-25 10:11:51 -08003134 * Enable interrupts for command done, data over, data empty,
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303135 * receive ready and error such as transmit, receive timeout, crc error
3136 */
3137 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3138 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3139 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003140 DW_MCI_ERROR_FLAGS);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003141 /* Enable mci interrupt */
3142 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303143
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003144 dev_info(host->dev,
3145 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303146 host->irq, width, fifo_size);
3147
Will Newtonf95f3852011-01-02 01:11:59 -05003148 /* We need at least one slot to succeed */
3149 for (i = 0; i < host->num_slots; i++) {
3150 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003151 if (ret)
3152 dev_dbg(host->dev, "slot %d init failed\n", i);
3153 else
3154 init_slots++;
3155 }
3156
3157 if (init_slots) {
3158 dev_info(host->dev, "%d slots initialized\n", init_slots);
3159 } else {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003160 dev_dbg(host->dev,
3161 "attempted to initialize %d slots, but failed on all\n",
3162 host->num_slots);
Doug Anderson6130e7a2014-10-14 09:33:09 -07003163 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003164 }
3165
Doug Andersonb793f652015-03-11 15:15:14 -07003166 /* Now that slots are all setup, we can enable card detect */
3167 dw_mci_enable_cd(host);
3168
Will Newtonf95f3852011-01-02 01:11:59 -05003169 if (host->quirks & DW_MCI_QUIRK_IDMAC_DTO)
Thomas Abraham4a909202012-09-17 18:16:35 +00003170 dev_info(host->dev, "Internal DMAC interrupt fix enabled.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05003171
3172 return 0;
3173
Will Newtonf95f3852011-01-02 01:11:59 -05003174err_dmaunmap:
3175 if (host->use_dma && host->dma_ops->exit)
3176 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003177
3178err_clk_ciu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003179 if (!IS_ERR(host->ciu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003180 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003181
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003182err_clk_biu:
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003183 if (!IS_ERR(host->biu_clk))
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003184 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003185
Will Newtonf95f3852011-01-02 01:11:59 -05003186 return ret;
3187}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303188EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05003189
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303190void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003191{
Will Newtonf95f3852011-01-02 01:11:59 -05003192 int i;
3193
Will Newtonf95f3852011-01-02 01:11:59 -05003194 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00003195 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05003196 if (host->slot[i])
3197 dw_mci_cleanup_slot(host->slot[i], i);
3198 }
3199
Prabu Thangamuthu048fd7e2015-05-28 12:21:06 +00003200 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3201 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3202
Will Newtonf95f3852011-01-02 01:11:59 -05003203 /* disable clock to CIU */
3204 mci_writel(host, CLKENA, 0);
3205 mci_writel(host, CLKSRC, 0);
3206
Will Newtonf95f3852011-01-02 01:11:59 -05003207 if (host->use_dma && host->dma_ops->exit)
3208 host->dma_ops->exit(host);
3209
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003210 if (!IS_ERR(host->ciu_clk))
3211 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003212
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003213 if (!IS_ERR(host->biu_clk))
3214 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05003215}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303216EXPORT_SYMBOL(dw_mci_remove);
3217
3218
Will Newtonf95f3852011-01-02 01:11:59 -05003219
Jaehoon Chung6fe88902011-12-08 19:23:03 +09003220#ifdef CONFIG_PM_SLEEP
Will Newtonf95f3852011-01-02 01:11:59 -05003221/*
3222 * TODO: we should probably disable the clock to the card in the suspend path.
3223 */
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303224int dw_mci_suspend(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003225{
Shawn Lin3fc7eae2015-09-16 14:41:23 +08003226 if (host->use_dma && host->dma_ops->exit)
3227 host->dma_ops->exit(host);
3228
Will Newtonf95f3852011-01-02 01:11:59 -05003229 return 0;
3230}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303231EXPORT_SYMBOL(dw_mci_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05003232
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303233int dw_mci_resume(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003234{
3235 int i, ret;
Will Newtonf95f3852011-01-02 01:11:59 -05003236
Sonny Rao3a33a942014-08-04 18:19:50 -07003237 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003238 ret = -ENODEV;
3239 return ret;
3240 }
3241
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04003242 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09003243 host->dma_ops->init(host);
3244
Seungwon Jeon52426892013-08-31 00:13:42 +09003245 /*
3246 * Restore the initial value at FIFOTH register
3247 * And Invalidate the prev_blksz with zero
3248 */
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003249 mci_writel(host, FIFOTH, host->fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +09003250 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003251
Doug Anderson2eb29442013-08-31 00:11:49 +09003252 /* Put in max timeout */
3253 mci_writel(host, TMOUT, 0xFFFFFFFF);
3254
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003255 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3256 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3257 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003258 DW_MCI_ERROR_FLAGS);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003259 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3260
Will Newtonf95f3852011-01-02 01:11:59 -05003261 for (i = 0; i < host->num_slots; i++) {
3262 struct dw_mci_slot *slot = host->slot[i];
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003263
Will Newtonf95f3852011-01-02 01:11:59 -05003264 if (!slot)
3265 continue;
Abhilash Kesavanab269122012-11-19 10:26:21 +05303266 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER) {
3267 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
3268 dw_mci_setup_bus(slot, true);
3269 }
Will Newtonf95f3852011-01-02 01:11:59 -05003270 }
Doug Andersonfa0c3282015-02-25 10:11:51 -08003271
3272 /* Now that slots are all setup, we can enable card detect */
3273 dw_mci_enable_cd(host);
3274
Will Newtonf95f3852011-01-02 01:11:59 -05003275 return 0;
3276}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303277EXPORT_SYMBOL(dw_mci_resume);
Jaehoon Chung6fe88902011-12-08 19:23:03 +09003278#endif /* CONFIG_PM_SLEEP */
3279
Will Newtonf95f3852011-01-02 01:11:59 -05003280static int __init dw_mci_init(void)
3281{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05303282 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303283 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003284}
3285
3286static void __exit dw_mci_exit(void)
3287{
Will Newtonf95f3852011-01-02 01:11:59 -05003288}
3289
3290module_init(dw_mci_init);
3291module_exit(dw_mci_exit);
3292
3293MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3294MODULE_AUTHOR("NXP Semiconductor VietNam");
3295MODULE_AUTHOR("Imagination Technologies Ltd");
3296MODULE_LICENSE("GPL v2");