rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2014-2015 MediaTek Inc. |
| 3 | * Author: Chaotian.Jing <chaotian.jing@mediatek.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/clk.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/dma-mapping.h> |
| 19 | #include <linux/ioport.h> |
| 20 | #include <linux/irq.h> |
| 21 | #include <linux/of_address.h> |
| 22 | #include <linux/of_irq.h> |
| 23 | #include <linux/of_gpio.h> |
| 24 | #include <linux/pinctrl/consumer.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pm.h> |
| 27 | #include <linux/pm_runtime.h> |
| 28 | #include <linux/regulator/consumer.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/spinlock.h> |
| 31 | #include <linux/interrupt.h> |
| 32 | |
| 33 | #include <linux/mmc/card.h> |
| 34 | #include <linux/mmc/core.h> |
| 35 | #include <linux/mmc/host.h> |
| 36 | #include <linux/mmc/mmc.h> |
| 37 | #include <linux/mmc/sd.h> |
| 38 | #include <linux/mmc/sdio.h> |
| 39 | #include <linux/mmc/slot-gpio.h> |
| 40 | |
| 41 | #define MAX_BD_NUM 1024 |
| 42 | |
| 43 | /*--------------------------------------------------------------------------*/ |
| 44 | /* Common Definition */ |
| 45 | /*--------------------------------------------------------------------------*/ |
| 46 | #define MSDC_BUS_1BITS 0x0 |
| 47 | #define MSDC_BUS_4BITS 0x1 |
| 48 | #define MSDC_BUS_8BITS 0x2 |
| 49 | |
| 50 | #define MSDC_BURST_64B 0x6 |
| 51 | |
| 52 | /*--------------------------------------------------------------------------*/ |
| 53 | /* Register Offset */ |
| 54 | /*--------------------------------------------------------------------------*/ |
| 55 | #define MSDC_CFG 0x0 |
| 56 | #define MSDC_IOCON 0x04 |
| 57 | #define MSDC_PS 0x08 |
| 58 | #define MSDC_INT 0x0c |
| 59 | #define MSDC_INTEN 0x10 |
| 60 | #define MSDC_FIFOCS 0x14 |
| 61 | #define SDC_CFG 0x30 |
| 62 | #define SDC_CMD 0x34 |
| 63 | #define SDC_ARG 0x38 |
| 64 | #define SDC_STS 0x3c |
| 65 | #define SDC_RESP0 0x40 |
| 66 | #define SDC_RESP1 0x44 |
| 67 | #define SDC_RESP2 0x48 |
| 68 | #define SDC_RESP3 0x4c |
| 69 | #define SDC_BLK_NUM 0x50 |
| 70 | #define SDC_ADV_CFG0 0x64 |
| 71 | #define EMMC_IOCON 0x7c |
| 72 | #define SDC_ACMD_RESP 0x80 |
| 73 | #define DMA_SA_H4BIT 0x8c |
| 74 | #define MSDC_DMA_SA 0x90 |
| 75 | #define MSDC_DMA_CTRL 0x98 |
| 76 | #define MSDC_DMA_CFG 0x9c |
| 77 | #define MSDC_PATCH_BIT 0xb0 |
| 78 | #define MSDC_PATCH_BIT1 0xb4 |
| 79 | #define MSDC_PATCH_BIT2 0xb8 |
| 80 | #define MSDC_PAD_TUNE 0xec |
| 81 | #define MSDC_PAD_TUNE0 0xf0 |
| 82 | #define PAD_DS_TUNE 0x188 |
| 83 | #define PAD_CMD_TUNE 0x18c |
| 84 | #define EMMC50_CFG0 0x208 |
| 85 | #define EMMC50_CFG3 0x220 |
| 86 | #define SDC_FIFO_CFG 0x228 |
| 87 | |
| 88 | /*--------------------------------------------------------------------------*/ |
| 89 | /* Top Register Offset */ |
| 90 | /*--------------------------------------------------------------------------*/ |
| 91 | #define EMMC_TOP_CONTROL (0x00) |
| 92 | #define EMMC_TOP_CMD (0x04) |
| 93 | #define EMMC50_PAD_DS_TUNE (0x0c) |
| 94 | |
| 95 | /*--------------------------------------------------------------------------*/ |
| 96 | /* Register Mask */ |
| 97 | /*--------------------------------------------------------------------------*/ |
| 98 | |
| 99 | /* MSDC_CFG mask */ |
| 100 | #define MSDC_CFG_MODE (0x1 << 0) /* RW */ |
| 101 | #define MSDC_CFG_CKPDN (0x1 << 1) /* RW */ |
| 102 | #define MSDC_CFG_RST (0x1 << 2) /* RW */ |
| 103 | #define MSDC_CFG_PIO (0x1 << 3) /* RW */ |
| 104 | #define MSDC_CFG_CKDRVEN (0x1 << 4) /* RW */ |
| 105 | #define MSDC_CFG_BV18SDT (0x1 << 5) /* RW */ |
| 106 | #define MSDC_CFG_BV18PSS (0x1 << 6) /* R */ |
| 107 | #define MSDC_CFG_CKSTB (0x1 << 7) /* R */ |
| 108 | #define MSDC_CFG_CKDIV (0xff << 8) /* RW */ |
| 109 | #define MSDC_CFG_CKMOD (0x3 << 16) /* RW */ |
| 110 | #define MSDC_CFG_HS400_CK_MODE (0x1 << 18) /* RW */ |
| 111 | #define MSDC_CFG_HS400_CK_MODE_EXTRA (0x1 << 22) /* RW */ |
| 112 | #define MSDC_CFG_CKDIV_EXTRA (0xfff << 8) /* RW */ |
| 113 | #define MSDC_CFG_CKMOD_EXTRA (0x3 << 20) /* RW */ |
| 114 | |
| 115 | /* MSDC_IOCON mask */ |
| 116 | #define MSDC_IOCON_SDR104CKS (0x1 << 0) /* RW */ |
| 117 | #define MSDC_IOCON_RSPL (0x1 << 1) /* RW */ |
| 118 | #define MSDC_IOCON_DSPL (0x1 << 2) /* RW */ |
| 119 | #define MSDC_IOCON_DDLSEL (0x1 << 3) /* RW */ |
| 120 | #define MSDC_IOCON_DDR50CKD (0x1 << 4) /* RW */ |
| 121 | #define MSDC_IOCON_DSPLSEL (0x1 << 5) /* RW */ |
| 122 | #define MSDC_IOCON_W_DSPL (0x1 << 8) /* RW */ |
| 123 | #define MSDC_IOCON_D0SPL (0x1 << 16) /* RW */ |
| 124 | #define MSDC_IOCON_D1SPL (0x1 << 17) /* RW */ |
| 125 | #define MSDC_IOCON_D2SPL (0x1 << 18) /* RW */ |
| 126 | #define MSDC_IOCON_D3SPL (0x1 << 19) /* RW */ |
| 127 | #define MSDC_IOCON_D4SPL (0x1 << 20) /* RW */ |
| 128 | #define MSDC_IOCON_D5SPL (0x1 << 21) /* RW */ |
| 129 | #define MSDC_IOCON_D6SPL (0x1 << 22) /* RW */ |
| 130 | #define MSDC_IOCON_D7SPL (0x1 << 23) /* RW */ |
| 131 | #define MSDC_IOCON_RISCSZ (0x3 << 24) /* RW */ |
| 132 | |
| 133 | /* MSDC_PS mask */ |
| 134 | #define MSDC_PS_CDEN (0x1 << 0) /* RW */ |
| 135 | #define MSDC_PS_CDSTS (0x1 << 1) /* R */ |
| 136 | #define MSDC_PS_CDDEBOUNCE (0xf << 12) /* RW */ |
| 137 | #define MSDC_PS_DAT (0xff << 16) /* R */ |
| 138 | #define MSDC_PS_CMD (0x1 << 24) /* R */ |
| 139 | #define MSDC_PS_WP (0x1 << 31) /* R */ |
| 140 | |
| 141 | /* MSDC_INT mask */ |
| 142 | #define MSDC_INT_MMCIRQ (0x1 << 0) /* W1C */ |
| 143 | #define MSDC_INT_CDSC (0x1 << 1) /* W1C */ |
| 144 | #define MSDC_INT_ACMDRDY (0x1 << 3) /* W1C */ |
| 145 | #define MSDC_INT_ACMDTMO (0x1 << 4) /* W1C */ |
| 146 | #define MSDC_INT_ACMDCRCERR (0x1 << 5) /* W1C */ |
| 147 | #define MSDC_INT_DMAQ_EMPTY (0x1 << 6) /* W1C */ |
| 148 | #define MSDC_INT_SDIOIRQ (0x1 << 7) /* W1C */ |
| 149 | #define MSDC_INT_CMDRDY (0x1 << 8) /* W1C */ |
| 150 | #define MSDC_INT_CMDTMO (0x1 << 9) /* W1C */ |
| 151 | #define MSDC_INT_RSPCRCERR (0x1 << 10) /* W1C */ |
| 152 | #define MSDC_INT_CSTA (0x1 << 11) /* R */ |
| 153 | #define MSDC_INT_XFER_COMPL (0x1 << 12) /* W1C */ |
| 154 | #define MSDC_INT_DXFER_DONE (0x1 << 13) /* W1C */ |
| 155 | #define MSDC_INT_DATTMO (0x1 << 14) /* W1C */ |
| 156 | #define MSDC_INT_DATCRCERR (0x1 << 15) /* W1C */ |
| 157 | #define MSDC_INT_ACMD19_DONE (0x1 << 16) /* W1C */ |
| 158 | #define MSDC_INT_DMA_BDCSERR (0x1 << 17) /* W1C */ |
| 159 | #define MSDC_INT_DMA_GPDCSERR (0x1 << 18) /* W1C */ |
| 160 | #define MSDC_INT_DMA_PROTECT (0x1 << 19) /* W1C */ |
| 161 | |
| 162 | /* MSDC_INTEN mask */ |
| 163 | #define MSDC_INTEN_MMCIRQ (0x1 << 0) /* RW */ |
| 164 | #define MSDC_INTEN_CDSC (0x1 << 1) /* RW */ |
| 165 | #define MSDC_INTEN_ACMDRDY (0x1 << 3) /* RW */ |
| 166 | #define MSDC_INTEN_ACMDTMO (0x1 << 4) /* RW */ |
| 167 | #define MSDC_INTEN_ACMDCRCERR (0x1 << 5) /* RW */ |
| 168 | #define MSDC_INTEN_DMAQ_EMPTY (0x1 << 6) /* RW */ |
| 169 | #define MSDC_INTEN_SDIOIRQ (0x1 << 7) /* RW */ |
| 170 | #define MSDC_INTEN_CMDRDY (0x1 << 8) /* RW */ |
| 171 | #define MSDC_INTEN_CMDTMO (0x1 << 9) /* RW */ |
| 172 | #define MSDC_INTEN_RSPCRCERR (0x1 << 10) /* RW */ |
| 173 | #define MSDC_INTEN_CSTA (0x1 << 11) /* RW */ |
| 174 | #define MSDC_INTEN_XFER_COMPL (0x1 << 12) /* RW */ |
| 175 | #define MSDC_INTEN_DXFER_DONE (0x1 << 13) /* RW */ |
| 176 | #define MSDC_INTEN_DATTMO (0x1 << 14) /* RW */ |
| 177 | #define MSDC_INTEN_DATCRCERR (0x1 << 15) /* RW */ |
| 178 | #define MSDC_INTEN_ACMD19_DONE (0x1 << 16) /* RW */ |
| 179 | #define MSDC_INTEN_DMA_BDCSERR (0x1 << 17) /* RW */ |
| 180 | #define MSDC_INTEN_DMA_GPDCSERR (0x1 << 18) /* RW */ |
| 181 | #define MSDC_INTEN_DMA_PROTECT (0x1 << 19) /* RW */ |
| 182 | |
| 183 | /* MSDC_FIFOCS mask */ |
| 184 | #define MSDC_FIFOCS_RXCNT (0xff << 0) /* R */ |
| 185 | #define MSDC_FIFOCS_TXCNT (0xff << 16) /* R */ |
| 186 | #define MSDC_FIFOCS_CLR (0x1 << 31) /* RW */ |
| 187 | |
| 188 | /* SDC_CFG mask */ |
| 189 | #define SDC_CFG_SDIOINTWKUP (0x1 << 0) /* RW */ |
| 190 | #define SDC_CFG_INSWKUP (0x1 << 1) /* RW */ |
| 191 | #define SDC_CFG_BUSWIDTH (0x3 << 16) /* RW */ |
| 192 | #define SDC_CFG_SDIO (0x1 << 19) /* RW */ |
| 193 | #define SDC_CFG_SDIOIDE (0x1 << 20) /* RW */ |
| 194 | #define SDC_CFG_INTATGAP (0x1 << 21) /* RW */ |
| 195 | #define SDC_CFG_DTOC (0xff << 24) /* RW */ |
| 196 | |
| 197 | /* SDC_STS mask */ |
| 198 | #define SDC_STS_SDCBUSY (0x1 << 0) /* RW */ |
| 199 | #define SDC_STS_CMDBUSY (0x1 << 1) /* RW */ |
| 200 | #define SDC_STS_SWR_COMPL (0x1 << 31) /* RW */ |
| 201 | |
| 202 | /* SDC_ADV_CFG0 mask */ |
| 203 | #define SDC_RX_ENHANCE_EN (0x1 << 20) /* RW */ |
| 204 | |
| 205 | /* DMA_SA_H4BIT mask */ |
| 206 | #define DMA_ADDR_HIGH_4BIT (0xf << 0) /* RW */ |
| 207 | |
| 208 | /* MSDC_DMA_CTRL mask */ |
| 209 | #define MSDC_DMA_CTRL_START (0x1 << 0) /* W */ |
| 210 | #define MSDC_DMA_CTRL_STOP (0x1 << 1) /* W */ |
| 211 | #define MSDC_DMA_CTRL_RESUME (0x1 << 2) /* W */ |
| 212 | #define MSDC_DMA_CTRL_MODE (0x1 << 8) /* RW */ |
| 213 | #define MSDC_DMA_CTRL_LASTBUF (0x1 << 10) /* RW */ |
| 214 | #define MSDC_DMA_CTRL_BRUSTSZ (0x7 << 12) /* RW */ |
| 215 | |
| 216 | /* MSDC_DMA_CFG mask */ |
| 217 | #define MSDC_DMA_CFG_STS (0x1 << 0) /* R */ |
| 218 | #define MSDC_DMA_CFG_DECSEN (0x1 << 1) /* RW */ |
| 219 | #define MSDC_DMA_CFG_AHBHPROT2 (0x2 << 8) /* RW */ |
| 220 | #define MSDC_DMA_CFG_ACTIVEEN (0x2 << 12) /* RW */ |
| 221 | #define MSDC_DMA_CFG_CS12B16B (0x1 << 16) /* RW */ |
| 222 | |
| 223 | /* MSDC_PATCH_BIT mask */ |
| 224 | #define MSDC_PATCH_BIT_ODDSUPP (0x1 << 1) /* RW */ |
| 225 | #define MSDC_INT_DAT_LATCH_CK_SEL (0x7 << 7) |
| 226 | #define MSDC_CKGEN_MSDC_DLY_SEL (0x1f << 10) |
| 227 | #define MSDC_PATCH_BIT_IODSSEL (0x1 << 16) /* RW */ |
| 228 | #define MSDC_PATCH_BIT_IOINTSEL (0x1 << 17) /* RW */ |
| 229 | #define MSDC_PATCH_BIT_BUSYDLY (0xf << 18) /* RW */ |
| 230 | #define MSDC_PATCH_BIT_WDOD (0xf << 22) /* RW */ |
| 231 | #define MSDC_PATCH_BIT_IDRTSEL (0x1 << 26) /* RW */ |
| 232 | #define MSDC_PATCH_BIT_CMDFSEL (0x1 << 27) /* RW */ |
| 233 | #define MSDC_PATCH_BIT_INTDLSEL (0x1 << 28) /* RW */ |
| 234 | #define MSDC_PATCH_BIT_SPCPUSH (0x1 << 29) /* RW */ |
| 235 | #define MSDC_PATCH_BIT_DECRCTMO (0x1 << 30) /* RW */ |
| 236 | |
| 237 | #define MSDC_PATCH_BIT1_STOP_DLY (0xf << 8) /* RW */ |
| 238 | #define MSDC_PATCH_BIT1_CMDTA (0x7 << 3) /* RW */ |
| 239 | |
| 240 | #define MSDC_PATCH_BIT2_CFGRESP (0x1 << 15) /* RW */ |
| 241 | #define MSDC_PATCH_BIT2_CFGCRCSTS (0x1 << 28) /* RW */ |
| 242 | #define MSDC_PB2_SUPPORT_64G (0x1 << 1) /* RW */ |
| 243 | #define MSDC_PB2_RESPWAIT (0x3 << 2) /* RW */ |
| 244 | #define MSDC_PB2_RESPSTSENSEL (0x7 << 16) /* RW */ |
| 245 | #define MSDC_PB2_CRCSTSENSEL (0x7 << 29) /* RW */ |
| 246 | |
| 247 | #define MSDC_PAD_TUNE_DATWRDLY (0x1f << 0) /* RW */ |
| 248 | #define MSDC_PAD_TUNE_DATRRDLY (0x1f << 8) /* RW */ |
| 249 | #define MSDC_PAD_TUNE_CMDRDLY (0x1f << 16) /* RW */ |
| 250 | #define MSDC_PAD_TUNE_CMDRRDLY (0x1f << 22) /* RW */ |
| 251 | #define MSDC_PAD_TUNE_CLKTDLY (0x1f << 27) /* RW */ |
| 252 | #define MSDC_PAD_TUNE_RXDLYSEL (0x1 << 15) /* RW */ |
| 253 | #define MSDC_PAD_TUNE_RD_SEL (0x1 << 13) /* RW */ |
| 254 | #define MSDC_PAD_TUNE_CMD_SEL (0x1 << 21) /* RW */ |
| 255 | |
| 256 | #define PAD_DS_TUNE_DLY1 (0x1f << 2) /* RW */ |
| 257 | #define PAD_DS_TUNE_DLY2 (0x1f << 7) /* RW */ |
| 258 | #define PAD_DS_TUNE_DLY3 (0x1f << 12) /* RW */ |
| 259 | |
| 260 | #define PAD_CMD_TUNE_RX_DLY3 (0x1f << 1) /* RW */ |
| 261 | |
| 262 | #define EMMC50_CFG_PADCMD_LATCHCK (0x1 << 0) /* RW */ |
| 263 | #define EMMC50_CFG_CRCSTS_EDGE (0x1 << 3) /* RW */ |
| 264 | #define EMMC50_CFG_CFCSTS_SEL (0x1 << 4) /* RW */ |
| 265 | |
| 266 | #define EMMC50_CFG3_OUTS_WR (0x1f << 0) /* RW */ |
| 267 | |
| 268 | #define SDC_FIFO_CFG_WRVALIDSEL (0x1 << 24) /* RW */ |
| 269 | #define SDC_FIFO_CFG_RDVALIDSEL (0x1 << 25) /* RW */ |
| 270 | |
| 271 | /* EMMC_TOP_CONTROL mask */ |
| 272 | #define PAD_RXDLY_SEL (0x1 << 0) /* RW */ |
| 273 | #define DELAY_EN (0x1 << 1) /* RW */ |
| 274 | #define PAD_DAT_RD_RXDLY2 (0x1F << 2) /* RW */ |
| 275 | #define PAD_DAT_RD_RXDLY (0x1F << 7) /* RW */ |
| 276 | #define PAD_DAT_RD_RXDLY2_SEL (0x1 << 12) /* RW */ |
| 277 | #define PAD_DAT_RD_RXDLY_SEL (0x1 << 13) /* RW */ |
| 278 | #define DATA_K_VALUE_SEL (0x1 << 14) /* RW */ |
| 279 | #define SDC_RX_ENH_EN (0x1 << 15) /* TW */ |
| 280 | |
| 281 | /* EMMC_TOP_CMD mask */ |
| 282 | #define PAD_CMD_RXDLY2 (0x1F << 0) /* RW */ |
| 283 | #define PAD_CMD_RXDLY (0x1F << 5) /* RW */ |
| 284 | #define PAD_CMD_RD_RXDLY2_SEL (0x1 << 10) /* RW */ |
| 285 | #define PAD_CMD_RD_RXDLY_SEL (0x1 << 11) /* RW */ |
| 286 | #define PAD_CMD_TX_DLY (0x1F << 12) /* RW */ |
| 287 | |
| 288 | #define REQ_CMD_EIO (0x1 << 0) |
| 289 | #define REQ_CMD_TMO (0x1 << 1) |
| 290 | #define REQ_DAT_ERR (0x1 << 2) |
| 291 | #define REQ_STOP_EIO (0x1 << 3) |
| 292 | #define REQ_STOP_TMO (0x1 << 4) |
| 293 | #define REQ_CMD_BUSY (0x1 << 5) |
| 294 | |
| 295 | #define MSDC_PREPARE_FLAG (0x1 << 0) |
| 296 | #define MSDC_ASYNC_FLAG (0x1 << 1) |
| 297 | #define MSDC_MMAP_FLAG (0x1 << 2) |
| 298 | |
| 299 | #define MTK_MMC_AUTOSUSPEND_DELAY 50 |
| 300 | #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */ |
| 301 | #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */ |
| 302 | |
| 303 | #define PAD_DELAY_MAX 32 /* PAD delay cells */ |
| 304 | /*--------------------------------------------------------------------------*/ |
| 305 | /* Descriptor Structure */ |
| 306 | /*--------------------------------------------------------------------------*/ |
| 307 | struct mt_gpdma_desc { |
| 308 | u32 gpd_info; |
| 309 | #define GPDMA_DESC_HWO (0x1 << 0) |
| 310 | #define GPDMA_DESC_BDP (0x1 << 1) |
| 311 | #define GPDMA_DESC_CHECKSUM (0xff << 8) /* bit8 ~ bit15 */ |
| 312 | #define GPDMA_DESC_INT (0x1 << 16) |
| 313 | #define GPDMA_DESC_NEXT_H4 (0xf << 24) |
| 314 | #define GPDMA_DESC_PTR_H4 (0xf << 28) |
| 315 | u32 next; |
| 316 | u32 ptr; |
| 317 | u32 gpd_data_len; |
| 318 | #define GPDMA_DESC_BUFLEN (0xffff) /* bit0 ~ bit15 */ |
| 319 | #define GPDMA_DESC_EXTLEN (0xff << 16) /* bit16 ~ bit23 */ |
| 320 | u32 arg; |
| 321 | u32 blknum; |
| 322 | u32 cmd; |
| 323 | }; |
| 324 | |
| 325 | struct mt_bdma_desc { |
| 326 | u32 bd_info; |
| 327 | #define BDMA_DESC_EOL (0x1 << 0) |
| 328 | #define BDMA_DESC_CHECKSUM (0xff << 8) /* bit8 ~ bit15 */ |
| 329 | #define BDMA_DESC_BLKPAD (0x1 << 17) |
| 330 | #define BDMA_DESC_DWPAD (0x1 << 18) |
| 331 | #define BDMA_DESC_NEXT_H4 (0xf << 24) |
| 332 | #define BDMA_DESC_PTR_H4 (0xf << 28) |
| 333 | u32 next; |
| 334 | u32 ptr; |
| 335 | u32 bd_data_len; |
| 336 | #define BDMA_DESC_BUFLEN (0xffff) /* bit0 ~ bit15 */ |
| 337 | }; |
| 338 | |
| 339 | struct msdc_dma { |
| 340 | struct scatterlist *sg; /* I/O scatter list */ |
| 341 | struct mt_gpdma_desc *gpd; /* pointer to gpd array */ |
| 342 | struct mt_bdma_desc *bd; /* pointer to bd array */ |
| 343 | dma_addr_t gpd_addr; /* the physical address of gpd array */ |
| 344 | dma_addr_t bd_addr; /* the physical address of bd array */ |
| 345 | }; |
| 346 | |
| 347 | struct msdc_save_para { |
| 348 | u32 msdc_cfg; |
| 349 | u32 iocon; |
| 350 | u32 sdc_cfg; |
| 351 | u32 pad_tune; |
| 352 | u32 patch_bit0; |
| 353 | u32 patch_bit1; |
| 354 | u32 patch_bit2; |
| 355 | u32 pad_ds_tune; |
| 356 | u32 pad_cmd_tune; |
| 357 | u32 emmc50_cfg0; |
| 358 | u32 emmc50_cfg3; |
| 359 | u32 sdc_fifo_cfg; |
| 360 | u32 emmc_top_control; |
| 361 | u32 emmc_top_cmd; |
| 362 | u32 emmc50_pad_ds_tune; |
| 363 | }; |
| 364 | |
| 365 | struct mtk_mmc_compatible { |
| 366 | u8 clk_div_bits; |
| 367 | bool hs400_tune; /* only used for MT8173 */ |
| 368 | u32 pad_tune_reg; |
| 369 | bool async_fifo; |
| 370 | bool data_tune; |
| 371 | bool busy_check; |
| 372 | bool stop_clk_fix; |
| 373 | bool enhance_rx; |
| 374 | bool support_64g; |
| 375 | bool tune_resp_data_together; |
| 376 | }; |
| 377 | |
| 378 | struct msdc_tune_para { |
| 379 | u32 iocon; |
| 380 | u32 pad_tune; |
| 381 | u32 pad_cmd_tune; |
| 382 | u32 emmc_top_control; |
| 383 | u32 emmc_top_cmd; |
| 384 | }; |
| 385 | |
| 386 | struct msdc_delay_phase { |
| 387 | u8 maxlen; |
| 388 | u8 start; |
| 389 | u8 final_phase; |
| 390 | }; |
| 391 | |
| 392 | struct msdc_host { |
| 393 | struct device *dev; |
| 394 | const struct mtk_mmc_compatible *dev_comp; |
| 395 | struct mmc_host *mmc; /* mmc structure */ |
| 396 | int cmd_rsp; |
| 397 | |
| 398 | spinlock_t lock; |
| 399 | struct mmc_request *mrq; |
| 400 | struct mmc_command *cmd; |
| 401 | struct mmc_data *data; |
| 402 | int error; |
| 403 | |
| 404 | void __iomem *base; /* host base address */ |
| 405 | void __iomem *top_base; /* host top register base address */ |
| 406 | |
| 407 | struct msdc_dma dma; /* dma channel */ |
| 408 | u64 dma_mask; |
| 409 | |
| 410 | u32 timeout_ns; /* data timeout ns */ |
| 411 | u32 timeout_clks; /* data timeout clks */ |
| 412 | |
| 413 | struct pinctrl *pinctrl; |
| 414 | struct pinctrl_state *pins_default; |
| 415 | struct pinctrl_state *pins_uhs; |
| 416 | struct delayed_work req_timeout; |
| 417 | int irq; /* host interrupt */ |
| 418 | |
| 419 | struct clk *src_clk; /* msdc source clock */ |
| 420 | struct clk *h_clk; /* msdc h_clk */ |
| 421 | struct clk *src_clk_cg; /* msdc source clock control gate */ |
| 422 | u32 mclk; /* mmc subsystem clock frequency */ |
| 423 | u32 src_clk_freq; /* source clock frequency */ |
| 424 | u32 sclk; /* SD/MS bus clock frequency */ |
| 425 | unsigned char timing; |
| 426 | bool vqmmc_enabled; |
| 427 | u32 latch_ck; |
| 428 | u32 hs400_ds_delay; |
| 429 | u32 hs200_cmd_int_delay; /* cmd internal delay for HS200/SDR104 */ |
| 430 | u32 hs400_cmd_int_delay; /* cmd internal delay for HS400 */ |
| 431 | bool hs400_cmd_resp_sel_rising; |
| 432 | /* cmd response sample selection for HS400 */ |
| 433 | bool hs400_mode; /* current eMMC will run at hs400 mode */ |
| 434 | struct msdc_save_para save_para; /* used when gate HCLK */ |
| 435 | struct msdc_tune_para def_tune_para; /* default tune setting */ |
| 436 | struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */ |
| 437 | }; |
| 438 | |
| 439 | static const struct mtk_mmc_compatible mt8135_compat = { |
| 440 | .clk_div_bits = 8, |
| 441 | .hs400_tune = false, |
| 442 | .pad_tune_reg = MSDC_PAD_TUNE, |
| 443 | .async_fifo = false, |
| 444 | .data_tune = false, |
| 445 | .busy_check = false, |
| 446 | .stop_clk_fix = false, |
| 447 | .enhance_rx = false, |
| 448 | .support_64g = false, |
| 449 | .tune_resp_data_together = false, |
| 450 | }; |
| 451 | |
| 452 | static const struct mtk_mmc_compatible mt8173_compat = { |
| 453 | .clk_div_bits = 8, |
| 454 | .hs400_tune = true, |
| 455 | .pad_tune_reg = MSDC_PAD_TUNE, |
| 456 | .async_fifo = false, |
| 457 | .data_tune = false, |
| 458 | .busy_check = false, |
| 459 | .stop_clk_fix = false, |
| 460 | .enhance_rx = false, |
| 461 | .support_64g = false, |
| 462 | .tune_resp_data_together = false, |
| 463 | }; |
| 464 | |
| 465 | static const struct mtk_mmc_compatible mt2701_compat = { |
| 466 | .clk_div_bits = 12, |
| 467 | .hs400_tune = false, |
| 468 | .pad_tune_reg = MSDC_PAD_TUNE0, |
| 469 | .async_fifo = true, |
| 470 | .data_tune = true, |
| 471 | .busy_check = false, |
| 472 | .stop_clk_fix = false, |
| 473 | .enhance_rx = false, |
| 474 | .support_64g = false, |
| 475 | .tune_resp_data_together = false, |
| 476 | }; |
| 477 | |
| 478 | static const struct mtk_mmc_compatible mt2712_compat = { |
| 479 | .clk_div_bits = 12, |
| 480 | .hs400_tune = false, |
| 481 | .pad_tune_reg = MSDC_PAD_TUNE0, |
| 482 | .async_fifo = true, |
| 483 | .data_tune = true, |
| 484 | .busy_check = true, |
| 485 | .stop_clk_fix = true, |
| 486 | .enhance_rx = true, |
| 487 | .support_64g = true, |
| 488 | .tune_resp_data_together = true, |
| 489 | }; |
| 490 | |
| 491 | static const struct mtk_mmc_compatible mt2731_compat = { |
| 492 | .clk_div_bits = 12, |
| 493 | .hs400_tune = false, |
| 494 | .pad_tune_reg = MSDC_PAD_TUNE0, |
| 495 | .async_fifo = true, |
| 496 | .data_tune = true, |
| 497 | .busy_check = true, |
| 498 | .stop_clk_fix = true, |
| 499 | .enhance_rx = true, |
| 500 | .support_64g = true, |
| 501 | .tune_resp_data_together = true, |
| 502 | }; |
| 503 | |
| 504 | static const struct mtk_mmc_compatible mt7622_compat = { |
| 505 | .clk_div_bits = 12, |
| 506 | .hs400_tune = false, |
| 507 | .pad_tune_reg = MSDC_PAD_TUNE0, |
| 508 | .async_fifo = true, |
| 509 | .data_tune = true, |
| 510 | .busy_check = true, |
| 511 | .stop_clk_fix = true, |
| 512 | .enhance_rx = true, |
| 513 | .support_64g = false, |
| 514 | .tune_resp_data_together = false, |
| 515 | }; |
| 516 | |
| 517 | static const struct mtk_mmc_compatible mt8183_compat = { |
| 518 | .clk_div_bits = 12, |
| 519 | .hs400_tune = false, |
| 520 | .pad_tune_reg = MSDC_PAD_TUNE0, |
| 521 | .async_fifo = true, |
| 522 | .data_tune = true, |
| 523 | .busy_check = true, |
| 524 | .stop_clk_fix = true, |
| 525 | .enhance_rx = true, |
| 526 | .support_64g = true, |
| 527 | .tune_resp_data_together = true, |
| 528 | }; |
| 529 | |
| 530 | static const struct of_device_id msdc_of_ids[] = { |
| 531 | { .compatible = "mediatek,mt8135-mmc", .data = &mt8135_compat}, |
| 532 | { .compatible = "mediatek,mt8173-mmc", .data = &mt8173_compat}, |
| 533 | { .compatible = "mediatek,mt2701-mmc", .data = &mt2701_compat}, |
| 534 | { .compatible = "mediatek,mt2712-mmc", .data = &mt2712_compat}, |
| 535 | { .compatible = "mediatek,mt2731-mmc", .data = &mt2731_compat}, |
| 536 | { .compatible = "mediatek,mt7622-mmc", .data = &mt7622_compat}, |
| 537 | { .compatible = "mediatek,mt8183-mmc", .data = &mt8183_compat}, |
| 538 | { .compatible = "mediatek,mt8183-sdio", .data = &mt8183_compat}, |
| 539 | {} |
| 540 | }; |
| 541 | MODULE_DEVICE_TABLE(of, msdc_of_ids); |
| 542 | |
| 543 | static void sdr_set_bits(void __iomem *reg, u32 bs) |
| 544 | { |
| 545 | u32 val = readl(reg); |
| 546 | |
| 547 | val |= bs; |
| 548 | writel(val, reg); |
| 549 | } |
| 550 | |
| 551 | static void sdr_clr_bits(void __iomem *reg, u32 bs) |
| 552 | { |
| 553 | u32 val = readl(reg); |
| 554 | |
| 555 | val &= ~bs; |
| 556 | writel(val, reg); |
| 557 | } |
| 558 | |
| 559 | static void sdr_set_field(void __iomem *reg, u32 field, u32 val) |
| 560 | { |
| 561 | unsigned int tv = readl(reg); |
| 562 | |
| 563 | tv &= ~field; |
| 564 | tv |= ((val) << (ffs((unsigned int)field) - 1)); |
| 565 | writel(tv, reg); |
| 566 | } |
| 567 | |
| 568 | static void sdr_get_field(void __iomem *reg, u32 field, u32 *val) |
| 569 | { |
| 570 | unsigned int tv = readl(reg); |
| 571 | |
| 572 | *val = ((tv & field) >> (ffs((unsigned int)field) - 1)); |
| 573 | } |
| 574 | |
| 575 | static void msdc_reset_hw(struct msdc_host *host) |
| 576 | { |
| 577 | u32 val; |
| 578 | |
| 579 | sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST); |
| 580 | while (readl(host->base + MSDC_CFG) & MSDC_CFG_RST) |
| 581 | cpu_relax(); |
| 582 | |
| 583 | sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR); |
| 584 | while (readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_CLR) |
| 585 | cpu_relax(); |
| 586 | |
| 587 | val = readl(host->base + MSDC_INT); |
| 588 | writel(val, host->base + MSDC_INT); |
| 589 | } |
| 590 | |
| 591 | static void msdc_cmd_next(struct msdc_host *host, |
| 592 | struct mmc_request *mrq, struct mmc_command *cmd); |
| 593 | |
| 594 | static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR | |
| 595 | MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY | |
| 596 | MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO; |
| 597 | static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | |
| 598 | MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR | |
| 599 | MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT; |
| 600 | |
| 601 | static u8 msdc_dma_calcs(u8 *buf, u32 len) |
| 602 | { |
| 603 | u32 i, sum = 0; |
| 604 | |
| 605 | for (i = 0; i < len; i++) |
| 606 | sum += buf[i]; |
| 607 | return 0xff - (u8) sum; |
| 608 | } |
| 609 | |
| 610 | static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma, |
| 611 | struct mmc_data *data) |
| 612 | { |
| 613 | unsigned int j, dma_len; |
| 614 | dma_addr_t dma_address; |
| 615 | u32 dma_ctrl; |
| 616 | struct scatterlist *sg; |
| 617 | struct mt_gpdma_desc *gpd; |
| 618 | struct mt_bdma_desc *bd; |
| 619 | |
| 620 | sg = data->sg; |
| 621 | |
| 622 | gpd = dma->gpd; |
| 623 | bd = dma->bd; |
| 624 | |
| 625 | /* modify gpd */ |
| 626 | gpd->gpd_info |= GPDMA_DESC_HWO; |
| 627 | gpd->gpd_info |= GPDMA_DESC_BDP; |
| 628 | /* need to clear first. use these bits to calc checksum */ |
| 629 | gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM; |
| 630 | gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8; |
| 631 | |
| 632 | /* modify bd */ |
| 633 | for_each_sg(data->sg, sg, data->sg_count, j) { |
| 634 | dma_address = sg_dma_address(sg); |
| 635 | dma_len = sg_dma_len(sg); |
| 636 | |
| 637 | /* init bd */ |
| 638 | bd[j].bd_info &= ~BDMA_DESC_BLKPAD; |
| 639 | bd[j].bd_info &= ~BDMA_DESC_DWPAD; |
| 640 | bd[j].ptr = lower_32_bits(dma_address); |
| 641 | if (host->dev_comp->support_64g) { |
| 642 | bd[j].bd_info &= ~BDMA_DESC_PTR_H4; |
| 643 | bd[j].bd_info |= (upper_32_bits(dma_address) & 0xf) |
| 644 | << 28; |
| 645 | } |
| 646 | bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN; |
| 647 | bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN); |
| 648 | |
| 649 | if (j == data->sg_count - 1) /* the last bd */ |
| 650 | bd[j].bd_info |= BDMA_DESC_EOL; |
| 651 | else |
| 652 | bd[j].bd_info &= ~BDMA_DESC_EOL; |
| 653 | |
| 654 | /* checksume need to clear first */ |
| 655 | bd[j].bd_info &= ~BDMA_DESC_CHECKSUM; |
| 656 | bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8; |
| 657 | } |
| 658 | |
| 659 | sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1); |
| 660 | dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL); |
| 661 | dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE); |
| 662 | dma_ctrl |= (MSDC_BURST_64B << 12 | 1 << 8); |
| 663 | writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL); |
| 664 | if (host->dev_comp->support_64g) |
| 665 | sdr_set_field(host->base + DMA_SA_H4BIT, DMA_ADDR_HIGH_4BIT, |
| 666 | upper_32_bits(dma->gpd_addr) & 0xf); |
| 667 | writel(lower_32_bits(dma->gpd_addr), host->base + MSDC_DMA_SA); |
| 668 | } |
| 669 | |
| 670 | static void msdc_prepare_data(struct msdc_host *host, struct mmc_request *mrq) |
| 671 | { |
| 672 | struct mmc_data *data = mrq->data; |
| 673 | |
| 674 | if (!(data->host_cookie & MSDC_PREPARE_FLAG)) { |
| 675 | data->host_cookie |= MSDC_PREPARE_FLAG; |
| 676 | data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len, |
| 677 | mmc_get_dma_dir(data)); |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | static void msdc_unprepare_data(struct msdc_host *host, struct mmc_request *mrq) |
| 682 | { |
| 683 | struct mmc_data *data = mrq->data; |
| 684 | |
| 685 | if (data->host_cookie & MSDC_ASYNC_FLAG) |
| 686 | return; |
| 687 | |
| 688 | if (data->host_cookie & MSDC_PREPARE_FLAG) { |
| 689 | dma_unmap_sg(host->dev, data->sg, data->sg_len, |
| 690 | mmc_get_dma_dir(data)); |
| 691 | data->host_cookie &= ~MSDC_PREPARE_FLAG; |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | /* clock control primitives */ |
| 696 | static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks) |
| 697 | { |
| 698 | u32 timeout, clk_ns; |
| 699 | u32 mode = 0; |
| 700 | |
| 701 | host->timeout_ns = ns; |
| 702 | host->timeout_clks = clks; |
| 703 | if (host->sclk == 0) { |
| 704 | timeout = 0; |
| 705 | } else { |
| 706 | clk_ns = 1000000000UL / host->sclk; |
| 707 | timeout = (ns + clk_ns - 1) / clk_ns + clks; |
| 708 | /* in 1048576 sclk cycle unit */ |
| 709 | timeout = (timeout + (0x1 << 20) - 1) >> 20; |
| 710 | if (host->dev_comp->clk_div_bits == 8) |
| 711 | sdr_get_field(host->base + MSDC_CFG, |
| 712 | MSDC_CFG_CKMOD, &mode); |
| 713 | else |
| 714 | sdr_get_field(host->base + MSDC_CFG, |
| 715 | MSDC_CFG_CKMOD_EXTRA, &mode); |
| 716 | /*DDR mode will double the clk cycles for data timeout */ |
| 717 | timeout = mode >= 2 ? timeout * 2 : timeout; |
| 718 | timeout = timeout > 1 ? timeout - 1 : 0; |
| 719 | timeout = timeout > 255 ? 255 : timeout; |
| 720 | } |
| 721 | sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, timeout); |
| 722 | } |
| 723 | |
| 724 | static void msdc_gate_clock(struct msdc_host *host) |
| 725 | { |
| 726 | clk_disable_unprepare(host->src_clk_cg); |
| 727 | clk_disable_unprepare(host->src_clk); |
| 728 | clk_disable_unprepare(host->h_clk); |
| 729 | } |
| 730 | |
| 731 | static void msdc_ungate_clock(struct msdc_host *host) |
| 732 | { |
| 733 | clk_prepare_enable(host->h_clk); |
| 734 | clk_prepare_enable(host->src_clk); |
| 735 | clk_prepare_enable(host->src_clk_cg); |
| 736 | while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB)) |
| 737 | cpu_relax(); |
| 738 | } |
| 739 | |
| 740 | static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz) |
| 741 | { |
| 742 | u32 mode; |
| 743 | u32 flags; |
| 744 | u32 div; |
| 745 | u32 sclk; |
| 746 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 747 | |
| 748 | if (!hz) { |
| 749 | dev_dbg(host->dev, "set mclk to 0\n"); |
| 750 | host->mclk = 0; |
| 751 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); |
| 752 | return; |
| 753 | } |
| 754 | |
| 755 | flags = readl(host->base + MSDC_INTEN); |
| 756 | sdr_clr_bits(host->base + MSDC_INTEN, flags); |
| 757 | if (host->dev_comp->clk_div_bits == 8) |
| 758 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE); |
| 759 | else |
| 760 | sdr_clr_bits(host->base + MSDC_CFG, |
| 761 | MSDC_CFG_HS400_CK_MODE_EXTRA); |
| 762 | if (timing == MMC_TIMING_UHS_DDR50 || |
| 763 | timing == MMC_TIMING_MMC_DDR52 || |
| 764 | timing == MMC_TIMING_MMC_HS400) { |
| 765 | if (timing == MMC_TIMING_MMC_HS400) |
| 766 | mode = 0x3; |
| 767 | else |
| 768 | mode = 0x2; /* ddr mode and use divisor */ |
| 769 | |
| 770 | if (hz >= (host->src_clk_freq >> 2)) { |
| 771 | div = 0; /* mean div = 1/4 */ |
| 772 | sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */ |
| 773 | } else { |
| 774 | div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); |
| 775 | sclk = (host->src_clk_freq >> 2) / div; |
| 776 | div = (div >> 1); |
| 777 | } |
| 778 | |
| 779 | if (timing == MMC_TIMING_MMC_HS400 && |
| 780 | hz >= (host->src_clk_freq >> 1)) { |
| 781 | if (host->dev_comp->clk_div_bits == 8) |
| 782 | sdr_set_bits(host->base + MSDC_CFG, |
| 783 | MSDC_CFG_HS400_CK_MODE); |
| 784 | else |
| 785 | sdr_set_bits(host->base + MSDC_CFG, |
| 786 | MSDC_CFG_HS400_CK_MODE_EXTRA); |
| 787 | sclk = host->src_clk_freq >> 1; |
| 788 | div = 0; /* div is ignore when bit18 is set */ |
| 789 | } |
| 790 | } else if (hz >= host->src_clk_freq) { |
| 791 | mode = 0x1; /* no divisor */ |
| 792 | div = 0; |
| 793 | sclk = host->src_clk_freq; |
| 794 | } else { |
| 795 | mode = 0x0; /* use divisor */ |
| 796 | if (hz >= (host->src_clk_freq >> 1)) { |
| 797 | div = 0; /* mean div = 1/2 */ |
| 798 | sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */ |
| 799 | } else { |
| 800 | div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); |
| 801 | sclk = (host->src_clk_freq >> 2) / div; |
| 802 | } |
| 803 | } |
| 804 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); |
| 805 | /* |
| 806 | * As src_clk/HCLK use the same bit to gate/ungate, |
| 807 | * So if want to only gate src_clk, need gate its parent(mux). |
| 808 | */ |
| 809 | if (host->src_clk_cg) |
| 810 | clk_disable_unprepare(host->src_clk_cg); |
| 811 | else |
| 812 | clk_disable_unprepare(clk_get_parent(host->src_clk)); |
| 813 | if (host->dev_comp->clk_div_bits == 8) |
| 814 | sdr_set_field(host->base + MSDC_CFG, |
| 815 | MSDC_CFG_CKMOD | MSDC_CFG_CKDIV, |
| 816 | (mode << 8) | div); |
| 817 | else |
| 818 | sdr_set_field(host->base + MSDC_CFG, |
| 819 | MSDC_CFG_CKMOD_EXTRA | MSDC_CFG_CKDIV_EXTRA, |
| 820 | (mode << 12) | div); |
| 821 | if (host->src_clk_cg) |
| 822 | clk_prepare_enable(host->src_clk_cg); |
| 823 | else |
| 824 | clk_prepare_enable(clk_get_parent(host->src_clk)); |
| 825 | |
| 826 | while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB)) |
| 827 | cpu_relax(); |
| 828 | sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); |
| 829 | host->sclk = sclk; |
| 830 | host->mclk = hz; |
| 831 | host->timing = timing; |
| 832 | /* need because clk changed. */ |
| 833 | msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); |
| 834 | sdr_set_bits(host->base + MSDC_INTEN, flags); |
| 835 | |
| 836 | /* |
| 837 | * mmc_select_hs400() will drop to 50Mhz and High speed mode, |
| 838 | * tune result of hs200/200Mhz is not suitable for 50Mhz |
| 839 | */ |
| 840 | if (host->sclk <= 52000000) { |
| 841 | writel(host->def_tune_para.iocon, host->base + MSDC_IOCON); |
| 842 | writel(host->def_tune_para.pad_tune, host->base + tune_reg); |
| 843 | if (host->top_base != NULL) { |
| 844 | writel(host->def_tune_para.emmc_top_control, |
| 845 | host->top_base + EMMC_TOP_CONTROL); |
| 846 | writel(host->def_tune_para.emmc_top_cmd, |
| 847 | host->top_base + EMMC_TOP_CMD); |
| 848 | } |
| 849 | } else { |
| 850 | writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON); |
| 851 | writel(host->saved_tune_para.pad_tune, host->base + tune_reg); |
| 852 | writel(host->saved_tune_para.pad_cmd_tune, |
| 853 | host->base + PAD_CMD_TUNE); |
| 854 | if (host->top_base != NULL) { |
| 855 | writel(host->saved_tune_para.emmc_top_control, |
| 856 | host->top_base + EMMC_TOP_CONTROL); |
| 857 | writel(host->saved_tune_para.emmc_top_cmd, |
| 858 | host->top_base + EMMC_TOP_CMD); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | if (timing == MMC_TIMING_MMC_HS400 && |
| 863 | host->dev_comp->hs400_tune) |
| 864 | sdr_set_field(host->base + PAD_CMD_TUNE, |
| 865 | MSDC_PAD_TUNE_CMDRRDLY, |
| 866 | host->hs400_cmd_int_delay); |
| 867 | dev_dbg(host->dev, "sclk: %d, timing: %d\n", host->sclk, timing); |
| 868 | } |
| 869 | |
| 870 | static inline u32 msdc_cmd_find_resp(struct msdc_host *host, |
| 871 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 872 | { |
| 873 | u32 resp; |
| 874 | |
| 875 | switch (mmc_resp_type(cmd)) { |
| 876 | /* Actually, R1, R5, R6, R7 are the same */ |
| 877 | case MMC_RSP_R1: |
| 878 | resp = 0x1; |
| 879 | break; |
| 880 | case MMC_RSP_R1B: |
| 881 | resp = 0x7; |
| 882 | break; |
| 883 | case MMC_RSP_R2: |
| 884 | resp = 0x2; |
| 885 | break; |
| 886 | case MMC_RSP_R3: |
| 887 | resp = 0x3; |
| 888 | break; |
| 889 | case MMC_RSP_NONE: |
| 890 | default: |
| 891 | resp = 0x0; |
| 892 | break; |
| 893 | } |
| 894 | |
| 895 | return resp; |
| 896 | } |
| 897 | |
| 898 | static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host, |
| 899 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 900 | { |
| 901 | /* rawcmd : |
| 902 | * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 | |
| 903 | * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode |
| 904 | */ |
| 905 | u32 opcode = cmd->opcode; |
| 906 | u32 resp = msdc_cmd_find_resp(host, mrq, cmd); |
| 907 | u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7); |
| 908 | |
| 909 | host->cmd_rsp = resp; |
| 910 | |
| 911 | if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) || |
| 912 | opcode == MMC_STOP_TRANSMISSION) |
| 913 | rawcmd |= (0x1 << 14); |
| 914 | else if (opcode == SD_SWITCH_VOLTAGE) |
| 915 | rawcmd |= (0x1 << 30); |
| 916 | else if (opcode == SD_APP_SEND_SCR || |
| 917 | opcode == SD_APP_SEND_NUM_WR_BLKS || |
| 918 | (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || |
| 919 | (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || |
| 920 | (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC)) |
| 921 | rawcmd |= (0x1 << 11); |
| 922 | |
| 923 | if (cmd->data) { |
| 924 | struct mmc_data *data = cmd->data; |
| 925 | |
| 926 | if (mmc_op_multi(opcode)) { |
| 927 | if (mmc_card_mmc(host->mmc->card) && mrq->sbc && |
| 928 | !(mrq->sbc->arg & 0xFFFF0000)) |
| 929 | rawcmd |= 0x2 << 28; /* AutoCMD23 */ |
| 930 | } |
| 931 | |
| 932 | rawcmd |= ((data->blksz & 0xFFF) << 16); |
| 933 | if (data->flags & MMC_DATA_WRITE) |
| 934 | rawcmd |= (0x1 << 13); |
| 935 | if (data->blocks > 1) |
| 936 | rawcmd |= (0x2 << 11); |
| 937 | else |
| 938 | rawcmd |= (0x1 << 11); |
| 939 | /* Always use dma mode */ |
| 940 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO); |
| 941 | |
| 942 | if (host->timeout_ns != data->timeout_ns || |
| 943 | host->timeout_clks != data->timeout_clks) |
| 944 | msdc_set_timeout(host, data->timeout_ns, |
| 945 | data->timeout_clks); |
| 946 | |
| 947 | writel(data->blocks, host->base + SDC_BLK_NUM); |
| 948 | } |
| 949 | return rawcmd; |
| 950 | } |
| 951 | |
| 952 | static void msdc_start_data(struct msdc_host *host, struct mmc_request *mrq, |
| 953 | struct mmc_command *cmd, struct mmc_data *data) |
| 954 | { |
| 955 | bool read; |
| 956 | unsigned long flags; |
| 957 | |
| 958 | WARN_ON(host->data); |
| 959 | host->data = data; |
| 960 | read = data->flags & MMC_DATA_READ; |
| 961 | |
| 962 | mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); |
| 963 | msdc_dma_setup(host, &host->dma, data); |
| 964 | spin_lock_irqsave(&host->lock, flags); |
| 965 | sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask); |
| 966 | spin_unlock_irqrestore(&host->lock, flags); |
| 967 | sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1); |
| 968 | dev_dbg(host->dev, "DMA start\n"); |
| 969 | dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n", |
| 970 | __func__, cmd->opcode, data->blocks, read); |
| 971 | } |
| 972 | |
| 973 | static int msdc_auto_cmd_done(struct msdc_host *host, int events, |
| 974 | struct mmc_command *cmd) |
| 975 | { |
| 976 | u32 *rsp = cmd->resp; |
| 977 | |
| 978 | rsp[0] = readl(host->base + SDC_ACMD_RESP); |
| 979 | |
| 980 | if (events & MSDC_INT_ACMDRDY) { |
| 981 | cmd->error = 0; |
| 982 | } else { |
| 983 | msdc_reset_hw(host); |
| 984 | if (events & MSDC_INT_ACMDCRCERR) { |
| 985 | cmd->error = -EILSEQ; |
| 986 | host->error |= REQ_STOP_EIO; |
| 987 | } else if (events & MSDC_INT_ACMDTMO) { |
| 988 | cmd->error = -ETIMEDOUT; |
| 989 | host->error |= REQ_STOP_TMO; |
| 990 | } |
| 991 | dev_err(host->dev, |
| 992 | "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n", |
| 993 | __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error); |
| 994 | } |
| 995 | return cmd->error; |
| 996 | } |
| 997 | |
| 998 | static void msdc_track_cmd_data(struct msdc_host *host, |
| 999 | struct mmc_command *cmd, struct mmc_data *data) |
| 1000 | { |
| 1001 | if (host->error) |
| 1002 | dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n", |
| 1003 | __func__, cmd->opcode, cmd->arg, host->error); |
| 1004 | } |
| 1005 | |
| 1006 | static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq) |
| 1007 | { |
| 1008 | unsigned long flags; |
| 1009 | bool ret; |
| 1010 | |
| 1011 | ret = cancel_delayed_work(&host->req_timeout); |
| 1012 | if (!ret) { |
| 1013 | /* delay work already running */ |
| 1014 | return; |
| 1015 | } |
| 1016 | spin_lock_irqsave(&host->lock, flags); |
| 1017 | host->mrq = NULL; |
| 1018 | spin_unlock_irqrestore(&host->lock, flags); |
| 1019 | |
| 1020 | msdc_track_cmd_data(host, mrq->cmd, mrq->data); |
| 1021 | if (mrq->data) |
| 1022 | msdc_unprepare_data(host, mrq); |
| 1023 | mmc_request_done(host->mmc, mrq); |
| 1024 | } |
| 1025 | |
| 1026 | /* returns true if command is fully handled; returns false otherwise */ |
| 1027 | static bool msdc_cmd_done(struct msdc_host *host, int events, |
| 1028 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 1029 | { |
| 1030 | bool done = false; |
| 1031 | bool sbc_error; |
| 1032 | unsigned long flags; |
| 1033 | u32 *rsp = cmd->resp; |
| 1034 | |
| 1035 | if (mrq->sbc && cmd == mrq->cmd && |
| 1036 | (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR |
| 1037 | | MSDC_INT_ACMDTMO))) |
| 1038 | msdc_auto_cmd_done(host, events, mrq->sbc); |
| 1039 | |
| 1040 | sbc_error = mrq->sbc && mrq->sbc->error; |
| 1041 | |
| 1042 | if (!sbc_error && !(events & (MSDC_INT_CMDRDY |
| 1043 | | MSDC_INT_RSPCRCERR |
| 1044 | | MSDC_INT_CMDTMO))) |
| 1045 | return done; |
| 1046 | |
| 1047 | spin_lock_irqsave(&host->lock, flags); |
| 1048 | done = !host->cmd; |
| 1049 | host->cmd = NULL; |
| 1050 | spin_unlock_irqrestore(&host->lock, flags); |
| 1051 | |
| 1052 | if (done) |
| 1053 | return true; |
| 1054 | |
| 1055 | spin_lock_irqsave(&host->lock, flags); |
| 1056 | sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask); |
| 1057 | spin_unlock_irqrestore(&host->lock, flags); |
| 1058 | |
| 1059 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 1060 | if (cmd->flags & MMC_RSP_136) { |
| 1061 | rsp[0] = readl(host->base + SDC_RESP3); |
| 1062 | rsp[1] = readl(host->base + SDC_RESP2); |
| 1063 | rsp[2] = readl(host->base + SDC_RESP1); |
| 1064 | rsp[3] = readl(host->base + SDC_RESP0); |
| 1065 | } else { |
| 1066 | rsp[0] = readl(host->base + SDC_RESP0); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (!sbc_error && !(events & MSDC_INT_CMDRDY)) { |
| 1071 | if (cmd->opcode != MMC_SEND_TUNING_BLOCK && |
| 1072 | cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) |
| 1073 | /* |
| 1074 | * should not clear fifo/interrupt as the tune data |
| 1075 | * may have alreay come. |
| 1076 | */ |
| 1077 | msdc_reset_hw(host); |
| 1078 | if (events & MSDC_INT_RSPCRCERR) { |
| 1079 | cmd->error = -EILSEQ; |
| 1080 | host->error |= REQ_CMD_EIO; |
| 1081 | } else if (events & MSDC_INT_CMDTMO) { |
| 1082 | cmd->error = -ETIMEDOUT; |
| 1083 | host->error |= REQ_CMD_TMO; |
| 1084 | } |
| 1085 | } |
| 1086 | if (cmd->error && |
| 1087 | cmd->opcode != MMC_SEND_TUNING_BLOCK && |
| 1088 | cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) |
| 1089 | dev_info(host->dev, |
| 1090 | "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n", |
| 1091 | __func__, cmd->opcode, cmd->arg, rsp[0], |
| 1092 | cmd->error); |
| 1093 | |
| 1094 | msdc_cmd_next(host, mrq, cmd); |
| 1095 | return true; |
| 1096 | } |
| 1097 | |
| 1098 | /* It is the core layer's responsibility to ensure card status |
| 1099 | * is correct before issue a request. but host design do below |
| 1100 | * checks recommended. |
| 1101 | */ |
| 1102 | static inline bool msdc_cmd_is_ready(struct msdc_host *host, |
| 1103 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 1104 | { |
| 1105 | /* The max busy time we can endure is 20ms */ |
| 1106 | unsigned long tmo = jiffies + msecs_to_jiffies(20); |
| 1107 | |
| 1108 | while ((readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) && |
| 1109 | time_before(jiffies, tmo)) |
| 1110 | cpu_relax(); |
| 1111 | if (readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) { |
| 1112 | dev_err(host->dev, "CMD bus busy detected\n"); |
| 1113 | host->error |= REQ_CMD_BUSY; |
| 1114 | msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); |
| 1115 | return false; |
| 1116 | } |
| 1117 | |
| 1118 | if (mmc_resp_type(cmd) == MMC_RSP_R1B || cmd->data) { |
| 1119 | tmo = jiffies + msecs_to_jiffies(20); |
| 1120 | /* R1B or with data, should check SDCBUSY */ |
| 1121 | while ((readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) && |
| 1122 | time_before(jiffies, tmo)) |
| 1123 | cpu_relax(); |
| 1124 | if (readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) { |
| 1125 | dev_err(host->dev, "Controller busy detected\n"); |
| 1126 | host->error |= REQ_CMD_BUSY; |
| 1127 | msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); |
| 1128 | return false; |
| 1129 | } |
| 1130 | } |
| 1131 | return true; |
| 1132 | } |
| 1133 | |
| 1134 | static void msdc_start_command(struct msdc_host *host, |
| 1135 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 1136 | { |
| 1137 | u32 rawcmd; |
| 1138 | unsigned long flags; |
| 1139 | |
| 1140 | WARN_ON(host->cmd); |
| 1141 | host->cmd = cmd; |
| 1142 | |
| 1143 | if (!msdc_cmd_is_ready(host, mrq, cmd)) |
| 1144 | return; |
| 1145 | |
| 1146 | if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 || |
| 1147 | readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) { |
| 1148 | dev_err(host->dev, "TX/RX FIFO non-empty before start of IO. Reset\n"); |
| 1149 | msdc_reset_hw(host); |
| 1150 | } |
| 1151 | |
| 1152 | cmd->error = 0; |
| 1153 | rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd); |
| 1154 | mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); |
| 1155 | |
| 1156 | spin_lock_irqsave(&host->lock, flags); |
| 1157 | sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask); |
| 1158 | spin_unlock_irqrestore(&host->lock, flags); |
| 1159 | writel(cmd->arg, host->base + SDC_ARG); |
| 1160 | writel(rawcmd, host->base + SDC_CMD); |
| 1161 | } |
| 1162 | |
| 1163 | static void msdc_cmd_next(struct msdc_host *host, |
| 1164 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 1165 | { |
| 1166 | if ((cmd->error && |
| 1167 | !(cmd->error == -EILSEQ && |
| 1168 | (cmd->opcode == MMC_SEND_TUNING_BLOCK || |
| 1169 | cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) || |
| 1170 | (mrq->sbc && mrq->sbc->error)) |
| 1171 | msdc_request_done(host, mrq); |
| 1172 | else if (cmd == mrq->sbc) |
| 1173 | msdc_start_command(host, mrq, mrq->cmd); |
| 1174 | else if (!cmd->data) |
| 1175 | msdc_request_done(host, mrq); |
| 1176 | else |
| 1177 | msdc_start_data(host, mrq, cmd, cmd->data); |
| 1178 | } |
| 1179 | |
| 1180 | static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1181 | { |
| 1182 | struct msdc_host *host = mmc_priv(mmc); |
| 1183 | |
| 1184 | host->error = 0; |
| 1185 | WARN_ON(host->mrq); |
| 1186 | host->mrq = mrq; |
| 1187 | |
| 1188 | if (mrq->data) |
| 1189 | msdc_prepare_data(host, mrq); |
| 1190 | |
| 1191 | /* if SBC is required, we have HW option and SW option. |
| 1192 | * if HW option is enabled, and SBC does not have "special" flags, |
| 1193 | * use HW option, otherwise use SW option |
| 1194 | */ |
| 1195 | if (mrq->sbc && (!mmc_card_mmc(mmc->card) || |
| 1196 | (mrq->sbc->arg & 0xFFFF0000))) |
| 1197 | msdc_start_command(host, mrq, mrq->sbc); |
| 1198 | else |
| 1199 | msdc_start_command(host, mrq, mrq->cmd); |
| 1200 | } |
| 1201 | |
| 1202 | static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1203 | { |
| 1204 | struct msdc_host *host = mmc_priv(mmc); |
| 1205 | struct mmc_data *data = mrq->data; |
| 1206 | |
| 1207 | if (!data) |
| 1208 | return; |
| 1209 | |
| 1210 | msdc_prepare_data(host, mrq); |
| 1211 | data->host_cookie |= MSDC_ASYNC_FLAG; |
| 1212 | } |
| 1213 | |
| 1214 | static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 1215 | int err) |
| 1216 | { |
| 1217 | struct msdc_host *host = mmc_priv(mmc); |
| 1218 | struct mmc_data *data; |
| 1219 | |
| 1220 | data = mrq->data; |
| 1221 | if (!data) |
| 1222 | return; |
| 1223 | if (data->host_cookie) { |
| 1224 | data->host_cookie &= ~MSDC_ASYNC_FLAG; |
| 1225 | msdc_unprepare_data(host, mrq); |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | static void msdc_data_xfer_next(struct msdc_host *host, |
| 1230 | struct mmc_request *mrq, struct mmc_data *data) |
| 1231 | { |
| 1232 | if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error && |
| 1233 | !mrq->sbc) |
| 1234 | msdc_start_command(host, mrq, mrq->stop); |
| 1235 | else |
| 1236 | msdc_request_done(host, mrq); |
| 1237 | } |
| 1238 | |
| 1239 | static bool msdc_data_xfer_done(struct msdc_host *host, u32 events, |
| 1240 | struct mmc_request *mrq, struct mmc_data *data) |
| 1241 | { |
| 1242 | struct mmc_command *stop = data->stop; |
| 1243 | unsigned long flags; |
| 1244 | bool done; |
| 1245 | unsigned int check_data = events & |
| 1246 | (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO |
| 1247 | | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR |
| 1248 | | MSDC_INT_DMA_PROTECT); |
| 1249 | |
| 1250 | spin_lock_irqsave(&host->lock, flags); |
| 1251 | done = !host->data; |
| 1252 | if (check_data) |
| 1253 | host->data = NULL; |
| 1254 | spin_unlock_irqrestore(&host->lock, flags); |
| 1255 | |
| 1256 | if (done) |
| 1257 | return true; |
| 1258 | |
| 1259 | if (check_data || (stop && stop->error)) { |
| 1260 | dev_dbg(host->dev, "DMA status: 0x%8X\n", |
| 1261 | readl(host->base + MSDC_DMA_CFG)); |
| 1262 | sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, |
| 1263 | 1); |
| 1264 | while (readl(host->base + MSDC_DMA_CFG) & MSDC_DMA_CFG_STS) |
| 1265 | cpu_relax(); |
| 1266 | sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask); |
| 1267 | dev_dbg(host->dev, "DMA stop\n"); |
| 1268 | |
| 1269 | if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) { |
| 1270 | data->bytes_xfered = data->blocks * data->blksz; |
| 1271 | } else { |
| 1272 | dev_dbg(host->dev, "interrupt events: %x\n", events); |
| 1273 | msdc_reset_hw(host); |
| 1274 | host->error |= REQ_DAT_ERR; |
| 1275 | data->bytes_xfered = 0; |
| 1276 | |
| 1277 | if (events & MSDC_INT_DATTMO) |
| 1278 | data->error = -ETIMEDOUT; |
| 1279 | else if (events & MSDC_INT_DATCRCERR) |
| 1280 | data->error = -EILSEQ; |
| 1281 | if (mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK && |
| 1282 | mrq->cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) { |
| 1283 | dev_info(host->dev, "%s: cmd=%d; blocks=%d", |
| 1284 | __func__, mrq->cmd->opcode, |
| 1285 | data->blocks); |
| 1286 | dev_info(host->dev, "data_error=%d xfer_size=%d\n", |
| 1287 | (int)data->error, data->bytes_xfered); |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | msdc_data_xfer_next(host, mrq, data); |
| 1292 | done = true; |
| 1293 | } |
| 1294 | return done; |
| 1295 | } |
| 1296 | |
| 1297 | static void msdc_set_buswidth(struct msdc_host *host, u32 width) |
| 1298 | { |
| 1299 | u32 val = readl(host->base + SDC_CFG); |
| 1300 | |
| 1301 | val &= ~SDC_CFG_BUSWIDTH; |
| 1302 | |
| 1303 | switch (width) { |
| 1304 | default: |
| 1305 | case MMC_BUS_WIDTH_1: |
| 1306 | val |= (MSDC_BUS_1BITS << 16); |
| 1307 | break; |
| 1308 | case MMC_BUS_WIDTH_4: |
| 1309 | val |= (MSDC_BUS_4BITS << 16); |
| 1310 | break; |
| 1311 | case MMC_BUS_WIDTH_8: |
| 1312 | val |= (MSDC_BUS_8BITS << 16); |
| 1313 | break; |
| 1314 | } |
| 1315 | |
| 1316 | writel(val, host->base + SDC_CFG); |
| 1317 | dev_dbg(host->dev, "Bus Width = %d", width); |
| 1318 | } |
| 1319 | |
| 1320 | static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1321 | { |
| 1322 | struct msdc_host *host = mmc_priv(mmc); |
| 1323 | int ret = 0; |
| 1324 | |
| 1325 | if (!IS_ERR(mmc->supply.vqmmc)) { |
| 1326 | if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 && |
| 1327 | ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) { |
| 1328 | dev_err(host->dev, "Unsupported signal voltage!\n"); |
| 1329 | return -EINVAL; |
| 1330 | } |
| 1331 | |
| 1332 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
| 1333 | if (ret) { |
| 1334 | dev_dbg(host->dev, "Regulator set error %d (%d)\n", |
| 1335 | ret, ios->signal_voltage); |
| 1336 | } else { |
| 1337 | /* Apply different pinctrl settings for different signal voltage */ |
| 1338 | if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) |
| 1339 | pinctrl_select_state(host->pinctrl, host->pins_uhs); |
| 1340 | else |
| 1341 | pinctrl_select_state(host->pinctrl, host->pins_default); |
| 1342 | } |
| 1343 | } |
| 1344 | return ret; |
| 1345 | } |
| 1346 | |
| 1347 | static int msdc_card_busy(struct mmc_host *mmc) |
| 1348 | { |
| 1349 | struct msdc_host *host = mmc_priv(mmc); |
| 1350 | u32 status = readl(host->base + MSDC_PS); |
| 1351 | |
| 1352 | /* only check if data0 is low */ |
| 1353 | return !(status & BIT(16)); |
| 1354 | } |
| 1355 | |
| 1356 | static void msdc_request_timeout(struct work_struct *work) |
| 1357 | { |
| 1358 | struct msdc_host *host = container_of(work, struct msdc_host, |
| 1359 | req_timeout.work); |
| 1360 | |
| 1361 | /* simulate HW timeout status */ |
| 1362 | dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__); |
| 1363 | if (host->mrq) { |
| 1364 | dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__, |
| 1365 | host->mrq, host->mrq->cmd->opcode); |
| 1366 | if (host->cmd) { |
| 1367 | dev_err(host->dev, "%s: aborting cmd=%d\n", |
| 1368 | __func__, host->cmd->opcode); |
| 1369 | msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq, |
| 1370 | host->cmd); |
| 1371 | } else if (host->data) { |
| 1372 | dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n", |
| 1373 | __func__, host->mrq->cmd->opcode, |
| 1374 | host->data->blocks); |
| 1375 | msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq, |
| 1376 | host->data); |
| 1377 | } |
| 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | static void __msdc_enable_sdio_irq(struct msdc_host *host, int enb) |
| 1382 | { |
| 1383 | unsigned long flags; |
| 1384 | |
| 1385 | spin_lock_irqsave(&host->lock, flags); |
| 1386 | if (enb) |
| 1387 | sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); |
| 1388 | else |
| 1389 | sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); |
| 1390 | |
| 1391 | spin_unlock_irqrestore(&host->lock, flags); |
| 1392 | } |
| 1393 | |
| 1394 | static irqreturn_t msdc_irq(int irq, void *dev_id) |
| 1395 | { |
| 1396 | struct msdc_host *host = (struct msdc_host *) dev_id; |
| 1397 | |
| 1398 | while (true) { |
| 1399 | unsigned long flags; |
| 1400 | struct mmc_request *mrq; |
| 1401 | struct mmc_command *cmd; |
| 1402 | struct mmc_data *data; |
| 1403 | u32 events, event_mask; |
| 1404 | |
| 1405 | spin_lock_irqsave(&host->lock, flags); |
| 1406 | events = readl(host->base + MSDC_INT); |
| 1407 | event_mask = readl(host->base + MSDC_INTEN); |
| 1408 | /* clear interrupts */ |
| 1409 | writel(events & event_mask, host->base + MSDC_INT); |
| 1410 | |
| 1411 | mrq = host->mrq; |
| 1412 | cmd = host->cmd; |
| 1413 | data = host->data; |
| 1414 | spin_unlock_irqrestore(&host->lock, flags); |
| 1415 | |
| 1416 | if ((events & event_mask) & MSDC_INT_SDIOIRQ) { |
| 1417 | __msdc_enable_sdio_irq(host, 0); |
| 1418 | sdio_signal_irq(host->mmc); |
| 1419 | if (!mrq) |
| 1420 | break; |
| 1421 | } |
| 1422 | |
| 1423 | if (!(events & (event_mask & ~MSDC_INT_SDIOIRQ))) |
| 1424 | break; |
| 1425 | |
| 1426 | if (!mrq) { |
| 1427 | dev_err(host->dev, |
| 1428 | "%s: MRQ=NULL; events=%08X; event_mask=%08X\n", |
| 1429 | __func__, events, event_mask); |
| 1430 | WARN_ON(1); |
| 1431 | break; |
| 1432 | } |
| 1433 | |
| 1434 | dev_dbg(host->dev, "%s: events=%08X\n", __func__, events); |
| 1435 | |
| 1436 | if (cmd) |
| 1437 | msdc_cmd_done(host, events, mrq, cmd); |
| 1438 | else if (data) |
| 1439 | msdc_data_xfer_done(host, events, mrq, data); |
| 1440 | } |
| 1441 | |
| 1442 | return IRQ_HANDLED; |
| 1443 | } |
| 1444 | |
| 1445 | static void msdc_init_hw(struct msdc_host *host) |
| 1446 | { |
| 1447 | u32 val; |
| 1448 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 1449 | |
| 1450 | /* Configure to MMC/SD mode, clock free running */ |
| 1451 | sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN); |
| 1452 | |
| 1453 | /* Reset */ |
| 1454 | msdc_reset_hw(host); |
| 1455 | |
| 1456 | /* Disable card detection */ |
| 1457 | sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN); |
| 1458 | |
| 1459 | /* Disable and clear all interrupts */ |
| 1460 | writel(0, host->base + MSDC_INTEN); |
| 1461 | val = readl(host->base + MSDC_INT); |
| 1462 | writel(val, host->base + MSDC_INT); |
| 1463 | |
| 1464 | writel(0, host->base + tune_reg); |
| 1465 | if (host->top_base != NULL) { |
| 1466 | writel(0, host->top_base + EMMC_TOP_CONTROL); |
| 1467 | writel(0, host->top_base + EMMC_TOP_CMD); |
| 1468 | } |
| 1469 | writel(0, host->base + MSDC_IOCON); |
| 1470 | sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0); |
| 1471 | writel(0x403c0046, host->base + MSDC_PATCH_BIT); |
| 1472 | sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1); |
| 1473 | writel(0xffff4089, host->base + MSDC_PATCH_BIT1); |
| 1474 | sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL); |
| 1475 | |
| 1476 | if (host->dev_comp->stop_clk_fix) { |
| 1477 | sdr_set_field(host->base + MSDC_PATCH_BIT1, |
| 1478 | MSDC_PATCH_BIT1_STOP_DLY, 3); |
| 1479 | sdr_clr_bits(host->base + SDC_FIFO_CFG, |
| 1480 | SDC_FIFO_CFG_WRVALIDSEL); |
| 1481 | sdr_clr_bits(host->base + SDC_FIFO_CFG, |
| 1482 | SDC_FIFO_CFG_RDVALIDSEL); |
| 1483 | } |
| 1484 | |
| 1485 | if (host->dev_comp->busy_check) |
| 1486 | sdr_clr_bits(host->base + MSDC_PATCH_BIT1, (1 << 7)); |
| 1487 | |
| 1488 | if (host->dev_comp->async_fifo) { |
| 1489 | sdr_set_field(host->base + MSDC_PATCH_BIT2, |
| 1490 | MSDC_PB2_RESPWAIT, 3); |
| 1491 | if (host->dev_comp->enhance_rx) { |
| 1492 | if (host->top_base != NULL) |
| 1493 | sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, |
| 1494 | SDC_RX_ENH_EN); |
| 1495 | else |
| 1496 | sdr_set_bits(host->base + SDC_ADV_CFG0, |
| 1497 | SDC_RX_ENHANCE_EN); |
| 1498 | } else { |
| 1499 | sdr_set_field(host->base + MSDC_PATCH_BIT2, |
| 1500 | MSDC_PB2_RESPSTSENSEL, 2); |
| 1501 | sdr_set_field(host->base + MSDC_PATCH_BIT2, |
| 1502 | MSDC_PB2_CRCSTSENSEL, 2); |
| 1503 | } |
| 1504 | /* use async fifo, then no need tune internal delay */ |
| 1505 | sdr_clr_bits(host->base + MSDC_PATCH_BIT2, |
| 1506 | MSDC_PATCH_BIT2_CFGRESP); |
| 1507 | sdr_set_bits(host->base + MSDC_PATCH_BIT2, |
| 1508 | MSDC_PATCH_BIT2_CFGCRCSTS); |
| 1509 | } |
| 1510 | |
| 1511 | if (host->dev_comp->support_64g) |
| 1512 | sdr_set_bits(host->base + MSDC_PATCH_BIT2, |
| 1513 | MSDC_PB2_SUPPORT_64G); |
| 1514 | if (host->dev_comp->data_tune) { |
| 1515 | if (host->top_base != NULL) { |
| 1516 | sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, |
| 1517 | PAD_DAT_RD_RXDLY_SEL); |
| 1518 | sdr_clr_bits(host->top_base + EMMC_TOP_CONTROL, |
| 1519 | DATA_K_VALUE_SEL); |
| 1520 | sdr_set_bits(host->top_base + EMMC_TOP_CMD, |
| 1521 | PAD_CMD_RD_RXDLY_SEL); |
| 1522 | } else { |
| 1523 | sdr_set_bits(host->base + tune_reg, |
| 1524 | MSDC_PAD_TUNE_RD_SEL | |
| 1525 | MSDC_PAD_TUNE_CMD_SEL); |
| 1526 | } |
| 1527 | } else { |
| 1528 | /* choose clock tune */ |
| 1529 | if (host->top_base != NULL) |
| 1530 | sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, |
| 1531 | PAD_RXDLY_SEL); |
| 1532 | else |
| 1533 | sdr_set_bits(host->base + tune_reg, |
| 1534 | MSDC_PAD_TUNE_RXDLYSEL); |
| 1535 | } |
| 1536 | |
| 1537 | /* Configure to enable SDIO mode. |
| 1538 | * it's must otherwise sdio cmd5 failed |
| 1539 | */ |
| 1540 | sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO); |
| 1541 | |
| 1542 | if (host->mmc->caps & MMC_CAP_SDIO_IRQ) { |
| 1543 | host->mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; |
| 1544 | sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); |
| 1545 | } else { |
| 1546 | /* disable detect SDIO device interrupt function */ |
| 1547 | sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); |
| 1548 | } |
| 1549 | |
| 1550 | /* Configure to default data timeout */ |
| 1551 | sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3); |
| 1552 | |
| 1553 | host->def_tune_para.iocon = readl(host->base + MSDC_IOCON); |
| 1554 | host->def_tune_para.pad_tune = readl(host->base + tune_reg); |
| 1555 | host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); |
| 1556 | host->saved_tune_para.pad_tune = readl(host->base + tune_reg); |
| 1557 | if (host->top_base != NULL) { |
| 1558 | host->def_tune_para.emmc_top_control = |
| 1559 | readl(host->top_base + EMMC_TOP_CONTROL); |
| 1560 | host->def_tune_para.emmc_top_cmd = |
| 1561 | readl(host->top_base + EMMC_TOP_CMD); |
| 1562 | host->saved_tune_para.emmc_top_control = |
| 1563 | readl(host->top_base + EMMC_TOP_CONTROL); |
| 1564 | host->saved_tune_para.emmc_top_cmd = |
| 1565 | readl(host->top_base + EMMC_TOP_CMD); |
| 1566 | } |
| 1567 | dev_dbg(host->dev, "init hardware done!"); |
| 1568 | } |
| 1569 | |
| 1570 | static void msdc_deinit_hw(struct msdc_host *host) |
| 1571 | { |
| 1572 | u32 val; |
| 1573 | /* Disable and clear all interrupts */ |
| 1574 | writel(0, host->base + MSDC_INTEN); |
| 1575 | |
| 1576 | val = readl(host->base + MSDC_INT); |
| 1577 | writel(val, host->base + MSDC_INT); |
| 1578 | } |
| 1579 | |
| 1580 | /* init gpd and bd list in msdc_drv_probe */ |
| 1581 | static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma) |
| 1582 | { |
| 1583 | struct mt_gpdma_desc *gpd = dma->gpd; |
| 1584 | struct mt_bdma_desc *bd = dma->bd; |
| 1585 | dma_addr_t dma_addr; |
| 1586 | int i; |
| 1587 | |
| 1588 | memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2); |
| 1589 | |
| 1590 | dma_addr = dma->gpd_addr + sizeof(struct mt_gpdma_desc); |
| 1591 | gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */ |
| 1592 | /* gpd->next is must set for desc DMA |
| 1593 | * That's why must alloc 2 gpd structure. |
| 1594 | */ |
| 1595 | gpd->next = lower_32_bits(dma_addr); |
| 1596 | if (host->dev_comp->support_64g) |
| 1597 | gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 24; |
| 1598 | |
| 1599 | dma_addr = dma->bd_addr; |
| 1600 | gpd->ptr = lower_32_bits(dma->bd_addr); /* physical address */ |
| 1601 | if (host->dev_comp->support_64g) |
| 1602 | gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 28; |
| 1603 | |
| 1604 | memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM); |
| 1605 | for (i = 0; i < (MAX_BD_NUM - 1); i++) { |
| 1606 | dma_addr = dma->bd_addr + sizeof(*bd) * (i + 1); |
| 1607 | bd[i].next = lower_32_bits(dma_addr); |
| 1608 | if (host->dev_comp->support_64g) |
| 1609 | bd[i].bd_info |= (upper_32_bits(dma_addr) & 0xf) << 24; |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1614 | { |
| 1615 | struct msdc_host *host = mmc_priv(mmc); |
| 1616 | int ret; |
| 1617 | |
| 1618 | msdc_set_buswidth(host, ios->bus_width); |
| 1619 | |
| 1620 | /* Suspend/Resume will do power off/on */ |
| 1621 | switch (ios->power_mode) { |
| 1622 | case MMC_POWER_UP: |
| 1623 | if (!IS_ERR(mmc->supply.vmmc)) { |
| 1624 | msdc_init_hw(host); |
| 1625 | ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, |
| 1626 | ios->vdd); |
| 1627 | if (ret) { |
| 1628 | dev_err(host->dev, "Failed to set vmmc power!\n"); |
| 1629 | return; |
| 1630 | } |
| 1631 | } |
| 1632 | break; |
| 1633 | case MMC_POWER_ON: |
| 1634 | if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { |
| 1635 | ret = regulator_enable(mmc->supply.vqmmc); |
| 1636 | if (ret) |
| 1637 | dev_err(host->dev, "Failed to set vqmmc power!\n"); |
| 1638 | else |
| 1639 | host->vqmmc_enabled = true; |
| 1640 | } |
| 1641 | break; |
| 1642 | case MMC_POWER_OFF: |
| 1643 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1644 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 1645 | |
| 1646 | if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { |
| 1647 | regulator_disable(mmc->supply.vqmmc); |
| 1648 | host->vqmmc_enabled = false; |
| 1649 | } |
| 1650 | break; |
| 1651 | default: |
| 1652 | break; |
| 1653 | } |
| 1654 | |
| 1655 | if (host->mclk != ios->clock || host->timing != ios->timing) |
| 1656 | msdc_set_mclk(host, ios->timing, ios->clock); |
| 1657 | } |
| 1658 | |
| 1659 | static u32 test_delay_bit(u32 delay, u32 bit) |
| 1660 | { |
| 1661 | bit %= PAD_DELAY_MAX; |
| 1662 | return delay & (1 << bit); |
| 1663 | } |
| 1664 | |
| 1665 | static int get_delay_len(u32 delay, u32 start_bit) |
| 1666 | { |
| 1667 | int i; |
| 1668 | |
| 1669 | for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) { |
| 1670 | if (test_delay_bit(delay, start_bit + i) == 0) |
| 1671 | return i; |
| 1672 | } |
| 1673 | return PAD_DELAY_MAX - start_bit; |
| 1674 | } |
| 1675 | |
| 1676 | static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay) |
| 1677 | { |
| 1678 | int start = 0, len = 0; |
| 1679 | int start_final = 0, len_final = 0; |
| 1680 | u8 final_phase = 0xff; |
| 1681 | struct msdc_delay_phase delay_phase = { 0, }; |
| 1682 | |
| 1683 | if (delay == 0) { |
| 1684 | dev_err(host->dev, "phase error: [map:%x]\n", delay); |
| 1685 | delay_phase.final_phase = final_phase; |
| 1686 | return delay_phase; |
| 1687 | } |
| 1688 | |
| 1689 | while (start < PAD_DELAY_MAX) { |
| 1690 | len = get_delay_len(delay, start); |
| 1691 | if (len_final < len) { |
| 1692 | start_final = start; |
| 1693 | len_final = len; |
| 1694 | } |
| 1695 | start += len ? len : 1; |
| 1696 | if (len >= 12 && start_final < 4) |
| 1697 | break; |
| 1698 | } |
| 1699 | |
| 1700 | /* The rule is that to find the smallest delay cell */ |
| 1701 | if (start_final == 0) |
| 1702 | final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX; |
| 1703 | else |
| 1704 | final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX; |
| 1705 | dev_info(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n", |
| 1706 | delay, len_final, final_phase); |
| 1707 | |
| 1708 | delay_phase.maxlen = len_final; |
| 1709 | delay_phase.start = start_final; |
| 1710 | delay_phase.final_phase = final_phase; |
| 1711 | return delay_phase; |
| 1712 | } |
| 1713 | |
| 1714 | static int msdc_tune_resp_data(struct mmc_host *mmc, u32 opcode) |
| 1715 | { |
| 1716 | struct msdc_host *host = mmc_priv(mmc); |
| 1717 | u32 rise_delay = 0, fall_delay = 0; |
| 1718 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; |
| 1719 | u8 final_delay, final_maxlen; |
| 1720 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 1721 | int err; |
| 1722 | int i, j; |
| 1723 | |
| 1724 | sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL, |
| 1725 | host->latch_ck); |
| 1726 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1727 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1728 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 1729 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
| 1730 | if (host->top_base != NULL) { |
| 1731 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1732 | PAD_CMD_RXDLY, i); |
| 1733 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 1734 | PAD_DAT_RD_RXDLY, i); |
| 1735 | } else { |
| 1736 | sdr_set_field(host->base + tune_reg, |
| 1737 | MSDC_PAD_TUNE_CMDRDLY, i); |
| 1738 | sdr_set_field(host->base + tune_reg, |
| 1739 | MSDC_PAD_TUNE_DATRRDLY, i); |
| 1740 | } |
| 1741 | /* |
| 1742 | * Using the same parameters, it may sometimes pass the test, |
| 1743 | * but sometimes it may fail. To make sure the parameters are |
| 1744 | * more stable, we test each set of parameters 3 times. |
| 1745 | */ |
| 1746 | for (j = 0; j < 3; j++) { |
| 1747 | err = mmc_send_tuning(mmc, opcode, NULL); |
| 1748 | if (!err) { |
| 1749 | rise_delay |= (1 << i); |
| 1750 | } else { |
| 1751 | rise_delay &= ~(1 << i); |
| 1752 | break; |
| 1753 | } |
| 1754 | } |
| 1755 | } |
| 1756 | final_rise_delay = get_best_delay(host, rise_delay); |
| 1757 | /* if rising edge has enough margin, then do not scan falling edge */ |
| 1758 | if (final_rise_delay.maxlen >= 12 || |
| 1759 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 1760 | goto skip_fall; |
| 1761 | |
| 1762 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1763 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1764 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 1765 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1766 | if (host->top_base != NULL) { |
| 1767 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1768 | PAD_CMD_RXDLY, i); |
| 1769 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 1770 | PAD_DAT_RD_RXDLY, i); |
| 1771 | } else { |
| 1772 | sdr_set_field(host->base + tune_reg, |
| 1773 | MSDC_PAD_TUNE_CMDRDLY, i); |
| 1774 | sdr_set_field(host->base + tune_reg, |
| 1775 | MSDC_PAD_TUNE_DATRRDLY, i); |
| 1776 | } |
| 1777 | |
| 1778 | for (j = 0; j < 3; j++) { |
| 1779 | err = mmc_send_tuning(mmc, opcode, NULL); |
| 1780 | if (!err) { |
| 1781 | fall_delay |= (1 << i); |
| 1782 | } else { |
| 1783 | fall_delay &= ~(1 << i); |
| 1784 | break; |
| 1785 | } |
| 1786 | } |
| 1787 | } |
| 1788 | final_fall_delay = get_best_delay(host, fall_delay); |
| 1789 | |
| 1790 | skip_fall: |
| 1791 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
| 1792 | if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4) |
| 1793 | final_maxlen = final_fall_delay.maxlen; |
| 1794 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1795 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1796 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1797 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 1798 | if (host->top_base != NULL) { |
| 1799 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1800 | PAD_CMD_RXDLY, |
| 1801 | final_rise_delay.final_phase); |
| 1802 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 1803 | PAD_DAT_RD_RXDLY, |
| 1804 | final_rise_delay.final_phase); |
| 1805 | } else { |
| 1806 | sdr_set_field(host->base + tune_reg, |
| 1807 | MSDC_PAD_TUNE_CMDRDLY, |
| 1808 | final_rise_delay.final_phase); |
| 1809 | sdr_set_field(host->base + tune_reg, |
| 1810 | MSDC_PAD_TUNE_DATRRDLY, |
| 1811 | final_rise_delay.final_phase); |
| 1812 | } |
| 1813 | final_delay = final_rise_delay.final_phase; |
| 1814 | } else { |
| 1815 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1816 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1817 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 1818 | if (host->top_base != NULL) { |
| 1819 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1820 | PAD_CMD_RXDLY, |
| 1821 | final_fall_delay.final_phase); |
| 1822 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 1823 | PAD_DAT_RD_RXDLY, |
| 1824 | final_fall_delay.final_phase); |
| 1825 | } else { |
| 1826 | sdr_set_field(host->base + tune_reg, |
| 1827 | MSDC_PAD_TUNE_CMDRDLY, |
| 1828 | final_fall_delay.final_phase); |
| 1829 | sdr_set_field(host->base + tune_reg, |
| 1830 | MSDC_PAD_TUNE_DATRRDLY, |
| 1831 | final_fall_delay.final_phase); |
| 1832 | } |
| 1833 | final_delay = final_fall_delay.final_phase; |
| 1834 | } |
| 1835 | |
| 1836 | dev_info(host->dev, "Final cmd/data pad delay: %x\n", final_delay); |
| 1837 | return final_delay == 0xff ? -EIO : 0; |
| 1838 | } |
| 1839 | |
| 1840 | static int msdc_tune_response(struct mmc_host *mmc, u32 opcode) |
| 1841 | { |
| 1842 | struct msdc_host *host = mmc_priv(mmc); |
| 1843 | u32 rise_delay = 0, fall_delay = 0; |
| 1844 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; |
| 1845 | struct msdc_delay_phase internal_delay_phase; |
| 1846 | u8 final_delay, final_maxlen; |
| 1847 | u32 internal_delay = 0; |
| 1848 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 1849 | int cmd_err; |
| 1850 | int i, j; |
| 1851 | |
| 1852 | if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 1853 | mmc->ios.timing == MMC_TIMING_UHS_SDR104) |
| 1854 | sdr_set_field(host->base + tune_reg, |
| 1855 | MSDC_PAD_TUNE_CMDRRDLY, |
| 1856 | host->hs200_cmd_int_delay); |
| 1857 | |
| 1858 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1859 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
| 1860 | if (host->top_base != NULL) |
| 1861 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1862 | PAD_CMD_RXDLY, i); |
| 1863 | else |
| 1864 | sdr_set_field(host->base + tune_reg, |
| 1865 | MSDC_PAD_TUNE_CMDRDLY, i); |
| 1866 | /* |
| 1867 | * Using the same parameters, it may sometimes pass the test, |
| 1868 | * but sometimes it may fail. To make sure the parameters are |
| 1869 | * more stable, we test each set of parameters 3 times. |
| 1870 | */ |
| 1871 | for (j = 0; j < 3; j++) { |
| 1872 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1873 | if (!cmd_err) { |
| 1874 | rise_delay |= (1 << i); |
| 1875 | } else { |
| 1876 | rise_delay &= ~(1 << i); |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | } |
| 1881 | final_rise_delay = get_best_delay(host, rise_delay); |
| 1882 | /* if rising edge has enough margin, then do not scan falling edge */ |
| 1883 | if (final_rise_delay.maxlen >= 12 || |
| 1884 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 1885 | goto skip_fall; |
| 1886 | |
| 1887 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1888 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1889 | if (host->top_base != NULL) |
| 1890 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1891 | PAD_CMD_RXDLY, i); |
| 1892 | else |
| 1893 | sdr_set_field(host->base + tune_reg, |
| 1894 | MSDC_PAD_TUNE_CMDRDLY, i); |
| 1895 | /* |
| 1896 | * Using the same parameters, it may sometimes pass the test, |
| 1897 | * but sometimes it may fail. To make sure the parameters are |
| 1898 | * more stable, we test each set of parameters 3 times. |
| 1899 | */ |
| 1900 | for (j = 0; j < 3; j++) { |
| 1901 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1902 | if (!cmd_err) { |
| 1903 | fall_delay |= (1 << i); |
| 1904 | } else { |
| 1905 | fall_delay &= ~(1 << i); |
| 1906 | break; |
| 1907 | } |
| 1908 | } |
| 1909 | } |
| 1910 | final_fall_delay = get_best_delay(host, fall_delay); |
| 1911 | |
| 1912 | skip_fall: |
| 1913 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
| 1914 | if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4) |
| 1915 | final_maxlen = final_fall_delay.maxlen; |
| 1916 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1917 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1918 | if (host->top_base != NULL) |
| 1919 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1920 | PAD_CMD_RXDLY, |
| 1921 | final_rise_delay.final_phase); |
| 1922 | else |
| 1923 | sdr_set_field(host->base + tune_reg, |
| 1924 | MSDC_PAD_TUNE_CMDRDLY, |
| 1925 | final_rise_delay.final_phase); |
| 1926 | final_delay = final_rise_delay.final_phase; |
| 1927 | } else { |
| 1928 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1929 | if (host->top_base != NULL) |
| 1930 | sdr_set_field(host->top_base + EMMC_TOP_CMD, |
| 1931 | PAD_CMD_RXDLY, |
| 1932 | final_fall_delay.final_phase); |
| 1933 | else |
| 1934 | sdr_set_field(host->base + tune_reg, |
| 1935 | MSDC_PAD_TUNE_CMDRDLY, |
| 1936 | final_fall_delay.final_phase); |
| 1937 | final_delay = final_fall_delay.final_phase; |
| 1938 | } |
| 1939 | if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay) |
| 1940 | goto skip_internal; |
| 1941 | |
| 1942 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1943 | sdr_set_field(host->base + tune_reg, |
| 1944 | MSDC_PAD_TUNE_CMDRRDLY, i); |
| 1945 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1946 | if (!cmd_err) |
| 1947 | internal_delay |= (1 << i); |
| 1948 | } |
| 1949 | dev_info(host->dev, "Final internal delay: 0x%x\n", internal_delay); |
| 1950 | internal_delay_phase = get_best_delay(host, internal_delay); |
| 1951 | sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRRDLY, |
| 1952 | internal_delay_phase.final_phase); |
| 1953 | skip_internal: |
| 1954 | dev_info(host->dev, "Final cmd pad delay: %x\n", final_delay); |
| 1955 | return final_delay == 0xff ? -EIO : 0; |
| 1956 | } |
| 1957 | |
| 1958 | static int hs400_tune_response(struct mmc_host *mmc, u32 opcode) |
| 1959 | { |
| 1960 | struct msdc_host *host = mmc_priv(mmc); |
| 1961 | u32 cmd_delay = 0; |
| 1962 | struct msdc_delay_phase final_cmd_delay = { 0,}; |
| 1963 | u8 final_delay; |
| 1964 | int cmd_err; |
| 1965 | int i, j; |
| 1966 | |
| 1967 | /* select EMMC50 PAD CMD tune */ |
| 1968 | sdr_set_bits(host->base + PAD_CMD_TUNE, BIT(0)); |
| 1969 | sdr_set_field(host->base + MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMDTA, 2); |
| 1970 | |
| 1971 | if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 1972 | mmc->ios.timing == MMC_TIMING_UHS_SDR104) |
| 1973 | sdr_set_field(host->base + MSDC_PAD_TUNE, |
| 1974 | MSDC_PAD_TUNE_CMDRRDLY, |
| 1975 | host->hs200_cmd_int_delay); |
| 1976 | |
| 1977 | if (host->hs400_cmd_resp_sel_rising) |
| 1978 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1979 | else |
| 1980 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1981 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
| 1982 | sdr_set_field(host->base + PAD_CMD_TUNE, |
| 1983 | PAD_CMD_TUNE_RX_DLY3, i); |
| 1984 | /* |
| 1985 | * Using the same parameters, it may sometimes pass the test, |
| 1986 | * but sometimes it may fail. To make sure the parameters are |
| 1987 | * more stable, we test each set of parameters 3 times. |
| 1988 | */ |
| 1989 | for (j = 0; j < 3; j++) { |
| 1990 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1991 | if (!cmd_err) { |
| 1992 | cmd_delay |= (1 << i); |
| 1993 | } else { |
| 1994 | cmd_delay &= ~(1 << i); |
| 1995 | break; |
| 1996 | } |
| 1997 | } |
| 1998 | } |
| 1999 | final_cmd_delay = get_best_delay(host, cmd_delay); |
| 2000 | sdr_set_field(host->base + PAD_CMD_TUNE, PAD_CMD_TUNE_RX_DLY3, |
| 2001 | final_cmd_delay.final_phase); |
| 2002 | final_delay = final_cmd_delay.final_phase; |
| 2003 | |
| 2004 | dev_info(host->dev, "Final cmd pad delay: %x\n", final_delay); |
| 2005 | return final_delay == 0xff ? -EIO : 0; |
| 2006 | } |
| 2007 | |
| 2008 | static int msdc_tune_data(struct mmc_host *mmc, u32 opcode) |
| 2009 | { |
| 2010 | struct msdc_host *host = mmc_priv(mmc); |
| 2011 | u32 rise_delay = 0, fall_delay = 0; |
| 2012 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; |
| 2013 | u8 final_delay, final_maxlen; |
| 2014 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 2015 | int i, ret; |
| 2016 | |
| 2017 | sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL, |
| 2018 | host->latch_ck); |
| 2019 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 2020 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 2021 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
| 2022 | if (host->top_base != NULL) |
| 2023 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 2024 | PAD_DAT_RD_RXDLY, i); |
| 2025 | else |
| 2026 | sdr_set_field(host->base + tune_reg, |
| 2027 | MSDC_PAD_TUNE_DATRRDLY, i); |
| 2028 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 2029 | if (!ret) |
| 2030 | rise_delay |= (1 << i); |
| 2031 | } |
| 2032 | final_rise_delay = get_best_delay(host, rise_delay); |
| 2033 | /* if rising edge has enough margin, then do not scan falling edge */ |
| 2034 | if (final_rise_delay.maxlen >= 12 || |
| 2035 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 2036 | goto skip_fall; |
| 2037 | |
| 2038 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 2039 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 2040 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 2041 | if (host->top_base != NULL) |
| 2042 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 2043 | PAD_DAT_RD_RXDLY, i); |
| 2044 | else |
| 2045 | sdr_set_field(host->base + tune_reg, |
| 2046 | MSDC_PAD_TUNE_DATRRDLY, i); |
| 2047 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 2048 | if (!ret) |
| 2049 | fall_delay |= (1 << i); |
| 2050 | } |
| 2051 | final_fall_delay = get_best_delay(host, fall_delay); |
| 2052 | |
| 2053 | skip_fall: |
| 2054 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
| 2055 | if (final_maxlen == final_rise_delay.maxlen) { |
| 2056 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 2057 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 2058 | if (host->top_base != NULL) |
| 2059 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 2060 | PAD_DAT_RD_RXDLY, |
| 2061 | final_rise_delay.final_phase); |
| 2062 | else |
| 2063 | sdr_set_field(host->base + tune_reg, |
| 2064 | MSDC_PAD_TUNE_DATRRDLY, |
| 2065 | final_rise_delay.final_phase); |
| 2066 | final_delay = final_rise_delay.final_phase; |
| 2067 | } else { |
| 2068 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 2069 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 2070 | if (host->top_base != NULL) |
| 2071 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 2072 | PAD_DAT_RD_RXDLY, |
| 2073 | final_fall_delay.final_phase); |
| 2074 | else |
| 2075 | sdr_set_field(host->base + tune_reg, |
| 2076 | MSDC_PAD_TUNE_DATRRDLY, |
| 2077 | final_fall_delay.final_phase); |
| 2078 | final_delay = final_fall_delay.final_phase; |
| 2079 | } |
| 2080 | |
| 2081 | dev_info(host->dev, "Final data pad delay: %x\n", final_delay); |
| 2082 | return final_delay == 0xff ? -EIO : 0; |
| 2083 | } |
| 2084 | |
| 2085 | static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 2086 | { |
| 2087 | struct msdc_host *host = mmc_priv(mmc); |
| 2088 | int ret; |
| 2089 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 2090 | |
| 2091 | if (host->dev_comp->tune_resp_data_together) { |
| 2092 | ret = msdc_tune_resp_data(mmc, opcode); |
| 2093 | if (ret == -EIO) |
| 2094 | dev_err(host->dev, "Tune cmd/data fail!\n"); |
| 2095 | if (host->hs400_mode) { |
| 2096 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 2097 | sdr_clr_bits(host->base + MSDC_IOCON, |
| 2098 | MSDC_IOCON_W_DSPL); |
| 2099 | if (host->top_base != NULL) |
| 2100 | sdr_set_field(host->top_base + EMMC_TOP_CONTROL, |
| 2101 | PAD_DAT_RD_RXDLY, 0); |
| 2102 | else |
| 2103 | sdr_set_field(host->base + tune_reg, |
| 2104 | MSDC_PAD_TUNE_DATRRDLY, 0); |
| 2105 | } |
| 2106 | } else { |
| 2107 | if (host->hs400_mode && |
| 2108 | host->dev_comp->hs400_tune) |
| 2109 | ret = hs400_tune_response(mmc, opcode); |
| 2110 | else |
| 2111 | ret = msdc_tune_response(mmc, opcode); |
| 2112 | if (ret == -EIO) { |
| 2113 | dev_err(host->dev, "Tune response fail!\n"); |
| 2114 | return ret; |
| 2115 | } |
| 2116 | if (host->hs400_mode == false) { |
| 2117 | ret = msdc_tune_data(mmc, opcode); |
| 2118 | if (ret == -EIO) |
| 2119 | dev_err(host->dev, "Tune data fail!\n"); |
| 2120 | } |
| 2121 | |
| 2122 | } |
| 2123 | |
| 2124 | host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); |
| 2125 | host->saved_tune_para.pad_tune = readl(host->base + tune_reg); |
| 2126 | host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); |
| 2127 | if (host->top_base != NULL) { |
| 2128 | host->saved_tune_para.emmc_top_control = readl(host->top_base + |
| 2129 | EMMC_TOP_CONTROL); |
| 2130 | host->saved_tune_para.emmc_top_cmd = readl(host->top_base + |
| 2131 | EMMC_TOP_CMD); |
| 2132 | } |
| 2133 | return ret; |
| 2134 | } |
| 2135 | |
| 2136 | static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) |
| 2137 | { |
| 2138 | struct msdc_host *host = mmc_priv(mmc); |
| 2139 | host->hs400_mode = true; |
| 2140 | |
| 2141 | if (host->top_base != NULL) |
| 2142 | writel(host->hs400_ds_delay, |
| 2143 | host->top_base + EMMC50_PAD_DS_TUNE); |
| 2144 | else |
| 2145 | writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE); |
| 2146 | /* hs400 mode must set it to 0 */ |
| 2147 | sdr_clr_bits(host->base + MSDC_PATCH_BIT2, MSDC_PATCH_BIT2_CFGCRCSTS); |
| 2148 | /* to improve read performance, set outstanding to 2 */ |
| 2149 | sdr_set_field(host->base + EMMC50_CFG3, EMMC50_CFG3_OUTS_WR, 2); |
| 2150 | |
| 2151 | return 0; |
| 2152 | } |
| 2153 | |
| 2154 | static void msdc_hw_reset(struct mmc_host *mmc) |
| 2155 | { |
| 2156 | struct msdc_host *host = mmc_priv(mmc); |
| 2157 | |
| 2158 | sdr_set_bits(host->base + EMMC_IOCON, 1); |
| 2159 | udelay(10); /* 10us is enough */ |
| 2160 | sdr_clr_bits(host->base + EMMC_IOCON, 1); |
| 2161 | } |
| 2162 | |
| 2163 | static void msdc_enable_sdio_irq(struct mmc_host *mmc, int enable) |
| 2164 | { |
| 2165 | struct msdc_host *host = mmc_priv(mmc); |
| 2166 | |
| 2167 | if (enable) |
| 2168 | pm_runtime_get_sync(host->dev); |
| 2169 | |
| 2170 | __msdc_enable_sdio_irq(host, enable); |
| 2171 | |
| 2172 | if (enable) { |
| 2173 | pm_runtime_mark_last_busy(host->dev); |
| 2174 | pm_runtime_put_autosuspend(host->dev); |
| 2175 | } else { |
| 2176 | pm_runtime_put_noidle(host->dev); |
| 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | static void msdc_ack_sdio_irq(struct mmc_host *mmc) |
| 2181 | { |
| 2182 | struct msdc_host *host = mmc_priv(mmc); |
| 2183 | |
| 2184 | __msdc_enable_sdio_irq(host, 1); |
| 2185 | } |
| 2186 | |
| 2187 | static int msdc_select_drive_strength(struct mmc_card *card, |
| 2188 | unsigned int max_dtr, int host_drv, int card_drv, int *drv_type) |
| 2189 | { |
| 2190 | struct mmc_host *mmc = card->host; |
| 2191 | struct msdc_host *host = mmc_priv(mmc); |
| 2192 | |
| 2193 | of_property_read_u32(host->dev->of_node, "drv-type", drv_type); |
| 2194 | if (card_drv & (1 << (*drv_type))) |
| 2195 | return *drv_type; |
| 2196 | else |
| 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | static const struct mmc_host_ops mt_msdc_ops = { |
| 2201 | .post_req = msdc_post_req, |
| 2202 | .pre_req = msdc_pre_req, |
| 2203 | .request = msdc_ops_request, |
| 2204 | .set_ios = msdc_ops_set_ios, |
| 2205 | .get_ro = mmc_gpio_get_ro, |
| 2206 | .get_cd = mmc_gpio_get_cd, |
| 2207 | .start_signal_voltage_switch = msdc_ops_switch_volt, |
| 2208 | .card_busy = msdc_card_busy, |
| 2209 | .execute_tuning = msdc_execute_tuning, |
| 2210 | .prepare_hs400_tuning = msdc_prepare_hs400_tuning, |
| 2211 | .hw_reset = msdc_hw_reset, |
| 2212 | .enable_sdio_irq = msdc_enable_sdio_irq, |
| 2213 | .ack_sdio_irq = msdc_ack_sdio_irq, |
| 2214 | .select_drive_strength = msdc_select_drive_strength, |
| 2215 | }; |
| 2216 | |
| 2217 | static void msdc_of_property_parse(struct platform_device *pdev, |
| 2218 | struct msdc_host *host) |
| 2219 | { |
| 2220 | of_property_read_u32(pdev->dev.of_node, "mediatek,latch-ck", |
| 2221 | &host->latch_ck); |
| 2222 | |
| 2223 | of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay", |
| 2224 | &host->hs400_ds_delay); |
| 2225 | |
| 2226 | of_property_read_u32(pdev->dev.of_node, "mediatek,hs200-cmd-int-delay", |
| 2227 | &host->hs200_cmd_int_delay); |
| 2228 | |
| 2229 | of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-cmd-int-delay", |
| 2230 | &host->hs400_cmd_int_delay); |
| 2231 | |
| 2232 | if (of_property_read_bool(pdev->dev.of_node, |
| 2233 | "mediatek,hs400-cmd-resp-sel-rising")) |
| 2234 | host->hs400_cmd_resp_sel_rising = true; |
| 2235 | else |
| 2236 | host->hs400_cmd_resp_sel_rising = false; |
| 2237 | } |
| 2238 | |
| 2239 | static int msdc_drv_probe(struct platform_device *pdev) |
| 2240 | { |
| 2241 | struct mmc_host *mmc; |
| 2242 | struct msdc_host *host; |
| 2243 | struct resource *res; |
| 2244 | const struct of_device_id *of_id; |
| 2245 | int ret; |
| 2246 | |
| 2247 | if (!pdev->dev.of_node) { |
| 2248 | dev_err(&pdev->dev, "No DT found\n"); |
| 2249 | return -EINVAL; |
| 2250 | } |
| 2251 | |
| 2252 | of_id = of_match_node(msdc_of_ids, pdev->dev.of_node); |
| 2253 | if (!of_id) |
| 2254 | return -EINVAL; |
| 2255 | /* Allocate MMC host for this device */ |
| 2256 | mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev); |
| 2257 | if (!mmc) |
| 2258 | return -ENOMEM; |
| 2259 | |
| 2260 | host = mmc_priv(mmc); |
| 2261 | ret = mmc_of_parse(mmc); |
| 2262 | if (ret) |
| 2263 | goto host_free; |
| 2264 | |
| 2265 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 2266 | host->base = devm_ioremap_resource(&pdev->dev, res); |
| 2267 | if (IS_ERR(host->base)) { |
| 2268 | ret = PTR_ERR(host->base); |
| 2269 | goto host_free; |
| 2270 | } |
| 2271 | |
| 2272 | res = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 2273 | host->top_base = devm_ioremap_resource(&pdev->dev, res); |
| 2274 | if (IS_ERR(host->top_base)) |
| 2275 | host->top_base = NULL; |
| 2276 | |
| 2277 | ret = mmc_regulator_get_supply(mmc); |
| 2278 | if (ret) |
| 2279 | goto host_free; |
| 2280 | |
| 2281 | host->src_clk = devm_clk_get(&pdev->dev, "source"); |
| 2282 | if (IS_ERR(host->src_clk)) { |
| 2283 | ret = PTR_ERR(host->src_clk); |
| 2284 | goto host_free; |
| 2285 | } |
| 2286 | |
| 2287 | host->h_clk = devm_clk_get(&pdev->dev, "hclk"); |
| 2288 | if (IS_ERR(host->h_clk)) { |
| 2289 | ret = PTR_ERR(host->h_clk); |
| 2290 | goto host_free; |
| 2291 | } |
| 2292 | |
| 2293 | /*source clock control gate is optional clock*/ |
| 2294 | host->src_clk_cg = devm_clk_get(&pdev->dev, "source_cg"); |
| 2295 | if (IS_ERR(host->src_clk_cg)) |
| 2296 | host->src_clk_cg = NULL; |
| 2297 | |
| 2298 | host->irq = platform_get_irq(pdev, 0); |
| 2299 | if (host->irq < 0) { |
| 2300 | ret = -EINVAL; |
| 2301 | goto host_free; |
| 2302 | } |
| 2303 | |
| 2304 | host->pinctrl = devm_pinctrl_get(&pdev->dev); |
| 2305 | if (IS_ERR(host->pinctrl)) { |
| 2306 | ret = PTR_ERR(host->pinctrl); |
| 2307 | dev_err(&pdev->dev, "Cannot find pinctrl!\n"); |
| 2308 | goto host_free; |
| 2309 | } |
| 2310 | |
| 2311 | host->pins_default = pinctrl_lookup_state(host->pinctrl, "default"); |
| 2312 | if (IS_ERR(host->pins_default)) { |
| 2313 | ret = PTR_ERR(host->pins_default); |
| 2314 | dev_err(&pdev->dev, "Cannot find pinctrl default!\n"); |
| 2315 | goto host_free; |
| 2316 | } |
| 2317 | |
| 2318 | host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs"); |
| 2319 | if (IS_ERR(host->pins_uhs)) { |
| 2320 | ret = PTR_ERR(host->pins_uhs); |
| 2321 | dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n"); |
| 2322 | goto host_free; |
| 2323 | } |
| 2324 | |
| 2325 | msdc_of_property_parse(pdev, host); |
| 2326 | |
| 2327 | host->dev = &pdev->dev; |
| 2328 | host->dev_comp = of_id->data; |
| 2329 | host->mmc = mmc; |
| 2330 | host->src_clk_freq = clk_get_rate(host->src_clk); |
| 2331 | /* Set host parameters to mmc */ |
| 2332 | mmc->ops = &mt_msdc_ops; |
| 2333 | if (host->dev_comp->clk_div_bits == 8) |
| 2334 | mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255); |
| 2335 | else |
| 2336 | mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 4095); |
| 2337 | |
| 2338 | mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23; |
| 2339 | /* MMC core transfer sizes tunable parameters */ |
| 2340 | mmc->max_segs = MAX_BD_NUM; |
| 2341 | mmc->max_seg_size = BDMA_DESC_BUFLEN; |
| 2342 | mmc->max_blk_size = 2048; |
| 2343 | mmc->max_req_size = 512 * 1024; |
| 2344 | mmc->max_blk_count = mmc->max_req_size / 512; |
| 2345 | if (host->dev_comp->support_64g) |
| 2346 | host->dma_mask = DMA_BIT_MASK(36); |
| 2347 | else |
| 2348 | host->dma_mask = DMA_BIT_MASK(32); |
| 2349 | mmc_dev(mmc)->dma_mask = &host->dma_mask; |
| 2350 | |
| 2351 | host->timeout_clks = 3 * 1048576; |
| 2352 | host->dma.gpd = dma_alloc_coherent(&pdev->dev, |
| 2353 | 2 * sizeof(struct mt_gpdma_desc), |
| 2354 | &host->dma.gpd_addr, GFP_KERNEL); |
| 2355 | host->dma.bd = dma_alloc_coherent(&pdev->dev, |
| 2356 | MAX_BD_NUM * sizeof(struct mt_bdma_desc), |
| 2357 | &host->dma.bd_addr, GFP_KERNEL); |
| 2358 | if (!host->dma.gpd || !host->dma.bd) { |
| 2359 | ret = -ENOMEM; |
| 2360 | goto release_mem; |
| 2361 | } |
| 2362 | msdc_init_gpd_bd(host, &host->dma); |
| 2363 | INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout); |
| 2364 | spin_lock_init(&host->lock); |
| 2365 | |
| 2366 | platform_set_drvdata(pdev, mmc); |
| 2367 | msdc_ungate_clock(host); |
| 2368 | msdc_init_hw(host); |
| 2369 | |
| 2370 | ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq, |
| 2371 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, pdev->name, host); |
| 2372 | if (ret) |
| 2373 | goto release; |
| 2374 | |
| 2375 | pm_runtime_set_active(host->dev); |
| 2376 | pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY); |
| 2377 | pm_runtime_use_autosuspend(host->dev); |
| 2378 | pm_runtime_enable(host->dev); |
| 2379 | ret = mmc_add_host(mmc); |
| 2380 | |
| 2381 | if (ret) |
| 2382 | goto end; |
| 2383 | |
| 2384 | return 0; |
| 2385 | end: |
| 2386 | pm_runtime_disable(host->dev); |
| 2387 | release: |
| 2388 | platform_set_drvdata(pdev, NULL); |
| 2389 | msdc_deinit_hw(host); |
| 2390 | msdc_gate_clock(host); |
| 2391 | release_mem: |
| 2392 | if (host->dma.gpd) |
| 2393 | dma_free_coherent(&pdev->dev, |
| 2394 | 2 * sizeof(struct mt_gpdma_desc), |
| 2395 | host->dma.gpd, host->dma.gpd_addr); |
| 2396 | if (host->dma.bd) |
| 2397 | dma_free_coherent(&pdev->dev, |
| 2398 | MAX_BD_NUM * sizeof(struct mt_bdma_desc), |
| 2399 | host->dma.bd, host->dma.bd_addr); |
| 2400 | host_free: |
| 2401 | mmc_free_host(mmc); |
| 2402 | |
| 2403 | return ret; |
| 2404 | } |
| 2405 | |
| 2406 | static int msdc_drv_remove(struct platform_device *pdev) |
| 2407 | { |
| 2408 | struct mmc_host *mmc; |
| 2409 | struct msdc_host *host; |
| 2410 | |
| 2411 | mmc = platform_get_drvdata(pdev); |
| 2412 | host = mmc_priv(mmc); |
| 2413 | |
| 2414 | pm_runtime_get_sync(host->dev); |
| 2415 | |
| 2416 | platform_set_drvdata(pdev, NULL); |
| 2417 | mmc_remove_host(host->mmc); |
| 2418 | msdc_deinit_hw(host); |
| 2419 | msdc_gate_clock(host); |
| 2420 | |
| 2421 | pm_runtime_disable(host->dev); |
| 2422 | pm_runtime_put_noidle(host->dev); |
| 2423 | dma_free_coherent(&pdev->dev, |
| 2424 | 2 * sizeof(struct mt_gpdma_desc), |
| 2425 | host->dma.gpd, host->dma.gpd_addr); |
| 2426 | dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc), |
| 2427 | host->dma.bd, host->dma.bd_addr); |
| 2428 | |
| 2429 | mmc_free_host(host->mmc); |
| 2430 | |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
| 2434 | #ifdef CONFIG_PM |
| 2435 | static void msdc_save_reg(struct msdc_host *host) |
| 2436 | { |
| 2437 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 2438 | |
| 2439 | host->save_para.msdc_cfg = readl(host->base + MSDC_CFG); |
| 2440 | host->save_para.iocon = readl(host->base + MSDC_IOCON); |
| 2441 | host->save_para.sdc_cfg = readl(host->base + SDC_CFG); |
| 2442 | host->save_para.pad_tune = readl(host->base + tune_reg); |
| 2443 | host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT); |
| 2444 | host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1); |
| 2445 | host->save_para.patch_bit2 = readl(host->base + MSDC_PATCH_BIT2); |
| 2446 | host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE); |
| 2447 | host->save_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); |
| 2448 | host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0); |
| 2449 | host->save_para.emmc50_cfg3 = readl(host->base + EMMC50_CFG3); |
| 2450 | host->save_para.sdc_fifo_cfg = readl(host->base + SDC_FIFO_CFG); |
| 2451 | if (host->top_base != NULL) { |
| 2452 | host->save_para.emmc_top_control = |
| 2453 | readl(host->top_base + EMMC_TOP_CONTROL); |
| 2454 | host->save_para.emmc_top_cmd = |
| 2455 | readl(host->top_base + EMMC_TOP_CMD); |
| 2456 | host->save_para.emmc50_pad_ds_tune = |
| 2457 | readl(host->top_base + EMMC50_PAD_DS_TUNE); |
| 2458 | |
| 2459 | } |
| 2460 | } |
| 2461 | |
| 2462 | static void msdc_restore_reg(struct msdc_host *host) |
| 2463 | { |
| 2464 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 2465 | |
| 2466 | writel(host->save_para.msdc_cfg, host->base + MSDC_CFG); |
| 2467 | writel(host->save_para.iocon, host->base + MSDC_IOCON); |
| 2468 | writel(host->save_para.sdc_cfg, host->base + SDC_CFG); |
| 2469 | writel(host->save_para.pad_tune, host->base + tune_reg); |
| 2470 | writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT); |
| 2471 | writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1); |
| 2472 | writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2); |
| 2473 | writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE); |
| 2474 | writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE); |
| 2475 | writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0); |
| 2476 | writel(host->save_para.emmc50_cfg3, host->base + EMMC50_CFG3); |
| 2477 | writel(host->save_para.sdc_fifo_cfg, host->base + SDC_FIFO_CFG); |
| 2478 | if (host->top_base != NULL) { |
| 2479 | writel(host->save_para.emmc_top_control, |
| 2480 | host->top_base + EMMC_TOP_CONTROL); |
| 2481 | writel(host->save_para.emmc_top_cmd, |
| 2482 | host->top_base + EMMC_TOP_CMD); |
| 2483 | writel(host->save_para.emmc50_pad_ds_tune, |
| 2484 | host->top_base + EMMC50_PAD_DS_TUNE); |
| 2485 | } |
| 2486 | } |
| 2487 | |
| 2488 | static int msdc_runtime_suspend(struct device *dev) |
| 2489 | { |
| 2490 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 2491 | struct msdc_host *host = mmc_priv(mmc); |
| 2492 | |
| 2493 | msdc_save_reg(host); |
| 2494 | msdc_gate_clock(host); |
| 2495 | return 0; |
| 2496 | } |
| 2497 | |
| 2498 | static int msdc_runtime_resume(struct device *dev) |
| 2499 | { |
| 2500 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 2501 | struct msdc_host *host = mmc_priv(mmc); |
| 2502 | |
| 2503 | msdc_ungate_clock(host); |
| 2504 | msdc_restore_reg(host); |
| 2505 | return 0; |
| 2506 | } |
| 2507 | #endif |
| 2508 | |
| 2509 | static const struct dev_pm_ops msdc_dev_pm_ops = { |
| 2510 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 2511 | pm_runtime_force_resume) |
| 2512 | SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL) |
| 2513 | }; |
| 2514 | |
| 2515 | static struct platform_driver mt_msdc_driver = { |
| 2516 | .probe = msdc_drv_probe, |
| 2517 | .remove = msdc_drv_remove, |
| 2518 | .driver = { |
| 2519 | .name = "mtk-msdc", |
| 2520 | .of_match_table = msdc_of_ids, |
| 2521 | .pm = &msdc_dev_pm_ops, |
| 2522 | }, |
| 2523 | }; |
| 2524 | |
| 2525 | module_platform_driver(mt_msdc_driver); |
| 2526 | MODULE_LICENSE("GPL v2"); |
| 2527 | MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver"); |