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