Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +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> |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 26 | #include <linux/pm.h> |
| 27 | #include <linux/pm_runtime.h> |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 28 | #include <linux/regulator/consumer.h> |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 29 | #include <linux/slab.h> |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 30 | #include <linux/spinlock.h> |
Ulf Hansson | b8789ec | 2016-12-30 13:47:23 +0100 | [diff] [blame] | 31 | #include <linux/interrupt.h> |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 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> |
Chaotian Jing | 8d53e41 | 2016-02-15 02:31:00 +0800 | [diff] [blame] | 39 | #include <linux/mmc/slot-gpio.h> |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 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 |
Chaotian Jing | c9b5061 | 2015-10-27 14:24:26 +0800 | [diff] [blame] | 70 | #define EMMC_IOCON 0x7c |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 71 | #define SDC_ACMD_RESP 0x80 |
| 72 | #define MSDC_DMA_SA 0x90 |
| 73 | #define MSDC_DMA_CTRL 0x98 |
| 74 | #define MSDC_DMA_CFG 0x9c |
| 75 | #define MSDC_PATCH_BIT 0xb0 |
| 76 | #define MSDC_PATCH_BIT1 0xb4 |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 77 | #define MSDC_PATCH_BIT2 0xb8 |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 78 | #define MSDC_PAD_TUNE 0xec |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 79 | #define MSDC_PAD_TUNE0 0xf0 |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 80 | #define PAD_DS_TUNE 0x188 |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 81 | #define PAD_CMD_TUNE 0x18c |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 82 | #define EMMC50_CFG0 0x208 |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 83 | |
| 84 | /*--------------------------------------------------------------------------*/ |
| 85 | /* Register Mask */ |
| 86 | /*--------------------------------------------------------------------------*/ |
| 87 | |
| 88 | /* MSDC_CFG mask */ |
| 89 | #define MSDC_CFG_MODE (0x1 << 0) /* RW */ |
| 90 | #define MSDC_CFG_CKPDN (0x1 << 1) /* RW */ |
| 91 | #define MSDC_CFG_RST (0x1 << 2) /* RW */ |
| 92 | #define MSDC_CFG_PIO (0x1 << 3) /* RW */ |
| 93 | #define MSDC_CFG_CKDRVEN (0x1 << 4) /* RW */ |
| 94 | #define MSDC_CFG_BV18SDT (0x1 << 5) /* RW */ |
| 95 | #define MSDC_CFG_BV18PSS (0x1 << 6) /* R */ |
| 96 | #define MSDC_CFG_CKSTB (0x1 << 7) /* R */ |
| 97 | #define MSDC_CFG_CKDIV (0xff << 8) /* RW */ |
| 98 | #define MSDC_CFG_CKMOD (0x3 << 16) /* RW */ |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 99 | #define MSDC_CFG_HS400_CK_MODE (0x1 << 18) /* RW */ |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 100 | #define MSDC_CFG_HS400_CK_MODE_EXTRA (0x1 << 22) /* RW */ |
| 101 | #define MSDC_CFG_CKDIV_EXTRA (0xfff << 8) /* RW */ |
| 102 | #define MSDC_CFG_CKMOD_EXTRA (0x3 << 20) /* RW */ |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 103 | |
| 104 | /* MSDC_IOCON mask */ |
| 105 | #define MSDC_IOCON_SDR104CKS (0x1 << 0) /* RW */ |
| 106 | #define MSDC_IOCON_RSPL (0x1 << 1) /* RW */ |
| 107 | #define MSDC_IOCON_DSPL (0x1 << 2) /* RW */ |
| 108 | #define MSDC_IOCON_DDLSEL (0x1 << 3) /* RW */ |
| 109 | #define MSDC_IOCON_DDR50CKD (0x1 << 4) /* RW */ |
| 110 | #define MSDC_IOCON_DSPLSEL (0x1 << 5) /* RW */ |
| 111 | #define MSDC_IOCON_W_DSPL (0x1 << 8) /* RW */ |
| 112 | #define MSDC_IOCON_D0SPL (0x1 << 16) /* RW */ |
| 113 | #define MSDC_IOCON_D1SPL (0x1 << 17) /* RW */ |
| 114 | #define MSDC_IOCON_D2SPL (0x1 << 18) /* RW */ |
| 115 | #define MSDC_IOCON_D3SPL (0x1 << 19) /* RW */ |
| 116 | #define MSDC_IOCON_D4SPL (0x1 << 20) /* RW */ |
| 117 | #define MSDC_IOCON_D5SPL (0x1 << 21) /* RW */ |
| 118 | #define MSDC_IOCON_D6SPL (0x1 << 22) /* RW */ |
| 119 | #define MSDC_IOCON_D7SPL (0x1 << 23) /* RW */ |
| 120 | #define MSDC_IOCON_RISCSZ (0x3 << 24) /* RW */ |
| 121 | |
| 122 | /* MSDC_PS mask */ |
| 123 | #define MSDC_PS_CDEN (0x1 << 0) /* RW */ |
| 124 | #define MSDC_PS_CDSTS (0x1 << 1) /* R */ |
| 125 | #define MSDC_PS_CDDEBOUNCE (0xf << 12) /* RW */ |
| 126 | #define MSDC_PS_DAT (0xff << 16) /* R */ |
| 127 | #define MSDC_PS_CMD (0x1 << 24) /* R */ |
| 128 | #define MSDC_PS_WP (0x1 << 31) /* R */ |
| 129 | |
| 130 | /* MSDC_INT mask */ |
| 131 | #define MSDC_INT_MMCIRQ (0x1 << 0) /* W1C */ |
| 132 | #define MSDC_INT_CDSC (0x1 << 1) /* W1C */ |
| 133 | #define MSDC_INT_ACMDRDY (0x1 << 3) /* W1C */ |
| 134 | #define MSDC_INT_ACMDTMO (0x1 << 4) /* W1C */ |
| 135 | #define MSDC_INT_ACMDCRCERR (0x1 << 5) /* W1C */ |
| 136 | #define MSDC_INT_DMAQ_EMPTY (0x1 << 6) /* W1C */ |
| 137 | #define MSDC_INT_SDIOIRQ (0x1 << 7) /* W1C */ |
| 138 | #define MSDC_INT_CMDRDY (0x1 << 8) /* W1C */ |
| 139 | #define MSDC_INT_CMDTMO (0x1 << 9) /* W1C */ |
| 140 | #define MSDC_INT_RSPCRCERR (0x1 << 10) /* W1C */ |
| 141 | #define MSDC_INT_CSTA (0x1 << 11) /* R */ |
| 142 | #define MSDC_INT_XFER_COMPL (0x1 << 12) /* W1C */ |
| 143 | #define MSDC_INT_DXFER_DONE (0x1 << 13) /* W1C */ |
| 144 | #define MSDC_INT_DATTMO (0x1 << 14) /* W1C */ |
| 145 | #define MSDC_INT_DATCRCERR (0x1 << 15) /* W1C */ |
| 146 | #define MSDC_INT_ACMD19_DONE (0x1 << 16) /* W1C */ |
| 147 | #define MSDC_INT_DMA_BDCSERR (0x1 << 17) /* W1C */ |
| 148 | #define MSDC_INT_DMA_GPDCSERR (0x1 << 18) /* W1C */ |
| 149 | #define MSDC_INT_DMA_PROTECT (0x1 << 19) /* W1C */ |
| 150 | |
| 151 | /* MSDC_INTEN mask */ |
| 152 | #define MSDC_INTEN_MMCIRQ (0x1 << 0) /* RW */ |
| 153 | #define MSDC_INTEN_CDSC (0x1 << 1) /* RW */ |
| 154 | #define MSDC_INTEN_ACMDRDY (0x1 << 3) /* RW */ |
| 155 | #define MSDC_INTEN_ACMDTMO (0x1 << 4) /* RW */ |
| 156 | #define MSDC_INTEN_ACMDCRCERR (0x1 << 5) /* RW */ |
| 157 | #define MSDC_INTEN_DMAQ_EMPTY (0x1 << 6) /* RW */ |
| 158 | #define MSDC_INTEN_SDIOIRQ (0x1 << 7) /* RW */ |
| 159 | #define MSDC_INTEN_CMDRDY (0x1 << 8) /* RW */ |
| 160 | #define MSDC_INTEN_CMDTMO (0x1 << 9) /* RW */ |
| 161 | #define MSDC_INTEN_RSPCRCERR (0x1 << 10) /* RW */ |
| 162 | #define MSDC_INTEN_CSTA (0x1 << 11) /* RW */ |
| 163 | #define MSDC_INTEN_XFER_COMPL (0x1 << 12) /* RW */ |
| 164 | #define MSDC_INTEN_DXFER_DONE (0x1 << 13) /* RW */ |
| 165 | #define MSDC_INTEN_DATTMO (0x1 << 14) /* RW */ |
| 166 | #define MSDC_INTEN_DATCRCERR (0x1 << 15) /* RW */ |
| 167 | #define MSDC_INTEN_ACMD19_DONE (0x1 << 16) /* RW */ |
| 168 | #define MSDC_INTEN_DMA_BDCSERR (0x1 << 17) /* RW */ |
| 169 | #define MSDC_INTEN_DMA_GPDCSERR (0x1 << 18) /* RW */ |
| 170 | #define MSDC_INTEN_DMA_PROTECT (0x1 << 19) /* RW */ |
| 171 | |
| 172 | /* MSDC_FIFOCS mask */ |
| 173 | #define MSDC_FIFOCS_RXCNT (0xff << 0) /* R */ |
| 174 | #define MSDC_FIFOCS_TXCNT (0xff << 16) /* R */ |
| 175 | #define MSDC_FIFOCS_CLR (0x1 << 31) /* RW */ |
| 176 | |
| 177 | /* SDC_CFG mask */ |
| 178 | #define SDC_CFG_SDIOINTWKUP (0x1 << 0) /* RW */ |
| 179 | #define SDC_CFG_INSWKUP (0x1 << 1) /* RW */ |
| 180 | #define SDC_CFG_BUSWIDTH (0x3 << 16) /* RW */ |
| 181 | #define SDC_CFG_SDIO (0x1 << 19) /* RW */ |
| 182 | #define SDC_CFG_SDIOIDE (0x1 << 20) /* RW */ |
| 183 | #define SDC_CFG_INTATGAP (0x1 << 21) /* RW */ |
| 184 | #define SDC_CFG_DTOC (0xff << 24) /* RW */ |
| 185 | |
| 186 | /* SDC_STS mask */ |
| 187 | #define SDC_STS_SDCBUSY (0x1 << 0) /* RW */ |
| 188 | #define SDC_STS_CMDBUSY (0x1 << 1) /* RW */ |
| 189 | #define SDC_STS_SWR_COMPL (0x1 << 31) /* RW */ |
| 190 | |
| 191 | /* MSDC_DMA_CTRL mask */ |
| 192 | #define MSDC_DMA_CTRL_START (0x1 << 0) /* W */ |
| 193 | #define MSDC_DMA_CTRL_STOP (0x1 << 1) /* W */ |
| 194 | #define MSDC_DMA_CTRL_RESUME (0x1 << 2) /* W */ |
| 195 | #define MSDC_DMA_CTRL_MODE (0x1 << 8) /* RW */ |
| 196 | #define MSDC_DMA_CTRL_LASTBUF (0x1 << 10) /* RW */ |
| 197 | #define MSDC_DMA_CTRL_BRUSTSZ (0x7 << 12) /* RW */ |
| 198 | |
| 199 | /* MSDC_DMA_CFG mask */ |
| 200 | #define MSDC_DMA_CFG_STS (0x1 << 0) /* R */ |
| 201 | #define MSDC_DMA_CFG_DECSEN (0x1 << 1) /* RW */ |
| 202 | #define MSDC_DMA_CFG_AHBHPROT2 (0x2 << 8) /* RW */ |
| 203 | #define MSDC_DMA_CFG_ACTIVEEN (0x2 << 12) /* RW */ |
| 204 | #define MSDC_DMA_CFG_CS12B16B (0x1 << 16) /* RW */ |
| 205 | |
| 206 | /* MSDC_PATCH_BIT mask */ |
| 207 | #define MSDC_PATCH_BIT_ODDSUPP (0x1 << 1) /* RW */ |
| 208 | #define MSDC_INT_DAT_LATCH_CK_SEL (0x7 << 7) |
| 209 | #define MSDC_CKGEN_MSDC_DLY_SEL (0x1f << 10) |
| 210 | #define MSDC_PATCH_BIT_IODSSEL (0x1 << 16) /* RW */ |
| 211 | #define MSDC_PATCH_BIT_IOINTSEL (0x1 << 17) /* RW */ |
| 212 | #define MSDC_PATCH_BIT_BUSYDLY (0xf << 18) /* RW */ |
| 213 | #define MSDC_PATCH_BIT_WDOD (0xf << 22) /* RW */ |
| 214 | #define MSDC_PATCH_BIT_IDRTSEL (0x1 << 26) /* RW */ |
| 215 | #define MSDC_PATCH_BIT_CMDFSEL (0x1 << 27) /* RW */ |
| 216 | #define MSDC_PATCH_BIT_INTDLSEL (0x1 << 28) /* RW */ |
| 217 | #define MSDC_PATCH_BIT_SPCPUSH (0x1 << 29) /* RW */ |
| 218 | #define MSDC_PATCH_BIT_DECRCTMO (0x1 << 30) /* RW */ |
| 219 | |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 220 | #define MSDC_PATCH_BIT2_CFGRESP (0x1 << 15) /* RW */ |
| 221 | #define MSDC_PATCH_BIT2_CFGCRCSTS (0x1 << 28) /* RW */ |
| 222 | #define MSDC_PB2_RESPWAIT (0x3 << 2) /* RW */ |
| 223 | #define MSDC_PB2_RESPSTSENSEL (0x7 << 16) /* RW */ |
| 224 | #define MSDC_PB2_CRCSTSENSEL (0x7 << 29) /* RW */ |
| 225 | |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 226 | #define MSDC_PAD_TUNE_DATWRDLY (0x1f << 0) /* RW */ |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 227 | #define MSDC_PAD_TUNE_DATRRDLY (0x1f << 8) /* RW */ |
| 228 | #define MSDC_PAD_TUNE_CMDRDLY (0x1f << 16) /* RW */ |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 229 | #define MSDC_PAD_TUNE_CMDRRDLY (0x1f << 22) /* RW */ |
| 230 | #define MSDC_PAD_TUNE_CLKTDLY (0x1f << 27) /* RW */ |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 231 | #define MSDC_PAD_TUNE_RXDLYSEL (0x1 << 15) /* RW */ |
| 232 | #define MSDC_PAD_TUNE_RD_SEL (0x1 << 13) /* RW */ |
| 233 | #define MSDC_PAD_TUNE_CMD_SEL (0x1 << 21) /* RW */ |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 234 | |
| 235 | #define PAD_DS_TUNE_DLY1 (0x1f << 2) /* RW */ |
| 236 | #define PAD_DS_TUNE_DLY2 (0x1f << 7) /* RW */ |
| 237 | #define PAD_DS_TUNE_DLY3 (0x1f << 12) /* RW */ |
| 238 | |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 239 | #define PAD_CMD_TUNE_RX_DLY3 (0x1f << 1) /* RW */ |
| 240 | |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 241 | #define EMMC50_CFG_PADCMD_LATCHCK (0x1 << 0) /* RW */ |
| 242 | #define EMMC50_CFG_CRCSTS_EDGE (0x1 << 3) /* RW */ |
| 243 | #define EMMC50_CFG_CFCSTS_SEL (0x1 << 4) /* RW */ |
| 244 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 245 | #define REQ_CMD_EIO (0x1 << 0) |
| 246 | #define REQ_CMD_TMO (0x1 << 1) |
| 247 | #define REQ_DAT_ERR (0x1 << 2) |
| 248 | #define REQ_STOP_EIO (0x1 << 3) |
| 249 | #define REQ_STOP_TMO (0x1 << 4) |
| 250 | #define REQ_CMD_BUSY (0x1 << 5) |
| 251 | |
| 252 | #define MSDC_PREPARE_FLAG (0x1 << 0) |
| 253 | #define MSDC_ASYNC_FLAG (0x1 << 1) |
| 254 | #define MSDC_MMAP_FLAG (0x1 << 2) |
| 255 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 256 | #define MTK_MMC_AUTOSUSPEND_DELAY 50 |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 257 | #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */ |
| 258 | #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */ |
| 259 | |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 260 | #define PAD_DELAY_MAX 32 /* PAD delay cells */ |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 261 | /*--------------------------------------------------------------------------*/ |
| 262 | /* Descriptor Structure */ |
| 263 | /*--------------------------------------------------------------------------*/ |
| 264 | struct mt_gpdma_desc { |
| 265 | u32 gpd_info; |
| 266 | #define GPDMA_DESC_HWO (0x1 << 0) |
| 267 | #define GPDMA_DESC_BDP (0x1 << 1) |
| 268 | #define GPDMA_DESC_CHECKSUM (0xff << 8) /* bit8 ~ bit15 */ |
| 269 | #define GPDMA_DESC_INT (0x1 << 16) |
| 270 | u32 next; |
| 271 | u32 ptr; |
| 272 | u32 gpd_data_len; |
| 273 | #define GPDMA_DESC_BUFLEN (0xffff) /* bit0 ~ bit15 */ |
| 274 | #define GPDMA_DESC_EXTLEN (0xff << 16) /* bit16 ~ bit23 */ |
| 275 | u32 arg; |
| 276 | u32 blknum; |
| 277 | u32 cmd; |
| 278 | }; |
| 279 | |
| 280 | struct mt_bdma_desc { |
| 281 | u32 bd_info; |
| 282 | #define BDMA_DESC_EOL (0x1 << 0) |
| 283 | #define BDMA_DESC_CHECKSUM (0xff << 8) /* bit8 ~ bit15 */ |
| 284 | #define BDMA_DESC_BLKPAD (0x1 << 17) |
| 285 | #define BDMA_DESC_DWPAD (0x1 << 18) |
| 286 | u32 next; |
| 287 | u32 ptr; |
| 288 | u32 bd_data_len; |
| 289 | #define BDMA_DESC_BUFLEN (0xffff) /* bit0 ~ bit15 */ |
| 290 | }; |
| 291 | |
| 292 | struct msdc_dma { |
| 293 | struct scatterlist *sg; /* I/O scatter list */ |
| 294 | struct mt_gpdma_desc *gpd; /* pointer to gpd array */ |
| 295 | struct mt_bdma_desc *bd; /* pointer to bd array */ |
| 296 | dma_addr_t gpd_addr; /* the physical address of gpd array */ |
| 297 | dma_addr_t bd_addr; /* the physical address of bd array */ |
| 298 | }; |
| 299 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 300 | struct msdc_save_para { |
| 301 | u32 msdc_cfg; |
| 302 | u32 iocon; |
| 303 | u32 sdc_cfg; |
| 304 | u32 pad_tune; |
| 305 | u32 patch_bit0; |
| 306 | u32 patch_bit1; |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 307 | u32 patch_bit2; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 308 | u32 pad_ds_tune; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 309 | u32 pad_cmd_tune; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 310 | u32 emmc50_cfg0; |
| 311 | }; |
| 312 | |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 313 | struct mtk_mmc_compatible { |
| 314 | u8 clk_div_bits; |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 315 | bool hs400_tune; /* only used for MT8173 */ |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 316 | u32 pad_tune_reg; |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 317 | bool async_fifo; |
| 318 | bool data_tune; |
Chaotian Jing | acde28c | 2017-10-16 09:46:34 +0800 | [diff] [blame^] | 319 | bool busy_check; |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 320 | }; |
| 321 | |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 322 | struct msdc_tune_para { |
| 323 | u32 iocon; |
| 324 | u32 pad_tune; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 325 | u32 pad_cmd_tune; |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 326 | }; |
| 327 | |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 328 | struct msdc_delay_phase { |
| 329 | u8 maxlen; |
| 330 | u8 start; |
| 331 | u8 final_phase; |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 332 | }; |
| 333 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 334 | struct msdc_host { |
| 335 | struct device *dev; |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 336 | const struct mtk_mmc_compatible *dev_comp; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 337 | struct mmc_host *mmc; /* mmc structure */ |
| 338 | int cmd_rsp; |
| 339 | |
| 340 | spinlock_t lock; |
| 341 | struct mmc_request *mrq; |
| 342 | struct mmc_command *cmd; |
| 343 | struct mmc_data *data; |
| 344 | int error; |
| 345 | |
| 346 | void __iomem *base; /* host base address */ |
| 347 | |
| 348 | struct msdc_dma dma; /* dma channel */ |
| 349 | u64 dma_mask; |
| 350 | |
| 351 | u32 timeout_ns; /* data timeout ns */ |
| 352 | u32 timeout_clks; /* data timeout clks */ |
| 353 | |
| 354 | struct pinctrl *pinctrl; |
| 355 | struct pinctrl_state *pins_default; |
| 356 | struct pinctrl_state *pins_uhs; |
| 357 | struct delayed_work req_timeout; |
| 358 | int irq; /* host interrupt */ |
| 359 | |
| 360 | struct clk *src_clk; /* msdc source clock */ |
| 361 | struct clk *h_clk; /* msdc h_clk */ |
| 362 | u32 mclk; /* mmc subsystem clock frequency */ |
| 363 | u32 src_clk_freq; /* source clock frequency */ |
| 364 | u32 sclk; /* SD/MS bus clock frequency */ |
Chaotian Jing | 6e62294 | 2015-10-27 14:24:24 +0800 | [diff] [blame] | 365 | unsigned char timing; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 366 | bool vqmmc_enabled; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 367 | u32 hs400_ds_delay; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 368 | u32 hs200_cmd_int_delay; /* cmd internal delay for HS200/SDR104 */ |
| 369 | u32 hs400_cmd_int_delay; /* cmd internal delay for HS400 */ |
| 370 | bool hs400_cmd_resp_sel_rising; |
| 371 | /* cmd response sample selection for HS400 */ |
Chaotian Jing | 5462ff3 | 2016-06-30 10:00:58 +0800 | [diff] [blame] | 372 | bool hs400_mode; /* current eMMC will run at hs400 mode */ |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 373 | struct msdc_save_para save_para; /* used when gate HCLK */ |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 374 | struct msdc_tune_para def_tune_para; /* default tune setting */ |
| 375 | struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */ |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 376 | }; |
| 377 | |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 378 | static const struct mtk_mmc_compatible mt8135_compat = { |
| 379 | .clk_div_bits = 8, |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 380 | .hs400_tune = false, |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 381 | .pad_tune_reg = MSDC_PAD_TUNE, |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 382 | .async_fifo = false, |
| 383 | .data_tune = false, |
Chaotian Jing | acde28c | 2017-10-16 09:46:34 +0800 | [diff] [blame^] | 384 | .busy_check = false, |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | static const struct mtk_mmc_compatible mt8173_compat = { |
| 388 | .clk_div_bits = 8, |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 389 | .hs400_tune = true, |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 390 | .pad_tune_reg = MSDC_PAD_TUNE, |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 391 | .async_fifo = false, |
| 392 | .data_tune = false, |
Chaotian Jing | acde28c | 2017-10-16 09:46:34 +0800 | [diff] [blame^] | 393 | .busy_check = false, |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 394 | }; |
| 395 | |
| 396 | static const struct mtk_mmc_compatible mt2701_compat = { |
| 397 | .clk_div_bits = 12, |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 398 | .hs400_tune = false, |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 399 | .pad_tune_reg = MSDC_PAD_TUNE0, |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 400 | .async_fifo = true, |
| 401 | .data_tune = true, |
Chaotian Jing | acde28c | 2017-10-16 09:46:34 +0800 | [diff] [blame^] | 402 | .busy_check = false, |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 403 | }; |
| 404 | |
| 405 | static const struct mtk_mmc_compatible mt2712_compat = { |
| 406 | .clk_div_bits = 12, |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 407 | .hs400_tune = false, |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 408 | .pad_tune_reg = MSDC_PAD_TUNE0, |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 409 | .async_fifo = true, |
| 410 | .data_tune = true, |
Chaotian Jing | acde28c | 2017-10-16 09:46:34 +0800 | [diff] [blame^] | 411 | .busy_check = true, |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 412 | }; |
| 413 | |
| 414 | static const struct of_device_id msdc_of_ids[] = { |
| 415 | { .compatible = "mediatek,mt8135-mmc", .data = &mt8135_compat}, |
| 416 | { .compatible = "mediatek,mt8173-mmc", .data = &mt8173_compat}, |
| 417 | { .compatible = "mediatek,mt2701-mmc", .data = &mt2701_compat}, |
| 418 | { .compatible = "mediatek,mt2712-mmc", .data = &mt2712_compat}, |
| 419 | {} |
| 420 | }; |
| 421 | MODULE_DEVICE_TABLE(of, msdc_of_ids); |
| 422 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 423 | static void sdr_set_bits(void __iomem *reg, u32 bs) |
| 424 | { |
| 425 | u32 val = readl(reg); |
| 426 | |
| 427 | val |= bs; |
| 428 | writel(val, reg); |
| 429 | } |
| 430 | |
| 431 | static void sdr_clr_bits(void __iomem *reg, u32 bs) |
| 432 | { |
| 433 | u32 val = readl(reg); |
| 434 | |
| 435 | val &= ~bs; |
| 436 | writel(val, reg); |
| 437 | } |
| 438 | |
| 439 | static void sdr_set_field(void __iomem *reg, u32 field, u32 val) |
| 440 | { |
| 441 | unsigned int tv = readl(reg); |
| 442 | |
| 443 | tv &= ~field; |
| 444 | tv |= ((val) << (ffs((unsigned int)field) - 1)); |
| 445 | writel(tv, reg); |
| 446 | } |
| 447 | |
| 448 | static void sdr_get_field(void __iomem *reg, u32 field, u32 *val) |
| 449 | { |
| 450 | unsigned int tv = readl(reg); |
| 451 | |
| 452 | *val = ((tv & field) >> (ffs((unsigned int)field) - 1)); |
| 453 | } |
| 454 | |
| 455 | static void msdc_reset_hw(struct msdc_host *host) |
| 456 | { |
| 457 | u32 val; |
| 458 | |
| 459 | sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST); |
| 460 | while (readl(host->base + MSDC_CFG) & MSDC_CFG_RST) |
| 461 | cpu_relax(); |
| 462 | |
| 463 | sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR); |
| 464 | while (readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_CLR) |
| 465 | cpu_relax(); |
| 466 | |
| 467 | val = readl(host->base + MSDC_INT); |
| 468 | writel(val, host->base + MSDC_INT); |
| 469 | } |
| 470 | |
| 471 | static void msdc_cmd_next(struct msdc_host *host, |
| 472 | struct mmc_request *mrq, struct mmc_command *cmd); |
| 473 | |
Chaotian Jing | 726a9aa | 2015-10-27 14:24:23 +0800 | [diff] [blame] | 474 | static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR | |
| 475 | MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY | |
| 476 | MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO; |
| 477 | static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 478 | MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR | |
| 479 | MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT; |
| 480 | |
| 481 | static u8 msdc_dma_calcs(u8 *buf, u32 len) |
| 482 | { |
| 483 | u32 i, sum = 0; |
| 484 | |
| 485 | for (i = 0; i < len; i++) |
| 486 | sum += buf[i]; |
| 487 | return 0xff - (u8) sum; |
| 488 | } |
| 489 | |
| 490 | static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma, |
| 491 | struct mmc_data *data) |
| 492 | { |
| 493 | unsigned int j, dma_len; |
| 494 | dma_addr_t dma_address; |
| 495 | u32 dma_ctrl; |
| 496 | struct scatterlist *sg; |
| 497 | struct mt_gpdma_desc *gpd; |
| 498 | struct mt_bdma_desc *bd; |
| 499 | |
| 500 | sg = data->sg; |
| 501 | |
| 502 | gpd = dma->gpd; |
| 503 | bd = dma->bd; |
| 504 | |
| 505 | /* modify gpd */ |
| 506 | gpd->gpd_info |= GPDMA_DESC_HWO; |
| 507 | gpd->gpd_info |= GPDMA_DESC_BDP; |
| 508 | /* need to clear first. use these bits to calc checksum */ |
| 509 | gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM; |
| 510 | gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8; |
| 511 | |
| 512 | /* modify bd */ |
| 513 | for_each_sg(data->sg, sg, data->sg_count, j) { |
| 514 | dma_address = sg_dma_address(sg); |
| 515 | dma_len = sg_dma_len(sg); |
| 516 | |
| 517 | /* init bd */ |
| 518 | bd[j].bd_info &= ~BDMA_DESC_BLKPAD; |
| 519 | bd[j].bd_info &= ~BDMA_DESC_DWPAD; |
| 520 | bd[j].ptr = (u32)dma_address; |
| 521 | bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN; |
| 522 | bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN); |
| 523 | |
| 524 | if (j == data->sg_count - 1) /* the last bd */ |
| 525 | bd[j].bd_info |= BDMA_DESC_EOL; |
| 526 | else |
| 527 | bd[j].bd_info &= ~BDMA_DESC_EOL; |
| 528 | |
| 529 | /* checksume need to clear first */ |
| 530 | bd[j].bd_info &= ~BDMA_DESC_CHECKSUM; |
| 531 | bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8; |
| 532 | } |
| 533 | |
| 534 | sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1); |
| 535 | dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL); |
| 536 | dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE); |
| 537 | dma_ctrl |= (MSDC_BURST_64B << 12 | 1 << 8); |
| 538 | writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL); |
| 539 | writel((u32)dma->gpd_addr, host->base + MSDC_DMA_SA); |
| 540 | } |
| 541 | |
| 542 | static void msdc_prepare_data(struct msdc_host *host, struct mmc_request *mrq) |
| 543 | { |
| 544 | struct mmc_data *data = mrq->data; |
| 545 | |
| 546 | if (!(data->host_cookie & MSDC_PREPARE_FLAG)) { |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 547 | data->host_cookie |= MSDC_PREPARE_FLAG; |
| 548 | data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len, |
Heiner Kallweit | feeef09 | 2017-03-26 20:45:56 +0200 | [diff] [blame] | 549 | mmc_get_dma_dir(data)); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| 553 | static void msdc_unprepare_data(struct msdc_host *host, struct mmc_request *mrq) |
| 554 | { |
| 555 | struct mmc_data *data = mrq->data; |
| 556 | |
| 557 | if (data->host_cookie & MSDC_ASYNC_FLAG) |
| 558 | return; |
| 559 | |
| 560 | if (data->host_cookie & MSDC_PREPARE_FLAG) { |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 561 | dma_unmap_sg(host->dev, data->sg, data->sg_len, |
Heiner Kallweit | feeef09 | 2017-03-26 20:45:56 +0200 | [diff] [blame] | 562 | mmc_get_dma_dir(data)); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 563 | data->host_cookie &= ~MSDC_PREPARE_FLAG; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | /* clock control primitives */ |
| 568 | static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks) |
| 569 | { |
| 570 | u32 timeout, clk_ns; |
| 571 | u32 mode = 0; |
| 572 | |
| 573 | host->timeout_ns = ns; |
| 574 | host->timeout_clks = clks; |
| 575 | if (host->sclk == 0) { |
| 576 | timeout = 0; |
| 577 | } else { |
| 578 | clk_ns = 1000000000UL / host->sclk; |
| 579 | timeout = (ns + clk_ns - 1) / clk_ns + clks; |
| 580 | /* in 1048576 sclk cycle unit */ |
| 581 | timeout = (timeout + (0x1 << 20) - 1) >> 20; |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 582 | if (host->dev_comp->clk_div_bits == 8) |
| 583 | sdr_get_field(host->base + MSDC_CFG, |
| 584 | MSDC_CFG_CKMOD, &mode); |
| 585 | else |
| 586 | sdr_get_field(host->base + MSDC_CFG, |
| 587 | MSDC_CFG_CKMOD_EXTRA, &mode); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 588 | /*DDR mode will double the clk cycles for data timeout */ |
| 589 | timeout = mode >= 2 ? timeout * 2 : timeout; |
| 590 | timeout = timeout > 1 ? timeout - 1 : 0; |
| 591 | timeout = timeout > 255 ? 255 : timeout; |
| 592 | } |
| 593 | sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, timeout); |
| 594 | } |
| 595 | |
| 596 | static void msdc_gate_clock(struct msdc_host *host) |
| 597 | { |
| 598 | clk_disable_unprepare(host->src_clk); |
| 599 | clk_disable_unprepare(host->h_clk); |
| 600 | } |
| 601 | |
| 602 | static void msdc_ungate_clock(struct msdc_host *host) |
| 603 | { |
| 604 | clk_prepare_enable(host->h_clk); |
| 605 | clk_prepare_enable(host->src_clk); |
| 606 | while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB)) |
| 607 | cpu_relax(); |
| 608 | } |
| 609 | |
Chaotian Jing | 6e62294 | 2015-10-27 14:24:24 +0800 | [diff] [blame] | 610 | static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz) |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 611 | { |
| 612 | u32 mode; |
| 613 | u32 flags; |
| 614 | u32 div; |
| 615 | u32 sclk; |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 616 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 617 | |
| 618 | if (!hz) { |
| 619 | dev_dbg(host->dev, "set mclk to 0\n"); |
| 620 | host->mclk = 0; |
| 621 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); |
| 622 | return; |
| 623 | } |
| 624 | |
| 625 | flags = readl(host->base + MSDC_INTEN); |
| 626 | sdr_clr_bits(host->base + MSDC_INTEN, flags); |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 627 | if (host->dev_comp->clk_div_bits == 8) |
| 628 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE); |
| 629 | else |
| 630 | sdr_clr_bits(host->base + MSDC_CFG, |
| 631 | MSDC_CFG_HS400_CK_MODE_EXTRA); |
Chaotian Jing | 6e62294 | 2015-10-27 14:24:24 +0800 | [diff] [blame] | 632 | if (timing == MMC_TIMING_UHS_DDR50 || |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 633 | timing == MMC_TIMING_MMC_DDR52 || |
| 634 | timing == MMC_TIMING_MMC_HS400) { |
| 635 | if (timing == MMC_TIMING_MMC_HS400) |
| 636 | mode = 0x3; |
| 637 | else |
| 638 | mode = 0x2; /* ddr mode and use divisor */ |
| 639 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 640 | if (hz >= (host->src_clk_freq >> 2)) { |
| 641 | div = 0; /* mean div = 1/4 */ |
| 642 | sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */ |
| 643 | } else { |
| 644 | div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); |
| 645 | sclk = (host->src_clk_freq >> 2) / div; |
| 646 | div = (div >> 1); |
| 647 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 648 | |
| 649 | if (timing == MMC_TIMING_MMC_HS400 && |
| 650 | hz >= (host->src_clk_freq >> 1)) { |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 651 | if (host->dev_comp->clk_div_bits == 8) |
| 652 | sdr_set_bits(host->base + MSDC_CFG, |
| 653 | MSDC_CFG_HS400_CK_MODE); |
| 654 | else |
| 655 | sdr_set_bits(host->base + MSDC_CFG, |
| 656 | MSDC_CFG_HS400_CK_MODE_EXTRA); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 657 | sclk = host->src_clk_freq >> 1; |
| 658 | div = 0; /* div is ignore when bit18 is set */ |
| 659 | } |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 660 | } else if (hz >= host->src_clk_freq) { |
| 661 | mode = 0x1; /* no divisor */ |
| 662 | div = 0; |
| 663 | sclk = host->src_clk_freq; |
| 664 | } else { |
| 665 | mode = 0x0; /* use divisor */ |
| 666 | if (hz >= (host->src_clk_freq >> 1)) { |
| 667 | div = 0; /* mean div = 1/2 */ |
| 668 | sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */ |
| 669 | } else { |
| 670 | div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); |
| 671 | sclk = (host->src_clk_freq >> 2) / div; |
| 672 | } |
| 673 | } |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 674 | if (host->dev_comp->clk_div_bits == 8) |
| 675 | sdr_set_field(host->base + MSDC_CFG, |
| 676 | MSDC_CFG_CKMOD | MSDC_CFG_CKDIV, |
| 677 | (mode << 8) | div); |
| 678 | else |
| 679 | sdr_set_field(host->base + MSDC_CFG, |
| 680 | MSDC_CFG_CKMOD_EXTRA | MSDC_CFG_CKDIV_EXTRA, |
| 681 | (mode << 12) | div); |
| 682 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 683 | sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); |
| 684 | while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB)) |
| 685 | cpu_relax(); |
| 686 | host->sclk = sclk; |
| 687 | host->mclk = hz; |
Chaotian Jing | 6e62294 | 2015-10-27 14:24:24 +0800 | [diff] [blame] | 688 | host->timing = timing; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 689 | /* need because clk changed. */ |
| 690 | msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); |
| 691 | sdr_set_bits(host->base + MSDC_INTEN, flags); |
| 692 | |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 693 | /* |
| 694 | * mmc_select_hs400() will drop to 50Mhz and High speed mode, |
| 695 | * tune result of hs200/200Mhz is not suitable for 50Mhz |
| 696 | */ |
| 697 | if (host->sclk <= 52000000) { |
| 698 | writel(host->def_tune_para.iocon, host->base + MSDC_IOCON); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 699 | writel(host->def_tune_para.pad_tune, host->base + tune_reg); |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 700 | } else { |
| 701 | writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 702 | writel(host->saved_tune_para.pad_tune, host->base + tune_reg); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 703 | writel(host->saved_tune_para.pad_cmd_tune, |
| 704 | host->base + PAD_CMD_TUNE); |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 705 | } |
| 706 | |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 707 | if (timing == MMC_TIMING_MMC_HS400 && |
| 708 | host->dev_comp->hs400_tune) |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 709 | sdr_set_field(host->base + PAD_CMD_TUNE, |
| 710 | MSDC_PAD_TUNE_CMDRRDLY, |
| 711 | host->hs400_cmd_int_delay); |
Chaotian Jing | 6e62294 | 2015-10-27 14:24:24 +0800 | [diff] [blame] | 712 | dev_dbg(host->dev, "sclk: %d, timing: %d\n", host->sclk, timing); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | static inline u32 msdc_cmd_find_resp(struct msdc_host *host, |
| 716 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 717 | { |
| 718 | u32 resp; |
| 719 | |
| 720 | switch (mmc_resp_type(cmd)) { |
| 721 | /* Actually, R1, R5, R6, R7 are the same */ |
| 722 | case MMC_RSP_R1: |
| 723 | resp = 0x1; |
| 724 | break; |
| 725 | case MMC_RSP_R1B: |
| 726 | resp = 0x7; |
| 727 | break; |
| 728 | case MMC_RSP_R2: |
| 729 | resp = 0x2; |
| 730 | break; |
| 731 | case MMC_RSP_R3: |
| 732 | resp = 0x3; |
| 733 | break; |
| 734 | case MMC_RSP_NONE: |
| 735 | default: |
| 736 | resp = 0x0; |
| 737 | break; |
| 738 | } |
| 739 | |
| 740 | return resp; |
| 741 | } |
| 742 | |
| 743 | static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host, |
| 744 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 745 | { |
| 746 | /* rawcmd : |
| 747 | * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 | |
| 748 | * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode |
| 749 | */ |
| 750 | u32 opcode = cmd->opcode; |
| 751 | u32 resp = msdc_cmd_find_resp(host, mrq, cmd); |
| 752 | u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7); |
| 753 | |
| 754 | host->cmd_rsp = resp; |
| 755 | |
| 756 | if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) || |
| 757 | opcode == MMC_STOP_TRANSMISSION) |
| 758 | rawcmd |= (0x1 << 14); |
| 759 | else if (opcode == SD_SWITCH_VOLTAGE) |
| 760 | rawcmd |= (0x1 << 30); |
| 761 | else if (opcode == SD_APP_SEND_SCR || |
| 762 | opcode == SD_APP_SEND_NUM_WR_BLKS || |
| 763 | (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || |
| 764 | (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || |
| 765 | (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC)) |
| 766 | rawcmd |= (0x1 << 11); |
| 767 | |
| 768 | if (cmd->data) { |
| 769 | struct mmc_data *data = cmd->data; |
| 770 | |
| 771 | if (mmc_op_multi(opcode)) { |
| 772 | if (mmc_card_mmc(host->mmc->card) && mrq->sbc && |
| 773 | !(mrq->sbc->arg & 0xFFFF0000)) |
| 774 | rawcmd |= 0x2 << 28; /* AutoCMD23 */ |
| 775 | } |
| 776 | |
| 777 | rawcmd |= ((data->blksz & 0xFFF) << 16); |
| 778 | if (data->flags & MMC_DATA_WRITE) |
| 779 | rawcmd |= (0x1 << 13); |
| 780 | if (data->blocks > 1) |
| 781 | rawcmd |= (0x2 << 11); |
| 782 | else |
| 783 | rawcmd |= (0x1 << 11); |
| 784 | /* Always use dma mode */ |
| 785 | sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO); |
| 786 | |
| 787 | if (host->timeout_ns != data->timeout_ns || |
| 788 | host->timeout_clks != data->timeout_clks) |
| 789 | msdc_set_timeout(host, data->timeout_ns, |
| 790 | data->timeout_clks); |
| 791 | |
| 792 | writel(data->blocks, host->base + SDC_BLK_NUM); |
| 793 | } |
| 794 | return rawcmd; |
| 795 | } |
| 796 | |
| 797 | static void msdc_start_data(struct msdc_host *host, struct mmc_request *mrq, |
| 798 | struct mmc_command *cmd, struct mmc_data *data) |
| 799 | { |
| 800 | bool read; |
| 801 | |
| 802 | WARN_ON(host->data); |
| 803 | host->data = data; |
| 804 | read = data->flags & MMC_DATA_READ; |
| 805 | |
| 806 | mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); |
| 807 | msdc_dma_setup(host, &host->dma, data); |
| 808 | sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask); |
| 809 | sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1); |
| 810 | dev_dbg(host->dev, "DMA start\n"); |
| 811 | dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n", |
| 812 | __func__, cmd->opcode, data->blocks, read); |
| 813 | } |
| 814 | |
| 815 | static int msdc_auto_cmd_done(struct msdc_host *host, int events, |
| 816 | struct mmc_command *cmd) |
| 817 | { |
| 818 | u32 *rsp = cmd->resp; |
| 819 | |
| 820 | rsp[0] = readl(host->base + SDC_ACMD_RESP); |
| 821 | |
| 822 | if (events & MSDC_INT_ACMDRDY) { |
| 823 | cmd->error = 0; |
| 824 | } else { |
| 825 | msdc_reset_hw(host); |
| 826 | if (events & MSDC_INT_ACMDCRCERR) { |
| 827 | cmd->error = -EILSEQ; |
| 828 | host->error |= REQ_STOP_EIO; |
| 829 | } else if (events & MSDC_INT_ACMDTMO) { |
| 830 | cmd->error = -ETIMEDOUT; |
| 831 | host->error |= REQ_STOP_TMO; |
| 832 | } |
| 833 | dev_err(host->dev, |
| 834 | "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n", |
| 835 | __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error); |
| 836 | } |
| 837 | return cmd->error; |
| 838 | } |
| 839 | |
| 840 | static void msdc_track_cmd_data(struct msdc_host *host, |
| 841 | struct mmc_command *cmd, struct mmc_data *data) |
| 842 | { |
| 843 | if (host->error) |
| 844 | dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n", |
| 845 | __func__, cmd->opcode, cmd->arg, host->error); |
| 846 | } |
| 847 | |
| 848 | static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq) |
| 849 | { |
| 850 | unsigned long flags; |
| 851 | bool ret; |
| 852 | |
| 853 | ret = cancel_delayed_work(&host->req_timeout); |
| 854 | if (!ret) { |
| 855 | /* delay work already running */ |
| 856 | return; |
| 857 | } |
| 858 | spin_lock_irqsave(&host->lock, flags); |
| 859 | host->mrq = NULL; |
| 860 | spin_unlock_irqrestore(&host->lock, flags); |
| 861 | |
| 862 | msdc_track_cmd_data(host, mrq->cmd, mrq->data); |
| 863 | if (mrq->data) |
| 864 | msdc_unprepare_data(host, mrq); |
| 865 | mmc_request_done(host->mmc, mrq); |
| 866 | } |
| 867 | |
| 868 | /* returns true if command is fully handled; returns false otherwise */ |
| 869 | static bool msdc_cmd_done(struct msdc_host *host, int events, |
| 870 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 871 | { |
| 872 | bool done = false; |
| 873 | bool sbc_error; |
| 874 | unsigned long flags; |
| 875 | u32 *rsp = cmd->resp; |
| 876 | |
| 877 | if (mrq->sbc && cmd == mrq->cmd && |
| 878 | (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR |
| 879 | | MSDC_INT_ACMDTMO))) |
| 880 | msdc_auto_cmd_done(host, events, mrq->sbc); |
| 881 | |
| 882 | sbc_error = mrq->sbc && mrq->sbc->error; |
| 883 | |
| 884 | if (!sbc_error && !(events & (MSDC_INT_CMDRDY |
| 885 | | MSDC_INT_RSPCRCERR |
| 886 | | MSDC_INT_CMDTMO))) |
| 887 | return done; |
| 888 | |
| 889 | spin_lock_irqsave(&host->lock, flags); |
| 890 | done = !host->cmd; |
| 891 | host->cmd = NULL; |
| 892 | spin_unlock_irqrestore(&host->lock, flags); |
| 893 | |
| 894 | if (done) |
| 895 | return true; |
| 896 | |
Chaotian Jing | 726a9aa | 2015-10-27 14:24:23 +0800 | [diff] [blame] | 897 | sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 898 | |
| 899 | if (cmd->flags & MMC_RSP_PRESENT) { |
| 900 | if (cmd->flags & MMC_RSP_136) { |
| 901 | rsp[0] = readl(host->base + SDC_RESP3); |
| 902 | rsp[1] = readl(host->base + SDC_RESP2); |
| 903 | rsp[2] = readl(host->base + SDC_RESP1); |
| 904 | rsp[3] = readl(host->base + SDC_RESP0); |
| 905 | } else { |
| 906 | rsp[0] = readl(host->base + SDC_RESP0); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | if (!sbc_error && !(events & MSDC_INT_CMDRDY)) { |
Chaotian Jing | ddc7138 | 2016-06-30 10:01:00 +0800 | [diff] [blame] | 911 | if (cmd->opcode != MMC_SEND_TUNING_BLOCK && |
| 912 | cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) |
| 913 | /* |
| 914 | * should not clear fifo/interrupt as the tune data |
| 915 | * may have alreay come. |
| 916 | */ |
| 917 | msdc_reset_hw(host); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 918 | if (events & MSDC_INT_RSPCRCERR) { |
| 919 | cmd->error = -EILSEQ; |
| 920 | host->error |= REQ_CMD_EIO; |
| 921 | } else if (events & MSDC_INT_CMDTMO) { |
| 922 | cmd->error = -ETIMEDOUT; |
| 923 | host->error |= REQ_CMD_TMO; |
| 924 | } |
| 925 | } |
| 926 | if (cmd->error) |
| 927 | dev_dbg(host->dev, |
| 928 | "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n", |
| 929 | __func__, cmd->opcode, cmd->arg, rsp[0], |
| 930 | cmd->error); |
| 931 | |
| 932 | msdc_cmd_next(host, mrq, cmd); |
| 933 | return true; |
| 934 | } |
| 935 | |
| 936 | /* It is the core layer's responsibility to ensure card status |
| 937 | * is correct before issue a request. but host design do below |
| 938 | * checks recommended. |
| 939 | */ |
| 940 | static inline bool msdc_cmd_is_ready(struct msdc_host *host, |
| 941 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 942 | { |
| 943 | /* The max busy time we can endure is 20ms */ |
| 944 | unsigned long tmo = jiffies + msecs_to_jiffies(20); |
| 945 | |
| 946 | while ((readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) && |
| 947 | time_before(jiffies, tmo)) |
| 948 | cpu_relax(); |
| 949 | if (readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) { |
| 950 | dev_err(host->dev, "CMD bus busy detected\n"); |
| 951 | host->error |= REQ_CMD_BUSY; |
| 952 | msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); |
| 953 | return false; |
| 954 | } |
| 955 | |
| 956 | if (mmc_resp_type(cmd) == MMC_RSP_R1B || cmd->data) { |
| 957 | tmo = jiffies + msecs_to_jiffies(20); |
| 958 | /* R1B or with data, should check SDCBUSY */ |
| 959 | while ((readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) && |
| 960 | time_before(jiffies, tmo)) |
| 961 | cpu_relax(); |
| 962 | if (readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) { |
| 963 | dev_err(host->dev, "Controller busy detected\n"); |
| 964 | host->error |= REQ_CMD_BUSY; |
| 965 | msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); |
| 966 | return false; |
| 967 | } |
| 968 | } |
| 969 | return true; |
| 970 | } |
| 971 | |
| 972 | static void msdc_start_command(struct msdc_host *host, |
| 973 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 974 | { |
| 975 | u32 rawcmd; |
| 976 | |
| 977 | WARN_ON(host->cmd); |
| 978 | host->cmd = cmd; |
| 979 | |
| 980 | if (!msdc_cmd_is_ready(host, mrq, cmd)) |
| 981 | return; |
| 982 | |
| 983 | if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 || |
| 984 | readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) { |
| 985 | dev_err(host->dev, "TX/RX FIFO non-empty before start of IO. Reset\n"); |
| 986 | msdc_reset_hw(host); |
| 987 | } |
| 988 | |
| 989 | cmd->error = 0; |
| 990 | rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd); |
| 991 | mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); |
| 992 | |
Chaotian Jing | 726a9aa | 2015-10-27 14:24:23 +0800 | [diff] [blame] | 993 | sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 994 | writel(cmd->arg, host->base + SDC_ARG); |
| 995 | writel(rawcmd, host->base + SDC_CMD); |
| 996 | } |
| 997 | |
| 998 | static void msdc_cmd_next(struct msdc_host *host, |
| 999 | struct mmc_request *mrq, struct mmc_command *cmd) |
| 1000 | { |
Chaotian Jing | ddc7138 | 2016-06-30 10:01:00 +0800 | [diff] [blame] | 1001 | if ((cmd->error && |
| 1002 | !(cmd->error == -EILSEQ && |
| 1003 | (cmd->opcode == MMC_SEND_TUNING_BLOCK || |
| 1004 | cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) || |
| 1005 | (mrq->sbc && mrq->sbc->error)) |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1006 | msdc_request_done(host, mrq); |
| 1007 | else if (cmd == mrq->sbc) |
| 1008 | msdc_start_command(host, mrq, mrq->cmd); |
| 1009 | else if (!cmd->data) |
| 1010 | msdc_request_done(host, mrq); |
| 1011 | else |
| 1012 | msdc_start_data(host, mrq, cmd, cmd->data); |
| 1013 | } |
| 1014 | |
| 1015 | static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq) |
| 1016 | { |
| 1017 | struct msdc_host *host = mmc_priv(mmc); |
| 1018 | |
| 1019 | host->error = 0; |
| 1020 | WARN_ON(host->mrq); |
| 1021 | host->mrq = mrq; |
| 1022 | |
| 1023 | if (mrq->data) |
| 1024 | msdc_prepare_data(host, mrq); |
| 1025 | |
| 1026 | /* if SBC is required, we have HW option and SW option. |
| 1027 | * if HW option is enabled, and SBC does not have "special" flags, |
| 1028 | * use HW option, otherwise use SW option |
| 1029 | */ |
| 1030 | if (mrq->sbc && (!mmc_card_mmc(mmc->card) || |
| 1031 | (mrq->sbc->arg & 0xFFFF0000))) |
| 1032 | msdc_start_command(host, mrq, mrq->sbc); |
| 1033 | else |
| 1034 | msdc_start_command(host, mrq, mrq->cmd); |
| 1035 | } |
| 1036 | |
Linus Walleij | d3c6aac | 2016-11-23 11:02:24 +0100 | [diff] [blame] | 1037 | static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1038 | { |
| 1039 | struct msdc_host *host = mmc_priv(mmc); |
| 1040 | struct mmc_data *data = mrq->data; |
| 1041 | |
| 1042 | if (!data) |
| 1043 | return; |
| 1044 | |
| 1045 | msdc_prepare_data(host, mrq); |
| 1046 | data->host_cookie |= MSDC_ASYNC_FLAG; |
| 1047 | } |
| 1048 | |
| 1049 | static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, |
| 1050 | int err) |
| 1051 | { |
| 1052 | struct msdc_host *host = mmc_priv(mmc); |
| 1053 | struct mmc_data *data; |
| 1054 | |
| 1055 | data = mrq->data; |
| 1056 | if (!data) |
| 1057 | return; |
| 1058 | if (data->host_cookie) { |
| 1059 | data->host_cookie &= ~MSDC_ASYNC_FLAG; |
| 1060 | msdc_unprepare_data(host, mrq); |
| 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | static void msdc_data_xfer_next(struct msdc_host *host, |
| 1065 | struct mmc_request *mrq, struct mmc_data *data) |
| 1066 | { |
| 1067 | if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error && |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1068 | !mrq->sbc) |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1069 | msdc_start_command(host, mrq, mrq->stop); |
| 1070 | else |
| 1071 | msdc_request_done(host, mrq); |
| 1072 | } |
| 1073 | |
| 1074 | static bool msdc_data_xfer_done(struct msdc_host *host, u32 events, |
| 1075 | struct mmc_request *mrq, struct mmc_data *data) |
| 1076 | { |
| 1077 | struct mmc_command *stop = data->stop; |
| 1078 | unsigned long flags; |
| 1079 | bool done; |
| 1080 | unsigned int check_data = events & |
| 1081 | (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO |
| 1082 | | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR |
| 1083 | | MSDC_INT_DMA_PROTECT); |
| 1084 | |
| 1085 | spin_lock_irqsave(&host->lock, flags); |
| 1086 | done = !host->data; |
| 1087 | if (check_data) |
| 1088 | host->data = NULL; |
| 1089 | spin_unlock_irqrestore(&host->lock, flags); |
| 1090 | |
| 1091 | if (done) |
| 1092 | return true; |
| 1093 | |
| 1094 | if (check_data || (stop && stop->error)) { |
| 1095 | dev_dbg(host->dev, "DMA status: 0x%8X\n", |
| 1096 | readl(host->base + MSDC_DMA_CFG)); |
| 1097 | sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, |
| 1098 | 1); |
| 1099 | while (readl(host->base + MSDC_DMA_CFG) & MSDC_DMA_CFG_STS) |
| 1100 | cpu_relax(); |
| 1101 | sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask); |
| 1102 | dev_dbg(host->dev, "DMA stop\n"); |
| 1103 | |
| 1104 | if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) { |
| 1105 | data->bytes_xfered = data->blocks * data->blksz; |
| 1106 | } else { |
Chaotian Jing | 2066fd2 | 2015-12-01 20:12:34 +0800 | [diff] [blame] | 1107 | dev_dbg(host->dev, "interrupt events: %x\n", events); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1108 | msdc_reset_hw(host); |
| 1109 | host->error |= REQ_DAT_ERR; |
| 1110 | data->bytes_xfered = 0; |
| 1111 | |
| 1112 | if (events & MSDC_INT_DATTMO) |
| 1113 | data->error = -ETIMEDOUT; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1114 | else if (events & MSDC_INT_DATCRCERR) |
| 1115 | data->error = -EILSEQ; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1116 | |
Chaotian Jing | 2066fd2 | 2015-12-01 20:12:34 +0800 | [diff] [blame] | 1117 | dev_dbg(host->dev, "%s: cmd=%d; blocks=%d", |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1118 | __func__, mrq->cmd->opcode, data->blocks); |
Chaotian Jing | 2066fd2 | 2015-12-01 20:12:34 +0800 | [diff] [blame] | 1119 | dev_dbg(host->dev, "data_error=%d xfer_size=%d\n", |
| 1120 | (int)data->error, data->bytes_xfered); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | msdc_data_xfer_next(host, mrq, data); |
| 1124 | done = true; |
| 1125 | } |
| 1126 | return done; |
| 1127 | } |
| 1128 | |
| 1129 | static void msdc_set_buswidth(struct msdc_host *host, u32 width) |
| 1130 | { |
| 1131 | u32 val = readl(host->base + SDC_CFG); |
| 1132 | |
| 1133 | val &= ~SDC_CFG_BUSWIDTH; |
| 1134 | |
| 1135 | switch (width) { |
| 1136 | default: |
| 1137 | case MMC_BUS_WIDTH_1: |
| 1138 | val |= (MSDC_BUS_1BITS << 16); |
| 1139 | break; |
| 1140 | case MMC_BUS_WIDTH_4: |
| 1141 | val |= (MSDC_BUS_4BITS << 16); |
| 1142 | break; |
| 1143 | case MMC_BUS_WIDTH_8: |
| 1144 | val |= (MSDC_BUS_8BITS << 16); |
| 1145 | break; |
| 1146 | } |
| 1147 | |
| 1148 | writel(val, host->base + SDC_CFG); |
| 1149 | dev_dbg(host->dev, "Bus Width = %d", width); |
| 1150 | } |
| 1151 | |
| 1152 | static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1153 | { |
| 1154 | struct msdc_host *host = mmc_priv(mmc); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1155 | int ret = 0; |
| 1156 | |
| 1157 | if (!IS_ERR(mmc->supply.vqmmc)) { |
Nicolas Boichat | fac49ce | 2016-03-03 18:19:45 +0800 | [diff] [blame] | 1158 | if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 && |
| 1159 | ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) { |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1160 | dev_err(host->dev, "Unsupported signal voltage!\n"); |
| 1161 | return -EINVAL; |
| 1162 | } |
| 1163 | |
Nicolas Boichat | fac49ce | 2016-03-03 18:19:45 +0800 | [diff] [blame] | 1164 | ret = mmc_regulator_set_vqmmc(mmc, ios); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1165 | if (ret) { |
Nicolas Boichat | fac49ce | 2016-03-03 18:19:45 +0800 | [diff] [blame] | 1166 | dev_dbg(host->dev, "Regulator set error %d (%d)\n", |
| 1167 | ret, ios->signal_voltage); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1168 | } else { |
| 1169 | /* Apply different pinctrl settings for different signal voltage */ |
| 1170 | if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) |
| 1171 | pinctrl_select_state(host->pinctrl, host->pins_uhs); |
| 1172 | else |
| 1173 | pinctrl_select_state(host->pinctrl, host->pins_default); |
| 1174 | } |
| 1175 | } |
| 1176 | return ret; |
| 1177 | } |
| 1178 | |
| 1179 | static int msdc_card_busy(struct mmc_host *mmc) |
| 1180 | { |
| 1181 | struct msdc_host *host = mmc_priv(mmc); |
| 1182 | u32 status = readl(host->base + MSDC_PS); |
| 1183 | |
yong mao | 3bc702e | 2017-01-03 16:49:57 +0800 | [diff] [blame] | 1184 | /* only check if data0 is low */ |
| 1185 | return !(status & BIT(16)); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | static void msdc_request_timeout(struct work_struct *work) |
| 1189 | { |
| 1190 | struct msdc_host *host = container_of(work, struct msdc_host, |
| 1191 | req_timeout.work); |
| 1192 | |
| 1193 | /* simulate HW timeout status */ |
| 1194 | dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__); |
| 1195 | if (host->mrq) { |
| 1196 | dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__, |
| 1197 | host->mrq, host->mrq->cmd->opcode); |
| 1198 | if (host->cmd) { |
| 1199 | dev_err(host->dev, "%s: aborting cmd=%d\n", |
| 1200 | __func__, host->cmd->opcode); |
| 1201 | msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq, |
| 1202 | host->cmd); |
| 1203 | } else if (host->data) { |
| 1204 | dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n", |
| 1205 | __func__, host->mrq->cmd->opcode, |
| 1206 | host->data->blocks); |
| 1207 | msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq, |
| 1208 | host->data); |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
| 1213 | static irqreturn_t msdc_irq(int irq, void *dev_id) |
| 1214 | { |
| 1215 | struct msdc_host *host = (struct msdc_host *) dev_id; |
| 1216 | |
| 1217 | while (true) { |
| 1218 | unsigned long flags; |
| 1219 | struct mmc_request *mrq; |
| 1220 | struct mmc_command *cmd; |
| 1221 | struct mmc_data *data; |
| 1222 | u32 events, event_mask; |
| 1223 | |
| 1224 | spin_lock_irqsave(&host->lock, flags); |
| 1225 | events = readl(host->base + MSDC_INT); |
| 1226 | event_mask = readl(host->base + MSDC_INTEN); |
| 1227 | /* clear interrupts */ |
| 1228 | writel(events & event_mask, host->base + MSDC_INT); |
| 1229 | |
| 1230 | mrq = host->mrq; |
| 1231 | cmd = host->cmd; |
| 1232 | data = host->data; |
| 1233 | spin_unlock_irqrestore(&host->lock, flags); |
| 1234 | |
| 1235 | if (!(events & event_mask)) |
| 1236 | break; |
| 1237 | |
| 1238 | if (!mrq) { |
| 1239 | dev_err(host->dev, |
| 1240 | "%s: MRQ=NULL; events=%08X; event_mask=%08X\n", |
| 1241 | __func__, events, event_mask); |
| 1242 | WARN_ON(1); |
| 1243 | break; |
| 1244 | } |
| 1245 | |
| 1246 | dev_dbg(host->dev, "%s: events=%08X\n", __func__, events); |
| 1247 | |
| 1248 | if (cmd) |
| 1249 | msdc_cmd_done(host, events, mrq, cmd); |
| 1250 | else if (data) |
| 1251 | msdc_data_xfer_done(host, events, mrq, data); |
| 1252 | } |
| 1253 | |
| 1254 | return IRQ_HANDLED; |
| 1255 | } |
| 1256 | |
| 1257 | static void msdc_init_hw(struct msdc_host *host) |
| 1258 | { |
| 1259 | u32 val; |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1260 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1261 | |
| 1262 | /* Configure to MMC/SD mode, clock free running */ |
| 1263 | sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN); |
| 1264 | |
| 1265 | /* Reset */ |
| 1266 | msdc_reset_hw(host); |
| 1267 | |
| 1268 | /* Disable card detection */ |
| 1269 | sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN); |
| 1270 | |
| 1271 | /* Disable and clear all interrupts */ |
| 1272 | writel(0, host->base + MSDC_INTEN); |
| 1273 | val = readl(host->base + MSDC_INT); |
| 1274 | writel(val, host->base + MSDC_INT); |
| 1275 | |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1276 | writel(0, host->base + tune_reg); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1277 | writel(0, host->base + MSDC_IOCON); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1278 | sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0); |
| 1279 | writel(0x403c0046, host->base + MSDC_PATCH_BIT); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1280 | sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1); |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1281 | writel(0xffff4089, host->base + MSDC_PATCH_BIT1); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1282 | sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL); |
Chaotian Jing | acde28c | 2017-10-16 09:46:34 +0800 | [diff] [blame^] | 1283 | if (host->dev_comp->busy_check) |
| 1284 | sdr_clr_bits(host->base + MSDC_PATCH_BIT1, (1 << 7)); |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1285 | if (host->dev_comp->async_fifo) { |
| 1286 | sdr_set_field(host->base + MSDC_PATCH_BIT2, |
| 1287 | MSDC_PB2_RESPWAIT, 3); |
| 1288 | sdr_set_field(host->base + MSDC_PATCH_BIT2, |
| 1289 | MSDC_PB2_RESPSTSENSEL, 2); |
| 1290 | sdr_set_field(host->base + MSDC_PATCH_BIT2, |
| 1291 | MSDC_PB2_CRCSTSENSEL, 2); |
| 1292 | /* use async fifo, then no need tune internal delay */ |
| 1293 | sdr_clr_bits(host->base + MSDC_PATCH_BIT2, |
| 1294 | MSDC_PATCH_BIT2_CFGRESP); |
| 1295 | sdr_set_bits(host->base + MSDC_PATCH_BIT2, |
| 1296 | MSDC_PATCH_BIT2_CFGCRCSTS); |
| 1297 | } |
| 1298 | |
| 1299 | if (host->dev_comp->data_tune) { |
| 1300 | sdr_set_bits(host->base + tune_reg, |
| 1301 | MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL); |
| 1302 | } else { |
| 1303 | /* choose clock tune */ |
| 1304 | sdr_set_bits(host->base + tune_reg, MSDC_PAD_TUNE_RXDLYSEL); |
| 1305 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1306 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1307 | /* Configure to enable SDIO mode. |
| 1308 | * it's must otherwise sdio cmd5 failed |
| 1309 | */ |
| 1310 | sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO); |
| 1311 | |
| 1312 | /* disable detect SDIO device interrupt function */ |
| 1313 | sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); |
| 1314 | |
| 1315 | /* Configure to default data timeout */ |
| 1316 | sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3); |
| 1317 | |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 1318 | host->def_tune_para.iocon = readl(host->base + MSDC_IOCON); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1319 | host->def_tune_para.pad_tune = readl(host->base + tune_reg); |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1320 | host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); |
| 1321 | host->saved_tune_para.pad_tune = readl(host->base + tune_reg); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1322 | dev_dbg(host->dev, "init hardware done!"); |
| 1323 | } |
| 1324 | |
| 1325 | static void msdc_deinit_hw(struct msdc_host *host) |
| 1326 | { |
| 1327 | u32 val; |
| 1328 | /* Disable and clear all interrupts */ |
| 1329 | writel(0, host->base + MSDC_INTEN); |
| 1330 | |
| 1331 | val = readl(host->base + MSDC_INT); |
| 1332 | writel(val, host->base + MSDC_INT); |
| 1333 | } |
| 1334 | |
| 1335 | /* init gpd and bd list in msdc_drv_probe */ |
| 1336 | static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma) |
| 1337 | { |
| 1338 | struct mt_gpdma_desc *gpd = dma->gpd; |
| 1339 | struct mt_bdma_desc *bd = dma->bd; |
| 1340 | int i; |
| 1341 | |
Chaotian Jing | 62b0d27 | 2015-10-27 14:24:25 +0800 | [diff] [blame] | 1342 | memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1343 | |
| 1344 | gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */ |
| 1345 | gpd->ptr = (u32)dma->bd_addr; /* physical address */ |
Chaotian Jing | 62b0d27 | 2015-10-27 14:24:25 +0800 | [diff] [blame] | 1346 | /* gpd->next is must set for desc DMA |
| 1347 | * That's why must alloc 2 gpd structure. |
| 1348 | */ |
| 1349 | gpd->next = (u32)dma->gpd_addr + sizeof(struct mt_gpdma_desc); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1350 | memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM); |
| 1351 | for (i = 0; i < (MAX_BD_NUM - 1); i++) |
| 1352 | bd[i].next = (u32)dma->bd_addr + sizeof(*bd) * (i + 1); |
| 1353 | } |
| 1354 | |
| 1355 | static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1356 | { |
| 1357 | struct msdc_host *host = mmc_priv(mmc); |
| 1358 | int ret; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1359 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1360 | msdc_set_buswidth(host, ios->bus_width); |
| 1361 | |
| 1362 | /* Suspend/Resume will do power off/on */ |
| 1363 | switch (ios->power_mode) { |
| 1364 | case MMC_POWER_UP: |
| 1365 | if (!IS_ERR(mmc->supply.vmmc)) { |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1366 | msdc_init_hw(host); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1367 | ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, |
| 1368 | ios->vdd); |
| 1369 | if (ret) { |
| 1370 | dev_err(host->dev, "Failed to set vmmc power!\n"); |
Ulf Hansson | 567979f | 2016-03-21 14:21:25 +0100 | [diff] [blame] | 1371 | return; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | break; |
| 1375 | case MMC_POWER_ON: |
| 1376 | if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { |
| 1377 | ret = regulator_enable(mmc->supply.vqmmc); |
| 1378 | if (ret) |
| 1379 | dev_err(host->dev, "Failed to set vqmmc power!\n"); |
| 1380 | else |
| 1381 | host->vqmmc_enabled = true; |
| 1382 | } |
| 1383 | break; |
| 1384 | case MMC_POWER_OFF: |
| 1385 | if (!IS_ERR(mmc->supply.vmmc)) |
| 1386 | mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); |
| 1387 | |
| 1388 | if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { |
| 1389 | regulator_disable(mmc->supply.vqmmc); |
| 1390 | host->vqmmc_enabled = false; |
| 1391 | } |
| 1392 | break; |
| 1393 | default: |
| 1394 | break; |
| 1395 | } |
| 1396 | |
Chaotian Jing | 6e62294 | 2015-10-27 14:24:24 +0800 | [diff] [blame] | 1397 | if (host->mclk != ios->clock || host->timing != ios->timing) |
| 1398 | msdc_set_mclk(host, ios->timing, ios->clock); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1399 | } |
| 1400 | |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1401 | static u32 test_delay_bit(u32 delay, u32 bit) |
| 1402 | { |
| 1403 | bit %= PAD_DELAY_MAX; |
| 1404 | return delay & (1 << bit); |
| 1405 | } |
| 1406 | |
| 1407 | static int get_delay_len(u32 delay, u32 start_bit) |
| 1408 | { |
| 1409 | int i; |
| 1410 | |
| 1411 | for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) { |
| 1412 | if (test_delay_bit(delay, start_bit + i) == 0) |
| 1413 | return i; |
| 1414 | } |
| 1415 | return PAD_DELAY_MAX - start_bit; |
| 1416 | } |
| 1417 | |
| 1418 | static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay) |
| 1419 | { |
| 1420 | int start = 0, len = 0; |
| 1421 | int start_final = 0, len_final = 0; |
| 1422 | u8 final_phase = 0xff; |
Geert Uytterhoeven | 62d494c | 2015-11-06 12:22:08 +0100 | [diff] [blame] | 1423 | struct msdc_delay_phase delay_phase = { 0, }; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1424 | |
| 1425 | if (delay == 0) { |
| 1426 | dev_err(host->dev, "phase error: [map:%x]\n", delay); |
| 1427 | delay_phase.final_phase = final_phase; |
| 1428 | return delay_phase; |
| 1429 | } |
| 1430 | |
| 1431 | while (start < PAD_DELAY_MAX) { |
| 1432 | len = get_delay_len(delay, start); |
| 1433 | if (len_final < len) { |
| 1434 | start_final = start; |
| 1435 | len_final = len; |
| 1436 | } |
| 1437 | start += len ? len : 1; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1438 | if (len >= 12 && start_final < 4) |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1439 | break; |
| 1440 | } |
| 1441 | |
| 1442 | /* The rule is that to find the smallest delay cell */ |
| 1443 | if (start_final == 0) |
| 1444 | final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX; |
| 1445 | else |
| 1446 | final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX; |
| 1447 | dev_info(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n", |
| 1448 | delay, len_final, final_phase); |
| 1449 | |
| 1450 | delay_phase.maxlen = len_final; |
| 1451 | delay_phase.start = start_final; |
| 1452 | delay_phase.final_phase = final_phase; |
| 1453 | return delay_phase; |
| 1454 | } |
| 1455 | |
| 1456 | static int msdc_tune_response(struct mmc_host *mmc, u32 opcode) |
| 1457 | { |
| 1458 | struct msdc_host *host = mmc_priv(mmc); |
| 1459 | u32 rise_delay = 0, fall_delay = 0; |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1460 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1461 | struct msdc_delay_phase internal_delay_phase; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1462 | u8 final_delay, final_maxlen; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1463 | u32 internal_delay = 0; |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1464 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1465 | int cmd_err; |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1466 | int i, j; |
| 1467 | |
| 1468 | if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 1469 | mmc->ios.timing == MMC_TIMING_UHS_SDR104) |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1470 | sdr_set_field(host->base + tune_reg, |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1471 | MSDC_PAD_TUNE_CMDRRDLY, |
| 1472 | host->hs200_cmd_int_delay); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1473 | |
| 1474 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1475 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1476 | sdr_set_field(host->base + tune_reg, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1477 | MSDC_PAD_TUNE_CMDRDLY, i); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1478 | /* |
| 1479 | * Using the same parameters, it may sometimes pass the test, |
| 1480 | * but sometimes it may fail. To make sure the parameters are |
| 1481 | * more stable, we test each set of parameters 3 times. |
| 1482 | */ |
| 1483 | for (j = 0; j < 3; j++) { |
| 1484 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1485 | if (!cmd_err) { |
| 1486 | rise_delay |= (1 << i); |
| 1487 | } else { |
| 1488 | rise_delay &= ~(1 << i); |
| 1489 | break; |
| 1490 | } |
| 1491 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1492 | } |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1493 | final_rise_delay = get_best_delay(host, rise_delay); |
| 1494 | /* if rising edge has enough margin, then do not scan falling edge */ |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1495 | if (final_rise_delay.maxlen >= 12 && final_rise_delay.start < 4) |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1496 | goto skip_fall; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1497 | |
| 1498 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1499 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1500 | sdr_set_field(host->base + tune_reg, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1501 | MSDC_PAD_TUNE_CMDRDLY, i); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1502 | /* |
| 1503 | * Using the same parameters, it may sometimes pass the test, |
| 1504 | * but sometimes it may fail. To make sure the parameters are |
| 1505 | * more stable, we test each set of parameters 3 times. |
| 1506 | */ |
| 1507 | for (j = 0; j < 3; j++) { |
| 1508 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1509 | if (!cmd_err) { |
| 1510 | fall_delay |= (1 << i); |
| 1511 | } else { |
| 1512 | fall_delay &= ~(1 << i); |
| 1513 | break; |
| 1514 | } |
| 1515 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1516 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1517 | final_fall_delay = get_best_delay(host, fall_delay); |
| 1518 | |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1519 | skip_fall: |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1520 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1521 | if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4) |
| 1522 | final_maxlen = final_fall_delay.maxlen; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1523 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1524 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1525 | sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1526 | final_rise_delay.final_phase); |
| 1527 | final_delay = final_rise_delay.final_phase; |
| 1528 | } else { |
| 1529 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1530 | sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1531 | final_fall_delay.final_phase); |
| 1532 | final_delay = final_fall_delay.final_phase; |
| 1533 | } |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1534 | if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay) |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1535 | goto skip_internal; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1536 | |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1537 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1538 | sdr_set_field(host->base + tune_reg, |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1539 | MSDC_PAD_TUNE_CMDRRDLY, i); |
| 1540 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1541 | if (!cmd_err) |
| 1542 | internal_delay |= (1 << i); |
| 1543 | } |
| 1544 | dev_dbg(host->dev, "Final internal delay: 0x%x\n", internal_delay); |
| 1545 | internal_delay_phase = get_best_delay(host, internal_delay); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1546 | sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRRDLY, |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1547 | internal_delay_phase.final_phase); |
| 1548 | skip_internal: |
| 1549 | dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay); |
| 1550 | return final_delay == 0xff ? -EIO : 0; |
| 1551 | } |
| 1552 | |
| 1553 | static int hs400_tune_response(struct mmc_host *mmc, u32 opcode) |
| 1554 | { |
| 1555 | struct msdc_host *host = mmc_priv(mmc); |
| 1556 | u32 cmd_delay = 0; |
| 1557 | struct msdc_delay_phase final_cmd_delay = { 0,}; |
| 1558 | u8 final_delay; |
| 1559 | int cmd_err; |
| 1560 | int i, j; |
| 1561 | |
| 1562 | /* select EMMC50 PAD CMD tune */ |
| 1563 | sdr_set_bits(host->base + PAD_CMD_TUNE, BIT(0)); |
| 1564 | |
| 1565 | if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || |
| 1566 | mmc->ios.timing == MMC_TIMING_UHS_SDR104) |
| 1567 | sdr_set_field(host->base + MSDC_PAD_TUNE, |
| 1568 | MSDC_PAD_TUNE_CMDRRDLY, |
| 1569 | host->hs200_cmd_int_delay); |
| 1570 | |
| 1571 | if (host->hs400_cmd_resp_sel_rising) |
| 1572 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1573 | else |
| 1574 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); |
| 1575 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
| 1576 | sdr_set_field(host->base + PAD_CMD_TUNE, |
| 1577 | PAD_CMD_TUNE_RX_DLY3, i); |
| 1578 | /* |
| 1579 | * Using the same parameters, it may sometimes pass the test, |
| 1580 | * but sometimes it may fail. To make sure the parameters are |
| 1581 | * more stable, we test each set of parameters 3 times. |
| 1582 | */ |
| 1583 | for (j = 0; j < 3; j++) { |
| 1584 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1585 | if (!cmd_err) { |
| 1586 | cmd_delay |= (1 << i); |
| 1587 | } else { |
| 1588 | cmd_delay &= ~(1 << i); |
| 1589 | break; |
| 1590 | } |
| 1591 | } |
| 1592 | } |
| 1593 | final_cmd_delay = get_best_delay(host, cmd_delay); |
| 1594 | sdr_set_field(host->base + PAD_CMD_TUNE, PAD_CMD_TUNE_RX_DLY3, |
| 1595 | final_cmd_delay.final_phase); |
| 1596 | final_delay = final_cmd_delay.final_phase; |
| 1597 | |
| 1598 | dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1599 | return final_delay == 0xff ? -EIO : 0; |
| 1600 | } |
| 1601 | |
| 1602 | static int msdc_tune_data(struct mmc_host *mmc, u32 opcode) |
| 1603 | { |
| 1604 | struct msdc_host *host = mmc_priv(mmc); |
| 1605 | u32 rise_delay = 0, fall_delay = 0; |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1606 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1607 | u8 final_delay, final_maxlen; |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1608 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1609 | int i, ret; |
| 1610 | |
| 1611 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1612 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 1613 | for (i = 0 ; i < PAD_DELAY_MAX; i++) { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1614 | sdr_set_field(host->base + tune_reg, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1615 | MSDC_PAD_TUNE_DATRRDLY, i); |
| 1616 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 1617 | if (!ret) |
| 1618 | rise_delay |= (1 << i); |
| 1619 | } |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1620 | final_rise_delay = get_best_delay(host, rise_delay); |
| 1621 | /* if rising edge has enough margin, then do not scan falling edge */ |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1622 | if (final_rise_delay.maxlen >= 12 || |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1623 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 1624 | goto skip_fall; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1625 | |
| 1626 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1627 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
| 1628 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1629 | sdr_set_field(host->base + tune_reg, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1630 | MSDC_PAD_TUNE_DATRRDLY, i); |
| 1631 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 1632 | if (!ret) |
| 1633 | fall_delay |= (1 << i); |
| 1634 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1635 | final_fall_delay = get_best_delay(host, fall_delay); |
| 1636 | |
Chaotian Jing | ae9c657 | 2016-06-30 10:01:01 +0800 | [diff] [blame] | 1637 | skip_fall: |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1638 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1639 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1640 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1641 | sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1642 | sdr_set_field(host->base + tune_reg, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1643 | MSDC_PAD_TUNE_DATRRDLY, |
| 1644 | final_rise_delay.final_phase); |
| 1645 | final_delay = final_rise_delay.final_phase; |
| 1646 | } else { |
| 1647 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); |
| 1648 | sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1649 | sdr_set_field(host->base + tune_reg, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1650 | MSDC_PAD_TUNE_DATRRDLY, |
| 1651 | final_fall_delay.final_phase); |
| 1652 | final_delay = final_fall_delay.final_phase; |
| 1653 | } |
| 1654 | |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1655 | dev_dbg(host->dev, "Final data pad delay: %x\n", final_delay); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1656 | return final_delay == 0xff ? -EIO : 0; |
| 1657 | } |
| 1658 | |
| 1659 | static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode) |
| 1660 | { |
| 1661 | struct msdc_host *host = mmc_priv(mmc); |
| 1662 | int ret; |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1663 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1664 | |
Chaotian Jing | 7f3d585 | 2017-10-16 09:46:31 +0800 | [diff] [blame] | 1665 | if (host->hs400_mode && |
| 1666 | host->dev_comp->hs400_tune) |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1667 | ret = hs400_tune_response(mmc, opcode); |
| 1668 | else |
| 1669 | ret = msdc_tune_response(mmc, opcode); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1670 | if (ret == -EIO) { |
| 1671 | dev_err(host->dev, "Tune response fail!\n"); |
Ulf Hansson | 567979f | 2016-03-21 14:21:25 +0100 | [diff] [blame] | 1672 | return ret; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1673 | } |
Chaotian Jing | 5462ff3 | 2016-06-30 10:00:58 +0800 | [diff] [blame] | 1674 | if (host->hs400_mode == false) { |
| 1675 | ret = msdc_tune_data(mmc, opcode); |
| 1676 | if (ret == -EIO) |
| 1677 | dev_err(host->dev, "Tune data fail!\n"); |
| 1678 | } |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1679 | |
Chaotian Jing | 86beac3 | 2016-06-30 10:00:59 +0800 | [diff] [blame] | 1680 | host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1681 | host->saved_tune_para.pad_tune = readl(host->base + tune_reg); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1682 | host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1683 | return ret; |
| 1684 | } |
| 1685 | |
| 1686 | static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) |
| 1687 | { |
| 1688 | struct msdc_host *host = mmc_priv(mmc); |
Chaotian Jing | 5462ff3 | 2016-06-30 10:00:58 +0800 | [diff] [blame] | 1689 | host->hs400_mode = true; |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1690 | |
| 1691 | writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE); |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1692 | /* hs400 mode must set it to 0 */ |
| 1693 | sdr_clr_bits(host->base + MSDC_PATCH_BIT2, MSDC_PATCH_BIT2_CFGCRCSTS); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
Chaotian Jing | c9b5061 | 2015-10-27 14:24:26 +0800 | [diff] [blame] | 1697 | static void msdc_hw_reset(struct mmc_host *mmc) |
| 1698 | { |
| 1699 | struct msdc_host *host = mmc_priv(mmc); |
| 1700 | |
| 1701 | sdr_set_bits(host->base + EMMC_IOCON, 1); |
| 1702 | udelay(10); /* 10us is enough */ |
| 1703 | sdr_clr_bits(host->base + EMMC_IOCON, 1); |
| 1704 | } |
| 1705 | |
Julia Lawall | be7815d | 2017-07-29 07:59:40 +0200 | [diff] [blame] | 1706 | static const struct mmc_host_ops mt_msdc_ops = { |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1707 | .post_req = msdc_post_req, |
| 1708 | .pre_req = msdc_pre_req, |
| 1709 | .request = msdc_ops_request, |
| 1710 | .set_ios = msdc_ops_set_ios, |
Chaotian Jing | 8d53e41 | 2016-02-15 02:31:00 +0800 | [diff] [blame] | 1711 | .get_ro = mmc_gpio_get_ro, |
Chaotian Jing | c7b16de | 2017-07-03 14:24:56 +0800 | [diff] [blame] | 1712 | .get_cd = mmc_gpio_get_cd, |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1713 | .start_signal_voltage_switch = msdc_ops_switch_volt, |
| 1714 | .card_busy = msdc_card_busy, |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1715 | .execute_tuning = msdc_execute_tuning, |
| 1716 | .prepare_hs400_tuning = msdc_prepare_hs400_tuning, |
Chaotian Jing | c9b5061 | 2015-10-27 14:24:26 +0800 | [diff] [blame] | 1717 | .hw_reset = msdc_hw_reset, |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1718 | }; |
| 1719 | |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1720 | static void msdc_of_property_parse(struct platform_device *pdev, |
| 1721 | struct msdc_host *host) |
| 1722 | { |
| 1723 | of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay", |
| 1724 | &host->hs400_ds_delay); |
| 1725 | |
| 1726 | of_property_read_u32(pdev->dev.of_node, "mediatek,hs200-cmd-int-delay", |
| 1727 | &host->hs200_cmd_int_delay); |
| 1728 | |
| 1729 | of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-cmd-int-delay", |
| 1730 | &host->hs400_cmd_int_delay); |
| 1731 | |
| 1732 | if (of_property_read_bool(pdev->dev.of_node, |
| 1733 | "mediatek,hs400-cmd-resp-sel-rising")) |
| 1734 | host->hs400_cmd_resp_sel_rising = true; |
| 1735 | else |
| 1736 | host->hs400_cmd_resp_sel_rising = false; |
| 1737 | } |
| 1738 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1739 | static int msdc_drv_probe(struct platform_device *pdev) |
| 1740 | { |
| 1741 | struct mmc_host *mmc; |
| 1742 | struct msdc_host *host; |
| 1743 | struct resource *res; |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 1744 | const struct of_device_id *of_id; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1745 | int ret; |
| 1746 | |
| 1747 | if (!pdev->dev.of_node) { |
| 1748 | dev_err(&pdev->dev, "No DT found\n"); |
| 1749 | return -EINVAL; |
| 1750 | } |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 1751 | |
| 1752 | of_id = of_match_node(msdc_of_ids, pdev->dev.of_node); |
| 1753 | if (!of_id) |
| 1754 | return -EINVAL; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1755 | /* Allocate MMC host for this device */ |
| 1756 | mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev); |
| 1757 | if (!mmc) |
| 1758 | return -ENOMEM; |
| 1759 | |
| 1760 | host = mmc_priv(mmc); |
| 1761 | ret = mmc_of_parse(mmc); |
| 1762 | if (ret) |
| 1763 | goto host_free; |
| 1764 | |
| 1765 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1766 | host->base = devm_ioremap_resource(&pdev->dev, res); |
| 1767 | if (IS_ERR(host->base)) { |
| 1768 | ret = PTR_ERR(host->base); |
| 1769 | goto host_free; |
| 1770 | } |
| 1771 | |
| 1772 | ret = mmc_regulator_get_supply(mmc); |
Wolfram Sang | 2f98ef6 | 2017-10-14 21:17:15 +0200 | [diff] [blame] | 1773 | if (ret) |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1774 | goto host_free; |
| 1775 | |
| 1776 | host->src_clk = devm_clk_get(&pdev->dev, "source"); |
| 1777 | if (IS_ERR(host->src_clk)) { |
| 1778 | ret = PTR_ERR(host->src_clk); |
| 1779 | goto host_free; |
| 1780 | } |
| 1781 | |
| 1782 | host->h_clk = devm_clk_get(&pdev->dev, "hclk"); |
| 1783 | if (IS_ERR(host->h_clk)) { |
| 1784 | ret = PTR_ERR(host->h_clk); |
| 1785 | goto host_free; |
| 1786 | } |
| 1787 | |
| 1788 | host->irq = platform_get_irq(pdev, 0); |
| 1789 | if (host->irq < 0) { |
| 1790 | ret = -EINVAL; |
| 1791 | goto host_free; |
| 1792 | } |
| 1793 | |
| 1794 | host->pinctrl = devm_pinctrl_get(&pdev->dev); |
| 1795 | if (IS_ERR(host->pinctrl)) { |
| 1796 | ret = PTR_ERR(host->pinctrl); |
| 1797 | dev_err(&pdev->dev, "Cannot find pinctrl!\n"); |
| 1798 | goto host_free; |
| 1799 | } |
| 1800 | |
| 1801 | host->pins_default = pinctrl_lookup_state(host->pinctrl, "default"); |
| 1802 | if (IS_ERR(host->pins_default)) { |
| 1803 | ret = PTR_ERR(host->pins_default); |
| 1804 | dev_err(&pdev->dev, "Cannot find pinctrl default!\n"); |
| 1805 | goto host_free; |
| 1806 | } |
| 1807 | |
| 1808 | host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs"); |
| 1809 | if (IS_ERR(host->pins_uhs)) { |
| 1810 | ret = PTR_ERR(host->pins_uhs); |
| 1811 | dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n"); |
| 1812 | goto host_free; |
| 1813 | } |
| 1814 | |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1815 | msdc_of_property_parse(pdev, host); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1816 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1817 | host->dev = &pdev->dev; |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 1818 | host->dev_comp = of_id->data; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1819 | host->mmc = mmc; |
| 1820 | host->src_clk_freq = clk_get_rate(host->src_clk); |
| 1821 | /* Set host parameters to mmc */ |
| 1822 | mmc->ops = &mt_msdc_ops; |
Chaotian Jing | 762d491 | 2017-10-16 09:46:29 +0800 | [diff] [blame] | 1823 | if (host->dev_comp->clk_div_bits == 8) |
| 1824 | mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255); |
| 1825 | else |
| 1826 | mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 4095); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1827 | |
| 1828 | mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23; |
| 1829 | /* MMC core transfer sizes tunable parameters */ |
| 1830 | mmc->max_segs = MAX_BD_NUM; |
| 1831 | mmc->max_seg_size = BDMA_DESC_BUFLEN; |
| 1832 | mmc->max_blk_size = 2048; |
| 1833 | mmc->max_req_size = 512 * 1024; |
| 1834 | mmc->max_blk_count = mmc->max_req_size / 512; |
| 1835 | host->dma_mask = DMA_BIT_MASK(32); |
| 1836 | mmc_dev(mmc)->dma_mask = &host->dma_mask; |
| 1837 | |
| 1838 | host->timeout_clks = 3 * 1048576; |
| 1839 | host->dma.gpd = dma_alloc_coherent(&pdev->dev, |
Chaotian Jing | 62b0d27 | 2015-10-27 14:24:25 +0800 | [diff] [blame] | 1840 | 2 * sizeof(struct mt_gpdma_desc), |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1841 | &host->dma.gpd_addr, GFP_KERNEL); |
| 1842 | host->dma.bd = dma_alloc_coherent(&pdev->dev, |
| 1843 | MAX_BD_NUM * sizeof(struct mt_bdma_desc), |
| 1844 | &host->dma.bd_addr, GFP_KERNEL); |
| 1845 | if (!host->dma.gpd || !host->dma.bd) { |
| 1846 | ret = -ENOMEM; |
| 1847 | goto release_mem; |
| 1848 | } |
| 1849 | msdc_init_gpd_bd(host, &host->dma); |
| 1850 | INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout); |
| 1851 | spin_lock_init(&host->lock); |
| 1852 | |
| 1853 | platform_set_drvdata(pdev, mmc); |
| 1854 | msdc_ungate_clock(host); |
| 1855 | msdc_init_hw(host); |
| 1856 | |
| 1857 | ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq, |
| 1858 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, pdev->name, host); |
| 1859 | if (ret) |
| 1860 | goto release; |
| 1861 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1862 | pm_runtime_set_active(host->dev); |
| 1863 | pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY); |
| 1864 | pm_runtime_use_autosuspend(host->dev); |
| 1865 | pm_runtime_enable(host->dev); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1866 | ret = mmc_add_host(mmc); |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1867 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1868 | if (ret) |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1869 | goto end; |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1870 | |
| 1871 | return 0; |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1872 | end: |
| 1873 | pm_runtime_disable(host->dev); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1874 | release: |
| 1875 | platform_set_drvdata(pdev, NULL); |
| 1876 | msdc_deinit_hw(host); |
| 1877 | msdc_gate_clock(host); |
| 1878 | release_mem: |
| 1879 | if (host->dma.gpd) |
| 1880 | dma_free_coherent(&pdev->dev, |
Chaotian Jing | 62b0d27 | 2015-10-27 14:24:25 +0800 | [diff] [blame] | 1881 | 2 * sizeof(struct mt_gpdma_desc), |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1882 | host->dma.gpd, host->dma.gpd_addr); |
| 1883 | if (host->dma.bd) |
| 1884 | dma_free_coherent(&pdev->dev, |
| 1885 | MAX_BD_NUM * sizeof(struct mt_bdma_desc), |
| 1886 | host->dma.bd, host->dma.bd_addr); |
| 1887 | host_free: |
| 1888 | mmc_free_host(mmc); |
| 1889 | |
| 1890 | return ret; |
| 1891 | } |
| 1892 | |
| 1893 | static int msdc_drv_remove(struct platform_device *pdev) |
| 1894 | { |
| 1895 | struct mmc_host *mmc; |
| 1896 | struct msdc_host *host; |
| 1897 | |
| 1898 | mmc = platform_get_drvdata(pdev); |
| 1899 | host = mmc_priv(mmc); |
| 1900 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1901 | pm_runtime_get_sync(host->dev); |
| 1902 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1903 | platform_set_drvdata(pdev, NULL); |
| 1904 | mmc_remove_host(host->mmc); |
| 1905 | msdc_deinit_hw(host); |
| 1906 | msdc_gate_clock(host); |
| 1907 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1908 | pm_runtime_disable(host->dev); |
| 1909 | pm_runtime_put_noidle(host->dev); |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1910 | dma_free_coherent(&pdev->dev, |
Phong LE | 16f2e0c | 2017-05-24 09:53:52 +0200 | [diff] [blame] | 1911 | 2 * sizeof(struct mt_gpdma_desc), |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1912 | host->dma.gpd, host->dma.gpd_addr); |
| 1913 | dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc), |
| 1914 | host->dma.bd, host->dma.bd_addr); |
| 1915 | |
| 1916 | mmc_free_host(host->mmc); |
| 1917 | |
| 1918 | return 0; |
| 1919 | } |
| 1920 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1921 | #ifdef CONFIG_PM |
| 1922 | static void msdc_save_reg(struct msdc_host *host) |
| 1923 | { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1924 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 1925 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1926 | host->save_para.msdc_cfg = readl(host->base + MSDC_CFG); |
| 1927 | host->save_para.iocon = readl(host->base + MSDC_IOCON); |
| 1928 | host->save_para.sdc_cfg = readl(host->base + SDC_CFG); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1929 | host->save_para.pad_tune = readl(host->base + tune_reg); |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1930 | host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT); |
| 1931 | host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1); |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1932 | host->save_para.patch_bit2 = readl(host->base + MSDC_PATCH_BIT2); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1933 | host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1934 | host->save_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1935 | host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0); |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1936 | } |
| 1937 | |
| 1938 | static void msdc_restore_reg(struct msdc_host *host) |
| 1939 | { |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1940 | u32 tune_reg = host->dev_comp->pad_tune_reg; |
| 1941 | |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1942 | writel(host->save_para.msdc_cfg, host->base + MSDC_CFG); |
| 1943 | writel(host->save_para.iocon, host->base + MSDC_IOCON); |
| 1944 | writel(host->save_para.sdc_cfg, host->base + SDC_CFG); |
Chaotian Jing | 39add25 | 2017-10-16 09:46:32 +0800 | [diff] [blame] | 1945 | writel(host->save_para.pad_tune, host->base + tune_reg); |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1946 | writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT); |
| 1947 | writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1); |
Chaotian Jing | 2fea581 | 2017-10-16 09:46:33 +0800 | [diff] [blame] | 1948 | writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1949 | writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE); |
yong mao | 1ede5cb | 2017-03-15 15:26:40 +0800 | [diff] [blame] | 1950 | writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE); |
Chaotian Jing | 6397b7f | 2015-10-27 14:24:29 +0800 | [diff] [blame] | 1951 | writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0); |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | static int msdc_runtime_suspend(struct device *dev) |
| 1955 | { |
| 1956 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 1957 | struct msdc_host *host = mmc_priv(mmc); |
| 1958 | |
| 1959 | msdc_save_reg(host); |
| 1960 | msdc_gate_clock(host); |
| 1961 | return 0; |
| 1962 | } |
| 1963 | |
| 1964 | static int msdc_runtime_resume(struct device *dev) |
| 1965 | { |
| 1966 | struct mmc_host *mmc = dev_get_drvdata(dev); |
| 1967 | struct msdc_host *host = mmc_priv(mmc); |
| 1968 | |
| 1969 | msdc_ungate_clock(host); |
| 1970 | msdc_restore_reg(host); |
| 1971 | return 0; |
| 1972 | } |
| 1973 | #endif |
| 1974 | |
| 1975 | static const struct dev_pm_ops msdc_dev_pm_ops = { |
| 1976 | SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend, |
| 1977 | pm_runtime_force_resume) |
| 1978 | SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL) |
| 1979 | }; |
| 1980 | |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1981 | static struct platform_driver mt_msdc_driver = { |
| 1982 | .probe = msdc_drv_probe, |
| 1983 | .remove = msdc_drv_remove, |
| 1984 | .driver = { |
| 1985 | .name = "mtk-msdc", |
| 1986 | .of_match_table = msdc_of_ids, |
Chaotian Jing | 4b8a43e | 2015-06-15 19:20:49 +0800 | [diff] [blame] | 1987 | .pm = &msdc_dev_pm_ops, |
Chaotian Jing | 2084890 | 2015-06-15 19:20:48 +0800 | [diff] [blame] | 1988 | }, |
| 1989 | }; |
| 1990 | |
| 1991 | module_platform_driver(mt_msdc_driver); |
| 1992 | MODULE_LICENSE("GPL v2"); |
| 1993 | MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver"); |