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>
13 #include <linux/phy/phy.h>
15 #include <linux/platform_device.h>
17 #define HOST_VSA_ADDR 0x0
18 #define HOST_VSA_DATA 0x4
19 #define PORT_SCR_CTL 0x2c
20 #define PORT_VSR_ADDR 0x78
21 #define PORT_VSR_DATA 0x7c
23 #define CONTROL_REGISTER 0x0
24 #define MBUS_SIZE_CONTROL 0x4
26 #define POWER_DOWN_PHY0 BIT(6)
27 #define POWER_DOWN_PHY1 BIT(14)
28 #define MBUS_WRITE_REQUEST_SIZE_128 (BIT(2) << 16)
29 #define MBUS_READ_REQUEST_SIZE_128 (BIT(2) << 19)
31 #define BG2_PHY_BASE 0x080
32 #define BG2Q_PHY_BASE 0x200
35 #define REF_FREF_SEL_25 BIT(0)
36 #define PHY_BERLIN_MODE_SATA (0x0 << 5)
39 #define USE_MAX_PLL_RATE BIT(12)
42 #define DATA_BIT_WIDTH_10 (0x0 << 10)
43 #define DATA_BIT_WIDTH_20 (0x1 << 10)
44 #define DATA_BIT_WIDTH_40 (0x2 << 10)
47 #define PHY_GEN_MAX_1_5 (0x0 << 10)
48 #define PHY_GEN_MAX_3_0 (0x1 << 10)
49 #define PHY_GEN_MAX_6_0 (0x2 << 10)
51 struct phy_berlin_desc
{
57 struct phy_berlin_priv
{
61 struct phy_berlin_desc
**phys
;
66 static inline void phy_berlin_sata_reg_setbits(void __iomem
*ctrl_reg
,
67 u32 phy_base
, u32 reg
, u32 mask
, u32 val
)
72 writel(phy_base
+ reg
, ctrl_reg
+ PORT_VSR_ADDR
);
75 regval
= readl(ctrl_reg
+ PORT_VSR_DATA
);
78 writel(regval
, ctrl_reg
+ PORT_VSR_DATA
);
81 static int phy_berlin_sata_power_on(struct phy
*phy
)
83 struct phy_berlin_desc
*desc
= phy_get_drvdata(phy
);
84 struct phy_berlin_priv
*priv
= dev_get_drvdata(phy
->dev
.parent
);
85 void __iomem
*ctrl_reg
= priv
->base
+ 0x60 + (desc
->index
* 0x80);
88 clk_prepare_enable(priv
->clk
);
90 spin_lock(&priv
->lock
);
93 writel(CONTROL_REGISTER
, priv
->base
+ HOST_VSA_ADDR
);
94 regval
= readl(priv
->base
+ HOST_VSA_DATA
);
95 regval
&= ~desc
->power_bit
;
96 writel(regval
, priv
->base
+ HOST_VSA_DATA
);
99 writel(MBUS_SIZE_CONTROL
, priv
->base
+ HOST_VSA_ADDR
);
100 regval
= readl(priv
->base
+ HOST_VSA_DATA
);
101 regval
|= MBUS_WRITE_REQUEST_SIZE_128
| MBUS_READ_REQUEST_SIZE_128
;
102 writel(regval
, priv
->base
+ HOST_VSA_DATA
);
104 /* set PHY mode and ref freq to 25 MHz */
105 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x01,
107 REF_FREF_SEL_25
| PHY_BERLIN_MODE_SATA
);
109 /* set PHY up to 6 Gbps */
110 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x25,
111 0x0c00, PHY_GEN_MAX_6_0
);
113 /* set 40 bits width */
114 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x23,
115 0x0c00, DATA_BIT_WIDTH_40
);
117 /* use max pll rate */
118 phy_berlin_sata_reg_setbits(ctrl_reg
, priv
->phy_base
, 0x02,
119 0x0000, USE_MAX_PLL_RATE
);
121 /* set Gen3 controller speed */
122 regval
= readl(ctrl_reg
+ PORT_SCR_CTL
);
123 regval
&= ~GENMASK(7, 4);
125 writel(regval
, ctrl_reg
+ PORT_SCR_CTL
);
127 spin_unlock(&priv
->lock
);
129 clk_disable_unprepare(priv
->clk
);
134 static int phy_berlin_sata_power_off(struct phy
*phy
)
136 struct phy_berlin_desc
*desc
= phy_get_drvdata(phy
);
137 struct phy_berlin_priv
*priv
= dev_get_drvdata(phy
->dev
.parent
);
140 clk_prepare_enable(priv
->clk
);
142 spin_lock(&priv
->lock
);
145 writel(CONTROL_REGISTER
, priv
->base
+ HOST_VSA_ADDR
);
146 regval
= readl(priv
->base
+ HOST_VSA_DATA
);
147 regval
|= desc
->power_bit
;
148 writel(regval
, priv
->base
+ HOST_VSA_DATA
);
150 spin_unlock(&priv
->lock
);
152 clk_disable_unprepare(priv
->clk
);
157 static struct phy
*phy_berlin_sata_phy_xlate(struct device
*dev
,
158 const struct of_phandle_args
*args
)
160 struct phy_berlin_priv
*priv
= dev_get_drvdata(dev
);
163 if (WARN_ON(args
->args
[0] >= priv
->nphys
))
164 return ERR_PTR(-ENODEV
);
166 for (i
= 0; i
< priv
->nphys
; i
++) {
167 if (priv
->phys
[i
]->index
== args
->args
[0])
171 if (i
== priv
->nphys
)
172 return ERR_PTR(-ENODEV
);
174 return priv
->phys
[i
]->phy
;
177 static const struct phy_ops phy_berlin_sata_ops
= {
178 .power_on
= phy_berlin_sata_power_on
,
179 .power_off
= phy_berlin_sata_power_off
,
180 .owner
= THIS_MODULE
,
183 static u32 phy_berlin_power_down_bits
[] = {
188 static int phy_berlin_sata_probe(struct platform_device
*pdev
)
190 struct device
*dev
= &pdev
->dev
;
191 struct device_node
*child
;
193 struct phy_provider
*phy_provider
;
194 struct phy_berlin_priv
*priv
;
195 struct resource
*res
;
199 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
203 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
207 priv
->base
= devm_ioremap(dev
, res
->start
, resource_size(res
));
211 priv
->clk
= devm_clk_get(dev
, NULL
);
212 if (IS_ERR(priv
->clk
))
213 return PTR_ERR(priv
->clk
);
215 priv
->nphys
= of_get_child_count(dev
->of_node
);
216 if (priv
->nphys
== 0)
219 priv
->phys
= devm_kcalloc(dev
, priv
->nphys
, sizeof(*priv
->phys
),
224 if (of_device_is_compatible(dev
->of_node
, "marvell,berlin2-sata-phy"))
225 priv
->phy_base
= BG2_PHY_BASE
;
227 priv
->phy_base
= BG2Q_PHY_BASE
;
229 dev_set_drvdata(dev
, priv
);
230 spin_lock_init(&priv
->lock
);
232 for_each_available_child_of_node(dev
->of_node
, child
) {
233 struct phy_berlin_desc
*phy_desc
;
235 if (of_property_read_u32(child
, "reg", &phy_id
)) {
236 dev_err(dev
, "missing reg property in node %pOFn\n",
242 if (phy_id
>= ARRAY_SIZE(phy_berlin_power_down_bits
)) {
243 dev_err(dev
, "invalid reg in node %pOFn\n", child
);
248 phy_desc
= devm_kzalloc(dev
, sizeof(*phy_desc
), GFP_KERNEL
);
254 phy
= devm_phy_create(dev
, NULL
, &phy_berlin_sata_ops
);
256 dev_err(dev
, "failed to create PHY %d\n", phy_id
);
262 phy_desc
->power_bit
= phy_berlin_power_down_bits
[phy_id
];
263 phy_desc
->index
= phy_id
;
264 phy_set_drvdata(phy
, phy_desc
);
266 priv
->phys
[i
++] = phy_desc
;
268 /* Make sure the PHY is off */
269 phy_berlin_sata_power_off(phy
);
273 devm_of_phy_provider_register(dev
, phy_berlin_sata_phy_xlate
);
274 return PTR_ERR_OR_ZERO(phy_provider
);
280 static const struct of_device_id phy_berlin_sata_of_match
[] = {
281 { .compatible
= "marvell,berlin2-sata-phy" },
282 { .compatible
= "marvell,berlin2q-sata-phy" },
285 MODULE_DEVICE_TABLE(of
, phy_berlin_sata_of_match
);
287 static struct platform_driver phy_berlin_sata_driver
= {
288 .probe
= phy_berlin_sata_probe
,
290 .name
= "phy-berlin-sata",
291 .of_match_table
= phy_berlin_sata_of_match
,
294 module_platform_driver(phy_berlin_sata_driver
);
296 MODULE_DESCRIPTION("Marvell Berlin SATA PHY driver");
297 MODULE_AUTHOR("Antoine Ténart <antoine.tenart@free-electrons.com>");
298 MODULE_LICENSE("GPL v2");