1 /* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; version 2 of the License
5 * This program is distributed in the hope that it will be useful,
6 * but WITHOUT ANY WARRANTY; without even the implied warranty of
7 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8 * GNU General Public License for more details.
10 * Copyright (C) 2009-2015 John Crispin <blogic@openwrt.org>
11 * Copyright (C) 2009-2015 Felix Fietkau <nbd@nbd.name>
12 * Copyright (C) 2013-2015 Michael Lee <igvtee@gmail.com>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/of_net.h>
19 #include <linux/of_mdio.h>
21 #include "mtk_eth_soc.h"
22 #include "mdio_rt2880.h"
25 #define FE_MDIO_RETRY 1000
27 static unsigned char *rt2880_speed_str(struct fe_priv
*priv
)
29 switch (priv
->phy
->speed
[0]) {
41 void rt2880_mdio_link_adjust(struct fe_priv
*priv
, int port
)
46 netif_carrier_off(priv
->netdev
);
47 netdev_info(priv
->netdev
, "link down\n");
51 mdio_cfg
= FE_MDIO_CFG_TX_CLK_SKEW_200
|
52 FE_MDIO_CFG_RX_CLK_SKEW_200
|
53 FE_MDIO_CFG_GP1_FRC_EN
;
55 if (priv
->phy
->duplex
[0] == DUPLEX_FULL
)
56 mdio_cfg
|= FE_MDIO_CFG_GP1_DUPLEX
;
58 if (priv
->phy
->tx_fc
[0])
59 mdio_cfg
|= FE_MDIO_CFG_GP1_FC_TX
;
61 if (priv
->phy
->rx_fc
[0])
62 mdio_cfg
|= FE_MDIO_CFG_GP1_FC_RX
;
64 switch (priv
->phy
->speed
[0]) {
66 mdio_cfg
|= FE_MDIO_CFG_GP1_SPEED_10
;
69 mdio_cfg
|= FE_MDIO_CFG_GP1_SPEED_100
;
72 mdio_cfg
|= FE_MDIO_CFG_GP1_SPEED_1000
;
78 fe_w32(mdio_cfg
, FE_MDIO_CFG
);
80 netif_carrier_on(priv
->netdev
);
81 netdev_info(priv
->netdev
, "link up (%sMbps/%s duplex)\n",
82 rt2880_speed_str(priv
),
83 (priv
->phy
->duplex
[0] == DUPLEX_FULL
) ? "Full" : "Half");
86 static int rt2880_mdio_wait_ready(struct fe_priv
*priv
)
90 retries
= FE_MDIO_RETRY
;
94 t
= fe_r32(FE_MDIO_ACCESS
);
95 if ((t
& BIT(31)) == 0)
104 dev_err(priv
->dev
, "MDIO operation timed out\n");
108 int rt2880_mdio_read(struct mii_bus
*bus
, int phy_addr
, int phy_reg
)
110 struct fe_priv
*priv
= bus
->priv
;
114 err
= rt2880_mdio_wait_ready(priv
);
118 t
= (phy_addr
<< 24) | (phy_reg
<< 16);
119 fe_w32(t
, FE_MDIO_ACCESS
);
121 fe_w32(t
, FE_MDIO_ACCESS
);
123 err
= rt2880_mdio_wait_ready(priv
);
127 pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__
,
128 phy_addr
, phy_reg
, fe_r32(FE_MDIO_ACCESS
) & 0xffff);
130 return fe_r32(FE_MDIO_ACCESS
) & 0xffff;
133 int rt2880_mdio_write(struct mii_bus
*bus
, int phy_addr
, int phy_reg
, u16 val
)
135 struct fe_priv
*priv
= bus
->priv
;
139 pr_debug("%s: addr=%04x, reg=%04x, value=%04x\n", __func__
,
140 phy_addr
, phy_reg
, fe_r32(FE_MDIO_ACCESS
) & 0xffff);
142 err
= rt2880_mdio_wait_ready(priv
);
146 t
= (1 << 30) | (phy_addr
<< 24) | (phy_reg
<< 16) | val
;
147 fe_w32(t
, FE_MDIO_ACCESS
);
149 fe_w32(t
, FE_MDIO_ACCESS
);
151 return rt2880_mdio_wait_ready(priv
);
154 void rt2880_port_init(struct fe_priv
*priv
, struct device_node
*np
)
156 const __be32
*id
= of_get_property(np
, "reg", NULL
);
159 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
162 phy_interface_t phy_mode
= PHY_INTERFACE_MODE_NA
;
165 if (!id
|| (be32_to_cpu(*id
) != 0)) {
166 pr_err("%s: invalid port id\n", np
->name
);
170 priv
->phy
->phy_fixed
[0] = of_get_property(np
,
171 "mediatek,fixed-link", &size
);
172 if (priv
->phy
->phy_fixed
[0] &&
173 (size
!= (4 * sizeof(*priv
->phy
->phy_fixed
[0])))) {
174 pr_err("%s: invalid fixed link property\n", np
->name
);
175 priv
->phy
->phy_fixed
[0] = NULL
;
179 #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 10, 0)
180 phy_mode
= of_get_phy_mode(np
);
182 of_get_phy_mode(np
, &phy_mode
);
185 case PHY_INTERFACE_MODE_RGMII
:
187 case PHY_INTERFACE_MODE_MII
:
189 case PHY_INTERFACE_MODE_RMII
:
192 if (!priv
->phy
->phy_fixed
[0])
193 dev_err(priv
->dev
, "port %d - invalid phy mode\n",
194 priv
->phy
->speed
[0]);
198 priv
->phy
->phy_node
[0] = of_parse_phandle(np
, "phy-handle", 0);
199 if (!priv
->phy
->phy_node
[0] && !priv
->phy
->phy_fixed
[0])
202 if (priv
->phy
->phy_fixed
[0]) {
203 link
= priv
->phy
->phy_fixed
[0];
204 priv
->phy
->speed
[0] = be32_to_cpup(link
++);
205 priv
->phy
->duplex
[0] = be32_to_cpup(link
++);
206 priv
->phy
->tx_fc
[0] = be32_to_cpup(link
++);
207 priv
->phy
->rx_fc
[0] = be32_to_cpup(link
++);
210 switch (priv
->phy
->speed
[0]) {
218 dev_err(priv
->dev
, "invalid link speed: %d\n",
219 priv
->phy
->speed
[0]);
220 priv
->phy
->phy_fixed
[0] = 0;
223 dev_info(priv
->dev
, "using fixed link parameters\n");
224 rt2880_mdio_link_adjust(priv
, 0);
228 if (priv
->phy
->phy_node
[0] && mdiobus_get_phy(priv
->mii_bus
, 0))
229 fe_connect_phy_node(priv
, priv
->phy
->phy_node
[0], 0);