Linux 4.19.133
[linux/fpc-iii.git] / drivers / phy / samsung / phy-exynos-pcie.c
bloba89c12faff39754421af30b8908b45912def5a3b
1 /*
2 * Samsung EXYNOS SoC series PCIe PHY driver
4 * Phy provider for PCIe controller on Exynos SoC series
6 * Copyright (C) 2017 Samsung Electronics Co., Ltd.
7 * Jaehoon Chung <jh80.chung@samsung.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
14 #include <linux/delay.h>
15 #include <linux/io.h>
16 #include <linux/iopoll.h>
17 #include <linux/init.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/of.h>
20 #include <linux/of_address.h>
21 #include <linux/of_platform.h>
22 #include <linux/platform_device.h>
23 #include <linux/phy/phy.h>
24 #include <linux/regmap.h>
26 /* PCIe Purple registers */
27 #define PCIE_PHY_GLOBAL_RESET 0x000
28 #define PCIE_PHY_COMMON_RESET 0x004
29 #define PCIE_PHY_CMN_REG 0x008
30 #define PCIE_PHY_MAC_RESET 0x00c
31 #define PCIE_PHY_PLL_LOCKED 0x010
32 #define PCIE_PHY_TRSVREG_RESET 0x020
33 #define PCIE_PHY_TRSV_RESET 0x024
35 /* PCIe PHY registers */
36 #define PCIE_PHY_IMPEDANCE 0x004
37 #define PCIE_PHY_PLL_DIV_0 0x008
38 #define PCIE_PHY_PLL_BIAS 0x00c
39 #define PCIE_PHY_DCC_FEEDBACK 0x014
40 #define PCIE_PHY_PLL_DIV_1 0x05c
41 #define PCIE_PHY_COMMON_POWER 0x064
42 #define PCIE_PHY_COMMON_PD_CMN BIT(3)
43 #define PCIE_PHY_TRSV0_EMP_LVL 0x084
44 #define PCIE_PHY_TRSV0_DRV_LVL 0x088
45 #define PCIE_PHY_TRSV0_RXCDR 0x0ac
46 #define PCIE_PHY_TRSV0_POWER 0x0c4
47 #define PCIE_PHY_TRSV0_PD_TSV BIT(7)
48 #define PCIE_PHY_TRSV0_LVCC 0x0dc
49 #define PCIE_PHY_TRSV1_EMP_LVL 0x144
50 #define PCIE_PHY_TRSV1_RXCDR 0x16c
51 #define PCIE_PHY_TRSV1_POWER 0x184
52 #define PCIE_PHY_TRSV1_PD_TSV BIT(7)
53 #define PCIE_PHY_TRSV1_LVCC 0x19c
54 #define PCIE_PHY_TRSV2_EMP_LVL 0x204
55 #define PCIE_PHY_TRSV2_RXCDR 0x22c
56 #define PCIE_PHY_TRSV2_POWER 0x244
57 #define PCIE_PHY_TRSV2_PD_TSV BIT(7)
58 #define PCIE_PHY_TRSV2_LVCC 0x25c
59 #define PCIE_PHY_TRSV3_EMP_LVL 0x2c4
60 #define PCIE_PHY_TRSV3_RXCDR 0x2ec
61 #define PCIE_PHY_TRSV3_POWER 0x304
62 #define PCIE_PHY_TRSV3_PD_TSV BIT(7)
63 #define PCIE_PHY_TRSV3_LVCC 0x31c
65 struct exynos_pcie_phy_data {
66 const struct phy_ops *ops;
69 /* For Exynos pcie phy */
70 struct exynos_pcie_phy {
71 const struct exynos_pcie_phy_data *drv_data;
72 void __iomem *phy_base;
73 void __iomem *blk_base; /* For exynos5440 */
76 static void exynos_pcie_phy_writel(void __iomem *base, u32 val, u32 offset)
78 writel(val, base + offset);
81 static u32 exynos_pcie_phy_readl(void __iomem *base, u32 offset)
83 return readl(base + offset);
86 /* For Exynos5440 specific functions */
87 static int exynos5440_pcie_phy_init(struct phy *phy)
89 struct exynos_pcie_phy *ep = phy_get_drvdata(phy);
91 /* DCC feedback control off */
92 exynos_pcie_phy_writel(ep->phy_base, 0x29, PCIE_PHY_DCC_FEEDBACK);
94 /* set TX/RX impedance */
95 exynos_pcie_phy_writel(ep->phy_base, 0xd5, PCIE_PHY_IMPEDANCE);
97 /* set 50Mhz PHY clock */
98 exynos_pcie_phy_writel(ep->phy_base, 0x14, PCIE_PHY_PLL_DIV_0);
99 exynos_pcie_phy_writel(ep->phy_base, 0x12, PCIE_PHY_PLL_DIV_1);
101 /* set TX Differential output for lane 0 */
102 exynos_pcie_phy_writel(ep->phy_base, 0x7f, PCIE_PHY_TRSV0_DRV_LVL);
104 /* set TX Pre-emphasis Level Control for lane 0 to minimum */
105 exynos_pcie_phy_writel(ep->phy_base, 0x0, PCIE_PHY_TRSV0_EMP_LVL);
107 /* set RX clock and data recovery bandwidth */
108 exynos_pcie_phy_writel(ep->phy_base, 0xe7, PCIE_PHY_PLL_BIAS);
109 exynos_pcie_phy_writel(ep->phy_base, 0x82, PCIE_PHY_TRSV0_RXCDR);
110 exynos_pcie_phy_writel(ep->phy_base, 0x82, PCIE_PHY_TRSV1_RXCDR);
111 exynos_pcie_phy_writel(ep->phy_base, 0x82, PCIE_PHY_TRSV2_RXCDR);
112 exynos_pcie_phy_writel(ep->phy_base, 0x82, PCIE_PHY_TRSV3_RXCDR);
114 /* change TX Pre-emphasis Level Control for lanes */
115 exynos_pcie_phy_writel(ep->phy_base, 0x39, PCIE_PHY_TRSV0_EMP_LVL);
116 exynos_pcie_phy_writel(ep->phy_base, 0x39, PCIE_PHY_TRSV1_EMP_LVL);
117 exynos_pcie_phy_writel(ep->phy_base, 0x39, PCIE_PHY_TRSV2_EMP_LVL);
118 exynos_pcie_phy_writel(ep->phy_base, 0x39, PCIE_PHY_TRSV3_EMP_LVL);
120 /* set LVCC */
121 exynos_pcie_phy_writel(ep->phy_base, 0x20, PCIE_PHY_TRSV0_LVCC);
122 exynos_pcie_phy_writel(ep->phy_base, 0xa0, PCIE_PHY_TRSV1_LVCC);
123 exynos_pcie_phy_writel(ep->phy_base, 0xa0, PCIE_PHY_TRSV2_LVCC);
124 exynos_pcie_phy_writel(ep->phy_base, 0xa0, PCIE_PHY_TRSV3_LVCC);
126 /* pulse for common reset */
127 exynos_pcie_phy_writel(ep->blk_base, 1, PCIE_PHY_COMMON_RESET);
128 udelay(500);
129 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_COMMON_RESET);
131 return 0;
134 static int exynos5440_pcie_phy_power_on(struct phy *phy)
136 struct exynos_pcie_phy *ep = phy_get_drvdata(phy);
137 u32 val;
139 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_COMMON_RESET);
140 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_CMN_REG);
141 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_TRSVREG_RESET);
142 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_TRSV_RESET);
144 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_COMMON_POWER);
145 val &= ~PCIE_PHY_COMMON_PD_CMN;
146 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_COMMON_POWER);
148 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV0_POWER);
149 val &= ~PCIE_PHY_TRSV0_PD_TSV;
150 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV0_POWER);
152 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV1_POWER);
153 val &= ~PCIE_PHY_TRSV1_PD_TSV;
154 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV1_POWER);
156 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV2_POWER);
157 val &= ~PCIE_PHY_TRSV2_PD_TSV;
158 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV2_POWER);
160 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV3_POWER);
161 val &= ~PCIE_PHY_TRSV3_PD_TSV;
162 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV3_POWER);
164 return 0;
167 static int exynos5440_pcie_phy_power_off(struct phy *phy)
169 struct exynos_pcie_phy *ep = phy_get_drvdata(phy);
170 u32 val;
172 if (readl_poll_timeout(ep->phy_base + PCIE_PHY_PLL_LOCKED, val,
173 (val != 0), 1, 500)) {
174 dev_err(&phy->dev, "PLL Locked: 0x%x\n", val);
175 return -ETIMEDOUT;
178 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_COMMON_POWER);
179 val |= PCIE_PHY_COMMON_PD_CMN;
180 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_COMMON_POWER);
182 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV0_POWER);
183 val |= PCIE_PHY_TRSV0_PD_TSV;
184 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV0_POWER);
186 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV1_POWER);
187 val |= PCIE_PHY_TRSV1_PD_TSV;
188 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV1_POWER);
190 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV2_POWER);
191 val |= PCIE_PHY_TRSV2_PD_TSV;
192 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV2_POWER);
194 val = exynos_pcie_phy_readl(ep->phy_base, PCIE_PHY_TRSV3_POWER);
195 val |= PCIE_PHY_TRSV3_PD_TSV;
196 exynos_pcie_phy_writel(ep->phy_base, val, PCIE_PHY_TRSV3_POWER);
198 return 0;
201 static int exynos5440_pcie_phy_reset(struct phy *phy)
203 struct exynos_pcie_phy *ep = phy_get_drvdata(phy);
205 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_MAC_RESET);
206 exynos_pcie_phy_writel(ep->blk_base, 1, PCIE_PHY_GLOBAL_RESET);
207 exynos_pcie_phy_writel(ep->blk_base, 0, PCIE_PHY_GLOBAL_RESET);
209 return 0;
212 static const struct phy_ops exynos5440_phy_ops = {
213 .init = exynos5440_pcie_phy_init,
214 .power_on = exynos5440_pcie_phy_power_on,
215 .power_off = exynos5440_pcie_phy_power_off,
216 .reset = exynos5440_pcie_phy_reset,
217 .owner = THIS_MODULE,
220 static const struct exynos_pcie_phy_data exynos5440_pcie_phy_data = {
221 .ops = &exynos5440_phy_ops,
224 static const struct of_device_id exynos_pcie_phy_match[] = {
226 .compatible = "samsung,exynos5440-pcie-phy",
227 .data = &exynos5440_pcie_phy_data,
232 static int exynos_pcie_phy_probe(struct platform_device *pdev)
234 struct device *dev = &pdev->dev;
235 struct exynos_pcie_phy *exynos_phy;
236 struct phy *generic_phy;
237 struct phy_provider *phy_provider;
238 struct resource *res;
239 const struct exynos_pcie_phy_data *drv_data;
241 drv_data = of_device_get_match_data(dev);
242 if (!drv_data)
243 return -ENODEV;
245 exynos_phy = devm_kzalloc(dev, sizeof(*exynos_phy), GFP_KERNEL);
246 if (!exynos_phy)
247 return -ENOMEM;
249 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
250 exynos_phy->phy_base = devm_ioremap_resource(dev, res);
251 if (IS_ERR(exynos_phy->phy_base))
252 return PTR_ERR(exynos_phy->phy_base);
254 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
255 exynos_phy->blk_base = devm_ioremap_resource(dev, res);
256 if (IS_ERR(exynos_phy->blk_base))
257 return PTR_ERR(exynos_phy->blk_base);
259 exynos_phy->drv_data = drv_data;
261 generic_phy = devm_phy_create(dev, dev->of_node, drv_data->ops);
262 if (IS_ERR(generic_phy)) {
263 dev_err(dev, "failed to create PHY\n");
264 return PTR_ERR(generic_phy);
267 phy_set_drvdata(generic_phy, exynos_phy);
268 phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
270 return PTR_ERR_OR_ZERO(phy_provider);
273 static struct platform_driver exynos_pcie_phy_driver = {
274 .probe = exynos_pcie_phy_probe,
275 .driver = {
276 .of_match_table = exynos_pcie_phy_match,
277 .name = "exynos_pcie_phy",
281 builtin_platform_driver(exynos_pcie_phy_driver);