18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Copyright (c) 2014, The Linux Foundation. All rights reserved. 48c2ecf20Sopenharmony_ci */ 58c2ecf20Sopenharmony_ci 68c2ecf20Sopenharmony_ci#include <linux/io.h> 78c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/of.h> 118c2ecf20Sopenharmony_ci#include <linux/time.h> 128c2ecf20Sopenharmony_ci#include <linux/delay.h> 138c2ecf20Sopenharmony_ci#include <linux/clk.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 168c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/* PHY registers */ 198c2ecf20Sopenharmony_ci#define UNIPHY_PLL_REFCLK_CFG 0x000 208c2ecf20Sopenharmony_ci#define UNIPHY_PLL_PWRGEN_CFG 0x014 218c2ecf20Sopenharmony_ci#define UNIPHY_PLL_GLB_CFG 0x020 228c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG0 0x038 238c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG1 0x03C 248c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG2 0x040 258c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG3 0x044 268c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG4 0x048 278c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG0 0x04C 288c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG1 0x050 298c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG2 0x054 308c2ecf20Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG3 0x058 318c2ecf20Sopenharmony_ci#define UNIPHY_PLL_LKDET_CFG0 0x05C 328c2ecf20Sopenharmony_ci#define UNIPHY_PLL_LKDET_CFG1 0x060 338c2ecf20Sopenharmony_ci#define UNIPHY_PLL_LKDET_CFG2 0x064 348c2ecf20Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG0 0x06C 358c2ecf20Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG8 0x08C 368c2ecf20Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG9 0x090 378c2ecf20Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG10 0x094 388c2ecf20Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG11 0x098 398c2ecf20Sopenharmony_ci#define UNIPHY_PLL_STATUS 0x0C0 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define SATA_PHY_SER_CTRL 0x100 428c2ecf20Sopenharmony_ci#define SATA_PHY_TX_DRIV_CTRL0 0x104 438c2ecf20Sopenharmony_ci#define SATA_PHY_TX_DRIV_CTRL1 0x108 448c2ecf20Sopenharmony_ci#define SATA_PHY_TX_IMCAL0 0x11C 458c2ecf20Sopenharmony_ci#define SATA_PHY_TX_IMCAL2 0x124 468c2ecf20Sopenharmony_ci#define SATA_PHY_RX_IMCAL0 0x128 478c2ecf20Sopenharmony_ci#define SATA_PHY_EQUAL 0x13C 488c2ecf20Sopenharmony_ci#define SATA_PHY_OOB_TERM 0x144 498c2ecf20Sopenharmony_ci#define SATA_PHY_CDR_CTRL0 0x148 508c2ecf20Sopenharmony_ci#define SATA_PHY_CDR_CTRL1 0x14C 518c2ecf20Sopenharmony_ci#define SATA_PHY_CDR_CTRL2 0x150 528c2ecf20Sopenharmony_ci#define SATA_PHY_CDR_CTRL3 0x154 538c2ecf20Sopenharmony_ci#define SATA_PHY_PI_CTRL0 0x168 548c2ecf20Sopenharmony_ci#define SATA_PHY_POW_DWN_CTRL0 0x180 558c2ecf20Sopenharmony_ci#define SATA_PHY_POW_DWN_CTRL1 0x184 568c2ecf20Sopenharmony_ci#define SATA_PHY_TX_DATA_CTRL 0x188 578c2ecf20Sopenharmony_ci#define SATA_PHY_ALIGNP 0x1A4 588c2ecf20Sopenharmony_ci#define SATA_PHY_TX_IMCAL_STAT 0x1E4 598c2ecf20Sopenharmony_ci#define SATA_PHY_RX_IMCAL_STAT 0x1E8 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci#define UNIPHY_PLL_LOCK BIT(0) 628c2ecf20Sopenharmony_ci#define SATA_PHY_TX_CAL BIT(0) 638c2ecf20Sopenharmony_ci#define SATA_PHY_RX_CAL BIT(0) 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* default timeout set to 1 sec */ 668c2ecf20Sopenharmony_ci#define TIMEOUT_MS 10000 678c2ecf20Sopenharmony_ci#define DELAY_INTERVAL_US 100 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_cistruct qcom_apq8064_sata_phy { 708c2ecf20Sopenharmony_ci void __iomem *mmio; 718c2ecf20Sopenharmony_ci struct clk *cfg_clk; 728c2ecf20Sopenharmony_ci struct device *dev; 738c2ecf20Sopenharmony_ci}; 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci/* Helper function to do poll and timeout */ 768c2ecf20Sopenharmony_cistatic int poll_timeout(void __iomem *addr, u32 mask) 778c2ecf20Sopenharmony_ci{ 788c2ecf20Sopenharmony_ci u32 val; 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci return readl_relaxed_poll_timeout(addr, val, (val & mask), 818c2ecf20Sopenharmony_ci DELAY_INTERVAL_US, TIMEOUT_MS * 1000); 828c2ecf20Sopenharmony_ci} 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_cistatic int qcom_apq8064_sata_phy_init(struct phy *generic_phy) 858c2ecf20Sopenharmony_ci{ 868c2ecf20Sopenharmony_ci struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy); 878c2ecf20Sopenharmony_ci void __iomem *base = phy->mmio; 888c2ecf20Sopenharmony_ci int ret = 0; 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci /* SATA phy initialization */ 918c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_SER_CTRL); 928c2ecf20Sopenharmony_ci writel_relaxed(0xB1, base + SATA_PHY_POW_DWN_CTRL0); 938c2ecf20Sopenharmony_ci /* Make sure the power down happens before power up */ 948c2ecf20Sopenharmony_ci mb(); 958c2ecf20Sopenharmony_ci usleep_range(10, 60); 968c2ecf20Sopenharmony_ci 978c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0); 988c2ecf20Sopenharmony_ci writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1); 998c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0); 1008c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0); 1018c2ecf20Sopenharmony_ci writel_relaxed(0x02, base + SATA_PHY_TX_IMCAL2); 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci /* Write UNIPHYPLL registers to configure PLL */ 1048c2ecf20Sopenharmony_ci writel_relaxed(0x04, base + UNIPHY_PLL_REFCLK_CFG); 1058c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_PWRGEN_CFG); 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci writel_relaxed(0x0A, base + UNIPHY_PLL_CAL_CFG0); 1088c2ecf20Sopenharmony_ci writel_relaxed(0xF3, base + UNIPHY_PLL_CAL_CFG8); 1098c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + UNIPHY_PLL_CAL_CFG9); 1108c2ecf20Sopenharmony_ci writel_relaxed(0xED, base + UNIPHY_PLL_CAL_CFG10); 1118c2ecf20Sopenharmony_ci writel_relaxed(0x02, base + UNIPHY_PLL_CAL_CFG11); 1128c2ecf20Sopenharmony_ci 1138c2ecf20Sopenharmony_ci writel_relaxed(0x36, base + UNIPHY_PLL_SDM_CFG0); 1148c2ecf20Sopenharmony_ci writel_relaxed(0x0D, base + UNIPHY_PLL_SDM_CFG1); 1158c2ecf20Sopenharmony_ci writel_relaxed(0xA3, base + UNIPHY_PLL_SDM_CFG2); 1168c2ecf20Sopenharmony_ci writel_relaxed(0xF0, base + UNIPHY_PLL_SDM_CFG3); 1178c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_SDM_CFG4); 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci writel_relaxed(0x19, base + UNIPHY_PLL_SSC_CFG0); 1208c2ecf20Sopenharmony_ci writel_relaxed(0xE1, base + UNIPHY_PLL_SSC_CFG1); 1218c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_SSC_CFG2); 1228c2ecf20Sopenharmony_ci writel_relaxed(0x11, base + UNIPHY_PLL_SSC_CFG3); 1238c2ecf20Sopenharmony_ci 1248c2ecf20Sopenharmony_ci writel_relaxed(0x04, base + UNIPHY_PLL_LKDET_CFG0); 1258c2ecf20Sopenharmony_ci writel_relaxed(0xFF, base + UNIPHY_PLL_LKDET_CFG1); 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci writel_relaxed(0x02, base + UNIPHY_PLL_GLB_CFG); 1288c2ecf20Sopenharmony_ci /* make sure global config LDO power down happens before power up */ 1298c2ecf20Sopenharmony_ci mb(); 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci writel_relaxed(0x03, base + UNIPHY_PLL_GLB_CFG); 1328c2ecf20Sopenharmony_ci writel_relaxed(0x05, base + UNIPHY_PLL_LKDET_CFG2); 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci /* PLL Lock wait */ 1358c2ecf20Sopenharmony_ci ret = poll_timeout(base + UNIPHY_PLL_STATUS, UNIPHY_PLL_LOCK); 1368c2ecf20Sopenharmony_ci if (ret) { 1378c2ecf20Sopenharmony_ci dev_err(phy->dev, "poll timeout UNIPHY_PLL_STATUS\n"); 1388c2ecf20Sopenharmony_ci return ret; 1398c2ecf20Sopenharmony_ci } 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci /* TX Calibration */ 1428c2ecf20Sopenharmony_ci ret = poll_timeout(base + SATA_PHY_TX_IMCAL_STAT, SATA_PHY_TX_CAL); 1438c2ecf20Sopenharmony_ci if (ret) { 1448c2ecf20Sopenharmony_ci dev_err(phy->dev, "poll timeout SATA_PHY_TX_IMCAL_STAT\n"); 1458c2ecf20Sopenharmony_ci return ret; 1468c2ecf20Sopenharmony_ci } 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci /* RX Calibration */ 1498c2ecf20Sopenharmony_ci ret = poll_timeout(base + SATA_PHY_RX_IMCAL_STAT, SATA_PHY_RX_CAL); 1508c2ecf20Sopenharmony_ci if (ret) { 1518c2ecf20Sopenharmony_ci dev_err(phy->dev, "poll timeout SATA_PHY_RX_IMCAL_STAT\n"); 1528c2ecf20Sopenharmony_ci return ret; 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci /* SATA phy calibrated succesfully, power up to functional mode */ 1568c2ecf20Sopenharmony_ci writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1); 1578c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0); 1588c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0); 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_POW_DWN_CTRL1); 1618c2ecf20Sopenharmony_ci writel_relaxed(0x59, base + SATA_PHY_CDR_CTRL0); 1628c2ecf20Sopenharmony_ci writel_relaxed(0x04, base + SATA_PHY_CDR_CTRL1); 1638c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL2); 1648c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_PI_CTRL0); 1658c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL3); 1668c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0); 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci writel_relaxed(0x11, base + SATA_PHY_TX_DATA_CTRL); 1698c2ecf20Sopenharmony_ci writel_relaxed(0x43, base + SATA_PHY_ALIGNP); 1708c2ecf20Sopenharmony_ci writel_relaxed(0x04, base + SATA_PHY_OOB_TERM); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_EQUAL); 1738c2ecf20Sopenharmony_ci writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL0); 1748c2ecf20Sopenharmony_ci writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL1); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return 0; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic int qcom_apq8064_sata_phy_exit(struct phy *generic_phy) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy); 1828c2ecf20Sopenharmony_ci void __iomem *base = phy->mmio; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci /* Power down PHY */ 1858c2ecf20Sopenharmony_ci writel_relaxed(0xF8, base + SATA_PHY_POW_DWN_CTRL0); 1868c2ecf20Sopenharmony_ci writel_relaxed(0xFE, base + SATA_PHY_POW_DWN_CTRL1); 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci /* Power down PLL block */ 1898c2ecf20Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_GLB_CFG); 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci return 0; 1928c2ecf20Sopenharmony_ci} 1938c2ecf20Sopenharmony_ci 1948c2ecf20Sopenharmony_cistatic const struct phy_ops qcom_apq8064_sata_phy_ops = { 1958c2ecf20Sopenharmony_ci .init = qcom_apq8064_sata_phy_init, 1968c2ecf20Sopenharmony_ci .exit = qcom_apq8064_sata_phy_exit, 1978c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 1988c2ecf20Sopenharmony_ci}; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_cistatic int qcom_apq8064_sata_phy_probe(struct platform_device *pdev) 2018c2ecf20Sopenharmony_ci{ 2028c2ecf20Sopenharmony_ci struct qcom_apq8064_sata_phy *phy; 2038c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2048c2ecf20Sopenharmony_ci struct resource *res; 2058c2ecf20Sopenharmony_ci struct phy_provider *phy_provider; 2068c2ecf20Sopenharmony_ci struct phy *generic_phy; 2078c2ecf20Sopenharmony_ci int ret; 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_ci phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); 2108c2ecf20Sopenharmony_ci if (!phy) 2118c2ecf20Sopenharmony_ci return -ENOMEM; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 2148c2ecf20Sopenharmony_ci phy->mmio = devm_ioremap_resource(dev, res); 2158c2ecf20Sopenharmony_ci if (IS_ERR(phy->mmio)) 2168c2ecf20Sopenharmony_ci return PTR_ERR(phy->mmio); 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci generic_phy = devm_phy_create(dev, NULL, &qcom_apq8064_sata_phy_ops); 2198c2ecf20Sopenharmony_ci if (IS_ERR(generic_phy)) { 2208c2ecf20Sopenharmony_ci dev_err(dev, "%s: failed to create phy\n", __func__); 2218c2ecf20Sopenharmony_ci return PTR_ERR(generic_phy); 2228c2ecf20Sopenharmony_ci } 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci phy->dev = dev; 2258c2ecf20Sopenharmony_ci phy_set_drvdata(generic_phy, phy); 2268c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, phy); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci phy->cfg_clk = devm_clk_get(dev, "cfg"); 2298c2ecf20Sopenharmony_ci if (IS_ERR(phy->cfg_clk)) { 2308c2ecf20Sopenharmony_ci dev_err(dev, "Failed to get sata cfg clock\n"); 2318c2ecf20Sopenharmony_ci return PTR_ERR(phy->cfg_clk); 2328c2ecf20Sopenharmony_ci } 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci ret = clk_prepare_enable(phy->cfg_clk); 2358c2ecf20Sopenharmony_ci if (ret) 2368c2ecf20Sopenharmony_ci return ret; 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 2398c2ecf20Sopenharmony_ci if (IS_ERR(phy_provider)) { 2408c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->cfg_clk); 2418c2ecf20Sopenharmony_ci dev_err(dev, "%s: failed to register phy\n", __func__); 2428c2ecf20Sopenharmony_ci return PTR_ERR(phy_provider); 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci 2458c2ecf20Sopenharmony_ci return 0; 2468c2ecf20Sopenharmony_ci} 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_cistatic int qcom_apq8064_sata_phy_remove(struct platform_device *pdev) 2498c2ecf20Sopenharmony_ci{ 2508c2ecf20Sopenharmony_ci struct qcom_apq8064_sata_phy *phy = platform_get_drvdata(pdev); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci clk_disable_unprepare(phy->cfg_clk); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci return 0; 2558c2ecf20Sopenharmony_ci} 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_cistatic const struct of_device_id qcom_apq8064_sata_phy_of_match[] = { 2588c2ecf20Sopenharmony_ci { .compatible = "qcom,apq8064-sata-phy" }, 2598c2ecf20Sopenharmony_ci { }, 2608c2ecf20Sopenharmony_ci}; 2618c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, qcom_apq8064_sata_phy_of_match); 2628c2ecf20Sopenharmony_ci 2638c2ecf20Sopenharmony_cistatic struct platform_driver qcom_apq8064_sata_phy_driver = { 2648c2ecf20Sopenharmony_ci .probe = qcom_apq8064_sata_phy_probe, 2658c2ecf20Sopenharmony_ci .remove = qcom_apq8064_sata_phy_remove, 2668c2ecf20Sopenharmony_ci .driver = { 2678c2ecf20Sopenharmony_ci .name = "qcom-apq8064-sata-phy", 2688c2ecf20Sopenharmony_ci .of_match_table = qcom_apq8064_sata_phy_of_match, 2698c2ecf20Sopenharmony_ci } 2708c2ecf20Sopenharmony_ci}; 2718c2ecf20Sopenharmony_cimodule_platform_driver(qcom_apq8064_sata_phy_driver); 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("QCOM apq8064 SATA PHY driver"); 2748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 275