blob: 435473f99a6c144c149e7d73dc41519fc7ef2c81 [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");
Christophe Roullier033dc762024-03-26 13:07:30 +0100143 struct regmap *regmap;
144 u32 regmap_mask;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100145 u32 value;
146
Christophe Roullier033dc762024-03-26 13:07:30 +0100147 regmap = syscon_regmap_lookup_by_phandle(dev, "st,syscon");
148 if (IS_ERR(regmap))
149 return PTR_ERR(regmap);
150
151 regmap_mask = dev_read_u32_index_default(dev, "st,syscon", 2,
152 SYSCFG_PMCSETR_ETH1_MASK);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100153
154 switch (interface_type) {
155 case PHY_INTERFACE_MODE_MII:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100156 dev_dbg(dev, "PHY_INTERFACE_MODE_MII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100157 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
158 SYSCFG_PMCSETR_ETH_SEL_GMII_MII);
Christophe Roullier033dc762024-03-26 13:07:30 +0100159 if (!is_mp13) /* Select MII mode on STM32MP15xx */
160 value |= SYSCFG_PMCSETR_ETH_SELMII;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100161 break;
Christophe Roullier033dc762024-03-26 13:07:30 +0100162 case PHY_INTERFACE_MODE_GMII: /* STM32MP15xx only */
Marek Vasut5a1640e2024-03-26 13:07:28 +0100163 dev_dbg(dev, "PHY_INTERFACE_MODE_GMII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100164 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
165 SYSCFG_PMCSETR_ETH_SEL_GMII_MII);
Marek Vasute77fa872024-03-26 13:07:29 +0100166 if (eth_clk_sel)
Marek Vasut577f46b2024-03-26 13:07:26 +0100167 value |= SYSCFG_PMCSETR_ETH_CLK_SEL;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100168 break;
169 case PHY_INTERFACE_MODE_RMII:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100170 dev_dbg(dev, "PHY_INTERFACE_MODE_RMII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100171 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
172 SYSCFG_PMCSETR_ETH_SEL_RMII);
Marek Vasute77fa872024-03-26 13:07:29 +0100173 if (eth_ref_clk_sel)
Marek Vasut577f46b2024-03-26 13:07:26 +0100174 value |= SYSCFG_PMCSETR_ETH_REF_CLK_SEL;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100175 break;
176 case PHY_INTERFACE_MODE_RGMII:
177 case PHY_INTERFACE_MODE_RGMII_ID:
178 case PHY_INTERFACE_MODE_RGMII_RXID:
179 case PHY_INTERFACE_MODE_RGMII_TXID:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100180 dev_dbg(dev, "PHY_INTERFACE_MODE_RGMII\n");
Marek Vasut577f46b2024-03-26 13:07:26 +0100181 value = FIELD_PREP(SYSCFG_PMCSETR_ETH_SEL_MASK,
182 SYSCFG_PMCSETR_ETH_SEL_RGMII);
Marek Vasute77fa872024-03-26 13:07:29 +0100183 if (eth_clk_sel)
Marek Vasut577f46b2024-03-26 13:07:26 +0100184 value |= SYSCFG_PMCSETR_ETH_CLK_SEL;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100185 break;
186 default:
Marek Vasut5a1640e2024-03-26 13:07:28 +0100187 dev_dbg(dev, "Do not manage %d interface\n",
188 interface_type);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100189 /* Do not manage others interfaces */
190 return -EINVAL;
191 }
192
Christophe Roullier033dc762024-03-26 13:07:30 +0100193 /* Shift value at correct ethernet MAC offset in SYSCFG_PMCSETR */
194 value <<= ffs(regmap_mask) - ffs(SYSCFG_PMCSETR_ETH1_MASK);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100195
Christophe Roullier033dc762024-03-26 13:07:30 +0100196 /* Update PMCCLRR (clear register) */
197 regmap_write(regmap, is_mp13 ?
198 SYSCFG_PMCCLRR_MP13 : SYSCFG_PMCCLRR_MP15,
199 regmap_mask);
200
201 return regmap_update_bits(regmap, SYSCFG_PMCSETR, regmap_mask, value);
Marek Vasut7595bfc2024-03-26 13:07:24 +0100202}
203
Marek Vasut426ca622024-03-26 13:07:22 +0100204static int eqos_probe_resources_stm32(struct udevice *dev)
205{
206 struct eqos_priv *eqos = dev_get_priv(dev);
Marek Vasut426ca622024-03-26 13:07:22 +0100207 phy_interface_t interface;
Marek Vasut7595bfc2024-03-26 13:07:24 +0100208 int ret;
Marek Vasut426ca622024-03-26 13:07:22 +0100209
Marek Vasut5a1640e2024-03-26 13:07:28 +0100210 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +0100211
212 interface = eqos->config->interface(dev);
213
214 if (interface == PHY_INTERFACE_MODE_NA) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100215 dev_err(dev, "Invalid PHY interface\n");
Marek Vasut426ca622024-03-26 13:07:22 +0100216 return -EINVAL;
217 }
218
Marek Vasut7595bfc2024-03-26 13:07:24 +0100219 ret = eqos_probe_syscfg_stm32(dev, interface);
Marek Vasut426ca622024-03-26 13:07:22 +0100220 if (ret)
221 return -EINVAL;
222
223 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
224 if (ret) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100225 dev_err(dev, "clk_get_by_name(master_bus) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100226 goto err_probe;
227 }
228
229 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
230 if (ret) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100231 dev_err(dev, "clk_get_by_name(rx) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100232 goto err_probe;
233 }
234
235 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
236 if (ret) {
Marek Vasut5a1640e2024-03-26 13:07:28 +0100237 dev_err(dev, "clk_get_by_name(tx) failed: %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100238 goto err_probe;
239 }
240
241 /* Get ETH_CLK clocks (optional) */
242 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
243 if (ret)
Marek Vasut5a1640e2024-03-26 13:07:28 +0100244 dev_warn(dev, "No phy clock provided %d\n", ret);
Marek Vasut426ca622024-03-26 13:07:22 +0100245
Marek Vasut5a1640e2024-03-26 13:07:28 +0100246 dev_dbg(dev, "%s: OK\n", __func__);
247
Marek Vasut426ca622024-03-26 13:07:22 +0100248 return 0;
249
250err_probe:
251
Marek Vasut5a1640e2024-03-26 13:07:28 +0100252 dev_dbg(dev, "%s: returns %d\n", __func__, ret);
253
Marek Vasut426ca622024-03-26 13:07:22 +0100254 return ret;
255}
256
257static int eqos_remove_resources_stm32(struct udevice *dev)
258{
Marek Vasut5a1640e2024-03-26 13:07:28 +0100259 dev_dbg(dev, "%s:\n", __func__);
Marek Vasut426ca622024-03-26 13:07:22 +0100260
261 return 0;
262}
263
264static struct eqos_ops eqos_stm32_ops = {
265 .eqos_inval_desc = eqos_inval_desc_generic,
266 .eqos_flush_desc = eqos_flush_desc_generic,
267 .eqos_inval_buffer = eqos_inval_buffer_generic,
268 .eqos_flush_buffer = eqos_flush_buffer_generic,
269 .eqos_probe_resources = eqos_probe_resources_stm32,
270 .eqos_remove_resources = eqos_remove_resources_stm32,
271 .eqos_stop_resets = eqos_null_ops,
272 .eqos_start_resets = eqos_null_ops,
273 .eqos_stop_clks = eqos_stop_clks_stm32,
274 .eqos_start_clks = eqos_start_clks_stm32,
275 .eqos_calibrate_pads = eqos_null_ops,
276 .eqos_disable_calibration = eqos_null_ops,
277 .eqos_set_tx_clk_speed = eqos_null_ops,
278 .eqos_get_enetaddr = eqos_null_ops,
279 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
280};
281
Christophe Roullier25a16862024-03-26 13:07:31 +0100282struct eqos_config __maybe_unused eqos_stm32mp13_config = {
283 .reg_access_always_ok = false,
284 .mdio_wait = 10000,
285 .swr_wait = 50,
286 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
287 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
288 .axi_bus_width = EQOS_AXI_WIDTH_32,
289 .interface = dev_read_phy_mode,
290 .ops = &eqos_stm32_ops
291};
292
Marek Vasut944ba372024-03-26 13:07:23 +0100293struct eqos_config __maybe_unused eqos_stm32mp15_config = {
Marek Vasut426ca622024-03-26 13:07:22 +0100294 .reg_access_always_ok = false,
295 .mdio_wait = 10000,
296 .swr_wait = 50,
297 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
298 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
299 .axi_bus_width = EQOS_AXI_WIDTH_64,
300 .interface = dev_read_phy_mode,
301 .ops = &eqos_stm32_ops
302};