blob: 650b94498f7cab36e8ccb08c91c06176c74c3ce5 [file] [log] [blame]
Chaotian Jing20848902015-06-15 19:20:48 +08001/*
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 Jing4b8a43e2015-06-15 19:20:49 +080026#include <linux/pm.h>
27#include <linux/pm_runtime.h>
Chaotian Jing20848902015-06-15 19:20:48 +080028#include <linux/regulator/consumer.h>
Chaotian Jing6397b7f2015-10-27 14:24:29 +080029#include <linux/slab.h>
Chaotian Jing20848902015-06-15 19:20:48 +080030#include <linux/spinlock.h>
Ulf Hanssonb8789ec2016-12-30 13:47:23 +010031#include <linux/interrupt.h>
Chaotian Jing20848902015-06-15 19:20:48 +080032
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 Jing8d53e412016-02-15 02:31:00 +080039#include <linux/mmc/slot-gpio.h>
Chaotian Jing20848902015-06-15 19:20:48 +080040
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 Jingc9b50612015-10-27 14:24:26 +080070#define EMMC_IOCON 0x7c
Chaotian Jing20848902015-06-15 19:20:48 +080071#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 Jing2fea5812017-10-16 09:46:33 +080077#define MSDC_PATCH_BIT2 0xb8
Chaotian Jing20848902015-06-15 19:20:48 +080078#define MSDC_PAD_TUNE 0xec
Chaotian Jing39add252017-10-16 09:46:32 +080079#define MSDC_PAD_TUNE0 0xf0
Chaotian Jing6397b7f2015-10-27 14:24:29 +080080#define PAD_DS_TUNE 0x188
yong mao1ede5cb2017-03-15 15:26:40 +080081#define PAD_CMD_TUNE 0x18c
Chaotian Jing6397b7f2015-10-27 14:24:29 +080082#define EMMC50_CFG0 0x208
Chaotian Jing20848902015-06-15 19:20:48 +080083
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 Jing6397b7f2015-10-27 14:24:29 +080099#define MSDC_CFG_HS400_CK_MODE (0x1 << 18) /* RW */
Chaotian Jing762d4912017-10-16 09:46:29 +0800100#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 Jing20848902015-06-15 19:20:48 +0800103
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 Jing2fea5812017-10-16 09:46:33 +0800220#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 mao1ede5cb2017-03-15 15:26:40 +0800226#define MSDC_PAD_TUNE_DATWRDLY (0x1f << 0) /* RW */
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800227#define MSDC_PAD_TUNE_DATRRDLY (0x1f << 8) /* RW */
228#define MSDC_PAD_TUNE_CMDRDLY (0x1f << 16) /* RW */
yong mao1ede5cb2017-03-15 15:26:40 +0800229#define MSDC_PAD_TUNE_CMDRRDLY (0x1f << 22) /* RW */
230#define MSDC_PAD_TUNE_CLKTDLY (0x1f << 27) /* RW */
Chaotian Jing2fea5812017-10-16 09:46:33 +0800231#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 Jing6397b7f2015-10-27 14:24:29 +0800234
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 mao1ede5cb2017-03-15 15:26:40 +0800239#define PAD_CMD_TUNE_RX_DLY3 (0x1f << 1) /* RW */
240
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800241#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 Jing20848902015-06-15 19:20:48 +0800245#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 Jing4b8a43e2015-06-15 19:20:49 +0800256#define MTK_MMC_AUTOSUSPEND_DELAY 50
Chaotian Jing20848902015-06-15 19:20:48 +0800257#define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */
258#define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */
259
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800260#define PAD_DELAY_MAX 32 /* PAD delay cells */
Chaotian Jing20848902015-06-15 19:20:48 +0800261/*--------------------------------------------------------------------------*/
262/* Descriptor Structure */
263/*--------------------------------------------------------------------------*/
264struct 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
280struct 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
292struct 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 Jing4b8a43e2015-06-15 19:20:49 +0800300struct 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 Jing2fea5812017-10-16 09:46:33 +0800307 u32 patch_bit2;
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800308 u32 pad_ds_tune;
yong mao1ede5cb2017-03-15 15:26:40 +0800309 u32 pad_cmd_tune;
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800310 u32 emmc50_cfg0;
311};
312
Chaotian Jing762d4912017-10-16 09:46:29 +0800313struct mtk_mmc_compatible {
314 u8 clk_div_bits;
Chaotian Jing7f3d5852017-10-16 09:46:31 +0800315 bool hs400_tune; /* only used for MT8173 */
Chaotian Jing39add252017-10-16 09:46:32 +0800316 u32 pad_tune_reg;
Chaotian Jing2fea5812017-10-16 09:46:33 +0800317 bool async_fifo;
318 bool data_tune;
Chaotian Jingacde28c2017-10-16 09:46:34 +0800319 bool busy_check;
Chaotian Jing762d4912017-10-16 09:46:29 +0800320};
321
Chaotian Jing86beac32016-06-30 10:00:59 +0800322struct msdc_tune_para {
323 u32 iocon;
324 u32 pad_tune;
yong mao1ede5cb2017-03-15 15:26:40 +0800325 u32 pad_cmd_tune;
Chaotian Jing86beac32016-06-30 10:00:59 +0800326};
327
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800328struct msdc_delay_phase {
329 u8 maxlen;
330 u8 start;
331 u8 final_phase;
Chaotian Jing4b8a43e2015-06-15 19:20:49 +0800332};
333
Chaotian Jing20848902015-06-15 19:20:48 +0800334struct msdc_host {
335 struct device *dev;
Chaotian Jing762d4912017-10-16 09:46:29 +0800336 const struct mtk_mmc_compatible *dev_comp;
Chaotian Jing20848902015-06-15 19:20:48 +0800337 struct mmc_host *mmc; /* mmc structure */
338 int cmd_rsp;
339
340 spinlock_t lock;
341 struct mmc_request *mrq;
342 struct mmc_command *cmd;
343 struct mmc_data *data;
344 int error;
345
346 void __iomem *base; /* host base address */
347
348 struct msdc_dma dma; /* dma channel */
349 u64 dma_mask;
350
351 u32 timeout_ns; /* data timeout ns */
352 u32 timeout_clks; /* data timeout clks */
353
354 struct pinctrl *pinctrl;
355 struct pinctrl_state *pins_default;
356 struct pinctrl_state *pins_uhs;
357 struct delayed_work req_timeout;
358 int irq; /* host interrupt */
359
360 struct clk *src_clk; /* msdc source clock */
361 struct clk *h_clk; /* msdc h_clk */
362 u32 mclk; /* mmc subsystem clock frequency */
363 u32 src_clk_freq; /* source clock frequency */
364 u32 sclk; /* SD/MS bus clock frequency */
Chaotian Jing6e622942015-10-27 14:24:24 +0800365 unsigned char timing;
Chaotian Jing20848902015-06-15 19:20:48 +0800366 bool vqmmc_enabled;
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800367 u32 hs400_ds_delay;
yong mao1ede5cb2017-03-15 15:26:40 +0800368 u32 hs200_cmd_int_delay; /* cmd internal delay for HS200/SDR104 */
369 u32 hs400_cmd_int_delay; /* cmd internal delay for HS400 */
370 bool hs400_cmd_resp_sel_rising;
371 /* cmd response sample selection for HS400 */
Chaotian Jing5462ff32016-06-30 10:00:58 +0800372 bool hs400_mode; /* current eMMC will run at hs400 mode */
Chaotian Jing4b8a43e2015-06-15 19:20:49 +0800373 struct msdc_save_para save_para; /* used when gate HCLK */
Chaotian Jing86beac32016-06-30 10:00:59 +0800374 struct msdc_tune_para def_tune_para; /* default tune setting */
375 struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */
Chaotian Jing20848902015-06-15 19:20:48 +0800376};
377
Chaotian Jing762d4912017-10-16 09:46:29 +0800378static const struct mtk_mmc_compatible mt8135_compat = {
379 .clk_div_bits = 8,
Chaotian Jing7f3d5852017-10-16 09:46:31 +0800380 .hs400_tune = false,
Chaotian Jing39add252017-10-16 09:46:32 +0800381 .pad_tune_reg = MSDC_PAD_TUNE,
Chaotian Jing2fea5812017-10-16 09:46:33 +0800382 .async_fifo = false,
383 .data_tune = false,
Chaotian Jingacde28c2017-10-16 09:46:34 +0800384 .busy_check = false,
Chaotian Jing762d4912017-10-16 09:46:29 +0800385};
386
387static const struct mtk_mmc_compatible mt8173_compat = {
388 .clk_div_bits = 8,
Chaotian Jing7f3d5852017-10-16 09:46:31 +0800389 .hs400_tune = true,
Chaotian Jing39add252017-10-16 09:46:32 +0800390 .pad_tune_reg = MSDC_PAD_TUNE,
Chaotian Jing2fea5812017-10-16 09:46:33 +0800391 .async_fifo = false,
392 .data_tune = false,
Chaotian Jingacde28c2017-10-16 09:46:34 +0800393 .busy_check = false,
Chaotian Jing762d4912017-10-16 09:46:29 +0800394};
395
396static const struct mtk_mmc_compatible mt2701_compat = {
397 .clk_div_bits = 12,
Chaotian Jing7f3d5852017-10-16 09:46:31 +0800398 .hs400_tune = false,
Chaotian Jing39add252017-10-16 09:46:32 +0800399 .pad_tune_reg = MSDC_PAD_TUNE0,
Chaotian Jing2fea5812017-10-16 09:46:33 +0800400 .async_fifo = true,
401 .data_tune = true,
Chaotian Jingacde28c2017-10-16 09:46:34 +0800402 .busy_check = false,
Chaotian Jing762d4912017-10-16 09:46:29 +0800403};
404
405static const struct mtk_mmc_compatible mt2712_compat = {
406 .clk_div_bits = 12,
Chaotian Jing7f3d5852017-10-16 09:46:31 +0800407 .hs400_tune = false,
Chaotian Jing39add252017-10-16 09:46:32 +0800408 .pad_tune_reg = MSDC_PAD_TUNE0,
Chaotian Jing2fea5812017-10-16 09:46:33 +0800409 .async_fifo = true,
410 .data_tune = true,
Chaotian Jingacde28c2017-10-16 09:46:34 +0800411 .busy_check = true,
Chaotian Jing762d4912017-10-16 09:46:29 +0800412};
413
414static const struct of_device_id msdc_of_ids[] = {
415 { .compatible = "mediatek,mt8135-mmc", .data = &mt8135_compat},
416 { .compatible = "mediatek,mt8173-mmc", .data = &mt8173_compat},
417 { .compatible = "mediatek,mt2701-mmc", .data = &mt2701_compat},
418 { .compatible = "mediatek,mt2712-mmc", .data = &mt2712_compat},
419 {}
420};
421MODULE_DEVICE_TABLE(of, msdc_of_ids);
422
Chaotian Jing20848902015-06-15 19:20:48 +0800423static void sdr_set_bits(void __iomem *reg, u32 bs)
424{
425 u32 val = readl(reg);
426
427 val |= bs;
428 writel(val, reg);
429}
430
431static void sdr_clr_bits(void __iomem *reg, u32 bs)
432{
433 u32 val = readl(reg);
434
435 val &= ~bs;
436 writel(val, reg);
437}
438
439static void sdr_set_field(void __iomem *reg, u32 field, u32 val)
440{
441 unsigned int tv = readl(reg);
442
443 tv &= ~field;
444 tv |= ((val) << (ffs((unsigned int)field) - 1));
445 writel(tv, reg);
446}
447
448static void sdr_get_field(void __iomem *reg, u32 field, u32 *val)
449{
450 unsigned int tv = readl(reg);
451
452 *val = ((tv & field) >> (ffs((unsigned int)field) - 1));
453}
454
455static void msdc_reset_hw(struct msdc_host *host)
456{
457 u32 val;
458
459 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST);
460 while (readl(host->base + MSDC_CFG) & MSDC_CFG_RST)
461 cpu_relax();
462
463 sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR);
464 while (readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_CLR)
465 cpu_relax();
466
467 val = readl(host->base + MSDC_INT);
468 writel(val, host->base + MSDC_INT);
469}
470
471static void msdc_cmd_next(struct msdc_host *host,
472 struct mmc_request *mrq, struct mmc_command *cmd);
473
Chaotian Jing726a9aa2015-10-27 14:24:23 +0800474static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR |
475 MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY |
476 MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO;
477static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO |
Chaotian Jing20848902015-06-15 19:20:48 +0800478 MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR |
479 MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT;
480
481static u8 msdc_dma_calcs(u8 *buf, u32 len)
482{
483 u32 i, sum = 0;
484
485 for (i = 0; i < len; i++)
486 sum += buf[i];
487 return 0xff - (u8) sum;
488}
489
490static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
491 struct mmc_data *data)
492{
493 unsigned int j, dma_len;
494 dma_addr_t dma_address;
495 u32 dma_ctrl;
496 struct scatterlist *sg;
497 struct mt_gpdma_desc *gpd;
498 struct mt_bdma_desc *bd;
499
500 sg = data->sg;
501
502 gpd = dma->gpd;
503 bd = dma->bd;
504
505 /* modify gpd */
506 gpd->gpd_info |= GPDMA_DESC_HWO;
507 gpd->gpd_info |= GPDMA_DESC_BDP;
508 /* need to clear first. use these bits to calc checksum */
509 gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM;
510 gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8;
511
512 /* modify bd */
513 for_each_sg(data->sg, sg, data->sg_count, j) {
514 dma_address = sg_dma_address(sg);
515 dma_len = sg_dma_len(sg);
516
517 /* init bd */
518 bd[j].bd_info &= ~BDMA_DESC_BLKPAD;
519 bd[j].bd_info &= ~BDMA_DESC_DWPAD;
520 bd[j].ptr = (u32)dma_address;
521 bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN;
522 bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN);
523
524 if (j == data->sg_count - 1) /* the last bd */
525 bd[j].bd_info |= BDMA_DESC_EOL;
526 else
527 bd[j].bd_info &= ~BDMA_DESC_EOL;
528
529 /* checksume need to clear first */
530 bd[j].bd_info &= ~BDMA_DESC_CHECKSUM;
531 bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8;
532 }
533
534 sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1);
535 dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL);
536 dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE);
537 dma_ctrl |= (MSDC_BURST_64B << 12 | 1 << 8);
538 writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL);
539 writel((u32)dma->gpd_addr, host->base + MSDC_DMA_SA);
540}
541
542static void msdc_prepare_data(struct msdc_host *host, struct mmc_request *mrq)
543{
544 struct mmc_data *data = mrq->data;
545
546 if (!(data->host_cookie & MSDC_PREPARE_FLAG)) {
Chaotian Jing20848902015-06-15 19:20:48 +0800547 data->host_cookie |= MSDC_PREPARE_FLAG;
548 data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200549 mmc_get_dma_dir(data));
Chaotian Jing20848902015-06-15 19:20:48 +0800550 }
551}
552
553static void msdc_unprepare_data(struct msdc_host *host, struct mmc_request *mrq)
554{
555 struct mmc_data *data = mrq->data;
556
557 if (data->host_cookie & MSDC_ASYNC_FLAG)
558 return;
559
560 if (data->host_cookie & MSDC_PREPARE_FLAG) {
Chaotian Jing20848902015-06-15 19:20:48 +0800561 dma_unmap_sg(host->dev, data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200562 mmc_get_dma_dir(data));
Chaotian Jing20848902015-06-15 19:20:48 +0800563 data->host_cookie &= ~MSDC_PREPARE_FLAG;
564 }
565}
566
567/* clock control primitives */
568static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
569{
570 u32 timeout, clk_ns;
571 u32 mode = 0;
572
573 host->timeout_ns = ns;
574 host->timeout_clks = clks;
575 if (host->sclk == 0) {
576 timeout = 0;
577 } else {
578 clk_ns = 1000000000UL / host->sclk;
579 timeout = (ns + clk_ns - 1) / clk_ns + clks;
580 /* in 1048576 sclk cycle unit */
581 timeout = (timeout + (0x1 << 20) - 1) >> 20;
Chaotian Jing762d4912017-10-16 09:46:29 +0800582 if (host->dev_comp->clk_div_bits == 8)
583 sdr_get_field(host->base + MSDC_CFG,
584 MSDC_CFG_CKMOD, &mode);
585 else
586 sdr_get_field(host->base + MSDC_CFG,
587 MSDC_CFG_CKMOD_EXTRA, &mode);
Chaotian Jing20848902015-06-15 19:20:48 +0800588 /*DDR mode will double the clk cycles for data timeout */
589 timeout = mode >= 2 ? timeout * 2 : timeout;
590 timeout = timeout > 1 ? timeout - 1 : 0;
591 timeout = timeout > 255 ? 255 : timeout;
592 }
593 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, timeout);
594}
595
596static void msdc_gate_clock(struct msdc_host *host)
597{
598 clk_disable_unprepare(host->src_clk);
599 clk_disable_unprepare(host->h_clk);
600}
601
602static void msdc_ungate_clock(struct msdc_host *host)
603{
604 clk_prepare_enable(host->h_clk);
605 clk_prepare_enable(host->src_clk);
606 while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
607 cpu_relax();
608}
609
Chaotian Jing6e622942015-10-27 14:24:24 +0800610static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz)
Chaotian Jing20848902015-06-15 19:20:48 +0800611{
612 u32 mode;
613 u32 flags;
614 u32 div;
615 u32 sclk;
Chaotian Jing39add252017-10-16 09:46:32 +0800616 u32 tune_reg = host->dev_comp->pad_tune_reg;
Chaotian Jing20848902015-06-15 19:20:48 +0800617
618 if (!hz) {
619 dev_dbg(host->dev, "set mclk to 0\n");
620 host->mclk = 0;
621 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
622 return;
623 }
624
625 flags = readl(host->base + MSDC_INTEN);
626 sdr_clr_bits(host->base + MSDC_INTEN, flags);
Chaotian Jing762d4912017-10-16 09:46:29 +0800627 if (host->dev_comp->clk_div_bits == 8)
628 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE);
629 else
630 sdr_clr_bits(host->base + MSDC_CFG,
631 MSDC_CFG_HS400_CK_MODE_EXTRA);
Chaotian Jing6e622942015-10-27 14:24:24 +0800632 if (timing == MMC_TIMING_UHS_DDR50 ||
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800633 timing == MMC_TIMING_MMC_DDR52 ||
634 timing == MMC_TIMING_MMC_HS400) {
635 if (timing == MMC_TIMING_MMC_HS400)
636 mode = 0x3;
637 else
638 mode = 0x2; /* ddr mode and use divisor */
639
Chaotian Jing20848902015-06-15 19:20:48 +0800640 if (hz >= (host->src_clk_freq >> 2)) {
641 div = 0; /* mean div = 1/4 */
642 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
643 } else {
644 div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
645 sclk = (host->src_clk_freq >> 2) / div;
646 div = (div >> 1);
647 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800648
649 if (timing == MMC_TIMING_MMC_HS400 &&
650 hz >= (host->src_clk_freq >> 1)) {
Chaotian Jing762d4912017-10-16 09:46:29 +0800651 if (host->dev_comp->clk_div_bits == 8)
652 sdr_set_bits(host->base + MSDC_CFG,
653 MSDC_CFG_HS400_CK_MODE);
654 else
655 sdr_set_bits(host->base + MSDC_CFG,
656 MSDC_CFG_HS400_CK_MODE_EXTRA);
Chaotian Jing6397b7f2015-10-27 14:24:29 +0800657 sclk = host->src_clk_freq >> 1;
658 div = 0; /* div is ignore when bit18 is set */
659 }
Chaotian Jing20848902015-06-15 19:20:48 +0800660 } else if (hz >= host->src_clk_freq) {
661 mode = 0x1; /* no divisor */
662 div = 0;
663 sclk = host->src_clk_freq;
664 } else {
665 mode = 0x0; /* use divisor */
666 if (hz >= (host->src_clk_freq >> 1)) {
667 div = 0; /* mean div = 1/2 */
668 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
669 } else {
670 div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2);
671 sclk = (host->src_clk_freq >> 2) / div;
672 }
673 }
Chaotian Jing762d4912017-10-16 09:46:29 +0800674 if (host->dev_comp->clk_div_bits == 8)
675 sdr_set_field(host->base + MSDC_CFG,
676 MSDC_CFG_CKMOD | MSDC_CFG_CKDIV,
677 (mode << 8) | div);
678 else
679 sdr_set_field(host->base + MSDC_CFG,
680 MSDC_CFG_CKMOD_EXTRA | MSDC_CFG_CKDIV_EXTRA,
681 (mode << 12) | div);
682
Chaotian Jing20848902015-06-15 19:20:48 +0800683 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
684 while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
685 cpu_relax();
686 host->sclk = sclk;
687 host->mclk = hz;
Chaotian Jing6e622942015-10-27 14:24:24 +0800688 host->timing = timing;
Chaotian Jing20848902015-06-15 19:20:48 +0800689 /* need because clk changed. */
690 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
691 sdr_set_bits(host->base + MSDC_INTEN, flags);
692
Chaotian Jing86beac32016-06-30 10:00:59 +0800693 /*
694 * mmc_select_hs400() will drop to 50Mhz and High speed mode,
695 * tune result of hs200/200Mhz is not suitable for 50Mhz
696 */
697 if (host->sclk <= 52000000) {
698 writel(host->def_tune_para.iocon, host->base + MSDC_IOCON);
Chaotian Jing39add252017-10-16 09:46:32 +0800699 writel(host->def_tune_para.pad_tune, host->base + tune_reg);
Chaotian Jing86beac32016-06-30 10:00:59 +0800700 } else {
701 writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON);
Chaotian Jing39add252017-10-16 09:46:32 +0800702 writel(host->saved_tune_para.pad_tune, host->base + tune_reg);
yong mao1ede5cb2017-03-15 15:26:40 +0800703 writel(host->saved_tune_para.pad_cmd_tune,
704 host->base + PAD_CMD_TUNE);
Chaotian Jing86beac32016-06-30 10:00:59 +0800705 }
706
Chaotian Jing7f3d5852017-10-16 09:46:31 +0800707 if (timing == MMC_TIMING_MMC_HS400 &&
708 host->dev_comp->hs400_tune)
yong mao1ede5cb2017-03-15 15:26:40 +0800709 sdr_set_field(host->base + PAD_CMD_TUNE,
710 MSDC_PAD_TUNE_CMDRRDLY,
711 host->hs400_cmd_int_delay);
Chaotian Jing6e622942015-10-27 14:24:24 +0800712 dev_dbg(host->dev, "sclk: %d, timing: %d\n", host->sclk, timing);
Chaotian Jing20848902015-06-15 19:20:48 +0800713}
714
715static inline u32 msdc_cmd_find_resp(struct msdc_host *host,
716 struct mmc_request *mrq, struct mmc_command *cmd)
717{
718 u32 resp;
719
720 switch (mmc_resp_type(cmd)) {
721 /* Actually, R1, R5, R6, R7 are the same */
722 case MMC_RSP_R1:
723 resp = 0x1;
724 break;
725 case MMC_RSP_R1B:
726 resp = 0x7;
727 break;
728 case MMC_RSP_R2:
729 resp = 0x2;
730 break;
731 case MMC_RSP_R3:
732 resp = 0x3;
733 break;
734 case MMC_RSP_NONE:
735 default:
736 resp = 0x0;
737 break;
738 }
739
740 return resp;
741}
742
743static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
744 struct mmc_request *mrq, struct mmc_command *cmd)
745{
746 /* rawcmd :
747 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
748 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
749 */
750 u32 opcode = cmd->opcode;
751 u32 resp = msdc_cmd_find_resp(host, mrq, cmd);
752 u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7);
753
754 host->cmd_rsp = resp;
755
756 if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) ||
757 opcode == MMC_STOP_TRANSMISSION)
758 rawcmd |= (0x1 << 14);
759 else if (opcode == SD_SWITCH_VOLTAGE)
760 rawcmd |= (0x1 << 30);
761 else if (opcode == SD_APP_SEND_SCR ||
762 opcode == SD_APP_SEND_NUM_WR_BLKS ||
763 (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
764 (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
765 (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC))
766 rawcmd |= (0x1 << 11);
767
768 if (cmd->data) {
769 struct mmc_data *data = cmd->data;
770
771 if (mmc_op_multi(opcode)) {
772 if (mmc_card_mmc(host->mmc->card) && mrq->sbc &&
773 !(mrq->sbc->arg & 0xFFFF0000))
774 rawcmd |= 0x2 << 28; /* AutoCMD23 */
775 }
776
777 rawcmd |= ((data->blksz & 0xFFF) << 16);
778 if (data->flags & MMC_DATA_WRITE)
779 rawcmd |= (0x1 << 13);
780 if (data->blocks > 1)
781 rawcmd |= (0x2 << 11);
782 else
783 rawcmd |= (0x1 << 11);
784 /* Always use dma mode */
785 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO);
786
787 if (host->timeout_ns != data->timeout_ns ||
788 host->timeout_clks != data->timeout_clks)
789 msdc_set_timeout(host, data->timeout_ns,
790 data->timeout_clks);
791
792 writel(data->blocks, host->base + SDC_BLK_NUM);
793 }
794 return rawcmd;
795}
796
797static void msdc_start_data(struct msdc_host *host, struct mmc_request *mrq,
798 struct mmc_command *cmd, struct mmc_data *data)
799{
800 bool read;
801
802 WARN_ON(host->data);
803 host->data = data;
804 read = data->flags & MMC_DATA_READ;
805
806 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
807 msdc_dma_setup(host, &host->dma, data);
808 sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask);
809 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
810 dev_dbg(host->dev, "DMA start\n");
811 dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n",
812 __func__, cmd->opcode, data->blocks, read);
813}
814
815static int msdc_auto_cmd_done(struct msdc_host *host, int events,
816 struct mmc_command *cmd)
817{
818 u32 *rsp = cmd->resp;
819
820 rsp[0] = readl(host->base + SDC_ACMD_RESP);
821
822 if (events & MSDC_INT_ACMDRDY) {
823 cmd->error = 0;
824 } else {
825 msdc_reset_hw(host);
826 if (events & MSDC_INT_ACMDCRCERR) {
827 cmd->error = -EILSEQ;
828 host->error |= REQ_STOP_EIO;
829 } else if (events & MSDC_INT_ACMDTMO) {
830 cmd->error = -ETIMEDOUT;
831 host->error |= REQ_STOP_TMO;
832 }
833 dev_err(host->dev,
834 "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n",
835 __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error);
836 }
837 return cmd->error;
838}
839
840static void msdc_track_cmd_data(struct msdc_host *host,
841 struct mmc_command *cmd, struct mmc_data *data)
842{
843 if (host->error)
844 dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n",
845 __func__, cmd->opcode, cmd->arg, host->error);
846}
847
848static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq)
849{
850 unsigned long flags;
851 bool ret;
852
853 ret = cancel_delayed_work(&host->req_timeout);
854 if (!ret) {
855 /* delay work already running */
856 return;
857 }
858 spin_lock_irqsave(&host->lock, flags);
859 host->mrq = NULL;
860 spin_unlock_irqrestore(&host->lock, flags);
861
862 msdc_track_cmd_data(host, mrq->cmd, mrq->data);
863 if (mrq->data)
864 msdc_unprepare_data(host, mrq);
865 mmc_request_done(host->mmc, mrq);
866}
867
868/* returns true if command is fully handled; returns false otherwise */
869static bool msdc_cmd_done(struct msdc_host *host, int events,
870 struct mmc_request *mrq, struct mmc_command *cmd)
871{
872 bool done = false;
873 bool sbc_error;
874 unsigned long flags;
875 u32 *rsp = cmd->resp;
876
877 if (mrq->sbc && cmd == mrq->cmd &&
878 (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR
879 | MSDC_INT_ACMDTMO)))
880 msdc_auto_cmd_done(host, events, mrq->sbc);
881
882 sbc_error = mrq->sbc && mrq->sbc->error;
883
884 if (!sbc_error && !(events & (MSDC_INT_CMDRDY
885 | MSDC_INT_RSPCRCERR
886 | MSDC_INT_CMDTMO)))
887 return done;
888
889 spin_lock_irqsave(&host->lock, flags);
890 done = !host->cmd;
891 host->cmd = NULL;
892 spin_unlock_irqrestore(&host->lock, flags);
893
894 if (done)
895 return true;
896
Chaotian Jing726a9aa2015-10-27 14:24:23 +0800897 sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask);
Chaotian Jing20848902015-06-15 19:20:48 +0800898
899 if (cmd->flags & MMC_RSP_PRESENT) {
900 if (cmd->flags & MMC_RSP_136) {
901 rsp[0] = readl(host->base + SDC_RESP3);
902 rsp[1] = readl(host->base + SDC_RESP2);
903 rsp[2] = readl(host->base + SDC_RESP1);
904 rsp[3] = readl(host->base + SDC_RESP0);
905 } else {
906 rsp[0] = readl(host->base + SDC_RESP0);
907 }
908 }
909
910 if (!sbc_error && !(events & MSDC_INT_CMDRDY)) {
Chaotian Jingddc71382016-06-30 10:01:00 +0800911 if (cmd->opcode != MMC_SEND_TUNING_BLOCK &&
912 cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)
913 /*
914 * should not clear fifo/interrupt as the tune data
915 * may have alreay come.
916 */
917 msdc_reset_hw(host);
Chaotian Jing20848902015-06-15 19:20:48 +0800918 if (events & MSDC_INT_RSPCRCERR) {
919 cmd->error = -EILSEQ;
920 host->error |= REQ_CMD_EIO;
921 } else if (events & MSDC_INT_CMDTMO) {
922 cmd->error = -ETIMEDOUT;
923 host->error |= REQ_CMD_TMO;
924 }
925 }
926 if (cmd->error)
927 dev_dbg(host->dev,
928 "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n",
929 __func__, cmd->opcode, cmd->arg, rsp[0],
930 cmd->error);
931
932 msdc_cmd_next(host, mrq, cmd);
933 return true;
934}
935
936/* It is the core layer's responsibility to ensure card status
937 * is correct before issue a request. but host design do below
938 * checks recommended.
939 */
940static inline bool msdc_cmd_is_ready(struct msdc_host *host,
941 struct mmc_request *mrq, struct mmc_command *cmd)
942{
943 /* The max busy time we can endure is 20ms */
944 unsigned long tmo = jiffies + msecs_to_jiffies(20);
945
946 while ((readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) &&
947 time_before(jiffies, tmo))
948 cpu_relax();
949 if (readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) {
950 dev_err(host->dev, "CMD bus busy detected\n");
951 host->error |= REQ_CMD_BUSY;
952 msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd);
953 return false;
954 }
955
956 if (mmc_resp_type(cmd) == MMC_RSP_R1B || cmd->data) {
957 tmo = jiffies + msecs_to_jiffies(20);
958 /* R1B or with data, should check SDCBUSY */
959 while ((readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) &&
960 time_before(jiffies, tmo))
961 cpu_relax();
962 if (readl(host->base + SDC_STS) & SDC_STS_SDCBUSY) {
963 dev_err(host->dev, "Controller busy detected\n");
964 host->error |= REQ_CMD_BUSY;
965 msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd);
966 return false;
967 }
968 }
969 return true;
970}
971
972static void msdc_start_command(struct msdc_host *host,
973 struct mmc_request *mrq, struct mmc_command *cmd)
974{
975 u32 rawcmd;
976
977 WARN_ON(host->cmd);
978 host->cmd = cmd;
979
980 if (!msdc_cmd_is_ready(host, mrq, cmd))
981 return;
982
983 if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 ||
984 readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) {
985 dev_err(host->dev, "TX/RX FIFO non-empty before start of IO. Reset\n");
986 msdc_reset_hw(host);
987 }
988
989 cmd->error = 0;
990 rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd);
991 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
992
Chaotian Jing726a9aa2015-10-27 14:24:23 +0800993 sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask);
Chaotian Jing20848902015-06-15 19:20:48 +0800994 writel(cmd->arg, host->base + SDC_ARG);
995 writel(rawcmd, host->base + SDC_CMD);
996}
997
998static void msdc_cmd_next(struct msdc_host *host,
999 struct mmc_request *mrq, struct mmc_command *cmd)
1000{
Chaotian Jingddc71382016-06-30 10:01:00 +08001001 if ((cmd->error &&
1002 !(cmd->error == -EILSEQ &&
1003 (cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1004 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) ||
1005 (mrq->sbc && mrq->sbc->error))
Chaotian Jing20848902015-06-15 19:20:48 +08001006 msdc_request_done(host, mrq);
1007 else if (cmd == mrq->sbc)
1008 msdc_start_command(host, mrq, mrq->cmd);
1009 else if (!cmd->data)
1010 msdc_request_done(host, mrq);
1011 else
1012 msdc_start_data(host, mrq, cmd, cmd->data);
1013}
1014
1015static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
1016{
1017 struct msdc_host *host = mmc_priv(mmc);
1018
1019 host->error = 0;
1020 WARN_ON(host->mrq);
1021 host->mrq = mrq;
1022
1023 if (mrq->data)
1024 msdc_prepare_data(host, mrq);
1025
1026 /* if SBC is required, we have HW option and SW option.
1027 * if HW option is enabled, and SBC does not have "special" flags,
1028 * use HW option, otherwise use SW option
1029 */
1030 if (mrq->sbc && (!mmc_card_mmc(mmc->card) ||
1031 (mrq->sbc->arg & 0xFFFF0000)))
1032 msdc_start_command(host, mrq, mrq->sbc);
1033 else
1034 msdc_start_command(host, mrq, mrq->cmd);
1035}
1036
Linus Walleijd3c6aac2016-11-23 11:02:24 +01001037static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Chaotian Jing20848902015-06-15 19:20:48 +08001038{
1039 struct msdc_host *host = mmc_priv(mmc);
1040 struct mmc_data *data = mrq->data;
1041
1042 if (!data)
1043 return;
1044
1045 msdc_prepare_data(host, mrq);
1046 data->host_cookie |= MSDC_ASYNC_FLAG;
1047}
1048
1049static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
1050 int err)
1051{
1052 struct msdc_host *host = mmc_priv(mmc);
1053 struct mmc_data *data;
1054
1055 data = mrq->data;
1056 if (!data)
1057 return;
1058 if (data->host_cookie) {
1059 data->host_cookie &= ~MSDC_ASYNC_FLAG;
1060 msdc_unprepare_data(host, mrq);
1061 }
1062}
1063
1064static void msdc_data_xfer_next(struct msdc_host *host,
1065 struct mmc_request *mrq, struct mmc_data *data)
1066{
1067 if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error &&
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001068 !mrq->sbc)
Chaotian Jing20848902015-06-15 19:20:48 +08001069 msdc_start_command(host, mrq, mrq->stop);
1070 else
1071 msdc_request_done(host, mrq);
1072}
1073
1074static bool msdc_data_xfer_done(struct msdc_host *host, u32 events,
1075 struct mmc_request *mrq, struct mmc_data *data)
1076{
1077 struct mmc_command *stop = data->stop;
1078 unsigned long flags;
1079 bool done;
1080 unsigned int check_data = events &
1081 (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO
1082 | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR
1083 | MSDC_INT_DMA_PROTECT);
1084
1085 spin_lock_irqsave(&host->lock, flags);
1086 done = !host->data;
1087 if (check_data)
1088 host->data = NULL;
1089 spin_unlock_irqrestore(&host->lock, flags);
1090
1091 if (done)
1092 return true;
1093
1094 if (check_data || (stop && stop->error)) {
1095 dev_dbg(host->dev, "DMA status: 0x%8X\n",
1096 readl(host->base + MSDC_DMA_CFG));
1097 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP,
1098 1);
1099 while (readl(host->base + MSDC_DMA_CFG) & MSDC_DMA_CFG_STS)
1100 cpu_relax();
1101 sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask);
1102 dev_dbg(host->dev, "DMA stop\n");
1103
1104 if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) {
1105 data->bytes_xfered = data->blocks * data->blksz;
1106 } else {
Chaotian Jing2066fd22015-12-01 20:12:34 +08001107 dev_dbg(host->dev, "interrupt events: %x\n", events);
Chaotian Jing20848902015-06-15 19:20:48 +08001108 msdc_reset_hw(host);
1109 host->error |= REQ_DAT_ERR;
1110 data->bytes_xfered = 0;
1111
1112 if (events & MSDC_INT_DATTMO)
1113 data->error = -ETIMEDOUT;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001114 else if (events & MSDC_INT_DATCRCERR)
1115 data->error = -EILSEQ;
Chaotian Jing20848902015-06-15 19:20:48 +08001116
Chaotian Jing2066fd22015-12-01 20:12:34 +08001117 dev_dbg(host->dev, "%s: cmd=%d; blocks=%d",
Chaotian Jing20848902015-06-15 19:20:48 +08001118 __func__, mrq->cmd->opcode, data->blocks);
Chaotian Jing2066fd22015-12-01 20:12:34 +08001119 dev_dbg(host->dev, "data_error=%d xfer_size=%d\n",
1120 (int)data->error, data->bytes_xfered);
Chaotian Jing20848902015-06-15 19:20:48 +08001121 }
1122
1123 msdc_data_xfer_next(host, mrq, data);
1124 done = true;
1125 }
1126 return done;
1127}
1128
1129static void msdc_set_buswidth(struct msdc_host *host, u32 width)
1130{
1131 u32 val = readl(host->base + SDC_CFG);
1132
1133 val &= ~SDC_CFG_BUSWIDTH;
1134
1135 switch (width) {
1136 default:
1137 case MMC_BUS_WIDTH_1:
1138 val |= (MSDC_BUS_1BITS << 16);
1139 break;
1140 case MMC_BUS_WIDTH_4:
1141 val |= (MSDC_BUS_4BITS << 16);
1142 break;
1143 case MMC_BUS_WIDTH_8:
1144 val |= (MSDC_BUS_8BITS << 16);
1145 break;
1146 }
1147
1148 writel(val, host->base + SDC_CFG);
1149 dev_dbg(host->dev, "Bus Width = %d", width);
1150}
1151
1152static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios)
1153{
1154 struct msdc_host *host = mmc_priv(mmc);
Chaotian Jing20848902015-06-15 19:20:48 +08001155 int ret = 0;
1156
1157 if (!IS_ERR(mmc->supply.vqmmc)) {
Nicolas Boichatfac49ce2016-03-03 18:19:45 +08001158 if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 &&
1159 ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
Chaotian Jing20848902015-06-15 19:20:48 +08001160 dev_err(host->dev, "Unsupported signal voltage!\n");
1161 return -EINVAL;
1162 }
1163
Nicolas Boichatfac49ce2016-03-03 18:19:45 +08001164 ret = mmc_regulator_set_vqmmc(mmc, ios);
Chaotian Jing20848902015-06-15 19:20:48 +08001165 if (ret) {
Nicolas Boichatfac49ce2016-03-03 18:19:45 +08001166 dev_dbg(host->dev, "Regulator set error %d (%d)\n",
1167 ret, ios->signal_voltage);
Chaotian Jing20848902015-06-15 19:20:48 +08001168 } else {
1169 /* Apply different pinctrl settings for different signal voltage */
1170 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
1171 pinctrl_select_state(host->pinctrl, host->pins_uhs);
1172 else
1173 pinctrl_select_state(host->pinctrl, host->pins_default);
1174 }
1175 }
1176 return ret;
1177}
1178
1179static int msdc_card_busy(struct mmc_host *mmc)
1180{
1181 struct msdc_host *host = mmc_priv(mmc);
1182 u32 status = readl(host->base + MSDC_PS);
1183
yong mao3bc702e2017-01-03 16:49:57 +08001184 /* only check if data0 is low */
1185 return !(status & BIT(16));
Chaotian Jing20848902015-06-15 19:20:48 +08001186}
1187
1188static void msdc_request_timeout(struct work_struct *work)
1189{
1190 struct msdc_host *host = container_of(work, struct msdc_host,
1191 req_timeout.work);
1192
1193 /* simulate HW timeout status */
1194 dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__);
1195 if (host->mrq) {
1196 dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__,
1197 host->mrq, host->mrq->cmd->opcode);
1198 if (host->cmd) {
1199 dev_err(host->dev, "%s: aborting cmd=%d\n",
1200 __func__, host->cmd->opcode);
1201 msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq,
1202 host->cmd);
1203 } else if (host->data) {
1204 dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n",
1205 __func__, host->mrq->cmd->opcode,
1206 host->data->blocks);
1207 msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq,
1208 host->data);
1209 }
1210 }
1211}
1212
1213static irqreturn_t msdc_irq(int irq, void *dev_id)
1214{
1215 struct msdc_host *host = (struct msdc_host *) dev_id;
1216
1217 while (true) {
1218 unsigned long flags;
1219 struct mmc_request *mrq;
1220 struct mmc_command *cmd;
1221 struct mmc_data *data;
1222 u32 events, event_mask;
1223
1224 spin_lock_irqsave(&host->lock, flags);
1225 events = readl(host->base + MSDC_INT);
1226 event_mask = readl(host->base + MSDC_INTEN);
1227 /* clear interrupts */
1228 writel(events & event_mask, host->base + MSDC_INT);
1229
1230 mrq = host->mrq;
1231 cmd = host->cmd;
1232 data = host->data;
1233 spin_unlock_irqrestore(&host->lock, flags);
1234
1235 if (!(events & event_mask))
1236 break;
1237
1238 if (!mrq) {
1239 dev_err(host->dev,
1240 "%s: MRQ=NULL; events=%08X; event_mask=%08X\n",
1241 __func__, events, event_mask);
1242 WARN_ON(1);
1243 break;
1244 }
1245
1246 dev_dbg(host->dev, "%s: events=%08X\n", __func__, events);
1247
1248 if (cmd)
1249 msdc_cmd_done(host, events, mrq, cmd);
1250 else if (data)
1251 msdc_data_xfer_done(host, events, mrq, data);
1252 }
1253
1254 return IRQ_HANDLED;
1255}
1256
1257static void msdc_init_hw(struct msdc_host *host)
1258{
1259 u32 val;
Chaotian Jing39add252017-10-16 09:46:32 +08001260 u32 tune_reg = host->dev_comp->pad_tune_reg;
Chaotian Jing20848902015-06-15 19:20:48 +08001261
1262 /* Configure to MMC/SD mode, clock free running */
1263 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN);
1264
1265 /* Reset */
1266 msdc_reset_hw(host);
1267
1268 /* Disable card detection */
1269 sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN);
1270
1271 /* Disable and clear all interrupts */
1272 writel(0, host->base + MSDC_INTEN);
1273 val = readl(host->base + MSDC_INT);
1274 writel(val, host->base + MSDC_INT);
1275
Chaotian Jing39add252017-10-16 09:46:32 +08001276 writel(0, host->base + tune_reg);
Chaotian Jing20848902015-06-15 19:20:48 +08001277 writel(0, host->base + MSDC_IOCON);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001278 sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0);
1279 writel(0x403c0046, host->base + MSDC_PATCH_BIT);
Chaotian Jing20848902015-06-15 19:20:48 +08001280 sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1);
Chaotian Jing2fea5812017-10-16 09:46:33 +08001281 writel(0xffff4089, host->base + MSDC_PATCH_BIT1);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001282 sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL);
Chaotian Jingacde28c2017-10-16 09:46:34 +08001283 if (host->dev_comp->busy_check)
1284 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, (1 << 7));
Chaotian Jing2fea5812017-10-16 09:46:33 +08001285 if (host->dev_comp->async_fifo) {
1286 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1287 MSDC_PB2_RESPWAIT, 3);
1288 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1289 MSDC_PB2_RESPSTSENSEL, 2);
1290 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1291 MSDC_PB2_CRCSTSENSEL, 2);
1292 /* use async fifo, then no need tune internal delay */
1293 sdr_clr_bits(host->base + MSDC_PATCH_BIT2,
1294 MSDC_PATCH_BIT2_CFGRESP);
1295 sdr_set_bits(host->base + MSDC_PATCH_BIT2,
1296 MSDC_PATCH_BIT2_CFGCRCSTS);
1297 }
1298
1299 if (host->dev_comp->data_tune) {
1300 sdr_set_bits(host->base + tune_reg,
1301 MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL);
1302 } else {
1303 /* choose clock tune */
1304 sdr_set_bits(host->base + tune_reg, MSDC_PAD_TUNE_RXDLYSEL);
1305 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001306
Chaotian Jing20848902015-06-15 19:20:48 +08001307 /* Configure to enable SDIO mode.
1308 * it's must otherwise sdio cmd5 failed
1309 */
1310 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO);
1311
1312 /* disable detect SDIO device interrupt function */
1313 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
1314
1315 /* Configure to default data timeout */
1316 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3);
1317
Chaotian Jing86beac32016-06-30 10:00:59 +08001318 host->def_tune_para.iocon = readl(host->base + MSDC_IOCON);
Chaotian Jing39add252017-10-16 09:46:32 +08001319 host->def_tune_para.pad_tune = readl(host->base + tune_reg);
Chaotian Jing2fea5812017-10-16 09:46:33 +08001320 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
1321 host->saved_tune_para.pad_tune = readl(host->base + tune_reg);
Chaotian Jing20848902015-06-15 19:20:48 +08001322 dev_dbg(host->dev, "init hardware done!");
1323}
1324
1325static void msdc_deinit_hw(struct msdc_host *host)
1326{
1327 u32 val;
1328 /* Disable and clear all interrupts */
1329 writel(0, host->base + MSDC_INTEN);
1330
1331 val = readl(host->base + MSDC_INT);
1332 writel(val, host->base + MSDC_INT);
1333}
1334
1335/* init gpd and bd list in msdc_drv_probe */
1336static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
1337{
1338 struct mt_gpdma_desc *gpd = dma->gpd;
1339 struct mt_bdma_desc *bd = dma->bd;
1340 int i;
1341
Chaotian Jing62b0d272015-10-27 14:24:25 +08001342 memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2);
Chaotian Jing20848902015-06-15 19:20:48 +08001343
1344 gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */
1345 gpd->ptr = (u32)dma->bd_addr; /* physical address */
Chaotian Jing62b0d272015-10-27 14:24:25 +08001346 /* gpd->next is must set for desc DMA
1347 * That's why must alloc 2 gpd structure.
1348 */
1349 gpd->next = (u32)dma->gpd_addr + sizeof(struct mt_gpdma_desc);
Chaotian Jing20848902015-06-15 19:20:48 +08001350 memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM);
1351 for (i = 0; i < (MAX_BD_NUM - 1); i++)
1352 bd[i].next = (u32)dma->bd_addr + sizeof(*bd) * (i + 1);
1353}
1354
1355static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1356{
1357 struct msdc_host *host = mmc_priv(mmc);
1358 int ret;
Chaotian Jing20848902015-06-15 19:20:48 +08001359
Chaotian Jing20848902015-06-15 19:20:48 +08001360 msdc_set_buswidth(host, ios->bus_width);
1361
1362 /* Suspend/Resume will do power off/on */
1363 switch (ios->power_mode) {
1364 case MMC_POWER_UP:
1365 if (!IS_ERR(mmc->supply.vmmc)) {
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001366 msdc_init_hw(host);
Chaotian Jing20848902015-06-15 19:20:48 +08001367 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1368 ios->vdd);
1369 if (ret) {
1370 dev_err(host->dev, "Failed to set vmmc power!\n");
Ulf Hansson567979f2016-03-21 14:21:25 +01001371 return;
Chaotian Jing20848902015-06-15 19:20:48 +08001372 }
1373 }
1374 break;
1375 case MMC_POWER_ON:
1376 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
1377 ret = regulator_enable(mmc->supply.vqmmc);
1378 if (ret)
1379 dev_err(host->dev, "Failed to set vqmmc power!\n");
1380 else
1381 host->vqmmc_enabled = true;
1382 }
1383 break;
1384 case MMC_POWER_OFF:
1385 if (!IS_ERR(mmc->supply.vmmc))
1386 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1387
1388 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
1389 regulator_disable(mmc->supply.vqmmc);
1390 host->vqmmc_enabled = false;
1391 }
1392 break;
1393 default:
1394 break;
1395 }
1396
Chaotian Jing6e622942015-10-27 14:24:24 +08001397 if (host->mclk != ios->clock || host->timing != ios->timing)
1398 msdc_set_mclk(host, ios->timing, ios->clock);
Chaotian Jing20848902015-06-15 19:20:48 +08001399}
1400
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001401static u32 test_delay_bit(u32 delay, u32 bit)
1402{
1403 bit %= PAD_DELAY_MAX;
1404 return delay & (1 << bit);
1405}
1406
1407static int get_delay_len(u32 delay, u32 start_bit)
1408{
1409 int i;
1410
1411 for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) {
1412 if (test_delay_bit(delay, start_bit + i) == 0)
1413 return i;
1414 }
1415 return PAD_DELAY_MAX - start_bit;
1416}
1417
1418static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay)
1419{
1420 int start = 0, len = 0;
1421 int start_final = 0, len_final = 0;
1422 u8 final_phase = 0xff;
Geert Uytterhoeven62d494c2015-11-06 12:22:08 +01001423 struct msdc_delay_phase delay_phase = { 0, };
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001424
1425 if (delay == 0) {
1426 dev_err(host->dev, "phase error: [map:%x]\n", delay);
1427 delay_phase.final_phase = final_phase;
1428 return delay_phase;
1429 }
1430
1431 while (start < PAD_DELAY_MAX) {
1432 len = get_delay_len(delay, start);
1433 if (len_final < len) {
1434 start_final = start;
1435 len_final = len;
1436 }
1437 start += len ? len : 1;
yong mao1ede5cb2017-03-15 15:26:40 +08001438 if (len >= 12 && start_final < 4)
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001439 break;
1440 }
1441
1442 /* The rule is that to find the smallest delay cell */
1443 if (start_final == 0)
1444 final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX;
1445 else
1446 final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX;
1447 dev_info(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n",
1448 delay, len_final, final_phase);
1449
1450 delay_phase.maxlen = len_final;
1451 delay_phase.start = start_final;
1452 delay_phase.final_phase = final_phase;
1453 return delay_phase;
1454}
1455
1456static int msdc_tune_response(struct mmc_host *mmc, u32 opcode)
1457{
1458 struct msdc_host *host = mmc_priv(mmc);
1459 u32 rise_delay = 0, fall_delay = 0;
Chaotian Jingae9c6572016-06-30 10:01:01 +08001460 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
yong mao1ede5cb2017-03-15 15:26:40 +08001461 struct msdc_delay_phase internal_delay_phase;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001462 u8 final_delay, final_maxlen;
yong mao1ede5cb2017-03-15 15:26:40 +08001463 u32 internal_delay = 0;
Chaotian Jing39add252017-10-16 09:46:32 +08001464 u32 tune_reg = host->dev_comp->pad_tune_reg;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001465 int cmd_err;
yong mao1ede5cb2017-03-15 15:26:40 +08001466 int i, j;
1467
1468 if (mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
1469 mmc->ios.timing == MMC_TIMING_UHS_SDR104)
Chaotian Jing39add252017-10-16 09:46:32 +08001470 sdr_set_field(host->base + tune_reg,
yong mao1ede5cb2017-03-15 15:26:40 +08001471 MSDC_PAD_TUNE_CMDRRDLY,
1472 host->hs200_cmd_int_delay);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001473
1474 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
1475 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
Chaotian Jing39add252017-10-16 09:46:32 +08001476 sdr_set_field(host->base + tune_reg,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001477 MSDC_PAD_TUNE_CMDRDLY, i);
yong mao1ede5cb2017-03-15 15:26:40 +08001478 /*
1479 * Using the same parameters, it may sometimes pass the test,
1480 * but sometimes it may fail. To make sure the parameters are
1481 * more stable, we test each set of parameters 3 times.
1482 */
1483 for (j = 0; j < 3; j++) {
1484 mmc_send_tuning(mmc, opcode, &cmd_err);
1485 if (!cmd_err) {
1486 rise_delay |= (1 << i);
1487 } else {
1488 rise_delay &= ~(1 << i);
1489 break;
1490 }
1491 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001492 }
Chaotian Jingae9c6572016-06-30 10:01:01 +08001493 final_rise_delay = get_best_delay(host, rise_delay);
1494 /* if rising edge has enough margin, then do not scan falling edge */
yong mao1ede5cb2017-03-15 15:26:40 +08001495 if (final_rise_delay.maxlen >= 12 && final_rise_delay.start < 4)
Chaotian Jingae9c6572016-06-30 10:01:01 +08001496 goto skip_fall;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001497
1498 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
1499 for (i = 0; i < PAD_DELAY_MAX; i++) {
Chaotian Jing39add252017-10-16 09:46:32 +08001500 sdr_set_field(host->base + tune_reg,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001501 MSDC_PAD_TUNE_CMDRDLY, i);
yong mao1ede5cb2017-03-15 15:26:40 +08001502 /*
1503 * Using the same parameters, it may sometimes pass the test,
1504 * but sometimes it may fail. To make sure the parameters are
1505 * more stable, we test each set of parameters 3 times.
1506 */
1507 for (j = 0; j < 3; j++) {
1508 mmc_send_tuning(mmc, opcode, &cmd_err);
1509 if (!cmd_err) {
1510 fall_delay |= (1 << i);
1511 } else {
1512 fall_delay &= ~(1 << i);
1513 break;
1514 }
1515 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001516 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001517 final_fall_delay = get_best_delay(host, fall_delay);
1518
Chaotian Jingae9c6572016-06-30 10:01:01 +08001519skip_fall:
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001520 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
yong mao1ede5cb2017-03-15 15:26:40 +08001521 if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4)
1522 final_maxlen = final_fall_delay.maxlen;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001523 if (final_maxlen == final_rise_delay.maxlen) {
1524 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
Chaotian Jing39add252017-10-16 09:46:32 +08001525 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001526 final_rise_delay.final_phase);
1527 final_delay = final_rise_delay.final_phase;
1528 } else {
1529 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
Chaotian Jing39add252017-10-16 09:46:32 +08001530 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001531 final_fall_delay.final_phase);
1532 final_delay = final_fall_delay.final_phase;
1533 }
Chaotian Jing2fea5812017-10-16 09:46:33 +08001534 if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay)
yong mao1ede5cb2017-03-15 15:26:40 +08001535 goto skip_internal;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001536
yong mao1ede5cb2017-03-15 15:26:40 +08001537 for (i = 0; i < PAD_DELAY_MAX; i++) {
Chaotian Jing39add252017-10-16 09:46:32 +08001538 sdr_set_field(host->base + tune_reg,
yong mao1ede5cb2017-03-15 15:26:40 +08001539 MSDC_PAD_TUNE_CMDRRDLY, i);
1540 mmc_send_tuning(mmc, opcode, &cmd_err);
1541 if (!cmd_err)
1542 internal_delay |= (1 << i);
1543 }
1544 dev_dbg(host->dev, "Final internal delay: 0x%x\n", internal_delay);
1545 internal_delay_phase = get_best_delay(host, internal_delay);
Chaotian Jing39add252017-10-16 09:46:32 +08001546 sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRRDLY,
yong mao1ede5cb2017-03-15 15:26:40 +08001547 internal_delay_phase.final_phase);
1548skip_internal:
1549 dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay);
1550 return final_delay == 0xff ? -EIO : 0;
1551}
1552
1553static int hs400_tune_response(struct mmc_host *mmc, u32 opcode)
1554{
1555 struct msdc_host *host = mmc_priv(mmc);
1556 u32 cmd_delay = 0;
1557 struct msdc_delay_phase final_cmd_delay = { 0,};
1558 u8 final_delay;
1559 int cmd_err;
1560 int i, j;
1561
1562 /* select EMMC50 PAD CMD tune */
1563 sdr_set_bits(host->base + PAD_CMD_TUNE, BIT(0));
1564
1565 if (mmc->ios.timing == MMC_TIMING_MMC_HS200 ||
1566 mmc->ios.timing == MMC_TIMING_UHS_SDR104)
1567 sdr_set_field(host->base + MSDC_PAD_TUNE,
1568 MSDC_PAD_TUNE_CMDRRDLY,
1569 host->hs200_cmd_int_delay);
1570
1571 if (host->hs400_cmd_resp_sel_rising)
1572 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
1573 else
1574 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL);
1575 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
1576 sdr_set_field(host->base + PAD_CMD_TUNE,
1577 PAD_CMD_TUNE_RX_DLY3, i);
1578 /*
1579 * Using the same parameters, it may sometimes pass the test,
1580 * but sometimes it may fail. To make sure the parameters are
1581 * more stable, we test each set of parameters 3 times.
1582 */
1583 for (j = 0; j < 3; j++) {
1584 mmc_send_tuning(mmc, opcode, &cmd_err);
1585 if (!cmd_err) {
1586 cmd_delay |= (1 << i);
1587 } else {
1588 cmd_delay &= ~(1 << i);
1589 break;
1590 }
1591 }
1592 }
1593 final_cmd_delay = get_best_delay(host, cmd_delay);
1594 sdr_set_field(host->base + PAD_CMD_TUNE, PAD_CMD_TUNE_RX_DLY3,
1595 final_cmd_delay.final_phase);
1596 final_delay = final_cmd_delay.final_phase;
1597
1598 dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001599 return final_delay == 0xff ? -EIO : 0;
1600}
1601
1602static int msdc_tune_data(struct mmc_host *mmc, u32 opcode)
1603{
1604 struct msdc_host *host = mmc_priv(mmc);
1605 u32 rise_delay = 0, fall_delay = 0;
Chaotian Jingae9c6572016-06-30 10:01:01 +08001606 struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,};
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001607 u8 final_delay, final_maxlen;
Chaotian Jing39add252017-10-16 09:46:32 +08001608 u32 tune_reg = host->dev_comp->pad_tune_reg;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001609 int i, ret;
1610
1611 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
1612 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
1613 for (i = 0 ; i < PAD_DELAY_MAX; i++) {
Chaotian Jing39add252017-10-16 09:46:32 +08001614 sdr_set_field(host->base + tune_reg,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001615 MSDC_PAD_TUNE_DATRRDLY, i);
1616 ret = mmc_send_tuning(mmc, opcode, NULL);
1617 if (!ret)
1618 rise_delay |= (1 << i);
1619 }
Chaotian Jingae9c6572016-06-30 10:01:01 +08001620 final_rise_delay = get_best_delay(host, rise_delay);
1621 /* if rising edge has enough margin, then do not scan falling edge */
yong mao1ede5cb2017-03-15 15:26:40 +08001622 if (final_rise_delay.maxlen >= 12 ||
Chaotian Jingae9c6572016-06-30 10:01:01 +08001623 (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4))
1624 goto skip_fall;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001625
1626 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
1627 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
1628 for (i = 0; i < PAD_DELAY_MAX; i++) {
Chaotian Jing39add252017-10-16 09:46:32 +08001629 sdr_set_field(host->base + tune_reg,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001630 MSDC_PAD_TUNE_DATRRDLY, i);
1631 ret = mmc_send_tuning(mmc, opcode, NULL);
1632 if (!ret)
1633 fall_delay |= (1 << i);
1634 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001635 final_fall_delay = get_best_delay(host, fall_delay);
1636
Chaotian Jingae9c6572016-06-30 10:01:01 +08001637skip_fall:
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001638 final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001639 if (final_maxlen == final_rise_delay.maxlen) {
1640 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
1641 sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
Chaotian Jing39add252017-10-16 09:46:32 +08001642 sdr_set_field(host->base + tune_reg,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001643 MSDC_PAD_TUNE_DATRRDLY,
1644 final_rise_delay.final_phase);
1645 final_delay = final_rise_delay.final_phase;
1646 } else {
1647 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL);
1648 sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL);
Chaotian Jing39add252017-10-16 09:46:32 +08001649 sdr_set_field(host->base + tune_reg,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001650 MSDC_PAD_TUNE_DATRRDLY,
1651 final_fall_delay.final_phase);
1652 final_delay = final_fall_delay.final_phase;
1653 }
1654
yong mao1ede5cb2017-03-15 15:26:40 +08001655 dev_dbg(host->dev, "Final data pad delay: %x\n", final_delay);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001656 return final_delay == 0xff ? -EIO : 0;
1657}
1658
1659static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode)
1660{
1661 struct msdc_host *host = mmc_priv(mmc);
1662 int ret;
Chaotian Jing39add252017-10-16 09:46:32 +08001663 u32 tune_reg = host->dev_comp->pad_tune_reg;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001664
Chaotian Jing7f3d5852017-10-16 09:46:31 +08001665 if (host->hs400_mode &&
1666 host->dev_comp->hs400_tune)
yong mao1ede5cb2017-03-15 15:26:40 +08001667 ret = hs400_tune_response(mmc, opcode);
1668 else
1669 ret = msdc_tune_response(mmc, opcode);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001670 if (ret == -EIO) {
1671 dev_err(host->dev, "Tune response fail!\n");
Ulf Hansson567979f2016-03-21 14:21:25 +01001672 return ret;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001673 }
Chaotian Jing5462ff32016-06-30 10:00:58 +08001674 if (host->hs400_mode == false) {
1675 ret = msdc_tune_data(mmc, opcode);
1676 if (ret == -EIO)
1677 dev_err(host->dev, "Tune data fail!\n");
1678 }
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001679
Chaotian Jing86beac32016-06-30 10:00:59 +08001680 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
Chaotian Jing39add252017-10-16 09:46:32 +08001681 host->saved_tune_para.pad_tune = readl(host->base + tune_reg);
yong mao1ede5cb2017-03-15 15:26:40 +08001682 host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001683 return ret;
1684}
1685
1686static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1687{
1688 struct msdc_host *host = mmc_priv(mmc);
Chaotian Jing5462ff32016-06-30 10:00:58 +08001689 host->hs400_mode = true;
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001690
1691 writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE);
Chaotian Jing2fea5812017-10-16 09:46:33 +08001692 /* hs400 mode must set it to 0 */
1693 sdr_clr_bits(host->base + MSDC_PATCH_BIT2, MSDC_PATCH_BIT2_CFGCRCSTS);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001694 return 0;
1695}
1696
Chaotian Jingc9b50612015-10-27 14:24:26 +08001697static void msdc_hw_reset(struct mmc_host *mmc)
1698{
1699 struct msdc_host *host = mmc_priv(mmc);
1700
1701 sdr_set_bits(host->base + EMMC_IOCON, 1);
1702 udelay(10); /* 10us is enough */
1703 sdr_clr_bits(host->base + EMMC_IOCON, 1);
1704}
1705
Julia Lawallbe7815d2017-07-29 07:59:40 +02001706static const struct mmc_host_ops mt_msdc_ops = {
Chaotian Jing20848902015-06-15 19:20:48 +08001707 .post_req = msdc_post_req,
1708 .pre_req = msdc_pre_req,
1709 .request = msdc_ops_request,
1710 .set_ios = msdc_ops_set_ios,
Chaotian Jing8d53e412016-02-15 02:31:00 +08001711 .get_ro = mmc_gpio_get_ro,
Chaotian Jingc7b16de2017-07-03 14:24:56 +08001712 .get_cd = mmc_gpio_get_cd,
Chaotian Jing20848902015-06-15 19:20:48 +08001713 .start_signal_voltage_switch = msdc_ops_switch_volt,
1714 .card_busy = msdc_card_busy,
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001715 .execute_tuning = msdc_execute_tuning,
1716 .prepare_hs400_tuning = msdc_prepare_hs400_tuning,
Chaotian Jingc9b50612015-10-27 14:24:26 +08001717 .hw_reset = msdc_hw_reset,
Chaotian Jing20848902015-06-15 19:20:48 +08001718};
1719
yong mao1ede5cb2017-03-15 15:26:40 +08001720static void msdc_of_property_parse(struct platform_device *pdev,
1721 struct msdc_host *host)
1722{
1723 of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay",
1724 &host->hs400_ds_delay);
1725
1726 of_property_read_u32(pdev->dev.of_node, "mediatek,hs200-cmd-int-delay",
1727 &host->hs200_cmd_int_delay);
1728
1729 of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-cmd-int-delay",
1730 &host->hs400_cmd_int_delay);
1731
1732 if (of_property_read_bool(pdev->dev.of_node,
1733 "mediatek,hs400-cmd-resp-sel-rising"))
1734 host->hs400_cmd_resp_sel_rising = true;
1735 else
1736 host->hs400_cmd_resp_sel_rising = false;
1737}
1738
Chaotian Jing20848902015-06-15 19:20:48 +08001739static int msdc_drv_probe(struct platform_device *pdev)
1740{
1741 struct mmc_host *mmc;
1742 struct msdc_host *host;
1743 struct resource *res;
Chaotian Jing762d4912017-10-16 09:46:29 +08001744 const struct of_device_id *of_id;
Chaotian Jing20848902015-06-15 19:20:48 +08001745 int ret;
1746
1747 if (!pdev->dev.of_node) {
1748 dev_err(&pdev->dev, "No DT found\n");
1749 return -EINVAL;
1750 }
Chaotian Jing762d4912017-10-16 09:46:29 +08001751
1752 of_id = of_match_node(msdc_of_ids, pdev->dev.of_node);
1753 if (!of_id)
1754 return -EINVAL;
Chaotian Jing20848902015-06-15 19:20:48 +08001755 /* Allocate MMC host for this device */
1756 mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
1757 if (!mmc)
1758 return -ENOMEM;
1759
1760 host = mmc_priv(mmc);
1761 ret = mmc_of_parse(mmc);
1762 if (ret)
1763 goto host_free;
1764
1765 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1766 host->base = devm_ioremap_resource(&pdev->dev, res);
1767 if (IS_ERR(host->base)) {
1768 ret = PTR_ERR(host->base);
1769 goto host_free;
1770 }
1771
1772 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang2f98ef62017-10-14 21:17:15 +02001773 if (ret)
Chaotian Jing20848902015-06-15 19:20:48 +08001774 goto host_free;
1775
1776 host->src_clk = devm_clk_get(&pdev->dev, "source");
1777 if (IS_ERR(host->src_clk)) {
1778 ret = PTR_ERR(host->src_clk);
1779 goto host_free;
1780 }
1781
1782 host->h_clk = devm_clk_get(&pdev->dev, "hclk");
1783 if (IS_ERR(host->h_clk)) {
1784 ret = PTR_ERR(host->h_clk);
1785 goto host_free;
1786 }
1787
1788 host->irq = platform_get_irq(pdev, 0);
1789 if (host->irq < 0) {
1790 ret = -EINVAL;
1791 goto host_free;
1792 }
1793
1794 host->pinctrl = devm_pinctrl_get(&pdev->dev);
1795 if (IS_ERR(host->pinctrl)) {
1796 ret = PTR_ERR(host->pinctrl);
1797 dev_err(&pdev->dev, "Cannot find pinctrl!\n");
1798 goto host_free;
1799 }
1800
1801 host->pins_default = pinctrl_lookup_state(host->pinctrl, "default");
1802 if (IS_ERR(host->pins_default)) {
1803 ret = PTR_ERR(host->pins_default);
1804 dev_err(&pdev->dev, "Cannot find pinctrl default!\n");
1805 goto host_free;
1806 }
1807
1808 host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs");
1809 if (IS_ERR(host->pins_uhs)) {
1810 ret = PTR_ERR(host->pins_uhs);
1811 dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n");
1812 goto host_free;
1813 }
1814
yong mao1ede5cb2017-03-15 15:26:40 +08001815 msdc_of_property_parse(pdev, host);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001816
Chaotian Jing20848902015-06-15 19:20:48 +08001817 host->dev = &pdev->dev;
Chaotian Jing762d4912017-10-16 09:46:29 +08001818 host->dev_comp = of_id->data;
Chaotian Jing20848902015-06-15 19:20:48 +08001819 host->mmc = mmc;
1820 host->src_clk_freq = clk_get_rate(host->src_clk);
1821 /* Set host parameters to mmc */
1822 mmc->ops = &mt_msdc_ops;
Chaotian Jing762d4912017-10-16 09:46:29 +08001823 if (host->dev_comp->clk_div_bits == 8)
1824 mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255);
1825 else
1826 mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 4095);
Chaotian Jing20848902015-06-15 19:20:48 +08001827
1828 mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23;
1829 /* MMC core transfer sizes tunable parameters */
1830 mmc->max_segs = MAX_BD_NUM;
1831 mmc->max_seg_size = BDMA_DESC_BUFLEN;
1832 mmc->max_blk_size = 2048;
1833 mmc->max_req_size = 512 * 1024;
1834 mmc->max_blk_count = mmc->max_req_size / 512;
1835 host->dma_mask = DMA_BIT_MASK(32);
1836 mmc_dev(mmc)->dma_mask = &host->dma_mask;
1837
1838 host->timeout_clks = 3 * 1048576;
1839 host->dma.gpd = dma_alloc_coherent(&pdev->dev,
Chaotian Jing62b0d272015-10-27 14:24:25 +08001840 2 * sizeof(struct mt_gpdma_desc),
Chaotian Jing20848902015-06-15 19:20:48 +08001841 &host->dma.gpd_addr, GFP_KERNEL);
1842 host->dma.bd = dma_alloc_coherent(&pdev->dev,
1843 MAX_BD_NUM * sizeof(struct mt_bdma_desc),
1844 &host->dma.bd_addr, GFP_KERNEL);
1845 if (!host->dma.gpd || !host->dma.bd) {
1846 ret = -ENOMEM;
1847 goto release_mem;
1848 }
1849 msdc_init_gpd_bd(host, &host->dma);
1850 INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout);
1851 spin_lock_init(&host->lock);
1852
1853 platform_set_drvdata(pdev, mmc);
1854 msdc_ungate_clock(host);
1855 msdc_init_hw(host);
1856
1857 ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq,
1858 IRQF_TRIGGER_LOW | IRQF_ONESHOT, pdev->name, host);
1859 if (ret)
1860 goto release;
1861
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001862 pm_runtime_set_active(host->dev);
1863 pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY);
1864 pm_runtime_use_autosuspend(host->dev);
1865 pm_runtime_enable(host->dev);
Chaotian Jing20848902015-06-15 19:20:48 +08001866 ret = mmc_add_host(mmc);
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001867
Chaotian Jing20848902015-06-15 19:20:48 +08001868 if (ret)
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001869 goto end;
Chaotian Jing20848902015-06-15 19:20:48 +08001870
1871 return 0;
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001872end:
1873 pm_runtime_disable(host->dev);
Chaotian Jing20848902015-06-15 19:20:48 +08001874release:
1875 platform_set_drvdata(pdev, NULL);
1876 msdc_deinit_hw(host);
1877 msdc_gate_clock(host);
1878release_mem:
1879 if (host->dma.gpd)
1880 dma_free_coherent(&pdev->dev,
Chaotian Jing62b0d272015-10-27 14:24:25 +08001881 2 * sizeof(struct mt_gpdma_desc),
Chaotian Jing20848902015-06-15 19:20:48 +08001882 host->dma.gpd, host->dma.gpd_addr);
1883 if (host->dma.bd)
1884 dma_free_coherent(&pdev->dev,
1885 MAX_BD_NUM * sizeof(struct mt_bdma_desc),
1886 host->dma.bd, host->dma.bd_addr);
1887host_free:
1888 mmc_free_host(mmc);
1889
1890 return ret;
1891}
1892
1893static int msdc_drv_remove(struct platform_device *pdev)
1894{
1895 struct mmc_host *mmc;
1896 struct msdc_host *host;
1897
1898 mmc = platform_get_drvdata(pdev);
1899 host = mmc_priv(mmc);
1900
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001901 pm_runtime_get_sync(host->dev);
1902
Chaotian Jing20848902015-06-15 19:20:48 +08001903 platform_set_drvdata(pdev, NULL);
1904 mmc_remove_host(host->mmc);
1905 msdc_deinit_hw(host);
1906 msdc_gate_clock(host);
1907
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001908 pm_runtime_disable(host->dev);
1909 pm_runtime_put_noidle(host->dev);
Chaotian Jing20848902015-06-15 19:20:48 +08001910 dma_free_coherent(&pdev->dev,
Phong LE16f2e0c2017-05-24 09:53:52 +02001911 2 * sizeof(struct mt_gpdma_desc),
Chaotian Jing20848902015-06-15 19:20:48 +08001912 host->dma.gpd, host->dma.gpd_addr);
1913 dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc),
1914 host->dma.bd, host->dma.bd_addr);
1915
1916 mmc_free_host(host->mmc);
1917
1918 return 0;
1919}
1920
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001921#ifdef CONFIG_PM
1922static void msdc_save_reg(struct msdc_host *host)
1923{
Chaotian Jing39add252017-10-16 09:46:32 +08001924 u32 tune_reg = host->dev_comp->pad_tune_reg;
1925
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001926 host->save_para.msdc_cfg = readl(host->base + MSDC_CFG);
1927 host->save_para.iocon = readl(host->base + MSDC_IOCON);
1928 host->save_para.sdc_cfg = readl(host->base + SDC_CFG);
Chaotian Jing39add252017-10-16 09:46:32 +08001929 host->save_para.pad_tune = readl(host->base + tune_reg);
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001930 host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT);
1931 host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1);
Chaotian Jing2fea5812017-10-16 09:46:33 +08001932 host->save_para.patch_bit2 = readl(host->base + MSDC_PATCH_BIT2);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001933 host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE);
yong mao1ede5cb2017-03-15 15:26:40 +08001934 host->save_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001935 host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0);
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001936}
1937
1938static void msdc_restore_reg(struct msdc_host *host)
1939{
Chaotian Jing39add252017-10-16 09:46:32 +08001940 u32 tune_reg = host->dev_comp->pad_tune_reg;
1941
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001942 writel(host->save_para.msdc_cfg, host->base + MSDC_CFG);
1943 writel(host->save_para.iocon, host->base + MSDC_IOCON);
1944 writel(host->save_para.sdc_cfg, host->base + SDC_CFG);
Chaotian Jing39add252017-10-16 09:46:32 +08001945 writel(host->save_para.pad_tune, host->base + tune_reg);
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001946 writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT);
1947 writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1);
Chaotian Jing2fea5812017-10-16 09:46:33 +08001948 writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001949 writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE);
yong mao1ede5cb2017-03-15 15:26:40 +08001950 writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE);
Chaotian Jing6397b7f2015-10-27 14:24:29 +08001951 writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0);
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001952}
1953
1954static int msdc_runtime_suspend(struct device *dev)
1955{
1956 struct mmc_host *mmc = dev_get_drvdata(dev);
1957 struct msdc_host *host = mmc_priv(mmc);
1958
1959 msdc_save_reg(host);
1960 msdc_gate_clock(host);
1961 return 0;
1962}
1963
1964static int msdc_runtime_resume(struct device *dev)
1965{
1966 struct mmc_host *mmc = dev_get_drvdata(dev);
1967 struct msdc_host *host = mmc_priv(mmc);
1968
1969 msdc_ungate_clock(host);
1970 msdc_restore_reg(host);
1971 return 0;
1972}
1973#endif
1974
1975static const struct dev_pm_ops msdc_dev_pm_ops = {
1976 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1977 pm_runtime_force_resume)
1978 SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL)
1979};
1980
Chaotian Jing20848902015-06-15 19:20:48 +08001981static struct platform_driver mt_msdc_driver = {
1982 .probe = msdc_drv_probe,
1983 .remove = msdc_drv_remove,
1984 .driver = {
1985 .name = "mtk-msdc",
1986 .of_match_table = msdc_of_ids,
Chaotian Jing4b8a43e2015-06-15 19:20:49 +08001987 .pm = &msdc_dev_pm_ops,
Chaotian Jing20848902015-06-15 19:20:48 +08001988 },
1989};
1990
1991module_platform_driver(mt_msdc_driver);
1992MODULE_LICENSE("GPL v2");
1993MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver");