18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * phy-uniphier-ahci.c - PHY driver for UniPhier AHCI controller 48c2ecf20Sopenharmony_ci * Copyright 2016-2020, Socionext Inc. 58c2ecf20Sopenharmony_ci * Author: Kunihiko Hayashi <hayashi.kunihiko@socionext.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 98c2ecf20Sopenharmony_ci#include <linux/bitops.h> 108c2ecf20Sopenharmony_ci#include <linux/clk.h> 118c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 128c2ecf20Sopenharmony_ci#include <linux/module.h> 138c2ecf20Sopenharmony_ci#include <linux/of.h> 148c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 158c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 168c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 178c2ecf20Sopenharmony_ci#include <linux/reset.h> 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_cistruct uniphier_ahciphy_priv { 208c2ecf20Sopenharmony_ci struct device *dev; 218c2ecf20Sopenharmony_ci void __iomem *base; 228c2ecf20Sopenharmony_ci struct clk *clk, *clk_parent; 238c2ecf20Sopenharmony_ci struct reset_control *rst, *rst_parent; 248c2ecf20Sopenharmony_ci const struct uniphier_ahciphy_soc_data *data; 258c2ecf20Sopenharmony_ci}; 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_cistruct uniphier_ahciphy_soc_data { 288c2ecf20Sopenharmony_ci int (*init)(struct uniphier_ahciphy_priv *priv); 298c2ecf20Sopenharmony_ci int (*power_on)(struct uniphier_ahciphy_priv *priv); 308c2ecf20Sopenharmony_ci int (*power_off)(struct uniphier_ahciphy_priv *priv); 318c2ecf20Sopenharmony_ci bool is_ready_high; 328c2ecf20Sopenharmony_ci bool is_phy_clk; 338c2ecf20Sopenharmony_ci}; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci/* for PXs2/PXs3 */ 368c2ecf20Sopenharmony_ci#define CKCTRL 0x0 378c2ecf20Sopenharmony_ci#define CKCTRL_P0_READY BIT(15) 388c2ecf20Sopenharmony_ci#define CKCTRL_P0_RESET BIT(10) 398c2ecf20Sopenharmony_ci#define CKCTRL_REF_SSP_EN BIT(9) 408c2ecf20Sopenharmony_ci#define TXCTRL0 0x4 418c2ecf20Sopenharmony_ci#define TXCTRL0_AMP_G3_MASK GENMASK(22, 16) 428c2ecf20Sopenharmony_ci#define TXCTRL0_AMP_G2_MASK GENMASK(14, 8) 438c2ecf20Sopenharmony_ci#define TXCTRL0_AMP_G1_MASK GENMASK(6, 0) 448c2ecf20Sopenharmony_ci#define TXCTRL1 0x8 458c2ecf20Sopenharmony_ci#define TXCTRL1_DEEMPH_G3_MASK GENMASK(21, 16) 468c2ecf20Sopenharmony_ci#define TXCTRL1_DEEMPH_G2_MASK GENMASK(13, 8) 478c2ecf20Sopenharmony_ci#define TXCTRL1_DEEMPH_G1_MASK GENMASK(5, 0) 488c2ecf20Sopenharmony_ci#define RXCTRL 0xc 498c2ecf20Sopenharmony_ci#define RXCTRL_LOS_LVL_MASK GENMASK(20, 16) 508c2ecf20Sopenharmony_ci#define RXCTRL_LOS_BIAS_MASK GENMASK(10, 8) 518c2ecf20Sopenharmony_ci#define RXCTRL_RX_EQ_MASK GENMASK(2, 0) 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_cistatic void uniphier_ahciphy_pxs2_enable(struct uniphier_ahciphy_priv *priv, 548c2ecf20Sopenharmony_ci bool enable) 558c2ecf20Sopenharmony_ci{ 568c2ecf20Sopenharmony_ci u32 val; 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci val = readl(priv->base + CKCTRL); 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci if (enable) { 618c2ecf20Sopenharmony_ci val |= CKCTRL_REF_SSP_EN; 628c2ecf20Sopenharmony_ci writel(val, priv->base + CKCTRL); 638c2ecf20Sopenharmony_ci val &= ~CKCTRL_P0_RESET; 648c2ecf20Sopenharmony_ci writel(val, priv->base + CKCTRL); 658c2ecf20Sopenharmony_ci } else { 668c2ecf20Sopenharmony_ci val |= CKCTRL_P0_RESET; 678c2ecf20Sopenharmony_ci writel(val, priv->base + CKCTRL); 688c2ecf20Sopenharmony_ci val &= ~CKCTRL_REF_SSP_EN; 698c2ecf20Sopenharmony_ci writel(val, priv->base + CKCTRL); 708c2ecf20Sopenharmony_ci } 718c2ecf20Sopenharmony_ci} 728c2ecf20Sopenharmony_ci 738c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_pxs2_power_on(struct uniphier_ahciphy_priv *priv) 748c2ecf20Sopenharmony_ci{ 758c2ecf20Sopenharmony_ci int ret; 768c2ecf20Sopenharmony_ci u32 val; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci uniphier_ahciphy_pxs2_enable(priv, true); 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ci /* wait until PLL is ready */ 818c2ecf20Sopenharmony_ci if (priv->data->is_ready_high) 828c2ecf20Sopenharmony_ci ret = readl_poll_timeout(priv->base + CKCTRL, val, 838c2ecf20Sopenharmony_ci (val & CKCTRL_P0_READY), 200, 400); 848c2ecf20Sopenharmony_ci else 858c2ecf20Sopenharmony_ci ret = readl_poll_timeout(priv->base + CKCTRL, val, 868c2ecf20Sopenharmony_ci !(val & CKCTRL_P0_READY), 200, 400); 878c2ecf20Sopenharmony_ci if (ret) { 888c2ecf20Sopenharmony_ci dev_err(priv->dev, "Failed to check whether PHY PLL is ready\n"); 898c2ecf20Sopenharmony_ci uniphier_ahciphy_pxs2_enable(priv, false); 908c2ecf20Sopenharmony_ci } 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci return ret; 938c2ecf20Sopenharmony_ci} 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_pxs2_power_off(struct uniphier_ahciphy_priv *priv) 968c2ecf20Sopenharmony_ci{ 978c2ecf20Sopenharmony_ci uniphier_ahciphy_pxs2_enable(priv, false); 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci return 0; 1008c2ecf20Sopenharmony_ci} 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_pxs3_init(struct uniphier_ahciphy_priv *priv) 1038c2ecf20Sopenharmony_ci{ 1048c2ecf20Sopenharmony_ci int i; 1058c2ecf20Sopenharmony_ci u32 val; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* setup port parameter */ 1088c2ecf20Sopenharmony_ci val = readl(priv->base + TXCTRL0); 1098c2ecf20Sopenharmony_ci val &= ~TXCTRL0_AMP_G3_MASK; 1108c2ecf20Sopenharmony_ci val |= FIELD_PREP(TXCTRL0_AMP_G3_MASK, 0x73); 1118c2ecf20Sopenharmony_ci val &= ~TXCTRL0_AMP_G2_MASK; 1128c2ecf20Sopenharmony_ci val |= FIELD_PREP(TXCTRL0_AMP_G2_MASK, 0x46); 1138c2ecf20Sopenharmony_ci val &= ~TXCTRL0_AMP_G1_MASK; 1148c2ecf20Sopenharmony_ci val |= FIELD_PREP(TXCTRL0_AMP_G1_MASK, 0x42); 1158c2ecf20Sopenharmony_ci writel(val, priv->base + TXCTRL0); 1168c2ecf20Sopenharmony_ci 1178c2ecf20Sopenharmony_ci val = readl(priv->base + TXCTRL1); 1188c2ecf20Sopenharmony_ci val &= ~TXCTRL1_DEEMPH_G3_MASK; 1198c2ecf20Sopenharmony_ci val |= FIELD_PREP(TXCTRL1_DEEMPH_G3_MASK, 0x23); 1208c2ecf20Sopenharmony_ci val &= ~TXCTRL1_DEEMPH_G2_MASK; 1218c2ecf20Sopenharmony_ci val |= FIELD_PREP(TXCTRL1_DEEMPH_G2_MASK, 0x05); 1228c2ecf20Sopenharmony_ci val &= ~TXCTRL1_DEEMPH_G1_MASK; 1238c2ecf20Sopenharmony_ci val |= FIELD_PREP(TXCTRL1_DEEMPH_G1_MASK, 0x05); 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci val = readl(priv->base + RXCTRL); 1268c2ecf20Sopenharmony_ci val &= ~RXCTRL_LOS_LVL_MASK; 1278c2ecf20Sopenharmony_ci val |= FIELD_PREP(RXCTRL_LOS_LVL_MASK, 0x9); 1288c2ecf20Sopenharmony_ci val &= ~RXCTRL_LOS_BIAS_MASK; 1298c2ecf20Sopenharmony_ci val |= FIELD_PREP(RXCTRL_LOS_BIAS_MASK, 0x2); 1308c2ecf20Sopenharmony_ci val &= ~RXCTRL_RX_EQ_MASK; 1318c2ecf20Sopenharmony_ci val |= FIELD_PREP(RXCTRL_RX_EQ_MASK, 0x1); 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci /* dummy read 25 times to make a wait time for the phy to stabilize */ 1348c2ecf20Sopenharmony_ci for (i = 0; i < 25; i++) 1358c2ecf20Sopenharmony_ci readl(priv->base + CKCTRL); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci return 0; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_init(struct phy *phy) 1418c2ecf20Sopenharmony_ci{ 1428c2ecf20Sopenharmony_ci struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy); 1438c2ecf20Sopenharmony_ci int ret; 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci ret = clk_prepare_enable(priv->clk_parent); 1468c2ecf20Sopenharmony_ci if (ret) 1478c2ecf20Sopenharmony_ci return ret; 1488c2ecf20Sopenharmony_ci 1498c2ecf20Sopenharmony_ci ret = reset_control_deassert(priv->rst_parent); 1508c2ecf20Sopenharmony_ci if (ret) 1518c2ecf20Sopenharmony_ci goto out_clk_disable; 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci if (priv->data->init) { 1548c2ecf20Sopenharmony_ci ret = priv->data->init(priv); 1558c2ecf20Sopenharmony_ci if (ret) 1568c2ecf20Sopenharmony_ci goto out_rst_assert; 1578c2ecf20Sopenharmony_ci } 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci return 0; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ciout_rst_assert: 1628c2ecf20Sopenharmony_ci reset_control_assert(priv->rst_parent); 1638c2ecf20Sopenharmony_ciout_clk_disable: 1648c2ecf20Sopenharmony_ci clk_disable_unprepare(priv->clk_parent); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci return ret; 1678c2ecf20Sopenharmony_ci} 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_exit(struct phy *phy) 1708c2ecf20Sopenharmony_ci{ 1718c2ecf20Sopenharmony_ci struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci reset_control_assert(priv->rst_parent); 1748c2ecf20Sopenharmony_ci clk_disable_unprepare(priv->clk_parent); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci return 0; 1778c2ecf20Sopenharmony_ci} 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_power_on(struct phy *phy) 1808c2ecf20Sopenharmony_ci{ 1818c2ecf20Sopenharmony_ci struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy); 1828c2ecf20Sopenharmony_ci int ret = 0; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci ret = clk_prepare_enable(priv->clk); 1858c2ecf20Sopenharmony_ci if (ret) 1868c2ecf20Sopenharmony_ci return ret; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci ret = reset_control_deassert(priv->rst); 1898c2ecf20Sopenharmony_ci if (ret) 1908c2ecf20Sopenharmony_ci goto out_clk_disable; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (priv->data->power_on) { 1938c2ecf20Sopenharmony_ci ret = priv->data->power_on(priv); 1948c2ecf20Sopenharmony_ci if (ret) 1958c2ecf20Sopenharmony_ci goto out_reset_assert; 1968c2ecf20Sopenharmony_ci } 1978c2ecf20Sopenharmony_ci 1988c2ecf20Sopenharmony_ci return 0; 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ciout_reset_assert: 2018c2ecf20Sopenharmony_ci reset_control_assert(priv->rst); 2028c2ecf20Sopenharmony_ciout_clk_disable: 2038c2ecf20Sopenharmony_ci clk_disable_unprepare(priv->clk); 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci return ret; 2068c2ecf20Sopenharmony_ci} 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_power_off(struct phy *phy) 2098c2ecf20Sopenharmony_ci{ 2108c2ecf20Sopenharmony_ci struct uniphier_ahciphy_priv *priv = phy_get_drvdata(phy); 2118c2ecf20Sopenharmony_ci int ret = 0; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci if (priv->data->power_off) 2148c2ecf20Sopenharmony_ci ret = priv->data->power_off(priv); 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci reset_control_assert(priv->rst); 2178c2ecf20Sopenharmony_ci clk_disable_unprepare(priv->clk); 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ci return ret; 2208c2ecf20Sopenharmony_ci} 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic const struct phy_ops uniphier_ahciphy_ops = { 2238c2ecf20Sopenharmony_ci .init = uniphier_ahciphy_init, 2248c2ecf20Sopenharmony_ci .exit = uniphier_ahciphy_exit, 2258c2ecf20Sopenharmony_ci .power_on = uniphier_ahciphy_power_on, 2268c2ecf20Sopenharmony_ci .power_off = uniphier_ahciphy_power_off, 2278c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 2288c2ecf20Sopenharmony_ci}; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistatic int uniphier_ahciphy_probe(struct platform_device *pdev) 2318c2ecf20Sopenharmony_ci{ 2328c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 2338c2ecf20Sopenharmony_ci struct uniphier_ahciphy_priv *priv; 2348c2ecf20Sopenharmony_ci struct phy *phy; 2358c2ecf20Sopenharmony_ci struct phy_provider *phy_provider; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 2388c2ecf20Sopenharmony_ci if (!priv) 2398c2ecf20Sopenharmony_ci return -ENOMEM; 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci priv->dev = dev; 2428c2ecf20Sopenharmony_ci priv->data = of_device_get_match_data(dev); 2438c2ecf20Sopenharmony_ci if (WARN_ON(!priv->data)) 2448c2ecf20Sopenharmony_ci return -EINVAL; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci priv->base = devm_platform_ioremap_resource(pdev, 0); 2478c2ecf20Sopenharmony_ci if (IS_ERR(priv->base)) 2488c2ecf20Sopenharmony_ci return PTR_ERR(priv->base); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci priv->clk_parent = devm_clk_get(dev, "link"); 2518c2ecf20Sopenharmony_ci if (IS_ERR(priv->clk_parent)) 2528c2ecf20Sopenharmony_ci return PTR_ERR(priv->clk_parent); 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci if (priv->data->is_phy_clk) { 2558c2ecf20Sopenharmony_ci priv->clk = devm_clk_get(dev, "phy"); 2568c2ecf20Sopenharmony_ci if (IS_ERR(priv->clk)) 2578c2ecf20Sopenharmony_ci return PTR_ERR(priv->clk); 2588c2ecf20Sopenharmony_ci } 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci priv->rst_parent = devm_reset_control_get_shared(dev, "link"); 2618c2ecf20Sopenharmony_ci if (IS_ERR(priv->rst_parent)) 2628c2ecf20Sopenharmony_ci return PTR_ERR(priv->rst_parent); 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci priv->rst = devm_reset_control_get_shared(dev, "phy"); 2658c2ecf20Sopenharmony_ci if (IS_ERR(priv->rst)) 2668c2ecf20Sopenharmony_ci return PTR_ERR(priv->rst); 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_ci phy = devm_phy_create(dev, dev->of_node, &uniphier_ahciphy_ops); 2698c2ecf20Sopenharmony_ci if (IS_ERR(phy)) { 2708c2ecf20Sopenharmony_ci dev_err(dev, "failed to create phy\n"); 2718c2ecf20Sopenharmony_ci return PTR_ERR(phy); 2728c2ecf20Sopenharmony_ci } 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci phy_set_drvdata(phy, priv); 2758c2ecf20Sopenharmony_ci phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate); 2768c2ecf20Sopenharmony_ci if (IS_ERR(phy_provider)) 2778c2ecf20Sopenharmony_ci return PTR_ERR(phy_provider); 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci return 0; 2808c2ecf20Sopenharmony_ci} 2818c2ecf20Sopenharmony_ci 2828c2ecf20Sopenharmony_cistatic const struct uniphier_ahciphy_soc_data uniphier_pxs2_data = { 2838c2ecf20Sopenharmony_ci .power_on = uniphier_ahciphy_pxs2_power_on, 2848c2ecf20Sopenharmony_ci .power_off = uniphier_ahciphy_pxs2_power_off, 2858c2ecf20Sopenharmony_ci .is_ready_high = false, 2868c2ecf20Sopenharmony_ci .is_phy_clk = false, 2878c2ecf20Sopenharmony_ci}; 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_cistatic const struct uniphier_ahciphy_soc_data uniphier_pxs3_data = { 2908c2ecf20Sopenharmony_ci .init = uniphier_ahciphy_pxs3_init, 2918c2ecf20Sopenharmony_ci .power_on = uniphier_ahciphy_pxs2_power_on, 2928c2ecf20Sopenharmony_ci .power_off = uniphier_ahciphy_pxs2_power_off, 2938c2ecf20Sopenharmony_ci .is_ready_high = true, 2948c2ecf20Sopenharmony_ci .is_phy_clk = true, 2958c2ecf20Sopenharmony_ci}; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic const struct of_device_id uniphier_ahciphy_match[] = { 2988c2ecf20Sopenharmony_ci { 2998c2ecf20Sopenharmony_ci .compatible = "socionext,uniphier-pxs2-ahci-phy", 3008c2ecf20Sopenharmony_ci .data = &uniphier_pxs2_data, 3018c2ecf20Sopenharmony_ci }, 3028c2ecf20Sopenharmony_ci { 3038c2ecf20Sopenharmony_ci .compatible = "socionext,uniphier-pxs3-ahci-phy", 3048c2ecf20Sopenharmony_ci .data = &uniphier_pxs3_data, 3058c2ecf20Sopenharmony_ci }, 3068c2ecf20Sopenharmony_ci { /* Sentinel */ }, 3078c2ecf20Sopenharmony_ci}; 3088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, uniphier_ahciphy_match); 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic struct platform_driver uniphier_ahciphy_driver = { 3118c2ecf20Sopenharmony_ci .probe = uniphier_ahciphy_probe, 3128c2ecf20Sopenharmony_ci .driver = { 3138c2ecf20Sopenharmony_ci .name = "uniphier-ahci-phy", 3148c2ecf20Sopenharmony_ci .of_match_table = uniphier_ahciphy_match, 3158c2ecf20Sopenharmony_ci }, 3168c2ecf20Sopenharmony_ci}; 3178c2ecf20Sopenharmony_cimodule_platform_driver(uniphier_ahciphy_driver); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ciMODULE_AUTHOR("Kunihiko Hayashi <hayashi.kunihiko@socionext.com>"); 3208c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("UniPhier PHY driver for AHCI controller"); 3218c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 322