Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Synopsys DesignWare Multimedia Card Interface driver |
| 4 | * (Based on NXP driver for lpc 31xx) |
| 5 | * |
| 6 | * Copyright (C) 2009 NXP Semiconductors |
| 7 | * Copyright (C) 2009, 2010 Imagination Technologies Ltd. |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/blkdev.h> |
| 11 | #include <linux/clk.h> |
| 12 | #include <linux/debugfs.h> |
| 13 | #include <linux/device.h> |
| 14 | #include <linux/dma-mapping.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/interrupt.h> |
Shawn Lin | b6d2d81 | 2017-02-17 10:56:39 +0800 | [diff] [blame] | 18 | #include <linux/iopoll.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 19 | #include <linux/ioport.h> |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 20 | #include <linux/ktime.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 21 | #include <linux/module.h> |
| 22 | #include <linux/platform_device.h> |
Douglas Anderson | a6db2c8 | 2017-04-11 15:55:43 -0700 | [diff] [blame] | 23 | #include <linux/pm_runtime.h> |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 24 | #include <linux/prandom.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 25 | #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 Anderson | b24c8b2 | 2014-12-02 15:42:46 -0800 | [diff] [blame] | 30 | #include <linux/mmc/card.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 31 | #include <linux/mmc/host.h> |
| 32 | #include <linux/mmc/mmc.h> |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 33 | #include <linux/mmc/sd.h> |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 34 | #include <linux/mmc/sdio.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 35 | #include <linux/bitops.h> |
Jaehoon Chung | c07946a | 2011-02-25 11:08:14 +0900 | [diff] [blame] | 36 | #include <linux/regulator/consumer.h> |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 37 | #include <linux/of.h> |
Doug Anderson | 55a6ceb | 2013-01-11 17:03:53 +0000 | [diff] [blame] | 38 | #include <linux/of_gpio.h> |
Zhangfei Gao | bf626e5 | 2014-01-09 22:35:10 +0800 | [diff] [blame] | 39 | #include <linux/mmc/slot-gpio.h> |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 40 | |
| 41 | #include "dw_mmc.h" |
| 42 | |
| 43 | /* Common flag combinations */ |
Jaehoon Chung | 3f7eec6 | 2013-05-27 13:47:57 +0900 | [diff] [blame] | 44 | #define DW_MCI_DATA_ERROR_FLAGS (SDMMC_INT_DRTO | SDMMC_INT_DCRC | \ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 45 | SDMMC_INT_HTO | SDMMC_INT_SBE | \ |
Doug Anderson | 7a3c567 | 2015-03-10 08:48:10 -0700 | [diff] [blame] | 46 | SDMMC_INT_EBE | SDMMC_INT_HLE) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 47 | #define DW_MCI_CMD_ERROR_FLAGS (SDMMC_INT_RTO | SDMMC_INT_RCRC | \ |
Doug Anderson | 7a3c567 | 2015-03-10 08:48:10 -0700 | [diff] [blame] | 48 | SDMMC_INT_RESP_ERR | SDMMC_INT_HLE) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 49 | #define DW_MCI_ERROR_FLAGS (DW_MCI_DATA_ERROR_FLAGS | \ |
Doug Anderson | 7a3c567 | 2015-03-10 08:48:10 -0700 | [diff] [blame] | 50 | DW_MCI_CMD_ERROR_FLAGS) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 51 | #define DW_MCI_SEND_STATUS 1 |
| 52 | #define DW_MCI_RECV_STATUS 2 |
| 53 | #define DW_MCI_DMA_THRESHOLD 16 |
| 54 | |
Seungwon Jeon | 1f44a2a | 2013-08-31 00:13:31 +0900 | [diff] [blame] | 55 | #define DW_MCI_FREQ_MAX 200000000 /* unit: HZ */ |
Jaehoon Chung | 72e8357 | 2016-11-17 16:40:35 +0900 | [diff] [blame] | 56 | #define DW_MCI_FREQ_MIN 100000 /* unit: HZ */ |
Seungwon Jeon | 1f44a2a | 2013-08-31 00:13:31 +0900 | [diff] [blame] | 57 | |
Joonyoung Shim | fc79a4d | 2013-04-26 15:35:22 +0900 | [diff] [blame] | 58 | #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 Lin | cc190d4 | 2016-09-02 12:14:39 +0800 | [diff] [blame] | 63 | #define DESC_RING_BUF_SZ PAGE_SIZE |
| 64 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 65 | struct idmac_desc_64addr { |
| 66 | u32 des0; /* Control Descriptor */ |
Shawn Lin | b6d2d81 | 2017-02-17 10:56:39 +0800 | [diff] [blame] | 67 | #define IDMAC_OWN_CLR64(x) \ |
| 68 | !((x) & cpu_to_le32(IDMAC_DES0_OWN)) |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 69 | |
| 70 | u32 des1; /* Reserved */ |
| 71 | |
| 72 | u32 des2; /*Buffer sizes */ |
| 73 | #define IDMAC_64ADDR_SET_BUFFER1_SIZE(d, s) \ |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 74 | ((d)->des2 = ((d)->des2 & cpu_to_le32(0x03ffe000)) | \ |
| 75 | ((cpu_to_le32(s)) & cpu_to_le32(0x1fff))) |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 76 | |
| 77 | u32 des3; /* Reserved */ |
| 78 | |
| 79 | u32 des4; /* Lower 32-bits of Buffer Address Pointer 1*/ |
| 80 | u32 des5; /* Upper 32-bits of Buffer Address Pointer 1*/ |
| 81 | |
| 82 | u32 des6; /* Lower 32-bits of Next Descriptor Address */ |
| 83 | u32 des7; /* Upper 32-bits of Next Descriptor Address */ |
| 84 | }; |
| 85 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 86 | struct idmac_desc { |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 87 | __le32 des0; /* Control Descriptor */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 88 | #define IDMAC_DES0_DIC BIT(1) |
| 89 | #define IDMAC_DES0_LD BIT(2) |
| 90 | #define IDMAC_DES0_FD BIT(3) |
| 91 | #define IDMAC_DES0_CH BIT(4) |
| 92 | #define IDMAC_DES0_ER BIT(5) |
| 93 | #define IDMAC_DES0_CES BIT(30) |
| 94 | #define IDMAC_DES0_OWN BIT(31) |
| 95 | |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 96 | __le32 des1; /* Buffer sizes */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 97 | #define IDMAC_SET_BUFFER1_SIZE(d, s) \ |
Ben Dooks | e5306c3 | 2016-06-07 14:37:19 +0100 | [diff] [blame] | 98 | ((d)->des1 = ((d)->des1 & cpu_to_le32(0x03ffe000)) | (cpu_to_le32((s) & 0x1fff))) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 99 | |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 100 | __le32 des2; /* buffer 1 physical address */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 101 | |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 102 | __le32 des3; /* buffer 2 physical address */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 103 | }; |
Alexey Brodkin | 5959b32 | 2015-06-25 11:25:07 +0300 | [diff] [blame] | 104 | |
| 105 | /* Each descriptor can transfer up to 4KB of data in chained mode */ |
| 106 | #define DW_MCI_DESC_DATA_LENGTH 0x1000 |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 107 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 108 | #if defined(CONFIG_DEBUG_FS) |
| 109 | static int dw_mci_req_show(struct seq_file *s, void *v) |
| 110 | { |
| 111 | struct dw_mci_slot *slot = s->private; |
| 112 | struct mmc_request *mrq; |
| 113 | struct mmc_command *cmd; |
| 114 | struct mmc_command *stop; |
| 115 | struct mmc_data *data; |
| 116 | |
| 117 | /* Make sure we get a consistent snapshot */ |
| 118 | spin_lock_bh(&slot->host->lock); |
| 119 | mrq = slot->mrq; |
| 120 | |
| 121 | if (mrq) { |
| 122 | cmd = mrq->cmd; |
| 123 | data = mrq->data; |
| 124 | stop = mrq->stop; |
| 125 | |
| 126 | if (cmd) |
| 127 | seq_printf(s, |
| 128 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 129 | cmd->opcode, cmd->arg, cmd->flags, |
| 130 | cmd->resp[0], cmd->resp[1], cmd->resp[2], |
| 131 | cmd->resp[2], cmd->error); |
| 132 | if (data) |
| 133 | seq_printf(s, "DATA %u / %u * %u flg %x err %d\n", |
| 134 | data->bytes_xfered, data->blocks, |
| 135 | data->blksz, data->flags, data->error); |
| 136 | if (stop) |
| 137 | seq_printf(s, |
| 138 | "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n", |
| 139 | stop->opcode, stop->arg, stop->flags, |
| 140 | stop->resp[0], stop->resp[1], stop->resp[2], |
| 141 | stop->resp[2], stop->error); |
| 142 | } |
| 143 | |
| 144 | spin_unlock_bh(&slot->host->lock); |
| 145 | |
| 146 | return 0; |
| 147 | } |
Shawn Lin | 64c1412b | 2018-02-23 16:47:26 +0800 | [diff] [blame] | 148 | DEFINE_SHOW_ATTRIBUTE(dw_mci_req); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 149 | |
| 150 | static int dw_mci_regs_show(struct seq_file *s, void *v) |
| 151 | { |
Jaehoon Chung | 21657ebd | 2016-11-17 16:40:33 +0900 | [diff] [blame] | 152 | struct dw_mci *host = s->private; |
| 153 | |
Shawn Lin | 5b43df8 | 2018-02-23 16:47:25 +0800 | [diff] [blame] | 154 | pm_runtime_get_sync(host->dev); |
| 155 | |
Jaehoon Chung | 21657ebd | 2016-11-17 16:40:33 +0900 | [diff] [blame] | 156 | seq_printf(s, "STATUS:\t0x%08x\n", mci_readl(host, STATUS)); |
| 157 | seq_printf(s, "RINTSTS:\t0x%08x\n", mci_readl(host, RINTSTS)); |
| 158 | seq_printf(s, "CMD:\t0x%08x\n", mci_readl(host, CMD)); |
| 159 | seq_printf(s, "CTRL:\t0x%08x\n", mci_readl(host, CTRL)); |
| 160 | seq_printf(s, "INTMASK:\t0x%08x\n", mci_readl(host, INTMASK)); |
| 161 | seq_printf(s, "CLKENA:\t0x%08x\n", mci_readl(host, CLKENA)); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 162 | |
Shawn Lin | 5b43df8 | 2018-02-23 16:47:25 +0800 | [diff] [blame] | 163 | pm_runtime_put_autosuspend(host->dev); |
| 164 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 165 | return 0; |
| 166 | } |
Shawn Lin | 64c1412b | 2018-02-23 16:47:26 +0800 | [diff] [blame] | 167 | DEFINE_SHOW_ATTRIBUTE(dw_mci_regs); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 168 | |
| 169 | static void dw_mci_init_debugfs(struct dw_mci_slot *slot) |
| 170 | { |
| 171 | struct mmc_host *mmc = slot->mmc; |
| 172 | struct dw_mci *host = slot->host; |
| 173 | struct dentry *root; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 174 | |
| 175 | root = mmc->debugfs_root; |
| 176 | if (!root) |
| 177 | return; |
| 178 | |
Greg Kroah-Hartman | fcac152 | 2019-06-12 10:25:30 +0200 | [diff] [blame] | 179 | debugfs_create_file("regs", S_IRUSR, root, host, &dw_mci_regs_fops); |
| 180 | debugfs_create_file("req", S_IRUSR, root, slot, &dw_mci_req_fops); |
Geert Uytterhoeven | 118e111 | 2019-10-25 11:41:30 +0200 | [diff] [blame] | 181 | debugfs_create_u32("state", S_IRUSR, root, &host->state); |
Geert Uytterhoeven | 0c40c1b | 2019-10-25 11:41:29 +0200 | [diff] [blame] | 182 | debugfs_create_xul("pending_events", S_IRUSR, root, |
| 183 | &host->pending_events); |
| 184 | debugfs_create_xul("completed_events", S_IRUSR, root, |
| 185 | &host->completed_events); |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 186 | #ifdef CONFIG_FAULT_INJECTION |
| 187 | fault_create_debugfs_attr("fail_data_crc", root, &host->fail_data_crc); |
| 188 | #endif |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 189 | } |
| 190 | #endif /* defined(CONFIG_DEBUG_FS) */ |
| 191 | |
Shawn Lin | 8e6db1f | 2017-02-17 10:56:41 +0800 | [diff] [blame] | 192 | static bool dw_mci_ctrl_reset(struct dw_mci *host, u32 reset) |
| 193 | { |
| 194 | u32 ctrl; |
| 195 | |
| 196 | ctrl = mci_readl(host, CTRL); |
| 197 | ctrl |= reset; |
| 198 | mci_writel(host, CTRL, ctrl); |
| 199 | |
| 200 | /* wait till resets clear */ |
| 201 | if (readl_poll_timeout_atomic(host->regs + SDMMC_CTRL, ctrl, |
| 202 | !(ctrl & reset), |
| 203 | 1, 500 * USEC_PER_MSEC)) { |
| 204 | dev_err(host->dev, |
| 205 | "Timeout resetting block (ctrl reset %#x)\n", |
| 206 | ctrl & reset); |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | return true; |
| 211 | } |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 212 | |
Shawn Lin | 4dba18d | 2017-02-17 10:59:44 +0800 | [diff] [blame] | 213 | static void dw_mci_wait_while_busy(struct dw_mci *host, u32 cmd_flags) |
| 214 | { |
| 215 | u32 status; |
| 216 | |
| 217 | /* |
| 218 | * Databook says that before issuing a new data transfer command |
| 219 | * we need to check to see if the card is busy. Data transfer commands |
| 220 | * all have SDMMC_CMD_PRV_DAT_WAIT set, so we'll key off that. |
| 221 | * |
| 222 | * ...also allow sending for SDMMC_CMD_VOLT_SWITCH where busy is |
| 223 | * expected. |
| 224 | */ |
| 225 | if ((cmd_flags & SDMMC_CMD_PRV_DAT_WAIT) && |
| 226 | !(cmd_flags & SDMMC_CMD_VOLT_SWITCH)) { |
| 227 | if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS, |
| 228 | status, |
| 229 | !(status & SDMMC_STATUS_BUSY), |
| 230 | 10, 500 * USEC_PER_MSEC)) |
| 231 | dev_err(host->dev, "Busy; trying anyway\n"); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | static void mci_send_cmd(struct dw_mci_slot *slot, u32 cmd, u32 arg) |
| 236 | { |
| 237 | struct dw_mci *host = slot->host; |
| 238 | unsigned int cmd_status = 0; |
| 239 | |
| 240 | mci_writel(host, CMDARG, arg); |
| 241 | wmb(); /* drain writebuffer */ |
| 242 | dw_mci_wait_while_busy(host, cmd); |
| 243 | mci_writel(host, CMD, SDMMC_CMD_START | cmd); |
| 244 | |
| 245 | if (readl_poll_timeout_atomic(host->regs + SDMMC_CMD, cmd_status, |
| 246 | !(cmd_status & SDMMC_CMD_START), |
| 247 | 1, 500 * USEC_PER_MSEC)) |
| 248 | dev_err(&slot->mmc->class_dev, |
| 249 | "Timeout sending command (cmd %#x arg %#x status %#x)\n", |
| 250 | cmd, arg, cmd_status); |
| 251 | } |
| 252 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 253 | static u32 dw_mci_prepare_command(struct mmc_host *mmc, struct mmc_command *cmd) |
| 254 | { |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 255 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 256 | struct dw_mci *host = slot->host; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 257 | u32 cmdr; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 258 | |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 259 | cmd->error = -EINPROGRESS; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 260 | cmdr = cmd->opcode; |
| 261 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 262 | if (cmd->opcode == MMC_STOP_TRANSMISSION || |
| 263 | cmd->opcode == MMC_GO_IDLE_STATE || |
| 264 | cmd->opcode == MMC_GO_INACTIVE_STATE || |
| 265 | (cmd->opcode == SD_IO_RW_DIRECT && |
| 266 | ((cmd->arg >> 9) & 0x1FFFF) == SDIO_CCCR_ABORT)) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 267 | cmdr |= SDMMC_CMD_STOP; |
Jaehoon Chung | 4a1b27a | 2014-03-03 11:36:44 +0900 | [diff] [blame] | 268 | else if (cmd->opcode != MMC_SEND_STATUS && cmd->data) |
| 269 | cmdr |= SDMMC_CMD_PRV_DAT_WAIT; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 270 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 271 | if (cmd->opcode == SD_SWITCH_VOLTAGE) { |
| 272 | u32 clk_en_a; |
| 273 | |
| 274 | /* Special bit makes CMD11 not die */ |
| 275 | cmdr |= SDMMC_CMD_VOLT_SWITCH; |
| 276 | |
| 277 | /* Change state to continue to handle CMD11 weirdness */ |
| 278 | WARN_ON(slot->host->state != STATE_SENDING_CMD); |
| 279 | slot->host->state = STATE_SENDING_CMD11; |
| 280 | |
| 281 | /* |
| 282 | * We need to disable low power mode (automatic clock stop) |
| 283 | * while doing voltage switch so we don't confuse the card, |
| 284 | * since stopping the clock is a specific part of the UHS |
| 285 | * voltage change dance. |
| 286 | * |
| 287 | * Note that low power mode (SDMMC_CLKEN_LOW_PWR) will be |
| 288 | * unconditionally turned back on in dw_mci_setup_bus() if it's |
| 289 | * ever called with a non-zero clock. That shouldn't happen |
| 290 | * until the voltage change is all done. |
| 291 | */ |
| 292 | clk_en_a = mci_readl(host, CLKENA); |
| 293 | clk_en_a &= ~(SDMMC_CLKEN_LOW_PWR << slot->id); |
| 294 | mci_writel(host, CLKENA, clk_en_a); |
| 295 | mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | |
| 296 | SDMMC_CMD_PRV_DAT_WAIT, 0); |
| 297 | } |
| 298 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 299 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 300 | /* We expect a response, so set this bit */ |
| 301 | cmdr |= SDMMC_CMD_RESP_EXP; |
| 302 | if (cmd->flags & MMC_RSP_136) |
| 303 | cmdr |= SDMMC_CMD_RESP_LONG; |
| 304 | } |
| 305 | |
| 306 | if (cmd->flags & MMC_RSP_CRC) |
| 307 | cmdr |= SDMMC_CMD_RESP_CRC; |
| 308 | |
Jaehoon Chung | 0349c08 | 2016-11-17 16:40:39 +0900 | [diff] [blame] | 309 | if (cmd->data) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 310 | cmdr |= SDMMC_CMD_DAT_EXP; |
Jaehoon Chung | 0349c08 | 2016-11-17 16:40:39 +0900 | [diff] [blame] | 311 | if (cmd->data->flags & MMC_DATA_WRITE) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 312 | cmdr |= SDMMC_CMD_DAT_WR; |
| 313 | } |
| 314 | |
Jaehoon Chung | aaaaeb7 | 2016-01-21 11:01:06 +0900 | [diff] [blame] | 315 | if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &slot->flags)) |
| 316 | cmdr |= SDMMC_CMD_USE_HOLD_REG; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 317 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 318 | return cmdr; |
| 319 | } |
| 320 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 321 | static u32 dw_mci_prep_stop_abort(struct dw_mci *host, struct mmc_command *cmd) |
| 322 | { |
| 323 | struct mmc_command *stop; |
| 324 | u32 cmdr; |
| 325 | |
| 326 | if (!cmd->data) |
| 327 | return 0; |
| 328 | |
| 329 | stop = &host->stop_abort; |
| 330 | cmdr = cmd->opcode; |
| 331 | memset(stop, 0, sizeof(struct mmc_command)); |
| 332 | |
| 333 | if (cmdr == MMC_READ_SINGLE_BLOCK || |
| 334 | cmdr == MMC_READ_MULTIPLE_BLOCK || |
| 335 | cmdr == MMC_WRITE_BLOCK || |
Ulf Hansson | 6c2c650 | 2014-12-01 16:13:39 +0100 | [diff] [blame] | 336 | cmdr == MMC_WRITE_MULTIPLE_BLOCK || |
| 337 | cmdr == MMC_SEND_TUNING_BLOCK || |
MÃ¥rten Lindahl | 9f0d3cc | 2021-11-03 19:27:16 +0100 | [diff] [blame] | 338 | cmdr == MMC_SEND_TUNING_BLOCK_HS200 || |
| 339 | cmdr == MMC_GEN_CMD) { |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 340 | stop->opcode = MMC_STOP_TRANSMISSION; |
| 341 | stop->arg = 0; |
| 342 | stop->flags = MMC_RSP_R1B | MMC_CMD_AC; |
| 343 | } else if (cmdr == SD_IO_RW_EXTENDED) { |
| 344 | stop->opcode = SD_IO_RW_DIRECT; |
| 345 | stop->arg |= (1 << 31) | (0 << 28) | (SDIO_CCCR_ABORT << 9) | |
| 346 | ((cmd->arg >> 28) & 0x7); |
| 347 | stop->flags = MMC_RSP_SPI_R5 | MMC_RSP_R5 | MMC_CMD_AC; |
| 348 | } else { |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | cmdr = stop->opcode | SDMMC_CMD_STOP | |
| 353 | SDMMC_CMD_RESP_CRC | SDMMC_CMD_RESP_EXP; |
| 354 | |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 355 | if (!test_bit(DW_MMC_CARD_NO_USE_HOLD, &host->slot->flags)) |
Jaehoon Chung | 8c005b4 | 2016-11-17 16:40:36 +0900 | [diff] [blame] | 356 | cmdr |= SDMMC_CMD_USE_HOLD_REG; |
| 357 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 358 | return cmdr; |
| 359 | } |
| 360 | |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 361 | static inline void dw_mci_set_cto(struct dw_mci *host) |
| 362 | { |
| 363 | unsigned int cto_clks; |
Douglas Anderson | 4c2357f | 2017-10-12 13:11:15 -0700 | [diff] [blame] | 364 | unsigned int cto_div; |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 365 | unsigned int cto_ms; |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 366 | unsigned long irqflags; |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 367 | |
| 368 | cto_clks = mci_readl(host, TMOUT) & 0xff; |
Douglas Anderson | 4c2357f | 2017-10-12 13:11:15 -0700 | [diff] [blame] | 369 | cto_div = (mci_readl(host, CLKDIV) & 0xff) * 2; |
| 370 | if (cto_div == 0) |
| 371 | cto_div = 1; |
Evgeniy Didin | c715160 | 2018-02-28 14:53:18 +0300 | [diff] [blame] | 372 | |
| 373 | cto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * cto_clks * cto_div, |
| 374 | host->bus_hz); |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 375 | |
| 376 | /* add a bit spare time */ |
| 377 | cto_ms += 10; |
| 378 | |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 379 | /* |
| 380 | * The durations we're working with are fairly short so we have to be |
| 381 | * extra careful about synchronization here. Specifically in hardware a |
| 382 | * command timeout is _at most_ 5.1 ms, so that means we expect an |
| 383 | * interrupt (either command done or timeout) to come rather quickly |
| 384 | * after the mci_writel. ...but just in case we have a long interrupt |
| 385 | * latency let's add a bit of paranoia. |
| 386 | * |
| 387 | * In general we'll assume that at least an interrupt will be asserted |
| 388 | * in hardware by the time the cto_timer runs. ...and if it hasn't |
| 389 | * been asserted in hardware by that time then we'll assume it'll never |
| 390 | * come. |
| 391 | */ |
| 392 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 393 | if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) |
| 394 | mod_timer(&host->cto_timer, |
| 395 | jiffies + msecs_to_jiffies(cto_ms) + 1); |
| 396 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 397 | } |
| 398 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 399 | static void dw_mci_start_command(struct dw_mci *host, |
| 400 | struct mmc_command *cmd, u32 cmd_flags) |
| 401 | { |
| 402 | host->cmd = cmd; |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 403 | dev_vdbg(host->dev, |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 404 | "start command: ARGR=0x%08x CMDR=0x%08x\n", |
| 405 | cmd->arg, cmd_flags); |
| 406 | |
| 407 | mci_writel(host, CMDARG, cmd->arg); |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 408 | wmb(); /* drain writebuffer */ |
Doug Anderson | 0bdbd0e | 2015-02-20 12:31:56 -0800 | [diff] [blame] | 409 | dw_mci_wait_while_busy(host, cmd_flags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 410 | |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 411 | mci_writel(host, CMD, cmd_flags | SDMMC_CMD_START); |
| 412 | |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 413 | /* response expected command only */ |
| 414 | if (cmd_flags & SDMMC_CMD_RESP_EXP) |
| 415 | dw_mci_set_cto(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 416 | } |
| 417 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 418 | static inline void send_stop_abort(struct dw_mci *host, struct mmc_data *data) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 419 | { |
Jaehoon Chung | e13c3c0 | 2016-11-17 16:40:37 +0900 | [diff] [blame] | 420 | struct mmc_command *stop = &host->stop_abort; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 421 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 422 | dw_mci_start_command(host, stop, host->stop_cmdr); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | /* DMA interface functions */ |
| 426 | static void dw_mci_stop_dma(struct dw_mci *host) |
| 427 | { |
James Hogan | 03e8cb5 | 2011-06-29 09:28:43 +0100 | [diff] [blame] | 428 | if (host->using_dma) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 429 | host->dma_ops->stop(host); |
| 430 | host->dma_ops->cleanup(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 431 | } |
Seungwon Jeon | aa50f25 | 2013-08-31 00:14:38 +0900 | [diff] [blame] | 432 | |
| 433 | /* Data transfer was stopped by the interrupt handler */ |
| 434 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 435 | } |
| 436 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 437 | static void dw_mci_dma_cleanup(struct dw_mci *host) |
| 438 | { |
| 439 | struct mmc_data *data = host->data; |
| 440 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 441 | if (data && data->host_cookie == COOKIE_MAPPED) { |
| 442 | dma_unmap_sg(host->dev, |
| 443 | data->sg, |
| 444 | data->sg_len, |
Heiner Kallweit | feeef09 | 2017-03-26 20:45:56 +0200 | [diff] [blame] | 445 | mmc_get_dma_dir(data)); |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 446 | data->host_cookie = COOKIE_UNMAPPED; |
| 447 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 448 | } |
| 449 | |
Seungwon Jeon | 5ce9d96 | 2013-08-31 00:14:33 +0900 | [diff] [blame] | 450 | static void dw_mci_idmac_reset(struct dw_mci *host) |
| 451 | { |
| 452 | u32 bmod = mci_readl(host, BMOD); |
| 453 | /* Software reset of DMA */ |
| 454 | bmod |= SDMMC_IDMAC_SWRESET; |
| 455 | mci_writel(host, BMOD, bmod); |
| 456 | } |
| 457 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 458 | static void dw_mci_idmac_stop_dma(struct dw_mci *host) |
| 459 | { |
| 460 | u32 temp; |
| 461 | |
| 462 | /* Disable and reset the IDMAC interface */ |
| 463 | temp = mci_readl(host, CTRL); |
| 464 | temp &= ~SDMMC_CTRL_USE_IDMAC; |
| 465 | temp |= SDMMC_CTRL_DMA_RESET; |
| 466 | mci_writel(host, CTRL, temp); |
| 467 | |
| 468 | /* Stop the IDMAC running */ |
| 469 | temp = mci_readl(host, BMOD); |
Jaehoon Chung | a5289a4 | 2011-02-25 11:08:13 +0900 | [diff] [blame] | 470 | temp &= ~(SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB); |
Seungwon Jeon | 5ce9d96 | 2013-08-31 00:14:33 +0900 | [diff] [blame] | 471 | temp |= SDMMC_IDMAC_SWRESET; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 472 | mci_writel(host, BMOD, temp); |
| 473 | } |
| 474 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 475 | static void dw_mci_dmac_complete_dma(void *arg) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 476 | { |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 477 | struct dw_mci *host = arg; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 478 | struct mmc_data *data = host->data; |
| 479 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 480 | dev_vdbg(host->dev, "DMA complete\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 481 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 482 | if ((host->use_dma == TRANS_MODE_EDMAC) && |
| 483 | data && (data->flags & MMC_DATA_READ)) |
| 484 | /* Invalidate cache after read */ |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 485 | dma_sync_sg_for_cpu(mmc_dev(host->slot->mmc), |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 486 | data->sg, |
| 487 | data->sg_len, |
| 488 | DMA_FROM_DEVICE); |
| 489 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 490 | host->dma_ops->cleanup(host); |
| 491 | |
| 492 | /* |
| 493 | * If the card was removed, data will be NULL. No point in trying to |
| 494 | * send the stop command or waiting for NBUSY in this case. |
| 495 | */ |
| 496 | if (data) { |
| 497 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 498 | tasklet_schedule(&host->tasklet); |
| 499 | } |
| 500 | } |
| 501 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 502 | static int dw_mci_idmac_init(struct dw_mci *host) |
| 503 | { |
Seungwon Jeon | 897b69e | 2012-09-19 13:58:31 +0800 | [diff] [blame] | 504 | int i; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 505 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 506 | if (host->dma_64bit_address == 1) { |
| 507 | struct idmac_desc_64addr *p; |
| 508 | /* Number of descriptors in the ring buffer */ |
Shawn Lin | cc190d4 | 2016-09-02 12:14:39 +0800 | [diff] [blame] | 509 | host->ring_size = |
| 510 | DESC_RING_BUF_SZ / sizeof(struct idmac_desc_64addr); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 511 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 512 | /* Forward link the descriptor list */ |
| 513 | for (i = 0, p = host->sg_cpu; i < host->ring_size - 1; |
| 514 | i++, p++) { |
| 515 | p->des6 = (host->sg_dma + |
| 516 | (sizeof(struct idmac_desc_64addr) * |
| 517 | (i + 1))) & 0xffffffff; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 518 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 519 | p->des7 = (u64)(host->sg_dma + |
| 520 | (sizeof(struct idmac_desc_64addr) * |
| 521 | (i + 1))) >> 32; |
| 522 | /* Initialize reserved and buffer size fields to "0" */ |
Evgeniy Didin | 47b7de2 | 2018-03-14 22:30:51 +0300 | [diff] [blame] | 523 | p->des0 = 0; |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 524 | p->des1 = 0; |
| 525 | p->des2 = 0; |
| 526 | p->des3 = 0; |
| 527 | } |
| 528 | |
| 529 | /* Set the last descriptor as the end-of-ring descriptor */ |
| 530 | p->des6 = host->sg_dma & 0xffffffff; |
| 531 | p->des7 = (u64)host->sg_dma >> 32; |
| 532 | p->des0 = IDMAC_DES0_ER; |
| 533 | |
| 534 | } else { |
| 535 | struct idmac_desc *p; |
| 536 | /* Number of descriptors in the ring buffer */ |
Shawn Lin | cc190d4 | 2016-09-02 12:14:39 +0800 | [diff] [blame] | 537 | host->ring_size = |
| 538 | DESC_RING_BUF_SZ / sizeof(struct idmac_desc); |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 539 | |
| 540 | /* Forward link the descriptor list */ |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 541 | for (i = 0, p = host->sg_cpu; |
| 542 | i < host->ring_size - 1; |
| 543 | i++, p++) { |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 544 | p->des3 = cpu_to_le32(host->sg_dma + |
| 545 | (sizeof(struct idmac_desc) * (i + 1))); |
Evgeniy Didin | 47b7de2 | 2018-03-14 22:30:51 +0300 | [diff] [blame] | 546 | p->des0 = 0; |
Zhangfei Gao | 4b24472 | 2015-04-30 22:16:28 +0800 | [diff] [blame] | 547 | p->des1 = 0; |
| 548 | } |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 549 | |
| 550 | /* Set the last descriptor as the end-of-ring descriptor */ |
Ben Dooks | 6687c42 | 2015-03-25 11:27:51 +0000 | [diff] [blame] | 551 | p->des3 = cpu_to_le32(host->sg_dma); |
| 552 | p->des0 = cpu_to_le32(IDMAC_DES0_ER); |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 553 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 554 | |
Seungwon Jeon | 5ce9d96 | 2013-08-31 00:14:33 +0900 | [diff] [blame] | 555 | dw_mci_idmac_reset(host); |
Seungwon Jeon | 141a712 | 2012-05-22 13:01:03 +0900 | [diff] [blame] | 556 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 557 | if (host->dma_64bit_address == 1) { |
| 558 | /* Mask out interrupts - get Tx & Rx complete only */ |
| 559 | mci_writel(host, IDSTS64, IDMAC_INT_CLR); |
| 560 | mci_writel(host, IDINTEN64, SDMMC_IDMAC_INT_NI | |
| 561 | SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 562 | |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 563 | /* Set the descriptor base address */ |
| 564 | mci_writel(host, DBADDRL, host->sg_dma & 0xffffffff); |
| 565 | mci_writel(host, DBADDRU, (u64)host->sg_dma >> 32); |
| 566 | |
| 567 | } else { |
| 568 | /* Mask out interrupts - get Tx & Rx complete only */ |
| 569 | mci_writel(host, IDSTS, IDMAC_INT_CLR); |
| 570 | mci_writel(host, IDINTEN, SDMMC_IDMAC_INT_NI | |
| 571 | SDMMC_IDMAC_INT_RI | SDMMC_IDMAC_INT_TI); |
| 572 | |
| 573 | /* Set the descriptor base address */ |
| 574 | mci_writel(host, DBADDR, host->sg_dma); |
| 575 | } |
| 576 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 580 | static inline int dw_mci_prepare_desc64(struct dw_mci *host, |
| 581 | struct mmc_data *data, |
| 582 | unsigned int sg_len) |
| 583 | { |
| 584 | unsigned int desc_len; |
| 585 | struct idmac_desc_64addr *desc_first, *desc_last, *desc; |
Shawn Lin | b6d2d81 | 2017-02-17 10:56:39 +0800 | [diff] [blame] | 586 | u32 val; |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 587 | int i; |
| 588 | |
| 589 | desc_first = desc_last = desc = host->sg_cpu; |
| 590 | |
| 591 | for (i = 0; i < sg_len; i++) { |
| 592 | unsigned int length = sg_dma_len(&data->sg[i]); |
| 593 | |
| 594 | u64 mem_addr = sg_dma_address(&data->sg[i]); |
| 595 | |
| 596 | for ( ; length ; desc++) { |
| 597 | desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ? |
| 598 | length : DW_MCI_DESC_DATA_LENGTH; |
| 599 | |
| 600 | length -= desc_len; |
| 601 | |
| 602 | /* |
| 603 | * Wait for the former clear OWN bit operation |
| 604 | * of IDMAC to make sure that this descriptor |
| 605 | * isn't still owned by IDMAC as IDMAC's write |
| 606 | * ops and CPU's read ops are asynchronous. |
| 607 | */ |
Shawn Lin | b6d2d81 | 2017-02-17 10:56:39 +0800 | [diff] [blame] | 608 | if (readl_poll_timeout_atomic(&desc->des0, val, |
| 609 | !(val & IDMAC_DES0_OWN), |
| 610 | 10, 100 * USEC_PER_MSEC)) |
| 611 | goto err_own_bit; |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 612 | |
| 613 | /* |
| 614 | * Set the OWN bit and disable interrupts |
| 615 | * for this descriptor |
| 616 | */ |
| 617 | desc->des0 = IDMAC_DES0_OWN | IDMAC_DES0_DIC | |
| 618 | IDMAC_DES0_CH; |
| 619 | |
| 620 | /* Buffer length */ |
| 621 | IDMAC_64ADDR_SET_BUFFER1_SIZE(desc, desc_len); |
| 622 | |
| 623 | /* Physical address to DMA to/from */ |
| 624 | desc->des4 = mem_addr & 0xffffffff; |
| 625 | desc->des5 = mem_addr >> 32; |
| 626 | |
| 627 | /* Update physical address for the next desc */ |
| 628 | mem_addr += desc_len; |
| 629 | |
| 630 | /* Save pointer to the last descriptor */ |
| 631 | desc_last = desc; |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | /* Set first descriptor */ |
| 636 | desc_first->des0 |= IDMAC_DES0_FD; |
| 637 | |
| 638 | /* Set last descriptor */ |
| 639 | desc_last->des0 &= ~(IDMAC_DES0_CH | IDMAC_DES0_DIC); |
| 640 | desc_last->des0 |= IDMAC_DES0_LD; |
| 641 | |
| 642 | return 0; |
| 643 | err_own_bit: |
| 644 | /* restore the descriptor chain as it's polluted */ |
Colin Ian King | 26be9d7 | 2016-11-16 18:55:01 +0000 | [diff] [blame] | 645 | dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n"); |
Shawn Lin | cc190d4 | 2016-09-02 12:14:39 +0800 | [diff] [blame] | 646 | memset(host->sg_cpu, 0, DESC_RING_BUF_SZ); |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 647 | dw_mci_idmac_init(host); |
| 648 | return -EINVAL; |
| 649 | } |
| 650 | |
| 651 | |
| 652 | static inline int dw_mci_prepare_desc32(struct dw_mci *host, |
| 653 | struct mmc_data *data, |
| 654 | unsigned int sg_len) |
| 655 | { |
| 656 | unsigned int desc_len; |
| 657 | struct idmac_desc *desc_first, *desc_last, *desc; |
Shawn Lin | b6d2d81 | 2017-02-17 10:56:39 +0800 | [diff] [blame] | 658 | u32 val; |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 659 | int i; |
| 660 | |
| 661 | desc_first = desc_last = desc = host->sg_cpu; |
| 662 | |
| 663 | for (i = 0; i < sg_len; i++) { |
| 664 | unsigned int length = sg_dma_len(&data->sg[i]); |
| 665 | |
| 666 | u32 mem_addr = sg_dma_address(&data->sg[i]); |
| 667 | |
| 668 | for ( ; length ; desc++) { |
| 669 | desc_len = (length <= DW_MCI_DESC_DATA_LENGTH) ? |
| 670 | length : DW_MCI_DESC_DATA_LENGTH; |
| 671 | |
| 672 | length -= desc_len; |
| 673 | |
| 674 | /* |
| 675 | * Wait for the former clear OWN bit operation |
| 676 | * of IDMAC to make sure that this descriptor |
| 677 | * isn't still owned by IDMAC as IDMAC's write |
| 678 | * ops and CPU's read ops are asynchronous. |
| 679 | */ |
Shawn Lin | b6d2d81 | 2017-02-17 10:56:39 +0800 | [diff] [blame] | 680 | if (readl_poll_timeout_atomic(&desc->des0, val, |
| 681 | IDMAC_OWN_CLR64(val), |
| 682 | 10, |
| 683 | 100 * USEC_PER_MSEC)) |
| 684 | goto err_own_bit; |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 685 | |
| 686 | /* |
| 687 | * Set the OWN bit and disable interrupts |
| 688 | * for this descriptor |
| 689 | */ |
| 690 | desc->des0 = cpu_to_le32(IDMAC_DES0_OWN | |
| 691 | IDMAC_DES0_DIC | |
| 692 | IDMAC_DES0_CH); |
| 693 | |
| 694 | /* Buffer length */ |
| 695 | IDMAC_SET_BUFFER1_SIZE(desc, desc_len); |
| 696 | |
| 697 | /* Physical address to DMA to/from */ |
| 698 | desc->des2 = cpu_to_le32(mem_addr); |
| 699 | |
| 700 | /* Update physical address for the next desc */ |
| 701 | mem_addr += desc_len; |
| 702 | |
| 703 | /* Save pointer to the last descriptor */ |
| 704 | desc_last = desc; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /* Set first descriptor */ |
| 709 | desc_first->des0 |= cpu_to_le32(IDMAC_DES0_FD); |
| 710 | |
| 711 | /* Set last descriptor */ |
| 712 | desc_last->des0 &= cpu_to_le32(~(IDMAC_DES0_CH | |
| 713 | IDMAC_DES0_DIC)); |
| 714 | desc_last->des0 |= cpu_to_le32(IDMAC_DES0_LD); |
| 715 | |
| 716 | return 0; |
| 717 | err_own_bit: |
| 718 | /* restore the descriptor chain as it's polluted */ |
Colin Ian King | 26be9d7 | 2016-11-16 18:55:01 +0000 | [diff] [blame] | 719 | dev_dbg(host->dev, "descriptor is still owned by IDMAC.\n"); |
Shawn Lin | cc190d4 | 2016-09-02 12:14:39 +0800 | [diff] [blame] | 720 | memset(host->sg_cpu, 0, DESC_RING_BUF_SZ); |
Shawn Lin | 3b2a067 | 2016-09-02 12:14:37 +0800 | [diff] [blame] | 721 | dw_mci_idmac_init(host); |
| 722 | return -EINVAL; |
| 723 | } |
| 724 | |
| 725 | static int dw_mci_idmac_start_dma(struct dw_mci *host, unsigned int sg_len) |
| 726 | { |
| 727 | u32 temp; |
| 728 | int ret; |
| 729 | |
| 730 | if (host->dma_64bit_address == 1) |
| 731 | ret = dw_mci_prepare_desc64(host, host->data, sg_len); |
| 732 | else |
| 733 | ret = dw_mci_prepare_desc32(host, host->data, sg_len); |
| 734 | |
| 735 | if (ret) |
| 736 | goto out; |
| 737 | |
| 738 | /* drain writebuffer */ |
| 739 | wmb(); |
| 740 | |
| 741 | /* Make sure to reset DMA in case we did PIO before this */ |
| 742 | dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET); |
| 743 | dw_mci_idmac_reset(host); |
| 744 | |
| 745 | /* Select IDMAC interface */ |
| 746 | temp = mci_readl(host, CTRL); |
| 747 | temp |= SDMMC_CTRL_USE_IDMAC; |
| 748 | mci_writel(host, CTRL, temp); |
| 749 | |
| 750 | /* drain writebuffer */ |
| 751 | wmb(); |
| 752 | |
| 753 | /* Enable the IDMAC */ |
| 754 | temp = mci_readl(host, BMOD); |
| 755 | temp |= SDMMC_IDMAC_ENABLE | SDMMC_IDMAC_FB; |
| 756 | mci_writel(host, BMOD, temp); |
| 757 | |
| 758 | /* Start it running */ |
| 759 | mci_writel(host, PLDMND, 1); |
| 760 | |
| 761 | out: |
| 762 | return ret; |
| 763 | } |
| 764 | |
Arnd Bergmann | 8e2b36e | 2012-11-06 22:55:31 +0100 | [diff] [blame] | 765 | static const struct dw_mci_dma_ops dw_mci_idmac_ops = { |
Seungwon Jeon | 885c3e8 | 2012-02-20 11:01:43 +0900 | [diff] [blame] | 766 | .init = dw_mci_idmac_init, |
| 767 | .start = dw_mci_idmac_start_dma, |
| 768 | .stop = dw_mci_idmac_stop_dma, |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 769 | .complete = dw_mci_dmac_complete_dma, |
Seungwon Jeon | 885c3e8 | 2012-02-20 11:01:43 +0900 | [diff] [blame] | 770 | .cleanup = dw_mci_dma_cleanup, |
| 771 | }; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 772 | |
| 773 | static void dw_mci_edmac_stop_dma(struct dw_mci *host) |
| 774 | { |
Shawn Lin | ab925a3 | 2016-03-09 10:34:46 +0800 | [diff] [blame] | 775 | dmaengine_terminate_async(host->dms->ch); |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | static int dw_mci_edmac_start_dma(struct dw_mci *host, |
| 779 | unsigned int sg_len) |
| 780 | { |
| 781 | struct dma_slave_config cfg; |
| 782 | struct dma_async_tx_descriptor *desc = NULL; |
| 783 | struct scatterlist *sgl = host->data->sg; |
Colin Ian King | 27d70d36 | 2017-09-03 14:39:50 +0100 | [diff] [blame] | 784 | static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256}; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 785 | u32 sg_elems = host->data->sg_len; |
| 786 | u32 fifoth_val; |
| 787 | u32 fifo_offset = host->fifo_reg - host->regs; |
| 788 | int ret = 0; |
| 789 | |
| 790 | /* Set external dma config: burst size, burst width */ |
Tony Lindgren | c3ff018 | 2021-08-10 11:16:43 +0300 | [diff] [blame] | 791 | memset(&cfg, 0, sizeof(cfg)); |
Arnd Bergmann | 260b316 | 2015-11-12 15:14:23 +0100 | [diff] [blame] | 792 | cfg.dst_addr = host->phy_regs + fifo_offset; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 793 | cfg.src_addr = cfg.dst_addr; |
| 794 | cfg.dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 795 | cfg.src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES; |
| 796 | |
| 797 | /* Match burst msize with external dma config */ |
| 798 | fifoth_val = mci_readl(host, FIFOTH); |
| 799 | cfg.dst_maxburst = mszs[(fifoth_val >> 28) & 0x7]; |
| 800 | cfg.src_maxburst = cfg.dst_maxburst; |
| 801 | |
| 802 | if (host->data->flags & MMC_DATA_WRITE) |
| 803 | cfg.direction = DMA_MEM_TO_DEV; |
| 804 | else |
| 805 | cfg.direction = DMA_DEV_TO_MEM; |
| 806 | |
| 807 | ret = dmaengine_slave_config(host->dms->ch, &cfg); |
| 808 | if (ret) { |
| 809 | dev_err(host->dev, "Failed to config edmac.\n"); |
| 810 | return -EBUSY; |
| 811 | } |
| 812 | |
| 813 | desc = dmaengine_prep_slave_sg(host->dms->ch, sgl, |
| 814 | sg_len, cfg.direction, |
| 815 | DMA_PREP_INTERRUPT | DMA_CTRL_ACK); |
| 816 | if (!desc) { |
| 817 | dev_err(host->dev, "Can't prepare slave sg.\n"); |
| 818 | return -EBUSY; |
| 819 | } |
| 820 | |
| 821 | /* Set dw_mci_dmac_complete_dma as callback */ |
| 822 | desc->callback = dw_mci_dmac_complete_dma; |
| 823 | desc->callback_param = (void *)host; |
| 824 | dmaengine_submit(desc); |
| 825 | |
| 826 | /* Flush cache before write */ |
| 827 | if (host->data->flags & MMC_DATA_WRITE) |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 828 | dma_sync_sg_for_device(mmc_dev(host->slot->mmc), sgl, |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 829 | sg_elems, DMA_TO_DEVICE); |
| 830 | |
| 831 | dma_async_issue_pending(host->dms->ch); |
| 832 | |
| 833 | return 0; |
| 834 | } |
| 835 | |
| 836 | static int dw_mci_edmac_init(struct dw_mci *host) |
| 837 | { |
| 838 | /* Request external dma channel */ |
| 839 | host->dms = kzalloc(sizeof(struct dw_mci_dma_slave), GFP_KERNEL); |
| 840 | if (!host->dms) |
| 841 | return -ENOMEM; |
| 842 | |
Peter Ujfalusi | c1fce22 | 2019-12-17 13:26:56 +0200 | [diff] [blame] | 843 | host->dms->ch = dma_request_chan(host->dev, "rx-tx"); |
| 844 | if (IS_ERR(host->dms->ch)) { |
| 845 | int ret = PTR_ERR(host->dms->ch); |
| 846 | |
Dan Carpenter | 4539d36 | 2015-10-22 22:53:46 +0300 | [diff] [blame] | 847 | dev_err(host->dev, "Failed to get external DMA channel.\n"); |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 848 | kfree(host->dms); |
| 849 | host->dms = NULL; |
Peter Ujfalusi | c1fce22 | 2019-12-17 13:26:56 +0200 | [diff] [blame] | 850 | return ret; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 851 | } |
| 852 | |
| 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static void dw_mci_edmac_exit(struct dw_mci *host) |
| 857 | { |
| 858 | if (host->dms) { |
| 859 | if (host->dms->ch) { |
| 860 | dma_release_channel(host->dms->ch); |
| 861 | host->dms->ch = NULL; |
| 862 | } |
| 863 | kfree(host->dms); |
| 864 | host->dms = NULL; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | static const struct dw_mci_dma_ops dw_mci_edmac_ops = { |
| 869 | .init = dw_mci_edmac_init, |
| 870 | .exit = dw_mci_edmac_exit, |
| 871 | .start = dw_mci_edmac_start_dma, |
| 872 | .stop = dw_mci_edmac_stop_dma, |
| 873 | .complete = dw_mci_dmac_complete_dma, |
| 874 | .cleanup = dw_mci_dma_cleanup, |
| 875 | }; |
Seungwon Jeon | 885c3e8 | 2012-02-20 11:01:43 +0900 | [diff] [blame] | 876 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 877 | static int dw_mci_pre_dma_transfer(struct dw_mci *host, |
| 878 | struct mmc_data *data, |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 879 | int cookie) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 880 | { |
| 881 | struct scatterlist *sg; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 882 | unsigned int i, sg_len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 883 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 884 | if (data->host_cookie == COOKIE_PRE_MAPPED) |
| 885 | return data->sg_len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 886 | |
| 887 | /* |
| 888 | * We don't do DMA on "complex" transfers, i.e. with |
| 889 | * non-word-aligned buffers or lengths. Also, we don't bother |
| 890 | * with all the DMA setup overhead for short transfers. |
| 891 | */ |
| 892 | if (data->blocks * data->blksz < DW_MCI_DMA_THRESHOLD) |
| 893 | return -EINVAL; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 894 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 895 | if (data->blksz & 3) |
| 896 | return -EINVAL; |
| 897 | |
| 898 | for_each_sg(data->sg, sg, data->sg_len, i) { |
| 899 | if (sg->offset & 3 || sg->length & 3) |
| 900 | return -EINVAL; |
| 901 | } |
| 902 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 903 | sg_len = dma_map_sg(host->dev, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 904 | data->sg, |
| 905 | data->sg_len, |
Heiner Kallweit | feeef09 | 2017-03-26 20:45:56 +0200 | [diff] [blame] | 906 | mmc_get_dma_dir(data)); |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 907 | if (sg_len == 0) |
| 908 | return -EINVAL; |
| 909 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 910 | data->host_cookie = cookie; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 911 | |
| 912 | return sg_len; |
| 913 | } |
| 914 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 915 | static void dw_mci_pre_req(struct mmc_host *mmc, |
Linus Walleij | d3c6aac | 2016-11-23 11:02:24 +0100 | [diff] [blame] | 916 | struct mmc_request *mrq) |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 917 | { |
| 918 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 919 | struct mmc_data *data = mrq->data; |
| 920 | |
| 921 | if (!slot->host->use_dma || !data) |
| 922 | return; |
| 923 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 924 | /* This data might be unmapped at this time */ |
| 925 | data->host_cookie = COOKIE_UNMAPPED; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 926 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 927 | if (dw_mci_pre_dma_transfer(slot->host, mrq->data, |
| 928 | COOKIE_PRE_MAPPED) < 0) |
| 929 | data->host_cookie = COOKIE_UNMAPPED; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | static void dw_mci_post_req(struct mmc_host *mmc, |
| 933 | struct mmc_request *mrq, |
| 934 | int err) |
| 935 | { |
| 936 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 937 | struct mmc_data *data = mrq->data; |
| 938 | |
| 939 | if (!slot->host->use_dma || !data) |
| 940 | return; |
| 941 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 942 | if (data->host_cookie != COOKIE_UNMAPPED) |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 943 | dma_unmap_sg(slot->host->dev, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 944 | data->sg, |
| 945 | data->sg_len, |
Heiner Kallweit | feeef09 | 2017-03-26 20:45:56 +0200 | [diff] [blame] | 946 | mmc_get_dma_dir(data)); |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 947 | data->host_cookie = COOKIE_UNMAPPED; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 948 | } |
| 949 | |
Shawn Lin | 671fa14 | 2017-02-17 10:56:42 +0800 | [diff] [blame] | 950 | static int dw_mci_get_cd(struct mmc_host *mmc) |
| 951 | { |
| 952 | int present; |
| 953 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 954 | struct dw_mci *host = slot->host; |
| 955 | int gpio_cd = mmc_gpio_get_cd(mmc); |
| 956 | |
| 957 | /* Use platform get_cd function, else try onboard card detect */ |
| 958 | if (((mmc->caps & MMC_CAP_NEEDS_POLL) |
| 959 | || !mmc_card_is_removable(mmc))) { |
| 960 | present = 1; |
| 961 | |
| 962 | if (!test_bit(DW_MMC_CARD_PRESENT, &slot->flags)) { |
| 963 | if (mmc->caps & MMC_CAP_NEEDS_POLL) { |
| 964 | dev_info(&mmc->class_dev, |
| 965 | "card is polling.\n"); |
| 966 | } else { |
| 967 | dev_info(&mmc->class_dev, |
| 968 | "card is non-removable.\n"); |
| 969 | } |
| 970 | set_bit(DW_MMC_CARD_PRESENT, &slot->flags); |
| 971 | } |
| 972 | |
| 973 | return present; |
| 974 | } else if (gpio_cd >= 0) |
| 975 | present = gpio_cd; |
| 976 | else |
| 977 | present = (mci_readl(slot->host, CDETECT) & (1 << slot->id)) |
| 978 | == 0 ? 1 : 0; |
| 979 | |
| 980 | spin_lock_bh(&host->lock); |
| 981 | if (present && !test_and_set_bit(DW_MMC_CARD_PRESENT, &slot->flags)) |
| 982 | dev_dbg(&mmc->class_dev, "card is present\n"); |
| 983 | else if (!present && |
| 984 | !test_and_clear_bit(DW_MMC_CARD_PRESENT, &slot->flags)) |
| 985 | dev_dbg(&mmc->class_dev, "card is not present\n"); |
| 986 | spin_unlock_bh(&host->lock); |
| 987 | |
| 988 | return present; |
| 989 | } |
| 990 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 991 | static void dw_mci_adjust_fifoth(struct dw_mci *host, struct mmc_data *data) |
| 992 | { |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 993 | unsigned int blksz = data->blksz; |
Colin Ian King | 27d70d36 | 2017-09-03 14:39:50 +0100 | [diff] [blame] | 994 | static const u32 mszs[] = {1, 4, 8, 16, 32, 64, 128, 256}; |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 995 | u32 fifo_width = 1 << host->data_shift; |
| 996 | u32 blksz_depth = blksz / fifo_width, fifoth_val; |
| 997 | u32 msize = 0, rx_wmark = 1, tx_wmark, tx_wmark_invers; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 998 | int idx = ARRAY_SIZE(mszs) - 1; |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 999 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 1000 | /* pio should ship this scenario */ |
| 1001 | if (!host->use_dma) |
| 1002 | return; |
| 1003 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1004 | tx_wmark = (host->fifo_depth) / 2; |
| 1005 | tx_wmark_invers = host->fifo_depth - tx_wmark; |
| 1006 | |
| 1007 | /* |
| 1008 | * MSIZE is '1', |
| 1009 | * if blksz is not a multiple of the FIFO width |
| 1010 | */ |
Shawn Lin | 2075356 | 2016-09-21 10:40:25 +0800 | [diff] [blame] | 1011 | if (blksz % fifo_width) |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1012 | goto done; |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1013 | |
| 1014 | do { |
| 1015 | if (!((blksz_depth % mszs[idx]) || |
| 1016 | (tx_wmark_invers % mszs[idx]))) { |
| 1017 | msize = idx; |
| 1018 | rx_wmark = mszs[idx] - 1; |
| 1019 | break; |
| 1020 | } |
| 1021 | } while (--idx > 0); |
| 1022 | /* |
| 1023 | * If idx is '0', it won't be tried |
| 1024 | * Thus, initial values are uesed |
| 1025 | */ |
| 1026 | done: |
| 1027 | fifoth_val = SDMMC_SET_FIFOTH(msize, rx_wmark, tx_wmark); |
| 1028 | mci_writel(host, FIFOTH, fifoth_val); |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1029 | } |
| 1030 | |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1031 | static void dw_mci_ctrl_thld(struct dw_mci *host, struct mmc_data *data) |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1032 | { |
| 1033 | unsigned int blksz = data->blksz; |
| 1034 | u32 blksz_depth, fifo_depth; |
| 1035 | u16 thld_size; |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1036 | u8 enable; |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1037 | |
James Hogan | 66dfd10 | 2014-11-17 17:49:05 +0000 | [diff] [blame] | 1038 | /* |
| 1039 | * CDTHRCTL doesn't exist prior to 240A (in fact that register offset is |
| 1040 | * in the FIFO region, so we really shouldn't access it). |
| 1041 | */ |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1042 | if (host->verid < DW_MMC_240A || |
| 1043 | (host->verid < DW_MMC_280A && data->flags & MMC_DATA_WRITE)) |
James Hogan | 66dfd10 | 2014-11-17 17:49:05 +0000 | [diff] [blame] | 1044 | return; |
| 1045 | |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1046 | /* |
| 1047 | * Card write Threshold is introduced since 2.80a |
| 1048 | * It's used when HS400 mode is enabled. |
| 1049 | */ |
| 1050 | if (data->flags & MMC_DATA_WRITE && |
x00270170 | 7a6b9f4d | 2018-07-03 15:06:27 +0800 | [diff] [blame] | 1051 | host->timing != MMC_TIMING_MMC_HS400) |
| 1052 | goto disable; |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1053 | |
| 1054 | if (data->flags & MMC_DATA_WRITE) |
| 1055 | enable = SDMMC_CARD_WR_THR_EN; |
| 1056 | else |
| 1057 | enable = SDMMC_CARD_RD_THR_EN; |
| 1058 | |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1059 | if (host->timing != MMC_TIMING_MMC_HS200 && |
x00270170 | 7a6b9f4d | 2018-07-03 15:06:27 +0800 | [diff] [blame] | 1060 | host->timing != MMC_TIMING_UHS_SDR104 && |
| 1061 | host->timing != MMC_TIMING_MMC_HS400) |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1062 | goto disable; |
| 1063 | |
| 1064 | blksz_depth = blksz / (1 << host->data_shift); |
| 1065 | fifo_depth = host->fifo_depth; |
| 1066 | |
| 1067 | if (blksz_depth > fifo_depth) |
| 1068 | goto disable; |
| 1069 | |
| 1070 | /* |
| 1071 | * If (blksz_depth) >= (fifo_depth >> 1), should be 'thld_size <= blksz' |
| 1072 | * If (blksz_depth) < (fifo_depth >> 1), should be thld_size = blksz |
| 1073 | * Currently just choose blksz. |
| 1074 | */ |
| 1075 | thld_size = blksz; |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1076 | mci_writel(host, CDTHRCTL, SDMMC_SET_THLD(thld_size, enable)); |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1077 | return; |
| 1078 | |
| 1079 | disable: |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1080 | mci_writel(host, CDTHRCTL, 0); |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1081 | } |
| 1082 | |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 1083 | static int dw_mci_submit_data_dma(struct dw_mci *host, struct mmc_data *data) |
| 1084 | { |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1085 | unsigned long irqflags; |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 1086 | int sg_len; |
| 1087 | u32 temp; |
| 1088 | |
| 1089 | host->using_dma = 0; |
| 1090 | |
| 1091 | /* If we don't have a channel, we can't do DMA */ |
| 1092 | if (!host->use_dma) |
| 1093 | return -ENODEV; |
| 1094 | |
Jaehoon Chung | a4cc7eb | 2016-11-17 16:40:38 +0900 | [diff] [blame] | 1095 | sg_len = dw_mci_pre_dma_transfer(host, data, COOKIE_MAPPED); |
Seungwon Jeon | a99aa9b | 2012-04-10 09:53:32 +0900 | [diff] [blame] | 1096 | if (sg_len < 0) { |
| 1097 | host->dma_ops->stop(host); |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 1098 | return sg_len; |
Seungwon Jeon | a99aa9b | 2012-04-10 09:53:32 +0900 | [diff] [blame] | 1099 | } |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 1100 | |
James Hogan | 03e8cb5 | 2011-06-29 09:28:43 +0100 | [diff] [blame] | 1101 | host->using_dma = 1; |
| 1102 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 1103 | if (host->use_dma == TRANS_MODE_IDMAC) |
| 1104 | dev_vdbg(host->dev, |
| 1105 | "sd sg_cpu: %#lx sg_dma: %#lx sg_len: %d\n", |
| 1106 | (unsigned long)host->sg_cpu, |
| 1107 | (unsigned long)host->sg_dma, |
| 1108 | sg_len); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1109 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1110 | /* |
| 1111 | * Decide the MSIZE and RX/TX Watermark. |
| 1112 | * If current block size is same with previous size, |
| 1113 | * no need to update fifoth. |
| 1114 | */ |
| 1115 | if (host->prev_blksz != data->blksz) |
| 1116 | dw_mci_adjust_fifoth(host, data); |
| 1117 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1118 | /* Enable the DMA interface */ |
| 1119 | temp = mci_readl(host, CTRL); |
| 1120 | temp |= SDMMC_CTRL_DMA_ENABLE; |
| 1121 | mci_writel(host, CTRL, temp); |
| 1122 | |
| 1123 | /* Disable RX/TX IRQs, let DMA handle it */ |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1124 | spin_lock_irqsave(&host->irq_lock, irqflags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1125 | temp = mci_readl(host, INTMASK); |
| 1126 | temp &= ~(SDMMC_INT_RXDR | SDMMC_INT_TXDR); |
| 1127 | mci_writel(host, INTMASK, temp); |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1128 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1129 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 1130 | if (host->dma_ops->start(host, sg_len)) { |
Jaehoon Chung | 647f80a | 2016-11-21 10:51:48 +0900 | [diff] [blame] | 1131 | host->dma_ops->stop(host); |
Shawn Lin | d12d0cb | 2016-09-02 12:14:38 +0800 | [diff] [blame] | 1132 | /* We can't do DMA, try PIO for this one */ |
| 1133 | dev_dbg(host->dev, |
| 1134 | "%s: fall back to PIO mode for current transfer\n", |
| 1135 | __func__); |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 1136 | return -ENODEV; |
| 1137 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
| 1142 | static void dw_mci_submit_data(struct dw_mci *host, struct mmc_data *data) |
| 1143 | { |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1144 | unsigned long irqflags; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 1145 | int flags = SG_MITER_ATOMIC; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1146 | u32 temp; |
| 1147 | |
| 1148 | data->error = -EINPROGRESS; |
| 1149 | |
| 1150 | WARN_ON(host->data); |
| 1151 | host->sg = NULL; |
| 1152 | host->data = data; |
| 1153 | |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1154 | if (data->flags & MMC_DATA_READ) |
James Hogan | 55c5efbc | 2011-06-29 09:29:58 +0100 | [diff] [blame] | 1155 | host->dir_status = DW_MCI_RECV_STATUS; |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1156 | else |
James Hogan | 55c5efbc | 2011-06-29 09:29:58 +0100 | [diff] [blame] | 1157 | host->dir_status = DW_MCI_SEND_STATUS; |
Jaehoon Chung | 7e4bf1b | 2016-06-21 14:35:38 +0900 | [diff] [blame] | 1158 | |
| 1159 | dw_mci_ctrl_thld(host, data); |
James Hogan | 55c5efbc | 2011-06-29 09:29:58 +0100 | [diff] [blame] | 1160 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1161 | if (dw_mci_submit_data_dma(host, data)) { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 1162 | if (host->data->flags & MMC_DATA_READ) |
| 1163 | flags |= SG_MITER_TO_SG; |
| 1164 | else |
| 1165 | flags |= SG_MITER_FROM_SG; |
| 1166 | |
| 1167 | sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1168 | host->sg = data->sg; |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 1169 | host->part_buf_start = 0; |
| 1170 | host->part_buf_count = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1171 | |
James Hogan | b40af3a | 2011-06-24 13:54:06 +0100 | [diff] [blame] | 1172 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR | SDMMC_INT_RXDR); |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1173 | |
| 1174 | spin_lock_irqsave(&host->irq_lock, irqflags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1175 | temp = mci_readl(host, INTMASK); |
| 1176 | temp |= SDMMC_INT_TXDR | SDMMC_INT_RXDR; |
| 1177 | mci_writel(host, INTMASK, temp); |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1178 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1179 | |
| 1180 | temp = mci_readl(host, CTRL); |
| 1181 | temp &= ~SDMMC_CTRL_DMA_ENABLE; |
| 1182 | mci_writel(host, CTRL, temp); |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1183 | |
| 1184 | /* |
Jun Nie | d6fced8 | 2017-01-11 15:37:26 +0900 | [diff] [blame] | 1185 | * Use the initial fifoth_val for PIO mode. If wm_algined |
| 1186 | * is set, we set watermark same as data size. |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1187 | * If next issued data may be transfered by DMA mode, |
| 1188 | * prev_blksz should be invalidated. |
| 1189 | */ |
Jun Nie | d6fced8 | 2017-01-11 15:37:26 +0900 | [diff] [blame] | 1190 | if (host->wm_aligned) |
| 1191 | dw_mci_adjust_fifoth(host, data); |
| 1192 | else |
| 1193 | mci_writel(host, FIFOTH, host->fifoth_val); |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 1194 | host->prev_blksz = 0; |
| 1195 | } else { |
| 1196 | /* |
| 1197 | * Keep the current block size. |
| 1198 | * It will be used to decide whether to update |
| 1199 | * fifoth register next time. |
| 1200 | */ |
| 1201 | host->prev_blksz = data->blksz; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Abhilash Kesavan | ab26912 | 2012-11-19 10:26:21 +0530 | [diff] [blame] | 1205 | static void dw_mci_setup_bus(struct dw_mci_slot *slot, bool force_clkinit) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1206 | { |
| 1207 | struct dw_mci *host = slot->host; |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1208 | unsigned int clock = slot->clock; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1209 | u32 div; |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1210 | u32 clk_en_a; |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1211 | u32 sdmmc_cmd_bits = SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT; |
| 1212 | |
| 1213 | /* We must continue to set bit 28 in CMD until the change is complete */ |
| 1214 | if (host->state == STATE_WAITING_CMD11_DONE) |
| 1215 | sdmmc_cmd_bits |= SDMMC_CMD_VOLT_SWITCH; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1216 | |
Shawn Lin | ff17898 | 2018-03-26 17:26:25 +0800 | [diff] [blame] | 1217 | slot->mmc->actual_clock = 0; |
| 1218 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1219 | if (!clock) { |
| 1220 | mci_writel(host, CLKENA, 0); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1221 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1222 | } else if (clock != host->current_speed || force_clkinit) { |
| 1223 | div = host->bus_hz / clock; |
| 1224 | if (host->bus_hz % clock && host->bus_hz > clock) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1225 | /* |
| 1226 | * move the + 1 after the divide to prevent |
| 1227 | * over-clocking the card. |
| 1228 | */ |
Seungwon Jeon | e419990 | 2012-05-22 13:01:21 +0900 | [diff] [blame] | 1229 | div += 1; |
| 1230 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1231 | div = (host->bus_hz != clock) ? DIV_ROUND_UP(div, 2) : 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1232 | |
Jaehoon Chung | e6cd7a8 | 2016-11-24 20:04:42 +0900 | [diff] [blame] | 1233 | if ((clock != slot->__clk_old && |
| 1234 | !test_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags)) || |
| 1235 | force_clkinit) { |
Shawn Lin | ce69e2f | 2017-01-17 09:22:55 +0800 | [diff] [blame] | 1236 | /* Silent the verbose log if calling from PM context */ |
| 1237 | if (!force_clkinit) |
| 1238 | dev_info(&slot->mmc->class_dev, |
| 1239 | "Bus speed (slot %d) = %dHz (slot req %dHz, actual %dHZ div = %d)\n", |
| 1240 | slot->id, host->bus_hz, clock, |
| 1241 | div ? ((host->bus_hz / div) >> 1) : |
| 1242 | host->bus_hz, div); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1243 | |
Jaehoon Chung | e6cd7a8 | 2016-11-24 20:04:42 +0900 | [diff] [blame] | 1244 | /* |
| 1245 | * If card is polling, display the message only |
| 1246 | * one time at boot time. |
| 1247 | */ |
| 1248 | if (slot->mmc->caps & MMC_CAP_NEEDS_POLL && |
| 1249 | slot->mmc->f_min == clock) |
| 1250 | set_bit(DW_MMC_CARD_NEEDS_POLL, &slot->flags); |
| 1251 | } |
| 1252 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1253 | /* disable clock */ |
| 1254 | mci_writel(host, CLKENA, 0); |
| 1255 | mci_writel(host, CLKSRC, 0); |
| 1256 | |
| 1257 | /* inform CIU */ |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1258 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1259 | |
| 1260 | /* set clock to desired speed */ |
| 1261 | mci_writel(host, CLKDIV, div); |
| 1262 | |
| 1263 | /* inform CIU */ |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1264 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1265 | |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1266 | /* enable clock; only low power if no SDIO */ |
| 1267 | clk_en_a = SDMMC_CLKEN_ENABLE << slot->id; |
Doug Anderson | b24c8b2 | 2014-12-02 15:42:46 -0800 | [diff] [blame] | 1268 | if (!test_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags)) |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1269 | clk_en_a |= SDMMC_CLKEN_LOW_PWR << slot->id; |
| 1270 | mci_writel(host, CLKENA, clk_en_a); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1271 | |
| 1272 | /* inform CIU */ |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1273 | mci_send_cmd(slot, sdmmc_cmd_bits, 0); |
Jaehoon Chung | 005d675 | 2016-09-22 14:12:00 +0900 | [diff] [blame] | 1274 | |
| 1275 | /* keep the last clock value that was requested from core */ |
| 1276 | slot->__clk_old = clock; |
Shawn Lin | ff17898 | 2018-03-26 17:26:25 +0800 | [diff] [blame] | 1277 | slot->mmc->actual_clock = div ? ((host->bus_hz / div) >> 1) : |
| 1278 | host->bus_hz; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1279 | } |
| 1280 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1281 | host->current_speed = clock; |
| 1282 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1283 | /* Set the current slot bus width */ |
Seungwon Jeon | 1d56c45 | 2011-06-20 17:23:53 +0900 | [diff] [blame] | 1284 | mci_writel(host, CTYPE, (slot->ctype << slot->id)); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1285 | } |
| 1286 | |
MÃ¥rten Lindahl | 6a8c201 | 2021-11-19 16:53:37 +0100 | [diff] [blame] | 1287 | static void dw_mci_set_data_timeout(struct dw_mci *host, |
| 1288 | unsigned int timeout_ns) |
| 1289 | { |
MÃ¥rten Lindahl | 25d5417 | 2021-12-20 12:30:25 +0100 | [diff] [blame] | 1290 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
MÃ¥rten Lindahl | 6a8c201 | 2021-11-19 16:53:37 +0100 | [diff] [blame] | 1291 | u32 clk_div, tmout; |
| 1292 | u64 tmp; |
| 1293 | |
MÃ¥rten Lindahl | 25d5417 | 2021-12-20 12:30:25 +0100 | [diff] [blame] | 1294 | if (drv_data && drv_data->set_data_timeout) |
| 1295 | return drv_data->set_data_timeout(host, timeout_ns); |
| 1296 | |
MÃ¥rten Lindahl | 6a8c201 | 2021-11-19 16:53:37 +0100 | [diff] [blame] | 1297 | clk_div = (mci_readl(host, CLKDIV) & 0xFF) * 2; |
| 1298 | if (clk_div == 0) |
| 1299 | clk_div = 1; |
| 1300 | |
| 1301 | tmp = DIV_ROUND_UP_ULL((u64)timeout_ns * host->bus_hz, NSEC_PER_SEC); |
| 1302 | tmp = DIV_ROUND_UP_ULL(tmp, clk_div); |
| 1303 | |
| 1304 | /* TMOUT[7:0] (RESPONSE_TIMEOUT) */ |
| 1305 | tmout = 0xFF; /* Set maximum */ |
| 1306 | |
| 1307 | /* TMOUT[31:8] (DATA_TIMEOUT) */ |
| 1308 | if (!tmp || tmp > 0xFFFFFF) |
| 1309 | tmout |= (0xFFFFFF << 8); |
| 1310 | else |
| 1311 | tmout |= (tmp & 0xFFFFFF) << 8; |
| 1312 | |
| 1313 | mci_writel(host, TMOUT, tmout); |
Dan Carpenter | ebc4dcf | 2021-12-17 18:03:48 +0300 | [diff] [blame] | 1314 | dev_dbg(host->dev, "timeout_ns: %u => TMOUT[31:8]: %#08x", |
MÃ¥rten Lindahl | 6a8c201 | 2021-11-19 16:53:37 +0100 | [diff] [blame] | 1315 | timeout_ns, tmout >> 8); |
| 1316 | } |
| 1317 | |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 1318 | static void __dw_mci_start_request(struct dw_mci *host, |
| 1319 | struct dw_mci_slot *slot, |
| 1320 | struct mmc_command *cmd) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1321 | { |
| 1322 | struct mmc_request *mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1323 | struct mmc_data *data; |
| 1324 | u32 cmdflags; |
| 1325 | |
| 1326 | mrq = slot->mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1327 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1328 | host->mrq = mrq; |
| 1329 | |
| 1330 | host->pending_events = 0; |
| 1331 | host->completed_events = 0; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1332 | host->cmd_status = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1333 | host->data_status = 0; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1334 | host->dir_status = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1335 | |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 1336 | data = cmd->data; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1337 | if (data) { |
MÃ¥rten Lindahl | 6a8c201 | 2021-11-19 16:53:37 +0100 | [diff] [blame] | 1338 | dw_mci_set_data_timeout(host, data->timeout_ns); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1339 | mci_writel(host, BYTCNT, data->blksz*data->blocks); |
| 1340 | mci_writel(host, BLKSIZ, data->blksz); |
| 1341 | } |
| 1342 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1343 | cmdflags = dw_mci_prepare_command(slot->mmc, cmd); |
| 1344 | |
| 1345 | /* this is the first command, send the initialization clock */ |
| 1346 | if (test_and_clear_bit(DW_MMC_CARD_NEED_INIT, &slot->flags)) |
| 1347 | cmdflags |= SDMMC_CMD_INIT; |
| 1348 | |
| 1349 | if (data) { |
| 1350 | dw_mci_submit_data(host, data); |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 1351 | wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | dw_mci_start_command(host, cmd, cmdflags); |
| 1355 | |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 1356 | if (cmd->opcode == SD_SWITCH_VOLTAGE) { |
Doug Anderson | 49ba030 | 2015-04-03 11:13:07 -0700 | [diff] [blame] | 1357 | unsigned long irqflags; |
| 1358 | |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 1359 | /* |
Doug Anderson | 8886a6f | 2015-04-03 11:13:05 -0700 | [diff] [blame] | 1360 | * Databook says to fail after 2ms w/ no response, but evidence |
| 1361 | * shows that sometimes the cmd11 interrupt takes over 130ms. |
| 1362 | * We'll set to 500ms, plus an extra jiffy just in case jiffies |
| 1363 | * is just about to roll over. |
Doug Anderson | 49ba030 | 2015-04-03 11:13:07 -0700 | [diff] [blame] | 1364 | * |
| 1365 | * We do this whole thing under spinlock and only if the |
| 1366 | * command hasn't already completed (indicating the the irq |
| 1367 | * already ran so we don't want the timeout). |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 1368 | */ |
Doug Anderson | 49ba030 | 2015-04-03 11:13:07 -0700 | [diff] [blame] | 1369 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 1370 | if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) |
| 1371 | mod_timer(&host->cmd11_timer, |
| 1372 | jiffies + msecs_to_jiffies(500) + 1); |
| 1373 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Jaehoon Chung | e13c3c0 | 2016-11-17 16:40:37 +0900 | [diff] [blame] | 1376 | host->stop_cmdr = dw_mci_prep_stop_abort(host, cmd); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1377 | } |
| 1378 | |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 1379 | static void dw_mci_start_request(struct dw_mci *host, |
| 1380 | struct dw_mci_slot *slot) |
| 1381 | { |
| 1382 | struct mmc_request *mrq = slot->mrq; |
| 1383 | struct mmc_command *cmd; |
| 1384 | |
| 1385 | cmd = mrq->sbc ? mrq->sbc : mrq->cmd; |
| 1386 | __dw_mci_start_request(host, slot, cmd); |
| 1387 | } |
| 1388 | |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1389 | /* must be called with host->lock held */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1390 | static void dw_mci_queue_request(struct dw_mci *host, struct dw_mci_slot *slot, |
| 1391 | struct mmc_request *mrq) |
| 1392 | { |
| 1393 | dev_vdbg(&slot->mmc->class_dev, "queue request: state=%d\n", |
| 1394 | host->state); |
| 1395 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1396 | slot->mrq = mrq; |
| 1397 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1398 | if (host->state == STATE_WAITING_CMD11_DONE) { |
| 1399 | dev_warn(&slot->mmc->class_dev, |
| 1400 | "Voltage change didn't complete\n"); |
| 1401 | /* |
| 1402 | * this case isn't expected to happen, so we can |
| 1403 | * either crash here or just try to continue on |
| 1404 | * in the closest possible state |
| 1405 | */ |
| 1406 | host->state = STATE_IDLE; |
| 1407 | } |
| 1408 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1409 | if (host->state == STATE_IDLE) { |
| 1410 | host->state = STATE_SENDING_CMD; |
| 1411 | dw_mci_start_request(host, slot); |
| 1412 | } else { |
| 1413 | list_add_tail(&slot->queue_node, &host->queue); |
| 1414 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1415 | } |
| 1416 | |
| 1417 | static void dw_mci_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1418 | { |
| 1419 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1420 | struct dw_mci *host = slot->host; |
| 1421 | |
| 1422 | WARN_ON(slot->mrq); |
| 1423 | |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1424 | /* |
| 1425 | * The check for card presence and queueing of the request must be |
| 1426 | * atomic, otherwise the card could be removed in between and the |
| 1427 | * request wouldn't fail until another card was inserted. |
| 1428 | */ |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1429 | |
Shawn Lin | 56f6911 | 2016-05-27 14:37:05 +0800 | [diff] [blame] | 1430 | if (!dw_mci_get_cd(mmc)) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1431 | mrq->cmd->error = -ENOMEDIUM; |
| 1432 | mmc_request_done(mmc, mrq); |
| 1433 | return; |
| 1434 | } |
| 1435 | |
Shawn Lin | 56f6911 | 2016-05-27 14:37:05 +0800 | [diff] [blame] | 1436 | spin_lock_bh(&host->lock); |
| 1437 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1438 | dw_mci_queue_request(host, slot, mrq); |
James Hogan | 7456caa | 2011-06-24 13:55:10 +0100 | [diff] [blame] | 1439 | |
| 1440 | spin_unlock_bh(&host->lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | static void dw_mci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1444 | { |
| 1445 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 1446 | const struct dw_mci_drv_data *drv_data = slot->host->drv_data; |
Jaehoon Chung | 41babf7 | 2011-02-24 13:46:11 +0900 | [diff] [blame] | 1447 | u32 regs; |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1448 | int ret; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1449 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1450 | switch (ios->bus_width) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1451 | case MMC_BUS_WIDTH_4: |
| 1452 | slot->ctype = SDMMC_CTYPE_4BIT; |
| 1453 | break; |
Jaehoon Chung | c9b2a06 | 2011-02-17 16:12:38 +0900 | [diff] [blame] | 1454 | case MMC_BUS_WIDTH_8: |
| 1455 | slot->ctype = SDMMC_CTYPE_8BIT; |
| 1456 | break; |
Jaehoon Chung | b2f7cb4 | 2012-11-08 17:35:31 +0900 | [diff] [blame] | 1457 | default: |
| 1458 | /* set default 1 bit mode */ |
| 1459 | slot->ctype = SDMMC_CTYPE_1BIT; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1460 | } |
| 1461 | |
Seungwon Jeon | 3f51429 | 2012-01-02 16:00:02 +0900 | [diff] [blame] | 1462 | regs = mci_readl(slot->host, UHS_REG); |
| 1463 | |
Jaehoon Chung | 41babf7 | 2011-02-24 13:46:11 +0900 | [diff] [blame] | 1464 | /* DDR mode set */ |
Seungwon Jeon | 8011313 | 2015-01-29 08:11:57 +0530 | [diff] [blame] | 1465 | if (ios->timing == MMC_TIMING_MMC_DDR52 || |
Jaehoon Chung | 7cc8d58 | 2015-10-21 19:49:42 +0900 | [diff] [blame] | 1466 | ios->timing == MMC_TIMING_UHS_DDR50 || |
Seungwon Jeon | 8011313 | 2015-01-29 08:11:57 +0530 | [diff] [blame] | 1467 | ios->timing == MMC_TIMING_MMC_HS400) |
Hyeonsu Kim | c69042a | 2013-02-22 09:32:46 +0900 | [diff] [blame] | 1468 | regs |= ((0x1 << slot->id) << 16); |
Seungwon Jeon | 3f51429 | 2012-01-02 16:00:02 +0900 | [diff] [blame] | 1469 | else |
Hyeonsu Kim | c69042a | 2013-02-22 09:32:46 +0900 | [diff] [blame] | 1470 | regs &= ~((0x1 << slot->id) << 16); |
Seungwon Jeon | 3f51429 | 2012-01-02 16:00:02 +0900 | [diff] [blame] | 1471 | |
| 1472 | mci_writel(slot->host, UHS_REG, regs); |
Seungwon Jeon | f1d2736 | 2013-08-31 00:13:55 +0900 | [diff] [blame] | 1473 | slot->host->timing = ios->timing; |
Jaehoon Chung | 41babf7 | 2011-02-24 13:46:11 +0900 | [diff] [blame] | 1474 | |
Doug Anderson | fdf492a | 2013-08-31 00:11:43 +0900 | [diff] [blame] | 1475 | /* |
| 1476 | * Use mirror of ios->clock to prevent race with mmc |
| 1477 | * core ios update when finding the minimum. |
| 1478 | */ |
| 1479 | slot->clock = ios->clock; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1480 | |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 1481 | if (drv_data && drv_data->set_ios) |
| 1482 | drv_data->set_ios(slot->host, ios); |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 1483 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1484 | switch (ios->power_mode) { |
| 1485 | case MMC_POWER_UP: |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1486 | if (!IS_ERR(mmc->supply.vmmc)) { |
| 1487 | ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, |
| 1488 | ios->vdd); |
| 1489 | if (ret) { |
| 1490 | dev_err(slot->host->dev, |
| 1491 | "failed to enable vmmc regulator\n"); |
| 1492 | /*return, if failed turn on vmmc*/ |
| 1493 | return; |
| 1494 | } |
| 1495 | } |
Doug Anderson | 29d0d16 | 2015-01-13 15:58:44 -0800 | [diff] [blame] | 1496 | set_bit(DW_MMC_CARD_NEED_INIT, &slot->flags); |
| 1497 | regs = mci_readl(slot->host, PWREN); |
| 1498 | regs |= (1 << slot->id); |
| 1499 | mci_writel(slot->host, PWREN, regs); |
| 1500 | break; |
| 1501 | case MMC_POWER_ON: |
Doug Anderson | d1f1dd8 | 2015-02-20 10:57:19 -0800 | [diff] [blame] | 1502 | if (!slot->host->vqmmc_enabled) { |
| 1503 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 1504 | ret = regulator_enable(mmc->supply.vqmmc); |
| 1505 | if (ret < 0) |
| 1506 | dev_err(slot->host->dev, |
| 1507 | "failed to enable vqmmc\n"); |
| 1508 | else |
| 1509 | slot->host->vqmmc_enabled = true; |
| 1510 | |
| 1511 | } else { |
| 1512 | /* Keep track so we don't reset again */ |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1513 | slot->host->vqmmc_enabled = true; |
Doug Anderson | d1f1dd8 | 2015-02-20 10:57:19 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
| 1516 | /* Reset our state machine after powering on */ |
| 1517 | dw_mci_ctrl_reset(slot->host, |
| 1518 | SDMMC_CTRL_ALL_RESET_FLAGS); |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1519 | } |
Doug Anderson | 655babb | 2015-02-20 10:57:18 -0800 | [diff] [blame] | 1520 | |
| 1521 | /* Adjust clock / bus width after power is up */ |
| 1522 | dw_mci_setup_bus(slot, false); |
| 1523 | |
James Hogan | e6f34e2 | 2013-03-12 10:43:32 +0000 | [diff] [blame] | 1524 | break; |
| 1525 | case MMC_POWER_OFF: |
Doug Anderson | 655babb | 2015-02-20 10:57:18 -0800 | [diff] [blame] | 1526 | /* Turn clock off before power goes down */ |
| 1527 | dw_mci_setup_bus(slot, false); |
| 1528 | |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1529 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1530 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 1531 | |
Doug Anderson | d1f1dd8 | 2015-02-20 10:57:19 -0800 | [diff] [blame] | 1532 | if (!IS_ERR(mmc->supply.vqmmc) && slot->host->vqmmc_enabled) |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1533 | regulator_disable(mmc->supply.vqmmc); |
Doug Anderson | d1f1dd8 | 2015-02-20 10:57:19 -0800 | [diff] [blame] | 1534 | slot->host->vqmmc_enabled = false; |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 1535 | |
Jaehoon Chung | 4366dcc | 2013-03-26 21:36:14 +0900 | [diff] [blame] | 1536 | regs = mci_readl(slot->host, PWREN); |
| 1537 | regs &= ~(1 << slot->id); |
| 1538 | mci_writel(slot->host, PWREN, regs); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1539 | break; |
| 1540 | default: |
| 1541 | break; |
| 1542 | } |
Doug Anderson | 655babb | 2015-02-20 10:57:18 -0800 | [diff] [blame] | 1543 | |
| 1544 | if (slot->host->state == STATE_WAITING_CMD11_DONE && ios->clock != 0) |
| 1545 | slot->host->state = STATE_IDLE; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1546 | } |
| 1547 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1548 | static int dw_mci_card_busy(struct mmc_host *mmc) |
| 1549 | { |
| 1550 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1551 | u32 status; |
| 1552 | |
| 1553 | /* |
| 1554 | * Check the busy bit which is low when DAT[3:0] |
| 1555 | * (the data lines) are 0000 |
| 1556 | */ |
| 1557 | status = mci_readl(slot->host, STATUS); |
| 1558 | |
| 1559 | return !!(status & SDMMC_STATUS_BUSY); |
| 1560 | } |
| 1561 | |
| 1562 | static int dw_mci_switch_voltage(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1563 | { |
| 1564 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1565 | struct dw_mci *host = slot->host; |
Zhangfei Gao | 8f7849c | 2015-05-14 16:45:18 +0800 | [diff] [blame] | 1566 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1567 | u32 uhs; |
| 1568 | u32 v18 = SDMMC_UHS_18V << slot->id; |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1569 | int ret; |
| 1570 | |
Zhangfei Gao | 8f7849c | 2015-05-14 16:45:18 +0800 | [diff] [blame] | 1571 | if (drv_data && drv_data->switch_voltage) |
| 1572 | return drv_data->switch_voltage(mmc, ios); |
| 1573 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1574 | /* |
| 1575 | * Program the voltage. Note that some instances of dw_mmc may use |
| 1576 | * the UHS_REG for this. For other instances (like exynos) the UHS_REG |
| 1577 | * does no harm but you need to set the regulator directly. Try both. |
| 1578 | */ |
| 1579 | uhs = mci_readl(host, UHS_REG); |
Douglas Anderson | e0848f5 | 2015-10-12 14:48:26 +0200 | [diff] [blame] | 1580 | if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1581 | uhs &= ~v18; |
Douglas Anderson | e0848f5 | 2015-10-12 14:48:26 +0200 | [diff] [blame] | 1582 | else |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1583 | uhs |= v18; |
Douglas Anderson | e0848f5 | 2015-10-12 14:48:26 +0200 | [diff] [blame] | 1584 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1585 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Douglas Anderson | e0848f5 | 2015-10-12 14:48:26 +0200 | [diff] [blame] | 1586 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Marek Vasut | 9cbe0fc | 2020-04-16 18:36:47 +0200 | [diff] [blame] | 1587 | if (ret < 0) { |
Doug Anderson | b19caf3 | 2014-10-10 21:16:16 -0700 | [diff] [blame] | 1588 | dev_dbg(&mmc->class_dev, |
Douglas Anderson | e0848f5 | 2015-10-12 14:48:26 +0200 | [diff] [blame] | 1589 | "Regulator set error %d - %s V\n", |
| 1590 | ret, uhs & v18 ? "1.8" : "3.3"); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1591 | return ret; |
| 1592 | } |
| 1593 | } |
| 1594 | mci_writel(host, UHS_REG, uhs); |
| 1595 | |
| 1596 | return 0; |
| 1597 | } |
| 1598 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1599 | static int dw_mci_get_ro(struct mmc_host *mmc) |
| 1600 | { |
| 1601 | int read_only; |
| 1602 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Jaehoon Chung | 9795a84 | 2014-03-03 11:36:46 +0900 | [diff] [blame] | 1603 | int gpio_ro = mmc_gpio_get_ro(mmc); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1604 | |
| 1605 | /* Use platform get_ro function, else try on board write protect */ |
Arnd Bergmann | 287980e | 2016-05-27 23:23:25 +0200 | [diff] [blame] | 1606 | if (gpio_ro >= 0) |
Jaehoon Chung | 9795a84 | 2014-03-03 11:36:46 +0900 | [diff] [blame] | 1607 | read_only = gpio_ro; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1608 | else |
| 1609 | read_only = |
| 1610 | mci_readl(slot->host, WRTPRT) & (1 << slot->id) ? 1 : 0; |
| 1611 | |
| 1612 | dev_dbg(&mmc->class_dev, "card is %s\n", |
| 1613 | read_only ? "read-only" : "read-write"); |
| 1614 | |
| 1615 | return read_only; |
| 1616 | } |
| 1617 | |
Shawn Lin | 935a665 | 2016-01-14 09:08:02 +0800 | [diff] [blame] | 1618 | static void dw_mci_hw_reset(struct mmc_host *mmc) |
| 1619 | { |
| 1620 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1621 | struct dw_mci *host = slot->host; |
| 1622 | int reset; |
| 1623 | |
| 1624 | if (host->use_dma == TRANS_MODE_IDMAC) |
| 1625 | dw_mci_idmac_reset(host); |
| 1626 | |
| 1627 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_DMA_RESET | |
| 1628 | SDMMC_CTRL_FIFO_RESET)) |
| 1629 | return; |
| 1630 | |
| 1631 | /* |
| 1632 | * According to eMMC spec, card reset procedure: |
| 1633 | * tRstW >= 1us: RST_n pulse width |
| 1634 | * tRSCA >= 200us: RST_n to Command time |
| 1635 | * tRSTH >= 1us: RST_n high period |
| 1636 | */ |
| 1637 | reset = mci_readl(host, RST_N); |
| 1638 | reset &= ~(SDMMC_RST_HWACTIVE << slot->id); |
| 1639 | mci_writel(host, RST_N, reset); |
| 1640 | usleep_range(1, 2); |
| 1641 | reset |= SDMMC_RST_HWACTIVE << slot->id; |
| 1642 | mci_writel(host, RST_N, reset); |
| 1643 | usleep_range(200, 300); |
| 1644 | } |
| 1645 | |
Ulf Hansson | 61840ed | 2021-10-20 12:29:07 +0200 | [diff] [blame] | 1646 | static void dw_mci_prepare_sdio_irq(struct dw_mci_slot *slot, bool prepare) |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1647 | { |
| 1648 | struct dw_mci *host = slot->host; |
Ulf Hansson | 61840ed | 2021-10-20 12:29:07 +0200 | [diff] [blame] | 1649 | const u32 clken_low_pwr = SDMMC_CLKEN_LOW_PWR << slot->id; |
| 1650 | u32 clk_en_a_old; |
| 1651 | u32 clk_en_a; |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1652 | |
Doug Anderson | b24c8b2 | 2014-12-02 15:42:46 -0800 | [diff] [blame] | 1653 | /* |
| 1654 | * Low power mode will stop the card clock when idle. According to the |
| 1655 | * description of the CLKENA register we should disable low power mode |
| 1656 | * for SDIO cards if we need SDIO interrupts to work. |
| 1657 | */ |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1658 | |
Ulf Hansson | 61840ed | 2021-10-20 12:29:07 +0200 | [diff] [blame] | 1659 | clk_en_a_old = mci_readl(host, CLKENA); |
| 1660 | if (prepare) { |
| 1661 | set_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags); |
| 1662 | clk_en_a = clk_en_a_old & ~clken_low_pwr; |
| 1663 | } else { |
| 1664 | clear_bit(DW_MMC_CARD_NO_LOW_PWR, &slot->flags); |
| 1665 | clk_en_a = clk_en_a_old | clken_low_pwr; |
| 1666 | } |
Doug Anderson | b24c8b2 | 2014-12-02 15:42:46 -0800 | [diff] [blame] | 1667 | |
Ulf Hansson | 61840ed | 2021-10-20 12:29:07 +0200 | [diff] [blame] | 1668 | if (clk_en_a != clk_en_a_old) { |
| 1669 | mci_writel(host, CLKENA, clk_en_a); |
| 1670 | mci_send_cmd(slot, SDMMC_CMD_UPD_CLK | SDMMC_CMD_PRV_DAT_WAIT, |
| 1671 | 0); |
Doug Anderson | 9623b5b | 2012-07-25 08:33:17 -0700 | [diff] [blame] | 1672 | } |
| 1673 | } |
| 1674 | |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 1675 | static void __dw_mci_enable_sdio_irq(struct dw_mci_slot *slot, int enb) |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1676 | { |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1677 | struct dw_mci *host = slot->host; |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1678 | unsigned long irqflags; |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1679 | u32 int_mask; |
| 1680 | |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1681 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 1682 | |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1683 | /* Enable/disable Slot Specific SDIO interrupt */ |
| 1684 | int_mask = mci_readl(host, INTMASK); |
Doug Anderson | b24c8b2 | 2014-12-02 15:42:46 -0800 | [diff] [blame] | 1685 | if (enb) |
| 1686 | int_mask |= SDMMC_INT_SDIO(slot->sdio_id); |
| 1687 | else |
| 1688 | int_mask &= ~SDMMC_INT_SDIO(slot->sdio_id); |
| 1689 | mci_writel(host, INTMASK, int_mask); |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 1690 | |
| 1691 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1692 | } |
| 1693 | |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 1694 | static void dw_mci_enable_sdio_irq(struct mmc_host *mmc, int enb) |
| 1695 | { |
| 1696 | struct dw_mci_slot *slot = mmc_priv(mmc); |
Ulf Hansson | ca8971c | 2017-04-18 13:37:32 +0200 | [diff] [blame] | 1697 | struct dw_mci *host = slot->host; |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 1698 | |
Ulf Hansson | 61840ed | 2021-10-20 12:29:07 +0200 | [diff] [blame] | 1699 | dw_mci_prepare_sdio_irq(slot, enb); |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 1700 | __dw_mci_enable_sdio_irq(slot, enb); |
Ulf Hansson | ca8971c | 2017-04-18 13:37:32 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Avoid runtime suspending the device when SDIO IRQ is enabled */ |
| 1703 | if (enb) |
| 1704 | pm_runtime_get_noresume(host->dev); |
| 1705 | else |
| 1706 | pm_runtime_put_noidle(host->dev); |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 1707 | } |
| 1708 | |
| 1709 | static void dw_mci_ack_sdio_irq(struct mmc_host *mmc) |
| 1710 | { |
| 1711 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1712 | |
| 1713 | __dw_mci_enable_sdio_irq(slot, 1); |
| 1714 | } |
| 1715 | |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1716 | static int dw_mci_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 1717 | { |
| 1718 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1719 | struct dw_mci *host = slot->host; |
| 1720 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 1721 | int err = -EINVAL; |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1722 | |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1723 | if (drv_data && drv_data->execute_tuning) |
Chaotian Jing | 9979dbe | 2015-10-27 14:24:28 +0800 | [diff] [blame] | 1724 | err = drv_data->execute_tuning(slot, opcode); |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1725 | return err; |
| 1726 | } |
| 1727 | |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 1728 | static int dw_mci_prepare_hs400_tuning(struct mmc_host *mmc, |
| 1729 | struct mmc_ios *ios) |
Seungwon Jeon | 8011313 | 2015-01-29 08:11:57 +0530 | [diff] [blame] | 1730 | { |
| 1731 | struct dw_mci_slot *slot = mmc_priv(mmc); |
| 1732 | struct dw_mci *host = slot->host; |
| 1733 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
| 1734 | |
| 1735 | if (drv_data && drv_data->prepare_hs400_tuning) |
| 1736 | return drv_data->prepare_hs400_tuning(host, ios); |
| 1737 | |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1741 | static bool dw_mci_reset(struct dw_mci *host) |
| 1742 | { |
| 1743 | u32 flags = SDMMC_CTRL_RESET | SDMMC_CTRL_FIFO_RESET; |
| 1744 | bool ret = false; |
Shawn Lin | bc2dcc1 | 2017-02-17 10:59:52 +0800 | [diff] [blame] | 1745 | u32 status = 0; |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1746 | |
| 1747 | /* |
| 1748 | * Resetting generates a block interrupt, hence setting |
| 1749 | * the scatter-gather pointer to NULL. |
| 1750 | */ |
| 1751 | if (host->sg) { |
| 1752 | sg_miter_stop(&host->sg_miter); |
| 1753 | host->sg = NULL; |
| 1754 | } |
| 1755 | |
| 1756 | if (host->use_dma) |
| 1757 | flags |= SDMMC_CTRL_DMA_RESET; |
| 1758 | |
| 1759 | if (dw_mci_ctrl_reset(host, flags)) { |
| 1760 | /* |
Shawn Lin | bc2dcc1 | 2017-02-17 10:59:52 +0800 | [diff] [blame] | 1761 | * In all cases we clear the RAWINTS |
| 1762 | * register to clear any interrupts. |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1763 | */ |
| 1764 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 1765 | |
Shawn Lin | bc2dcc1 | 2017-02-17 10:59:52 +0800 | [diff] [blame] | 1766 | if (!host->use_dma) { |
| 1767 | ret = true; |
| 1768 | goto ciu_out; |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1769 | } |
Shawn Lin | bc2dcc1 | 2017-02-17 10:59:52 +0800 | [diff] [blame] | 1770 | |
| 1771 | /* Wait for dma_req to be cleared */ |
| 1772 | if (readl_poll_timeout_atomic(host->regs + SDMMC_STATUS, |
| 1773 | status, |
| 1774 | !(status & SDMMC_STATUS_DMA_REQ), |
| 1775 | 1, 500 * USEC_PER_MSEC)) { |
| 1776 | dev_err(host->dev, |
| 1777 | "%s: Timeout waiting for dma_req to be cleared\n", |
| 1778 | __func__); |
| 1779 | goto ciu_out; |
| 1780 | } |
| 1781 | |
| 1782 | /* when using DMA next we reset the fifo again */ |
| 1783 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_FIFO_RESET)) |
| 1784 | goto ciu_out; |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1785 | } else { |
| 1786 | /* if the controller reset bit did clear, then set clock regs */ |
| 1787 | if (!(mci_readl(host, CTRL) & SDMMC_CTRL_RESET)) { |
| 1788 | dev_err(host->dev, |
| 1789 | "%s: fifo/dma reset bits didn't clear but ciu was reset, doing clock update\n", |
| 1790 | __func__); |
| 1791 | goto ciu_out; |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | if (host->use_dma == TRANS_MODE_IDMAC) |
Evgeniy Didin | 47b7de2 | 2018-03-14 22:30:51 +0300 | [diff] [blame] | 1796 | /* It is also required that we reinit idmac */ |
| 1797 | dw_mci_idmac_init(host); |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1798 | |
| 1799 | ret = true; |
| 1800 | |
| 1801 | ciu_out: |
| 1802 | /* After a CTRL reset we need to have CIU set clock registers */ |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 1803 | mci_send_cmd(host->slot, SDMMC_CMD_UPD_CLK, 0); |
Shawn Lin | 4e7392b | 2017-02-17 10:56:40 +0800 | [diff] [blame] | 1804 | |
| 1805 | return ret; |
| 1806 | } |
| 1807 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1808 | static const struct mmc_host_ops dw_mci_ops = { |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1809 | .request = dw_mci_request, |
Seungwon Jeon | 9aa5140 | 2012-02-06 16:55:07 +0900 | [diff] [blame] | 1810 | .pre_req = dw_mci_pre_req, |
| 1811 | .post_req = dw_mci_post_req, |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1812 | .set_ios = dw_mci_set_ios, |
| 1813 | .get_ro = dw_mci_get_ro, |
| 1814 | .get_cd = dw_mci_get_cd, |
Shawn Lin | 935a665 | 2016-01-14 09:08:02 +0800 | [diff] [blame] | 1815 | .hw_reset = dw_mci_hw_reset, |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 1816 | .enable_sdio_irq = dw_mci_enable_sdio_irq, |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 1817 | .ack_sdio_irq = dw_mci_ack_sdio_irq, |
Seungwon Jeon | 0976f16 | 2013-08-31 00:12:42 +0900 | [diff] [blame] | 1818 | .execute_tuning = dw_mci_execute_tuning, |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1819 | .card_busy = dw_mci_card_busy, |
| 1820 | .start_signal_voltage_switch = dw_mci_switch_voltage, |
Seungwon Jeon | 8011313 | 2015-01-29 08:11:57 +0530 | [diff] [blame] | 1821 | .prepare_hs400_tuning = dw_mci_prepare_hs400_tuning, |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1822 | }; |
| 1823 | |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 1824 | #ifdef CONFIG_FAULT_INJECTION |
| 1825 | static enum hrtimer_restart dw_mci_fault_timer(struct hrtimer *t) |
| 1826 | { |
| 1827 | struct dw_mci *host = container_of(t, struct dw_mci, fault_timer); |
| 1828 | unsigned long flags; |
| 1829 | |
| 1830 | spin_lock_irqsave(&host->irq_lock, flags); |
| 1831 | |
Vincent Whitchurch | 26391e4 | 2021-08-25 13:42:13 +0200 | [diff] [blame] | 1832 | /* |
| 1833 | * Only inject an error if we haven't already got an error or data over |
| 1834 | * interrupt. |
| 1835 | */ |
| 1836 | if (!host->data_status) { |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 1837 | host->data_status = SDMMC_INT_DCRC; |
Vincent Whitchurch | 26391e4 | 2021-08-25 13:42:13 +0200 | [diff] [blame] | 1838 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
| 1839 | tasklet_schedule(&host->tasklet); |
| 1840 | } |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 1841 | |
| 1842 | spin_unlock_irqrestore(&host->irq_lock, flags); |
| 1843 | |
| 1844 | return HRTIMER_NORESTART; |
| 1845 | } |
| 1846 | |
| 1847 | static void dw_mci_start_fault_timer(struct dw_mci *host) |
| 1848 | { |
| 1849 | struct mmc_data *data = host->data; |
| 1850 | |
| 1851 | if (!data || data->blocks <= 1) |
| 1852 | return; |
| 1853 | |
| 1854 | if (!should_fail(&host->fail_data_crc, 1)) |
| 1855 | return; |
| 1856 | |
| 1857 | /* |
| 1858 | * Try to inject the error at random points during the data transfer. |
| 1859 | */ |
| 1860 | hrtimer_start(&host->fault_timer, |
| 1861 | ms_to_ktime(prandom_u32() % 25), |
| 1862 | HRTIMER_MODE_REL); |
| 1863 | } |
| 1864 | |
| 1865 | static void dw_mci_stop_fault_timer(struct dw_mci *host) |
| 1866 | { |
| 1867 | hrtimer_cancel(&host->fault_timer); |
| 1868 | } |
| 1869 | |
| 1870 | static void dw_mci_init_fault(struct dw_mci *host) |
| 1871 | { |
| 1872 | host->fail_data_crc = (struct fault_attr) FAULT_ATTR_INITIALIZER; |
| 1873 | |
| 1874 | hrtimer_init(&host->fault_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); |
| 1875 | host->fault_timer.function = dw_mci_fault_timer; |
| 1876 | } |
| 1877 | #else |
| 1878 | static void dw_mci_init_fault(struct dw_mci *host) |
| 1879 | { |
| 1880 | } |
| 1881 | |
| 1882 | static void dw_mci_start_fault_timer(struct dw_mci *host) |
| 1883 | { |
| 1884 | } |
| 1885 | |
| 1886 | static void dw_mci_stop_fault_timer(struct dw_mci *host) |
| 1887 | { |
| 1888 | } |
| 1889 | #endif |
| 1890 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1891 | static void dw_mci_request_end(struct dw_mci *host, struct mmc_request *mrq) |
| 1892 | __releases(&host->lock) |
| 1893 | __acquires(&host->lock) |
| 1894 | { |
| 1895 | struct dw_mci_slot *slot; |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 1896 | struct mmc_host *prev_mmc = host->slot->mmc; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1897 | |
| 1898 | WARN_ON(host->cmd || host->data); |
| 1899 | |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 1900 | host->slot->mrq = NULL; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1901 | host->mrq = NULL; |
| 1902 | if (!list_empty(&host->queue)) { |
| 1903 | slot = list_entry(host->queue.next, |
| 1904 | struct dw_mci_slot, queue_node); |
| 1905 | list_del(&slot->queue_node); |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 1906 | dev_vdbg(host->dev, "list not empty: %s is next\n", |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1907 | mmc_hostname(slot->mmc)); |
| 1908 | host->state = STATE_SENDING_CMD; |
| 1909 | dw_mci_start_request(host, slot); |
| 1910 | } else { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 1911 | dev_vdbg(host->dev, "list empty\n"); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 1912 | |
| 1913 | if (host->state == STATE_SENDING_CMD11) |
| 1914 | host->state = STATE_WAITING_CMD11_DONE; |
| 1915 | else |
| 1916 | host->state = STATE_IDLE; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | spin_unlock(&host->lock); |
| 1920 | mmc_request_done(prev_mmc, mrq); |
| 1921 | spin_lock(&host->lock); |
| 1922 | } |
| 1923 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1924 | static int dw_mci_command_complete(struct dw_mci *host, struct mmc_command *cmd) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1925 | { |
| 1926 | u32 status = host->cmd_status; |
| 1927 | |
| 1928 | host->cmd_status = 0; |
| 1929 | |
| 1930 | /* Read the response from the card (up to 16 bytes) */ |
| 1931 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 1932 | if (cmd->flags & MMC_RSP_136) { |
| 1933 | cmd->resp[3] = mci_readl(host, RESP0); |
| 1934 | cmd->resp[2] = mci_readl(host, RESP1); |
| 1935 | cmd->resp[1] = mci_readl(host, RESP2); |
| 1936 | cmd->resp[0] = mci_readl(host, RESP3); |
| 1937 | } else { |
| 1938 | cmd->resp[0] = mci_readl(host, RESP0); |
| 1939 | cmd->resp[1] = 0; |
| 1940 | cmd->resp[2] = 0; |
| 1941 | cmd->resp[3] = 0; |
| 1942 | } |
| 1943 | } |
| 1944 | |
| 1945 | if (status & SDMMC_INT_RTO) |
| 1946 | cmd->error = -ETIMEDOUT; |
| 1947 | else if ((cmd->flags & MMC_RSP_CRC) && (status & SDMMC_INT_RCRC)) |
| 1948 | cmd->error = -EILSEQ; |
| 1949 | else if (status & SDMMC_INT_RESP_ERR) |
| 1950 | cmd->error = -EIO; |
| 1951 | else |
| 1952 | cmd->error = 0; |
| 1953 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1954 | return cmd->error; |
| 1955 | } |
| 1956 | |
| 1957 | static int dw_mci_data_complete(struct dw_mci *host, struct mmc_data *data) |
| 1958 | { |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 1959 | u32 status = host->data_status; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1960 | |
| 1961 | if (status & DW_MCI_DATA_ERROR_FLAGS) { |
| 1962 | if (status & SDMMC_INT_DRTO) { |
| 1963 | data->error = -ETIMEDOUT; |
| 1964 | } else if (status & SDMMC_INT_DCRC) { |
| 1965 | data->error = -EILSEQ; |
| 1966 | } else if (status & SDMMC_INT_EBE) { |
| 1967 | if (host->dir_status == |
| 1968 | DW_MCI_SEND_STATUS) { |
| 1969 | /* |
| 1970 | * No data CRC status was returned. |
| 1971 | * The number of bytes transferred |
| 1972 | * will be exaggerated in PIO mode. |
| 1973 | */ |
| 1974 | data->bytes_xfered = 0; |
| 1975 | data->error = -ETIMEDOUT; |
| 1976 | } else if (host->dir_status == |
| 1977 | DW_MCI_RECV_STATUS) { |
Shawn Lin | e7a1dec | 2016-08-22 10:57:16 +0800 | [diff] [blame] | 1978 | data->error = -EILSEQ; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1979 | } |
| 1980 | } else { |
| 1981 | /* SDMMC_INT_SBE is included */ |
Shawn Lin | e7a1dec | 2016-08-22 10:57:16 +0800 | [diff] [blame] | 1982 | data->error = -EILSEQ; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1983 | } |
| 1984 | |
Doug Anderson | e6cc012 | 2014-04-22 16:51:21 -0700 | [diff] [blame] | 1985 | dev_dbg(host->dev, "data error, status 0x%08x\n", status); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1986 | |
| 1987 | /* |
| 1988 | * After an error, there may be data lingering |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 1989 | * in the FIFO |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1990 | */ |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 1991 | dw_mci_reset(host); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 1992 | } else { |
| 1993 | data->bytes_xfered = data->blocks * data->blksz; |
| 1994 | data->error = 0; |
| 1995 | } |
| 1996 | |
| 1997 | return data->error; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 1998 | } |
| 1999 | |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2000 | static void dw_mci_set_drto(struct dw_mci *host) |
| 2001 | { |
MÃ¥rten Lindahl | 25d5417 | 2021-12-20 12:30:25 +0100 | [diff] [blame] | 2002 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2003 | unsigned int drto_clks; |
Douglas Anderson | 9d9491a | 2017-10-12 13:11:17 -0700 | [diff] [blame] | 2004 | unsigned int drto_div; |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2005 | unsigned int drto_ms; |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 2006 | unsigned long irqflags; |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2007 | |
MÃ¥rten Lindahl | 25d5417 | 2021-12-20 12:30:25 +0100 | [diff] [blame] | 2008 | if (drv_data && drv_data->get_drto_clks) |
| 2009 | drto_clks = drv_data->get_drto_clks(host); |
| 2010 | else |
| 2011 | drto_clks = mci_readl(host, TMOUT) >> 8; |
Douglas Anderson | 9d9491a | 2017-10-12 13:11:17 -0700 | [diff] [blame] | 2012 | drto_div = (mci_readl(host, CLKDIV) & 0xff) * 2; |
| 2013 | if (drto_div == 0) |
| 2014 | drto_div = 1; |
Evgeniy Didin | c715160 | 2018-02-28 14:53:18 +0300 | [diff] [blame] | 2015 | |
| 2016 | drto_ms = DIV_ROUND_UP_ULL((u64)MSEC_PER_SEC * drto_clks * drto_div, |
| 2017 | host->bus_hz); |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2018 | |
MÃ¥rten Lindahl | 25d5417 | 2021-12-20 12:30:25 +0100 | [diff] [blame] | 2019 | dev_dbg(host->dev, "drto_ms: %u\n", drto_ms); |
| 2020 | |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2021 | /* add a bit spare time */ |
| 2022 | drto_ms += 10; |
| 2023 | |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 2024 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 2025 | if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) |
| 2026 | mod_timer(&host->dto_timer, |
| 2027 | jiffies + msecs_to_jiffies(drto_ms)); |
| 2028 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2029 | } |
| 2030 | |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2031 | static bool dw_mci_clear_pending_cmd_complete(struct dw_mci *host) |
| 2032 | { |
| 2033 | if (!test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) |
| 2034 | return false; |
| 2035 | |
| 2036 | /* |
| 2037 | * Really be certain that the timer has stopped. This is a bit of |
| 2038 | * paranoia and could only really happen if we had really bad |
| 2039 | * interrupt latency and the interrupt routine and timeout were |
| 2040 | * running concurrently so that the del_timer() in the interrupt |
| 2041 | * handler couldn't run. |
| 2042 | */ |
| 2043 | WARN_ON(del_timer_sync(&host->cto_timer)); |
| 2044 | clear_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 2045 | |
| 2046 | return true; |
| 2047 | } |
| 2048 | |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 2049 | static bool dw_mci_clear_pending_data_complete(struct dw_mci *host) |
| 2050 | { |
| 2051 | if (!test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) |
| 2052 | return false; |
| 2053 | |
| 2054 | /* Extra paranoia just like dw_mci_clear_pending_cmd_complete() */ |
| 2055 | WARN_ON(del_timer_sync(&host->dto_timer)); |
| 2056 | clear_bit(EVENT_DATA_COMPLETE, &host->pending_events); |
| 2057 | |
| 2058 | return true; |
| 2059 | } |
| 2060 | |
Emil Renner Berthing | 6078df1 | 2021-02-04 16:18:41 +0100 | [diff] [blame] | 2061 | static void dw_mci_tasklet_func(struct tasklet_struct *t) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2062 | { |
Emil Renner Berthing | 6078df1 | 2021-02-04 16:18:41 +0100 | [diff] [blame] | 2063 | struct dw_mci *host = from_tasklet(host, t, tasklet); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2064 | struct mmc_data *data; |
| 2065 | struct mmc_command *cmd; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2066 | struct mmc_request *mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2067 | enum dw_mci_state state; |
| 2068 | enum dw_mci_state prev_state; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2069 | unsigned int err; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2070 | |
| 2071 | spin_lock(&host->lock); |
| 2072 | |
| 2073 | state = host->state; |
| 2074 | data = host->data; |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2075 | mrq = host->mrq; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2076 | |
| 2077 | do { |
| 2078 | prev_state = state; |
| 2079 | |
| 2080 | switch (state) { |
| 2081 | case STATE_IDLE: |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 2082 | case STATE_WAITING_CMD11_DONE: |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2083 | break; |
| 2084 | |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 2085 | case STATE_SENDING_CMD11: |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2086 | case STATE_SENDING_CMD: |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2087 | if (!dw_mci_clear_pending_cmd_complete(host)) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2088 | break; |
| 2089 | |
| 2090 | cmd = host->cmd; |
| 2091 | host->cmd = NULL; |
| 2092 | set_bit(EVENT_CMD_COMPLETE, &host->completed_events); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2093 | err = dw_mci_command_complete(host, cmd); |
| 2094 | if (cmd == mrq->sbc && !err) { |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 2095 | __dw_mci_start_request(host, host->slot, |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2096 | mrq->cmd); |
Seungwon Jeon | 053b3ce | 2011-12-22 18:01:29 +0900 | [diff] [blame] | 2097 | goto unlock; |
| 2098 | } |
| 2099 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2100 | if (cmd->data && err) { |
Doug Anderson | 46d1795 | 2016-04-26 10:03:58 +0200 | [diff] [blame] | 2101 | /* |
| 2102 | * During UHS tuning sequence, sending the stop |
| 2103 | * command after the response CRC error would |
| 2104 | * throw the system into a confused state |
| 2105 | * causing all future tuning phases to report |
| 2106 | * failure. |
| 2107 | * |
| 2108 | * In such case controller will move into a data |
| 2109 | * transfer state after a response error or |
| 2110 | * response CRC error. Let's let that finish |
| 2111 | * before trying to send a stop, so we'll go to |
| 2112 | * STATE_SENDING_DATA. |
| 2113 | * |
| 2114 | * Although letting the data transfer take place |
| 2115 | * will waste a bit of time (we already know |
| 2116 | * the command was bad), it can't cause any |
| 2117 | * errors since it's possible it would have |
| 2118 | * taken place anyway if this tasklet got |
| 2119 | * delayed. Allowing the transfer to take place |
| 2120 | * avoids races and keeps things simple. |
| 2121 | */ |
Christian Löhle | 43592c8 | 2021-09-16 05:59:19 +0000 | [diff] [blame] | 2122 | if (err != -ETIMEDOUT && |
| 2123 | host->dir_status == DW_MCI_RECV_STATUS) { |
Doug Anderson | 46d1795 | 2016-04-26 10:03:58 +0200 | [diff] [blame] | 2124 | state = STATE_SENDING_DATA; |
| 2125 | continue; |
| 2126 | } |
| 2127 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 2128 | send_stop_abort(host, data); |
Vincent Whitchurch | 25f8203 | 2021-06-30 12:22:32 +0200 | [diff] [blame] | 2129 | dw_mci_stop_dma(host); |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 2130 | state = STATE_SENDING_STOP; |
| 2131 | break; |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 2132 | } |
| 2133 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2134 | if (!cmd->data || err) { |
| 2135 | dw_mci_request_end(host, mrq); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2136 | goto unlock; |
| 2137 | } |
| 2138 | |
| 2139 | prev_state = state = STATE_SENDING_DATA; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2140 | fallthrough; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2141 | |
| 2142 | case STATE_SENDING_DATA: |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 2143 | /* |
| 2144 | * We could get a data error and never a transfer |
| 2145 | * complete so we'd better check for it here. |
| 2146 | * |
| 2147 | * Note that we don't really care if we also got a |
| 2148 | * transfer complete; stopping the DMA and sending an |
| 2149 | * abort won't hurt. |
| 2150 | */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2151 | if (test_and_clear_bit(EVENT_DATA_ERROR, |
| 2152 | &host->pending_events)) { |
Jaehoon Chung | e13c3c0 | 2016-11-17 16:40:37 +0900 | [diff] [blame] | 2153 | if (!(host->data_status & (SDMMC_INT_DRTO | |
addy ke | bdb9a90 | 2015-02-20 10:55:25 +0800 | [diff] [blame] | 2154 | SDMMC_INT_EBE))) |
| 2155 | send_stop_abort(host, data); |
Vincent Whitchurch | 25f8203 | 2021-06-30 12:22:32 +0200 | [diff] [blame] | 2156 | dw_mci_stop_dma(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2157 | state = STATE_DATA_ERROR; |
| 2158 | break; |
| 2159 | } |
| 2160 | |
| 2161 | if (!test_and_clear_bit(EVENT_XFER_COMPLETE, |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2162 | &host->pending_events)) { |
| 2163 | /* |
| 2164 | * If all data-related interrupts don't come |
| 2165 | * within the given time in reading data state. |
| 2166 | */ |
Jaehoon Chung | 16a3457 | 2016-06-21 14:35:37 +0900 | [diff] [blame] | 2167 | if (host->dir_status == DW_MCI_RECV_STATUS) |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2168 | dw_mci_set_drto(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2169 | break; |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2170 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2171 | |
| 2172 | set_bit(EVENT_XFER_COMPLETE, &host->completed_events); |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 2173 | |
| 2174 | /* |
| 2175 | * Handle an EVENT_DATA_ERROR that might have shown up |
| 2176 | * before the transfer completed. This might not have |
| 2177 | * been caught by the check above because the interrupt |
| 2178 | * could have gone off between the previous check and |
| 2179 | * the check for transfer complete. |
| 2180 | * |
| 2181 | * Technically this ought not be needed assuming we |
| 2182 | * get a DATA_COMPLETE eventually (we'll notice the |
| 2183 | * error and end the request), but it shouldn't hurt. |
| 2184 | * |
| 2185 | * This has the advantage of sending the stop command. |
| 2186 | */ |
| 2187 | if (test_and_clear_bit(EVENT_DATA_ERROR, |
| 2188 | &host->pending_events)) { |
Jaehoon Chung | e13c3c0 | 2016-11-17 16:40:37 +0900 | [diff] [blame] | 2189 | if (!(host->data_status & (SDMMC_INT_DRTO | |
addy ke | bdb9a90 | 2015-02-20 10:55:25 +0800 | [diff] [blame] | 2190 | SDMMC_INT_EBE))) |
| 2191 | send_stop_abort(host, data); |
Vincent Whitchurch | 25f8203 | 2021-06-30 12:22:32 +0200 | [diff] [blame] | 2192 | dw_mci_stop_dma(host); |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 2193 | state = STATE_DATA_ERROR; |
| 2194 | break; |
| 2195 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2196 | prev_state = state = STATE_DATA_BUSY; |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 2197 | |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2198 | fallthrough; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2199 | |
| 2200 | case STATE_DATA_BUSY: |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 2201 | if (!dw_mci_clear_pending_data_complete(host)) { |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2202 | /* |
| 2203 | * If data error interrupt comes but data over |
| 2204 | * interrupt doesn't come within the given time. |
| 2205 | * in reading data state. |
| 2206 | */ |
Jaehoon Chung | 16a3457 | 2016-06-21 14:35:37 +0900 | [diff] [blame] | 2207 | if (host->dir_status == DW_MCI_RECV_STATUS) |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2208 | dw_mci_set_drto(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2209 | break; |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2210 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2211 | |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 2212 | dw_mci_stop_fault_timer(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2213 | host->data = NULL; |
| 2214 | set_bit(EVENT_DATA_COMPLETE, &host->completed_events); |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2215 | err = dw_mci_data_complete(host, data); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2216 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2217 | if (!err) { |
| 2218 | if (!data->stop || mrq->sbc) { |
Sachin Kamat | 17c8bc8 | 2014-02-25 15:18:28 +0530 | [diff] [blame] | 2219 | if (mrq->sbc && data->stop) |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2220 | data->stop->error = 0; |
| 2221 | dw_mci_request_end(host, mrq); |
| 2222 | goto unlock; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2223 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2224 | |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 2225 | /* stop command for open-ended transfer*/ |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2226 | if (data->stop) |
| 2227 | send_stop_abort(host, data); |
Doug Anderson | 2aa3546 | 2014-08-13 08:13:43 -0700 | [diff] [blame] | 2228 | } else { |
| 2229 | /* |
| 2230 | * If we don't have a command complete now we'll |
| 2231 | * never get one since we just reset everything; |
| 2232 | * better end the request. |
| 2233 | * |
| 2234 | * If we do have a command complete we'll fall |
| 2235 | * through to the SENDING_STOP command and |
| 2236 | * everything will be peachy keen. |
| 2237 | */ |
| 2238 | if (!test_bit(EVENT_CMD_COMPLETE, |
| 2239 | &host->pending_events)) { |
| 2240 | host->cmd = NULL; |
| 2241 | dw_mci_request_end(host, mrq); |
| 2242 | goto unlock; |
| 2243 | } |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 2244 | } |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2245 | |
| 2246 | /* |
| 2247 | * If err has non-zero, |
| 2248 | * stop-abort command has been already issued. |
| 2249 | */ |
| 2250 | prev_state = state = STATE_SENDING_STOP; |
| 2251 | |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 2252 | fallthrough; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2253 | |
| 2254 | case STATE_SENDING_STOP: |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2255 | if (!dw_mci_clear_pending_cmd_complete(host)) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2256 | break; |
| 2257 | |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 2258 | /* CMD error in data command */ |
Seungwon Jeon | 31bff45 | 2013-08-31 00:14:23 +0900 | [diff] [blame] | 2259 | if (mrq->cmd->error && mrq->data) |
Sonny Rao | 3a33a94 | 2014-08-04 18:19:50 -0700 | [diff] [blame] | 2260 | dw_mci_reset(host); |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 2261 | |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 2262 | dw_mci_stop_fault_timer(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2263 | host->cmd = NULL; |
Seungwon Jeon | 71abb13 | 2013-08-31 00:13:59 +0900 | [diff] [blame] | 2264 | host->data = NULL; |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 2265 | |
Jaehoon Chung | e13c3c0 | 2016-11-17 16:40:37 +0900 | [diff] [blame] | 2266 | if (!mrq->sbc && mrq->stop) |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2267 | dw_mci_command_complete(host, mrq->stop); |
Seungwon Jeon | 90c2143 | 2013-08-31 00:14:05 +0900 | [diff] [blame] | 2268 | else |
| 2269 | host->cmd_status = 0; |
| 2270 | |
Seungwon Jeon | e352c81 | 2013-08-31 00:14:17 +0900 | [diff] [blame] | 2271 | dw_mci_request_end(host, mrq); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2272 | goto unlock; |
| 2273 | |
| 2274 | case STATE_DATA_ERROR: |
| 2275 | if (!test_and_clear_bit(EVENT_XFER_COMPLETE, |
| 2276 | &host->pending_events)) |
| 2277 | break; |
| 2278 | |
| 2279 | state = STATE_DATA_BUSY; |
| 2280 | break; |
| 2281 | } |
| 2282 | } while (state != prev_state); |
| 2283 | |
| 2284 | host->state = state; |
| 2285 | unlock: |
| 2286 | spin_unlock(&host->lock); |
| 2287 | |
| 2288 | } |
| 2289 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2290 | /* push final bytes to part_buf, only use during push */ |
| 2291 | static void dw_mci_set_part_bytes(struct dw_mci *host, void *buf, int cnt) |
| 2292 | { |
| 2293 | memcpy((void *)&host->part_buf, buf, cnt); |
| 2294 | host->part_buf_count = cnt; |
| 2295 | } |
| 2296 | |
| 2297 | /* append bytes to part_buf, only use during push */ |
| 2298 | static int dw_mci_push_part_bytes(struct dw_mci *host, void *buf, int cnt) |
| 2299 | { |
| 2300 | cnt = min(cnt, (1 << host->data_shift) - host->part_buf_count); |
| 2301 | memcpy((void *)&host->part_buf + host->part_buf_count, buf, cnt); |
| 2302 | host->part_buf_count += cnt; |
| 2303 | return cnt; |
| 2304 | } |
| 2305 | |
| 2306 | /* pull first bytes from part_buf, only use during pull */ |
| 2307 | static int dw_mci_pull_part_bytes(struct dw_mci *host, void *buf, int cnt) |
| 2308 | { |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2309 | cnt = min_t(int, cnt, host->part_buf_count); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2310 | if (cnt) { |
| 2311 | memcpy(buf, (void *)&host->part_buf + host->part_buf_start, |
| 2312 | cnt); |
| 2313 | host->part_buf_count -= cnt; |
| 2314 | host->part_buf_start += cnt; |
| 2315 | } |
| 2316 | return cnt; |
| 2317 | } |
| 2318 | |
| 2319 | /* pull final bytes from the part_buf, assuming it's just been filled */ |
| 2320 | static void dw_mci_pull_final_bytes(struct dw_mci *host, void *buf, int cnt) |
| 2321 | { |
| 2322 | memcpy(buf, &host->part_buf, cnt); |
| 2323 | host->part_buf_start = cnt; |
| 2324 | host->part_buf_count = (1 << host->data_shift) - cnt; |
| 2325 | } |
| 2326 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2327 | static void dw_mci_push_data16(struct dw_mci *host, void *buf, int cnt) |
| 2328 | { |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2329 | struct mmc_data *data = host->data; |
| 2330 | int init_cnt = cnt; |
| 2331 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2332 | /* try and push anything in the part_buf */ |
| 2333 | if (unlikely(host->part_buf_count)) { |
| 2334 | int len = dw_mci_push_part_bytes(host, buf, cnt); |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2335 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2336 | buf += len; |
| 2337 | cnt -= len; |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2338 | if (host->part_buf_count == 2) { |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2339 | mci_fifo_writew(host->fifo_reg, host->part_buf16); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2340 | host->part_buf_count = 0; |
| 2341 | } |
| 2342 | } |
| 2343 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 2344 | if (unlikely((unsigned long)buf & 0x1)) { |
| 2345 | while (cnt >= 2) { |
| 2346 | u16 aligned_buf[64]; |
| 2347 | int len = min(cnt & -2, (int)sizeof(aligned_buf)); |
| 2348 | int items = len >> 1; |
| 2349 | int i; |
| 2350 | /* memcpy from input buffer into aligned buffer */ |
| 2351 | memcpy(aligned_buf, buf, len); |
| 2352 | buf += len; |
| 2353 | cnt -= len; |
| 2354 | /* push data from aligned buffer into fifo */ |
| 2355 | for (i = 0; i < items; ++i) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2356 | mci_fifo_writew(host->fifo_reg, aligned_buf[i]); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2357 | } |
| 2358 | } else |
| 2359 | #endif |
| 2360 | { |
| 2361 | u16 *pdata = buf; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2362 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2363 | for (; cnt >= 2; cnt -= 2) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2364 | mci_fifo_writew(host->fifo_reg, *pdata++); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2365 | buf = pdata; |
| 2366 | } |
| 2367 | /* put anything remaining in the part_buf */ |
| 2368 | if (cnt) { |
| 2369 | dw_mci_set_part_bytes(host, buf, cnt); |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2370 | /* Push data if we have reached the expected data length */ |
| 2371 | if ((data->bytes_xfered + init_cnt) == |
| 2372 | (data->blksz * data->blocks)) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2373 | mci_fifo_writew(host->fifo_reg, host->part_buf16); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2374 | } |
| 2375 | } |
| 2376 | |
| 2377 | static void dw_mci_pull_data16(struct dw_mci *host, void *buf, int cnt) |
| 2378 | { |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2379 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 2380 | if (unlikely((unsigned long)buf & 0x1)) { |
| 2381 | while (cnt >= 2) { |
| 2382 | /* pull data from fifo into aligned buffer */ |
| 2383 | u16 aligned_buf[64]; |
| 2384 | int len = min(cnt & -2, (int)sizeof(aligned_buf)); |
| 2385 | int items = len >> 1; |
| 2386 | int i; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2387 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2388 | for (i = 0; i < items; ++i) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2389 | aligned_buf[i] = mci_fifo_readw(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2390 | /* memcpy from aligned buffer into output buffer */ |
| 2391 | memcpy(buf, aligned_buf, len); |
| 2392 | buf += len; |
| 2393 | cnt -= len; |
| 2394 | } |
| 2395 | } else |
| 2396 | #endif |
| 2397 | { |
| 2398 | u16 *pdata = buf; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2399 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2400 | for (; cnt >= 2; cnt -= 2) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2401 | *pdata++ = mci_fifo_readw(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2402 | buf = pdata; |
| 2403 | } |
| 2404 | if (cnt) { |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2405 | host->part_buf16 = mci_fifo_readw(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2406 | dw_mci_pull_final_bytes(host, buf, cnt); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2407 | } |
| 2408 | } |
| 2409 | |
| 2410 | static void dw_mci_push_data32(struct dw_mci *host, void *buf, int cnt) |
| 2411 | { |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2412 | struct mmc_data *data = host->data; |
| 2413 | int init_cnt = cnt; |
| 2414 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2415 | /* try and push anything in the part_buf */ |
| 2416 | if (unlikely(host->part_buf_count)) { |
| 2417 | int len = dw_mci_push_part_bytes(host, buf, cnt); |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2418 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2419 | buf += len; |
| 2420 | cnt -= len; |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2421 | if (host->part_buf_count == 4) { |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2422 | mci_fifo_writel(host->fifo_reg, host->part_buf32); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2423 | host->part_buf_count = 0; |
| 2424 | } |
| 2425 | } |
| 2426 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 2427 | if (unlikely((unsigned long)buf & 0x3)) { |
| 2428 | while (cnt >= 4) { |
| 2429 | u32 aligned_buf[32]; |
| 2430 | int len = min(cnt & -4, (int)sizeof(aligned_buf)); |
| 2431 | int items = len >> 2; |
| 2432 | int i; |
| 2433 | /* memcpy from input buffer into aligned buffer */ |
| 2434 | memcpy(aligned_buf, buf, len); |
| 2435 | buf += len; |
| 2436 | cnt -= len; |
| 2437 | /* push data from aligned buffer into fifo */ |
| 2438 | for (i = 0; i < items; ++i) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2439 | mci_fifo_writel(host->fifo_reg, aligned_buf[i]); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2440 | } |
| 2441 | } else |
| 2442 | #endif |
| 2443 | { |
| 2444 | u32 *pdata = buf; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2445 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2446 | for (; cnt >= 4; cnt -= 4) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2447 | mci_fifo_writel(host->fifo_reg, *pdata++); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2448 | buf = pdata; |
| 2449 | } |
| 2450 | /* put anything remaining in the part_buf */ |
| 2451 | if (cnt) { |
| 2452 | dw_mci_set_part_bytes(host, buf, cnt); |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2453 | /* Push data if we have reached the expected data length */ |
| 2454 | if ((data->bytes_xfered + init_cnt) == |
| 2455 | (data->blksz * data->blocks)) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2456 | mci_fifo_writel(host->fifo_reg, host->part_buf32); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | static void dw_mci_pull_data32(struct dw_mci *host, void *buf, int cnt) |
| 2461 | { |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2462 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 2463 | if (unlikely((unsigned long)buf & 0x3)) { |
| 2464 | while (cnt >= 4) { |
| 2465 | /* pull data from fifo into aligned buffer */ |
| 2466 | u32 aligned_buf[32]; |
| 2467 | int len = min(cnt & -4, (int)sizeof(aligned_buf)); |
| 2468 | int items = len >> 2; |
| 2469 | int i; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2470 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2471 | for (i = 0; i < items; ++i) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2472 | aligned_buf[i] = mci_fifo_readl(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2473 | /* memcpy from aligned buffer into output buffer */ |
| 2474 | memcpy(buf, aligned_buf, len); |
| 2475 | buf += len; |
| 2476 | cnt -= len; |
| 2477 | } |
| 2478 | } else |
| 2479 | #endif |
| 2480 | { |
| 2481 | u32 *pdata = buf; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2482 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2483 | for (; cnt >= 4; cnt -= 4) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2484 | *pdata++ = mci_fifo_readl(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2485 | buf = pdata; |
| 2486 | } |
| 2487 | if (cnt) { |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2488 | host->part_buf32 = mci_fifo_readl(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2489 | dw_mci_pull_final_bytes(host, buf, cnt); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2490 | } |
| 2491 | } |
| 2492 | |
| 2493 | static void dw_mci_push_data64(struct dw_mci *host, void *buf, int cnt) |
| 2494 | { |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2495 | struct mmc_data *data = host->data; |
| 2496 | int init_cnt = cnt; |
| 2497 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2498 | /* try and push anything in the part_buf */ |
| 2499 | if (unlikely(host->part_buf_count)) { |
| 2500 | int len = dw_mci_push_part_bytes(host, buf, cnt); |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2501 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2502 | buf += len; |
| 2503 | cnt -= len; |
Seungwon Jeon | c09fbd7 | 2013-03-25 16:28:22 +0900 | [diff] [blame] | 2504 | |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2505 | if (host->part_buf_count == 8) { |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2506 | mci_fifo_writeq(host->fifo_reg, host->part_buf); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2507 | host->part_buf_count = 0; |
| 2508 | } |
| 2509 | } |
| 2510 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 2511 | if (unlikely((unsigned long)buf & 0x7)) { |
| 2512 | while (cnt >= 8) { |
| 2513 | u64 aligned_buf[16]; |
| 2514 | int len = min(cnt & -8, (int)sizeof(aligned_buf)); |
| 2515 | int items = len >> 3; |
| 2516 | int i; |
| 2517 | /* memcpy from input buffer into aligned buffer */ |
| 2518 | memcpy(aligned_buf, buf, len); |
| 2519 | buf += len; |
| 2520 | cnt -= len; |
| 2521 | /* push data from aligned buffer into fifo */ |
| 2522 | for (i = 0; i < items; ++i) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2523 | mci_fifo_writeq(host->fifo_reg, aligned_buf[i]); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2524 | } |
| 2525 | } else |
| 2526 | #endif |
| 2527 | { |
| 2528 | u64 *pdata = buf; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2529 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2530 | for (; cnt >= 8; cnt -= 8) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2531 | mci_fifo_writeq(host->fifo_reg, *pdata++); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2532 | buf = pdata; |
| 2533 | } |
| 2534 | /* put anything remaining in the part_buf */ |
| 2535 | if (cnt) { |
| 2536 | dw_mci_set_part_bytes(host, buf, cnt); |
Markos Chandras | cfbeb59c | 2013-03-12 10:53:13 +0000 | [diff] [blame] | 2537 | /* Push data if we have reached the expected data length */ |
| 2538 | if ((data->bytes_xfered + init_cnt) == |
| 2539 | (data->blksz * data->blocks)) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2540 | mci_fifo_writeq(host->fifo_reg, host->part_buf); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2541 | } |
| 2542 | } |
| 2543 | |
| 2544 | static void dw_mci_pull_data64(struct dw_mci *host, void *buf, int cnt) |
| 2545 | { |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2546 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
| 2547 | if (unlikely((unsigned long)buf & 0x7)) { |
| 2548 | while (cnt >= 8) { |
| 2549 | /* pull data from fifo into aligned buffer */ |
| 2550 | u64 aligned_buf[16]; |
| 2551 | int len = min(cnt & -8, (int)sizeof(aligned_buf)); |
| 2552 | int items = len >> 3; |
| 2553 | int i; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2554 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2555 | for (i = 0; i < items; ++i) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2556 | aligned_buf[i] = mci_fifo_readq(host->fifo_reg); |
| 2557 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2558 | /* memcpy from aligned buffer into output buffer */ |
| 2559 | memcpy(buf, aligned_buf, len); |
| 2560 | buf += len; |
| 2561 | cnt -= len; |
| 2562 | } |
| 2563 | } else |
| 2564 | #endif |
| 2565 | { |
| 2566 | u64 *pdata = buf; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2567 | |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2568 | for (; cnt >= 8; cnt -= 8) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2569 | *pdata++ = mci_fifo_readq(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2570 | buf = pdata; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2571 | } |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2572 | if (cnt) { |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 2573 | host->part_buf = mci_fifo_readq(host->fifo_reg); |
James Hogan | 34b664a | 2011-06-24 13:57:56 +0100 | [diff] [blame] | 2574 | dw_mci_pull_final_bytes(host, buf, cnt); |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | static void dw_mci_pull_data(struct dw_mci *host, void *buf, int cnt) |
| 2579 | { |
| 2580 | int len; |
| 2581 | |
| 2582 | /* get remaining partial bytes */ |
| 2583 | len = dw_mci_pull_part_bytes(host, buf, cnt); |
| 2584 | if (unlikely(len == cnt)) |
| 2585 | return; |
| 2586 | buf += len; |
| 2587 | cnt -= len; |
| 2588 | |
| 2589 | /* get the rest of the data */ |
| 2590 | host->pull_data(host, buf, cnt); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2591 | } |
| 2592 | |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 2593 | static void dw_mci_read_data_pio(struct dw_mci *host, bool dto) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2594 | { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2595 | struct sg_mapping_iter *sg_miter = &host->sg_miter; |
| 2596 | void *buf; |
| 2597 | unsigned int offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2598 | struct mmc_data *data = host->data; |
| 2599 | int shift = host->data_shift; |
| 2600 | u32 status; |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 2601 | unsigned int len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2602 | unsigned int remain, fcnt; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2603 | |
| 2604 | do { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2605 | if (!sg_miter_next(sg_miter)) |
| 2606 | goto done; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2607 | |
Imre Deak | 4225fc8 | 2013-02-27 17:02:57 -0800 | [diff] [blame] | 2608 | host->sg = sg_miter->piter.sg; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2609 | buf = sg_miter->addr; |
| 2610 | remain = sg_miter->length; |
| 2611 | offset = 0; |
| 2612 | |
| 2613 | do { |
| 2614 | fcnt = (SDMMC_GET_FCNT(mci_readl(host, STATUS)) |
| 2615 | << shift) + host->part_buf_count; |
| 2616 | len = min(remain, fcnt); |
| 2617 | if (!len) |
| 2618 | break; |
| 2619 | dw_mci_pull_data(host, (void *)(buf + offset), len); |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 2620 | data->bytes_xfered += len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2621 | offset += len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2622 | remain -= len; |
| 2623 | } while (remain); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2624 | |
Seungwon Jeon | e74f3a9 | 2012-08-01 09:30:46 +0900 | [diff] [blame] | 2625 | sg_miter->consumed = offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2626 | status = mci_readl(host, MINTSTS); |
| 2627 | mci_writel(host, RINTSTS, SDMMC_INT_RXDR); |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 2628 | /* if the RXDR is ready read again */ |
| 2629 | } while ((status & SDMMC_INT_RXDR) || |
| 2630 | (dto && SDMMC_GET_FCNT(mci_readl(host, STATUS)))); |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2631 | |
| 2632 | if (!remain) { |
| 2633 | if (!sg_miter_next(sg_miter)) |
| 2634 | goto done; |
| 2635 | sg_miter->consumed = 0; |
| 2636 | } |
| 2637 | sg_miter_stop(sg_miter); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2638 | return; |
| 2639 | |
| 2640 | done: |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2641 | sg_miter_stop(sg_miter); |
| 2642 | host->sg = NULL; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2643 | smp_wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2644 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 2645 | } |
| 2646 | |
| 2647 | static void dw_mci_write_data_pio(struct dw_mci *host) |
| 2648 | { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2649 | struct sg_mapping_iter *sg_miter = &host->sg_miter; |
| 2650 | void *buf; |
| 2651 | unsigned int offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2652 | struct mmc_data *data = host->data; |
| 2653 | int shift = host->data_shift; |
| 2654 | u32 status; |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 2655 | unsigned int len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2656 | unsigned int fifo_depth = host->fifo_depth; |
| 2657 | unsigned int remain, fcnt; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2658 | |
| 2659 | do { |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2660 | if (!sg_miter_next(sg_miter)) |
| 2661 | goto done; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2662 | |
Imre Deak | 4225fc8 | 2013-02-27 17:02:57 -0800 | [diff] [blame] | 2663 | host->sg = sg_miter->piter.sg; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2664 | buf = sg_miter->addr; |
| 2665 | remain = sg_miter->length; |
| 2666 | offset = 0; |
| 2667 | |
| 2668 | do { |
| 2669 | fcnt = ((fifo_depth - |
| 2670 | SDMMC_GET_FCNT(mci_readl(host, STATUS))) |
| 2671 | << shift) - host->part_buf_count; |
| 2672 | len = min(remain, fcnt); |
| 2673 | if (!len) |
| 2674 | break; |
| 2675 | host->push_data(host, (void *)(buf + offset), len); |
Markos Chandras | 3e4b0d8 | 2013-03-22 12:50:05 -0400 | [diff] [blame] | 2676 | data->bytes_xfered += len; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2677 | offset += len; |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2678 | remain -= len; |
| 2679 | } while (remain); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2680 | |
Seungwon Jeon | e74f3a9 | 2012-08-01 09:30:46 +0900 | [diff] [blame] | 2681 | sg_miter->consumed = offset; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2682 | status = mci_readl(host, MINTSTS); |
| 2683 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2684 | } while (status & SDMMC_INT_TXDR); /* if TXDR write again */ |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2685 | |
| 2686 | if (!remain) { |
| 2687 | if (!sg_miter_next(sg_miter)) |
| 2688 | goto done; |
| 2689 | sg_miter->consumed = 0; |
| 2690 | } |
| 2691 | sg_miter_stop(sg_miter); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2692 | return; |
| 2693 | |
| 2694 | done: |
Seungwon Jeon | f9c2a0d | 2012-02-09 14:32:43 +0900 | [diff] [blame] | 2695 | sg_miter_stop(sg_miter); |
| 2696 | host->sg = NULL; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2697 | smp_wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2698 | set_bit(EVENT_XFER_COMPLETE, &host->pending_events); |
| 2699 | } |
| 2700 | |
| 2701 | static void dw_mci_cmd_interrupt(struct dw_mci *host, u32 status) |
| 2702 | { |
Douglas Anderson | 0363b12 | 2017-10-12 13:11:14 -0700 | [diff] [blame] | 2703 | del_timer(&host->cto_timer); |
| 2704 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2705 | if (!host->cmd_status) |
| 2706 | host->cmd_status = status; |
| 2707 | |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2708 | smp_wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2709 | |
| 2710 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 2711 | tasklet_schedule(&host->tasklet); |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 2712 | |
| 2713 | dw_mci_start_fault_timer(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2714 | } |
| 2715 | |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2716 | static void dw_mci_handle_cd(struct dw_mci *host) |
| 2717 | { |
Jaehoon Chung | b23475f | 2017-06-05 13:41:32 +0900 | [diff] [blame] | 2718 | struct dw_mci_slot *slot = host->slot; |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2719 | |
Jaehoon Chung | 5887024 | 2017-06-05 13:41:31 +0900 | [diff] [blame] | 2720 | mmc_detect_change(slot->mmc, |
| 2721 | msecs_to_jiffies(host->pdata->detect_delay_ms)); |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2722 | } |
| 2723 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2724 | static irqreturn_t dw_mci_interrupt(int irq, void *dev_id) |
| 2725 | { |
| 2726 | struct dw_mci *host = dev_id; |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2727 | u32 pending; |
Jaehoon Chung | b23475f | 2017-06-05 13:41:32 +0900 | [diff] [blame] | 2728 | struct dw_mci_slot *slot = host->slot; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2729 | |
Markos Chandras | 1fb5f68 | 2013-03-12 10:53:11 +0000 | [diff] [blame] | 2730 | pending = mci_readl(host, MINTSTS); /* read-only mask reg */ |
| 2731 | |
| 2732 | if (pending) { |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 2733 | /* Check volt switch first, since it can look like an error */ |
| 2734 | if ((host->state == STATE_SENDING_CMD11) && |
| 2735 | (pending & SDMMC_INT_VOLT_SWITCH)) { |
| 2736 | mci_writel(host, RINTSTS, SDMMC_INT_VOLT_SWITCH); |
| 2737 | pending &= ~SDMMC_INT_VOLT_SWITCH; |
Doug Anderson | 49ba030 | 2015-04-03 11:13:07 -0700 | [diff] [blame] | 2738 | |
| 2739 | /* |
| 2740 | * Hold the lock; we know cmd11_timer can't be kicked |
| 2741 | * off after the lock is released, so safe to delete. |
| 2742 | */ |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2743 | spin_lock(&host->irq_lock); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 2744 | dw_mci_cmd_interrupt(host, pending); |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2745 | spin_unlock(&host->irq_lock); |
Doug Anderson | 49ba030 | 2015-04-03 11:13:07 -0700 | [diff] [blame] | 2746 | |
| 2747 | del_timer(&host->cmd11_timer); |
Doug Anderson | 0173055 | 2014-08-22 19:17:51 +0530 | [diff] [blame] | 2748 | } |
| 2749 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2750 | if (pending & DW_MCI_CMD_ERROR_FLAGS) { |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2751 | spin_lock(&host->irq_lock); |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2752 | |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 2753 | del_timer(&host->cto_timer); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2754 | mci_writel(host, RINTSTS, DW_MCI_CMD_ERROR_FLAGS); |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2755 | host->cmd_status = pending; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2756 | smp_wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2757 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2758 | |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2759 | spin_unlock(&host->irq_lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | if (pending & DW_MCI_DATA_ERROR_FLAGS) { |
Vincent Whitchurch | 26391e4 | 2021-08-25 13:42:13 +0200 | [diff] [blame] | 2763 | spin_lock(&host->irq_lock); |
| 2764 | |
MÃ¥rten Lindahl | 1a6fe7b | 2021-12-20 12:30:26 +0100 | [diff] [blame] | 2765 | if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT) |
| 2766 | del_timer(&host->dto_timer); |
| 2767 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2768 | /* if there is an error report DATA_ERROR */ |
| 2769 | mci_writel(host, RINTSTS, DW_MCI_DATA_ERROR_FLAGS); |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2770 | host->data_status = pending; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2771 | smp_wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2772 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
MÃ¥rten Lindahl | 1a6fe7b | 2021-12-20 12:30:26 +0100 | [diff] [blame] | 2773 | |
| 2774 | if (host->quirks & DW_MMC_QUIRK_EXTENDED_TMOUT) |
| 2775 | /* In case of error, we cannot expect a DTO */ |
| 2776 | set_bit(EVENT_DATA_COMPLETE, |
| 2777 | &host->pending_events); |
| 2778 | |
Seungwon Jeon | 9b2026a | 2012-08-01 09:30:40 +0900 | [diff] [blame] | 2779 | tasklet_schedule(&host->tasklet); |
Vincent Whitchurch | 26391e4 | 2021-08-25 13:42:13 +0200 | [diff] [blame] | 2780 | |
| 2781 | spin_unlock(&host->irq_lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2782 | } |
| 2783 | |
| 2784 | if (pending & SDMMC_INT_DATA_OVER) { |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2785 | spin_lock(&host->irq_lock); |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 2786 | |
Jaehoon Chung | 16a3457 | 2016-06-21 14:35:37 +0900 | [diff] [blame] | 2787 | del_timer(&host->dto_timer); |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 2788 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2789 | mci_writel(host, RINTSTS, SDMMC_INT_DATA_OVER); |
| 2790 | if (!host->data_status) |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2791 | host->data_status = pending; |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 2792 | smp_wmb(); /* drain writebuffer */ |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2793 | if (host->dir_status == DW_MCI_RECV_STATUS) { |
| 2794 | if (host->sg != NULL) |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 2795 | dw_mci_read_data_pio(host, true); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2796 | } |
| 2797 | set_bit(EVENT_DATA_COMPLETE, &host->pending_events); |
| 2798 | tasklet_schedule(&host->tasklet); |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 2799 | |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2800 | spin_unlock(&host->irq_lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2801 | } |
| 2802 | |
| 2803 | if (pending & SDMMC_INT_RXDR) { |
| 2804 | mci_writel(host, RINTSTS, SDMMC_INT_RXDR); |
James Hogan | b40af3a | 2011-06-24 13:54:06 +0100 | [diff] [blame] | 2805 | if (host->dir_status == DW_MCI_RECV_STATUS && host->sg) |
Kyoungil Kim | 87a74d3 | 2013-01-22 16:46:30 +0900 | [diff] [blame] | 2806 | dw_mci_read_data_pio(host, false); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | if (pending & SDMMC_INT_TXDR) { |
| 2810 | mci_writel(host, RINTSTS, SDMMC_INT_TXDR); |
James Hogan | b40af3a | 2011-06-24 13:54:06 +0100 | [diff] [blame] | 2811 | if (host->dir_status == DW_MCI_SEND_STATUS && host->sg) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2812 | dw_mci_write_data_pio(host); |
| 2813 | } |
| 2814 | |
| 2815 | if (pending & SDMMC_INT_CMD_DONE) { |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2816 | spin_lock(&host->irq_lock); |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2817 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2818 | mci_writel(host, RINTSTS, SDMMC_INT_CMD_DONE); |
Seungwon Jeon | 182c908 | 2012-08-01 09:30:30 +0900 | [diff] [blame] | 2819 | dw_mci_cmd_interrupt(host, pending); |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 2820 | |
Tian Tao | 9f7d4c9 | 2020-11-06 09:56:53 +0800 | [diff] [blame] | 2821 | spin_unlock(&host->irq_lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2822 | } |
| 2823 | |
| 2824 | if (pending & SDMMC_INT_CD) { |
| 2825 | mci_writel(host, RINTSTS, SDMMC_INT_CD); |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 2826 | dw_mci_handle_cd(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2827 | } |
| 2828 | |
Jaehoon Chung | 5887024 | 2017-06-05 13:41:31 +0900 | [diff] [blame] | 2829 | if (pending & SDMMC_INT_SDIO(slot->sdio_id)) { |
| 2830 | mci_writel(host, RINTSTS, |
| 2831 | SDMMC_INT_SDIO(slot->sdio_id)); |
| 2832 | __dw_mci_enable_sdio_irq(slot, 0); |
| 2833 | sdio_signal_irq(slot->mmc); |
Shashidhar Hiremath | 1a5c8e1 | 2011-08-29 13:11:46 +0530 | [diff] [blame] | 2834 | } |
| 2835 | |
Markos Chandras | 1fb5f68 | 2013-03-12 10:53:11 +0000 | [diff] [blame] | 2836 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2837 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 2838 | if (host->use_dma != TRANS_MODE_IDMAC) |
| 2839 | return IRQ_HANDLED; |
| 2840 | |
| 2841 | /* Handle IDMA interrupts */ |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 2842 | if (host->dma_64bit_address == 1) { |
| 2843 | pending = mci_readl(host, IDSTS64); |
| 2844 | if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { |
| 2845 | mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_TI | |
| 2846 | SDMMC_IDMAC_INT_RI); |
| 2847 | mci_writel(host, IDSTS64, SDMMC_IDMAC_INT_NI); |
Shawn Lin | faecf41 | 2016-06-24 15:39:52 +0800 | [diff] [blame] | 2848 | if (!test_bit(EVENT_DATA_ERROR, &host->pending_events)) |
| 2849 | host->dma_ops->complete((void *)host); |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 2850 | } |
| 2851 | } else { |
| 2852 | pending = mci_readl(host, IDSTS); |
| 2853 | if (pending & (SDMMC_IDMAC_INT_TI | SDMMC_IDMAC_INT_RI)) { |
| 2854 | mci_writel(host, IDSTS, SDMMC_IDMAC_INT_TI | |
| 2855 | SDMMC_IDMAC_INT_RI); |
| 2856 | mci_writel(host, IDSTS, SDMMC_IDMAC_INT_NI); |
Shawn Lin | faecf41 | 2016-06-24 15:39:52 +0800 | [diff] [blame] | 2857 | if (!test_bit(EVENT_DATA_ERROR, &host->pending_events)) |
| 2858 | host->dma_ops->complete((void *)host); |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 2859 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2860 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2861 | |
| 2862 | return IRQ_HANDLED; |
| 2863 | } |
| 2864 | |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2865 | static int dw_mci_init_slot_caps(struct dw_mci_slot *slot) |
| 2866 | { |
| 2867 | struct dw_mci *host = slot->host; |
| 2868 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
| 2869 | struct mmc_host *mmc = slot->mmc; |
| 2870 | int ctrl_id; |
| 2871 | |
| 2872 | if (host->pdata->caps) |
| 2873 | mmc->caps = host->pdata->caps; |
| 2874 | |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2875 | if (host->pdata->pm_caps) |
| 2876 | mmc->pm_caps = host->pdata->pm_caps; |
| 2877 | |
John Keeping | 0dc7a3e | 2021-11-24 18:45:59 +0000 | [diff] [blame] | 2878 | if (drv_data) |
| 2879 | mmc->caps |= drv_data->common_caps; |
| 2880 | |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2881 | if (host->dev->of_node) { |
| 2882 | ctrl_id = of_alias_get_id(host->dev->of_node, "mshc"); |
| 2883 | if (ctrl_id < 0) |
| 2884 | ctrl_id = 0; |
| 2885 | } else { |
| 2886 | ctrl_id = to_platform_device(host->dev)->id; |
| 2887 | } |
Shawn Lin | 0d84b9e | 2018-02-24 14:17:23 +0800 | [diff] [blame] | 2888 | |
| 2889 | if (drv_data && drv_data->caps) { |
| 2890 | if (ctrl_id >= drv_data->num_caps) { |
| 2891 | dev_err(host->dev, "invalid controller id %d\n", |
| 2892 | ctrl_id); |
| 2893 | return -EINVAL; |
| 2894 | } |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2895 | mmc->caps |= drv_data->caps[ctrl_id]; |
Shawn Lin | 0d84b9e | 2018-02-24 14:17:23 +0800 | [diff] [blame] | 2896 | } |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2897 | |
| 2898 | if (host->pdata->caps2) |
| 2899 | mmc->caps2 = host->pdata->caps2; |
| 2900 | |
Jaehoon Chung | 86b93a4 | 2018-02-23 15:41:33 +0900 | [diff] [blame] | 2901 | mmc->f_min = DW_MCI_FREQ_MIN; |
| 2902 | if (!mmc->f_max) |
| 2903 | mmc->f_max = DW_MCI_FREQ_MAX; |
| 2904 | |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2905 | /* Process SDIO IRQs through the sdio_irq_work. */ |
| 2906 | if (mmc->caps & MMC_CAP_SDIO_IRQ) |
| 2907 | mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; |
| 2908 | |
| 2909 | return 0; |
| 2910 | } |
| 2911 | |
Jaehoon Chung | e4a65ef7 | 2017-06-05 13:41:33 +0900 | [diff] [blame] | 2912 | static int dw_mci_init_slot(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2913 | { |
| 2914 | struct mmc_host *mmc; |
| 2915 | struct dw_mci_slot *slot; |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2916 | int ret; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2917 | |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 2918 | mmc = mmc_alloc_host(sizeof(struct dw_mci_slot), host->dev); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2919 | if (!mmc) |
| 2920 | return -ENOMEM; |
| 2921 | |
| 2922 | slot = mmc_priv(mmc); |
Jaehoon Chung | e4a65ef7 | 2017-06-05 13:41:33 +0900 | [diff] [blame] | 2923 | slot->id = 0; |
| 2924 | slot->sdio_id = host->sdio_id0 + slot->id; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2925 | slot->mmc = mmc; |
| 2926 | slot->host = host; |
Jaehoon Chung | b23475f | 2017-06-05 13:41:32 +0900 | [diff] [blame] | 2927 | host->slot = slot; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2928 | |
| 2929 | mmc->ops = &dw_mci_ops; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2930 | |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 2931 | /*if there are external regulators, get them*/ |
| 2932 | ret = mmc_regulator_get_supply(mmc); |
Wolfram Sang | 0f3a47b | 2017-10-14 21:17:11 +0200 | [diff] [blame] | 2933 | if (ret) |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2934 | goto err_host_allocated; |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 2935 | |
| 2936 | if (!mmc->ocr_avail) |
| 2937 | mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2938 | |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2939 | ret = mmc_of_parse(mmc); |
| 2940 | if (ret) |
| 2941 | goto err_host_allocated; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2942 | |
Shawn Lin | a4faa49 | 2018-02-24 14:17:22 +0800 | [diff] [blame] | 2943 | ret = dw_mci_init_slot_caps(slot); |
| 2944 | if (ret) |
| 2945 | goto err_host_allocated; |
Ulf Hansson | 32dba73 | 2017-04-18 13:29:20 +0200 | [diff] [blame] | 2946 | |
Jaehoon Chung | 2b708df | 2015-08-06 16:23:25 +0900 | [diff] [blame] | 2947 | /* Useful defaults if platform data is unset. */ |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 2948 | if (host->use_dma == TRANS_MODE_IDMAC) { |
Jaehoon Chung | 2b708df | 2015-08-06 16:23:25 +0900 | [diff] [blame] | 2949 | mmc->max_segs = host->ring_size; |
Jaehoon Chung | 225faf8 | 2016-05-04 11:24:14 +0900 | [diff] [blame] | 2950 | mmc->max_blk_size = 65535; |
Jaehoon Chung | 2b708df | 2015-08-06 16:23:25 +0900 | [diff] [blame] | 2951 | mmc->max_seg_size = 0x1000; |
| 2952 | mmc->max_req_size = mmc->max_seg_size * host->ring_size; |
| 2953 | mmc->max_blk_count = mmc->max_req_size / 512; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 2954 | } else if (host->use_dma == TRANS_MODE_EDMAC) { |
| 2955 | mmc->max_segs = 64; |
Jaehoon Chung | 225faf8 | 2016-05-04 11:24:14 +0900 | [diff] [blame] | 2956 | mmc->max_blk_size = 65535; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 2957 | mmc->max_blk_count = 65535; |
| 2958 | mmc->max_req_size = |
| 2959 | mmc->max_blk_size * mmc->max_blk_count; |
| 2960 | mmc->max_seg_size = mmc->max_req_size; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2961 | } else { |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 2962 | /* TRANS_MODE_PIO */ |
Jaehoon Chung | 2b708df | 2015-08-06 16:23:25 +0900 | [diff] [blame] | 2963 | mmc->max_segs = 64; |
Jaehoon Chung | 225faf8 | 2016-05-04 11:24:14 +0900 | [diff] [blame] | 2964 | mmc->max_blk_size = 65535; /* BLKSIZ is 16 bits */ |
Jaehoon Chung | 2b708df | 2015-08-06 16:23:25 +0900 | [diff] [blame] | 2965 | mmc->max_blk_count = 512; |
| 2966 | mmc->max_req_size = mmc->max_blk_size * |
| 2967 | mmc->max_blk_count; |
| 2968 | mmc->max_seg_size = mmc->max_req_size; |
Jaehoon Chung | a39e574 | 2012-02-04 17:00:27 -0500 | [diff] [blame] | 2969 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2970 | |
Shawn Lin | c0834a5 | 2016-05-27 14:36:40 +0800 | [diff] [blame] | 2971 | dw_mci_get_cd(mmc); |
Jaehoon Chung | ae0eb34 | 2014-03-03 11:36:48 +0900 | [diff] [blame] | 2972 | |
Jaehoon Chung | 0cea529 | 2013-02-15 23:45:45 +0900 | [diff] [blame] | 2973 | ret = mmc_add_host(mmc); |
| 2974 | if (ret) |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2975 | goto err_host_allocated; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2976 | |
| 2977 | #if defined(CONFIG_DEBUG_FS) |
| 2978 | dw_mci_init_debugfs(slot); |
| 2979 | #endif |
| 2980 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2981 | return 0; |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2982 | |
Doug Anderson | 3cf890f | 2014-08-25 11:19:04 -0700 | [diff] [blame] | 2983 | err_host_allocated: |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 2984 | mmc_free_host(mmc); |
Yuvaraj CD | 51da224 | 2014-08-22 19:17:50 +0530 | [diff] [blame] | 2985 | return ret; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2986 | } |
| 2987 | |
Jaehoon Chung | e4a65ef7 | 2017-06-05 13:41:33 +0900 | [diff] [blame] | 2988 | static void dw_mci_cleanup_slot(struct dw_mci_slot *slot) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2989 | { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2990 | /* Debugfs stuff is cleaned up by mmc core */ |
| 2991 | mmc_remove_host(slot->mmc); |
Jaehoon Chung | b23475f | 2017-06-05 13:41:32 +0900 | [diff] [blame] | 2992 | slot->host->slot = NULL; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 2993 | mmc_free_host(slot->mmc); |
| 2994 | } |
| 2995 | |
| 2996 | static void dw_mci_init_dma(struct dw_mci *host) |
| 2997 | { |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 2998 | int addr_config; |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 2999 | struct device *dev = host->dev; |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 3000 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3001 | /* |
| 3002 | * Check tansfer mode from HCON[17:16] |
| 3003 | * Clear the ambiguous description of dw_mmc databook: |
| 3004 | * 2b'00: No DMA Interface -> Actually means using Internal DMA block |
| 3005 | * 2b'01: DesignWare DMA Interface -> Synopsys DW-DMA block |
| 3006 | * 2b'10: Generic DMA Interface -> non-Synopsys generic DMA block |
| 3007 | * 2b'11: Non DW DMA Interface -> pio only |
| 3008 | * Compared to DesignWare DMA Interface, Generic DMA Interface has a |
| 3009 | * simpler request/acknowledge handshake mechanism and both of them |
| 3010 | * are regarded as external dma master for dw_mmc. |
| 3011 | */ |
| 3012 | host->use_dma = SDMMC_GET_TRANS_MODE(mci_readl(host, HCON)); |
| 3013 | if (host->use_dma == DMA_INTERFACE_IDMA) { |
| 3014 | host->use_dma = TRANS_MODE_IDMAC; |
| 3015 | } else if (host->use_dma == DMA_INTERFACE_DWDMA || |
| 3016 | host->use_dma == DMA_INTERFACE_GDMA) { |
| 3017 | host->use_dma = TRANS_MODE_EDMAC; |
Prabu Thangamuthu | 69d99fd | 2014-10-20 07:12:33 +0000 | [diff] [blame] | 3018 | } else { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3019 | goto no_dma; |
| 3020 | } |
| 3021 | |
| 3022 | /* Determine which DMA interface to use */ |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3023 | if (host->use_dma == TRANS_MODE_IDMAC) { |
| 3024 | /* |
| 3025 | * Check ADDR_CONFIG bit in HCON to find |
| 3026 | * IDMAC address bus width |
| 3027 | */ |
Shawn Lin | 7069275 | 2015-09-16 14:41:37 +0800 | [diff] [blame] | 3028 | addr_config = SDMMC_GET_ADDR_CONFIG(mci_readl(host, HCON)); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3029 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3030 | if (addr_config == 1) { |
| 3031 | /* host supports IDMAC in 64-bit address mode */ |
| 3032 | host->dma_64bit_address = 1; |
| 3033 | dev_info(host->dev, |
| 3034 | "IDMAC supports 64-bit address mode.\n"); |
| 3035 | if (!dma_set_mask(host->dev, DMA_BIT_MASK(64))) |
| 3036 | dma_set_coherent_mask(host->dev, |
| 3037 | DMA_BIT_MASK(64)); |
| 3038 | } else { |
| 3039 | /* host supports IDMAC in 32-bit address mode */ |
| 3040 | host->dma_64bit_address = 0; |
| 3041 | dev_info(host->dev, |
| 3042 | "IDMAC supports 32-bit address mode.\n"); |
| 3043 | } |
| 3044 | |
| 3045 | /* Alloc memory for sg translation */ |
Shawn Lin | cc190d4 | 2016-09-02 12:14:39 +0800 | [diff] [blame] | 3046 | host->sg_cpu = dmam_alloc_coherent(host->dev, |
| 3047 | DESC_RING_BUF_SZ, |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3048 | &host->sg_dma, GFP_KERNEL); |
| 3049 | if (!host->sg_cpu) { |
| 3050 | dev_err(host->dev, |
| 3051 | "%s: could not alloc DMA memory\n", |
| 3052 | __func__); |
| 3053 | goto no_dma; |
| 3054 | } |
| 3055 | |
| 3056 | host->dma_ops = &dw_mci_idmac_ops; |
| 3057 | dev_info(host->dev, "Using internal DMA controller.\n"); |
| 3058 | } else { |
| 3059 | /* TRANS_MODE_EDMAC: check dma bindings again */ |
David Woods | 852ff5f | 2017-05-26 17:53:20 -0400 | [diff] [blame] | 3060 | if ((device_property_read_string_array(dev, "dma-names", |
| 3061 | NULL, 0) < 0) || |
| 3062 | !device_property_present(dev, "dmas")) { |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3063 | goto no_dma; |
| 3064 | } |
| 3065 | host->dma_ops = &dw_mci_edmac_ops; |
| 3066 | dev_info(host->dev, "Using external DMA controller.\n"); |
| 3067 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3068 | |
Jaehoon Chung | e1631f9 | 2012-04-18 15:42:31 +0900 | [diff] [blame] | 3069 | if (host->dma_ops->init && host->dma_ops->start && |
| 3070 | host->dma_ops->stop && host->dma_ops->cleanup) { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3071 | if (host->dma_ops->init(host)) { |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 3072 | dev_err(host->dev, "%s: Unable to initialize DMA Controller.\n", |
| 3073 | __func__); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3074 | goto no_dma; |
| 3075 | } |
| 3076 | } else { |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 3077 | dev_err(host->dev, "DMA initialization not found.\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3078 | goto no_dma; |
| 3079 | } |
| 3080 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3081 | return; |
| 3082 | |
| 3083 | no_dma: |
Thomas Abraham | 4a90920 | 2012-09-17 18:16:35 +0000 | [diff] [blame] | 3084 | dev_info(host->dev, "Using PIO mode.\n"); |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3085 | host->use_dma = TRANS_MODE_PIO; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3086 | } |
| 3087 | |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3088 | static void dw_mci_cmd11_timer(struct timer_list *t) |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 3089 | { |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3090 | struct dw_mci *host = from_timer(host, t, cmd11_timer); |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 3091 | |
Doug Anderson | fd67419 | 2015-04-03 11:13:06 -0700 | [diff] [blame] | 3092 | if (host->state != STATE_SENDING_CMD11) { |
| 3093 | dev_warn(host->dev, "Unexpected CMD11 timeout\n"); |
| 3094 | return; |
| 3095 | } |
Doug Anderson | 5c93516 | 2015-03-09 16:18:21 -0700 | [diff] [blame] | 3096 | |
| 3097 | host->cmd_status = SDMMC_INT_RTO; |
| 3098 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 3099 | tasklet_schedule(&host->tasklet); |
| 3100 | } |
| 3101 | |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3102 | static void dw_mci_cto_timer(struct timer_list *t) |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 3103 | { |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3104 | struct dw_mci *host = from_timer(host, t, cto_timer); |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 3105 | unsigned long irqflags; |
| 3106 | u32 pending; |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 3107 | |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 3108 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 3109 | |
| 3110 | /* |
| 3111 | * If somehow we have very bad interrupt latency it's remotely possible |
| 3112 | * that the timer could fire while the interrupt is still pending or |
| 3113 | * while the interrupt is midway through running. Let's be paranoid |
| 3114 | * and detect those two cases. Note that this is paranoia is somewhat |
| 3115 | * justified because in this function we don't actually cancel the |
| 3116 | * pending command in the controller--we just assume it will never come. |
| 3117 | */ |
| 3118 | pending = mci_readl(host, MINTSTS); /* read-only mask reg */ |
| 3119 | if (pending & (DW_MCI_CMD_ERROR_FLAGS | SDMMC_INT_CMD_DONE)) { |
| 3120 | /* The interrupt should fire; no need to act but we can warn */ |
| 3121 | dev_warn(host->dev, "Unexpected interrupt latency\n"); |
| 3122 | goto exit; |
| 3123 | } |
| 3124 | if (test_bit(EVENT_CMD_COMPLETE, &host->pending_events)) { |
| 3125 | /* Presumably interrupt handler couldn't delete the timer */ |
| 3126 | dev_warn(host->dev, "CTO timeout when already completed\n"); |
| 3127 | goto exit; |
| 3128 | } |
| 3129 | |
| 3130 | /* |
| 3131 | * Continued paranoia to make sure we're in the state we expect. |
| 3132 | * This paranoia isn't really justified but it seems good to be safe. |
| 3133 | */ |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 3134 | switch (host->state) { |
| 3135 | case STATE_SENDING_CMD11: |
| 3136 | case STATE_SENDING_CMD: |
| 3137 | case STATE_SENDING_STOP: |
| 3138 | /* |
| 3139 | * If CMD_DONE interrupt does NOT come in sending command |
| 3140 | * state, we should notify the driver to terminate current |
| 3141 | * transfer and report a command timeout to the core. |
| 3142 | */ |
| 3143 | host->cmd_status = SDMMC_INT_RTO; |
| 3144 | set_bit(EVENT_CMD_COMPLETE, &host->pending_events); |
| 3145 | tasklet_schedule(&host->tasklet); |
| 3146 | break; |
| 3147 | default: |
| 3148 | dev_warn(host->dev, "Unexpected command timeout, state %d\n", |
| 3149 | host->state); |
| 3150 | break; |
| 3151 | } |
Douglas Anderson | 8892b70 | 2017-10-12 13:11:16 -0700 | [diff] [blame] | 3152 | |
| 3153 | exit: |
| 3154 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Addy Ke | 03de192 | 2017-07-11 17:38:37 +0800 | [diff] [blame] | 3155 | } |
| 3156 | |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3157 | static void dw_mci_dto_timer(struct timer_list *t) |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 3158 | { |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3159 | struct dw_mci *host = from_timer(host, t, dto_timer); |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 3160 | unsigned long irqflags; |
| 3161 | u32 pending; |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 3162 | |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 3163 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 3164 | |
| 3165 | /* |
| 3166 | * The DTO timer is much longer than the CTO timer, so it's even less |
| 3167 | * likely that we'll these cases, but it pays to be paranoid. |
| 3168 | */ |
| 3169 | pending = mci_readl(host, MINTSTS); /* read-only mask reg */ |
| 3170 | if (pending & SDMMC_INT_DATA_OVER) { |
| 3171 | /* The interrupt should fire; no need to act but we can warn */ |
| 3172 | dev_warn(host->dev, "Unexpected data interrupt latency\n"); |
| 3173 | goto exit; |
| 3174 | } |
| 3175 | if (test_bit(EVENT_DATA_COMPLETE, &host->pending_events)) { |
| 3176 | /* Presumably interrupt handler couldn't delete the timer */ |
| 3177 | dev_warn(host->dev, "DTO timeout when already completed\n"); |
| 3178 | goto exit; |
| 3179 | } |
| 3180 | |
| 3181 | /* |
| 3182 | * Continued paranoia to make sure we're in the state we expect. |
| 3183 | * This paranoia isn't really justified but it seems good to be safe. |
| 3184 | */ |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 3185 | switch (host->state) { |
| 3186 | case STATE_SENDING_DATA: |
| 3187 | case STATE_DATA_BUSY: |
| 3188 | /* |
| 3189 | * If DTO interrupt does NOT come in sending data state, |
| 3190 | * we should notify the driver to terminate current transfer |
| 3191 | * and report a data timeout to the core. |
| 3192 | */ |
| 3193 | host->data_status = SDMMC_INT_DRTO; |
| 3194 | set_bit(EVENT_DATA_ERROR, &host->pending_events); |
| 3195 | set_bit(EVENT_DATA_COMPLETE, &host->pending_events); |
| 3196 | tasklet_schedule(&host->tasklet); |
| 3197 | break; |
| 3198 | default: |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 3199 | dev_warn(host->dev, "Unexpected data timeout, state %d\n", |
| 3200 | host->state); |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 3201 | break; |
| 3202 | } |
Douglas Anderson | 93c23ae | 2017-10-12 13:11:18 -0700 | [diff] [blame] | 3203 | |
| 3204 | exit: |
| 3205 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 3206 | } |
| 3207 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3208 | #ifdef CONFIG_OF |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3209 | static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host) |
| 3210 | { |
| 3211 | struct dw_mci_board *pdata; |
| 3212 | struct device *dev = host->dev; |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 3213 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Shawn Lin | e8cc37b | 2016-01-21 14:52:52 +0800 | [diff] [blame] | 3214 | int ret; |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 3215 | u32 clock_frequency; |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3216 | |
| 3217 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
Beomho Seo | bf3707e | 2014-12-23 21:07:33 +0900 | [diff] [blame] | 3218 | if (!pdata) |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3219 | return ERR_PTR(-ENOMEM); |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3220 | |
Guodong Xu | d6786fe | 2016-08-12 16:51:26 +0800 | [diff] [blame] | 3221 | /* find reset controller when exist */ |
Philipp Zabel | a93d6f3 | 2017-07-19 17:25:42 +0200 | [diff] [blame] | 3222 | pdata->rstc = devm_reset_control_get_optional_exclusive(dev, "reset"); |
Philipp Zabel | baf6fe4 | 2021-03-05 10:07:24 +0100 | [diff] [blame] | 3223 | if (IS_ERR(pdata->rstc)) |
| 3224 | return ERR_CAST(pdata->rstc); |
Guodong Xu | d6786fe | 2016-08-12 16:51:26 +0800 | [diff] [blame] | 3225 | |
David Woods | 852ff5f | 2017-05-26 17:53:20 -0400 | [diff] [blame] | 3226 | if (device_property_read_u32(dev, "fifo-depth", &pdata->fifo_depth)) |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 3227 | dev_info(dev, |
| 3228 | "fifo-depth property not found, using value of FIFOTH register as default\n"); |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3229 | |
David Woods | 852ff5f | 2017-05-26 17:53:20 -0400 | [diff] [blame] | 3230 | device_property_read_u32(dev, "card-detect-delay", |
| 3231 | &pdata->detect_delay_ms); |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3232 | |
David Woods | 852ff5f | 2017-05-26 17:53:20 -0400 | [diff] [blame] | 3233 | device_property_read_u32(dev, "data-addr", &host->data_addr_override); |
Jun Nie | a0361c1 | 2017-01-11 15:35:35 +0900 | [diff] [blame] | 3234 | |
David Woods | 852ff5f | 2017-05-26 17:53:20 -0400 | [diff] [blame] | 3235 | if (device_property_present(dev, "fifo-watermark-aligned")) |
Jun Nie | d6fced8 | 2017-01-11 15:37:26 +0900 | [diff] [blame] | 3236 | host->wm_aligned = true; |
| 3237 | |
David Woods | 852ff5f | 2017-05-26 17:53:20 -0400 | [diff] [blame] | 3238 | if (!device_property_read_u32(dev, "clock-frequency", &clock_frequency)) |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 3239 | pdata->bus_hz = clock_frequency; |
| 3240 | |
James Hogan | cb27a84 | 2012-10-16 09:43:08 +0100 | [diff] [blame] | 3241 | if (drv_data && drv_data->parse_dt) { |
| 3242 | ret = drv_data->parse_dt(host); |
Thomas Abraham | 800d78b | 2012-09-17 18:16:42 +0000 | [diff] [blame] | 3243 | if (ret) |
| 3244 | return ERR_PTR(ret); |
| 3245 | } |
| 3246 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3247 | return pdata; |
| 3248 | } |
| 3249 | |
| 3250 | #else /* CONFIG_OF */ |
| 3251 | static struct dw_mci_board *dw_mci_parse_dt(struct dw_mci *host) |
| 3252 | { |
| 3253 | return ERR_PTR(-EINVAL); |
| 3254 | } |
| 3255 | #endif /* CONFIG_OF */ |
| 3256 | |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3257 | static void dw_mci_enable_cd(struct dw_mci *host) |
| 3258 | { |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3259 | unsigned long irqflags; |
| 3260 | u32 temp; |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3261 | |
Shawn Lin | e8cc37b | 2016-01-21 14:52:52 +0800 | [diff] [blame] | 3262 | /* |
| 3263 | * No need for CD if all slots have a non-error GPIO |
| 3264 | * as well as broken card detection is found. |
| 3265 | */ |
Jaehoon Chung | e47c0b9 | 2017-06-05 13:41:35 +0900 | [diff] [blame] | 3266 | if (host->slot->mmc->caps & MMC_CAP_NEEDS_POLL) |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3267 | return; |
| 3268 | |
Jaehoon Chung | e47c0b9 | 2017-06-05 13:41:35 +0900 | [diff] [blame] | 3269 | if (mmc_gpio_get_cd(host->slot->mmc) < 0) { |
Jaehoon Chung | 5887024 | 2017-06-05 13:41:31 +0900 | [diff] [blame] | 3270 | spin_lock_irqsave(&host->irq_lock, irqflags); |
| 3271 | temp = mci_readl(host, INTMASK); |
| 3272 | temp |= SDMMC_INT_CD; |
| 3273 | mci_writel(host, INTMASK, temp); |
| 3274 | spin_unlock_irqrestore(&host->irq_lock, irqflags); |
| 3275 | } |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3276 | } |
| 3277 | |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 3278 | int dw_mci_probe(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3279 | { |
Arnd Bergmann | e95baf1 | 2012-11-08 14:26:11 +0000 | [diff] [blame] | 3280 | const struct dw_mci_drv_data *drv_data = host->drv_data; |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 3281 | int width, i, ret = 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3282 | u32 fifo_size; |
| 3283 | |
Thomas Abraham | c91eab4 | 2012-09-17 18:16:40 +0000 | [diff] [blame] | 3284 | if (!host->pdata) { |
| 3285 | host->pdata = dw_mci_parse_dt(host); |
Krzysztof Kozlowski | 308d272 | 2020-09-02 21:36:56 +0200 | [diff] [blame] | 3286 | if (IS_ERR(host->pdata)) |
| 3287 | return dev_err_probe(host->dev, PTR_ERR(host->pdata), |
| 3288 | "platform data not available\n"); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3289 | } |
| 3290 | |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 3291 | host->biu_clk = devm_clk_get(host->dev, "biu"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3292 | if (IS_ERR(host->biu_clk)) { |
| 3293 | dev_dbg(host->dev, "biu clock not available\n"); |
| 3294 | } else { |
| 3295 | ret = clk_prepare_enable(host->biu_clk); |
| 3296 | if (ret) { |
| 3297 | dev_err(host->dev, "failed to enable biu clock\n"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3298 | return ret; |
| 3299 | } |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3300 | } |
| 3301 | |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 3302 | host->ciu_clk = devm_clk_get(host->dev, "ciu"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3303 | if (IS_ERR(host->ciu_clk)) { |
| 3304 | dev_dbg(host->dev, "ciu clock not available\n"); |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 3305 | host->bus_hz = host->pdata->bus_hz; |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3306 | } else { |
| 3307 | ret = clk_prepare_enable(host->ciu_clk); |
| 3308 | if (ret) { |
| 3309 | dev_err(host->dev, "failed to enable ciu clock\n"); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3310 | goto err_clk_biu; |
| 3311 | } |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3312 | |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 3313 | if (host->pdata->bus_hz) { |
| 3314 | ret = clk_set_rate(host->ciu_clk, host->pdata->bus_hz); |
| 3315 | if (ret) |
| 3316 | dev_warn(host->dev, |
Jaehoon Chung | 612de4c | 2014-03-03 11:36:42 +0900 | [diff] [blame] | 3317 | "Unable to set bus rate to %uHz\n", |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 3318 | host->pdata->bus_hz); |
| 3319 | } |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3320 | host->bus_hz = clk_get_rate(host->ciu_clk); |
Doug Anderson | 3c6d89e | 2013-06-07 10:28:30 -0700 | [diff] [blame] | 3321 | } |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3322 | |
Jaehoon Chung | 612de4c | 2014-03-03 11:36:42 +0900 | [diff] [blame] | 3323 | if (!host->bus_hz) { |
| 3324 | dev_err(host->dev, |
| 3325 | "Platform data must supply bus speed\n"); |
| 3326 | ret = -ENODEV; |
| 3327 | goto err_clk_ciu; |
| 3328 | } |
| 3329 | |
Philipp Zabel | baf6fe4 | 2021-03-05 10:07:24 +0100 | [diff] [blame] | 3330 | if (host->pdata->rstc) { |
liwei | 941e372 | 2017-08-11 16:06:23 +0800 | [diff] [blame] | 3331 | reset_control_assert(host->pdata->rstc); |
| 3332 | usleep_range(10, 50); |
| 3333 | reset_control_deassert(host->pdata->rstc); |
| 3334 | } |
| 3335 | |
Yuvaraj Kumar C D | 002f0d5 | 2013-08-31 00:12:19 +0900 | [diff] [blame] | 3336 | if (drv_data && drv_data->init) { |
| 3337 | ret = drv_data->init(host); |
| 3338 | if (ret) { |
| 3339 | dev_err(host->dev, |
| 3340 | "implementation specific init failed\n"); |
| 3341 | goto err_clk_ciu; |
| 3342 | } |
| 3343 | } |
| 3344 | |
Kees Cook | 3797772 | 2017-10-30 14:45:00 -0700 | [diff] [blame] | 3345 | timer_setup(&host->cmd11_timer, dw_mci_cmd11_timer, 0); |
| 3346 | timer_setup(&host->cto_timer, dw_mci_cto_timer, 0); |
| 3347 | timer_setup(&host->dto_timer, dw_mci_dto_timer, 0); |
Addy Ke | 57e1048 | 2015-08-11 01:27:18 +0900 | [diff] [blame] | 3348 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3349 | spin_lock_init(&host->lock); |
Doug Anderson | f8c58c1 | 2014-12-02 15:42:47 -0800 | [diff] [blame] | 3350 | spin_lock_init(&host->irq_lock); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3351 | INIT_LIST_HEAD(&host->queue); |
| 3352 | |
Vincent Whitchurch | 2b8ac06 | 2021-07-01 10:05:34 +0200 | [diff] [blame] | 3353 | dw_mci_init_fault(host); |
| 3354 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3355 | /* |
| 3356 | * Get the host data width - this assumes that HCON has been set with |
| 3357 | * the correct values. |
| 3358 | */ |
Shawn Lin | 7069275 | 2015-09-16 14:41:37 +0800 | [diff] [blame] | 3359 | i = SDMMC_GET_HDATA_WIDTH(mci_readl(host, HCON)); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3360 | if (!i) { |
| 3361 | host->push_data = dw_mci_push_data16; |
| 3362 | host->pull_data = dw_mci_pull_data16; |
| 3363 | width = 16; |
| 3364 | host->data_shift = 1; |
| 3365 | } else if (i == 2) { |
| 3366 | host->push_data = dw_mci_push_data64; |
| 3367 | host->pull_data = dw_mci_pull_data64; |
| 3368 | width = 64; |
| 3369 | host->data_shift = 3; |
| 3370 | } else { |
| 3371 | /* Check for a reserved value, and warn if it is */ |
| 3372 | WARN((i != 1), |
| 3373 | "HCON reports a reserved host data width!\n" |
| 3374 | "Defaulting to 32-bit access.\n"); |
| 3375 | host->push_data = dw_mci_push_data32; |
| 3376 | host->pull_data = dw_mci_pull_data32; |
| 3377 | width = 32; |
| 3378 | host->data_shift = 2; |
| 3379 | } |
| 3380 | |
| 3381 | /* Reset all blocks */ |
Shawn Lin | 3744415 | 2016-01-22 15:43:12 +0800 | [diff] [blame] | 3382 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) { |
| 3383 | ret = -ENODEV; |
| 3384 | goto err_clk_ciu; |
| 3385 | } |
Seungwon Jeon | 141a712 | 2012-05-22 13:01:03 +0900 | [diff] [blame] | 3386 | |
| 3387 | host->dma_ops = host->pdata->dma_ops; |
| 3388 | dw_mci_init_dma(host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3389 | |
| 3390 | /* Clear the interrupts for the host controller */ |
| 3391 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 3392 | mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ |
| 3393 | |
| 3394 | /* Put in max timeout */ |
| 3395 | mci_writel(host, TMOUT, 0xFFFFFFFF); |
| 3396 | |
| 3397 | /* |
| 3398 | * FIFO threshold settings RxMark = fifo_size / 2 - 1, |
| 3399 | * Tx Mark = fifo_size / 2 DMA Size = 8 |
| 3400 | */ |
James Hogan | b86d825 | 2011-06-24 13:57:18 +0100 | [diff] [blame] | 3401 | if (!host->pdata->fifo_depth) { |
| 3402 | /* |
| 3403 | * Power-on value of RX_WMark is FIFO_DEPTH-1, but this may |
| 3404 | * have been overwritten by the bootloader, just like we're |
| 3405 | * about to do, so if you know the value for your hardware, you |
| 3406 | * should put it in the platform data. |
| 3407 | */ |
| 3408 | fifo_size = mci_readl(host, FIFOTH); |
Jaehoon Chung | 8234e86 | 2012-01-11 09:28:21 +0000 | [diff] [blame] | 3409 | fifo_size = 1 + ((fifo_size >> 16) & 0xfff); |
James Hogan | b86d825 | 2011-06-24 13:57:18 +0100 | [diff] [blame] | 3410 | } else { |
| 3411 | fifo_size = host->pdata->fifo_depth; |
| 3412 | } |
| 3413 | host->fifo_depth = fifo_size; |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 3414 | host->fifoth_val = |
| 3415 | SDMMC_SET_FIFOTH(0x2, fifo_size / 2 - 1, fifo_size / 2); |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 3416 | mci_writel(host, FIFOTH, host->fifoth_val); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3417 | |
| 3418 | /* disable clock to CIU */ |
| 3419 | mci_writel(host, CLKENA, 0); |
| 3420 | mci_writel(host, CLKSRC, 0); |
| 3421 | |
James Hogan | 6300876 | 2013-03-12 10:43:54 +0000 | [diff] [blame] | 3422 | /* |
| 3423 | * In 2.40a spec, Data offset is changed. |
| 3424 | * Need to check the version-id and set data-offset for DATA register. |
| 3425 | */ |
| 3426 | host->verid = SDMMC_GET_VERID(mci_readl(host, VERID)); |
| 3427 | dev_info(host->dev, "Version ID is %04x\n", host->verid); |
| 3428 | |
Jun Nie | a0361c1 | 2017-01-11 15:35:35 +0900 | [diff] [blame] | 3429 | if (host->data_addr_override) |
| 3430 | host->fifo_reg = host->regs + host->data_addr_override; |
| 3431 | else if (host->verid < DW_MMC_240A) |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 3432 | host->fifo_reg = host->regs + DATA_OFFSET; |
James Hogan | 6300876 | 2013-03-12 10:43:54 +0000 | [diff] [blame] | 3433 | else |
Ben Dooks | 76184ac | 2015-03-25 11:27:52 +0000 | [diff] [blame] | 3434 | host->fifo_reg = host->regs + DATA_240A_OFFSET; |
James Hogan | 6300876 | 2013-03-12 10:43:54 +0000 | [diff] [blame] | 3435 | |
Emil Renner Berthing | 6078df1 | 2021-02-04 16:18:41 +0100 | [diff] [blame] | 3436 | tasklet_setup(&host->tasklet, dw_mci_tasklet_func); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 3437 | ret = devm_request_irq(host->dev, host->irq, dw_mci_interrupt, |
| 3438 | host->irq_flags, "dw-mci", host); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3439 | if (ret) |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 3440 | goto err_dmaunmap; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3441 | |
Jaehoon Chung | d30a8f7 | 2017-06-05 13:41:30 +0900 | [diff] [blame] | 3442 | /* |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3443 | * Enable interrupts for command done, data over, data empty, |
Yuvaraj CD | 2da1d7f | 2012-10-08 14:29:51 +0530 | [diff] [blame] | 3444 | * receive ready and error such as transmit, receive timeout, crc error |
| 3445 | */ |
Yuvaraj CD | 2da1d7f | 2012-10-08 14:29:51 +0530 | [diff] [blame] | 3446 | mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | |
| 3447 | SDMMC_INT_TXDR | SDMMC_INT_RXDR | |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3448 | DW_MCI_ERROR_FLAGS); |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 3449 | /* Enable mci interrupt */ |
| 3450 | mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); |
Yuvaraj CD | 2da1d7f | 2012-10-08 14:29:51 +0530 | [diff] [blame] | 3451 | |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 3452 | dev_info(host->dev, |
| 3453 | "DW MMC controller at irq %d,%d bit host data width,%u deep fifo\n", |
Yuvaraj CD | 2da1d7f | 2012-10-08 14:29:51 +0530 | [diff] [blame] | 3454 | host->irq, width, fifo_size); |
| 3455 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3456 | /* We need at least one slot to succeed */ |
Jaehoon Chung | e4a65ef7 | 2017-06-05 13:41:33 +0900 | [diff] [blame] | 3457 | ret = dw_mci_init_slot(host); |
Jaehoon Chung | 5887024 | 2017-06-05 13:41:31 +0900 | [diff] [blame] | 3458 | if (ret) { |
| 3459 | dev_dbg(host->dev, "slot %d init failed\n", i); |
Doug Anderson | 6130e7a | 2014-10-14 09:33:09 -0700 | [diff] [blame] | 3460 | goto err_dmaunmap; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3461 | } |
| 3462 | |
Doug Anderson | b793f65 | 2015-03-11 15:15:14 -0700 | [diff] [blame] | 3463 | /* Now that slots are all setup, we can enable card detect */ |
| 3464 | dw_mci_enable_cd(host); |
| 3465 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3466 | return 0; |
| 3467 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3468 | err_dmaunmap: |
| 3469 | if (host->use_dma && host->dma_ops->exit) |
| 3470 | host->dma_ops->exit(host); |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3471 | |
Philipp Zabel | baf6fe4 | 2021-03-05 10:07:24 +0100 | [diff] [blame] | 3472 | reset_control_assert(host->pdata->rstc); |
Guodong Xu | d6786fe | 2016-08-12 16:51:26 +0800 | [diff] [blame] | 3473 | |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3474 | err_clk_ciu: |
Jaehoon Chung | 7037f3b | 2016-07-15 10:54:08 +0900 | [diff] [blame] | 3475 | clk_disable_unprepare(host->ciu_clk); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 3476 | |
Thomas Abraham | f90a061 | 2012-09-17 18:16:38 +0000 | [diff] [blame] | 3477 | err_clk_biu: |
Jaehoon Chung | 7037f3b | 2016-07-15 10:54:08 +0900 | [diff] [blame] | 3478 | clk_disable_unprepare(host->biu_clk); |
Seungwon Jeon | 780f22a | 2012-11-28 19:26:03 +0900 | [diff] [blame] | 3479 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3480 | return ret; |
| 3481 | } |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 3482 | EXPORT_SYMBOL(dw_mci_probe); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3483 | |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 3484 | void dw_mci_remove(struct dw_mci *host) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3485 | { |
Jaehoon Chung | e4a65ef7 | 2017-06-05 13:41:33 +0900 | [diff] [blame] | 3486 | dev_dbg(host->dev, "remove slot\n"); |
Jaehoon Chung | b23475f | 2017-06-05 13:41:32 +0900 | [diff] [blame] | 3487 | if (host->slot) |
Jaehoon Chung | e4a65ef7 | 2017-06-05 13:41:33 +0900 | [diff] [blame] | 3488 | dw_mci_cleanup_slot(host->slot); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3489 | |
Prabu Thangamuthu | 048fd7e | 2015-05-28 12:21:06 +0000 | [diff] [blame] | 3490 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 3491 | mci_writel(host, INTMASK, 0); /* disable all mmc interrupt first */ |
| 3492 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3493 | /* disable clock to CIU */ |
| 3494 | mci_writel(host, CLKENA, 0); |
| 3495 | mci_writel(host, CLKSRC, 0); |
| 3496 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3497 | if (host->use_dma && host->dma_ops->exit) |
| 3498 | host->dma_ops->exit(host); |
| 3499 | |
Philipp Zabel | baf6fe4 | 2021-03-05 10:07:24 +0100 | [diff] [blame] | 3500 | reset_control_assert(host->pdata->rstc); |
Guodong Xu | d6786fe | 2016-08-12 16:51:26 +0800 | [diff] [blame] | 3501 | |
Jaehoon Chung | 7037f3b | 2016-07-15 10:54:08 +0900 | [diff] [blame] | 3502 | clk_disable_unprepare(host->ciu_clk); |
| 3503 | clk_disable_unprepare(host->biu_clk); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3504 | } |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 3505 | EXPORT_SYMBOL(dw_mci_remove); |
| 3506 | |
| 3507 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3508 | |
Shawn Lin | e9ed883 | 2016-10-12 10:50:35 +0800 | [diff] [blame] | 3509 | #ifdef CONFIG_PM |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3510 | int dw_mci_runtime_suspend(struct device *dev) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3511 | { |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3512 | struct dw_mci *host = dev_get_drvdata(dev); |
| 3513 | |
Shawn Lin | 3fc7eae | 2015-09-16 14:41:23 +0800 | [diff] [blame] | 3514 | if (host->use_dma && host->dma_ops->exit) |
| 3515 | host->dma_ops->exit(host); |
| 3516 | |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3517 | clk_disable_unprepare(host->ciu_clk); |
| 3518 | |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 3519 | if (host->slot && |
| 3520 | (mmc_can_gpio_cd(host->slot->mmc) || |
| 3521 | !mmc_card_is_removable(host->slot->mmc))) |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3522 | clk_disable_unprepare(host->biu_clk); |
| 3523 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3524 | return 0; |
| 3525 | } |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3526 | EXPORT_SYMBOL(dw_mci_runtime_suspend); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3527 | |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3528 | int dw_mci_runtime_resume(struct device *dev) |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3529 | { |
Jaehoon Chung | b23475f | 2017-06-05 13:41:32 +0900 | [diff] [blame] | 3530 | int ret = 0; |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3531 | struct dw_mci *host = dev_get_drvdata(dev); |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3532 | |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 3533 | if (host->slot && |
| 3534 | (mmc_can_gpio_cd(host->slot->mmc) || |
| 3535 | !mmc_card_is_removable(host->slot->mmc))) { |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3536 | ret = clk_prepare_enable(host->biu_clk); |
| 3537 | if (ret) |
| 3538 | return ret; |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 3539 | } |
| 3540 | |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3541 | ret = clk_prepare_enable(host->ciu_clk); |
| 3542 | if (ret) |
Joonyoung Shim | df9bcc2 | 2016-11-25 12:47:15 +0900 | [diff] [blame] | 3543 | goto err; |
| 3544 | |
| 3545 | if (!dw_mci_ctrl_reset(host, SDMMC_CTRL_ALL_RESET_FLAGS)) { |
| 3546 | clk_disable_unprepare(host->ciu_clk); |
| 3547 | ret = -ENODEV; |
| 3548 | goto err; |
| 3549 | } |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3550 | |
Jonathan Kliegman | 3bfe619 | 2012-06-14 13:31:55 -0400 | [diff] [blame] | 3551 | if (host->use_dma && host->dma_ops->init) |
Seungwon Jeon | 141a712 | 2012-05-22 13:01:03 +0900 | [diff] [blame] | 3552 | host->dma_ops->init(host); |
| 3553 | |
Seungwon Jeon | 5242689 | 2013-08-31 00:13:42 +0900 | [diff] [blame] | 3554 | /* |
| 3555 | * Restore the initial value at FIFOTH register |
| 3556 | * And Invalidate the prev_blksz with zero |
| 3557 | */ |
Colin Ian King | 6b62e12 | 2019-09-22 13:54:43 +0200 | [diff] [blame] | 3558 | mci_writel(host, FIFOTH, host->fifoth_val); |
| 3559 | host->prev_blksz = 0; |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 3560 | |
Doug Anderson | 2eb2944 | 2013-08-31 00:11:49 +0900 | [diff] [blame] | 3561 | /* Put in max timeout */ |
| 3562 | mci_writel(host, TMOUT, 0xFFFFFFFF); |
| 3563 | |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 3564 | mci_writel(host, RINTSTS, 0xFFFFFFFF); |
| 3565 | mci_writel(host, INTMASK, SDMMC_INT_CMD_DONE | SDMMC_INT_DATA_OVER | |
| 3566 | SDMMC_INT_TXDR | SDMMC_INT_RXDR | |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3567 | DW_MCI_ERROR_FLAGS); |
Jaehoon Chung | e61cf11 | 2011-03-17 20:32:33 +0900 | [diff] [blame] | 3568 | mci_writel(host, CTRL, SDMMC_CTRL_INT_ENABLE); |
| 3569 | |
Shawn Lin | 0e3a22c | 2015-08-03 15:07:21 +0800 | [diff] [blame] | 3570 | |
Jaehoon Chung | e47c0b9 | 2017-06-05 13:41:35 +0900 | [diff] [blame] | 3571 | if (host->slot->mmc->pm_flags & MMC_PM_KEEP_POWER) |
| 3572 | dw_mci_set_ios(host->slot->mmc, &host->slot->mmc->ios); |
Ziyuan Xu | e9748e0 | 2017-01-17 09:22:56 +0800 | [diff] [blame] | 3573 | |
Jaehoon Chung | 5887024 | 2017-06-05 13:41:31 +0900 | [diff] [blame] | 3574 | /* Force setup bus to guarantee available clock output */ |
Jaehoon Chung | e47c0b9 | 2017-06-05 13:41:35 +0900 | [diff] [blame] | 3575 | dw_mci_setup_bus(host->slot, true); |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3576 | |
Ulf Hansson | 7c52660 | 2019-09-08 12:12:27 +0200 | [diff] [blame] | 3577 | /* Re-enable SDIO interrupts. */ |
| 3578 | if (sdio_irq_claimed(host->slot->mmc)) |
| 3579 | __dw_mci_enable_sdio_irq(host->slot, 1); |
| 3580 | |
Doug Anderson | fa0c328 | 2015-02-25 10:11:51 -0800 | [diff] [blame] | 3581 | /* Now that slots are all setup, we can enable card detect */ |
| 3582 | dw_mci_enable_cd(host); |
| 3583 | |
Joonyoung Shim | df9bcc2 | 2016-11-25 12:47:15 +0900 | [diff] [blame] | 3584 | return 0; |
| 3585 | |
| 3586 | err: |
Jaehoon Chung | 42f989c | 2017-06-05 13:41:34 +0900 | [diff] [blame] | 3587 | if (host->slot && |
| 3588 | (mmc_can_gpio_cd(host->slot->mmc) || |
| 3589 | !mmc_card_is_removable(host->slot->mmc))) |
Joonyoung Shim | df9bcc2 | 2016-11-25 12:47:15 +0900 | [diff] [blame] | 3590 | clk_disable_unprepare(host->biu_clk); |
| 3591 | |
Shawn Lin | ed24e1f | 2016-10-12 10:56:55 +0800 | [diff] [blame] | 3592 | return ret; |
Shawn Lin | e9ed883 | 2016-10-12 10:50:35 +0800 | [diff] [blame] | 3593 | } |
| 3594 | EXPORT_SYMBOL(dw_mci_runtime_resume); |
| 3595 | #endif /* CONFIG_PM */ |
Jaehoon Chung | 6fe8890 | 2011-12-08 19:23:03 +0900 | [diff] [blame] | 3596 | |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3597 | static int __init dw_mci_init(void) |
| 3598 | { |
Sachin Kamat | 8e1c4e4 | 2013-04-04 11:25:11 +0530 | [diff] [blame] | 3599 | pr_info("Synopsys Designware Multimedia Card Interface Driver\n"); |
Shashidhar Hiremath | 62ca803 | 2012-01-13 16:04:57 +0530 | [diff] [blame] | 3600 | return 0; |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3601 | } |
| 3602 | |
| 3603 | static void __exit dw_mci_exit(void) |
| 3604 | { |
Will Newton | f95f385 | 2011-01-02 01:11:59 -0500 | [diff] [blame] | 3605 | } |
| 3606 | |
| 3607 | module_init(dw_mci_init); |
| 3608 | module_exit(dw_mci_exit); |
| 3609 | |
| 3610 | MODULE_DESCRIPTION("DW Multimedia Card Interface driver"); |
| 3611 | MODULE_AUTHOR("NXP Semiconductor VietNam"); |
| 3612 | MODULE_AUTHOR("Imagination Technologies Ltd"); |
| 3613 | MODULE_LICENSE("GPL v2"); |