162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (c) 2014, The Linux Foundation. All rights reserved. 462306a36Sopenharmony_ci */ 562306a36Sopenharmony_ci 662306a36Sopenharmony_ci#include <linux/io.h> 762306a36Sopenharmony_ci#include <linux/iopoll.h> 862306a36Sopenharmony_ci#include <linux/kernel.h> 962306a36Sopenharmony_ci#include <linux/module.h> 1062306a36Sopenharmony_ci#include <linux/of.h> 1162306a36Sopenharmony_ci#include <linux/time.h> 1262306a36Sopenharmony_ci#include <linux/delay.h> 1362306a36Sopenharmony_ci#include <linux/clk.h> 1462306a36Sopenharmony_ci#include <linux/slab.h> 1562306a36Sopenharmony_ci#include <linux/platform_device.h> 1662306a36Sopenharmony_ci#include <linux/phy/phy.h> 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci/* PHY registers */ 1962306a36Sopenharmony_ci#define UNIPHY_PLL_REFCLK_CFG 0x000 2062306a36Sopenharmony_ci#define UNIPHY_PLL_PWRGEN_CFG 0x014 2162306a36Sopenharmony_ci#define UNIPHY_PLL_GLB_CFG 0x020 2262306a36Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG0 0x038 2362306a36Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG1 0x03C 2462306a36Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG2 0x040 2562306a36Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG3 0x044 2662306a36Sopenharmony_ci#define UNIPHY_PLL_SDM_CFG4 0x048 2762306a36Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG0 0x04C 2862306a36Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG1 0x050 2962306a36Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG2 0x054 3062306a36Sopenharmony_ci#define UNIPHY_PLL_SSC_CFG3 0x058 3162306a36Sopenharmony_ci#define UNIPHY_PLL_LKDET_CFG0 0x05C 3262306a36Sopenharmony_ci#define UNIPHY_PLL_LKDET_CFG1 0x060 3362306a36Sopenharmony_ci#define UNIPHY_PLL_LKDET_CFG2 0x064 3462306a36Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG0 0x06C 3562306a36Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG8 0x08C 3662306a36Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG9 0x090 3762306a36Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG10 0x094 3862306a36Sopenharmony_ci#define UNIPHY_PLL_CAL_CFG11 0x098 3962306a36Sopenharmony_ci#define UNIPHY_PLL_STATUS 0x0C0 4062306a36Sopenharmony_ci 4162306a36Sopenharmony_ci#define SATA_PHY_SER_CTRL 0x100 4262306a36Sopenharmony_ci#define SATA_PHY_TX_DRIV_CTRL0 0x104 4362306a36Sopenharmony_ci#define SATA_PHY_TX_DRIV_CTRL1 0x108 4462306a36Sopenharmony_ci#define SATA_PHY_TX_IMCAL0 0x11C 4562306a36Sopenharmony_ci#define SATA_PHY_TX_IMCAL2 0x124 4662306a36Sopenharmony_ci#define SATA_PHY_RX_IMCAL0 0x128 4762306a36Sopenharmony_ci#define SATA_PHY_EQUAL 0x13C 4862306a36Sopenharmony_ci#define SATA_PHY_OOB_TERM 0x144 4962306a36Sopenharmony_ci#define SATA_PHY_CDR_CTRL0 0x148 5062306a36Sopenharmony_ci#define SATA_PHY_CDR_CTRL1 0x14C 5162306a36Sopenharmony_ci#define SATA_PHY_CDR_CTRL2 0x150 5262306a36Sopenharmony_ci#define SATA_PHY_CDR_CTRL3 0x154 5362306a36Sopenharmony_ci#define SATA_PHY_PI_CTRL0 0x168 5462306a36Sopenharmony_ci#define SATA_PHY_POW_DWN_CTRL0 0x180 5562306a36Sopenharmony_ci#define SATA_PHY_POW_DWN_CTRL1 0x184 5662306a36Sopenharmony_ci#define SATA_PHY_TX_DATA_CTRL 0x188 5762306a36Sopenharmony_ci#define SATA_PHY_ALIGNP 0x1A4 5862306a36Sopenharmony_ci#define SATA_PHY_TX_IMCAL_STAT 0x1E4 5962306a36Sopenharmony_ci#define SATA_PHY_RX_IMCAL_STAT 0x1E8 6062306a36Sopenharmony_ci 6162306a36Sopenharmony_ci#define UNIPHY_PLL_LOCK BIT(0) 6262306a36Sopenharmony_ci#define SATA_PHY_TX_CAL BIT(0) 6362306a36Sopenharmony_ci#define SATA_PHY_RX_CAL BIT(0) 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci/* default timeout set to 1 sec */ 6662306a36Sopenharmony_ci#define TIMEOUT_MS 10000 6762306a36Sopenharmony_ci#define DELAY_INTERVAL_US 100 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_cistruct qcom_apq8064_sata_phy { 7062306a36Sopenharmony_ci void __iomem *mmio; 7162306a36Sopenharmony_ci struct clk *cfg_clk; 7262306a36Sopenharmony_ci struct device *dev; 7362306a36Sopenharmony_ci}; 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci/* Helper function to do poll and timeout */ 7662306a36Sopenharmony_cistatic int poll_timeout(void __iomem *addr, u32 mask) 7762306a36Sopenharmony_ci{ 7862306a36Sopenharmony_ci u32 val; 7962306a36Sopenharmony_ci 8062306a36Sopenharmony_ci return readl_relaxed_poll_timeout(addr, val, (val & mask), 8162306a36Sopenharmony_ci DELAY_INTERVAL_US, TIMEOUT_MS * 1000); 8262306a36Sopenharmony_ci} 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_cistatic int qcom_apq8064_sata_phy_init(struct phy *generic_phy) 8562306a36Sopenharmony_ci{ 8662306a36Sopenharmony_ci struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy); 8762306a36Sopenharmony_ci void __iomem *base = phy->mmio; 8862306a36Sopenharmony_ci int ret = 0; 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci /* SATA phy initialization */ 9162306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_SER_CTRL); 9262306a36Sopenharmony_ci writel_relaxed(0xB1, base + SATA_PHY_POW_DWN_CTRL0); 9362306a36Sopenharmony_ci /* Make sure the power down happens before power up */ 9462306a36Sopenharmony_ci mb(); 9562306a36Sopenharmony_ci usleep_range(10, 60); 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0); 9862306a36Sopenharmony_ci writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1); 9962306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0); 10062306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0); 10162306a36Sopenharmony_ci writel_relaxed(0x02, base + SATA_PHY_TX_IMCAL2); 10262306a36Sopenharmony_ci 10362306a36Sopenharmony_ci /* Write UNIPHYPLL registers to configure PLL */ 10462306a36Sopenharmony_ci writel_relaxed(0x04, base + UNIPHY_PLL_REFCLK_CFG); 10562306a36Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_PWRGEN_CFG); 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci writel_relaxed(0x0A, base + UNIPHY_PLL_CAL_CFG0); 10862306a36Sopenharmony_ci writel_relaxed(0xF3, base + UNIPHY_PLL_CAL_CFG8); 10962306a36Sopenharmony_ci writel_relaxed(0x01, base + UNIPHY_PLL_CAL_CFG9); 11062306a36Sopenharmony_ci writel_relaxed(0xED, base + UNIPHY_PLL_CAL_CFG10); 11162306a36Sopenharmony_ci writel_relaxed(0x02, base + UNIPHY_PLL_CAL_CFG11); 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci writel_relaxed(0x36, base + UNIPHY_PLL_SDM_CFG0); 11462306a36Sopenharmony_ci writel_relaxed(0x0D, base + UNIPHY_PLL_SDM_CFG1); 11562306a36Sopenharmony_ci writel_relaxed(0xA3, base + UNIPHY_PLL_SDM_CFG2); 11662306a36Sopenharmony_ci writel_relaxed(0xF0, base + UNIPHY_PLL_SDM_CFG3); 11762306a36Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_SDM_CFG4); 11862306a36Sopenharmony_ci 11962306a36Sopenharmony_ci writel_relaxed(0x19, base + UNIPHY_PLL_SSC_CFG0); 12062306a36Sopenharmony_ci writel_relaxed(0xE1, base + UNIPHY_PLL_SSC_CFG1); 12162306a36Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_SSC_CFG2); 12262306a36Sopenharmony_ci writel_relaxed(0x11, base + UNIPHY_PLL_SSC_CFG3); 12362306a36Sopenharmony_ci 12462306a36Sopenharmony_ci writel_relaxed(0x04, base + UNIPHY_PLL_LKDET_CFG0); 12562306a36Sopenharmony_ci writel_relaxed(0xFF, base + UNIPHY_PLL_LKDET_CFG1); 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci writel_relaxed(0x02, base + UNIPHY_PLL_GLB_CFG); 12862306a36Sopenharmony_ci /* make sure global config LDO power down happens before power up */ 12962306a36Sopenharmony_ci mb(); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci writel_relaxed(0x03, base + UNIPHY_PLL_GLB_CFG); 13262306a36Sopenharmony_ci writel_relaxed(0x05, base + UNIPHY_PLL_LKDET_CFG2); 13362306a36Sopenharmony_ci 13462306a36Sopenharmony_ci /* PLL Lock wait */ 13562306a36Sopenharmony_ci ret = poll_timeout(base + UNIPHY_PLL_STATUS, UNIPHY_PLL_LOCK); 13662306a36Sopenharmony_ci if (ret) { 13762306a36Sopenharmony_ci dev_err(phy->dev, "poll timeout UNIPHY_PLL_STATUS\n"); 13862306a36Sopenharmony_ci return ret; 13962306a36Sopenharmony_ci } 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci /* TX Calibration */ 14262306a36Sopenharmony_ci ret = poll_timeout(base + SATA_PHY_TX_IMCAL_STAT, SATA_PHY_TX_CAL); 14362306a36Sopenharmony_ci if (ret) { 14462306a36Sopenharmony_ci dev_err(phy->dev, "poll timeout SATA_PHY_TX_IMCAL_STAT\n"); 14562306a36Sopenharmony_ci return ret; 14662306a36Sopenharmony_ci } 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci /* RX Calibration */ 14962306a36Sopenharmony_ci ret = poll_timeout(base + SATA_PHY_RX_IMCAL_STAT, SATA_PHY_RX_CAL); 15062306a36Sopenharmony_ci if (ret) { 15162306a36Sopenharmony_ci dev_err(phy->dev, "poll timeout SATA_PHY_RX_IMCAL_STAT\n"); 15262306a36Sopenharmony_ci return ret; 15362306a36Sopenharmony_ci } 15462306a36Sopenharmony_ci 15562306a36Sopenharmony_ci /* SATA phy calibrated successfully, power up to functional mode */ 15662306a36Sopenharmony_ci writel_relaxed(0x3E, base + SATA_PHY_POW_DWN_CTRL1); 15762306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_RX_IMCAL0); 15862306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_TX_IMCAL0); 15962306a36Sopenharmony_ci 16062306a36Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_POW_DWN_CTRL1); 16162306a36Sopenharmony_ci writel_relaxed(0x59, base + SATA_PHY_CDR_CTRL0); 16262306a36Sopenharmony_ci writel_relaxed(0x04, base + SATA_PHY_CDR_CTRL1); 16362306a36Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL2); 16462306a36Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_PI_CTRL0); 16562306a36Sopenharmony_ci writel_relaxed(0x00, base + SATA_PHY_CDR_CTRL3); 16662306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_POW_DWN_CTRL0); 16762306a36Sopenharmony_ci 16862306a36Sopenharmony_ci writel_relaxed(0x11, base + SATA_PHY_TX_DATA_CTRL); 16962306a36Sopenharmony_ci writel_relaxed(0x43, base + SATA_PHY_ALIGNP); 17062306a36Sopenharmony_ci writel_relaxed(0x04, base + SATA_PHY_OOB_TERM); 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_ci writel_relaxed(0x01, base + SATA_PHY_EQUAL); 17362306a36Sopenharmony_ci writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL0); 17462306a36Sopenharmony_ci writel_relaxed(0x09, base + SATA_PHY_TX_DRIV_CTRL1); 17562306a36Sopenharmony_ci 17662306a36Sopenharmony_ci return 0; 17762306a36Sopenharmony_ci} 17862306a36Sopenharmony_ci 17962306a36Sopenharmony_cistatic int qcom_apq8064_sata_phy_exit(struct phy *generic_phy) 18062306a36Sopenharmony_ci{ 18162306a36Sopenharmony_ci struct qcom_apq8064_sata_phy *phy = phy_get_drvdata(generic_phy); 18262306a36Sopenharmony_ci void __iomem *base = phy->mmio; 18362306a36Sopenharmony_ci 18462306a36Sopenharmony_ci /* Power down PHY */ 18562306a36Sopenharmony_ci writel_relaxed(0xF8, base + SATA_PHY_POW_DWN_CTRL0); 18662306a36Sopenharmony_ci writel_relaxed(0xFE, base + SATA_PHY_POW_DWN_CTRL1); 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci /* Power down PLL block */ 18962306a36Sopenharmony_ci writel_relaxed(0x00, base + UNIPHY_PLL_GLB_CFG); 19062306a36Sopenharmony_ci 19162306a36Sopenharmony_ci return 0; 19262306a36Sopenharmony_ci} 19362306a36Sopenharmony_ci 19462306a36Sopenharmony_cistatic const struct phy_ops qcom_apq8064_sata_phy_ops = { 19562306a36Sopenharmony_ci .init = qcom_apq8064_sata_phy_init, 19662306a36Sopenharmony_ci .exit = qcom_apq8064_sata_phy_exit, 19762306a36Sopenharmony_ci .owner = THIS_MODULE, 19862306a36Sopenharmony_ci}; 19962306a36Sopenharmony_ci 20062306a36Sopenharmony_cistatic int qcom_apq8064_sata_phy_probe(struct platform_device *pdev) 20162306a36Sopenharmony_ci{ 20262306a36Sopenharmony_ci struct qcom_apq8064_sata_phy *phy; 20362306a36Sopenharmony_ci struct device *dev = &pdev->dev; 20462306a36Sopenharmony_ci struct phy_provider *phy_provider; 20562306a36Sopenharmony_ci struct phy *generic_phy; 20662306a36Sopenharmony_ci int ret; 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_ci phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL); 20962306a36Sopenharmony_ci if (!phy) 21062306a36Sopenharmony_ci return -ENOMEM; 21162306a36Sopenharmony_ci 21262306a36Sopenharmony_ci phy->mmio = devm_platform_ioremap_resource(pdev, 0); 21362306a36Sopenharmony_ci if (IS_ERR(phy->mmio)) 21462306a36Sopenharmony_ci return PTR_ERR(phy->mmio); 21562306a36Sopenharmony_ci 21662306a36Sopenharmony_ci generic_phy = devm_phy_create(dev, NULL, &qcom_apq8064_sata_phy_ops); 21762306a36Sopenharmony_ci if (IS_ERR(generic_phy)) { 21862306a36Sopenharmony_ci dev_err(dev, "%s: failed to create phy\n", __func__); 21962306a36Sopenharmony_ci return PTR_ERR(generic_phy); 22062306a36Sopenharmony_ci } 22162306a36Sopenharmony_ci 22262306a36Sopenharmony_ci phy->dev = dev; 22362306a36Sopenharmony_ci phy_set_drvdata(generic_phy, phy); 22462306a36Sopenharmony_ci platform_set_drvdata(pdev, phy); 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci phy->cfg_clk = devm_clk_get(dev, "cfg"); 22762306a36Sopenharmony_ci if (IS_ERR(phy->cfg_clk)) { 22862306a36Sopenharmony_ci dev_err(dev, "Failed to get sata cfg clock\n"); 22962306a36Sopenharmony_ci return PTR_ERR(phy->cfg_clk); 23062306a36Sopenharmony_ci } 23162306a36Sopenharmony_ci 23262306a36Sopenharmony_ci ret = clk_prepare_enable(phy->cfg_clk); 23362306a36Sopenharmony_ci if (ret) 23462306a36Sopenharmony_ci return ret; 23562306a36Sopenharmony_ci 23662306a36Sopenharmony_ci phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 23762306a36Sopenharmony_ci if (IS_ERR(phy_provider)) { 23862306a36Sopenharmony_ci clk_disable_unprepare(phy->cfg_clk); 23962306a36Sopenharmony_ci dev_err(dev, "%s: failed to register phy\n", __func__); 24062306a36Sopenharmony_ci return PTR_ERR(phy_provider); 24162306a36Sopenharmony_ci } 24262306a36Sopenharmony_ci 24362306a36Sopenharmony_ci return 0; 24462306a36Sopenharmony_ci} 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_cistatic void qcom_apq8064_sata_phy_remove(struct platform_device *pdev) 24762306a36Sopenharmony_ci{ 24862306a36Sopenharmony_ci struct qcom_apq8064_sata_phy *phy = platform_get_drvdata(pdev); 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ci clk_disable_unprepare(phy->cfg_clk); 25162306a36Sopenharmony_ci} 25262306a36Sopenharmony_ci 25362306a36Sopenharmony_cistatic const struct of_device_id qcom_apq8064_sata_phy_of_match[] = { 25462306a36Sopenharmony_ci { .compatible = "qcom,apq8064-sata-phy" }, 25562306a36Sopenharmony_ci { }, 25662306a36Sopenharmony_ci}; 25762306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, qcom_apq8064_sata_phy_of_match); 25862306a36Sopenharmony_ci 25962306a36Sopenharmony_cistatic struct platform_driver qcom_apq8064_sata_phy_driver = { 26062306a36Sopenharmony_ci .probe = qcom_apq8064_sata_phy_probe, 26162306a36Sopenharmony_ci .remove_new = qcom_apq8064_sata_phy_remove, 26262306a36Sopenharmony_ci .driver = { 26362306a36Sopenharmony_ci .name = "qcom-apq8064-sata-phy", 26462306a36Sopenharmony_ci .of_match_table = qcom_apq8064_sata_phy_of_match, 26562306a36Sopenharmony_ci } 26662306a36Sopenharmony_ci}; 26762306a36Sopenharmony_cimodule_platform_driver(qcom_apq8064_sata_phy_driver); 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ciMODULE_DESCRIPTION("QCOM apq8064 SATA PHY driver"); 27062306a36Sopenharmony_ciMODULE_LICENSE("GPL v2"); 271