blob: c04a5769e2ead5b436466f7a4aa4780ef98a010f [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) {
Jaehoon Chung005d6752016-09-22 14:12:00 +09001185 dev_info(&slot->mmc->class_dev,
1186 "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n",
1187 slot->id, host->bus_hz, clock,
1188 div ? ((host->bus_hz / div) >> 1) :
1189 host->bus_hz, div);
Will Newtonf95f3852011-01-02 01:11:59 -05001190
Jaehoon Chunge6cd7a82016-11-24 20:04:42 +09001191 /*
1192 * If card is polling, display the message only
1193 * one time at boot time.
1194 */
1195 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL &&
1196 slot->mmc->f_min == clock)
1197 set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags);
1198 }
1199
Will Newtonf95f3852011-01-02 01:11:59 -05001200 /* disable clock */
1201 mci_writel(host, CLKENA, 0);
1202 mci_writel(host, CLKSRC, 0);
1203
1204 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301205 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001206
1207 /* set clock to desired speed */
1208 mci_writel(host, CLKDIV, div);
1209
1210 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301211 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Will Newtonf95f3852011-01-02 01:11:59 -05001212
Doug Anderson9623b5b2012-07-25 08:33:17 -07001213 /* enable clock; only low power if no SDIO */
1214 clk_en_a = SDMMC_CLKEN_ENABLE << slot->id;
Doug Andersonb24c8b22014-12-02 15:42:46 -08001215 if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags))
Doug Anderson9623b5b2012-07-25 08:33:17 -07001216 clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id;
1217 mci_writel(host, CLKENA, clk_en_a);
Will Newtonf95f3852011-01-02 01:11:59 -05001218
1219 /* inform CIU */
Doug Anderson01730552014-08-22 19:17:51 +05301220 mci_send_cmd(slot, sdmmc_cmd_bits, 0);
Jaehoon Chung005d6752016-09-22 14:12:00 +09001221
1222 /* keep the last clock value that was requested from core */
1223 slot->__clk_old = clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001224 }
1225
Doug Andersonfdf492a2013-08-31 00:11:43 +09001226 host->current_speed = clock;
1227
Will Newtonf95f3852011-01-02 01:11:59 -05001228 /* Set the current slot bus width */
Seungwon Jeon1d56c452011-06-20 17:23:53 +09001229 mci_writel(host, CTYPE, (slot->ctype << slot->id));
Will Newtonf95f3852011-01-02 01:11:59 -05001230}
1231
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001232static void __dw_mci_start_request(struct dw_mci *host,
1233 struct dw_mci_slot *slot,
1234 struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001235{
1236 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001237 struct mmc_data *data;
1238 u32 cmdflags;
1239
1240 mrq = slot->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001241
Will Newtonf95f3852011-01-02 01:11:59 -05001242 host->cur_slot = slot;
1243 host->mrq = mrq;
1244
1245 host->pending_events = 0;
1246 host->completed_events = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001247 host->cmd_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001248 host->data_status = 0;
Seungwon Jeone352c812013-08-31 00:14:17 +09001249 host->dir_status = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05001250
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001251 data = cmd->data;
Will Newtonf95f3852011-01-02 01:11:59 -05001252 if (data) {
Jaehoon Chungf16afa82014-03-03 11:36:45 +09001253 mci_writel(host, TMOUT, 0xFFFFFFFF);
Will Newtonf95f3852011-01-02 01:11:59 -05001254 mci_writel(host, BYTCNT, data->blksz*data->blocks);
1255 mci_writel(host, BLKSIZ, data->blksz);
1256 }
1257
Will Newtonf95f3852011-01-02 01:11:59 -05001258 cmdflags = dw_mci_prepare_command(slot->mmc, cmd);
1259
1260 /* this is the first command, send the initialization clock */
1261 if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags))
1262 cmdflags |= SDMMC_CMD_INIT;
1263
1264 if (data) {
1265 dw_mci_submit_data(host, data);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001266 wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05001267 }
1268
1269 dw_mci_start_command(host, cmd, cmdflags);
1270
Doug Anderson5c935162015-03-09 16:18:21 -07001271 if (cmd->opcode == SD_SWITCH_VOLTAGE) {
Doug Anderson49ba0302015-04-03 11:13:07 -07001272 unsigned long irqflags;
1273
Doug Anderson5c935162015-03-09 16:18:21 -07001274 /*
Doug Anderson8886a6f2015-04-03 11:13:05 -07001275 * Databook says to fail after 2ms w/ no response, but evidence
1276 * shows that sometimes the cmd11 interrupt takes over 130ms.
1277 * We'll set to 500ms, plus an extra jiffy just in case jiffies
1278 * is just about to roll over.
Doug Anderson49ba0302015-04-03 11:13:07 -07001279 *
1280 * We do this whole thing under spinlock and only if the
1281 * command hasn't already completed (indicating the the irq
1282 * already ran so we don't want the timeout).
Doug Anderson5c935162015-03-09 16:18:21 -07001283 */
Doug Anderson49ba0302015-04-03 11:13:07 -07001284 spin_lock_irqsave(&host->irq_lock, irqflags);
1285 if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events))
1286 mod_timer(&host->cmd11_timer,
1287 jiffies + msecs_to_jiffies(500) + 1);
1288 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Doug Anderson5c935162015-03-09 16:18:21 -07001289 }
1290
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001291 host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd);
Will Newtonf95f3852011-01-02 01:11:59 -05001292}
1293
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001294static void dw_mci_start_request(struct dw_mci *host,
1295 struct dw_mci_slot *slot)
1296{
1297 struct mmc_request *mrq = slot->mrq;
1298 struct mmc_command *cmd;
1299
1300 cmd = mrq->sbc ? mrq->sbc : mrq->cmd;
1301 __dw_mci_start_request(host, slot, cmd);
1302}
1303
James Hogan7456caa2011-06-24 13:55:10 +01001304/* must be called with host->lock held */
Will Newtonf95f3852011-01-02 01:11:59 -05001305static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot,
1306 struct mmc_request *mrq)
1307{
1308 dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n",
1309 host->state);
1310
Will Newtonf95f3852011-01-02 01:11:59 -05001311 slot->mrq = mrq;
1312
Doug Anderson01730552014-08-22 19:17:51 +05301313 if (host->state == STATE_WAITING_CMD11_DONE) {
1314 dev_warn(&slot->mmc->class_dev,
1315 "Voltage change didn't complete\n");
1316 /*
1317 * this case isn't expected to happen, so we can
1318 * either crash here or just try to continue on
1319 * in the closest possible state
1320 */
1321 host->state = STATE_IDLE;
1322 }
1323
Will Newtonf95f3852011-01-02 01:11:59 -05001324 if (host->state == STATE_IDLE) {
1325 host->state = STATE_SENDING_CMD;
1326 dw_mci_start_request(host, slot);
1327 } else {
1328 list_add_tail(&slot->queue_node, &host->queue);
1329 }
Will Newtonf95f3852011-01-02 01:11:59 -05001330}
1331
1332static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1333{
1334 struct dw_mci_slot *slot = mmc_priv(mmc);
1335 struct dw_mci *host = slot->host;
1336
1337 WARN_ON(slot->mrq);
1338
James Hogan7456caa2011-06-24 13:55:10 +01001339 /*
1340 * The check for card presence and queueing of the request must be
1341 * atomic, otherwise the card could be removed in between and the
1342 * request wouldn't fail until another card was inserted.
1343 */
James Hogan7456caa2011-06-24 13:55:10 +01001344
Shawn Lin56f69112016-05-27 14:37:05 +08001345 if (!dw_mci_get_cd(mmc)) {
Will Newtonf95f3852011-01-02 01:11:59 -05001346 mrq->cmd->error = -ENOMEDIUM;
1347 mmc_request_done(mmc, mrq);
1348 return;
1349 }
1350
Shawn Lin56f69112016-05-27 14:37:05 +08001351 spin_lock_bh(&host->lock);
1352
Will Newtonf95f3852011-01-02 01:11:59 -05001353 dw_mci_queue_request(host, slot, mrq);
James Hogan7456caa2011-06-24 13:55:10 +01001354
1355 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001356}
1357
1358static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1359{
1360 struct dw_mci_slot *slot = mmc_priv(mmc);
Arnd Bergmanne95baf12012-11-08 14:26:11 +00001361 const struct dw_mci_drv_data *drv_data = slot->host->drv_data;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001362 u32 regs;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301363 int ret;
Will Newtonf95f3852011-01-02 01:11:59 -05001364
Will Newtonf95f3852011-01-02 01:11:59 -05001365 switch (ios->bus_width) {
Will Newtonf95f3852011-01-02 01:11:59 -05001366 case MMC_BUS_WIDTH_4:
1367 slot->ctype = SDMMC_CTYPE_4BIT;
1368 break;
Jaehoon Chungc9b2a062011-02-17 16:12:38 +09001369 case MMC_BUS_WIDTH_8:
1370 slot->ctype = SDMMC_CTYPE_8BIT;
1371 break;
Jaehoon Chungb2f7cb42012-11-08 17:35:31 +09001372 default:
1373 /* set default 1 bit mode */
1374 slot->ctype = SDMMC_CTYPE_1BIT;
Will Newtonf95f3852011-01-02 01:11:59 -05001375 }
1376
Seungwon Jeon3f514292012-01-02 16:00:02 +09001377 regs = mci_readl(slot->host, UHS_REG);
1378
Jaehoon Chung41babf72011-02-24 13:46:11 +09001379 /* DDR mode set */
Seungwon Jeon80113132015-01-29 08:11:57 +05301380 if (ios->timing == MMC_TIMING_MMC_DDR52 ||
Jaehoon Chung7cc8d582015-10-21 19:49:42 +09001381 ios->timing == MMC_TIMING_UHS_DDR50 ||
Seungwon Jeon80113132015-01-29 08:11:57 +05301382 ios->timing == MMC_TIMING_MMC_HS400)
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001383 regs |= ((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001384 else
Hyeonsu Kimc69042a2013-02-22 09:32:46 +09001385 regs &= ~((0x1 << slot->id) << 16);
Seungwon Jeon3f514292012-01-02 16:00:02 +09001386
1387 mci_writel(slot->host, UHS_REG, regs);
Seungwon Jeonf1d27362013-08-31 00:13:55 +09001388 slot->host->timing = ios->timing;
Jaehoon Chung41babf72011-02-24 13:46:11 +09001389
Doug Andersonfdf492a2013-08-31 00:11:43 +09001390 /*
1391 * Use mirror of ios->clock to prevent race with mmc
1392 * core ios update when finding the minimum.
1393 */
1394 slot->clock = ios->clock;
Will Newtonf95f3852011-01-02 01:11:59 -05001395
James Hogancb27a842012-10-16 09:43:08 +01001396 if (drv_data && drv_data->set_ios)
1397 drv_data->set_ios(slot->host, ios);
Thomas Abraham800d78b2012-09-17 18:16:42 +00001398
Will Newtonf95f3852011-01-02 01:11:59 -05001399 switch (ios->power_mode) {
1400 case MMC_POWER_UP:
Yuvaraj CD51da2242014-08-22 19:17:50 +05301401 if (!IS_ERR(mmc->supply.vmmc)) {
1402 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1403 ios->vdd);
1404 if (ret) {
1405 dev_err(slot->host->dev,
1406 "failed to enable vmmc regulator\n");
1407 /*return, if failed turn on vmmc*/
1408 return;
1409 }
1410 }
Doug Anderson29d0d162015-01-13 15:58:44 -08001411 set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags);
1412 regs = mci_readl(slot->host, PWREN);
1413 regs |= (1 << slot->id);
1414 mci_writel(slot->host, PWREN, regs);
1415 break;
1416 case MMC_POWER_ON:
Doug Andersond1f1dd82015-02-20 10:57:19 -08001417 if (!slot->host->vqmmc_enabled) {
1418 if (!IS_ERR(mmc->supply.vqmmc)) {
1419 ret = regulator_enable(mmc->supply.vqmmc);
1420 if (ret < 0)
1421 dev_err(slot->host->dev,
1422 "failed to enable vqmmc\n");
1423 else
1424 slot->host->vqmmc_enabled = true;
1425
1426 } else {
1427 /* Keep track so we don't reset again */
Yuvaraj CD51da2242014-08-22 19:17:50 +05301428 slot->host->vqmmc_enabled = true;
Doug Andersond1f1dd82015-02-20 10:57:19 -08001429 }
1430
1431 /* Reset our state machine after powering on */
1432 dw_mci_ctrl_reset(slot->host,
1433 SDMMC_CTRL_ALL_RESET_FLAGS);
Yuvaraj CD51da2242014-08-22 19:17:50 +05301434 }
Doug Anderson655babb2015-02-20 10:57:18 -08001435
1436 /* Adjust clock / bus width after power is up */
1437 dw_mci_setup_bus(slot, false);
1438
James Hogane6f34e22013-03-12 10:43:32 +00001439 break;
1440 case MMC_POWER_OFF:
Doug Anderson655babb2015-02-20 10:57:18 -08001441 /* Turn clock off before power goes down */
1442 dw_mci_setup_bus(slot, false);
1443
Yuvaraj CD51da2242014-08-22 19:17:50 +05301444 if (!IS_ERR(mmc->supply.vmmc))
1445 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1446
Doug Andersond1f1dd82015-02-20 10:57:19 -08001447 if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled)
Yuvaraj CD51da2242014-08-22 19:17:50 +05301448 regulator_disable(mmc->supply.vqmmc);
Doug Andersond1f1dd82015-02-20 10:57:19 -08001449 slot->host->vqmmc_enabled = false;
Yuvaraj CD51da2242014-08-22 19:17:50 +05301450
Jaehoon Chung4366dcc2013-03-26 21:36:14 +09001451 regs = mci_readl(slot->host, PWREN);
1452 regs &= ~(1 << slot->id);
1453 mci_writel(slot->host, PWREN, regs);
Will Newtonf95f3852011-01-02 01:11:59 -05001454 break;
1455 default:
1456 break;
1457 }
Doug Anderson655babb2015-02-20 10:57:18 -08001458
1459 if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0)
1460 slot->host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001461}
1462
Doug Anderson01730552014-08-22 19:17:51 +05301463static int dw_mci_card_busy(struct mmc_host *mmc)
1464{
1465 struct dw_mci_slot *slot = mmc_priv(mmc);
1466 u32 status;
1467
1468 /*
1469 * Check the busy bit which is low when DAT[3:0]
1470 * (the data lines) are 0000
1471 */
1472 status = mci_readl(slot->host, STATUS);
1473
1474 return !!(status & SDMMC_STATUS_BUSY);
1475}
1476
1477static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios)
1478{
1479 struct dw_mci_slot *slot = mmc_priv(mmc);
1480 struct dw_mci *host = slot->host;
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001481 const struct dw_mci_drv_data *drv_data = host->drv_data;
Doug Anderson01730552014-08-22 19:17:51 +05301482 u32 uhs;
1483 u32 v18 = SDMMC_UHS_18V << slot->id;
Doug Anderson01730552014-08-22 19:17:51 +05301484 int ret;
1485
Zhangfei Gao8f7849c2015-05-14 16:45:18 +08001486 if (drv_data && drv_data->switch_voltage)
1487 return drv_data->switch_voltage(mmc, ios);
1488
Doug Anderson01730552014-08-22 19:17:51 +05301489 /*
1490 * Program the voltage. Note that some instances of dw_mmc may use
1491 * the UHS_REG for this. For other instances (like exynos) the UHS_REG
1492 * does no harm but you need to set the regulator directly. Try both.
1493 */
1494 uhs = mci_readl(host, UHS_REG);
Douglas Andersone0848f52015-10-12 14:48:26 +02001495 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
Doug Anderson01730552014-08-22 19:17:51 +05301496 uhs &= ~v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001497 else
Doug Anderson01730552014-08-22 19:17:51 +05301498 uhs |= v18;
Douglas Andersone0848f52015-10-12 14:48:26 +02001499
Doug Anderson01730552014-08-22 19:17:51 +05301500 if (!IS_ERR(mmc->supply.vqmmc)) {
Douglas Andersone0848f52015-10-12 14:48:26 +02001501 ret = mmc_regulator_set_vqmmc(mmc, ios);
Doug Anderson01730552014-08-22 19:17:51 +05301502
1503 if (ret) {
Doug Andersonb19caf32014-10-10 21:16:16 -07001504 dev_dbg(&mmc->class_dev,
Douglas Andersone0848f52015-10-12 14:48:26 +02001505 "Regulator set error %d - %s V\n",
1506 ret, uhs & v18 ? "1.8" : "3.3");
Doug Anderson01730552014-08-22 19:17:51 +05301507 return ret;
1508 }
1509 }
1510 mci_writel(host, UHS_REG, uhs);
1511
1512 return 0;
1513}
1514
Will Newtonf95f3852011-01-02 01:11:59 -05001515static int dw_mci_get_ro(struct mmc_host *mmc)
1516{
1517 int read_only;
1518 struct dw_mci_slot *slot = mmc_priv(mmc);
Jaehoon Chung9795a842014-03-03 11:36:46 +09001519 int gpio_ro = mmc_gpio_get_ro(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001520
1521 /* Use platform get_ro function, else try on board write protect */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001522 if (gpio_ro >= 0)
Jaehoon Chung9795a842014-03-03 11:36:46 +09001523 read_only = gpio_ro;
Will Newtonf95f3852011-01-02 01:11:59 -05001524 else
1525 read_only =
1526 mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0;
1527
1528 dev_dbg(&mmc->class_dev, "card is %s\n",
1529 read_only ? "read-only" : "read-write");
1530
1531 return read_only;
1532}
1533
1534static int dw_mci_get_cd(struct mmc_host *mmc)
1535{
1536 int present;
1537 struct dw_mci_slot *slot = mmc_priv(mmc);
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001538 struct dw_mci *host = slot->host;
1539 int gpio_cd = mmc_gpio_get_cd(mmc);
Will Newtonf95f3852011-01-02 01:11:59 -05001540
1541 /* Use platform get_cd function, else try onboard card detect */
Jaehoon Chungd10111c2016-11-24 20:04:41 +09001542 if (((mmc->caps & MMC_CAP_NEEDS_POLL)
1543 || !mmc_card_is_removable(mmc))) {
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09001544 present = 1;
Jaehoon Chungd10111c2016-11-24 20:04:41 +09001545
1546 if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) {
1547 if (mmc->caps & MMC_CAP_NEEDS_POLL) {
1548 dev_info(&mmc->class_dev,
1549 "card is polling.\n");
1550 } else {
1551 dev_info(&mmc->class_dev,
1552 "card is non-removable.\n");
1553 }
1554 set_bit(DW_MMC_CARD_PRESENT, &slot->flags);
1555 }
1556
1557 return present;
1558 } else if (gpio_cd >= 0)
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001559 present = gpio_cd;
Will Newtonf95f3852011-01-02 01:11:59 -05001560 else
1561 present = (mci_readl(slot->host, CDETECT) & (1 << slot->id))
1562 == 0 ? 1 : 0;
1563
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001564 spin_lock_bh(&host->lock);
Jaehoon Chung1f4d5072016-11-17 16:40:34 +09001565 if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags))
Will Newtonf95f3852011-01-02 01:11:59 -05001566 dev_dbg(&mmc->class_dev, "card is present\n");
Jaehoon Chung1c238a92016-11-24 20:04:40 +09001567 else if (!present &&
1568 !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags))
Will Newtonf95f3852011-01-02 01:11:59 -05001569 dev_dbg(&mmc->class_dev, "card is not present\n");
Zhangfei Gao7cf347b2014-01-16 20:48:47 +08001570 spin_unlock_bh(&host->lock);
Will Newtonf95f3852011-01-02 01:11:59 -05001571
1572 return present;
1573}
1574
Shawn Lin935a6652016-01-14 09:08:02 +08001575static void dw_mci_hw_reset(struct mmc_host *mmc)
1576{
1577 struct dw_mci_slot *slot = mmc_priv(mmc);
1578 struct dw_mci *host = slot->host;
1579 int reset;
1580
1581 if (host->use_dma == TRANS_MODE_IDMAC)
1582 dw_mci_idmac_reset(host);
1583
1584 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET |
1585 SDMMC_CTRL_FIFO_RESET))
1586 return;
1587
1588 /*
1589 * According to eMMC spec, card reset procedure:
1590 * tRstW >= 1us: RST_n pulse width
1591 * tRSCA >= 200us: RST_n to Command time
1592 * tRSTH >= 1us: RST_n high period
1593 */
1594 reset = mci_readl(host, RST_N);
1595 reset &= ~(SDMMC_RST_HWACTIVE << slot->id);
1596 mci_writel(host, RST_N, reset);
1597 usleep_range(1, 2);
1598 reset |= SDMMC_RST_HWACTIVE << slot->id;
1599 mci_writel(host, RST_N, reset);
1600 usleep_range(200, 300);
1601}
1602
Doug Andersonb24c8b22014-12-02 15:42:46 -08001603static void dw_mci_init_card(struct mmc_host *mmc, struct mmc_card *card)
Doug Anderson9623b5b2012-07-25 08:33:17 -07001604{
Doug Andersonb24c8b22014-12-02 15:42:46 -08001605 struct dw_mci_slot *slot = mmc_priv(mmc);
Doug Anderson9623b5b2012-07-25 08:33:17 -07001606 struct dw_mci *host = slot->host;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001607
Doug Andersonb24c8b22014-12-02 15:42:46 -08001608 /*
1609 * Low power mode will stop the card clock when idle. According to the
1610 * description of the CLKENA register we should disable low power mode
1611 * for SDIO cards if we need SDIO interrupts to work.
1612 */
1613 if (mmc->caps & MMC_CAP_SDIO_IRQ) {
1614 const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id;
1615 u32 clk_en_a_old;
1616 u32 clk_en_a;
Doug Anderson9623b5b2012-07-25 08:33:17 -07001617
Doug Andersonb24c8b22014-12-02 15:42:46 -08001618 clk_en_a_old = mci_readl(host, CLKENA);
1619
1620 if (card->type == MMC_TYPE_SDIO ||
1621 card->type == MMC_TYPE_SD_COMBO) {
1622 set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1623 clk_en_a = clk_en_a_old & ~clken_low_pwr;
1624 } else {
1625 clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags);
1626 clk_en_a = clk_en_a_old | clken_low_pwr;
1627 }
1628
1629 if (clk_en_a != clk_en_a_old) {
1630 mci_writel(host, CLKENA, clk_en_a);
1631 mci_send_cmd(slot, SDMMC_CMD_UPD_CLK |
1632 SDMMC_CMD_PRV_DAT_WAIT, 0);
1633 }
Doug Anderson9623b5b2012-07-25 08:33:17 -07001634 }
1635}
1636
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301637static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb)
1638{
1639 struct dw_mci_slot *slot = mmc_priv(mmc);
1640 struct dw_mci *host = slot->host;
Doug Andersonf8c58c12014-12-02 15:42:47 -08001641 unsigned long irqflags;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301642 u32 int_mask;
1643
Doug Andersonf8c58c12014-12-02 15:42:47 -08001644 spin_lock_irqsave(&host->irq_lock, irqflags);
1645
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301646 /* Enable/disable Slot Specific SDIO interrupt */
1647 int_mask = mci_readl(host, INTMASK);
Doug Andersonb24c8b22014-12-02 15:42:46 -08001648 if (enb)
1649 int_mask |= SDMMC_INT_SDIO(slot->sdio_id);
1650 else
1651 int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id);
1652 mci_writel(host, INTMASK, int_mask);
Doug Andersonf8c58c12014-12-02 15:42:47 -08001653
1654 spin_unlock_irqrestore(&host->irq_lock, irqflags);
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301655}
1656
Seungwon Jeon0976f162013-08-31 00:12:42 +09001657static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode)
1658{
1659 struct dw_mci_slot *slot = mmc_priv(mmc);
1660 struct dw_mci *host = slot->host;
1661 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001662 int err = -EINVAL;
Seungwon Jeon0976f162013-08-31 00:12:42 +09001663
Seungwon Jeon0976f162013-08-31 00:12:42 +09001664 if (drv_data && drv_data->execute_tuning)
Chaotian Jing9979dbe2015-10-27 14:24:28 +08001665 err = drv_data->execute_tuning(slot, opcode);
Seungwon Jeon0976f162013-08-31 00:12:42 +09001666 return err;
1667}
1668
Shawn Lin0e3a22c2015-08-03 15:07:21 +08001669static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc,
1670 struct mmc_ios *ios)
Seungwon Jeon80113132015-01-29 08:11:57 +05301671{
1672 struct dw_mci_slot *slot = mmc_priv(mmc);
1673 struct dw_mci *host = slot->host;
1674 const struct dw_mci_drv_data *drv_data = host->drv_data;
1675
1676 if (drv_data && drv_data->prepare_hs400_tuning)
1677 return drv_data->prepare_hs400_tuning(host, ios);
1678
1679 return 0;
1680}
1681
Will Newtonf95f3852011-01-02 01:11:59 -05001682static const struct mmc_host_ops dw_mci_ops = {
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301683 .request = dw_mci_request,
Seungwon Jeon9aa51402012-02-06 16:55:07 +09001684 .pre_req = dw_mci_pre_req,
1685 .post_req = dw_mci_post_req,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301686 .set_ios = dw_mci_set_ios,
1687 .get_ro = dw_mci_get_ro,
1688 .get_cd = dw_mci_get_cd,
Shawn Lin935a6652016-01-14 09:08:02 +08001689 .hw_reset = dw_mci_hw_reset,
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05301690 .enable_sdio_irq = dw_mci_enable_sdio_irq,
Seungwon Jeon0976f162013-08-31 00:12:42 +09001691 .execute_tuning = dw_mci_execute_tuning,
Doug Anderson01730552014-08-22 19:17:51 +05301692 .card_busy = dw_mci_card_busy,
1693 .start_signal_voltage_switch = dw_mci_switch_voltage,
Doug Andersonb24c8b22014-12-02 15:42:46 -08001694 .init_card = dw_mci_init_card,
Seungwon Jeon80113132015-01-29 08:11:57 +05301695 .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning,
Will Newtonf95f3852011-01-02 01:11:59 -05001696};
1697
1698static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq)
1699 __releases(&host->lock)
1700 __acquires(&host->lock)
1701{
1702 struct dw_mci_slot *slot;
1703 struct mmc_host *prev_mmc = host->cur_slot->mmc;
1704
1705 WARN_ON(host->cmd || host->data);
1706
1707 host->cur_slot->mrq = NULL;
1708 host->mrq = NULL;
1709 if (!list_empty(&host->queue)) {
1710 slot = list_entry(host->queue.next,
1711 struct dw_mci_slot, queue_node);
1712 list_del(&slot->queue_node);
Thomas Abraham4a909202012-09-17 18:16:35 +00001713 dev_vdbg(host->dev, "list not empty: %s is next\n",
Will Newtonf95f3852011-01-02 01:11:59 -05001714 mmc_hostname(slot->mmc));
1715 host->state = STATE_SENDING_CMD;
1716 dw_mci_start_request(host, slot);
1717 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00001718 dev_vdbg(host->dev, "list empty\n");
Doug Anderson01730552014-08-22 19:17:51 +05301719
1720 if (host->state == STATE_SENDING_CMD11)
1721 host->state = STATE_WAITING_CMD11_DONE;
1722 else
1723 host->state = STATE_IDLE;
Will Newtonf95f3852011-01-02 01:11:59 -05001724 }
1725
1726 spin_unlock(&host->lock);
1727 mmc_request_done(prev_mmc, mrq);
1728 spin_lock(&host->lock);
1729}
1730
Seungwon Jeone352c812013-08-31 00:14:17 +09001731static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd)
Will Newtonf95f3852011-01-02 01:11:59 -05001732{
1733 u32 status = host->cmd_status;
1734
1735 host->cmd_status = 0;
1736
1737 /* Read the response from the card (up to 16 bytes) */
1738 if (cmd->flags & MMC_RSP_PRESENT) {
1739 if (cmd->flags & MMC_RSP_136) {
1740 cmd->resp[3] = mci_readl(host, RESP0);
1741 cmd->resp[2] = mci_readl(host, RESP1);
1742 cmd->resp[1] = mci_readl(host, RESP2);
1743 cmd->resp[0] = mci_readl(host, RESP3);
1744 } else {
1745 cmd->resp[0] = mci_readl(host, RESP0);
1746 cmd->resp[1] = 0;
1747 cmd->resp[2] = 0;
1748 cmd->resp[3] = 0;
1749 }
1750 }
1751
1752 if (status & SDMMC_INT_RTO)
1753 cmd->error = -ETIMEDOUT;
1754 else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC))
1755 cmd->error = -EILSEQ;
1756 else if (status & SDMMC_INT_RESP_ERR)
1757 cmd->error = -EIO;
1758 else
1759 cmd->error = 0;
1760
Seungwon Jeone352c812013-08-31 00:14:17 +09001761 return cmd->error;
1762}
1763
1764static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data)
1765{
Seungwon Jeon31bff452013-08-31 00:14:23 +09001766 u32 status = host->data_status;
Seungwon Jeone352c812013-08-31 00:14:17 +09001767
1768 if (status & DW_MCI_DATA_ERROR_FLAGS) {
1769 if (status & SDMMC_INT_DRTO) {
1770 data->error = -ETIMEDOUT;
1771 } else if (status & SDMMC_INT_DCRC) {
1772 data->error = -EILSEQ;
1773 } else if (status & SDMMC_INT_EBE) {
1774 if (host->dir_status ==
1775 DW_MCI_SEND_STATUS) {
1776 /*
1777 * No data CRC status was returned.
1778 * The number of bytes transferred
1779 * will be exaggerated in PIO mode.
1780 */
1781 data->bytes_xfered = 0;
1782 data->error = -ETIMEDOUT;
1783 } else if (host->dir_status ==
1784 DW_MCI_RECV_STATUS) {
Shawn Line7a1dec2016-08-22 10:57:16 +08001785 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001786 }
1787 } else {
1788 /* SDMMC_INT_SBE is included */
Shawn Line7a1dec2016-08-22 10:57:16 +08001789 data->error = -EILSEQ;
Seungwon Jeone352c812013-08-31 00:14:17 +09001790 }
1791
Doug Andersone6cc0122014-04-22 16:51:21 -07001792 dev_dbg(host->dev, "data error, status 0x%08x\n", status);
Seungwon Jeone352c812013-08-31 00:14:17 +09001793
1794 /*
1795 * After an error, there may be data lingering
Seungwon Jeon31bff452013-08-31 00:14:23 +09001796 * in the FIFO
Seungwon Jeone352c812013-08-31 00:14:17 +09001797 */
Sonny Rao3a33a942014-08-04 18:19:50 -07001798 dw_mci_reset(host);
Seungwon Jeone352c812013-08-31 00:14:17 +09001799 } else {
1800 data->bytes_xfered = data->blocks * data->blksz;
1801 data->error = 0;
1802 }
1803
1804 return data->error;
Will Newtonf95f3852011-01-02 01:11:59 -05001805}
1806
Addy Ke57e10482015-08-11 01:27:18 +09001807static void dw_mci_set_drto(struct dw_mci *host)
1808{
1809 unsigned int drto_clks;
1810 unsigned int drto_ms;
1811
1812 drto_clks = mci_readl(host, TMOUT) >> 8;
1813 drto_ms = DIV_ROUND_UP(drto_clks, host->bus_hz / 1000);
1814
1815 /* add a bit spare time */
1816 drto_ms += 10;
1817
1818 mod_timer(&host->dto_timer, jiffies + msecs_to_jiffies(drto_ms));
1819}
1820
Will Newtonf95f3852011-01-02 01:11:59 -05001821static void dw_mci_tasklet_func(unsigned long priv)
1822{
1823 struct dw_mci *host = (struct dw_mci *)priv;
1824 struct mmc_data *data;
1825 struct mmc_command *cmd;
Seungwon Jeone352c812013-08-31 00:14:17 +09001826 struct mmc_request *mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001827 enum dw_mci_state state;
1828 enum dw_mci_state prev_state;
Seungwon Jeone352c812013-08-31 00:14:17 +09001829 unsigned int err;
Will Newtonf95f3852011-01-02 01:11:59 -05001830
1831 spin_lock(&host->lock);
1832
1833 state = host->state;
1834 data = host->data;
Seungwon Jeone352c812013-08-31 00:14:17 +09001835 mrq = host->mrq;
Will Newtonf95f3852011-01-02 01:11:59 -05001836
1837 do {
1838 prev_state = state;
1839
1840 switch (state) {
1841 case STATE_IDLE:
Doug Anderson01730552014-08-22 19:17:51 +05301842 case STATE_WAITING_CMD11_DONE:
Will Newtonf95f3852011-01-02 01:11:59 -05001843 break;
1844
Doug Anderson01730552014-08-22 19:17:51 +05301845 case STATE_SENDING_CMD11:
Will Newtonf95f3852011-01-02 01:11:59 -05001846 case STATE_SENDING_CMD:
1847 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
1848 &host->pending_events))
1849 break;
1850
1851 cmd = host->cmd;
1852 host->cmd = NULL;
1853 set_bit(EVENT_CMD_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001854 err = dw_mci_command_complete(host, cmd);
1855 if (cmd == mrq->sbc && !err) {
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001856 prev_state = state = STATE_SENDING_CMD;
1857 __dw_mci_start_request(host, host->cur_slot,
Seungwon Jeone352c812013-08-31 00:14:17 +09001858 mrq->cmd);
Seungwon Jeon053b3ce2011-12-22 18:01:29 +09001859 goto unlock;
1860 }
1861
Seungwon Jeone352c812013-08-31 00:14:17 +09001862 if (cmd->data && err) {
Doug Anderson46d17952016-04-26 10:03:58 +02001863 /*
1864 * During UHS tuning sequence, sending the stop
1865 * command after the response CRC error would
1866 * throw the system into a confused state
1867 * causing all future tuning phases to report
1868 * failure.
1869 *
1870 * In such case controller will move into a data
1871 * transfer state after a response error or
1872 * response CRC error. Let's let that finish
1873 * before trying to send a stop, so we'll go to
1874 * STATE_SENDING_DATA.
1875 *
1876 * Although letting the data transfer take place
1877 * will waste a bit of time (we already know
1878 * the command was bad), it can't cause any
1879 * errors since it's possible it would have
1880 * taken place anyway if this tasklet got
1881 * delayed. Allowing the transfer to take place
1882 * avoids races and keeps things simple.
1883 */
1884 if ((err != -ETIMEDOUT) &&
1885 (cmd->opcode == MMC_SEND_TUNING_BLOCK)) {
1886 state = STATE_SENDING_DATA;
1887 continue;
1888 }
1889
Seungwon Jeon71abb132013-08-31 00:13:59 +09001890 dw_mci_stop_dma(host);
Seungwon Jeon90c21432013-08-31 00:14:05 +09001891 send_stop_abort(host, data);
1892 state = STATE_SENDING_STOP;
1893 break;
Seungwon Jeon71abb132013-08-31 00:13:59 +09001894 }
1895
Seungwon Jeone352c812013-08-31 00:14:17 +09001896 if (!cmd->data || err) {
1897 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05001898 goto unlock;
1899 }
1900
1901 prev_state = state = STATE_SENDING_DATA;
1902 /* fall through */
1903
1904 case STATE_SENDING_DATA:
Doug Anderson2aa35462014-08-13 08:13:43 -07001905 /*
1906 * We could get a data error and never a transfer
1907 * complete so we'd better check for it here.
1908 *
1909 * Note that we don't really care if we also got a
1910 * transfer complete; stopping the DMA and sending an
1911 * abort won't hurt.
1912 */
Will Newtonf95f3852011-01-02 01:11:59 -05001913 if (test_and_clear_bit(EVENT_DATA_ERROR,
1914 &host->pending_events)) {
1915 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001916 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08001917 SDMMC_INT_EBE)))
1918 send_stop_abort(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001919 state = STATE_DATA_ERROR;
1920 break;
1921 }
1922
1923 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001924 &host->pending_events)) {
1925 /*
1926 * If all data-related interrupts don't come
1927 * within the given time in reading data state.
1928 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09001929 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09001930 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001931 break;
Addy Ke57e10482015-08-11 01:27:18 +09001932 }
Will Newtonf95f3852011-01-02 01:11:59 -05001933
1934 set_bit(EVENT_XFER_COMPLETE, &host->completed_events);
Doug Anderson2aa35462014-08-13 08:13:43 -07001935
1936 /*
1937 * Handle an EVENT_DATA_ERROR that might have shown up
1938 * before the transfer completed. This might not have
1939 * been caught by the check above because the interrupt
1940 * could have gone off between the previous check and
1941 * the check for transfer complete.
1942 *
1943 * Technically this ought not be needed assuming we
1944 * get a DATA_COMPLETE eventually (we'll notice the
1945 * error and end the request), but it shouldn't hurt.
1946 *
1947 * This has the advantage of sending the stop command.
1948 */
1949 if (test_and_clear_bit(EVENT_DATA_ERROR,
1950 &host->pending_events)) {
1951 dw_mci_stop_dma(host);
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09001952 if (!(host->data_status & (SDMMC_INT_DRTO |
addy kebdb9a902015-02-20 10:55:25 +08001953 SDMMC_INT_EBE)))
1954 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001955 state = STATE_DATA_ERROR;
1956 break;
1957 }
Will Newtonf95f3852011-01-02 01:11:59 -05001958 prev_state = state = STATE_DATA_BUSY;
Doug Anderson2aa35462014-08-13 08:13:43 -07001959
Will Newtonf95f3852011-01-02 01:11:59 -05001960 /* fall through */
1961
1962 case STATE_DATA_BUSY:
1963 if (!test_and_clear_bit(EVENT_DATA_COMPLETE,
Addy Ke57e10482015-08-11 01:27:18 +09001964 &host->pending_events)) {
1965 /*
1966 * If data error interrupt comes but data over
1967 * interrupt doesn't come within the given time.
1968 * in reading data state.
1969 */
Jaehoon Chung16a34572016-06-21 14:35:37 +09001970 if (host->dir_status == DW_MCI_RECV_STATUS)
Addy Ke57e10482015-08-11 01:27:18 +09001971 dw_mci_set_drto(host);
Will Newtonf95f3852011-01-02 01:11:59 -05001972 break;
Addy Ke57e10482015-08-11 01:27:18 +09001973 }
Will Newtonf95f3852011-01-02 01:11:59 -05001974
1975 host->data = NULL;
1976 set_bit(EVENT_DATA_COMPLETE, &host->completed_events);
Seungwon Jeone352c812013-08-31 00:14:17 +09001977 err = dw_mci_data_complete(host, data);
Will Newtonf95f3852011-01-02 01:11:59 -05001978
Seungwon Jeone352c812013-08-31 00:14:17 +09001979 if (!err) {
1980 if (!data->stop || mrq->sbc) {
Sachin Kamat17c8bc82014-02-25 15:18:28 +05301981 if (mrq->sbc && data->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09001982 data->stop->error = 0;
1983 dw_mci_request_end(host, mrq);
1984 goto unlock;
Will Newtonf95f3852011-01-02 01:11:59 -05001985 }
Will Newtonf95f3852011-01-02 01:11:59 -05001986
Seungwon Jeon90c21432013-08-31 00:14:05 +09001987 /* stop command for open-ended transfer*/
Seungwon Jeone352c812013-08-31 00:14:17 +09001988 if (data->stop)
1989 send_stop_abort(host, data);
Doug Anderson2aa35462014-08-13 08:13:43 -07001990 } else {
1991 /*
1992 * If we don't have a command complete now we'll
1993 * never get one since we just reset everything;
1994 * better end the request.
1995 *
1996 * If we do have a command complete we'll fall
1997 * through to the SENDING_STOP command and
1998 * everything will be peachy keen.
1999 */
2000 if (!test_bit(EVENT_CMD_COMPLETE,
2001 &host->pending_events)) {
2002 host->cmd = NULL;
2003 dw_mci_request_end(host, mrq);
2004 goto unlock;
2005 }
Seungwon Jeon90c21432013-08-31 00:14:05 +09002006 }
Seungwon Jeone352c812013-08-31 00:14:17 +09002007
2008 /*
2009 * If err has non-zero,
2010 * stop-abort command has been already issued.
2011 */
2012 prev_state = state = STATE_SENDING_STOP;
2013
Will Newtonf95f3852011-01-02 01:11:59 -05002014 /* fall through */
2015
2016 case STATE_SENDING_STOP:
2017 if (!test_and_clear_bit(EVENT_CMD_COMPLETE,
2018 &host->pending_events))
2019 break;
2020
Seungwon Jeon71abb132013-08-31 00:13:59 +09002021 /* CMD error in data command */
Seungwon Jeon31bff452013-08-31 00:14:23 +09002022 if (mrq->cmd->error && mrq->data)
Sonny Rao3a33a942014-08-04 18:19:50 -07002023 dw_mci_reset(host);
Seungwon Jeon71abb132013-08-31 00:13:59 +09002024
Will Newtonf95f3852011-01-02 01:11:59 -05002025 host->cmd = NULL;
Seungwon Jeon71abb132013-08-31 00:13:59 +09002026 host->data = NULL;
Seungwon Jeon90c21432013-08-31 00:14:05 +09002027
Jaehoon Chunge13c3c02016-11-17 16:40:37 +09002028 if (!mrq->sbc && mrq->stop)
Seungwon Jeone352c812013-08-31 00:14:17 +09002029 dw_mci_command_complete(host, mrq->stop);
Seungwon Jeon90c21432013-08-31 00:14:05 +09002030 else
2031 host->cmd_status = 0;
2032
Seungwon Jeone352c812013-08-31 00:14:17 +09002033 dw_mci_request_end(host, mrq);
Will Newtonf95f3852011-01-02 01:11:59 -05002034 goto unlock;
2035
2036 case STATE_DATA_ERROR:
2037 if (!test_and_clear_bit(EVENT_XFER_COMPLETE,
2038 &host->pending_events))
2039 break;
2040
2041 state = STATE_DATA_BUSY;
2042 break;
2043 }
2044 } while (state != prev_state);
2045
2046 host->state = state;
2047unlock:
2048 spin_unlock(&host->lock);
2049
2050}
2051
James Hogan34b664a2011-06-24 13:57:56 +01002052/* push final bytes to part_buf, only use during push */
2053static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt)
2054{
2055 memcpy((void *)&host->part_buf, buf, cnt);
2056 host->part_buf_count = cnt;
2057}
2058
2059/* append bytes to part_buf, only use during push */
2060static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt)
2061{
2062 cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count);
2063 memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt);
2064 host->part_buf_count += cnt;
2065 return cnt;
2066}
2067
2068/* pull first bytes from part_buf, only use during pull */
2069static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt)
2070{
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002071 cnt = min_t(int, cnt, host->part_buf_count);
James Hogan34b664a2011-06-24 13:57:56 +01002072 if (cnt) {
2073 memcpy(buf, (void *)&host->part_buf + host->part_buf_start,
2074 cnt);
2075 host->part_buf_count -= cnt;
2076 host->part_buf_start += cnt;
2077 }
2078 return cnt;
2079}
2080
2081/* pull final bytes from the part_buf, assuming it's just been filled */
2082static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt)
2083{
2084 memcpy(buf, &host->part_buf, cnt);
2085 host->part_buf_start = cnt;
2086 host->part_buf_count = (1 << host->data_shift) - cnt;
2087}
2088
Will Newtonf95f3852011-01-02 01:11:59 -05002089static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt)
2090{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002091 struct mmc_data *data = host->data;
2092 int init_cnt = cnt;
2093
James Hogan34b664a2011-06-24 13:57:56 +01002094 /* try and push anything in the part_buf */
2095 if (unlikely(host->part_buf_count)) {
2096 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002097
James Hogan34b664a2011-06-24 13:57:56 +01002098 buf += len;
2099 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002100 if (host->part_buf_count == 2) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002101 mci_fifo_writew(host->fifo_reg, host->part_buf16);
James Hogan34b664a2011-06-24 13:57:56 +01002102 host->part_buf_count = 0;
2103 }
2104 }
2105#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2106 if (unlikely((unsigned long)buf & 0x1)) {
2107 while (cnt >= 2) {
2108 u16 aligned_buf[64];
2109 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2110 int items = len >> 1;
2111 int i;
2112 /* memcpy from input buffer into aligned buffer */
2113 memcpy(aligned_buf, buf, len);
2114 buf += len;
2115 cnt -= len;
2116 /* push data from aligned buffer into fifo */
2117 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002118 mci_fifo_writew(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002119 }
2120 } else
2121#endif
2122 {
2123 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002124
James Hogan34b664a2011-06-24 13:57:56 +01002125 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002126 mci_fifo_writew(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002127 buf = pdata;
2128 }
2129 /* put anything remaining in the part_buf */
2130 if (cnt) {
2131 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002132 /* Push data if we have reached the expected data length */
2133 if ((data->bytes_xfered + init_cnt) ==
2134 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002135 mci_fifo_writew(host->fifo_reg, host->part_buf16);
Will Newtonf95f3852011-01-02 01:11:59 -05002136 }
2137}
2138
2139static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt)
2140{
James Hogan34b664a2011-06-24 13:57:56 +01002141#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2142 if (unlikely((unsigned long)buf & 0x1)) {
2143 while (cnt >= 2) {
2144 /* pull data from fifo into aligned buffer */
2145 u16 aligned_buf[64];
2146 int len = min(cnt & -2, (int)sizeof(aligned_buf));
2147 int items = len >> 1;
2148 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002149
James Hogan34b664a2011-06-24 13:57:56 +01002150 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002151 aligned_buf[i] = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002152 /* memcpy from aligned buffer into output buffer */
2153 memcpy(buf, aligned_buf, len);
2154 buf += len;
2155 cnt -= len;
2156 }
2157 } else
2158#endif
2159 {
2160 u16 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002161
James Hogan34b664a2011-06-24 13:57:56 +01002162 for (; cnt >= 2; cnt -= 2)
Ben Dooks76184ac2015-03-25 11:27:52 +00002163 *pdata++ = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002164 buf = pdata;
2165 }
2166 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002167 host->part_buf16 = mci_fifo_readw(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002168 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002169 }
2170}
2171
2172static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt)
2173{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002174 struct mmc_data *data = host->data;
2175 int init_cnt = cnt;
2176
James Hogan34b664a2011-06-24 13:57:56 +01002177 /* try and push anything in the part_buf */
2178 if (unlikely(host->part_buf_count)) {
2179 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002180
James Hogan34b664a2011-06-24 13:57:56 +01002181 buf += len;
2182 cnt -= len;
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002183 if (host->part_buf_count == 4) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002184 mci_fifo_writel(host->fifo_reg, host->part_buf32);
James Hogan34b664a2011-06-24 13:57:56 +01002185 host->part_buf_count = 0;
2186 }
2187 }
2188#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2189 if (unlikely((unsigned long)buf & 0x3)) {
2190 while (cnt >= 4) {
2191 u32 aligned_buf[32];
2192 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2193 int items = len >> 2;
2194 int i;
2195 /* memcpy from input buffer into aligned buffer */
2196 memcpy(aligned_buf, buf, len);
2197 buf += len;
2198 cnt -= len;
2199 /* push data from aligned buffer into fifo */
2200 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002201 mci_fifo_writel(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002202 }
2203 } else
2204#endif
2205 {
2206 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002207
James Hogan34b664a2011-06-24 13:57:56 +01002208 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002209 mci_fifo_writel(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002210 buf = pdata;
2211 }
2212 /* put anything remaining in the part_buf */
2213 if (cnt) {
2214 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002215 /* Push data if we have reached the expected data length */
2216 if ((data->bytes_xfered + init_cnt) ==
2217 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002218 mci_fifo_writel(host->fifo_reg, host->part_buf32);
Will Newtonf95f3852011-01-02 01:11:59 -05002219 }
2220}
2221
2222static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt)
2223{
James Hogan34b664a2011-06-24 13:57:56 +01002224#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2225 if (unlikely((unsigned long)buf & 0x3)) {
2226 while (cnt >= 4) {
2227 /* pull data from fifo into aligned buffer */
2228 u32 aligned_buf[32];
2229 int len = min(cnt & -4, (int)sizeof(aligned_buf));
2230 int items = len >> 2;
2231 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002232
James Hogan34b664a2011-06-24 13:57:56 +01002233 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002234 aligned_buf[i] = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002235 /* memcpy from aligned buffer into output buffer */
2236 memcpy(buf, aligned_buf, len);
2237 buf += len;
2238 cnt -= len;
2239 }
2240 } else
2241#endif
2242 {
2243 u32 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002244
James Hogan34b664a2011-06-24 13:57:56 +01002245 for (; cnt >= 4; cnt -= 4)
Ben Dooks76184ac2015-03-25 11:27:52 +00002246 *pdata++ = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002247 buf = pdata;
2248 }
2249 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002250 host->part_buf32 = mci_fifo_readl(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002251 dw_mci_pull_final_bytes(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002252 }
2253}
2254
2255static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt)
2256{
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002257 struct mmc_data *data = host->data;
2258 int init_cnt = cnt;
2259
James Hogan34b664a2011-06-24 13:57:56 +01002260 /* try and push anything in the part_buf */
2261 if (unlikely(host->part_buf_count)) {
2262 int len = dw_mci_push_part_bytes(host, buf, cnt);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002263
James Hogan34b664a2011-06-24 13:57:56 +01002264 buf += len;
2265 cnt -= len;
Seungwon Jeonc09fbd72013-03-25 16:28:22 +09002266
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002267 if (host->part_buf_count == 8) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002268 mci_fifo_writeq(host->fifo_reg, host->part_buf);
James Hogan34b664a2011-06-24 13:57:56 +01002269 host->part_buf_count = 0;
2270 }
2271 }
2272#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2273 if (unlikely((unsigned long)buf & 0x7)) {
2274 while (cnt >= 8) {
2275 u64 aligned_buf[16];
2276 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2277 int items = len >> 3;
2278 int i;
2279 /* memcpy from input buffer into aligned buffer */
2280 memcpy(aligned_buf, buf, len);
2281 buf += len;
2282 cnt -= len;
2283 /* push data from aligned buffer into fifo */
2284 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002285 mci_fifo_writeq(host->fifo_reg, aligned_buf[i]);
James Hogan34b664a2011-06-24 13:57:56 +01002286 }
2287 } else
2288#endif
2289 {
2290 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002291
James Hogan34b664a2011-06-24 13:57:56 +01002292 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002293 mci_fifo_writeq(host->fifo_reg, *pdata++);
James Hogan34b664a2011-06-24 13:57:56 +01002294 buf = pdata;
2295 }
2296 /* put anything remaining in the part_buf */
2297 if (cnt) {
2298 dw_mci_set_part_bytes(host, buf, cnt);
Markos Chandrascfbeb59c2013-03-12 10:53:13 +00002299 /* Push data if we have reached the expected data length */
2300 if ((data->bytes_xfered + init_cnt) ==
2301 (data->blksz * data->blocks))
Ben Dooks76184ac2015-03-25 11:27:52 +00002302 mci_fifo_writeq(host->fifo_reg, host->part_buf);
Will Newtonf95f3852011-01-02 01:11:59 -05002303 }
2304}
2305
2306static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt)
2307{
James Hogan34b664a2011-06-24 13:57:56 +01002308#ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
2309 if (unlikely((unsigned long)buf & 0x7)) {
2310 while (cnt >= 8) {
2311 /* pull data from fifo into aligned buffer */
2312 u64 aligned_buf[16];
2313 int len = min(cnt & -8, (int)sizeof(aligned_buf));
2314 int items = len >> 3;
2315 int i;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002316
James Hogan34b664a2011-06-24 13:57:56 +01002317 for (i = 0; i < items; ++i)
Ben Dooks76184ac2015-03-25 11:27:52 +00002318 aligned_buf[i] = mci_fifo_readq(host->fifo_reg);
2319
James Hogan34b664a2011-06-24 13:57:56 +01002320 /* memcpy from aligned buffer into output buffer */
2321 memcpy(buf, aligned_buf, len);
2322 buf += len;
2323 cnt -= len;
2324 }
2325 } else
2326#endif
2327 {
2328 u64 *pdata = buf;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002329
James Hogan34b664a2011-06-24 13:57:56 +01002330 for (; cnt >= 8; cnt -= 8)
Ben Dooks76184ac2015-03-25 11:27:52 +00002331 *pdata++ = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002332 buf = pdata;
Will Newtonf95f3852011-01-02 01:11:59 -05002333 }
James Hogan34b664a2011-06-24 13:57:56 +01002334 if (cnt) {
Ben Dooks76184ac2015-03-25 11:27:52 +00002335 host->part_buf = mci_fifo_readq(host->fifo_reg);
James Hogan34b664a2011-06-24 13:57:56 +01002336 dw_mci_pull_final_bytes(host, buf, cnt);
2337 }
2338}
2339
2340static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt)
2341{
2342 int len;
2343
2344 /* get remaining partial bytes */
2345 len = dw_mci_pull_part_bytes(host, buf, cnt);
2346 if (unlikely(len == cnt))
2347 return;
2348 buf += len;
2349 cnt -= len;
2350
2351 /* get the rest of the data */
2352 host->pull_data(host, buf, cnt);
Will Newtonf95f3852011-01-02 01:11:59 -05002353}
2354
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002355static void dw_mci_read_data_pio(struct dw_mci *host, bool dto)
Will Newtonf95f3852011-01-02 01:11:59 -05002356{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002357 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2358 void *buf;
2359 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002360 struct mmc_data *data = host->data;
2361 int shift = host->data_shift;
2362 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002363 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002364 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002365
2366 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002367 if (!sg_miter_next(sg_miter))
2368 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002369
Imre Deak4225fc82013-02-27 17:02:57 -08002370 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002371 buf = sg_miter->addr;
2372 remain = sg_miter->length;
2373 offset = 0;
2374
2375 do {
2376 fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS))
2377 << shift) + host->part_buf_count;
2378 len = min(remain, fcnt);
2379 if (!len)
2380 break;
2381 dw_mci_pull_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002382 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002383 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002384 remain -= len;
2385 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002386
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002387 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002388 status = mci_readl(host, MINTSTS);
2389 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002390 /* if the RXDR is ready read again */
2391 } while ((status & SDMMC_INT_RXDR) ||
2392 (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS))));
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002393
2394 if (!remain) {
2395 if (!sg_miter_next(sg_miter))
2396 goto done;
2397 sg_miter->consumed = 0;
2398 }
2399 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002400 return;
2401
2402done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002403 sg_miter_stop(sg_miter);
2404 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002405 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002406 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2407}
2408
2409static void dw_mci_write_data_pio(struct dw_mci *host)
2410{
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002411 struct sg_mapping_iter *sg_miter = &host->sg_miter;
2412 void *buf;
2413 unsigned int offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002414 struct mmc_data *data = host->data;
2415 int shift = host->data_shift;
2416 u32 status;
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002417 unsigned int len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002418 unsigned int fifo_depth = host->fifo_depth;
2419 unsigned int remain, fcnt;
Will Newtonf95f3852011-01-02 01:11:59 -05002420
2421 do {
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002422 if (!sg_miter_next(sg_miter))
2423 goto done;
Will Newtonf95f3852011-01-02 01:11:59 -05002424
Imre Deak4225fc82013-02-27 17:02:57 -08002425 host->sg = sg_miter->piter.sg;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002426 buf = sg_miter->addr;
2427 remain = sg_miter->length;
2428 offset = 0;
2429
2430 do {
2431 fcnt = ((fifo_depth -
2432 SDMMC_GET_FCNT(mci_readl(host, STATUS)))
2433 << shift) - host->part_buf_count;
2434 len = min(remain, fcnt);
2435 if (!len)
2436 break;
2437 host->push_data(host, (void *)(buf + offset), len);
Markos Chandras3e4b0d82013-03-22 12:50:05 -04002438 data->bytes_xfered += len;
Will Newtonf95f3852011-01-02 01:11:59 -05002439 offset += len;
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002440 remain -= len;
2441 } while (remain);
Will Newtonf95f3852011-01-02 01:11:59 -05002442
Seungwon Jeone74f3a92012-08-01 09:30:46 +09002443 sg_miter->consumed = offset;
Will Newtonf95f3852011-01-02 01:11:59 -05002444 status = mci_readl(host, MINTSTS);
2445 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
Will Newtonf95f3852011-01-02 01:11:59 -05002446 } while (status & SDMMC_INT_TXDR); /* if TXDR write again */
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002447
2448 if (!remain) {
2449 if (!sg_miter_next(sg_miter))
2450 goto done;
2451 sg_miter->consumed = 0;
2452 }
2453 sg_miter_stop(sg_miter);
Will Newtonf95f3852011-01-02 01:11:59 -05002454 return;
2455
2456done:
Seungwon Jeonf9c2a0d2012-02-09 14:32:43 +09002457 sg_miter_stop(sg_miter);
2458 host->sg = NULL;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002459 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002460 set_bit(EVENT_XFER_COMPLETE, &host->pending_events);
2461}
2462
2463static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status)
2464{
2465 if (!host->cmd_status)
2466 host->cmd_status = status;
2467
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002468 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002469
2470 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2471 tasklet_schedule(&host->tasklet);
2472}
2473
Doug Anderson6130e7a2014-10-14 09:33:09 -07002474static void dw_mci_handle_cd(struct dw_mci *host)
2475{
2476 int i;
2477
2478 for (i = 0; i < host->num_slots; i++) {
2479 struct dw_mci_slot *slot = host->slot[i];
2480
2481 if (!slot)
2482 continue;
2483
2484 if (slot->mmc->ops->card_event)
2485 slot->mmc->ops->card_event(slot->mmc);
2486 mmc_detect_change(slot->mmc,
2487 msecs_to_jiffies(host->pdata->detect_delay_ms));
2488 }
2489}
2490
Will Newtonf95f3852011-01-02 01:11:59 -05002491static irqreturn_t dw_mci_interrupt(int irq, void *dev_id)
2492{
2493 struct dw_mci *host = dev_id;
Seungwon Jeon182c9082012-08-01 09:30:30 +09002494 u32 pending;
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302495 int i;
Will Newtonf95f3852011-01-02 01:11:59 -05002496
Markos Chandras1fb5f682013-03-12 10:53:11 +00002497 pending = mci_readl(host, MINTSTS); /* read-only mask reg */
2498
2499 if (pending) {
Doug Anderson01730552014-08-22 19:17:51 +05302500 /* Check volt switch first, since it can look like an error */
2501 if ((host->state == STATE_SENDING_CMD11) &&
2502 (pending & SDMMC_INT_VOLT_SWITCH)) {
Doug Anderson49ba0302015-04-03 11:13:07 -07002503 unsigned long irqflags;
Doug Anderson5c935162015-03-09 16:18:21 -07002504
Doug Anderson01730552014-08-22 19:17:51 +05302505 mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH);
2506 pending &= ~SDMMC_INT_VOLT_SWITCH;
Doug Anderson49ba0302015-04-03 11:13:07 -07002507
2508 /*
2509 * Hold the lock; we know cmd11_timer can't be kicked
2510 * off after the lock is released, so safe to delete.
2511 */
2512 spin_lock_irqsave(&host->irq_lock, irqflags);
Doug Anderson01730552014-08-22 19:17:51 +05302513 dw_mci_cmd_interrupt(host, pending);
Doug Anderson49ba0302015-04-03 11:13:07 -07002514 spin_unlock_irqrestore(&host->irq_lock, irqflags);
2515
2516 del_timer(&host->cmd11_timer);
Doug Anderson01730552014-08-22 19:17:51 +05302517 }
2518
Will Newtonf95f3852011-01-02 01:11:59 -05002519 if (pending & DW_MCI_CMD_ERROR_FLAGS) {
2520 mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002521 host->cmd_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002522 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002523 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
Will Newtonf95f3852011-01-02 01:11:59 -05002524 }
2525
2526 if (pending & DW_MCI_DATA_ERROR_FLAGS) {
2527 /* if there is an error report DATA_ERROR */
2528 mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002529 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002530 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002531 set_bit(EVENT_DATA_ERROR, &host->pending_events);
Seungwon Jeon9b2026a2012-08-01 09:30:40 +09002532 tasklet_schedule(&host->tasklet);
Will Newtonf95f3852011-01-02 01:11:59 -05002533 }
2534
2535 if (pending & SDMMC_INT_DATA_OVER) {
Jaehoon Chung16a34572016-06-21 14:35:37 +09002536 del_timer(&host->dto_timer);
Addy Ke57e10482015-08-11 01:27:18 +09002537
Will Newtonf95f3852011-01-02 01:11:59 -05002538 mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER);
2539 if (!host->data_status)
Seungwon Jeon182c9082012-08-01 09:30:30 +09002540 host->data_status = pending;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002541 smp_wmb(); /* drain writebuffer */
Will Newtonf95f3852011-01-02 01:11:59 -05002542 if (host->dir_status == DW_MCI_RECV_STATUS) {
2543 if (host->sg != NULL)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002544 dw_mci_read_data_pio(host, true);
Will Newtonf95f3852011-01-02 01:11:59 -05002545 }
2546 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2547 tasklet_schedule(&host->tasklet);
2548 }
2549
2550 if (pending & SDMMC_INT_RXDR) {
2551 mci_writel(host, RINTSTS, SDMMC_INT_RXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002552 if (host->dir_status == DW_MCI_RECV_STATUS && host->sg)
Kyoungil Kim87a74d32013-01-22 16:46:30 +09002553 dw_mci_read_data_pio(host, false);
Will Newtonf95f3852011-01-02 01:11:59 -05002554 }
2555
2556 if (pending & SDMMC_INT_TXDR) {
2557 mci_writel(host, RINTSTS, SDMMC_INT_TXDR);
James Hoganb40af3a2011-06-24 13:54:06 +01002558 if (host->dir_status == DW_MCI_SEND_STATUS && host->sg)
Will Newtonf95f3852011-01-02 01:11:59 -05002559 dw_mci_write_data_pio(host);
2560 }
2561
2562 if (pending & SDMMC_INT_CMD_DONE) {
2563 mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE);
Seungwon Jeon182c9082012-08-01 09:30:30 +09002564 dw_mci_cmd_interrupt(host, pending);
Will Newtonf95f3852011-01-02 01:11:59 -05002565 }
2566
2567 if (pending & SDMMC_INT_CD) {
2568 mci_writel(host, RINTSTS, SDMMC_INT_CD);
Doug Anderson6130e7a2014-10-14 09:33:09 -07002569 dw_mci_handle_cd(host);
Will Newtonf95f3852011-01-02 01:11:59 -05002570 }
2571
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302572 /* Handle SDIO Interrupts */
2573 for (i = 0; i < host->num_slots; i++) {
2574 struct dw_mci_slot *slot = host->slot[i];
Doug Andersoned2540e2015-02-25 10:11:52 -08002575
2576 if (!slot)
2577 continue;
2578
Addy Ke76756232014-11-04 22:03:09 +08002579 if (pending & SDMMC_INT_SDIO(slot->sdio_id)) {
2580 mci_writel(host, RINTSTS,
2581 SDMMC_INT_SDIO(slot->sdio_id));
Shashidhar Hiremath1a5c8e12011-08-29 13:11:46 +05302582 mmc_signal_sdio_irq(slot->mmc);
2583 }
2584 }
2585
Markos Chandras1fb5f682013-03-12 10:53:11 +00002586 }
Will Newtonf95f3852011-01-02 01:11:59 -05002587
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002588 if (host->use_dma != TRANS_MODE_IDMAC)
2589 return IRQ_HANDLED;
2590
2591 /* Handle IDMA interrupts */
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002592 if (host->dma_64bit_address == 1) {
2593 pending = mci_readl(host, IDSTS64);
2594 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2595 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI |
2596 SDMMC_IDMAC_INT_RI);
2597 mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002598 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2599 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002600 }
2601 } else {
2602 pending = mci_readl(host, IDSTS);
2603 if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) {
2604 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI |
2605 SDMMC_IDMAC_INT_RI);
2606 mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI);
Shawn Linfaecf412016-06-24 15:39:52 +08002607 if (!test_bit(EVENT_DATA_ERROR, &host->pending_events))
2608 host->dma_ops->complete((void *)host);
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002609 }
Will Newtonf95f3852011-01-02 01:11:59 -05002610 }
Will Newtonf95f3852011-01-02 01:11:59 -05002611
2612 return IRQ_HANDLED;
2613}
2614
Jaehoon Chung36c179a2012-08-23 20:31:48 +09002615static int dw_mci_init_slot(struct dw_mci *host, unsigned int id)
Will Newtonf95f3852011-01-02 01:11:59 -05002616{
2617 struct mmc_host *mmc;
2618 struct dw_mci_slot *slot;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002619 const struct dw_mci_drv_data *drv_data = host->drv_data;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002620 int ctrl_id, ret;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002621 u32 freq[2];
Will Newtonf95f3852011-01-02 01:11:59 -05002622
Thomas Abraham4a909202012-09-17 18:16:35 +00002623 mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev);
Will Newtonf95f3852011-01-02 01:11:59 -05002624 if (!mmc)
2625 return -ENOMEM;
2626
2627 slot = mmc_priv(mmc);
2628 slot->id = id;
Addy Ke76756232014-11-04 22:03:09 +08002629 slot->sdio_id = host->sdio_id0 + id;
Will Newtonf95f3852011-01-02 01:11:59 -05002630 slot->mmc = mmc;
2631 slot->host = host;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002632 host->slot[id] = slot;
Will Newtonf95f3852011-01-02 01:11:59 -05002633
2634 mmc->ops = &dw_mci_ops;
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002635 if (of_property_read_u32_array(host->dev->of_node,
2636 "clock-freq-min-max", freq, 2)) {
2637 mmc->f_min = DW_MCI_FREQ_MIN;
2638 mmc->f_max = DW_MCI_FREQ_MAX;
2639 } else {
Jaehoon Chungb0230302016-11-17 16:40:40 +09002640 dev_info(host->dev,
2641 "'clock-freq-min-max' property was deprecated.\n");
Seungwon Jeon1f44a2a2013-08-31 00:13:31 +09002642 mmc->f_min = freq[0];
2643 mmc->f_max = freq[1];
2644 }
Will Newtonf95f3852011-01-02 01:11:59 -05002645
Yuvaraj CD51da2242014-08-22 19:17:50 +05302646 /*if there are external regulators, get them*/
2647 ret = mmc_regulator_get_supply(mmc);
2648 if (ret == -EPROBE_DEFER)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002649 goto err_host_allocated;
Yuvaraj CD51da2242014-08-22 19:17:50 +05302650
2651 if (!mmc->ocr_avail)
2652 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Will Newtonf95f3852011-01-02 01:11:59 -05002653
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002654 if (host->pdata->caps)
2655 mmc->caps = host->pdata->caps;
Jaehoon Chungfc3d7722011-02-25 11:08:15 +09002656
Jaehoon Chung6024e162016-07-15 10:54:50 +09002657 /*
2658 * Support MMC_CAP_ERASE by default.
2659 * It needs to use trim/discard/erase commands.
2660 */
2661 mmc->caps |= MMC_CAP_ERASE;
2662
Abhilash Kesavanab269122012-11-19 10:26:21 +05302663 if (host->pdata->pm_caps)
2664 mmc->pm_caps = host->pdata->pm_caps;
2665
Thomas Abraham800d78b2012-09-17 18:16:42 +00002666 if (host->dev->of_node) {
2667 ctrl_id = of_alias_get_id(host->dev->of_node, "mshc");
2668 if (ctrl_id < 0)
2669 ctrl_id = 0;
2670 } else {
2671 ctrl_id = to_platform_device(host->dev)->id;
2672 }
James Hogancb27a842012-10-16 09:43:08 +01002673 if (drv_data && drv_data->caps)
2674 mmc->caps |= drv_data->caps[ctrl_id];
Thomas Abraham800d78b2012-09-17 18:16:42 +00002675
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002676 if (host->pdata->caps2)
2677 mmc->caps2 = host->pdata->caps2;
Seungwon Jeon4f408cc2011-12-09 14:55:52 +09002678
Doug Anderson3cf890f2014-08-25 11:19:04 -07002679 ret = mmc_of_parse(mmc);
2680 if (ret)
2681 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002682
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002683 /* Useful defaults if platform data is unset. */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002684 if (host->use_dma == TRANS_MODE_IDMAC) {
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002685 mmc->max_segs = host->ring_size;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002686 mmc->max_blk_size = 65535;
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002687 mmc->max_seg_size = 0x1000;
2688 mmc->max_req_size = mmc->max_seg_size * host->ring_size;
2689 mmc->max_blk_count = mmc->max_req_size / 512;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002690 } else if (host->use_dma == TRANS_MODE_EDMAC) {
2691 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002692 mmc->max_blk_size = 65535;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002693 mmc->max_blk_count = 65535;
2694 mmc->max_req_size =
2695 mmc->max_blk_size * mmc->max_blk_count;
2696 mmc->max_seg_size = mmc->max_req_size;
Will Newtonf95f3852011-01-02 01:11:59 -05002697 } else {
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002698 /* TRANS_MODE_PIO */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002699 mmc->max_segs = 64;
Jaehoon Chung225faf82016-05-04 11:24:14 +09002700 mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */
Jaehoon Chung2b708df2015-08-06 16:23:25 +09002701 mmc->max_blk_count = 512;
2702 mmc->max_req_size = mmc->max_blk_size *
2703 mmc->max_blk_count;
2704 mmc->max_seg_size = mmc->max_req_size;
Jaehoon Chunga39e5742012-02-04 17:00:27 -05002705 }
Will Newtonf95f3852011-01-02 01:11:59 -05002706
Shawn Linc0834a52016-05-27 14:36:40 +08002707 dw_mci_get_cd(mmc);
Jaehoon Chungae0eb342014-03-03 11:36:48 +09002708
Jaehoon Chung0cea5292013-02-15 23:45:45 +09002709 ret = mmc_add_host(mmc);
2710 if (ret)
Doug Anderson3cf890f2014-08-25 11:19:04 -07002711 goto err_host_allocated;
Will Newtonf95f3852011-01-02 01:11:59 -05002712
2713#if defined(CONFIG_DEBUG_FS)
2714 dw_mci_init_debugfs(slot);
2715#endif
2716
Will Newtonf95f3852011-01-02 01:11:59 -05002717 return 0;
Thomas Abraham800d78b2012-09-17 18:16:42 +00002718
Doug Anderson3cf890f2014-08-25 11:19:04 -07002719err_host_allocated:
Thomas Abraham800d78b2012-09-17 18:16:42 +00002720 mmc_free_host(mmc);
Yuvaraj CD51da2242014-08-22 19:17:50 +05302721 return ret;
Will Newtonf95f3852011-01-02 01:11:59 -05002722}
2723
2724static void dw_mci_cleanup_slot(struct dw_mci_slot *slot, unsigned int id)
2725{
Will Newtonf95f3852011-01-02 01:11:59 -05002726 /* Debugfs stuff is cleaned up by mmc core */
2727 mmc_remove_host(slot->mmc);
2728 slot->host->slot[id] = NULL;
2729 mmc_free_host(slot->mmc);
2730}
2731
2732static void dw_mci_init_dma(struct dw_mci *host)
2733{
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002734 int addr_config;
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002735 struct device *dev = host->dev;
2736 struct device_node *np = dev->of_node;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002737
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002738 /*
2739 * Check tansfer mode from HCON[17:16]
2740 * Clear the ambiguous description of dw_mmc databook:
2741 * 2b'00: No DMA Interface -> Actually means using Internal DMA block
2742 * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block
2743 * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block
2744 * 2b'11: Non DW DMA Interface -> pio only
2745 * Compared to DesignWare DMA Interface, Generic DMA Interface has a
2746 * simpler request/acknowledge handshake mechanism and both of them
2747 * are regarded as external dma master for dw_mmc.
2748 */
2749 host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON));
2750 if (host->use_dma == DMA_INTERFACE_IDMA) {
2751 host->use_dma = TRANS_MODE_IDMAC;
2752 } else if (host->use_dma == DMA_INTERFACE_DWDMA ||
2753 host->use_dma == DMA_INTERFACE_GDMA) {
2754 host->use_dma = TRANS_MODE_EDMAC;
Prabu Thangamuthu69d99fd2014-10-20 07:12:33 +00002755 } else {
Will Newtonf95f3852011-01-02 01:11:59 -05002756 goto no_dma;
2757 }
2758
2759 /* Determine which DMA interface to use */
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002760 if (host->use_dma == TRANS_MODE_IDMAC) {
2761 /*
2762 * Check ADDR_CONFIG bit in HCON to find
2763 * IDMAC address bus width
2764 */
Shawn Lin70692752015-09-16 14:41:37 +08002765 addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05002766
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002767 if (addr_config == 1) {
2768 /* host supports IDMAC in 64-bit address mode */
2769 host->dma_64bit_address = 1;
2770 dev_info(host->dev,
2771 "IDMAC supports 64-bit address mode.\n");
2772 if (!dma_set_mask(host->dev, DMA_BIT_MASK(64)))
2773 dma_set_coherent_mask(host->dev,
2774 DMA_BIT_MASK(64));
2775 } else {
2776 /* host supports IDMAC in 32-bit address mode */
2777 host->dma_64bit_address = 0;
2778 dev_info(host->dev,
2779 "IDMAC supports 32-bit address mode.\n");
2780 }
2781
2782 /* Alloc memory for sg translation */
Shawn Lincc190d42016-09-02 12:14:39 +08002783 host->sg_cpu = dmam_alloc_coherent(host->dev,
2784 DESC_RING_BUF_SZ,
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002785 &host->sg_dma, GFP_KERNEL);
2786 if (!host->sg_cpu) {
2787 dev_err(host->dev,
2788 "%s: could not alloc DMA memory\n",
2789 __func__);
2790 goto no_dma;
2791 }
2792
2793 host->dma_ops = &dw_mci_idmac_ops;
2794 dev_info(host->dev, "Using internal DMA controller.\n");
2795 } else {
2796 /* TRANS_MODE_EDMAC: check dma bindings again */
2797 if ((of_property_count_strings(np, "dma-names") < 0) ||
2798 (!of_find_property(np, "dmas", NULL))) {
2799 goto no_dma;
2800 }
2801 host->dma_ops = &dw_mci_edmac_ops;
2802 dev_info(host->dev, "Using external DMA controller.\n");
2803 }
Will Newtonf95f3852011-01-02 01:11:59 -05002804
Jaehoon Chunge1631f92012-04-18 15:42:31 +09002805 if (host->dma_ops->init && host->dma_ops->start &&
2806 host->dma_ops->stop && host->dma_ops->cleanup) {
Will Newtonf95f3852011-01-02 01:11:59 -05002807 if (host->dma_ops->init(host)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002808 dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n",
2809 __func__);
Will Newtonf95f3852011-01-02 01:11:59 -05002810 goto no_dma;
2811 }
2812 } else {
Thomas Abraham4a909202012-09-17 18:16:35 +00002813 dev_err(host->dev, "DMA initialization not found.\n");
Will Newtonf95f3852011-01-02 01:11:59 -05002814 goto no_dma;
2815 }
2816
Will Newtonf95f3852011-01-02 01:11:59 -05002817 return;
2818
2819no_dma:
Thomas Abraham4a909202012-09-17 18:16:35 +00002820 dev_info(host->dev, "Using PIO mode.\n");
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002821 host->use_dma = TRANS_MODE_PIO;
Will Newtonf95f3852011-01-02 01:11:59 -05002822}
2823
Seungwon Jeon31bff452013-08-31 00:14:23 +09002824static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset)
Will Newtonf95f3852011-01-02 01:11:59 -05002825{
2826 unsigned long timeout = jiffies + msecs_to_jiffies(500);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002827 u32 ctrl;
Will Newtonf95f3852011-01-02 01:11:59 -05002828
Seungwon Jeon31bff452013-08-31 00:14:23 +09002829 ctrl = mci_readl(host, CTRL);
2830 ctrl |= reset;
2831 mci_writel(host, CTRL, ctrl);
Will Newtonf95f3852011-01-02 01:11:59 -05002832
2833 /* wait till resets clear */
2834 do {
2835 ctrl = mci_readl(host, CTRL);
Seungwon Jeon31bff452013-08-31 00:14:23 +09002836 if (!(ctrl & reset))
Will Newtonf95f3852011-01-02 01:11:59 -05002837 return true;
2838 } while (time_before(jiffies, timeout));
2839
Seungwon Jeon31bff452013-08-31 00:14:23 +09002840 dev_err(host->dev,
2841 "Timeout resetting block (ctrl reset %#x)\n",
2842 ctrl & reset);
Will Newtonf95f3852011-01-02 01:11:59 -05002843
2844 return false;
2845}
2846
Sonny Rao3a33a942014-08-04 18:19:50 -07002847static bool dw_mci_reset(struct dw_mci *host)
Seungwon Jeon31bff452013-08-31 00:14:23 +09002848{
Sonny Rao3a33a942014-08-04 18:19:50 -07002849 u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET;
2850 bool ret = false;
2851
Seungwon Jeon31bff452013-08-31 00:14:23 +09002852 /*
2853 * Reseting generates a block interrupt, hence setting
2854 * the scatter-gather pointer to NULL.
2855 */
2856 if (host->sg) {
2857 sg_miter_stop(&host->sg_miter);
2858 host->sg = NULL;
2859 }
2860
Sonny Rao3a33a942014-08-04 18:19:50 -07002861 if (host->use_dma)
2862 flags |= SDMMC_CTRL_DMA_RESET;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002863
Sonny Rao3a33a942014-08-04 18:19:50 -07002864 if (dw_mci_ctrl_reset(host, flags)) {
2865 /*
2866 * In all cases we clear the RAWINTS register to clear any
2867 * interrupts.
2868 */
2869 mci_writel(host, RINTSTS, 0xFFFFFFFF);
2870
2871 /* if using dma we wait for dma_req to clear */
2872 if (host->use_dma) {
2873 unsigned long timeout = jiffies + msecs_to_jiffies(500);
2874 u32 status;
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002875
Sonny Rao3a33a942014-08-04 18:19:50 -07002876 do {
2877 status = mci_readl(host, STATUS);
2878 if (!(status & SDMMC_STATUS_DMA_REQ))
2879 break;
2880 cpu_relax();
2881 } while (time_before(jiffies, timeout));
2882
2883 if (status & SDMMC_STATUS_DMA_REQ) {
2884 dev_err(host->dev,
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002885 "%s: Timeout waiting for dma_req to clear during reset\n",
2886 __func__);
Sonny Rao3a33a942014-08-04 18:19:50 -07002887 goto ciu_out;
2888 }
2889
2890 /* when using DMA next we reset the fifo again */
2891 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET))
2892 goto ciu_out;
2893 }
2894 } else {
2895 /* if the controller reset bit did clear, then set clock regs */
2896 if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002897 dev_err(host->dev,
2898 "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n",
Sonny Rao3a33a942014-08-04 18:19:50 -07002899 __func__);
2900 goto ciu_out;
2901 }
2902 }
2903
Shawn Lin3fc7eae2015-09-16 14:41:23 +08002904 if (host->use_dma == TRANS_MODE_IDMAC)
2905 /* It is also recommended that we reset and reprogram idmac */
2906 dw_mci_idmac_reset(host);
Sonny Rao3a33a942014-08-04 18:19:50 -07002907
2908 ret = true;
2909
2910ciu_out:
2911 /* After a CTRL reset we need to have CIU set clock registers */
2912 mci_send_cmd(host->cur_slot, SDMMC_CMD_UPD_CLK, 0);
2913
2914 return ret;
Seungwon Jeon31bff452013-08-31 00:14:23 +09002915}
2916
Doug Anderson5c935162015-03-09 16:18:21 -07002917static void dw_mci_cmd11_timer(unsigned long arg)
2918{
2919 struct dw_mci *host = (struct dw_mci *)arg;
2920
Doug Andersonfd674192015-04-03 11:13:06 -07002921 if (host->state != STATE_SENDING_CMD11) {
2922 dev_warn(host->dev, "Unexpected CMD11 timeout\n");
2923 return;
2924 }
Doug Anderson5c935162015-03-09 16:18:21 -07002925
2926 host->cmd_status = SDMMC_INT_RTO;
2927 set_bit(EVENT_CMD_COMPLETE, &host->pending_events);
2928 tasklet_schedule(&host->tasklet);
2929}
2930
Addy Ke57e10482015-08-11 01:27:18 +09002931static void dw_mci_dto_timer(unsigned long arg)
2932{
2933 struct dw_mci *host = (struct dw_mci *)arg;
2934
2935 switch (host->state) {
2936 case STATE_SENDING_DATA:
2937 case STATE_DATA_BUSY:
2938 /*
2939 * If DTO interrupt does NOT come in sending data state,
2940 * we should notify the driver to terminate current transfer
2941 * and report a data timeout to the core.
2942 */
2943 host->data_status = SDMMC_INT_DRTO;
2944 set_bit(EVENT_DATA_ERROR, &host->pending_events);
2945 set_bit(EVENT_DATA_COMPLETE, &host->pending_events);
2946 tasklet_schedule(&host->tasklet);
2947 break;
2948 default:
2949 break;
2950 }
2951}
2952
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002953#ifdef CONFIG_OF
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002954static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
2955{
2956 struct dw_mci_board *pdata;
2957 struct device *dev = host->dev;
2958 struct device_node *np = dev->of_node;
Arnd Bergmanne95baf12012-11-08 14:26:11 +00002959 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shawn Line8cc37b2016-01-21 14:52:52 +08002960 int ret;
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002961 u32 clock_frequency;
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002962
2963 pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
Beomho Seobf3707e2014-12-23 21:07:33 +09002964 if (!pdata)
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002965 return ERR_PTR(-ENOMEM);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002966
Guodong Xud6786fe2016-08-12 16:51:26 +08002967 /* find reset controller when exist */
Jaehoon Chung3a667e32016-10-31 11:49:42 +09002968 pdata->rstc = devm_reset_control_get_optional(dev, "reset");
Guodong Xud6786fe2016-08-12 16:51:26 +08002969 if (IS_ERR(pdata->rstc)) {
2970 if (PTR_ERR(pdata->rstc) == -EPROBE_DEFER)
2971 return ERR_PTR(-EPROBE_DEFER);
2972 }
2973
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002974 /* find out number of slots supported */
Shawn Lin8a629d22016-02-02 14:11:25 +08002975 of_property_read_u32(np, "num-slots", &pdata->num_slots);
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002976
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002977 if (of_property_read_u32(np, "fifo-depth", &pdata->fifo_depth))
Shawn Lin0e3a22c2015-08-03 15:07:21 +08002978 dev_info(dev,
2979 "fifo-depth property not found, using value of FIFOTH register as default\n");
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002980
2981 of_property_read_u32(np, "card-detect-delay", &pdata->detect_delay_ms);
2982
Jun Niea0361c12017-01-11 15:35:35 +09002983 of_property_read_u32(np, "data-addr", &host->data_addr_override);
2984
Jun Nied6fced82017-01-11 15:37:26 +09002985 if (of_get_property(np, "fifo-watermark-aligned", NULL))
2986 host->wm_aligned = true;
2987
Doug Anderson3c6d89e2013-06-07 10:28:30 -07002988 if (!of_property_read_u32(np, "clock-frequency", &clock_frequency))
2989 pdata->bus_hz = clock_frequency;
2990
James Hogancb27a842012-10-16 09:43:08 +01002991 if (drv_data && drv_data->parse_dt) {
2992 ret = drv_data->parse_dt(host);
Thomas Abraham800d78b2012-09-17 18:16:42 +00002993 if (ret)
2994 return ERR_PTR(ret);
2995 }
2996
Thomas Abrahamc91eab42012-09-17 18:16:40 +00002997 return pdata;
2998}
2999
3000#else /* CONFIG_OF */
3001static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host)
3002{
3003 return ERR_PTR(-EINVAL);
3004}
3005#endif /* CONFIG_OF */
3006
Doug Andersonfa0c3282015-02-25 10:11:51 -08003007static void dw_mci_enable_cd(struct dw_mci *host)
3008{
Doug Andersonfa0c3282015-02-25 10:11:51 -08003009 unsigned long irqflags;
3010 u32 temp;
3011 int i;
Shawn Line8cc37b2016-01-21 14:52:52 +08003012 struct dw_mci_slot *slot;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003013
Shawn Line8cc37b2016-01-21 14:52:52 +08003014 /*
3015 * No need for CD if all slots have a non-error GPIO
3016 * as well as broken card detection is found.
3017 */
Doug Andersonfa0c3282015-02-25 10:11:51 -08003018 for (i = 0; i < host->num_slots; i++) {
Shawn Line8cc37b2016-01-21 14:52:52 +08003019 slot = host->slot[i];
3020 if (slot->mmc->caps & MMC_CAP_NEEDS_POLL)
3021 return;
Doug Andersonfa0c3282015-02-25 10:11:51 -08003022
Arnd Bergmann287980e2016-05-27 23:23:25 +02003023 if (mmc_gpio_get_cd(slot->mmc) < 0)
Doug Andersonfa0c3282015-02-25 10:11:51 -08003024 break;
3025 }
3026 if (i == host->num_slots)
3027 return;
3028
3029 spin_lock_irqsave(&host->irq_lock, irqflags);
3030 temp = mci_readl(host, INTMASK);
3031 temp |= SDMMC_INT_CD;
3032 mci_writel(host, INTMASK, temp);
3033 spin_unlock_irqrestore(&host->irq_lock, irqflags);
3034}
3035
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303036int dw_mci_probe(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003037{
Arnd Bergmanne95baf12012-11-08 14:26:11 +00003038 const struct dw_mci_drv_data *drv_data = host->drv_data;
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303039 int width, i, ret = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003040 u32 fifo_size;
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003041 int init_slots = 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003042
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003043 if (!host->pdata) {
3044 host->pdata = dw_mci_parse_dt(host);
Guodong Xud6786fe2016-08-12 16:51:26 +08003045 if (PTR_ERR(host->pdata) == -EPROBE_DEFER) {
3046 return -EPROBE_DEFER;
3047 } else if (IS_ERR(host->pdata)) {
Thomas Abrahamc91eab42012-09-17 18:16:40 +00003048 dev_err(host->dev, "platform data not available\n");
3049 return -EINVAL;
3050 }
Will Newtonf95f3852011-01-02 01:11:59 -05003051 }
3052
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003053 host->biu_clk = devm_clk_get(host->dev, "biu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003054 if (IS_ERR(host->biu_clk)) {
3055 dev_dbg(host->dev, "biu clock not available\n");
3056 } else {
3057 ret = clk_prepare_enable(host->biu_clk);
3058 if (ret) {
3059 dev_err(host->dev, "failed to enable biu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003060 return ret;
3061 }
Will Newtonf95f3852011-01-02 01:11:59 -05003062 }
3063
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003064 host->ciu_clk = devm_clk_get(host->dev, "ciu");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003065 if (IS_ERR(host->ciu_clk)) {
3066 dev_dbg(host->dev, "ciu clock not available\n");
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003067 host->bus_hz = host->pdata->bus_hz;
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003068 } else {
3069 ret = clk_prepare_enable(host->ciu_clk);
3070 if (ret) {
3071 dev_err(host->dev, "failed to enable ciu clock\n");
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003072 goto err_clk_biu;
3073 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003074
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003075 if (host->pdata->bus_hz) {
3076 ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz);
3077 if (ret)
3078 dev_warn(host->dev,
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003079 "Unable to set bus rate to %uHz\n",
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003080 host->pdata->bus_hz);
3081 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003082 host->bus_hz = clk_get_rate(host->ciu_clk);
Doug Anderson3c6d89e2013-06-07 10:28:30 -07003083 }
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003084
Jaehoon Chung612de4c2014-03-03 11:36:42 +09003085 if (!host->bus_hz) {
3086 dev_err(host->dev,
3087 "Platform data must supply bus speed\n");
3088 ret = -ENODEV;
3089 goto err_clk_ciu;
3090 }
3091
Yuvaraj Kumar C D002f0d52013-08-31 00:12:19 +09003092 if (drv_data && drv_data->init) {
3093 ret = drv_data->init(host);
3094 if (ret) {
3095 dev_err(host->dev,
3096 "implementation specific init failed\n");
3097 goto err_clk_ciu;
3098 }
3099 }
3100
Guodong Xud6786fe2016-08-12 16:51:26 +08003101 if (!IS_ERR(host->pdata->rstc)) {
3102 reset_control_assert(host->pdata->rstc);
3103 usleep_range(10, 50);
3104 reset_control_deassert(host->pdata->rstc);
3105 }
3106
Doug Anderson5c935162015-03-09 16:18:21 -07003107 setup_timer(&host->cmd11_timer,
3108 dw_mci_cmd11_timer, (unsigned long)host);
3109
Jaehoon Chung16a34572016-06-21 14:35:37 +09003110 setup_timer(&host->dto_timer,
3111 dw_mci_dto_timer, (unsigned long)host);
Addy Ke57e10482015-08-11 01:27:18 +09003112
Will Newtonf95f3852011-01-02 01:11:59 -05003113 spin_lock_init(&host->lock);
Doug Andersonf8c58c12014-12-02 15:42:47 -08003114 spin_lock_init(&host->irq_lock);
Will Newtonf95f3852011-01-02 01:11:59 -05003115 INIT_LIST_HEAD(&host->queue);
3116
Will Newtonf95f3852011-01-02 01:11:59 -05003117 /*
3118 * Get the host data width - this assumes that HCON has been set with
3119 * the correct values.
3120 */
Shawn Lin70692752015-09-16 14:41:37 +08003121 i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON));
Will Newtonf95f3852011-01-02 01:11:59 -05003122 if (!i) {
3123 host->push_data = dw_mci_push_data16;
3124 host->pull_data = dw_mci_pull_data16;
3125 width = 16;
3126 host->data_shift = 1;
3127 } else if (i == 2) {
3128 host->push_data = dw_mci_push_data64;
3129 host->pull_data = dw_mci_pull_data64;
3130 width = 64;
3131 host->data_shift = 3;
3132 } else {
3133 /* Check for a reserved value, and warn if it is */
3134 WARN((i != 1),
3135 "HCON reports a reserved host data width!\n"
3136 "Defaulting to 32-bit access.\n");
3137 host->push_data = dw_mci_push_data32;
3138 host->pull_data = dw_mci_pull_data32;
3139 width = 32;
3140 host->data_shift = 2;
3141 }
3142
3143 /* Reset all blocks */
Shawn Lin37444152016-01-22 15:43:12 +08003144 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3145 ret = -ENODEV;
3146 goto err_clk_ciu;
3147 }
Seungwon Jeon141a7122012-05-22 13:01:03 +09003148
3149 host->dma_ops = host->pdata->dma_ops;
3150 dw_mci_init_dma(host);
Will Newtonf95f3852011-01-02 01:11:59 -05003151
3152 /* Clear the interrupts for the host controller */
3153 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3154 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3155
3156 /* Put in max timeout */
3157 mci_writel(host, TMOUT, 0xFFFFFFFF);
3158
3159 /*
3160 * FIFO threshold settings RxMark = fifo_size / 2 - 1,
3161 * Tx Mark = fifo_size / 2 DMA Size = 8
3162 */
James Hoganb86d8252011-06-24 13:57:18 +01003163 if (!host->pdata->fifo_depth) {
3164 /*
3165 * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may
3166 * have been overwritten by the bootloader, just like we're
3167 * about to do, so if you know the value for your hardware, you
3168 * should put it in the platform data.
3169 */
3170 fifo_size = mci_readl(host, FIFOTH);
Jaehoon Chung8234e862012-01-11 09:28:21 +00003171 fifo_size = 1 + ((fifo_size >> 16) & 0xfff);
James Hoganb86d8252011-06-24 13:57:18 +01003172 } else {
3173 fifo_size = host->pdata->fifo_depth;
3174 }
3175 host->fifo_depth = fifo_size;
Seungwon Jeon52426892013-08-31 00:13:42 +09003176 host->fifoth_val =
3177 SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003178 mci_writel(host, FIFOTH, host->fifoth_val);
Will Newtonf95f3852011-01-02 01:11:59 -05003179
3180 /* disable clock to CIU */
3181 mci_writel(host, CLKENA, 0);
3182 mci_writel(host, CLKSRC, 0);
3183
James Hogan63008762013-03-12 10:43:54 +00003184 /*
3185 * In 2.40a spec, Data offset is changed.
3186 * Need to check the version-id and set data-offset for DATA register.
3187 */
3188 host->verid = SDMMC_GET_VERID(mci_readl(host, VERID));
3189 dev_info(host->dev, "Version ID is %04x\n", host->verid);
3190
Jun Niea0361c12017-01-11 15:35:35 +09003191 if (host->data_addr_override)
3192 host->fifo_reg = host->regs + host->data_addr_override;
3193 else if (host->verid < DW_MMC_240A)
Ben Dooks76184ac2015-03-25 11:27:52 +00003194 host->fifo_reg = host->regs + DATA_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003195 else
Ben Dooks76184ac2015-03-25 11:27:52 +00003196 host->fifo_reg = host->regs + DATA_240A_OFFSET;
James Hogan63008762013-03-12 10:43:54 +00003197
Will Newtonf95f3852011-01-02 01:11:59 -05003198 tasklet_init(&host->tasklet, dw_mci_tasklet_func, (unsigned long)host);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003199 ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt,
3200 host->irq_flags, "dw-mci", host);
Will Newtonf95f3852011-01-02 01:11:59 -05003201 if (ret)
Doug Anderson6130e7a2014-10-14 09:33:09 -07003202 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003203
Will Newtonf95f3852011-01-02 01:11:59 -05003204 if (host->pdata->num_slots)
3205 host->num_slots = host->pdata->num_slots;
3206 else
Shawn Lin8a629d22016-02-02 14:11:25 +08003207 host->num_slots = 1;
3208
3209 if (host->num_slots < 1 ||
3210 host->num_slots > SDMMC_GET_SLOT_NUM(mci_readl(host, HCON))) {
3211 dev_err(host->dev,
3212 "Platform data must supply correct num_slots.\n");
3213 ret = -ENODEV;
3214 goto err_clk_ciu;
3215 }
Will Newtonf95f3852011-01-02 01:11:59 -05003216
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303217 /*
Doug Andersonfa0c3282015-02-25 10:11:51 -08003218 * Enable interrupts for command done, data over, data empty,
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303219 * receive ready and error such as transmit, receive timeout, crc error
3220 */
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303221 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3222 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003223 DW_MCI_ERROR_FLAGS);
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003224 /* Enable mci interrupt */
3225 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303226
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003227 dev_info(host->dev,
3228 "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n",
Yuvaraj CD2da1d7f2012-10-08 14:29:51 +05303229 host->irq, width, fifo_size);
3230
Will Newtonf95f3852011-01-02 01:11:59 -05003231 /* We need at least one slot to succeed */
3232 for (i = 0; i < host->num_slots; i++) {
3233 ret = dw_mci_init_slot(host, i);
Thomas Abraham1c2215b2012-09-17 18:16:37 +00003234 if (ret)
3235 dev_dbg(host->dev, "slot %d init failed\n", i);
3236 else
3237 init_slots++;
3238 }
3239
3240 if (init_slots) {
3241 dev_info(host->dev, "%d slots initialized\n", init_slots);
3242 } else {
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003243 dev_dbg(host->dev,
3244 "attempted to initialize %d slots, but failed on all\n",
3245 host->num_slots);
Doug Anderson6130e7a2014-10-14 09:33:09 -07003246 goto err_dmaunmap;
Will Newtonf95f3852011-01-02 01:11:59 -05003247 }
3248
Doug Andersonb793f652015-03-11 15:15:14 -07003249 /* Now that slots are all setup, we can enable card detect */
3250 dw_mci_enable_cd(host);
3251
Will Newtonf95f3852011-01-02 01:11:59 -05003252 return 0;
3253
Will Newtonf95f3852011-01-02 01:11:59 -05003254err_dmaunmap:
3255 if (host->use_dma && host->dma_ops->exit)
3256 host->dma_ops->exit(host);
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003257
Guodong Xud6786fe2016-08-12 16:51:26 +08003258 if (!IS_ERR(host->pdata->rstc))
3259 reset_control_assert(host->pdata->rstc);
3260
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003261err_clk_ciu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003262 clk_disable_unprepare(host->ciu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003263
Thomas Abrahamf90a0612012-09-17 18:16:38 +00003264err_clk_biu:
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003265 clk_disable_unprepare(host->biu_clk);
Seungwon Jeon780f22a2012-11-28 19:26:03 +09003266
Will Newtonf95f3852011-01-02 01:11:59 -05003267 return ret;
3268}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303269EXPORT_SYMBOL(dw_mci_probe);
Will Newtonf95f3852011-01-02 01:11:59 -05003270
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303271void dw_mci_remove(struct dw_mci *host)
Will Newtonf95f3852011-01-02 01:11:59 -05003272{
Will Newtonf95f3852011-01-02 01:11:59 -05003273 int i;
3274
Will Newtonf95f3852011-01-02 01:11:59 -05003275 for (i = 0; i < host->num_slots; i++) {
Thomas Abraham4a909202012-09-17 18:16:35 +00003276 dev_dbg(host->dev, "remove slot %d\n", i);
Will Newtonf95f3852011-01-02 01:11:59 -05003277 if (host->slot[i])
3278 dw_mci_cleanup_slot(host->slot[i], i);
3279 }
3280
Prabu Thangamuthu048fd7e2015-05-28 12:21:06 +00003281 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3282 mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */
3283
Will Newtonf95f3852011-01-02 01:11:59 -05003284 /* disable clock to CIU */
3285 mci_writel(host, CLKENA, 0);
3286 mci_writel(host, CLKSRC, 0);
3287
Will Newtonf95f3852011-01-02 01:11:59 -05003288 if (host->use_dma && host->dma_ops->exit)
3289 host->dma_ops->exit(host);
3290
Guodong Xud6786fe2016-08-12 16:51:26 +08003291 if (!IS_ERR(host->pdata->rstc))
3292 reset_control_assert(host->pdata->rstc);
3293
Jaehoon Chung7037f3b2016-07-15 10:54:08 +09003294 clk_disable_unprepare(host->ciu_clk);
3295 clk_disable_unprepare(host->biu_clk);
Will Newtonf95f3852011-01-02 01:11:59 -05003296}
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303297EXPORT_SYMBOL(dw_mci_remove);
3298
3299
Will Newtonf95f3852011-01-02 01:11:59 -05003300
Shawn Line9ed8832016-10-12 10:50:35 +08003301#ifdef CONFIG_PM
Shawn Lined24e1f2016-10-12 10:56:55 +08003302int dw_mci_runtime_suspend(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003303{
Shawn Lined24e1f2016-10-12 10:56:55 +08003304 struct dw_mci *host = dev_get_drvdata(dev);
3305
Shawn Lin3fc7eae2015-09-16 14:41:23 +08003306 if (host->use_dma && host->dma_ops->exit)
3307 host->dma_ops->exit(host);
3308
Shawn Lined24e1f2016-10-12 10:56:55 +08003309 clk_disable_unprepare(host->ciu_clk);
3310
3311 if (host->cur_slot &&
3312 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3313 !mmc_card_is_removable(host->cur_slot->mmc)))
3314 clk_disable_unprepare(host->biu_clk);
3315
Will Newtonf95f3852011-01-02 01:11:59 -05003316 return 0;
3317}
Shawn Lined24e1f2016-10-12 10:56:55 +08003318EXPORT_SYMBOL(dw_mci_runtime_suspend);
Will Newtonf95f3852011-01-02 01:11:59 -05003319
Shawn Lined24e1f2016-10-12 10:56:55 +08003320int dw_mci_runtime_resume(struct device *dev)
Will Newtonf95f3852011-01-02 01:11:59 -05003321{
Shawn Lined24e1f2016-10-12 10:56:55 +08003322 int i, ret = 0;
3323 struct dw_mci *host = dev_get_drvdata(dev);
Will Newtonf95f3852011-01-02 01:11:59 -05003324
Shawn Lined24e1f2016-10-12 10:56:55 +08003325 if (host->cur_slot &&
3326 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3327 !mmc_card_is_removable(host->cur_slot->mmc))) {
3328 ret = clk_prepare_enable(host->biu_clk);
3329 if (ret)
3330 return ret;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003331 }
3332
Shawn Lined24e1f2016-10-12 10:56:55 +08003333 ret = clk_prepare_enable(host->ciu_clk);
3334 if (ret)
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003335 goto err;
3336
3337 if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) {
3338 clk_disable_unprepare(host->ciu_clk);
3339 ret = -ENODEV;
3340 goto err;
3341 }
Shawn Lined24e1f2016-10-12 10:56:55 +08003342
Jonathan Kliegman3bfe6192012-06-14 13:31:55 -04003343 if (host->use_dma && host->dma_ops->init)
Seungwon Jeon141a7122012-05-22 13:01:03 +09003344 host->dma_ops->init(host);
3345
Seungwon Jeon52426892013-08-31 00:13:42 +09003346 /*
3347 * Restore the initial value at FIFOTH register
3348 * And Invalidate the prev_blksz with zero
3349 */
Shawn Lined24e1f2016-10-12 10:56:55 +08003350 mci_writel(host, FIFOTH, host->fifoth_val);
3351 host->prev_blksz = 0;
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003352
Doug Anderson2eb29442013-08-31 00:11:49 +09003353 /* Put in max timeout */
3354 mci_writel(host, TMOUT, 0xFFFFFFFF);
3355
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003356 mci_writel(host, RINTSTS, 0xFFFFFFFF);
3357 mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER |
3358 SDMMC_INT_TXDR | SDMMC_INT_RXDR |
Doug Andersonfa0c3282015-02-25 10:11:51 -08003359 DW_MCI_ERROR_FLAGS);
Jaehoon Chunge61cf112011-03-17 20:32:33 +09003360 mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE);
3361
Will Newtonf95f3852011-01-02 01:11:59 -05003362 for (i = 0; i < host->num_slots; i++) {
3363 struct dw_mci_slot *slot = host->slot[i];
Shawn Lin0e3a22c2015-08-03 15:07:21 +08003364
Will Newtonf95f3852011-01-02 01:11:59 -05003365 if (!slot)
3366 continue;
Ziyuan Xue9748e02017-01-17 09:22:56 +08003367 if (slot->mmc->pm_flags & MMC_PM_KEEP_POWER)
Abhilash Kesavanab269122012-11-19 10:26:21 +05303368 dw_mci_set_ios(slot->mmc, &slot->mmc->ios);
Ziyuan Xue9748e02017-01-17 09:22:56 +08003369
3370 /* Force setup bus to guarantee available clock output */
3371 dw_mci_setup_bus(slot, true);
Will Newtonf95f3852011-01-02 01:11:59 -05003372 }
Doug Andersonfa0c3282015-02-25 10:11:51 -08003373
3374 /* Now that slots are all setup, we can enable card detect */
3375 dw_mci_enable_cd(host);
3376
Joonyoung Shimdf9bcc22016-11-25 12:47:15 +09003377 return 0;
3378
3379err:
3380 if (host->cur_slot &&
3381 (mmc_can_gpio_cd(host->cur_slot->mmc) ||
3382 !mmc_card_is_removable(host->cur_slot->mmc)))
3383 clk_disable_unprepare(host->biu_clk);
3384
Shawn Lined24e1f2016-10-12 10:56:55 +08003385 return ret;
Shawn Line9ed8832016-10-12 10:50:35 +08003386}
3387EXPORT_SYMBOL(dw_mci_runtime_resume);
3388#endif /* CONFIG_PM */
Jaehoon Chung6fe88902011-12-08 19:23:03 +09003389
Will Newtonf95f3852011-01-02 01:11:59 -05003390static int __init dw_mci_init(void)
3391{
Sachin Kamat8e1c4e42013-04-04 11:25:11 +05303392 pr_info("Synopsys Designware Multimedia Card Interface Driver\n");
Shashidhar Hiremath62ca8032012-01-13 16:04:57 +05303393 return 0;
Will Newtonf95f3852011-01-02 01:11:59 -05003394}
3395
3396static void __exit dw_mci_exit(void)
3397{
Will Newtonf95f3852011-01-02 01:11:59 -05003398}
3399
3400module_init(dw_mci_init);
3401module_exit(dw_mci_exit);
3402
3403MODULE_DESCRIPTION("DW Multimedia Card Interface driver");
3404MODULE_AUTHOR("NXP Semiconductor VietNam");
3405MODULE_AUTHOR("Imagination Technologies Ltd");
3406MODULE_LICENSE("GPL v2");