blob: e491e3f915dc8e46e04bb3602a045f11dd072955 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2014-2021 MediaTek Inc.
4 * Author: Chaotian.Jing <chaotian.jing@mediatek.com>
5 */
6#include <linux/module.h>
7#include <linux/clk.h>
8#include <linux/delay.h>
9#include <linux/dma-mapping.h>
10#include <linux/ioport.h>
11#include <linux/irq.h>
12#include <linux/of_address.h>
13#include <linux/of_irq.h>
14#include <linux/of_gpio.h>
15#include <linux/pinctrl/consumer.h>
16#include <linux/platform_device.h>
17#include <linux/pm.h>
18#include <linux/pm_runtime.h>
19#include <linux/regulator/consumer.h>
20#include <linux/slab.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23
24#include <linux/mmc/card.h>
25#include <linux/mmc/core.h>
26#include <linux/mmc/host.h>
27#include <linux/mmc/mmc.h>
28#include <linux/mmc/sd.h>
29#include <linux/mmc/sdio.h>
30#include <linux/mmc/slot-gpio.h>
31
32#include <mtk-sdio.h>
33
34static const struct mtk_mmc_compatible msdc_sdio_compat = {
35 .clk_div_bits = 12,
36 .pad_tune_reg = MSDC_PAD_TUNE0,
37 .async_fifo = true,
38 .data_tune = true,
39 .busy_check = true,
40 .stop_clk_fix = true,
41 .enhance_rx = true,
42 .support_64g = false,
43 .fix_200m = true,
44};
45
46static const struct mtk_mmc_compatible mt2731_sdio_compat = {
47 .clk_div_bits = 12,
48 .pad_tune_reg = MSDC_PAD_TUNE0,
49 .async_fifo = true,
50 .data_tune = true,
51 .busy_check = true,
52 .stop_clk_fix = true,
53 .enhance_rx = true,
54 .support_64g = true,
55 .fix_200m = false,
56};
57
58static const struct of_device_id msdc_of_ids[] = {
59 { .compatible = "mediatek,mt8167-sdio",
60 .data = &msdc_sdio_compat
61 },
62 { .compatible = "mediatek,mt2712-sdio",
63 .data = &msdc_sdio_compat
64 },
65 { .compatible = "mediatek,mt2731-sdio",
66 .data = &mt2731_sdio_compat
67 },
68 {}
69};
70MODULE_DEVICE_TABLE(of, msdc_of_ids);
71
72static void sdr_set_bits(void __iomem *reg, u32 bs)
73{
74 u32 val = readl(reg);
75
76 val |= bs;
77 writel(val, reg);
78}
79
80static void sdr_clr_bits(void __iomem *reg, u32 bs)
81{
82 u32 val = readl(reg);
83
84 val &= ~bs;
85 writel(val, reg);
86}
87
88static void sdr_set_field(void __iomem *reg, u32 field, u32 val)
89{
90 unsigned int tv = readl(reg);
91
92 tv &= ~field;
93 tv |= ((val) << (ffs((unsigned int)field) - 1));
94 writel(tv, reg);
95}
96
97static void sdr_get_field(void __iomem *reg, u32 field, u32 *val)
98{
99 unsigned int tv = readl(reg);
100
101 *val = ((tv & field) >> (ffs((unsigned int)field) - 1));
102}
103
104static void msdc_reset_hw(struct msdc_host *host)
105{
106 u32 val;
107
108 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST);
109 while (readl(host->base + MSDC_CFG) & MSDC_CFG_RST)
110 cpu_relax();
111
112 sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR);
113 while (readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_CLR)
114 cpu_relax();
115
116 val = readl(host->base + MSDC_INT);
117 writel(val, host->base + MSDC_INT);
118}
119
120static void msdc_cmd_next(struct msdc_host *host,
121 struct mmc_request *mrq, struct mmc_command *cmd);
122static void msdc_recheck_sdio_irq(struct msdc_host *host);
123
124static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR |
125 MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY |
126 MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO;
127static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO |
128 MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR |
129 MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT;
130
131static u8 msdc_dma_calcs(u8 *buf, u32 len)
132{
133 u32 i, sum = 0;
134
135 for (i = 0; i < len; i++)
136 sum += buf[i];
137 return 0xff - (u8) sum;
138}
139
140static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma,
141 struct mmc_data *data)
142{
143 unsigned int j, dma_len;
144 dma_addr_t dma_address;
145 u32 dma_ctrl;
146 struct scatterlist *sg;
147 struct mt_gpdma_desc *gpd;
148 struct mt_bdma_desc *bd;
149
150 sg = data->sg;
151
152 gpd = dma->gpd;
153 bd = dma->bd;
154
155 /* modify gpd */
156 gpd->gpd_info |= GPDMA_DESC_HWO;
157 gpd->gpd_info |= GPDMA_DESC_BDP;
158 /* need to clear first. use these bits to calc checksum */
159 gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM;
160 gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8;
161
162 /* modify bd */
163 for_each_sg(data->sg, sg, data->sg_count, j) {
164 dma_address = sg_dma_address(sg);
165 dma_len = sg_dma_len(sg);
166
167 /* init bd */
168 bd[j].bd_info &= ~BDMA_DESC_BLKPAD;
169 bd[j].bd_info &= ~BDMA_DESC_DWPAD;
170 bd[j].ptr = (u32)dma_address;
171 bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN;
172 bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN);
173
174 if (j == data->sg_count - 1) /* the last bd */
175 bd[j].bd_info |= BDMA_DESC_EOL;
176 else
177 bd[j].bd_info &= ~BDMA_DESC_EOL;
178
179 /* checksume need to clear first */
180 bd[j].bd_info &= ~BDMA_DESC_CHECKSUM;
181 bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8;
182 }
183
184 sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1);
185 dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL);
186 dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE);
187 dma_ctrl |= (MSDC_BURST_64B << 12 | 1 << 8);
188 writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL);
189 writel((u32)dma->gpd_addr, host->base + MSDC_DMA_SA);
190}
191
192static void msdc_prepare_data(struct msdc_host *host, struct mmc_request *mrq)
193{
194 struct mmc_data *data = mrq->data;
195
196 if (!(data->host_cookie & MSDC_PREPARE_FLAG)) {
197 bool read = (data->flags & MMC_DATA_READ) != 0;
198
199 data->host_cookie |= MSDC_PREPARE_FLAG;
200 data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len,
201 read ? DMA_FROM_DEVICE :
202 DMA_TO_DEVICE);
203 }
204}
205
206static void msdc_unprepare_data(struct msdc_host *host, struct mmc_request *mrq)
207{
208 struct mmc_data *data = mrq->data;
209
210 if (data->host_cookie & MSDC_ASYNC_FLAG)
211 return;
212
213 if (data->host_cookie & MSDC_PREPARE_FLAG) {
214 bool read = (data->flags & MMC_DATA_READ) != 0;
215
216 dma_unmap_sg(host->dev, data->sg, data->sg_len,
217 read ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
218 data->host_cookie &= ~MSDC_PREPARE_FLAG;
219 }
220}
221
222/* clock control primitives */
223static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks)
224{
225 u32 timeout, clk_ns;
226 u32 mode = 0;
227
228 host->timeout_ns = ns;
229 host->timeout_clks = clks;
230 if (host->sclk == 0) {
231 timeout = 0;
232 } else {
233 clk_ns = 1000000000UL / host->sclk;
234 timeout = (ns + clk_ns - 1) / clk_ns + clks;
235 /* in 1048576 sclk cycle unit */
236 timeout = (timeout + (0x1 << 20) - 1) >> 20;
237 sdr_get_field(host->base + MSDC_CFG, MSDC_CFG_CKMOD, &mode);
238 /*DDR mode will double the clk cycles for data timeout */
239 timeout = mode >= 2 ? timeout * 2 : timeout;
240 timeout = timeout > 1 ? timeout - 1 : 0;
241 timeout = timeout > 255 ? 255 : timeout;
242 }
243 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, timeout);
244}
245
246static void msdc_gate_clock(struct msdc_host *host)
247{
248 clk_disable_unprepare(host->src_clk_cg);
249 clk_disable_unprepare(host->src_clk);
250 clk_disable_unprepare(host->bus_clk);
251 clk_disable_unprepare(host->h_clk);
252
253 host->sdio_clk_cnt--;
254 if (!host->sdio_clk_cnt)
255 host->clock_on = false;
256}
257
258static void msdc_ungate_clock(struct msdc_host *host)
259{
260 clk_prepare_enable(host->h_clk);
261 clk_prepare_enable(host->bus_clk);
262 clk_prepare_enable(host->src_clk);
263 clk_prepare_enable(host->src_clk_cg);
264 while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
265 cpu_relax();
266 host->clock_on = true;
267 host->sdio_clk_cnt++;
268}
269
270static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz)
271{
272 u32 mode;
273 u32 flags;
274 u32 div;
275 u32 sclk;
276 u32 tune_reg = host->dev_comp->pad_tune_reg;
277 unsigned long irq_flags;
278
279 if (!hz) {
280 dev_info(host->dev, "set mclk to 0\n");
281 host->mclk = 0;
282 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
283 return;
284 }
285
286 if (hz >= 100 * 1000 * 1000 && sdio_online_tune_fail)
287 hz = 50 * 1000 * 1000;
288
289 spin_lock_irqsave(&host->irqlock, irq_flags);
290 flags = readl(host->base + MSDC_INTEN);
291 sdr_clr_bits(host->base + MSDC_INTEN, flags);
292 spin_unlock_irqrestore(&host->irqlock, irq_flags);
293
294 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE);
295 if (timing == MMC_TIMING_UHS_DDR50 ||
296 timing == MMC_TIMING_MMC_DDR52 ||
297 timing == MMC_TIMING_MMC_HS400) {
298 if (timing == MMC_TIMING_MMC_HS400)
299 mode = 0x3;
300 else
301 mode = 0x2; /* ddr mode and use divisor */
302
303 if (hz >= (host->src_clk_freq >> 2)) {
304 div = 0; /* mean div = 1/4 */
305 sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */
306 } else {
307 div = (host->src_clk_freq + ((hz << 2) - 1)) /
308 (hz << 2);
309 sclk = (host->src_clk_freq >> 2) / div;
310 div = (div >> 1);
311 }
312
313 if (timing == MMC_TIMING_MMC_HS400 &&
314 hz >= (host->src_clk_freq >> 1)) {
315 sdr_set_bits(host->base + MSDC_CFG,
316 MSDC_CFG_HS400_CK_MODE);
317 sclk = host->src_clk_freq >> 1;
318 div = 0; /* div is ignore when bit18 is set */
319 }
320 } else if (hz >= host->src_clk_freq) {
321 mode = 0x1; /* no divisor */
322 div = 0;
323 sclk = host->src_clk_freq;
324 } else {
325 mode = 0x0; /* use divisor */
326 if (hz >= (host->src_clk_freq >> 1)) {
327 div = 0; /* mean div = 1/2 */
328 sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */
329 } else {
330 div = (host->src_clk_freq + ((hz << 2) - 1)) /
331 (hz << 2);
332 sclk = (host->src_clk_freq >> 2) / div;
333 }
334 }
335 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
336 /*
337 * As src_clk/HCLK use the same bit to gate/ungate,
338 * So if want to only gate src_clk, need gate its parent(mux).
339 */
340 if (host->src_clk_cg)
341 clk_disable_unprepare(host->src_clk_cg);
342 else
343 clk_disable_unprepare(clk_get_parent(host->src_clk));
344 sdr_set_field(host->base + MSDC_CFG, MSDC_CFG_CKMOD | MSDC_CFG_CKDIV,
345 (mode << 12) | div);
346 if (host->src_clk_cg)
347 clk_prepare_enable(host->src_clk_cg);
348 else
349 clk_prepare_enable(clk_get_parent(host->src_clk));
350
351 while (!(readl(host->base + MSDC_CFG) & MSDC_CFG_CKSTB))
352 cpu_relax();
353 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN);
354 host->sclk = sclk;
355 host->mclk = hz;
356 host->timing = timing;
357 /* need because clk changed. */
358 msdc_set_timeout(host, host->timeout_ns, host->timeout_clks);
359
360 spin_lock_irqsave(&host->irqlock, irq_flags);
361 sdr_set_bits(host->base + MSDC_INTEN, flags);
362 spin_unlock_irqrestore(&host->irqlock, irq_flags);
363
364 if (host->sclk <= 52000000) {
365 sdr_set_field(host->base + MSDC_PATCH_BIT1,
366 MSDC_PATCH_BIT1_WRDAT_CRCS, 0x1);
367 sdr_set_field(host->base + MSDC_PATCH_BIT1,
368 MSDC_PATCH_BIT1_CMD_RSP, 0x1);
369 sdr_set_field(host->base + MSDC_PATCH_BIT0,
370 MSDC_PB0_RD_DAT_SEL, 0x1);
371 sdr_set_field(host->base + MSDC_PATCH_BIT0,
372 MSDC_CKGEN_MSDC_DLY_SEL, 0x4);
373
374 /* add for top_base */
375 writel(host->def_tune_para.iocon, host->base + MSDC_IOCON);
376 writel(host->def_tune_para.pad_tune, host->base + tune_reg);
377 if (host->top_base) {
378 writel(host->def_tune_para.emmc_top_control,
379 host->top_base + EMMC_TOP_CONTROL);
380 writel(host->def_tune_para.emmc_top_cmd,
381 host->top_base + EMMC_TOP_CMD);
382 }
383 } else {
384 sdr_set_field(host->base + MSDC_PATCH_BIT1,
385 MSDC_PATCH_BIT1_WRDAT_CRCS, 0x2);
386 sdr_set_field(host->base + MSDC_PATCH_BIT1,
387 MSDC_PATCH_BIT1_CMD_RSP, 0x4);
388 /* add for top_base */
389 writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON);
390 writel(host->saved_tune_para.pad_tune, host->base + tune_reg);
391 writel(host->saved_tune_para.pad_cmd_tune,
392 host->base + PAD_CMD_TUNE);
393 if (host->top_base) {
394 writel(host->saved_tune_para.emmc_top_control,
395 host->top_base + EMMC_TOP_CONTROL);
396 writel(host->saved_tune_para.emmc_top_cmd,
397 host->top_base + EMMC_TOP_CMD);
398 }
399 }
400
401 dev_info(host->dev, "sclk: %d, timing: %d hz:%d cfg:0x%x\n", host->sclk,
402 timing, hz, readl(host->base + MSDC_CFG));
403}
404
405static inline u32 msdc_cmd_find_resp(struct msdc_host *host,
406 struct mmc_request *mrq, struct mmc_command *cmd)
407{
408 u32 resp;
409
410 switch (mmc_resp_type(cmd)) {
411 /* Actually, R1, R5, R6, R7 are the same */
412 case MMC_RSP_R1:
413 resp = 0x1;
414 break;
415 case MMC_RSP_R1B:
416 resp = 0x7;
417 break;
418 case MMC_RSP_R2:
419 resp = 0x2;
420 break;
421 case MMC_RSP_R3:
422 resp = 0x3;
423 break;
424 case MMC_RSP_NONE:
425 default:
426 resp = 0x0;
427 break;
428 }
429
430 return resp;
431}
432
433static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host,
434 struct mmc_request *mrq, struct mmc_command *cmd)
435{
436 /* rawcmd :
437 * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 |
438 * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode
439 */
440 u32 opcode = cmd->opcode;
441 u32 resp = msdc_cmd_find_resp(host, mrq, cmd);
442 u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7);
443
444 host->cmd_rsp = resp;
445
446 if ((opcode == SD_IO_RW_DIRECT &&
447 ((cmd->arg >> 9) & 0x1ffff) == SDIO_CCCR_ABORT) ||
448 opcode == MMC_STOP_TRANSMISSION)
449 rawcmd |= (0x1 << 14);
450 else if (opcode == SD_SWITCH_VOLTAGE)
451 rawcmd |= (0x1 << 30);
452 else if (opcode == SD_APP_SEND_SCR ||
453 opcode == SD_APP_SEND_NUM_WR_BLKS ||
454 (opcode == SD_SWITCH &&
455 mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
456 (opcode == SD_APP_SD_STATUS &&
457 mmc_cmd_type(cmd) == MMC_CMD_ADTC) ||
458 (opcode == MMC_SEND_EXT_CSD &&
459 mmc_cmd_type(cmd) == MMC_CMD_ADTC))
460 rawcmd |= (0x1 << 11);
461
462 if (cmd->data) {
463 struct mmc_data *data = cmd->data;
464
465 if (mmc_op_multi(opcode)) {
466 if (mmc_card_mmc(host->mmc->card) && mrq->sbc &&
467 !(mrq->sbc->arg & 0xFFFF0000))
468 rawcmd |= 0x2 << 28; /* AutoCMD23 */
469 }
470
471 rawcmd |= ((data->blksz & 0xFFF) << 16);
472 if (data->flags & MMC_DATA_WRITE)
473 rawcmd |= (0x1 << 13);
474 if (data->blocks > 1)
475 rawcmd |= (0x2 << 11);
476 else
477 rawcmd |= (0x1 << 11);
478 /* Always use dma mode */
479 sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO);
480
481 if (host->timeout_ns != data->timeout_ns ||
482 host->timeout_clks != data->timeout_clks)
483 msdc_set_timeout(host, data->timeout_ns,
484 data->timeout_clks);
485
486 writel(data->blocks, host->base + SDC_BLK_NUM);
487 }
488 return rawcmd;
489}
490
491static void msdc_start_data(struct msdc_host *host, struct mmc_request *mrq,
492 struct mmc_command *cmd, struct mmc_data *data)
493{
494 unsigned long flags;
495 bool read;
496
497 WARN_ON(host->data);
498 host->data = data;
499 read = data->flags & MMC_DATA_READ;
500
501 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
502 msdc_dma_setup(host, &host->dma, data);
503
504 spin_lock_irqsave(&host->irqlock, flags);
505 sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask);
506 sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1);
507 spin_unlock_irqrestore(&host->irqlock, flags);
508
509 dev_dbg(host->dev, "DMA start\n");
510 dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n",
511 __func__, cmd->opcode, data->blocks, read);
512}
513
514static int msdc_auto_cmd_done(struct msdc_host *host, int events,
515 struct mmc_command *cmd)
516{
517 u32 *rsp = cmd->resp;
518
519 rsp[0] = readl(host->base + SDC_ACMD_RESP);
520
521 if (events & MSDC_INT_ACMDRDY) {
522 cmd->error = 0;
523 } else {
524 msdc_reset_hw(host);
525 if (events & MSDC_INT_ACMDCRCERR) {
526 cmd->error = -EILSEQ;
527 host->error |= REQ_STOP_EIO;
528 } else if (events & MSDC_INT_ACMDTMO) {
529 cmd->error = -ETIMEDOUT;
530 host->error |= REQ_STOP_TMO;
531 }
532 dev_info(host->dev,
533 "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n",
534 __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error);
535 }
536 return cmd->error;
537}
538
539static void msdc_track_cmd_data(struct msdc_host *host,
540 struct mmc_command *cmd, struct mmc_data *data)
541{
542 if (host->error)
543 dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n",
544 __func__, cmd->opcode, cmd->arg, host->error);
545}
546
547static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq)
548{
549 unsigned long flags;
550 bool ret;
551
552 ret = cancel_delayed_work(&host->req_timeout);
553 if (!ret && in_interrupt()) {
554 /* delay work already running */
555 return;
556 }
557 spin_lock_irqsave(&host->lock, flags);
558 host->mrq = NULL;
559 spin_unlock_irqrestore(&host->lock, flags);
560
561 msdc_track_cmd_data(host, mrq->cmd, mrq->data);
562 if (mrq->data)
563 msdc_unprepare_data(host, mrq);
564 mmc_request_done(host->mmc, mrq);
565 msdc_recheck_sdio_irq(host);
566}
567
568/* returns true if command is fully handled; returns false otherwise */
569static bool msdc_cmd_done(struct msdc_host *host, int events,
570 struct mmc_request *mrq, struct mmc_command *cmd)
571{
572 bool done = false;
573 bool sbc_error;
574 unsigned long flags;
575 u32 *rsp = cmd->resp;
576
577 if (mrq->sbc && cmd == mrq->cmd &&
578 (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR
579 | MSDC_INT_ACMDTMO)))
580 msdc_auto_cmd_done(host, events, mrq->sbc);
581
582 sbc_error = mrq->sbc && mrq->sbc->error;
583
584 if (!sbc_error && !(events & (MSDC_INT_CMDRDY
585 | MSDC_INT_RSPCRCERR
586 | MSDC_INT_CMDTMO)))
587 return done;
588
589 done = !host->cmd;
590 spin_lock_irqsave(&host->lock, flags);
591 host->cmd = NULL;
592 spin_unlock_irqrestore(&host->lock, flags);
593
594 if (done)
595 return true;
596
597 spin_lock_irqsave(&host->irqlock, flags);
598 sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask);
599 spin_unlock_irqrestore(&host->irqlock, flags);
600
601 if (cmd->flags & MMC_RSP_PRESENT) {
602 if (cmd->flags & MMC_RSP_136) {
603 rsp[0] = readl(host->base + SDC_RESP3);
604 rsp[1] = readl(host->base + SDC_RESP2);
605 rsp[2] = readl(host->base + SDC_RESP1);
606 rsp[3] = readl(host->base + SDC_RESP0);
607 } else {
608 rsp[0] = readl(host->base + SDC_RESP0);
609 }
610 }
611
612 if (!sbc_error && !(events & MSDC_INT_CMDRDY)) {
613 if (cmd->opcode != MMC_SEND_TUNING_BLOCK &&
614 cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)
615 /*
616 * should not clear fifo/interrupt as the tune data
617 * may have alreay come.
618 */
619 msdc_reset_hw(host);
620 if (events & MSDC_INT_RSPCRCERR) {
621 cmd->error = -EILSEQ;
622 host->error |= REQ_CMD_EIO;
623 } else if (events & MSDC_INT_CMDTMO) {
624 cmd->error = -ETIMEDOUT;
625 host->error |= REQ_CMD_TMO;
626 }
627 }
628 if (cmd->error)
629 dev_dbg(host->dev,
630 "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n",
631 __func__, cmd->opcode, cmd->arg, rsp[0],
632 cmd->error);
633
634 msdc_cmd_next(host, mrq, cmd);
635 return true;
636}
637
638static int msdc_card_busy(struct mmc_host *mmc)
639{
640 struct msdc_host *host = mmc_priv(mmc);
641 u32 status = readl(host->base + MSDC_PS);
642
643 /* only check if data0 is low */
644 return !(status & BIT(16));
645}
646
647/* It is the core layer's responsibility to ensure card status
648 * is correct before issue a request. but host design do below
649 * checks recommended.
650 */
651static inline bool msdc_cmd_is_ready(struct msdc_host *host,
652 struct mmc_request *mrq, struct mmc_command *cmd)
653{
654 /* The max busy time we can endure is 20ms */
655 unsigned long tmo = jiffies + msecs_to_jiffies(20);
656 u32 count = 0;
657
658 if (in_interrupt()) {
659 while ((readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) &&
660 (count < 1000)) {
661 udelay(1);
662 count++;
663 }
664 } else {
665 while ((readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) &&
666 time_before(jiffies, tmo))
667 cpu_relax();
668 }
669
670 if (readl(host->base + SDC_STS) & SDC_STS_CMDBUSY) {
671 dev_info(host->dev, "CMD bus busy detected\n");
672 host->error |= REQ_CMD_BUSY;
673 msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd);
674 return false;
675 }
676
677 if (cmd->opcode != MMC_SEND_STATUS) {
678 count = 0;
679 /* Consider that CMD6 crc error before card was init done,
680 * mmc_retune() will return directly as host->card is null.
681 * and CMD6 will retry 3 times, must ensure card is in transfer
682 * state when retry.
683 */
684 tmo = jiffies + msecs_to_jiffies(60 * 1000);
685 while (1) {
686 if (msdc_card_busy(host->mmc)) {
687 if (in_interrupt()) {
688 udelay(1);
689 count++;
690 } else {
691 msleep_interruptible(10);
692 }
693 } else {
694 break;
695 }
696 /* Timeout if the device never
697 * leaves the program state.
698 */
699 if (count > 1000 || time_after(jiffies, tmo)) {
700 pr_info("%s: Card is in programming state!\n",
701 mmc_hostname(host->mmc));
702 host->error |= REQ_CMD_BUSY;
703 msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd);
704 return false;
705 }
706 }
707 }
708 return true;
709}
710
711static void msdc_start_command(struct msdc_host *host,
712 struct mmc_request *mrq, struct mmc_command *cmd)
713{
714 unsigned long flags;
715 u32 rawcmd;
716
717 WARN_ON(host->cmd);
718 host->cmd = cmd;
719
720 mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT);
721 if (!msdc_cmd_is_ready(host, mrq, cmd))
722 return;
723
724 if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 ||
725 readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) {
726 dev_info(host->dev,
727 "TX/RX FIFO non-empty before start of IO. Reset\n");
728 msdc_reset_hw(host);
729 }
730
731 cmd->error = 0;
732 rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd);
733
734 spin_lock_irqsave(&host->irqlock, flags);
735 sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask);
736 spin_unlock_irqrestore(&host->irqlock, flags);
737
738 writel(cmd->arg, host->base + SDC_ARG);
739 writel(rawcmd, host->base + SDC_CMD);
740
741}
742
743static void msdc_cmd_next(struct msdc_host *host,
744 struct mmc_request *mrq, struct mmc_command *cmd)
745{
746 if ((cmd->error &&
747 !(cmd->error == -EILSEQ &&
748 (cmd->opcode == MMC_SEND_TUNING_BLOCK ||
749 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200))) ||
750 (mrq->sbc && mrq->sbc->error))
751 msdc_request_done(host, mrq);
752 else if (cmd == mrq->sbc)
753 msdc_start_command(host, mrq, mrq->cmd);
754 else if (!cmd->data)
755 msdc_request_done(host, mrq);
756 else
757 msdc_start_data(host, mrq, cmd, cmd->data);
758}
759
760static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq)
761{
762 struct msdc_host *host = mmc_priv(mmc);
763
764 host->error = 0;
765 WARN_ON(host->mrq);
766 host->mrq = mrq;
767
768 if (mrq->data)
769 msdc_prepare_data(host, mrq);
770
771 /* if SBC is required, we have HW option and SW option.
772 * if HW option is enabled, and SBC does not have "special" flags,
773 * use HW option, otherwise use SW option
774 */
775 if (mrq->sbc && (!mmc_card_mmc(mmc->card) ||
776 (mrq->sbc->arg & 0xFFFF0000)))
777 msdc_start_command(host, mrq, mrq->sbc);
778 else
779 msdc_start_command(host, mrq, mrq->cmd);
780}
781
782static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
783{
784 struct msdc_host *host = mmc_priv(mmc);
785 struct mmc_data *data = mrq->data;
786
787 if (!data)
788 return;
789
790 msdc_prepare_data(host, mrq);
791 data->host_cookie |= MSDC_ASYNC_FLAG;
792}
793
794static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
795 int err)
796{
797 struct msdc_host *host = mmc_priv(mmc);
798 struct mmc_data *data;
799
800 data = mrq->data;
801 if (!data)
802 return;
803 if (data->host_cookie) {
804 data->host_cookie &= ~MSDC_ASYNC_FLAG;
805 msdc_unprepare_data(host, mrq);
806 }
807}
808
809static void msdc_data_xfer_next(struct msdc_host *host,
810 struct mmc_request *mrq, struct mmc_data *data)
811{
812 if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error &&
813 !mrq->sbc)
814 msdc_start_command(host, mrq, mrq->stop);
815 else
816 msdc_request_done(host, mrq);
817}
818
819static bool msdc_data_xfer_done(struct msdc_host *host, u32 events,
820 struct mmc_request *mrq, struct mmc_data *data)
821{
822 struct mmc_command *stop = data->stop;
823 unsigned long flags;
824 bool done;
825 unsigned int check_data = events &
826 (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO
827 | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR
828 | MSDC_INT_DMA_PROTECT);
829
830 done = !host->data;
831 spin_lock_irqsave(&host->lock, flags);
832 if (check_data)
833 host->data = NULL;
834 spin_unlock_irqrestore(&host->lock, flags);
835
836 if (done)
837 return true;
838
839 if (check_data || (stop && stop->error)) {
840 dev_dbg(host->dev, "DMA status: 0x%8X\n",
841 readl(host->base + MSDC_DMA_CFG));
842 sdr_set_field(host->base + MSDC_DMA_CTRL,
843 MSDC_DMA_CTRL_STOP, 1);
844 while (readl(host->base + MSDC_DMA_CFG) & MSDC_DMA_CFG_STS)
845 cpu_relax();
846
847 spin_lock_irqsave(&host->irqlock, flags);
848 sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask);
849 spin_unlock_irqrestore(&host->irqlock, flags);
850
851 dev_dbg(host->dev, "DMA stop\n");
852
853 if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) {
854 data->bytes_xfered = data->blocks * data->blksz;
855 } else {
856 dev_info(host->dev, "interrupt events: %x\n", events);
857 msdc_reset_hw(host);
858 host->error |= REQ_DAT_ERR;
859 data->bytes_xfered = 0;
860
861 if (events & MSDC_INT_DATTMO)
862 data->error = -ETIMEDOUT;
863 else if (events & MSDC_INT_DATCRCERR)
864 data->error = -EILSEQ;
865
866 dev_info(host->dev, "%s: cmd=%d arg=0x%8X blocks=%d",
867 __func__, mrq->cmd->opcode,
868 mrq->cmd->arg, data->blocks);
869 dev_info(host->dev, "data_error=%d xfer_size=%d\n",
870 (int)data->error, data->bytes_xfered);
871 }
872
873 msdc_data_xfer_next(host, mrq, data);
874 done = true;
875 }
876 return done;
877}
878
879static void msdc_set_buswidth(struct msdc_host *host, u32 width)
880{
881 u32 val = readl(host->base + SDC_CFG);
882
883 val &= ~SDC_CFG_BUSWIDTH;
884
885 switch (width) {
886 default:
887 case MMC_BUS_WIDTH_1:
888 val |= (MSDC_BUS_1BITS << 16);
889 break;
890 case MMC_BUS_WIDTH_4:
891 val |= (MSDC_BUS_4BITS << 16);
892 break;
893 case MMC_BUS_WIDTH_8:
894 val |= (MSDC_BUS_8BITS << 16);
895 break;
896 }
897
898 writel(val, host->base + SDC_CFG);
899 dev_dbg(host->dev, "Bus Width = %d", width);
900}
901
902static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios)
903{
904
905#if 0
906 struct msdc_host *host = mmc_priv(mmc);
907 int min_uv, max_uv;
908 int ret = 0;
909
910 if (!IS_ERR(mmc->supply.vqmmc)) {
911 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
912 min_uv = 3300000;
913 max_uv = 3300000;
914 } else if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) {
915 min_uv = 1800000;
916 max_uv = 1800000;
917 } else {
918 dev_info(host->dev, "Unsupported signal voltage!\n");
919 return -EINVAL;
920 }
921
922 ret = regulator_set_voltage(mmc->supply.vqmmc, min_uv, max_uv);
923 if (ret) {
924 dev_dbg(host->dev, "Regulator set error %d (%d)\n",
925 ret, ios->signal_voltage);
926 } else {
927 /* Apply different pinctrl settings
928 * for different signal voltage
929 */
930 if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
931 pinctrl_select_state(host->pinctrl,
932 host->pins_uhs);
933 else
934 pinctrl_select_state(host->pinctrl,
935 host->pins_default);
936 }
937 }
938 return ret;
939#endif
940 return 0;
941}
942
943static void msdc_request_timeout(struct work_struct *work)
944{
945 struct msdc_host *host = container_of(work, struct msdc_host,
946 req_timeout.work);
947
948 /* simulate HW timeout status */
949 dev_info(host->dev, "%s: aborting cmd/data/mrq\n", __func__);
950 if (host->mrq) {
951 dev_info(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__,
952 host->mrq, host->mrq->cmd->opcode);
953 if (host->cmd) {
954 dev_info(host->dev,
955 "%s: aborting cmd=%d, arg=0x%x\n", __func__,
956 host->cmd->opcode, host->cmd->arg);
957 msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq,
958 host->cmd);
959 } else if (host->data) {
960 dev_info(host->dev,
961 "%s: aborting data: cmd%d; %d blocks\n",
962 __func__, host->mrq->cmd->opcode,
963 host->data->blocks);
964 msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq,
965 host->data);
966 }
967 }
968}
969
970static irqreturn_t msdc_irq(int irq, void *dev_id)
971{
972 unsigned long flags;
973 struct msdc_host *host = (struct msdc_host *) dev_id;
974 struct mmc_request *mrq;
975 struct mmc_command *cmd;
976 struct mmc_data *data;
977 u32 events, event_mask;
978
979 spin_lock_irqsave(&host->irqlock, flags);
980 events = readl(host->base + MSDC_INT);
981 event_mask = readl(host->base + MSDC_INTEN);
982 /* clear interrupts */
983 writel(events & event_mask, host->base + MSDC_INT);
984
985 mrq = host->mrq;
986 cmd = host->cmd;
987 data = host->data;
988 spin_unlock_irqrestore(&host->irqlock, flags);
989
990 if ((events & event_mask) & MSDC_INT_SDIOIRQ) {
991 mmc_signal_sdio_irq(host->mmc);
992 if (!mrq)
993 return IRQ_HANDLED;
994 }
995
996 if (!(events & (event_mask & ~MSDC_INT_SDIOIRQ)))
997 return IRQ_HANDLED;
998
999 if (!mrq) {
1000 dev_info(host->dev,
1001 "%s: MRQ=NULL; events=%08X; event_mask=%08X\n",
1002 __func__, events, event_mask);
1003 WARN_ON(1);
1004 return IRQ_HANDLED;
1005 }
1006
1007 if (cmd)
1008 msdc_cmd_done(host, events, mrq, cmd);
1009 else if (data)
1010 msdc_data_xfer_done(host, events, mrq, data);
1011
1012 return IRQ_HANDLED;
1013}
1014
1015static struct msdc_host *sdio_host;
1016
1017static void sdio_status_notify_cb(int card_present, void *dev_id)
1018{
1019 struct msdc_host *host = (struct msdc_host *)dev_id;
1020
1021 pr_info("%s: card_present %d\n", mmc_hostname(host->mmc), card_present);
1022
1023 if (card_present == 1) {
1024 host->mmc->rescan_disable = 0;
1025 mmc_detect_change(host->mmc, 0);
1026 } else if (card_present == 0) {
1027 host->mmc->detect_change = 0;
1028 host->mmc->rescan_disable = 1;
1029 }
1030}
1031
1032void sdio_card_detect(int card_present)
1033{
1034 pr_info("%s: enter present:%d\n", __func__, card_present);
1035 if (sdio_host)
1036 sdio_status_notify_cb(card_present, sdio_host);
1037
1038}
1039EXPORT_SYMBOL(sdio_card_detect);
1040
1041static void msdc_init_hw(struct msdc_host *host)
1042{
1043 u32 val;
1044 u32 tune_reg = host->dev_comp->pad_tune_reg;
1045 unsigned long flags;
1046
1047 /* Configure to MMC/SD mode, clock free running */
1048 sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN);
1049
1050 /* Reset */
1051 msdc_reset_hw(host);
1052
1053 /* Disable card detection */
1054 sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN);
1055
1056 /* Disable and clear all interrupts */
1057 spin_lock_irqsave(&host->irqlock, flags);
1058 writel(0, host->base + MSDC_INTEN);
1059 val = readl(host->base + MSDC_INT);
1060 writel(val, host->base + MSDC_INT);
1061 spin_unlock_irqrestore(&host->irqlock, flags);
1062
1063 /* add for top_base */
1064 writel(0, host->base + tune_reg);
1065 if (host->top_base) {
1066 writel(0, host->top_base + EMMC_TOP_CONTROL);
1067 writel(0, host->top_base + EMMC_TOP_CMD);
1068 }
1069
1070 writel(0, host->base + MSDC_IOCON);
1071 sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0);
1072 writel(0x403c0000, host->base + MSDC_PATCH_BIT0);
1073 sdr_set_field(host->base + MSDC_PATCH_BIT0, MSDC_CKGEN_MSDC_DLY_SEL, 1);
1074 writel(0xffff00c9, host->base + MSDC_PATCH_BIT1);
1075
1076 /* For SDIO3.0+ IP, this bit should be set to 0 */
1077 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_SINGLE_BURST);
1078
1079 sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL);
1080
1081 if (host->dev_comp->stop_clk_fix) {
1082 sdr_set_field(host->base + MSDC_PATCH_BIT1,
1083 MSDC_PB1_STOP_DLY_SEL, 3);
1084 sdr_clr_bits(host->base + SDC_FIFO_CFG,
1085 MSDC_WR_VALID_SEL);
1086 sdr_clr_bits(host->base + SDC_FIFO_CFG,
1087 MSDC_RD_VALID_SEL);
1088 }
1089
1090 if (host->dev_comp->busy_check)
1091 sdr_clr_bits(host->base + MSDC_PATCH_BIT1, (1 << 7));
1092
1093 if (host->dev_comp->async_fifo) {
1094 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1095 MSDC_PB2_RESPWAITCNT, 3);
1096 if (host->dev_comp->enhance_rx) {
1097 if (host->top_base)
1098 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL,
1099 SDC_RX_ENH_EN);
1100 else
1101 sdr_set_bits(host->base + SDC_ADV_CFG0,
1102 SDC_RX_ENHANCE_EN);
1103 } else {
1104 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1105 MSDC_PB2_RESPSTSENSEL, 2);
1106 sdr_set_field(host->base + MSDC_PATCH_BIT2,
1107 MSDC_PB2_CRCSTSENSEL, 2);
1108 }
1109 /* use async fifo, then no need tune internal delay */
1110 sdr_clr_bits(host->base + MSDC_PATCH_BIT2,
1111 MSDC_PB2_CFGRESP);
1112 sdr_set_bits(host->base + MSDC_PATCH_BIT2,
1113 MSDC_PB2_CFGCRCSTS);
1114 }
1115
1116 if (host->dev_comp->support_64g)
1117 sdr_set_bits(host->base + MSDC_PATCH_BIT2,
1118 MSDC_PB2_SUPPORT64G);
1119 if (host->dev_comp->data_tune) {
1120 if (host->top_base) {
1121 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL,
1122 PAD_DAT_RD_RXDLY_SEL);
1123 sdr_clr_bits(host->top_base + EMMC_TOP_CONTROL,
1124 DATA_K_VALUE_SEL);
1125 sdr_set_bits(host->top_base + EMMC_TOP_CMD,
1126 PAD_CMD_RD_RXDLY_SEL);
1127 }
1128 } else {
1129 /* choose clock tune */
1130 if (host->top_base)
1131 sdr_set_bits(host->top_base + EMMC_TOP_CONTROL,
1132 PAD_RXDLY_SEL);
1133 }
1134
1135 /* Configure to enable SDIO mode.
1136 * it's must otherwise sdio cmd5 failed
1137 */
1138 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO);
1139
1140 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
1141 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
1142 else
1143 /* disable detect SDIO device interrupt function */
1144 sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
1145 /* Configure to default data timeout */
1146 sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3);
1147
1148 host->def_tune_para.iocon = readl(host->base + MSDC_IOCON);
1149 host->def_tune_para.pad_tune = readl(host->base + tune_reg);
1150 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
1151 host->saved_tune_para.pad_tune = readl(host->base + tune_reg);
1152 if (host->top_base) {
1153 host->def_tune_para.emmc_top_control =
1154 readl(host->top_base + EMMC_TOP_CONTROL);
1155 host->def_tune_para.emmc_top_cmd =
1156 readl(host->top_base + EMMC_TOP_CMD);
1157 host->saved_tune_para.emmc_top_control =
1158 readl(host->top_base + EMMC_TOP_CONTROL);
1159 host->saved_tune_para.emmc_top_cmd =
1160 readl(host->top_base + EMMC_TOP_CMD);
1161 } else {
1162 host->def_tune_para.pad_tune0 =
1163 readl(host->base + MSDC_PAD_TUNE0);
1164 host->def_tune_para.pad_tune1 =
1165 readl(host->base + MSDC_PAD_TUNE1);
1166 }
1167 dev_dbg(host->dev, "init hardware done!");
1168}
1169
1170static void msdc_deinit_hw(struct msdc_host *host)
1171{
1172 u32 val;
1173 unsigned long flags;
1174
1175 /* Disable and clear all interrupts */
1176 spin_lock_irqsave(&host->irqlock, flags);
1177 writel(0, host->base + MSDC_INTEN);
1178
1179 val = readl(host->base + MSDC_INT);
1180 writel(val, host->base + MSDC_INT);
1181 spin_unlock_irqrestore(&host->irqlock, flags);
1182}
1183
1184/* init gpd and bd list in msdc_drv_probe */
1185static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma)
1186{
1187 struct mt_gpdma_desc *gpd = dma->gpd;
1188 struct mt_bdma_desc *bd = dma->bd;
1189 int i;
1190
1191 memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2);
1192
1193 gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */
1194 gpd->ptr = (u32)dma->bd_addr; /* physical address */
1195 /* gpd->next is must set for desc DMA
1196 * That's why must alloc 2 gpd structure.
1197 */
1198 gpd->next = (u32)dma->gpd_addr + sizeof(struct mt_gpdma_desc);
1199 memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM);
1200 for (i = 0; i < (MAX_BD_NUM - 1); i++)
1201 bd[i].next = (u32)dma->bd_addr + sizeof(*bd) * (i + 1);
1202}
1203
1204static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1205{
1206 struct msdc_host *host = mmc_priv(mmc);
1207
1208 msdc_set_buswidth(host, ios->bus_width);
1209
1210 /* Suspend/Resume will do power off/on */
1211#ifdef DO_POWER_ONOFF
1212 switch (ios->power_mode) {
1213 case MMC_POWER_UP:
1214 if (!IS_ERR(mmc->supply.vmmc)) {
1215 msdc_init_hw(host);
1216 ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc,
1217 ios->vdd);
1218 if (ret) {
1219 dev_info(host->dev, "Failed to set vmmc power!\n");
1220 return;
1221 }
1222 }
1223 break;
1224 case MMC_POWER_ON:
1225 if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
1226 ret = regulator_enable(mmc->supply.vqmmc);
1227 if (ret)
1228 dev_info(host->dev, "Failed to set vqmmc power!\n");
1229 else
1230 host->vqmmc_enabled = true;
1231 }
1232 break;
1233 case MMC_POWER_OFF:
1234/* power always on */
1235 if (!IS_ERR(mmc->supply.vmmc))
1236 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
1237
1238 if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
1239 regulator_disable(mmc->supply.vqmmc);
1240 host->vqmmc_enabled = false;
1241 }
1242 break;
1243 default:
1244 break;
1245 }
1246#endif
1247
1248 if (host->mclk != ios->clock || host->timing != ios->timing)
1249 msdc_set_mclk(host, ios->timing, ios->clock);
1250}
1251
1252/*************** SDIO AUTOK ******************/
1253/* 100ms */
1254#define AUTOK_CMD_TIMEOUT (HZ / 10)
1255/* 1s x 3 */
1256#define AUTOK_DAT_TIMEOUT (HZ * 3)
1257
1258#define MSDC_FIFO_THD_1K (1024)
1259#define TUNE_TX_CNT (100)
1260#define MSDC_FIFO_SZ (128)
1261/*#define TUNE_DATA_TX_ADDR (0x358000)*/
1262/* Use negative value to represent address from end of device,
1263 * 33 blocks used by SGPT at end of device,
1264 * 32768 blocks used by flashinfo immediate before SGPT
1265 */
1266#define TUNE_DATA_TX_ADDR (-33-32768)
1267#define CMDQ
1268#define AUTOK_LATCH_CK_EMMC_TUNE_TIMES (10) /* 5.0IP eMMC 1KB fifo ZIZE */
1269#define AUTOK_LATCH_CK_SDIO_TUNE_TIMES (20) /* 4.5IP SDIO 128fifo ZIZE */
1270#define AUTOK_LATCH_CK_SD_TUNE_TIMES (3) /* 4.5IP SD 128fifo ZIZE */
1271#define AUTOK_CMD_TIMES (20)
1272#define AUTOK_TUNING_INACCURACY (3) /* scan result may find xxxooxxx */
1273#define AUTOK_MARGIN_THOLD (5)
1274#define AUTOK_BD_WIDTH_REF (3)
1275
1276#define AUTOK_READ 0
1277#define AUTOK_WRITE 1
1278
1279#define AUTOK_FINAL_CKGEN_SEL (0)
1280#define SCALE_TA_CNTR (8)
1281#define SCALE_CMD_RSP_TA_CNTR (8)
1282#define SCALE_WDAT_CRC_TA_CNTR (8)
1283#define SCALE_INT_DAT_LATCH_CK_SEL (8)
1284#define SCALE_INTERNAL_DLY_CNTR (32)
1285#define SCALE_PAD_DAT_DLY_CNTR (32)
1286
1287#define TUNING_INACCURACY (2)
1288
1289/* autok platform specific setting */
1290#define AUTOK_CKGEN_VALUE (0)
1291#define AUTOK_CMD_LATCH_EN_HS400_VALUE (3)
1292#define AUTOK_CMD_LATCH_EN_NON_HS400_VALUE (2)
1293#define AUTOK_CRC_LATCH_EN_HS400_VALUE (0)
1294#define AUTOK_CRC_LATCH_EN_NON_HS400_VALUE (2)
1295#define AUTOK_LATCH_CK_VALUE (1)
1296#define AUTOK_CMD_TA_VALUE (0)
1297#define AUTOK_CRC_TA_VALUE (0)
1298#define AUTOK_CRC_MA_VALUE (1)
1299#define AUTOK_BUSY_MA_VALUE (1)
1300#define AUTOK_STOP_DLY_SEL (6)
1301#define AUTOK_POP_EN_CNT (0)
1302#define AUTOK_WR_VALID_SEL (0)
1303#define AUTOK_RD_VALID_SEL (0)
1304#define AUTOK_POP_MARK_WATER (1)
1305#define AUTOK_STATE_CLEAR (1)
1306
1307#define E_RESULT_PASS (0)
1308#define E_RESULT_CMD_TMO (1<<0)
1309#define E_RESULT_RSP_CRC (1<<1)
1310#define E_RESULT_DAT_CRC (1<<2)
1311#define E_RESULT_DAT_TMO (1<<3)
1312#define E_RESULT_W_CRC (1<<4)
1313#define E_RESULT_ERR (1<<5)
1314#define E_RESULT_START (1<<6)
1315#define E_RESULT_PW_SMALL (1<<7)
1316#define E_RESULT_KEEP_OLD (1<<8)
1317#define E_RESULT_CMP_ERR (1<<9)
1318#define E_RESULT_FATAL_ERR (1<<10)
1319
1320#define E_RESULT_MAX
1321
1322#ifndef NULL
1323#define NULL 0
1324#endif
1325#ifndef TRUE
1326#define TRUE (0 == 0)
1327#endif
1328#ifndef FALSE
1329#define FALSE (0 != 0)
1330#endif
1331
1332#define ATK_OFF 0
1333#define ATK_ERROR 1
1334#define ATK_RES 2
1335#define ATK_WARN 3
1336#define ATK_TRACE 4
1337#define ATK_LOUD 5
1338
1339unsigned int autok_debug_level = ATK_RES;
1340
1341#define ATK_DBG(_level, _fmt ...) \
1342({ \
1343 if (autok_debug_level >= _level) { \
1344 pr_info(_fmt); \
1345 } \
1346})
1347
1348#define ATK_ERR(_fmt ...) \
1349({ \
1350 pr_info(_fmt); \
1351})
1352
1353enum AUTOK_PARAM {
1354 /* command response sample selection
1355 * (MSDC_SMPL_RISING, MSDC_SMPL_FALLING)
1356 */
1357 CMD_EDGE,
1358
1359 /* read data sample selection (MSDC_SMPL_RISING, MSDC_SMPL_FALLING) */
1360 RDATA_EDGE,
1361
1362 /* read data async fifo out edge select */
1363 RD_FIFO_EDGE,
1364
1365 /* write data crc status async fifo out edge select */
1366 WD_FIFO_EDGE,
1367
1368 /* [Data Tune]CMD Pad RX Delay Line1 Control.
1369 * This register is used to fine-tune CMD pad macro respose
1370 * latch timing. Total 32 stages[Data Tune]
1371 */
1372 CMD_RD_D_DLY1,
1373
1374 /* [Data Tune]CMD Pad RX Delay Line1 Sel-> delay cell1 enable */
1375 CMD_RD_D_DLY1_SEL,
1376
1377 /* [Data Tune]CMD Pad RX Delay Line2 Control. This register is used to
1378 * fine-tune CMD pad macro respose latch timing.
1379 * Total 32 stages[Data Tune]
1380 */
1381 CMD_RD_D_DLY2,
1382
1383 /* [Data Tune]CMD Pad RX Delay Line1 Sel-> delay cell2 enable */
1384 CMD_RD_D_DLY2_SEL,
1385
1386 /* [Data Tune]DAT Pad RX Delay Line1 Control (for MSDC RD),
1387 * Total 32 stages [Data Tune]
1388 */
1389 DAT_RD_D_DLY1,
1390
1391 /* [Data Tune]DAT Pad RX Delay Line1 Sel-> delay cell1 enable */
1392 DAT_RD_D_DLY1_SEL,
1393
1394 /* [Data Tune]DAT Pad RX Delay Line2 Control (for MSDC RD),
1395 * Total 32 stages [Data Tune]
1396 */
1397 DAT_RD_D_DLY2,
1398
1399 /* [Data Tune]DAT Pad RX Delay Line2 Sel-> delay cell2 enable */
1400 DAT_RD_D_DLY2_SEL,
1401
1402 /* Internal MSDC clock phase selection. Total 8 stages,
1403 * each stage can delay 1 clock period of msdc_src_ck
1404 */
1405 INT_DAT_LATCH_CK,
1406
1407 /* DS Pad Z clk delay count, range: 0~63, Z dly1(0~31)+Z dly2(0~31) */
1408 EMMC50_DS_Z_DLY1,
1409
1410 /* DS Pad Z clk del sel: [dly2_sel:dly1_sel]
1411 * -> [0,1]: dly1 enable [1,2]:dl2 & dly1 enable ,else :no dly enable
1412 */
1413 EMMC50_DS_Z_DLY1_SEL,
1414
1415 /* DS Pad Z clk delay count, range: 0~63, Z dly1(0~31)+Z dly2(0~31) */
1416 EMMC50_DS_Z_DLY2,
1417
1418 /* DS Pad Z clk del sel: [dly2_sel:dly1_sel]
1419 * -> [0,1]: dly1 enable [1,2]:dl2 & dly1 enable ,else :no dly enable
1420 */
1421 EMMC50_DS_Z_DLY2_SEL,
1422
1423 /* DS Pad Z_DLY clk delay count, range: 0~31 */
1424 EMMC50_DS_ZDLY_DLY,
1425 TUNING_PARAM_COUNT,
1426
1427 /* Data line rising/falling latch fine tune selection
1428 * in read transaction.
1429 * 1'b0: All data line share one value
1430 * indicated by MSDC_IOCON.R_D_SMPL.
1431 * 1'b1: Each data line has its own selection value
1432 * indicated by Data line (x): MSDC_IOCON.R_D(x)_SMPL
1433 */
1434 READ_DATA_SMPL_SEL,
1435
1436 /* Data line rising/falling latch fine tune selection
1437 * in write transaction.
1438 * 1'b0: All data line share one value indicated
1439 * by MSDC_IOCON.W_D_SMPL.
1440 * 1'b1: Each data line has its own selection value indicated
1441 * by Data line (x): MSDC_IOCON.W_D(x)_SMPL
1442 */
1443 WRITE_DATA_SMPL_SEL,
1444
1445 /* Data line delay line fine tune selection.
1446 * 1'b0: All data line share one delay
1447 * selection value indicated by PAD_TUNE.PAD_DAT_RD_RXDLY.
1448 * 1'b1: Each data line has its own delay selection value indicated by
1449 * Data line (x): DAT_RD_DLY(x).DAT0_RD_DLY
1450 */
1451 DATA_DLYLINE_SEL,
1452
1453 /* [Data Tune]CMD & DATA Pin tune Data Selection[Data Tune Sel] */
1454 MSDC_DAT_TUNE_SEL,
1455
1456 /* [Async_FIFO Mode Sel For Write Path] */
1457 MSDC_WCRC_ASYNC_FIFO_SEL,
1458
1459 /* [Async_FIFO Mode Sel For CMD Path] */
1460 MSDC_RESP_ASYNC_FIFO_SEL,
1461
1462 /* Write Path Mux for emmc50 function & emmc45 function ,
1463 * Only emmc50 design valid,[1-eMMC50, 0-eMMC45]
1464 */
1465 EMMC50_WDATA_MUX_EN,
1466
1467 /* CMD Path Mux for emmc50 function & emmc45 function ,
1468 * Only emmc50 design valid,[1-eMMC50, 0-eMMC45]
1469 */
1470 EMMC50_CMD_MUX_EN,
1471
1472 /* write data crc status async fifo output edge select */
1473 EMMC50_WDATA_EDGE,
1474
1475 /* CKBUF in CKGEN Delay Selection. Total 32 stages */
1476 CKGEN_MSDC_DLY_SEL,
1477
1478 /* CMD response turn around period.
1479 * The turn around cycle = CMD_RSP_TA_CNTR + 2,
1480 * Only for USH104 mode, this register should be
1481 * set to 0 in non-UHS104 mode
1482 */
1483 CMD_RSP_TA_CNTR,
1484
1485 /* Write data and CRC status turn around period.
1486 * The turn around cycle = WRDAT_CRCS_TA_CNTR + 2,
1487 * Only for USH104 mode, this register should be
1488 * set to 0 in non-UHS104 mode
1489 */
1490 WRDAT_CRCS_TA_CNTR,
1491
1492 /* CLK Pad TX Delay Control.
1493 * This register is used to add delay to CLK phase.
1494 * Total 32 stages
1495 */
1496 PAD_CLK_TXDLY_AUTOK,
1497 TOTAL_PARAM_COUNT
1498};
1499
1500/*
1501 *********************************************************
1502 * Feature Control Defination *
1503 *********************************************************
1504 */
1505#define AUTOK_OFFLINE_TUNE_TX_ENABLE 1
1506#define AUTOK_OFFLINE_TUNE_ENABLE 0
1507#define HS400_OFFLINE_TUNE_ENABLE 0
1508#define HS200_OFFLINE_TUNE_ENABLE 0
1509#define HS400_DSCLK_NEED_TUNING 0
1510#define AUTOK_PARAM_DUMP_ENABLE 0
1511/* #define CHIP_DENALI_3_DAT_TUNE */
1512/* #define SDIO_TUNE_WRITE_PATH */
1513
1514enum TUNE_TYPE {
1515 TUNE_CMD = 0,
1516 TUNE_DATA,
1517 TUNE_LATCH_CK,
1518};
1519
1520#define autok_msdc_retry(expr, retry, cnt) \
1521 do { \
1522 int backup = cnt; \
1523 while (retry) { \
1524 if (!(expr)) \
1525 break; \
1526 if (cnt-- == 0) { \
1527 retry--; cnt = backup; \
1528 } \
1529 } \
1530 WARN_ON(retry == 0); \
1531} while (0)
1532
1533#define autok_msdc_reset() \
1534 do { \
1535 int retry = 3, cnt = 1000; \
1536 sdr_set_bits(base + MSDC_CFG, MSDC_CFG_RST); \
1537 /* ensure reset operation be sequential */ \
1538 mb(); \
1539 autok_msdc_retry(readl(base + MSDC_CFG) & \
1540 MSDC_CFG_RST, retry, cnt); \
1541 } while (0)
1542
1543#define msdc_rxfifocnt() \
1544 ((readl(base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) >> 0)
1545#define msdc_txfifocnt() \
1546 ((readl(base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16)
1547
1548#define wait_cond(cond, tmo, left) \
1549 do { \
1550 u32 t = tmo; \
1551 while (1) { \
1552 if ((cond) || (t == 0)) \
1553 break; \
1554 if (t > 0) { \
1555 ndelay(1); \
1556 t--; \
1557 } \
1558 } \
1559 left = t; \
1560 } while (0)
1561
1562#define wait_cond_tmo(cond, tmo) \
1563 do { \
1564 unsigned long timeout = jiffies + tmo; \
1565 while (1) { \
1566 if ((cond) || (tmo == 0)) \
1567 break; \
1568 if (time_after(jiffies, timeout) && (!cond)) \
1569 tmo = 0; \
1570 } \
1571 } while (0)
1572
1573#define msdc_clear_fifo() \
1574 do { \
1575 int retry = 5, cnt = 1000; \
1576 sdr_set_bits(base + MSDC_FIFOCS, MSDC_FIFOCS_CLR); \
1577 /* ensure fifo clear operation be sequential */ \
1578 mb(); \
1579 autok_msdc_retry(readl(base + MSDC_FIFOCS) & MSDC_FIFOCS_CLR, \
1580 retry, cnt); \
1581 } while (0)
1582
1583struct AUTOK_PARAM_RANGE {
1584 unsigned int start;
1585 unsigned int end;
1586};
1587
1588struct AUTOK_PARAM_INFO {
1589 struct AUTOK_PARAM_RANGE range;
1590 char *param_name;
1591};
1592
1593struct BOUND_INFO {
1594 unsigned int Bound_Start;
1595 unsigned int Bound_End;
1596 unsigned int Bound_width;
1597 bool is_fullbound;
1598};
1599
1600#define BD_MAX_CNT 4 /* Max Allowed Boundary Number */
1601struct AUTOK_SCAN_RES {
1602 /* Bound info record, currently only allow max to 2 bounds exist,
1603 * but in extreme case, may have 4 bounds
1604 */
1605 struct BOUND_INFO bd_info[BD_MAX_CNT];
1606 /* Bound cnt record, must be in rang [0,3] */
1607 unsigned int bd_cnt;
1608 /* Full boundary cnt record */
1609 unsigned int fbd_cnt;
1610};
1611
1612struct AUTOK_REF_INFO {
1613 /* inf[0] - rising edge res, inf[1] - falling edge res */
1614 struct AUTOK_SCAN_RES scan_info[2];
1615 /* optimised sample edge select */
1616 unsigned int opt_edge_sel;
1617 /* optimised dly cnt sel */
1618 unsigned int opt_dly_cnt;
1619 /* 1clk cycle equal how many delay cell cnt, if cycle_cnt is 0,
1620 * that is cannot calc cycle_cnt by current Boundary info
1621 */
1622 unsigned int cycle_cnt;
1623};
1624
1625unsigned int do_autok_offline_tune_tx;
1626u8 sdio_autok_res[TUNING_PARAM_COUNT];
1627
1628const struct AUTOK_PARAM_INFO autok_param_info[] = {
1629 {{0, 1}, "CMD_EDGE"},
1630 /* async fifo mode Pad dat edge must fix to 0 */
1631 {{0, 1}, "RDATA_EDGE"},
1632 {{0, 1}, "RD_FIFO_EDGE"},
1633 {{0, 1}, "WD_FIFO_EDGE"},
1634
1635 /* Cmd Pad Tune Data Phase */
1636 {{0, 31}, "CMD_RD_D_DLY1"},
1637 {{0, 1}, "CMD_RD_D_DLY1_SEL"},
1638 {{0, 31}, "CMD_RD_D_DLY2"},
1639 {{0, 1}, "CMD_RD_D_DLY2_SEL"},
1640
1641 /* Data Pad Tune Data Phase */
1642 {{0, 31}, "DAT_RD_D_DLY1"},
1643 {{0, 1}, "DAT_RD_D_DLY1_SEL"},
1644 {{0, 31}, "DAT_RD_D_DLY2"},
1645 {{0, 1}, "DAT_RD_D_DLY2_SEL"},
1646
1647 /* Latch CK Delay for data read when clock stop */
1648 {{0, 7}, "INT_DAT_LATCH_CK"},
1649
1650 /* eMMC50 Related tuning param */
1651 {{0, 31}, "EMMC50_DS_Z_DLY1"},
1652 {{0, 1}, "EMMC50_DS_Z_DLY1_SEL"},
1653 {{0, 31}, "EMMC50_DS_Z_DLY2"},
1654 {{0, 1}, "EMMC50_DS_Z_DLY2_SEL"},
1655 {{0, 31}, "EMMC50_DS_ZDLY_DLY"},
1656
1657 /* ================================================= */
1658 /* Timming Related Mux & Common Setting Config */
1659 /* all data line path share sample edge */
1660 {{0, 1}, "READ_DATA_SMPL_SEL"},
1661 {{0, 1}, "WRITE_DATA_SMPL_SEL"},
1662 /* clK tune all data Line share dly */
1663 {{0, 1}, "DATA_DLYLINE_SEL"},
1664 /* data tune mode select */
1665 {{0, 1}, "MSDC_WCRC_ASYNC_FIFO_SEL"},
1666 /* data tune mode select */
1667 {{0, 1}, "MSDC_RESP_ASYNC_FIFO_SEL"},
1668
1669 /* eMMC50 Function Mux */
1670 /* write path switch to emmc45 */
1671 {{0, 1}, "EMMC50_WDATA_MUX_EN"},
1672 /* response path switch to emmc45 */
1673 {{0, 1}, "EMMC50_CMD_MUX_EN"},
1674 {{0, 1}, "EMMC50_WDATA_EDGE"},
1675 /* Common Setting Config */
1676 {{0, 31}, "CKGEN_MSDC_DLY_SEL"},
1677 {{1, 7}, "CMD_RSP_TA_CNTR"},
1678 {{1, 7}, "WRDAT_CRCS_TA_CNTR"},
1679 /* tx clk dly fix to 0 for HQA res */
1680 {{0, 31}, "PAD_CLK_TXDLY_AUTOK"},
1681};
1682
1683static int autok_send_tune_cmd(struct msdc_host *host, unsigned int opcode,
1684 enum TUNE_TYPE tune_type_value)
1685{
1686 void __iomem *base = host->base;
1687 unsigned int value;
1688 unsigned int rawcmd = 0;
1689 unsigned int arg = 0;
1690 unsigned int sts = 0;
1691 unsigned int wints = 0;
1692 unsigned long tmo = 0;
1693 unsigned int left = 0;
1694 unsigned int fifo_have = 0;
1695 unsigned int fifo_1k_cnt = 0;
1696 unsigned int i = 0;
1697 int ret = E_RESULT_PASS;
1698
1699 switch (opcode) {
1700 case MMC_SEND_EXT_CSD:
1701 rawcmd = (512 << 16) | (0 << 13) | (1 << 11) | (1 << 7) | (8);
1702 arg = 0;
1703 if (tune_type_value == TUNE_LATCH_CK)
1704 writel(host->tune_latch_ck_cnt, base + SDC_BLK_NUM);
1705 else
1706 writel(1, base + SDC_BLK_NUM);
1707 break;
1708 case MMC_STOP_TRANSMISSION:
1709 rawcmd = (1 << 14) | (7 << 7) | (12);
1710 arg = 0;
1711 break;
1712 case MMC_SEND_STATUS:
1713 rawcmd = (1 << 7) | (13);
1714 arg = (1 << 16);
1715 break;
1716 case MMC_READ_SINGLE_BLOCK:
1717 left = 512;
1718 rawcmd = (512 << 16) | (0 << 13) | (1 << 11) | (1 << 7) | (17);
1719 arg = 0;
1720 if (tune_type_value == TUNE_LATCH_CK)
1721 writel(host->tune_latch_ck_cnt, base + SDC_BLK_NUM);
1722 else
1723 writel(1, base + SDC_BLK_NUM);
1724 break;
1725 case MMC_SEND_TUNING_BLOCK:
1726 left = 64;
1727 rawcmd = (64 << 16) | (0 << 13) | (1 << 11) | (1 << 7) | (19);
1728 arg = 0;
1729 if (tune_type_value == TUNE_LATCH_CK)
1730 writel(host->tune_latch_ck_cnt, base + SDC_BLK_NUM);
1731 else
1732 writel(1, base + SDC_BLK_NUM);
1733 break;
1734 case MMC_SEND_TUNING_BLOCK_HS200:
1735 left = 128;
1736 rawcmd = (128 << 16) | (0 << 13) | (1 << 11) | (1 << 7) | (21);
1737 arg = 0;
1738 if (tune_type_value == TUNE_LATCH_CK)
1739 writel(host->tune_latch_ck_cnt, base + SDC_BLK_NUM);
1740 else
1741 writel(1, base + SDC_BLK_NUM);
1742 break;
1743 case MMC_WRITE_BLOCK:
1744 rawcmd = (512 << 16) | (1 << 13) | (1 << 11) | (1 << 7) | (24);
1745 if (TUNE_DATA_TX_ADDR >= 0)
1746 arg = TUNE_DATA_TX_ADDR;
1747 else
1748 arg = host->mmc->card->ext_csd.sectors
1749 + TUNE_DATA_TX_ADDR;
1750 break;
1751 case SD_IO_RW_DIRECT:
1752 break;
1753 case SD_IO_RW_EXTENDED:
1754 break;
1755 }
1756
1757 while ((readl(base + SDC_STS) & SDC_STS_SDCBUSY))
1758 ;
1759
1760 /* clear fifo */
1761 if ((tune_type_value == TUNE_CMD) || (tune_type_value == TUNE_DATA)) {
1762 autok_msdc_reset();
1763 msdc_clear_fifo();
1764 writel(0xffffffff, base + MSDC_INT);
1765 }
1766
1767 /* start command */
1768 writel(arg, base + SDC_ARG);
1769 writel(rawcmd, base + SDC_CMD);
1770
1771 /* wait interrupt status */
1772 wints = MSDC_INT_CMDTMO | MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR;
1773 tmo = AUTOK_CMD_TIMEOUT; /* 100ms */
1774 wait_cond_tmo(((sts = readl(base + MSDC_INT)) & wints), tmo);
1775 if (tmo == 0) {
1776 ret = E_RESULT_CMD_TMO;
1777 goto end;
1778 }
1779
1780 writel((sts & wints), base + MSDC_INT);
1781 if (sts == 0) {
1782 ret = E_RESULT_CMD_TMO;
1783 goto end;
1784 }
1785
1786 if (sts & MSDC_INT_CMDRDY) {
1787 if (tune_type_value == TUNE_CMD) {
1788 ret = E_RESULT_PASS;
1789 goto end;
1790 }
1791 } else if (sts & MSDC_INT_RSPCRCERR) {
1792 ret = E_RESULT_RSP_CRC;
1793 goto end;
1794 } else if (sts & MSDC_INT_CMDTMO) {
1795 ret = E_RESULT_CMD_TMO;
1796 goto end;
1797 }
1798 if ((tune_type_value != TUNE_LATCH_CK) &&
1799 (tune_type_value != TUNE_DATA))
1800 goto skip_tune_latch_ck_and_tune_data;
1801
1802 tmo = jiffies + AUTOK_DAT_TIMEOUT;
1803 while ((readl(base + SDC_STS) & SDC_STS_SDCBUSY) && (tmo != 0)) {
1804 if (time_after(jiffies, tmo))
1805 tmo = 0;
1806 if (tune_type_value == TUNE_LATCH_CK) {
1807 fifo_have = msdc_rxfifocnt();
1808 if ((opcode == MMC_SEND_TUNING_BLOCK_HS200) ||
1809 (opcode == MMC_READ_SINGLE_BLOCK) ||
1810 (opcode == MMC_SEND_EXT_CSD) ||
1811 (opcode == MMC_SEND_TUNING_BLOCK)) {
1812 sdr_set_field(base + MSDC_DBG_SEL,
1813 0xffff << 0, 0x0b);
1814 sdr_get_field(base + MSDC_DBG_OUT,
1815 0x7ff << 0, &fifo_1k_cnt);
1816 if ((fifo_1k_cnt >= MSDC_FIFO_THD_1K) &&
1817 (fifo_have >= MSDC_FIFO_SZ)) {
1818 value = readl(base + MSDC_RXDATA);
1819 value = readl(base + MSDC_RXDATA);
1820 value = readl(base + MSDC_RXDATA);
1821 value = readl(base + MSDC_RXDATA);
1822 }
1823 }
1824 } else if ((tune_type_value == TUNE_DATA) &&
1825 (opcode == MMC_WRITE_BLOCK)) {
1826 for (i = 0; i < 64; i++) {
1827 writel(0x5af00fa5, base + MSDC_TXDATA);
1828 writel(0x33cc33cc, base + MSDC_TXDATA);
1829 }
1830
1831 while ((readl(base + SDC_STS) & SDC_STS_SDCBUSY))
1832 ;
1833 }
1834 }
1835
1836 if ((tmo == 0) && (readl(base + SDC_STS) & SDC_STS_SDCBUSY)) {
1837 ret |= E_RESULT_FATAL_ERR;
1838 goto end;
1839 }
1840
1841 sts = readl(base + MSDC_INT);
1842 wints = MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO;
1843 if (sts) {
1844 /* clear status */
1845 writel((sts & wints), base + MSDC_INT);
1846 if (sts & MSDC_INT_XFER_COMPL)
1847 ret = E_RESULT_PASS;
1848 if (MSDC_INT_DATCRCERR & sts)
1849 ret = E_RESULT_DAT_CRC;
1850 if (MSDC_INT_DATTMO & sts)
1851 ret = E_RESULT_DAT_TMO;
1852 }
1853
1854skip_tune_latch_ck_and_tune_data:
1855 while ((readl(base + SDC_STS) & SDC_STS_SDCBUSY))
1856 ;
1857 if ((tune_type_value == TUNE_CMD) || (tune_type_value == TUNE_DATA))
1858 msdc_clear_fifo();
1859
1860end:
1861 if (opcode == MMC_STOP_TRANSMISSION) {
1862 while ((readl(base + MSDC_PS) & 0x10000) != 0x10000)
1863 ;
1864 }
1865
1866 return ret;
1867}
1868
1869static int autok_simple_score64(char *res_str64, u64 result64)
1870{
1871 unsigned int bit = 0;
1872 unsigned int num = 0;
1873 unsigned int old = 0;
1874
1875 if (result64 == 0) {
1876 /* maybe result is 0 */
1877 strcpy(res_str64,
1878 "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
1879 return 64;
1880 }
1881 if (result64 == 0xFFFFFFFFFFFFFFFF) {
1882 strcpy(res_str64,
1883 "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
1884 return 0;
1885 }
1886
1887 /* calc continue zero number */
1888 while (bit < 64) {
1889 if (result64 & ((u64) (1LL << bit))) {
1890 res_str64[bit] = 'X';
1891 bit++;
1892 if (old < num)
1893 old = num;
1894 num = 0;
1895 continue;
1896 }
1897 res_str64[bit] = 'O';
1898 bit++;
1899 num++;
1900 }
1901 if (num > old)
1902 old = num;
1903
1904 return old;
1905}
1906
1907enum {
1908 RD_SCAN_NONE,
1909 RD_SCAN_PAD_BOUND_S,
1910 RD_SCAN_PAD_BOUND_E,
1911 RD_SCAN_PAD_MARGIN,
1912};
1913
1914static int autok_check_scan_res64(u64 rawdat, struct AUTOK_SCAN_RES *scan_res)
1915{
1916 unsigned int bit;
1917 unsigned int filter = 4;
1918 struct BOUND_INFO *pBD = (struct BOUND_INFO *)scan_res->bd_info;
1919 unsigned int RawScanSta = RD_SCAN_NONE;
1920
1921 for (bit = 0; bit < 64; bit++) {
1922 if (rawdat & (1LL << bit)) {
1923 switch (RawScanSta) {
1924 case RD_SCAN_NONE:
1925 RawScanSta = RD_SCAN_PAD_BOUND_S;
1926 pBD->Bound_Start = 0;
1927 pBD->Bound_width = 1;
1928 scan_res->bd_cnt += 1;
1929 break;
1930 case RD_SCAN_PAD_MARGIN:
1931 RawScanSta = RD_SCAN_PAD_BOUND_S;
1932 pBD->Bound_Start = bit;
1933 pBD->Bound_width = 1;
1934 scan_res->bd_cnt += 1;
1935 break;
1936 case RD_SCAN_PAD_BOUND_E:
1937 if ((filter) && ((bit - pBD->Bound_End) <=
1938 AUTOK_TUNING_INACCURACY)) {
1939 ATK_DBG(ATK_TRACE,
1940 "[AUTOK]WARN: Try to filter the holes\n");
1941 RawScanSta = RD_SCAN_PAD_BOUND_S;
1942
1943 pBD->Bound_width += (bit -
1944 pBD->Bound_End);
1945 pBD->Bound_End = 0;
1946 filter--;
1947
1948 /* update full bound info */
1949 if (pBD->is_fullbound) {
1950 pBD->is_fullbound = 0;
1951 scan_res->fbd_cnt -= 1;
1952 }
1953 } else {
1954 /* No filter Check and Get the next
1955 * boundary information
1956 */
1957 RawScanSta = RD_SCAN_PAD_BOUND_S;
1958 pBD++;
1959 pBD->Bound_Start = bit;
1960 pBD->Bound_width = 1;
1961 scan_res->bd_cnt += 1;
1962 if (scan_res->bd_cnt > BD_MAX_CNT) {
1963 ATK_ERR(
1964 "[AUTOK]Error: more than %d Boundary Exist\n",
1965 BD_MAX_CNT);
1966 return -1;
1967 }
1968 }
1969 break;
1970 case RD_SCAN_PAD_BOUND_S:
1971 pBD->Bound_width++;
1972 break;
1973 default:
1974 break;
1975 }
1976 } else {
1977 switch (RawScanSta) {
1978 case RD_SCAN_NONE:
1979 RawScanSta = RD_SCAN_PAD_MARGIN;
1980 break;
1981 case RD_SCAN_PAD_BOUND_S:
1982 RawScanSta = RD_SCAN_PAD_BOUND_E;
1983 pBD->Bound_End = bit - 1;
1984 /* update full bound info */
1985 if (pBD->Bound_Start > 0) {
1986 pBD->is_fullbound = 1;
1987 scan_res->fbd_cnt += 1;
1988 }
1989 break;
1990 case RD_SCAN_PAD_MARGIN:
1991 case RD_SCAN_PAD_BOUND_E:
1992 default:
1993 break;
1994 }
1995 }
1996 }
1997 if ((pBD->Bound_End == 0) && (pBD->Bound_width != 0))
1998 pBD->Bound_End = pBD->Bound_Start + pBD->Bound_width - 1;
1999
2000 return 0;
2001}
2002
2003static int autok_pad_dly_sel(struct AUTOK_REF_INFO *pInfo)
2004{
2005 /* scan result @ rising edge */
2006 struct AUTOK_SCAN_RES *pBdInfo_R = NULL;
2007 /* scan result @ falling edge */
2008 struct AUTOK_SCAN_RES *pBdInfo_F = NULL;
2009 /* Save the first boundary info for calc optimised dly count */
2010 struct BOUND_INFO *pBdPrev = NULL;
2011 /* Save the second boundary info for calc optimised dly count */
2012 struct BOUND_INFO *pBdNext = NULL;
2013 struct BOUND_INFO *pBdTmp = NULL;
2014 /* Full Boundary count */
2015 unsigned int FBound_Cnt_R = 0;
2016 unsigned int Bound_Cnt_R = 0;
2017 unsigned int Bound_Cnt_F = 0;
2018 unsigned int cycle_cnt = 64;
2019 int uBD_mid_prev = 0;
2020 int uBD_mid_next = 0;
2021 int uBD_width = 3;
2022 int uDlySel_F = 0;
2023 int uDlySel_R = 0;
2024 /* for falling edge margin compress */
2025 int uMgLost_F = 0;
2026 /* for rising edge margin compress */
2027 int uMgLost_R = 0;
2028 unsigned int i;
2029 unsigned int ret = 0;
2030
2031 pBdInfo_R = &(pInfo->scan_info[0]);
2032 pBdInfo_F = &(pInfo->scan_info[1]);
2033 FBound_Cnt_R = pBdInfo_R->fbd_cnt;
2034 Bound_Cnt_R = pBdInfo_R->bd_cnt;
2035 Bound_Cnt_F = pBdInfo_F->bd_cnt;
2036
2037 switch (FBound_Cnt_R) {
2038 case 4: /* SSSS Corner may cover 2~3T */
2039 case 3:
2040 ATK_ERR("[AUTOK]Warning: Too Many Full boundary count:%d\r\n",
2041 FBound_Cnt_R);
2042 case 2: /* mode_1 : 2 full boudary */
2043 for (i = 0; i < BD_MAX_CNT; i++) {
2044 if (pBdInfo_R->bd_info[i].is_fullbound) {
2045 if (pBdPrev == NULL) {
2046 pBdPrev = &(pBdInfo_R->bd_info[i]);
2047 } else {
2048 pBdNext = &(pBdInfo_R->bd_info[i]);
2049 break;
2050 }
2051 }
2052 }
2053
2054 if (pBdPrev && pBdNext) {
2055 uBD_mid_prev = (pBdPrev->Bound_Start +
2056 pBdPrev->Bound_End) / 2;
2057 uBD_mid_next = (pBdNext->Bound_Start +
2058 pBdNext->Bound_End) / 2;
2059 /* while in 2 full bound case, bd_width calc */
2060 uBD_width = (pBdPrev->Bound_width +
2061 pBdNext->Bound_width) / 2;
2062 cycle_cnt = uBD_mid_next - uBD_mid_prev;
2063 /* delay count sel at rising edge */
2064 if (uBD_mid_prev >= cycle_cnt / 2) {
2065 uDlySel_R = uBD_mid_prev - cycle_cnt / 2;
2066 uMgLost_R = 0;
2067 } else if ((cycle_cnt / 2 - uBD_mid_prev) >
2068 AUTOK_MARGIN_THOLD) {
2069 uDlySel_R = uBD_mid_prev + cycle_cnt / 2;
2070 uMgLost_R = 0;
2071 } else {
2072 uDlySel_R = 0;
2073 uMgLost_R = cycle_cnt / 2 - uBD_mid_prev;
2074 }
2075 /* delay count sel at falling edge */
2076 pBdTmp = &(pBdInfo_R->bd_info[0]);
2077 if (pBdTmp->is_fullbound) {
2078 /* ooooxxxooooooxxxooo */
2079 uDlySel_F = uBD_mid_prev;
2080 uMgLost_F = 0;
2081 } else {
2082 /* xooooooxxxoooooooxxxoo */
2083 if (pBdTmp->Bound_End > uBD_width / 2) {
2084 uDlySel_F = (pBdTmp->Bound_End) -
2085 (uBD_width / 2);
2086 uMgLost_F = 0;
2087 } else {
2088 uDlySel_F = 0;
2089 uMgLost_F = (uBD_width / 2) -
2090 (pBdTmp->Bound_End);
2091 }
2092 }
2093 } else {
2094 /* error can not find 2 foull boary */
2095 ATK_ERR("[AUTOK] can not find 2 full boudary @Mode1\n");
2096 return -1;
2097 }
2098 break;
2099
2100 case 1: /* rising edge find one full boundary */
2101 if (Bound_Cnt_R > 1) {
2102 /* mode_2: 1 full boundary and boundary count > 1 */
2103 pBdPrev = &(pBdInfo_R->bd_info[0]);
2104 pBdNext = &(pBdInfo_R->bd_info[1]);
2105
2106 if (pBdPrev->is_fullbound)
2107 uBD_width = pBdPrev->Bound_width;
2108 else
2109 uBD_width = pBdNext->Bound_width;
2110
2111 if ((pBdPrev->is_fullbound) ||
2112 (pBdNext->is_fullbound)) {
2113 if (pBdPrev->Bound_Start > 0)
2114 cycle_cnt = pBdNext->Bound_Start -
2115 pBdPrev->Bound_Start;
2116 else
2117 cycle_cnt = pBdNext->Bound_End -
2118 pBdPrev->Bound_End;
2119
2120 /* delay count sel@rising & falling edge */
2121 if (pBdPrev->is_fullbound) {
2122 uBD_mid_prev = (pBdPrev->Bound_Start +
2123 pBdPrev->Bound_End) / 2;
2124 uDlySel_F = uBD_mid_prev;
2125 uMgLost_F = 0;
2126 if (uBD_mid_prev >= cycle_cnt / 2) {
2127 uDlySel_R = uBD_mid_prev -
2128 cycle_cnt / 2;
2129 uMgLost_R = 0;
2130 } else if ((cycle_cnt / 2 -
2131 uBD_mid_prev) >
2132 AUTOK_MARGIN_THOLD) {
2133 uDlySel_R = uBD_mid_prev +
2134 cycle_cnt / 2;
2135 uMgLost_R = 0;
2136 } else {
2137 uDlySel_R = 0;
2138 uMgLost_R = cycle_cnt / 2 -
2139 uBD_mid_prev;
2140 }
2141 } else {
2142 /* first boundary not full boudary */
2143 uBD_mid_next = (pBdNext->Bound_Start +
2144 pBdNext->Bound_End) / 2;
2145 uDlySel_R = uBD_mid_next -
2146 cycle_cnt / 2;
2147 uMgLost_R = 0;
2148 if (pBdPrev->Bound_End >
2149 uBD_width / 2) {
2150 uDlySel_F = pBdPrev->Bound_End -
2151 (uBD_width / 2);
2152 uMgLost_F = 0;
2153 } else {
2154 uDlySel_F = 0;
2155 uMgLost_F = (uBD_width / 2) -
2156 (pBdPrev->Bound_End);
2157 }
2158 }
2159 } else {
2160 /* full bound must in first 2 boundary */
2161 return -1;
2162 }
2163 } else if (Bound_Cnt_F > 0) {
2164 /* mode_3: 1 full boundary and only
2165 * one boundary exist @rising edge
2166 */
2167 /* this boundary is full bound */
2168 pBdPrev = &(pBdInfo_R->bd_info[0]);
2169 pBdNext = &(pBdInfo_F->bd_info[0]);
2170 uBD_mid_prev = (pBdPrev->Bound_Start +
2171 pBdPrev->Bound_End) / 2;
2172 uBD_width = pBdPrev->Bound_width;
2173
2174 if (pBdNext->Bound_Start == 0) {
2175 cycle_cnt = (pBdPrev->Bound_End -
2176 pBdNext->Bound_End) * 2;
2177 } else if (pBdNext->Bound_End == 63) {
2178 cycle_cnt = (pBdNext->Bound_Start -
2179 pBdPrev->Bound_Start) * 2;
2180 } else {
2181 uBD_mid_next = (pBdNext->Bound_Start +
2182 pBdNext->Bound_End) / 2;
2183
2184 if (uBD_mid_next > uBD_mid_prev)
2185 cycle_cnt = (uBD_mid_next -
2186 uBD_mid_prev) * 2;
2187 else
2188 cycle_cnt = (uBD_mid_prev -
2189 uBD_mid_next) * 2;
2190 }
2191
2192 uDlySel_F = uBD_mid_prev;
2193 uMgLost_F = 0;
2194
2195 if (uBD_mid_prev >= cycle_cnt / 2) {
2196 /* case 1 */
2197 uDlySel_R = uBD_mid_prev - cycle_cnt / 2;
2198 uMgLost_R = 0;
2199 } else if (cycle_cnt / 2 - uBD_mid_prev <=
2200 AUTOK_MARGIN_THOLD) {
2201 /* case 2 */
2202 uDlySel_R = 0;
2203 uMgLost_R = cycle_cnt / 2 - uBD_mid_prev;
2204 } else if (cycle_cnt / 2 + uBD_mid_prev <= 63) {
2205 /* case 3 */
2206 uDlySel_R = cycle_cnt / 2 + uBD_mid_prev;
2207 uMgLost_R = 0;
2208 } else if (32 - uBD_mid_prev <= AUTOK_MARGIN_THOLD) {
2209 /* case 4 */
2210 uDlySel_R = 0;
2211 uMgLost_R = cycle_cnt / 2 - uBD_mid_prev;
2212 } else { /* case 5 */
2213 uDlySel_R = 63;
2214 uMgLost_R = uBD_mid_prev + cycle_cnt / 2 - 63;
2215 }
2216 } else {
2217 /* mode_4: falling edge no boundary found & rising
2218 * edge only one full boundary exist
2219 */
2220 /* this boundary is full bound */
2221 pBdPrev = &(pBdInfo_R->bd_info[0]);
2222 uBD_mid_prev = (pBdPrev->Bound_Start +
2223 pBdPrev->Bound_End) / 2;
2224 uBD_width = pBdPrev->Bound_width;
2225
2226 if (pBdPrev->Bound_End > (64 - pBdPrev->Bound_Start))
2227 cycle_cnt = 2 * (pBdPrev->Bound_End + 1);
2228 else
2229 cycle_cnt = 2 * (64 - pBdPrev->Bound_Start);
2230
2231 uDlySel_R = (uBD_mid_prev >= 32) ? 0 : 63;
2232 /* Margin enough donot care margin lost */
2233 uMgLost_R = 0xFF;
2234 uDlySel_F = uBD_mid_prev;
2235 /* Margin enough donot care margin lost */
2236 uMgLost_F = 0xFF;
2237
2238 ATK_ERR("[AUTOK]Warning: 1T > %d\n", cycle_cnt);
2239 }
2240 break;
2241
2242 case 0: /* rising edge cannot find full boudary */
2243 if (Bound_Cnt_R == 2) {
2244 pBdPrev = &(pBdInfo_R->bd_info[0]);
2245 /* this boundary is full bound */
2246 pBdNext = &(pBdInfo_F->bd_info[0]);
2247
2248 if (pBdNext->is_fullbound) {
2249 /* mode_5: rising_edge 2 boundary
2250 * (not full bound), falling edge
2251 * one full boundary
2252 */
2253 uBD_width = pBdNext->Bound_width;
2254 cycle_cnt = 2 * (pBdNext->Bound_End -
2255 pBdPrev->Bound_End);
2256 uBD_mid_next = (pBdNext->Bound_Start +
2257 pBdNext->Bound_End) / 2;
2258 uDlySel_R = uBD_mid_next;
2259 uMgLost_R = 0;
2260 if (pBdPrev->Bound_End >= uBD_width / 2) {
2261 uDlySel_F = pBdPrev->Bound_End -
2262 uBD_width / 2;
2263 uMgLost_F = 0;
2264 } else {
2265 uDlySel_F = 0;
2266 uMgLost_F = uBD_width / 2 -
2267 pBdPrev->Bound_End;
2268 }
2269 } else {
2270 /* for falling edge there must be one full
2271 * boundary between two bounary_mid at rising
2272 */
2273 return -1;
2274 }
2275 } else if (Bound_Cnt_R == 1) {
2276 if (Bound_Cnt_F > 1) {
2277 /* when rising_edge have only one boundary
2278 * (not full bound), falling edge should not
2279 * more than 1Bound exist
2280 */
2281 return -1;
2282 } else if (Bound_Cnt_F == 1) {
2283 /* mode_6: rising edge only 1 boundary
2284 * (not full Bound)
2285 * & falling edge have only 1 bound too
2286 */
2287 pBdPrev = &(pBdInfo_R->bd_info[0]);
2288 pBdNext = &(pBdInfo_F->bd_info[0]);
2289 if (pBdNext->is_fullbound) {
2290 uBD_width = pBdNext->Bound_width;
2291 } else {
2292 if (pBdNext->Bound_width >
2293 pBdPrev->Bound_width)
2294 uBD_width =
2295 (pBdNext->Bound_width + 1);
2296 else
2297 uBD_width =
2298 (pBdPrev->Bound_width + 1);
2299
2300 if (uBD_width < AUTOK_BD_WIDTH_REF)
2301 uBD_width = AUTOK_BD_WIDTH_REF;
2302 } /* Boundary width calc done */
2303
2304 if (pBdPrev->Bound_Start == 0) {
2305 /* Current Desing Not Allowed */
2306 if (pBdNext->Bound_Start == 0)
2307 return -1;
2308
2309 cycle_cnt = (pBdNext->Bound_Start -
2310 pBdPrev->Bound_End +
2311 uBD_width) * 2;
2312 } else if (pBdPrev->Bound_End == 63) {
2313 /* Current Desing Not Allowed */
2314 if (pBdNext->Bound_End == 63)
2315 return -1;
2316
2317 cycle_cnt = (pBdPrev->Bound_Start -
2318 pBdNext->Bound_End +
2319 uBD_width) * 2;
2320 } /* cycle count calc done */
2321
2322 /* calc optimise delay count */
2323 if (pBdPrev->Bound_Start == 0) {
2324 /* falling edge sel */
2325 if (pBdPrev->Bound_End >=
2326 uBD_width / 2) {
2327 uDlySel_F = pBdPrev->Bound_End -
2328 uBD_width / 2;
2329 uMgLost_F = 0;
2330 } else {
2331 uDlySel_F = 0;
2332 uMgLost_F = uBD_width / 2 -
2333 pBdPrev->Bound_End;
2334 }
2335
2336 /* rising edge sel */
2337 if (pBdPrev->Bound_End - uBD_width / 2 +
2338 cycle_cnt / 2 > 63) {
2339 uDlySel_R = 63;
2340 uMgLost_R =
2341 pBdPrev->Bound_End -
2342 uBD_width / 2 +
2343 cycle_cnt / 2 - 63;
2344 } else {
2345 uDlySel_R =
2346 pBdPrev->Bound_End -
2347 uBD_width / 2 +
2348 cycle_cnt / 2;
2349 uMgLost_R = 0;
2350 }
2351 } else if (pBdPrev->Bound_End == 63) {
2352 /* falling edge sel */
2353 if (pBdPrev->Bound_Start +
2354 uBD_width / 2 < 63) {
2355 uDlySel_F =
2356 pBdPrev->Bound_Start +
2357 uBD_width / 2;
2358 uMgLost_F = 0;
2359 } else {
2360 uDlySel_F = 63;
2361 uMgLost_F =
2362 pBdPrev->Bound_Start +
2363 uBD_width / 2 - 63;
2364 }
2365
2366 /* rising edge sel */
2367 if (pBdPrev->Bound_Start +
2368 uBD_width / 2 - cycle_cnt / 2 < 0) {
2369 uDlySel_R = 0;
2370 uMgLost_R =
2371 cycle_cnt / 2 -
2372 (pBdPrev->Bound_Start +
2373 uBD_width / 2);
2374 } else {
2375 uDlySel_R =
2376 pBdPrev->Bound_Start +
2377 uBD_width / 2 -
2378 cycle_cnt / 2;
2379 uMgLost_R = 0;
2380 }
2381 } else {
2382 return -1;
2383 }
2384 } else if (Bound_Cnt_F == 0) {
2385 /* mode_7: rising edge only one bound
2386 * (not full), falling no boundary
2387 */
2388 cycle_cnt = 128;
2389 pBdPrev = &(pBdInfo_R->bd_info[0]);
2390 if (pBdPrev->Bound_Start == 0) {
2391 uDlySel_F = 0;
2392 uDlySel_R = 63;
2393 } else if (pBdPrev->Bound_End == 63) {
2394 uDlySel_F = 63;
2395 uDlySel_R = 0;
2396 } else {
2397 return -1;
2398 }
2399 uMgLost_F = 0xFF;
2400 uMgLost_R = 0xFF;
2401
2402 ATK_ERR("[AUTOK]Warning: 1T > %d\n", cycle_cnt);
2403 }
2404 } else if (Bound_Cnt_R == 0) { /* Rising Edge No Boundary */
2405 if (Bound_Cnt_F > 1) {
2406 /* falling edge not allowed two boundary
2407 * Exist for this case
2408 */
2409 return -1;
2410 } else if (Bound_Cnt_F > 0) {
2411 /* mode_8: falling edge has one Boundary */
2412 pBdPrev = &(pBdInfo_F->bd_info[0]);
2413
2414 /* this boundary is full bound */
2415 if (pBdPrev->is_fullbound) {
2416 uBD_mid_prev =
2417 (pBdPrev->Bound_Start +
2418 pBdPrev->Bound_End) / 2;
2419
2420 if (pBdPrev->Bound_End >
2421 (64 - pBdPrev->Bound_Start))
2422 cycle_cnt =
2423 2 * (pBdPrev->Bound_End + 1);
2424 else
2425 cycle_cnt =
2426 2 * (64 - pBdPrev->Bound_Start);
2427
2428 uDlySel_R = uBD_mid_prev;
2429 uMgLost_R = 0xFF;
2430 uDlySel_F =
2431 (uBD_mid_prev >= 32) ? 0 : 63;
2432 uMgLost_F = 0xFF;
2433 } else {
2434 cycle_cnt = 128;
2435
2436 uDlySel_R = (pBdPrev->Bound_Start ==
2437 0) ? 0 : 63;
2438 uMgLost_R = 0xFF;
2439 uDlySel_F = (pBdPrev->Bound_Start ==
2440 0) ? 63 : 0;
2441 uMgLost_F = 0xFF;
2442 }
2443
2444 ATK_ERR("[AUTOK]Warning: 1T > %d\n", cycle_cnt);
2445 } else {
2446 /* falling edge no boundary. no need tuning */
2447 cycle_cnt = 128;
2448 uDlySel_F = 0;
2449 uMgLost_F = 0xFF;
2450 uDlySel_R = 0;
2451 uMgLost_R = 0xFF;
2452 ATK_ERR("[AUTOK]Warning: 1T > %d\n", cycle_cnt);
2453 }
2454 } else {
2455 /* Error if bound_cnt > 3 there must be
2456 * at least one full boundary exist
2457 */
2458 return -1;
2459 }
2460 break;
2461
2462 default:
2463 /* warning if boundary count > 4
2464 * (from current hw design, this case cannot happen)
2465 */
2466 return -1;
2467 }
2468
2469 /* Select Optimised Sample edge & delay count (the small one) */
2470 pInfo->cycle_cnt = cycle_cnt;
2471 if (uDlySel_R <= uDlySel_F) {
2472 pInfo->opt_edge_sel = 0;
2473 pInfo->opt_dly_cnt = uDlySel_R;
2474 } else {
2475 pInfo->opt_edge_sel = 1;
2476 pInfo->opt_dly_cnt = uDlySel_F;
2477
2478 }
2479 ATK_ERR("[AUTOK]Analysis Result: 1T = %d\n ", cycle_cnt);
2480 return ret;
2481}
2482
2483/*
2484 ************************************************************************
2485 * FUNCTION
2486 * autok_adjust_param
2487 *
2488 * DESCRIPTION
2489 * This function for auto-K, adjust msdc parameter
2490 *
2491 * PARAMETERS
2492 * host: msdc host manipulator pointer
2493 * param: enum of msdc parameter
2494 * value: value of msdc parameter
2495 * rw: AUTOK_READ/AUTOK_WRITE
2496 *
2497 * RETURN VALUES
2498 * error code: 0 success,
2499 * -1 parameter input error
2500 * -2 read/write fail
2501 * -3 else error
2502 *************************************************************************
2503 */
2504static int autok_adjust_param(struct msdc_host *host,
2505 enum AUTOK_PARAM param,
2506 u32 *value,
2507 int rw)
2508{
2509 void __iomem *base = host->base;
2510 void __iomem *top_base = host->top_base;
2511 u32 *reg;
2512 u32 field = 0;
2513
2514 switch (param) {
2515 case READ_DATA_SMPL_SEL:
2516 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2517 pr_debug("READ_DATA_SMPL_SEL(%d) is out of [0~1]\n",
2518 *value);
2519 return -1;
2520 }
2521
2522 reg = (u32 *) (base + MSDC_IOCON);
2523 field = (u32) (MSDC_IOCON_R_D_SMPL_SEL);
2524 break;
2525 case WRITE_DATA_SMPL_SEL:
2526 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2527 pr_debug("WRITE_DATA_SMPL_SEL(%d) is out of [0~1]\n",
2528 *value);
2529 return -1;
2530 }
2531
2532 reg = (u32 *) (base + MSDC_IOCON);
2533 field = (u32) (MSDC_IOCON_W_D_SMPL_SEL);
2534 break;
2535 case DATA_DLYLINE_SEL:
2536 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2537 pr_debug("DATA_DLYLINE_SEL(%d) is out of [0~1]\n",
2538 *value);
2539 return -1;
2540 }
2541 if (top_base) {
2542 reg = (u32 *) (top_base + EMMC_TOP_CONTROL);
2543 field = (u32) (DATA_K_VALUE_SEL);
2544 } else {
2545 reg = (u32 *) (base + MSDC_IOCON);
2546 field = (u32) (MSDC_IOCON_DDLSEL);
2547 }
2548 break;
2549 case MSDC_DAT_TUNE_SEL: /* 0-Dat tune 1-CLk tune ; */
2550 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2551 pr_debug("DATA_TUNE_SEL(%d) is out of [0~1]\n",
2552 *value);
2553 return -1;
2554 }
2555 if (top_base) {
2556 reg = (u32 *) (top_base + EMMC_TOP_CONTROL);
2557 field = (u32) (PAD_RXDLY_SEL);
2558 } else {
2559 reg = (u32 *) (base + MSDC_PAD_TUNE0);
2560 field = (u32) (MSDC_PAD_TUNE0_RXDLYSEL);
2561 }
2562 break;
2563 case MSDC_WCRC_ASYNC_FIFO_SEL:
2564 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2565 pr_debug("WCRC_ASYNC_FIFO_SEL(%d) is out of [0~1]\n",
2566 *value);
2567 return -1;
2568 }
2569 reg = (u32 *) (base + MSDC_PATCH_BIT2);
2570 field = (u32) (MSDC_PB2_CFGCRCSTS);
2571 break;
2572 case MSDC_RESP_ASYNC_FIFO_SEL:
2573 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2574 pr_debug("RESP_ASYNC_FIFO_SEL(%d) is out of [0~1]\n",
2575 *value);
2576 return -1;
2577 }
2578 reg = (u32 *) (base + MSDC_PATCH_BIT2);
2579 field = (u32) (MSDC_PB2_CFGRESP);
2580 break;
2581 case CMD_EDGE:
2582 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2583 pr_debug("CMD_EDGE(%d) is out of [0~1]\n", *value);
2584 return -1;
2585 }
2586 reg = (u32 *) (base + MSDC_IOCON);
2587 field = (u32) (MSDC_IOCON_RSPL);
2588 break;
2589 case RDATA_EDGE:
2590 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2591 pr_debug("RDATA_EDGE(%d) is out of [0~1]\n", *value);
2592 return -1;
2593 }
2594 reg = (u32 *) (base + MSDC_IOCON);
2595 field = (u32) (MSDC_IOCON_R_D_SMPL);
2596 break;
2597 case RD_FIFO_EDGE:
2598 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2599 pr_debug("RD_FIFO_EDGE(%d) is out of [0~1]\n", *value);
2600 return -1;
2601 }
2602 reg = (u32 *) (base + MSDC_PATCH_BIT0);
2603 field = (u32) (MSDC_PB0_RD_DAT_SEL);
2604 break;
2605 case WD_FIFO_EDGE:
2606 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2607 pr_debug("WD_FIFO_EDGE(%d) is out of [0~1]\n", *value);
2608 return -1;
2609 }
2610 reg = (u32 *) (base + MSDC_PATCH_BIT2);
2611 field = (u32) (MSDC_PB2_CFGCRCSTSEDGE);
2612 break;
2613 case CMD_RD_D_DLY1:
2614 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2615 pr_debug("CMD_RD_D_DLY1(%d) is out of [0~31]\n",
2616 *value);
2617 return -1;
2618 }
2619 if (top_base) {
2620 reg = (u32 *) (top_base + EMMC_TOP_CMD);
2621 field = (u32) (PAD_CMD_RXDLY);
2622 } else {
2623 reg = (u32 *) (base + MSDC_PAD_TUNE0);
2624 field = (u32) (MSDC_PAD_TUNE0_CMDRDLY);
2625 }
2626 break;
2627 case CMD_RD_D_DLY1_SEL:
2628 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2629 pr_debug("CMD_RD_D_DLY1_SEL(%d) is out of [0~1]\n",
2630 *value);
2631 return -1;
2632 }
2633 if (top_base) {
2634 reg = (u32 *) (top_base + EMMC_TOP_CMD);
2635 field = (u32) (PAD_CMD_RD_RXDLY_SEL);
2636 } else {
2637 reg = (u32 *) (base + MSDC_PAD_TUNE0);
2638 field = (u32) (MSDC_PAD_TUNE0_CMDRRDLYSEL);
2639 }
2640 break;
2641 case CMD_RD_D_DLY2:
2642 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2643 pr_debug("CMD_RD_D_DLY2(%d) is out of [0~31]\n",
2644 *value);
2645 return -1;
2646 }
2647 if (top_base) {
2648 reg = (u32 *) (top_base + EMMC_TOP_CMD);
2649 field = (u32) (PAD_CMD_RXDLY2);
2650 } else {
2651 reg = (u32 *) (base + MSDC_PAD_TUNE1);
2652 field = (u32) (MSDC_PAD_TUNE1_CMDRDLY2);
2653 }
2654 break;
2655 case CMD_RD_D_DLY2_SEL:
2656 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2657 pr_debug("CMD_RD_D_DLY2_SEL(%d) is out of [0~1]\n",
2658 *value);
2659 return -1;
2660 }
2661 if (top_base) {
2662 reg = (u32 *) (top_base + EMMC_TOP_CMD);
2663 field = (u32) (PAD_CMD_RD_RXDLY2_SEL);
2664 } else {
2665 reg = (u32 *) (base + MSDC_PAD_TUNE1);
2666 field = (u32) (MSDC_PAD_TUNE1_CMDRRDLY2SEL);
2667 }
2668 break;
2669 case DAT_RD_D_DLY1:
2670 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2671 pr_debug("DAT_RD_D_DLY1(%d) is out of [0~31]\n",
2672 *value);
2673 return -1;
2674 }
2675 if (top_base) {
2676 reg = (u32 *) (top_base + EMMC_TOP_CONTROL);
2677 field = (u32) (PAD_DAT_RD_RXDLY);
2678 } else {
2679 reg = (u32 *) (base + MSDC_PAD_TUNE0);
2680 field = (u32) (MSDC_PAD_TUNE0_DATRRDLY);
2681 }
2682 break;
2683 case DAT_RD_D_DLY1_SEL:
2684 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2685 pr_debug("DAT_RD_D_DLY1_SEL(%d) is out of [0~1]\n",
2686 *value);
2687 return -1;
2688 }
2689 if (top_base) {
2690 reg = (u32 *) (top_base + EMMC_TOP_CONTROL);
2691 field = (u32) (PAD_DAT_RD_RXDLY_SEL);
2692 } else {
2693 reg = (u32 *) (base + MSDC_PAD_TUNE0);
2694 field = (u32) (MSDC_PAD_TUNE0_DATRRDLYSEL);
2695 }
2696 break;
2697 case DAT_RD_D_DLY2:
2698 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2699 pr_debug("DAT_RD_D_DLY2(%d) is out of [0~31]\n",
2700 *value);
2701 return -1;
2702 }
2703 if (top_base) {
2704 reg = (u32 *) (top_base + EMMC_TOP_CONTROL);
2705 field = (u32) (PAD_DAT_RD_RXDLY2);
2706 } else {
2707 reg = (u32 *) (base + MSDC_PAD_TUNE1);
2708 field = (u32) (MSDC_PAD_TUNE1_DATRRDLY2);
2709 }
2710 break;
2711 case DAT_RD_D_DLY2_SEL:
2712 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2713 pr_debug("DAT_RD_D_DLY2_SEL(%d) is out of [0~1]\n",
2714 *value);
2715 return -1;
2716 }
2717 if (top_base) {
2718 reg = (u32 *) (top_base + EMMC_TOP_CONTROL);
2719 field = (u32) (PAD_DAT_RD_RXDLY2_SEL);
2720 } else {
2721 reg = (u32 *) (base + MSDC_PAD_TUNE1);
2722 field = (u32) (MSDC_PAD_TUNE1_DATRRDLY2SEL);
2723 }
2724 break;
2725 case INT_DAT_LATCH_CK:
2726 if ((rw == AUTOK_WRITE) && (*value > 7)) {
2727 pr_debug("INT_DAT_LATCH_CK(%d) is out of [0~7]\n",
2728 *value);
2729 return -1;
2730 }
2731 reg = (u32 *) (base + MSDC_PATCH_BIT0);
2732 field = (u32) (MSDC_PB0_INT_DAT_LATCH_CK_SEL);
2733 break;
2734 case CKGEN_MSDC_DLY_SEL:
2735 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2736 pr_debug("CKGEN_MSDC_DLY_SEL(%d) is out of [0~31]\n",
2737 *value);
2738 return -1;
2739 }
2740 reg = (u32 *) (base + MSDC_PATCH_BIT0);
2741 field = (u32) (MSDC_PB0_CKGEN_MSDC_DLY_SEL);
2742 break;
2743 case CMD_RSP_TA_CNTR:
2744 if ((rw == AUTOK_WRITE) && (*value > 7)) {
2745 pr_debug("CMD_RSP_TA_CNTR(%d) is out of [0~7]\n",
2746 *value);
2747 return -1;
2748 }
2749 reg = (u32 *) (base + MSDC_PATCH_BIT1);
2750 field = (u32) (MSDC_PB1_CMD_RSP_TA_CNTR);
2751 break;
2752 case WRDAT_CRCS_TA_CNTR:
2753 if ((rw == AUTOK_WRITE) && (*value > 7)) {
2754 pr_debug("WRDAT_CRCS_TA_CNTR(%d) is out of [0~7]\n",
2755 *value);
2756 return -1;
2757 }
2758 reg = (u32 *) (base + MSDC_PATCH_BIT1);
2759 field = (u32) (MSDC_PB1_WRDAT_CRCS_TA_CNTR);
2760 break;
2761 case PAD_CLK_TXDLY_AUTOK:
2762 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2763 pr_debug("PAD_CLK_TXDLY(%d) is out of [0~31]\n",
2764 *value);
2765 return -1;
2766 }
2767 if (top_base) {
2768 reg = (u32 *) (top_base + TOP_EMMC50_PAD_CTL0);
2769 field = (u32) (PAD_CLK_TXDLY);
2770 } else {
2771 reg = (u32 *) (base + MSDC_PAD_TUNE0);
2772 field = (u32) (MSDC_PAD_TUNE0_CLKTXDLY);
2773 }
2774 break;
2775 case EMMC50_WDATA_MUX_EN:
2776 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2777 pr_debug("EMMC50_WDATA_MUX_EN(%d) is out of [0~1]\n",
2778 *value);
2779 return -1;
2780 }
2781 reg = (u32 *) (base + EMMC50_CFG0);
2782 field = (u32) (MSDC_EMMC50_CFG_CRC_STS_SEL);
2783 break;
2784 case EMMC50_CMD_MUX_EN:
2785 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2786 pr_debug("EMMC50_CMD_MUX_EN(%d) is out of [0~1]\n",
2787 *value);
2788 return -1;
2789 }
2790 reg = (u32 *) (base + EMMC50_CFG0);
2791 field = (u32) (MSDC_EMMC50_CFG_CMD_RESP_SEL);
2792 break;
2793 case EMMC50_WDATA_EDGE:
2794 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2795 pr_debug("EMMC50_WDATA_EDGE(%d) is out of [0~1]\n",
2796 *value);
2797 return -1;
2798 }
2799 reg = (u32 *) (base + EMMC50_CFG0);
2800 field = (u32) (MSDC_EMMC50_CFG_CRC_STS_EDGE);
2801 break;
2802 case EMMC50_DS_Z_DLY1:
2803 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2804 pr_debug("EMMC50_DS_Z_DLY1(%d) is out of [0~31]\n",
2805 *value);
2806 return -1;
2807 }
2808 if (top_base) {
2809 reg = (u32 *) (top_base + TOP_EMMC50_PAD_DS_TUNE);
2810 field = (u32) (PAD_DS_DLY1);
2811 } else {
2812 reg = (u32 *) (base + EMMC50_PAD_DS_TUNE);
2813 field = (u32) (MSDC_EMMC50_PAD_DS_TUNE_DLY1);
2814 }
2815 break;
2816 case EMMC50_DS_Z_DLY1_SEL:
2817 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2818 pr_debug("EMMC50_DS_Z_DLY1_SEL(%d) is out of [0~1]\n",
2819 *value);
2820 return -1;
2821 }
2822 if (top_base) {
2823 reg = (u32 *) (top_base + TOP_EMMC50_PAD_DS_TUNE);
2824 field = (u32) (PAD_DS_DLY_SEL);
2825 } else {
2826 reg = (u32 *) (base + EMMC50_PAD_DS_TUNE);
2827 field = (u32) (MSDC_EMMC50_PAD_DS_TUNE_DLYSEL);
2828 }
2829 break;
2830 case EMMC50_DS_Z_DLY2:
2831 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2832 pr_debug("EMMC50_DS_Z_DLY2(%d) is out of [0~31]\n",
2833 *value);
2834 return -1;
2835 }
2836 if (top_base) {
2837 reg = (u32 *) (top_base + TOP_EMMC50_PAD_DS_TUNE);
2838 field = (u32) (PAD_DS_DLY2);
2839 } else {
2840 reg = (u32 *) (base + EMMC50_PAD_DS_TUNE);
2841 field = (u32) (MSDC_EMMC50_PAD_DS_TUNE_DLY2);
2842 }
2843 break;
2844 case EMMC50_DS_Z_DLY2_SEL:
2845 if ((rw == AUTOK_WRITE) && (*value > 1)) {
2846 pr_debug("EMMC50_DS_Z_DLY2_SEL(%d) is out of [0~1]\n",
2847 *value);
2848 return -1;
2849 }
2850 if (top_base) {
2851 reg = (u32 *) (top_base + TOP_EMMC50_PAD_DS_TUNE);
2852 field = (u32) (PAD_DS_DLY2_SEL);
2853 } else {
2854 reg = (u32 *) (base + EMMC50_PAD_DS_TUNE);
2855 field = (u32) (MSDC_EMMC50_PAD_DS_TUNE_DLY2SEL);
2856 }
2857 break;
2858 case EMMC50_DS_ZDLY_DLY:
2859 if ((rw == AUTOK_WRITE) && (*value > 31)) {
2860 pr_debug("EMMC50_DS_Z_DLY(%d) is out of [0~31]\n",
2861 *value);
2862 return -1;
2863 }
2864 if (top_base) {
2865 reg = (u32 *) (top_base + TOP_EMMC50_PAD_DS_TUNE);
2866 field = (u32) (PAD_DS_DLY3);
2867 } else {
2868 reg = (u32 *) (base + EMMC50_PAD_DS_TUNE);
2869 field = (u32) (MSDC_EMMC50_PAD_DS_TUNE_DLY3);
2870 }
2871 break;
2872 default:
2873 pr_debug("Value of [enum AUTOK_PARAM param] is wrong\n");
2874 return -1;
2875 }
2876
2877 if (rw == AUTOK_READ)
2878 sdr_get_field(reg, field, value);
2879 else if (rw == AUTOK_WRITE) {
2880 sdr_set_field(reg, field, *value);
2881 if (param == CKGEN_MSDC_DLY_SEL)
2882 mdelay(1);
2883 } else {
2884 pr_debug("Value of [int rw] is wrong\n");
2885 return -1;
2886 }
2887
2888 return 0;
2889}
2890
2891static int autok_param_update(enum AUTOK_PARAM param_id,
2892 unsigned int result, u8 *autok_tune_res)
2893{
2894 if (param_id < TUNING_PARAM_COUNT) {
2895 if ((result > autok_param_info[param_id].range.end) ||
2896 (result < autok_param_info[param_id].range.start)) {
2897 ATK_ERR("[AUTOK]param:%d out of range[%d,%d]\n",
2898 result,
2899 autok_param_info[param_id].range.start,
2900 autok_param_info[param_id].range.end);
2901 return -1;
2902 }
2903 autok_tune_res[param_id] = (u8) result;
2904 return 0;
2905 }
2906 ATK_ERR("[AUTOK]param not found\r\n");
2907
2908 return -1;
2909}
2910
2911static int autok_param_apply(struct msdc_host *host, u8 *autok_tune_res)
2912{
2913 unsigned int i = 0;
2914 unsigned int value = 0;
2915
2916 for (i = 0; i < TUNING_PARAM_COUNT; i++) {
2917 value = (u8) autok_tune_res[i];
2918 autok_adjust_param(host, i, &value, AUTOK_WRITE);
2919 }
2920
2921 return 0;
2922}
2923
2924void autok_tuning_parameter_init(struct msdc_host *host, u8 *res)
2925{
2926 unsigned int ret = 0;
2927 /* void __iomem *base = host->base; */
2928
2929 /* MSDC_SET_FIELD(MSDC_PATCH_BIT2, 7<<29, 2); */
2930 /* MSDC_SET_FIELD(MSDC_PATCH_BIT2, 7<<16, 4); */
2931
2932 ret = autok_param_apply(host, res);
2933}
2934
2935static int autok_result_dump(struct msdc_host *host, u8 *autok_tune_res)
2936{
2937 ATK_ERR("[AUTOK]CMD [EDGE:%d DLY1:%d DLY2:%d ]\n",
2938 autok_tune_res[0], autok_tune_res[4], autok_tune_res[6]);
2939 ATK_ERR("[AUTOK]DAT [RDAT_EDGE:%d RD_FIFO_EDGE:%d WD_FIFO_EDGE:%d]\n",
2940 autok_tune_res[1], autok_tune_res[2], autok_tune_res[3]);
2941 ATK_ERR("[AUTOK]DAT [LATCH_CK:%d DLY1:%d DLY2:%d ]\n",
2942 autok_tune_res[12], autok_tune_res[8], autok_tune_res[10]);
2943 ATK_ERR("[AUTOK]DS [DLY1:%d DLY2:%d DLY3:%d]\n",
2944 autok_tune_res[13], autok_tune_res[15], autok_tune_res[17]);
2945
2946 return 0;
2947}
2948
2949/* online tuning for latch ck */
2950int autok_execute_tuning_latch_ck(struct msdc_host *host, unsigned int opcode,
2951 unsigned int latch_ck_initail_value)
2952{
2953 unsigned int ret = 0;
2954 unsigned int j, k;
2955 void __iomem *base = host->base;
2956 unsigned int tune_time;
2957
2958 writel(0xffffffff, base + MSDC_INT);
2959 tune_time = AUTOK_LATCH_CK_SDIO_TUNE_TIMES;
2960 for (j = latch_ck_initail_value; j < 8;
2961 j += (host->src_clk_freq / host->sclk)) {
2962 host->tune_latch_ck_cnt = 0;
2963 msdc_clear_fifo();
2964 sdr_set_field(base + MSDC_PATCH_BIT0,
2965 MSDC_PB0_INT_DAT_LATCH_CK_SEL, j);
2966 for (k = 0; k < tune_time; k++) {
2967 if (opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2968 switch (k) {
2969 case 0:
2970 host->tune_latch_ck_cnt = 1;
2971 break;
2972 default:
2973 host->tune_latch_ck_cnt = k;
2974 break;
2975 }
2976 } else if (opcode == MMC_SEND_TUNING_BLOCK) {
2977 switch (k) {
2978 case 0:
2979 case 1:
2980 case 2:
2981 host->tune_latch_ck_cnt = 1;
2982 break;
2983 default:
2984 host->tune_latch_ck_cnt = k - 1;
2985 break;
2986 }
2987 } else if (opcode == MMC_SEND_EXT_CSD) {
2988 host->tune_latch_ck_cnt = k + 1;
2989 } else
2990 host->tune_latch_ck_cnt++;
2991 ret = autok_send_tune_cmd(host, opcode, TUNE_LATCH_CK);
2992 if ((ret &
2993 (E_RESULT_CMD_TMO | E_RESULT_RSP_CRC)) != 0) {
2994 ATK_ERR("[AUTOK]CMD Fail when tune LATCH CK\n");
2995 break;
2996 } else if ((ret &
2997 (E_RESULT_DAT_CRC |
2998 E_RESULT_DAT_TMO)) != 0) {
2999 ATK_ERR("[AUTOK]Tune LATCH_CK error %d\r\n", j);
3000 break;
3001 }
3002 }
3003 if (ret == 0) {
3004 sdr_set_field(base + MSDC_PATCH_BIT0,
3005 MSDC_PB0_INT_DAT_LATCH_CK_SEL, j);
3006 break;
3007 }
3008 }
3009 host->tune_latch_ck_cnt = 0;
3010 return j;
3011}
3012
3013/*
3014 ******************************************************
3015 * Function: msdc_autok_adjust_paddly *
3016 * Param : value - delay cnt from 0 to 63 *
3017 * pad_sel - 0 for cmd pad and 1 for data pad *
3018 ******************************************************
3019 */
3020#define CMD_PAD_RDLY 0
3021#define DAT_PAD_RDLY 1
3022#define DS_PAD_RDLY 2
3023static void msdc_autok_adjust_paddly(struct msdc_host *host,
3024 unsigned int *value,
3025 unsigned int pad_sel)
3026{
3027 unsigned int uCfgL = 0;
3028 unsigned int uCfgLSel = 0;
3029 unsigned int uCfgH = 0;
3030 unsigned int uCfgHSel = 0;
3031 unsigned int dly_cnt = *value;
3032
3033 uCfgL = (dly_cnt > 31) ? (31) : dly_cnt;
3034 uCfgH = (dly_cnt > 31) ? (dly_cnt - 32) : 0;
3035
3036 uCfgLSel = (uCfgL > 0) ? 1 : 0;
3037 uCfgHSel = (uCfgH > 0) ? 1 : 0;
3038 switch (pad_sel) {
3039 case CMD_PAD_RDLY:
3040 autok_adjust_param(host, CMD_RD_D_DLY1, &uCfgL, AUTOK_WRITE);
3041 autok_adjust_param(host, CMD_RD_D_DLY2, &uCfgH, AUTOK_WRITE);
3042
3043 autok_adjust_param(host, CMD_RD_D_DLY1_SEL,
3044 &uCfgLSel, AUTOK_WRITE);
3045 autok_adjust_param(host, CMD_RD_D_DLY2_SEL,
3046 &uCfgHSel, AUTOK_WRITE);
3047 break;
3048 case DAT_PAD_RDLY:
3049 autok_adjust_param(host, DAT_RD_D_DLY1, &uCfgL, AUTOK_WRITE);
3050 autok_adjust_param(host, DAT_RD_D_DLY2, &uCfgH, AUTOK_WRITE);
3051
3052 autok_adjust_param(host, DAT_RD_D_DLY1_SEL,
3053 &uCfgLSel, AUTOK_WRITE);
3054 autok_adjust_param(host, DAT_RD_D_DLY2_SEL,
3055 &uCfgHSel, AUTOK_WRITE);
3056 break;
3057 case DS_PAD_RDLY:
3058 autok_adjust_param(host, EMMC50_DS_Z_DLY1, &uCfgL, AUTOK_WRITE);
3059 autok_adjust_param(host, EMMC50_DS_Z_DLY2, &uCfgH, AUTOK_WRITE);
3060
3061 autok_adjust_param(host, EMMC50_DS_Z_DLY1_SEL,
3062 &uCfgLSel, AUTOK_WRITE);
3063 autok_adjust_param(host, EMMC50_DS_Z_DLY2_SEL,
3064 &uCfgHSel, AUTOK_WRITE);
3065 break;
3066 }
3067}
3068
3069static void autok_paddly_update(unsigned int pad_sel,
3070 unsigned int dly_cnt,
3071 u8 *autok_tune_res)
3072{
3073 unsigned int uCfgL = 0;
3074 unsigned int uCfgLSel = 0;
3075 unsigned int uCfgH = 0;
3076 unsigned int uCfgHSel = 0;
3077
3078 uCfgL = (dly_cnt > 31) ? (31) : dly_cnt;
3079 uCfgH = (dly_cnt > 31) ? (dly_cnt - 32) : 0;
3080
3081 uCfgLSel = (uCfgL > 0) ? 1 : 0;
3082 uCfgHSel = (uCfgH > 0) ? 1 : 0;
3083 switch (pad_sel) {
3084 case CMD_PAD_RDLY:
3085 autok_param_update(CMD_RD_D_DLY1, uCfgL, autok_tune_res);
3086 autok_param_update(CMD_RD_D_DLY2, uCfgH, autok_tune_res);
3087
3088 autok_param_update(CMD_RD_D_DLY1_SEL, uCfgLSel, autok_tune_res);
3089 autok_param_update(CMD_RD_D_DLY2_SEL, uCfgHSel, autok_tune_res);
3090 break;
3091 case DAT_PAD_RDLY:
3092 autok_param_update(DAT_RD_D_DLY1, uCfgL, autok_tune_res);
3093 autok_param_update(DAT_RD_D_DLY2, uCfgH, autok_tune_res);
3094
3095 autok_param_update(DAT_RD_D_DLY1_SEL, uCfgLSel, autok_tune_res);
3096 autok_param_update(DAT_RD_D_DLY2_SEL, uCfgHSel, autok_tune_res);
3097 break;
3098 case DS_PAD_RDLY:
3099 autok_param_update(EMMC50_DS_Z_DLY1, uCfgL, autok_tune_res);
3100 autok_param_update(EMMC50_DS_Z_DLY2, uCfgH, autok_tune_res);
3101
3102 autok_param_update(EMMC50_DS_Z_DLY1_SEL,
3103 uCfgLSel, autok_tune_res);
3104 autok_param_update(EMMC50_DS_Z_DLY2_SEL,
3105 uCfgHSel, autok_tune_res);
3106 break;
3107 }
3108}
3109
3110/*
3111 ******************************************************
3112 * Exectue tuning IF Implenment *
3113 ******************************************************
3114 */
3115static int autok_write_param(struct msdc_host *host,
3116 enum AUTOK_PARAM param, u32 value)
3117{
3118 autok_adjust_param(host, param, &value, AUTOK_WRITE);
3119
3120 return 0;
3121}
3122
3123int autok_path_sel(struct msdc_host *host)
3124{
3125 void __iomem *base = host->base;
3126
3127 autok_write_param(host, READ_DATA_SMPL_SEL, 0);
3128 autok_write_param(host, WRITE_DATA_SMPL_SEL, 0);
3129
3130 /* clK tune all data Line share dly */
3131 autok_write_param(host, DATA_DLYLINE_SEL, 0);
3132
3133 /* data tune mode select */
3134#if defined(CHIP_DENALI_3_DAT_TUNE)
3135 autok_write_param(host, MSDC_DAT_TUNE_SEL, 1);
3136#else
3137 autok_write_param(host, MSDC_DAT_TUNE_SEL, 0);
3138#endif
3139 autok_write_param(host, MSDC_WCRC_ASYNC_FIFO_SEL, 1);
3140 autok_write_param(host, MSDC_RESP_ASYNC_FIFO_SEL, 0);
3141
3142 /* eMMC50 Function Mux */
3143 /* write path switch to emmc45 */
3144 autok_write_param(host, EMMC50_WDATA_MUX_EN, 0);
3145
3146 /* response path switch to emmc45 */
3147 autok_write_param(host, EMMC50_CMD_MUX_EN, 0);
3148 autok_write_param(host, EMMC50_WDATA_EDGE, 0);
3149
3150 /* Common Setting Config */
3151 autok_write_param(host, CKGEN_MSDC_DLY_SEL, AUTOK_CKGEN_VALUE);
3152 autok_write_param(host, CMD_RSP_TA_CNTR, AUTOK_CMD_TA_VALUE);
3153 autok_write_param(host, WRDAT_CRCS_TA_CNTR, AUTOK_CRC_TA_VALUE);
3154
3155 sdr_set_field(base + MSDC_PATCH_BIT1, MSDC_PB1_GET_BUSY_MA,
3156 AUTOK_BUSY_MA_VALUE);
3157 sdr_set_field(base + MSDC_PATCH_BIT1, MSDC_PB1_GET_CRC_MA,
3158 AUTOK_CRC_MA_VALUE);
3159#ifdef AUTOK_PATH_SET
3160 sdr_set_field(base + MSDC_PATCH_BIT1, MSDC_PB1_STOP_DLY_SEL,
3161 AUTOK_STOP_DLY_SEL);
3162 sdr_set_field(base + MSDC_PATCH_BIT1, MSDC_PB1_POP_MARK_WATER,
3163 AUTOK_POP_MARK_WATER);
3164 sdr_set_field(base + MSDC_PATCH_BIT1, MSDC_PB1_STATE_CLEAR,
3165 AUTOK_STATE_CLEAR);
3166
3167 sdr_set_field(base + SDC_FIFO_CFG, MSDC_WR_VALID_SEL,
3168 AUTOK_WR_VALID_SEL);
3169 sdr_set_field(base + SDC_FIFO_CFG, MSDC_RD_VALID_SEL,
3170 AUTOK_RD_VALID_SEL);
3171#endif
3172 return 0;
3173}
3174
3175int autok_init_sdr104(struct msdc_host *host)
3176{
3177 void __iomem *base = host->base;
3178
3179 /* driver may miss data tune path setting in the interim */
3180 autok_path_sel(host);
3181
3182 /* if any specific config need modify add here */
3183 /* LATCH_TA_EN Config for WCRC Path non_HS400 */
3184 sdr_set_field(base + MSDC_PATCH_BIT2, MSDC_PB2_CRCSTSENSEL,
3185 AUTOK_CRC_LATCH_EN_NON_HS400_VALUE);
3186
3187 /* LATCH_TA_EN Config for CMD Path non_HS400 */
3188 sdr_set_field(base + MSDC_PATCH_BIT2, MSDC_PB2_RESPSTSENSEL,
3189 AUTOK_CMD_LATCH_EN_NON_HS400_VALUE);
3190
3191 return 0;
3192}
3193
3194/* online tuning for SDIO/SD */
3195int execute_online_tuning(struct msdc_host *host, u8 *res)
3196{
3197 void __iomem *base = host->base;
3198 unsigned int ret = 0;
3199 unsigned int uCmdEdge = 0;
3200 unsigned int uDatEdge = 0;
3201 u64 RawData64 = 0LL;
3202 unsigned int score = 0;
3203 unsigned int j, k;
3204 unsigned int opcode = MMC_SEND_TUNING_BLOCK;
3205 struct AUTOK_REF_INFO uCmdDatInfo;
3206 struct AUTOK_SCAN_RES *pBdInfo;
3207 char tune_result_str64[65];
3208 u8 p_autok_tune_res[TUNING_PARAM_COUNT];
3209
3210 autok_init_sdr104(host);
3211 memset((void *)p_autok_tune_res, 0,
3212 sizeof(p_autok_tune_res) / sizeof(u8));
3213
3214 /* Step1 : Tuning Cmd Path */
3215 autok_tuning_parameter_init(host, p_autok_tune_res);
3216 memset(&uCmdDatInfo, 0, sizeof(struct AUTOK_REF_INFO));
3217
3218 uCmdEdge = 0;
3219 do {
3220 pBdInfo = (struct AUTOK_SCAN_RES *)&
3221 (uCmdDatInfo.scan_info[uCmdEdge]);
3222 autok_adjust_param(host, CMD_EDGE, &uCmdEdge, AUTOK_WRITE);
3223 RawData64 = 0LL;
3224 for (j = 0; j < 64; j++) {
3225 msdc_autok_adjust_paddly(host, &j, CMD_PAD_RDLY);
3226 for (k = 0; k < AUTOK_CMD_TIMES / 2; k++) {
3227 ret = autok_send_tune_cmd(host, opcode,
3228 TUNE_CMD);
3229 if ((ret & E_RESULT_RSP_CRC) != 0) {
3230 RawData64 |= (u64) (1LL << j);
3231 break;
3232 } else if ((ret & E_RESULT_CMD_TMO) != 0) {
3233 autok_msdc_reset();
3234 msdc_clear_fifo();
3235 writel(0xffffffff, base + MSDC_INT);
3236 RawData64 |= (u64) (1LL << j);
3237 break;
3238 } else if ((ret & E_RESULT_FATAL_ERR) != 0)
3239 goto fail;
3240 }
3241 }
3242 score = autok_simple_score64(tune_result_str64, RawData64);
3243 ATK_DBG(ATK_RES, "[AUTOK]CMD %d \t %d \t %s\r\n",
3244 uCmdEdge, score, tune_result_str64);
3245 if (autok_check_scan_res64(RawData64, pBdInfo) != 0) {
3246 host->autok_error = -1;
3247 return -1;
3248 }
3249 #ifdef AUTOK_DEBUG
3250 ATK_DBG(ATK_RES,
3251 "[AUTOK]Edge:%d \t BoundaryCnt:%d \t FullBoundaryCnt:%d \t\n",
3252 uCmdEdge, pBdInfo->bd_cnt, pBdInfo->fbd_cnt);
3253
3254 for (i = 0; i < BD_MAX_CNT; i++) {
3255 ATK_DBG(ATK_RES,
3256 "[AUTOK]BoundInf[%d]: S:%d \t E:%d \t W:%d \t FullBound:%d\n",
3257 i, pBdInfo->bd_info[i].Bound_Start,
3258 pBdInfo->bd_info[i].Bound_End, pBdInfo->bd_info[i].Bound_width,
3259 pBdInfo->bd_info[i].is_fullbound);
3260 }
3261 #endif
3262
3263 uCmdEdge ^= 0x1;
3264 } while (uCmdEdge);
3265
3266 if (autok_pad_dly_sel(&uCmdDatInfo) == 0) {
3267 autok_param_update(CMD_EDGE, uCmdDatInfo.opt_edge_sel,
3268 p_autok_tune_res);
3269 autok_paddly_update(CMD_PAD_RDLY, uCmdDatInfo.opt_dly_cnt,
3270 p_autok_tune_res);
3271 } else {
3272 ATK_DBG(ATK_RES, "[AUTOK]======Analysis Fail!!=======\n");
3273 }
3274
3275 /* Step2 : Tuning Data Path */
3276 autok_tuning_parameter_init(host, p_autok_tune_res);
3277 memset(&uCmdDatInfo, 0, sizeof(struct AUTOK_REF_INFO));
3278
3279 uDatEdge = 0;
3280 do {
3281 pBdInfo = (struct AUTOK_SCAN_RES *)&
3282 (uCmdDatInfo.scan_info[uDatEdge]);
3283 autok_adjust_param(host, RD_FIFO_EDGE, &uDatEdge, AUTOK_WRITE);
3284 RawData64 = 0LL;
3285 for (j = 0; j < 64; j++) {
3286 msdc_autok_adjust_paddly(host, &j, DAT_PAD_RDLY);
3287 for (k = 0; k < AUTOK_CMD_TIMES / 2; k++) {
3288 ret = autok_send_tune_cmd(host, opcode,
3289 TUNE_DATA);
3290 if ((ret & (E_RESULT_CMD_TMO |
3291 E_RESULT_RSP_CRC)) != 0) {
3292 ATK_ERR("[AUTOK]Tune read CMD Fail\n");
3293 host->autok_error = -1;
3294 goto fail;
3295 } else if ((ret & (E_RESULT_DAT_CRC |
3296 E_RESULT_DAT_TMO)) != 0) {
3297 RawData64 |= (u64) (1LL << j);
3298 break;
3299 }
3300 }
3301 }
3302 score = autok_simple_score64(tune_result_str64, RawData64);
3303 ATK_DBG(ATK_RES, "[AUTOK]DAT %d \t %d \t %s\r\n",
3304 uDatEdge, score, tune_result_str64);
3305 if (autok_check_scan_res64(RawData64, pBdInfo) != 0) {
3306 host->autok_error = -1;
3307 return -1;
3308 }
3309 #ifdef AUTOK_DEBUG
3310 ATK_DBG(ATK_RES,
3311 "[AUTOK]Edge:%d \t BoundaryCnt:%d \t FullBoundaryCnt:%d \t\n",
3312 uDatEdge, pBdInfo->bd_cnt, pBdInfo->fbd_cnt);
3313
3314 for (i = 0; i < BD_MAX_CNT; i++) {
3315 ATK_DBG(ATK_RES,
3316 "[AUTOK]BoundInf[%d]: S:%d \t E:%d \t W:%d \t FullBound:%d\r\n",
3317 i, pBdInfo->bd_info[i].Bound_Start,
3318 pBdInfo->bd_info[i].Bound_End, pBdInfo->bd_info[i].Bound_width,
3319 pBdInfo->bd_info[i].is_fullbound);
3320 }
3321 #endif
3322
3323 uDatEdge ^= 0x1;
3324 } while (uDatEdge);
3325
3326 if (autok_pad_dly_sel(&uCmdDatInfo) == 0) {
3327 autok_param_update(RD_FIFO_EDGE, uCmdDatInfo.opt_edge_sel,
3328 p_autok_tune_res);
3329 autok_paddly_update(DAT_PAD_RDLY, uCmdDatInfo.opt_dly_cnt,
3330 p_autok_tune_res);
3331 autok_param_update(WD_FIFO_EDGE, uCmdDatInfo.opt_edge_sel,
3332 p_autok_tune_res);
3333 } else {
3334 ATK_DBG(ATK_RES, "[AUTOK][Error]=====Analysis Fail!!=======\n");
3335 }
3336
3337 autok_tuning_parameter_init(host, p_autok_tune_res);
3338
3339 /* Step3 : Tuning LATCH CK */
3340 p_autok_tune_res[INT_DAT_LATCH_CK] = autok_execute_tuning_latch_ck(host,
3341 opcode, p_autok_tune_res[INT_DAT_LATCH_CK]);
3342
3343 autok_result_dump(host, p_autok_tune_res);
3344 if (res != NULL) {
3345 memcpy((void *)res, (void *)p_autok_tune_res,
3346 sizeof(p_autok_tune_res) / sizeof(u8));
3347 }
3348 host->autok_error = 0;
3349
3350 return 0;
3351fail:
3352 return -1;
3353}
3354
3355int autok_execute_tuning(struct msdc_host *host, u8 *res)
3356{
3357 int ret = 0;
3358 struct timeval tm_s, tm_e;
3359 unsigned int tm_val = 0;
3360 unsigned int clk_pwdn = 0;
3361 unsigned int int_en = 0;
3362 unsigned int dtoc = 0;
3363 void __iomem *base = host->base;
3364
3365 do_gettimeofday(&tm_s);
3366
3367 int_en = readl(base + MSDC_INTEN);
3368 writel(0, base + MSDC_INTEN);
3369 sdr_get_field(base + MSDC_CFG, MSDC_CFG_CKPDN, &clk_pwdn);
3370 sdr_set_field(base + MSDC_CFG, MSDC_CFG_CKPDN, 1);
3371
3372 sdr_get_field(base + SDC_CFG, SDC_CFG_DTOC, &dtoc);
3373 sdr_set_field(base + SDC_CFG, SDC_CFG_DTOC, 0);
3374
3375 if (execute_online_tuning(host, res) != 0)
3376 ATK_ERR("[AUTOK] ========Error: Autok Failed========\n");
3377
3378 autok_msdc_reset();
3379 msdc_clear_fifo();
3380 writel(0xffffffff, base + MSDC_INT);
3381 writel(int_en, base + MSDC_INTEN);
3382 sdr_set_field(base + SDC_CFG, SDC_CFG_DTOC, dtoc);
3383 sdr_set_field(base + MSDC_CFG, MSDC_CFG_CKPDN, clk_pwdn);
3384
3385 do_gettimeofday(&tm_e);
3386 tm_val = (tm_e.tv_sec - tm_s.tv_sec) * 1000 +
3387 (tm_e.tv_usec - tm_s.tv_usec) / 1000;
3388 ATK_ERR("[AUTOK]=========Time Cost:%d ms========\n", tm_val);
3389
3390 return ret;
3391}
3392
3393static void msdc_dump_register(struct msdc_host *host)
3394{
3395 void __iomem *base = host->base;
3396 void __iomem *top_base = host->top_base;
3397
3398 pr_info("SDIO [0x%x] MSDC_CFG=0x%.8x\n",
3399 MSDC_CFG, readl(base + MSDC_CFG));
3400 pr_info("SDIO [0x%x] MSDC_IOCON=0x%.8x\n",
3401 MSDC_IOCON, readl(base + MSDC_IOCON));
3402 pr_info("SDIO [0x%x] MSDC_PATCH_BIT0=0x%.8x\n",
3403 MSDC_PATCH_BIT0, readl(base + MSDC_PATCH_BIT0));
3404 pr_info("SDIO [0x%x] MSDC_PATCH_BIT1=0x%.8x\n",
3405 MSDC_PATCH_BIT1, readl(base + MSDC_PATCH_BIT1));
3406 pr_info("SDIO [0x%x] MSDC_PATCH_BIT2=0x%.8x\n",
3407 MSDC_PATCH_BIT2, readl(base + MSDC_PATCH_BIT2));
3408 pr_info("SDIO [0x%x] MSDC_PAD_TUNE0=0x%.8x\n",
3409 MSDC_PAD_TUNE0, readl(base + MSDC_PAD_TUNE0));
3410 pr_info("SDIO [0x%x] MSDC_PAD_TUNE1=0x%.8x\n",
3411 MSDC_PAD_TUNE1, readl(base + MSDC_PAD_TUNE1));
3412 pr_info("SDIO [0x%x] SDC_FIFO_CFG=0x%.8x\n",
3413 SDC_FIFO_CFG, readl(base + SDC_FIFO_CFG));
3414
3415 if (top_base) {
3416 pr_info("SDIO [0x%x] EMMC_TOP_CONTROL=0x%.8x\n",
3417 EMMC_TOP_CONTROL,
3418 readl(top_base + EMMC_TOP_CONTROL));
3419 pr_info("SDIO [0x%x] TOP_EMMC50_PAD_CTL0=0x%.8x\n",
3420 TOP_EMMC50_PAD_CTL0,
3421 readl(top_base + TOP_EMMC50_PAD_CTL0));
3422 pr_info("SDIO [0x%x] TOP_EMMC50_PAD_DS_TUNE=0x%.8x\n",
3423 TOP_EMMC50_PAD_DS_TUNE,
3424 readl(top_base + TOP_EMMC50_PAD_DS_TUNE));
3425 }
3426}
3427
3428static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode)
3429{
3430 struct msdc_host *host = mmc_priv(mmc);
3431 u32 tune_reg = host->dev_comp->pad_tune_reg;
3432
3433 if (host->autok_done) {
3434 autok_init_sdr104(host);
3435 autok_param_apply(host, sdio_autok_res);
3436 } else {
3437 autok_execute_tuning(host, sdio_autok_res);
3438 host->autok_done = true;
3439 }
3440
3441 /* add for top_base */
3442 host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON);
3443 host->saved_tune_para.pad_tune = readl(host->base + tune_reg);
3444 host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE);
3445 if (host->top_base) {
3446 host->saved_tune_para.emmc_top_control = readl(host->top_base +
3447 EMMC_TOP_CONTROL);
3448 host->saved_tune_para.emmc_top_cmd = readl(host->top_base +
3449 EMMC_TOP_CMD);
3450 }
3451
3452 msdc_dump_register(host);
3453 return 0;
3454}
3455
3456static void msdc_hw_reset(struct mmc_host *mmc)
3457{
3458 struct msdc_host *host = mmc_priv(mmc);
3459
3460 sdr_set_bits(host->base + EMMC_IOCON, 1);
3461 udelay(10); /* 10us is enough */
3462 sdr_clr_bits(host->base + EMMC_IOCON, 1);
3463}
3464
3465/*
3466 * msdc_recheck_sdio_irq - recheck whether the SDIO IRQ is lost
3467 * @host: The host to check.
3468 *
3469 * Host controller may lost interrupt in some special case.
3470 * Add sdio IRQ recheck mechanism to make sure all interrupts
3471 * can be processed immediately
3472 *
3473 */
3474static void msdc_recheck_sdio_irq(struct msdc_host *host)
3475{
3476 u32 reg_int, reg_ps, reg_inten;
3477
3478 reg_inten = readl(host->base + MSDC_INTEN);
3479 if (host->clock_on && (host->mmc->caps & MMC_CAP_SDIO_IRQ)
3480 && host->irq_thread_alive &&
3481 (reg_inten & MSDC_INTEN_SDIOIRQ)) {
3482 reg_int = readl(host->base + MSDC_INT);
3483 reg_ps = readl(host->base + MSDC_PS);
3484 if (!((reg_int & MSDC_INT_SDIOIRQ) || (reg_ps & MSDC_PS_DATA1)))
3485 mmc_signal_sdio_irq(host->mmc);
3486 }
3487}
3488
3489static void msdc_enable_sdio_irq(struct mmc_host *mmc, int enable)
3490{
3491 unsigned long flags;
3492 struct msdc_host *host = mmc_priv(mmc);
3493
3494 host->irq_thread_alive = true;
3495
3496#ifdef SUPPORT_LEGACY_SDIO
3497 if (host->cap_eirq) {
3498 if (enable)
3499 host->enable_sdio_eirq(); /* combo_sdio_enable_eirq */
3500 else
3501 host->disable_sdio_eirq(); /* combo_sdio_disable_eirq */
3502 return;
3503 }
3504#endif
3505
3506 if (enable) {
3507 pm_runtime_get_sync(host->dev);
3508 spin_lock_irqsave(&host->irqlock, flags);
3509 sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE);
3510 sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ);
3511 spin_unlock_irqrestore(&host->irqlock, flags);
3512 pm_runtime_mark_last_busy(host->dev);
3513 pm_runtime_put_autosuspend(host->dev);
3514 } else {
3515 spin_lock_irqsave(&host->irqlock, flags);
3516 sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ);
3517 /*
3518 *if no msdc_recheck_sdio_irq(), then no
3519 * race condition of disable_irq
3520 * twice and only enable_irq once time.
3521 */
3522 if (likely(host->sdio_irq_cnt > 0)) {
3523 disable_irq_nosync(host->eint_irq);
3524 host->sdio_irq_cnt--;
3525#ifdef SDIO_EAI_SUPPORT
3526 if (mmc->card && (mmc->card->cccr.eai == 0))
3527 pm_runtime_put_noidle(host->dev);
3528#endif
3529 }
3530 spin_unlock_irqrestore(&host->irqlock, flags);
3531 }
3532}
3533
3534static struct mmc_host_ops mt_msdc_ops = {
3535 .post_req = msdc_post_req,
3536 .pre_req = msdc_pre_req,
3537 .request = msdc_ops_request,
3538 .set_ios = msdc_ops_set_ios,
3539 .get_ro = mmc_gpio_get_ro,
3540 .start_signal_voltage_switch = msdc_ops_switch_volt,
3541 .card_busy = msdc_card_busy,
3542 .execute_tuning = msdc_execute_tuning,
3543 .hw_reset = msdc_hw_reset,
3544 .enable_sdio_irq = msdc_enable_sdio_irq,
3545};
3546
3547static irqreturn_t sdio_eint_irq(int irq, void *dev_id)
3548{
3549 struct msdc_host *host = (struct msdc_host *)dev_id;
3550
3551 mmc_signal_sdio_irq(host->mmc);
3552
3553 return IRQ_HANDLED;
3554}
3555
3556static int request_dat1_eint_irq(struct msdc_host *host)
3557{
3558 struct gpio_desc *desc;
3559 int ret = 0;
3560 int irq;
3561
3562 desc = devm_gpiod_get_index(host->dev, "eint", 0, GPIOD_IN);
3563 if (IS_ERR(desc))
3564 return PTR_ERR(desc);
3565
3566 irq = gpiod_to_irq(desc);
3567 if (irq >= 0) {
3568 irq_set_status_flags(irq, IRQ_NOAUTOEN);
3569 ret = devm_request_threaded_irq(host->dev, irq,
3570 NULL, sdio_eint_irq,
3571 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
3572 "sdio-eint", host);
3573 } else {
3574 ret = irq;
3575 }
3576
3577 host->eint_irq = irq;
3578 return ret;
3579}
3580
3581#ifdef SUPPORT_LEGACY_SDIO
3582/* For backward compatible, remove later */
3583int wait_sdio_autok_ready(void *data)
3584{
3585 return 0;
3586}
3587EXPORT_SYMBOL(wait_sdio_autok_ready);
3588
3589static void register_legacy_sdio_apis(struct msdc_host *host)
3590{
3591 if (!mt_sdio_ops) {
3592 pr_info("%s: WARN mt_sdio_ops = NULL\n",
3593 mmc_hostname(host->mmc));
3594 return;
3595 }
3596
3597 host->request_sdio_eirq = mt_sdio_ops[SDIO_USE_PORT].sdio_request_eirq;
3598 host->enable_sdio_eirq = mt_sdio_ops[SDIO_USE_PORT].sdio_enable_eirq;
3599 host->disable_sdio_eirq = mt_sdio_ops[SDIO_USE_PORT].sdio_disable_eirq;
3600 host->register_pm = mt_sdio_ops[SDIO_USE_PORT].sdio_register_pm;
3601}
3602
3603static void msdc_eirq_sdio(void *data)
3604{
3605 struct msdc_host *host = (struct msdc_host *)data;
3606
3607 mmc_signal_sdio_irq(host->mmc);
3608}
3609
3610static void msdc_pm(pm_message_t state, void *data)
3611{
3612 struct msdc_host *host = (struct msdc_host *)data;
3613
3614 int evt = state.event;
3615
3616 if ((evt == PM_EVENT_SUSPEND) || (evt == PM_EVENT_USER_SUSPEND)) {
3617 if (host->suspend != 0)
3618 return;
3619
3620 pr_info("msdc%d -> %s Suspend\n", SDIO_USE_PORT,
3621 evt == PM_EVENT_SUSPEND ? "PM" : "USR");
3622 host->suspend = 1;
3623 host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
3624 mmc_remove_host(host->mmc);
3625 }
3626
3627 if ((evt == PM_EVENT_RESUME) || (evt == PM_EVENT_USER_RESUME)) {
3628 if (host->suspend == 0)
3629 return;
3630
3631 pr_info("msdc%d -> %s Resume\n", SDIO_USE_PORT,
3632 evt == PM_EVENT_RESUME ? "PM" : "USR");
3633 host->suspend = 0;
3634 host->mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
3635 host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
3636 host->mmc->rescan_entered = 0;
3637 mmc_add_host(host->mmc);
3638 }
3639}
3640#endif
3641
3642static int msdc_drv_probe(struct platform_device *pdev)
3643{
3644 struct mmc_host *mmc;
3645 struct msdc_host *host;
3646 struct resource *res;
3647 const struct of_device_id *of_id;
3648 int ret;
3649
3650 if (!pdev->dev.of_node) {
3651 dev_info(&pdev->dev, "No DT found\n");
3652 return -EINVAL;
3653 }
3654
3655 of_id = of_match_node(msdc_of_ids, pdev->dev.of_node);
3656 if (!of_id)
3657 return -EINVAL;
3658
3659 /* Allocate MMC host for this device */
3660 mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev);
3661 if (!mmc)
3662 return -ENOMEM;
3663
3664 host = mmc_priv(mmc);
3665 ret = mmc_of_parse(mmc);
3666 if (ret)
3667 goto host_free;
3668
3669 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3670 host->base = devm_ioremap_resource(&pdev->dev, res);
3671 if (IS_ERR(host->base)) {
3672 ret = PTR_ERR(host->base);
3673 goto host_free;
3674 }
3675
3676 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
3677 host->top_base = devm_ioremap_resource(&pdev->dev, res);
3678 if (IS_ERR(host->top_base))
3679 host->top_base = NULL;
3680
3681 ret = mmc_regulator_get_supply(mmc);
3682 if (ret == -EPROBE_DEFER)
3683 goto host_free;
3684
3685 host->src_clk = devm_clk_get(&pdev->dev, "source");
3686 if (IS_ERR(host->src_clk)) {
3687 ret = PTR_ERR(host->src_clk);
3688 goto host_free;
3689 }
3690
3691 host->h_clk = devm_clk_get(&pdev->dev, "hclk");
3692 if (IS_ERR(host->h_clk)) {
3693 ret = PTR_ERR(host->h_clk);
3694 goto host_free;
3695 }
3696
3697 host->bus_clk = devm_clk_get(&pdev->dev, "bus_clk");
3698 if (IS_ERR(host->bus_clk))
3699 host->bus_clk = NULL;
3700 host->src_clk_cg = devm_clk_get(&pdev->dev, "source_cg");
3701 if (IS_ERR(host->src_clk_cg)) {
3702 ret = PTR_ERR(host->src_clk_cg);
3703 goto host_free;
3704 }
3705
3706 host->src_mux = NULL;
3707 host->src_mux = devm_clk_get(&pdev->dev, "src-mux");
3708 if (!IS_ERR(host->src_mux)) {
3709 dev_info(&pdev->dev, "Got Src Clock Mux\n");
3710 host->src_pll = devm_clk_get(&pdev->dev, "src-pll");
3711 if (!IS_ERR(host->src_pll)) {
3712 dev_info(&pdev->dev, "Got Src Clock PLL\n");
3713 clk_set_parent(host->src_mux, host->src_pll);
3714 if (!of_property_read_u32(pdev->dev.of_node,
3715 "pll-frequency", &host->pll_frequency)) {
3716 dev_info(&pdev->dev, "Got PLL Frequency:%d\n",
3717 host->pll_frequency);
3718 clk_set_rate(host->src_pll,
3719 host->pll_frequency);
3720 }
3721 }
3722 }
3723
3724 host->irq = platform_get_irq(pdev, 0);
3725 if (host->irq < 0) {
3726 ret = -EINVAL;
3727 goto host_free;
3728 }
3729
3730 host->pinctrl = devm_pinctrl_get(&pdev->dev);
3731 if (IS_ERR(host->pinctrl)) {
3732 ret = PTR_ERR(host->pinctrl);
3733 dev_info(&pdev->dev, "Cannot find pinctrl!\n");
3734 goto host_free;
3735 }
3736
3737 host->pins_default = pinctrl_lookup_state(host->pinctrl, "default");
3738 if (IS_ERR(host->pins_default)) {
3739 ret = PTR_ERR(host->pins_default);
3740 dev_info(&pdev->dev, "Cannot find pinctrl default!\n");
3741 goto host_free;
3742 }
3743
3744 host->pins_dat1 = pinctrl_lookup_state(host->pinctrl, "state_dat1");
3745 if (IS_ERR(host->pins_dat1)) {
3746 ret = PTR_ERR(host->pins_dat1);
3747 dev_info(&pdev->dev, "Cannot find pinctrl dat1\n");
3748 goto host_free;
3749 }
3750
3751 host->pins_dat1_eint = pinctrl_lookup_state(host->pinctrl,
3752 "state_eint");
3753 if (IS_ERR(host->pins_dat1_eint)) {
3754 ret = PTR_ERR(host->pins_dat1_eint);
3755 dev_info(&pdev->dev, "Cannot find pinctrl dat1 eint\n");
3756 goto host_free;
3757 }
3758
3759 if (!of_property_read_u32(pdev->dev.of_node,
3760 "hs400-ds-delay", &host->hs400_ds_delay))
3761 dev_dbg(&pdev->dev, "hs400-ds-delay: %x\n",
3762 host->hs400_ds_delay);
3763
3764#ifdef SUPPORT_LEGACY_SDIO
3765 if (of_property_read_bool(pdev->dev.of_node, "cap-sdio-irq"))
3766 host->cap_eirq = false;
3767 else
3768 host->cap_eirq = true;
3769#endif
3770
3771 host->dev = &pdev->dev;
3772 host->dev_comp = of_id->data;
3773 host->mmc = mmc;
3774 host->src_clk_freq = clk_get_rate(host->src_clk);
3775
3776 /* Set host parameters to mmc */
3777 mmc->ops = &mt_msdc_ops;
3778 mmc->f_min = host->src_clk_freq /
3779 (4 * ((1 << host->dev_comp->clk_div_bits) - 1));
3780 if (host->dev_comp->fix_200m &&
3781 host->src_clk_freq > 200000000) {
3782 host->src_clk_freq = 200000000;
3783 pr_info("%s: %s fix clk freq %d\n",
3784 __func__, mmc_hostname(host->mmc),
3785 host->src_clk_freq);
3786 }
3787
3788 mmc->ocr_avail = MMC_VDD_28_29 | MMC_VDD_29_30 | MMC_VDD_30_31 |
3789 MMC_VDD_31_32 | MMC_VDD_32_33;
3790
3791 mmc->caps |= MMC_CAP_ERASE | MMC_CAP_CMD23;
3792 /* MMC core transfer sizes tunable parameters */
3793#ifdef SUPPORT_LEGACY_SDIO
3794 if (host->cap_eirq)
3795 mmc->caps |= MMC_CAP_SDIO_IRQ;
3796#endif
3797 mmc->max_segs = MAX_BD_NUM;
3798 mmc->max_seg_size = BDMA_DESC_BUFLEN;
3799 mmc->max_blk_size = 2048;
3800 mmc->max_req_size = 512 * 1024;
3801 mmc->max_blk_count = mmc->max_req_size / 512;
3802 host->dma_mask = DMA_BIT_MASK(32);
3803 mmc_dev(mmc)->dma_mask = &host->dma_mask;
3804
3805 host->timeout_clks = 3 * 1048576;
3806 host->irq_thread_alive = false;
3807 host->dma.gpd = dma_alloc_coherent(&pdev->dev,
3808 2 * sizeof(struct mt_gpdma_desc),
3809 &host->dma.gpd_addr, GFP_KERNEL);
3810 host->dma.bd = dma_alloc_coherent(&pdev->dev,
3811 MAX_BD_NUM * sizeof(struct mt_bdma_desc),
3812 &host->dma.bd_addr, GFP_KERNEL);
3813 if (!host->dma.gpd || !host->dma.bd) {
3814 ret = -ENOMEM;
3815 goto release_mem;
3816 }
3817 msdc_init_gpd_bd(host, &host->dma);
3818 INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout);
3819 spin_lock_init(&host->lock);
3820 spin_lock_init(&host->irqlock);
3821
3822 platform_set_drvdata(pdev, mmc);
3823 msdc_ungate_clock(host);
3824 msdc_init_hw(host);
3825
3826 ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq,
3827 IRQF_TRIGGER_LOW | IRQF_ONESHOT, pdev->name, host);
3828 if (ret)
3829 goto release;
3830
3831#ifdef SUPPORT_LEGACY_SDIO
3832 host->suspend = 0;
3833
3834 register_legacy_sdio_apis(host);
3835 if (host->request_sdio_eirq)
3836 host->request_sdio_eirq(msdc_eirq_sdio, (void *)host);
3837 if (host->register_pm) {
3838 host->register_pm(msdc_pm, (void *)host);
3839
3840 /* pm not controlled by system but by client. */
3841 mmc->pm_flags |= MMC_PM_IGNORE_PM_NOTIFY;
3842 }
3843#endif
3844 ret = request_dat1_eint_irq(host);
3845 if (ret) {
3846 dev_info(host->dev, "failed to register dat1 eint irq!\n");
3847 goto release;
3848 }
3849
3850 pm_runtime_set_active(host->dev);
3851 pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY);
3852 pm_runtime_use_autosuspend(host->dev);
3853 pm_runtime_enable(host->dev);
3854
3855 host->mmc->caps |= MMC_CAP_NONREMOVABLE;
3856 host->mmc->pm_caps |= MMC_PM_KEEP_POWER;
3857 host->mmc->pm_flags |= MMC_PM_KEEP_POWER;
3858 ret = mmc_add_host(mmc);
3859 pr_info("%s: add new sdio_host %s, index=%d, ret=%d\n", __func__,
3860 mmc_hostname(host->mmc), mmc->index, ret);
3861
3862 sdio_host = host;
3863 if (ret)
3864 goto end;
3865 sdio_proc_init(sdio_host->mmc);
3866
3867 return 0;
3868end:
3869 pm_runtime_disable(host->dev);
3870release:
3871 platform_set_drvdata(pdev, NULL);
3872 msdc_deinit_hw(host);
3873 msdc_gate_clock(host);
3874release_mem:
3875 if (host->dma.gpd)
3876 dma_free_coherent(&pdev->dev,
3877 2 * sizeof(struct mt_gpdma_desc),
3878 host->dma.gpd, host->dma.gpd_addr);
3879 if (host->dma.bd)
3880 dma_free_coherent(&pdev->dev,
3881 MAX_BD_NUM * sizeof(struct mt_bdma_desc),
3882 host->dma.bd, host->dma.bd_addr);
3883host_free:
3884 mmc_free_host(mmc);
3885
3886 return ret;
3887}
3888
3889static int msdc_drv_remove(struct platform_device *pdev)
3890{
3891 struct mmc_host *mmc;
3892 struct msdc_host *host;
3893
3894 mmc = platform_get_drvdata(pdev);
3895 host = mmc_priv(mmc);
3896
3897 pm_runtime_get_sync(host->dev);
3898
3899 platform_set_drvdata(pdev, NULL);
3900 mmc_remove_host(host->mmc);
3901 msdc_deinit_hw(host);
3902 msdc_gate_clock(host);
3903
3904 if (host->mmc->caps & MMC_CAP_SDIO_IRQ)
3905 pm_runtime_put_sync(host->dev);
3906
3907 pm_runtime_disable(host->dev);
3908 pm_runtime_put_noidle(host->dev);
3909 dma_free_coherent(&pdev->dev,
3910 sizeof(struct mt_gpdma_desc),
3911 host->dma.gpd, host->dma.gpd_addr);
3912 dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc),
3913 host->dma.bd, host->dma.bd_addr);
3914
3915 mmc_free_host(host->mmc);
3916
3917 return 0;
3918}
3919
3920#ifdef CONFIG_PM
3921static void msdc_save_reg(struct msdc_host *host)
3922{
3923 void __iomem *base = host->base;
3924 void __iomem *top_base = host->top_base;
3925 u32 tune_reg = host->dev_comp->pad_tune_reg;
3926
3927 host->save_para.msdc_cfg = readl(base + MSDC_CFG);
3928 host->save_para.iocon = readl(base + MSDC_IOCON);
3929 host->save_para.sdc_cfg = readl(base + SDC_CFG);
3930 host->save_para.pad_tune = readl(base + tune_reg);
3931 host->save_para.patch_bit0 = readl(base + MSDC_PATCH_BIT0);
3932 host->save_para.patch_bit1 = readl(base + MSDC_PATCH_BIT1);
3933 host->save_para.patch_bit2 = readl(base + MSDC_PATCH_BIT2);
3934 host->save_para.pad_ds_tune = readl(base + PAD_DS_TUNE);
3935 host->save_para.pad_cmd_tune = readl(base + PAD_CMD_TUNE);
3936 host->save_para.emmc50_cfg0 = readl(base + EMMC50_CFG0);
3937 host->save_para.emmc50_cfg3 = readl(base + EMMC50_CFG3);
3938 host->save_para.sdc_fifo_cfg = readl(base + SDC_FIFO_CFG);
3939 if (top_base) {
3940 host->save_para.emmc_top_control =
3941 readl(top_base + EMMC_TOP_CONTROL);
3942 host->save_para.emmc_top_cmd =
3943 readl(top_base + EMMC_TOP_CMD);
3944 host->save_para.emmc50_pad_ds_tune =
3945 readl(top_base + TOP_EMMC50_PAD_DS_TUNE);
3946 host->save_para.top_emmc50_pad_dat_tune[0] =
3947 readl(top_base + TOP_EMMC50_PAD_DAT0_TUNE);
3948 host->save_para.top_emmc50_pad_dat_tune[1] =
3949 readl(top_base + TOP_EMMC50_PAD_DAT1_TUNE);
3950 host->save_para.top_emmc50_pad_dat_tune[2] =
3951 readl(top_base + TOP_EMMC50_PAD_DAT2_TUNE);
3952 host->save_para.top_emmc50_pad_dat_tune[3] =
3953 readl(top_base + TOP_EMMC50_PAD_DAT3_TUNE);
3954 } else {
3955 host->save_para.pad_tune0 =
3956 readl(base + MSDC_PAD_TUNE0);
3957 host->save_para.pad_tune1 =
3958 readl(base + MSDC_PAD_TUNE1);
3959 }
3960}
3961
3962static void msdc_restore_reg(struct msdc_host *host)
3963{
3964 void __iomem *base = host->base;
3965 void __iomem *top_base = host->top_base;
3966 u32 tune_reg = host->dev_comp->pad_tune_reg;
3967
3968 writel(host->save_para.msdc_cfg, host->base + MSDC_CFG);
3969 writel(host->save_para.iocon, host->base + MSDC_IOCON);
3970 writel(host->save_para.sdc_cfg, host->base + SDC_CFG);
3971 writel(host->save_para.pad_tune, host->base + tune_reg);
3972 writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT0);
3973 writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1);
3974 writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2);
3975 writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE);
3976 writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE);
3977 writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0);
3978 writel(host->save_para.emmc50_cfg3, host->base + EMMC50_CFG3);
3979 writel(host->save_para.sdc_fifo_cfg, host->base + SDC_FIFO_CFG);
3980
3981 if (top_base) {
3982 writel(host->save_para.emmc_top_control,
3983 top_base + EMMC_TOP_CONTROL);
3984 writel(host->save_para.emmc_top_cmd,
3985 top_base + EMMC_TOP_CMD);
3986 writel(host->save_para.emmc50_pad_ds_tune,
3987 top_base + TOP_EMMC50_PAD_DS_TUNE);
3988 writel(host->save_para.top_emmc50_pad_dat_tune[0],
3989 top_base + TOP_EMMC50_PAD_DAT0_TUNE);
3990 writel(host->save_para.top_emmc50_pad_dat_tune[1],
3991 top_base + TOP_EMMC50_PAD_DAT1_TUNE);
3992 writel(host->save_para.top_emmc50_pad_dat_tune[2],
3993 top_base + TOP_EMMC50_PAD_DAT2_TUNE);
3994 writel(host->save_para.top_emmc50_pad_dat_tune[3],
3995 top_base + TOP_EMMC50_PAD_DAT3_TUNE);
3996 } else {
3997 writel(host->save_para.pad_tune0,
3998 base + MSDC_PAD_TUNE0);
3999 writel(host->save_para.pad_tune1,
4000 base + MSDC_PAD_TUNE1);
4001 }
4002}
4003
4004static int msdc_runtime_suspend(struct device *dev)
4005{
4006 struct mmc_host *mmc = dev_get_drvdata(dev);
4007 struct msdc_host *host = mmc_priv(mmc);
4008 unsigned long flags;
4009
4010#ifdef SUPPORT_LEGACY_SDIO
4011 msdc_save_reg(host);
4012 msdc_gate_clock(host);
4013 pinctrl_select_state(host->pinctrl, host->pins_dat1_eint);
4014 return 0;
4015#else
4016 msdc_save_reg(host);
4017 disable_irq(host->irq);
4018 msdc_gate_clock(host);
4019 pinctrl_select_state(host->pinctrl, host->pins_dat1_eint);
4020 spin_lock_irqsave(&host->irqlock, flags);
4021 if (host->sdio_irq_cnt == 0) {
4022 enable_irq(host->eint_irq);
4023 host->sdio_irq_cnt++;
4024#ifdef SDIO_EAI_SUPPORT
4025 /*
4026 * if SDIO card do not support async irq,
4027 * make clk always on.
4028 */
4029 if (mmc->card && (mmc->card->cccr.eai == 0))
4030 pm_runtime_get_noresume(host->dev);
4031#endif
4032 }
4033 spin_unlock_irqrestore(&host->irqlock, flags);
4034 return 0;
4035#endif
4036}
4037
4038static int msdc_runtime_resume(struct device *dev)
4039{
4040 struct mmc_host *mmc = dev_get_drvdata(dev);
4041 struct msdc_host *host = mmc_priv(mmc);
4042 unsigned long flags;
4043
4044#ifdef SUPPORT_LEGACY_SDIO
4045 pinctrl_select_state(host->pinctrl, host->pins_dat1);
4046 msdc_ungate_clock(host);
4047 msdc_restore_reg(host);
4048 return 0;
4049#else
4050 spin_lock_irqsave(&host->irqlock, flags);
4051 if (host->sdio_irq_cnt > 0) {
4052 disable_irq_nosync(host->eint_irq);
4053 host->sdio_irq_cnt--;
4054#ifdef SDIO_EAI_SUPPORT
4055 if (mmc->card && (mmc->card->cccr.eai == 0))
4056 pm_runtime_put_noidle(host->dev);
4057#endif
4058 }
4059 spin_unlock_irqrestore(&host->irqlock, flags);
4060 pinctrl_select_state(host->pinctrl, host->pins_dat1);
4061 msdc_ungate_clock(host);
4062 msdc_restore_reg(host);
4063 enable_irq(host->irq);
4064 return 0;
4065#endif
4066}
4067#endif
4068
4069static const struct dev_pm_ops msdc_dev_pm_ops = {
4070 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
4071 pm_runtime_force_resume)
4072 SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL)
4073};
4074
4075static struct platform_driver mt_sdio_driver = {
4076 .probe = msdc_drv_probe,
4077 .remove = msdc_drv_remove,
4078 .driver = {
4079 .name = "mtk-sdio",
4080 .of_match_table = msdc_of_ids,
4081 .pm = &msdc_dev_pm_ops,
4082 },
4083};
4084
4085module_platform_driver(mt_sdio_driver);
4086MODULE_LICENSE("GPL v2");
4087MODULE_DESCRIPTION("MediaTek SDIO Driver");
4088