11802d0beSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 220848903SChaotian Jing /* 3527f36f5SAxe Yang * Copyright (c) 2014-2015, 2022 MediaTek Inc. 420848903SChaotian Jing * Author: Chaotian.Jing <chaotian.jing@mediatek.com> 520848903SChaotian Jing */ 620848903SChaotian Jing 720848903SChaotian Jing #include <linux/module.h> 84fe54318SAngeloGioacchino Del Regno #include <linux/bitops.h> 920848903SChaotian Jing #include <linux/clk.h> 1020848903SChaotian Jing #include <linux/delay.h> 1120848903SChaotian Jing #include <linux/dma-mapping.h> 1243e5fee3SDerong Liu #include <linux/iopoll.h> 1320848903SChaotian Jing #include <linux/ioport.h> 1420848903SChaotian Jing #include <linux/irq.h> 1520848903SChaotian Jing #include <linux/of_address.h> 16909b3456SRyder Lee #include <linux/of_device.h> 1720848903SChaotian Jing #include <linux/of_irq.h> 1820848903SChaotian Jing #include <linux/of_gpio.h> 1920848903SChaotian Jing #include <linux/pinctrl/consumer.h> 2020848903SChaotian Jing #include <linux/platform_device.h> 214b8a43e9SChaotian Jing #include <linux/pm.h> 224b8a43e9SChaotian Jing #include <linux/pm_runtime.h> 23527f36f5SAxe Yang #include <linux/pm_wakeirq.h> 2420848903SChaotian Jing #include <linux/regulator/consumer.h> 256397b7f5SChaotian Jing #include <linux/slab.h> 2620848903SChaotian Jing #include <linux/spinlock.h> 27b8789ec4SUlf Hansson #include <linux/interrupt.h> 28855d388dSWenbin Mei #include <linux/reset.h> 2920848903SChaotian Jing 3020848903SChaotian Jing #include <linux/mmc/card.h> 3120848903SChaotian Jing #include <linux/mmc/core.h> 3220848903SChaotian Jing #include <linux/mmc/host.h> 3320848903SChaotian Jing #include <linux/mmc/mmc.h> 3420848903SChaotian Jing #include <linux/mmc/sd.h> 3520848903SChaotian Jing #include <linux/mmc/sdio.h> 368d53e412SChaotian Jing #include <linux/mmc/slot-gpio.h> 3720848903SChaotian Jing 3888bd652bSChun-Hung Wu #include "cqhci.h" 3988bd652bSChun-Hung Wu 4020848903SChaotian Jing #define MAX_BD_NUM 1024 41f5eccd94SWenbin Mei #define MSDC_NR_CLOCKS 3 4220848903SChaotian Jing 4320848903SChaotian Jing /*--------------------------------------------------------------------------*/ 4420848903SChaotian Jing /* Common Definition */ 4520848903SChaotian Jing /*--------------------------------------------------------------------------*/ 4620848903SChaotian Jing #define MSDC_BUS_1BITS 0x0 4720848903SChaotian Jing #define MSDC_BUS_4BITS 0x1 4820848903SChaotian Jing #define MSDC_BUS_8BITS 0x2 4920848903SChaotian Jing 5020848903SChaotian Jing #define MSDC_BURST_64B 0x6 5120848903SChaotian Jing 5220848903SChaotian Jing /*--------------------------------------------------------------------------*/ 5320848903SChaotian Jing /* Register Offset */ 5420848903SChaotian Jing /*--------------------------------------------------------------------------*/ 5520848903SChaotian Jing #define MSDC_CFG 0x0 5620848903SChaotian Jing #define MSDC_IOCON 0x04 5720848903SChaotian Jing #define MSDC_PS 0x08 5820848903SChaotian Jing #define MSDC_INT 0x0c 5920848903SChaotian Jing #define MSDC_INTEN 0x10 6020848903SChaotian Jing #define MSDC_FIFOCS 0x14 6120848903SChaotian Jing #define SDC_CFG 0x30 6220848903SChaotian Jing #define SDC_CMD 0x34 6320848903SChaotian Jing #define SDC_ARG 0x38 6420848903SChaotian Jing #define SDC_STS 0x3c 6520848903SChaotian Jing #define SDC_RESP0 0x40 6620848903SChaotian Jing #define SDC_RESP1 0x44 6720848903SChaotian Jing #define SDC_RESP2 0x48 6820848903SChaotian Jing #define SDC_RESP3 0x4c 6920848903SChaotian Jing #define SDC_BLK_NUM 0x50 70d9dcbfc8SChaotian Jing #define SDC_ADV_CFG0 0x64 71c9b5061eSChaotian Jing #define EMMC_IOCON 0x7c 7220848903SChaotian Jing #define SDC_ACMD_RESP 0x80 732a9bde19SChaotian Jing #define DMA_SA_H4BIT 0x8c 7420848903SChaotian Jing #define MSDC_DMA_SA 0x90 7520848903SChaotian Jing #define MSDC_DMA_CTRL 0x98 7620848903SChaotian Jing #define MSDC_DMA_CFG 0x9c 7720848903SChaotian Jing #define MSDC_PATCH_BIT 0xb0 7820848903SChaotian Jing #define MSDC_PATCH_BIT1 0xb4 792fea5819SChaotian Jing #define MSDC_PATCH_BIT2 0xb8 8020848903SChaotian Jing #define MSDC_PAD_TUNE 0xec 8139add252SChaotian Jing #define MSDC_PAD_TUNE0 0xf0 826397b7f5SChaotian Jing #define PAD_DS_TUNE 0x188 831ede5cb8Syong mao #define PAD_CMD_TUNE 0x18c 8413b4e1e9SWenbin Mei #define EMMC51_CFG0 0x204 856397b7f5SChaotian Jing #define EMMC50_CFG0 0x208 8613b4e1e9SWenbin Mei #define EMMC50_CFG1 0x20c 87c8609b22SChaotian Jing #define EMMC50_CFG3 0x220 88d9dcbfc8SChaotian Jing #define SDC_FIFO_CFG 0x228 8913b4e1e9SWenbin Mei #define CQHCI_SETTING 0x7fc 9020848903SChaotian Jing 9120848903SChaotian Jing /*--------------------------------------------------------------------------*/ 92a2e6d1f6SChaotian Jing /* Top Pad Register Offset */ 93a2e6d1f6SChaotian Jing /*--------------------------------------------------------------------------*/ 94a2e6d1f6SChaotian Jing #define EMMC_TOP_CONTROL 0x00 95a2e6d1f6SChaotian Jing #define EMMC_TOP_CMD 0x04 96a2e6d1f6SChaotian Jing #define EMMC50_PAD_DS_TUNE 0x0c 97a2e6d1f6SChaotian Jing 98a2e6d1f6SChaotian Jing /*--------------------------------------------------------------------------*/ 9920848903SChaotian Jing /* Register Mask */ 10020848903SChaotian Jing /*--------------------------------------------------------------------------*/ 10120848903SChaotian Jing 10220848903SChaotian Jing /* MSDC_CFG mask */ 1034fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_MODE BIT(0) /* RW */ 1044fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKPDN BIT(1) /* RW */ 1054fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_RST BIT(2) /* RW */ 1064fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_PIO BIT(3) /* RW */ 1074fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKDRVEN BIT(4) /* RW */ 1084fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_BV18SDT BIT(5) /* RW */ 1094fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_BV18PSS BIT(6) /* R */ 1104fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKSTB BIT(7) /* R */ 1114fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKDIV GENMASK(15, 8) /* RW */ 1124fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKMOD GENMASK(17, 16) /* RW */ 1134fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_HS400_CK_MODE BIT(18) /* RW */ 1144fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_HS400_CK_MODE_EXTRA BIT(22) /* RW */ 1154fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKDIV_EXTRA GENMASK(19, 8) /* RW */ 1164fe54318SAngeloGioacchino Del Regno #define MSDC_CFG_CKMOD_EXTRA GENMASK(21, 20) /* RW */ 11720848903SChaotian Jing 11820848903SChaotian Jing /* MSDC_IOCON mask */ 1194fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_SDR104CKS BIT(0) /* RW */ 1204fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_RSPL BIT(1) /* RW */ 1214fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_DSPL BIT(2) /* RW */ 1224fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_DDLSEL BIT(3) /* RW */ 1234fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_DDR50CKD BIT(4) /* RW */ 1244fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_DSPLSEL BIT(5) /* RW */ 1254fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_W_DSPL BIT(8) /* RW */ 1264fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D0SPL BIT(16) /* RW */ 1274fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D1SPL BIT(17) /* RW */ 1284fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D2SPL BIT(18) /* RW */ 1294fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D3SPL BIT(19) /* RW */ 1304fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D4SPL BIT(20) /* RW */ 1314fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D5SPL BIT(21) /* RW */ 1324fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D6SPL BIT(22) /* RW */ 1334fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_D7SPL BIT(23) /* RW */ 1344fe54318SAngeloGioacchino Del Regno #define MSDC_IOCON_RISCSZ GENMASK(25, 24) /* RW */ 13520848903SChaotian Jing 13620848903SChaotian Jing /* MSDC_PS mask */ 1374fe54318SAngeloGioacchino Del Regno #define MSDC_PS_CDEN BIT(0) /* RW */ 1384fe54318SAngeloGioacchino Del Regno #define MSDC_PS_CDSTS BIT(1) /* R */ 1394fe54318SAngeloGioacchino Del Regno #define MSDC_PS_CDDEBOUNCE GENMASK(15, 12) /* RW */ 1404fe54318SAngeloGioacchino Del Regno #define MSDC_PS_DAT GENMASK(23, 16) /* R */ 1414fe54318SAngeloGioacchino Del Regno #define MSDC_PS_DATA1 BIT(17) /* R */ 1424fe54318SAngeloGioacchino Del Regno #define MSDC_PS_CMD BIT(24) /* R */ 1434fe54318SAngeloGioacchino Del Regno #define MSDC_PS_WP BIT(31) /* R */ 14420848903SChaotian Jing 14520848903SChaotian Jing /* MSDC_INT mask */ 1464fe54318SAngeloGioacchino Del Regno #define MSDC_INT_MMCIRQ BIT(0) /* W1C */ 1474fe54318SAngeloGioacchino Del Regno #define MSDC_INT_CDSC BIT(1) /* W1C */ 1484fe54318SAngeloGioacchino Del Regno #define MSDC_INT_ACMDRDY BIT(3) /* W1C */ 1494fe54318SAngeloGioacchino Del Regno #define MSDC_INT_ACMDTMO BIT(4) /* W1C */ 1504fe54318SAngeloGioacchino Del Regno #define MSDC_INT_ACMDCRCERR BIT(5) /* W1C */ 1514fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DMAQ_EMPTY BIT(6) /* W1C */ 1524fe54318SAngeloGioacchino Del Regno #define MSDC_INT_SDIOIRQ BIT(7) /* W1C */ 1534fe54318SAngeloGioacchino Del Regno #define MSDC_INT_CMDRDY BIT(8) /* W1C */ 1544fe54318SAngeloGioacchino Del Regno #define MSDC_INT_CMDTMO BIT(9) /* W1C */ 1554fe54318SAngeloGioacchino Del Regno #define MSDC_INT_RSPCRCERR BIT(10) /* W1C */ 1564fe54318SAngeloGioacchino Del Regno #define MSDC_INT_CSTA BIT(11) /* R */ 1574fe54318SAngeloGioacchino Del Regno #define MSDC_INT_XFER_COMPL BIT(12) /* W1C */ 1584fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DXFER_DONE BIT(13) /* W1C */ 1594fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DATTMO BIT(14) /* W1C */ 1604fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DATCRCERR BIT(15) /* W1C */ 1614fe54318SAngeloGioacchino Del Regno #define MSDC_INT_ACMD19_DONE BIT(16) /* W1C */ 1624fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DMA_BDCSERR BIT(17) /* W1C */ 1634fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DMA_GPDCSERR BIT(18) /* W1C */ 1644fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DMA_PROTECT BIT(19) /* W1C */ 1654fe54318SAngeloGioacchino Del Regno #define MSDC_INT_CMDQ BIT(28) /* W1C */ 16620848903SChaotian Jing 16720848903SChaotian Jing /* MSDC_INTEN mask */ 1684fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_MMCIRQ BIT(0) /* RW */ 1694fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_CDSC BIT(1) /* RW */ 1704fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_ACMDRDY BIT(3) /* RW */ 1714fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_ACMDTMO BIT(4) /* RW */ 1724fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_ACMDCRCERR BIT(5) /* RW */ 1734fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DMAQ_EMPTY BIT(6) /* RW */ 1744fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_SDIOIRQ BIT(7) /* RW */ 1754fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_CMDRDY BIT(8) /* RW */ 1764fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_CMDTMO BIT(9) /* RW */ 1774fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_RSPCRCERR BIT(10) /* RW */ 1784fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_CSTA BIT(11) /* RW */ 1794fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_XFER_COMPL BIT(12) /* RW */ 1804fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DXFER_DONE BIT(13) /* RW */ 1814fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DATTMO BIT(14) /* RW */ 1824fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DATCRCERR BIT(15) /* RW */ 1834fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_ACMD19_DONE BIT(16) /* RW */ 1844fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DMA_BDCSERR BIT(17) /* RW */ 1854fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DMA_GPDCSERR BIT(18) /* RW */ 1864fe54318SAngeloGioacchino Del Regno #define MSDC_INTEN_DMA_PROTECT BIT(19) /* RW */ 18720848903SChaotian Jing 18820848903SChaotian Jing /* MSDC_FIFOCS mask */ 1894fe54318SAngeloGioacchino Del Regno #define MSDC_FIFOCS_RXCNT GENMASK(7, 0) /* R */ 1904fe54318SAngeloGioacchino Del Regno #define MSDC_FIFOCS_TXCNT GENMASK(23, 16) /* R */ 1914fe54318SAngeloGioacchino Del Regno #define MSDC_FIFOCS_CLR BIT(31) /* RW */ 19220848903SChaotian Jing 19320848903SChaotian Jing /* SDC_CFG mask */ 1944fe54318SAngeloGioacchino Del Regno #define SDC_CFG_SDIOINTWKUP BIT(0) /* RW */ 1954fe54318SAngeloGioacchino Del Regno #define SDC_CFG_INSWKUP BIT(1) /* RW */ 1964fe54318SAngeloGioacchino Del Regno #define SDC_CFG_WRDTOC GENMASK(14, 2) /* RW */ 1974fe54318SAngeloGioacchino Del Regno #define SDC_CFG_BUSWIDTH GENMASK(17, 16) /* RW */ 1984fe54318SAngeloGioacchino Del Regno #define SDC_CFG_SDIO BIT(19) /* RW */ 1994fe54318SAngeloGioacchino Del Regno #define SDC_CFG_SDIOIDE BIT(20) /* RW */ 2004fe54318SAngeloGioacchino Del Regno #define SDC_CFG_INTATGAP BIT(21) /* RW */ 2014fe54318SAngeloGioacchino Del Regno #define SDC_CFG_DTOC GENMASK(31, 24) /* RW */ 20220848903SChaotian Jing 20320848903SChaotian Jing /* SDC_STS mask */ 2044fe54318SAngeloGioacchino Del Regno #define SDC_STS_SDCBUSY BIT(0) /* RW */ 2054fe54318SAngeloGioacchino Del Regno #define SDC_STS_CMDBUSY BIT(1) /* RW */ 2064fe54318SAngeloGioacchino Del Regno #define SDC_STS_SWR_COMPL BIT(31) /* RW */ 20720848903SChaotian Jing 2084fe54318SAngeloGioacchino Del Regno #define SDC_DAT1_IRQ_TRIGGER BIT(19) /* RW */ 209d9dcbfc8SChaotian Jing /* SDC_ADV_CFG0 mask */ 2104fe54318SAngeloGioacchino Del Regno #define SDC_RX_ENHANCE_EN BIT(20) /* RW */ 211d9dcbfc8SChaotian Jing 2122a9bde19SChaotian Jing /* DMA_SA_H4BIT mask */ 2134fe54318SAngeloGioacchino Del Regno #define DMA_ADDR_HIGH_4BIT GENMASK(3, 0) /* RW */ 2142a9bde19SChaotian Jing 21520848903SChaotian Jing /* MSDC_DMA_CTRL mask */ 2164fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CTRL_START BIT(0) /* W */ 2174fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CTRL_STOP BIT(1) /* W */ 2184fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CTRL_RESUME BIT(2) /* W */ 2194fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CTRL_MODE BIT(8) /* RW */ 2204fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CTRL_LASTBUF BIT(10) /* RW */ 2214fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CTRL_BRUSTSZ GENMASK(14, 12) /* RW */ 22220848903SChaotian Jing 22320848903SChaotian Jing /* MSDC_DMA_CFG mask */ 2244fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CFG_STS BIT(0) /* R */ 2254fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CFG_DECSEN BIT(1) /* RW */ 2264fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CFG_AHBHPROT2 BIT(9) /* RW */ 2274fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CFG_ACTIVEEN BIT(13) /* RW */ 2284fe54318SAngeloGioacchino Del Regno #define MSDC_DMA_CFG_CS12B16B BIT(16) /* RW */ 22920848903SChaotian Jing 23020848903SChaotian Jing /* MSDC_PATCH_BIT mask */ 2314fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_ODDSUPP BIT(1) /* RW */ 2324fe54318SAngeloGioacchino Del Regno #define MSDC_INT_DAT_LATCH_CK_SEL GENMASK(9, 7) 2334fe54318SAngeloGioacchino Del Regno #define MSDC_CKGEN_MSDC_DLY_SEL GENMASK(14, 10) 2344fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_IODSSEL BIT(16) /* RW */ 2354fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_IOINTSEL BIT(17) /* RW */ 2364fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_BUSYDLY GENMASK(21, 18) /* RW */ 2374fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_WDOD GENMASK(25, 22) /* RW */ 2384fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_IDRTSEL BIT(26) /* RW */ 2394fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_CMDFSEL BIT(27) /* RW */ 2404fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_INTDLSEL BIT(28) /* RW */ 2414fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_SPCPUSH BIT(29) /* RW */ 2424fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT_DECRCTMO BIT(30) /* RW */ 24320848903SChaotian Jing 2444fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT1_CMDTA GENMASK(5, 3) /* RW */ 2454fe54318SAngeloGioacchino Del Regno #define MSDC_PB1_BUSY_CHECK_SEL BIT(7) /* RW */ 2464fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT1_STOP_DLY GENMASK(11, 8) /* RW */ 247d9dcbfc8SChaotian Jing 2484fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT2_CFGRESP BIT(15) /* RW */ 2494fe54318SAngeloGioacchino Del Regno #define MSDC_PATCH_BIT2_CFGCRCSTS BIT(28) /* RW */ 2504fe54318SAngeloGioacchino Del Regno #define MSDC_PB2_SUPPORT_64G BIT(1) /* RW */ 2514fe54318SAngeloGioacchino Del Regno #define MSDC_PB2_RESPWAIT GENMASK(3, 2) /* RW */ 2524fe54318SAngeloGioacchino Del Regno #define MSDC_PB2_RESPSTSENSEL GENMASK(18, 16) /* RW */ 2534fe54318SAngeloGioacchino Del Regno #define MSDC_PB2_CRCSTSENSEL GENMASK(31, 29) /* RW */ 2542fea5819SChaotian Jing 2554fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_DATWRDLY GENMASK(4, 0) /* RW */ 2564fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_DATRRDLY GENMASK(12, 8) /* RW */ 2574fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_CMDRDLY GENMASK(20, 16) /* RW */ 2584fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_CMDRRDLY GENMASK(26, 22) /* RW */ 2594fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_CLKTDLY GENMASK(31, 27) /* RW */ 2604fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_RXDLYSEL BIT(15) /* RW */ 2614fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_RD_SEL BIT(13) /* RW */ 2624fe54318SAngeloGioacchino Del Regno #define MSDC_PAD_TUNE_CMD_SEL BIT(21) /* RW */ 2636397b7f5SChaotian Jing 2644fe54318SAngeloGioacchino Del Regno #define PAD_DS_TUNE_DLY_SEL BIT(0) /* RW */ 2654fe54318SAngeloGioacchino Del Regno #define PAD_DS_TUNE_DLY1 GENMASK(6, 2) /* RW */ 2664fe54318SAngeloGioacchino Del Regno #define PAD_DS_TUNE_DLY2 GENMASK(11, 7) /* RW */ 2674fe54318SAngeloGioacchino Del Regno #define PAD_DS_TUNE_DLY3 GENMASK(16, 12) /* RW */ 2686397b7f5SChaotian Jing 2694fe54318SAngeloGioacchino Del Regno #define PAD_CMD_TUNE_RX_DLY3 GENMASK(5, 1) /* RW */ 2701ede5cb8Syong mao 27113b4e1e9SWenbin Mei /* EMMC51_CFG0 mask */ 2724fe54318SAngeloGioacchino Del Regno #define CMDQ_RDAT_CNT GENMASK(21, 12) /* RW */ 27313b4e1e9SWenbin Mei 2744fe54318SAngeloGioacchino Del Regno #define EMMC50_CFG_PADCMD_LATCHCK BIT(0) /* RW */ 2754fe54318SAngeloGioacchino Del Regno #define EMMC50_CFG_CRCSTS_EDGE BIT(3) /* RW */ 2764fe54318SAngeloGioacchino Del Regno #define EMMC50_CFG_CFCSTS_SEL BIT(4) /* RW */ 2774fe54318SAngeloGioacchino Del Regno #define EMMC50_CFG_CMD_RESP_SEL BIT(9) /* RW */ 27813b4e1e9SWenbin Mei 27913b4e1e9SWenbin Mei /* EMMC50_CFG1 mask */ 2804fe54318SAngeloGioacchino Del Regno #define EMMC50_CFG1_DS_CFG BIT(28) /* RW */ 2816397b7f5SChaotian Jing 2824fe54318SAngeloGioacchino Del Regno #define EMMC50_CFG3_OUTS_WR GENMASK(4, 0) /* RW */ 283c8609b22SChaotian Jing 2844fe54318SAngeloGioacchino Del Regno #define SDC_FIFO_CFG_WRVALIDSEL BIT(24) /* RW */ 2854fe54318SAngeloGioacchino Del Regno #define SDC_FIFO_CFG_RDVALIDSEL BIT(25) /* RW */ 286d9dcbfc8SChaotian Jing 28713b4e1e9SWenbin Mei /* CQHCI_SETTING */ 2884fe54318SAngeloGioacchino Del Regno #define CQHCI_RD_CMD_WND_SEL BIT(14) /* RW */ 2894fe54318SAngeloGioacchino Del Regno #define CQHCI_WR_CMD_WND_SEL BIT(15) /* RW */ 29013b4e1e9SWenbin Mei 291a2e6d1f6SChaotian Jing /* EMMC_TOP_CONTROL mask */ 2924fe54318SAngeloGioacchino Del Regno #define PAD_RXDLY_SEL BIT(0) /* RW */ 2934fe54318SAngeloGioacchino Del Regno #define DELAY_EN BIT(1) /* RW */ 2944fe54318SAngeloGioacchino Del Regno #define PAD_DAT_RD_RXDLY2 GENMASK(6, 2) /* RW */ 2954fe54318SAngeloGioacchino Del Regno #define PAD_DAT_RD_RXDLY GENMASK(11, 7) /* RW */ 2964fe54318SAngeloGioacchino Del Regno #define PAD_DAT_RD_RXDLY2_SEL BIT(12) /* RW */ 2974fe54318SAngeloGioacchino Del Regno #define PAD_DAT_RD_RXDLY_SEL BIT(13) /* RW */ 2984fe54318SAngeloGioacchino Del Regno #define DATA_K_VALUE_SEL BIT(14) /* RW */ 2994fe54318SAngeloGioacchino Del Regno #define SDC_RX_ENH_EN BIT(15) /* TW */ 300a2e6d1f6SChaotian Jing 301a2e6d1f6SChaotian Jing /* EMMC_TOP_CMD mask */ 3024fe54318SAngeloGioacchino Del Regno #define PAD_CMD_RXDLY2 GENMASK(4, 0) /* RW */ 3034fe54318SAngeloGioacchino Del Regno #define PAD_CMD_RXDLY GENMASK(9, 5) /* RW */ 3044fe54318SAngeloGioacchino Del Regno #define PAD_CMD_RD_RXDLY2_SEL BIT(10) /* RW */ 3054fe54318SAngeloGioacchino Del Regno #define PAD_CMD_RD_RXDLY_SEL BIT(11) /* RW */ 3064fe54318SAngeloGioacchino Del Regno #define PAD_CMD_TX_DLY GENMASK(16, 12) /* RW */ 307a2e6d1f6SChaotian Jing 308c4ac38c6SWenbin Mei /* EMMC50_PAD_DS_TUNE mask */ 3094fe54318SAngeloGioacchino Del Regno #define PAD_DS_DLY_SEL BIT(16) /* RW */ 3104fe54318SAngeloGioacchino Del Regno #define PAD_DS_DLY1 GENMASK(14, 10) /* RW */ 3114fe54318SAngeloGioacchino Del Regno #define PAD_DS_DLY3 GENMASK(4, 0) /* RW */ 312c4ac38c6SWenbin Mei 3134fe54318SAngeloGioacchino Del Regno #define REQ_CMD_EIO BIT(0) 3144fe54318SAngeloGioacchino Del Regno #define REQ_CMD_TMO BIT(1) 3154fe54318SAngeloGioacchino Del Regno #define REQ_DAT_ERR BIT(2) 3164fe54318SAngeloGioacchino Del Regno #define REQ_STOP_EIO BIT(3) 3174fe54318SAngeloGioacchino Del Regno #define REQ_STOP_TMO BIT(4) 3184fe54318SAngeloGioacchino Del Regno #define REQ_CMD_BUSY BIT(5) 31920848903SChaotian Jing 3204fe54318SAngeloGioacchino Del Regno #define MSDC_PREPARE_FLAG BIT(0) 3214fe54318SAngeloGioacchino Del Regno #define MSDC_ASYNC_FLAG BIT(1) 3224fe54318SAngeloGioacchino Del Regno #define MSDC_MMAP_FLAG BIT(2) 32320848903SChaotian Jing 3244b8a43e9SChaotian Jing #define MTK_MMC_AUTOSUSPEND_DELAY 50 32520848903SChaotian Jing #define CMD_TIMEOUT (HZ/10 * 5) /* 100ms x5 */ 32620848903SChaotian Jing #define DAT_TIMEOUT (HZ * 5) /* 1000ms x5 */ 32720848903SChaotian Jing 328d087bde5SNeilBrown #define DEFAULT_DEBOUNCE (8) /* 8 cycles CD debounce */ 329d087bde5SNeilBrown 3306397b7f5SChaotian Jing #define PAD_DELAY_MAX 32 /* PAD delay cells */ 33120848903SChaotian Jing /*--------------------------------------------------------------------------*/ 33220848903SChaotian Jing /* Descriptor Structure */ 33320848903SChaotian Jing /*--------------------------------------------------------------------------*/ 33420848903SChaotian Jing struct mt_gpdma_desc { 33520848903SChaotian Jing u32 gpd_info; 3364fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_HWO BIT(0) 3374fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_BDP BIT(1) 3384fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_CHECKSUM GENMASK(15, 8) 3394fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_INT BIT(16) 3404fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_NEXT_H4 GENMASK(27, 24) 3414fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_PTR_H4 GENMASK(31, 28) 34220848903SChaotian Jing u32 next; 34320848903SChaotian Jing u32 ptr; 34420848903SChaotian Jing u32 gpd_data_len; 3454fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_BUFLEN GENMASK(15, 0) 3464fe54318SAngeloGioacchino Del Regno #define GPDMA_DESC_EXTLEN GENMASK(23, 16) 34720848903SChaotian Jing u32 arg; 34820848903SChaotian Jing u32 blknum; 34920848903SChaotian Jing u32 cmd; 35020848903SChaotian Jing }; 35120848903SChaotian Jing 35220848903SChaotian Jing struct mt_bdma_desc { 35320848903SChaotian Jing u32 bd_info; 3544fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_EOL BIT(0) 3554fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_CHECKSUM GENMASK(15, 8) 3564fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_BLKPAD BIT(17) 3574fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_DWPAD BIT(18) 3584fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_NEXT_H4 GENMASK(27, 24) 3594fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_PTR_H4 GENMASK(31, 28) 36020848903SChaotian Jing u32 next; 36120848903SChaotian Jing u32 ptr; 36220848903SChaotian Jing u32 bd_data_len; 3634fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_BUFLEN GENMASK(15, 0) 3644fe54318SAngeloGioacchino Del Regno #define BDMA_DESC_BUFLEN_EXT GENMASK(23, 0) 36520848903SChaotian Jing }; 36620848903SChaotian Jing 36720848903SChaotian Jing struct msdc_dma { 36820848903SChaotian Jing struct scatterlist *sg; /* I/O scatter list */ 36920848903SChaotian Jing struct mt_gpdma_desc *gpd; /* pointer to gpd array */ 37020848903SChaotian Jing struct mt_bdma_desc *bd; /* pointer to bd array */ 37120848903SChaotian Jing dma_addr_t gpd_addr; /* the physical address of gpd array */ 37220848903SChaotian Jing dma_addr_t bd_addr; /* the physical address of bd array */ 37320848903SChaotian Jing }; 37420848903SChaotian Jing 3754b8a43e9SChaotian Jing struct msdc_save_para { 3764b8a43e9SChaotian Jing u32 msdc_cfg; 3774b8a43e9SChaotian Jing u32 iocon; 3784b8a43e9SChaotian Jing u32 sdc_cfg; 3794b8a43e9SChaotian Jing u32 pad_tune; 3804b8a43e9SChaotian Jing u32 patch_bit0; 3814b8a43e9SChaotian Jing u32 patch_bit1; 3822fea5819SChaotian Jing u32 patch_bit2; 3836397b7f5SChaotian Jing u32 pad_ds_tune; 3841ede5cb8Syong mao u32 pad_cmd_tune; 3856397b7f5SChaotian Jing u32 emmc50_cfg0; 386c8609b22SChaotian Jing u32 emmc50_cfg3; 387d9dcbfc8SChaotian Jing u32 sdc_fifo_cfg; 388a2e6d1f6SChaotian Jing u32 emmc_top_control; 389a2e6d1f6SChaotian Jing u32 emmc_top_cmd; 390a2e6d1f6SChaotian Jing u32 emmc50_pad_ds_tune; 3916397b7f5SChaotian Jing }; 3926397b7f5SChaotian Jing 393762d491aSChaotian Jing struct mtk_mmc_compatible { 394762d491aSChaotian Jing u8 clk_div_bits; 3959e2582e5Syong mao bool recheck_sdio_irq; 3967f3d5852SChaotian Jing bool hs400_tune; /* only used for MT8173 */ 39739add252SChaotian Jing u32 pad_tune_reg; 3982fea5819SChaotian Jing bool async_fifo; 3992fea5819SChaotian Jing bool data_tune; 400acde28c4SChaotian Jing bool busy_check; 401d9dcbfc8SChaotian Jing bool stop_clk_fix; 402d9dcbfc8SChaotian Jing bool enhance_rx; 4032a9bde19SChaotian Jing bool support_64g; 404d087bde5SNeilBrown bool use_internal_cd; 405762d491aSChaotian Jing }; 406762d491aSChaotian Jing 40786beac37SChaotian Jing struct msdc_tune_para { 40886beac37SChaotian Jing u32 iocon; 40986beac37SChaotian Jing u32 pad_tune; 4101ede5cb8Syong mao u32 pad_cmd_tune; 411a2e6d1f6SChaotian Jing u32 emmc_top_control; 412a2e6d1f6SChaotian Jing u32 emmc_top_cmd; 41386beac37SChaotian Jing }; 41486beac37SChaotian Jing 4156397b7f5SChaotian Jing struct msdc_delay_phase { 4166397b7f5SChaotian Jing u8 maxlen; 4176397b7f5SChaotian Jing u8 start; 4186397b7f5SChaotian Jing u8 final_phase; 4194b8a43e9SChaotian Jing }; 4204b8a43e9SChaotian Jing 42120848903SChaotian Jing struct msdc_host { 42220848903SChaotian Jing struct device *dev; 423762d491aSChaotian Jing const struct mtk_mmc_compatible *dev_comp; 42420848903SChaotian Jing int cmd_rsp; 42520848903SChaotian Jing 42620848903SChaotian Jing spinlock_t lock; 42720848903SChaotian Jing struct mmc_request *mrq; 42820848903SChaotian Jing struct mmc_command *cmd; 42920848903SChaotian Jing struct mmc_data *data; 43020848903SChaotian Jing int error; 43120848903SChaotian Jing 43220848903SChaotian Jing void __iomem *base; /* host base address */ 433a2e6d1f6SChaotian Jing void __iomem *top_base; /* host top register base address */ 43420848903SChaotian Jing 43520848903SChaotian Jing struct msdc_dma dma; /* dma channel */ 43620848903SChaotian Jing u64 dma_mask; 43720848903SChaotian Jing 43820848903SChaotian Jing u32 timeout_ns; /* data timeout ns */ 43920848903SChaotian Jing u32 timeout_clks; /* data timeout clks */ 44020848903SChaotian Jing 44120848903SChaotian Jing struct pinctrl *pinctrl; 44220848903SChaotian Jing struct pinctrl_state *pins_default; 44320848903SChaotian Jing struct pinctrl_state *pins_uhs; 444527f36f5SAxe Yang struct pinctrl_state *pins_eint; 44520848903SChaotian Jing struct delayed_work req_timeout; 44620848903SChaotian Jing int irq; /* host interrupt */ 447527f36f5SAxe Yang int eint_irq; /* interrupt from sdio device for waking up system */ 448855d388dSWenbin Mei struct reset_control *reset; 44920848903SChaotian Jing 45020848903SChaotian Jing struct clk *src_clk; /* msdc source clock */ 45120848903SChaotian Jing struct clk *h_clk; /* msdc h_clk */ 452258bac4aSChaotian Jing struct clk *bus_clk; /* bus clock which used to access register */ 4533c1a8844SChaotian Jing struct clk *src_clk_cg; /* msdc source clock control gate */ 454f5eccd94SWenbin Mei struct clk *sys_clk_cg; /* msdc subsys clock control gate */ 4557b438d03SMengqi Zhang struct clk *crypto_clk; /* msdc crypto clock control gate */ 456f5eccd94SWenbin Mei struct clk_bulk_data bulk_clks[MSDC_NR_CLOCKS]; 45720848903SChaotian Jing u32 mclk; /* mmc subsystem clock frequency */ 45820848903SChaotian Jing u32 src_clk_freq; /* source clock frequency */ 4596e622947SChaotian Jing unsigned char timing; 46020848903SChaotian Jing bool vqmmc_enabled; 461d17bb71cSChaotian Jing u32 latch_ck; 4626397b7f5SChaotian Jing u32 hs400_ds_delay; 463c4ac38c6SWenbin Mei u32 hs400_ds_dly3; 4641ede5cb8Syong mao u32 hs200_cmd_int_delay; /* cmd internal delay for HS200/SDR104 */ 4651ede5cb8Syong mao u32 hs400_cmd_int_delay; /* cmd internal delay for HS400 */ 4661ede5cb8Syong mao bool hs400_cmd_resp_sel_rising; 4671ede5cb8Syong mao /* cmd response sample selection for HS400 */ 4685462ff39SChaotian Jing bool hs400_mode; /* current eMMC will run at hs400 mode */ 469c4ac38c6SWenbin Mei bool hs400_tuning; /* hs400 mode online tuning */ 470d087bde5SNeilBrown bool internal_cd; /* Use internal card-detect logic */ 47188bd652bSChun-Hung Wu bool cqhci; /* support eMMC hw cmdq */ 4724b8a43e9SChaotian Jing struct msdc_save_para save_para; /* used when gate HCLK */ 47386beac37SChaotian Jing struct msdc_tune_para def_tune_para; /* default tune setting */ 47486beac37SChaotian Jing struct msdc_tune_para saved_tune_para; /* tune result of CMD21/CMD19 */ 47588bd652bSChun-Hung Wu struct cqhci_host *cq_host; 47620848903SChaotian Jing }; 47720848903SChaotian Jing 478d4dc6ecaSAngeloGioacchino Del Regno static const struct mtk_mmc_compatible mt2701_compat = { 479d4dc6ecaSAngeloGioacchino Del Regno .clk_div_bits = 12, 480d4dc6ecaSAngeloGioacchino Del Regno .recheck_sdio_irq = true, 481d4dc6ecaSAngeloGioacchino Del Regno .hs400_tune = false, 482d4dc6ecaSAngeloGioacchino Del Regno .pad_tune_reg = MSDC_PAD_TUNE0, 483d4dc6ecaSAngeloGioacchino Del Regno .async_fifo = true, 484d4dc6ecaSAngeloGioacchino Del Regno .data_tune = true, 485d4dc6ecaSAngeloGioacchino Del Regno .busy_check = false, 486d4dc6ecaSAngeloGioacchino Del Regno .stop_clk_fix = false, 487d4dc6ecaSAngeloGioacchino Del Regno .enhance_rx = false, 488d4dc6ecaSAngeloGioacchino Del Regno .support_64g = false, 489d4dc6ecaSAngeloGioacchino Del Regno }; 490d4dc6ecaSAngeloGioacchino Del Regno 491d4dc6ecaSAngeloGioacchino Del Regno static const struct mtk_mmc_compatible mt2712_compat = { 492d4dc6ecaSAngeloGioacchino Del Regno .clk_div_bits = 12, 493d4dc6ecaSAngeloGioacchino Del Regno .recheck_sdio_irq = false, 494d4dc6ecaSAngeloGioacchino Del Regno .hs400_tune = false, 495d4dc6ecaSAngeloGioacchino Del Regno .pad_tune_reg = MSDC_PAD_TUNE0, 496d4dc6ecaSAngeloGioacchino Del Regno .async_fifo = true, 497d4dc6ecaSAngeloGioacchino Del Regno .data_tune = true, 498d4dc6ecaSAngeloGioacchino Del Regno .busy_check = true, 499d4dc6ecaSAngeloGioacchino Del Regno .stop_clk_fix = true, 500d4dc6ecaSAngeloGioacchino Del Regno .enhance_rx = true, 501d4dc6ecaSAngeloGioacchino Del Regno .support_64g = true, 502d4dc6ecaSAngeloGioacchino Del Regno }; 503d4dc6ecaSAngeloGioacchino Del Regno 504d4dc6ecaSAngeloGioacchino Del Regno static const struct mtk_mmc_compatible mt6779_compat = { 505d4dc6ecaSAngeloGioacchino Del Regno .clk_div_bits = 12, 506d4dc6ecaSAngeloGioacchino Del Regno .recheck_sdio_irq = false, 507d4dc6ecaSAngeloGioacchino Del Regno .hs400_tune = false, 508d4dc6ecaSAngeloGioacchino Del Regno .pad_tune_reg = MSDC_PAD_TUNE0, 509d4dc6ecaSAngeloGioacchino Del Regno .async_fifo = true, 510d4dc6ecaSAngeloGioacchino Del Regno .data_tune = true, 511d4dc6ecaSAngeloGioacchino Del Regno .busy_check = true, 512d4dc6ecaSAngeloGioacchino Del Regno .stop_clk_fix = true, 513d4dc6ecaSAngeloGioacchino Del Regno .enhance_rx = true, 514d4dc6ecaSAngeloGioacchino Del Regno .support_64g = true, 515d4dc6ecaSAngeloGioacchino Del Regno }; 516d4dc6ecaSAngeloGioacchino Del Regno 517f7209cbfSAngeloGioacchino Del Regno static const struct mtk_mmc_compatible mt6795_compat = { 518f7209cbfSAngeloGioacchino Del Regno .clk_div_bits = 8, 519f7209cbfSAngeloGioacchino Del Regno .recheck_sdio_irq = false, 520f7209cbfSAngeloGioacchino Del Regno .hs400_tune = true, 521f7209cbfSAngeloGioacchino Del Regno .pad_tune_reg = MSDC_PAD_TUNE, 522f7209cbfSAngeloGioacchino Del Regno .async_fifo = false, 523f7209cbfSAngeloGioacchino Del Regno .data_tune = false, 524f7209cbfSAngeloGioacchino Del Regno .busy_check = false, 525f7209cbfSAngeloGioacchino Del Regno .stop_clk_fix = false, 526f7209cbfSAngeloGioacchino Del Regno .enhance_rx = false, 527f7209cbfSAngeloGioacchino Del Regno .support_64g = false, 528f7209cbfSAngeloGioacchino Del Regno }; 529f7209cbfSAngeloGioacchino Del Regno 530d4dc6ecaSAngeloGioacchino Del Regno static const struct mtk_mmc_compatible mt7620_compat = { 531d4dc6ecaSAngeloGioacchino Del Regno .clk_div_bits = 8, 532d4dc6ecaSAngeloGioacchino Del Regno .recheck_sdio_irq = true, 533d4dc6ecaSAngeloGioacchino Del Regno .hs400_tune = false, 534d4dc6ecaSAngeloGioacchino Del Regno .pad_tune_reg = MSDC_PAD_TUNE, 535d4dc6ecaSAngeloGioacchino Del Regno .async_fifo = false, 536d4dc6ecaSAngeloGioacchino Del Regno .data_tune = false, 537d4dc6ecaSAngeloGioacchino Del Regno .busy_check = false, 538d4dc6ecaSAngeloGioacchino Del Regno .stop_clk_fix = false, 539d4dc6ecaSAngeloGioacchino Del Regno .enhance_rx = false, 540d4dc6ecaSAngeloGioacchino Del Regno .use_internal_cd = true, 541d4dc6ecaSAngeloGioacchino Del Regno }; 542d4dc6ecaSAngeloGioacchino Del Regno 543d4dc6ecaSAngeloGioacchino Del Regno static const struct mtk_mmc_compatible mt7622_compat = { 544d4dc6ecaSAngeloGioacchino Del Regno .clk_div_bits = 12, 545d4dc6ecaSAngeloGioacchino Del Regno .recheck_sdio_irq = true, 546d4dc6ecaSAngeloGioacchino Del Regno .hs400_tune = false, 547d4dc6ecaSAngeloGioacchino Del Regno .pad_tune_reg = MSDC_PAD_TUNE0, 548d4dc6ecaSAngeloGioacchino Del Regno .async_fifo = true, 549d4dc6ecaSAngeloGioacchino Del Regno .data_tune = true, 550d4dc6ecaSAngeloGioacchino Del Regno .busy_check = true, 551d4dc6ecaSAngeloGioacchino Del Regno .stop_clk_fix = true, 552d4dc6ecaSAngeloGioacchino Del Regno .enhance_rx = true, 553d4dc6ecaSAngeloGioacchino Del Regno .support_64g = false, 554d4dc6ecaSAngeloGioacchino Del Regno }; 555d4dc6ecaSAngeloGioacchino Del Regno 55624e961b9SSam Shih static const struct mtk_mmc_compatible mt7986_compat = { 55724e961b9SSam Shih .clk_div_bits = 12, 55824e961b9SSam Shih .recheck_sdio_irq = true, 55924e961b9SSam Shih .hs400_tune = false, 56024e961b9SSam Shih .pad_tune_reg = MSDC_PAD_TUNE0, 56124e961b9SSam Shih .async_fifo = true, 56224e961b9SSam Shih .data_tune = true, 56324e961b9SSam Shih .busy_check = true, 56424e961b9SSam Shih .stop_clk_fix = true, 56524e961b9SSam Shih .enhance_rx = true, 56624e961b9SSam Shih .support_64g = true, 56724e961b9SSam Shih }; 56824e961b9SSam Shih 569762d491aSChaotian Jing static const struct mtk_mmc_compatible mt8135_compat = { 570762d491aSChaotian Jing .clk_div_bits = 8, 571903a72ecSyong mao .recheck_sdio_irq = true, 5727f3d5852SChaotian Jing .hs400_tune = false, 57339add252SChaotian Jing .pad_tune_reg = MSDC_PAD_TUNE, 5742fea5819SChaotian Jing .async_fifo = false, 5752fea5819SChaotian Jing .data_tune = false, 576acde28c4SChaotian Jing .busy_check = false, 577d9dcbfc8SChaotian Jing .stop_clk_fix = false, 578d9dcbfc8SChaotian Jing .enhance_rx = false, 5792a9bde19SChaotian Jing .support_64g = false, 580762d491aSChaotian Jing }; 581762d491aSChaotian Jing 582762d491aSChaotian Jing static const struct mtk_mmc_compatible mt8173_compat = { 583762d491aSChaotian Jing .clk_div_bits = 8, 5849e2582e5Syong mao .recheck_sdio_irq = true, 5857f3d5852SChaotian Jing .hs400_tune = true, 58639add252SChaotian Jing .pad_tune_reg = MSDC_PAD_TUNE, 5872fea5819SChaotian Jing .async_fifo = false, 5882fea5819SChaotian Jing .data_tune = false, 589acde28c4SChaotian Jing .busy_check = false, 590d9dcbfc8SChaotian Jing .stop_clk_fix = false, 591d9dcbfc8SChaotian Jing .enhance_rx = false, 5922a9bde19SChaotian Jing .support_64g = false, 593762d491aSChaotian Jing }; 594762d491aSChaotian Jing 595a2e6d1f6SChaotian Jing static const struct mtk_mmc_compatible mt8183_compat = { 596a2e6d1f6SChaotian Jing .clk_div_bits = 12, 5979e2582e5Syong mao .recheck_sdio_irq = false, 598a2e6d1f6SChaotian Jing .hs400_tune = false, 599a2e6d1f6SChaotian Jing .pad_tune_reg = MSDC_PAD_TUNE0, 600a2e6d1f6SChaotian Jing .async_fifo = true, 601a2e6d1f6SChaotian Jing .data_tune = true, 602a2e6d1f6SChaotian Jing .busy_check = true, 603a2e6d1f6SChaotian Jing .stop_clk_fix = true, 604a2e6d1f6SChaotian Jing .enhance_rx = true, 605a2e6d1f6SChaotian Jing .support_64g = true, 606a2e6d1f6SChaotian Jing }; 607a2e6d1f6SChaotian Jing 60889822b73SFabien Parent static const struct mtk_mmc_compatible mt8516_compat = { 60989822b73SFabien Parent .clk_div_bits = 12, 610903a72ecSyong mao .recheck_sdio_irq = true, 61189822b73SFabien Parent .hs400_tune = false, 61289822b73SFabien Parent .pad_tune_reg = MSDC_PAD_TUNE0, 61389822b73SFabien Parent .async_fifo = true, 61489822b73SFabien Parent .data_tune = true, 61589822b73SFabien Parent .busy_check = true, 61689822b73SFabien Parent .stop_clk_fix = true, 61789822b73SFabien Parent }; 61889822b73SFabien Parent 619762d491aSChaotian Jing static const struct of_device_id msdc_of_ids[] = { 620d4dc6ecaSAngeloGioacchino Del Regno { .compatible = "mediatek,mt2701-mmc", .data = &mt2701_compat}, 621d4dc6ecaSAngeloGioacchino Del Regno { .compatible = "mediatek,mt2712-mmc", .data = &mt2712_compat}, 622d4dc6ecaSAngeloGioacchino Del Regno { .compatible = "mediatek,mt6779-mmc", .data = &mt6779_compat}, 623f7209cbfSAngeloGioacchino Del Regno { .compatible = "mediatek,mt6795-mmc", .data = &mt6795_compat}, 624d4dc6ecaSAngeloGioacchino Del Regno { .compatible = "mediatek,mt7620-mmc", .data = &mt7620_compat}, 625d4dc6ecaSAngeloGioacchino Del Regno { .compatible = "mediatek,mt7622-mmc", .data = &mt7622_compat}, 62624e961b9SSam Shih { .compatible = "mediatek,mt7986-mmc", .data = &mt7986_compat}, 627762d491aSChaotian Jing { .compatible = "mediatek,mt8135-mmc", .data = &mt8135_compat}, 628762d491aSChaotian Jing { .compatible = "mediatek,mt8173-mmc", .data = &mt8173_compat}, 629a2e6d1f6SChaotian Jing { .compatible = "mediatek,mt8183-mmc", .data = &mt8183_compat}, 63089822b73SFabien Parent { .compatible = "mediatek,mt8516-mmc", .data = &mt8516_compat}, 631d4dc6ecaSAngeloGioacchino Del Regno 632762d491aSChaotian Jing {} 633762d491aSChaotian Jing }; 634762d491aSChaotian Jing MODULE_DEVICE_TABLE(of, msdc_of_ids); 635762d491aSChaotian Jing 63620848903SChaotian Jing static void sdr_set_bits(void __iomem *reg, u32 bs) 63720848903SChaotian Jing { 63820848903SChaotian Jing u32 val = readl(reg); 63920848903SChaotian Jing 64020848903SChaotian Jing val |= bs; 64120848903SChaotian Jing writel(val, reg); 64220848903SChaotian Jing } 64320848903SChaotian Jing 64420848903SChaotian Jing static void sdr_clr_bits(void __iomem *reg, u32 bs) 64520848903SChaotian Jing { 64620848903SChaotian Jing u32 val = readl(reg); 64720848903SChaotian Jing 64820848903SChaotian Jing val &= ~bs; 64920848903SChaotian Jing writel(val, reg); 65020848903SChaotian Jing } 65120848903SChaotian Jing 65220848903SChaotian Jing static void sdr_set_field(void __iomem *reg, u32 field, u32 val) 65320848903SChaotian Jing { 65420848903SChaotian Jing unsigned int tv = readl(reg); 65520848903SChaotian Jing 65620848903SChaotian Jing tv &= ~field; 65720848903SChaotian Jing tv |= ((val) << (ffs((unsigned int)field) - 1)); 65820848903SChaotian Jing writel(tv, reg); 65920848903SChaotian Jing } 66020848903SChaotian Jing 66120848903SChaotian Jing static void sdr_get_field(void __iomem *reg, u32 field, u32 *val) 66220848903SChaotian Jing { 66320848903SChaotian Jing unsigned int tv = readl(reg); 66420848903SChaotian Jing 66520848903SChaotian Jing *val = ((tv & field) >> (ffs((unsigned int)field) - 1)); 66620848903SChaotian Jing } 66720848903SChaotian Jing 66820848903SChaotian Jing static void msdc_reset_hw(struct msdc_host *host) 66920848903SChaotian Jing { 67020848903SChaotian Jing u32 val; 67120848903SChaotian Jing 67220848903SChaotian Jing sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_RST); 673ffaea6ebSAngeloGioacchino Del Regno readl_poll_timeout(host->base + MSDC_CFG, val, !(val & MSDC_CFG_RST), 0, 0); 67420848903SChaotian Jing 67520848903SChaotian Jing sdr_set_bits(host->base + MSDC_FIFOCS, MSDC_FIFOCS_CLR); 676ffaea6ebSAngeloGioacchino Del Regno readl_poll_timeout(host->base + MSDC_FIFOCS, val, 677ffaea6ebSAngeloGioacchino Del Regno !(val & MSDC_FIFOCS_CLR), 0, 0); 67820848903SChaotian Jing 67920848903SChaotian Jing val = readl(host->base + MSDC_INT); 68020848903SChaotian Jing writel(val, host->base + MSDC_INT); 68120848903SChaotian Jing } 68220848903SChaotian Jing 68320848903SChaotian Jing static void msdc_cmd_next(struct msdc_host *host, 68420848903SChaotian Jing struct mmc_request *mrq, struct mmc_command *cmd); 6859e2582e5Syong mao static void __msdc_enable_sdio_irq(struct msdc_host *host, int enb); 68620848903SChaotian Jing 687726a9aacSChaotian Jing static const u32 cmd_ints_mask = MSDC_INTEN_CMDRDY | MSDC_INTEN_RSPCRCERR | 688726a9aacSChaotian Jing MSDC_INTEN_CMDTMO | MSDC_INTEN_ACMDRDY | 689726a9aacSChaotian Jing MSDC_INTEN_ACMDCRCERR | MSDC_INTEN_ACMDTMO; 690726a9aacSChaotian Jing static const u32 data_ints_mask = MSDC_INTEN_XFER_COMPL | MSDC_INTEN_DATTMO | 69120848903SChaotian Jing MSDC_INTEN_DATCRCERR | MSDC_INTEN_DMA_BDCSERR | 69220848903SChaotian Jing MSDC_INTEN_DMA_GPDCSERR | MSDC_INTEN_DMA_PROTECT; 69320848903SChaotian Jing 69420848903SChaotian Jing static u8 msdc_dma_calcs(u8 *buf, u32 len) 69520848903SChaotian Jing { 69620848903SChaotian Jing u32 i, sum = 0; 69720848903SChaotian Jing 69820848903SChaotian Jing for (i = 0; i < len; i++) 69920848903SChaotian Jing sum += buf[i]; 70020848903SChaotian Jing return 0xff - (u8) sum; 70120848903SChaotian Jing } 70220848903SChaotian Jing 70320848903SChaotian Jing static inline void msdc_dma_setup(struct msdc_host *host, struct msdc_dma *dma, 70420848903SChaotian Jing struct mmc_data *data) 70520848903SChaotian Jing { 70620848903SChaotian Jing unsigned int j, dma_len; 70720848903SChaotian Jing dma_addr_t dma_address; 70820848903SChaotian Jing u32 dma_ctrl; 70920848903SChaotian Jing struct scatterlist *sg; 71020848903SChaotian Jing struct mt_gpdma_desc *gpd; 71120848903SChaotian Jing struct mt_bdma_desc *bd; 71220848903SChaotian Jing 71320848903SChaotian Jing sg = data->sg; 71420848903SChaotian Jing 71520848903SChaotian Jing gpd = dma->gpd; 71620848903SChaotian Jing bd = dma->bd; 71720848903SChaotian Jing 71820848903SChaotian Jing /* modify gpd */ 71920848903SChaotian Jing gpd->gpd_info |= GPDMA_DESC_HWO; 72020848903SChaotian Jing gpd->gpd_info |= GPDMA_DESC_BDP; 72120848903SChaotian Jing /* need to clear first. use these bits to calc checksum */ 72220848903SChaotian Jing gpd->gpd_info &= ~GPDMA_DESC_CHECKSUM; 72320848903SChaotian Jing gpd->gpd_info |= msdc_dma_calcs((u8 *) gpd, 16) << 8; 72420848903SChaotian Jing 72520848903SChaotian Jing /* modify bd */ 72620848903SChaotian Jing for_each_sg(data->sg, sg, data->sg_count, j) { 72720848903SChaotian Jing dma_address = sg_dma_address(sg); 72820848903SChaotian Jing dma_len = sg_dma_len(sg); 72920848903SChaotian Jing 73020848903SChaotian Jing /* init bd */ 73120848903SChaotian Jing bd[j].bd_info &= ~BDMA_DESC_BLKPAD; 73220848903SChaotian Jing bd[j].bd_info &= ~BDMA_DESC_DWPAD; 7332a9bde19SChaotian Jing bd[j].ptr = lower_32_bits(dma_address); 7342a9bde19SChaotian Jing if (host->dev_comp->support_64g) { 7352a9bde19SChaotian Jing bd[j].bd_info &= ~BDMA_DESC_PTR_H4; 7362a9bde19SChaotian Jing bd[j].bd_info |= (upper_32_bits(dma_address) & 0xf) 7372a9bde19SChaotian Jing << 28; 7382a9bde19SChaotian Jing } 7396ef042bdSChaotian Jing 7406ef042bdSChaotian Jing if (host->dev_comp->support_64g) { 7416ef042bdSChaotian Jing bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN_EXT; 7426ef042bdSChaotian Jing bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN_EXT); 7436ef042bdSChaotian Jing } else { 74420848903SChaotian Jing bd[j].bd_data_len &= ~BDMA_DESC_BUFLEN; 74520848903SChaotian Jing bd[j].bd_data_len |= (dma_len & BDMA_DESC_BUFLEN); 7466ef042bdSChaotian Jing } 74720848903SChaotian Jing 74820848903SChaotian Jing if (j == data->sg_count - 1) /* the last bd */ 74920848903SChaotian Jing bd[j].bd_info |= BDMA_DESC_EOL; 75020848903SChaotian Jing else 75120848903SChaotian Jing bd[j].bd_info &= ~BDMA_DESC_EOL; 75220848903SChaotian Jing 753*4b323f02SYu Zhe /* checksum need to clear first */ 75420848903SChaotian Jing bd[j].bd_info &= ~BDMA_DESC_CHECKSUM; 75520848903SChaotian Jing bd[j].bd_info |= msdc_dma_calcs((u8 *)(&bd[j]), 16) << 8; 75620848903SChaotian Jing } 75720848903SChaotian Jing 75820848903SChaotian Jing sdr_set_field(host->base + MSDC_DMA_CFG, MSDC_DMA_CFG_DECSEN, 1); 75920848903SChaotian Jing dma_ctrl = readl_relaxed(host->base + MSDC_DMA_CTRL); 76020848903SChaotian Jing dma_ctrl &= ~(MSDC_DMA_CTRL_BRUSTSZ | MSDC_DMA_CTRL_MODE); 7614fe54318SAngeloGioacchino Del Regno dma_ctrl |= (MSDC_BURST_64B << 12 | BIT(8)); 76220848903SChaotian Jing writel_relaxed(dma_ctrl, host->base + MSDC_DMA_CTRL); 7632a9bde19SChaotian Jing if (host->dev_comp->support_64g) 7642a9bde19SChaotian Jing sdr_set_field(host->base + DMA_SA_H4BIT, DMA_ADDR_HIGH_4BIT, 7652a9bde19SChaotian Jing upper_32_bits(dma->gpd_addr) & 0xf); 7662a9bde19SChaotian Jing writel(lower_32_bits(dma->gpd_addr), host->base + MSDC_DMA_SA); 76720848903SChaotian Jing } 76820848903SChaotian Jing 76915107135SYue Hu static void msdc_prepare_data(struct msdc_host *host, struct mmc_data *data) 77020848903SChaotian Jing { 77120848903SChaotian Jing if (!(data->host_cookie & MSDC_PREPARE_FLAG)) { 77220848903SChaotian Jing data->host_cookie |= MSDC_PREPARE_FLAG; 77320848903SChaotian Jing data->sg_count = dma_map_sg(host->dev, data->sg, data->sg_len, 774feeef096SHeiner Kallweit mmc_get_dma_dir(data)); 77520848903SChaotian Jing } 77620848903SChaotian Jing } 77720848903SChaotian Jing 77815107135SYue Hu static void msdc_unprepare_data(struct msdc_host *host, struct mmc_data *data) 77920848903SChaotian Jing { 78020848903SChaotian Jing if (data->host_cookie & MSDC_ASYNC_FLAG) 78120848903SChaotian Jing return; 78220848903SChaotian Jing 78320848903SChaotian Jing if (data->host_cookie & MSDC_PREPARE_FLAG) { 78420848903SChaotian Jing dma_unmap_sg(host->dev, data->sg, data->sg_len, 785feeef096SHeiner Kallweit mmc_get_dma_dir(data)); 78620848903SChaotian Jing data->host_cookie &= ~MSDC_PREPARE_FLAG; 78720848903SChaotian Jing } 78820848903SChaotian Jing } 78920848903SChaotian Jing 790557011b6SChun-Hung Wu static u64 msdc_timeout_cal(struct msdc_host *host, u64 ns, u64 clks) 79120848903SChaotian Jing { 7920caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 793557011b6SChun-Hung Wu u64 timeout, clk_ns; 79420848903SChaotian Jing u32 mode = 0; 79520848903SChaotian Jing 7960caf60c4SAmey Narkhede if (mmc->actual_clock == 0) { 79720848903SChaotian Jing timeout = 0; 79820848903SChaotian Jing } else { 799557011b6SChun-Hung Wu clk_ns = 1000000000ULL; 8000caf60c4SAmey Narkhede do_div(clk_ns, mmc->actual_clock); 801557011b6SChun-Hung Wu timeout = ns + clk_ns - 1; 802557011b6SChun-Hung Wu do_div(timeout, clk_ns); 803557011b6SChun-Hung Wu timeout += clks; 80420848903SChaotian Jing /* in 1048576 sclk cycle unit */ 8054fe54318SAngeloGioacchino Del Regno timeout = DIV_ROUND_UP(timeout, BIT(20)); 806762d491aSChaotian Jing if (host->dev_comp->clk_div_bits == 8) 807762d491aSChaotian Jing sdr_get_field(host->base + MSDC_CFG, 808762d491aSChaotian Jing MSDC_CFG_CKMOD, &mode); 809762d491aSChaotian Jing else 810762d491aSChaotian Jing sdr_get_field(host->base + MSDC_CFG, 811762d491aSChaotian Jing MSDC_CFG_CKMOD_EXTRA, &mode); 81220848903SChaotian Jing /*DDR mode will double the clk cycles for data timeout */ 81320848903SChaotian Jing timeout = mode >= 2 ? timeout * 2 : timeout; 81420848903SChaotian Jing timeout = timeout > 1 ? timeout - 1 : 0; 81520848903SChaotian Jing } 816557011b6SChun-Hung Wu return timeout; 817557011b6SChun-Hung Wu } 818557011b6SChun-Hung Wu 819557011b6SChun-Hung Wu /* clock control primitives */ 820557011b6SChun-Hung Wu static void msdc_set_timeout(struct msdc_host *host, u64 ns, u64 clks) 821557011b6SChun-Hung Wu { 822557011b6SChun-Hung Wu u64 timeout; 823557011b6SChun-Hung Wu 824557011b6SChun-Hung Wu host->timeout_ns = ns; 825557011b6SChun-Hung Wu host->timeout_clks = clks; 826557011b6SChun-Hung Wu 827557011b6SChun-Hung Wu timeout = msdc_timeout_cal(host, ns, clks); 828557011b6SChun-Hung Wu sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 829557011b6SChun-Hung Wu (u32)(timeout > 255 ? 255 : timeout)); 83020848903SChaotian Jing } 83120848903SChaotian Jing 83288bd652bSChun-Hung Wu static void msdc_set_busy_timeout(struct msdc_host *host, u64 ns, u64 clks) 83388bd652bSChun-Hung Wu { 83488bd652bSChun-Hung Wu u64 timeout; 83588bd652bSChun-Hung Wu 83688bd652bSChun-Hung Wu timeout = msdc_timeout_cal(host, ns, clks); 83788bd652bSChun-Hung Wu sdr_set_field(host->base + SDC_CFG, SDC_CFG_WRDTOC, 83888bd652bSChun-Hung Wu (u32)(timeout > 8191 ? 8191 : timeout)); 83988bd652bSChun-Hung Wu } 84088bd652bSChun-Hung Wu 84120848903SChaotian Jing static void msdc_gate_clock(struct msdc_host *host) 84220848903SChaotian Jing { 843f5eccd94SWenbin Mei clk_bulk_disable_unprepare(MSDC_NR_CLOCKS, host->bulk_clks); 8447b438d03SMengqi Zhang clk_disable_unprepare(host->crypto_clk); 8453c1a8844SChaotian Jing clk_disable_unprepare(host->src_clk_cg); 84620848903SChaotian Jing clk_disable_unprepare(host->src_clk); 847258bac4aSChaotian Jing clk_disable_unprepare(host->bus_clk); 84820848903SChaotian Jing clk_disable_unprepare(host->h_clk); 84920848903SChaotian Jing } 85020848903SChaotian Jing 851ffaea6ebSAngeloGioacchino Del Regno static int msdc_ungate_clock(struct msdc_host *host) 85220848903SChaotian Jing { 853ffaea6ebSAngeloGioacchino Del Regno u32 val; 854f5eccd94SWenbin Mei int ret; 855f5eccd94SWenbin Mei 85620848903SChaotian Jing clk_prepare_enable(host->h_clk); 857258bac4aSChaotian Jing clk_prepare_enable(host->bus_clk); 85820848903SChaotian Jing clk_prepare_enable(host->src_clk); 8593c1a8844SChaotian Jing clk_prepare_enable(host->src_clk_cg); 8607b438d03SMengqi Zhang clk_prepare_enable(host->crypto_clk); 861f5eccd94SWenbin Mei ret = clk_bulk_prepare_enable(MSDC_NR_CLOCKS, host->bulk_clks); 862f5eccd94SWenbin Mei if (ret) { 863f5eccd94SWenbin Mei dev_err(host->dev, "Cannot enable pclk/axi/ahb clock gates\n"); 864ffaea6ebSAngeloGioacchino Del Regno return ret; 865f5eccd94SWenbin Mei } 866f5eccd94SWenbin Mei 867ffaea6ebSAngeloGioacchino Del Regno return readl_poll_timeout(host->base + MSDC_CFG, val, 868ffaea6ebSAngeloGioacchino Del Regno (val & MSDC_CFG_CKSTB), 1, 20000); 86920848903SChaotian Jing } 87020848903SChaotian Jing 8716e622947SChaotian Jing static void msdc_set_mclk(struct msdc_host *host, unsigned char timing, u32 hz) 87220848903SChaotian Jing { 8730caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 87420848903SChaotian Jing u32 mode; 87520848903SChaotian Jing u32 flags; 87620848903SChaotian Jing u32 div; 87720848903SChaotian Jing u32 sclk; 87839add252SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 879ffaea6ebSAngeloGioacchino Del Regno u32 val; 88020848903SChaotian Jing 88120848903SChaotian Jing if (!hz) { 88220848903SChaotian Jing dev_dbg(host->dev, "set mclk to 0\n"); 88320848903SChaotian Jing host->mclk = 0; 8840caf60c4SAmey Narkhede mmc->actual_clock = 0; 88520848903SChaotian Jing sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); 88620848903SChaotian Jing return; 88720848903SChaotian Jing } 88820848903SChaotian Jing 88920848903SChaotian Jing flags = readl(host->base + MSDC_INTEN); 89020848903SChaotian Jing sdr_clr_bits(host->base + MSDC_INTEN, flags); 891762d491aSChaotian Jing if (host->dev_comp->clk_div_bits == 8) 8926397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_HS400_CK_MODE); 893762d491aSChaotian Jing else 894762d491aSChaotian Jing sdr_clr_bits(host->base + MSDC_CFG, 895762d491aSChaotian Jing MSDC_CFG_HS400_CK_MODE_EXTRA); 8966e622947SChaotian Jing if (timing == MMC_TIMING_UHS_DDR50 || 8976397b7f5SChaotian Jing timing == MMC_TIMING_MMC_DDR52 || 8986397b7f5SChaotian Jing timing == MMC_TIMING_MMC_HS400) { 8996397b7f5SChaotian Jing if (timing == MMC_TIMING_MMC_HS400) 9006397b7f5SChaotian Jing mode = 0x3; 9016397b7f5SChaotian Jing else 90220848903SChaotian Jing mode = 0x2; /* ddr mode and use divisor */ 9036397b7f5SChaotian Jing 90420848903SChaotian Jing if (hz >= (host->src_clk_freq >> 2)) { 90520848903SChaotian Jing div = 0; /* mean div = 1/4 */ 90620848903SChaotian Jing sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */ 90720848903SChaotian Jing } else { 90820848903SChaotian Jing div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); 90920848903SChaotian Jing sclk = (host->src_clk_freq >> 2) / div; 91020848903SChaotian Jing div = (div >> 1); 91120848903SChaotian Jing } 9126397b7f5SChaotian Jing 9136397b7f5SChaotian Jing if (timing == MMC_TIMING_MMC_HS400 && 9146397b7f5SChaotian Jing hz >= (host->src_clk_freq >> 1)) { 915762d491aSChaotian Jing if (host->dev_comp->clk_div_bits == 8) 9166397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_CFG, 9176397b7f5SChaotian Jing MSDC_CFG_HS400_CK_MODE); 918762d491aSChaotian Jing else 919762d491aSChaotian Jing sdr_set_bits(host->base + MSDC_CFG, 920762d491aSChaotian Jing MSDC_CFG_HS400_CK_MODE_EXTRA); 9216397b7f5SChaotian Jing sclk = host->src_clk_freq >> 1; 9226397b7f5SChaotian Jing div = 0; /* div is ignore when bit18 is set */ 9236397b7f5SChaotian Jing } 92420848903SChaotian Jing } else if (hz >= host->src_clk_freq) { 92520848903SChaotian Jing mode = 0x1; /* no divisor */ 92620848903SChaotian Jing div = 0; 92720848903SChaotian Jing sclk = host->src_clk_freq; 92820848903SChaotian Jing } else { 92920848903SChaotian Jing mode = 0x0; /* use divisor */ 93020848903SChaotian Jing if (hz >= (host->src_clk_freq >> 1)) { 93120848903SChaotian Jing div = 0; /* mean div = 1/2 */ 93220848903SChaotian Jing sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */ 93320848903SChaotian Jing } else { 93420848903SChaotian Jing div = (host->src_clk_freq + ((hz << 2) - 1)) / (hz << 2); 93520848903SChaotian Jing sclk = (host->src_clk_freq >> 2) / div; 93620848903SChaotian Jing } 93720848903SChaotian Jing } 9383c1a8844SChaotian Jing sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); 939e5e8b224SAngeloGioacchino Del Regno 9403c1a8844SChaotian Jing clk_disable_unprepare(host->src_clk_cg); 941762d491aSChaotian Jing if (host->dev_comp->clk_div_bits == 8) 942762d491aSChaotian Jing sdr_set_field(host->base + MSDC_CFG, 943762d491aSChaotian Jing MSDC_CFG_CKMOD | MSDC_CFG_CKDIV, 94440ceda09Syong mao (mode << 8) | div); 945762d491aSChaotian Jing else 946762d491aSChaotian Jing sdr_set_field(host->base + MSDC_CFG, 947762d491aSChaotian Jing MSDC_CFG_CKMOD_EXTRA | MSDC_CFG_CKDIV_EXTRA, 948762d491aSChaotian Jing (mode << 12) | div); 949762d491aSChaotian Jing 950e5e8b224SAngeloGioacchino Del Regno clk_prepare_enable(host->src_clk_cg); 951ffaea6ebSAngeloGioacchino Del Regno readl_poll_timeout(host->base + MSDC_CFG, val, (val & MSDC_CFG_CKSTB), 0, 0); 9523c1a8844SChaotian Jing sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_CKPDN); 9530caf60c4SAmey Narkhede mmc->actual_clock = sclk; 95420848903SChaotian Jing host->mclk = hz; 9556e622947SChaotian Jing host->timing = timing; 95620848903SChaotian Jing /* need because clk changed. */ 95720848903SChaotian Jing msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); 95820848903SChaotian Jing sdr_set_bits(host->base + MSDC_INTEN, flags); 95920848903SChaotian Jing 96086beac37SChaotian Jing /* 96186beac37SChaotian Jing * mmc_select_hs400() will drop to 50Mhz and High speed mode, 96286beac37SChaotian Jing * tune result of hs200/200Mhz is not suitable for 50Mhz 96386beac37SChaotian Jing */ 9640caf60c4SAmey Narkhede if (mmc->actual_clock <= 52000000) { 96586beac37SChaotian Jing writel(host->def_tune_para.iocon, host->base + MSDC_IOCON); 966a2e6d1f6SChaotian Jing if (host->top_base) { 967a2e6d1f6SChaotian Jing writel(host->def_tune_para.emmc_top_control, 968a2e6d1f6SChaotian Jing host->top_base + EMMC_TOP_CONTROL); 969a2e6d1f6SChaotian Jing writel(host->def_tune_para.emmc_top_cmd, 970a2e6d1f6SChaotian Jing host->top_base + EMMC_TOP_CMD); 971a2e6d1f6SChaotian Jing } else { 972a2e6d1f6SChaotian Jing writel(host->def_tune_para.pad_tune, 973a2e6d1f6SChaotian Jing host->base + tune_reg); 974a2e6d1f6SChaotian Jing } 97586beac37SChaotian Jing } else { 97686beac37SChaotian Jing writel(host->saved_tune_para.iocon, host->base + MSDC_IOCON); 9771ede5cb8Syong mao writel(host->saved_tune_para.pad_cmd_tune, 9781ede5cb8Syong mao host->base + PAD_CMD_TUNE); 979a2e6d1f6SChaotian Jing if (host->top_base) { 980a2e6d1f6SChaotian Jing writel(host->saved_tune_para.emmc_top_control, 981a2e6d1f6SChaotian Jing host->top_base + EMMC_TOP_CONTROL); 982a2e6d1f6SChaotian Jing writel(host->saved_tune_para.emmc_top_cmd, 983a2e6d1f6SChaotian Jing host->top_base + EMMC_TOP_CMD); 984a2e6d1f6SChaotian Jing } else { 985a2e6d1f6SChaotian Jing writel(host->saved_tune_para.pad_tune, 986a2e6d1f6SChaotian Jing host->base + tune_reg); 987a2e6d1f6SChaotian Jing } 98886beac37SChaotian Jing } 98986beac37SChaotian Jing 9907f3d5852SChaotian Jing if (timing == MMC_TIMING_MMC_HS400 && 9917f3d5852SChaotian Jing host->dev_comp->hs400_tune) 9923751e008SChaotian Jing sdr_set_field(host->base + tune_reg, 9931ede5cb8Syong mao MSDC_PAD_TUNE_CMDRRDLY, 9941ede5cb8Syong mao host->hs400_cmd_int_delay); 9950caf60c4SAmey Narkhede dev_dbg(host->dev, "sclk: %d, timing: %d\n", mmc->actual_clock, 99656f6cbbeSChaotian Jing timing); 99720848903SChaotian Jing } 99820848903SChaotian Jing 99920848903SChaotian Jing static inline u32 msdc_cmd_find_resp(struct msdc_host *host, 1000961e40f7SChanWoo Lee struct mmc_command *cmd) 100120848903SChaotian Jing { 100220848903SChaotian Jing u32 resp; 100320848903SChaotian Jing 100420848903SChaotian Jing switch (mmc_resp_type(cmd)) { 100520848903SChaotian Jing /* Actually, R1, R5, R6, R7 are the same */ 100620848903SChaotian Jing case MMC_RSP_R1: 100720848903SChaotian Jing resp = 0x1; 100820848903SChaotian Jing break; 100920848903SChaotian Jing case MMC_RSP_R1B: 101020848903SChaotian Jing resp = 0x7; 101120848903SChaotian Jing break; 101220848903SChaotian Jing case MMC_RSP_R2: 101320848903SChaotian Jing resp = 0x2; 101420848903SChaotian Jing break; 101520848903SChaotian Jing case MMC_RSP_R3: 101620848903SChaotian Jing resp = 0x3; 101720848903SChaotian Jing break; 101820848903SChaotian Jing case MMC_RSP_NONE: 101920848903SChaotian Jing default: 102020848903SChaotian Jing resp = 0x0; 102120848903SChaotian Jing break; 102220848903SChaotian Jing } 102320848903SChaotian Jing 102420848903SChaotian Jing return resp; 102520848903SChaotian Jing } 102620848903SChaotian Jing 102720848903SChaotian Jing static inline u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host, 102820848903SChaotian Jing struct mmc_request *mrq, struct mmc_command *cmd) 102920848903SChaotian Jing { 10300caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 103120848903SChaotian Jing /* rawcmd : 103220848903SChaotian Jing * vol_swt << 30 | auto_cmd << 28 | blklen << 16 | go_irq << 15 | 103320848903SChaotian Jing * stop << 14 | rw << 13 | dtype << 11 | rsptyp << 7 | brk << 6 | opcode 103420848903SChaotian Jing */ 103520848903SChaotian Jing u32 opcode = cmd->opcode; 1036961e40f7SChanWoo Lee u32 resp = msdc_cmd_find_resp(host, cmd); 103720848903SChaotian Jing u32 rawcmd = (opcode & 0x3f) | ((resp & 0x7) << 7); 103820848903SChaotian Jing 103920848903SChaotian Jing host->cmd_rsp = resp; 104020848903SChaotian Jing 104120848903SChaotian Jing if ((opcode == SD_IO_RW_DIRECT && cmd->flags == (unsigned int) -1) || 104220848903SChaotian Jing opcode == MMC_STOP_TRANSMISSION) 10434fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(14); 104420848903SChaotian Jing else if (opcode == SD_SWITCH_VOLTAGE) 10454fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(30); 104620848903SChaotian Jing else if (opcode == SD_APP_SEND_SCR || 104720848903SChaotian Jing opcode == SD_APP_SEND_NUM_WR_BLKS || 104820848903SChaotian Jing (opcode == SD_SWITCH && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || 104920848903SChaotian Jing (opcode == SD_APP_SD_STATUS && mmc_cmd_type(cmd) == MMC_CMD_ADTC) || 105020848903SChaotian Jing (opcode == MMC_SEND_EXT_CSD && mmc_cmd_type(cmd) == MMC_CMD_ADTC)) 10514fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(11); 105220848903SChaotian Jing 105320848903SChaotian Jing if (cmd->data) { 105420848903SChaotian Jing struct mmc_data *data = cmd->data; 105520848903SChaotian Jing 105620848903SChaotian Jing if (mmc_op_multi(opcode)) { 10570caf60c4SAmey Narkhede if (mmc_card_mmc(mmc->card) && mrq->sbc && 105820848903SChaotian Jing !(mrq->sbc->arg & 0xFFFF0000)) 10594fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(29); /* AutoCMD23 */ 106020848903SChaotian Jing } 106120848903SChaotian Jing 106220848903SChaotian Jing rawcmd |= ((data->blksz & 0xFFF) << 16); 106320848903SChaotian Jing if (data->flags & MMC_DATA_WRITE) 10644fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(13); 106520848903SChaotian Jing if (data->blocks > 1) 10664fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(12); 106720848903SChaotian Jing else 10684fe54318SAngeloGioacchino Del Regno rawcmd |= BIT(11); 106920848903SChaotian Jing /* Always use dma mode */ 107020848903SChaotian Jing sdr_clr_bits(host->base + MSDC_CFG, MSDC_CFG_PIO); 107120848903SChaotian Jing 107220848903SChaotian Jing if (host->timeout_ns != data->timeout_ns || 107320848903SChaotian Jing host->timeout_clks != data->timeout_clks) 107420848903SChaotian Jing msdc_set_timeout(host, data->timeout_ns, 107520848903SChaotian Jing data->timeout_clks); 107620848903SChaotian Jing 107720848903SChaotian Jing writel(data->blocks, host->base + SDC_BLK_NUM); 107820848903SChaotian Jing } 107920848903SChaotian Jing return rawcmd; 108020848903SChaotian Jing } 108120848903SChaotian Jing 1082d74179b8SChanWoo Lee static void msdc_start_data(struct msdc_host *host, struct mmc_command *cmd, 1083d74179b8SChanWoo Lee struct mmc_data *data) 108420848903SChaotian Jing { 108520848903SChaotian Jing bool read; 108620848903SChaotian Jing 108720848903SChaotian Jing WARN_ON(host->data); 108820848903SChaotian Jing host->data = data; 108920848903SChaotian Jing read = data->flags & MMC_DATA_READ; 109020848903SChaotian Jing 109120848903SChaotian Jing mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); 109220848903SChaotian Jing msdc_dma_setup(host, &host->dma, data); 109320848903SChaotian Jing sdr_set_bits(host->base + MSDC_INTEN, data_ints_mask); 109420848903SChaotian Jing sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_START, 1); 109520848903SChaotian Jing dev_dbg(host->dev, "DMA start\n"); 109620848903SChaotian Jing dev_dbg(host->dev, "%s: cmd=%d DMA data: %d blocks; read=%d\n", 109720848903SChaotian Jing __func__, cmd->opcode, data->blocks, read); 109820848903SChaotian Jing } 109920848903SChaotian Jing 110020848903SChaotian Jing static int msdc_auto_cmd_done(struct msdc_host *host, int events, 110120848903SChaotian Jing struct mmc_command *cmd) 110220848903SChaotian Jing { 110320848903SChaotian Jing u32 *rsp = cmd->resp; 110420848903SChaotian Jing 110520848903SChaotian Jing rsp[0] = readl(host->base + SDC_ACMD_RESP); 110620848903SChaotian Jing 110720848903SChaotian Jing if (events & MSDC_INT_ACMDRDY) { 110820848903SChaotian Jing cmd->error = 0; 110920848903SChaotian Jing } else { 111020848903SChaotian Jing msdc_reset_hw(host); 111120848903SChaotian Jing if (events & MSDC_INT_ACMDCRCERR) { 111220848903SChaotian Jing cmd->error = -EILSEQ; 111320848903SChaotian Jing host->error |= REQ_STOP_EIO; 111420848903SChaotian Jing } else if (events & MSDC_INT_ACMDTMO) { 111520848903SChaotian Jing cmd->error = -ETIMEDOUT; 111620848903SChaotian Jing host->error |= REQ_STOP_TMO; 111720848903SChaotian Jing } 111820848903SChaotian Jing dev_err(host->dev, 111920848903SChaotian Jing "%s: AUTO_CMD%d arg=%08X; rsp %08X; cmd_error=%d\n", 112020848903SChaotian Jing __func__, cmd->opcode, cmd->arg, rsp[0], cmd->error); 112120848903SChaotian Jing } 112220848903SChaotian Jing return cmd->error; 112320848903SChaotian Jing } 112420848903SChaotian Jing 11256ec5a7b7SLee Jones /* 11269e2582e5Syong mao * msdc_recheck_sdio_irq - recheck whether the SDIO irq is lost 11279e2582e5Syong mao * 11289e2582e5Syong mao * Host controller may lost interrupt in some special case. 11299e2582e5Syong mao * Add SDIO irq recheck mechanism to make sure all interrupts 11309e2582e5Syong mao * can be processed immediately 11319e2582e5Syong mao */ 11329e2582e5Syong mao static void msdc_recheck_sdio_irq(struct msdc_host *host) 11339e2582e5Syong mao { 11340caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 11359e2582e5Syong mao u32 reg_int, reg_inten, reg_ps; 11369e2582e5Syong mao 11370caf60c4SAmey Narkhede if (mmc->caps & MMC_CAP_SDIO_IRQ) { 11389e2582e5Syong mao reg_inten = readl(host->base + MSDC_INTEN); 11399e2582e5Syong mao if (reg_inten & MSDC_INTEN_SDIOIRQ) { 11409e2582e5Syong mao reg_int = readl(host->base + MSDC_INT); 11419e2582e5Syong mao reg_ps = readl(host->base + MSDC_PS); 11429e2582e5Syong mao if (!(reg_int & MSDC_INT_SDIOIRQ || 11439e2582e5Syong mao reg_ps & MSDC_PS_DATA1)) { 11449e2582e5Syong mao __msdc_enable_sdio_irq(host, 0); 11450caf60c4SAmey Narkhede sdio_signal_irq(mmc); 11469e2582e5Syong mao } 11479e2582e5Syong mao } 11489e2582e5Syong mao } 11499e2582e5Syong mao } 11509e2582e5Syong mao 1151d74179b8SChanWoo Lee static void msdc_track_cmd_data(struct msdc_host *host, struct mmc_command *cmd) 115220848903SChaotian Jing { 115320848903SChaotian Jing if (host->error) 115420848903SChaotian Jing dev_dbg(host->dev, "%s: cmd=%d arg=%08X; host->error=0x%08X\n", 115520848903SChaotian Jing __func__, cmd->opcode, cmd->arg, host->error); 115620848903SChaotian Jing } 115720848903SChaotian Jing 115820848903SChaotian Jing static void msdc_request_done(struct msdc_host *host, struct mmc_request *mrq) 115920848903SChaotian Jing { 116020848903SChaotian Jing unsigned long flags; 116120848903SChaotian Jing 11620354ca6eSChaotian Jing /* 11630354ca6eSChaotian Jing * No need check the return value of cancel_delayed_work, as only ONE 11640354ca6eSChaotian Jing * path will go here! 11650354ca6eSChaotian Jing */ 11660354ca6eSChaotian Jing cancel_delayed_work(&host->req_timeout); 11670354ca6eSChaotian Jing 116820848903SChaotian Jing spin_lock_irqsave(&host->lock, flags); 116920848903SChaotian Jing host->mrq = NULL; 117020848903SChaotian Jing spin_unlock_irqrestore(&host->lock, flags); 117120848903SChaotian Jing 1172d74179b8SChanWoo Lee msdc_track_cmd_data(host, mrq->cmd); 117320848903SChaotian Jing if (mrq->data) 117415107135SYue Hu msdc_unprepare_data(host, mrq->data); 117520314ce3Sjjian zhou if (host->error) 117620314ce3Sjjian zhou msdc_reset_hw(host); 11770caf60c4SAmey Narkhede mmc_request_done(mmc_from_priv(host), mrq); 11789e2582e5Syong mao if (host->dev_comp->recheck_sdio_irq) 11799e2582e5Syong mao msdc_recheck_sdio_irq(host); 118020848903SChaotian Jing } 118120848903SChaotian Jing 118220848903SChaotian Jing /* returns true if command is fully handled; returns false otherwise */ 118320848903SChaotian Jing static bool msdc_cmd_done(struct msdc_host *host, int events, 118420848903SChaotian Jing struct mmc_request *mrq, struct mmc_command *cmd) 118520848903SChaotian Jing { 118620848903SChaotian Jing bool done = false; 118720848903SChaotian Jing bool sbc_error; 118820848903SChaotian Jing unsigned long flags; 11890354ca6eSChaotian Jing u32 *rsp; 119020848903SChaotian Jing 119120848903SChaotian Jing if (mrq->sbc && cmd == mrq->cmd && 119220848903SChaotian Jing (events & (MSDC_INT_ACMDRDY | MSDC_INT_ACMDCRCERR 119320848903SChaotian Jing | MSDC_INT_ACMDTMO))) 119420848903SChaotian Jing msdc_auto_cmd_done(host, events, mrq->sbc); 119520848903SChaotian Jing 119620848903SChaotian Jing sbc_error = mrq->sbc && mrq->sbc->error; 119720848903SChaotian Jing 119820848903SChaotian Jing if (!sbc_error && !(events & (MSDC_INT_CMDRDY 119920848903SChaotian Jing | MSDC_INT_RSPCRCERR 120020848903SChaotian Jing | MSDC_INT_CMDTMO))) 120120848903SChaotian Jing return done; 120220848903SChaotian Jing 120320848903SChaotian Jing spin_lock_irqsave(&host->lock, flags); 120420848903SChaotian Jing done = !host->cmd; 120520848903SChaotian Jing host->cmd = NULL; 120620848903SChaotian Jing spin_unlock_irqrestore(&host->lock, flags); 120720848903SChaotian Jing 120820848903SChaotian Jing if (done) 120920848903SChaotian Jing return true; 12100354ca6eSChaotian Jing rsp = cmd->resp; 121120848903SChaotian Jing 1212726a9aacSChaotian Jing sdr_clr_bits(host->base + MSDC_INTEN, cmd_ints_mask); 121320848903SChaotian Jing 121420848903SChaotian Jing if (cmd->flags & MMC_RSP_PRESENT) { 121520848903SChaotian Jing if (cmd->flags & MMC_RSP_136) { 121620848903SChaotian Jing rsp[0] = readl(host->base + SDC_RESP3); 121720848903SChaotian Jing rsp[1] = readl(host->base + SDC_RESP2); 121820848903SChaotian Jing rsp[2] = readl(host->base + SDC_RESP1); 121920848903SChaotian Jing rsp[3] = readl(host->base + SDC_RESP0); 122020848903SChaotian Jing } else { 122120848903SChaotian Jing rsp[0] = readl(host->base + SDC_RESP0); 122220848903SChaotian Jing } 122320848903SChaotian Jing } 122420848903SChaotian Jing 122520848903SChaotian Jing if (!sbc_error && !(events & MSDC_INT_CMDRDY)) { 1226da6e0f70SChaotian Jing if (events & MSDC_INT_CMDTMO || 1227da6e0f70SChaotian Jing (cmd->opcode != MMC_SEND_TUNING_BLOCK && 1228c4ac38c6SWenbin Mei cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200 && 1229c4ac38c6SWenbin Mei !host->hs400_tuning)) 1230ddc71387SChaotian Jing /* 1231ddc71387SChaotian Jing * should not clear fifo/interrupt as the tune data 1232*4b323f02SYu Zhe * may have already come when cmd19/cmd21 gets response 1233da6e0f70SChaotian Jing * CRC error. 1234ddc71387SChaotian Jing */ 123520848903SChaotian Jing msdc_reset_hw(host); 123620848903SChaotian Jing if (events & MSDC_INT_RSPCRCERR) { 123720848903SChaotian Jing cmd->error = -EILSEQ; 123820848903SChaotian Jing host->error |= REQ_CMD_EIO; 123920848903SChaotian Jing } else if (events & MSDC_INT_CMDTMO) { 124020848903SChaotian Jing cmd->error = -ETIMEDOUT; 124120848903SChaotian Jing host->error |= REQ_CMD_TMO; 124220848903SChaotian Jing } 124320848903SChaotian Jing } 124420848903SChaotian Jing if (cmd->error) 124520848903SChaotian Jing dev_dbg(host->dev, 124620848903SChaotian Jing "%s: cmd=%d arg=%08X; rsp %08X; cmd_error=%d\n", 124720848903SChaotian Jing __func__, cmd->opcode, cmd->arg, rsp[0], 124820848903SChaotian Jing cmd->error); 124920848903SChaotian Jing 125020848903SChaotian Jing msdc_cmd_next(host, mrq, cmd); 125120848903SChaotian Jing return true; 125220848903SChaotian Jing } 125320848903SChaotian Jing 125420848903SChaotian Jing /* It is the core layer's responsibility to ensure card status 125520848903SChaotian Jing * is correct before issue a request. but host design do below 125620848903SChaotian Jing * checks recommended. 125720848903SChaotian Jing */ 125820848903SChaotian Jing static inline bool msdc_cmd_is_ready(struct msdc_host *host, 125920848903SChaotian Jing struct mmc_request *mrq, struct mmc_command *cmd) 126020848903SChaotian Jing { 1261ffaea6ebSAngeloGioacchino Del Regno u32 val; 1262ffaea6ebSAngeloGioacchino Del Regno int ret; 126320848903SChaotian Jing 1264ffaea6ebSAngeloGioacchino Del Regno /* The max busy time we can endure is 20ms */ 1265ffaea6ebSAngeloGioacchino Del Regno ret = readl_poll_timeout_atomic(host->base + SDC_STS, val, 1266ffaea6ebSAngeloGioacchino Del Regno !(val & SDC_STS_CMDBUSY), 1, 20000); 1267ffaea6ebSAngeloGioacchino Del Regno if (ret) { 126820848903SChaotian Jing dev_err(host->dev, "CMD bus busy detected\n"); 126920848903SChaotian Jing host->error |= REQ_CMD_BUSY; 127020848903SChaotian Jing msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); 127120848903SChaotian Jing return false; 127220848903SChaotian Jing } 127320848903SChaotian Jing 127420848903SChaotian Jing if (mmc_resp_type(cmd) == MMC_RSP_R1B || cmd->data) { 127520848903SChaotian Jing /* R1B or with data, should check SDCBUSY */ 1276ffaea6ebSAngeloGioacchino Del Regno ret = readl_poll_timeout_atomic(host->base + SDC_STS, val, 1277ffaea6ebSAngeloGioacchino Del Regno !(val & SDC_STS_SDCBUSY), 1, 20000); 1278ffaea6ebSAngeloGioacchino Del Regno if (ret) { 127920848903SChaotian Jing dev_err(host->dev, "Controller busy detected\n"); 128020848903SChaotian Jing host->error |= REQ_CMD_BUSY; 128120848903SChaotian Jing msdc_cmd_done(host, MSDC_INT_CMDTMO, mrq, cmd); 128220848903SChaotian Jing return false; 128320848903SChaotian Jing } 128420848903SChaotian Jing } 128520848903SChaotian Jing return true; 128620848903SChaotian Jing } 128720848903SChaotian Jing 128820848903SChaotian Jing static void msdc_start_command(struct msdc_host *host, 128920848903SChaotian Jing struct mmc_request *mrq, struct mmc_command *cmd) 129020848903SChaotian Jing { 129120848903SChaotian Jing u32 rawcmd; 12925215b2e9Sjjian zhou unsigned long flags; 129320848903SChaotian Jing 129420848903SChaotian Jing WARN_ON(host->cmd); 129520848903SChaotian Jing host->cmd = cmd; 129620848903SChaotian Jing 1297f38a9774SChaotian Jing mod_delayed_work(system_wq, &host->req_timeout, DAT_TIMEOUT); 129820848903SChaotian Jing if (!msdc_cmd_is_ready(host, mrq, cmd)) 129920848903SChaotian Jing return; 130020848903SChaotian Jing 130120848903SChaotian Jing if ((readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_TXCNT) >> 16 || 130220848903SChaotian Jing readl(host->base + MSDC_FIFOCS) & MSDC_FIFOCS_RXCNT) { 130320848903SChaotian Jing dev_err(host->dev, "TX/RX FIFO non-empty before start of IO. Reset\n"); 130420848903SChaotian Jing msdc_reset_hw(host); 130520848903SChaotian Jing } 130620848903SChaotian Jing 130720848903SChaotian Jing cmd->error = 0; 130820848903SChaotian Jing rawcmd = msdc_cmd_prepare_raw_cmd(host, mrq, cmd); 130920848903SChaotian Jing 13105215b2e9Sjjian zhou spin_lock_irqsave(&host->lock, flags); 1311726a9aacSChaotian Jing sdr_set_bits(host->base + MSDC_INTEN, cmd_ints_mask); 13125215b2e9Sjjian zhou spin_unlock_irqrestore(&host->lock, flags); 13135215b2e9Sjjian zhou 131420848903SChaotian Jing writel(cmd->arg, host->base + SDC_ARG); 131520848903SChaotian Jing writel(rawcmd, host->base + SDC_CMD); 131620848903SChaotian Jing } 131720848903SChaotian Jing 131820848903SChaotian Jing static void msdc_cmd_next(struct msdc_host *host, 131920848903SChaotian Jing struct mmc_request *mrq, struct mmc_command *cmd) 132020848903SChaotian Jing { 1321ddc71387SChaotian Jing if ((cmd->error && 1322ddc71387SChaotian Jing !(cmd->error == -EILSEQ && 1323ddc71387SChaotian Jing (cmd->opcode == MMC_SEND_TUNING_BLOCK || 1324c4ac38c6SWenbin Mei cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200 || 1325c4ac38c6SWenbin Mei host->hs400_tuning))) || 1326ddc71387SChaotian Jing (mrq->sbc && mrq->sbc->error)) 132720848903SChaotian Jing msdc_request_done(host, mrq); 132820848903SChaotian Jing else if (cmd == mrq->sbc) 132920848903SChaotian Jing msdc_start_command(host, mrq, mrq->cmd); 133020848903SChaotian Jing else if (!cmd->data) 133120848903SChaotian Jing msdc_request_done(host, mrq); 133220848903SChaotian Jing else 1333d74179b8SChanWoo Lee msdc_start_data(host, cmd, cmd->data); 133420848903SChaotian Jing } 133520848903SChaotian Jing 133620848903SChaotian Jing static void msdc_ops_request(struct mmc_host *mmc, struct mmc_request *mrq) 133720848903SChaotian Jing { 133820848903SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 133920848903SChaotian Jing 134020848903SChaotian Jing host->error = 0; 134120848903SChaotian Jing WARN_ON(host->mrq); 134220848903SChaotian Jing host->mrq = mrq; 134320848903SChaotian Jing 134420848903SChaotian Jing if (mrq->data) 134515107135SYue Hu msdc_prepare_data(host, mrq->data); 134620848903SChaotian Jing 134720848903SChaotian Jing /* if SBC is required, we have HW option and SW option. 134820848903SChaotian Jing * if HW option is enabled, and SBC does not have "special" flags, 134920848903SChaotian Jing * use HW option, otherwise use SW option 135020848903SChaotian Jing */ 135120848903SChaotian Jing if (mrq->sbc && (!mmc_card_mmc(mmc->card) || 135220848903SChaotian Jing (mrq->sbc->arg & 0xFFFF0000))) 135320848903SChaotian Jing msdc_start_command(host, mrq, mrq->sbc); 135420848903SChaotian Jing else 135520848903SChaotian Jing msdc_start_command(host, mrq, mrq->cmd); 135620848903SChaotian Jing } 135720848903SChaotian Jing 1358d3c6aac3SLinus Walleij static void msdc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq) 135920848903SChaotian Jing { 136020848903SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 136120848903SChaotian Jing struct mmc_data *data = mrq->data; 136220848903SChaotian Jing 136320848903SChaotian Jing if (!data) 136420848903SChaotian Jing return; 136520848903SChaotian Jing 136615107135SYue Hu msdc_prepare_data(host, data); 136720848903SChaotian Jing data->host_cookie |= MSDC_ASYNC_FLAG; 136820848903SChaotian Jing } 136920848903SChaotian Jing 137020848903SChaotian Jing static void msdc_post_req(struct mmc_host *mmc, struct mmc_request *mrq, 137120848903SChaotian Jing int err) 137220848903SChaotian Jing { 137320848903SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 137415107135SYue Hu struct mmc_data *data = mrq->data; 137520848903SChaotian Jing 137620848903SChaotian Jing if (!data) 137720848903SChaotian Jing return; 137815107135SYue Hu 137920848903SChaotian Jing if (data->host_cookie) { 138020848903SChaotian Jing data->host_cookie &= ~MSDC_ASYNC_FLAG; 138115107135SYue Hu msdc_unprepare_data(host, data); 138220848903SChaotian Jing } 138320848903SChaotian Jing } 138420848903SChaotian Jing 1385f0ed43edSYue Hu static void msdc_data_xfer_next(struct msdc_host *host, struct mmc_request *mrq) 138620848903SChaotian Jing { 138720848903SChaotian Jing if (mmc_op_multi(mrq->cmd->opcode) && mrq->stop && !mrq->stop->error && 13886397b7f5SChaotian Jing !mrq->sbc) 138920848903SChaotian Jing msdc_start_command(host, mrq, mrq->stop); 139020848903SChaotian Jing else 139120848903SChaotian Jing msdc_request_done(host, mrq); 139220848903SChaotian Jing } 139320848903SChaotian Jing 139489bcd9a6SMengqi Zhang static void msdc_data_xfer_done(struct msdc_host *host, u32 events, 139520848903SChaotian Jing struct mmc_request *mrq, struct mmc_data *data) 139620848903SChaotian Jing { 13970354ca6eSChaotian Jing struct mmc_command *stop; 139820848903SChaotian Jing unsigned long flags; 139920848903SChaotian Jing bool done; 140020848903SChaotian Jing unsigned int check_data = events & 140120848903SChaotian Jing (MSDC_INT_XFER_COMPL | MSDC_INT_DATCRCERR | MSDC_INT_DATTMO 140220848903SChaotian Jing | MSDC_INT_DMA_BDCSERR | MSDC_INT_DMA_GPDCSERR 140320848903SChaotian Jing | MSDC_INT_DMA_PROTECT); 1404ffaea6ebSAngeloGioacchino Del Regno u32 val; 1405ffaea6ebSAngeloGioacchino Del Regno int ret; 140620848903SChaotian Jing 140720848903SChaotian Jing spin_lock_irqsave(&host->lock, flags); 140820848903SChaotian Jing done = !host->data; 140920848903SChaotian Jing if (check_data) 141020848903SChaotian Jing host->data = NULL; 141120848903SChaotian Jing spin_unlock_irqrestore(&host->lock, flags); 141220848903SChaotian Jing 141320848903SChaotian Jing if (done) 141489bcd9a6SMengqi Zhang return; 14150354ca6eSChaotian Jing stop = data->stop; 141620848903SChaotian Jing 141720848903SChaotian Jing if (check_data || (stop && stop->error)) { 141820848903SChaotian Jing dev_dbg(host->dev, "DMA status: 0x%8X\n", 141920848903SChaotian Jing readl(host->base + MSDC_DMA_CFG)); 142020848903SChaotian Jing sdr_set_field(host->base + MSDC_DMA_CTRL, MSDC_DMA_CTRL_STOP, 142120848903SChaotian Jing 1); 1422ffaea6ebSAngeloGioacchino Del Regno 142389bcd9a6SMengqi Zhang ret = readl_poll_timeout_atomic(host->base + MSDC_DMA_CTRL, val, 142489bcd9a6SMengqi Zhang !(val & MSDC_DMA_CTRL_STOP), 1, 20000); 142589bcd9a6SMengqi Zhang if (ret) 142689bcd9a6SMengqi Zhang dev_dbg(host->dev, "DMA stop timed out\n"); 142789bcd9a6SMengqi Zhang 1428ffaea6ebSAngeloGioacchino Del Regno ret = readl_poll_timeout_atomic(host->base + MSDC_DMA_CFG, val, 1429ffaea6ebSAngeloGioacchino Del Regno !(val & MSDC_DMA_CFG_STS), 1, 20000); 143089bcd9a6SMengqi Zhang if (ret) 143189bcd9a6SMengqi Zhang dev_dbg(host->dev, "DMA inactive timed out\n"); 1432ffaea6ebSAngeloGioacchino Del Regno 143320848903SChaotian Jing sdr_clr_bits(host->base + MSDC_INTEN, data_ints_mask); 143420848903SChaotian Jing dev_dbg(host->dev, "DMA stop\n"); 143520848903SChaotian Jing 143620848903SChaotian Jing if ((events & MSDC_INT_XFER_COMPL) && (!stop || !stop->error)) { 143720848903SChaotian Jing data->bytes_xfered = data->blocks * data->blksz; 143820848903SChaotian Jing } else { 14392066fd28SChaotian Jing dev_dbg(host->dev, "interrupt events: %x\n", events); 144020848903SChaotian Jing msdc_reset_hw(host); 144120848903SChaotian Jing host->error |= REQ_DAT_ERR; 144220848903SChaotian Jing data->bytes_xfered = 0; 144320848903SChaotian Jing 144420848903SChaotian Jing if (events & MSDC_INT_DATTMO) 144520848903SChaotian Jing data->error = -ETIMEDOUT; 14466397b7f5SChaotian Jing else if (events & MSDC_INT_DATCRCERR) 14476397b7f5SChaotian Jing data->error = -EILSEQ; 144820848903SChaotian Jing 14492066fd28SChaotian Jing dev_dbg(host->dev, "%s: cmd=%d; blocks=%d", 145020848903SChaotian Jing __func__, mrq->cmd->opcode, data->blocks); 14512066fd28SChaotian Jing dev_dbg(host->dev, "data_error=%d xfer_size=%d\n", 145220848903SChaotian Jing (int)data->error, data->bytes_xfered); 145320848903SChaotian Jing } 145420848903SChaotian Jing 1455f0ed43edSYue Hu msdc_data_xfer_next(host, mrq); 145620848903SChaotian Jing } 145720848903SChaotian Jing } 145820848903SChaotian Jing 145920848903SChaotian Jing static void msdc_set_buswidth(struct msdc_host *host, u32 width) 146020848903SChaotian Jing { 146120848903SChaotian Jing u32 val = readl(host->base + SDC_CFG); 146220848903SChaotian Jing 146320848903SChaotian Jing val &= ~SDC_CFG_BUSWIDTH; 146420848903SChaotian Jing 146520848903SChaotian Jing switch (width) { 146620848903SChaotian Jing default: 146720848903SChaotian Jing case MMC_BUS_WIDTH_1: 146820848903SChaotian Jing val |= (MSDC_BUS_1BITS << 16); 146920848903SChaotian Jing break; 147020848903SChaotian Jing case MMC_BUS_WIDTH_4: 147120848903SChaotian Jing val |= (MSDC_BUS_4BITS << 16); 147220848903SChaotian Jing break; 147320848903SChaotian Jing case MMC_BUS_WIDTH_8: 147420848903SChaotian Jing val |= (MSDC_BUS_8BITS << 16); 147520848903SChaotian Jing break; 147620848903SChaotian Jing } 147720848903SChaotian Jing 147820848903SChaotian Jing writel(val, host->base + SDC_CFG); 147920848903SChaotian Jing dev_dbg(host->dev, "Bus Width = %d", width); 148020848903SChaotian Jing } 148120848903SChaotian Jing 148220848903SChaotian Jing static int msdc_ops_switch_volt(struct mmc_host *mmc, struct mmc_ios *ios) 148320848903SChaotian Jing { 148420848903SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 14859cbe0fc8SMarek Vasut int ret; 148620848903SChaotian Jing 148720848903SChaotian Jing if (!IS_ERR(mmc->supply.vqmmc)) { 1488fac49ce5SNicolas Boichat if (ios->signal_voltage != MMC_SIGNAL_VOLTAGE_330 && 1489fac49ce5SNicolas Boichat ios->signal_voltage != MMC_SIGNAL_VOLTAGE_180) { 149020848903SChaotian Jing dev_err(host->dev, "Unsupported signal voltage!\n"); 149120848903SChaotian Jing return -EINVAL; 149220848903SChaotian Jing } 149320848903SChaotian Jing 1494fac49ce5SNicolas Boichat ret = mmc_regulator_set_vqmmc(mmc, ios); 14959cbe0fc8SMarek Vasut if (ret < 0) { 1496fac49ce5SNicolas Boichat dev_dbg(host->dev, "Regulator set error %d (%d)\n", 1497fac49ce5SNicolas Boichat ret, ios->signal_voltage); 14989cbe0fc8SMarek Vasut return ret; 14999cbe0fc8SMarek Vasut } 15009cbe0fc8SMarek Vasut 150120848903SChaotian Jing /* Apply different pinctrl settings for different signal voltage */ 150220848903SChaotian Jing if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_180) 150320848903SChaotian Jing pinctrl_select_state(host->pinctrl, host->pins_uhs); 150420848903SChaotian Jing else 150520848903SChaotian Jing pinctrl_select_state(host->pinctrl, host->pins_default); 150620848903SChaotian Jing } 15079cbe0fc8SMarek Vasut return 0; 150820848903SChaotian Jing } 150920848903SChaotian Jing 151020848903SChaotian Jing static int msdc_card_busy(struct mmc_host *mmc) 151120848903SChaotian Jing { 151220848903SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 151320848903SChaotian Jing u32 status = readl(host->base + MSDC_PS); 151420848903SChaotian Jing 15153bc702edSyong mao /* only check if data0 is low */ 15163bc702edSyong mao return !(status & BIT(16)); 151720848903SChaotian Jing } 151820848903SChaotian Jing 151920848903SChaotian Jing static void msdc_request_timeout(struct work_struct *work) 152020848903SChaotian Jing { 152120848903SChaotian Jing struct msdc_host *host = container_of(work, struct msdc_host, 152220848903SChaotian Jing req_timeout.work); 152320848903SChaotian Jing 152420848903SChaotian Jing /* simulate HW timeout status */ 152520848903SChaotian Jing dev_err(host->dev, "%s: aborting cmd/data/mrq\n", __func__); 152620848903SChaotian Jing if (host->mrq) { 152720848903SChaotian Jing dev_err(host->dev, "%s: aborting mrq=%p cmd=%d\n", __func__, 152820848903SChaotian Jing host->mrq, host->mrq->cmd->opcode); 152920848903SChaotian Jing if (host->cmd) { 153020848903SChaotian Jing dev_err(host->dev, "%s: aborting cmd=%d\n", 153120848903SChaotian Jing __func__, host->cmd->opcode); 153220848903SChaotian Jing msdc_cmd_done(host, MSDC_INT_CMDTMO, host->mrq, 153320848903SChaotian Jing host->cmd); 153420848903SChaotian Jing } else if (host->data) { 153520848903SChaotian Jing dev_err(host->dev, "%s: abort data: cmd%d; %d blocks\n", 153620848903SChaotian Jing __func__, host->mrq->cmd->opcode, 153720848903SChaotian Jing host->data->blocks); 153820848903SChaotian Jing msdc_data_xfer_done(host, MSDC_INT_DATTMO, host->mrq, 153920848903SChaotian Jing host->data); 154020848903SChaotian Jing } 154120848903SChaotian Jing } 154220848903SChaotian Jing } 154320848903SChaotian Jing 15448a5df8acSjjian zhou static void __msdc_enable_sdio_irq(struct msdc_host *host, int enb) 15458a5df8acSjjian zhou { 15468a5df8acSjjian zhou if (enb) { 15478a5df8acSjjian zhou sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); 15488a5df8acSjjian zhou sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); 15499e2582e5Syong mao if (host->dev_comp->recheck_sdio_irq) 15509e2582e5Syong mao msdc_recheck_sdio_irq(host); 15518a5df8acSjjian zhou } else { 15528a5df8acSjjian zhou sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); 15538a5df8acSjjian zhou sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); 15548a5df8acSjjian zhou } 15558a5df8acSjjian zhou } 15568a5df8acSjjian zhou 15578a5df8acSjjian zhou static void msdc_enable_sdio_irq(struct mmc_host *mmc, int enb) 15585215b2e9Sjjian zhou { 15595215b2e9Sjjian zhou struct msdc_host *host = mmc_priv(mmc); 1560527f36f5SAxe Yang unsigned long flags; 1561527f36f5SAxe Yang int ret; 15625215b2e9Sjjian zhou 15635215b2e9Sjjian zhou spin_lock_irqsave(&host->lock, flags); 15648a5df8acSjjian zhou __msdc_enable_sdio_irq(host, enb); 15655215b2e9Sjjian zhou spin_unlock_irqrestore(&host->lock, flags); 15665215b2e9Sjjian zhou 1567527f36f5SAxe Yang if (mmc_card_enable_async_irq(mmc->card) && host->pins_eint) { 1568527f36f5SAxe Yang if (enb) { 1569527f36f5SAxe Yang /* 1570527f36f5SAxe Yang * In dev_pm_set_dedicated_wake_irq_reverse(), eint pin will be set to 1571527f36f5SAxe Yang * GPIO mode. We need to restore it to SDIO DAT1 mode after that. 1572527f36f5SAxe Yang * Since the current pinstate is pins_uhs, to ensure pinctrl select take 1573527f36f5SAxe Yang * affect successfully, we change the pinstate to pins_eint firstly. 1574527f36f5SAxe Yang */ 1575527f36f5SAxe Yang pinctrl_select_state(host->pinctrl, host->pins_eint); 1576527f36f5SAxe Yang ret = dev_pm_set_dedicated_wake_irq_reverse(host->dev, host->eint_irq); 1577527f36f5SAxe Yang 1578527f36f5SAxe Yang if (ret) { 1579527f36f5SAxe Yang dev_err(host->dev, "Failed to register SDIO wakeup irq!\n"); 1580527f36f5SAxe Yang host->pins_eint = NULL; 15815215b2e9Sjjian zhou pm_runtime_get_noresume(host->dev); 1582527f36f5SAxe Yang } else { 1583527f36f5SAxe Yang dev_dbg(host->dev, "SDIO eint irq: %d!\n", host->eint_irq); 1584527f36f5SAxe Yang } 1585527f36f5SAxe Yang 1586527f36f5SAxe Yang pinctrl_select_state(host->pinctrl, host->pins_uhs); 1587527f36f5SAxe Yang } else { 1588527f36f5SAxe Yang dev_pm_clear_wake_irq(host->dev); 1589527f36f5SAxe Yang } 1590527f36f5SAxe Yang } else { 1591527f36f5SAxe Yang if (enb) { 1592527f36f5SAxe Yang /* Ensure host->pins_eint is NULL */ 1593527f36f5SAxe Yang host->pins_eint = NULL; 1594527f36f5SAxe Yang pm_runtime_get_noresume(host->dev); 1595527f36f5SAxe Yang } else { 15965215b2e9Sjjian zhou pm_runtime_put_noidle(host->dev); 15975215b2e9Sjjian zhou } 1598527f36f5SAxe Yang } 1599527f36f5SAxe Yang } 16005215b2e9Sjjian zhou 160188bd652bSChun-Hung Wu static irqreturn_t msdc_cmdq_irq(struct msdc_host *host, u32 intsts) 160288bd652bSChun-Hung Wu { 16030caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 160488bd652bSChun-Hung Wu int cmd_err = 0, dat_err = 0; 160588bd652bSChun-Hung Wu 160688bd652bSChun-Hung Wu if (intsts & MSDC_INT_RSPCRCERR) { 160788bd652bSChun-Hung Wu cmd_err = -EILSEQ; 160888bd652bSChun-Hung Wu dev_err(host->dev, "%s: CMD CRC ERR", __func__); 160988bd652bSChun-Hung Wu } else if (intsts & MSDC_INT_CMDTMO) { 161088bd652bSChun-Hung Wu cmd_err = -ETIMEDOUT; 161188bd652bSChun-Hung Wu dev_err(host->dev, "%s: CMD TIMEOUT ERR", __func__); 161288bd652bSChun-Hung Wu } 161388bd652bSChun-Hung Wu 161488bd652bSChun-Hung Wu if (intsts & MSDC_INT_DATCRCERR) { 161588bd652bSChun-Hung Wu dat_err = -EILSEQ; 161688bd652bSChun-Hung Wu dev_err(host->dev, "%s: DATA CRC ERR", __func__); 161788bd652bSChun-Hung Wu } else if (intsts & MSDC_INT_DATTMO) { 161888bd652bSChun-Hung Wu dat_err = -ETIMEDOUT; 161988bd652bSChun-Hung Wu dev_err(host->dev, "%s: DATA TIMEOUT ERR", __func__); 162088bd652bSChun-Hung Wu } 162188bd652bSChun-Hung Wu 162288bd652bSChun-Hung Wu if (cmd_err || dat_err) { 162388bd652bSChun-Hung Wu dev_err(host->dev, "cmd_err = %d, dat_err =%d, intsts = 0x%x", 162488bd652bSChun-Hung Wu cmd_err, dat_err, intsts); 162588bd652bSChun-Hung Wu } 162688bd652bSChun-Hung Wu 16270caf60c4SAmey Narkhede return cqhci_irq(mmc, 0, cmd_err, dat_err); 162888bd652bSChun-Hung Wu } 162988bd652bSChun-Hung Wu 163020848903SChaotian Jing static irqreturn_t msdc_irq(int irq, void *dev_id) 163120848903SChaotian Jing { 163220848903SChaotian Jing struct msdc_host *host = (struct msdc_host *) dev_id; 16330caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 163420848903SChaotian Jing 163520848903SChaotian Jing while (true) { 163620848903SChaotian Jing struct mmc_request *mrq; 163720848903SChaotian Jing struct mmc_command *cmd; 163820848903SChaotian Jing struct mmc_data *data; 163920848903SChaotian Jing u32 events, event_mask; 164020848903SChaotian Jing 16419baf7c5eSTian Tao spin_lock(&host->lock); 164220848903SChaotian Jing events = readl(host->base + MSDC_INT); 164320848903SChaotian Jing event_mask = readl(host->base + MSDC_INTEN); 16448a5df8acSjjian zhou if ((events & event_mask) & MSDC_INT_SDIOIRQ) 16458a5df8acSjjian zhou __msdc_enable_sdio_irq(host, 0); 164620848903SChaotian Jing /* clear interrupts */ 164720848903SChaotian Jing writel(events & event_mask, host->base + MSDC_INT); 164820848903SChaotian Jing 164920848903SChaotian Jing mrq = host->mrq; 165020848903SChaotian Jing cmd = host->cmd; 165120848903SChaotian Jing data = host->data; 16529baf7c5eSTian Tao spin_unlock(&host->lock); 165320848903SChaotian Jing 16548a5df8acSjjian zhou if ((events & event_mask) & MSDC_INT_SDIOIRQ) 16550caf60c4SAmey Narkhede sdio_signal_irq(mmc); 16565215b2e9Sjjian zhou 1657d087bde5SNeilBrown if ((events & event_mask) & MSDC_INT_CDSC) { 1658d087bde5SNeilBrown if (host->internal_cd) 16590caf60c4SAmey Narkhede mmc_detect_change(mmc, msecs_to_jiffies(20)); 1660d087bde5SNeilBrown events &= ~MSDC_INT_CDSC; 1661d087bde5SNeilBrown } 1662d087bde5SNeilBrown 16635215b2e9Sjjian zhou if (!(events & (event_mask & ~MSDC_INT_SDIOIRQ))) 166420848903SChaotian Jing break; 166520848903SChaotian Jing 16660caf60c4SAmey Narkhede if ((mmc->caps2 & MMC_CAP2_CQE) && 166788bd652bSChun-Hung Wu (events & MSDC_INT_CMDQ)) { 166888bd652bSChun-Hung Wu msdc_cmdq_irq(host, events); 166988bd652bSChun-Hung Wu /* clear interrupts */ 167088bd652bSChun-Hung Wu writel(events, host->base + MSDC_INT); 167188bd652bSChun-Hung Wu return IRQ_HANDLED; 167288bd652bSChun-Hung Wu } 167388bd652bSChun-Hung Wu 167420848903SChaotian Jing if (!mrq) { 167520848903SChaotian Jing dev_err(host->dev, 167620848903SChaotian Jing "%s: MRQ=NULL; events=%08X; event_mask=%08X\n", 167720848903SChaotian Jing __func__, events, event_mask); 167820848903SChaotian Jing WARN_ON(1); 167920848903SChaotian Jing break; 168020848903SChaotian Jing } 168120848903SChaotian Jing 168220848903SChaotian Jing dev_dbg(host->dev, "%s: events=%08X\n", __func__, events); 168320848903SChaotian Jing 168420848903SChaotian Jing if (cmd) 168520848903SChaotian Jing msdc_cmd_done(host, events, mrq, cmd); 168620848903SChaotian Jing else if (data) 168720848903SChaotian Jing msdc_data_xfer_done(host, events, mrq, data); 168820848903SChaotian Jing } 168920848903SChaotian Jing 169020848903SChaotian Jing return IRQ_HANDLED; 169120848903SChaotian Jing } 169220848903SChaotian Jing 169320848903SChaotian Jing static void msdc_init_hw(struct msdc_host *host) 169420848903SChaotian Jing { 169520848903SChaotian Jing u32 val; 169639add252SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 169783b27217SAngeloGioacchino Del Regno struct mmc_host *mmc = mmc_from_priv(host); 169820848903SChaotian Jing 1699855d388dSWenbin Mei if (host->reset) { 1700855d388dSWenbin Mei reset_control_assert(host->reset); 1701855d388dSWenbin Mei usleep_range(10, 50); 1702855d388dSWenbin Mei reset_control_deassert(host->reset); 1703855d388dSWenbin Mei } 1704855d388dSWenbin Mei 170520848903SChaotian Jing /* Configure to MMC/SD mode, clock free running */ 170620848903SChaotian Jing sdr_set_bits(host->base + MSDC_CFG, MSDC_CFG_MODE | MSDC_CFG_CKPDN); 170720848903SChaotian Jing 170820848903SChaotian Jing /* Reset */ 170920848903SChaotian Jing msdc_reset_hw(host); 171020848903SChaotian Jing 171120848903SChaotian Jing /* Disable and clear all interrupts */ 171220848903SChaotian Jing writel(0, host->base + MSDC_INTEN); 171320848903SChaotian Jing val = readl(host->base + MSDC_INT); 171420848903SChaotian Jing writel(val, host->base + MSDC_INT); 171520848903SChaotian Jing 1716d087bde5SNeilBrown /* Configure card detection */ 1717d087bde5SNeilBrown if (host->internal_cd) { 1718d087bde5SNeilBrown sdr_set_field(host->base + MSDC_PS, MSDC_PS_CDDEBOUNCE, 1719d087bde5SNeilBrown DEFAULT_DEBOUNCE); 1720d087bde5SNeilBrown sdr_set_bits(host->base + MSDC_PS, MSDC_PS_CDEN); 1721d087bde5SNeilBrown sdr_set_bits(host->base + MSDC_INTEN, MSDC_INTEN_CDSC); 1722d087bde5SNeilBrown sdr_set_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP); 1723d087bde5SNeilBrown } else { 1724d087bde5SNeilBrown sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP); 1725d087bde5SNeilBrown sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN); 1726d087bde5SNeilBrown sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_CDSC); 1727d087bde5SNeilBrown } 1728d087bde5SNeilBrown 1729a2e6d1f6SChaotian Jing if (host->top_base) { 1730a2e6d1f6SChaotian Jing writel(0, host->top_base + EMMC_TOP_CONTROL); 1731a2e6d1f6SChaotian Jing writel(0, host->top_base + EMMC_TOP_CMD); 1732a2e6d1f6SChaotian Jing } else { 173339add252SChaotian Jing writel(0, host->base + tune_reg); 1734a2e6d1f6SChaotian Jing } 173520848903SChaotian Jing writel(0, host->base + MSDC_IOCON); 17366397b7f5SChaotian Jing sdr_set_field(host->base + MSDC_IOCON, MSDC_IOCON_DDLSEL, 0); 17376397b7f5SChaotian Jing writel(0x403c0046, host->base + MSDC_PATCH_BIT); 173820848903SChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_CKGEN_MSDC_DLY_SEL, 1); 17392fea5819SChaotian Jing writel(0xffff4089, host->base + MSDC_PATCH_BIT1); 17406397b7f5SChaotian Jing sdr_set_bits(host->base + EMMC50_CFG0, EMMC50_CFG_CFCSTS_SEL); 1741d9dcbfc8SChaotian Jing 1742d9dcbfc8SChaotian Jing if (host->dev_comp->stop_clk_fix) { 1743d9dcbfc8SChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT1, 1744d9dcbfc8SChaotian Jing MSDC_PATCH_BIT1_STOP_DLY, 3); 1745d9dcbfc8SChaotian Jing sdr_clr_bits(host->base + SDC_FIFO_CFG, 1746d9dcbfc8SChaotian Jing SDC_FIFO_CFG_WRVALIDSEL); 1747d9dcbfc8SChaotian Jing sdr_clr_bits(host->base + SDC_FIFO_CFG, 1748d9dcbfc8SChaotian Jing SDC_FIFO_CFG_RDVALIDSEL); 1749d9dcbfc8SChaotian Jing } 1750d9dcbfc8SChaotian Jing 1751acde28c4SChaotian Jing if (host->dev_comp->busy_check) 17524fe54318SAngeloGioacchino Del Regno sdr_clr_bits(host->base + MSDC_PATCH_BIT1, BIT(7)); 1753d9dcbfc8SChaotian Jing 17542fea5819SChaotian Jing if (host->dev_comp->async_fifo) { 17552fea5819SChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT2, 17562fea5819SChaotian Jing MSDC_PB2_RESPWAIT, 3); 1757d9dcbfc8SChaotian Jing if (host->dev_comp->enhance_rx) { 1758a2e6d1f6SChaotian Jing if (host->top_base) 1759a2e6d1f6SChaotian Jing sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, 1760a2e6d1f6SChaotian Jing SDC_RX_ENH_EN); 1761a2e6d1f6SChaotian Jing else 1762d9dcbfc8SChaotian Jing sdr_set_bits(host->base + SDC_ADV_CFG0, 1763d9dcbfc8SChaotian Jing SDC_RX_ENHANCE_EN); 1764d9dcbfc8SChaotian Jing } else { 17652fea5819SChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT2, 17662fea5819SChaotian Jing MSDC_PB2_RESPSTSENSEL, 2); 17672fea5819SChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT2, 17682fea5819SChaotian Jing MSDC_PB2_CRCSTSENSEL, 2); 1769d9dcbfc8SChaotian Jing } 17702fea5819SChaotian Jing /* use async fifo, then no need tune internal delay */ 17712fea5819SChaotian Jing sdr_clr_bits(host->base + MSDC_PATCH_BIT2, 17722fea5819SChaotian Jing MSDC_PATCH_BIT2_CFGRESP); 17732fea5819SChaotian Jing sdr_set_bits(host->base + MSDC_PATCH_BIT2, 17742fea5819SChaotian Jing MSDC_PATCH_BIT2_CFGCRCSTS); 17752fea5819SChaotian Jing } 17762fea5819SChaotian Jing 17772a9bde19SChaotian Jing if (host->dev_comp->support_64g) 17782a9bde19SChaotian Jing sdr_set_bits(host->base + MSDC_PATCH_BIT2, 17792a9bde19SChaotian Jing MSDC_PB2_SUPPORT_64G); 17802fea5819SChaotian Jing if (host->dev_comp->data_tune) { 1781a2e6d1f6SChaotian Jing if (host->top_base) { 1782a2e6d1f6SChaotian Jing sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, 1783a2e6d1f6SChaotian Jing PAD_DAT_RD_RXDLY_SEL); 1784a2e6d1f6SChaotian Jing sdr_clr_bits(host->top_base + EMMC_TOP_CONTROL, 1785a2e6d1f6SChaotian Jing DATA_K_VALUE_SEL); 1786a2e6d1f6SChaotian Jing sdr_set_bits(host->top_base + EMMC_TOP_CMD, 1787a2e6d1f6SChaotian Jing PAD_CMD_RD_RXDLY_SEL); 1788a2e6d1f6SChaotian Jing } else { 17892fea5819SChaotian Jing sdr_set_bits(host->base + tune_reg, 1790a2e6d1f6SChaotian Jing MSDC_PAD_TUNE_RD_SEL | 1791a2e6d1f6SChaotian Jing MSDC_PAD_TUNE_CMD_SEL); 1792a2e6d1f6SChaotian Jing } 17932fea5819SChaotian Jing } else { 17942fea5819SChaotian Jing /* choose clock tune */ 1795a2e6d1f6SChaotian Jing if (host->top_base) 1796a2e6d1f6SChaotian Jing sdr_set_bits(host->top_base + EMMC_TOP_CONTROL, 1797a2e6d1f6SChaotian Jing PAD_RXDLY_SEL); 1798a2e6d1f6SChaotian Jing else 1799a2e6d1f6SChaotian Jing sdr_set_bits(host->base + tune_reg, 1800a2e6d1f6SChaotian Jing MSDC_PAD_TUNE_RXDLYSEL); 18012fea5819SChaotian Jing } 18026397b7f5SChaotian Jing 180383b27217SAngeloGioacchino Del Regno if (mmc->caps2 & MMC_CAP2_NO_SDIO) { 180483b27217SAngeloGioacchino Del Regno sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIO); 180583b27217SAngeloGioacchino Del Regno sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INTEN_SDIOIRQ); 180683b27217SAngeloGioacchino Del Regno sdr_clr_bits(host->base + SDC_ADV_CFG0, SDC_DAT1_IRQ_TRIGGER); 180783b27217SAngeloGioacchino Del Regno } else { 180883b27217SAngeloGioacchino Del Regno /* Configure to enable SDIO mode, otherwise SDIO CMD5 fails */ 180920848903SChaotian Jing sdr_set_bits(host->base + SDC_CFG, SDC_CFG_SDIO); 181020848903SChaotian Jing 18115215b2e9Sjjian zhou /* Config SDIO device detect interrupt function */ 181220848903SChaotian Jing sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_SDIOIDE); 181326c71a13Syong mao sdr_set_bits(host->base + SDC_ADV_CFG0, SDC_DAT1_IRQ_TRIGGER); 181483b27217SAngeloGioacchino Del Regno } 181520848903SChaotian Jing 181620848903SChaotian Jing /* Configure to default data timeout */ 181720848903SChaotian Jing sdr_set_field(host->base + SDC_CFG, SDC_CFG_DTOC, 3); 181820848903SChaotian Jing 181986beac37SChaotian Jing host->def_tune_para.iocon = readl(host->base + MSDC_IOCON); 18202fea5819SChaotian Jing host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); 1821a2e6d1f6SChaotian Jing if (host->top_base) { 1822a2e6d1f6SChaotian Jing host->def_tune_para.emmc_top_control = 1823a2e6d1f6SChaotian Jing readl(host->top_base + EMMC_TOP_CONTROL); 1824a2e6d1f6SChaotian Jing host->def_tune_para.emmc_top_cmd = 1825a2e6d1f6SChaotian Jing readl(host->top_base + EMMC_TOP_CMD); 1826a2e6d1f6SChaotian Jing host->saved_tune_para.emmc_top_control = 1827a2e6d1f6SChaotian Jing readl(host->top_base + EMMC_TOP_CONTROL); 1828a2e6d1f6SChaotian Jing host->saved_tune_para.emmc_top_cmd = 1829a2e6d1f6SChaotian Jing readl(host->top_base + EMMC_TOP_CMD); 1830a2e6d1f6SChaotian Jing } else { 1831a2e6d1f6SChaotian Jing host->def_tune_para.pad_tune = readl(host->base + tune_reg); 18322fea5819SChaotian Jing host->saved_tune_para.pad_tune = readl(host->base + tune_reg); 1833a2e6d1f6SChaotian Jing } 183420848903SChaotian Jing dev_dbg(host->dev, "init hardware done!"); 183520848903SChaotian Jing } 183620848903SChaotian Jing 183720848903SChaotian Jing static void msdc_deinit_hw(struct msdc_host *host) 183820848903SChaotian Jing { 183920848903SChaotian Jing u32 val; 1840d087bde5SNeilBrown 1841d087bde5SNeilBrown if (host->internal_cd) { 1842d087bde5SNeilBrown /* Disabled card-detect */ 1843d087bde5SNeilBrown sdr_clr_bits(host->base + MSDC_PS, MSDC_PS_CDEN); 1844d087bde5SNeilBrown sdr_clr_bits(host->base + SDC_CFG, SDC_CFG_INSWKUP); 1845d087bde5SNeilBrown } 1846d087bde5SNeilBrown 184720848903SChaotian Jing /* Disable and clear all interrupts */ 184820848903SChaotian Jing writel(0, host->base + MSDC_INTEN); 184920848903SChaotian Jing 185020848903SChaotian Jing val = readl(host->base + MSDC_INT); 185120848903SChaotian Jing writel(val, host->base + MSDC_INT); 185220848903SChaotian Jing } 185320848903SChaotian Jing 185420848903SChaotian Jing /* init gpd and bd list in msdc_drv_probe */ 185520848903SChaotian Jing static void msdc_init_gpd_bd(struct msdc_host *host, struct msdc_dma *dma) 185620848903SChaotian Jing { 185720848903SChaotian Jing struct mt_gpdma_desc *gpd = dma->gpd; 185820848903SChaotian Jing struct mt_bdma_desc *bd = dma->bd; 18592a9bde19SChaotian Jing dma_addr_t dma_addr; 186020848903SChaotian Jing int i; 186120848903SChaotian Jing 186262b0d27aSChaotian Jing memset(gpd, 0, sizeof(struct mt_gpdma_desc) * 2); 186320848903SChaotian Jing 18642a9bde19SChaotian Jing dma_addr = dma->gpd_addr + sizeof(struct mt_gpdma_desc); 186520848903SChaotian Jing gpd->gpd_info = GPDMA_DESC_BDP; /* hwo, cs, bd pointer */ 186662b0d27aSChaotian Jing /* gpd->next is must set for desc DMA 186762b0d27aSChaotian Jing * That's why must alloc 2 gpd structure. 186862b0d27aSChaotian Jing */ 18692a9bde19SChaotian Jing gpd->next = lower_32_bits(dma_addr); 18702a9bde19SChaotian Jing if (host->dev_comp->support_64g) 18712a9bde19SChaotian Jing gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 24; 18722a9bde19SChaotian Jing 18732a9bde19SChaotian Jing dma_addr = dma->bd_addr; 18742a9bde19SChaotian Jing gpd->ptr = lower_32_bits(dma->bd_addr); /* physical address */ 18752a9bde19SChaotian Jing if (host->dev_comp->support_64g) 18762a9bde19SChaotian Jing gpd->gpd_info |= (upper_32_bits(dma_addr) & 0xf) << 28; 18772a9bde19SChaotian Jing 187820848903SChaotian Jing memset(bd, 0, sizeof(struct mt_bdma_desc) * MAX_BD_NUM); 18792a9bde19SChaotian Jing for (i = 0; i < (MAX_BD_NUM - 1); i++) { 18802a9bde19SChaotian Jing dma_addr = dma->bd_addr + sizeof(*bd) * (i + 1); 18812a9bde19SChaotian Jing bd[i].next = lower_32_bits(dma_addr); 18822a9bde19SChaotian Jing if (host->dev_comp->support_64g) 18832a9bde19SChaotian Jing bd[i].bd_info |= (upper_32_bits(dma_addr) & 0xf) << 24; 18842a9bde19SChaotian Jing } 188520848903SChaotian Jing } 188620848903SChaotian Jing 188720848903SChaotian Jing static void msdc_ops_set_ios(struct mmc_host *mmc, struct mmc_ios *ios) 188820848903SChaotian Jing { 188920848903SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 189020848903SChaotian Jing int ret; 189120848903SChaotian Jing 189220848903SChaotian Jing msdc_set_buswidth(host, ios->bus_width); 189320848903SChaotian Jing 189420848903SChaotian Jing /* Suspend/Resume will do power off/on */ 189520848903SChaotian Jing switch (ios->power_mode) { 189620848903SChaotian Jing case MMC_POWER_UP: 189720848903SChaotian Jing if (!IS_ERR(mmc->supply.vmmc)) { 18986397b7f5SChaotian Jing msdc_init_hw(host); 189920848903SChaotian Jing ret = mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 190020848903SChaotian Jing ios->vdd); 190120848903SChaotian Jing if (ret) { 190220848903SChaotian Jing dev_err(host->dev, "Failed to set vmmc power!\n"); 1903567979fbSUlf Hansson return; 190420848903SChaotian Jing } 190520848903SChaotian Jing } 190620848903SChaotian Jing break; 190720848903SChaotian Jing case MMC_POWER_ON: 190820848903SChaotian Jing if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) { 190920848903SChaotian Jing ret = regulator_enable(mmc->supply.vqmmc); 191020848903SChaotian Jing if (ret) 191120848903SChaotian Jing dev_err(host->dev, "Failed to set vqmmc power!\n"); 191220848903SChaotian Jing else 191320848903SChaotian Jing host->vqmmc_enabled = true; 191420848903SChaotian Jing } 191520848903SChaotian Jing break; 191620848903SChaotian Jing case MMC_POWER_OFF: 191720848903SChaotian Jing if (!IS_ERR(mmc->supply.vmmc)) 191820848903SChaotian Jing mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0); 191920848903SChaotian Jing 192020848903SChaotian Jing if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) { 192120848903SChaotian Jing regulator_disable(mmc->supply.vqmmc); 192220848903SChaotian Jing host->vqmmc_enabled = false; 192320848903SChaotian Jing } 192420848903SChaotian Jing break; 192520848903SChaotian Jing default: 192620848903SChaotian Jing break; 192720848903SChaotian Jing } 192820848903SChaotian Jing 19296e622947SChaotian Jing if (host->mclk != ios->clock || host->timing != ios->timing) 19306e622947SChaotian Jing msdc_set_mclk(host, ios->timing, ios->clock); 193120848903SChaotian Jing } 193220848903SChaotian Jing 19336397b7f5SChaotian Jing static u32 test_delay_bit(u32 delay, u32 bit) 19346397b7f5SChaotian Jing { 19356397b7f5SChaotian Jing bit %= PAD_DELAY_MAX; 19364fe54318SAngeloGioacchino Del Regno return delay & BIT(bit); 19376397b7f5SChaotian Jing } 19386397b7f5SChaotian Jing 19396397b7f5SChaotian Jing static int get_delay_len(u32 delay, u32 start_bit) 19406397b7f5SChaotian Jing { 19416397b7f5SChaotian Jing int i; 19426397b7f5SChaotian Jing 19436397b7f5SChaotian Jing for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) { 19446397b7f5SChaotian Jing if (test_delay_bit(delay, start_bit + i) == 0) 19456397b7f5SChaotian Jing return i; 19466397b7f5SChaotian Jing } 19476397b7f5SChaotian Jing return PAD_DELAY_MAX - start_bit; 19486397b7f5SChaotian Jing } 19496397b7f5SChaotian Jing 19506397b7f5SChaotian Jing static struct msdc_delay_phase get_best_delay(struct msdc_host *host, u32 delay) 19516397b7f5SChaotian Jing { 19526397b7f5SChaotian Jing int start = 0, len = 0; 19536397b7f5SChaotian Jing int start_final = 0, len_final = 0; 19546397b7f5SChaotian Jing u8 final_phase = 0xff; 195562d494caSGeert Uytterhoeven struct msdc_delay_phase delay_phase = { 0, }; 19566397b7f5SChaotian Jing 19576397b7f5SChaotian Jing if (delay == 0) { 19586397b7f5SChaotian Jing dev_err(host->dev, "phase error: [map:%x]\n", delay); 19596397b7f5SChaotian Jing delay_phase.final_phase = final_phase; 19606397b7f5SChaotian Jing return delay_phase; 19616397b7f5SChaotian Jing } 19626397b7f5SChaotian Jing 19636397b7f5SChaotian Jing while (start < PAD_DELAY_MAX) { 19646397b7f5SChaotian Jing len = get_delay_len(delay, start); 19656397b7f5SChaotian Jing if (len_final < len) { 19666397b7f5SChaotian Jing start_final = start; 19676397b7f5SChaotian Jing len_final = len; 19686397b7f5SChaotian Jing } 19696397b7f5SChaotian Jing start += len ? len : 1; 19701ede5cb8Syong mao if (len >= 12 && start_final < 4) 19716397b7f5SChaotian Jing break; 19726397b7f5SChaotian Jing } 19736397b7f5SChaotian Jing 19746397b7f5SChaotian Jing /* The rule is that to find the smallest delay cell */ 19756397b7f5SChaotian Jing if (start_final == 0) 19766397b7f5SChaotian Jing final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX; 19776397b7f5SChaotian Jing else 19786397b7f5SChaotian Jing final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX; 197933106d78SAlexandre Bailon dev_dbg(host->dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n", 19806397b7f5SChaotian Jing delay, len_final, final_phase); 19816397b7f5SChaotian Jing 19826397b7f5SChaotian Jing delay_phase.maxlen = len_final; 19836397b7f5SChaotian Jing delay_phase.start = start_final; 19846397b7f5SChaotian Jing delay_phase.final_phase = final_phase; 19856397b7f5SChaotian Jing return delay_phase; 19866397b7f5SChaotian Jing } 19876397b7f5SChaotian Jing 1988fd82cc30SChaotian Jing static inline void msdc_set_cmd_delay(struct msdc_host *host, u32 value) 1989fd82cc30SChaotian Jing { 1990fd82cc30SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 1991fd82cc30SChaotian Jing 1992fd82cc30SChaotian Jing if (host->top_base) 1993fd82cc30SChaotian Jing sdr_set_field(host->top_base + EMMC_TOP_CMD, PAD_CMD_RXDLY, 1994fd82cc30SChaotian Jing value); 1995fd82cc30SChaotian Jing else 1996fd82cc30SChaotian Jing sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRDLY, 1997fd82cc30SChaotian Jing value); 1998fd82cc30SChaotian Jing } 1999fd82cc30SChaotian Jing 2000fd82cc30SChaotian Jing static inline void msdc_set_data_delay(struct msdc_host *host, u32 value) 2001fd82cc30SChaotian Jing { 2002fd82cc30SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 2003fd82cc30SChaotian Jing 2004fd82cc30SChaotian Jing if (host->top_base) 2005fd82cc30SChaotian Jing sdr_set_field(host->top_base + EMMC_TOP_CONTROL, 2006fd82cc30SChaotian Jing PAD_DAT_RD_RXDLY, value); 2007fd82cc30SChaotian Jing else 2008fd82cc30SChaotian Jing sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_DATRRDLY, 2009fd82cc30SChaotian Jing value); 2010fd82cc30SChaotian Jing } 2011fd82cc30SChaotian Jing 20126397b7f5SChaotian Jing static int msdc_tune_response(struct mmc_host *mmc, u32 opcode) 20136397b7f5SChaotian Jing { 20146397b7f5SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 20156397b7f5SChaotian Jing u32 rise_delay = 0, fall_delay = 0; 2016ae9c657eSChaotian Jing struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; 20171ede5cb8Syong mao struct msdc_delay_phase internal_delay_phase; 20186397b7f5SChaotian Jing u8 final_delay, final_maxlen; 20191ede5cb8Syong mao u32 internal_delay = 0; 202039add252SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 20216397b7f5SChaotian Jing int cmd_err; 20221ede5cb8Syong mao int i, j; 20231ede5cb8Syong mao 20241ede5cb8Syong mao if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || 20251ede5cb8Syong mao mmc->ios.timing == MMC_TIMING_UHS_SDR104) 202639add252SChaotian Jing sdr_set_field(host->base + tune_reg, 20271ede5cb8Syong mao MSDC_PAD_TUNE_CMDRRDLY, 20281ede5cb8Syong mao host->hs200_cmd_int_delay); 20296397b7f5SChaotian Jing 20306397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 20316397b7f5SChaotian Jing for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2032fd82cc30SChaotian Jing msdc_set_cmd_delay(host, i); 20331ede5cb8Syong mao /* 20341ede5cb8Syong mao * Using the same parameters, it may sometimes pass the test, 20351ede5cb8Syong mao * but sometimes it may fail. To make sure the parameters are 20361ede5cb8Syong mao * more stable, we test each set of parameters 3 times. 20371ede5cb8Syong mao */ 20381ede5cb8Syong mao for (j = 0; j < 3; j++) { 20396397b7f5SChaotian Jing mmc_send_tuning(mmc, opcode, &cmd_err); 20401ede5cb8Syong mao if (!cmd_err) { 20414fe54318SAngeloGioacchino Del Regno rise_delay |= BIT(i); 20421ede5cb8Syong mao } else { 20434fe54318SAngeloGioacchino Del Regno rise_delay &= ~BIT(i); 20441ede5cb8Syong mao break; 20451ede5cb8Syong mao } 20461ede5cb8Syong mao } 20476397b7f5SChaotian Jing } 2048ae9c657eSChaotian Jing final_rise_delay = get_best_delay(host, rise_delay); 2049ae9c657eSChaotian Jing /* if rising edge has enough margin, then do not scan falling edge */ 20506b10c9abSChaotian Jing if (final_rise_delay.maxlen >= 12 || 20516b10c9abSChaotian Jing (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) 2052ae9c657eSChaotian Jing goto skip_fall; 20536397b7f5SChaotian Jing 20546397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 20556397b7f5SChaotian Jing for (i = 0; i < PAD_DELAY_MAX; i++) { 2056fd82cc30SChaotian Jing msdc_set_cmd_delay(host, i); 20571ede5cb8Syong mao /* 20581ede5cb8Syong mao * Using the same parameters, it may sometimes pass the test, 20591ede5cb8Syong mao * but sometimes it may fail. To make sure the parameters are 20601ede5cb8Syong mao * more stable, we test each set of parameters 3 times. 20611ede5cb8Syong mao */ 20621ede5cb8Syong mao for (j = 0; j < 3; j++) { 20636397b7f5SChaotian Jing mmc_send_tuning(mmc, opcode, &cmd_err); 20641ede5cb8Syong mao if (!cmd_err) { 20654fe54318SAngeloGioacchino Del Regno fall_delay |= BIT(i); 20661ede5cb8Syong mao } else { 20674fe54318SAngeloGioacchino Del Regno fall_delay &= ~BIT(i); 20681ede5cb8Syong mao break; 20691ede5cb8Syong mao } 20701ede5cb8Syong mao } 20716397b7f5SChaotian Jing } 20726397b7f5SChaotian Jing final_fall_delay = get_best_delay(host, fall_delay); 20736397b7f5SChaotian Jing 2074ae9c657eSChaotian Jing skip_fall: 20756397b7f5SChaotian Jing final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); 20761ede5cb8Syong mao if (final_fall_delay.maxlen >= 12 && final_fall_delay.start < 4) 20771ede5cb8Syong mao final_maxlen = final_fall_delay.maxlen; 20786397b7f5SChaotian Jing if (final_maxlen == final_rise_delay.maxlen) { 20796397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 20806397b7f5SChaotian Jing final_delay = final_rise_delay.final_phase; 20816397b7f5SChaotian Jing } else { 20826397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 20836397b7f5SChaotian Jing final_delay = final_fall_delay.final_phase; 20846397b7f5SChaotian Jing } 2085fd82cc30SChaotian Jing msdc_set_cmd_delay(host, final_delay); 2086fd82cc30SChaotian Jing 20872fea5819SChaotian Jing if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay) 20881ede5cb8Syong mao goto skip_internal; 20896397b7f5SChaotian Jing 20901ede5cb8Syong mao for (i = 0; i < PAD_DELAY_MAX; i++) { 209139add252SChaotian Jing sdr_set_field(host->base + tune_reg, 20921ede5cb8Syong mao MSDC_PAD_TUNE_CMDRRDLY, i); 20931ede5cb8Syong mao mmc_send_tuning(mmc, opcode, &cmd_err); 20941ede5cb8Syong mao if (!cmd_err) 20954fe54318SAngeloGioacchino Del Regno internal_delay |= BIT(i); 20961ede5cb8Syong mao } 20971ede5cb8Syong mao dev_dbg(host->dev, "Final internal delay: 0x%x\n", internal_delay); 20981ede5cb8Syong mao internal_delay_phase = get_best_delay(host, internal_delay); 209939add252SChaotian Jing sdr_set_field(host->base + tune_reg, MSDC_PAD_TUNE_CMDRRDLY, 21001ede5cb8Syong mao internal_delay_phase.final_phase); 21011ede5cb8Syong mao skip_internal: 21021ede5cb8Syong mao dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay); 21031ede5cb8Syong mao return final_delay == 0xff ? -EIO : 0; 21041ede5cb8Syong mao } 21051ede5cb8Syong mao 21061ede5cb8Syong mao static int hs400_tune_response(struct mmc_host *mmc, u32 opcode) 21071ede5cb8Syong mao { 21081ede5cb8Syong mao struct msdc_host *host = mmc_priv(mmc); 21091ede5cb8Syong mao u32 cmd_delay = 0; 21101ede5cb8Syong mao struct msdc_delay_phase final_cmd_delay = { 0,}; 21111ede5cb8Syong mao u8 final_delay; 21121ede5cb8Syong mao int cmd_err; 21131ede5cb8Syong mao int i, j; 21141ede5cb8Syong mao 21151ede5cb8Syong mao /* select EMMC50 PAD CMD tune */ 21161ede5cb8Syong mao sdr_set_bits(host->base + PAD_CMD_TUNE, BIT(0)); 21178f34e5bdSChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT1, MSDC_PATCH_BIT1_CMDTA, 2); 21181ede5cb8Syong mao 21191ede5cb8Syong mao if (mmc->ios.timing == MMC_TIMING_MMC_HS200 || 21201ede5cb8Syong mao mmc->ios.timing == MMC_TIMING_UHS_SDR104) 21211ede5cb8Syong mao sdr_set_field(host->base + MSDC_PAD_TUNE, 21221ede5cb8Syong mao MSDC_PAD_TUNE_CMDRRDLY, 21231ede5cb8Syong mao host->hs200_cmd_int_delay); 21241ede5cb8Syong mao 21251ede5cb8Syong mao if (host->hs400_cmd_resp_sel_rising) 21261ede5cb8Syong mao sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 21271ede5cb8Syong mao else 21281ede5cb8Syong mao sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 21291ede5cb8Syong mao for (i = 0 ; i < PAD_DELAY_MAX; i++) { 21301ede5cb8Syong mao sdr_set_field(host->base + PAD_CMD_TUNE, 21311ede5cb8Syong mao PAD_CMD_TUNE_RX_DLY3, i); 21321ede5cb8Syong mao /* 21331ede5cb8Syong mao * Using the same parameters, it may sometimes pass the test, 21341ede5cb8Syong mao * but sometimes it may fail. To make sure the parameters are 21351ede5cb8Syong mao * more stable, we test each set of parameters 3 times. 21361ede5cb8Syong mao */ 21371ede5cb8Syong mao for (j = 0; j < 3; j++) { 21381ede5cb8Syong mao mmc_send_tuning(mmc, opcode, &cmd_err); 21391ede5cb8Syong mao if (!cmd_err) { 21404fe54318SAngeloGioacchino Del Regno cmd_delay |= BIT(i); 21411ede5cb8Syong mao } else { 21424fe54318SAngeloGioacchino Del Regno cmd_delay &= ~BIT(i); 21431ede5cb8Syong mao break; 21441ede5cb8Syong mao } 21451ede5cb8Syong mao } 21461ede5cb8Syong mao } 21471ede5cb8Syong mao final_cmd_delay = get_best_delay(host, cmd_delay); 21481ede5cb8Syong mao sdr_set_field(host->base + PAD_CMD_TUNE, PAD_CMD_TUNE_RX_DLY3, 21491ede5cb8Syong mao final_cmd_delay.final_phase); 21501ede5cb8Syong mao final_delay = final_cmd_delay.final_phase; 21511ede5cb8Syong mao 21521ede5cb8Syong mao dev_dbg(host->dev, "Final cmd pad delay: %x\n", final_delay); 21536397b7f5SChaotian Jing return final_delay == 0xff ? -EIO : 0; 21546397b7f5SChaotian Jing } 21556397b7f5SChaotian Jing 21566397b7f5SChaotian Jing static int msdc_tune_data(struct mmc_host *mmc, u32 opcode) 21576397b7f5SChaotian Jing { 21586397b7f5SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 21596397b7f5SChaotian Jing u32 rise_delay = 0, fall_delay = 0; 2160ae9c657eSChaotian Jing struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; 21616397b7f5SChaotian Jing u8 final_delay, final_maxlen; 21626397b7f5SChaotian Jing int i, ret; 21636397b7f5SChaotian Jing 2164d17bb71cSChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL, 2165d17bb71cSChaotian Jing host->latch_ck); 21666397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 21676397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 21686397b7f5SChaotian Jing for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2169fd82cc30SChaotian Jing msdc_set_data_delay(host, i); 21706397b7f5SChaotian Jing ret = mmc_send_tuning(mmc, opcode, NULL); 21716397b7f5SChaotian Jing if (!ret) 21724fe54318SAngeloGioacchino Del Regno rise_delay |= BIT(i); 21736397b7f5SChaotian Jing } 2174ae9c657eSChaotian Jing final_rise_delay = get_best_delay(host, rise_delay); 2175ae9c657eSChaotian Jing /* if rising edge has enough margin, then do not scan falling edge */ 21761ede5cb8Syong mao if (final_rise_delay.maxlen >= 12 || 2177ae9c657eSChaotian Jing (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) 2178ae9c657eSChaotian Jing goto skip_fall; 21796397b7f5SChaotian Jing 21806397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 21816397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 21826397b7f5SChaotian Jing for (i = 0; i < PAD_DELAY_MAX; i++) { 2183fd82cc30SChaotian Jing msdc_set_data_delay(host, i); 21846397b7f5SChaotian Jing ret = mmc_send_tuning(mmc, opcode, NULL); 21856397b7f5SChaotian Jing if (!ret) 21864fe54318SAngeloGioacchino Del Regno fall_delay |= BIT(i); 21876397b7f5SChaotian Jing } 21886397b7f5SChaotian Jing final_fall_delay = get_best_delay(host, fall_delay); 21896397b7f5SChaotian Jing 2190ae9c657eSChaotian Jing skip_fall: 21916397b7f5SChaotian Jing final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); 21926397b7f5SChaotian Jing if (final_maxlen == final_rise_delay.maxlen) { 21936397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 21946397b7f5SChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 21956397b7f5SChaotian Jing final_delay = final_rise_delay.final_phase; 21966397b7f5SChaotian Jing } else { 21976397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_DSPL); 21986397b7f5SChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_W_DSPL); 21996397b7f5SChaotian Jing final_delay = final_fall_delay.final_phase; 22006397b7f5SChaotian Jing } 2201fd82cc30SChaotian Jing msdc_set_data_delay(host, final_delay); 22026397b7f5SChaotian Jing 22031ede5cb8Syong mao dev_dbg(host->dev, "Final data pad delay: %x\n", final_delay); 22046397b7f5SChaotian Jing return final_delay == 0xff ? -EIO : 0; 22056397b7f5SChaotian Jing } 22066397b7f5SChaotian Jing 220786601d0eSChaotian Jing /* 220886601d0eSChaotian Jing * MSDC IP which supports data tune + async fifo can do CMD/DAT tune 220986601d0eSChaotian Jing * together, which can save the tuning time. 221086601d0eSChaotian Jing */ 221186601d0eSChaotian Jing static int msdc_tune_together(struct mmc_host *mmc, u32 opcode) 221286601d0eSChaotian Jing { 221386601d0eSChaotian Jing struct msdc_host *host = mmc_priv(mmc); 221486601d0eSChaotian Jing u32 rise_delay = 0, fall_delay = 0; 221586601d0eSChaotian Jing struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0,}; 221686601d0eSChaotian Jing u8 final_delay, final_maxlen; 221786601d0eSChaotian Jing int i, ret; 221886601d0eSChaotian Jing 221986601d0eSChaotian Jing sdr_set_field(host->base + MSDC_PATCH_BIT, MSDC_INT_DAT_LATCH_CK_SEL, 222086601d0eSChaotian Jing host->latch_ck); 222186601d0eSChaotian Jing 222286601d0eSChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 222386601d0eSChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, 222486601d0eSChaotian Jing MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 222586601d0eSChaotian Jing for (i = 0 ; i < PAD_DELAY_MAX; i++) { 2226fd82cc30SChaotian Jing msdc_set_cmd_delay(host, i); 2227fd82cc30SChaotian Jing msdc_set_data_delay(host, i); 222886601d0eSChaotian Jing ret = mmc_send_tuning(mmc, opcode, NULL); 222986601d0eSChaotian Jing if (!ret) 22304fe54318SAngeloGioacchino Del Regno rise_delay |= BIT(i); 223186601d0eSChaotian Jing } 223286601d0eSChaotian Jing final_rise_delay = get_best_delay(host, rise_delay); 223386601d0eSChaotian Jing /* if rising edge has enough margin, then do not scan falling edge */ 223486601d0eSChaotian Jing if (final_rise_delay.maxlen >= 12 || 223586601d0eSChaotian Jing (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) 223686601d0eSChaotian Jing goto skip_fall; 223786601d0eSChaotian Jing 223886601d0eSChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 223986601d0eSChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, 224086601d0eSChaotian Jing MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 224186601d0eSChaotian Jing for (i = 0; i < PAD_DELAY_MAX; i++) { 2242fd82cc30SChaotian Jing msdc_set_cmd_delay(host, i); 2243fd82cc30SChaotian Jing msdc_set_data_delay(host, i); 224486601d0eSChaotian Jing ret = mmc_send_tuning(mmc, opcode, NULL); 224586601d0eSChaotian Jing if (!ret) 22464fe54318SAngeloGioacchino Del Regno fall_delay |= BIT(i); 224786601d0eSChaotian Jing } 224886601d0eSChaotian Jing final_fall_delay = get_best_delay(host, fall_delay); 224986601d0eSChaotian Jing 225086601d0eSChaotian Jing skip_fall: 225186601d0eSChaotian Jing final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); 225286601d0eSChaotian Jing if (final_maxlen == final_rise_delay.maxlen) { 225386601d0eSChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 225486601d0eSChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, 225586601d0eSChaotian Jing MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 225686601d0eSChaotian Jing final_delay = final_rise_delay.final_phase; 225786601d0eSChaotian Jing } else { 225886601d0eSChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, MSDC_IOCON_RSPL); 225986601d0eSChaotian Jing sdr_set_bits(host->base + MSDC_IOCON, 226086601d0eSChaotian Jing MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 226186601d0eSChaotian Jing final_delay = final_fall_delay.final_phase; 226286601d0eSChaotian Jing } 226386601d0eSChaotian Jing 2264fd82cc30SChaotian Jing msdc_set_cmd_delay(host, final_delay); 2265fd82cc30SChaotian Jing msdc_set_data_delay(host, final_delay); 2266a2e6d1f6SChaotian Jing 226786601d0eSChaotian Jing dev_dbg(host->dev, "Final pad delay: %x\n", final_delay); 226886601d0eSChaotian Jing return final_delay == 0xff ? -EIO : 0; 226986601d0eSChaotian Jing } 227086601d0eSChaotian Jing 22716397b7f5SChaotian Jing static int msdc_execute_tuning(struct mmc_host *mmc, u32 opcode) 22726397b7f5SChaotian Jing { 22736397b7f5SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 22746397b7f5SChaotian Jing int ret; 227539add252SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 22766397b7f5SChaotian Jing 227786601d0eSChaotian Jing if (host->dev_comp->data_tune && host->dev_comp->async_fifo) { 227886601d0eSChaotian Jing ret = msdc_tune_together(mmc, opcode); 227986601d0eSChaotian Jing if (host->hs400_mode) { 228086601d0eSChaotian Jing sdr_clr_bits(host->base + MSDC_IOCON, 228186601d0eSChaotian Jing MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); 2282fd82cc30SChaotian Jing msdc_set_data_delay(host, 0); 228386601d0eSChaotian Jing } 228486601d0eSChaotian Jing goto tune_done; 228586601d0eSChaotian Jing } 22867f3d5852SChaotian Jing if (host->hs400_mode && 22877f3d5852SChaotian Jing host->dev_comp->hs400_tune) 22881ede5cb8Syong mao ret = hs400_tune_response(mmc, opcode); 22891ede5cb8Syong mao else 22906397b7f5SChaotian Jing ret = msdc_tune_response(mmc, opcode); 22916397b7f5SChaotian Jing if (ret == -EIO) { 22926397b7f5SChaotian Jing dev_err(host->dev, "Tune response fail!\n"); 2293567979fbSUlf Hansson return ret; 22946397b7f5SChaotian Jing } 22955462ff39SChaotian Jing if (host->hs400_mode == false) { 22966397b7f5SChaotian Jing ret = msdc_tune_data(mmc, opcode); 22976397b7f5SChaotian Jing if (ret == -EIO) 22986397b7f5SChaotian Jing dev_err(host->dev, "Tune data fail!\n"); 22995462ff39SChaotian Jing } 23006397b7f5SChaotian Jing 230186601d0eSChaotian Jing tune_done: 230286beac37SChaotian Jing host->saved_tune_para.iocon = readl(host->base + MSDC_IOCON); 230339add252SChaotian Jing host->saved_tune_para.pad_tune = readl(host->base + tune_reg); 23041ede5cb8Syong mao host->saved_tune_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); 2305a2e6d1f6SChaotian Jing if (host->top_base) { 2306a2e6d1f6SChaotian Jing host->saved_tune_para.emmc_top_control = readl(host->top_base + 2307a2e6d1f6SChaotian Jing EMMC_TOP_CONTROL); 2308a2e6d1f6SChaotian Jing host->saved_tune_para.emmc_top_cmd = readl(host->top_base + 2309a2e6d1f6SChaotian Jing EMMC_TOP_CMD); 2310a2e6d1f6SChaotian Jing } 23116397b7f5SChaotian Jing return ret; 23126397b7f5SChaotian Jing } 23136397b7f5SChaotian Jing 23146397b7f5SChaotian Jing static int msdc_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios) 23156397b7f5SChaotian Jing { 23166397b7f5SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 23175462ff39SChaotian Jing host->hs400_mode = true; 23186397b7f5SChaotian Jing 2319a2e6d1f6SChaotian Jing if (host->top_base) 2320a2e6d1f6SChaotian Jing writel(host->hs400_ds_delay, 2321a2e6d1f6SChaotian Jing host->top_base + EMMC50_PAD_DS_TUNE); 2322a2e6d1f6SChaotian Jing else 23236397b7f5SChaotian Jing writel(host->hs400_ds_delay, host->base + PAD_DS_TUNE); 23242fea5819SChaotian Jing /* hs400 mode must set it to 0 */ 23252fea5819SChaotian Jing sdr_clr_bits(host->base + MSDC_PATCH_BIT2, MSDC_PATCH_BIT2_CFGCRCSTS); 2326c8609b22SChaotian Jing /* to improve read performance, set outstanding to 2 */ 2327c8609b22SChaotian Jing sdr_set_field(host->base + EMMC50_CFG3, EMMC50_CFG3_OUTS_WR, 2); 2328c8609b22SChaotian Jing 23296397b7f5SChaotian Jing return 0; 23306397b7f5SChaotian Jing } 23316397b7f5SChaotian Jing 2332c4ac38c6SWenbin Mei static int msdc_execute_hs400_tuning(struct mmc_host *mmc, struct mmc_card *card) 2333c4ac38c6SWenbin Mei { 2334c4ac38c6SWenbin Mei struct msdc_host *host = mmc_priv(mmc); 2335c4ac38c6SWenbin Mei struct msdc_delay_phase dly1_delay; 2336c4ac38c6SWenbin Mei u32 val, result_dly1 = 0; 2337c4ac38c6SWenbin Mei u8 *ext_csd; 2338c4ac38c6SWenbin Mei int i, ret; 2339c4ac38c6SWenbin Mei 2340c4ac38c6SWenbin Mei if (host->top_base) { 2341c4ac38c6SWenbin Mei sdr_set_bits(host->top_base + EMMC50_PAD_DS_TUNE, 2342c4ac38c6SWenbin Mei PAD_DS_DLY_SEL); 2343c4ac38c6SWenbin Mei if (host->hs400_ds_dly3) 2344c4ac38c6SWenbin Mei sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE, 2345c4ac38c6SWenbin Mei PAD_DS_DLY3, host->hs400_ds_dly3); 2346c4ac38c6SWenbin Mei } else { 2347c4ac38c6SWenbin Mei sdr_set_bits(host->base + PAD_DS_TUNE, PAD_DS_TUNE_DLY_SEL); 2348c4ac38c6SWenbin Mei if (host->hs400_ds_dly3) 2349c4ac38c6SWenbin Mei sdr_set_field(host->base + PAD_DS_TUNE, 2350c4ac38c6SWenbin Mei PAD_DS_TUNE_DLY3, host->hs400_ds_dly3); 2351c4ac38c6SWenbin Mei } 2352c4ac38c6SWenbin Mei 2353c4ac38c6SWenbin Mei host->hs400_tuning = true; 2354c4ac38c6SWenbin Mei for (i = 0; i < PAD_DELAY_MAX; i++) { 2355c4ac38c6SWenbin Mei if (host->top_base) 2356c4ac38c6SWenbin Mei sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE, 2357c4ac38c6SWenbin Mei PAD_DS_DLY1, i); 2358c4ac38c6SWenbin Mei else 2359c4ac38c6SWenbin Mei sdr_set_field(host->base + PAD_DS_TUNE, 2360c4ac38c6SWenbin Mei PAD_DS_TUNE_DLY1, i); 2361c4ac38c6SWenbin Mei ret = mmc_get_ext_csd(card, &ext_csd); 2362d594b35dSWenbin Mei if (!ret) { 23634fe54318SAngeloGioacchino Del Regno result_dly1 |= BIT(i); 2364d594b35dSWenbin Mei kfree(ext_csd); 2365d594b35dSWenbin Mei } 2366c4ac38c6SWenbin Mei } 2367c4ac38c6SWenbin Mei host->hs400_tuning = false; 2368c4ac38c6SWenbin Mei 2369c4ac38c6SWenbin Mei dly1_delay = get_best_delay(host, result_dly1); 2370c4ac38c6SWenbin Mei if (dly1_delay.maxlen == 0) { 2371c4ac38c6SWenbin Mei dev_err(host->dev, "Failed to get DLY1 delay!\n"); 2372c4ac38c6SWenbin Mei goto fail; 2373c4ac38c6SWenbin Mei } 2374c4ac38c6SWenbin Mei if (host->top_base) 2375c4ac38c6SWenbin Mei sdr_set_field(host->top_base + EMMC50_PAD_DS_TUNE, 2376c4ac38c6SWenbin Mei PAD_DS_DLY1, dly1_delay.final_phase); 2377c4ac38c6SWenbin Mei else 2378c4ac38c6SWenbin Mei sdr_set_field(host->base + PAD_DS_TUNE, 2379c4ac38c6SWenbin Mei PAD_DS_TUNE_DLY1, dly1_delay.final_phase); 2380c4ac38c6SWenbin Mei 2381c4ac38c6SWenbin Mei if (host->top_base) 2382c4ac38c6SWenbin Mei val = readl(host->top_base + EMMC50_PAD_DS_TUNE); 2383c4ac38c6SWenbin Mei else 2384c4ac38c6SWenbin Mei val = readl(host->base + PAD_DS_TUNE); 2385c4ac38c6SWenbin Mei 2386f0c88b04SFabien Parent dev_info(host->dev, "Final PAD_DS_TUNE: 0x%x\n", val); 2387c4ac38c6SWenbin Mei 2388c4ac38c6SWenbin Mei return 0; 2389c4ac38c6SWenbin Mei 2390c4ac38c6SWenbin Mei fail: 2391c4ac38c6SWenbin Mei dev_err(host->dev, "Failed to tuning DS pin delay!\n"); 2392c4ac38c6SWenbin Mei return -EIO; 2393c4ac38c6SWenbin Mei } 2394c4ac38c6SWenbin Mei 2395c9b5061eSChaotian Jing static void msdc_hw_reset(struct mmc_host *mmc) 2396c9b5061eSChaotian Jing { 2397c9b5061eSChaotian Jing struct msdc_host *host = mmc_priv(mmc); 2398c9b5061eSChaotian Jing 2399c9b5061eSChaotian Jing sdr_set_bits(host->base + EMMC_IOCON, 1); 2400c9b5061eSChaotian Jing udelay(10); /* 10us is enough */ 2401c9b5061eSChaotian Jing sdr_clr_bits(host->base + EMMC_IOCON, 1); 2402c9b5061eSChaotian Jing } 2403c9b5061eSChaotian Jing 24045215b2e9Sjjian zhou static void msdc_ack_sdio_irq(struct mmc_host *mmc) 24055215b2e9Sjjian zhou { 24068a5df8acSjjian zhou unsigned long flags; 24078a5df8acSjjian zhou struct msdc_host *host = mmc_priv(mmc); 24088a5df8acSjjian zhou 24098a5df8acSjjian zhou spin_lock_irqsave(&host->lock, flags); 24108a5df8acSjjian zhou __msdc_enable_sdio_irq(host, 1); 24118a5df8acSjjian zhou spin_unlock_irqrestore(&host->lock, flags); 24125215b2e9Sjjian zhou } 24135215b2e9Sjjian zhou 2414d087bde5SNeilBrown static int msdc_get_cd(struct mmc_host *mmc) 2415d087bde5SNeilBrown { 2416d087bde5SNeilBrown struct msdc_host *host = mmc_priv(mmc); 2417d087bde5SNeilBrown int val; 2418d087bde5SNeilBrown 2419d087bde5SNeilBrown if (mmc->caps & MMC_CAP_NONREMOVABLE) 2420d087bde5SNeilBrown return 1; 2421d087bde5SNeilBrown 2422d087bde5SNeilBrown if (!host->internal_cd) 2423d087bde5SNeilBrown return mmc_gpio_get_cd(mmc); 2424d087bde5SNeilBrown 2425d087bde5SNeilBrown val = readl(host->base + MSDC_PS) & MSDC_PS_CDSTS; 2426d087bde5SNeilBrown if (mmc->caps2 & MMC_CAP2_CD_ACTIVE_HIGH) 2427d087bde5SNeilBrown return !!val; 2428d087bde5SNeilBrown else 2429d087bde5SNeilBrown return !val; 2430d087bde5SNeilBrown } 2431d087bde5SNeilBrown 243213b4e1e9SWenbin Mei static void msdc_hs400_enhanced_strobe(struct mmc_host *mmc, 243313b4e1e9SWenbin Mei struct mmc_ios *ios) 243413b4e1e9SWenbin Mei { 243513b4e1e9SWenbin Mei struct msdc_host *host = mmc_priv(mmc); 243613b4e1e9SWenbin Mei 243713b4e1e9SWenbin Mei if (ios->enhanced_strobe) { 243813b4e1e9SWenbin Mei msdc_prepare_hs400_tuning(mmc, ios); 243913b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_PADCMD_LATCHCK, 1); 244013b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_CMD_RESP_SEL, 1); 244113b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC50_CFG1, EMMC50_CFG1_DS_CFG, 1); 244213b4e1e9SWenbin Mei 244313b4e1e9SWenbin Mei sdr_clr_bits(host->base + CQHCI_SETTING, CQHCI_RD_CMD_WND_SEL); 244413b4e1e9SWenbin Mei sdr_clr_bits(host->base + CQHCI_SETTING, CQHCI_WR_CMD_WND_SEL); 244513b4e1e9SWenbin Mei sdr_clr_bits(host->base + EMMC51_CFG0, CMDQ_RDAT_CNT); 244613b4e1e9SWenbin Mei } else { 244713b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_PADCMD_LATCHCK, 0); 244813b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC50_CFG0, EMMC50_CFG_CMD_RESP_SEL, 0); 244913b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC50_CFG1, EMMC50_CFG1_DS_CFG, 0); 245013b4e1e9SWenbin Mei 245113b4e1e9SWenbin Mei sdr_set_bits(host->base + CQHCI_SETTING, CQHCI_RD_CMD_WND_SEL); 245213b4e1e9SWenbin Mei sdr_set_bits(host->base + CQHCI_SETTING, CQHCI_WR_CMD_WND_SEL); 245313b4e1e9SWenbin Mei sdr_set_field(host->base + EMMC51_CFG0, CMDQ_RDAT_CNT, 0xb4); 245413b4e1e9SWenbin Mei } 245513b4e1e9SWenbin Mei } 245613b4e1e9SWenbin Mei 245788bd652bSChun-Hung Wu static void msdc_cqe_enable(struct mmc_host *mmc) 245888bd652bSChun-Hung Wu { 245988bd652bSChun-Hung Wu struct msdc_host *host = mmc_priv(mmc); 246088bd652bSChun-Hung Wu 246188bd652bSChun-Hung Wu /* enable cmdq irq */ 246288bd652bSChun-Hung Wu writel(MSDC_INT_CMDQ, host->base + MSDC_INTEN); 246388bd652bSChun-Hung Wu /* enable busy check */ 246488bd652bSChun-Hung Wu sdr_set_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_BUSY_CHECK_SEL); 246588bd652bSChun-Hung Wu /* default write data / busy timeout 20s */ 246688bd652bSChun-Hung Wu msdc_set_busy_timeout(host, 20 * 1000000000ULL, 0); 246788bd652bSChun-Hung Wu /* default read data timeout 1s */ 246888bd652bSChun-Hung Wu msdc_set_timeout(host, 1000000000ULL, 0); 246988bd652bSChun-Hung Wu } 247088bd652bSChun-Hung Wu 24717f4bc2e8SWei Yongjun static void msdc_cqe_disable(struct mmc_host *mmc, bool recovery) 247288bd652bSChun-Hung Wu { 247388bd652bSChun-Hung Wu struct msdc_host *host = mmc_priv(mmc); 247443e5fee3SDerong Liu unsigned int val = 0; 247588bd652bSChun-Hung Wu 247688bd652bSChun-Hung Wu /* disable cmdq irq */ 247788bd652bSChun-Hung Wu sdr_clr_bits(host->base + MSDC_INTEN, MSDC_INT_CMDQ); 247888bd652bSChun-Hung Wu /* disable busy check */ 247988bd652bSChun-Hung Wu sdr_clr_bits(host->base + MSDC_PATCH_BIT1, MSDC_PB1_BUSY_CHECK_SEL); 248088bd652bSChun-Hung Wu 2481cc5d1692SWenbin Mei val = readl(host->base + MSDC_INT); 2482cc5d1692SWenbin Mei writel(val, host->base + MSDC_INT); 2483cc5d1692SWenbin Mei 248488bd652bSChun-Hung Wu if (recovery) { 248588bd652bSChun-Hung Wu sdr_set_field(host->base + MSDC_DMA_CTRL, 248688bd652bSChun-Hung Wu MSDC_DMA_CTRL_STOP, 1); 248789bcd9a6SMengqi Zhang if (WARN_ON(readl_poll_timeout(host->base + MSDC_DMA_CTRL, val, 248889bcd9a6SMengqi Zhang !(val & MSDC_DMA_CTRL_STOP), 1, 3000))) 248989bcd9a6SMengqi Zhang return; 249043e5fee3SDerong Liu if (WARN_ON(readl_poll_timeout(host->base + MSDC_DMA_CFG, val, 249143e5fee3SDerong Liu !(val & MSDC_DMA_CFG_STS), 1, 3000))) 249243e5fee3SDerong Liu return; 249388bd652bSChun-Hung Wu msdc_reset_hw(host); 249488bd652bSChun-Hung Wu } 249588bd652bSChun-Hung Wu } 249688bd652bSChun-Hung Wu 2497e282f204SChun-Hung Wu static void msdc_cqe_pre_enable(struct mmc_host *mmc) 2498e282f204SChun-Hung Wu { 2499e282f204SChun-Hung Wu struct cqhci_host *cq_host = mmc->cqe_private; 2500e282f204SChun-Hung Wu u32 reg; 2501e282f204SChun-Hung Wu 2502e282f204SChun-Hung Wu reg = cqhci_readl(cq_host, CQHCI_CFG); 2503e282f204SChun-Hung Wu reg |= CQHCI_ENABLE; 2504e282f204SChun-Hung Wu cqhci_writel(cq_host, reg, CQHCI_CFG); 2505e282f204SChun-Hung Wu } 2506e282f204SChun-Hung Wu 2507e282f204SChun-Hung Wu static void msdc_cqe_post_disable(struct mmc_host *mmc) 2508e282f204SChun-Hung Wu { 2509e282f204SChun-Hung Wu struct cqhci_host *cq_host = mmc->cqe_private; 2510e282f204SChun-Hung Wu u32 reg; 2511e282f204SChun-Hung Wu 2512e282f204SChun-Hung Wu reg = cqhci_readl(cq_host, CQHCI_CFG); 2513e282f204SChun-Hung Wu reg &= ~CQHCI_ENABLE; 2514e282f204SChun-Hung Wu cqhci_writel(cq_host, reg, CQHCI_CFG); 2515e282f204SChun-Hung Wu } 2516e282f204SChun-Hung Wu 2517be7815d6SJulia Lawall static const struct mmc_host_ops mt_msdc_ops = { 251820848903SChaotian Jing .post_req = msdc_post_req, 251920848903SChaotian Jing .pre_req = msdc_pre_req, 252020848903SChaotian Jing .request = msdc_ops_request, 252120848903SChaotian Jing .set_ios = msdc_ops_set_ios, 25228d53e412SChaotian Jing .get_ro = mmc_gpio_get_ro, 2523d087bde5SNeilBrown .get_cd = msdc_get_cd, 252413b4e1e9SWenbin Mei .hs400_enhanced_strobe = msdc_hs400_enhanced_strobe, 25255215b2e9Sjjian zhou .enable_sdio_irq = msdc_enable_sdio_irq, 25265215b2e9Sjjian zhou .ack_sdio_irq = msdc_ack_sdio_irq, 252720848903SChaotian Jing .start_signal_voltage_switch = msdc_ops_switch_volt, 252820848903SChaotian Jing .card_busy = msdc_card_busy, 25296397b7f5SChaotian Jing .execute_tuning = msdc_execute_tuning, 25306397b7f5SChaotian Jing .prepare_hs400_tuning = msdc_prepare_hs400_tuning, 2531c4ac38c6SWenbin Mei .execute_hs400_tuning = msdc_execute_hs400_tuning, 253232f18e59SWolfram Sang .card_hw_reset = msdc_hw_reset, 253320848903SChaotian Jing }; 253420848903SChaotian Jing 253588bd652bSChun-Hung Wu static const struct cqhci_host_ops msdc_cmdq_ops = { 253688bd652bSChun-Hung Wu .enable = msdc_cqe_enable, 253788bd652bSChun-Hung Wu .disable = msdc_cqe_disable, 2538e282f204SChun-Hung Wu .pre_enable = msdc_cqe_pre_enable, 2539e282f204SChun-Hung Wu .post_disable = msdc_cqe_post_disable, 254088bd652bSChun-Hung Wu }; 254188bd652bSChun-Hung Wu 25421ede5cb8Syong mao static void msdc_of_property_parse(struct platform_device *pdev, 25431ede5cb8Syong mao struct msdc_host *host) 25441ede5cb8Syong mao { 2545d17bb71cSChaotian Jing of_property_read_u32(pdev->dev.of_node, "mediatek,latch-ck", 2546d17bb71cSChaotian Jing &host->latch_ck); 2547d17bb71cSChaotian Jing 25481ede5cb8Syong mao of_property_read_u32(pdev->dev.of_node, "hs400-ds-delay", 25491ede5cb8Syong mao &host->hs400_ds_delay); 25501ede5cb8Syong mao 2551c4ac38c6SWenbin Mei of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-ds-dly3", 2552c4ac38c6SWenbin Mei &host->hs400_ds_dly3); 2553c4ac38c6SWenbin Mei 25541ede5cb8Syong mao of_property_read_u32(pdev->dev.of_node, "mediatek,hs200-cmd-int-delay", 25551ede5cb8Syong mao &host->hs200_cmd_int_delay); 25561ede5cb8Syong mao 25571ede5cb8Syong mao of_property_read_u32(pdev->dev.of_node, "mediatek,hs400-cmd-int-delay", 25581ede5cb8Syong mao &host->hs400_cmd_int_delay); 25591ede5cb8Syong mao 25601ede5cb8Syong mao if (of_property_read_bool(pdev->dev.of_node, 25611ede5cb8Syong mao "mediatek,hs400-cmd-resp-sel-rising")) 25621ede5cb8Syong mao host->hs400_cmd_resp_sel_rising = true; 25631ede5cb8Syong mao else 25641ede5cb8Syong mao host->hs400_cmd_resp_sel_rising = false; 256588bd652bSChun-Hung Wu 256688bd652bSChun-Hung Wu if (of_property_read_bool(pdev->dev.of_node, 256788bd652bSChun-Hung Wu "supports-cqe")) 256888bd652bSChun-Hung Wu host->cqhci = true; 256988bd652bSChun-Hung Wu else 257088bd652bSChun-Hung Wu host->cqhci = false; 25711ede5cb8Syong mao } 25721ede5cb8Syong mao 2573f5eccd94SWenbin Mei static int msdc_of_clock_parse(struct platform_device *pdev, 2574f5eccd94SWenbin Mei struct msdc_host *host) 2575f5eccd94SWenbin Mei { 2576f5eccd94SWenbin Mei int ret; 2577f5eccd94SWenbin Mei 2578f5eccd94SWenbin Mei host->src_clk = devm_clk_get(&pdev->dev, "source"); 2579f5eccd94SWenbin Mei if (IS_ERR(host->src_clk)) 2580f5eccd94SWenbin Mei return PTR_ERR(host->src_clk); 2581f5eccd94SWenbin Mei 2582f5eccd94SWenbin Mei host->h_clk = devm_clk_get(&pdev->dev, "hclk"); 2583f5eccd94SWenbin Mei if (IS_ERR(host->h_clk)) 2584f5eccd94SWenbin Mei return PTR_ERR(host->h_clk); 2585f5eccd94SWenbin Mei 2586f5eccd94SWenbin Mei host->bus_clk = devm_clk_get_optional(&pdev->dev, "bus_clk"); 2587f5eccd94SWenbin Mei if (IS_ERR(host->bus_clk)) 2588f5eccd94SWenbin Mei host->bus_clk = NULL; 2589f5eccd94SWenbin Mei 2590f5eccd94SWenbin Mei /*source clock control gate is optional clock*/ 2591f5eccd94SWenbin Mei host->src_clk_cg = devm_clk_get_optional(&pdev->dev, "source_cg"); 2592f5eccd94SWenbin Mei if (IS_ERR(host->src_clk_cg)) 2593996be7b7SAngeloGioacchino Del Regno return PTR_ERR(host->src_clk_cg); 2594f5eccd94SWenbin Mei 2595e5e8b224SAngeloGioacchino Del Regno /* 2596e5e8b224SAngeloGioacchino Del Regno * Fallback for legacy device-trees: src_clk and HCLK use the same 2597e5e8b224SAngeloGioacchino Del Regno * bit to control gating but they are parented to a different mux, 2598e5e8b224SAngeloGioacchino Del Regno * hence if our intention is to gate only the source, required 2599e5e8b224SAngeloGioacchino Del Regno * during a clk mode switch to avoid hw hangs, we need to gate 2600e5e8b224SAngeloGioacchino Del Regno * its parent (specified as a different clock only on new DTs). 2601e5e8b224SAngeloGioacchino Del Regno */ 2602e5e8b224SAngeloGioacchino Del Regno if (!host->src_clk_cg) { 2603e5e8b224SAngeloGioacchino Del Regno host->src_clk_cg = clk_get_parent(host->src_clk); 2604e5e8b224SAngeloGioacchino Del Regno if (IS_ERR(host->src_clk_cg)) 2605e5e8b224SAngeloGioacchino Del Regno return PTR_ERR(host->src_clk_cg); 2606e5e8b224SAngeloGioacchino Del Regno } 2607e5e8b224SAngeloGioacchino Del Regno 2608c61bfb1cSGaosheng Cui /* If present, always enable for this clock gate */ 2609c61bfb1cSGaosheng Cui host->sys_clk_cg = devm_clk_get_optional_enabled(&pdev->dev, "sys_cg"); 2610f5eccd94SWenbin Mei if (IS_ERR(host->sys_clk_cg)) 2611f5eccd94SWenbin Mei host->sys_clk_cg = NULL; 2612f5eccd94SWenbin Mei 2613f5eccd94SWenbin Mei host->bulk_clks[0].id = "pclk_cg"; 2614f5eccd94SWenbin Mei host->bulk_clks[1].id = "axi_cg"; 2615f5eccd94SWenbin Mei host->bulk_clks[2].id = "ahb_cg"; 2616f5eccd94SWenbin Mei ret = devm_clk_bulk_get_optional(&pdev->dev, MSDC_NR_CLOCKS, 2617f5eccd94SWenbin Mei host->bulk_clks); 2618f5eccd94SWenbin Mei if (ret) { 2619f5eccd94SWenbin Mei dev_err(&pdev->dev, "Cannot get pclk/axi/ahb clock gates\n"); 2620f5eccd94SWenbin Mei return ret; 2621f5eccd94SWenbin Mei } 2622f5eccd94SWenbin Mei 2623f5eccd94SWenbin Mei return 0; 2624f5eccd94SWenbin Mei } 2625f5eccd94SWenbin Mei 262620848903SChaotian Jing static int msdc_drv_probe(struct platform_device *pdev) 262720848903SChaotian Jing { 262820848903SChaotian Jing struct mmc_host *mmc; 262920848903SChaotian Jing struct msdc_host *host; 263020848903SChaotian Jing struct resource *res; 263120848903SChaotian Jing int ret; 263220848903SChaotian Jing 263320848903SChaotian Jing if (!pdev->dev.of_node) { 263420848903SChaotian Jing dev_err(&pdev->dev, "No DT found\n"); 263520848903SChaotian Jing return -EINVAL; 263620848903SChaotian Jing } 2637762d491aSChaotian Jing 263820848903SChaotian Jing /* Allocate MMC host for this device */ 263920848903SChaotian Jing mmc = mmc_alloc_host(sizeof(struct msdc_host), &pdev->dev); 264020848903SChaotian Jing if (!mmc) 264120848903SChaotian Jing return -ENOMEM; 264220848903SChaotian Jing 264320848903SChaotian Jing host = mmc_priv(mmc); 264420848903SChaotian Jing ret = mmc_of_parse(mmc); 264520848903SChaotian Jing if (ret) 264620848903SChaotian Jing goto host_free; 264720848903SChaotian Jing 2648bc068d38SYangtao Li host->base = devm_platform_ioremap_resource(pdev, 0); 264920848903SChaotian Jing if (IS_ERR(host->base)) { 265020848903SChaotian Jing ret = PTR_ERR(host->base); 265120848903SChaotian Jing goto host_free; 265220848903SChaotian Jing } 265320848903SChaotian Jing 2654a2e6d1f6SChaotian Jing res = platform_get_resource(pdev, IORESOURCE_MEM, 1); 2655b65be635SFabien Parent if (res) { 2656a2e6d1f6SChaotian Jing host->top_base = devm_ioremap_resource(&pdev->dev, res); 2657a2e6d1f6SChaotian Jing if (IS_ERR(host->top_base)) 2658a2e6d1f6SChaotian Jing host->top_base = NULL; 2659b65be635SFabien Parent } 2660a2e6d1f6SChaotian Jing 266120848903SChaotian Jing ret = mmc_regulator_get_supply(mmc); 26622f98ef63SWolfram Sang if (ret) 266320848903SChaotian Jing goto host_free; 266420848903SChaotian Jing 2665f5eccd94SWenbin Mei ret = msdc_of_clock_parse(pdev, host); 2666f5eccd94SWenbin Mei if (ret) 266720848903SChaotian Jing goto host_free; 26683c1a8844SChaotian Jing 2669855d388dSWenbin Mei host->reset = devm_reset_control_get_optional_exclusive(&pdev->dev, 2670855d388dSWenbin Mei "hrst"); 2671bbba85faSZheng Liang if (IS_ERR(host->reset)) { 2672bbba85faSZheng Liang ret = PTR_ERR(host->reset); 2673bbba85faSZheng Liang goto host_free; 2674bbba85faSZheng Liang } 2675855d388dSWenbin Mei 26767b438d03SMengqi Zhang /* only eMMC has crypto property */ 26777b438d03SMengqi Zhang if (!(mmc->caps2 & MMC_CAP2_NO_MMC)) { 26787b438d03SMengqi Zhang host->crypto_clk = devm_clk_get_optional(&pdev->dev, "crypto"); 26797b438d03SMengqi Zhang if (IS_ERR(host->crypto_clk)) 26807b438d03SMengqi Zhang host->crypto_clk = NULL; 26817b438d03SMengqi Zhang else 26827b438d03SMengqi Zhang mmc->caps2 |= MMC_CAP2_CRYPTO; 26837b438d03SMengqi Zhang } 26847b438d03SMengqi Zhang 268520848903SChaotian Jing host->irq = platform_get_irq(pdev, 0); 268620848903SChaotian Jing if (host->irq < 0) { 268720848903SChaotian Jing ret = -EINVAL; 268820848903SChaotian Jing goto host_free; 268920848903SChaotian Jing } 269020848903SChaotian Jing 269120848903SChaotian Jing host->pinctrl = devm_pinctrl_get(&pdev->dev); 269220848903SChaotian Jing if (IS_ERR(host->pinctrl)) { 269320848903SChaotian Jing ret = PTR_ERR(host->pinctrl); 269420848903SChaotian Jing dev_err(&pdev->dev, "Cannot find pinctrl!\n"); 269520848903SChaotian Jing goto host_free; 269620848903SChaotian Jing } 269720848903SChaotian Jing 269820848903SChaotian Jing host->pins_default = pinctrl_lookup_state(host->pinctrl, "default"); 269920848903SChaotian Jing if (IS_ERR(host->pins_default)) { 270020848903SChaotian Jing ret = PTR_ERR(host->pins_default); 270120848903SChaotian Jing dev_err(&pdev->dev, "Cannot find pinctrl default!\n"); 270220848903SChaotian Jing goto host_free; 270320848903SChaotian Jing } 270420848903SChaotian Jing 270520848903SChaotian Jing host->pins_uhs = pinctrl_lookup_state(host->pinctrl, "state_uhs"); 270620848903SChaotian Jing if (IS_ERR(host->pins_uhs)) { 270720848903SChaotian Jing ret = PTR_ERR(host->pins_uhs); 270820848903SChaotian Jing dev_err(&pdev->dev, "Cannot find pinctrl uhs!\n"); 270920848903SChaotian Jing goto host_free; 271020848903SChaotian Jing } 271120848903SChaotian Jing 2712527f36f5SAxe Yang /* Support for SDIO eint irq ? */ 2713527f36f5SAxe Yang if ((mmc->pm_caps & MMC_PM_WAKE_SDIO_IRQ) && (mmc->pm_caps & MMC_PM_KEEP_POWER)) { 2714527f36f5SAxe Yang host->eint_irq = platform_get_irq_byname(pdev, "sdio_wakeup"); 2715527f36f5SAxe Yang if (host->eint_irq > 0) { 2716527f36f5SAxe Yang host->pins_eint = pinctrl_lookup_state(host->pinctrl, "state_eint"); 2717527f36f5SAxe Yang if (IS_ERR(host->pins_eint)) { 2718527f36f5SAxe Yang dev_err(&pdev->dev, "Cannot find pinctrl eint!\n"); 2719527f36f5SAxe Yang host->pins_eint = NULL; 2720527f36f5SAxe Yang } else { 2721527f36f5SAxe Yang device_init_wakeup(&pdev->dev, true); 2722527f36f5SAxe Yang } 2723527f36f5SAxe Yang } 2724527f36f5SAxe Yang } 2725527f36f5SAxe Yang 27261ede5cb8Syong mao msdc_of_property_parse(pdev, host); 27276397b7f5SChaotian Jing 272820848903SChaotian Jing host->dev = &pdev->dev; 2729909b3456SRyder Lee host->dev_comp = of_device_get_match_data(&pdev->dev); 273020848903SChaotian Jing host->src_clk_freq = clk_get_rate(host->src_clk); 273120848903SChaotian Jing /* Set host parameters to mmc */ 273220848903SChaotian Jing mmc->ops = &mt_msdc_ops; 2733762d491aSChaotian Jing if (host->dev_comp->clk_div_bits == 8) 273440ceda09Syong mao mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 255); 2735762d491aSChaotian Jing else 2736762d491aSChaotian Jing mmc->f_min = DIV_ROUND_UP(host->src_clk_freq, 4 * 4095); 273720848903SChaotian Jing 2738d087bde5SNeilBrown if (!(mmc->caps & MMC_CAP_NONREMOVABLE) && 2739d087bde5SNeilBrown !mmc_can_gpio_cd(mmc) && 2740d087bde5SNeilBrown host->dev_comp->use_internal_cd) { 2741d087bde5SNeilBrown /* 2742d087bde5SNeilBrown * Is removable but no GPIO declared, so 2743d087bde5SNeilBrown * use internal functionality. 2744d087bde5SNeilBrown */ 2745d087bde5SNeilBrown host->internal_cd = true; 2746d087bde5SNeilBrown } 2747d087bde5SNeilBrown 27485215b2e9Sjjian zhou if (mmc->caps & MMC_CAP_SDIO_IRQ) 27495215b2e9Sjjian zhou mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD; 27505215b2e9Sjjian zhou 27511be64c79SUlf Hansson mmc->caps |= MMC_CAP_CMD23; 275288bd652bSChun-Hung Wu if (host->cqhci) 275388bd652bSChun-Hung Wu mmc->caps2 |= MMC_CAP2_CQE | MMC_CAP2_CQE_DCMD; 275420848903SChaotian Jing /* MMC core transfer sizes tunable parameters */ 275520848903SChaotian Jing mmc->max_segs = MAX_BD_NUM; 27566ef042bdSChaotian Jing if (host->dev_comp->support_64g) 27576ef042bdSChaotian Jing mmc->max_seg_size = BDMA_DESC_BUFLEN_EXT; 27586ef042bdSChaotian Jing else 275920848903SChaotian Jing mmc->max_seg_size = BDMA_DESC_BUFLEN; 276020848903SChaotian Jing mmc->max_blk_size = 2048; 276120848903SChaotian Jing mmc->max_req_size = 512 * 1024; 276220848903SChaotian Jing mmc->max_blk_count = mmc->max_req_size / 512; 27632a9bde19SChaotian Jing if (host->dev_comp->support_64g) 27642a9bde19SChaotian Jing host->dma_mask = DMA_BIT_MASK(36); 27652a9bde19SChaotian Jing else 276620848903SChaotian Jing host->dma_mask = DMA_BIT_MASK(32); 276720848903SChaotian Jing mmc_dev(mmc)->dma_mask = &host->dma_mask; 276820848903SChaotian Jing 2769e8a1ff65SWenbin Mei host->timeout_clks = 3 * 1048576; 2770e8a1ff65SWenbin Mei host->dma.gpd = dma_alloc_coherent(&pdev->dev, 2771e8a1ff65SWenbin Mei 2 * sizeof(struct mt_gpdma_desc), 2772e8a1ff65SWenbin Mei &host->dma.gpd_addr, GFP_KERNEL); 2773e8a1ff65SWenbin Mei host->dma.bd = dma_alloc_coherent(&pdev->dev, 2774e8a1ff65SWenbin Mei MAX_BD_NUM * sizeof(struct mt_bdma_desc), 2775e8a1ff65SWenbin Mei &host->dma.bd_addr, GFP_KERNEL); 2776e8a1ff65SWenbin Mei if (!host->dma.gpd || !host->dma.bd) { 2777e8a1ff65SWenbin Mei ret = -ENOMEM; 2778e8a1ff65SWenbin Mei goto release_mem; 2779e8a1ff65SWenbin Mei } 2780e8a1ff65SWenbin Mei msdc_init_gpd_bd(host, &host->dma); 2781e8a1ff65SWenbin Mei INIT_DELAYED_WORK(&host->req_timeout, msdc_request_timeout); 2782e8a1ff65SWenbin Mei spin_lock_init(&host->lock); 2783e8a1ff65SWenbin Mei 2784e8a1ff65SWenbin Mei platform_set_drvdata(pdev, mmc); 2785ffaea6ebSAngeloGioacchino Del Regno ret = msdc_ungate_clock(host); 2786ffaea6ebSAngeloGioacchino Del Regno if (ret) { 2787ffaea6ebSAngeloGioacchino Del Regno dev_err(&pdev->dev, "Cannot ungate clocks!\n"); 2788ffaea6ebSAngeloGioacchino Del Regno goto release_mem; 2789ffaea6ebSAngeloGioacchino Del Regno } 2790e8a1ff65SWenbin Mei msdc_init_hw(host); 2791e8a1ff65SWenbin Mei 279288bd652bSChun-Hung Wu if (mmc->caps2 & MMC_CAP2_CQE) { 27930caf60c4SAmey Narkhede host->cq_host = devm_kzalloc(mmc->parent, 279488bd652bSChun-Hung Wu sizeof(*host->cq_host), 279588bd652bSChun-Hung Wu GFP_KERNEL); 279688bd652bSChun-Hung Wu if (!host->cq_host) { 279788bd652bSChun-Hung Wu ret = -ENOMEM; 279888bd652bSChun-Hung Wu goto host_free; 279988bd652bSChun-Hung Wu } 280088bd652bSChun-Hung Wu host->cq_host->caps |= CQHCI_TASK_DESC_SZ_128; 280188bd652bSChun-Hung Wu host->cq_host->mmio = host->base + 0x800; 280288bd652bSChun-Hung Wu host->cq_host->ops = &msdc_cmdq_ops; 280388bd652bSChun-Hung Wu ret = cqhci_init(host->cq_host, mmc, true); 280488bd652bSChun-Hung Wu if (ret) 280588bd652bSChun-Hung Wu goto host_free; 280688bd652bSChun-Hung Wu mmc->max_segs = 128; 280788bd652bSChun-Hung Wu /* cqhci 16bit length */ 280888bd652bSChun-Hung Wu /* 0 size, means 65536 so we don't have to -1 here */ 280988bd652bSChun-Hung Wu mmc->max_seg_size = 64 * 1024; 281088bd652bSChun-Hung Wu } 281188bd652bSChun-Hung Wu 281220848903SChaotian Jing ret = devm_request_irq(&pdev->dev, host->irq, msdc_irq, 281342edb0d5SNeilBrown IRQF_TRIGGER_NONE, pdev->name, host); 281420848903SChaotian Jing if (ret) 281520848903SChaotian Jing goto release; 281620848903SChaotian Jing 28174b8a43e9SChaotian Jing pm_runtime_set_active(host->dev); 28184b8a43e9SChaotian Jing pm_runtime_set_autosuspend_delay(host->dev, MTK_MMC_AUTOSUSPEND_DELAY); 28194b8a43e9SChaotian Jing pm_runtime_use_autosuspend(host->dev); 28204b8a43e9SChaotian Jing pm_runtime_enable(host->dev); 282120848903SChaotian Jing ret = mmc_add_host(mmc); 28224b8a43e9SChaotian Jing 282320848903SChaotian Jing if (ret) 28244b8a43e9SChaotian Jing goto end; 282520848903SChaotian Jing 282620848903SChaotian Jing return 0; 28274b8a43e9SChaotian Jing end: 28284b8a43e9SChaotian Jing pm_runtime_disable(host->dev); 282920848903SChaotian Jing release: 283020848903SChaotian Jing platform_set_drvdata(pdev, NULL); 283120848903SChaotian Jing msdc_deinit_hw(host); 283220848903SChaotian Jing msdc_gate_clock(host); 283320848903SChaotian Jing release_mem: 283420848903SChaotian Jing if (host->dma.gpd) 283520848903SChaotian Jing dma_free_coherent(&pdev->dev, 283662b0d27aSChaotian Jing 2 * sizeof(struct mt_gpdma_desc), 283720848903SChaotian Jing host->dma.gpd, host->dma.gpd_addr); 283820848903SChaotian Jing if (host->dma.bd) 283920848903SChaotian Jing dma_free_coherent(&pdev->dev, 284020848903SChaotian Jing MAX_BD_NUM * sizeof(struct mt_bdma_desc), 284120848903SChaotian Jing host->dma.bd, host->dma.bd_addr); 284220848903SChaotian Jing host_free: 284320848903SChaotian Jing mmc_free_host(mmc); 284420848903SChaotian Jing 284520848903SChaotian Jing return ret; 284620848903SChaotian Jing } 284720848903SChaotian Jing 284820848903SChaotian Jing static int msdc_drv_remove(struct platform_device *pdev) 284920848903SChaotian Jing { 285020848903SChaotian Jing struct mmc_host *mmc; 285120848903SChaotian Jing struct msdc_host *host; 285220848903SChaotian Jing 285320848903SChaotian Jing mmc = platform_get_drvdata(pdev); 285420848903SChaotian Jing host = mmc_priv(mmc); 285520848903SChaotian Jing 28564b8a43e9SChaotian Jing pm_runtime_get_sync(host->dev); 28574b8a43e9SChaotian Jing 285820848903SChaotian Jing platform_set_drvdata(pdev, NULL); 28590caf60c4SAmey Narkhede mmc_remove_host(mmc); 286020848903SChaotian Jing msdc_deinit_hw(host); 286120848903SChaotian Jing msdc_gate_clock(host); 286220848903SChaotian Jing 28634b8a43e9SChaotian Jing pm_runtime_disable(host->dev); 28644b8a43e9SChaotian Jing pm_runtime_put_noidle(host->dev); 286520848903SChaotian Jing dma_free_coherent(&pdev->dev, 286616f2e0c6SPhong LE 2 * sizeof(struct mt_gpdma_desc), 286720848903SChaotian Jing host->dma.gpd, host->dma.gpd_addr); 286820848903SChaotian Jing dma_free_coherent(&pdev->dev, MAX_BD_NUM * sizeof(struct mt_bdma_desc), 286920848903SChaotian Jing host->dma.bd, host->dma.bd_addr); 287020848903SChaotian Jing 28710caf60c4SAmey Narkhede mmc_free_host(mmc); 287220848903SChaotian Jing 287320848903SChaotian Jing return 0; 287420848903SChaotian Jing } 287520848903SChaotian Jing 28764b8a43e9SChaotian Jing static void msdc_save_reg(struct msdc_host *host) 28774b8a43e9SChaotian Jing { 287839add252SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 287939add252SChaotian Jing 28804b8a43e9SChaotian Jing host->save_para.msdc_cfg = readl(host->base + MSDC_CFG); 28814b8a43e9SChaotian Jing host->save_para.iocon = readl(host->base + MSDC_IOCON); 28824b8a43e9SChaotian Jing host->save_para.sdc_cfg = readl(host->base + SDC_CFG); 28834b8a43e9SChaotian Jing host->save_para.patch_bit0 = readl(host->base + MSDC_PATCH_BIT); 28844b8a43e9SChaotian Jing host->save_para.patch_bit1 = readl(host->base + MSDC_PATCH_BIT1); 28852fea5819SChaotian Jing host->save_para.patch_bit2 = readl(host->base + MSDC_PATCH_BIT2); 28866397b7f5SChaotian Jing host->save_para.pad_ds_tune = readl(host->base + PAD_DS_TUNE); 28871ede5cb8Syong mao host->save_para.pad_cmd_tune = readl(host->base + PAD_CMD_TUNE); 28886397b7f5SChaotian Jing host->save_para.emmc50_cfg0 = readl(host->base + EMMC50_CFG0); 2889c8609b22SChaotian Jing host->save_para.emmc50_cfg3 = readl(host->base + EMMC50_CFG3); 2890d9dcbfc8SChaotian Jing host->save_para.sdc_fifo_cfg = readl(host->base + SDC_FIFO_CFG); 2891a2e6d1f6SChaotian Jing if (host->top_base) { 2892a2e6d1f6SChaotian Jing host->save_para.emmc_top_control = 2893a2e6d1f6SChaotian Jing readl(host->top_base + EMMC_TOP_CONTROL); 2894a2e6d1f6SChaotian Jing host->save_para.emmc_top_cmd = 2895a2e6d1f6SChaotian Jing readl(host->top_base + EMMC_TOP_CMD); 2896a2e6d1f6SChaotian Jing host->save_para.emmc50_pad_ds_tune = 2897a2e6d1f6SChaotian Jing readl(host->top_base + EMMC50_PAD_DS_TUNE); 2898a2e6d1f6SChaotian Jing } else { 2899a2e6d1f6SChaotian Jing host->save_para.pad_tune = readl(host->base + tune_reg); 2900a2e6d1f6SChaotian Jing } 29014b8a43e9SChaotian Jing } 29024b8a43e9SChaotian Jing 29034b8a43e9SChaotian Jing static void msdc_restore_reg(struct msdc_host *host) 29044b8a43e9SChaotian Jing { 29050caf60c4SAmey Narkhede struct mmc_host *mmc = mmc_from_priv(host); 290639add252SChaotian Jing u32 tune_reg = host->dev_comp->pad_tune_reg; 290739add252SChaotian Jing 29084b8a43e9SChaotian Jing writel(host->save_para.msdc_cfg, host->base + MSDC_CFG); 29094b8a43e9SChaotian Jing writel(host->save_para.iocon, host->base + MSDC_IOCON); 29104b8a43e9SChaotian Jing writel(host->save_para.sdc_cfg, host->base + SDC_CFG); 29114b8a43e9SChaotian Jing writel(host->save_para.patch_bit0, host->base + MSDC_PATCH_BIT); 29124b8a43e9SChaotian Jing writel(host->save_para.patch_bit1, host->base + MSDC_PATCH_BIT1); 29132fea5819SChaotian Jing writel(host->save_para.patch_bit2, host->base + MSDC_PATCH_BIT2); 29146397b7f5SChaotian Jing writel(host->save_para.pad_ds_tune, host->base + PAD_DS_TUNE); 29151ede5cb8Syong mao writel(host->save_para.pad_cmd_tune, host->base + PAD_CMD_TUNE); 29166397b7f5SChaotian Jing writel(host->save_para.emmc50_cfg0, host->base + EMMC50_CFG0); 2917c8609b22SChaotian Jing writel(host->save_para.emmc50_cfg3, host->base + EMMC50_CFG3); 2918d9dcbfc8SChaotian Jing writel(host->save_para.sdc_fifo_cfg, host->base + SDC_FIFO_CFG); 2919a2e6d1f6SChaotian Jing if (host->top_base) { 2920a2e6d1f6SChaotian Jing writel(host->save_para.emmc_top_control, 2921a2e6d1f6SChaotian Jing host->top_base + EMMC_TOP_CONTROL); 2922a2e6d1f6SChaotian Jing writel(host->save_para.emmc_top_cmd, 2923a2e6d1f6SChaotian Jing host->top_base + EMMC_TOP_CMD); 2924a2e6d1f6SChaotian Jing writel(host->save_para.emmc50_pad_ds_tune, 2925a2e6d1f6SChaotian Jing host->top_base + EMMC50_PAD_DS_TUNE); 2926a2e6d1f6SChaotian Jing } else { 2927a2e6d1f6SChaotian Jing writel(host->save_para.pad_tune, host->base + tune_reg); 2928a2e6d1f6SChaotian Jing } 29291c81d69dSUlf Hansson 29300caf60c4SAmey Narkhede if (sdio_irq_claimed(mmc)) 29311c81d69dSUlf Hansson __msdc_enable_sdio_irq(host, 1); 29324b8a43e9SChaotian Jing } 29334b8a43e9SChaotian Jing 2934c0d638a0SArnd Bergmann static int __maybe_unused msdc_runtime_suspend(struct device *dev) 29354b8a43e9SChaotian Jing { 29364b8a43e9SChaotian Jing struct mmc_host *mmc = dev_get_drvdata(dev); 29374b8a43e9SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 29384b8a43e9SChaotian Jing 29394b8a43e9SChaotian Jing msdc_save_reg(host); 2940527f36f5SAxe Yang 2941527f36f5SAxe Yang if (sdio_irq_claimed(mmc)) { 2942527f36f5SAxe Yang if (host->pins_eint) { 2943527f36f5SAxe Yang disable_irq(host->irq); 2944527f36f5SAxe Yang pinctrl_select_state(host->pinctrl, host->pins_eint); 2945527f36f5SAxe Yang } 2946527f36f5SAxe Yang 2947527f36f5SAxe Yang __msdc_enable_sdio_irq(host, 0); 2948527f36f5SAxe Yang } 29494b8a43e9SChaotian Jing msdc_gate_clock(host); 29504b8a43e9SChaotian Jing return 0; 29514b8a43e9SChaotian Jing } 29524b8a43e9SChaotian Jing 2953c0d638a0SArnd Bergmann static int __maybe_unused msdc_runtime_resume(struct device *dev) 29544b8a43e9SChaotian Jing { 29554b8a43e9SChaotian Jing struct mmc_host *mmc = dev_get_drvdata(dev); 29564b8a43e9SChaotian Jing struct msdc_host *host = mmc_priv(mmc); 2957ffaea6ebSAngeloGioacchino Del Regno int ret; 29584b8a43e9SChaotian Jing 2959ffaea6ebSAngeloGioacchino Del Regno ret = msdc_ungate_clock(host); 2960ffaea6ebSAngeloGioacchino Del Regno if (ret) 2961ffaea6ebSAngeloGioacchino Del Regno return ret; 2962ffaea6ebSAngeloGioacchino Del Regno 29634b8a43e9SChaotian Jing msdc_restore_reg(host); 2964527f36f5SAxe Yang 2965527f36f5SAxe Yang if (sdio_irq_claimed(mmc) && host->pins_eint) { 2966527f36f5SAxe Yang pinctrl_select_state(host->pinctrl, host->pins_uhs); 2967527f36f5SAxe Yang enable_irq(host->irq); 2968527f36f5SAxe Yang } 29694b8a43e9SChaotian Jing return 0; 29704b8a43e9SChaotian Jing } 2971c0a2074aSWenbin Mei 2972c0d638a0SArnd Bergmann static int __maybe_unused msdc_suspend(struct device *dev) 2973c0a2074aSWenbin Mei { 2974c0a2074aSWenbin Mei struct mmc_host *mmc = dev_get_drvdata(dev); 2975527f36f5SAxe Yang struct msdc_host *host = mmc_priv(mmc); 2976c0a2074aSWenbin Mei int ret; 2977cc5d1692SWenbin Mei u32 val; 2978c0a2074aSWenbin Mei 2979c0a2074aSWenbin Mei if (mmc->caps2 & MMC_CAP2_CQE) { 2980c0a2074aSWenbin Mei ret = cqhci_suspend(mmc); 2981c0a2074aSWenbin Mei if (ret) 2982c0a2074aSWenbin Mei return ret; 2983cc5d1692SWenbin Mei val = readl(host->base + MSDC_INT); 2984cc5d1692SWenbin Mei writel(val, host->base + MSDC_INT); 2985c0a2074aSWenbin Mei } 2986c0a2074aSWenbin Mei 2987527f36f5SAxe Yang /* 2988527f36f5SAxe Yang * Bump up runtime PM usage counter otherwise dev->power.needs_force_resume will 2989527f36f5SAxe Yang * not be marked as 1, pm_runtime_force_resume() will go out directly. 2990527f36f5SAxe Yang */ 2991527f36f5SAxe Yang if (sdio_irq_claimed(mmc) && host->pins_eint) 2992527f36f5SAxe Yang pm_runtime_get_noresume(dev); 2993527f36f5SAxe Yang 2994c0a2074aSWenbin Mei return pm_runtime_force_suspend(dev); 2995c0a2074aSWenbin Mei } 2996c0a2074aSWenbin Mei 2997c0d638a0SArnd Bergmann static int __maybe_unused msdc_resume(struct device *dev) 2998c0a2074aSWenbin Mei { 2999527f36f5SAxe Yang struct mmc_host *mmc = dev_get_drvdata(dev); 3000527f36f5SAxe Yang struct msdc_host *host = mmc_priv(mmc); 3001527f36f5SAxe Yang 3002527f36f5SAxe Yang if (sdio_irq_claimed(mmc) && host->pins_eint) 3003527f36f5SAxe Yang pm_runtime_put_noidle(dev); 3004527f36f5SAxe Yang 3005c0a2074aSWenbin Mei return pm_runtime_force_resume(dev); 3006c0a2074aSWenbin Mei } 30074b8a43e9SChaotian Jing 30084b8a43e9SChaotian Jing static const struct dev_pm_ops msdc_dev_pm_ops = { 3009c0a2074aSWenbin Mei SET_SYSTEM_SLEEP_PM_OPS(msdc_suspend, msdc_resume) 30104b8a43e9SChaotian Jing SET_RUNTIME_PM_OPS(msdc_runtime_suspend, msdc_runtime_resume, NULL) 30114b8a43e9SChaotian Jing }; 30124b8a43e9SChaotian Jing 301320848903SChaotian Jing static struct platform_driver mt_msdc_driver = { 301420848903SChaotian Jing .probe = msdc_drv_probe, 301520848903SChaotian Jing .remove = msdc_drv_remove, 301620848903SChaotian Jing .driver = { 301720848903SChaotian Jing .name = "mtk-msdc", 301821b2cec6SDouglas Anderson .probe_type = PROBE_PREFER_ASYNCHRONOUS, 301920848903SChaotian Jing .of_match_table = msdc_of_ids, 30204b8a43e9SChaotian Jing .pm = &msdc_dev_pm_ops, 302120848903SChaotian Jing }, 302220848903SChaotian Jing }; 302320848903SChaotian Jing 302420848903SChaotian Jing module_platform_driver(mt_msdc_driver); 302520848903SChaotian Jing MODULE_LICENSE("GPL v2"); 302620848903SChaotian Jing MODULE_DESCRIPTION("MediaTek SD/MMC Card Driver"); 3027