blob: a9ac0b4573131f48cad46044e018b5de479cf695 [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/bitops.h>
Jaehoon Chungc07946a2011-02-25 11:08:14 +090036#include <linux/regulator/consumer.h>
Thomas Abrahamc91eab42012-09-17 18:16:40 +000037#include <linux/of.h>
Doug Anderson55a6ceb2013-01-11 17:03:53 +000038#include <linux/of_gpio.h>
Zhangfei Gaobf626e52014-01-09 22:35:10 +080039#include <linux/mmc/slot-gpio.h>
Will Newtonf95f3852011-01-02 01:11:59 -050040
41#include "dw_mmc.h"
42
43/* Common flag combinations */
Jaehoon Chung3f7eec62013-05-27 13:47:57 +090044#define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \
Will Newtonf95f3852011-01-02 01:11:59 -050045 SDMMC_INT_HTO | SDMMC_INT_SBE | \
Doug Anderson7a3c5672015-03-10 08:48:10 -070046 SDMMC_INT_EBE | SDMMC_INT_HLE)
Will Newtonf95f3852011-01-02 01:11:59 -050047#define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \
Doug Anderson7a3c5672015-03-10 08:48:10 -070048 SDMMC_INT_RESP_ERR | SDMMC_INT_HLE)
Will Newtonf95f3852011-01-02 01:11:59 -050049#define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \
Doug Anderson7a3c5672015-03-10 08:48:10 -070050 DW_MCI_CMD_ERROR_FLAGS)
Will Newtonf95f3852011-01-02 01:11:59 -050051#define DW_MCI_SEND_STATUS 1
52#define DW_MCI_RECV_STATUS 2
53#define DW_MCI_DMA_THRESHOLD 16
54
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090055#define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */
Jaehoon Chung72e83572016-11-17 16:40:35 +090056#define DW_MCI_FREQ_MIN 100000 /* unit: HZ */
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +090057
Joonyoung Shimfc79a4d2013-04-26 15:35:22 +090058#define IDMAC_INT_CLR (SDMMC_IDMAC_INT_AI | SDMMC_IDMAC_INT_NI | \
59 SDMMC_IDMAC_INT_CES | SDMMC_IDMAC_INT_DU | \
60 SDMMC_IDMAC_INT_FBE | SDMMC_IDMAC_INT_RI | \
61 SDMMC_IDMAC_INT_TI)
62
Shawn Lincc190d42016-09-02 12:14:39 +080063#define DESC_RING_BUF_SZ PAGE_SIZE
64
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000065struct idmac_desc_64addr {
66 u32 des0; /* Control Descriptor */
67
68 u32 des1; /* Reserved */
69
70 u32 des2; /*Buffer sizes */
71#define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \
Ben Dooks6687c422015-03-25 11:27:51 +000072 ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \
73 ((cpu_to_le32(s)) & cpu_to_le32(0x1fff)))
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +000074
75 u32 des3; /* Reserved */
76
77 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/
78 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/
79
80 u32 des6; /* Lower 32-bits of Next Descriptor Address */
81 u32 des7; /* Upper 32-bits of Next Descriptor Address */
82};
83
Will Newtonf95f3852011-01-02 01:11:59 -050084struct idmac_desc {
Ben Dooks6687c422015-03-25 11:27:51 +000085 __le32 des0; /* Control Descriptor */
Will Newtonf95f3852011-01-02 01:11:59 -050086#define IDMAC_DES0_DIC BIT(1)
87#define IDMAC_DES0_LD BIT(2)
88#define IDMAC_DES0_FD BIT(3)
89#define IDMAC_DES0_CH BIT(4)
90#define IDMAC_DES0_ER BIT(5)
91#define IDMAC_DES0_CES BIT(30)
92#define IDMAC_DES0_OWN BIT(31)
93
Ben Dooks6687c422015-03-25 11:27:51 +000094 __le32 des1; /* Buffer sizes */
Will Newtonf95f3852011-01-02 01:11:59 -050095#define IDMAC_SET_BUFFER1_SIZE(d, s) \
Ben Dookse5306c32016-06-07 14:37:19 +010096 ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff)))
Will Newtonf95f3852011-01-02 01:11:59 -050097
Ben Dooks6687c422015-03-25 11:27:51 +000098 __le32 des2; /* buffer 1 physical address */
Will Newtonf95f3852011-01-02 01:11:59 -050099
Ben Dooks6687c422015-03-25 11:27:51 +0000100 __le32 des3; /* buffer 2 physical address */
Will Newtonf95f3852011-01-02 01:11:59 -0500101};
Alexey Brodkin5959b322015-06-25 11:25:07 +0300102
103/* Each descriptor can transfer up to 4KB of data in chained mode */
104#define DW_MCI_DESC_DATA_LENGTH 0x1000
Will Newtonf95f3852011-01-02 01:11:59 -0500105
Sonny Rao3a33a942014-08-04 18:19:50 -0700106static bool dw_mci_reset(struct dw_mci *host);
Sonny Rao536f6b92014-10-16 09:58:05 -0700107static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset);
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800108static int dw_mci_card_busy(struct mmc_host *mmc);
Shawn Lin56f69112016-05-27 14:37:05 +0800109static int dw_mci_get_cd(struct mmc_host *mmc);
Seungwon Jeon31bff452013-08-31 00:14:23 +0900110
Will Newtonf95f3852011-01-02 01:11:59 -0500111#if defined(CONFIG_DEBUG_FS)
112static int dw_mci_req_show(struct seq_file *s, void *v)
113{
114 struct dw_mci_slot *slot = s->private;
115 struct mmc_request *mrq;
116 struct mmc_command *cmd;
117 struct mmc_command *stop;
118 struct mmc_data *data;
119
120 /* Make sure we get a consistent snapshot */
121 spin_lock_bh(&slot->host->lock);
122 mrq = slot->mrq;
123
124 if (mrq) {
125 cmd = mrq->cmd;
126 data = mrq->data;
127 stop = mrq->stop;
128
129 if (cmd)
130 seq_printf(s,
131 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
132 cmd->opcode, cmd->arg, cmd->flags,
133 cmd->resp[0], cmd->resp[1], cmd->resp[2],
134 cmd->resp[2], cmd->error);
135 if (data)
136 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
137 data->bytes_xfered, data->blocks,
138 data->blksz, data->flags, data->error);
139 if (stop)
140 seq_printf(s,
141 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
142 stop->opcode, stop->arg, stop->flags,
143 stop->resp[0], stop->resp[1], stop->resp[2],
144 stop->resp[2], stop->error);
145 }
146
147 spin_unlock_bh(&slot->host->lock);
148
149 return 0;
150}
151
152static int dw_mci_req_open(struct inode *inode, struct file *file)
153{
154 return single_open(file, dw_mci_req_show, inode->i_private);
155}
156
157static const struct file_operations dw_mci_req_fops = {
158 .owner = THIS_MODULE,
159 .open = dw_mci_req_open,
160 .read = seq_read,
161 .llseek = seq_lseek,
162 .release = single_release,
163};
164
165static int dw_mci_regs_show(struct seq_file *s, void *v)
166{
Jaehoon Chung21657ebd2016-11-17 16:40:33 +0900167 struct dw_mci *host = s->private;
168
169 seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS));
170 seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS));
171 seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD));
172 seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL));
173 seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK));
174 seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA));
Will Newtonf95f3852011-01-02 01:11:59 -0500175
176 return 0;
177}
178
179static int dw_mci_regs_open(struct inode *inode, struct file *file)
180{
181 return single_open(file, dw_mci_regs_show, inode->i_private);
182}
183
184static const struct file_operations dw_mci_regs_fops = {
185 .owner = THIS_MODULE,
186 .open = dw_mci_regs_open,
187 .read = seq_read,
188 .llseek = seq_lseek,
189 .release = single_release,
190};
191
192static void dw_mci_init_debugfs(struct dw_mci_slot *slot)
193{
194 struct mmc_host *mmc = slot->mmc;
195 struct dw_mci *host = slot->host;
196 struct dentry *root;
197 struct dentry *node;
198
199 root = mmc->debugfs_root;
200 if (!root)
201 return;
202
203 node = debugfs_create_file("regs", S_IRUSR, root, host,
204 &dw_mci_regs_fops);
205 if (!node)
206 goto err;
207
208 node = debugfs_create_file("req", S_IRUSR, root, slot,
209 &dw_mci_req_fops);
210 if (!node)
211 goto err;
212
213 node = debugfs_create_u32("state", S_IRUSR, root, (u32 *)&host->state);
214 if (!node)
215 goto err;
216
217 node = debugfs_create_x32("pending_events", S_IRUSR, root,
218 (u32 *)&host->pending_events);
219 if (!node)
220 goto err;
221
222 node = debugfs_create_x32("completed_events", S_IRUSR, root,
223 (u32 *)&host->completed_events);
224 if (!node)
225 goto err;
226
227 return;
228
229err:
230 dev_err(&mmc->class_dev, "failed to initialize debugfs for slot\n");
231}
232#endif /* defined(CONFIG_DEBUG_FS) */
233
Doug Anderson01730552014-08-22 19:17:51 +0530234static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg);
235
Will Newtonf95f3852011-01-02 01:11:59 -0500236static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd)
237{
Thomas Abraham800d78b2012-09-17 18:16:42 +0000238 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson01730552014-08-22 19:17:51 +0530239 struct dw_mci *host = slot->host;
Will Newtonf95f3852011-01-02 01:11:59 -0500240 u32 cmdr;
Will Newtonf95f3852011-01-02 01:11:59 -0500241
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800242 cmd->error = -EINPROGRESS;
Will Newtonf95f3852011-01-02 01:11:59 -0500243 cmdr = cmd->opcode;
244
Seungwon Jeon90c21432013-08-31 00:14:05 +0900245 if (cmd->opcode == MMC_STOP_TRANSMISSION ||
246 cmd->opcode == MMC_GO_IDLE_STATE ||
247 cmd->opcode == MMC_GO_INACTIVE_STATE ||
248 (cmd->opcode == SD_IO_RW_DIRECT &&
249 ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT))
Will Newtonf95f3852011-01-02 01:11:59 -0500250 cmdr |= SDMMC_CMD_STOP;
Jaehoon Chung4a1b27a2014-03-03 11:36:44 +0900251 else if (cmd->opcode != MMC_SEND_STATUS && cmd->data)
252 cmdr |= SDMMC_CMD_PRV_DAT_WAIT;
Will Newtonf95f3852011-01-02 01:11:59 -0500253
Doug Anderson01730552014-08-22 19:17:51 +0530254 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
255 u32 clk_en_a;
256
257 /* Special bit makes CMD11 not die */
258 cmdr |= SDMMC_CMD_VOLT_SWITCH;
259
260 /* Change state to continue to handle CMD11 weirdness */
261 WARN_ON(slot->host->state != STATE_SENDING_CMD);
262 slot->host->state = STATE_SENDING_CMD11;
263
264 /*
265 * We need to disable low power mode (automatic clock stop)
266 * while doing voltage switch so we don't confuse the card,
267 * since stopping the clock is a specific part of the UHS
268 * voltage change dance.
269 *
270 * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be
271 * unconditionally turned back on in dw_mci_setup_bus() if it's
272 * ever called with a non-zero clock. That shouldn't happen
273 * until the voltage change is all done.
274 */
275 clk_en_a = mci_readl(host, CLKENA);
276 clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id);
277 mci_writel(host, CLKENA, clk_en_a);
278 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
279 SDMMC_CMD_PRV_DAT_WAIT, 0);
280 }
281
Will Newtonf95f3852011-01-02 01:11:59 -0500282 if (cmd->flags & MMC_RSP_PRESENT) {
283 /* We expect a response, so set this bit */
284 cmdr |= SDMMC_CMD_RESP_EXP;
285 if (cmd->flags & MMC_RSP_136)
286 cmdr |= SDMMC_CMD_RESP_LONG;
287 }
288
289 if (cmd->flags & MMC_RSP_CRC)
290 cmdr |= SDMMC_CMD_RESP_CRC;
291
Jaehoon Chung0349c082016-11-17 16:40:39 +0900292 if (cmd->data) {
Will Newtonf95f3852011-01-02 01:11:59 -0500293 cmdr |= SDMMC_CMD_DAT_EXP;
Jaehoon Chung0349c082016-11-17 16:40:39 +0900294 if (cmd->data->flags & MMC_DATA_WRITE)
Will Newtonf95f3852011-01-02 01:11:59 -0500295 cmdr |= SDMMC_CMD_DAT_WR;
296 }
297
Jaehoon Chungaaaaeb72016-01-21 11:01:06 +0900298 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags))
299 cmdr |= SDMMC_CMD_USE_HOLD_REG;
Thomas Abraham800d78b2012-09-17 18:16:42 +0000300
Will Newtonf95f3852011-01-02 01:11:59 -0500301 return cmdr;
302}
303
Seungwon Jeon90c21432013-08-31 00:14:05 +0900304static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd)
305{
306 struct mmc_command *stop;
307 u32 cmdr;
308
309 if (!cmd->data)
310 return 0;
311
312 stop = &host->stop_abort;
313 cmdr = cmd->opcode;
314 memset(stop, 0, sizeof(struct mmc_command));
315
316 if (cmdr == MMC_READ_SINGLE_BLOCK ||
317 cmdr == MMC_READ_MULTIPLE_BLOCK ||
318 cmdr == MMC_WRITE_BLOCK ||
Ulf Hansson6c2c6502014-12-01 16:13:39 +0100319 cmdr == MMC_WRITE_MULTIPLE_BLOCK ||
320 cmdr == MMC_SEND_TUNING_BLOCK ||
321 cmdr == MMC_SEND_TUNING_BLOCK_HS200) {
Seungwon Jeon90c21432013-08-31 00:14:05 +0900322 stop->opcode = MMC_STOP_TRANSMISSION;
323 stop->arg = 0;
324 stop->flags = MMC_RSP_R1B | MMC_CMD_AC;
325 } else if (cmdr == SD_IO_RW_EXTENDED) {
326 stop->opcode = SD_IO_RW_DIRECT;
327 stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) |
328 ((cmd->arg >> 28) & 0x7);
329 stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC;
330 } else {
331 return 0;
332 }
333
334 cmdr = stop->opcode | SDMMC_CMD_STOP |
335 SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP;
336
Jaehoon Chung8c005b42016-11-17 16:40:36 +0900337 if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->cur_slot->flags))
338 cmdr |= SDMMC_CMD_USE_HOLD_REG;
339
Seungwon Jeon90c21432013-08-31 00:14:05 +0900340 return cmdr;
341}
342
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800343static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags)
344{
345 unsigned long timeout = jiffies + msecs_to_jiffies(500);
346
347 /*
348 * Databook says that before issuing a new data transfer command
349 * we need to check to see if the card is busy. Data transfer commands
350 * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that.
351 *
352 * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is
353 * expected.
354 */
355 if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) &&
356 !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) {
357 while (mci_readl(host, STATUS) & SDMMC_STATUS_BUSY) {
358 if (time_after(jiffies, timeout)) {
359 /* Command will fail; we'll pass error then */
360 dev_err(host->dev, "Busy; trying anyway\n");
361 break;
362 }
363 udelay(10);
364 }
365 }
366}
367
Will Newtonf95f3852011-01-02 01:11:59 -0500368static void dw_mci_start_command(struct dw_mci *host,
369 struct mmc_command *cmd, u32 cmd_flags)
370{
371 host->cmd = cmd;
Thomas Abraham4a909202012-09-17 18:16:35 +0000372 dev_vdbg(host->dev,
Will Newtonf95f3852011-01-02 01:11:59 -0500373 "start command: ARGR=0x%08x CMDR=0x%08x\n",
374 cmd->arg, cmd_flags);
375
376 mci_writel(host, CMDARG, cmd->arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800377 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -0800378 dw_mci_wait_while_busy(host, cmd_flags);
Will Newtonf95f3852011-01-02 01:11:59 -0500379
380 mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START);
381}
382
Seungwon Jeon90c21432013-08-31 00:14:05 +0900383static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data)
Will Newtonf95f3852011-01-02 01:11:59 -0500384{
Jaehoon Chunge13c3c02016-11-17 16:40:37 +0900385 struct mmc_command *stop = &host->stop_abort;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800386
Seungwon Jeon90c21432013-08-31 00:14:05 +0900387 dw_mci_start_command(host, stop, host->stop_cmdr);
Will Newtonf95f3852011-01-02 01:11:59 -0500388}
389
390/* DMA interface functions */
391static void dw_mci_stop_dma(struct dw_mci *host)
392{
James Hogan03e8cb52011-06-29 09:28:43 +0100393 if (host->using_dma) {
Will Newtonf95f3852011-01-02 01:11:59 -0500394 host->dma_ops->stop(host);
395 host->dma_ops->cleanup(host);
Will Newtonf95f3852011-01-02 01:11:59 -0500396 }
Seungwon Jeonaa50f252013-08-31 00:14:38 +0900397
398 /* Data transfer was stopped by the interrupt handler */
399 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -0500400}
401
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900402static int dw_mci_get_dma_dir(struct mmc_data *data)
403{
404 if (data->flags & MMC_DATA_WRITE)
405 return DMA_TO_DEVICE;
406 else
407 return DMA_FROM_DEVICE;
408}
409
Will Newtonf95f3852011-01-02 01:11:59 -0500410static void dw_mci_dma_cleanup(struct dw_mci *host)
411{
412 struct mmc_data *data = host->data;
413
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900414 if (data && data->host_cookie == COOKIE_MAPPED) {
415 dma_unmap_sg(host->dev,
416 data->sg,
417 data->sg_len,
418 dw_mci_get_dma_dir(data));
419 data->host_cookie = COOKIE_UNMAPPED;
420 }
Will Newtonf95f3852011-01-02 01:11:59 -0500421}
422
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900423static void dw_mci_idmac_reset(struct dw_mci *host)
424{
425 u32 bmod = mci_readl(host, BMOD);
426 /* Software reset of DMA */
427 bmod |= SDMMC_IDMAC_SWRESET;
428 mci_writel(host, BMOD, bmod);
429}
430
Will Newtonf95f3852011-01-02 01:11:59 -0500431static void dw_mci_idmac_stop_dma(struct dw_mci *host)
432{
433 u32 temp;
434
435 /* Disable and reset the IDMAC interface */
436 temp = mci_readl(host, CTRL);
437 temp &= ~SDMMC_CTRL_USE_IDMAC;
438 temp |= SDMMC_CTRL_DMA_RESET;
439 mci_writel(host, CTRL, temp);
440
441 /* Stop the IDMAC running */
442 temp = mci_readl(host, BMOD);
Jaehoon Chunga5289a42011-02-25 11:08:13 +0900443 temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB);
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900444 temp |= SDMMC_IDMAC_SWRESET;
Will Newtonf95f3852011-01-02 01:11:59 -0500445 mci_writel(host, BMOD, temp);
446}
447
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800448static void dw_mci_dmac_complete_dma(void *arg)
Will Newtonf95f3852011-01-02 01:11:59 -0500449{
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800450 struct dw_mci *host = arg;
Will Newtonf95f3852011-01-02 01:11:59 -0500451 struct mmc_data *data = host->data;
452
Thomas Abraham4a909202012-09-17 18:16:35 +0000453 dev_vdbg(host->dev, "DMA complete\n");
Will Newtonf95f3852011-01-02 01:11:59 -0500454
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800455 if ((host->use_dma == TRANS_MODE_EDMAC) &&
456 data && (data->flags & MMC_DATA_READ))
457 /* Invalidate cache after read */
458 dma_sync_sg_for_cpu(mmc_dev(host->cur_slot->mmc),
459 data->sg,
460 data->sg_len,
461 DMA_FROM_DEVICE);
462
Will Newtonf95f3852011-01-02 01:11:59 -0500463 host->dma_ops->cleanup(host);
464
465 /*
466 * If the card was removed, data will be NULL. No point in trying to
467 * send the stop command or waiting for NBUSY in this case.
468 */
469 if (data) {
470 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
471 tasklet_schedule(&host->tasklet);
472 }
473}
474
Will Newtonf95f3852011-01-02 01:11:59 -0500475static int dw_mci_idmac_init(struct dw_mci *host)
476{
Seungwon Jeon897b69e2012-09-19 13:58:31 +0800477 int i;
Will Newtonf95f3852011-01-02 01:11:59 -0500478
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000479 if (host->dma_64bit_address == 1) {
480 struct idmac_desc_64addr *p;
481 /* Number of descriptors in the ring buffer */
Shawn Lincc190d42016-09-02 12:14:39 +0800482 host->ring_size =
483 DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr);
Will Newtonf95f3852011-01-02 01:11:59 -0500484
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000485 /* Forward link the descriptor list */
486 for (i = 0, p = host->sg_cpu; i < host->ring_size - 1;
487 i++, p++) {
488 p->des6 = (host->sg_dma +
489 (sizeof(struct idmac_desc_64addr) *
490 (i + 1))) & 0xffffffff;
Will Newtonf95f3852011-01-02 01:11:59 -0500491
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000492 p->des7 = (u64)(host->sg_dma +
493 (sizeof(struct idmac_desc_64addr) *
494 (i + 1))) >> 32;
495 /* Initialize reserved and buffer size fields to "0" */
496 p->des1 = 0;
497 p->des2 = 0;
498 p->des3 = 0;
499 }
500
501 /* Set the last descriptor as the end-of-ring descriptor */
502 p->des6 = host->sg_dma & 0xffffffff;
503 p->des7 = (u64)host->sg_dma >> 32;
504 p->des0 = IDMAC_DES0_ER;
505
506 } else {
507 struct idmac_desc *p;
508 /* Number of descriptors in the ring buffer */
Shawn Lincc190d42016-09-02 12:14:39 +0800509 host->ring_size =
510 DESC_RING_BUF_SZ / sizeof(struct idmac_desc);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000511
512 /* Forward link the descriptor list */
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800513 for (i = 0, p = host->sg_cpu;
514 i < host->ring_size - 1;
515 i++, p++) {
Ben Dooks6687c422015-03-25 11:27:51 +0000516 p->des3 = cpu_to_le32(host->sg_dma +
517 (sizeof(struct idmac_desc) * (i + 1)));
Zhangfei Gao4b244722015-04-30 22:16:28 +0800518 p->des1 = 0;
519 }
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000520
521 /* Set the last descriptor as the end-of-ring descriptor */
Ben Dooks6687c422015-03-25 11:27:51 +0000522 p->des3 = cpu_to_le32(host->sg_dma);
523 p->des0 = cpu_to_le32(IDMAC_DES0_ER);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000524 }
Will Newtonf95f3852011-01-02 01:11:59 -0500525
Seungwon Jeon5ce9d962013-08-31 00:14:33 +0900526 dw_mci_idmac_reset(host);
Seungwon Jeon141a7122012-05-22 13:01:03 +0900527
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000528 if (host->dma_64bit_address == 1) {
529 /* Mask out interrupts - get Tx & Rx complete only */
530 mci_writel(host, IDSTS64, IDMAC_INT_CLR);
531 mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI |
532 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
Will Newtonf95f3852011-01-02 01:11:59 -0500533
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +0000534 /* Set the descriptor base address */
535 mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff);
536 mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32);
537
538 } else {
539 /* Mask out interrupts - get Tx & Rx complete only */
540 mci_writel(host, IDSTS, IDMAC_INT_CLR);
541 mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI |
542 SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI);
543
544 /* Set the descriptor base address */
545 mci_writel(host, DBADDR, host->sg_dma);
546 }
547
Will Newtonf95f3852011-01-02 01:11:59 -0500548 return 0;
549}
550
Shawn Lin3b2a0672016-09-02 12:14:37 +0800551static inline int dw_mci_prepare_desc64(struct dw_mci *host,
552 struct mmc_data *data,
553 unsigned int sg_len)
554{
555 unsigned int desc_len;
556 struct idmac_desc_64addr *desc_first, *desc_last, *desc;
557 unsigned long timeout;
558 int i;
559
560 desc_first = desc_last = desc = host->sg_cpu;
561
562 for (i = 0; i < sg_len; i++) {
563 unsigned int length = sg_dma_len(&data->sg[i]);
564
565 u64 mem_addr = sg_dma_address(&data->sg[i]);
566
567 for ( ; length ; desc++) {
568 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
569 length : DW_MCI_DESC_DATA_LENGTH;
570
571 length -= desc_len;
572
573 /*
574 * Wait for the former clear OWN bit operation
575 * of IDMAC to make sure that this descriptor
576 * isn't still owned by IDMAC as IDMAC's write
577 * ops and CPU's read ops are asynchronous.
578 */
579 timeout = jiffies + msecs_to_jiffies(100);
580 while (readl(&desc->des0) & IDMAC_DES0_OWN) {
581 if (time_after(jiffies, timeout))
582 goto err_own_bit;
583 udelay(10);
584 }
585
586 /*
587 * Set the OWN bit and disable interrupts
588 * for this descriptor
589 */
590 desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC |
591 IDMAC_DES0_CH;
592
593 /* Buffer length */
594 IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len);
595
596 /* Physical address to DMA to/from */
597 desc->des4 = mem_addr & 0xffffffff;
598 desc->des5 = mem_addr >> 32;
599
600 /* Update physical address for the next desc */
601 mem_addr += desc_len;
602
603 /* Save pointer to the last descriptor */
604 desc_last = desc;
605 }
606 }
607
608 /* Set first descriptor */
609 desc_first->des0 |= IDMAC_DES0_FD;
610
611 /* Set last descriptor */
612 desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC);
613 desc_last->des0 |= IDMAC_DES0_LD;
614
615 return 0;
616err_own_bit:
617 /* restore the descriptor chain as it's polluted */
Colin Ian King26be9d72016-11-16 18:55:01 +0000618 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
Shawn Lincc190d42016-09-02 12:14:39 +0800619 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
Shawn Lin3b2a0672016-09-02 12:14:37 +0800620 dw_mci_idmac_init(host);
621 return -EINVAL;
622}
623
624
625static inline int dw_mci_prepare_desc32(struct dw_mci *host,
626 struct mmc_data *data,
627 unsigned int sg_len)
628{
629 unsigned int desc_len;
630 struct idmac_desc *desc_first, *desc_last, *desc;
631 unsigned long timeout;
632 int i;
633
634 desc_first = desc_last = desc = host->sg_cpu;
635
636 for (i = 0; i < sg_len; i++) {
637 unsigned int length = sg_dma_len(&data->sg[i]);
638
639 u32 mem_addr = sg_dma_address(&data->sg[i]);
640
641 for ( ; length ; desc++) {
642 desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ?
643 length : DW_MCI_DESC_DATA_LENGTH;
644
645 length -= desc_len;
646
647 /*
648 * Wait for the former clear OWN bit operation
649 * of IDMAC to make sure that this descriptor
650 * isn't still owned by IDMAC as IDMAC's write
651 * ops and CPU's read ops are asynchronous.
652 */
653 timeout = jiffies + msecs_to_jiffies(100);
654 while (readl(&desc->des0) &
655 cpu_to_le32(IDMAC_DES0_OWN)) {
656 if (time_after(jiffies, timeout))
657 goto err_own_bit;
658 udelay(10);
659 }
660
661 /*
662 * Set the OWN bit and disable interrupts
663 * for this descriptor
664 */
665 desc->des0 = cpu_to_le32(IDMAC_DES0_OWN |
666 IDMAC_DES0_DIC |
667 IDMAC_DES0_CH);
668
669 /* Buffer length */
670 IDMAC_SET_BUFFER1_SIZE(desc, desc_len);
671
672 /* Physical address to DMA to/from */
673 desc->des2 = cpu_to_le32(mem_addr);
674
675 /* Update physical address for the next desc */
676 mem_addr += desc_len;
677
678 /* Save pointer to the last descriptor */
679 desc_last = desc;
680 }
681 }
682
683 /* Set first descriptor */
684 desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD);
685
686 /* Set last descriptor */
687 desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH |
688 IDMAC_DES0_DIC));
689 desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD);
690
691 return 0;
692err_own_bit:
693 /* restore the descriptor chain as it's polluted */
Colin Ian King26be9d72016-11-16 18:55:01 +0000694 dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n");
Shawn Lincc190d42016-09-02 12:14:39 +0800695 memset(host->sg_cpu, 0, DESC_RING_BUF_SZ);
Shawn Lin3b2a0672016-09-02 12:14:37 +0800696 dw_mci_idmac_init(host);
697 return -EINVAL;
698}
699
700static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len)
701{
702 u32 temp;
703 int ret;
704
705 if (host->dma_64bit_address == 1)
706 ret = dw_mci_prepare_desc64(host, host->data, sg_len);
707 else
708 ret = dw_mci_prepare_desc32(host, host->data, sg_len);
709
710 if (ret)
711 goto out;
712
713 /* drain writebuffer */
714 wmb();
715
716 /* Make sure to reset DMA in case we did PIO before this */
717 dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET);
718 dw_mci_idmac_reset(host);
719
720 /* Select IDMAC interface */
721 temp = mci_readl(host, CTRL);
722 temp |= SDMMC_CTRL_USE_IDMAC;
723 mci_writel(host, CTRL, temp);
724
725 /* drain writebuffer */
726 wmb();
727
728 /* Enable the IDMAC */
729 temp = mci_readl(host, BMOD);
730 temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB;
731 mci_writel(host, BMOD, temp);
732
733 /* Start it running */
734 mci_writel(host, PLDMND, 1);
735
736out:
737 return ret;
738}
739
Arnd Bergmann8e2b36e2012-11-06 22:55:31 +0100740static const struct dw_mci_dma_ops dw_mci_idmac_ops = {
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900741 .init = dw_mci_idmac_init,
742 .start = dw_mci_idmac_start_dma,
743 .stop = dw_mci_idmac_stop_dma,
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800744 .complete = dw_mci_dmac_complete_dma,
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900745 .cleanup = dw_mci_dma_cleanup,
746};
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800747
748static void dw_mci_edmac_stop_dma(struct dw_mci *host)
749{
Shawn Linab925a32016-03-09 10:34:46 +0800750 dmaengine_terminate_async(host->dms->ch);
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800751}
752
753static int dw_mci_edmac_start_dma(struct dw_mci *host,
754 unsigned int sg_len)
755{
756 struct dma_slave_config cfg;
757 struct dma_async_tx_descriptor *desc = NULL;
758 struct scatterlist *sgl = host->data->sg;
759 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
760 u32 sg_elems = host->data->sg_len;
761 u32 fifoth_val;
762 u32 fifo_offset = host->fifo_reg - host->regs;
763 int ret = 0;
764
765 /* Set external dma config: burst size, burst width */
Arnd Bergmann260b3162015-11-12 15:14:23 +0100766 cfg.dst_addr = host->phy_regs + fifo_offset;
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800767 cfg.src_addr = cfg.dst_addr;
768 cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
769 cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES;
770
771 /* Match burst msize with external dma config */
772 fifoth_val = mci_readl(host, FIFOTH);
773 cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7];
774 cfg.src_maxburst = cfg.dst_maxburst;
775
776 if (host->data->flags & MMC_DATA_WRITE)
777 cfg.direction = DMA_MEM_TO_DEV;
778 else
779 cfg.direction = DMA_DEV_TO_MEM;
780
781 ret = dmaengine_slave_config(host->dms->ch, &cfg);
782 if (ret) {
783 dev_err(host->dev, "Failed to config edmac.\n");
784 return -EBUSY;
785 }
786
787 desc = dmaengine_prep_slave_sg(host->dms->ch, sgl,
788 sg_len, cfg.direction,
789 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
790 if (!desc) {
791 dev_err(host->dev, "Can't prepare slave sg.\n");
792 return -EBUSY;
793 }
794
795 /* Set dw_mci_dmac_complete_dma as callback */
796 desc->callback = dw_mci_dmac_complete_dma;
797 desc->callback_param = (void *)host;
798 dmaengine_submit(desc);
799
800 /* Flush cache before write */
801 if (host->data->flags & MMC_DATA_WRITE)
802 dma_sync_sg_for_device(mmc_dev(host->cur_slot->mmc), sgl,
803 sg_elems, DMA_TO_DEVICE);
804
805 dma_async_issue_pending(host->dms->ch);
806
807 return 0;
808}
809
810static int dw_mci_edmac_init(struct dw_mci *host)
811{
812 /* Request external dma channel */
813 host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL);
814 if (!host->dms)
815 return -ENOMEM;
816
817 host->dms->ch = dma_request_slave_channel(host->dev, "rx-tx");
818 if (!host->dms->ch) {
Dan Carpenter4539d362015-10-22 22:53:46 +0300819 dev_err(host->dev, "Failed to get external DMA channel.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800820 kfree(host->dms);
821 host->dms = NULL;
822 return -ENXIO;
823 }
824
825 return 0;
826}
827
828static void dw_mci_edmac_exit(struct dw_mci *host)
829{
830 if (host->dms) {
831 if (host->dms->ch) {
832 dma_release_channel(host->dms->ch);
833 host->dms->ch = NULL;
834 }
835 kfree(host->dms);
836 host->dms = NULL;
837 }
838}
839
840static const struct dw_mci_dma_ops dw_mci_edmac_ops = {
841 .init = dw_mci_edmac_init,
842 .exit = dw_mci_edmac_exit,
843 .start = dw_mci_edmac_start_dma,
844 .stop = dw_mci_edmac_stop_dma,
845 .complete = dw_mci_dmac_complete_dma,
846 .cleanup = dw_mci_dma_cleanup,
847};
Seungwon Jeon885c3e82012-02-20 11:01:43 +0900848
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900849static int dw_mci_pre_dma_transfer(struct dw_mci *host,
850 struct mmc_data *data,
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900851 int cookie)
Will Newtonf95f3852011-01-02 01:11:59 -0500852{
853 struct scatterlist *sg;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900854 unsigned int i, sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500855
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900856 if (data->host_cookie == COOKIE_PRE_MAPPED)
857 return data->sg_len;
Will Newtonf95f3852011-01-02 01:11:59 -0500858
859 /*
860 * We don't do DMA on "complex" transfers, i.e. with
861 * non-word-aligned buffers or lengths. Also, we don't bother
862 * with all the DMA setup overhead for short transfers.
863 */
864 if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD)
865 return -EINVAL;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900866
Will Newtonf95f3852011-01-02 01:11:59 -0500867 if (data->blksz & 3)
868 return -EINVAL;
869
870 for_each_sg(data->sg, sg, data->sg_len, i) {
871 if (sg->offset & 3 || sg->length & 3)
872 return -EINVAL;
873 }
874
Thomas Abraham4a909202012-09-17 18:16:35 +0000875 sg_len = dma_map_sg(host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900876 data->sg,
877 data->sg_len,
878 dw_mci_get_dma_dir(data));
879 if (sg_len == 0)
880 return -EINVAL;
881
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900882 data->host_cookie = cookie;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900883
884 return sg_len;
885}
886
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900887static void dw_mci_pre_req(struct mmc_host *mmc,
Linus Walleijd3c6aac2016-11-23 11:02:24 +0100888 struct mmc_request *mrq)
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900889{
890 struct dw_mci_slot *slot = mmc_priv(mmc);
891 struct mmc_data *data = mrq->data;
892
893 if (!slot->host->use_dma || !data)
894 return;
895
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900896 /* This data might be unmapped at this time */
897 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900898
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900899 if (dw_mci_pre_dma_transfer(slot->host, mrq->data,
900 COOKIE_PRE_MAPPED) < 0)
901 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900902}
903
904static void dw_mci_post_req(struct mmc_host *mmc,
905 struct mmc_request *mrq,
906 int err)
907{
908 struct dw_mci_slot *slot = mmc_priv(mmc);
909 struct mmc_data *data = mrq->data;
910
911 if (!slot->host->use_dma || !data)
912 return;
913
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900914 if (data->host_cookie != COOKIE_UNMAPPED)
Thomas Abraham4a909202012-09-17 18:16:35 +0000915 dma_unmap_sg(slot->host->dev,
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900916 data->sg,
917 data->sg_len,
918 dw_mci_get_dma_dir(data));
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +0900919 data->host_cookie = COOKIE_UNMAPPED;
Seungwon Jeon9aa51402012-02-06 16:55:07 +0900920}
921
Seungwon Jeon52426892013-08-31 00:13:42 +0900922static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data)
923{
Seungwon Jeon52426892013-08-31 00:13:42 +0900924 unsigned int blksz = data->blksz;
925 const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256};
926 u32 fifo_width = 1 << host->data_shift;
927 u32 blksz_depth = blksz / fifo_width, fifoth_val;
928 u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers;
Shawn Lin0e3a22c2015-08-03 15:07:21 +0800929 int idx = ARRAY_SIZE(mszs) - 1;
Seungwon Jeon52426892013-08-31 00:13:42 +0900930
Shawn Lin3fc7eae2015-09-16 14:41:23 +0800931 /* pio should ship this scenario */
932 if (!host->use_dma)
933 return;
934
Seungwon Jeon52426892013-08-31 00:13:42 +0900935 tx_wmark = (host->fifo_depth) / 2;
936 tx_wmark_invers = host->fifo_depth - tx_wmark;
937
938 /*
939 * MSIZE is '1',
940 * if blksz is not a multiple of the FIFO width
941 */
Shawn Lin20753562016-09-21 10:40:25 +0800942 if (blksz % fifo_width)
Seungwon Jeon52426892013-08-31 00:13:42 +0900943 goto done;
Seungwon Jeon52426892013-08-31 00:13:42 +0900944
945 do {
946 if (!((blksz_depth % mszs[idx]) ||
947 (tx_wmark_invers % mszs[idx]))) {
948 msize = idx;
949 rx_wmark = mszs[idx] - 1;
950 break;
951 }
952 } while (--idx > 0);
953 /*
954 * If idx is '0', it won't be tried
955 * Thus, initial values are uesed
956 */
957done:
958 fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark);
959 mci_writel(host, FIFOTH, fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +0900960}
961
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900962static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data)
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900963{
964 unsigned int blksz = data->blksz;
965 u32 blksz_depth, fifo_depth;
966 u16 thld_size;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900967 u8 enable;
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900968
James Hogan66dfd102014-11-17 17:49:05 +0000969 /*
970 * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is
971 * in the FIFO region, so we really shouldn't access it).
972 */
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900973 if (host->verid < DW_MMC_240A ||
974 (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE))
James Hogan66dfd102014-11-17 17:49:05 +0000975 return;
976
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +0900977 /*
978 * Card write Threshold is introduced since 2.80a
979 * It's used when HS400 mode is enabled.
980 */
981 if (data->flags & MMC_DATA_WRITE &&
982 !(host->timing != MMC_TIMING_MMC_HS400))
983 return;
984
985 if (data->flags & MMC_DATA_WRITE)
986 enable = SDMMC_CARD_WR_THR_EN;
987 else
988 enable = SDMMC_CARD_RD_THR_EN;
989
Seungwon Jeonf1d27362013-08-31 00:13:55 +0900990 if (host->timing != MMC_TIMING_MMC_HS200 &&
991 host->timing != MMC_TIMING_UHS_SDR104)
992 goto disable;
993
994 blksz_depth = blksz / (1 << host->data_shift);
995 fifo_depth = host->fifo_depth;
996
997 if (blksz_depth > fifo_depth)
998 goto disable;
999
1000 /*
1001 * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz'
1002 * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz
1003 * Currently just choose blksz.
1004 */
1005 thld_size = blksz;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001006 mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable));
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001007 return;
1008
1009disable:
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001010 mci_writel(host, CDTHRCTL, 0);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001011}
1012
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001013static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data)
1014{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001015 unsigned long irqflags;
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001016 int sg_len;
1017 u32 temp;
1018
1019 host->using_dma = 0;
1020
1021 /* If we don't have a channel, we can't do DMA */
1022 if (!host->use_dma)
1023 return -ENODEV;
1024
Jaehoon Chunga4cc7eb2016-11-17 16:40:38 +09001025 sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Seungwon Jeona99aa9b2012-04-10 09:53:32 +09001026 if (sg_len < 0) {
1027 host->dma_ops->stop(host);
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001028 return sg_len;
Seungwon Jeona99aa9b2012-04-10 09:53:32 +09001029 }
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001030
James Hogan03e8cb52011-06-29 09:28:43 +01001031 host->using_dma = 1;
1032
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001033 if (host->use_dma == TRANS_MODE_IDMAC)
1034 dev_vdbg(host->dev,
1035 "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n",
1036 (unsigned long)host->sg_cpu,
1037 (unsigned long)host->sg_dma,
1038 sg_len);
Will Newtonf95f3852011-01-02 01:11:59 -05001039
Seungwon Jeon52426892013-08-31 00:13:42 +09001040 /*
1041 * Decide the MSIZE and RX/TX Watermark.
1042 * If current block size is same with previous size,
1043 * no need to update fifoth.
1044 */
1045 if (host->prev_blksz != data->blksz)
1046 dw_mci_adjust_fifoth(host, data);
1047
Will Newtonf95f3852011-01-02 01:11:59 -05001048 /* Enable the DMA interface */
1049 temp = mci_readl(host, CTRL);
1050 temp |= SDMMC_CTRL_DMA_ENABLE;
1051 mci_writel(host, CTRL, temp);
1052
1053 /* Disable RX/TX IRQs, let DMA handle it */
Doug Andersonf8c58c12014-12-02 15:42:47 -08001054 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001055 temp = mci_readl(host, INTMASK);
1056 temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR);
1057 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001058 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001059
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001060 if (host->dma_ops->start(host, sg_len)) {
Jaehoon Chung647f80a2016-11-21 10:51:48 +09001061 host->dma_ops->stop(host);
Shawn Lind12d0cb2016-09-02 12:14:38 +08001062 /* We can't do DMA, try PIO for this one */
1063 dev_dbg(host->dev,
1064 "%s: fall back to PIO mode for current transfer\n",
1065 __func__);
Shawn Lin3fc7eae2015-09-16 14:41:23 +08001066 return -ENODEV;
1067 }
Will Newtonf95f3852011-01-02 01:11:59 -05001068
1069 return 0;
1070}
1071
1072static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data)
1073{
Doug Andersonf8c58c12014-12-02 15:42:47 -08001074 unsigned long irqflags;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001075 int flags = SG_MITER_ATOMIC;
Will Newtonf95f3852011-01-02 01:11:59 -05001076 u32 temp;
1077
1078 data->error = -EINPROGRESS;
1079
1080 WARN_ON(host->data);
1081 host->sg = NULL;
1082 host->data = data;
1083
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001084 if (data->flags & MMC_DATA_READ)
James Hogan55c5efbc2011-06-29 09:29:58 +01001085 host->dir_status = DW_MCI_RECV_STATUS;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001086 else
James Hogan55c5efbc2011-06-29 09:29:58 +01001087 host->dir_status = DW_MCI_SEND_STATUS;
Jaehoon Chung7e4bf1b2016-06-21 14:35:38 +09001088
1089 dw_mci_ctrl_thld(host, data);
James Hogan55c5efbc2011-06-29 09:29:58 +01001090
Will Newtonf95f3852011-01-02 01:11:59 -05001091 if (dw_mci_submit_data_dma(host, data)) {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09001092 if (host->data->flags & MMC_DATA_READ)
1093 flags |= SG_MITER_TO_SG;
1094 else
1095 flags |= SG_MITER_FROM_SG;
1096
1097 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Will Newtonf95f3852011-01-02 01:11:59 -05001098 host->sg = data->sg;
James Hogan34b664a2011-06-24 13:57:56 +01001099 host->part_buf_start = 0;
1100 host->part_buf_count = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001101
James Hoganb40af3a2011-06-24 13:54:06 +01001102 mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001103
1104 spin_lock_irqsave(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001105 temp = mci_readl(host, INTMASK);
1106 temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR;
1107 mci_writel(host, INTMASK, temp);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001108 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Will Newtonf95f3852011-01-02 01:11:59 -05001109
1110 temp = mci_readl(host, CTRL);
1111 temp &= ~SDMMC_CTRL_DMA_ENABLE;
1112 mci_writel(host, CTRL, temp);
Seungwon Jeon52426892013-08-31 00:13:42 +09001113
1114 /*
Jun Nied6fced82017-01-11 15:37:26 +09001115 * Use the initial fifoth_val for PIO mode. If wm_algined
1116 * is set, we set watermark same as data size.
Seungwon Jeon52426892013-08-31 00:13:42 +09001117 * If next issued data may be transfered by DMA mode,
1118 * prev_blksz should be invalidated.
1119 */
Jun Nied6fced82017-01-11 15:37:26 +09001120 if (host->wm_aligned)
1121 dw_mci_adjust_fifoth(host, data);
1122 else
1123 mci_writel(host, FIFOTH, host->fifoth_val);
Seungwon Jeon52426892013-08-31 00:13:42 +09001124 host->prev_blksz = 0;
1125 } else {
1126 /*
1127 * Keep the current block size.
1128 * It will be used to decide whether to update
1129 * fifoth register next time.
1130 */
1131 host->prev_blksz = data->blksz;
Will Newtonf95f3852011-01-02 01:11:59 -05001132 }
1133}
1134
1135static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg)
1136{
1137 struct dw_mci *host = slot->host;
1138 unsigned long timeout = jiffies + msecs_to_jiffies(500);
1139 unsigned int cmd_status = 0;
1140
1141 mci_writel(host, CMDARG, arg);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001142 wmb(); /* drain writebuffer */
Doug Anderson0bdbd0e2015-02-20 12:31:56 -08001143 dw_mci_wait_while_busy(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001144 mci_writel(host, CMD, SDMMC_CMD_START | cmd);
1145
1146 while (time_before(jiffies, timeout)) {
1147 cmd_status = mci_readl(host, CMD);
1148 if (!(cmd_status & SDMMC_CMD_START))
1149 return;
1150 }
1151 dev_err(&slot->mmc->class_dev,
1152 "Timeout sending command (cmd %#x arg %#x status %#x)\n",
1153 cmd, arg, cmd_status);
1154}
1155
Abhilash Kesavanab269122012-11-19 10:26:21 +05301156static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit)
Will Newtonf95f3852011-01-02 01:11:59 -05001157{
1158 struct dw_mci *host = slot->host;
Doug Andersonfdf492a2013-08-31 00:11:43 +09001159 unsigned int clock = slot->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001160 u32 div;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001161 u32 clk_en_a;
Doug Anderson01730552014-08-22 19:17:51 +05301162 u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT;
1163
1164 /* We must continue to set bit 28 in CMD until the change is complete */
1165 if (host->state == STATE_WAITING_CMD11_DONE)
1166 sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH;
Will Newtonf95f3852011-01-02 01:11:59 -05001167
Doug Andersonfdf492a2013-08-31 00:11:43 +09001168 if (!clock) {
1169 mci_writel(host, CLKENA, 0);
Doug Anderson01730552014-08-22 19:17:51 +05301170 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Doug Andersonfdf492a2013-08-31 00:11:43 +09001171 } else if (clock != host->current_speed || force_clkinit) {
1172 div = host->bus_hz / clock;
1173 if (host->bus_hz % clock && host->bus_hz > clock)
Will Newtonf95f3852011-01-02 01:11:59 -05001174 /*
1175 * move the + 1 after the divide to prevent
1176 * over-clocking the card.
1177 */
Seungwon Jeone4199902012-05-22 13:01:21 +09001178 div += 1;
1179
Doug Andersonfdf492a2013-08-31 00:11:43 +09001180 div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001181
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001182 if ((clock != slot->__clk_old &&
1183 !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) ||
1184 force_clkinit) {
Shawn Lince69e2f2017-01-17 09:22:55 +08001185 /* Silent the verbose log if calling from PM context */
1186 if (!force_clkinit)
1187 dev_info(&slot->mmc->class_dev,
1188 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1189 slot->id, host->bus_hz, clock,
1190 div ? ((host->bus_hz / div) >> 1) :
1191 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -05001192
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001193 /*
1194 * If card is polling, display the message only
1195 * one time at boot time.
1196 */
1197 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1198 slot->mmc->f_min == clock)
1199 set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1200 }
1201
Will Newtonf95f3852011-01-02 01:11:59 -05001202 /* disable clock */
1203 mci_writel(host, CLKENA, 0);
1204 mci_writel(host, CLKSRC, 0);
1205
1206 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301207 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001208
1209 /* set clock to desired speed */
1210 mci_writel(host, CLKDIV, div);
1211
1212 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301213 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001214
Doug Anderson9623b5b2012-07-25 08:33:17 -07001215 /* enable clock; only low power if no SDIO */
1216 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Doug Andersonb24c8b22014-12-02 15:42:46 -08001217 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
Doug Anderson9623b5b2012-07-25 08:33:17 -07001218 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1219 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -05001220
1221 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301222 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Jaehoon Chung005d6752016-09-22 14:12:00 +09001223
1224 /* keep the last clock value that was requested from core */
1225 slot->__clk_old = clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001226 }
1227
Doug Andersonfdf492a2013-08-31 00:11:43 +09001228 host->current_speed = clock;
1229
Will Newtonf95f3852011-01-02 01:11:59 -05001230 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +09001231 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -05001232}
1233
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001234static void __dw_mci_start_request(struct dw_mci *host,
1235 struct dw_mci_slot *slot,
1236 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001237{
1238 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001239 struct mmc_data *data;
1240 u32 cmdflags;
1241
1242 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001243
Will Newtonf95f3852011-01-02 01:11:59 -05001244 host->cur_slot = slot;
1245 host->mrq = mrq;
1246
1247 host->pending_events = 0;
1248 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001249 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001250 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001251 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001252
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001253 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -05001254 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +09001255 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -05001256 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1257 mci_writel(host, BLKSIZ, data->blksz);
1258 }
1259
Will Newtonf95f3852011-01-02 01:11:59 -05001260 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1261
1262 /* this is the first command, send the initialization clock */
1263 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1264 cmdflags |= SDMMC_CMD_INIT;
1265
1266 if (data) {
1267 dw_mci_submit_data(host, data);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001268 wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05001269 }
1270
1271 dw_mci_start_command(host, cmd, cmdflags);
1272
Doug Anderson5c935162015-03-09 16:18:21 -07001273 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
Doug Anderson49ba0302015-04-03 11:13:07 -07001274 unsigned long irqflags;
1275
Doug Anderson5c935162015-03-09 16:18:21 -07001276 /*
Doug Anderson8886a6f2015-04-03 11:13:05 -07001277 * Databook says to fail after 2ms w/ no response, but evidence
1278 * shows that sometimes the cmd11 interrupt takes over 130ms.
1279 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1280 * is just about to roll over.
Doug Anderson49ba0302015-04-03 11:13:07 -07001281 *
1282 * We do this whole thing under spinlock and only if the
1283 * command hasn't already completed (indicating the the irq
1284 * already ran so we don't want the timeout).
Doug Anderson5c935162015-03-09 16:18:21 -07001285 */
Doug Anderson49ba0302015-04-03 11:13:07 -07001286 spin_lock_irqsave(&host->irq_lock, irqflags);
1287 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1288 mod_timer(&host->cmd11_timer,
1289 jiffies + msecs_to_jiffies(500) + 1);
1290 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Doug Anderson5c935162015-03-09 16:18:21 -07001291 }
1292
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001293 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001294}
1295
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001296static void dw_mci_start_request(struct dw_mci *host,
1297 struct dw_mci_slot *slot)
1298{
1299 struct mmc_request *mrq = slot->mrq;
1300 struct mmc_command *cmd;
1301
1302 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1303 __dw_mci_start_request(host, slot, cmd);
1304}
1305
James Hogan7456caa2011-06-24 13:55:10 +01001306/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -05001307static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1308 struct mmc_request *mrq)
1309{
1310 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1311 host->state);
1312
Will Newtonf95f3852011-01-02 01:11:59 -05001313 slot->mrq = mrq;
1314
Doug Anderson01730552014-08-22 19:17:51 +05301315 if (host->state == STATE_WAITING_CMD11_DONE) {
1316 dev_warn(&slot->mmc->class_dev,
1317 "Voltage change didn't complete\n");
1318 /*
1319 * this case isn't expected to happen, so we can
1320 * either crash here or just try to continue on
1321 * in the closest possible state
1322 */
1323 host->state = STATE_IDLE;
1324 }
1325
Will Newtonf95f3852011-01-02 01:11:59 -05001326 if (host->state == STATE_IDLE) {
1327 host->state = STATE_SENDING_CMD;
1328 dw_mci_start_request(host, slot);
1329 } else {
1330 list_add_tail(&slot->queue_node, &host->queue);
1331 }
Will Newtonf95f3852011-01-02 01:11:59 -05001332}
1333
1334static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1335{
1336 struct dw_mci_slot *slot = mmc_priv(mmc);
1337 struct dw_mci *host = slot->host;
1338
1339 WARN_ON(slot->mrq);
1340
James Hogan7456caa2011-06-24 13:55:10 +01001341 /*
1342 * The check for card presence and queueing of the request must be
1343 * atomic, otherwise the card could be removed in between and the
1344 * request wouldn't fail until another card was inserted.
1345 */
James Hogan7456caa2011-06-24 13:55:10 +01001346
Shawn Lin56f69112016-05-27 14:37:05 +08001347 if (!dw_mci_get_cd(mmc)) {
Will Newtonf95f3852011-01-02 01:11:59 -05001348 mrq->cmd->error = -ENOMEDIUM;
1349 mmc_request_done(mmc, mrq);
1350 return;
1351 }
1352
Shawn Lin56f69112016-05-27 14:37:05 +08001353 spin_lock_bh(&host->lock);
1354
Will Newtonf95f3852011-01-02 01:11:59 -05001355 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001356
1357 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001358}
1359
1360static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1361{
1362 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001363 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001364 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301365 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001366
Will Newtonf95f3852011-01-02 01:11:59 -05001367 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001368 case MMC_BUS_WIDTH_4:
1369 slot->ctype = SDMMC_CTYPE_4BIT;
1370 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001371 case MMC_BUS_WIDTH_8:
1372 slot->ctype = SDMMC_CTYPE_8BIT;
1373 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001374 default:
1375 /* set default 1 bit mode */
1376 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001377 }
1378
Seungwon Jeon3f514292012-01-02 16:00:02 +09001379 regs = mci_readl(slot->host, UHS_REG);
1380
Jaehoon Chung41babf72011-02-24 13:46:11 +09001381 /* DDR mode set */
Seungwon Jeon80113132015-01-29 08:11:57 +05301382 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
Jaehoon Chung7cc8d582015-10-21 19:49:42 +09001383 ios->timing == MMC_TIMING_UHS_DDR50 ||
Seungwon Jeon80113132015-01-29 08:11:57 +05301384 ios->timing == MMC_TIMING_MMC_HS400)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001385 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001386 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001387 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001388
1389 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001390 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001391
Doug Andersonfdf492a2013-08-31 00:11:43 +09001392 /*
1393 * Use mirror of ios->clock to prevent race with mmc
1394 * core ios update when finding the minimum.
1395 */
1396 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001397
James Hogancb27a842012-10-16 09:43:08 +01001398 if (drv_data && drv_data->set_ios)
1399 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001400
Will Newtonf95f3852011-01-02 01:11:59 -05001401 switch (ios->power_mode) {
1402 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301403 if (!IS_ERR(mmc->supply.vmmc)) {
1404 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1405 ios->vdd);
1406 if (ret) {
1407 dev_err(slot->host->dev,
1408 "failed to enable vmmc regulator\n");
1409 /*return, if failed turn on vmmc*/
1410 return;
1411 }
1412 }
Doug Anderson29d0d162015-01-13 15:58:44 -08001413 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1414 regs = mci_readl(slot->host, PWREN);
1415 regs |= (1 << slot->id);
1416 mci_writel(slot->host, PWREN, regs);
1417 break;
1418 case MMC_POWER_ON:
Doug Andersond1f1dd82015-02-20 10:57:19 -08001419 if (!slot->host->vqmmc_enabled) {
1420 if (!IS_ERR(mmc->supply.vqmmc)) {
1421 ret = regulator_enable(mmc->supply.vqmmc);
1422 if (ret < 0)
1423 dev_err(slot->host->dev,
1424 "failed to enable vqmmc\n");
1425 else
1426 slot->host->vqmmc_enabled = true;
1427
1428 } else {
1429 /* Keep track so we don't reset again */
Yuvaraj CD51da2242014-08-22 19:17:50 +05301430 slot->host->vqmmc_enabled = true;
Doug Andersond1f1dd82015-02-20 10:57:19 -08001431 }
1432
1433 /* Reset our state machine after powering on */
1434 dw_mci_ctrl_reset(slot->host,
1435 SDMMC_CTRL_ALL_RESET_FLAGS);
Yuvaraj CD51da2242014-08-22 19:17:50 +05301436 }
Doug Anderson655babb2015-02-20 10:57:18 -08001437
1438 /* Adjust clock / bus width after power is up */
1439 dw_mci_setup_bus(slot, false);
1440
James Hogane6f34e22013-03-12 10:43:32 +00001441 break;
1442 case MMC_POWER_OFF:
Doug Anderson655babb2015-02-20 10:57:18 -08001443 /* Turn clock off before power goes down */
1444 dw_mci_setup_bus(slot, false);
1445
Yuvaraj CD51da2242014-08-22 19:17:50 +05301446 if (!IS_ERR(mmc->supply.vmmc))
1447 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1448
Doug Andersond1f1dd82015-02-20 10:57:19 -08001449 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
Yuvaraj CD51da2242014-08-22 19:17:50 +05301450 regulator_disable(mmc->supply.vqmmc);
Doug Andersond1f1dd82015-02-20 10:57:19 -08001451 slot->host->vqmmc_enabled = false;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301452
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001453 regs = mci_readl(slot->host, PWREN);
1454 regs &= ~(1 << slot->id);
1455 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001456 break;
1457 default:
1458 break;
1459 }
Doug Anderson655babb2015-02-20 10:57:18 -08001460
1461 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1462 slot->host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001463}
1464
Doug Anderson01730552014-08-22 19:17:51 +05301465static int dw_mci_card_busy(struct mmc_host *mmc)
1466{
1467 struct dw_mci_slot *slot = mmc_priv(mmc);
1468 u32 status;
1469
1470 /*
1471 * Check the busy bit which is low when DAT[3:0]
1472 * (the data lines) are 0000
1473 */
1474 status = mci_readl(slot->host, STATUS);
1475
1476 return !!(status & SDMMC_STATUS_BUSY);
1477}
1478
1479static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1480{
1481 struct dw_mci_slot *slot = mmc_priv(mmc);
1482 struct dw_mci *host = slot->host;
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001483 const struct dw_mci_drv_data *drv_data = host->drv_data;
Doug Anderson01730552014-08-22 19:17:51 +05301484 u32 uhs;
1485 u32 v18 = SDMMC_UHS_18V << slot->id;
Doug Anderson01730552014-08-22 19:17:51 +05301486 int ret;
1487
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001488 if (drv_data && drv_data->switch_voltage)
1489 return drv_data->switch_voltage(mmc, ios);
1490
Doug Anderson01730552014-08-22 19:17:51 +05301491 /*
1492 * Program the voltage. Note that some instances of dw_mmc may use
1493 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1494 * does no harm but you need to set the regulator directly. Try both.
1495 */
1496 uhs = mci_readl(host, UHS_REG);
Douglas Andersone0848f52015-10-12 14:48:26 +02001497 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
Doug Anderson01730552014-08-22 19:17:51 +05301498 uhs &= ~v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001499 else
Doug Anderson01730552014-08-22 19:17:51 +05301500 uhs |= v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001501
Doug Anderson01730552014-08-22 19:17:51 +05301502 if (!IS_ERR(mmc->supply.vqmmc)) {
Douglas Andersone0848f52015-10-12 14:48:26 +02001503 ret = mmc_regulator_set_vqmmc(mmc, ios);
Doug Anderson01730552014-08-22 19:17:51 +05301504
1505 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001506 dev_dbg(&mmc->class_dev,
Douglas Andersone0848f52015-10-12 14:48:26 +02001507 "Regulator set error %d - %s V\n",
1508 ret, uhs & v18 ? "1.8" : "3.3");
Doug Anderson01730552014-08-22 19:17:51 +05301509 return ret;
1510 }
1511 }
1512 mci_writel(host, UHS_REG, uhs);
1513
1514 return 0;
1515}
1516
Will Newtonf95f3852011-01-02 01:11:59 -05001517static int dw_mci_get_ro(struct mmc_host *mmc)
1518{
1519 int read_only;
1520 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001521 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001522
1523 /* Use platform get_ro function, else try on board write protect */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001524 if (gpio_ro >= 0)
Jaehoon Chung9795a842014-03-03 11:36:46 +09001525 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001526 else
1527 read_only =
1528 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1529
1530 dev_dbg(&mmc->class_dev, "card is %s\n",
1531 read_only ? "read-only" : "read-write");
1532
1533 return read_only;
1534}
1535
1536static int dw_mci_get_cd(struct mmc_host *mmc)
1537{
1538 int present;
1539 struct dw_mci_slot *slot = mmc_priv(mmc);
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001540 struct dw_mci *host = slot->host;
1541 int gpio_cd = mmc_gpio_get_cd(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001542
1543 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungd10111c2016-11-24 20:04:41 +09001544 if (((mmc->caps & MMC_CAP_NEEDS_POLL)
1545 || !mmc_card_is_removable(mmc))) {
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001546 present = 1;
Jaehoon Chungd10111c2016-11-24 20:04:41 +09001547
1548 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1549 if (mmc->caps & MMC_CAP_NEEDS_POLL) {
1550 dev_info(&mmc->class_dev,
1551 "card is polling.\n");
1552 } else {
1553 dev_info(&mmc->class_dev,
1554 "card is non-removable.\n");
1555 }
1556 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1557 }
1558
1559 return present;
1560 } else if (gpio_cd >= 0)
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001561 present = gpio_cd;
Will Newtonf95f3852011-01-02 01:11:59 -05001562 else
1563 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1564 == 0 ? 1 : 0;
1565
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001566 spin_lock_bh(&host->lock);
Jaehoon Chung1f4d5072016-11-17 16:40:34 +09001567 if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
Will Newtonf95f3852011-01-02 01:11:59 -05001568 dev_dbg(&mmc->class_dev, "card is present\n");
Jaehoon Chung1c238a92016-11-24 20:04:40 +09001569 else if (!present &&
1570 !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
Will Newtonf95f3852011-01-02 01:11:59 -05001571 dev_dbg(&mmc->class_dev, "card is not present\n");
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001572 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001573
1574 return present;
1575}
1576
Shawn Lin935a6652016-01-14 09:08:02 +08001577static void dw_mci_hw_reset(struct mmc_host *mmc)
1578{
1579 struct dw_mci_slot *slot = mmc_priv(mmc);
1580 struct dw_mci *host = slot->host;
1581 int reset;
1582
1583 if (host->use_dma == TRANS_MODE_IDMAC)
1584 dw_mci_idmac_reset(host);
1585
1586 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1587 SDMMC_CTRL_FIFO_RESET))
1588 return;
1589
1590 /*
1591 * According to eMMC spec, card reset procedure:
1592 * tRstW >= 1us: RST_n pulse width
1593 * tRSCA >= 200us: RST_n to Command time
1594 * tRSTH >= 1us: RST_n high period
1595 */
1596 reset = mci_readl(host, RST_N);
1597 reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1598 mci_writel(host, RST_N, reset);
1599 usleep_range(1, 2);
1600 reset |= SDMMC_RST_HWACTIVE << slot->id;
1601 mci_writel(host, RST_N, reset);
1602 usleep_range(200, 300);
1603}
1604
Doug Andersonb24c8b22014-12-02 15:42:46 -08001605static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
Doug Anderson9623b5b2012-07-25 08:33:17 -07001606{
Doug Andersonb24c8b22014-12-02 15:42:46 -08001607 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson9623b5b2012-07-25 08:33:17 -07001608 struct dw_mci *host = slot->host;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001609
Doug Andersonb24c8b22014-12-02 15:42:46 -08001610 /*
1611 * Low power mode will stop the card clock when idle. According to the
1612 * description of the CLKENA register we should disable low power mode
1613 * for SDIO cards if we need SDIO interrupts to work.
1614 */
1615 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1616 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1617 u32 clk_en_a_old;
1618 u32 clk_en_a;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001619
Doug Andersonb24c8b22014-12-02 15:42:46 -08001620 clk_en_a_old = mci_readl(host, CLKENA);
1621
1622 if (card->type == MMC_TYPE_SDIO ||
1623 card->type == MMC_TYPE_SD_COMBO) {
1624 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1625 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1626 } else {
1627 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1628 clk_en_a = clk_en_a_old | clken_low_pwr;
1629 }
1630
1631 if (clk_en_a != clk_en_a_old) {
1632 mci_writel(host, CLKENA, clk_en_a);
1633 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1634 SDMMC_CMD_PRV_DAT_WAIT, 0);
1635 }
Doug Anderson9623b5b2012-07-25 08:33:17 -07001636 }
1637}
1638
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301639static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1640{
1641 struct dw_mci_slot *slot = mmc_priv(mmc);
1642 struct dw_mci *host = slot->host;
Doug Andersonf8c58c12014-12-02 15:42:47 -08001643 unsigned long irqflags;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301644 u32 int_mask;
1645
Doug Andersonf8c58c12014-12-02 15:42:47 -08001646 spin_lock_irqsave(&host->irq_lock, irqflags);
1647
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301648 /* Enable/disable Slot Specific SDIO interrupt */
1649 int_mask = mci_readl(host, INTMASK);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001650 if (enb)
1651 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1652 else
1653 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1654 mci_writel(host, INTMASK, int_mask);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001655
1656 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301657}
1658
Seungwon Jeon0976f162013-08-31 00:12:42 +09001659static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1660{
1661 struct dw_mci_slot *slot = mmc_priv(mmc);
1662 struct dw_mci *host = slot->host;
1663 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001664 int err = -EINVAL;
Seungwon Jeon0976f162013-08-31 00:12:42 +09001665
Seungwon Jeon0976f162013-08-31 00:12:42 +09001666 if (drv_data && drv_data->execute_tuning)
Chaotian Jing9979dbe2015-10-27 14:24:28 +08001667 err = drv_data->execute_tuning(slot, opcode);
Seungwon Jeon0976f162013-08-31 00:12:42 +09001668 return err;
1669}
1670
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001671static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1672 struct mmc_ios *ios)
Seungwon Jeon80113132015-01-29 08:11:57 +05301673{
1674 struct dw_mci_slot *slot = mmc_priv(mmc);
1675 struct dw_mci *host = slot->host;
1676 const struct dw_mci_drv_data *drv_data = host->drv_data;
1677
1678 if (drv_data && drv_data->prepare_hs400_tuning)
1679 return drv_data->prepare_hs400_tuning(host, ios);
1680
1681 return 0;
1682}
1683
Will Newtonf95f3852011-01-02 01:11:59 -05001684static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301685 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001686 .pre_req = dw_mci_pre_req,
1687 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301688 .set_ios = dw_mci_set_ios,
1689 .get_ro = dw_mci_get_ro,
1690 .get_cd = dw_mci_get_cd,
Shawn Lin935a6652016-01-14 09:08:02 +08001691 .hw_reset = dw_mci_hw_reset,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301692 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001693 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301694 .card_busy = dw_mci_card_busy,
1695 .start_signal_voltage_switch = dw_mci_switch_voltage,
Doug Andersonb24c8b22014-12-02 15:42:46 -08001696 .init_card = dw_mci_init_card,
Seungwon Jeon80113132015-01-29 08:11:57 +05301697 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
Will Newtonf95f3852011-01-02 01:11:59 -05001698};
1699
1700static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1701 __releases(&host->lock)
1702 __acquires(&host->lock)
1703{
1704 struct dw_mci_slot *slot;
1705 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1706
1707 WARN_ON(host->cmd || host->data);
1708
1709 host->cur_slot->mrq = NULL;
1710 host->mrq = NULL;
1711 if (!list_empty(&host->queue)) {
1712 slot = list_entry(host->queue.next,
1713 struct dw_mci_slot, queue_node);
1714 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001715 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001716 mmc_hostname(slot->mmc));
1717 host->state = STATE_SENDING_CMD;
1718 dw_mci_start_request(host, slot);
1719 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001720 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301721
1722 if (host->state == STATE_SENDING_CMD11)
1723 host->state = STATE_WAITING_CMD11_DONE;
1724 else
1725 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001726 }
1727
1728 spin_unlock(&host->lock);
1729 mmc_request_done(prev_mmc, mrq);
1730 spin_lock(&host->lock);
1731}
1732
Seungwon Jeone352c812013-08-31 00:14:17 +09001733static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001734{
1735 u32 status = host->cmd_status;
1736
1737 host->cmd_status = 0;
1738
1739 /* Read the response from the card (up to 16 bytes) */
1740 if (cmd->flags & MMC_RSP_PRESENT) {
1741 if (cmd->flags & MMC_RSP_136) {
1742 cmd->resp[3] = mci_readl(host, RESP0);
1743 cmd->resp[2] = mci_readl(host, RESP1);
1744 cmd->resp[1] = mci_readl(host, RESP2);
1745 cmd->resp[0] = mci_readl(host, RESP3);
1746 } else {
1747 cmd->resp[0] = mci_readl(host, RESP0);
1748 cmd->resp[1] = 0;
1749 cmd->resp[2] = 0;
1750 cmd->resp[3] = 0;
1751 }
1752 }
1753
1754 if (status & SDMMC_INT_RTO)
1755 cmd->error = -ETIMEDOUT;
1756 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1757 cmd->error = -EILSEQ;
1758 else if (status & SDMMC_INT_RESP_ERR)
1759 cmd->error = -EIO;
1760 else
1761 cmd->error = 0;
1762
Seungwon Jeone352c812013-08-31 00:14:17 +09001763 return cmd->error;
1764}
1765
1766static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1767{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001768 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001769
1770 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1771 if (status & SDMMC_INT_DRTO) {
1772 data->error = -ETIMEDOUT;
1773 } else if (status & SDMMC_INT_DCRC) {
1774 data->error = -EILSEQ;
1775 } else if (status & SDMMC_INT_EBE) {
1776 if (host->dir_status ==
1777 DW_MCI_SEND_STATUS) {
1778 /*
1779 * No data CRC status was returned.
1780 * The number of bytes transferred
1781 * will be exaggerated in PIO mode.
1782 */
1783 data->bytes_xfered = 0;
1784 data->error = -ETIMEDOUT;
1785 } else if (host->dir_status ==
1786 DW_MCI_RECV_STATUS) {
Shawn Line7a1dec2016-08-22 10:57:16 +08001787 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001788 }
1789 } else {
1790 /* SDMMC_INT_SBE is included */
Shawn Line7a1dec2016-08-22 10:57:16 +08001791 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001792 }
1793
Doug Andersone6cc0122014-04-22 16:51:21 -07001794 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001795
1796 /*
1797 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001798 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001799 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001800 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001801 } else {
1802 data->bytes_xfered = data->blocks * data->blksz;
1803 data->error = 0;
1804 }
1805
1806 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001807}
1808
Addy Ke57e10482015-08-11 01:27:18 +09001809static void dw_mci_set_drto(struct dw_mci *host)
1810{
1811 unsigned int drto_clks;
1812 unsigned int drto_ms;
1813
1814 drto_clks = mci_readl(host, TMOUT) >> 8;
1815 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1816
1817 /* add a bit spare time */
1818 drto_ms += 10;
1819
1820 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1821}
1822
Will Newtonf95f3852011-01-02 01:11:59 -05001823static void dw_mci_tasklet_func(unsigned long priv)
1824{
1825 struct dw_mci *host = (struct dw_mci *)priv;
1826 struct mmc_data *data;
1827 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001828 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001829 enum dw_mci_state state;
1830 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001831 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001832
1833 spin_lock(&host->lock);
1834
1835 state = host->state;
1836 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001837 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001838
1839 do {
1840 prev_state = state;
1841
1842 switch (state) {
1843 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301844 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001845 break;
1846
Doug Anderson01730552014-08-22 19:17:51 +05301847 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001848 case STATE_SENDING_CMD:
1849 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1850 &host->pending_events))
1851 break;
1852
1853 cmd = host->cmd;
1854 host->cmd = NULL;
1855 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001856 err = dw_mci_command_complete(host, cmd);
1857 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001858 prev_state = state = STATE_SENDING_CMD;
1859 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001860 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001861 goto unlock;
1862 }
1863
Seungwon Jeone352c812013-08-31 00:14:17 +09001864 if (cmd->data && err) {
Doug Anderson46d17952016-04-26 10:03:58 +02001865 /*
1866 * During UHS tuning sequence, sending the stop
1867 * command after the response CRC error would
1868 * throw the system into a confused state
1869 * causing all future tuning phases to report
1870 * failure.
1871 *
1872 * In such case controller will move into a data
1873 * transfer state after a response error or
1874 * response CRC error. Let's let that finish
1875 * before trying to send a stop, so we'll go to
1876 * STATE_SENDING_DATA.
1877 *
1878 * Although letting the data transfer take place
1879 * will waste a bit of time (we already know
1880 * the command was bad), it can't cause any
1881 * errors since it's possible it would have
1882 * taken place anyway if this tasklet got
1883 * delayed. Allowing the transfer to take place
1884 * avoids races and keeps things simple.
1885 */
1886 if ((err != -ETIMEDOUT) &&
1887 (cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
1888 state = STATE_SENDING_DATA;
1889 continue;
1890 }
1891
Seungwon Jeon71abb132013-08-31 00:13:59 +09001892 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001893 send_stop_abort(host, data);
1894 state = STATE_SENDING_STOP;
1895 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001896 }
1897
Seungwon Jeone352c812013-08-31 00:14:17 +09001898 if (!cmd->data || err) {
1899 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001900 goto unlock;
1901 }
1902
1903 prev_state = state = STATE_SENDING_DATA;
1904 /* fall through */
1905
1906 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001907 /*
1908 * We could get a data error and never a transfer
1909 * complete so we'd better check for it here.
1910 *
1911 * Note that we don't really care if we also got a
1912 * transfer complete; stopping the DMA and sending an
1913 * abort won't hurt.
1914 */
Will Newtonf95f3852011-01-02 01:11:59 -05001915 if (test_and_clear_bit(EVENT_DATA_ERROR,
1916 &host->pending_events)) {
1917 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001918 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08001919 SDMMC_INT_EBE)))
1920 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001921 state = STATE_DATA_ERROR;
1922 break;
1923 }
1924
1925 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001926 &host->pending_events)) {
1927 /*
1928 * If all data-related interrupts don't come
1929 * within the given time in reading data state.
1930 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09001931 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09001932 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001933 break;
Addy Ke57e10482015-08-11 01:27:18 +09001934 }
Will Newtonf95f3852011-01-02 01:11:59 -05001935
1936 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07001937
1938 /*
1939 * Handle an EVENT_DATA_ERROR that might have shown up
1940 * before the transfer completed. This might not have
1941 * been caught by the check above because the interrupt
1942 * could have gone off between the previous check and
1943 * the check for transfer complete.
1944 *
1945 * Technically this ought not be needed assuming we
1946 * get a DATA_COMPLETE eventually (we'll notice the
1947 * error and end the request), but it shouldn't hurt.
1948 *
1949 * This has the advantage of sending the stop command.
1950 */
1951 if (test_and_clear_bit(EVENT_DATA_ERROR,
1952 &host->pending_events)) {
1953 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001954 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08001955 SDMMC_INT_EBE)))
1956 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001957 state = STATE_DATA_ERROR;
1958 break;
1959 }
Will Newtonf95f3852011-01-02 01:11:59 -05001960 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07001961
Will Newtonf95f3852011-01-02 01:11:59 -05001962 /* fall through */
1963
1964 case STATE_DATA_BUSY:
1965 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001966 &host->pending_events)) {
1967 /*
1968 * If data error interrupt comes but data over
1969 * interrupt doesn't come within the given time.
1970 * in reading data state.
1971 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09001972 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09001973 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001974 break;
Addy Ke57e10482015-08-11 01:27:18 +09001975 }
Will Newtonf95f3852011-01-02 01:11:59 -05001976
1977 host->data = NULL;
1978 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001979 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001980
Seungwon Jeone352c812013-08-31 00:14:17 +09001981 if (!err) {
1982 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05301983 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09001984 data->stop->error = 0;
1985 dw_mci_request_end(host, mrq);
1986 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05001987 }
Will Newtonf95f3852011-01-02 01:11:59 -05001988
Seungwon Jeon90c21432013-08-31 00:14:05 +09001989 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09001990 if (data->stop)
1991 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001992 } else {
1993 /*
1994 * If we don't have a command complete now we'll
1995 * never get one since we just reset everything;
1996 * better end the request.
1997 *
1998 * If we do have a command complete we'll fall
1999 * through to the SENDING_STOP command and
2000 * everything will be peachy keen.
2001 */
2002 if (!test_bit(EVENT_CMD_COMPLETE,
2003 &host->pending_events)) {
2004 host->cmd = NULL;
2005 dw_mci_request_end(host, mrq);
2006 goto unlock;
2007 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09002008 }
Seungwon Jeone352c812013-08-31 00:14:17 +09002009
2010 /*
2011 * If err has non-zero,
2012 * stop-abort command has been already issued.
2013 */
2014 prev_state = state = STATE_SENDING_STOP;
2015
Will Newtonf95f3852011-01-02 01:11:59 -05002016 /* fall through */
2017
2018 case STATE_SENDING_STOP:
2019 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2020 &host->pending_events))
2021 break;
2022
Seungwon Jeon71abb132013-08-31 00:13:59 +09002023 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09002024 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07002025 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09002026
Will Newtonf95f3852011-01-02 01:11:59 -05002027 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09002028 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09002029
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002030 if (!mrq->sbc && mrq->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09002031 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09002032 else
2033 host->cmd_status = 0;
2034
Seungwon Jeone352c812013-08-31 00:14:17 +09002035 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05002036 goto unlock;
2037
2038 case STATE_DATA_ERROR:
2039 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2040 &host->pending_events))
2041 break;
2042
2043 state = STATE_DATA_BUSY;
2044 break;
2045 }
2046 } while (state != prev_state);
2047
2048 host->state = state;
2049unlock:
2050 spin_unlock(&host->lock);
2051
2052}
2053
James Hogan34b664a2011-06-24 13:57:56 +01002054/* push final bytes to part_buf, only use during push */
2055static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2056{
2057 memcpy((void *)&host->part_buf, buf, cnt);
2058 host->part_buf_count = cnt;
2059}
2060
2061/* append bytes to part_buf, only use during push */
2062static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2063{
2064 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2065 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2066 host->part_buf_count += cnt;
2067 return cnt;
2068}
2069
2070/* pull first bytes from part_buf, only use during pull */
2071static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2072{
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002073 cnt = min_t(int, cnt, host->part_buf_count);
James Hogan34b664a2011-06-24 13:57:56 +01002074 if (cnt) {
2075 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2076 cnt);
2077 host->part_buf_count -= cnt;
2078 host->part_buf_start += cnt;
2079 }
2080 return cnt;
2081}
2082
2083/* pull final bytes from the part_buf, assuming it's just been filled */
2084static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2085{
2086 memcpy(buf, &host->part_buf, cnt);
2087 host->part_buf_start = cnt;
2088 host->part_buf_count = (1 << host->data_shift) - cnt;
2089}
2090
Will Newtonf95f3852011-01-02 01:11:59 -05002091static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2092{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002093 struct mmc_data *data = host->data;
2094 int init_cnt = cnt;
2095
James Hogan34b664a2011-06-24 13:57:56 +01002096 /* try and push anything in the part_buf */
2097 if (unlikely(host->part_buf_count)) {
2098 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002099
James Hogan34b664a2011-06-24 13:57:56 +01002100 buf += len;
2101 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002102 if (host->part_buf_count == 2) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002103 mci_fifo_writew(host->fifo_reg, host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01002104 host->part_buf_count = 0;
2105 }
2106 }
2107#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2108 if (unlikely((unsigned long)buf & 0x1)) {
2109 while (cnt >= 2) {
2110 u16 aligned_buf[64];
2111 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2112 int items = len >> 1;
2113 int i;
2114 /* memcpy from input buffer into aligned buffer */
2115 memcpy(aligned_buf, buf, len);
2116 buf += len;
2117 cnt -= len;
2118 /* push data from aligned buffer into fifo */
2119 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002120 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002121 }
2122 } else
2123#endif
2124 {
2125 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002126
James Hogan34b664a2011-06-24 13:57:56 +01002127 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002128 mci_fifo_writew(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002129 buf = pdata;
2130 }
2131 /* put anything remaining in the part_buf */
2132 if (cnt) {
2133 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002134 /* Push data if we have reached the expected data length */
2135 if ((data->bytes_xfered + init_cnt) ==
2136 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002137 mci_fifo_writew(host->fifo_reg, host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05002138 }
2139}
2140
2141static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2142{
James Hogan34b664a2011-06-24 13:57:56 +01002143#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2144 if (unlikely((unsigned long)buf & 0x1)) {
2145 while (cnt >= 2) {
2146 /* pull data from fifo into aligned buffer */
2147 u16 aligned_buf[64];
2148 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2149 int items = len >> 1;
2150 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002151
James Hogan34b664a2011-06-24 13:57:56 +01002152 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002153 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002154 /* memcpy from aligned buffer into output buffer */
2155 memcpy(buf, aligned_buf, len);
2156 buf += len;
2157 cnt -= len;
2158 }
2159 } else
2160#endif
2161 {
2162 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002163
James Hogan34b664a2011-06-24 13:57:56 +01002164 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002165 *pdata++ = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002166 buf = pdata;
2167 }
2168 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002169 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002170 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002171 }
2172}
2173
2174static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2175{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002176 struct mmc_data *data = host->data;
2177 int init_cnt = cnt;
2178
James Hogan34b664a2011-06-24 13:57:56 +01002179 /* try and push anything in the part_buf */
2180 if (unlikely(host->part_buf_count)) {
2181 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002182
James Hogan34b664a2011-06-24 13:57:56 +01002183 buf += len;
2184 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002185 if (host->part_buf_count == 4) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002186 mci_fifo_writel(host->fifo_reg, host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01002187 host->part_buf_count = 0;
2188 }
2189 }
2190#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2191 if (unlikely((unsigned long)buf & 0x3)) {
2192 while (cnt >= 4) {
2193 u32 aligned_buf[32];
2194 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2195 int items = len >> 2;
2196 int i;
2197 /* memcpy from input buffer into aligned buffer */
2198 memcpy(aligned_buf, buf, len);
2199 buf += len;
2200 cnt -= len;
2201 /* push data from aligned buffer into fifo */
2202 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002203 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002204 }
2205 } else
2206#endif
2207 {
2208 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002209
James Hogan34b664a2011-06-24 13:57:56 +01002210 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002211 mci_fifo_writel(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002212 buf = pdata;
2213 }
2214 /* put anything remaining in the part_buf */
2215 if (cnt) {
2216 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002217 /* Push data if we have reached the expected data length */
2218 if ((data->bytes_xfered + init_cnt) ==
2219 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002220 mci_fifo_writel(host->fifo_reg, host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05002221 }
2222}
2223
2224static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2225{
James Hogan34b664a2011-06-24 13:57:56 +01002226#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2227 if (unlikely((unsigned long)buf & 0x3)) {
2228 while (cnt >= 4) {
2229 /* pull data from fifo into aligned buffer */
2230 u32 aligned_buf[32];
2231 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2232 int items = len >> 2;
2233 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002234
James Hogan34b664a2011-06-24 13:57:56 +01002235 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002236 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002237 /* memcpy from aligned buffer into output buffer */
2238 memcpy(buf, aligned_buf, len);
2239 buf += len;
2240 cnt -= len;
2241 }
2242 } else
2243#endif
2244 {
2245 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002246
James Hogan34b664a2011-06-24 13:57:56 +01002247 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002248 *pdata++ = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002249 buf = pdata;
2250 }
2251 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002252 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002253 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002254 }
2255}
2256
2257static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2258{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002259 struct mmc_data *data = host->data;
2260 int init_cnt = cnt;
2261
James Hogan34b664a2011-06-24 13:57:56 +01002262 /* try and push anything in the part_buf */
2263 if (unlikely(host->part_buf_count)) {
2264 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002265
James Hogan34b664a2011-06-24 13:57:56 +01002266 buf += len;
2267 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09002268
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002269 if (host->part_buf_count == 8) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002270 mci_fifo_writeq(host->fifo_reg, host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01002271 host->part_buf_count = 0;
2272 }
2273 }
2274#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2275 if (unlikely((unsigned long)buf & 0x7)) {
2276 while (cnt >= 8) {
2277 u64 aligned_buf[16];
2278 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2279 int items = len >> 3;
2280 int i;
2281 /* memcpy from input buffer into aligned buffer */
2282 memcpy(aligned_buf, buf, len);
2283 buf += len;
2284 cnt -= len;
2285 /* push data from aligned buffer into fifo */
2286 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002287 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002288 }
2289 } else
2290#endif
2291 {
2292 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002293
James Hogan34b664a2011-06-24 13:57:56 +01002294 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002295 mci_fifo_writeq(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002296 buf = pdata;
2297 }
2298 /* put anything remaining in the part_buf */
2299 if (cnt) {
2300 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002301 /* Push data if we have reached the expected data length */
2302 if ((data->bytes_xfered + init_cnt) ==
2303 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002304 mci_fifo_writeq(host->fifo_reg, host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05002305 }
2306}
2307
2308static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2309{
James Hogan34b664a2011-06-24 13:57:56 +01002310#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2311 if (unlikely((unsigned long)buf & 0x7)) {
2312 while (cnt >= 8) {
2313 /* pull data from fifo into aligned buffer */
2314 u64 aligned_buf[16];
2315 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2316 int items = len >> 3;
2317 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002318
James Hogan34b664a2011-06-24 13:57:56 +01002319 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002320 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2321
James Hogan34b664a2011-06-24 13:57:56 +01002322 /* memcpy from aligned buffer into output buffer */
2323 memcpy(buf, aligned_buf, len);
2324 buf += len;
2325 cnt -= len;
2326 }
2327 } else
2328#endif
2329 {
2330 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002331
James Hogan34b664a2011-06-24 13:57:56 +01002332 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002333 *pdata++ = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002334 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05002335 }
James Hogan34b664a2011-06-24 13:57:56 +01002336 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002337 host->part_buf = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002338 dw_mci_pull_final_bytes(host, buf, cnt);
2339 }
2340}
2341
2342static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2343{
2344 int len;
2345
2346 /* get remaining partial bytes */
2347 len = dw_mci_pull_part_bytes(host, buf, cnt);
2348 if (unlikely(len == cnt))
2349 return;
2350 buf += len;
2351 cnt -= len;
2352
2353 /* get the rest of the data */
2354 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002355}
2356
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002357static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05002358{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002359 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2360 void *buf;
2361 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002362 struct mmc_data *data = host->data;
2363 int shift = host->data_shift;
2364 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002365 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002366 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002367
2368 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002369 if (!sg_miter_next(sg_miter))
2370 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002371
Imre Deak4225fc82013-02-27 17:02:57 -08002372 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002373 buf = sg_miter->addr;
2374 remain = sg_miter->length;
2375 offset = 0;
2376
2377 do {
2378 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2379 << shift) + host->part_buf_count;
2380 len = min(remain, fcnt);
2381 if (!len)
2382 break;
2383 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002384 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002385 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002386 remain -= len;
2387 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002388
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002389 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002390 status = mci_readl(host, MINTSTS);
2391 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002392 /* if the RXDR is ready read again */
2393 } while ((status & SDMMC_INT_RXDR) ||
2394 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002395
2396 if (!remain) {
2397 if (!sg_miter_next(sg_miter))
2398 goto done;
2399 sg_miter->consumed = 0;
2400 }
2401 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002402 return;
2403
2404done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002405 sg_miter_stop(sg_miter);
2406 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002407 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002408 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2409}
2410
2411static void dw_mci_write_data_pio(struct dw_mci *host)
2412{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002413 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2414 void *buf;
2415 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002416 struct mmc_data *data = host->data;
2417 int shift = host->data_shift;
2418 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002419 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002420 unsigned int fifo_depth = host->fifo_depth;
2421 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002422
2423 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002424 if (!sg_miter_next(sg_miter))
2425 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002426
Imre Deak4225fc82013-02-27 17:02:57 -08002427 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002428 buf = sg_miter->addr;
2429 remain = sg_miter->length;
2430 offset = 0;
2431
2432 do {
2433 fcnt = ((fifo_depth -
2434 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2435 << shift) - host->part_buf_count;
2436 len = min(remain, fcnt);
2437 if (!len)
2438 break;
2439 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002440 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002441 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002442 remain -= len;
2443 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002444
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002445 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002446 status = mci_readl(host, MINTSTS);
2447 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002448 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002449
2450 if (!remain) {
2451 if (!sg_miter_next(sg_miter))
2452 goto done;
2453 sg_miter->consumed = 0;
2454 }
2455 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002456 return;
2457
2458done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002459 sg_miter_stop(sg_miter);
2460 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002461 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002462 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2463}
2464
2465static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2466{
2467 if (!host->cmd_status)
2468 host->cmd_status = status;
2469
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002470 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002471
2472 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2473 tasklet_schedule(&host->tasklet);
2474}
2475
Doug Anderson6130e7a2014-10-14 09:33:09 -07002476static void dw_mci_handle_cd(struct dw_mci *host)
2477{
2478 int i;
2479
2480 for (i = 0; i < host->num_slots; i++) {
2481 struct dw_mci_slot *slot = host->slot[i];
2482
2483 if (!slot)
2484 continue;
2485
2486 if (slot->mmc->ops->card_event)
2487 slot->mmc->ops->card_event(slot->mmc);
2488 mmc_detect_change(slot->mmc,
2489 msecs_to_jiffies(host->pdata->detect_delay_ms));
2490 }
2491}
2492
Will Newtonf95f3852011-01-02 01:11:59 -05002493static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2494{
2495 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002496 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302497 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05002498
Markos Chandras1fb5f682013-03-12 10:53:11 +00002499 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2500
2501 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302502 /* Check volt switch first, since it can look like an error */
2503 if ((host->state == STATE_SENDING_CMD11) &&
2504 (pending & SDMMC_INT_VOLT_SWITCH)) {
Doug Anderson49ba0302015-04-03 11:13:07 -07002505 unsigned long irqflags;
Doug Anderson5c935162015-03-09 16:18:21 -07002506
Doug Anderson01730552014-08-22 19:17:51 +05302507 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2508 pending &= ~SDMMC_INT_VOLT_SWITCH;
Doug Anderson49ba0302015-04-03 11:13:07 -07002509
2510 /*
2511 * Hold the lock; we know cmd11_timer can't be kicked
2512 * off after the lock is released, so safe to delete.
2513 */
2514 spin_lock_irqsave(&host->irq_lock, irqflags);
Doug Anderson01730552014-08-22 19:17:51 +05302515 dw_mci_cmd_interrupt(host, pending);
Doug Anderson49ba0302015-04-03 11:13:07 -07002516 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2517
2518 del_timer(&host->cmd11_timer);
Doug Anderson01730552014-08-22 19:17:51 +05302519 }
2520
Will Newtonf95f3852011-01-02 01:11:59 -05002521 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2522 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002523 host->cmd_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002524 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002525 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002526 }
2527
2528 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2529 /* if there is an error report DATA_ERROR */
2530 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002531 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002532 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002533 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002534 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002535 }
2536
2537 if (pending & SDMMC_INT_DATA_OVER) {
Jaehoon Chung16a34572016-06-21 14:35:37 +09002538 del_timer(&host->dto_timer);
Addy Ke57e10482015-08-11 01:27:18 +09002539
Will Newtonf95f3852011-01-02 01:11:59 -05002540 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2541 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002542 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002543 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002544 if (host->dir_status == DW_MCI_RECV_STATUS) {
2545 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002546 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002547 }
2548 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2549 tasklet_schedule(&host->tasklet);
2550 }
2551
2552 if (pending & SDMMC_INT_RXDR) {
2553 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002554 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002555 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002556 }
2557
2558 if (pending & SDMMC_INT_TXDR) {
2559 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002560 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002561 dw_mci_write_data_pio(host);
2562 }
2563
2564 if (pending & SDMMC_INT_CMD_DONE) {
2565 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002566 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002567 }
2568
2569 if (pending & SDMMC_INT_CD) {
2570 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002571 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002572 }
2573
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302574 /* Handle SDIO Interrupts */
2575 for (i = 0; i < host->num_slots; i++) {
2576 struct dw_mci_slot *slot = host->slot[i];
Doug Andersoned2540e2015-02-25 10:11:52 -08002577
2578 if (!slot)
2579 continue;
2580
Addy Ke76756232014-11-04 22:03:09 +08002581 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2582 mci_writel(host, RINTSTS,
2583 SDMMC_INT_SDIO(slot->sdio_id));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302584 mmc_signal_sdio_irq(slot->mmc);
2585 }
2586 }
2587
Markos Chandras1fb5f682013-03-12 10:53:11 +00002588 }
Will Newtonf95f3852011-01-02 01:11:59 -05002589
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002590 if (host->use_dma != TRANS_MODE_IDMAC)
2591 return IRQ_HANDLED;
2592
2593 /* Handle IDMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002594 if (host->dma_64bit_address == 1) {
2595 pending = mci_readl(host, IDSTS64);
2596 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2597 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2598 SDMMC_IDMAC_INT_RI);
2599 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002600 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2601 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002602 }
2603 } else {
2604 pending = mci_readl(host, IDSTS);
2605 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2606 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2607 SDMMC_IDMAC_INT_RI);
2608 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002609 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2610 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002611 }
Will Newtonf95f3852011-01-02 01:11:59 -05002612 }
Will Newtonf95f3852011-01-02 01:11:59 -05002613
2614 return IRQ_HANDLED;
2615}
2616
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002617static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002618{
2619 struct mmc_host *mmc;
2620 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002621 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002622 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002623 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002624
Thomas Abraham4a909202012-09-17 18:16:35 +00002625 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002626 if (!mmc)
2627 return -ENOMEM;
2628
2629 slot = mmc_priv(mmc);
2630 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002631 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002632 slot->mmc = mmc;
2633 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002634 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002635
2636 mmc->ops = &dw_mci_ops;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002637 if (of_property_read_u32_array(host->dev->of_node,
2638 "clock-freq-min-max", freq, 2)) {
2639 mmc->f_min = DW_MCI_FREQ_MIN;
2640 mmc->f_max = DW_MCI_FREQ_MAX;
2641 } else {
Jaehoon Chungb0230302016-11-17 16:40:40 +09002642 dev_info(host->dev,
2643 "'clock-freq-min-max' property was deprecated.\n");
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002644 mmc->f_min = freq[0];
2645 mmc->f_max = freq[1];
2646 }
Will Newtonf95f3852011-01-02 01:11:59 -05002647
Yuvaraj CD51da2242014-08-22 19:17:50 +05302648 /*if there are external regulators, get them*/
2649 ret = mmc_regulator_get_supply(mmc);
2650 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002651 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302652
2653 if (!mmc->ocr_avail)
2654 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002655
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002656 if (host->pdata->caps)
2657 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002658
Jaehoon Chung6024e162016-07-15 10:54:50 +09002659 /*
2660 * Support MMC_CAP_ERASE by default.
2661 * It needs to use trim/discard/erase commands.
2662 */
2663 mmc->caps |= MMC_CAP_ERASE;
2664
Abhilash Kesavanab269122012-11-19 10:26:21 +05302665 if (host->pdata->pm_caps)
2666 mmc->pm_caps = host->pdata->pm_caps;
2667
Thomas Abraham800d78b2012-09-17 18:16:42 +00002668 if (host->dev->of_node) {
2669 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2670 if (ctrl_id < 0)
2671 ctrl_id = 0;
2672 } else {
2673 ctrl_id = to_platform_device(host->dev)->id;
2674 }
James Hogancb27a842012-10-16 09:43:08 +01002675 if (drv_data && drv_data->caps)
2676 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002677
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002678 if (host->pdata->caps2)
2679 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002680
Doug Anderson3cf890f2014-08-25 11:19:04 -07002681 ret = mmc_of_parse(mmc);
2682 if (ret)
2683 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002684
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002685 /* Useful defaults if platform data is unset. */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002686 if (host->use_dma == TRANS_MODE_IDMAC) {
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002687 mmc->max_segs = host->ring_size;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002688 mmc->max_blk_size = 65535;
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002689 mmc->max_seg_size = 0x1000;
2690 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2691 mmc->max_blk_count = mmc->max_req_size / 512;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002692 } else if (host->use_dma == TRANS_MODE_EDMAC) {
2693 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002694 mmc->max_blk_size = 65535;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002695 mmc->max_blk_count = 65535;
2696 mmc->max_req_size =
2697 mmc->max_blk_size * mmc->max_blk_count;
2698 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002699 } else {
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002700 /* TRANS_MODE_PIO */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002701 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002702 mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002703 mmc->max_blk_count = 512;
2704 mmc->max_req_size = mmc->max_blk_size *
2705 mmc->max_blk_count;
2706 mmc->max_seg_size = mmc->max_req_size;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002707 }
Will Newtonf95f3852011-01-02 01:11:59 -05002708
Shawn Linc0834a52016-05-27 14:36:40 +08002709 dw_mci_get_cd(mmc);
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002710
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002711 ret = mmc_add_host(mmc);
2712 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002713 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002714
2715#if defined(CONFIG_DEBUG_FS)
2716 dw_mci_init_debugfs(slot);
2717#endif
2718
Will Newtonf95f3852011-01-02 01:11:59 -05002719 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002720
Doug Anderson3cf890f2014-08-25 11:19:04 -07002721err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002722 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302723 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002724}
2725
2726static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2727{
Will Newtonf95f3852011-01-02 01:11:59 -05002728 /* Debugfs stuff is cleaned up by mmc core */
2729 mmc_remove_host(slot->mmc);
2730 slot->host->slot[id] = NULL;
2731 mmc_free_host(slot->mmc);
2732}
2733
2734static void dw_mci_init_dma(struct dw_mci *host)
2735{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002736 int addr_config;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002737 struct device *dev = host->dev;
2738 struct device_node *np = dev->of_node;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002739
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002740 /*
2741 * Check tansfer mode from HCON[17:16]
2742 * Clear the ambiguous description of dw_mmc databook:
2743 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2744 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2745 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2746 * 2b'11: Non DW DMA Interface -> pio only
2747 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2748 * simpler request/acknowledge handshake mechanism and both of them
2749 * are regarded as external dma master for dw_mmc.
2750 */
2751 host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
2752 if (host->use_dma == DMA_INTERFACE_IDMA) {
2753 host->use_dma = TRANS_MODE_IDMAC;
2754 } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
2755 host->use_dma == DMA_INTERFACE_GDMA) {
2756 host->use_dma = TRANS_MODE_EDMAC;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002757 } else {
Will Newtonf95f3852011-01-02 01:11:59 -05002758 goto no_dma;
2759 }
2760
2761 /* Determine which DMA interface to use */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002762 if (host->use_dma == TRANS_MODE_IDMAC) {
2763 /*
2764 * Check ADDR_CONFIG bit in HCON to find
2765 * IDMAC address bus width
2766 */
Shawn Lin70692752015-09-16 14:41:37 +08002767 addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05002768
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002769 if (addr_config == 1) {
2770 /* host supports IDMAC in 64-bit address mode */
2771 host->dma_64bit_address = 1;
2772 dev_info(host->dev,
2773 "IDMAC supports 64-bit address mode.\n");
2774 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2775 dma_set_coherent_mask(host->dev,
2776 DMA_BIT_MASK(64));
2777 } else {
2778 /* host supports IDMAC in 32-bit address mode */
2779 host->dma_64bit_address = 0;
2780 dev_info(host->dev,
2781 "IDMAC supports 32-bit address mode.\n");
2782 }
2783
2784 /* Alloc memory for sg translation */
Shawn Lincc190d42016-09-02 12:14:39 +08002785 host->sg_cpu = dmam_alloc_coherent(host->dev,
2786 DESC_RING_BUF_SZ,
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002787 &host->sg_dma, GFP_KERNEL);
2788 if (!host->sg_cpu) {
2789 dev_err(host->dev,
2790 "%s: could not alloc DMA memory\n",
2791 __func__);
2792 goto no_dma;
2793 }
2794
2795 host->dma_ops = &dw_mci_idmac_ops;
2796 dev_info(host->dev, "Using internal DMA controller.\n");
2797 } else {
2798 /* TRANS_MODE_EDMAC: check dma bindings again */
2799 if ((of_property_count_strings(np, "dma-names") < 0) ||
2800 (!of_find_property(np, "dmas", NULL))) {
2801 goto no_dma;
2802 }
2803 host->dma_ops = &dw_mci_edmac_ops;
2804 dev_info(host->dev, "Using external DMA controller.\n");
2805 }
Will Newtonf95f3852011-01-02 01:11:59 -05002806
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002807 if (host->dma_ops->init && host->dma_ops->start &&
2808 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002809 if (host->dma_ops->init(host)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002810 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2811 __func__);
Will Newtonf95f3852011-01-02 01:11:59 -05002812 goto no_dma;
2813 }
2814 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002815 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002816 goto no_dma;
2817 }
2818
Will Newtonf95f3852011-01-02 01:11:59 -05002819 return;
2820
2821no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002822 dev_info(host->dev, "Using PIO mode.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002823 host->use_dma = TRANS_MODE_PIO;
Will Newtonf95f3852011-01-02 01:11:59 -05002824}
2825
Seungwon Jeon31bff452013-08-31 00:14:23 +09002826static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
Will Newtonf95f3852011-01-02 01:11:59 -05002827{
2828 unsigned long timeout = jiffies + msecs_to_jiffies(500);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002829 u32 ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05002830
Seungwon Jeon31bff452013-08-31 00:14:23 +09002831 ctrl = mci_readl(host, CTRL);
2832 ctrl |= reset;
2833 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05002834
2835 /* wait till resets clear */
2836 do {
2837 ctrl = mci_readl(host, CTRL);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002838 if (!(ctrl & reset))
Will Newtonf95f3852011-01-02 01:11:59 -05002839 return true;
2840 } while (time_before(jiffies, timeout));
2841
Seungwon Jeon31bff452013-08-31 00:14:23 +09002842 dev_err(host->dev,
2843 "Timeout resetting block (ctrl reset %#x)\n",
2844 ctrl & reset);
Will Newtonf95f3852011-01-02 01:11:59 -05002845
2846 return false;
2847}
2848
Sonny Rao3a33a942014-08-04 18:19:50 -07002849static bool dw_mci_reset(struct dw_mci *host)
Seungwon Jeon31bff452013-08-31 00:14:23 +09002850{
Sonny Rao3a33a942014-08-04 18:19:50 -07002851 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2852 bool ret = false;
2853
Seungwon Jeon31bff452013-08-31 00:14:23 +09002854 /*
2855 * Reseting generates a block interrupt, hence setting
2856 * the scatter-gather pointer to NULL.
2857 */
2858 if (host->sg) {
2859 sg_miter_stop(&host->sg_miter);
2860 host->sg = NULL;
2861 }
2862
Sonny Rao3a33a942014-08-04 18:19:50 -07002863 if (host->use_dma)
2864 flags |= SDMMC_CTRL_DMA_RESET;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002865
Sonny Rao3a33a942014-08-04 18:19:50 -07002866 if (dw_mci_ctrl_reset(host, flags)) {
2867 /*
2868 * In all cases we clear the RAWINTS register to clear any
2869 * interrupts.
2870 */
2871 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2872
2873 /* if using dma we wait for dma_req to clear */
2874 if (host->use_dma) {
2875 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2876 u32 status;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002877
Sonny Rao3a33a942014-08-04 18:19:50 -07002878 do {
2879 status = mci_readl(host, STATUS);
2880 if (!(status & SDMMC_STATUS_DMA_REQ))
2881 break;
2882 cpu_relax();
2883 } while (time_before(jiffies, timeout));
2884
2885 if (status & SDMMC_STATUS_DMA_REQ) {
2886 dev_err(host->dev,
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002887 "%s: Timeout waiting for dma_req to clear during reset\n",
2888 __func__);
Sonny Rao3a33a942014-08-04 18:19:50 -07002889 goto ciu_out;
2890 }
2891
2892 /* when using DMA next we reset the fifo again */
2893 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2894 goto ciu_out;
2895 }
2896 } else {
2897 /* if the controller reset bit did clear, then set clock regs */
2898 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002899 dev_err(host->dev,
2900 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
Sonny Rao3a33a942014-08-04 18:19:50 -07002901 __func__);
2902 goto ciu_out;
2903 }
2904 }
2905
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002906 if (host->use_dma == TRANS_MODE_IDMAC)
2907 /* It is also recommended that we reset and reprogram idmac */
2908 dw_mci_idmac_reset(host);
Sonny Rao3a33a942014-08-04 18:19:50 -07002909
2910 ret = true;
2911
2912ciu_out:
2913 /* After a CTRL reset we need to have CIU set clock registers */
2914 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2915
2916 return ret;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002917}
2918
Doug Anderson5c935162015-03-09 16:18:21 -07002919static void dw_mci_cmd11_timer(unsigned long arg)
2920{
2921 struct dw_mci *host = (struct dw_mci *)arg;
2922
Doug Andersonfd674192015-04-03 11:13:06 -07002923 if (host->state != STATE_SENDING_CMD11) {
2924 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2925 return;
2926 }
Doug Anderson5c935162015-03-09 16:18:21 -07002927
2928 host->cmd_status = SDMMC_INT_RTO;
2929 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2930 tasklet_schedule(&host->tasklet);
2931}
2932
Addy Ke57e10482015-08-11 01:27:18 +09002933static void dw_mci_dto_timer(unsigned long arg)
2934{
2935 struct dw_mci *host = (struct dw_mci *)arg;
2936
2937 switch (host->state) {
2938 case STATE_SENDING_DATA:
2939 case STATE_DATA_BUSY:
2940 /*
2941 * If DTO interrupt does NOT come in sending data state,
2942 * we should notify the driver to terminate current transfer
2943 * and report a data timeout to the core.
2944 */
2945 host->data_status = SDMMC_INT_DRTO;
2946 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2947 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2948 tasklet_schedule(&host->tasklet);
2949 break;
2950 default:
2951 break;
2952 }
2953}
2954
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002955#ifdef CONFIG_OF
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002956static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2957{
2958 struct dw_mci_board *pdata;
2959 struct device *dev = host->dev;
2960 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002961 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Line8cc37b2016-01-21 14:52:52 +08002962 int ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002963 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002964
2965 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +09002966 if (!pdata)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002967 return ERR_PTR(-ENOMEM);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002968
Guodong Xud6786fe2016-08-12 16:51:26 +08002969 /* find reset controller when exist */
Jaehoon Chung3a667e32016-10-31 11:49:42 +09002970 pdata->rstc = devm_reset_control_get_optional(dev, "reset");
Guodong Xud6786fe2016-08-12 16:51:26 +08002971 if (IS_ERR(pdata->rstc)) {
2972 if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
2973 return ERR_PTR(-EPROBE_DEFER);
2974 }
2975
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002976 /* find out number of slots supported */
Shawn Lin8a629d22016-02-02 14:11:25 +08002977 of_property_read_u32(np, "num-slots", &pdata->num_slots);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002978
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002979 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002980 dev_info(dev,
2981 "fifo-depth property not found, using value of FIFOTH register as default\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002982
2983 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2984
Jun Niea0361c12017-01-11 15:35:35 +09002985 of_property_read_u32(np, "data-addr", &host->data_addr_override);
2986
Jun Nied6fced82017-01-11 15:37:26 +09002987 if (of_get_property(np, "fifo-watermark-aligned", NULL))
2988 host->wm_aligned = true;
2989
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002990 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2991 pdata->bus_hz = clock_frequency;
2992
James Hogancb27a842012-10-16 09:43:08 +01002993 if (drv_data && drv_data->parse_dt) {
2994 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002995 if (ret)
2996 return ERR_PTR(ret);
2997 }
2998
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002999 return pdata;
3000}
3001
3002#else /* CONFIG_OF */
3003static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3004{
3005 return ERR_PTR(-EINVAL);
3006}
3007#endif /* CONFIG_OF */
3008
Doug Andersonfa0c3282015-02-25 10:11:51 -08003009static void dw_mci_enable_cd(struct dw_mci *host)
3010{
Doug Andersonfa0c3282015-02-25 10:11:51 -08003011 unsigned long irqflags;
3012 u32 temp;
3013 int i;
Shawn Line8cc37b2016-01-21 14:52:52 +08003014 struct dw_mci_slot *slot;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003015
Shawn Line8cc37b2016-01-21 14:52:52 +08003016 /*
3017 * No need for CD if all slots have a non-error GPIO
3018 * as well as broken card detection is found.
3019 */
Doug Andersonfa0c3282015-02-25 10:11:51 -08003020 for (i = 0; i < host->num_slots; i++) {
Shawn Line8cc37b2016-01-21 14:52:52 +08003021 slot = host->slot[i];
3022 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3023 return;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003024
Arnd Bergmann287980e2016-05-27 23:23:25 +02003025 if (mmc_gpio_get_cd(slot->mmc) < 0)
Doug Andersonfa0c3282015-02-25 10:11:51 -08003026 break;
3027 }
3028 if (i == host->num_slots)
3029 return;
3030
3031 spin_lock_irqsave(&host->irq_lock, irqflags);
3032 temp = mci_readl(host, INTMASK);
3033 temp |= SDMMC_INT_CD;
3034 mci_writel(host, INTMASK, temp);
3035 spin_unlock_irqrestore(&host->irq_lock, irqflags);
3036}
3037
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303038int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003039{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00003040 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303041 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003042 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003043 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003044
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003045 if (!host->pdata) {
3046 host->pdata = dw_mci_parse_dt(host);
Guodong Xud6786fe2016-08-12 16:51:26 +08003047 if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
3048 return -EPROBE_DEFER;
3049 } else if (IS_ERR(host->pdata)) {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003050 dev_err(host->dev, "platform data not available\n");
3051 return -EINVAL;
3052 }
Will Newtonf95f3852011-01-02 01:11:59 -05003053 }
3054
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003055 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003056 if (IS_ERR(host->biu_clk)) {
3057 dev_dbg(host->dev, "biu clock not available\n");
3058 } else {
3059 ret = clk_prepare_enable(host->biu_clk);
3060 if (ret) {
3061 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003062 return ret;
3063 }
Will Newtonf95f3852011-01-02 01:11:59 -05003064 }
3065
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003066 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003067 if (IS_ERR(host->ciu_clk)) {
3068 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003069 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003070 } else {
3071 ret = clk_prepare_enable(host->ciu_clk);
3072 if (ret) {
3073 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003074 goto err_clk_biu;
3075 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003076
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003077 if (host->pdata->bus_hz) {
3078 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
3079 if (ret)
3080 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003081 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003082 host->pdata->bus_hz);
3083 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003084 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003085 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003086
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003087 if (!host->bus_hz) {
3088 dev_err(host->dev,
3089 "Platform data must supply bus speed\n");
3090 ret = -ENODEV;
3091 goto err_clk_ciu;
3092 }
3093
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09003094 if (drv_data && drv_data->init) {
3095 ret = drv_data->init(host);
3096 if (ret) {
3097 dev_err(host->dev,
3098 "implementation specific init failed\n");
3099 goto err_clk_ciu;
3100 }
3101 }
3102
Guodong Xud6786fe2016-08-12 16:51:26 +08003103 if (!IS_ERR(host->pdata->rstc)) {
3104 reset_control_assert(host->pdata->rstc);
3105 usleep_range(10, 50);
3106 reset_control_deassert(host->pdata->rstc);
3107 }
3108
Doug Anderson5c935162015-03-09 16:18:21 -07003109 setup_timer(&host->cmd11_timer,
3110 dw_mci_cmd11_timer, (unsigned long)host);
3111
Jaehoon Chung16a34572016-06-21 14:35:37 +09003112 setup_timer(&host->dto_timer,
3113 dw_mci_dto_timer, (unsigned long)host);
Addy Ke57e10482015-08-11 01:27:18 +09003114
Will Newtonf95f3852011-01-02 01:11:59 -05003115 spin_lock_init(&host->lock);
Doug Andersonf8c58c12014-12-02 15:42:47 -08003116 spin_lock_init(&host->irq_lock);
Will Newtonf95f3852011-01-02 01:11:59 -05003117 INIT_LIST_HEAD(&host->queue);
3118
Will Newtonf95f3852011-01-02 01:11:59 -05003119 /*
3120 * Get the host data width - this assumes that HCON has been set with
3121 * the correct values.
3122 */
Shawn Lin70692752015-09-16 14:41:37 +08003123 i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05003124 if (!i) {
3125 host->push_data = dw_mci_push_data16;
3126 host->pull_data = dw_mci_pull_data16;
3127 width = 16;
3128 host->data_shift = 1;
3129 } else if (i == 2) {
3130 host->push_data = dw_mci_push_data64;
3131 host->pull_data = dw_mci_pull_data64;
3132 width = 64;
3133 host->data_shift = 3;
3134 } else {
3135 /* Check for a reserved value, and warn if it is */
3136 WARN((i != 1),
3137 "HCON reports a reserved host data width!\n"
3138 "Defaulting to 32-bit access.\n");
3139 host->push_data = dw_mci_push_data32;
3140 host->pull_data = dw_mci_pull_data32;
3141 width = 32;
3142 host->data_shift = 2;
3143 }
3144
3145 /* Reset all blocks */
Shawn Lin37444152016-01-22 15:43:12 +08003146 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3147 ret = -ENODEV;
3148 goto err_clk_ciu;
3149 }
Seungwon Jeon141a7122012-05-22 13:01:03 +09003150
3151 host->dma_ops = host->pdata->dma_ops;
3152 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05003153
3154 /* Clear the interrupts for the host controller */
3155 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3156 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3157
3158 /* Put in max timeout */
3159 mci_writel(host, TMOUT, 0xFFFFFFFF);
3160
3161 /*
3162 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3163 * Tx Mark = fifo_size / 2 DMA Size = 8
3164 */
James Hoganb86d8252011-06-24 13:57:18 +01003165 if (!host->pdata->fifo_depth) {
3166 /*
3167 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3168 * have been overwritten by the bootloader, just like we're
3169 * about to do, so if you know the value for your hardware, you
3170 * should put it in the platform data.
3171 */
3172 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00003173 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01003174 } else {
3175 fifo_size = host->pdata->fifo_depth;
3176 }
3177 host->fifo_depth = fifo_size;
Seungwon Jeon52426892013-08-31 00:13:42 +09003178 host->fifoth_val =
3179 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003180 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05003181
3182 /* disable clock to CIU */
3183 mci_writel(host, CLKENA, 0);
3184 mci_writel(host, CLKSRC, 0);
3185
James Hogan63008762013-03-12 10:43:54 +00003186 /*
3187 * In 2.40a spec, Data offset is changed.
3188 * Need to check the version-id and set data-offset for DATA register.
3189 */
3190 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3191 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3192
Jun Niea0361c12017-01-11 15:35:35 +09003193 if (host->data_addr_override)
3194 host->fifo_reg = host->regs + host->data_addr_override;
3195 else if (host->verid < DW_MMC_240A)
Ben Dooks76184ac2015-03-25 11:27:52 +00003196 host->fifo_reg = host->regs + DATA_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003197 else
Ben Dooks76184ac2015-03-25 11:27:52 +00003198 host->fifo_reg = host->regs + DATA_240A_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003199
Will Newtonf95f3852011-01-02 01:11:59 -05003200 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003201 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3202 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05003203 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07003204 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003205
Will Newtonf95f3852011-01-02 01:11:59 -05003206 if (host->pdata->num_slots)
3207 host->num_slots = host->pdata->num_slots;
3208 else
Shawn Lin8a629d22016-02-02 14:11:25 +08003209 host->num_slots = 1;
3210
3211 if (host->num_slots < 1 ||
3212 host->num_slots > SDMMC_GET_SLOT_NUM(mci_readl(host, HCON))) {
3213 dev_err(host->dev,
3214 "Platform data must supply correct num_slots.\n");
3215 ret = -ENODEV;
3216 goto err_clk_ciu;
3217 }
Will Newtonf95f3852011-01-02 01:11:59 -05003218
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303219 /*
Doug Andersonfa0c3282015-02-25 10:11:51 -08003220 * Enable interrupts for command done, data over, data empty,
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303221 * receive ready and error such as transmit, receive timeout, crc error
3222 */
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303223 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3224 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003225 DW_MCI_ERROR_FLAGS);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003226 /* Enable mci interrupt */
3227 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303228
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003229 dev_info(host->dev,
3230 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303231 host->irq, width, fifo_size);
3232
Will Newtonf95f3852011-01-02 01:11:59 -05003233 /* We need at least one slot to succeed */
3234 for (i = 0; i < host->num_slots; i++) {
3235 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003236 if (ret)
3237 dev_dbg(host->dev, "slot %d init failed\n", i);
3238 else
3239 init_slots++;
3240 }
3241
3242 if (init_slots) {
3243 dev_info(host->dev, "%d slots initialized\n", init_slots);
3244 } else {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003245 dev_dbg(host->dev,
3246 "attempted to initialize %d slots, but failed on all\n",
3247 host->num_slots);
Doug Anderson6130e7a2014-10-14 09:33:09 -07003248 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003249 }
3250
Doug Andersonb793f652015-03-11 15:15:14 -07003251 /* Now that slots are all setup, we can enable card detect */
3252 dw_mci_enable_cd(host);
3253
Will Newtonf95f3852011-01-02 01:11:59 -05003254 return 0;
3255
Will Newtonf95f3852011-01-02 01:11:59 -05003256err_dmaunmap:
3257 if (host->use_dma && host->dma_ops->exit)
3258 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003259
Guodong Xud6786fe2016-08-12 16:51:26 +08003260 if (!IS_ERR(host->pdata->rstc))
3261 reset_control_assert(host->pdata->rstc);
3262
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003263err_clk_ciu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003264 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003265
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003266err_clk_biu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003267 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003268
Will Newtonf95f3852011-01-02 01:11:59 -05003269 return ret;
3270}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303271EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05003272
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303273void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003274{
Will Newtonf95f3852011-01-02 01:11:59 -05003275 int i;
3276
Will Newtonf95f3852011-01-02 01:11:59 -05003277 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00003278 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05003279 if (host->slot[i])
3280 dw_mci_cleanup_slot(host->slot[i], i);
3281 }
3282
Prabu Thangamuthu048fd7e2015-05-28 12:21:06 +00003283 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3284 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3285
Will Newtonf95f3852011-01-02 01:11:59 -05003286 /* disable clock to CIU */
3287 mci_writel(host, CLKENA, 0);
3288 mci_writel(host, CLKSRC, 0);
3289
Will Newtonf95f3852011-01-02 01:11:59 -05003290 if (host->use_dma && host->dma_ops->exit)
3291 host->dma_ops->exit(host);
3292
Guodong Xud6786fe2016-08-12 16:51:26 +08003293 if (!IS_ERR(host->pdata->rstc))
3294 reset_control_assert(host->pdata->rstc);
3295
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003296 clk_disable_unprepare(host->ciu_clk);
3297 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05003298}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303299EXPORT_SYMBOL(dw_mci_remove);
3300
3301
Will Newtonf95f3852011-01-02 01:11:59 -05003302
Shawn Line9ed8832016-10-12 10:50:35 +08003303#ifdef CONFIG_PM
Shawn Lined24e1f2016-10-12 10:56:55 +08003304int dw_mci_runtime_suspend(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003305{
Shawn Lined24e1f2016-10-12 10:56:55 +08003306 struct dw_mci *host = dev_get_drvdata(dev);
3307
Shawn Lin3fc7eae2015-09-16 14:41:23 +08003308 if (host->use_dma && host->dma_ops->exit)
3309 host->dma_ops->exit(host);
3310
Shawn Lined24e1f2016-10-12 10:56:55 +08003311 clk_disable_unprepare(host->ciu_clk);
3312
3313 if (host->cur_slot &&
3314 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3315 !mmc_card_is_removable(host->cur_slot->mmc)))
3316 clk_disable_unprepare(host->biu_clk);
3317
Will Newtonf95f3852011-01-02 01:11:59 -05003318 return 0;
3319}
Shawn Lined24e1f2016-10-12 10:56:55 +08003320EXPORT_SYMBOL(dw_mci_runtime_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05003321
Shawn Lined24e1f2016-10-12 10:56:55 +08003322int dw_mci_runtime_resume(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003323{
Shawn Lined24e1f2016-10-12 10:56:55 +08003324 int i, ret = 0;
3325 struct dw_mci *host = dev_get_drvdata(dev);
Will Newtonf95f3852011-01-02 01:11:59 -05003326
Shawn Lined24e1f2016-10-12 10:56:55 +08003327 if (host->cur_slot &&
3328 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3329 !mmc_card_is_removable(host->cur_slot->mmc))) {
3330 ret = clk_prepare_enable(host->biu_clk);
3331 if (ret)
3332 return ret;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003333 }
3334
Shawn Lined24e1f2016-10-12 10:56:55 +08003335 ret = clk_prepare_enable(host->ciu_clk);
3336 if (ret)
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003337 goto err;
3338
3339 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3340 clk_disable_unprepare(host->ciu_clk);
3341 ret = -ENODEV;
3342 goto err;
3343 }
Shawn Lined24e1f2016-10-12 10:56:55 +08003344
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04003345 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09003346 host->dma_ops->init(host);
3347
Seungwon Jeon52426892013-08-31 00:13:42 +09003348 /*
3349 * Restore the initial value at FIFOTH register
3350 * And Invalidate the prev_blksz with zero
3351 */
Shawn Lined24e1f2016-10-12 10:56:55 +08003352 mci_writel(host, FIFOTH, host->fifoth_val);
3353 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003354
Doug Anderson2eb29442013-08-31 00:11:49 +09003355 /* Put in max timeout */
3356 mci_writel(host, TMOUT, 0xFFFFFFFF);
3357
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003358 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3359 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3360 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003361 DW_MCI_ERROR_FLAGS);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003362 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3363
Will Newtonf95f3852011-01-02 01:11:59 -05003364 for (i = 0; i < host->num_slots; i++) {
3365 struct dw_mci_slot *slot = host->slot[i];
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003366
Will Newtonf95f3852011-01-02 01:11:59 -05003367 if (!slot)
3368 continue;
Ziyuan Xue9748e02017-01-17 09:22:56 +08003369 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
Abhilash Kesavanab269122012-11-19 10:26:21 +05303370 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
Ziyuan Xue9748e02017-01-17 09:22:56 +08003371
3372 /* Force setup bus to guarantee available clock output */
3373 dw_mci_setup_bus(slot, true);
Will Newtonf95f3852011-01-02 01:11:59 -05003374 }
Doug Andersonfa0c3282015-02-25 10:11:51 -08003375
3376 /* Now that slots are all setup, we can enable card detect */
3377 dw_mci_enable_cd(host);
3378
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003379 return 0;
3380
3381err:
3382 if (host->cur_slot &&
3383 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3384 !mmc_card_is_removable(host->cur_slot->mmc)))
3385 clk_disable_unprepare(host->biu_clk);
3386
Shawn Lined24e1f2016-10-12 10:56:55 +08003387 return ret;
Shawn Line9ed8832016-10-12 10:50:35 +08003388}
3389EXPORT_SYMBOL(dw_mci_runtime_resume);
3390#endif /* CONFIG_PM */
Jaehoon Chung6fe88902011-12-08 19:23:03 +09003391
Will Newtonf95f3852011-01-02 01:11:59 -05003392static int __init dw_mci_init(void)
3393{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05303394 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303395 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003396}
3397
3398static void __exit dw_mci_exit(void)
3399{
Will Newtonf95f3852011-01-02 01:11:59 -05003400}
3401
3402module_init(dw_mci_init);
3403module_exit(dw_mci_exit);
3404
3405MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3406MODULE_AUTHOR("NXP Semiconductor VietNam");
3407MODULE_AUTHOR("Imagination Technologies Ltd");
3408MODULE_LICENSE("GPL v2");