blob: cffaa10b7055b47d613062eb16f9fdabb89495c1 [file] [log] [blame]
Marek Vasut34374262024-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
Marek Vasut34374262024-03-26 13:07:22 +01009#include <asm/cache.h>
10#include <asm/gpio.h>
11#include <asm/io.h>
12#include <clk.h>
13#include <cpu_func.h>
14#include <dm.h>
15#include <dm/device_compat.h>
16#include <errno.h>
17#include <eth_phy.h>
18#include <log.h>
19#include <malloc.h>
20#include <memalign.h>
21#include <miiphy.h>
22#include <net.h>
23#include <netdev.h>
24#include <phy.h>
Christophe Roulliera440d192024-03-26 13:07:30 +010025#include <regmap.h>
Marek Vasut34374262024-03-26 13:07:22 +010026#include <reset.h>
Marek Vasutd100c1a2024-03-26 13:07:24 +010027#include <syscon.h>
Marek Vasut34374262024-03-26 13:07:22 +010028#include <wait_bit.h>
Marek Vasut416592e2024-03-26 13:07:26 +010029#include <linux/bitfield.h>
Marek Vasut34374262024-03-26 13:07:22 +010030#include <linux/delay.h>
31
32#include "dwc_eth_qos.h"
33
Marek Vasutd100c1a2024-03-26 13:07:24 +010034/* SYSCFG registers */
35#define SYSCFG_PMCSETR 0x04
Christophe Roulliera440d192024-03-26 13:07:30 +010036#define SYSCFG_PMCCLRR_MP13 0x08
37#define SYSCFG_PMCCLRR_MP15 0x44
38
39#define SYSCFG_PMCSETR_ETH1_MASK GENMASK(23, 16)
40#define SYSCFG_PMCSETR_ETH2_MASK GENMASK(31, 24)
Marek Vasutd100c1a2024-03-26 13:07:24 +010041
42#define SYSCFG_PMCSETR_ETH_CLK_SEL BIT(16)
43#define SYSCFG_PMCSETR_ETH_REF_CLK_SEL BIT(17)
44
Christophe Roulliera440d192024-03-26 13:07:30 +010045/* STM32MP15xx specific bit */
Marek Vasutd100c1a2024-03-26 13:07:24 +010046#define SYSCFG_PMCSETR_ETH_SELMII BIT(20)
47
48#define SYSCFG_PMCSETR_ETH_SEL_MASK GENMASK(23, 21)
Marek Vasut416592e2024-03-26 13:07:26 +010049#define SYSCFG_PMCSETR_ETH_SEL_GMII_MII 0x0
50#define SYSCFG_PMCSETR_ETH_SEL_RGMII 0x1
51#define SYSCFG_PMCSETR_ETH_SEL_RMII 0x4
Marek Vasutd100c1a2024-03-26 13:07:24 +010052
Marek Vasut34374262024-03-26 13:07:22 +010053static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
54{
Marek Vasutb204c2a2024-03-26 13:07:25 +010055 struct eqos_priv __maybe_unused *eqos = dev_get_priv(dev);
56
57 if (!CONFIG_IS_ENABLED(CLK))
58 return 0;
Marek Vasut34374262024-03-26 13:07:22 +010059
60 return clk_get_rate(&eqos->clk_master_bus);
Marek Vasut34374262024-03-26 13:07:22 +010061}
62
63static int eqos_start_clks_stm32(struct udevice *dev)
64{
Marek Vasutb204c2a2024-03-26 13:07:25 +010065 struct eqos_priv __maybe_unused *eqos = dev_get_priv(dev);
Marek Vasut34374262024-03-26 13:07:22 +010066 int ret;
67
Marek Vasutb204c2a2024-03-26 13:07:25 +010068 if (!CONFIG_IS_ENABLED(CLK))
69 return 0;
70
Marek Vasut2e8f75b2024-03-26 13:07:28 +010071 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut34374262024-03-26 13:07:22 +010072
73 ret = clk_enable(&eqos->clk_master_bus);
74 if (ret < 0) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +010075 dev_err(dev, "clk_enable(clk_master_bus) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +010076 goto err;
77 }
78
79 ret = clk_enable(&eqos->clk_rx);
80 if (ret < 0) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +010081 dev_err(dev, "clk_enable(clk_rx) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +010082 goto err_disable_clk_master_bus;
83 }
84
85 ret = clk_enable(&eqos->clk_tx);
86 if (ret < 0) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +010087 dev_err(dev, "clk_enable(clk_tx) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +010088 goto err_disable_clk_rx;
89 }
90
91 if (clk_valid(&eqos->clk_ck) && !eqos->clk_ck_enabled) {
92 ret = clk_enable(&eqos->clk_ck);
93 if (ret < 0) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +010094 dev_err(dev, "clk_enable(clk_ck) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +010095 goto err_disable_clk_tx;
96 }
97 eqos->clk_ck_enabled = true;
98 }
Marek Vasut34374262024-03-26 13:07:22 +010099
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100100 dev_dbg(dev, "%s: OK\n", __func__);
Marek Vasut34374262024-03-26 13:07:22 +0100101 return 0;
102
Marek Vasut34374262024-03-26 13:07:22 +0100103err_disable_clk_tx:
104 clk_disable(&eqos->clk_tx);
105err_disable_clk_rx:
106 clk_disable(&eqos->clk_rx);
107err_disable_clk_master_bus:
108 clk_disable(&eqos->clk_master_bus);
109err:
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100110 dev_dbg(dev, "%s: FAILED: %d\n", __func__, ret);
111
Marek Vasut34374262024-03-26 13:07:22 +0100112 return ret;
Marek Vasut34374262024-03-26 13:07:22 +0100113}
114
115static int eqos_stop_clks_stm32(struct udevice *dev)
116{
Marek Vasutb204c2a2024-03-26 13:07:25 +0100117 struct eqos_priv __maybe_unused *eqos = dev_get_priv(dev);
118
119 if (!CONFIG_IS_ENABLED(CLK))
120 return 0;
Marek Vasut34374262024-03-26 13:07:22 +0100121
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100122 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut34374262024-03-26 13:07:22 +0100123
124 clk_disable(&eqos->clk_tx);
125 clk_disable(&eqos->clk_rx);
126 clk_disable(&eqos->clk_master_bus);
Marek Vasut34374262024-03-26 13:07:22 +0100127
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100128 dev_dbg(dev, "%s: OK\n", __func__);
129
Marek Vasut34374262024-03-26 13:07:22 +0100130 return 0;
131}
132
Marek Vasutd100c1a2024-03-26 13:07:24 +0100133static int eqos_probe_syscfg_stm32(struct udevice *dev,
134 phy_interface_t interface_type)
135{
Marek Vasut22265e22024-03-26 13:07:29 +0100136 /* Ethernet 50MHz RMII clock selection. */
137 const bool eth_ref_clk_sel = dev_read_bool(dev, "st,eth-ref-clk-sel");
Christophe Roulliera440d192024-03-26 13:07:30 +0100138 /* SoC is STM32MP13xx with two ethernet MACs */
139 const bool is_mp13 = device_is_compatible(dev, "st,stm32mp13-dwmac");
Marek Vasut22265e22024-03-26 13:07:29 +0100140 /* Gigabit Ethernet 125MHz clock selection. */
141 const bool eth_clk_sel = dev_read_bool(dev, "st,eth-clk-sel");
Marek Vasut1ef28c52024-03-26 13:07:32 +0100142 /* Ethernet clock source is RCC. */
143 const bool ext_phyclk = dev_read_bool(dev, "st,ext-phyclk");
Christophe Roulliera440d192024-03-26 13:07:30 +0100144 struct regmap *regmap;
145 u32 regmap_mask;
Marek Vasutd100c1a2024-03-26 13:07:24 +0100146 u32 value;
147
Christophe Roulliera440d192024-03-26 13:07:30 +0100148 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscon");
149 if (IS_ERR(regmap))
150 return PTR_ERR(regmap);
151
152 regmap_mask = dev_read_u32_index_default(dev, "st,syscon", 2,
153 SYSCFG_PMCSETR_ETH1_MASK);
Marek Vasutd100c1a2024-03-26 13:07:24 +0100154
155 switch (interface_type) {
156 case PHY_INTERFACE_MODE_MII:
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100157 dev_dbg(dev, "PHY_INTERFACE_MODE_MII\n");
Marek Vasut416592e2024-03-26 13:07:26 +0100158 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
159 SYSCFG_PMCSETR_ETH_SEL_GMII_MII);
Marek Vasut1ef28c52024-03-26 13:07:32 +0100160 /*
161 * STM32MP15xx supports both MII and GMII, STM32MP13xx MII only.
162 * SYSCFG_PMCSETR ETH_SELMII is present only on STM32MP15xx and
163 * acts as a selector between 0:GMII and 1:MII. As STM32MP13xx
164 * supports only MII, ETH_SELMII is not present.
165 */
Christophe Roulliera440d192024-03-26 13:07:30 +0100166 if (!is_mp13) /* Select MII mode on STM32MP15xx */
167 value |= SYSCFG_PMCSETR_ETH_SELMII;
Marek Vasutd100c1a2024-03-26 13:07:24 +0100168 break;
Christophe Roulliera440d192024-03-26 13:07:30 +0100169 case PHY_INTERFACE_MODE_GMII: /* STM32MP15xx only */
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100170 dev_dbg(dev, "PHY_INTERFACE_MODE_GMII\n");
Marek Vasut416592e2024-03-26 13:07:26 +0100171 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
172 SYSCFG_PMCSETR_ETH_SEL_GMII_MII);
Marek Vasut1ef28c52024-03-26 13:07:32 +0100173 /*
174 * If eth_clk_sel is set, use internal ETH_CLKx clock from RCC,
175 * otherwise use external clock from IO pin (requires matching
176 * GPIO block AF setting of that pin).
177 */
178 if (eth_clk_sel || ext_phyclk)
Marek Vasut416592e2024-03-26 13:07:26 +0100179 value |= SYSCFG_PMCSETR_ETH_CLK_SEL;
Marek Vasutd100c1a2024-03-26 13:07:24 +0100180 break;
181 case PHY_INTERFACE_MODE_RMII:
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100182 dev_dbg(dev, "PHY_INTERFACE_MODE_RMII\n");
Marek Vasut416592e2024-03-26 13:07:26 +0100183 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
184 SYSCFG_PMCSETR_ETH_SEL_RMII);
Marek Vasut1ef28c52024-03-26 13:07:32 +0100185 /*
186 * If eth_ref_clk_sel is set, use internal clock from RCC,
187 * otherwise use external clock from ETHn_RX_CLK/ETHn_REF_CLK
188 * IO pin (requires matching GPIO block AF setting of that
189 * pin).
190 */
191 if (eth_ref_clk_sel || ext_phyclk)
Marek Vasut416592e2024-03-26 13:07:26 +0100192 value |= SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
Marek Vasutd100c1a2024-03-26 13:07:24 +0100193 break;
194 case PHY_INTERFACE_MODE_RGMII:
195 case PHY_INTERFACE_MODE_RGMII_ID:
196 case PHY_INTERFACE_MODE_RGMII_RXID:
197 case PHY_INTERFACE_MODE_RGMII_TXID:
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100198 dev_dbg(dev, "PHY_INTERFACE_MODE_RGMII\n");
Marek Vasut416592e2024-03-26 13:07:26 +0100199 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
200 SYSCFG_PMCSETR_ETH_SEL_RGMII);
Marek Vasut1ef28c52024-03-26 13:07:32 +0100201 /*
202 * If eth_clk_sel is set, use internal ETH_CLKx clock from RCC,
203 * otherwise use external clock from ETHx_CLK125 pin (requires
204 * matching GPIO block AF setting of that pin).
205 */
206 if (eth_clk_sel || ext_phyclk)
Marek Vasut416592e2024-03-26 13:07:26 +0100207 value |= SYSCFG_PMCSETR_ETH_CLK_SEL;
Marek Vasutd100c1a2024-03-26 13:07:24 +0100208 break;
209 default:
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100210 dev_dbg(dev, "Do not manage %d interface\n",
211 interface_type);
Marek Vasutd100c1a2024-03-26 13:07:24 +0100212 /* Do not manage others interfaces */
213 return -EINVAL;
214 }
215
Christophe Roulliera440d192024-03-26 13:07:30 +0100216 /* Shift value at correct ethernet MAC offset in SYSCFG_PMCSETR */
217 value <<= ffs(regmap_mask) - ffs(SYSCFG_PMCSETR_ETH1_MASK);
Marek Vasutd100c1a2024-03-26 13:07:24 +0100218
Christophe Roulliera440d192024-03-26 13:07:30 +0100219 /* Update PMCCLRR (clear register) */
220 regmap_write(regmap, is_mp13 ?
221 SYSCFG_PMCCLRR_MP13 : SYSCFG_PMCCLRR_MP15,
222 regmap_mask);
223
224 return regmap_update_bits(regmap, SYSCFG_PMCSETR, regmap_mask, value);
Marek Vasutd100c1a2024-03-26 13:07:24 +0100225}
226
Marek Vasut34374262024-03-26 13:07:22 +0100227static int eqos_probe_resources_stm32(struct udevice *dev)
228{
229 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut34374262024-03-26 13:07:22 +0100230 phy_interface_t interface;
Marek Vasutd100c1a2024-03-26 13:07:24 +0100231 int ret;
Marek Vasut34374262024-03-26 13:07:22 +0100232
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100233 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut34374262024-03-26 13:07:22 +0100234
235 interface = eqos->config->interface(dev);
236
237 if (interface == PHY_INTERFACE_MODE_NA) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100238 dev_err(dev, "Invalid PHY interface\n");
Marek Vasut34374262024-03-26 13:07:22 +0100239 return -EINVAL;
240 }
241
Marek Vasutd100c1a2024-03-26 13:07:24 +0100242 ret = eqos_probe_syscfg_stm32(dev, interface);
Marek Vasut34374262024-03-26 13:07:22 +0100243 if (ret)
244 return -EINVAL;
245
246 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
247 if (ret) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100248 dev_err(dev, "clk_get_by_name(master_bus) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +0100249 goto err_probe;
250 }
251
252 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
253 if (ret) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100254 dev_err(dev, "clk_get_by_name(rx) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +0100255 goto err_probe;
256 }
257
258 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
259 if (ret) {
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100260 dev_err(dev, "clk_get_by_name(tx) failed: %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +0100261 goto err_probe;
262 }
263
264 /* Get ETH_CLK clocks (optional) */
265 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
266 if (ret)
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100267 dev_warn(dev, "No phy clock provided %d\n", ret);
Marek Vasut34374262024-03-26 13:07:22 +0100268
Heesub Shin0da5d222024-04-28 23:24:05 +0900269 /* Get reset gpio pin (optional) */
270 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
271 &eqos->phy_reset_gpio, GPIOD_IS_OUT);
272 if (ret)
273 pr_warn("No phy reset gpio provided: %d\n", ret);
274
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100275 dev_dbg(dev, "%s: OK\n", __func__);
276
Marek Vasut34374262024-03-26 13:07:22 +0100277 return 0;
278
279err_probe:
280
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100281 dev_dbg(dev, "%s: returns %d\n", __func__, ret);
282
Marek Vasut34374262024-03-26 13:07:22 +0100283 return ret;
284}
285
Heesub Shin0da5d222024-04-28 23:24:05 +0900286static int eqos_start_resets_stm32(struct udevice *dev)
287{
288 struct eqos_priv *eqos = dev_get_priv(dev);
289
290 debug("%s(dev=%p):\n", __func__, dev);
291
292 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
293 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
294 udelay(2);
295 dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
296 }
297
298 return 0;
299}
300
Marek Vasut34374262024-03-26 13:07:22 +0100301static int eqos_remove_resources_stm32(struct udevice *dev)
302{
Marek Vasut2e8f75b2024-03-26 13:07:28 +0100303 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut34374262024-03-26 13:07:22 +0100304
305 return 0;
306}
307
308static struct eqos_ops eqos_stm32_ops = {
309 .eqos_inval_desc = eqos_inval_desc_generic,
310 .eqos_flush_desc = eqos_flush_desc_generic,
311 .eqos_inval_buffer = eqos_inval_buffer_generic,
312 .eqos_flush_buffer = eqos_flush_buffer_generic,
313 .eqos_probe_resources = eqos_probe_resources_stm32,
314 .eqos_remove_resources = eqos_remove_resources_stm32,
315 .eqos_stop_resets = eqos_null_ops,
Heesub Shin0da5d222024-04-28 23:24:05 +0900316 .eqos_start_resets = eqos_start_resets_stm32,
Marek Vasut34374262024-03-26 13:07:22 +0100317 .eqos_stop_clks = eqos_stop_clks_stm32,
318 .eqos_start_clks = eqos_start_clks_stm32,
319 .eqos_calibrate_pads = eqos_null_ops,
320 .eqos_disable_calibration = eqos_null_ops,
321 .eqos_set_tx_clk_speed = eqos_null_ops,
322 .eqos_get_enetaddr = eqos_null_ops,
323 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
324};
325
Christophe Roullier882b2282024-03-26 13:07:31 +0100326struct eqos_config __maybe_unused eqos_stm32mp13_config = {
327 .reg_access_always_ok = false,
328 .mdio_wait = 10000,
329 .swr_wait = 50,
330 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
331 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
332 .axi_bus_width = EQOS_AXI_WIDTH_32,
333 .interface = dev_read_phy_mode,
334 .ops = &eqos_stm32_ops
335};
336
Marek Vasut85d1de92024-03-26 13:07:23 +0100337struct eqos_config __maybe_unused eqos_stm32mp15_config = {
Marek Vasut34374262024-03-26 13:07:22 +0100338 .reg_access_always_ok = false,
339 .mdio_wait = 10000,
340 .swr_wait = 50,
341 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
342 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
343 .axi_bus_width = EQOS_AXI_WIDTH_64,
344 .interface = dev_read_phy_mode,
345 .ops = &eqos_stm32_ops
346};