blob: 642432834f503a17bc95ef76d9b4a4fcd075b920 [file] [log] [blame]
Peng Fan5721a822022-07-26 16:41:15 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright 2022 NXP
4 */
5
Peng Fan5721a822022-07-26 16:41:15 +08006#include <clk.h>
7#include <cpu_func.h>
8#include <dm.h>
Marek Vasut6a9b7bf2023-03-06 15:53:47 +01009#include <dm/device_compat.h>
Peng Fan5721a822022-07-26 16:41:15 +080010#include <errno.h>
11#include <eth_phy.h>
12#include <log.h>
13#include <malloc.h>
14#include <memalign.h>
15#include <miiphy.h>
16#include <net.h>
17#include <netdev.h>
18#include <phy.h>
19#include <reset.h>
20#include <wait_bit.h>
21#include <asm/arch/clock.h>
22#include <asm/cache.h>
23#include <asm/gpio.h>
24#include <asm/io.h>
25#include <asm/mach-imx/sys_proto.h>
26#include <linux/delay.h>
27
28#include "dwc_eth_qos.h"
29
30__weak u32 imx_get_eqos_csr_clk(void)
31{
32 return 100 * 1000000;
33}
34
Peng Fan5721a822022-07-26 16:41:15 +080035static ulong eqos_get_tick_clk_rate_imx(struct udevice *dev)
36{
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010037 struct eqos_priv *eqos = dev_get_priv(dev);
38
39 return clk_get_rate(&eqos->clk_master_bus);
Peng Fan5721a822022-07-26 16:41:15 +080040}
41
42static int eqos_probe_resources_imx(struct udevice *dev)
43{
44 struct eqos_priv *eqos = dev_get_priv(dev);
45 phy_interface_t interface;
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010046 int ret;
Peng Fan5721a822022-07-26 16:41:15 +080047
48 debug("%s(dev=%p):\n", __func__, dev);
49
Philip Oberfichtnerd6d22da2024-08-02 11:25:37 +020050 ret = eqos_get_base_addr_dt(dev);
51 if (ret) {
52 dev_dbg(dev, "eqos_get_base_addr_dt failed: %d", ret);
53 goto err_probe;
54 }
55
Peng Fan5721a822022-07-26 16:41:15 +080056 interface = eqos->config->interface(dev);
57
58 if (interface == PHY_INTERFACE_MODE_NA) {
59 pr_err("Invalid PHY interface\n");
60 return -EINVAL;
61 }
62
Marek Vasute6576952023-03-06 15:53:49 +010063 ret = board_interface_eth_init(dev, interface);
64 if (ret)
65 return -EINVAL;
66
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010067 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
68
69 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
70 if (ret) {
71 dev_dbg(dev, "clk_get_by_name(master_bus) failed: %d", ret);
72 goto err_probe;
73 }
74
75 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
76 if (ret) {
77 dev_dbg(dev, "clk_get_by_name(ptp_ref) failed: %d", ret);
Sean Andersond318eb32023-12-16 14:38:42 -050078 goto err_probe;
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010079 }
80
81 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
82 if (ret) {
83 dev_dbg(dev, "clk_get_by_name(tx) failed: %d", ret);
Sean Andersond318eb32023-12-16 14:38:42 -050084 goto err_probe;
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010085 }
86
87 ret = clk_get_by_name(dev, "pclk", &eqos->clk_ck);
88 if (ret) {
89 dev_dbg(dev, "clk_get_by_name(pclk) failed: %d", ret);
Sean Andersond318eb32023-12-16 14:38:42 -050090 goto err_probe;
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010091 }
92
Peng Fan5721a822022-07-26 16:41:15 +080093 debug("%s: OK\n", __func__);
94 return 0;
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010095
Marek Vasut6a9b7bf2023-03-06 15:53:47 +010096err_probe:
97
98 debug("%s: returns %d\n", __func__, ret);
99 return ret;
100}
101
102static int eqos_remove_resources_imx(struct udevice *dev)
103{
Marek Vasut6a9b7bf2023-03-06 15:53:47 +0100104 debug("%s(dev=%p):\n", __func__, dev);
Marek Vasut6a9b7bf2023-03-06 15:53:47 +0100105 return 0;
106}
107
108static int eqos_start_clks_imx(struct udevice *dev)
109{
110 struct eqos_priv *eqos = dev_get_priv(dev);
111 int ret;
112
113 debug("%s(dev=%p):\n", __func__, dev);
114
115 ret = clk_enable(&eqos->clk_master_bus);
116 if (ret < 0) {
117 dev_dbg(dev, "clk_enable(clk_master_bus) failed: %d", ret);
118 goto err;
119 }
120
121 ret = clk_enable(&eqos->clk_ptp_ref);
122 if (ret < 0) {
123 dev_dbg(dev, "clk_enable(clk_ptp_ref) failed: %d", ret);
124 goto err_disable_clk_master_bus;
125 }
126
127 ret = clk_enable(&eqos->clk_tx);
128 if (ret < 0) {
129 dev_dbg(dev, "clk_enable(clk_tx) failed: %d", ret);
130 goto err_disable_clk_ptp_ref;
131 }
132
133 ret = clk_enable(&eqos->clk_ck);
134 if (ret < 0) {
135 dev_dbg(dev, "clk_enable(clk_ck) failed: %d", ret);
136 goto err_disable_clk_tx;
137 }
138
139 debug("%s: OK\n", __func__);
140 return 0;
141
142err_disable_clk_tx:
143 clk_disable(&eqos->clk_tx);
144err_disable_clk_ptp_ref:
145 clk_disable(&eqos->clk_ptp_ref);
146err_disable_clk_master_bus:
147 clk_disable(&eqos->clk_master_bus);
148err:
149 debug("%s: FAILED: %d\n", __func__, ret);
150 return ret;
151}
152
153static int eqos_stop_clks_imx(struct udevice *dev)
154{
155 struct eqos_priv *eqos = dev_get_priv(dev);
156
157 debug("%s(dev=%p):\n", __func__, dev);
158
159 clk_disable(&eqos->clk_ck);
160 clk_disable(&eqos->clk_tx);
161 clk_disable(&eqos->clk_ptp_ref);
162 clk_disable(&eqos->clk_master_bus);
163
164 debug("%s: OK\n", __func__);
165 return 0;
Peng Fan5721a822022-07-26 16:41:15 +0800166}
167
168static int eqos_set_tx_clk_speed_imx(struct udevice *dev)
169{
170 struct eqos_priv *eqos = dev_get_priv(dev);
171 ulong rate;
172 int ret;
173
Sébastien Szymanski28b7fc42023-10-17 11:44:58 +0200174 if (device_is_compatible(dev, "nxp,imx93-dwmac-eqos"))
175 return 0;
176
Peng Fan5721a822022-07-26 16:41:15 +0800177 debug("%s(dev=%p):\n", __func__, dev);
178
Marek Vasut03705e12023-03-06 15:53:48 +0100179 if (eqos->phy->interface == PHY_INTERFACE_MODE_RMII)
180 rate = 5000; /* 5000 kHz = 5 MHz */
181 else
182 rate = 2500; /* 2500 kHz = 2.5 MHz */
183
184 if (eqos->phy->speed == SPEED_1000 &&
185 (eqos->phy->interface == PHY_INTERFACE_MODE_RGMII ||
186 eqos->phy->interface == PHY_INTERFACE_MODE_RGMII_ID ||
187 eqos->phy->interface == PHY_INTERFACE_MODE_RGMII_RXID ||
188 eqos->phy->interface == PHY_INTERFACE_MODE_RGMII_TXID)) {
189 rate *= 50; /* Use 50x base rate i.e. 125 MHz */
190 } else if (eqos->phy->speed == SPEED_100) {
191 rate *= 10; /* Use 10x base rate */
192 } else if (eqos->phy->speed == SPEED_10) {
193 rate *= 1; /* Use base rate */
194 } else {
Peng Fan5721a822022-07-26 16:41:15 +0800195 pr_err("invalid speed %d", eqos->phy->speed);
196 return -EINVAL;
197 }
198
Marek Vasut03705e12023-03-06 15:53:48 +0100199 rate *= 1000; /* clk_set_rate() operates in Hz */
200
Marek Vasut6a9b7bf2023-03-06 15:53:47 +0100201 ret = clk_set_rate(&eqos->clk_tx, rate);
Peng Fan5721a822022-07-26 16:41:15 +0800202 if (ret < 0) {
203 pr_err("imx (tx_clk, %lu) failed: %d", rate, ret);
204 return ret;
205 }
206
207 return 0;
208}
209
Peng Fanbf69a7b92022-07-26 16:41:17 +0800210static int eqos_get_enetaddr_imx(struct udevice *dev)
211{
212 struct eth_pdata *pdata = dev_get_plat(dev);
213
214 imx_get_mac_from_fuse(dev_seq(dev), pdata->enetaddr);
215
216 return 0;
217}
218
Peng Fan5721a822022-07-26 16:41:15 +0800219static struct eqos_ops eqos_imx_ops = {
220 .eqos_inval_desc = eqos_inval_desc_generic,
221 .eqos_flush_desc = eqos_flush_desc_generic,
222 .eqos_inval_buffer = eqos_inval_buffer_generic,
223 .eqos_flush_buffer = eqos_flush_buffer_generic,
224 .eqos_probe_resources = eqos_probe_resources_imx,
Marek Vasut6a9b7bf2023-03-06 15:53:47 +0100225 .eqos_remove_resources = eqos_remove_resources_imx,
Peng Fan5721a822022-07-26 16:41:15 +0800226 .eqos_stop_resets = eqos_null_ops,
227 .eqos_start_resets = eqos_null_ops,
Marek Vasut6a9b7bf2023-03-06 15:53:47 +0100228 .eqos_stop_clks = eqos_stop_clks_imx,
229 .eqos_start_clks = eqos_start_clks_imx,
Peng Fan5721a822022-07-26 16:41:15 +0800230 .eqos_calibrate_pads = eqos_null_ops,
231 .eqos_disable_calibration = eqos_null_ops,
232 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_imx,
Peng Fanbf69a7b92022-07-26 16:41:17 +0800233 .eqos_get_enetaddr = eqos_get_enetaddr_imx,
234 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_imx,
Peng Fan5721a822022-07-26 16:41:15 +0800235};
236
237struct eqos_config __maybe_unused eqos_imx_config = {
238 .reg_access_always_ok = false,
239 .mdio_wait = 10,
240 .swr_wait = 50,
241 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
242 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
243 .axi_bus_width = EQOS_AXI_WIDTH_64,
244 .interface = dev_read_phy_mode,
245 .ops = &eqos_imx_ops
246};