Skip to content

Commit 5c1fc7c

Browse files
Russell King (Oracle)kuba-moo
authored andcommitted
net: stmmac: rk: use rk_encode_wm16() for clock selection
Use rk_encode_wm16() for RMII clock gating control, and also for the io_clksel bit used to select the transmit clock between CRU-derived and IO-derived clock sources. Both of these were configured via the "set_clock_selection" method in the SoC specific operations, but there is no requirement to change the io_clksel except when enabling clocks. It is also possible that we don't need to ungate the RMII clock if we are operating in RGMII mode, but this commit makes no change there. Split up the configuration of these as separate functions, and remove the set_clock_selection() method. Since these clocking bits are in the same register that we call the "speed" register, move the logic for writing that register into rk_write_speed_grf_reg(). Signed-off-by: Russell King (Oracle) <rmk+kernel@armlinux.org.uk> Reviewed-by: Heiko Stuebner <heiko@sntech.de> Tested-by: Heiko Stuebner <heiko@sntech.de> #px30,rk3328,rk3568,rk3588 Link: https://patch.msgid.link/E1vnYy6-00000007hp9-1AJM@rmk-PC.armlinux.org.uk Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent d7d9203 commit 5c1fc7c

1 file changed

Lines changed: 75 additions & 99 deletions

File tree

drivers/net/ethernet/stmicro/stmmac/dwmac-rk.c

Lines changed: 75 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,17 @@
2727
struct rk_priv_data;
2828

2929
struct rk_clock_fields {
30+
/* io_clksel_cru_mask - io_clksel bit in clock GRF register which,
31+
* when set, selects the tx clock from CRU.
32+
*/
33+
u16 io_clksel_cru_mask;
34+
/* io_clksel_io_mask - io_clksel bit in clock GRF register which,
35+
* when set, selects the tx clock from IO.
36+
*/
37+
u16 io_clksel_io_mask;
3038
u16 gmii_clk_sel_mask;
3139
u16 rmii_clk_sel_mask;
40+
u16 rmii_gate_en_mask;
3241
u16 mac_speed_mask;
3342
};
3443

@@ -39,8 +48,6 @@ struct rk_gmac_ops {
3948
void (*set_to_rmii)(struct rk_priv_data *bsp_priv);
4049
int (*set_speed)(struct rk_priv_data *bsp_priv,
4150
phy_interface_t interface, int speed);
42-
void (*set_clock_selection)(struct rk_priv_data *bsp_priv, bool input,
43-
bool enable);
4451
void (*integrated_phy_powerup)(struct rk_priv_data *bsp_priv);
4552
void (*integrated_phy_powerdown)(struct rk_priv_data *bsp_priv);
4653

@@ -171,6 +178,54 @@ static int rk_write_clock_grf_reg(struct rk_priv_data *bsp_priv, u32 val)
171178
return regmap_write(regmap, bsp_priv->clock_grf_reg, val);
172179
}
173180

181+
static int rk_set_rmii_gate_en(struct rk_priv_data *bsp_priv, bool state)
182+
{
183+
u32 val;
184+
185+
if (!bsp_priv->clock.rmii_gate_en_mask)
186+
return 0;
187+
188+
val = rk_encode_wm16(state, bsp_priv->clock.rmii_gate_en_mask);
189+
190+
return rk_write_clock_grf_reg(bsp_priv, val);
191+
}
192+
193+
static int rk_ungate_rmii_clock(struct rk_priv_data *bsp_priv)
194+
{
195+
return rk_set_rmii_gate_en(bsp_priv, false);
196+
}
197+
198+
static int rk_gate_rmii_clock(struct rk_priv_data *bsp_priv)
199+
{
200+
return rk_set_rmii_gate_en(bsp_priv, true);
201+
}
202+
203+
static int rk_configure_io_clksel(struct rk_priv_data *bsp_priv)
204+
{
205+
bool io, cru;
206+
u32 val;
207+
208+
if (!bsp_priv->clock.io_clksel_io_mask &&
209+
!bsp_priv->clock.io_clksel_cru_mask)
210+
return 0;
211+
212+
io = bsp_priv->clock_input;
213+
cru = !io;
214+
215+
/* The io_clksel configuration can be either:
216+
* 0=CRU, 1=IO (rk3506, rk3520, rk3576) or
217+
* 0=IO, 1=CRU (rk3588)
218+
* where CRU means the transmit clock comes from the CRU and IO
219+
* means the transmit clock comes from IO.
220+
*
221+
* Handle this by having two masks.
222+
*/
223+
val = rk_encode_wm16(io, bsp_priv->clock.io_clksel_io_mask) |
224+
rk_encode_wm16(cru, bsp_priv->clock.io_clksel_cru_mask);
225+
226+
return rk_write_clock_grf_reg(bsp_priv, val);
227+
}
228+
174229
static int rk_set_clk_mac_speed(struct rk_priv_data *bsp_priv,
175230
phy_interface_t interface, int speed)
176231
{
@@ -637,12 +692,6 @@ static const struct rk_gmac_ops rk3399_ops = {
637692

638693
#define RK3506_GMAC_RMII_MODE GRF_BIT(1)
639694

640-
#define RK3506_GMAC_CLK_SELECT_CRU GRF_CLR_BIT(5)
641-
#define RK3506_GMAC_CLK_SELECT_IO GRF_BIT(5)
642-
643-
#define RK3506_GMAC_CLK_RMII_GATE GRF_BIT(2)
644-
#define RK3506_GMAC_CLK_RMII_NOGATE GRF_CLR_BIT(2)
645-
646695
static int rk3506_init(struct rk_priv_data *bsp_priv)
647696
{
648697
switch (bsp_priv->id) {
@@ -667,26 +716,13 @@ static void rk3506_set_to_rmii(struct rk_priv_data *bsp_priv)
667716
regmap_write(bsp_priv->grf, offset, RK3506_GMAC_RMII_MODE);
668717
}
669718

670-
static void rk3506_set_clock_selection(struct rk_priv_data *bsp_priv,
671-
bool input, bool enable)
672-
{
673-
unsigned int value, offset, id = bsp_priv->id;
674-
675-
offset = (id == 1) ? RK3506_GRF_SOC_CON11 : RK3506_GRF_SOC_CON8;
676-
677-
value = input ? RK3506_GMAC_CLK_SELECT_IO :
678-
RK3506_GMAC_CLK_SELECT_CRU;
679-
value |= enable ? RK3506_GMAC_CLK_RMII_NOGATE :
680-
RK3506_GMAC_CLK_RMII_GATE;
681-
regmap_write(bsp_priv->grf, offset, value);
682-
}
683-
684719
static const struct rk_gmac_ops rk3506_ops = {
685720
.init = rk3506_init,
686721
.set_to_rmii = rk3506_set_to_rmii,
687-
.set_clock_selection = rk3506_set_clock_selection,
688722

723+
.clock.io_clksel_io_mask = BIT_U16(5),
689724
.clock.rmii_clk_sel_mask = BIT_U16(3),
725+
.clock.rmii_gate_en_mask = BIT_U16(2),
690726

691727
.regs_valid = true,
692728
.regs = {
@@ -714,27 +750,22 @@ static const struct rk_gmac_ops rk3506_ops = {
714750
#define RK3528_GMAC1_PHY_INTF_SEL_RGMII GRF_CLR_BIT(8)
715751
#define RK3528_GMAC1_PHY_INTF_SEL_RMII GRF_BIT(8)
716752

717-
#define RK3528_GMAC1_CLK_SELECT_CRU GRF_CLR_BIT(12)
718-
#define RK3528_GMAC1_CLK_SELECT_IO GRF_BIT(12)
719-
720-
#define RK3528_GMAC0_CLK_RMII_GATE GRF_BIT(2)
721-
#define RK3528_GMAC0_CLK_RMII_NOGATE GRF_CLR_BIT(2)
722-
#define RK3528_GMAC1_CLK_RMII_GATE GRF_BIT(9)
723-
#define RK3528_GMAC1_CLK_RMII_NOGATE GRF_CLR_BIT(9)
724-
725753
static int rk3528_init(struct rk_priv_data *bsp_priv)
726754
{
727755
switch (bsp_priv->id) {
728756
case 0:
729757
bsp_priv->clock_grf_reg = RK3528_VO_GRF_GMAC_CON;
730758
bsp_priv->clock.rmii_clk_sel_mask = BIT_U16(3);
759+
bsp_priv->clock.rmii_gate_en_mask = BIT_U16(2);
731760
bsp_priv->supports_rgmii = false;
732761
return 0;
733762

734763
case 1:
735764
bsp_priv->clock_grf_reg = RK3528_VPU_GRF_GMAC_CON5;
765+
bsp_priv->clock.io_clksel_io_mask = BIT_U16(12);
736766
bsp_priv->clock.gmii_clk_sel_mask = GENMASK_U16(11, 10);
737767
bsp_priv->clock.rmii_clk_sel_mask = BIT_U16(10);
768+
bsp_priv->clock.rmii_gate_en_mask = BIT_U16(9);
738769
return 0;
739770

740771
default:
@@ -766,24 +797,6 @@ static void rk3528_set_to_rmii(struct rk_priv_data *bsp_priv)
766797
RK3528_GMAC0_PHY_INTF_SEL_RMII);
767798
}
768799

769-
static void rk3528_set_clock_selection(struct rk_priv_data *bsp_priv,
770-
bool input, bool enable)
771-
{
772-
unsigned int val;
773-
774-
if (bsp_priv->id == 1) {
775-
val = input ? RK3528_GMAC1_CLK_SELECT_IO :
776-
RK3528_GMAC1_CLK_SELECT_CRU;
777-
val |= enable ? RK3528_GMAC1_CLK_RMII_NOGATE :
778-
RK3528_GMAC1_CLK_RMII_GATE;
779-
regmap_write(bsp_priv->grf, RK3528_VPU_GRF_GMAC_CON5, val);
780-
} else {
781-
val = enable ? RK3528_GMAC0_CLK_RMII_NOGATE :
782-
RK3528_GMAC0_CLK_RMII_GATE;
783-
regmap_write(bsp_priv->grf, RK3528_VO_GRF_GMAC_CON, val);
784-
}
785-
}
786-
787800
static void rk3528_integrated_phy_powerup(struct rk_priv_data *bsp_priv)
788801
{
789802
rk_gmac_integrated_fephy_powerup(bsp_priv, RK3528_VO_GRF_MACPHY_CON0);
@@ -798,7 +811,6 @@ static const struct rk_gmac_ops rk3528_ops = {
798811
.init = rk3528_init,
799812
.set_to_rgmii = rk3528_set_to_rgmii,
800813
.set_to_rmii = rk3528_set_to_rmii,
801-
.set_clock_selection = rk3528_set_clock_selection,
802814
.integrated_phy_powerup = rk3528_integrated_phy_powerup,
803815
.integrated_phy_powerdown = rk3528_integrated_phy_powerdown,
804816
.regs_valid = true,
@@ -896,12 +908,6 @@ static const struct rk_gmac_ops rk3568_ops = {
896908
#define RK3576_GRF_GMAC_CON0 0X0020
897909
#define RK3576_GRF_GMAC_CON1 0X0024
898910

899-
#define RK3576_GMAC_CLK_SELECT_IO GRF_BIT(7)
900-
#define RK3576_GMAC_CLK_SELECT_CRU GRF_CLR_BIT(7)
901-
902-
#define RK3576_GMAC_CLK_RMII_GATE GRF_BIT(4)
903-
#define RK3576_GMAC_CLK_RMII_NOGATE GRF_CLR_BIT(4)
904-
905911
static int rk3576_init(struct rk_priv_data *bsp_priv)
906912
{
907913
switch (bsp_priv->id) {
@@ -943,31 +949,16 @@ static void rk3576_set_to_rgmii(struct rk_priv_data *bsp_priv,
943949
RK3576_GMAC_CLK_RX_DL_CFG(rx_delay));
944950
}
945951

946-
static void rk3576_set_clock_selection(struct rk_priv_data *bsp_priv, bool input,
947-
bool enable)
948-
{
949-
unsigned int val = input ? RK3576_GMAC_CLK_SELECT_IO :
950-
RK3576_GMAC_CLK_SELECT_CRU;
951-
unsigned int offset_con;
952-
953-
val |= enable ? RK3576_GMAC_CLK_RMII_NOGATE :
954-
RK3576_GMAC_CLK_RMII_GATE;
955-
956-
offset_con = bsp_priv->id == 1 ? RK3576_GRF_GMAC_CON1 :
957-
RK3576_GRF_GMAC_CON0;
958-
959-
regmap_write(bsp_priv->grf, offset_con, val);
960-
}
961-
962952
static const struct rk_gmac_ops rk3576_ops = {
963953
.init = rk3576_init,
964954
.set_to_rgmii = rk3576_set_to_rgmii,
965-
.set_clock_selection = rk3576_set_clock_selection,
966955

967956
.gmac_rmii_mode_mask = BIT_U16(3),
968957

958+
.clock.io_clksel_io_mask = BIT_U16(7),
969959
.clock.gmii_clk_sel_mask = GENMASK_U16(6, 5),
970960
.clock.rmii_clk_sel_mask = BIT_U16(5),
961+
.clock.rmii_gate_en_mask = BIT_U16(4),
971962

972963
.supports_rmii = true,
973964

@@ -1000,25 +991,23 @@ static const struct rk_gmac_ops rk3576_ops = {
1000991
#define RK3588_GMAC_CLK_RMII_MODE(id) GRF_BIT(5 * (id))
1001992
#define RK3588_GMAC_CLK_RGMII_MODE(id) GRF_CLR_BIT(5 * (id))
1002993

1003-
#define RK3588_GMAC_CLK_SELECT_CRU(id) GRF_BIT(5 * (id) + 4)
1004-
#define RK3588_GMAC_CLK_SELECT_IO(id) GRF_CLR_BIT(5 * (id) + 4)
1005-
1006-
#define RK3588_GMAC_CLK_RMII_GATE(id) GRF_BIT(5 * (id) + 1)
1007-
#define RK3588_GMAC_CLK_RMII_NOGATE(id) GRF_CLR_BIT(5 * (id) + 1)
1008-
1009994
static int rk3588_init(struct rk_priv_data *bsp_priv)
1010995
{
1011996
switch (bsp_priv->id) {
1012997
case 0:
1013998
bsp_priv->gmac_phy_intf_sel_mask = GENMASK_U16(5, 3);
999+
bsp_priv->clock.io_clksel_cru_mask = BIT_U16(4);
10141000
bsp_priv->clock.gmii_clk_sel_mask = GENMASK_U16(3, 2);
10151001
bsp_priv->clock.rmii_clk_sel_mask = BIT_U16(2);
1002+
bsp_priv->clock.rmii_gate_en_mask = BIT_U16(1);
10161003
return 0;
10171004

10181005
case 1:
10191006
bsp_priv->gmac_phy_intf_sel_mask = GENMASK_U16(11, 9);
1007+
bsp_priv->clock.io_clksel_cru_mask = BIT_U16(9);
10201008
bsp_priv->clock.gmii_clk_sel_mask = GENMASK_U16(8, 7);
10211009
bsp_priv->clock.rmii_clk_sel_mask = BIT_U16(7);
1010+
bsp_priv->clock.rmii_gate_en_mask = BIT_U16(6);
10221011
return 0;
10231012

10241013
default:
@@ -1052,23 +1041,10 @@ static void rk3588_set_to_rmii(struct rk_priv_data *bsp_priv)
10521041
RK3588_GMAC_CLK_RMII_MODE(bsp_priv->id));
10531042
}
10541043

1055-
static void rk3588_set_clock_selection(struct rk_priv_data *bsp_priv, bool input,
1056-
bool enable)
1057-
{
1058-
unsigned int val = input ? RK3588_GMAC_CLK_SELECT_IO(bsp_priv->id) :
1059-
RK3588_GMAC_CLK_SELECT_CRU(bsp_priv->id);
1060-
1061-
val |= enable ? RK3588_GMAC_CLK_RMII_NOGATE(bsp_priv->id) :
1062-
RK3588_GMAC_CLK_RMII_GATE(bsp_priv->id);
1063-
1064-
regmap_write(bsp_priv->php_grf, RK3588_GRF_CLK_CON1, val);
1065-
}
1066-
10671044
static const struct rk_gmac_ops rk3588_ops = {
10681045
.init = rk3588_init,
10691046
.set_to_rgmii = rk3588_set_to_rgmii,
10701047
.set_to_rmii = rk3588_set_to_rmii,
1071-
.set_clock_selection = rk3588_set_clock_selection,
10721048

10731049
.gmac_grf_reg_in_php = true,
10741050
.gmac_grf_reg = RK3588_GRF_GMAC_CON0,
@@ -1216,19 +1192,15 @@ static int gmac_clk_enable(struct rk_priv_data *bsp_priv, bool enable)
12161192
if (ret)
12171193
return ret;
12181194

1219-
if (bsp_priv->ops && bsp_priv->ops->set_clock_selection)
1220-
bsp_priv->ops->set_clock_selection(bsp_priv,
1221-
bsp_priv->clock_input, true);
1195+
rk_configure_io_clksel(bsp_priv);
1196+
rk_ungate_rmii_clock(bsp_priv);
12221197

12231198
mdelay(5);
12241199
bsp_priv->clk_enabled = true;
12251200
}
12261201
} else {
12271202
if (bsp_priv->clk_enabled) {
1228-
if (bsp_priv->ops && bsp_priv->ops->set_clock_selection) {
1229-
bsp_priv->ops->set_clock_selection(bsp_priv,
1230-
bsp_priv->clock_input, false);
1231-
}
1203+
rk_gate_rmii_clock(bsp_priv);
12321204

12331205
clk_bulk_disable_unprepare(bsp_priv->num_clks,
12341206
bsp_priv->clks);
@@ -1395,6 +1367,10 @@ static struct rk_priv_data *rk_gmac_setup(struct platform_device *pdev,
13951367
}
13961368
}
13971369

1370+
if (bsp_priv->clock.io_clksel_cru_mask &&
1371+
bsp_priv->clock.io_clksel_io_mask)
1372+
dev_warn(dev, "both CRU and IO io_clksel masks should not be populated - driver may malfunction\n");
1373+
13981374
return bsp_priv;
13991375
}
14001376

0 commit comments

Comments
 (0)