blob: 9ee82b54c62b2d84a0f7938551cb215a6e385ac7 [file] [log] [blame]
Marek Vasut426ca622024-03-26 13:07:22 +01001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2024, Marek Vasut <marex@denx.de>
4 *
5 * This is code moved from drivers/net/dwc_eth_qos.c , which is:
6 * Copyright (c) 2016, NVIDIA CORPORATION.
7 */
8
9#include <common.h>
10#include <asm/cache.h>
11#include <asm/gpio.h>
12#include <asm/io.h>
13#include <clk.h>
14#include <cpu_func.h>
15#include <dm.h>
16#include <dm/device_compat.h>
17#include <errno.h>
18#include <eth_phy.h>
19#include <log.h>
20#include <malloc.h>
21#include <memalign.h>
22#include <miiphy.h>
23#include <net.h>
24#include <netdev.h>
25#include <phy.h>
Christophe Roullier033dc762024-03-26 13:07:30 +010026#include <regmap.h>
Marek Vasut426ca622024-03-26 13:07:22 +010027#include <reset.h>
Marek Vasut7595bfc2024-03-26 13:07:24 +010028#include <syscon.h>
Marek Vasut426ca622024-03-26 13:07:22 +010029#include <wait_bit.h>
Marek Vasut577f46b2024-03-26 13:07:26 +010030#include <linux/bitfield.h>
Marek Vasut426ca622024-03-26 13:07:22 +010031#include <linux/delay.h>
32
33#include "dwc_eth_qos.h"
34
Marek Vasut7595bfc2024-03-26 13:07:24 +010035/* SYSCFG registers */
36#define SYSCFG_PMCSETR 0x04
Christophe Roullier033dc762024-03-26 13:07:30 +010037#define SYSCFG_PMCCLRR_MP13 0x08
38#define SYSCFG_PMCCLRR_MP15 0x44
39
40#define SYSCFG_PMCSETR_ETH1_MASK GENMASK(23, 16)
41#define SYSCFG_PMCSETR_ETH2_MASK GENMASK(31, 24)
Marek Vasut7595bfc2024-03-26 13:07:24 +010042
43#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
44#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
45
Christophe Roullier033dc762024-03-26 13:07:30 +010046/* STM32MP15xx specific bit */
Marek Vasut7595bfc2024-03-26 13:07:24 +010047#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
48
49#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
Marek Vasut577f46b2024-03-26 13:07:26 +010050#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0x0
51#define SYSCFG_PMCSETR_ETH_SEL_RGMII 0x1
52#define SYSCFG_PMCSETR_ETH_SEL_RMII 0x4
Marek Vasut7595bfc2024-03-26 13:07:24 +010053
Marek Vasut426ca622024-03-26 13:07:22 +010054static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
55{
Marek Vasutb14101c2024-03-26 13:07:25 +010056 struct eqos_priv __maybe_unused *eqos = dev_get_priv(dev);
57
58 if (!CONFIG_IS_ENABLED(CLK))
59 return 0;
Marek Vasut426ca622024-03-26 13:07:22 +010060
61 return clk_get_rate(&eqos->clk_master_bus);
Marek Vasut426ca622024-03-26 13:07:22 +010062}
63
64static int eqos_start_clks_stm32(struct udevice *dev)
65{
Marek Vasutb14101c2024-03-26 13:07:25 +010066 struct eqos_priv __maybe_unused *eqos = dev_get_priv(dev);
Marek Vasut426ca622024-03-26 13:07:22 +010067 int ret;
68
Marek Vasutb14101c2024-03-26 13:07:25 +010069 if (!CONFIG_IS_ENABLED(CLK))
70 return 0;
71
Marek Vasut5a1640e2024-03-26 13:07:28 +010072 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +010073
74 ret = clk_enable(&eqos->clk_master_bus);
75 if (ret < 0) {
Marek Vasut5a1640e2024-03-26 13:07:28 +010076 dev_err(dev, "clk_enable(clk_master_bus) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +010077 goto err;
78 }
79
80 ret = clk_enable(&eqos->clk_rx);
81 if (ret < 0) {
Marek Vasut5a1640e2024-03-26 13:07:28 +010082 dev_err(dev, "clk_enable(clk_rx) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +010083 goto err_disable_clk_master_bus;
84 }
85
86 ret = clk_enable(&eqos->clk_tx);
87 if (ret < 0) {
Marek Vasut5a1640e2024-03-26 13:07:28 +010088 dev_err(dev, "clk_enable(clk_tx) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +010089 goto err_disable_clk_rx;
90 }
91
92 if (clk_valid(&eqos->clk_ck) && !eqos->clk_ck_enabled) {
93 ret = clk_enable(&eqos->clk_ck);
94 if (ret < 0) {
Marek Vasut5a1640e2024-03-26 13:07:28 +010095 dev_err(dev, "clk_enable(clk_ck) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +010096 goto err_disable_clk_tx;
97 }
98 eqos->clk_ck_enabled = true;
99 }
Marek Vasut426ca622024-03-26 13:07:22 +0100100
Marek Vasut5a1640e2024-03-26 13:07:28 +0100101 dev_dbg(dev, "%s: OK\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +0100102 return 0;
103
Marek Vasut426ca622024-03-26 13:07:22 +0100104err_disable_clk_tx:
105 clk_disable(&eqos->clk_tx);
106err_disable_clk_rx:
107 clk_disable(&eqos->clk_rx);
108err_disable_clk_master_bus:
109 clk_disable(&eqos->clk_master_bus);
110err:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100111 dev_dbg(dev, "%s: FAILED: %d\n", __func__, ret);
112
Marek Vasut426ca622024-03-26 13:07:22 +0100113 return ret;
Marek Vasut426ca622024-03-26 13:07:22 +0100114}
115
116static int eqos_stop_clks_stm32(struct udevice *dev)
117{
Marek Vasutb14101c2024-03-26 13:07:25 +0100118 struct eqos_priv __maybe_unused *eqos = dev_get_priv(dev);
119
120 if (!CONFIG_IS_ENABLED(CLK))
121 return 0;
Marek Vasut426ca622024-03-26 13:07:22 +0100122
Marek Vasut5a1640e2024-03-26 13:07:28 +0100123 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +0100124
125 clk_disable(&eqos->clk_tx);
126 clk_disable(&eqos->clk_rx);
127 clk_disable(&eqos->clk_master_bus);
Marek Vasut426ca622024-03-26 13:07:22 +0100128
Marek Vasut5a1640e2024-03-26 13:07:28 +0100129 dev_dbg(dev, "%s: OK\n", __func__);
130
Marek Vasut426ca622024-03-26 13:07:22 +0100131 return 0;
132}
133
Marek Vasut7595bfc2024-03-26 13:07:24 +0100134static int eqos_probe_syscfg_stm32(struct udevice *dev,
135 phy_interface_t interface_type)
136{
Marek Vasute77fa872024-03-26 13:07:29 +0100137 /* Ethernet 50MHz RMII clock selection. */
138 const bool eth_ref_clk_sel = dev_read_bool(dev, "st,eth-ref-clk-sel");
Christophe Roullier033dc762024-03-26 13:07:30 +0100139 /* SoC is STM32MP13xx with two ethernet MACs */
140 const bool is_mp13 = device_is_compatible(dev, "st,stm32mp13-dwmac");
Marek Vasute77fa872024-03-26 13:07:29 +0100141 /* Gigabit Ethernet 125MHz clock selection. */
142 const bool eth_clk_sel = dev_read_bool(dev, "st,eth-clk-sel");
Marek Vasut6c4b2452024-03-26 13:07:32 +0100143 /* Ethernet clock source is RCC. */
144 const bool ext_phyclk = dev_read_bool(dev, "st,ext-phyclk");
Christophe Roullier033dc762024-03-26 13:07:30 +0100145 struct regmap *regmap;
146 u32 regmap_mask;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100147 u32 value;
148
Christophe Roullier033dc762024-03-26 13:07:30 +0100149 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscon");
150 if (IS_ERR(regmap))
151 return PTR_ERR(regmap);
152
153 regmap_mask = dev_read_u32_index_default(dev, "st,syscon", 2,
154 SYSCFG_PMCSETR_ETH1_MASK);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100155
156 switch (interface_type) {
157 case PHY_INTERFACE_MODE_MII:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100158 dev_dbg(dev, "PHY_INTERFACE_MODE_MII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100159 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
160 SYSCFG_PMCSETR_ETH_SEL_GMII_MII);
Marek Vasut6c4b2452024-03-26 13:07:32 +0100161 /*
162 * STM32MP15xx supports both MII and GMII, STM32MP13xx MII only.
163 * SYSCFG_PMCSETR ETH_SELMII is present only on STM32MP15xx and
164 * acts as a selector between 0:GMII and 1:MII. As STM32MP13xx
165 * supports only MII, ETH_SELMII is not present.
166 */
Christophe Roullier033dc762024-03-26 13:07:30 +0100167 if (!is_mp13) /* Select MII mode on STM32MP15xx */
168 value |= SYSCFG_PMCSETR_ETH_SELMII;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100169 break;
Christophe Roullier033dc762024-03-26 13:07:30 +0100170 case PHY_INTERFACE_MODE_GMII: /* STM32MP15xx only */
Marek Vasut5a1640e2024-03-26 13:07:28 +0100171 dev_dbg(dev, "PHY_INTERFACE_MODE_GMII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100172 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
173 SYSCFG_PMCSETR_ETH_SEL_GMII_MII);
Marek Vasut6c4b2452024-03-26 13:07:32 +0100174 /*
175 * If eth_clk_sel is set, use internal ETH_CLKx clock from RCC,
176 * otherwise use external clock from IO pin (requires matching
177 * GPIO block AF setting of that pin).
178 */
179 if (eth_clk_sel || ext_phyclk)
Marek Vasut577f46b2024-03-26 13:07:26 +0100180 value |= SYSCFG_PMCSETR_ETH_CLK_SEL;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100181 break;
182 case PHY_INTERFACE_MODE_RMII:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100183 dev_dbg(dev, "PHY_INTERFACE_MODE_RMII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100184 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
185 SYSCFG_PMCSETR_ETH_SEL_RMII);
Marek Vasut6c4b2452024-03-26 13:07:32 +0100186 /*
187 * If eth_ref_clk_sel is set, use internal clock from RCC,
188 * otherwise use external clock from ETHn_RX_CLK/ETHn_REF_CLK
189 * IO pin (requires matching GPIO block AF setting of that
190 * pin).
191 */
192 if (eth_ref_clk_sel || ext_phyclk)
Marek Vasut577f46b2024-03-26 13:07:26 +0100193 value |= SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100194 break;
195 case PHY_INTERFACE_MODE_RGMII:
196 case PHY_INTERFACE_MODE_RGMII_ID:
197 case PHY_INTERFACE_MODE_RGMII_RXID:
198 case PHY_INTERFACE_MODE_RGMII_TXID:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100199 dev_dbg(dev, "PHY_INTERFACE_MODE_RGMII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100200 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
201 SYSCFG_PMCSETR_ETH_SEL_RGMII);
Marek Vasut6c4b2452024-03-26 13:07:32 +0100202 /*
203 * If eth_clk_sel is set, use internal ETH_CLKx clock from RCC,
204 * otherwise use external clock from ETHx_CLK125 pin (requires
205 * matching GPIO block AF setting of that pin).
206 */
207 if (eth_clk_sel || ext_phyclk)
Marek Vasut577f46b2024-03-26 13:07:26 +0100208 value |= SYSCFG_PMCSETR_ETH_CLK_SEL;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100209 break;
210 default:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100211 dev_dbg(dev, "Do not manage %d interface\n",
212 interface_type);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100213 /* Do not manage others interfaces */
214 return -EINVAL;
215 }
216
Christophe Roullier033dc762024-03-26 13:07:30 +0100217 /* Shift value at correct ethernet MAC offset in SYSCFG_PMCSETR */
218 value <<= ffs(regmap_mask) - ffs(SYSCFG_PMCSETR_ETH1_MASK);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100219
Christophe Roullier033dc762024-03-26 13:07:30 +0100220 /* Update PMCCLRR (clear register) */
221 regmap_write(regmap, is_mp13 ?
222 SYSCFG_PMCCLRR_MP13 : SYSCFG_PMCCLRR_MP15,
223 regmap_mask);
224
225 return regmap_update_bits(regmap, SYSCFG_PMCSETR, regmap_mask, value);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100226}
227
Marek Vasut426ca622024-03-26 13:07:22 +0100228static int eqos_probe_resources_stm32(struct udevice *dev)
229{
230 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut426ca622024-03-26 13:07:22 +0100231 phy_interface_t interface;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100232 int ret;
Marek Vasut426ca622024-03-26 13:07:22 +0100233
Marek Vasut5a1640e2024-03-26 13:07:28 +0100234 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +0100235
236 interface = eqos->config->interface(dev);
237
238 if (interface == PHY_INTERFACE_MODE_NA) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100239 dev_err(dev, "Invalid PHY interface\n");
Marek Vasut426ca622024-03-26 13:07:22 +0100240 return -EINVAL;
241 }
242
Marek Vasut7595bfc2024-03-26 13:07:24 +0100243 ret = eqos_probe_syscfg_stm32(dev, interface);
Marek Vasut426ca622024-03-26 13:07:22 +0100244 if (ret)
245 return -EINVAL;
246
247 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
248 if (ret) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100249 dev_err(dev, "clk_get_by_name(master_bus) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100250 goto err_probe;
251 }
252
253 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
254 if (ret) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100255 dev_err(dev, "clk_get_by_name(rx) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100256 goto err_probe;
257 }
258
259 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
260 if (ret) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100261 dev_err(dev, "clk_get_by_name(tx) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100262 goto err_probe;
263 }
264
265 /* Get ETH_CLK clocks (optional) */
266 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
267 if (ret)
Marek Vasut5a1640e2024-03-26 13:07:28 +0100268 dev_warn(dev, "No phy clock provided %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100269
Marek Vasut5a1640e2024-03-26 13:07:28 +0100270 dev_dbg(dev, "%s: OK\n", __func__);
271
Marek Vasut426ca622024-03-26 13:07:22 +0100272 return 0;
273
274err_probe:
275
Marek Vasut5a1640e2024-03-26 13:07:28 +0100276 dev_dbg(dev, "%s: returns %d\n", __func__, ret);
277
Marek Vasut426ca622024-03-26 13:07:22 +0100278 return ret;
279}
280
281static int eqos_remove_resources_stm32(struct udevice *dev)
282{
Marek Vasut5a1640e2024-03-26 13:07:28 +0100283 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +0100284
285 return 0;
286}
287
288static struct eqos_ops eqos_stm32_ops = {
289 .eqos_inval_desc = eqos_inval_desc_generic,
290 .eqos_flush_desc = eqos_flush_desc_generic,
291 .eqos_inval_buffer = eqos_inval_buffer_generic,
292 .eqos_flush_buffer = eqos_flush_buffer_generic,
293 .eqos_probe_resources = eqos_probe_resources_stm32,
294 .eqos_remove_resources = eqos_remove_resources_stm32,
295 .eqos_stop_resets = eqos_null_ops,
296 .eqos_start_resets = eqos_null_ops,
297 .eqos_stop_clks = eqos_stop_clks_stm32,
298 .eqos_start_clks = eqos_start_clks_stm32,
299 .eqos_calibrate_pads = eqos_null_ops,
300 .eqos_disable_calibration = eqos_null_ops,
301 .eqos_set_tx_clk_speed = eqos_null_ops,
302 .eqos_get_enetaddr = eqos_null_ops,
303 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
304};
305
Christophe Roullier25a16862024-03-26 13:07:31 +0100306struct eqos_config __maybe_unused eqos_stm32mp13_config = {
307 .reg_access_always_ok = false,
308 .mdio_wait = 10000,
309 .swr_wait = 50,
310 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
311 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
312 .axi_bus_width = EQOS_AXI_WIDTH_32,
313 .interface = dev_read_phy_mode,
314 .ops = &eqos_stm32_ops
315};
316
Marek Vasut944ba372024-03-26 13:07:23 +0100317struct eqos_config __maybe_unused eqos_stm32mp15_config = {
Marek Vasut426ca622024-03-26 13:07:22 +0100318 .reg_access_always_ok = false,
319 .mdio_wait = 10000,
320 .swr_wait = 50,
321 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
322 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
323 .axi_bus_width = EQOS_AXI_WIDTH_64,
324 .interface = dev_read_phy_mode,
325 .ops = &eqos_stm32_ops
326};