1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright Altera Corporation (C) 2016. All rights reserved.
4 * Author: Tien Hock Loh <thloh@altera.com>
7 #include <linux/mfd/syscon.h>
9 #include <linux/of_address.h>
10 #include <linux/of_net.h>
11 #include <linux/phy.h>
12 #include <linux/regmap.h>
13 #include <linux/reset.h>
14 #include <linux/stmmac.h>
17 #include "stmmac_platform.h"
18 #include "altr_tse_pcs.h"
20 #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0
21 #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII BIT(1)
22 #define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII BIT(2)
23 #define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2
24 #define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK GENMASK(1, 0)
26 #define TSE_PCS_CONTROL_AN_EN_MASK BIT(12)
27 #define TSE_PCS_CONTROL_REG 0x00
28 #define TSE_PCS_CONTROL_RESTART_AN_MASK BIT(9)
29 #define TSE_PCS_CTRL_AUTONEG_SGMII 0x1140
30 #define TSE_PCS_IF_MODE_REG 0x28
31 #define TSE_PCS_LINK_TIMER_0_REG 0x24
32 #define TSE_PCS_LINK_TIMER_1_REG 0x26
33 #define TSE_PCS_SIZE 0x40
34 #define TSE_PCS_STATUS_AN_COMPLETED_MASK BIT(5)
35 #define TSE_PCS_STATUS_LINK_MASK 0x0004
36 #define TSE_PCS_STATUS_REG 0x02
37 #define TSE_PCS_SGMII_SPEED_1000 BIT(3)
38 #define TSE_PCS_SGMII_SPEED_100 BIT(2)
39 #define TSE_PCS_SGMII_SPEED_10 0x0
40 #define TSE_PCS_SW_RST_MASK 0x8000
41 #define TSE_PCS_PARTNER_ABILITY_REG 0x0A
42 #define TSE_PCS_PARTNER_DUPLEX_FULL 0x1000
43 #define TSE_PCS_PARTNER_DUPLEX_HALF 0x0000
44 #define TSE_PCS_PARTNER_DUPLEX_MASK 0x1000
45 #define TSE_PCS_PARTNER_SPEED_MASK GENMASK(11, 10)
46 #define TSE_PCS_PARTNER_SPEED_1000 BIT(11)
47 #define TSE_PCS_PARTNER_SPEED_100 BIT(10)
48 #define TSE_PCS_PARTNER_SPEED_10 0x0000
49 #define TSE_PCS_PARTNER_SPEED_1000 BIT(11)
50 #define TSE_PCS_PARTNER_SPEED_100 BIT(10)
51 #define TSE_PCS_PARTNER_SPEED_10 0x0000
52 #define TSE_PCS_SGMII_SPEED_MASK GENMASK(3, 2)
53 #define TSE_PCS_SGMII_LINK_TIMER_0 0x0D40
54 #define TSE_PCS_SGMII_LINK_TIMER_1 0x0003
55 #define TSE_PCS_SW_RESET_TIMEOUT 100
56 #define TSE_PCS_USE_SGMII_AN_MASK BIT(1)
57 #define TSE_PCS_USE_SGMII_ENA BIT(0)
58 #define TSE_PCS_IF_USE_SGMII 0x03
60 #define SGMII_ADAPTER_CTRL_REG 0x00
61 #define SGMII_ADAPTER_DISABLE 0x0001
62 #define SGMII_ADAPTER_ENABLE 0x0000
64 #define AUTONEGO_LINK_TIMER 20
66 static int tse_pcs_reset(void __iomem
*base
, struct tse_pcs
*pcs
)
71 val
= readw(base
+ TSE_PCS_CONTROL_REG
);
72 val
|= TSE_PCS_SW_RST_MASK
;
73 writew(val
, base
+ TSE_PCS_CONTROL_REG
);
75 while (counter
< TSE_PCS_SW_RESET_TIMEOUT
) {
76 val
= readw(base
+ TSE_PCS_CONTROL_REG
);
77 val
&= TSE_PCS_SW_RST_MASK
;
83 if (counter
>= TSE_PCS_SW_RESET_TIMEOUT
) {
84 dev_err(pcs
->dev
, "PCS could not get out of sw reset\n");
91 int tse_pcs_init(void __iomem
*base
, struct tse_pcs
*pcs
)
95 writew(TSE_PCS_IF_USE_SGMII
, base
+ TSE_PCS_IF_MODE_REG
);
97 writew(TSE_PCS_CTRL_AUTONEG_SGMII
, base
+ TSE_PCS_CONTROL_REG
);
99 writew(TSE_PCS_SGMII_LINK_TIMER_0
, base
+ TSE_PCS_LINK_TIMER_0_REG
);
100 writew(TSE_PCS_SGMII_LINK_TIMER_1
, base
+ TSE_PCS_LINK_TIMER_1_REG
);
102 ret
= tse_pcs_reset(base
, pcs
);
104 writew(SGMII_ADAPTER_ENABLE
,
105 pcs
->sgmii_adapter_base
+ SGMII_ADAPTER_CTRL_REG
);
110 static void pcs_link_timer_callback(struct tse_pcs
*pcs
)
113 void __iomem
*tse_pcs_base
= pcs
->tse_pcs_base
;
114 void __iomem
*sgmii_adapter_base
= pcs
->sgmii_adapter_base
;
116 val
= readw(tse_pcs_base
+ TSE_PCS_STATUS_REG
);
117 val
&= TSE_PCS_STATUS_LINK_MASK
;
120 dev_dbg(pcs
->dev
, "Adapter: Link is established\n");
121 writew(SGMII_ADAPTER_ENABLE
,
122 sgmii_adapter_base
+ SGMII_ADAPTER_CTRL_REG
);
124 mod_timer(&pcs
->aneg_link_timer
, jiffies
+
125 msecs_to_jiffies(AUTONEGO_LINK_TIMER
));
129 static void auto_nego_timer_callback(struct tse_pcs
*pcs
)
134 void __iomem
*tse_pcs_base
= pcs
->tse_pcs_base
;
135 void __iomem
*sgmii_adapter_base
= pcs
->sgmii_adapter_base
;
137 val
= readw(tse_pcs_base
+ TSE_PCS_STATUS_REG
);
138 val
&= TSE_PCS_STATUS_AN_COMPLETED_MASK
;
141 dev_dbg(pcs
->dev
, "Adapter: Auto Negotiation is completed\n");
142 val
= readw(tse_pcs_base
+ TSE_PCS_PARTNER_ABILITY_REG
);
143 speed
= val
& TSE_PCS_PARTNER_SPEED_MASK
;
144 duplex
= val
& TSE_PCS_PARTNER_DUPLEX_MASK
;
146 if (speed
== TSE_PCS_PARTNER_SPEED_10
&&
147 duplex
== TSE_PCS_PARTNER_DUPLEX_FULL
)
149 "Adapter: Link Partner is Up - 10/Full\n");
150 else if (speed
== TSE_PCS_PARTNER_SPEED_100
&&
151 duplex
== TSE_PCS_PARTNER_DUPLEX_FULL
)
153 "Adapter: Link Partner is Up - 100/Full\n");
154 else if (speed
== TSE_PCS_PARTNER_SPEED_1000
&&
155 duplex
== TSE_PCS_PARTNER_DUPLEX_FULL
)
157 "Adapter: Link Partner is Up - 1000/Full\n");
158 else if (speed
== TSE_PCS_PARTNER_SPEED_10
&&
159 duplex
== TSE_PCS_PARTNER_DUPLEX_HALF
)
161 "Adapter does not support Half Duplex\n");
162 else if (speed
== TSE_PCS_PARTNER_SPEED_100
&&
163 duplex
== TSE_PCS_PARTNER_DUPLEX_HALF
)
165 "Adapter does not support Half Duplex\n");
166 else if (speed
== TSE_PCS_PARTNER_SPEED_1000
&&
167 duplex
== TSE_PCS_PARTNER_DUPLEX_HALF
)
169 "Adapter does not support Half Duplex\n");
172 "Adapter: Invalid Partner Speed and Duplex\n");
174 if (duplex
== TSE_PCS_PARTNER_DUPLEX_FULL
&&
175 (speed
== TSE_PCS_PARTNER_SPEED_10
||
176 speed
== TSE_PCS_PARTNER_SPEED_100
||
177 speed
== TSE_PCS_PARTNER_SPEED_1000
))
178 writew(SGMII_ADAPTER_ENABLE
,
179 sgmii_adapter_base
+ SGMII_ADAPTER_CTRL_REG
);
181 val
= readw(tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
182 val
|= TSE_PCS_CONTROL_RESTART_AN_MASK
;
183 writew(val
, tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
185 tse_pcs_reset(tse_pcs_base
, pcs
);
186 mod_timer(&pcs
->aneg_link_timer
, jiffies
+
187 msecs_to_jiffies(AUTONEGO_LINK_TIMER
));
191 static void aneg_link_timer_callback(struct timer_list
*t
)
193 struct tse_pcs
*pcs
= from_timer(pcs
, t
, aneg_link_timer
);
195 if (pcs
->autoneg
== AUTONEG_ENABLE
)
196 auto_nego_timer_callback(pcs
);
197 else if (pcs
->autoneg
== AUTONEG_DISABLE
)
198 pcs_link_timer_callback(pcs
);
201 void tse_pcs_fix_mac_speed(struct tse_pcs
*pcs
, struct phy_device
*phy_dev
,
204 void __iomem
*tse_pcs_base
= pcs
->tse_pcs_base
;
205 void __iomem
*sgmii_adapter_base
= pcs
->sgmii_adapter_base
;
208 writew(SGMII_ADAPTER_ENABLE
,
209 sgmii_adapter_base
+ SGMII_ADAPTER_CTRL_REG
);
211 pcs
->autoneg
= phy_dev
->autoneg
;
213 if (phy_dev
->autoneg
== AUTONEG_ENABLE
) {
214 val
= readw(tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
215 val
|= TSE_PCS_CONTROL_AN_EN_MASK
;
216 writew(val
, tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
218 val
= readw(tse_pcs_base
+ TSE_PCS_IF_MODE_REG
);
219 val
|= TSE_PCS_USE_SGMII_AN_MASK
;
220 writew(val
, tse_pcs_base
+ TSE_PCS_IF_MODE_REG
);
222 val
= readw(tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
223 val
|= TSE_PCS_CONTROL_RESTART_AN_MASK
;
225 tse_pcs_reset(tse_pcs_base
, pcs
);
227 timer_setup(&pcs
->aneg_link_timer
, aneg_link_timer_callback
,
229 mod_timer(&pcs
->aneg_link_timer
, jiffies
+
230 msecs_to_jiffies(AUTONEGO_LINK_TIMER
));
231 } else if (phy_dev
->autoneg
== AUTONEG_DISABLE
) {
232 val
= readw(tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
233 val
&= ~TSE_PCS_CONTROL_AN_EN_MASK
;
234 writew(val
, tse_pcs_base
+ TSE_PCS_CONTROL_REG
);
236 val
= readw(tse_pcs_base
+ TSE_PCS_IF_MODE_REG
);
237 val
&= ~TSE_PCS_USE_SGMII_AN_MASK
;
238 writew(val
, tse_pcs_base
+ TSE_PCS_IF_MODE_REG
);
240 val
= readw(tse_pcs_base
+ TSE_PCS_IF_MODE_REG
);
241 val
&= ~TSE_PCS_SGMII_SPEED_MASK
;
245 val
|= TSE_PCS_SGMII_SPEED_1000
;
248 val
|= TSE_PCS_SGMII_SPEED_100
;
251 val
|= TSE_PCS_SGMII_SPEED_10
;
256 writew(val
, tse_pcs_base
+ TSE_PCS_IF_MODE_REG
);
258 tse_pcs_reset(tse_pcs_base
, pcs
);
260 timer_setup(&pcs
->aneg_link_timer
, aneg_link_timer_callback
,
262 mod_timer(&pcs
->aneg_link_timer
, jiffies
+
263 msecs_to_jiffies(AUTONEGO_LINK_TIMER
));