blob: f048e9d58518658176f513fa2829389996776acf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stephen Warren50709602016-10-21 14:46:47 -06002/*
3 * Copyright (c) 2016, NVIDIA CORPORATION.
4 *
Stephen Warren50709602016-10-21 14:46:47 -06005 * Portions based on U-Boot's rtl8169.c.
6 */
7
8/*
9 * This driver supports the Synopsys Designware Ethernet QOS (Quality Of
10 * Service) IP block. The IP supports multiple options for bus type, clocking/
11 * reset structure, and feature list.
12 *
13 * The driver is written such that generic core logic is kept separate from
14 * configuration-specific logic. Code that interacts with configuration-
15 * specific resources is split out into separate functions to avoid polluting
16 * common code. If/when this driver is enhanced to support multiple
17 * configurations, the core code should be adapted to call all configuration-
18 * specific functions through function pointers, with the definition of those
19 * function pointers being supplied by struct udevice_id eqos_ids[]'s .data
20 * field.
21 *
22 * The following configurations are currently supported:
23 * tegra186:
24 * NVIDIA's Tegra186 chip. This configuration uses an AXI master/DMA bus, an
25 * AHB slave/register bus, contains the DMA, MTL, and MAC sub-blocks, and
26 * supports a single RGMII PHY. This configuration also has SW control over
27 * all clock and reset signals to the HW block.
28 */
Patrick Delaunay9e6ed382020-09-09 18:30:06 +020029
Stephen Warren50709602016-10-21 14:46:47 -060030#include <common.h>
31#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070032#include <cpu_func.h>
Stephen Warren50709602016-10-21 14:46:47 -060033#include <dm.h>
34#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060035#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070036#include <malloc.h>
Stephen Warren50709602016-10-21 14:46:47 -060037#include <memalign.h>
38#include <miiphy.h>
39#include <net.h>
40#include <netdev.h>
41#include <phy.h>
42#include <reset.h>
43#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060044#include <asm/cache.h>
Stephen Warren50709602016-10-21 14:46:47 -060045#include <asm/gpio.h>
46#include <asm/io.h>
Ye Liad122b72020-05-03 22:41:15 +080047#include <eth_phy.h>
Fugang Duandd455e62020-05-03 22:41:18 +080048#ifdef CONFIG_ARCH_IMX8M
49#include <asm/arch/clock.h>
50#include <asm/mach-imx/sys_proto.h>
51#endif
Simon Glass4dcacfc2020-05-10 11:40:13 -060052#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060053#include <linux/delay.h>
Stephen Warren50709602016-10-21 14:46:47 -060054
55/* Core registers */
56
57#define EQOS_MAC_REGS_BASE 0x000
58struct eqos_mac_regs {
59 uint32_t configuration; /* 0x000 */
60 uint32_t unused_004[(0x070 - 0x004) / 4]; /* 0x004 */
61 uint32_t q0_tx_flow_ctrl; /* 0x070 */
62 uint32_t unused_070[(0x090 - 0x074) / 4]; /* 0x074 */
63 uint32_t rx_flow_ctrl; /* 0x090 */
64 uint32_t unused_094; /* 0x094 */
65 uint32_t txq_prty_map0; /* 0x098 */
66 uint32_t unused_09c; /* 0x09c */
67 uint32_t rxq_ctrl0; /* 0x0a0 */
68 uint32_t unused_0a4; /* 0x0a4 */
69 uint32_t rxq_ctrl2; /* 0x0a8 */
70 uint32_t unused_0ac[(0x0dc - 0x0ac) / 4]; /* 0x0ac */
71 uint32_t us_tic_counter; /* 0x0dc */
72 uint32_t unused_0e0[(0x11c - 0x0e0) / 4]; /* 0x0e0 */
73 uint32_t hw_feature0; /* 0x11c */
74 uint32_t hw_feature1; /* 0x120 */
75 uint32_t hw_feature2; /* 0x124 */
76 uint32_t unused_128[(0x200 - 0x128) / 4]; /* 0x128 */
77 uint32_t mdio_address; /* 0x200 */
78 uint32_t mdio_data; /* 0x204 */
79 uint32_t unused_208[(0x300 - 0x208) / 4]; /* 0x208 */
80 uint32_t address0_high; /* 0x300 */
81 uint32_t address0_low; /* 0x304 */
82};
83
84#define EQOS_MAC_CONFIGURATION_GPSLCE BIT(23)
85#define EQOS_MAC_CONFIGURATION_CST BIT(21)
86#define EQOS_MAC_CONFIGURATION_ACS BIT(20)
87#define EQOS_MAC_CONFIGURATION_WD BIT(19)
88#define EQOS_MAC_CONFIGURATION_JD BIT(17)
89#define EQOS_MAC_CONFIGURATION_JE BIT(16)
90#define EQOS_MAC_CONFIGURATION_PS BIT(15)
91#define EQOS_MAC_CONFIGURATION_FES BIT(14)
92#define EQOS_MAC_CONFIGURATION_DM BIT(13)
Fugang Duan37aae5f2020-05-03 22:41:17 +080093#define EQOS_MAC_CONFIGURATION_LM BIT(12)
Stephen Warren50709602016-10-21 14:46:47 -060094#define EQOS_MAC_CONFIGURATION_TE BIT(1)
95#define EQOS_MAC_CONFIGURATION_RE BIT(0)
96
97#define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT 16
98#define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_MASK 0xffff
99#define EQOS_MAC_Q0_TX_FLOW_CTRL_TFE BIT(1)
100
101#define EQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
102
103#define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT 0
104#define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK 0xff
105
106#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT 0
107#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK 3
108#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_NOT_ENABLED 0
109#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB 2
Christophe Roullier6beb7802019-05-17 15:08:44 +0200110#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV 1
Stephen Warren50709602016-10-21 14:46:47 -0600111
112#define EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT 0
113#define EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK 0xff
114
Fugang Duan37aae5f2020-05-03 22:41:17 +0800115#define EQOS_MAC_HW_FEATURE0_MMCSEL_SHIFT 8
116#define EQOS_MAC_HW_FEATURE0_HDSEL_SHIFT 2
117#define EQOS_MAC_HW_FEATURE0_GMIISEL_SHIFT 1
118#define EQOS_MAC_HW_FEATURE0_MIISEL_SHIFT 0
119
Stephen Warren50709602016-10-21 14:46:47 -0600120#define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT 6
121#define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK 0x1f
122#define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT 0
123#define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK 0x1f
124
Fugang Duan37aae5f2020-05-03 22:41:17 +0800125#define EQOS_MAC_HW_FEATURE3_ASP_SHIFT 28
126#define EQOS_MAC_HW_FEATURE3_ASP_MASK 0x3
127
Stephen Warren50709602016-10-21 14:46:47 -0600128#define EQOS_MAC_MDIO_ADDRESS_PA_SHIFT 21
129#define EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT 16
130#define EQOS_MAC_MDIO_ADDRESS_CR_SHIFT 8
131#define EQOS_MAC_MDIO_ADDRESS_CR_20_35 2
Christophe Roullier6beb7802019-05-17 15:08:44 +0200132#define EQOS_MAC_MDIO_ADDRESS_CR_250_300 5
Stephen Warren50709602016-10-21 14:46:47 -0600133#define EQOS_MAC_MDIO_ADDRESS_SKAP BIT(4)
134#define EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT 2
135#define EQOS_MAC_MDIO_ADDRESS_GOC_READ 3
136#define EQOS_MAC_MDIO_ADDRESS_GOC_WRITE 1
137#define EQOS_MAC_MDIO_ADDRESS_C45E BIT(1)
138#define EQOS_MAC_MDIO_ADDRESS_GB BIT(0)
139
140#define EQOS_MAC_MDIO_DATA_GD_MASK 0xffff
141
142#define EQOS_MTL_REGS_BASE 0xd00
143struct eqos_mtl_regs {
144 uint32_t txq0_operation_mode; /* 0xd00 */
145 uint32_t unused_d04; /* 0xd04 */
146 uint32_t txq0_debug; /* 0xd08 */
147 uint32_t unused_d0c[(0xd18 - 0xd0c) / 4]; /* 0xd0c */
148 uint32_t txq0_quantum_weight; /* 0xd18 */
149 uint32_t unused_d1c[(0xd30 - 0xd1c) / 4]; /* 0xd1c */
150 uint32_t rxq0_operation_mode; /* 0xd30 */
151 uint32_t unused_d34; /* 0xd34 */
152 uint32_t rxq0_debug; /* 0xd38 */
153};
154
155#define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT 16
156#define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK 0x1ff
157#define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT 2
158#define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_MASK 3
159#define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED 2
160#define EQOS_MTL_TXQ0_OPERATION_MODE_TSF BIT(1)
161#define EQOS_MTL_TXQ0_OPERATION_MODE_FTQ BIT(0)
162
163#define EQOS_MTL_TXQ0_DEBUG_TXQSTS BIT(4)
164#define EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT 1
165#define EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK 3
166
167#define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT 20
168#define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK 0x3ff
169#define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT 14
170#define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK 0x3f
171#define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT 8
172#define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK 0x3f
173#define EQOS_MTL_RXQ0_OPERATION_MODE_EHFC BIT(7)
174#define EQOS_MTL_RXQ0_OPERATION_MODE_RSF BIT(5)
175
176#define EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT 16
177#define EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK 0x7fff
178#define EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT 4
179#define EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK 3
180
181#define EQOS_DMA_REGS_BASE 0x1000
182struct eqos_dma_regs {
183 uint32_t mode; /* 0x1000 */
184 uint32_t sysbus_mode; /* 0x1004 */
185 uint32_t unused_1008[(0x1100 - 0x1008) / 4]; /* 0x1008 */
186 uint32_t ch0_control; /* 0x1100 */
187 uint32_t ch0_tx_control; /* 0x1104 */
188 uint32_t ch0_rx_control; /* 0x1108 */
189 uint32_t unused_110c; /* 0x110c */
190 uint32_t ch0_txdesc_list_haddress; /* 0x1110 */
191 uint32_t ch0_txdesc_list_address; /* 0x1114 */
192 uint32_t ch0_rxdesc_list_haddress; /* 0x1118 */
193 uint32_t ch0_rxdesc_list_address; /* 0x111c */
194 uint32_t ch0_txdesc_tail_pointer; /* 0x1120 */
195 uint32_t unused_1124; /* 0x1124 */
196 uint32_t ch0_rxdesc_tail_pointer; /* 0x1128 */
197 uint32_t ch0_txdesc_ring_length; /* 0x112c */
198 uint32_t ch0_rxdesc_ring_length; /* 0x1130 */
199};
200
201#define EQOS_DMA_MODE_SWR BIT(0)
202
203#define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT 16
204#define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK 0xf
205#define EQOS_DMA_SYSBUS_MODE_EAME BIT(11)
206#define EQOS_DMA_SYSBUS_MODE_BLEN16 BIT(3)
207#define EQOS_DMA_SYSBUS_MODE_BLEN8 BIT(2)
208#define EQOS_DMA_SYSBUS_MODE_BLEN4 BIT(1)
209
Marek Vasut89077732021-01-07 11:12:16 +0100210#define EQOS_DMA_CH0_CONTROL_DSL_SHIFT 18
Stephen Warren50709602016-10-21 14:46:47 -0600211#define EQOS_DMA_CH0_CONTROL_PBLX8 BIT(16)
212
213#define EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT 16
214#define EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK 0x3f
215#define EQOS_DMA_CH0_TX_CONTROL_OSP BIT(4)
216#define EQOS_DMA_CH0_TX_CONTROL_ST BIT(0)
217
218#define EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT 16
219#define EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK 0x3f
220#define EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT 1
221#define EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK 0x3fff
222#define EQOS_DMA_CH0_RX_CONTROL_SR BIT(0)
223
224/* These registers are Tegra186-specific */
225#define EQOS_TEGRA186_REGS_BASE 0x8800
226struct eqos_tegra186_regs {
227 uint32_t sdmemcomppadctrl; /* 0x8800 */
228 uint32_t auto_cal_config; /* 0x8804 */
229 uint32_t unused_8808; /* 0x8808 */
230 uint32_t auto_cal_status; /* 0x880c */
231};
232
233#define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
234
235#define EQOS_AUTO_CAL_CONFIG_START BIT(31)
236#define EQOS_AUTO_CAL_CONFIG_ENABLE BIT(29)
237
238#define EQOS_AUTO_CAL_STATUS_ACTIVE BIT(31)
239
240/* Descriptors */
Stephen Warren50709602016-10-21 14:46:47 -0600241#define EQOS_DESCRIPTORS_TX 4
242#define EQOS_DESCRIPTORS_RX 4
243#define EQOS_DESCRIPTORS_NUM (EQOS_DESCRIPTORS_TX + EQOS_DESCRIPTORS_RX)
Stephen Warren50709602016-10-21 14:46:47 -0600244#define EQOS_BUFFER_ALIGN ARCH_DMA_MINALIGN
245#define EQOS_MAX_PACKET_SIZE ALIGN(1568, ARCH_DMA_MINALIGN)
246#define EQOS_RX_BUFFER_SIZE (EQOS_DESCRIPTORS_RX * EQOS_MAX_PACKET_SIZE)
247
Stephen Warren50709602016-10-21 14:46:47 -0600248struct eqos_desc {
249 u32 des0;
250 u32 des1;
251 u32 des2;
252 u32 des3;
253};
254
255#define EQOS_DESC3_OWN BIT(31)
256#define EQOS_DESC3_FD BIT(29)
257#define EQOS_DESC3_LD BIT(28)
258#define EQOS_DESC3_BUF1V BIT(24)
259
Marek Vasut89077732021-01-07 11:12:16 +0100260#define EQOS_AXI_WIDTH_32 4
261#define EQOS_AXI_WIDTH_64 8
262#define EQOS_AXI_WIDTH_128 16
263
Stephen Warren50709602016-10-21 14:46:47 -0600264struct eqos_config {
265 bool reg_access_always_ok;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200266 int mdio_wait;
267 int swr_wait;
268 int config_mac;
269 int config_mac_mdio;
Marek Vasut89077732021-01-07 11:12:16 +0100270 unsigned int axi_bus_width;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200271 phy_interface_t (*interface)(struct udevice *dev);
272 struct eqos_ops *ops;
273};
274
275struct eqos_ops {
276 void (*eqos_inval_desc)(void *desc);
277 void (*eqos_flush_desc)(void *desc);
278 void (*eqos_inval_buffer)(void *buf, size_t size);
279 void (*eqos_flush_buffer)(void *buf, size_t size);
280 int (*eqos_probe_resources)(struct udevice *dev);
281 int (*eqos_remove_resources)(struct udevice *dev);
282 int (*eqos_stop_resets)(struct udevice *dev);
283 int (*eqos_start_resets)(struct udevice *dev);
284 void (*eqos_stop_clks)(struct udevice *dev);
285 int (*eqos_start_clks)(struct udevice *dev);
286 int (*eqos_calibrate_pads)(struct udevice *dev);
287 int (*eqos_disable_calibration)(struct udevice *dev);
288 int (*eqos_set_tx_clk_speed)(struct udevice *dev);
289 ulong (*eqos_get_tick_clk_rate)(struct udevice *dev);
Stephen Warren50709602016-10-21 14:46:47 -0600290};
291
292struct eqos_priv {
293 struct udevice *dev;
294 const struct eqos_config *config;
295 fdt_addr_t regs;
296 struct eqos_mac_regs *mac_regs;
297 struct eqos_mtl_regs *mtl_regs;
298 struct eqos_dma_regs *dma_regs;
299 struct eqos_tegra186_regs *tegra186_regs;
300 struct reset_ctl reset_ctl;
301 struct gpio_desc phy_reset_gpio;
302 struct clk clk_master_bus;
303 struct clk clk_rx;
304 struct clk clk_ptp_ref;
305 struct clk clk_tx;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200306 struct clk clk_ck;
Stephen Warren50709602016-10-21 14:46:47 -0600307 struct clk clk_slave_bus;
308 struct mii_dev *mii;
309 struct phy_device *phy;
Patrick Delaunay5c8db372020-03-18 10:50:16 +0100310 int phyaddr;
311 u32 max_speed;
Stephen Warren50709602016-10-21 14:46:47 -0600312 void *descs;
Stephen Warren50709602016-10-21 14:46:47 -0600313 int tx_desc_idx, rx_desc_idx;
Marek Vasut89077732021-01-07 11:12:16 +0100314 unsigned int desc_size;
Stephen Warren50709602016-10-21 14:46:47 -0600315 void *tx_dma_buf;
316 void *rx_dma_buf;
317 void *rx_pkt;
318 bool started;
319 bool reg_access_ok;
Daniil Stas81597922021-05-23 22:24:48 +0000320 bool clk_ck_enabled;
Stephen Warren50709602016-10-21 14:46:47 -0600321};
322
323/*
324 * TX and RX descriptors are 16 bytes. This causes problems with the cache
325 * maintenance on CPUs where the cache-line size exceeds the size of these
326 * descriptors. What will happen is that when the driver receives a packet
327 * it will be immediately requeued for the hardware to reuse. The CPU will
328 * therefore need to flush the cache-line containing the descriptor, which
329 * will cause all other descriptors in the same cache-line to be flushed
330 * along with it. If one of those descriptors had been written to by the
331 * device those changes (and the associated packet) will be lost.
332 *
333 * To work around this, we make use of non-cached memory if available. If
334 * descriptors are mapped uncached there's no need to manually flush them
335 * or invalidate them.
336 *
337 * Note that this only applies to descriptors. The packet data buffers do
338 * not have the same constraints since they are 1536 bytes large, so they
339 * are unlikely to share cache-lines.
340 */
Marek Vasut89077732021-01-07 11:12:16 +0100341static void *eqos_alloc_descs(struct eqos_priv *eqos, unsigned int num)
Stephen Warren50709602016-10-21 14:46:47 -0600342{
Marek Vasut89077732021-01-07 11:12:16 +0100343 eqos->desc_size = ALIGN(sizeof(struct eqos_desc),
344 (unsigned int)ARCH_DMA_MINALIGN);
345
346 return memalign(eqos->desc_size, num * eqos->desc_size);
Stephen Warren50709602016-10-21 14:46:47 -0600347}
348
349static void eqos_free_descs(void *descs)
350{
Stephen Warren50709602016-10-21 14:46:47 -0600351 free(descs);
Stephen Warren50709602016-10-21 14:46:47 -0600352}
353
Marek Vasut89077732021-01-07 11:12:16 +0100354static struct eqos_desc *eqos_get_desc(struct eqos_priv *eqos,
355 unsigned int num, bool rx)
Stephen Warren50709602016-10-21 14:46:47 -0600356{
Marek Vasut89077732021-01-07 11:12:16 +0100357 return eqos->descs +
358 ((rx ? EQOS_DESCRIPTORS_TX : 0) + num) * eqos->desc_size;
Stephen Warren50709602016-10-21 14:46:47 -0600359}
360
Fugang Duan37aae5f2020-05-03 22:41:17 +0800361static void eqos_inval_desc_generic(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -0600362{
Marek Vasut89077732021-01-07 11:12:16 +0100363 unsigned long start = (unsigned long)desc;
364 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
365 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200366
367 invalidate_dcache_range(start, end);
Stephen Warren50709602016-10-21 14:46:47 -0600368}
369
Fugang Duan37aae5f2020-05-03 22:41:17 +0800370static void eqos_flush_desc_generic(void *desc)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200371{
Marek Vasut89077732021-01-07 11:12:16 +0100372 unsigned long start = (unsigned long)desc;
373 unsigned long end = ALIGN(start + sizeof(struct eqos_desc),
374 ARCH_DMA_MINALIGN);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200375
376 flush_dcache_range(start, end);
Christophe Roullier6beb7802019-05-17 15:08:44 +0200377}
378
379static void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600380{
381 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
382 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
383
384 invalidate_dcache_range(start, end);
385}
386
Fugang Duan37aae5f2020-05-03 22:41:17 +0800387static void eqos_inval_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200388{
389 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
390 unsigned long end = roundup((unsigned long)buf + size,
391 ARCH_DMA_MINALIGN);
392
393 invalidate_dcache_range(start, end);
394}
395
396static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600397{
398 flush_cache((unsigned long)buf, size);
399}
400
Fugang Duan37aae5f2020-05-03 22:41:17 +0800401static void eqos_flush_buffer_generic(void *buf, size_t size)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200402{
403 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
404 unsigned long end = roundup((unsigned long)buf + size,
405 ARCH_DMA_MINALIGN);
406
407 flush_dcache_range(start, end);
408}
409
Stephen Warren50709602016-10-21 14:46:47 -0600410static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
411{
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100412 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
413 EQOS_MAC_MDIO_ADDRESS_GB, false,
414 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600415}
416
417static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
418 int mdio_reg)
419{
420 struct eqos_priv *eqos = bus->priv;
421 u32 val;
422 int ret;
423
424 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
425 mdio_reg);
426
427 ret = eqos_mdio_wait_idle(eqos);
428 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900429 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600430 return ret;
431 }
432
433 val = readl(&eqos->mac_regs->mdio_address);
434 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
435 EQOS_MAC_MDIO_ADDRESS_C45E;
436 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
437 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200438 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600439 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
440 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
441 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
442 EQOS_MAC_MDIO_ADDRESS_GB;
443 writel(val, &eqos->mac_regs->mdio_address);
444
Christophe Roullier6beb7802019-05-17 15:08:44 +0200445 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600446
447 ret = eqos_mdio_wait_idle(eqos);
448 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900449 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600450 return ret;
451 }
452
453 val = readl(&eqos->mac_regs->mdio_data);
454 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
455
456 debug("%s: val=%x\n", __func__, val);
457
458 return val;
459}
460
461static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
462 int mdio_reg, u16 mdio_val)
463{
464 struct eqos_priv *eqos = bus->priv;
465 u32 val;
466 int ret;
467
468 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
469 mdio_addr, mdio_reg, mdio_val);
470
471 ret = eqos_mdio_wait_idle(eqos);
472 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900473 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600474 return ret;
475 }
476
477 writel(mdio_val, &eqos->mac_regs->mdio_data);
478
479 val = readl(&eqos->mac_regs->mdio_address);
480 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
481 EQOS_MAC_MDIO_ADDRESS_C45E;
482 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
483 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200484 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600485 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
486 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
487 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
488 EQOS_MAC_MDIO_ADDRESS_GB;
489 writel(val, &eqos->mac_regs->mdio_address);
490
Christophe Roullier6beb7802019-05-17 15:08:44 +0200491 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600492
493 ret = eqos_mdio_wait_idle(eqos);
494 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900495 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600496 return ret;
497 }
498
499 return 0;
500}
501
502static int eqos_start_clks_tegra186(struct udevice *dev)
503{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800504#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600505 struct eqos_priv *eqos = dev_get_priv(dev);
506 int ret;
507
508 debug("%s(dev=%p):\n", __func__, dev);
509
510 ret = clk_enable(&eqos->clk_slave_bus);
511 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900512 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600513 goto err;
514 }
515
516 ret = clk_enable(&eqos->clk_master_bus);
517 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900518 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600519 goto err_disable_clk_slave_bus;
520 }
521
522 ret = clk_enable(&eqos->clk_rx);
523 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900524 pr_err("clk_enable(clk_rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600525 goto err_disable_clk_master_bus;
526 }
527
528 ret = clk_enable(&eqos->clk_ptp_ref);
529 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900530 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600531 goto err_disable_clk_rx;
532 }
533
534 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
535 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900536 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600537 goto err_disable_clk_ptp_ref;
538 }
539
540 ret = clk_enable(&eqos->clk_tx);
541 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900542 pr_err("clk_enable(clk_tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600543 goto err_disable_clk_ptp_ref;
544 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800545#endif
Stephen Warren50709602016-10-21 14:46:47 -0600546
547 debug("%s: OK\n", __func__);
548 return 0;
549
Fugang Duan37aae5f2020-05-03 22:41:17 +0800550#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600551err_disable_clk_ptp_ref:
552 clk_disable(&eqos->clk_ptp_ref);
553err_disable_clk_rx:
554 clk_disable(&eqos->clk_rx);
555err_disable_clk_master_bus:
556 clk_disable(&eqos->clk_master_bus);
557err_disable_clk_slave_bus:
558 clk_disable(&eqos->clk_slave_bus);
559err:
560 debug("%s: FAILED: %d\n", __func__, ret);
561 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800562#endif
Stephen Warren50709602016-10-21 14:46:47 -0600563}
564
Christophe Roullier6beb7802019-05-17 15:08:44 +0200565static int eqos_start_clks_stm32(struct udevice *dev)
566{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800567#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200568 struct eqos_priv *eqos = dev_get_priv(dev);
569 int ret;
570
571 debug("%s(dev=%p):\n", __func__, dev);
572
573 ret = clk_enable(&eqos->clk_master_bus);
574 if (ret < 0) {
575 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
576 goto err;
577 }
578
579 ret = clk_enable(&eqos->clk_rx);
580 if (ret < 0) {
581 pr_err("clk_enable(clk_rx) failed: %d", ret);
582 goto err_disable_clk_master_bus;
583 }
584
585 ret = clk_enable(&eqos->clk_tx);
586 if (ret < 0) {
587 pr_err("clk_enable(clk_tx) failed: %d", ret);
588 goto err_disable_clk_rx;
589 }
590
Daniil Stas81597922021-05-23 22:24:48 +0000591 if (clk_valid(&eqos->clk_ck) && !eqos->clk_ck_enabled) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200592 ret = clk_enable(&eqos->clk_ck);
593 if (ret < 0) {
594 pr_err("clk_enable(clk_ck) failed: %d", ret);
595 goto err_disable_clk_tx;
596 }
Daniil Stas81597922021-05-23 22:24:48 +0000597 eqos->clk_ck_enabled = true;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200598 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800599#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200600
601 debug("%s: OK\n", __func__);
602 return 0;
603
Fugang Duan37aae5f2020-05-03 22:41:17 +0800604#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200605err_disable_clk_tx:
606 clk_disable(&eqos->clk_tx);
607err_disable_clk_rx:
608 clk_disable(&eqos->clk_rx);
609err_disable_clk_master_bus:
610 clk_disable(&eqos->clk_master_bus);
611err:
612 debug("%s: FAILED: %d\n", __func__, ret);
613 return ret;
Fugang Duan37aae5f2020-05-03 22:41:17 +0800614#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200615}
616
Fugang Duan37aae5f2020-05-03 22:41:17 +0800617static int eqos_start_clks_imx(struct udevice *dev)
618{
619 return 0;
620}
621
Patrick Delaunay6864a5992019-08-01 11:29:02 +0200622static void eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600623{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800624#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600625 struct eqos_priv *eqos = dev_get_priv(dev);
626
627 debug("%s(dev=%p):\n", __func__, dev);
628
629 clk_disable(&eqos->clk_tx);
630 clk_disable(&eqos->clk_ptp_ref);
631 clk_disable(&eqos->clk_rx);
632 clk_disable(&eqos->clk_master_bus);
633 clk_disable(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800634#endif
Stephen Warren50709602016-10-21 14:46:47 -0600635
636 debug("%s: OK\n", __func__);
637}
638
Patrick Delaunay6864a5992019-08-01 11:29:02 +0200639static void eqos_stop_clks_stm32(struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200640{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800641#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200642 struct eqos_priv *eqos = dev_get_priv(dev);
643
644 debug("%s(dev=%p):\n", __func__, dev);
645
646 clk_disable(&eqos->clk_tx);
647 clk_disable(&eqos->clk_rx);
648 clk_disable(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800649#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +0200650
651 debug("%s: OK\n", __func__);
652}
653
Fugang Duan37aae5f2020-05-03 22:41:17 +0800654static void eqos_stop_clks_imx(struct udevice *dev)
655{
656 /* empty */
657}
658
Stephen Warren50709602016-10-21 14:46:47 -0600659static int eqos_start_resets_tegra186(struct udevice *dev)
660{
661 struct eqos_priv *eqos = dev_get_priv(dev);
662 int ret;
663
664 debug("%s(dev=%p):\n", __func__, dev);
665
666 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
667 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900668 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600669 return ret;
670 }
671
672 udelay(2);
673
674 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
675 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900676 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600677 return ret;
678 }
679
680 ret = reset_assert(&eqos->reset_ctl);
681 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900682 pr_err("reset_assert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600683 return ret;
684 }
685
686 udelay(2);
687
688 ret = reset_deassert(&eqos->reset_ctl);
689 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900690 pr_err("reset_deassert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600691 return ret;
692 }
693
694 debug("%s: OK\n", __func__);
695 return 0;
696}
697
Christophe Roullier6beb7802019-05-17 15:08:44 +0200698static int eqos_start_resets_stm32(struct udevice *dev)
699{
Christophe Roullier104dab52020-03-18 10:50:15 +0100700 struct eqos_priv *eqos = dev_get_priv(dev);
701 int ret;
702
703 debug("%s(dev=%p):\n", __func__, dev);
704 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
705 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
706 if (ret < 0) {
707 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d",
708 ret);
709 return ret;
710 }
711
712 udelay(2);
713
714 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
715 if (ret < 0) {
716 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d",
717 ret);
718 return ret;
719 }
720 }
721 debug("%s: OK\n", __func__);
722
Fugang Duan37aae5f2020-05-03 22:41:17 +0800723 return 0;
724}
725
726static int eqos_start_resets_imx(struct udevice *dev)
727{
Christophe Roullier6beb7802019-05-17 15:08:44 +0200728 return 0;
729}
730
Stephen Warren50709602016-10-21 14:46:47 -0600731static int eqos_stop_resets_tegra186(struct udevice *dev)
732{
733 struct eqos_priv *eqos = dev_get_priv(dev);
734
735 reset_assert(&eqos->reset_ctl);
736 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
737
Christophe Roullier6beb7802019-05-17 15:08:44 +0200738 return 0;
739}
740
741static int eqos_stop_resets_stm32(struct udevice *dev)
742{
Christophe Roullier104dab52020-03-18 10:50:15 +0100743 struct eqos_priv *eqos = dev_get_priv(dev);
744 int ret;
745
746 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
747 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
748 if (ret < 0) {
749 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d",
750 ret);
751 return ret;
752 }
753 }
754
Fugang Duan37aae5f2020-05-03 22:41:17 +0800755 return 0;
756}
757
758static int eqos_stop_resets_imx(struct udevice *dev)
759{
Stephen Warren50709602016-10-21 14:46:47 -0600760 return 0;
761}
762
763static int eqos_calibrate_pads_tegra186(struct udevice *dev)
764{
765 struct eqos_priv *eqos = dev_get_priv(dev);
766 int ret;
767
768 debug("%s(dev=%p):\n", __func__, dev);
769
770 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
771 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
772
773 udelay(1);
774
775 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
776 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
777
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100778 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
779 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600780 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900781 pr_err("calibrate didn't start");
Stephen Warren50709602016-10-21 14:46:47 -0600782 goto failed;
783 }
784
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100785 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
786 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600787 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900788 pr_err("calibrate didn't finish");
Stephen Warren50709602016-10-21 14:46:47 -0600789 goto failed;
790 }
791
792 ret = 0;
793
794failed:
795 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
796 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
797
798 debug("%s: returns %d\n", __func__, ret);
799
800 return ret;
801}
802
803static int eqos_disable_calibration_tegra186(struct udevice *dev)
804{
805 struct eqos_priv *eqos = dev_get_priv(dev);
806
807 debug("%s(dev=%p):\n", __func__, dev);
808
809 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
810 EQOS_AUTO_CAL_CONFIG_ENABLE);
811
812 return 0;
813}
814
815static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
816{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800817#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600818 struct eqos_priv *eqos = dev_get_priv(dev);
819
820 return clk_get_rate(&eqos->clk_slave_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800821#else
822 return 0;
823#endif
Stephen Warren50709602016-10-21 14:46:47 -0600824}
825
Christophe Roullier6beb7802019-05-17 15:08:44 +0200826static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
827{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800828#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +0200829 struct eqos_priv *eqos = dev_get_priv(dev);
830
831 return clk_get_rate(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +0800832#else
833 return 0;
834#endif
835}
836
Fugang Duandd455e62020-05-03 22:41:18 +0800837__weak u32 imx_get_eqos_csr_clk(void)
Fugang Duan37aae5f2020-05-03 22:41:17 +0800838{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800839 return 100 * 1000000;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200840}
Fugang Duandd455e62020-05-03 22:41:18 +0800841__weak int imx_eqos_txclk_set_rate(unsigned long rate)
842{
843 return 0;
844}
845
846static ulong eqos_get_tick_clk_rate_imx(struct udevice *dev)
847{
848 return imx_get_eqos_csr_clk();
849}
Christophe Roullier6beb7802019-05-17 15:08:44 +0200850
851static int eqos_calibrate_pads_stm32(struct udevice *dev)
852{
853 return 0;
854}
855
Fugang Duan37aae5f2020-05-03 22:41:17 +0800856static int eqos_calibrate_pads_imx(struct udevice *dev)
857{
858 return 0;
859}
860
Christophe Roullier6beb7802019-05-17 15:08:44 +0200861static int eqos_disable_calibration_stm32(struct udevice *dev)
862{
863 return 0;
864}
865
Fugang Duan37aae5f2020-05-03 22:41:17 +0800866static int eqos_disable_calibration_imx(struct udevice *dev)
867{
868 return 0;
869}
870
Stephen Warren50709602016-10-21 14:46:47 -0600871static int eqos_set_full_duplex(struct udevice *dev)
872{
873 struct eqos_priv *eqos = dev_get_priv(dev);
874
875 debug("%s(dev=%p):\n", __func__, dev);
876
877 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
878
879 return 0;
880}
881
882static int eqos_set_half_duplex(struct udevice *dev)
883{
884 struct eqos_priv *eqos = dev_get_priv(dev);
885
886 debug("%s(dev=%p):\n", __func__, dev);
887
888 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
889
890 /* WAR: Flush TX queue when switching to half-duplex */
891 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
892 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
893
894 return 0;
895}
896
897static int eqos_set_gmii_speed(struct udevice *dev)
898{
899 struct eqos_priv *eqos = dev_get_priv(dev);
900
901 debug("%s(dev=%p):\n", __func__, dev);
902
903 clrbits_le32(&eqos->mac_regs->configuration,
904 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
905
906 return 0;
907}
908
909static int eqos_set_mii_speed_100(struct udevice *dev)
910{
911 struct eqos_priv *eqos = dev_get_priv(dev);
912
913 debug("%s(dev=%p):\n", __func__, dev);
914
915 setbits_le32(&eqos->mac_regs->configuration,
916 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
917
918 return 0;
919}
920
921static int eqos_set_mii_speed_10(struct udevice *dev)
922{
923 struct eqos_priv *eqos = dev_get_priv(dev);
924
925 debug("%s(dev=%p):\n", __func__, dev);
926
927 clrsetbits_le32(&eqos->mac_regs->configuration,
928 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
929
930 return 0;
931}
932
933static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
934{
Fugang Duan37aae5f2020-05-03 22:41:17 +0800935#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -0600936 struct eqos_priv *eqos = dev_get_priv(dev);
937 ulong rate;
938 int ret;
939
940 debug("%s(dev=%p):\n", __func__, dev);
941
942 switch (eqos->phy->speed) {
943 case SPEED_1000:
944 rate = 125 * 1000 * 1000;
945 break;
946 case SPEED_100:
947 rate = 25 * 1000 * 1000;
948 break;
949 case SPEED_10:
950 rate = 2.5 * 1000 * 1000;
951 break;
952 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900953 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600954 return -EINVAL;
955 }
956
957 ret = clk_set_rate(&eqos->clk_tx, rate);
958 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900959 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600960 return ret;
961 }
Fugang Duan37aae5f2020-05-03 22:41:17 +0800962#endif
Stephen Warren50709602016-10-21 14:46:47 -0600963
964 return 0;
965}
966
Christophe Roullier6beb7802019-05-17 15:08:44 +0200967static int eqos_set_tx_clk_speed_stm32(struct udevice *dev)
968{
969 return 0;
970}
971
Fugang Duan37aae5f2020-05-03 22:41:17 +0800972static int eqos_set_tx_clk_speed_imx(struct udevice *dev)
973{
Fugang Duandd455e62020-05-03 22:41:18 +0800974 struct eqos_priv *eqos = dev_get_priv(dev);
975 ulong rate;
976 int ret;
977
978 debug("%s(dev=%p):\n", __func__, dev);
979
980 switch (eqos->phy->speed) {
981 case SPEED_1000:
982 rate = 125 * 1000 * 1000;
983 break;
984 case SPEED_100:
985 rate = 25 * 1000 * 1000;
986 break;
987 case SPEED_10:
988 rate = 2.5 * 1000 * 1000;
989 break;
990 default:
991 pr_err("invalid speed %d", eqos->phy->speed);
992 return -EINVAL;
993 }
994
995 ret = imx_eqos_txclk_set_rate(rate);
996 if (ret < 0) {
997 pr_err("imx (tx_clk, %lu) failed: %d", rate, ret);
998 return ret;
999 }
1000
Fugang Duan37aae5f2020-05-03 22:41:17 +08001001 return 0;
1002}
1003
Stephen Warren50709602016-10-21 14:46:47 -06001004static int eqos_adjust_link(struct udevice *dev)
1005{
1006 struct eqos_priv *eqos = dev_get_priv(dev);
1007 int ret;
1008 bool en_calibration;
1009
1010 debug("%s(dev=%p):\n", __func__, dev);
1011
1012 if (eqos->phy->duplex)
1013 ret = eqos_set_full_duplex(dev);
1014 else
1015 ret = eqos_set_half_duplex(dev);
1016 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001017 pr_err("eqos_set_*_duplex() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001018 return ret;
1019 }
1020
1021 switch (eqos->phy->speed) {
1022 case SPEED_1000:
1023 en_calibration = true;
1024 ret = eqos_set_gmii_speed(dev);
1025 break;
1026 case SPEED_100:
1027 en_calibration = true;
1028 ret = eqos_set_mii_speed_100(dev);
1029 break;
1030 case SPEED_10:
1031 en_calibration = false;
1032 ret = eqos_set_mii_speed_10(dev);
1033 break;
1034 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001035 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -06001036 return -EINVAL;
1037 }
1038 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001039 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001040 return ret;
1041 }
1042
1043 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001044 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001045 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001046 pr_err("eqos_calibrate_pads() failed: %d",
1047 ret);
Stephen Warren50709602016-10-21 14:46:47 -06001048 return ret;
1049 }
1050 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001051 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001052 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001053 pr_err("eqos_disable_calibration() failed: %d",
1054 ret);
Stephen Warren50709602016-10-21 14:46:47 -06001055 return ret;
1056 }
1057 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001058 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001059 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001060 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001061 return ret;
1062 }
1063
1064 return 0;
1065}
1066
1067static int eqos_write_hwaddr(struct udevice *dev)
1068{
Simon Glassfa20e932020-12-03 16:55:20 -07001069 struct eth_pdata *plat = dev_get_plat(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001070 struct eqos_priv *eqos = dev_get_priv(dev);
1071 uint32_t val;
1072
1073 /*
1074 * This function may be called before start() or after stop(). At that
1075 * time, on at least some configurations of the EQoS HW, all clocks to
1076 * the EQoS HW block will be stopped, and a reset signal applied. If
1077 * any register access is attempted in this state, bus timeouts or CPU
1078 * hangs may occur. This check prevents that.
1079 *
1080 * A simple solution to this problem would be to not implement
1081 * write_hwaddr(), since start() always writes the MAC address into HW
1082 * anyway. However, it is desirable to implement write_hwaddr() to
1083 * support the case of SW that runs subsequent to U-Boot which expects
1084 * the MAC address to already be programmed into the EQoS registers,
1085 * which must happen irrespective of whether the U-Boot user (or
1086 * scripts) actually made use of the EQoS device, and hence
1087 * irrespective of whether start() was ever called.
1088 *
1089 * Note that this requirement by subsequent SW is not valid for
1090 * Tegra186, and is likely not valid for any non-PCI instantiation of
1091 * the EQoS HW block. This function is implemented solely as
1092 * future-proofing with the expectation the driver will eventually be
1093 * ported to some system where the expectation above is true.
1094 */
1095 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
1096 return 0;
1097
1098 /* Update the MAC address */
1099 val = (plat->enetaddr[5] << 8) |
1100 (plat->enetaddr[4]);
1101 writel(val, &eqos->mac_regs->address0_high);
1102 val = (plat->enetaddr[3] << 24) |
1103 (plat->enetaddr[2] << 16) |
1104 (plat->enetaddr[1] << 8) |
1105 (plat->enetaddr[0]);
1106 writel(val, &eqos->mac_regs->address0_low);
1107
1108 return 0;
1109}
1110
Ye Li3fb1a0e2020-05-03 22:41:20 +08001111static int eqos_read_rom_hwaddr(struct udevice *dev)
1112{
Simon Glassfa20e932020-12-03 16:55:20 -07001113 struct eth_pdata *pdata = dev_get_plat(dev);
Ye Li3fb1a0e2020-05-03 22:41:20 +08001114
1115#ifdef CONFIG_ARCH_IMX8M
Simon Glass3e14a222020-12-16 21:20:16 -07001116 imx_get_mac_from_fuse(dev_seq(dev), pdata->enetaddr);
Ye Li3fb1a0e2020-05-03 22:41:20 +08001117#endif
1118 return !is_valid_ethaddr(pdata->enetaddr);
1119}
1120
Stephen Warren50709602016-10-21 14:46:47 -06001121static int eqos_start(struct udevice *dev)
1122{
1123 struct eqos_priv *eqos = dev_get_priv(dev);
1124 int ret, i;
1125 ulong rate;
1126 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
1127 ulong last_rx_desc;
Marek Vasut89077732021-01-07 11:12:16 +01001128 ulong desc_pad;
Stephen Warren50709602016-10-21 14:46:47 -06001129
1130 debug("%s(dev=%p):\n", __func__, dev);
1131
1132 eqos->tx_desc_idx = 0;
1133 eqos->rx_desc_idx = 0;
1134
Christophe Roullier6beb7802019-05-17 15:08:44 +02001135 ret = eqos->config->ops->eqos_start_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001136 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001137 pr_err("eqos_start_clks() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001138 goto err;
1139 }
1140
Christophe Roullier6beb7802019-05-17 15:08:44 +02001141 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001142 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001143 pr_err("eqos_start_resets() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001144 goto err_stop_clks;
1145 }
1146
1147 udelay(10);
1148
1149 eqos->reg_access_ok = true;
1150
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +01001151 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001152 EQOS_DMA_MODE_SWR, false,
1153 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -06001154 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001155 pr_err("EQOS_DMA_MODE_SWR stuck");
Stephen Warren50709602016-10-21 14:46:47 -06001156 goto err_stop_resets;
1157 }
1158
Christophe Roullier6beb7802019-05-17 15:08:44 +02001159 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001160 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001161 pr_err("eqos_calibrate_pads() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001162 goto err_stop_resets;
1163 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001164 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001165
Stephen Warren50709602016-10-21 14:46:47 -06001166 val = (rate / 1000000) - 1;
1167 writel(val, &eqos->mac_regs->us_tic_counter);
1168
Christophe Roullier6beb7802019-05-17 15:08:44 +02001169 /*
1170 * if PHY was already connected and configured,
1171 * don't need to reconnect/reconfigure again
1172 */
Stephen Warren50709602016-10-21 14:46:47 -06001173 if (!eqos->phy) {
Ye Liad122b72020-05-03 22:41:15 +08001174 int addr = -1;
1175#ifdef CONFIG_DM_ETH_PHY
1176 addr = eth_phy_get_addr(dev);
1177#endif
1178#ifdef DWC_NET_PHYADDR
1179 addr = DWC_NET_PHYADDR;
1180#endif
1181 eqos->phy = phy_connect(eqos->mii, addr, dev,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001182 eqos->config->interface(dev));
1183 if (!eqos->phy) {
1184 pr_err("phy_connect() failed");
1185 goto err_stop_resets;
1186 }
Patrick Delaunay5c8db372020-03-18 10:50:16 +01001187
1188 if (eqos->max_speed) {
1189 ret = phy_set_supported(eqos->phy, eqos->max_speed);
1190 if (ret) {
1191 pr_err("phy_set_supported() failed: %d", ret);
1192 goto err_shutdown_phy;
1193 }
1194 }
1195
Christophe Roullier6beb7802019-05-17 15:08:44 +02001196 ret = phy_config(eqos->phy);
1197 if (ret < 0) {
1198 pr_err("phy_config() failed: %d", ret);
1199 goto err_shutdown_phy;
1200 }
Stephen Warren50709602016-10-21 14:46:47 -06001201 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001202
Stephen Warren50709602016-10-21 14:46:47 -06001203 ret = phy_startup(eqos->phy);
1204 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001205 pr_err("phy_startup() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001206 goto err_shutdown_phy;
1207 }
1208
1209 if (!eqos->phy->link) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001210 pr_err("No link");
Stephen Warren50709602016-10-21 14:46:47 -06001211 goto err_shutdown_phy;
1212 }
1213
1214 ret = eqos_adjust_link(dev);
1215 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001216 pr_err("eqos_adjust_link() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001217 goto err_shutdown_phy;
1218 }
1219
1220 /* Configure MTL */
1221
1222 /* Enable Store and Forward mode for TX */
1223 /* Program Tx operating mode */
1224 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1225 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
1226 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
1227 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
1228
1229 /* Transmit Queue weight */
1230 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
1231
1232 /* Enable Store and Forward mode for RX, since no jumbo frame */
1233 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
Daniil Stas470c06c2021-05-30 13:34:09 +00001234 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
Stephen Warren50709602016-10-21 14:46:47 -06001235
1236 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
1237 val = readl(&eqos->mac_regs->hw_feature1);
1238 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
1239 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
1240 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
1241 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
1242
1243 /*
1244 * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
1245 * r/tqs is encoded as (n / 256) - 1.
1246 */
1247 tqs = (128 << tx_fifo_sz) / 256 - 1;
1248 rqs = (128 << rx_fifo_sz) / 256 - 1;
1249
1250 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1251 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
1252 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
1253 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
1254 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1255 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
1256 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
1257 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
1258
1259 /* Flow control used only if each channel gets 4KB or more FIFO */
1260 if (rqs >= ((4096 / 256) - 1)) {
1261 u32 rfd, rfa;
1262
1263 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1264 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
1265
1266 /*
1267 * Set Threshold for Activating Flow Contol space for min 2
1268 * frames ie, (1500 * 1) = 1500 bytes.
1269 *
1270 * Set Threshold for Deactivating Flow Contol for space of
1271 * min 1 frame (frame size 1500bytes) in receive fifo
1272 */
1273 if (rqs == ((4096 / 256) - 1)) {
1274 /*
1275 * This violates the above formula because of FIFO size
1276 * limit therefore overflow may occur inspite of this.
1277 */
1278 rfd = 0x3; /* Full-3K */
1279 rfa = 0x1; /* Full-1.5K */
1280 } else if (rqs == ((8192 / 256) - 1)) {
1281 rfd = 0x6; /* Full-4K */
1282 rfa = 0xa; /* Full-6K */
1283 } else if (rqs == ((16384 / 256) - 1)) {
1284 rfd = 0x6; /* Full-4K */
1285 rfa = 0x12; /* Full-10K */
1286 } else {
1287 rfd = 0x6; /* Full-4K */
1288 rfa = 0x1E; /* Full-16K */
1289 }
1290
1291 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1292 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
1293 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1294 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
1295 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
1296 (rfd <<
1297 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1298 (rfa <<
1299 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
1300 }
1301
1302 /* Configure MAC */
1303
1304 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
1305 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
1306 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001307 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -06001308 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
1309
Fugang Duan37aae5f2020-05-03 22:41:17 +08001310 /* Multicast and Broadcast Queue Enable */
1311 setbits_le32(&eqos->mac_regs->unused_0a4,
1312 0x00100000);
1313 /* enable promise mode */
1314 setbits_le32(&eqos->mac_regs->unused_004[1],
1315 0x1);
1316
Stephen Warren50709602016-10-21 14:46:47 -06001317 /* Set TX flow control parameters */
1318 /* Set Pause Time */
1319 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1320 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
1321 /* Assign priority for TX flow control */
1322 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
1323 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
1324 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
1325 /* Assign priority for RX flow control */
1326 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
1327 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
1328 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
1329 /* Enable flow control */
1330 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1331 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
1332 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
1333 EQOS_MAC_RX_FLOW_CTRL_RFE);
1334
1335 clrsetbits_le32(&eqos->mac_regs->configuration,
1336 EQOS_MAC_CONFIGURATION_GPSLCE |
1337 EQOS_MAC_CONFIGURATION_WD |
1338 EQOS_MAC_CONFIGURATION_JD |
1339 EQOS_MAC_CONFIGURATION_JE,
1340 EQOS_MAC_CONFIGURATION_CST |
1341 EQOS_MAC_CONFIGURATION_ACS);
1342
1343 eqos_write_hwaddr(dev);
1344
1345 /* Configure DMA */
1346
1347 /* Enable OSP mode */
1348 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1349 EQOS_DMA_CH0_TX_CONTROL_OSP);
1350
1351 /* RX buffer size. Must be a multiple of bus width */
1352 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1353 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
1354 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
1355 EQOS_MAX_PACKET_SIZE <<
1356 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
1357
Marek Vasut89077732021-01-07 11:12:16 +01001358 desc_pad = (eqos->desc_size - sizeof(struct eqos_desc)) /
1359 eqos->config->axi_bus_width;
1360
Stephen Warren50709602016-10-21 14:46:47 -06001361 setbits_le32(&eqos->dma_regs->ch0_control,
Marek Vasut89077732021-01-07 11:12:16 +01001362 EQOS_DMA_CH0_CONTROL_PBLX8 |
1363 (desc_pad << EQOS_DMA_CH0_CONTROL_DSL_SHIFT));
Stephen Warren50709602016-10-21 14:46:47 -06001364
1365 /*
1366 * Burst length must be < 1/2 FIFO size.
1367 * FIFO size in tqs is encoded as (n / 256) - 1.
1368 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1369 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1370 */
1371 pbl = tqs + 1;
1372 if (pbl > 32)
1373 pbl = 32;
1374 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1375 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1376 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1377 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1378
1379 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1380 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1381 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1382 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1383
1384 /* DMA performance configuration */
1385 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1386 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1387 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1388 writel(val, &eqos->dma_regs->sysbus_mode);
1389
1390 /* Set up descriptors */
1391
Marek Vasut89077732021-01-07 11:12:16 +01001392 memset(eqos->descs, 0, eqos->desc_size * EQOS_DESCRIPTORS_NUM);
1393
1394 for (i = 0; i < EQOS_DESCRIPTORS_TX; i++) {
1395 struct eqos_desc *tx_desc = eqos_get_desc(eqos, i, false);
1396 eqos->config->ops->eqos_flush_desc(tx_desc);
1397 }
1398
Stephen Warren50709602016-10-21 14:46:47 -06001399 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
Marek Vasut89077732021-01-07 11:12:16 +01001400 struct eqos_desc *rx_desc = eqos_get_desc(eqos, i, true);
Stephen Warren50709602016-10-21 14:46:47 -06001401 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1402 (i * EQOS_MAX_PACKET_SIZE));
Marek Vasutd54c98e2020-03-23 02:02:57 +01001403 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Fugang Duan37aae5f2020-05-03 22:41:17 +08001404 mb();
Marek Vasut873f8e42020-03-23 02:09:01 +01001405 eqos->config->ops->eqos_flush_desc(rx_desc);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001406 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf +
1407 (i * EQOS_MAX_PACKET_SIZE),
1408 EQOS_MAX_PACKET_SIZE);
Stephen Warren50709602016-10-21 14:46:47 -06001409 }
Stephen Warren50709602016-10-21 14:46:47 -06001410
1411 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001412 writel((ulong)eqos_get_desc(eqos, 0, false),
1413 &eqos->dma_regs->ch0_txdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001414 writel(EQOS_DESCRIPTORS_TX - 1,
1415 &eqos->dma_regs->ch0_txdesc_ring_length);
1416
1417 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
Marek Vasut89077732021-01-07 11:12:16 +01001418 writel((ulong)eqos_get_desc(eqos, 0, true),
1419 &eqos->dma_regs->ch0_rxdesc_list_address);
Stephen Warren50709602016-10-21 14:46:47 -06001420 writel(EQOS_DESCRIPTORS_RX - 1,
1421 &eqos->dma_regs->ch0_rxdesc_ring_length);
1422
1423 /* Enable everything */
Stephen Warren50709602016-10-21 14:46:47 -06001424 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1425 EQOS_DMA_CH0_TX_CONTROL_ST);
1426 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1427 EQOS_DMA_CH0_RX_CONTROL_SR);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001428 setbits_le32(&eqos->mac_regs->configuration,
1429 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
Stephen Warren50709602016-10-21 14:46:47 -06001430
1431 /* TX tail pointer not written until we need to TX a packet */
1432 /*
1433 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1434 * first descriptor, implying all descriptors were available. However,
1435 * that's not distinguishable from none of the descriptors being
1436 * available.
1437 */
Marek Vasut89077732021-01-07 11:12:16 +01001438 last_rx_desc = (ulong)eqos_get_desc(eqos, EQOS_DESCRIPTORS_RX - 1, true);
Stephen Warren50709602016-10-21 14:46:47 -06001439 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1440
1441 eqos->started = true;
1442
1443 debug("%s: OK\n", __func__);
1444 return 0;
1445
1446err_shutdown_phy:
1447 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001448err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001449 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001450err_stop_clks:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001451 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001452err:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001453 pr_err("FAILED: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001454 return ret;
1455}
1456
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001457static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001458{
1459 struct eqos_priv *eqos = dev_get_priv(dev);
1460 int i;
1461
1462 debug("%s(dev=%p):\n", __func__, dev);
1463
1464 if (!eqos->started)
1465 return;
1466 eqos->started = false;
1467 eqos->reg_access_ok = false;
1468
1469 /* Disable TX DMA */
1470 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1471 EQOS_DMA_CH0_TX_CONTROL_ST);
1472
1473 /* Wait for TX all packets to drain out of MTL */
1474 for (i = 0; i < 1000000; i++) {
1475 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1476 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1477 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1478 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1479 if ((trcsts != 1) && (!txqsts))
1480 break;
1481 }
1482
1483 /* Turn off MAC TX and RX */
1484 clrbits_le32(&eqos->mac_regs->configuration,
1485 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1486
1487 /* Wait for all RX packets to drain out of MTL */
1488 for (i = 0; i < 1000000; i++) {
1489 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1490 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1491 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1492 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1493 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1494 if ((!prxq) && (!rxqsts))
1495 break;
1496 }
1497
1498 /* Turn off RX DMA */
1499 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1500 EQOS_DMA_CH0_RX_CONTROL_SR);
1501
1502 if (eqos->phy) {
1503 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001504 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001505 eqos->config->ops->eqos_stop_resets(dev);
1506 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001507
1508 debug("%s: OK\n", __func__);
1509}
1510
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001511static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001512{
1513 struct eqos_priv *eqos = dev_get_priv(dev);
1514 struct eqos_desc *tx_desc;
1515 int i;
1516
1517 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1518 length);
1519
1520 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001521 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001522
Marek Vasut89077732021-01-07 11:12:16 +01001523 tx_desc = eqos_get_desc(eqos, eqos->tx_desc_idx, false);
Stephen Warren50709602016-10-21 14:46:47 -06001524 eqos->tx_desc_idx++;
1525 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1526
1527 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1528 tx_desc->des1 = 0;
1529 tx_desc->des2 = length;
1530 /*
1531 * Make sure that if HW sees the _OWN write below, it will see all the
1532 * writes to the rest of the descriptor too.
1533 */
1534 mb();
1535 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001536 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001537
Marek Vasut89077732021-01-07 11:12:16 +01001538 writel((ulong)eqos_get_desc(eqos, eqos->tx_desc_idx, false),
Marek Vasutf4f1f4d2020-03-23 02:03:50 +01001539 &eqos->dma_regs->ch0_txdesc_tail_pointer);
Stephen Warren50709602016-10-21 14:46:47 -06001540
1541 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001542 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001543 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1544 return 0;
1545 udelay(1);
1546 }
1547
1548 debug("%s: TX timeout\n", __func__);
1549
1550 return -ETIMEDOUT;
1551}
1552
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001553static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001554{
1555 struct eqos_priv *eqos = dev_get_priv(dev);
1556 struct eqos_desc *rx_desc;
1557 int length;
1558
1559 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1560
Marek Vasut89077732021-01-07 11:12:16 +01001561 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasutc4db8442020-03-23 02:09:21 +01001562 eqos->config->ops->eqos_inval_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001563 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1564 debug("%s: RX packet not available\n", __func__);
1565 return -EAGAIN;
1566 }
1567
1568 *packetp = eqos->rx_dma_buf +
1569 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1570 length = rx_desc->des3 & 0x7fff;
1571 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1572
Christophe Roullier6beb7802019-05-17 15:08:44 +02001573 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001574
1575 return length;
1576}
1577
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001578static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001579{
1580 struct eqos_priv *eqos = dev_get_priv(dev);
1581 uchar *packet_expected;
1582 struct eqos_desc *rx_desc;
1583
1584 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1585
1586 packet_expected = eqos->rx_dma_buf +
1587 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1588 if (packet != packet_expected) {
1589 debug("%s: Unexpected packet (expected %p)\n", __func__,
1590 packet_expected);
1591 return -EINVAL;
1592 }
1593
Fugang Duan37aae5f2020-05-03 22:41:17 +08001594 eqos->config->ops->eqos_inval_buffer(packet, length);
1595
Marek Vasut89077732021-01-07 11:12:16 +01001596 rx_desc = eqos_get_desc(eqos, eqos->rx_desc_idx, true);
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001597
Marek Vasut091b6db2020-03-23 02:11:46 +01001598 rx_desc->des0 = 0;
1599 mb();
1600 eqos->config->ops->eqos_flush_desc(rx_desc);
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001601 eqos->config->ops->eqos_inval_buffer(packet, length);
Stephen Warren50709602016-10-21 14:46:47 -06001602 rx_desc->des0 = (u32)(ulong)packet;
1603 rx_desc->des1 = 0;
1604 rx_desc->des2 = 0;
1605 /*
1606 * Make sure that if HW sees the _OWN write below, it will see all the
1607 * writes to the rest of the descriptor too.
1608 */
1609 mb();
Marek Vasutd54c98e2020-03-23 02:02:57 +01001610 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001611 eqos->config->ops->eqos_flush_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001612
1613 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1614
1615 eqos->rx_desc_idx++;
1616 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1617
1618 return 0;
1619}
1620
1621static int eqos_probe_resources_core(struct udevice *dev)
1622{
1623 struct eqos_priv *eqos = dev_get_priv(dev);
1624 int ret;
1625
1626 debug("%s(dev=%p):\n", __func__, dev);
1627
Marek Vasut89077732021-01-07 11:12:16 +01001628 eqos->descs = eqos_alloc_descs(eqos, EQOS_DESCRIPTORS_NUM);
Stephen Warren50709602016-10-21 14:46:47 -06001629 if (!eqos->descs) {
1630 debug("%s: eqos_alloc_descs() failed\n", __func__);
1631 ret = -ENOMEM;
1632 goto err;
1633 }
Stephen Warren50709602016-10-21 14:46:47 -06001634
1635 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1636 if (!eqos->tx_dma_buf) {
1637 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1638 ret = -ENOMEM;
1639 goto err_free_descs;
1640 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001641 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001642
1643 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1644 if (!eqos->rx_dma_buf) {
1645 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1646 ret = -ENOMEM;
1647 goto err_free_tx_dma_buf;
1648 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001649 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001650
1651 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1652 if (!eqos->rx_pkt) {
1653 debug("%s: malloc(rx_pkt) failed\n", __func__);
1654 ret = -ENOMEM;
1655 goto err_free_rx_dma_buf;
1656 }
1657 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1658
Marek Vasute8e5c2b2020-03-23 02:09:55 +01001659 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1660 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1661
Stephen Warren50709602016-10-21 14:46:47 -06001662 debug("%s: OK\n", __func__);
1663 return 0;
1664
1665err_free_rx_dma_buf:
1666 free(eqos->rx_dma_buf);
1667err_free_tx_dma_buf:
1668 free(eqos->tx_dma_buf);
1669err_free_descs:
1670 eqos_free_descs(eqos->descs);
1671err:
1672
1673 debug("%s: returns %d\n", __func__, ret);
1674 return ret;
1675}
1676
1677static int eqos_remove_resources_core(struct udevice *dev)
1678{
1679 struct eqos_priv *eqos = dev_get_priv(dev);
1680
1681 debug("%s(dev=%p):\n", __func__, dev);
1682
1683 free(eqos->rx_pkt);
1684 free(eqos->rx_dma_buf);
1685 free(eqos->tx_dma_buf);
1686 eqos_free_descs(eqos->descs);
1687
1688 debug("%s: OK\n", __func__);
1689 return 0;
1690}
1691
1692static int eqos_probe_resources_tegra186(struct udevice *dev)
1693{
1694 struct eqos_priv *eqos = dev_get_priv(dev);
1695 int ret;
1696
1697 debug("%s(dev=%p):\n", __func__, dev);
1698
1699 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1700 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001701 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001702 return ret;
1703 }
1704
1705 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1706 &eqos->phy_reset_gpio,
1707 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1708 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001709 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001710 goto err_free_reset_eqos;
1711 }
1712
1713 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1714 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001715 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001716 goto err_free_gpio_phy_reset;
1717 }
1718
1719 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1720 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001721 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001722 goto err_free_clk_slave_bus;
1723 }
1724
1725 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1726 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001727 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001728 goto err_free_clk_master_bus;
1729 }
1730
1731 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1732 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001733 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001734 goto err_free_clk_rx;
1735 return ret;
1736 }
1737
1738 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1739 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001740 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001741 goto err_free_clk_ptp_ref;
1742 }
1743
1744 debug("%s: OK\n", __func__);
1745 return 0;
1746
1747err_free_clk_ptp_ref:
1748 clk_free(&eqos->clk_ptp_ref);
1749err_free_clk_rx:
1750 clk_free(&eqos->clk_rx);
1751err_free_clk_master_bus:
1752 clk_free(&eqos->clk_master_bus);
1753err_free_clk_slave_bus:
1754 clk_free(&eqos->clk_slave_bus);
1755err_free_gpio_phy_reset:
1756 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1757err_free_reset_eqos:
1758 reset_free(&eqos->reset_ctl);
1759
1760 debug("%s: returns %d\n", __func__, ret);
1761 return ret;
1762}
1763
Christophe Roullier6beb7802019-05-17 15:08:44 +02001764/* board-specific Ethernet Interface initializations. */
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001765__weak int board_interface_eth_init(struct udevice *dev,
1766 phy_interface_t interface_type)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001767{
1768 return 0;
1769}
1770
1771static int eqos_probe_resources_stm32(struct udevice *dev)
1772{
1773 struct eqos_priv *eqos = dev_get_priv(dev);
1774 int ret;
1775 phy_interface_t interface;
Christophe Roullier104dab52020-03-18 10:50:15 +01001776 struct ofnode_phandle_args phandle_args;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001777
1778 debug("%s(dev=%p):\n", __func__, dev);
1779
1780 interface = eqos->config->interface(dev);
1781
1782 if (interface == PHY_INTERFACE_MODE_NONE) {
1783 pr_err("Invalid PHY interface\n");
1784 return -EINVAL;
1785 }
1786
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001787 ret = board_interface_eth_init(dev, interface);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001788 if (ret)
1789 return -EINVAL;
1790
Patrick Delaunay5c8db372020-03-18 10:50:16 +01001791 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1792
Christophe Roullier6beb7802019-05-17 15:08:44 +02001793 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1794 if (ret) {
1795 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1796 goto err_probe;
1797 }
1798
1799 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1800 if (ret) {
1801 pr_err("clk_get_by_name(rx) failed: %d", ret);
1802 goto err_free_clk_master_bus;
1803 }
1804
1805 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1806 if (ret) {
1807 pr_err("clk_get_by_name(tx) failed: %d", ret);
1808 goto err_free_clk_rx;
1809 }
1810
1811 /* Get ETH_CLK clocks (optional) */
1812 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1813 if (ret)
1814 pr_warn("No phy clock provided %d", ret);
1815
Patrick Delaunay5c8db372020-03-18 10:50:16 +01001816 eqos->phyaddr = -1;
Christophe Roullier104dab52020-03-18 10:50:15 +01001817 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1818 &phandle_args);
1819 if (!ret) {
1820 /* search "reset-gpios" in phy node */
1821 ret = gpio_request_by_name_nodev(phandle_args.node,
1822 "reset-gpios", 0,
1823 &eqos->phy_reset_gpio,
1824 GPIOD_IS_OUT |
1825 GPIOD_IS_OUT_ACTIVE);
1826 if (ret)
1827 pr_warn("gpio_request_by_name(phy reset) not provided %d",
1828 ret);
Patrick Delaunay5c8db372020-03-18 10:50:16 +01001829
1830 eqos->phyaddr = ofnode_read_u32_default(phandle_args.node,
1831 "reg", -1);
Christophe Roullier104dab52020-03-18 10:50:15 +01001832 }
1833
Christophe Roullier6beb7802019-05-17 15:08:44 +02001834 debug("%s: OK\n", __func__);
1835 return 0;
1836
1837err_free_clk_rx:
1838 clk_free(&eqos->clk_rx);
1839err_free_clk_master_bus:
1840 clk_free(&eqos->clk_master_bus);
1841err_probe:
1842
1843 debug("%s: returns %d\n", __func__, ret);
1844 return ret;
1845}
1846
1847static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
1848{
1849 const char *phy_mode;
1850 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
1851
1852 debug("%s(dev=%p):\n", __func__, dev);
1853
Patrick Delaunay9e6ed382020-09-09 18:30:06 +02001854 phy_mode = dev_read_prop(dev, "phy-mode", NULL);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001855 if (phy_mode)
1856 interface = phy_get_interface_by_name(phy_mode);
1857
1858 return interface;
1859}
1860
1861static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
1862{
1863 return PHY_INTERFACE_MODE_MII;
1864}
1865
Fugang Duan37aae5f2020-05-03 22:41:17 +08001866static int eqos_probe_resources_imx(struct udevice *dev)
1867{
1868 struct eqos_priv *eqos = dev_get_priv(dev);
1869 phy_interface_t interface;
1870
1871 debug("%s(dev=%p):\n", __func__, dev);
1872
1873 interface = eqos->config->interface(dev);
1874
1875 if (interface == PHY_INTERFACE_MODE_NONE) {
1876 pr_err("Invalid PHY interface\n");
1877 return -EINVAL;
1878 }
1879
1880 debug("%s: OK\n", __func__);
1881 return 0;
1882}
1883
1884static phy_interface_t eqos_get_interface_imx(struct udevice *dev)
1885{
Fugang Duandd455e62020-05-03 22:41:18 +08001886 const char *phy_mode;
1887 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
1888
1889 debug("%s(dev=%p):\n", __func__, dev);
1890
Patrick Delaunay9e6ed382020-09-09 18:30:06 +02001891 phy_mode = dev_read_prop(dev, "phy-mode", NULL);
Fugang Duandd455e62020-05-03 22:41:18 +08001892 if (phy_mode)
1893 interface = phy_get_interface_by_name(phy_mode);
1894
1895 return interface;
Fugang Duan37aae5f2020-05-03 22:41:17 +08001896}
1897
Stephen Warren50709602016-10-21 14:46:47 -06001898static int eqos_remove_resources_tegra186(struct udevice *dev)
1899{
1900 struct eqos_priv *eqos = dev_get_priv(dev);
1901
1902 debug("%s(dev=%p):\n", __func__, dev);
1903
Fugang Duan37aae5f2020-05-03 22:41:17 +08001904#ifdef CONFIG_CLK
Stephen Warren50709602016-10-21 14:46:47 -06001905 clk_free(&eqos->clk_tx);
1906 clk_free(&eqos->clk_ptp_ref);
1907 clk_free(&eqos->clk_rx);
1908 clk_free(&eqos->clk_slave_bus);
1909 clk_free(&eqos->clk_master_bus);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001910#endif
Stephen Warren50709602016-10-21 14:46:47 -06001911 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1912 reset_free(&eqos->reset_ctl);
1913
1914 debug("%s: OK\n", __func__);
1915 return 0;
1916}
1917
Christophe Roullier6beb7802019-05-17 15:08:44 +02001918static int eqos_remove_resources_stm32(struct udevice *dev)
1919{
Fugang Duan37aae5f2020-05-03 22:41:17 +08001920#ifdef CONFIG_CLK
Christophe Roullier6beb7802019-05-17 15:08:44 +02001921 struct eqos_priv *eqos = dev_get_priv(dev);
1922
1923 debug("%s(dev=%p):\n", __func__, dev);
1924
1925 clk_free(&eqos->clk_tx);
1926 clk_free(&eqos->clk_rx);
1927 clk_free(&eqos->clk_master_bus);
1928 if (clk_valid(&eqos->clk_ck))
1929 clk_free(&eqos->clk_ck);
Fugang Duan37aae5f2020-05-03 22:41:17 +08001930#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02001931
Christophe Roullier104dab52020-03-18 10:50:15 +01001932 if (dm_gpio_is_valid(&eqos->phy_reset_gpio))
1933 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1934
Christophe Roullier6beb7802019-05-17 15:08:44 +02001935 debug("%s: OK\n", __func__);
1936 return 0;
1937}
1938
Fugang Duan37aae5f2020-05-03 22:41:17 +08001939static int eqos_remove_resources_imx(struct udevice *dev)
1940{
1941 return 0;
1942}
1943
Stephen Warren50709602016-10-21 14:46:47 -06001944static int eqos_probe(struct udevice *dev)
1945{
1946 struct eqos_priv *eqos = dev_get_priv(dev);
1947 int ret;
1948
1949 debug("%s(dev=%p):\n", __func__, dev);
1950
1951 eqos->dev = dev;
1952 eqos->config = (void *)dev_get_driver_data(dev);
1953
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001954 eqos->regs = dev_read_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001955 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +09001956 pr_err("dev_read_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001957 return -ENODEV;
1958 }
1959 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1960 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1961 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1962 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1963
1964 ret = eqos_probe_resources_core(dev);
1965 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001966 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001967 return ret;
1968 }
1969
Christophe Roullier6beb7802019-05-17 15:08:44 +02001970 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001971 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001972 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001973 goto err_remove_resources_core;
1974 }
1975
Ye Liad122b72020-05-03 22:41:15 +08001976#ifdef CONFIG_DM_ETH_PHY
1977 eqos->mii = eth_phy_get_mdio_bus(dev);
1978#endif
Stephen Warren50709602016-10-21 14:46:47 -06001979 if (!eqos->mii) {
Ye Liad122b72020-05-03 22:41:15 +08001980 eqos->mii = mdio_alloc();
1981 if (!eqos->mii) {
1982 pr_err("mdio_alloc() failed");
1983 ret = -ENOMEM;
1984 goto err_remove_resources_tegra;
1985 }
1986 eqos->mii->read = eqos_mdio_read;
1987 eqos->mii->write = eqos_mdio_write;
1988 eqos->mii->priv = eqos;
1989 strcpy(eqos->mii->name, dev->name);
Stephen Warren50709602016-10-21 14:46:47 -06001990
Ye Liad122b72020-05-03 22:41:15 +08001991 ret = mdio_register(eqos->mii);
1992 if (ret < 0) {
1993 pr_err("mdio_register() failed: %d", ret);
1994 goto err_free_mdio;
1995 }
Stephen Warren50709602016-10-21 14:46:47 -06001996 }
1997
Ye Liad122b72020-05-03 22:41:15 +08001998#ifdef CONFIG_DM_ETH_PHY
1999 eth_phy_set_mdio_bus(dev, eqos->mii);
2000#endif
2001
Stephen Warren50709602016-10-21 14:46:47 -06002002 debug("%s: OK\n", __func__);
2003 return 0;
2004
2005err_free_mdio:
2006 mdio_free(eqos->mii);
2007err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02002008 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06002009err_remove_resources_core:
2010 eqos_remove_resources_core(dev);
2011
2012 debug("%s: returns %d\n", __func__, ret);
2013 return ret;
2014}
2015
2016static int eqos_remove(struct udevice *dev)
2017{
2018 struct eqos_priv *eqos = dev_get_priv(dev);
2019
2020 debug("%s(dev=%p):\n", __func__, dev);
2021
2022 mdio_unregister(eqos->mii);
2023 mdio_free(eqos->mii);
Christophe Roullier6beb7802019-05-17 15:08:44 +02002024 eqos->config->ops->eqos_remove_resources(dev);
2025
Stephen Warren50709602016-10-21 14:46:47 -06002026 eqos_probe_resources_core(dev);
2027
2028 debug("%s: OK\n", __func__);
2029 return 0;
2030}
2031
2032static const struct eth_ops eqos_ops = {
2033 .start = eqos_start,
2034 .stop = eqos_stop,
2035 .send = eqos_send,
2036 .recv = eqos_recv,
2037 .free_pkt = eqos_free_pkt,
2038 .write_hwaddr = eqos_write_hwaddr,
Ye Li3fb1a0e2020-05-03 22:41:20 +08002039 .read_rom_hwaddr = eqos_read_rom_hwaddr,
Stephen Warren50709602016-10-21 14:46:47 -06002040};
2041
Christophe Roullier6beb7802019-05-17 15:08:44 +02002042static struct eqos_ops eqos_tegra186_ops = {
Marek Vasut89077732021-01-07 11:12:16 +01002043 .eqos_inval_desc = eqos_inval_desc_generic,
2044 .eqos_flush_desc = eqos_flush_desc_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02002045 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
2046 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
2047 .eqos_probe_resources = eqos_probe_resources_tegra186,
2048 .eqos_remove_resources = eqos_remove_resources_tegra186,
2049 .eqos_stop_resets = eqos_stop_resets_tegra186,
2050 .eqos_start_resets = eqos_start_resets_tegra186,
2051 .eqos_stop_clks = eqos_stop_clks_tegra186,
2052 .eqos_start_clks = eqos_start_clks_tegra186,
2053 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
2054 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
2055 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
2056 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
2057};
2058
Patrick Delaunay68083902020-06-08 11:27:19 +02002059static const struct eqos_config __maybe_unused eqos_tegra186_config = {
Stephen Warren50709602016-10-21 14:46:47 -06002060 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02002061 .mdio_wait = 10,
2062 .swr_wait = 10,
2063 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
2064 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
Marek Vasut89077732021-01-07 11:12:16 +01002065 .axi_bus_width = EQOS_AXI_WIDTH_128,
Christophe Roullier6beb7802019-05-17 15:08:44 +02002066 .interface = eqos_get_interface_tegra186,
2067 .ops = &eqos_tegra186_ops
2068};
2069
2070static struct eqos_ops eqos_stm32_ops = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08002071 .eqos_inval_desc = eqos_inval_desc_generic,
2072 .eqos_flush_desc = eqos_flush_desc_generic,
2073 .eqos_inval_buffer = eqos_inval_buffer_generic,
2074 .eqos_flush_buffer = eqos_flush_buffer_generic,
Christophe Roullier6beb7802019-05-17 15:08:44 +02002075 .eqos_probe_resources = eqos_probe_resources_stm32,
2076 .eqos_remove_resources = eqos_remove_resources_stm32,
2077 .eqos_stop_resets = eqos_stop_resets_stm32,
2078 .eqos_start_resets = eqos_start_resets_stm32,
2079 .eqos_stop_clks = eqos_stop_clks_stm32,
2080 .eqos_start_clks = eqos_start_clks_stm32,
2081 .eqos_calibrate_pads = eqos_calibrate_pads_stm32,
2082 .eqos_disable_calibration = eqos_disable_calibration_stm32,
2083 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_stm32,
2084 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
2085};
2086
Patrick Delaunay68083902020-06-08 11:27:19 +02002087static const struct eqos_config __maybe_unused eqos_stm32_config = {
Christophe Roullier6beb7802019-05-17 15:08:44 +02002088 .reg_access_always_ok = false,
2089 .mdio_wait = 10000,
2090 .swr_wait = 50,
2091 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
2092 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01002093 .axi_bus_width = EQOS_AXI_WIDTH_64,
Christophe Roullier6beb7802019-05-17 15:08:44 +02002094 .interface = eqos_get_interface_stm32,
2095 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06002096};
2097
Fugang Duan37aae5f2020-05-03 22:41:17 +08002098static struct eqos_ops eqos_imx_ops = {
2099 .eqos_inval_desc = eqos_inval_desc_generic,
2100 .eqos_flush_desc = eqos_flush_desc_generic,
2101 .eqos_inval_buffer = eqos_inval_buffer_generic,
2102 .eqos_flush_buffer = eqos_flush_buffer_generic,
2103 .eqos_probe_resources = eqos_probe_resources_imx,
2104 .eqos_remove_resources = eqos_remove_resources_imx,
2105 .eqos_stop_resets = eqos_stop_resets_imx,
2106 .eqos_start_resets = eqos_start_resets_imx,
2107 .eqos_stop_clks = eqos_stop_clks_imx,
2108 .eqos_start_clks = eqos_start_clks_imx,
2109 .eqos_calibrate_pads = eqos_calibrate_pads_imx,
2110 .eqos_disable_calibration = eqos_disable_calibration_imx,
2111 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_imx,
2112 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_imx
2113};
2114
Patrick Delaunay68083902020-06-08 11:27:19 +02002115struct eqos_config __maybe_unused eqos_imx_config = {
Fugang Duan37aae5f2020-05-03 22:41:17 +08002116 .reg_access_always_ok = false,
Ye Lif369e692020-12-28 20:15:10 +08002117 .mdio_wait = 10,
Fugang Duan37aae5f2020-05-03 22:41:17 +08002118 .swr_wait = 50,
2119 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
2120 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
Marek Vasut89077732021-01-07 11:12:16 +01002121 .axi_bus_width = EQOS_AXI_WIDTH_64,
Fugang Duan37aae5f2020-05-03 22:41:17 +08002122 .interface = eqos_get_interface_imx,
2123 .ops = &eqos_imx_ops
2124};
2125
Stephen Warren50709602016-10-21 14:46:47 -06002126static const struct udevice_id eqos_ids[] = {
Patrick Delaunay68083902020-06-08 11:27:19 +02002127#if IS_ENABLED(CONFIG_DWC_ETH_QOS_TEGRA186)
Stephen Warren50709602016-10-21 14:46:47 -06002128 {
2129 .compatible = "nvidia,tegra186-eqos",
2130 .data = (ulong)&eqos_tegra186_config
2131 },
Patrick Delaunay68083902020-06-08 11:27:19 +02002132#endif
2133#if IS_ENABLED(CONFIG_DWC_ETH_QOS_STM32)
Christophe Roullier6beb7802019-05-17 15:08:44 +02002134 {
Patrick Delaunaya0466f62020-05-14 15:00:23 +02002135 .compatible = "st,stm32mp1-dwmac",
Christophe Roullier6beb7802019-05-17 15:08:44 +02002136 .data = (ulong)&eqos_stm32_config
2137 },
Patrick Delaunay68083902020-06-08 11:27:19 +02002138#endif
2139#if IS_ENABLED(CONFIG_DWC_ETH_QOS_IMX)
Fugang Duan37aae5f2020-05-03 22:41:17 +08002140 {
2141 .compatible = "fsl,imx-eqos",
2142 .data = (ulong)&eqos_imx_config
2143 },
Patrick Delaunay68083902020-06-08 11:27:19 +02002144#endif
Christophe Roullier6beb7802019-05-17 15:08:44 +02002145
Stephen Warren50709602016-10-21 14:46:47 -06002146 { }
2147};
2148
2149U_BOOT_DRIVER(eth_eqos) = {
2150 .name = "eth_eqos",
2151 .id = UCLASS_ETH,
Fugang Duan37aae5f2020-05-03 22:41:17 +08002152 .of_match = of_match_ptr(eqos_ids),
Stephen Warren50709602016-10-21 14:46:47 -06002153 .probe = eqos_probe,
2154 .remove = eqos_remove,
2155 .ops = &eqos_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07002156 .priv_auto = sizeof(struct eqos_priv),
Simon Glass71fa5b42020-12-03 16:55:18 -07002157 .plat_auto = sizeof(struct eth_pdata),
Stephen Warren50709602016-10-21 14:46:47 -06002158};