1 From 5654ec78dd7e64b1e04777b24007344329e6a63b Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Thu, 14 Oct 2021 00:39:11 +0200
4 Subject: net: dsa: qca8k: rework rgmii delay logic and scan for cpu port 6
6 Future proof commit. This switch have 2 CPU ports and one valid
7 configuration is first CPU port set to sgmii and second CPU port set to
8 rgmii-id. The current implementation detects delay only for CPU port
9 zero set to rgmii and doesn't count any delay set in a secondary CPU
10 port. Drop the current delay scan function and move it to the sgmii
11 parser function to generalize and implicitly add support for secondary
12 CPU port set to rgmii-id. Introduce new logic where delay is enabled
13 also with internal delay binding declared and rgmii set as PHY mode.
15 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
16 Signed-off-by: David S. Miller <davem@davemloft.net>
18 drivers/net/dsa/qca8k.c | 165 ++++++++++++++++++++++++------------------------
19 drivers/net/dsa/qca8k.h | 10 ++-
20 2 files changed, 89 insertions(+), 86 deletions(-)
22 --- a/drivers/net/dsa/qca8k.c
23 +++ b/drivers/net/dsa/qca8k.c
24 @@ -889,68 +889,6 @@ qca8k_setup_mdio_bus(struct qca8k_priv *
28 -qca8k_setup_of_rgmii_delay(struct qca8k_priv *priv)
30 - struct device_node *port_dn;
31 - phy_interface_t mode;
32 - struct dsa_port *dp;
35 - /* CPU port is already checked */
36 - dp = dsa_to_port(priv->ds, 0);
40 - /* Check if port 0 is set to the correct type */
41 - of_get_phy_mode(port_dn, &mode);
42 - if (mode != PHY_INTERFACE_MODE_RGMII_ID &&
43 - mode != PHY_INTERFACE_MODE_RGMII_RXID &&
44 - mode != PHY_INTERFACE_MODE_RGMII_TXID) {
49 - case PHY_INTERFACE_MODE_RGMII_ID:
50 - case PHY_INTERFACE_MODE_RGMII_RXID:
51 - if (of_property_read_u32(port_dn, "rx-internal-delay-ps", &val))
54 - /* Switch regs accept value in ns, convert ps to ns */
57 - if (val > QCA8K_MAX_DELAY) {
58 - dev_err(priv->dev, "rgmii rx delay is limited to a max value of 3ns, setting to the max value");
62 - priv->rgmii_rx_delay = val;
63 - /* Stop here if we need to check only for rx delay */
64 - if (mode != PHY_INTERFACE_MODE_RGMII_ID)
68 - case PHY_INTERFACE_MODE_RGMII_TXID:
69 - if (of_property_read_u32(port_dn, "tx-internal-delay-ps", &val))
72 - /* Switch regs accept value in ns, convert ps to ns */
75 - if (val > QCA8K_MAX_DELAY) {
76 - dev_err(priv->dev, "rgmii tx delay is limited to a max value of 3ns, setting to the max value");
80 - priv->rgmii_tx_delay = val;
90 qca8k_setup_mac_pwr_sel(struct qca8k_priv *priv)
93 @@ -996,19 +934,21 @@ static int qca8k_find_cpu_port(struct ds
95 qca8k_parse_port_config(struct qca8k_priv *priv)
97 + int port, cpu_port_index = 0, ret;
98 struct device_node *port_dn;
104 /* We have 2 CPU port. Check them */
105 - for (port = 0; port < QCA8K_NUM_PORTS; port++) {
106 + for (port = 0; port < QCA8K_NUM_PORTS && cpu_port_index < QCA8K_NUM_CPU_PORTS; port++) {
107 /* Skip every other port */
108 if (port != 0 && port != 6)
111 dp = dsa_to_port(priv->ds, port);
115 if (!of_device_is_available(port_dn))
117 @@ -1017,12 +957,54 @@ qca8k_parse_port_config(struct qca8k_pri
121 - if (mode == PHY_INTERFACE_MODE_SGMII) {
123 + case PHY_INTERFACE_MODE_RGMII:
124 + case PHY_INTERFACE_MODE_RGMII_ID:
125 + case PHY_INTERFACE_MODE_RGMII_TXID:
126 + case PHY_INTERFACE_MODE_RGMII_RXID:
129 + if (!of_property_read_u32(port_dn, "tx-internal-delay-ps", &delay))
130 + /* Switch regs accept value in ns, convert ps to ns */
131 + delay = delay / 1000;
132 + else if (mode == PHY_INTERFACE_MODE_RGMII_ID ||
133 + mode == PHY_INTERFACE_MODE_RGMII_TXID)
136 + if (delay > QCA8K_MAX_DELAY) {
137 + dev_err(priv->dev, "rgmii tx delay is limited to a max value of 3ns, setting to the max value");
141 + priv->rgmii_tx_delay[cpu_port_index] = delay;
145 + if (!of_property_read_u32(port_dn, "rx-internal-delay-ps", &delay))
146 + /* Switch regs accept value in ns, convert ps to ns */
147 + delay = delay / 1000;
148 + else if (mode == PHY_INTERFACE_MODE_RGMII_ID ||
149 + mode == PHY_INTERFACE_MODE_RGMII_RXID)
152 + if (delay > QCA8K_MAX_DELAY) {
153 + dev_err(priv->dev, "rgmii rx delay is limited to a max value of 3ns, setting to the max value");
157 + priv->rgmii_rx_delay[cpu_port_index] = delay;
160 + case PHY_INTERFACE_MODE_SGMII:
161 if (of_property_read_bool(port_dn, "qca,sgmii-txclk-falling-edge"))
162 priv->sgmii_tx_clk_falling_edge = true;
164 if (of_property_read_bool(port_dn, "qca,sgmii-rxclk-falling-edge"))
165 priv->sgmii_rx_clk_falling_edge = true;
173 @@ -1059,10 +1041,6 @@ qca8k_setup(struct dsa_switch *ds)
177 - ret = qca8k_setup_of_rgmii_delay(priv);
181 ret = qca8k_setup_mac_pwr_sel(priv);
184 @@ -1229,8 +1207,8 @@ qca8k_phylink_mac_config(struct dsa_swit
185 const struct phylink_link_state *state)
187 struct qca8k_priv *priv = ds->priv;
190 + int cpu_port_index, ret;
191 + u32 reg, val, delay;
194 case 0: /* 1st CPU port */
195 @@ -1242,6 +1220,7 @@ qca8k_phylink_mac_config(struct dsa_swit
198 reg = QCA8K_REG_PORT0_PAD_CTRL;
199 + cpu_port_index = QCA8K_CPU_PORT0;
203 @@ -1260,6 +1239,7 @@ qca8k_phylink_mac_config(struct dsa_swit
206 reg = QCA8K_REG_PORT6_PAD_CTRL;
207 + cpu_port_index = QCA8K_CPU_PORT6;
210 dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
211 @@ -1274,23 +1254,40 @@ qca8k_phylink_mac_config(struct dsa_swit
213 switch (state->interface) {
214 case PHY_INTERFACE_MODE_RGMII:
215 - /* RGMII mode means no delay so don't enable the delay */
216 - qca8k_write(priv, reg, QCA8K_PORT_PAD_RGMII_EN);
218 case PHY_INTERFACE_MODE_RGMII_ID:
219 case PHY_INTERFACE_MODE_RGMII_TXID:
220 case PHY_INTERFACE_MODE_RGMII_RXID:
221 - /* RGMII_ID needs internal delay. This is enabled through
222 - * PORT5_PAD_CTRL for all ports, rather than individual port
224 + val = QCA8K_PORT_PAD_RGMII_EN;
226 + /* Delay can be declared in 3 different way.
227 + * Mode to rgmii and internal-delay standard binding defined
228 + * rgmii-id or rgmii-tx/rx phy mode set.
229 + * The parse logic set a delay different than 0 only when one
230 + * of the 3 different way is used. In all other case delay is
231 + * not enabled. With ID or TX/RXID delay is enabled and set
232 + * to the default and recommended value.
234 + if (priv->rgmii_tx_delay[cpu_port_index]) {
235 + delay = priv->rgmii_tx_delay[cpu_port_index];
237 + val |= QCA8K_PORT_PAD_RGMII_TX_DELAY(delay) |
238 + QCA8K_PORT_PAD_RGMII_TX_DELAY_EN;
241 + if (priv->rgmii_rx_delay[cpu_port_index]) {
242 + delay = priv->rgmii_rx_delay[cpu_port_index];
244 + val |= QCA8K_PORT_PAD_RGMII_RX_DELAY(delay) |
245 + QCA8K_PORT_PAD_RGMII_RX_DELAY_EN;
248 + /* Set RGMII delay based on the selected values */
249 + qca8k_write(priv, reg, val);
251 + /* QCA8337 requires to set rgmii rx delay for all ports.
252 + * This is enabled through PORT5_PAD_CTRL for all ports,
253 + * rather than individual port registers.
255 - qca8k_write(priv, reg,
256 - QCA8K_PORT_PAD_RGMII_EN |
257 - QCA8K_PORT_PAD_RGMII_TX_DELAY(priv->rgmii_tx_delay) |
258 - QCA8K_PORT_PAD_RGMII_RX_DELAY(priv->rgmii_rx_delay) |
259 - QCA8K_PORT_PAD_RGMII_TX_DELAY_EN |
260 - QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
261 - /* QCA8337 requires to set rgmii rx delay */
262 if (priv->switch_id == QCA8K_ID_QCA8337)
263 qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL,
264 QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
265 --- a/drivers/net/dsa/qca8k.h
266 +++ b/drivers/net/dsa/qca8k.h
268 #include <linux/gpio.h>
270 #define QCA8K_NUM_PORTS 7
271 +#define QCA8K_NUM_CPU_PORTS 2
272 #define QCA8K_MAX_MTU 9000
274 #define PHY_ID_QCA8327 0x004dd034
275 @@ -255,13 +256,18 @@ struct qca8k_match_data {
289 bool sgmii_rx_clk_falling_edge;
290 bool sgmii_tx_clk_falling_edge;
291 + u8 rgmii_rx_delay[QCA8K_NUM_CPU_PORTS]; /* 0: CPU port0, 1: CPU port6 */
292 + u8 rgmii_tx_delay[QCA8K_NUM_CPU_PORTS]; /* 0: CPU port0, 1: CPU port6 */
293 bool legacy_phy_port_mapping;
294 struct regmap *regmap;