1 // SPDX-License-Identifier: GPL-2.0-only
3 * COMBPHY driver for HiSilicon STB SoCs
5 * Copyright (C) 2016-2017 HiSilicon Co., Ltd. http://www.hisilicon.com
7 * Authors: Jianguo Sun <sunjianguo1@huawei.com>
10 #include <linux/clk.h>
11 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
17 #include <linux/phy/phy.h>
18 #include <linux/platform_device.h>
19 #include <linux/regmap.h>
20 #include <linux/reset.h>
21 #include <dt-bindings/phy/phy.h>
23 #define COMBPHY_MODE_PCIE 0
24 #define COMBPHY_MODE_USB3 1
25 #define COMBPHY_MODE_SATA 2
27 #define COMBPHY_CFG_REG 0x0
28 #define COMBPHY_BYPASS_CODEC BIT(31)
29 #define COMBPHY_TEST_WRITE BIT(24)
30 #define COMBPHY_TEST_DATA_SHIFT 20
31 #define COMBPHY_TEST_DATA_MASK GENMASK(23, 20)
32 #define COMBPHY_TEST_ADDR_SHIFT 12
33 #define COMBPHY_TEST_ADDR_MASK GENMASK(16, 12)
34 #define COMBPHY_CLKREF_OUT_OEN BIT(0)
36 struct histb_combphy_mode
{
44 struct histb_combphy_priv
{
46 struct regmap
*syscon
;
47 struct reset_control
*por_rst
;
50 struct histb_combphy_mode mode
;
53 static void nano_register_write(struct histb_combphy_priv
*priv
,
56 void __iomem
*reg
= priv
->mmio
+ COMBPHY_CFG_REG
;
59 /* Set up address and data for the write */
61 val
&= ~COMBPHY_TEST_ADDR_MASK
;
62 val
|= addr
<< COMBPHY_TEST_ADDR_SHIFT
;
63 val
&= ~COMBPHY_TEST_DATA_MASK
;
64 val
|= data
<< COMBPHY_TEST_DATA_SHIFT
;
67 /* Flip strobe control to trigger the write */
68 val
&= ~COMBPHY_TEST_WRITE
;
70 val
|= COMBPHY_TEST_WRITE
;
74 static int is_mode_fixed(struct histb_combphy_mode
*mode
)
76 return (mode
->fixed
!= PHY_NONE
) ? true : false;
79 static int histb_combphy_set_mode(struct histb_combphy_priv
*priv
)
81 struct histb_combphy_mode
*mode
= &priv
->mode
;
82 struct regmap
*syscon
= priv
->syscon
;
85 if (is_mode_fixed(mode
))
88 switch (mode
->select
) {
90 hw_sel
= COMBPHY_MODE_SATA
;
93 hw_sel
= COMBPHY_MODE_PCIE
;
96 hw_sel
= COMBPHY_MODE_USB3
;
102 return regmap_update_bits(syscon
, mode
->reg
, mode
->mask
,
103 hw_sel
<< mode
->shift
);
106 static int histb_combphy_init(struct phy
*phy
)
108 struct histb_combphy_priv
*priv
= phy_get_drvdata(phy
);
112 ret
= histb_combphy_set_mode(priv
);
116 /* Clear bypass bit to enable encoding/decoding */
117 val
= readl(priv
->mmio
+ COMBPHY_CFG_REG
);
118 val
&= ~COMBPHY_BYPASS_CODEC
;
119 writel(val
, priv
->mmio
+ COMBPHY_CFG_REG
);
121 ret
= clk_prepare_enable(priv
->ref_clk
);
125 reset_control_deassert(priv
->por_rst
);
127 /* Enable EP clock */
128 val
= readl(priv
->mmio
+ COMBPHY_CFG_REG
);
129 val
|= COMBPHY_CLKREF_OUT_OEN
;
130 writel(val
, priv
->mmio
+ COMBPHY_CFG_REG
);
132 /* Need to wait for EP clock stable */
135 /* Configure nano phy registers as suggested by vendor */
136 nano_register_write(priv
, 0x1, 0x8);
137 nano_register_write(priv
, 0xc, 0x9);
138 nano_register_write(priv
, 0x1a, 0x4);
143 static int histb_combphy_exit(struct phy
*phy
)
145 struct histb_combphy_priv
*priv
= phy_get_drvdata(phy
);
148 /* Disable EP clock */
149 val
= readl(priv
->mmio
+ COMBPHY_CFG_REG
);
150 val
&= ~COMBPHY_CLKREF_OUT_OEN
;
151 writel(val
, priv
->mmio
+ COMBPHY_CFG_REG
);
153 reset_control_assert(priv
->por_rst
);
154 clk_disable_unprepare(priv
->ref_clk
);
159 static const struct phy_ops histb_combphy_ops
= {
160 .init
= histb_combphy_init
,
161 .exit
= histb_combphy_exit
,
162 .owner
= THIS_MODULE
,
165 static struct phy
*histb_combphy_xlate(struct device
*dev
,
166 const struct of_phandle_args
*args
)
168 struct histb_combphy_priv
*priv
= dev_get_drvdata(dev
);
169 struct histb_combphy_mode
*mode
= &priv
->mode
;
171 if (args
->args_count
< 1) {
172 dev_err(dev
, "invalid number of arguments\n");
173 return ERR_PTR(-EINVAL
);
176 mode
->select
= args
->args
[0];
178 if (mode
->select
< PHY_TYPE_SATA
|| mode
->select
> PHY_TYPE_USB3
) {
179 dev_err(dev
, "invalid phy mode select argument\n");
180 return ERR_PTR(-EINVAL
);
183 if (is_mode_fixed(mode
) && mode
->select
!= mode
->fixed
) {
184 dev_err(dev
, "mode select %d mismatch fixed phy mode %d\n",
185 mode
->select
, mode
->fixed
);
186 return ERR_PTR(-EINVAL
);
192 static int histb_combphy_probe(struct platform_device
*pdev
)
194 struct phy_provider
*phy_provider
;
195 struct device
*dev
= &pdev
->dev
;
196 struct histb_combphy_priv
*priv
;
197 struct device_node
*np
= dev
->of_node
;
198 struct histb_combphy_mode
*mode
;
202 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
206 priv
->mmio
= devm_platform_ioremap_resource(pdev
, 0);
207 if (IS_ERR(priv
->mmio
)) {
208 ret
= PTR_ERR(priv
->mmio
);
212 priv
->syscon
= syscon_node_to_regmap(np
->parent
);
213 if (IS_ERR(priv
->syscon
)) {
214 dev_err(dev
, "failed to find peri_ctrl syscon regmap\n");
215 return PTR_ERR(priv
->syscon
);
219 mode
->fixed
= PHY_NONE
;
221 ret
= of_property_read_u32(np
, "hisilicon,fixed-mode", &mode
->fixed
);
223 dev_dbg(dev
, "found fixed phy mode %d\n", mode
->fixed
);
225 ret
= of_property_read_u32_array(np
, "hisilicon,mode-select-bits",
226 vals
, ARRAY_SIZE(vals
));
228 if (is_mode_fixed(mode
)) {
229 dev_err(dev
, "found select bits for fixed mode phy\n");
234 mode
->shift
= vals
[1];
235 mode
->mask
= vals
[2];
236 dev_dbg(dev
, "found mode select bits\n");
238 if (!is_mode_fixed(mode
)) {
239 dev_err(dev
, "no valid select bits found for non-fixed phy\n");
244 priv
->ref_clk
= devm_clk_get(dev
, NULL
);
245 if (IS_ERR(priv
->ref_clk
)) {
246 dev_err(dev
, "failed to find ref clock\n");
247 return PTR_ERR(priv
->ref_clk
);
250 priv
->por_rst
= devm_reset_control_get(dev
, NULL
);
251 if (IS_ERR(priv
->por_rst
)) {
252 dev_err(dev
, "failed to get poweron reset\n");
253 return PTR_ERR(priv
->por_rst
);
256 priv
->phy
= devm_phy_create(dev
, NULL
, &histb_combphy_ops
);
257 if (IS_ERR(priv
->phy
)) {
258 dev_err(dev
, "failed to create combphy\n");
259 return PTR_ERR(priv
->phy
);
262 dev_set_drvdata(dev
, priv
);
263 phy_set_drvdata(priv
->phy
, priv
);
265 phy_provider
= devm_of_phy_provider_register(dev
, histb_combphy_xlate
);
266 return PTR_ERR_OR_ZERO(phy_provider
);
269 static const struct of_device_id histb_combphy_of_match
[] = {
270 { .compatible
= "hisilicon,hi3798cv200-combphy" },
273 MODULE_DEVICE_TABLE(of
, histb_combphy_of_match
);
275 static struct platform_driver histb_combphy_driver
= {
276 .probe
= histb_combphy_probe
,
279 .of_match_table
= histb_combphy_of_match
,
282 module_platform_driver(histb_combphy_driver
);
284 MODULE_DESCRIPTION("HiSilicon STB COMBPHY driver");
285 MODULE_LICENSE("GPL v2");