18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (C) 2019 MediaTek Inc. 48c2ecf20Sopenharmony_ci * Author: Stanley Chu <stanley.chu@mediatek.com> 58c2ecf20Sopenharmony_ci */ 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci#include <linux/clk.h> 88c2ecf20Sopenharmony_ci#include <linux/delay.h> 98c2ecf20Sopenharmony_ci#include <linux/io.h> 108c2ecf20Sopenharmony_ci#include <linux/module.h> 118c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 128c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 138c2ecf20Sopenharmony_ci 148c2ecf20Sopenharmony_ci/* mphy register and offsets */ 158c2ecf20Sopenharmony_ci#define MP_GLB_DIG_8C 0x008C 168c2ecf20Sopenharmony_ci#define FRC_PLL_ISO_EN BIT(8) 178c2ecf20Sopenharmony_ci#define PLL_ISO_EN BIT(9) 188c2ecf20Sopenharmony_ci#define FRC_FRC_PWR_ON BIT(10) 198c2ecf20Sopenharmony_ci#define PLL_PWR_ON BIT(11) 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#define MP_LN_DIG_RX_9C 0xA09C 228c2ecf20Sopenharmony_ci#define FSM_DIFZ_FRC BIT(18) 238c2ecf20Sopenharmony_ci 248c2ecf20Sopenharmony_ci#define MP_LN_DIG_RX_AC 0xA0AC 258c2ecf20Sopenharmony_ci#define FRC_RX_SQ_EN BIT(0) 268c2ecf20Sopenharmony_ci#define RX_SQ_EN BIT(1) 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#define MP_LN_RX_44 0xB044 298c2ecf20Sopenharmony_ci#define FRC_CDR_PWR_ON BIT(17) 308c2ecf20Sopenharmony_ci#define CDR_PWR_ON BIT(18) 318c2ecf20Sopenharmony_ci#define FRC_CDR_ISO_EN BIT(19) 328c2ecf20Sopenharmony_ci#define CDR_ISO_EN BIT(20) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_cistruct ufs_mtk_phy { 358c2ecf20Sopenharmony_ci struct device *dev; 368c2ecf20Sopenharmony_ci void __iomem *mmio; 378c2ecf20Sopenharmony_ci struct clk *mp_clk; 388c2ecf20Sopenharmony_ci struct clk *unipro_clk; 398c2ecf20Sopenharmony_ci}; 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_cistatic inline u32 mphy_readl(struct ufs_mtk_phy *phy, u32 reg) 428c2ecf20Sopenharmony_ci{ 438c2ecf20Sopenharmony_ci return readl(phy->mmio + reg); 448c2ecf20Sopenharmony_ci} 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_cistatic inline void mphy_writel(struct ufs_mtk_phy *phy, u32 val, u32 reg) 478c2ecf20Sopenharmony_ci{ 488c2ecf20Sopenharmony_ci writel(val, phy->mmio + reg); 498c2ecf20Sopenharmony_ci} 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_cistatic void mphy_set_bit(struct ufs_mtk_phy *phy, u32 reg, u32 bit) 528c2ecf20Sopenharmony_ci{ 538c2ecf20Sopenharmony_ci u32 val; 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci val = mphy_readl(phy, reg); 568c2ecf20Sopenharmony_ci val |= bit; 578c2ecf20Sopenharmony_ci mphy_writel(phy, val, reg); 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_cistatic void mphy_clr_bit(struct ufs_mtk_phy *phy, u32 reg, u32 bit) 618c2ecf20Sopenharmony_ci{ 628c2ecf20Sopenharmony_ci u32 val; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci val = mphy_readl(phy, reg); 658c2ecf20Sopenharmony_ci val &= ~bit; 668c2ecf20Sopenharmony_ci mphy_writel(phy, val, reg); 678c2ecf20Sopenharmony_ci} 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistatic struct ufs_mtk_phy *get_ufs_mtk_phy(struct phy *generic_phy) 708c2ecf20Sopenharmony_ci{ 718c2ecf20Sopenharmony_ci return (struct ufs_mtk_phy *)phy_get_drvdata(generic_phy); 728c2ecf20Sopenharmony_ci} 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_cistatic int ufs_mtk_phy_clk_init(struct ufs_mtk_phy *phy) 758c2ecf20Sopenharmony_ci{ 768c2ecf20Sopenharmony_ci struct device *dev = phy->dev; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci phy->unipro_clk = devm_clk_get(dev, "unipro"); 798c2ecf20Sopenharmony_ci if (IS_ERR(phy->unipro_clk)) { 808c2ecf20Sopenharmony_ci dev_err(dev, "failed to get clock: unipro"); 818c2ecf20Sopenharmony_ci return PTR_ERR(phy->unipro_clk); 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci phy->mp_clk = devm_clk_get(dev, "mp"); 858c2ecf20Sopenharmony_ci if (IS_ERR(phy->mp_clk)) { 868c2ecf20Sopenharmony_ci dev_err(dev, "failed to get clock: mp"); 878c2ecf20Sopenharmony_ci return PTR_ERR(phy->mp_clk); 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci return 0; 918c2ecf20Sopenharmony_ci} 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_cistatic void ufs_mtk_phy_set_active(struct ufs_mtk_phy *phy) 948c2ecf20Sopenharmony_ci{ 958c2ecf20Sopenharmony_ci /* release DA_MP_PLL_PWR_ON */ 968c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_GLB_DIG_8C, PLL_PWR_ON); 978c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_GLB_DIG_8C, FRC_FRC_PWR_ON); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci /* release DA_MP_PLL_ISO_EN */ 1008c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_GLB_DIG_8C, PLL_ISO_EN); 1018c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_GLB_DIG_8C, FRC_PLL_ISO_EN); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* release DA_MP_CDR_PWR_ON */ 1048c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_RX_44, CDR_PWR_ON); 1058c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_RX_44, FRC_CDR_PWR_ON); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* release DA_MP_CDR_ISO_EN */ 1088c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_RX_44, CDR_ISO_EN); 1098c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_RX_44, FRC_CDR_ISO_EN); 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci /* release DA_MP_RX0_SQ_EN */ 1128c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_DIG_RX_AC, RX_SQ_EN); 1138c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_DIG_RX_AC, FRC_RX_SQ_EN); 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci /* delay 1us to wait DIFZ stable */ 1168c2ecf20Sopenharmony_ci udelay(1); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci /* release DIFZ */ 1198c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_DIG_RX_9C, FSM_DIFZ_FRC); 1208c2ecf20Sopenharmony_ci} 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_cistatic void ufs_mtk_phy_set_deep_hibern(struct ufs_mtk_phy *phy) 1238c2ecf20Sopenharmony_ci{ 1248c2ecf20Sopenharmony_ci /* force DIFZ */ 1258c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_DIG_RX_9C, FSM_DIFZ_FRC); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci /* force DA_MP_RX0_SQ_EN */ 1288c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_DIG_RX_AC, FRC_RX_SQ_EN); 1298c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_DIG_RX_AC, RX_SQ_EN); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci /* force DA_MP_CDR_ISO_EN */ 1328c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_RX_44, FRC_CDR_ISO_EN); 1338c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_RX_44, CDR_ISO_EN); 1348c2ecf20Sopenharmony_ci 1358c2ecf20Sopenharmony_ci /* force DA_MP_CDR_PWR_ON */ 1368c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_LN_RX_44, FRC_CDR_PWR_ON); 1378c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_LN_RX_44, CDR_PWR_ON); 1388c2ecf20Sopenharmony_ci 1398c2ecf20Sopenharmony_ci /* force DA_MP_PLL_ISO_EN */ 1408c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_GLB_DIG_8C, FRC_PLL_ISO_EN); 1418c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_GLB_DIG_8C, PLL_ISO_EN); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci /* force DA_MP_PLL_PWR_ON */ 1448c2ecf20Sopenharmony_ci mphy_set_bit(phy, MP_GLB_DIG_8C, FRC_FRC_PWR_ON); 1458c2ecf20Sopenharmony_ci mphy_clr_bit(phy, MP_GLB_DIG_8C, PLL_PWR_ON); 1468c2ecf20Sopenharmony_ci} 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_cistatic int ufs_mtk_phy_power_on(struct phy *generic_phy) 1498c2ecf20Sopenharmony_ci{ 1508c2ecf20Sopenharmony_ci struct ufs_mtk_phy *phy = get_ufs_mtk_phy(generic_phy); 1518c2ecf20Sopenharmony_ci int ret; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci ret = clk_prepare_enable(phy->unipro_clk); 1548c2ecf20Sopenharmony_ci if (ret) { 1558c2ecf20Sopenharmony_ci dev_err(phy->dev, "unipro_clk enable failed %d\n", ret); 1568c2ecf20Sopenharmony_ci goto out; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci ret = clk_prepare_enable(phy->mp_clk); 1608c2ecf20Sopenharmony_ci if (ret) { 1618c2ecf20Sopenharmony_ci dev_err(phy->dev, "mp_clk enable failed %d\n", ret); 1628c2ecf20Sopenharmony_ci goto out_unprepare_unipro_clk; 1638c2ecf20Sopenharmony_ci } 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci ufs_mtk_phy_set_active(phy); 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci return 0; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ciout_unprepare_unipro_clk: 1708c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->unipro_clk); 1718c2ecf20Sopenharmony_ciout: 1728c2ecf20Sopenharmony_ci return ret; 1738c2ecf20Sopenharmony_ci} 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_cistatic int ufs_mtk_phy_power_off(struct phy *generic_phy) 1768c2ecf20Sopenharmony_ci{ 1778c2ecf20Sopenharmony_ci struct ufs_mtk_phy *phy = get_ufs_mtk_phy(generic_phy); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci ufs_mtk_phy_set_deep_hibern(phy); 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->unipro_clk); 1828c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->mp_clk); 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci return 0; 1858c2ecf20Sopenharmony_ci} 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_cistatic const struct phy_ops ufs_mtk_phy_ops = { 1888c2ecf20Sopenharmony_ci .power_on = ufs_mtk_phy_power_on, 1898c2ecf20Sopenharmony_ci .power_off = ufs_mtk_phy_power_off, 1908c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1918c2ecf20Sopenharmony_ci}; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_cistatic int ufs_mtk_phy_probe(struct platform_device *pdev) 1948c2ecf20Sopenharmony_ci{ 1958c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 1968c2ecf20Sopenharmony_ci struct phy *generic_phy; 1978c2ecf20Sopenharmony_ci struct phy_provider *phy_provider; 1988c2ecf20Sopenharmony_ci struct resource *res; 1998c2ecf20Sopenharmony_ci struct ufs_mtk_phy *phy; 2008c2ecf20Sopenharmony_ci int ret; 2018c2ecf20Sopenharmony_ci 2028c2ecf20Sopenharmony_ci phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); 2038c2ecf20Sopenharmony_ci if (!phy) 2048c2ecf20Sopenharmony_ci return -ENOMEM; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2078c2ecf20Sopenharmony_ci phy->mmio = devm_ioremap_resource(dev, res); 2088c2ecf20Sopenharmony_ci if (IS_ERR(phy->mmio)) 2098c2ecf20Sopenharmony_ci return PTR_ERR(phy->mmio); 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci phy->dev = dev; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci ret = ufs_mtk_phy_clk_init(phy); 2148c2ecf20Sopenharmony_ci if (ret) 2158c2ecf20Sopenharmony_ci return ret; 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_ci generic_phy = devm_phy_create(dev, NULL, &ufs_mtk_phy_ops); 2188c2ecf20Sopenharmony_ci if (IS_ERR(generic_phy)) 2198c2ecf20Sopenharmony_ci return PTR_ERR(generic_phy); 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci phy_set_drvdata(generic_phy, phy); 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci return PTR_ERR_OR_ZERO(phy_provider); 2268c2ecf20Sopenharmony_ci} 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_cistatic const struct of_device_id ufs_mtk_phy_of_match[] = { 2298c2ecf20Sopenharmony_ci {.compatible = "mediatek,mt8183-ufsphy"}, 2308c2ecf20Sopenharmony_ci {}, 2318c2ecf20Sopenharmony_ci}; 2328c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ufs_mtk_phy_of_match); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_cistatic struct platform_driver ufs_mtk_phy_driver = { 2358c2ecf20Sopenharmony_ci .probe = ufs_mtk_phy_probe, 2368c2ecf20Sopenharmony_ci .driver = { 2378c2ecf20Sopenharmony_ci .of_match_table = ufs_mtk_phy_of_match, 2388c2ecf20Sopenharmony_ci .name = "ufs_mtk_phy", 2398c2ecf20Sopenharmony_ci }, 2408c2ecf20Sopenharmony_ci}; 2418c2ecf20Sopenharmony_cimodule_platform_driver(ufs_mtk_phy_driver); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Universal Flash Storage (UFS) MediaTek MPHY"); 2448c2ecf20Sopenharmony_ciMODULE_AUTHOR("Stanley Chu <stanley.chu@mediatek.com>"); 2458c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 246