1 // SPDX-License-Identifier: GPL-2.0
3 * Marvell Berlin SATA PHY driver
5 * Copyright (C) 2014 Marvell Technology Group Ltd.
7 * Antoine Ténart <antoine.tenart@free-electrons.com>
10 #include <linux/clk.h>
11 #include <linux/module.h>
12 #include <linux/phy/phy.h>
14 #include <linux/platform_device.h>
16 #define HOST_VSA_ADDR 0x0
17 #define HOST_VSA_DATA 0x4
18 #define PORT_SCR_CTL 0x2c
19 #define PORT_VSR_ADDR 0x78
20 #define PORT_VSR_DATA 0x7c
22 #define CONTROL_REGISTER 0x0
23 #define MBUS_SIZE_CONTROL 0x4
25 #define POWER_DOWN_PHY0 BIT(6)
26 #define POWER_DOWN_PHY1 BIT(14)
27 #define MBUS_WRITE_REQUEST_SIZE_128 (BIT(2) << 16)
28 #define MBUS_READ_REQUEST_SIZE_128 (BIT(2) << 19)
30 #define BG2_PHY_BASE 0x080
31 #define BG2Q_PHY_BASE 0x200
34 #define REF_FREF_SEL_25 BIT(0)
35 #define PHY_BERLIN_MODE_SATA (0x0 << 5)
38 #define USE_MAX_PLL_RATE BIT(12)
41 #define DATA_BIT_WIDTH_10 (0x0 << 10)
42 #define DATA_BIT_WIDTH_20 (0x1 << 10)
43 #define DATA_BIT_WIDTH_40 (0x2 << 10)
46 #define PHY_GEN_MAX_1_5 (0x0 << 10)
47 #define PHY_GEN_MAX_3_0 (0x1 << 10)
48 #define PHY_GEN_MAX_6_0 (0x2 << 10)
50 struct phy_berlin_desc
{
56 struct phy_berlin_priv
{
60 struct phy_berlin_desc
**phys
;
65 static inline void phy_berlin_sata_reg_setbits(void __iomem
*ctrl_reg
,
66 u32 phy_base
, u32 reg
, u32 mask
, u32 val
)
71 writel(phy_base
+ reg
, ctrl_reg
+ PORT_VSR_ADDR
);
74 regval
= readl(ctrl_reg
+ PORT_VSR_DATA
);
77 writel(regval
, ctrl_reg
+ PORT_VSR_DATA
);
80 static int phy_berlin_sata_power_on(struct phy
*phy
)
82 struct phy_berlin_desc
*desc
= phy_get_drvdata(phy
);
83 struct phy_berlin_priv
*priv
= dev_get_drvdata(phy
->dev
.parent
);
84 void __iomem
*ctrl_reg
= priv
->base
+ 0x60 + (desc
->index
* 0x80);
87 clk_prepare_enable(priv
->clk
);
89 spin_lock(&priv
->lock
);
92 writel(CONTROL_REGISTER
, priv
->base
+ HOST_VSA_ADDR
);
93 regval
= readl(priv
->base
+ HOST_VSA_DATA
);
94 regval
&= ~desc
->power_bit
;
95 writel(regval
, priv
->base
+ HOST_VSA_DATA
);
98 writel(MBUS_SIZE_CONTROL
, priv
->base
+ HOST_VSA_ADDR
);
99 regval
= readl(priv
->base
+ HOST_VSA_DATA
);
100 regval
|= MBUS_WRITE_REQUEST_SIZE_128
| MBUS_READ_REQUEST_SIZE_128
;
101 writel(regval
, priv
->base
+ HOST_VSA_DATA
);
103 /* set PHY mode and ref freq to 25 MHz */
104 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x01,
106 REF_FREF_SEL_25
| PHY_BERLIN_MODE_SATA
);
108 /* set PHY up to 6 Gbps */
109 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x25,
110 0x0c00, PHY_GEN_MAX_6_0
);
112 /* set 40 bits width */
113 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x23,
114 0x0c00, DATA_BIT_WIDTH_40
);
116 /* use max pll rate */
117 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x02,
118 0x0000, USE_MAX_PLL_RATE
);
120 /* set Gen3 controller speed */
121 regval
= readl(ctrl_reg
+ PORT_SCR_CTL
);
122 regval
&= ~GENMASK(7, 4);
124 writel(regval
, ctrl_reg
+ PORT_SCR_CTL
);
126 spin_unlock(&priv
->lock
);
128 clk_disable_unprepare(priv
->clk
);
133 static int phy_berlin_sata_power_off(struct phy
*phy
)
135 struct phy_berlin_desc
*desc
= phy_get_drvdata(phy
);
136 struct phy_berlin_priv
*priv
= dev_get_drvdata(phy
->dev
.parent
);
139 clk_prepare_enable(priv
->clk
);
141 spin_lock(&priv
->lock
);
144 writel(CONTROL_REGISTER
, priv
->base
+ HOST_VSA_ADDR
);
145 regval
= readl(priv
->base
+ HOST_VSA_DATA
);
146 regval
|= desc
->power_bit
;
147 writel(regval
, priv
->base
+ HOST_VSA_DATA
);
149 spin_unlock(&priv
->lock
);
151 clk_disable_unprepare(priv
->clk
);
156 static struct phy
*phy_berlin_sata_phy_xlate(struct device
*dev
,
157 struct of_phandle_args
*args
)
159 struct phy_berlin_priv
*priv
= dev_get_drvdata(dev
);
162 if (WARN_ON(args
->args
[0] >= priv
->nphys
))
163 return ERR_PTR(-ENODEV
);
165 for (i
= 0; i
< priv
->nphys
; i
++) {
166 if (priv
->phys
[i
]->index
== args
->args
[0])
170 if (i
== priv
->nphys
)
171 return ERR_PTR(-ENODEV
);
173 return priv
->phys
[i
]->phy
;
176 static const struct phy_ops phy_berlin_sata_ops
= {
177 .power_on
= phy_berlin_sata_power_on
,
178 .power_off
= phy_berlin_sata_power_off
,
179 .owner
= THIS_MODULE
,
182 static u32 phy_berlin_power_down_bits
[] = {
187 static int phy_berlin_sata_probe(struct platform_device
*pdev
)
189 struct device
*dev
= &pdev
->dev
;
190 struct device_node
*child
;
192 struct phy_provider
*phy_provider
;
193 struct phy_berlin_priv
*priv
;
194 struct resource
*res
;
198 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
202 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
206 priv
->base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
210 priv
->clk
= devm_clk_get(dev
, NULL
);
211 if (IS_ERR(priv
->clk
))
212 return PTR_ERR(priv
->clk
);
214 priv
->nphys
= of_get_child_count(dev
->of_node
);
215 if (priv
->nphys
== 0)
218 priv
->phys
= devm_kcalloc(dev
, priv
->nphys
, sizeof(*priv
->phys
),
223 if (of_device_is_compatible(dev
->of_node
, "marvell,berlin2-sata-phy"))
224 priv
->phy_base
= BG2_PHY_BASE
;
226 priv
->phy_base
= BG2Q_PHY_BASE
;
228 dev_set_drvdata(dev
, priv
);
229 spin_lock_init(&priv
->lock
);
231 for_each_available_child_of_node(dev
->of_node
, child
) {
232 struct phy_berlin_desc
*phy_desc
;
234 if (of_property_read_u32(child
, "reg", &phy_id
)) {
235 dev_err(dev
, "missing reg property in node %pOFn\n",
241 if (phy_id
>= ARRAY_SIZE(phy_berlin_power_down_bits
)) {
242 dev_err(dev
, "invalid reg in node %pOFn\n", child
);
247 phy_desc
= devm_kzalloc(dev
, sizeof(*phy_desc
), GFP_KERNEL
);
253 phy
= devm_phy_create(dev
, NULL
, &phy_berlin_sata_ops
);
255 dev_err(dev
, "failed to create PHY %d\n", phy_id
);
261 phy_desc
->power_bit
= phy_berlin_power_down_bits
[phy_id
];
262 phy_desc
->index
= phy_id
;
263 phy_set_drvdata(phy
, phy_desc
);
265 priv
->phys
[i
++] = phy_desc
;
267 /* Make sure the PHY is off */
268 phy_berlin_sata_power_off(phy
);
272 devm_of_phy_provider_register(dev
, phy_berlin_sata_phy_xlate
);
273 return PTR_ERR_OR_ZERO(phy_provider
);
279 static const struct of_device_id phy_berlin_sata_of_match
[] = {
280 { .compatible
= "marvell,berlin2-sata-phy" },
281 { .compatible
= "marvell,berlin2q-sata-phy" },
284 MODULE_DEVICE_TABLE(of
, phy_berlin_sata_of_match
);
286 static struct platform_driver phy_berlin_sata_driver
= {
287 .probe
= phy_berlin_sata_probe
,
289 .name
= "phy-berlin-sata",
290 .of_match_table
= phy_berlin_sata_of_match
,
293 module_platform_driver(phy_berlin_sata_driver
);
295 MODULE_DESCRIPTION("Marvell Berlin SATA PHY driver");
296 MODULE_AUTHOR("Antoine Ténart <antoine.tenart@free-electrons.com>");
297 MODULE_LICENSE("GPL v2");