1 // SPDX-License-Identifier: GPL-2.0-only
3 * Samsung SATA SerDes(PHY) driver
5 * Copyright (C) 2013 Samsung Electronics Co., Ltd.
6 * Authors: Girish K S <ks.giri@samsung.com>
7 * Yuvaraj Kumar C D <yuvaraj.cd@samsung.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/phy/phy.h>
19 #include <linux/platform_device.h>
20 #include <linux/regmap.h>
21 #include <linux/spinlock.h>
22 #include <linux/mfd/syscon.h>
24 #define SATAPHY_CONTROL_OFFSET 0x0724
25 #define EXYNOS5_SATAPHY_PMU_ENABLE BIT(0)
26 #define EXYNOS5_SATA_RESET 0x4
27 #define RESET_GLOBAL_RST_N BIT(0)
28 #define RESET_CMN_RST_N BIT(1)
29 #define RESET_CMN_BLOCK_RST_N BIT(2)
30 #define RESET_CMN_I2C_RST_N BIT(3)
31 #define RESET_TX_RX_PIPE_RST_N BIT(4)
32 #define RESET_TX_RX_BLOCK_RST_N BIT(5)
33 #define RESET_TX_RX_I2C_RST_N (BIT(6) | BIT(7))
34 #define LINK_RESET 0xf0000
35 #define EXYNOS5_SATA_MODE0 0x10
36 #define SATA_SPD_GEN3 BIT(1)
37 #define EXYNOS5_SATA_CTRL0 0x14
38 #define CTRL0_P0_PHY_CALIBRATED_SEL BIT(9)
39 #define CTRL0_P0_PHY_CALIBRATED BIT(8)
40 #define EXYNOS5_SATA_PHSATA_CTRLM 0xe0
41 #define PHCTRLM_REF_RATE BIT(1)
42 #define PHCTRLM_HIGH_SPEED BIT(0)
43 #define EXYNOS5_SATA_PHSATA_STATM 0xf0
44 #define PHSTATM_PLL_LOCKED BIT(0)
46 #define PHY_PLL_TIMEOUT (usecs_to_jiffies(1000))
48 struct exynos_sata_phy
{
52 struct regmap
*pmureg
;
53 struct i2c_client
*client
;
56 static int wait_for_reg_status(void __iomem
*base
, u32 reg
, u32 checkbit
,
59 unsigned long timeout
= jiffies
+ PHY_PLL_TIMEOUT
;
61 while (time_before(jiffies
, timeout
)) {
62 if ((readl(base
+ reg
) & checkbit
) == status
)
69 static int exynos_sata_phy_power_on(struct phy
*phy
)
71 struct exynos_sata_phy
*sata_phy
= phy_get_drvdata(phy
);
73 return regmap_update_bits(sata_phy
->pmureg
, SATAPHY_CONTROL_OFFSET
,
74 EXYNOS5_SATAPHY_PMU_ENABLE
, true);
78 static int exynos_sata_phy_power_off(struct phy
*phy
)
80 struct exynos_sata_phy
*sata_phy
= phy_get_drvdata(phy
);
82 return regmap_update_bits(sata_phy
->pmureg
, SATAPHY_CONTROL_OFFSET
,
83 EXYNOS5_SATAPHY_PMU_ENABLE
, false);
87 static int exynos_sata_phy_init(struct phy
*phy
)
91 u8 buf
[] = { 0x3a, 0x0b };
92 struct exynos_sata_phy
*sata_phy
= phy_get_drvdata(phy
);
94 ret
= regmap_update_bits(sata_phy
->pmureg
, SATAPHY_CONTROL_OFFSET
,
95 EXYNOS5_SATAPHY_PMU_ENABLE
, true);
97 dev_err(&sata_phy
->phy
->dev
, "phy init failed\n");
99 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
101 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
102 val
|= RESET_GLOBAL_RST_N
| RESET_CMN_RST_N
| RESET_CMN_BLOCK_RST_N
103 | RESET_CMN_I2C_RST_N
| RESET_TX_RX_PIPE_RST_N
104 | RESET_TX_RX_BLOCK_RST_N
| RESET_TX_RX_I2C_RST_N
;
105 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
107 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
109 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
111 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
112 val
|= RESET_CMN_RST_N
;
113 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
115 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_PHSATA_CTRLM
);
116 val
&= ~PHCTRLM_REF_RATE
;
117 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_PHSATA_CTRLM
);
119 /* High speed enable for Gen3 */
120 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_PHSATA_CTRLM
);
121 val
|= PHCTRLM_HIGH_SPEED
;
122 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_PHSATA_CTRLM
);
124 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_CTRL0
);
125 val
|= CTRL0_P0_PHY_CALIBRATED_SEL
| CTRL0_P0_PHY_CALIBRATED
;
126 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_CTRL0
);
128 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_MODE0
);
129 val
|= SATA_SPD_GEN3
;
130 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_MODE0
);
132 ret
= i2c_master_send(sata_phy
->client
, buf
, sizeof(buf
));
136 /* release cmu reset */
137 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
138 val
&= ~RESET_CMN_RST_N
;
139 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
141 val
= readl(sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
142 val
|= RESET_CMN_RST_N
;
143 writel(val
, sata_phy
->regs
+ EXYNOS5_SATA_RESET
);
145 ret
= wait_for_reg_status(sata_phy
->regs
,
146 EXYNOS5_SATA_PHSATA_STATM
,
147 PHSTATM_PLL_LOCKED
, 1);
149 dev_err(&sata_phy
->phy
->dev
,
150 "PHY PLL locking failed\n");
154 static const struct phy_ops exynos_sata_phy_ops
= {
155 .init
= exynos_sata_phy_init
,
156 .power_on
= exynos_sata_phy_power_on
,
157 .power_off
= exynos_sata_phy_power_off
,
158 .owner
= THIS_MODULE
,
161 static int exynos_sata_phy_probe(struct platform_device
*pdev
)
163 struct exynos_sata_phy
*sata_phy
;
164 struct device
*dev
= &pdev
->dev
;
165 struct resource
*res
;
166 struct phy_provider
*phy_provider
;
167 struct device_node
*node
;
170 sata_phy
= devm_kzalloc(dev
, sizeof(*sata_phy
), GFP_KERNEL
);
174 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
176 sata_phy
->regs
= devm_ioremap_resource(dev
, res
);
177 if (IS_ERR(sata_phy
->regs
))
178 return PTR_ERR(sata_phy
->regs
);
180 sata_phy
->pmureg
= syscon_regmap_lookup_by_phandle(dev
->of_node
,
181 "samsung,syscon-phandle");
182 if (IS_ERR(sata_phy
->pmureg
)) {
183 dev_err(dev
, "syscon regmap lookup failed.\n");
184 return PTR_ERR(sata_phy
->pmureg
);
187 node
= of_parse_phandle(dev
->of_node
,
188 "samsung,exynos-sataphy-i2c-phandle", 0);
192 sata_phy
->client
= of_find_i2c_device_by_node(node
);
193 if (!sata_phy
->client
)
194 return -EPROBE_DEFER
;
196 dev_set_drvdata(dev
, sata_phy
);
198 sata_phy
->phyclk
= devm_clk_get(dev
, "sata_phyctrl");
199 if (IS_ERR(sata_phy
->phyclk
)) {
200 dev_err(dev
, "failed to get clk for PHY\n");
201 return PTR_ERR(sata_phy
->phyclk
);
204 ret
= clk_prepare_enable(sata_phy
->phyclk
);
206 dev_err(dev
, "failed to enable source clk\n");
210 sata_phy
->phy
= devm_phy_create(dev
, NULL
, &exynos_sata_phy_ops
);
211 if (IS_ERR(sata_phy
->phy
)) {
212 clk_disable_unprepare(sata_phy
->phyclk
);
213 dev_err(dev
, "failed to create PHY\n");
214 return PTR_ERR(sata_phy
->phy
);
217 phy_set_drvdata(sata_phy
->phy
, sata_phy
);
219 phy_provider
= devm_of_phy_provider_register(dev
,
220 of_phy_simple_xlate
);
221 if (IS_ERR(phy_provider
)) {
222 clk_disable_unprepare(sata_phy
->phyclk
);
223 return PTR_ERR(phy_provider
);
229 static const struct of_device_id exynos_sata_phy_of_match
[] = {
230 { .compatible
= "samsung,exynos5250-sata-phy" },
233 MODULE_DEVICE_TABLE(of
, exynos_sata_phy_of_match
);
235 static struct platform_driver exynos_sata_phy_driver
= {
236 .probe
= exynos_sata_phy_probe
,
238 .of_match_table
= exynos_sata_phy_of_match
,
239 .name
= "samsung,sata-phy",
240 .suppress_bind_attrs
= true,
243 module_platform_driver(exynos_sata_phy_driver
);
245 MODULE_DESCRIPTION("Samsung SerDes PHY driver");
246 MODULE_LICENSE("GPL v2");
247 MODULE_AUTHOR("Girish K S <ks.giri@samsung.com>");
248 MODULE_AUTHOR("Yuvaraj C D <yuvaraj.cd@samsung.com>");