blob: 0564bebf76c007ae2fb37c819febf371951b731a [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 */
Stephen Warren50709602016-10-21 14:46:47 -060029#include <common.h>
30#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -070031#include <cpu_func.h>
Stephen Warren50709602016-10-21 14:46:47 -060032#include <dm.h>
33#include <errno.h>
Simon Glass9bc15642020-02-03 07:36:16 -070034#include <malloc.h>
Stephen Warren50709602016-10-21 14:46:47 -060035#include <memalign.h>
36#include <miiphy.h>
37#include <net.h>
38#include <netdev.h>
39#include <phy.h>
40#include <reset.h>
41#include <wait_bit.h>
42#include <asm/gpio.h>
43#include <asm/io.h>
44
45/* Core registers */
46
47#define EQOS_MAC_REGS_BASE 0x000
48struct eqos_mac_regs {
49 uint32_t configuration; /* 0x000 */
50 uint32_t unused_004[(0x070 - 0x004) / 4]; /* 0x004 */
51 uint32_t q0_tx_flow_ctrl; /* 0x070 */
52 uint32_t unused_070[(0x090 - 0x074) / 4]; /* 0x074 */
53 uint32_t rx_flow_ctrl; /* 0x090 */
54 uint32_t unused_094; /* 0x094 */
55 uint32_t txq_prty_map0; /* 0x098 */
56 uint32_t unused_09c; /* 0x09c */
57 uint32_t rxq_ctrl0; /* 0x0a0 */
58 uint32_t unused_0a4; /* 0x0a4 */
59 uint32_t rxq_ctrl2; /* 0x0a8 */
60 uint32_t unused_0ac[(0x0dc - 0x0ac) / 4]; /* 0x0ac */
61 uint32_t us_tic_counter; /* 0x0dc */
62 uint32_t unused_0e0[(0x11c - 0x0e0) / 4]; /* 0x0e0 */
63 uint32_t hw_feature0; /* 0x11c */
64 uint32_t hw_feature1; /* 0x120 */
65 uint32_t hw_feature2; /* 0x124 */
66 uint32_t unused_128[(0x200 - 0x128) / 4]; /* 0x128 */
67 uint32_t mdio_address; /* 0x200 */
68 uint32_t mdio_data; /* 0x204 */
69 uint32_t unused_208[(0x300 - 0x208) / 4]; /* 0x208 */
70 uint32_t address0_high; /* 0x300 */
71 uint32_t address0_low; /* 0x304 */
72};
73
74#define EQOS_MAC_CONFIGURATION_GPSLCE BIT(23)
75#define EQOS_MAC_CONFIGURATION_CST BIT(21)
76#define EQOS_MAC_CONFIGURATION_ACS BIT(20)
77#define EQOS_MAC_CONFIGURATION_WD BIT(19)
78#define EQOS_MAC_CONFIGURATION_JD BIT(17)
79#define EQOS_MAC_CONFIGURATION_JE BIT(16)
80#define EQOS_MAC_CONFIGURATION_PS BIT(15)
81#define EQOS_MAC_CONFIGURATION_FES BIT(14)
82#define EQOS_MAC_CONFIGURATION_DM BIT(13)
83#define EQOS_MAC_CONFIGURATION_TE BIT(1)
84#define EQOS_MAC_CONFIGURATION_RE BIT(0)
85
86#define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT 16
87#define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_MASK 0xffff
88#define EQOS_MAC_Q0_TX_FLOW_CTRL_TFE BIT(1)
89
90#define EQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
91
92#define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT 0
93#define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK 0xff
94
95#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT 0
96#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK 3
97#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_NOT_ENABLED 0
98#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB 2
Christophe Roullier6beb7802019-05-17 15:08:44 +020099#define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV 1
Stephen Warren50709602016-10-21 14:46:47 -0600100
101#define EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT 0
102#define EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK 0xff
103
104#define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT 6
105#define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK 0x1f
106#define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT 0
107#define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK 0x1f
108
109#define EQOS_MAC_MDIO_ADDRESS_PA_SHIFT 21
110#define EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT 16
111#define EQOS_MAC_MDIO_ADDRESS_CR_SHIFT 8
112#define EQOS_MAC_MDIO_ADDRESS_CR_20_35 2
Christophe Roullier6beb7802019-05-17 15:08:44 +0200113#define EQOS_MAC_MDIO_ADDRESS_CR_250_300 5
Stephen Warren50709602016-10-21 14:46:47 -0600114#define EQOS_MAC_MDIO_ADDRESS_SKAP BIT(4)
115#define EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT 2
116#define EQOS_MAC_MDIO_ADDRESS_GOC_READ 3
117#define EQOS_MAC_MDIO_ADDRESS_GOC_WRITE 1
118#define EQOS_MAC_MDIO_ADDRESS_C45E BIT(1)
119#define EQOS_MAC_MDIO_ADDRESS_GB BIT(0)
120
121#define EQOS_MAC_MDIO_DATA_GD_MASK 0xffff
122
123#define EQOS_MTL_REGS_BASE 0xd00
124struct eqos_mtl_regs {
125 uint32_t txq0_operation_mode; /* 0xd00 */
126 uint32_t unused_d04; /* 0xd04 */
127 uint32_t txq0_debug; /* 0xd08 */
128 uint32_t unused_d0c[(0xd18 - 0xd0c) / 4]; /* 0xd0c */
129 uint32_t txq0_quantum_weight; /* 0xd18 */
130 uint32_t unused_d1c[(0xd30 - 0xd1c) / 4]; /* 0xd1c */
131 uint32_t rxq0_operation_mode; /* 0xd30 */
132 uint32_t unused_d34; /* 0xd34 */
133 uint32_t rxq0_debug; /* 0xd38 */
134};
135
136#define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT 16
137#define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK 0x1ff
138#define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT 2
139#define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_MASK 3
140#define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED 2
141#define EQOS_MTL_TXQ0_OPERATION_MODE_TSF BIT(1)
142#define EQOS_MTL_TXQ0_OPERATION_MODE_FTQ BIT(0)
143
144#define EQOS_MTL_TXQ0_DEBUG_TXQSTS BIT(4)
145#define EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT 1
146#define EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK 3
147
148#define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT 20
149#define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK 0x3ff
150#define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT 14
151#define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK 0x3f
152#define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT 8
153#define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK 0x3f
154#define EQOS_MTL_RXQ0_OPERATION_MODE_EHFC BIT(7)
155#define EQOS_MTL_RXQ0_OPERATION_MODE_RSF BIT(5)
156
157#define EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT 16
158#define EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK 0x7fff
159#define EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT 4
160#define EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK 3
161
162#define EQOS_DMA_REGS_BASE 0x1000
163struct eqos_dma_regs {
164 uint32_t mode; /* 0x1000 */
165 uint32_t sysbus_mode; /* 0x1004 */
166 uint32_t unused_1008[(0x1100 - 0x1008) / 4]; /* 0x1008 */
167 uint32_t ch0_control; /* 0x1100 */
168 uint32_t ch0_tx_control; /* 0x1104 */
169 uint32_t ch0_rx_control; /* 0x1108 */
170 uint32_t unused_110c; /* 0x110c */
171 uint32_t ch0_txdesc_list_haddress; /* 0x1110 */
172 uint32_t ch0_txdesc_list_address; /* 0x1114 */
173 uint32_t ch0_rxdesc_list_haddress; /* 0x1118 */
174 uint32_t ch0_rxdesc_list_address; /* 0x111c */
175 uint32_t ch0_txdesc_tail_pointer; /* 0x1120 */
176 uint32_t unused_1124; /* 0x1124 */
177 uint32_t ch0_rxdesc_tail_pointer; /* 0x1128 */
178 uint32_t ch0_txdesc_ring_length; /* 0x112c */
179 uint32_t ch0_rxdesc_ring_length; /* 0x1130 */
180};
181
182#define EQOS_DMA_MODE_SWR BIT(0)
183
184#define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT 16
185#define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK 0xf
186#define EQOS_DMA_SYSBUS_MODE_EAME BIT(11)
187#define EQOS_DMA_SYSBUS_MODE_BLEN16 BIT(3)
188#define EQOS_DMA_SYSBUS_MODE_BLEN8 BIT(2)
189#define EQOS_DMA_SYSBUS_MODE_BLEN4 BIT(1)
190
191#define EQOS_DMA_CH0_CONTROL_PBLX8 BIT(16)
192
193#define EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT 16
194#define EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK 0x3f
195#define EQOS_DMA_CH0_TX_CONTROL_OSP BIT(4)
196#define EQOS_DMA_CH0_TX_CONTROL_ST BIT(0)
197
198#define EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT 16
199#define EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK 0x3f
200#define EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT 1
201#define EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK 0x3fff
202#define EQOS_DMA_CH0_RX_CONTROL_SR BIT(0)
203
204/* These registers are Tegra186-specific */
205#define EQOS_TEGRA186_REGS_BASE 0x8800
206struct eqos_tegra186_regs {
207 uint32_t sdmemcomppadctrl; /* 0x8800 */
208 uint32_t auto_cal_config; /* 0x8804 */
209 uint32_t unused_8808; /* 0x8808 */
210 uint32_t auto_cal_status; /* 0x880c */
211};
212
213#define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
214
215#define EQOS_AUTO_CAL_CONFIG_START BIT(31)
216#define EQOS_AUTO_CAL_CONFIG_ENABLE BIT(29)
217
218#define EQOS_AUTO_CAL_STATUS_ACTIVE BIT(31)
219
220/* Descriptors */
221
222#define EQOS_DESCRIPTOR_WORDS 4
223#define EQOS_DESCRIPTOR_SIZE (EQOS_DESCRIPTOR_WORDS * 4)
224/* We assume ARCH_DMA_MINALIGN >= 16; 16 is the EQOS HW minimum */
225#define EQOS_DESCRIPTOR_ALIGN ARCH_DMA_MINALIGN
226#define EQOS_DESCRIPTORS_TX 4
227#define EQOS_DESCRIPTORS_RX 4
228#define EQOS_DESCRIPTORS_NUM (EQOS_DESCRIPTORS_TX + EQOS_DESCRIPTORS_RX)
229#define EQOS_DESCRIPTORS_SIZE ALIGN(EQOS_DESCRIPTORS_NUM * \
230 EQOS_DESCRIPTOR_SIZE, ARCH_DMA_MINALIGN)
231#define EQOS_BUFFER_ALIGN ARCH_DMA_MINALIGN
232#define EQOS_MAX_PACKET_SIZE ALIGN(1568, ARCH_DMA_MINALIGN)
233#define EQOS_RX_BUFFER_SIZE (EQOS_DESCRIPTORS_RX * EQOS_MAX_PACKET_SIZE)
234
235/*
236 * Warn if the cache-line size is larger than the descriptor size. In such
237 * cases the driver will likely fail because the CPU needs to flush the cache
238 * when requeuing RX buffers, therefore descriptors written by the hardware
239 * may be discarded. Architectures with full IO coherence, such as x86, do not
240 * experience this issue, and hence are excluded from this condition.
241 *
242 * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
243 * the driver to allocate descriptors from a pool of non-cached memory.
244 */
245#if EQOS_DESCRIPTOR_SIZE < ARCH_DMA_MINALIGN
246#if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400247 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86)
Stephen Warren50709602016-10-21 14:46:47 -0600248#warning Cache line size is larger than descriptor size
249#endif
250#endif
251
252struct eqos_desc {
253 u32 des0;
254 u32 des1;
255 u32 des2;
256 u32 des3;
257};
258
259#define EQOS_DESC3_OWN BIT(31)
260#define EQOS_DESC3_FD BIT(29)
261#define EQOS_DESC3_LD BIT(28)
262#define EQOS_DESC3_BUF1V BIT(24)
263
264struct 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;
270 phy_interface_t (*interface)(struct udevice *dev);
271 struct eqos_ops *ops;
272};
273
274struct eqos_ops {
275 void (*eqos_inval_desc)(void *desc);
276 void (*eqos_flush_desc)(void *desc);
277 void (*eqos_inval_buffer)(void *buf, size_t size);
278 void (*eqos_flush_buffer)(void *buf, size_t size);
279 int (*eqos_probe_resources)(struct udevice *dev);
280 int (*eqos_remove_resources)(struct udevice *dev);
281 int (*eqos_stop_resets)(struct udevice *dev);
282 int (*eqos_start_resets)(struct udevice *dev);
283 void (*eqos_stop_clks)(struct udevice *dev);
284 int (*eqos_start_clks)(struct udevice *dev);
285 int (*eqos_calibrate_pads)(struct udevice *dev);
286 int (*eqos_disable_calibration)(struct udevice *dev);
287 int (*eqos_set_tx_clk_speed)(struct udevice *dev);
288 ulong (*eqos_get_tick_clk_rate)(struct udevice *dev);
Stephen Warren50709602016-10-21 14:46:47 -0600289};
290
291struct eqos_priv {
292 struct udevice *dev;
293 const struct eqos_config *config;
294 fdt_addr_t regs;
295 struct eqos_mac_regs *mac_regs;
296 struct eqos_mtl_regs *mtl_regs;
297 struct eqos_dma_regs *dma_regs;
298 struct eqos_tegra186_regs *tegra186_regs;
299 struct reset_ctl reset_ctl;
300 struct gpio_desc phy_reset_gpio;
301 struct clk clk_master_bus;
302 struct clk clk_rx;
303 struct clk clk_ptp_ref;
304 struct clk clk_tx;
Christophe Roullier6beb7802019-05-17 15:08:44 +0200305 struct clk clk_ck;
Stephen Warren50709602016-10-21 14:46:47 -0600306 struct clk clk_slave_bus;
307 struct mii_dev *mii;
308 struct phy_device *phy;
309 void *descs;
310 struct eqos_desc *tx_descs;
311 struct eqos_desc *rx_descs;
312 int tx_desc_idx, rx_desc_idx;
313 void *tx_dma_buf;
314 void *rx_dma_buf;
315 void *rx_pkt;
316 bool started;
317 bool reg_access_ok;
318};
319
320/*
321 * TX and RX descriptors are 16 bytes. This causes problems with the cache
322 * maintenance on CPUs where the cache-line size exceeds the size of these
323 * descriptors. What will happen is that when the driver receives a packet
324 * it will be immediately requeued for the hardware to reuse. The CPU will
325 * therefore need to flush the cache-line containing the descriptor, which
326 * will cause all other descriptors in the same cache-line to be flushed
327 * along with it. If one of those descriptors had been written to by the
328 * device those changes (and the associated packet) will be lost.
329 *
330 * To work around this, we make use of non-cached memory if available. If
331 * descriptors are mapped uncached there's no need to manually flush them
332 * or invalidate them.
333 *
334 * Note that this only applies to descriptors. The packet data buffers do
335 * not have the same constraints since they are 1536 bytes large, so they
336 * are unlikely to share cache-lines.
337 */
338static void *eqos_alloc_descs(unsigned int num)
339{
340#ifdef CONFIG_SYS_NONCACHED_MEMORY
341 return (void *)noncached_alloc(EQOS_DESCRIPTORS_SIZE,
342 EQOS_DESCRIPTOR_ALIGN);
343#else
344 return memalign(EQOS_DESCRIPTOR_ALIGN, EQOS_DESCRIPTORS_SIZE);
345#endif
346}
347
348static void eqos_free_descs(void *descs)
349{
350#ifdef CONFIG_SYS_NONCACHED_MEMORY
351 /* FIXME: noncached_alloc() has no opposite */
352#else
353 free(descs);
354#endif
355}
356
Christophe Roullier6beb7802019-05-17 15:08:44 +0200357static void eqos_inval_desc_tegra186(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -0600358{
359#ifndef CONFIG_SYS_NONCACHED_MEMORY
360 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
361 unsigned long end = ALIGN(start + EQOS_DESCRIPTOR_SIZE,
362 ARCH_DMA_MINALIGN);
363
364 invalidate_dcache_range(start, end);
365#endif
366}
367
Christophe Roullier6beb7802019-05-17 15:08:44 +0200368static void eqos_inval_desc_stm32(void *desc)
Stephen Warren50709602016-10-21 14:46:47 -0600369{
370#ifndef CONFIG_SYS_NONCACHED_MEMORY
Christophe Roullier6beb7802019-05-17 15:08:44 +0200371 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
372 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
373 ARCH_DMA_MINALIGN);
374
375 invalidate_dcache_range(start, end);
376#endif
377}
378
379static void eqos_flush_desc_tegra186(void *desc)
380{
381#ifndef CONFIG_SYS_NONCACHED_MEMORY
Stephen Warren50709602016-10-21 14:46:47 -0600382 flush_cache((unsigned long)desc, EQOS_DESCRIPTOR_SIZE);
383#endif
384}
385
Christophe Roullier6beb7802019-05-17 15:08:44 +0200386static void eqos_flush_desc_stm32(void *desc)
387{
388#ifndef CONFIG_SYS_NONCACHED_MEMORY
389 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
390 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
391 ARCH_DMA_MINALIGN);
392
393 flush_dcache_range(start, end);
394#endif
395}
396
397static void eqos_inval_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600398{
399 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
400 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
401
402 invalidate_dcache_range(start, end);
403}
404
Christophe Roullier6beb7802019-05-17 15:08:44 +0200405static void eqos_inval_buffer_stm32(void *buf, size_t size)
406{
407 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
408 unsigned long end = roundup((unsigned long)buf + size,
409 ARCH_DMA_MINALIGN);
410
411 invalidate_dcache_range(start, end);
412}
413
414static void eqos_flush_buffer_tegra186(void *buf, size_t size)
Stephen Warren50709602016-10-21 14:46:47 -0600415{
416 flush_cache((unsigned long)buf, size);
417}
418
Christophe Roullier6beb7802019-05-17 15:08:44 +0200419static void eqos_flush_buffer_stm32(void *buf, size_t size)
420{
421 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
422 unsigned long end = roundup((unsigned long)buf + size,
423 ARCH_DMA_MINALIGN);
424
425 flush_dcache_range(start, end);
426}
427
Stephen Warren50709602016-10-21 14:46:47 -0600428static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
429{
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100430 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
431 EQOS_MAC_MDIO_ADDRESS_GB, false,
432 1000000, true);
Stephen Warren50709602016-10-21 14:46:47 -0600433}
434
435static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
436 int mdio_reg)
437{
438 struct eqos_priv *eqos = bus->priv;
439 u32 val;
440 int ret;
441
442 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
443 mdio_reg);
444
445 ret = eqos_mdio_wait_idle(eqos);
446 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900447 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600448 return ret;
449 }
450
451 val = readl(&eqos->mac_regs->mdio_address);
452 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
453 EQOS_MAC_MDIO_ADDRESS_C45E;
454 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
455 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200456 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600457 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
458 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
459 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
460 EQOS_MAC_MDIO_ADDRESS_GB;
461 writel(val, &eqos->mac_regs->mdio_address);
462
Christophe Roullier6beb7802019-05-17 15:08:44 +0200463 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600464
465 ret = eqos_mdio_wait_idle(eqos);
466 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900467 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600468 return ret;
469 }
470
471 val = readl(&eqos->mac_regs->mdio_data);
472 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
473
474 debug("%s: val=%x\n", __func__, val);
475
476 return val;
477}
478
479static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
480 int mdio_reg, u16 mdio_val)
481{
482 struct eqos_priv *eqos = bus->priv;
483 u32 val;
484 int ret;
485
486 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
487 mdio_addr, mdio_reg, mdio_val);
488
489 ret = eqos_mdio_wait_idle(eqos);
490 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900491 pr_err("MDIO not idle at entry");
Stephen Warren50709602016-10-21 14:46:47 -0600492 return ret;
493 }
494
495 writel(mdio_val, &eqos->mac_regs->mdio_data);
496
497 val = readl(&eqos->mac_regs->mdio_address);
498 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
499 EQOS_MAC_MDIO_ADDRESS_C45E;
500 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
501 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
Christophe Roullier6beb7802019-05-17 15:08:44 +0200502 (eqos->config->config_mac_mdio <<
Stephen Warren50709602016-10-21 14:46:47 -0600503 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
504 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
505 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
506 EQOS_MAC_MDIO_ADDRESS_GB;
507 writel(val, &eqos->mac_regs->mdio_address);
508
Christophe Roullier6beb7802019-05-17 15:08:44 +0200509 udelay(eqos->config->mdio_wait);
Stephen Warren50709602016-10-21 14:46:47 -0600510
511 ret = eqos_mdio_wait_idle(eqos);
512 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900513 pr_err("MDIO read didn't complete");
Stephen Warren50709602016-10-21 14:46:47 -0600514 return ret;
515 }
516
517 return 0;
518}
519
520static int eqos_start_clks_tegra186(struct udevice *dev)
521{
522 struct eqos_priv *eqos = dev_get_priv(dev);
523 int ret;
524
525 debug("%s(dev=%p):\n", __func__, dev);
526
527 ret = clk_enable(&eqos->clk_slave_bus);
528 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900529 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600530 goto err;
531 }
532
533 ret = clk_enable(&eqos->clk_master_bus);
534 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900535 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600536 goto err_disable_clk_slave_bus;
537 }
538
539 ret = clk_enable(&eqos->clk_rx);
540 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900541 pr_err("clk_enable(clk_rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600542 goto err_disable_clk_master_bus;
543 }
544
545 ret = clk_enable(&eqos->clk_ptp_ref);
546 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900547 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600548 goto err_disable_clk_rx;
549 }
550
551 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
552 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900553 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600554 goto err_disable_clk_ptp_ref;
555 }
556
557 ret = clk_enable(&eqos->clk_tx);
558 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900559 pr_err("clk_enable(clk_tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600560 goto err_disable_clk_ptp_ref;
561 }
562
563 debug("%s: OK\n", __func__);
564 return 0;
565
566err_disable_clk_ptp_ref:
567 clk_disable(&eqos->clk_ptp_ref);
568err_disable_clk_rx:
569 clk_disable(&eqos->clk_rx);
570err_disable_clk_master_bus:
571 clk_disable(&eqos->clk_master_bus);
572err_disable_clk_slave_bus:
573 clk_disable(&eqos->clk_slave_bus);
574err:
575 debug("%s: FAILED: %d\n", __func__, ret);
576 return ret;
577}
578
Christophe Roullier6beb7802019-05-17 15:08:44 +0200579static int eqos_start_clks_stm32(struct udevice *dev)
580{
581 struct eqos_priv *eqos = dev_get_priv(dev);
582 int ret;
583
584 debug("%s(dev=%p):\n", __func__, dev);
585
586 ret = clk_enable(&eqos->clk_master_bus);
587 if (ret < 0) {
588 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
589 goto err;
590 }
591
592 ret = clk_enable(&eqos->clk_rx);
593 if (ret < 0) {
594 pr_err("clk_enable(clk_rx) failed: %d", ret);
595 goto err_disable_clk_master_bus;
596 }
597
598 ret = clk_enable(&eqos->clk_tx);
599 if (ret < 0) {
600 pr_err("clk_enable(clk_tx) failed: %d", ret);
601 goto err_disable_clk_rx;
602 }
603
604 if (clk_valid(&eqos->clk_ck)) {
605 ret = clk_enable(&eqos->clk_ck);
606 if (ret < 0) {
607 pr_err("clk_enable(clk_ck) failed: %d", ret);
608 goto err_disable_clk_tx;
609 }
610 }
611
612 debug("%s: OK\n", __func__);
613 return 0;
614
615err_disable_clk_tx:
616 clk_disable(&eqos->clk_tx);
617err_disable_clk_rx:
618 clk_disable(&eqos->clk_rx);
619err_disable_clk_master_bus:
620 clk_disable(&eqos->clk_master_bus);
621err:
622 debug("%s: FAILED: %d\n", __func__, ret);
623 return ret;
624}
625
Patrick Delaunay6864a5992019-08-01 11:29:02 +0200626static void eqos_stop_clks_tegra186(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -0600627{
628 struct eqos_priv *eqos = dev_get_priv(dev);
629
630 debug("%s(dev=%p):\n", __func__, dev);
631
632 clk_disable(&eqos->clk_tx);
633 clk_disable(&eqos->clk_ptp_ref);
634 clk_disable(&eqos->clk_rx);
635 clk_disable(&eqos->clk_master_bus);
636 clk_disable(&eqos->clk_slave_bus);
637
638 debug("%s: OK\n", __func__);
639}
640
Patrick Delaunay6864a5992019-08-01 11:29:02 +0200641static void eqos_stop_clks_stm32(struct udevice *dev)
Christophe Roullier6beb7802019-05-17 15:08:44 +0200642{
643 struct eqos_priv *eqos = dev_get_priv(dev);
644
645 debug("%s(dev=%p):\n", __func__, dev);
646
647 clk_disable(&eqos->clk_tx);
648 clk_disable(&eqos->clk_rx);
649 clk_disable(&eqos->clk_master_bus);
650 if (clk_valid(&eqos->clk_ck))
651 clk_disable(&eqos->clk_ck);
652
653 debug("%s: OK\n", __func__);
654}
655
Stephen Warren50709602016-10-21 14:46:47 -0600656static int eqos_start_resets_tegra186(struct udevice *dev)
657{
658 struct eqos_priv *eqos = dev_get_priv(dev);
659 int ret;
660
661 debug("%s(dev=%p):\n", __func__, dev);
662
663 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
664 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900665 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600666 return ret;
667 }
668
669 udelay(2);
670
671 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
672 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900673 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600674 return ret;
675 }
676
677 ret = reset_assert(&eqos->reset_ctl);
678 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900679 pr_err("reset_assert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600680 return ret;
681 }
682
683 udelay(2);
684
685 ret = reset_deassert(&eqos->reset_ctl);
686 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900687 pr_err("reset_deassert() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600688 return ret;
689 }
690
691 debug("%s: OK\n", __func__);
692 return 0;
693}
694
Christophe Roullier6beb7802019-05-17 15:08:44 +0200695static int eqos_start_resets_stm32(struct udevice *dev)
696{
697 return 0;
698}
699
Stephen Warren50709602016-10-21 14:46:47 -0600700static int eqos_stop_resets_tegra186(struct udevice *dev)
701{
702 struct eqos_priv *eqos = dev_get_priv(dev);
703
704 reset_assert(&eqos->reset_ctl);
705 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
706
Christophe Roullier6beb7802019-05-17 15:08:44 +0200707 return 0;
708}
709
710static int eqos_stop_resets_stm32(struct udevice *dev)
711{
Stephen Warren50709602016-10-21 14:46:47 -0600712 return 0;
713}
714
715static int eqos_calibrate_pads_tegra186(struct udevice *dev)
716{
717 struct eqos_priv *eqos = dev_get_priv(dev);
718 int ret;
719
720 debug("%s(dev=%p):\n", __func__, dev);
721
722 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
723 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
724
725 udelay(1);
726
727 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
728 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
729
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100730 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
731 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600732 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900733 pr_err("calibrate didn't start");
Stephen Warren50709602016-10-21 14:46:47 -0600734 goto failed;
735 }
736
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100737 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
738 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
Stephen Warren50709602016-10-21 14:46:47 -0600739 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900740 pr_err("calibrate didn't finish");
Stephen Warren50709602016-10-21 14:46:47 -0600741 goto failed;
742 }
743
744 ret = 0;
745
746failed:
747 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
748 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
749
750 debug("%s: returns %d\n", __func__, ret);
751
752 return ret;
753}
754
755static int eqos_disable_calibration_tegra186(struct udevice *dev)
756{
757 struct eqos_priv *eqos = dev_get_priv(dev);
758
759 debug("%s(dev=%p):\n", __func__, dev);
760
761 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
762 EQOS_AUTO_CAL_CONFIG_ENABLE);
763
764 return 0;
765}
766
767static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
768{
769 struct eqos_priv *eqos = dev_get_priv(dev);
770
771 return clk_get_rate(&eqos->clk_slave_bus);
772}
773
Christophe Roullier6beb7802019-05-17 15:08:44 +0200774static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
775{
776 struct eqos_priv *eqos = dev_get_priv(dev);
777
778 return clk_get_rate(&eqos->clk_master_bus);
779}
780
781static int eqos_calibrate_pads_stm32(struct udevice *dev)
782{
783 return 0;
784}
785
786static int eqos_disable_calibration_stm32(struct udevice *dev)
787{
788 return 0;
789}
790
Stephen Warren50709602016-10-21 14:46:47 -0600791static int eqos_set_full_duplex(struct udevice *dev)
792{
793 struct eqos_priv *eqos = dev_get_priv(dev);
794
795 debug("%s(dev=%p):\n", __func__, dev);
796
797 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
798
799 return 0;
800}
801
802static int eqos_set_half_duplex(struct udevice *dev)
803{
804 struct eqos_priv *eqos = dev_get_priv(dev);
805
806 debug("%s(dev=%p):\n", __func__, dev);
807
808 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
809
810 /* WAR: Flush TX queue when switching to half-duplex */
811 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
812 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
813
814 return 0;
815}
816
817static int eqos_set_gmii_speed(struct udevice *dev)
818{
819 struct eqos_priv *eqos = dev_get_priv(dev);
820
821 debug("%s(dev=%p):\n", __func__, dev);
822
823 clrbits_le32(&eqos->mac_regs->configuration,
824 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
825
826 return 0;
827}
828
829static int eqos_set_mii_speed_100(struct udevice *dev)
830{
831 struct eqos_priv *eqos = dev_get_priv(dev);
832
833 debug("%s(dev=%p):\n", __func__, dev);
834
835 setbits_le32(&eqos->mac_regs->configuration,
836 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
837
838 return 0;
839}
840
841static int eqos_set_mii_speed_10(struct udevice *dev)
842{
843 struct eqos_priv *eqos = dev_get_priv(dev);
844
845 debug("%s(dev=%p):\n", __func__, dev);
846
847 clrsetbits_le32(&eqos->mac_regs->configuration,
848 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
849
850 return 0;
851}
852
853static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
854{
855 struct eqos_priv *eqos = dev_get_priv(dev);
856 ulong rate;
857 int ret;
858
859 debug("%s(dev=%p):\n", __func__, dev);
860
861 switch (eqos->phy->speed) {
862 case SPEED_1000:
863 rate = 125 * 1000 * 1000;
864 break;
865 case SPEED_100:
866 rate = 25 * 1000 * 1000;
867 break;
868 case SPEED_10:
869 rate = 2.5 * 1000 * 1000;
870 break;
871 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900872 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600873 return -EINVAL;
874 }
875
876 ret = clk_set_rate(&eqos->clk_tx, rate);
877 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900878 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
Stephen Warren50709602016-10-21 14:46:47 -0600879 return ret;
880 }
881
882 return 0;
883}
884
Christophe Roullier6beb7802019-05-17 15:08:44 +0200885static int eqos_set_tx_clk_speed_stm32(struct udevice *dev)
886{
887 return 0;
888}
889
Stephen Warren50709602016-10-21 14:46:47 -0600890static int eqos_adjust_link(struct udevice *dev)
891{
892 struct eqos_priv *eqos = dev_get_priv(dev);
893 int ret;
894 bool en_calibration;
895
896 debug("%s(dev=%p):\n", __func__, dev);
897
898 if (eqos->phy->duplex)
899 ret = eqos_set_full_duplex(dev);
900 else
901 ret = eqos_set_half_duplex(dev);
902 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900903 pr_err("eqos_set_*_duplex() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600904 return ret;
905 }
906
907 switch (eqos->phy->speed) {
908 case SPEED_1000:
909 en_calibration = true;
910 ret = eqos_set_gmii_speed(dev);
911 break;
912 case SPEED_100:
913 en_calibration = true;
914 ret = eqos_set_mii_speed_100(dev);
915 break;
916 case SPEED_10:
917 en_calibration = false;
918 ret = eqos_set_mii_speed_10(dev);
919 break;
920 default:
Masahiro Yamada81e10422017-09-16 14:10:41 +0900921 pr_err("invalid speed %d", eqos->phy->speed);
Stephen Warren50709602016-10-21 14:46:47 -0600922 return -EINVAL;
923 }
924 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +0900925 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600926 return ret;
927 }
928
929 if (en_calibration) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200930 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600931 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200932 pr_err("eqos_calibrate_pads() failed: %d",
933 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600934 return ret;
935 }
936 } else {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200937 ret = eqos->config->ops->eqos_disable_calibration(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600938 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200939 pr_err("eqos_disable_calibration() failed: %d",
940 ret);
Stephen Warren50709602016-10-21 14:46:47 -0600941 return ret;
942 }
943 }
Christophe Roullier6beb7802019-05-17 15:08:44 +0200944 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
Stephen Warren50709602016-10-21 14:46:47 -0600945 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +0200946 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -0600947 return ret;
948 }
949
950 return 0;
951}
952
953static int eqos_write_hwaddr(struct udevice *dev)
954{
955 struct eth_pdata *plat = dev_get_platdata(dev);
956 struct eqos_priv *eqos = dev_get_priv(dev);
957 uint32_t val;
958
959 /*
960 * This function may be called before start() or after stop(). At that
961 * time, on at least some configurations of the EQoS HW, all clocks to
962 * the EQoS HW block will be stopped, and a reset signal applied. If
963 * any register access is attempted in this state, bus timeouts or CPU
964 * hangs may occur. This check prevents that.
965 *
966 * A simple solution to this problem would be to not implement
967 * write_hwaddr(), since start() always writes the MAC address into HW
968 * anyway. However, it is desirable to implement write_hwaddr() to
969 * support the case of SW that runs subsequent to U-Boot which expects
970 * the MAC address to already be programmed into the EQoS registers,
971 * which must happen irrespective of whether the U-Boot user (or
972 * scripts) actually made use of the EQoS device, and hence
973 * irrespective of whether start() was ever called.
974 *
975 * Note that this requirement by subsequent SW is not valid for
976 * Tegra186, and is likely not valid for any non-PCI instantiation of
977 * the EQoS HW block. This function is implemented solely as
978 * future-proofing with the expectation the driver will eventually be
979 * ported to some system where the expectation above is true.
980 */
981 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
982 return 0;
983
984 /* Update the MAC address */
985 val = (plat->enetaddr[5] << 8) |
986 (plat->enetaddr[4]);
987 writel(val, &eqos->mac_regs->address0_high);
988 val = (plat->enetaddr[3] << 24) |
989 (plat->enetaddr[2] << 16) |
990 (plat->enetaddr[1] << 8) |
991 (plat->enetaddr[0]);
992 writel(val, &eqos->mac_regs->address0_low);
993
994 return 0;
995}
996
997static int eqos_start(struct udevice *dev)
998{
999 struct eqos_priv *eqos = dev_get_priv(dev);
1000 int ret, i;
1001 ulong rate;
1002 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
1003 ulong last_rx_desc;
1004
1005 debug("%s(dev=%p):\n", __func__, dev);
1006
1007 eqos->tx_desc_idx = 0;
1008 eqos->rx_desc_idx = 0;
1009
Christophe Roullier6beb7802019-05-17 15:08:44 +02001010 ret = eqos->config->ops->eqos_start_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001011 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001012 pr_err("eqos_start_clks() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001013 goto err;
1014 }
1015
Christophe Roullier6beb7802019-05-17 15:08:44 +02001016 ret = eqos->config->ops->eqos_start_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001017 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001018 pr_err("eqos_start_resets() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001019 goto err_stop_clks;
1020 }
1021
1022 udelay(10);
1023
1024 eqos->reg_access_ok = true;
1025
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +01001026 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001027 EQOS_DMA_MODE_SWR, false,
1028 eqos->config->swr_wait, false);
Stephen Warren50709602016-10-21 14:46:47 -06001029 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001030 pr_err("EQOS_DMA_MODE_SWR stuck");
Stephen Warren50709602016-10-21 14:46:47 -06001031 goto err_stop_resets;
1032 }
1033
Christophe Roullier6beb7802019-05-17 15:08:44 +02001034 ret = eqos->config->ops->eqos_calibrate_pads(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001035 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001036 pr_err("eqos_calibrate_pads() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001037 goto err_stop_resets;
1038 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001039 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001040
Stephen Warren50709602016-10-21 14:46:47 -06001041 val = (rate / 1000000) - 1;
1042 writel(val, &eqos->mac_regs->us_tic_counter);
1043
Christophe Roullier6beb7802019-05-17 15:08:44 +02001044 /*
1045 * if PHY was already connected and configured,
1046 * don't need to reconnect/reconfigure again
1047 */
Stephen Warren50709602016-10-21 14:46:47 -06001048 if (!eqos->phy) {
Marek Vasut5d372682019-12-18 07:48:50 +01001049 eqos->phy = phy_connect(eqos->mii, -1, dev,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001050 eqos->config->interface(dev));
1051 if (!eqos->phy) {
1052 pr_err("phy_connect() failed");
1053 goto err_stop_resets;
1054 }
1055 ret = phy_config(eqos->phy);
1056 if (ret < 0) {
1057 pr_err("phy_config() failed: %d", ret);
1058 goto err_shutdown_phy;
1059 }
Stephen Warren50709602016-10-21 14:46:47 -06001060 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001061
Stephen Warren50709602016-10-21 14:46:47 -06001062 ret = phy_startup(eqos->phy);
1063 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001064 pr_err("phy_startup() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001065 goto err_shutdown_phy;
1066 }
1067
1068 if (!eqos->phy->link) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001069 pr_err("No link");
Stephen Warren50709602016-10-21 14:46:47 -06001070 goto err_shutdown_phy;
1071 }
1072
1073 ret = eqos_adjust_link(dev);
1074 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001075 pr_err("eqos_adjust_link() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001076 goto err_shutdown_phy;
1077 }
1078
1079 /* Configure MTL */
1080
1081 /* Enable Store and Forward mode for TX */
1082 /* Program Tx operating mode */
1083 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1084 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
1085 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
1086 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
1087
1088 /* Transmit Queue weight */
1089 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
1090
1091 /* Enable Store and Forward mode for RX, since no jumbo frame */
1092 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1093 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
1094
1095 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
1096 val = readl(&eqos->mac_regs->hw_feature1);
1097 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
1098 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
1099 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
1100 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
1101
1102 /*
1103 * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
1104 * r/tqs is encoded as (n / 256) - 1.
1105 */
1106 tqs = (128 << tx_fifo_sz) / 256 - 1;
1107 rqs = (128 << rx_fifo_sz) / 256 - 1;
1108
1109 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1110 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
1111 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
1112 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
1113 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1114 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
1115 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
1116 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
1117
1118 /* Flow control used only if each channel gets 4KB or more FIFO */
1119 if (rqs >= ((4096 / 256) - 1)) {
1120 u32 rfd, rfa;
1121
1122 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1123 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
1124
1125 /*
1126 * Set Threshold for Activating Flow Contol space for min 2
1127 * frames ie, (1500 * 1) = 1500 bytes.
1128 *
1129 * Set Threshold for Deactivating Flow Contol for space of
1130 * min 1 frame (frame size 1500bytes) in receive fifo
1131 */
1132 if (rqs == ((4096 / 256) - 1)) {
1133 /*
1134 * This violates the above formula because of FIFO size
1135 * limit therefore overflow may occur inspite of this.
1136 */
1137 rfd = 0x3; /* Full-3K */
1138 rfa = 0x1; /* Full-1.5K */
1139 } else if (rqs == ((8192 / 256) - 1)) {
1140 rfd = 0x6; /* Full-4K */
1141 rfa = 0xa; /* Full-6K */
1142 } else if (rqs == ((16384 / 256) - 1)) {
1143 rfd = 0x6; /* Full-4K */
1144 rfa = 0x12; /* Full-10K */
1145 } else {
1146 rfd = 0x6; /* Full-4K */
1147 rfa = 0x1E; /* Full-16K */
1148 }
1149
1150 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1151 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
1152 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1153 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
1154 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
1155 (rfd <<
1156 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1157 (rfa <<
1158 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
1159 }
1160
1161 /* Configure MAC */
1162
1163 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
1164 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
1165 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001166 eqos->config->config_mac <<
Stephen Warren50709602016-10-21 14:46:47 -06001167 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
1168
1169 /* Set TX flow control parameters */
1170 /* Set Pause Time */
1171 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1172 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
1173 /* Assign priority for TX flow control */
1174 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
1175 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
1176 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
1177 /* Assign priority for RX flow control */
1178 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
1179 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
1180 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
1181 /* Enable flow control */
1182 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1183 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
1184 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
1185 EQOS_MAC_RX_FLOW_CTRL_RFE);
1186
1187 clrsetbits_le32(&eqos->mac_regs->configuration,
1188 EQOS_MAC_CONFIGURATION_GPSLCE |
1189 EQOS_MAC_CONFIGURATION_WD |
1190 EQOS_MAC_CONFIGURATION_JD |
1191 EQOS_MAC_CONFIGURATION_JE,
1192 EQOS_MAC_CONFIGURATION_CST |
1193 EQOS_MAC_CONFIGURATION_ACS);
1194
1195 eqos_write_hwaddr(dev);
1196
1197 /* Configure DMA */
1198
1199 /* Enable OSP mode */
1200 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1201 EQOS_DMA_CH0_TX_CONTROL_OSP);
1202
1203 /* RX buffer size. Must be a multiple of bus width */
1204 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1205 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
1206 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
1207 EQOS_MAX_PACKET_SIZE <<
1208 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
1209
1210 setbits_le32(&eqos->dma_regs->ch0_control,
1211 EQOS_DMA_CH0_CONTROL_PBLX8);
1212
1213 /*
1214 * Burst length must be < 1/2 FIFO size.
1215 * FIFO size in tqs is encoded as (n / 256) - 1.
1216 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1217 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1218 */
1219 pbl = tqs + 1;
1220 if (pbl > 32)
1221 pbl = 32;
1222 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1223 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1224 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1225 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1226
1227 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1228 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1229 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1230 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1231
1232 /* DMA performance configuration */
1233 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1234 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1235 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1236 writel(val, &eqos->dma_regs->sysbus_mode);
1237
1238 /* Set up descriptors */
1239
1240 memset(eqos->descs, 0, EQOS_DESCRIPTORS_SIZE);
1241 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
1242 struct eqos_desc *rx_desc = &(eqos->rx_descs[i]);
1243 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1244 (i * EQOS_MAX_PACKET_SIZE));
1245 rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1246 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001247 eqos->config->ops->eqos_flush_desc(eqos->descs);
Stephen Warren50709602016-10-21 14:46:47 -06001248
1249 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
1250 writel((ulong)eqos->tx_descs, &eqos->dma_regs->ch0_txdesc_list_address);
1251 writel(EQOS_DESCRIPTORS_TX - 1,
1252 &eqos->dma_regs->ch0_txdesc_ring_length);
1253
1254 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
1255 writel((ulong)eqos->rx_descs, &eqos->dma_regs->ch0_rxdesc_list_address);
1256 writel(EQOS_DESCRIPTORS_RX - 1,
1257 &eqos->dma_regs->ch0_rxdesc_ring_length);
1258
1259 /* Enable everything */
1260
1261 setbits_le32(&eqos->mac_regs->configuration,
1262 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1263
1264 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1265 EQOS_DMA_CH0_TX_CONTROL_ST);
1266 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1267 EQOS_DMA_CH0_RX_CONTROL_SR);
1268
1269 /* TX tail pointer not written until we need to TX a packet */
1270 /*
1271 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1272 * first descriptor, implying all descriptors were available. However,
1273 * that's not distinguishable from none of the descriptors being
1274 * available.
1275 */
1276 last_rx_desc = (ulong)&(eqos->rx_descs[(EQOS_DESCRIPTORS_RX - 1)]);
1277 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1278
1279 eqos->started = true;
1280
1281 debug("%s: OK\n", __func__);
1282 return 0;
1283
1284err_shutdown_phy:
1285 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001286err_stop_resets:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001287 eqos->config->ops->eqos_stop_resets(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001288err_stop_clks:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001289 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001290err:
Masahiro Yamada81e10422017-09-16 14:10:41 +09001291 pr_err("FAILED: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001292 return ret;
1293}
1294
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001295static void eqos_stop(struct udevice *dev)
Stephen Warren50709602016-10-21 14:46:47 -06001296{
1297 struct eqos_priv *eqos = dev_get_priv(dev);
1298 int i;
1299
1300 debug("%s(dev=%p):\n", __func__, dev);
1301
1302 if (!eqos->started)
1303 return;
1304 eqos->started = false;
1305 eqos->reg_access_ok = false;
1306
1307 /* Disable TX DMA */
1308 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1309 EQOS_DMA_CH0_TX_CONTROL_ST);
1310
1311 /* Wait for TX all packets to drain out of MTL */
1312 for (i = 0; i < 1000000; i++) {
1313 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1314 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1315 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1316 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1317 if ((trcsts != 1) && (!txqsts))
1318 break;
1319 }
1320
1321 /* Turn off MAC TX and RX */
1322 clrbits_le32(&eqos->mac_regs->configuration,
1323 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1324
1325 /* Wait for all RX packets to drain out of MTL */
1326 for (i = 0; i < 1000000; i++) {
1327 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1328 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1329 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1330 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1331 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1332 if ((!prxq) && (!rxqsts))
1333 break;
1334 }
1335
1336 /* Turn off RX DMA */
1337 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1338 EQOS_DMA_CH0_RX_CONTROL_SR);
1339
1340 if (eqos->phy) {
1341 phy_shutdown(eqos->phy);
Stephen Warren50709602016-10-21 14:46:47 -06001342 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001343 eqos->config->ops->eqos_stop_resets(dev);
1344 eqos->config->ops->eqos_stop_clks(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001345
1346 debug("%s: OK\n", __func__);
1347}
1348
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001349static int eqos_send(struct udevice *dev, void *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001350{
1351 struct eqos_priv *eqos = dev_get_priv(dev);
1352 struct eqos_desc *tx_desc;
1353 int i;
1354
1355 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1356 length);
1357
1358 memcpy(eqos->tx_dma_buf, packet, length);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001359 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
Stephen Warren50709602016-10-21 14:46:47 -06001360
1361 tx_desc = &(eqos->tx_descs[eqos->tx_desc_idx]);
1362 eqos->tx_desc_idx++;
1363 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1364
1365 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1366 tx_desc->des1 = 0;
1367 tx_desc->des2 = length;
1368 /*
1369 * Make sure that if HW sees the _OWN write below, it will see all the
1370 * writes to the rest of the descriptor too.
1371 */
1372 mb();
1373 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001374 eqos->config->ops->eqos_flush_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001375
1376 writel((ulong)(tx_desc + 1), &eqos->dma_regs->ch0_txdesc_tail_pointer);
1377
1378 for (i = 0; i < 1000000; i++) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001379 eqos->config->ops->eqos_inval_desc(tx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001380 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1381 return 0;
1382 udelay(1);
1383 }
1384
1385 debug("%s: TX timeout\n", __func__);
1386
1387 return -ETIMEDOUT;
1388}
1389
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001390static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
Stephen Warren50709602016-10-21 14:46:47 -06001391{
1392 struct eqos_priv *eqos = dev_get_priv(dev);
1393 struct eqos_desc *rx_desc;
1394 int length;
1395
1396 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1397
1398 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1399 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1400 debug("%s: RX packet not available\n", __func__);
1401 return -EAGAIN;
1402 }
1403
1404 *packetp = eqos->rx_dma_buf +
1405 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1406 length = rx_desc->des3 & 0x7fff;
1407 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1408
Christophe Roullier6beb7802019-05-17 15:08:44 +02001409 eqos->config->ops->eqos_inval_buffer(*packetp, length);
Stephen Warren50709602016-10-21 14:46:47 -06001410
1411 return length;
1412}
1413
Patrick Delaunay6864a5992019-08-01 11:29:02 +02001414static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
Stephen Warren50709602016-10-21 14:46:47 -06001415{
1416 struct eqos_priv *eqos = dev_get_priv(dev);
1417 uchar *packet_expected;
1418 struct eqos_desc *rx_desc;
1419
1420 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1421
1422 packet_expected = eqos->rx_dma_buf +
1423 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1424 if (packet != packet_expected) {
1425 debug("%s: Unexpected packet (expected %p)\n", __func__,
1426 packet_expected);
1427 return -EINVAL;
1428 }
1429
1430 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1431 rx_desc->des0 = (u32)(ulong)packet;
1432 rx_desc->des1 = 0;
1433 rx_desc->des2 = 0;
1434 /*
1435 * Make sure that if HW sees the _OWN write below, it will see all the
1436 * writes to the rest of the descriptor too.
1437 */
1438 mb();
1439 rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001440 eqos->config->ops->eqos_flush_desc(rx_desc);
Stephen Warren50709602016-10-21 14:46:47 -06001441
1442 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1443
1444 eqos->rx_desc_idx++;
1445 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1446
1447 return 0;
1448}
1449
1450static int eqos_probe_resources_core(struct udevice *dev)
1451{
1452 struct eqos_priv *eqos = dev_get_priv(dev);
1453 int ret;
1454
1455 debug("%s(dev=%p):\n", __func__, dev);
1456
1457 eqos->descs = eqos_alloc_descs(EQOS_DESCRIPTORS_TX +
1458 EQOS_DESCRIPTORS_RX);
1459 if (!eqos->descs) {
1460 debug("%s: eqos_alloc_descs() failed\n", __func__);
1461 ret = -ENOMEM;
1462 goto err;
1463 }
1464 eqos->tx_descs = (struct eqos_desc *)eqos->descs;
1465 eqos->rx_descs = (eqos->tx_descs + EQOS_DESCRIPTORS_TX);
1466 debug("%s: tx_descs=%p, rx_descs=%p\n", __func__, eqos->tx_descs,
1467 eqos->rx_descs);
1468
1469 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1470 if (!eqos->tx_dma_buf) {
1471 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1472 ret = -ENOMEM;
1473 goto err_free_descs;
1474 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001475 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001476
1477 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1478 if (!eqos->rx_dma_buf) {
1479 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1480 ret = -ENOMEM;
1481 goto err_free_tx_dma_buf;
1482 }
Christophe Roullier6beb7802019-05-17 15:08:44 +02001483 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
Stephen Warren50709602016-10-21 14:46:47 -06001484
1485 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1486 if (!eqos->rx_pkt) {
1487 debug("%s: malloc(rx_pkt) failed\n", __func__);
1488 ret = -ENOMEM;
1489 goto err_free_rx_dma_buf;
1490 }
1491 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1492
1493 debug("%s: OK\n", __func__);
1494 return 0;
1495
1496err_free_rx_dma_buf:
1497 free(eqos->rx_dma_buf);
1498err_free_tx_dma_buf:
1499 free(eqos->tx_dma_buf);
1500err_free_descs:
1501 eqos_free_descs(eqos->descs);
1502err:
1503
1504 debug("%s: returns %d\n", __func__, ret);
1505 return ret;
1506}
1507
1508static int eqos_remove_resources_core(struct udevice *dev)
1509{
1510 struct eqos_priv *eqos = dev_get_priv(dev);
1511
1512 debug("%s(dev=%p):\n", __func__, dev);
1513
1514 free(eqos->rx_pkt);
1515 free(eqos->rx_dma_buf);
1516 free(eqos->tx_dma_buf);
1517 eqos_free_descs(eqos->descs);
1518
1519 debug("%s: OK\n", __func__);
1520 return 0;
1521}
1522
1523static int eqos_probe_resources_tegra186(struct udevice *dev)
1524{
1525 struct eqos_priv *eqos = dev_get_priv(dev);
1526 int ret;
1527
1528 debug("%s(dev=%p):\n", __func__, dev);
1529
1530 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1531 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001532 pr_err("reset_get_by_name(rst) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001533 return ret;
1534 }
1535
1536 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1537 &eqos->phy_reset_gpio,
1538 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1539 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001540 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001541 goto err_free_reset_eqos;
1542 }
1543
1544 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1545 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001546 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001547 goto err_free_gpio_phy_reset;
1548 }
1549
1550 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1551 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001552 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001553 goto err_free_clk_slave_bus;
1554 }
1555
1556 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1557 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001558 pr_err("clk_get_by_name(rx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001559 goto err_free_clk_master_bus;
1560 }
1561
1562 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1563 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001564 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001565 goto err_free_clk_rx;
1566 return ret;
1567 }
1568
1569 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1570 if (ret) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001571 pr_err("clk_get_by_name(tx) failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001572 goto err_free_clk_ptp_ref;
1573 }
1574
1575 debug("%s: OK\n", __func__);
1576 return 0;
1577
1578err_free_clk_ptp_ref:
1579 clk_free(&eqos->clk_ptp_ref);
1580err_free_clk_rx:
1581 clk_free(&eqos->clk_rx);
1582err_free_clk_master_bus:
1583 clk_free(&eqos->clk_master_bus);
1584err_free_clk_slave_bus:
1585 clk_free(&eqos->clk_slave_bus);
1586err_free_gpio_phy_reset:
1587 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1588err_free_reset_eqos:
1589 reset_free(&eqos->reset_ctl);
1590
1591 debug("%s: returns %d\n", __func__, ret);
1592 return ret;
1593}
1594
Christophe Roullier6beb7802019-05-17 15:08:44 +02001595/* board-specific Ethernet Interface initializations. */
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001596__weak int board_interface_eth_init(struct udevice *dev,
1597 phy_interface_t interface_type)
Christophe Roullier6beb7802019-05-17 15:08:44 +02001598{
1599 return 0;
1600}
1601
1602static int eqos_probe_resources_stm32(struct udevice *dev)
1603{
1604 struct eqos_priv *eqos = dev_get_priv(dev);
1605 int ret;
1606 phy_interface_t interface;
Christophe Roullier6beb7802019-05-17 15:08:44 +02001607
1608 debug("%s(dev=%p):\n", __func__, dev);
1609
1610 interface = eqos->config->interface(dev);
1611
1612 if (interface == PHY_INTERFACE_MODE_NONE) {
1613 pr_err("Invalid PHY interface\n");
1614 return -EINVAL;
1615 }
1616
Patrick Delaunaybff66f92019-08-01 11:29:03 +02001617 ret = board_interface_eth_init(dev, interface);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001618 if (ret)
1619 return -EINVAL;
1620
1621 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1622 if (ret) {
1623 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1624 goto err_probe;
1625 }
1626
1627 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1628 if (ret) {
1629 pr_err("clk_get_by_name(rx) failed: %d", ret);
1630 goto err_free_clk_master_bus;
1631 }
1632
1633 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1634 if (ret) {
1635 pr_err("clk_get_by_name(tx) failed: %d", ret);
1636 goto err_free_clk_rx;
1637 }
1638
1639 /* Get ETH_CLK clocks (optional) */
1640 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1641 if (ret)
1642 pr_warn("No phy clock provided %d", ret);
1643
1644 debug("%s: OK\n", __func__);
1645 return 0;
1646
1647err_free_clk_rx:
1648 clk_free(&eqos->clk_rx);
1649err_free_clk_master_bus:
1650 clk_free(&eqos->clk_master_bus);
1651err_probe:
1652
1653 debug("%s: returns %d\n", __func__, ret);
1654 return ret;
1655}
1656
1657static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
1658{
1659 const char *phy_mode;
1660 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
1661
1662 debug("%s(dev=%p):\n", __func__, dev);
1663
1664 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1665 NULL);
1666 if (phy_mode)
1667 interface = phy_get_interface_by_name(phy_mode);
1668
1669 return interface;
1670}
1671
1672static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
1673{
1674 return PHY_INTERFACE_MODE_MII;
1675}
1676
Stephen Warren50709602016-10-21 14:46:47 -06001677static int eqos_remove_resources_tegra186(struct udevice *dev)
1678{
1679 struct eqos_priv *eqos = dev_get_priv(dev);
1680
1681 debug("%s(dev=%p):\n", __func__, dev);
1682
1683 clk_free(&eqos->clk_tx);
1684 clk_free(&eqos->clk_ptp_ref);
1685 clk_free(&eqos->clk_rx);
1686 clk_free(&eqos->clk_slave_bus);
1687 clk_free(&eqos->clk_master_bus);
1688 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1689 reset_free(&eqos->reset_ctl);
1690
1691 debug("%s: OK\n", __func__);
1692 return 0;
1693}
1694
Christophe Roullier6beb7802019-05-17 15:08:44 +02001695static int eqos_remove_resources_stm32(struct udevice *dev)
1696{
1697 struct eqos_priv *eqos = dev_get_priv(dev);
1698
1699 debug("%s(dev=%p):\n", __func__, dev);
1700
1701 clk_free(&eqos->clk_tx);
1702 clk_free(&eqos->clk_rx);
1703 clk_free(&eqos->clk_master_bus);
1704 if (clk_valid(&eqos->clk_ck))
1705 clk_free(&eqos->clk_ck);
1706
1707 debug("%s: OK\n", __func__);
1708 return 0;
1709}
1710
Stephen Warren50709602016-10-21 14:46:47 -06001711static int eqos_probe(struct udevice *dev)
1712{
1713 struct eqos_priv *eqos = dev_get_priv(dev);
1714 int ret;
1715
1716 debug("%s(dev=%p):\n", __func__, dev);
1717
1718 eqos->dev = dev;
1719 eqos->config = (void *)dev_get_driver_data(dev);
1720
Simon Glassba1dea42017-05-17 17:18:05 -06001721 eqos->regs = devfdt_get_addr(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001722 if (eqos->regs == FDT_ADDR_T_NONE) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001723 pr_err("devfdt_get_addr() failed");
Stephen Warren50709602016-10-21 14:46:47 -06001724 return -ENODEV;
1725 }
1726 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1727 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1728 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1729 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1730
1731 ret = eqos_probe_resources_core(dev);
1732 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001733 pr_err("eqos_probe_resources_core() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001734 return ret;
1735 }
1736
Christophe Roullier6beb7802019-05-17 15:08:44 +02001737 ret = eqos->config->ops->eqos_probe_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001738 if (ret < 0) {
Christophe Roullier6beb7802019-05-17 15:08:44 +02001739 pr_err("eqos_probe_resources() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001740 goto err_remove_resources_core;
1741 }
1742
1743 eqos->mii = mdio_alloc();
1744 if (!eqos->mii) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001745 pr_err("mdio_alloc() failed");
Christophe Roullier6beb7802019-05-17 15:08:44 +02001746 ret = -ENOMEM;
Stephen Warren50709602016-10-21 14:46:47 -06001747 goto err_remove_resources_tegra;
1748 }
1749 eqos->mii->read = eqos_mdio_read;
1750 eqos->mii->write = eqos_mdio_write;
1751 eqos->mii->priv = eqos;
1752 strcpy(eqos->mii->name, dev->name);
1753
1754 ret = mdio_register(eqos->mii);
1755 if (ret < 0) {
Masahiro Yamada81e10422017-09-16 14:10:41 +09001756 pr_err("mdio_register() failed: %d", ret);
Stephen Warren50709602016-10-21 14:46:47 -06001757 goto err_free_mdio;
1758 }
1759
1760 debug("%s: OK\n", __func__);
1761 return 0;
1762
1763err_free_mdio:
1764 mdio_free(eqos->mii);
1765err_remove_resources_tegra:
Christophe Roullier6beb7802019-05-17 15:08:44 +02001766 eqos->config->ops->eqos_remove_resources(dev);
Stephen Warren50709602016-10-21 14:46:47 -06001767err_remove_resources_core:
1768 eqos_remove_resources_core(dev);
1769
1770 debug("%s: returns %d\n", __func__, ret);
1771 return ret;
1772}
1773
1774static int eqos_remove(struct udevice *dev)
1775{
1776 struct eqos_priv *eqos = dev_get_priv(dev);
1777
1778 debug("%s(dev=%p):\n", __func__, dev);
1779
1780 mdio_unregister(eqos->mii);
1781 mdio_free(eqos->mii);
Christophe Roullier6beb7802019-05-17 15:08:44 +02001782 eqos->config->ops->eqos_remove_resources(dev);
1783
Stephen Warren50709602016-10-21 14:46:47 -06001784 eqos_probe_resources_core(dev);
1785
1786 debug("%s: OK\n", __func__);
1787 return 0;
1788}
1789
1790static const struct eth_ops eqos_ops = {
1791 .start = eqos_start,
1792 .stop = eqos_stop,
1793 .send = eqos_send,
1794 .recv = eqos_recv,
1795 .free_pkt = eqos_free_pkt,
1796 .write_hwaddr = eqos_write_hwaddr,
1797};
1798
Christophe Roullier6beb7802019-05-17 15:08:44 +02001799static struct eqos_ops eqos_tegra186_ops = {
1800 .eqos_inval_desc = eqos_inval_desc_tegra186,
1801 .eqos_flush_desc = eqos_flush_desc_tegra186,
1802 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1803 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1804 .eqos_probe_resources = eqos_probe_resources_tegra186,
1805 .eqos_remove_resources = eqos_remove_resources_tegra186,
1806 .eqos_stop_resets = eqos_stop_resets_tegra186,
1807 .eqos_start_resets = eqos_start_resets_tegra186,
1808 .eqos_stop_clks = eqos_stop_clks_tegra186,
1809 .eqos_start_clks = eqos_start_clks_tegra186,
1810 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1811 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1812 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
1813 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1814};
1815
Stephen Warren50709602016-10-21 14:46:47 -06001816static const struct eqos_config eqos_tegra186_config = {
1817 .reg_access_always_ok = false,
Christophe Roullier6beb7802019-05-17 15:08:44 +02001818 .mdio_wait = 10,
1819 .swr_wait = 10,
1820 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1821 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
1822 .interface = eqos_get_interface_tegra186,
1823 .ops = &eqos_tegra186_ops
1824};
1825
1826static struct eqos_ops eqos_stm32_ops = {
1827 .eqos_inval_desc = eqos_inval_desc_stm32,
1828 .eqos_flush_desc = eqos_flush_desc_stm32,
1829 .eqos_inval_buffer = eqos_inval_buffer_stm32,
1830 .eqos_flush_buffer = eqos_flush_buffer_stm32,
1831 .eqos_probe_resources = eqos_probe_resources_stm32,
1832 .eqos_remove_resources = eqos_remove_resources_stm32,
1833 .eqos_stop_resets = eqos_stop_resets_stm32,
1834 .eqos_start_resets = eqos_start_resets_stm32,
1835 .eqos_stop_clks = eqos_stop_clks_stm32,
1836 .eqos_start_clks = eqos_start_clks_stm32,
1837 .eqos_calibrate_pads = eqos_calibrate_pads_stm32,
1838 .eqos_disable_calibration = eqos_disable_calibration_stm32,
1839 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_stm32,
1840 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1841};
1842
1843static const struct eqos_config eqos_stm32_config = {
1844 .reg_access_always_ok = false,
1845 .mdio_wait = 10000,
1846 .swr_wait = 50,
1847 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1848 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
1849 .interface = eqos_get_interface_stm32,
1850 .ops = &eqos_stm32_ops
Stephen Warren50709602016-10-21 14:46:47 -06001851};
1852
1853static const struct udevice_id eqos_ids[] = {
1854 {
1855 .compatible = "nvidia,tegra186-eqos",
1856 .data = (ulong)&eqos_tegra186_config
1857 },
Christophe Roullier6beb7802019-05-17 15:08:44 +02001858 {
1859 .compatible = "snps,dwmac-4.20a",
1860 .data = (ulong)&eqos_stm32_config
1861 },
1862
Stephen Warren50709602016-10-21 14:46:47 -06001863 { }
1864};
1865
1866U_BOOT_DRIVER(eth_eqos) = {
1867 .name = "eth_eqos",
1868 .id = UCLASS_ETH,
1869 .of_match = eqos_ids,
1870 .probe = eqos_probe,
1871 .remove = eqos_remove,
1872 .ops = &eqos_ops,
1873 .priv_auto_alloc_size = sizeof(struct eqos_priv),
1874 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1875};