1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016, NVIDIA CORPORATION.
5 * Portions based on U-Boot's rtl8169.c.
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.
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
22 * The following configurations are currently supported:
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.
48 #define EQOS_MAC_REGS_BASE 0x000
49 struct eqos_mac_regs {
50 uint32_t configuration; /* 0x000 */
51 uint32_t unused_004[(0x070 - 0x004) / 4]; /* 0x004 */
52 uint32_t q0_tx_flow_ctrl; /* 0x070 */
53 uint32_t unused_070[(0x090 - 0x074) / 4]; /* 0x074 */
54 uint32_t rx_flow_ctrl; /* 0x090 */
55 uint32_t unused_094; /* 0x094 */
56 uint32_t txq_prty_map0; /* 0x098 */
57 uint32_t unused_09c; /* 0x09c */
58 uint32_t rxq_ctrl0; /* 0x0a0 */
59 uint32_t unused_0a4; /* 0x0a4 */
60 uint32_t rxq_ctrl2; /* 0x0a8 */
61 uint32_t unused_0ac[(0x0dc - 0x0ac) / 4]; /* 0x0ac */
62 uint32_t us_tic_counter; /* 0x0dc */
63 uint32_t unused_0e0[(0x11c - 0x0e0) / 4]; /* 0x0e0 */
64 uint32_t hw_feature0; /* 0x11c */
65 uint32_t hw_feature1; /* 0x120 */
66 uint32_t hw_feature2; /* 0x124 */
67 uint32_t unused_128[(0x200 - 0x128) / 4]; /* 0x128 */
68 uint32_t mdio_address; /* 0x200 */
69 uint32_t mdio_data; /* 0x204 */
70 uint32_t unused_208[(0x300 - 0x208) / 4]; /* 0x208 */
71 uint32_t address0_high; /* 0x300 */
72 uint32_t address0_low; /* 0x304 */
75 #define EQOS_MAC_CONFIGURATION_GPSLCE BIT(23)
76 #define EQOS_MAC_CONFIGURATION_CST BIT(21)
77 #define EQOS_MAC_CONFIGURATION_ACS BIT(20)
78 #define EQOS_MAC_CONFIGURATION_WD BIT(19)
79 #define EQOS_MAC_CONFIGURATION_JD BIT(17)
80 #define EQOS_MAC_CONFIGURATION_JE BIT(16)
81 #define EQOS_MAC_CONFIGURATION_PS BIT(15)
82 #define EQOS_MAC_CONFIGURATION_FES BIT(14)
83 #define EQOS_MAC_CONFIGURATION_DM BIT(13)
84 #define EQOS_MAC_CONFIGURATION_TE BIT(1)
85 #define EQOS_MAC_CONFIGURATION_RE BIT(0)
87 #define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT 16
88 #define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_MASK 0xffff
89 #define EQOS_MAC_Q0_TX_FLOW_CTRL_TFE BIT(1)
91 #define EQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
93 #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT 0
94 #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK 0xff
96 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT 0
97 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK 3
98 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_NOT_ENABLED 0
99 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB 2
100 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV 1
102 #define EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT 0
103 #define EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK 0xff
105 #define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT 6
106 #define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK 0x1f
107 #define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT 0
108 #define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK 0x1f
110 #define EQOS_MAC_MDIO_ADDRESS_PA_SHIFT 21
111 #define EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT 16
112 #define EQOS_MAC_MDIO_ADDRESS_CR_SHIFT 8
113 #define EQOS_MAC_MDIO_ADDRESS_CR_20_35 2
114 #define EQOS_MAC_MDIO_ADDRESS_CR_250_300 5
115 #define EQOS_MAC_MDIO_ADDRESS_SKAP BIT(4)
116 #define EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT 2
117 #define EQOS_MAC_MDIO_ADDRESS_GOC_READ 3
118 #define EQOS_MAC_MDIO_ADDRESS_GOC_WRITE 1
119 #define EQOS_MAC_MDIO_ADDRESS_C45E BIT(1)
120 #define EQOS_MAC_MDIO_ADDRESS_GB BIT(0)
122 #define EQOS_MAC_MDIO_DATA_GD_MASK 0xffff
124 #define EQOS_MTL_REGS_BASE 0xd00
125 struct eqos_mtl_regs {
126 uint32_t txq0_operation_mode; /* 0xd00 */
127 uint32_t unused_d04; /* 0xd04 */
128 uint32_t txq0_debug; /* 0xd08 */
129 uint32_t unused_d0c[(0xd18 - 0xd0c) / 4]; /* 0xd0c */
130 uint32_t txq0_quantum_weight; /* 0xd18 */
131 uint32_t unused_d1c[(0xd30 - 0xd1c) / 4]; /* 0xd1c */
132 uint32_t rxq0_operation_mode; /* 0xd30 */
133 uint32_t unused_d34; /* 0xd34 */
134 uint32_t rxq0_debug; /* 0xd38 */
137 #define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT 16
138 #define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK 0x1ff
139 #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT 2
140 #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_MASK 3
141 #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED 2
142 #define EQOS_MTL_TXQ0_OPERATION_MODE_TSF BIT(1)
143 #define EQOS_MTL_TXQ0_OPERATION_MODE_FTQ BIT(0)
145 #define EQOS_MTL_TXQ0_DEBUG_TXQSTS BIT(4)
146 #define EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT 1
147 #define EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK 3
149 #define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT 20
150 #define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK 0x3ff
151 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT 14
152 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK 0x3f
153 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT 8
154 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK 0x3f
155 #define EQOS_MTL_RXQ0_OPERATION_MODE_EHFC BIT(7)
156 #define EQOS_MTL_RXQ0_OPERATION_MODE_RSF BIT(5)
158 #define EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT 16
159 #define EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK 0x7fff
160 #define EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT 4
161 #define EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK 3
163 #define EQOS_DMA_REGS_BASE 0x1000
164 struct eqos_dma_regs {
165 uint32_t mode; /* 0x1000 */
166 uint32_t sysbus_mode; /* 0x1004 */
167 uint32_t unused_1008[(0x1100 - 0x1008) / 4]; /* 0x1008 */
168 uint32_t ch0_control; /* 0x1100 */
169 uint32_t ch0_tx_control; /* 0x1104 */
170 uint32_t ch0_rx_control; /* 0x1108 */
171 uint32_t unused_110c; /* 0x110c */
172 uint32_t ch0_txdesc_list_haddress; /* 0x1110 */
173 uint32_t ch0_txdesc_list_address; /* 0x1114 */
174 uint32_t ch0_rxdesc_list_haddress; /* 0x1118 */
175 uint32_t ch0_rxdesc_list_address; /* 0x111c */
176 uint32_t ch0_txdesc_tail_pointer; /* 0x1120 */
177 uint32_t unused_1124; /* 0x1124 */
178 uint32_t ch0_rxdesc_tail_pointer; /* 0x1128 */
179 uint32_t ch0_txdesc_ring_length; /* 0x112c */
180 uint32_t ch0_rxdesc_ring_length; /* 0x1130 */
183 #define EQOS_DMA_MODE_SWR BIT(0)
185 #define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT 16
186 #define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK 0xf
187 #define EQOS_DMA_SYSBUS_MODE_EAME BIT(11)
188 #define EQOS_DMA_SYSBUS_MODE_BLEN16 BIT(3)
189 #define EQOS_DMA_SYSBUS_MODE_BLEN8 BIT(2)
190 #define EQOS_DMA_SYSBUS_MODE_BLEN4 BIT(1)
192 #define EQOS_DMA_CH0_CONTROL_PBLX8 BIT(16)
194 #define EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT 16
195 #define EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK 0x3f
196 #define EQOS_DMA_CH0_TX_CONTROL_OSP BIT(4)
197 #define EQOS_DMA_CH0_TX_CONTROL_ST BIT(0)
199 #define EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT 16
200 #define EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK 0x3f
201 #define EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT 1
202 #define EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK 0x3fff
203 #define EQOS_DMA_CH0_RX_CONTROL_SR BIT(0)
205 /* These registers are Tegra186-specific */
206 #define EQOS_TEGRA186_REGS_BASE 0x8800
207 struct eqos_tegra186_regs {
208 uint32_t sdmemcomppadctrl; /* 0x8800 */
209 uint32_t auto_cal_config; /* 0x8804 */
210 uint32_t unused_8808; /* 0x8808 */
211 uint32_t auto_cal_status; /* 0x880c */
214 #define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
216 #define EQOS_AUTO_CAL_CONFIG_START BIT(31)
217 #define EQOS_AUTO_CAL_CONFIG_ENABLE BIT(29)
219 #define EQOS_AUTO_CAL_STATUS_ACTIVE BIT(31)
223 #define EQOS_DESCRIPTOR_WORDS 4
224 #define EQOS_DESCRIPTOR_SIZE (EQOS_DESCRIPTOR_WORDS * 4)
225 /* We assume ARCH_DMA_MINALIGN >= 16; 16 is the EQOS HW minimum */
226 #define EQOS_DESCRIPTOR_ALIGN ARCH_DMA_MINALIGN
227 #define EQOS_DESCRIPTORS_TX 4
228 #define EQOS_DESCRIPTORS_RX 4
229 #define EQOS_DESCRIPTORS_NUM (EQOS_DESCRIPTORS_TX + EQOS_DESCRIPTORS_RX)
230 #define EQOS_DESCRIPTORS_SIZE ALIGN(EQOS_DESCRIPTORS_NUM * \
231 EQOS_DESCRIPTOR_SIZE, ARCH_DMA_MINALIGN)
232 #define EQOS_BUFFER_ALIGN ARCH_DMA_MINALIGN
233 #define EQOS_MAX_PACKET_SIZE ALIGN(1568, ARCH_DMA_MINALIGN)
234 #define EQOS_RX_BUFFER_SIZE (EQOS_DESCRIPTORS_RX * EQOS_MAX_PACKET_SIZE)
237 * Warn if the cache-line size is larger than the descriptor size. In such
238 * cases the driver will likely fail because the CPU needs to flush the cache
239 * when requeuing RX buffers, therefore descriptors written by the hardware
240 * may be discarded. Architectures with full IO coherence, such as x86, do not
241 * experience this issue, and hence are excluded from this condition.
243 * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
244 * the driver to allocate descriptors from a pool of non-cached memory.
246 #if EQOS_DESCRIPTOR_SIZE < ARCH_DMA_MINALIGN
247 #if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
248 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86)
249 #warning Cache line size is larger than descriptor size
260 #define EQOS_DESC3_OWN BIT(31)
261 #define EQOS_DESC3_FD BIT(29)
262 #define EQOS_DESC3_LD BIT(28)
263 #define EQOS_DESC3_BUF1V BIT(24)
266 bool reg_access_always_ok;
271 phy_interface_t (*interface)(struct udevice *dev);
272 struct eqos_ops *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);
294 const struct eqos_config *config;
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;
304 struct clk clk_ptp_ref;
307 struct clk clk_slave_bus;
309 struct phy_device *phy;
313 struct eqos_desc *tx_descs;
314 struct eqos_desc *rx_descs;
315 int tx_desc_idx, rx_desc_idx;
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.
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.
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.
341 static void *eqos_alloc_descs(unsigned int num)
343 #ifdef CONFIG_SYS_NONCACHED_MEMORY
344 return (void *)noncached_alloc(EQOS_DESCRIPTORS_SIZE,
345 EQOS_DESCRIPTOR_ALIGN);
347 return memalign(EQOS_DESCRIPTOR_ALIGN, EQOS_DESCRIPTORS_SIZE);
351 static void eqos_free_descs(void *descs)
353 #ifdef CONFIG_SYS_NONCACHED_MEMORY
354 /* FIXME: noncached_alloc() has no opposite */
360 static void eqos_inval_desc_tegra186(void *desc)
362 #ifndef CONFIG_SYS_NONCACHED_MEMORY
363 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
364 unsigned long end = ALIGN(start + EQOS_DESCRIPTOR_SIZE,
367 invalidate_dcache_range(start, end);
371 static void eqos_inval_desc_stm32(void *desc)
373 #ifndef CONFIG_SYS_NONCACHED_MEMORY
374 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
375 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
378 invalidate_dcache_range(start, end);
382 static void eqos_flush_desc_tegra186(void *desc)
384 #ifndef CONFIG_SYS_NONCACHED_MEMORY
385 flush_cache((unsigned long)desc, EQOS_DESCRIPTOR_SIZE);
389 static void eqos_flush_desc_stm32(void *desc)
391 #ifndef CONFIG_SYS_NONCACHED_MEMORY
392 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
393 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
396 flush_dcache_range(start, end);
400 static void eqos_inval_buffer_tegra186(void *buf, size_t size)
402 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
403 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
405 invalidate_dcache_range(start, end);
408 static void eqos_inval_buffer_stm32(void *buf, size_t size)
410 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
411 unsigned long end = roundup((unsigned long)buf + size,
414 invalidate_dcache_range(start, end);
417 static void eqos_flush_buffer_tegra186(void *buf, size_t size)
419 flush_cache((unsigned long)buf, size);
422 static void eqos_flush_buffer_stm32(void *buf, size_t size)
424 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
425 unsigned long end = roundup((unsigned long)buf + size,
428 flush_dcache_range(start, end);
431 static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
433 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
434 EQOS_MAC_MDIO_ADDRESS_GB, false,
438 static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
441 struct eqos_priv *eqos = bus->priv;
445 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
448 ret = eqos_mdio_wait_idle(eqos);
450 pr_err("MDIO not idle at entry");
454 val = readl(&eqos->mac_regs->mdio_address);
455 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
456 EQOS_MAC_MDIO_ADDRESS_C45E;
457 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
458 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
459 (eqos->config->config_mac_mdio <<
460 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
461 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
462 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
463 EQOS_MAC_MDIO_ADDRESS_GB;
464 writel(val, &eqos->mac_regs->mdio_address);
466 udelay(eqos->config->mdio_wait);
468 ret = eqos_mdio_wait_idle(eqos);
470 pr_err("MDIO read didn't complete");
474 val = readl(&eqos->mac_regs->mdio_data);
475 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
477 debug("%s: val=%x\n", __func__, val);
482 static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
483 int mdio_reg, u16 mdio_val)
485 struct eqos_priv *eqos = bus->priv;
489 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
490 mdio_addr, mdio_reg, mdio_val);
492 ret = eqos_mdio_wait_idle(eqos);
494 pr_err("MDIO not idle at entry");
498 writel(mdio_val, &eqos->mac_regs->mdio_data);
500 val = readl(&eqos->mac_regs->mdio_address);
501 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
502 EQOS_MAC_MDIO_ADDRESS_C45E;
503 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
504 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
505 (eqos->config->config_mac_mdio <<
506 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
507 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
508 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
509 EQOS_MAC_MDIO_ADDRESS_GB;
510 writel(val, &eqos->mac_regs->mdio_address);
512 udelay(eqos->config->mdio_wait);
514 ret = eqos_mdio_wait_idle(eqos);
516 pr_err("MDIO read didn't complete");
523 static int eqos_start_clks_tegra186(struct udevice *dev)
525 struct eqos_priv *eqos = dev_get_priv(dev);
528 debug("%s(dev=%p):\n", __func__, dev);
530 ret = clk_enable(&eqos->clk_slave_bus);
532 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
536 ret = clk_enable(&eqos->clk_master_bus);
538 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
539 goto err_disable_clk_slave_bus;
542 ret = clk_enable(&eqos->clk_rx);
544 pr_err("clk_enable(clk_rx) failed: %d", ret);
545 goto err_disable_clk_master_bus;
548 ret = clk_enable(&eqos->clk_ptp_ref);
550 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
551 goto err_disable_clk_rx;
554 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
556 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
557 goto err_disable_clk_ptp_ref;
560 ret = clk_enable(&eqos->clk_tx);
562 pr_err("clk_enable(clk_tx) failed: %d", ret);
563 goto err_disable_clk_ptp_ref;
566 debug("%s: OK\n", __func__);
569 err_disable_clk_ptp_ref:
570 clk_disable(&eqos->clk_ptp_ref);
572 clk_disable(&eqos->clk_rx);
573 err_disable_clk_master_bus:
574 clk_disable(&eqos->clk_master_bus);
575 err_disable_clk_slave_bus:
576 clk_disable(&eqos->clk_slave_bus);
578 debug("%s: FAILED: %d\n", __func__, ret);
582 static int eqos_start_clks_stm32(struct udevice *dev)
584 struct eqos_priv *eqos = dev_get_priv(dev);
587 debug("%s(dev=%p):\n", __func__, dev);
589 ret = clk_enable(&eqos->clk_master_bus);
591 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
595 ret = clk_enable(&eqos->clk_rx);
597 pr_err("clk_enable(clk_rx) failed: %d", ret);
598 goto err_disable_clk_master_bus;
601 ret = clk_enable(&eqos->clk_tx);
603 pr_err("clk_enable(clk_tx) failed: %d", ret);
604 goto err_disable_clk_rx;
607 if (clk_valid(&eqos->clk_ck)) {
608 ret = clk_enable(&eqos->clk_ck);
610 pr_err("clk_enable(clk_ck) failed: %d", ret);
611 goto err_disable_clk_tx;
615 debug("%s: OK\n", __func__);
619 clk_disable(&eqos->clk_tx);
621 clk_disable(&eqos->clk_rx);
622 err_disable_clk_master_bus:
623 clk_disable(&eqos->clk_master_bus);
625 debug("%s: FAILED: %d\n", __func__, ret);
629 static void eqos_stop_clks_tegra186(struct udevice *dev)
631 struct eqos_priv *eqos = dev_get_priv(dev);
633 debug("%s(dev=%p):\n", __func__, dev);
635 clk_disable(&eqos->clk_tx);
636 clk_disable(&eqos->clk_ptp_ref);
637 clk_disable(&eqos->clk_rx);
638 clk_disable(&eqos->clk_master_bus);
639 clk_disable(&eqos->clk_slave_bus);
641 debug("%s: OK\n", __func__);
644 static void eqos_stop_clks_stm32(struct udevice *dev)
646 struct eqos_priv *eqos = dev_get_priv(dev);
648 debug("%s(dev=%p):\n", __func__, dev);
650 clk_disable(&eqos->clk_tx);
651 clk_disable(&eqos->clk_rx);
652 clk_disable(&eqos->clk_master_bus);
653 if (clk_valid(&eqos->clk_ck))
654 clk_disable(&eqos->clk_ck);
656 debug("%s: OK\n", __func__);
659 static int eqos_start_resets_tegra186(struct udevice *dev)
661 struct eqos_priv *eqos = dev_get_priv(dev);
664 debug("%s(dev=%p):\n", __func__, dev);
666 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
668 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
674 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
676 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
680 ret = reset_assert(&eqos->reset_ctl);
682 pr_err("reset_assert() failed: %d", ret);
688 ret = reset_deassert(&eqos->reset_ctl);
690 pr_err("reset_deassert() failed: %d", ret);
694 debug("%s: OK\n", __func__);
698 static int eqos_start_resets_stm32(struct udevice *dev)
700 struct eqos_priv *eqos = dev_get_priv(dev);
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);
707 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d",
714 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
716 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d",
721 debug("%s: OK\n", __func__);
726 static int eqos_stop_resets_tegra186(struct udevice *dev)
728 struct eqos_priv *eqos = dev_get_priv(dev);
730 reset_assert(&eqos->reset_ctl);
731 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
736 static int eqos_stop_resets_stm32(struct udevice *dev)
738 struct eqos_priv *eqos = dev_get_priv(dev);
741 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
742 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
744 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d",
753 static int eqos_calibrate_pads_tegra186(struct udevice *dev)
755 struct eqos_priv *eqos = dev_get_priv(dev);
758 debug("%s(dev=%p):\n", __func__, dev);
760 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
761 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
765 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
766 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
768 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
769 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
771 pr_err("calibrate didn't start");
775 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
776 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
778 pr_err("calibrate didn't finish");
785 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
786 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
788 debug("%s: returns %d\n", __func__, ret);
793 static int eqos_disable_calibration_tegra186(struct udevice *dev)
795 struct eqos_priv *eqos = dev_get_priv(dev);
797 debug("%s(dev=%p):\n", __func__, dev);
799 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
800 EQOS_AUTO_CAL_CONFIG_ENABLE);
805 static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
807 struct eqos_priv *eqos = dev_get_priv(dev);
809 return clk_get_rate(&eqos->clk_slave_bus);
812 static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
814 struct eqos_priv *eqos = dev_get_priv(dev);
816 return clk_get_rate(&eqos->clk_master_bus);
819 static int eqos_calibrate_pads_stm32(struct udevice *dev)
824 static int eqos_disable_calibration_stm32(struct udevice *dev)
829 static int eqos_set_full_duplex(struct udevice *dev)
831 struct eqos_priv *eqos = dev_get_priv(dev);
833 debug("%s(dev=%p):\n", __func__, dev);
835 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
840 static int eqos_set_half_duplex(struct udevice *dev)
842 struct eqos_priv *eqos = dev_get_priv(dev);
844 debug("%s(dev=%p):\n", __func__, dev);
846 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
848 /* WAR: Flush TX queue when switching to half-duplex */
849 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
850 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
855 static int eqos_set_gmii_speed(struct udevice *dev)
857 struct eqos_priv *eqos = dev_get_priv(dev);
859 debug("%s(dev=%p):\n", __func__, dev);
861 clrbits_le32(&eqos->mac_regs->configuration,
862 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
867 static int eqos_set_mii_speed_100(struct udevice *dev)
869 struct eqos_priv *eqos = dev_get_priv(dev);
871 debug("%s(dev=%p):\n", __func__, dev);
873 setbits_le32(&eqos->mac_regs->configuration,
874 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
879 static int eqos_set_mii_speed_10(struct udevice *dev)
881 struct eqos_priv *eqos = dev_get_priv(dev);
883 debug("%s(dev=%p):\n", __func__, dev);
885 clrsetbits_le32(&eqos->mac_regs->configuration,
886 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
891 static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
893 struct eqos_priv *eqos = dev_get_priv(dev);
897 debug("%s(dev=%p):\n", __func__, dev);
899 switch (eqos->phy->speed) {
901 rate = 125 * 1000 * 1000;
904 rate = 25 * 1000 * 1000;
907 rate = 2.5 * 1000 * 1000;
910 pr_err("invalid speed %d", eqos->phy->speed);
914 ret = clk_set_rate(&eqos->clk_tx, rate);
916 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
923 static int eqos_set_tx_clk_speed_stm32(struct udevice *dev)
928 static int eqos_adjust_link(struct udevice *dev)
930 struct eqos_priv *eqos = dev_get_priv(dev);
934 debug("%s(dev=%p):\n", __func__, dev);
936 if (eqos->phy->duplex)
937 ret = eqos_set_full_duplex(dev);
939 ret = eqos_set_half_duplex(dev);
941 pr_err("eqos_set_*_duplex() failed: %d", ret);
945 switch (eqos->phy->speed) {
947 en_calibration = true;
948 ret = eqos_set_gmii_speed(dev);
951 en_calibration = true;
952 ret = eqos_set_mii_speed_100(dev);
955 en_calibration = false;
956 ret = eqos_set_mii_speed_10(dev);
959 pr_err("invalid speed %d", eqos->phy->speed);
963 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
967 if (en_calibration) {
968 ret = eqos->config->ops->eqos_calibrate_pads(dev);
970 pr_err("eqos_calibrate_pads() failed: %d",
975 ret = eqos->config->ops->eqos_disable_calibration(dev);
977 pr_err("eqos_disable_calibration() failed: %d",
982 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
984 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
991 static int eqos_write_hwaddr(struct udevice *dev)
993 struct eth_pdata *plat = dev_get_platdata(dev);
994 struct eqos_priv *eqos = dev_get_priv(dev);
998 * This function may be called before start() or after stop(). At that
999 * time, on at least some configurations of the EQoS HW, all clocks to
1000 * the EQoS HW block will be stopped, and a reset signal applied. If
1001 * any register access is attempted in this state, bus timeouts or CPU
1002 * hangs may occur. This check prevents that.
1004 * A simple solution to this problem would be to not implement
1005 * write_hwaddr(), since start() always writes the MAC address into HW
1006 * anyway. However, it is desirable to implement write_hwaddr() to
1007 * support the case of SW that runs subsequent to U-Boot which expects
1008 * the MAC address to already be programmed into the EQoS registers,
1009 * which must happen irrespective of whether the U-Boot user (or
1010 * scripts) actually made use of the EQoS device, and hence
1011 * irrespective of whether start() was ever called.
1013 * Note that this requirement by subsequent SW is not valid for
1014 * Tegra186, and is likely not valid for any non-PCI instantiation of
1015 * the EQoS HW block. This function is implemented solely as
1016 * future-proofing with the expectation the driver will eventually be
1017 * ported to some system where the expectation above is true.
1019 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
1022 /* Update the MAC address */
1023 val = (plat->enetaddr[5] << 8) |
1024 (plat->enetaddr[4]);
1025 writel(val, &eqos->mac_regs->address0_high);
1026 val = (plat->enetaddr[3] << 24) |
1027 (plat->enetaddr[2] << 16) |
1028 (plat->enetaddr[1] << 8) |
1029 (plat->enetaddr[0]);
1030 writel(val, &eqos->mac_regs->address0_low);
1035 static int eqos_start(struct udevice *dev)
1037 struct eqos_priv *eqos = dev_get_priv(dev);
1040 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
1043 debug("%s(dev=%p):\n", __func__, dev);
1045 eqos->tx_desc_idx = 0;
1046 eqos->rx_desc_idx = 0;
1048 ret = eqos->config->ops->eqos_start_clks(dev);
1050 pr_err("eqos_start_clks() failed: %d", ret);
1054 ret = eqos->config->ops->eqos_start_resets(dev);
1056 pr_err("eqos_start_resets() failed: %d", ret);
1062 eqos->reg_access_ok = true;
1064 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
1065 EQOS_DMA_MODE_SWR, false,
1066 eqos->config->swr_wait, false);
1068 pr_err("EQOS_DMA_MODE_SWR stuck");
1069 goto err_stop_resets;
1072 ret = eqos->config->ops->eqos_calibrate_pads(dev);
1074 pr_err("eqos_calibrate_pads() failed: %d", ret);
1075 goto err_stop_resets;
1077 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
1079 val = (rate / 1000000) - 1;
1080 writel(val, &eqos->mac_regs->us_tic_counter);
1083 * if PHY was already connected and configured,
1084 * don't need to reconnect/reconfigure again
1088 #ifdef CONFIG_DM_ETH_PHY
1089 addr = eth_phy_get_addr(dev);
1091 #ifdef DWC_NET_PHYADDR
1092 addr = DWC_NET_PHYADDR;
1094 eqos->phy = phy_connect(eqos->mii, addr, dev,
1095 eqos->config->interface(dev));
1097 pr_err("phy_connect() failed");
1098 goto err_stop_resets;
1101 if (eqos->max_speed) {
1102 ret = phy_set_supported(eqos->phy, eqos->max_speed);
1104 pr_err("phy_set_supported() failed: %d", ret);
1105 goto err_shutdown_phy;
1109 ret = phy_config(eqos->phy);
1111 pr_err("phy_config() failed: %d", ret);
1112 goto err_shutdown_phy;
1116 ret = phy_startup(eqos->phy);
1118 pr_err("phy_startup() failed: %d", ret);
1119 goto err_shutdown_phy;
1122 if (!eqos->phy->link) {
1124 goto err_shutdown_phy;
1127 ret = eqos_adjust_link(dev);
1129 pr_err("eqos_adjust_link() failed: %d", ret);
1130 goto err_shutdown_phy;
1135 /* Enable Store and Forward mode for TX */
1136 /* Program Tx operating mode */
1137 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1138 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
1139 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
1140 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
1142 /* Transmit Queue weight */
1143 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
1145 /* Enable Store and Forward mode for RX, since no jumbo frame */
1146 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1147 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
1149 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
1150 val = readl(&eqos->mac_regs->hw_feature1);
1151 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
1152 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
1153 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
1154 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
1157 * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
1158 * r/tqs is encoded as (n / 256) - 1.
1160 tqs = (128 << tx_fifo_sz) / 256 - 1;
1161 rqs = (128 << rx_fifo_sz) / 256 - 1;
1163 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1164 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
1165 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
1166 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
1167 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1168 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
1169 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
1170 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
1172 /* Flow control used only if each channel gets 4KB or more FIFO */
1173 if (rqs >= ((4096 / 256) - 1)) {
1176 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1177 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
1180 * Set Threshold for Activating Flow Contol space for min 2
1181 * frames ie, (1500 * 1) = 1500 bytes.
1183 * Set Threshold for Deactivating Flow Contol for space of
1184 * min 1 frame (frame size 1500bytes) in receive fifo
1186 if (rqs == ((4096 / 256) - 1)) {
1188 * This violates the above formula because of FIFO size
1189 * limit therefore overflow may occur inspite of this.
1191 rfd = 0x3; /* Full-3K */
1192 rfa = 0x1; /* Full-1.5K */
1193 } else if (rqs == ((8192 / 256) - 1)) {
1194 rfd = 0x6; /* Full-4K */
1195 rfa = 0xa; /* Full-6K */
1196 } else if (rqs == ((16384 / 256) - 1)) {
1197 rfd = 0x6; /* Full-4K */
1198 rfa = 0x12; /* Full-10K */
1200 rfd = 0x6; /* Full-4K */
1201 rfa = 0x1E; /* Full-16K */
1204 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1205 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
1206 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1207 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
1208 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
1210 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1212 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
1217 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
1218 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
1219 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
1220 eqos->config->config_mac <<
1221 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
1223 /* Set TX flow control parameters */
1224 /* Set Pause Time */
1225 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1226 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
1227 /* Assign priority for TX flow control */
1228 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
1229 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
1230 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
1231 /* Assign priority for RX flow control */
1232 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
1233 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
1234 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
1235 /* Enable flow control */
1236 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1237 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
1238 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
1239 EQOS_MAC_RX_FLOW_CTRL_RFE);
1241 clrsetbits_le32(&eqos->mac_regs->configuration,
1242 EQOS_MAC_CONFIGURATION_GPSLCE |
1243 EQOS_MAC_CONFIGURATION_WD |
1244 EQOS_MAC_CONFIGURATION_JD |
1245 EQOS_MAC_CONFIGURATION_JE,
1246 EQOS_MAC_CONFIGURATION_CST |
1247 EQOS_MAC_CONFIGURATION_ACS);
1249 eqos_write_hwaddr(dev);
1253 /* Enable OSP mode */
1254 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1255 EQOS_DMA_CH0_TX_CONTROL_OSP);
1257 /* RX buffer size. Must be a multiple of bus width */
1258 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1259 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
1260 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
1261 EQOS_MAX_PACKET_SIZE <<
1262 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
1264 setbits_le32(&eqos->dma_regs->ch0_control,
1265 EQOS_DMA_CH0_CONTROL_PBLX8);
1268 * Burst length must be < 1/2 FIFO size.
1269 * FIFO size in tqs is encoded as (n / 256) - 1.
1270 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1271 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1276 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1277 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1278 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1279 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1281 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1282 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1283 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1284 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1286 /* DMA performance configuration */
1287 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1288 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1289 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1290 writel(val, &eqos->dma_regs->sysbus_mode);
1292 /* Set up descriptors */
1294 memset(eqos->descs, 0, EQOS_DESCRIPTORS_SIZE);
1295 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
1296 struct eqos_desc *rx_desc = &(eqos->rx_descs[i]);
1297 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1298 (i * EQOS_MAX_PACKET_SIZE));
1299 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1300 eqos->config->ops->eqos_flush_desc(rx_desc);
1303 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
1304 writel((ulong)eqos->tx_descs, &eqos->dma_regs->ch0_txdesc_list_address);
1305 writel(EQOS_DESCRIPTORS_TX - 1,
1306 &eqos->dma_regs->ch0_txdesc_ring_length);
1308 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
1309 writel((ulong)eqos->rx_descs, &eqos->dma_regs->ch0_rxdesc_list_address);
1310 writel(EQOS_DESCRIPTORS_RX - 1,
1311 &eqos->dma_regs->ch0_rxdesc_ring_length);
1313 /* Enable everything */
1315 setbits_le32(&eqos->mac_regs->configuration,
1316 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1318 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1319 EQOS_DMA_CH0_TX_CONTROL_ST);
1320 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1321 EQOS_DMA_CH0_RX_CONTROL_SR);
1323 /* TX tail pointer not written until we need to TX a packet */
1325 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1326 * first descriptor, implying all descriptors were available. However,
1327 * that's not distinguishable from none of the descriptors being
1330 last_rx_desc = (ulong)&(eqos->rx_descs[(EQOS_DESCRIPTORS_RX - 1)]);
1331 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1333 eqos->started = true;
1335 debug("%s: OK\n", __func__);
1339 phy_shutdown(eqos->phy);
1341 eqos->config->ops->eqos_stop_resets(dev);
1343 eqos->config->ops->eqos_stop_clks(dev);
1345 pr_err("FAILED: %d", ret);
1349 static void eqos_stop(struct udevice *dev)
1351 struct eqos_priv *eqos = dev_get_priv(dev);
1354 debug("%s(dev=%p):\n", __func__, dev);
1358 eqos->started = false;
1359 eqos->reg_access_ok = false;
1361 /* Disable TX DMA */
1362 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1363 EQOS_DMA_CH0_TX_CONTROL_ST);
1365 /* Wait for TX all packets to drain out of MTL */
1366 for (i = 0; i < 1000000; i++) {
1367 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1368 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1369 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1370 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1371 if ((trcsts != 1) && (!txqsts))
1375 /* Turn off MAC TX and RX */
1376 clrbits_le32(&eqos->mac_regs->configuration,
1377 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1379 /* Wait for all RX packets to drain out of MTL */
1380 for (i = 0; i < 1000000; i++) {
1381 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1382 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1383 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1384 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1385 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1386 if ((!prxq) && (!rxqsts))
1390 /* Turn off RX DMA */
1391 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1392 EQOS_DMA_CH0_RX_CONTROL_SR);
1395 phy_shutdown(eqos->phy);
1397 eqos->config->ops->eqos_stop_resets(dev);
1398 eqos->config->ops->eqos_stop_clks(dev);
1400 debug("%s: OK\n", __func__);
1403 static int eqos_send(struct udevice *dev, void *packet, int length)
1405 struct eqos_priv *eqos = dev_get_priv(dev);
1406 struct eqos_desc *tx_desc;
1409 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1412 memcpy(eqos->tx_dma_buf, packet, length);
1413 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
1415 tx_desc = &(eqos->tx_descs[eqos->tx_desc_idx]);
1416 eqos->tx_desc_idx++;
1417 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1419 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1421 tx_desc->des2 = length;
1423 * Make sure that if HW sees the _OWN write below, it will see all the
1424 * writes to the rest of the descriptor too.
1427 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
1428 eqos->config->ops->eqos_flush_desc(tx_desc);
1430 writel((ulong)(&(eqos->tx_descs[eqos->tx_desc_idx])),
1431 &eqos->dma_regs->ch0_txdesc_tail_pointer);
1433 for (i = 0; i < 1000000; i++) {
1434 eqos->config->ops->eqos_inval_desc(tx_desc);
1435 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1440 debug("%s: TX timeout\n", __func__);
1445 static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
1447 struct eqos_priv *eqos = dev_get_priv(dev);
1448 struct eqos_desc *rx_desc;
1451 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1453 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1454 eqos->config->ops->eqos_inval_desc(rx_desc);
1455 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1456 debug("%s: RX packet not available\n", __func__);
1460 *packetp = eqos->rx_dma_buf +
1461 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1462 length = rx_desc->des3 & 0x7fff;
1463 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1465 eqos->config->ops->eqos_inval_buffer(*packetp, length);
1470 static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
1472 struct eqos_priv *eqos = dev_get_priv(dev);
1473 uchar *packet_expected;
1474 struct eqos_desc *rx_desc;
1476 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1478 packet_expected = eqos->rx_dma_buf +
1479 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1480 if (packet != packet_expected) {
1481 debug("%s: Unexpected packet (expected %p)\n", __func__,
1486 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1490 eqos->config->ops->eqos_flush_desc(rx_desc);
1491 eqos->config->ops->eqos_inval_buffer(packet, length);
1492 rx_desc->des0 = (u32)(ulong)packet;
1496 * Make sure that if HW sees the _OWN write below, it will see all the
1497 * writes to the rest of the descriptor too.
1500 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1501 eqos->config->ops->eqos_flush_desc(rx_desc);
1503 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1505 eqos->rx_desc_idx++;
1506 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1511 static int eqos_probe_resources_core(struct udevice *dev)
1513 struct eqos_priv *eqos = dev_get_priv(dev);
1516 debug("%s(dev=%p):\n", __func__, dev);
1518 eqos->descs = eqos_alloc_descs(EQOS_DESCRIPTORS_TX +
1519 EQOS_DESCRIPTORS_RX);
1521 debug("%s: eqos_alloc_descs() failed\n", __func__);
1525 eqos->tx_descs = (struct eqos_desc *)eqos->descs;
1526 eqos->rx_descs = (eqos->tx_descs + EQOS_DESCRIPTORS_TX);
1527 debug("%s: tx_descs=%p, rx_descs=%p\n", __func__, eqos->tx_descs,
1530 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1531 if (!eqos->tx_dma_buf) {
1532 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1534 goto err_free_descs;
1536 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
1538 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1539 if (!eqos->rx_dma_buf) {
1540 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1542 goto err_free_tx_dma_buf;
1544 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
1546 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1547 if (!eqos->rx_pkt) {
1548 debug("%s: malloc(rx_pkt) failed\n", __func__);
1550 goto err_free_rx_dma_buf;
1552 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1554 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1555 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1557 debug("%s: OK\n", __func__);
1560 err_free_rx_dma_buf:
1561 free(eqos->rx_dma_buf);
1562 err_free_tx_dma_buf:
1563 free(eqos->tx_dma_buf);
1565 eqos_free_descs(eqos->descs);
1568 debug("%s: returns %d\n", __func__, ret);
1572 static int eqos_remove_resources_core(struct udevice *dev)
1574 struct eqos_priv *eqos = dev_get_priv(dev);
1576 debug("%s(dev=%p):\n", __func__, dev);
1579 free(eqos->rx_dma_buf);
1580 free(eqos->tx_dma_buf);
1581 eqos_free_descs(eqos->descs);
1583 debug("%s: OK\n", __func__);
1587 static int eqos_probe_resources_tegra186(struct udevice *dev)
1589 struct eqos_priv *eqos = dev_get_priv(dev);
1592 debug("%s(dev=%p):\n", __func__, dev);
1594 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1596 pr_err("reset_get_by_name(rst) failed: %d", ret);
1600 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1601 &eqos->phy_reset_gpio,
1602 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1604 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
1605 goto err_free_reset_eqos;
1608 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1610 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
1611 goto err_free_gpio_phy_reset;
1614 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1616 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1617 goto err_free_clk_slave_bus;
1620 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1622 pr_err("clk_get_by_name(rx) failed: %d", ret);
1623 goto err_free_clk_master_bus;
1626 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1628 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
1629 goto err_free_clk_rx;
1633 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1635 pr_err("clk_get_by_name(tx) failed: %d", ret);
1636 goto err_free_clk_ptp_ref;
1639 debug("%s: OK\n", __func__);
1642 err_free_clk_ptp_ref:
1643 clk_free(&eqos->clk_ptp_ref);
1645 clk_free(&eqos->clk_rx);
1646 err_free_clk_master_bus:
1647 clk_free(&eqos->clk_master_bus);
1648 err_free_clk_slave_bus:
1649 clk_free(&eqos->clk_slave_bus);
1650 err_free_gpio_phy_reset:
1651 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1652 err_free_reset_eqos:
1653 reset_free(&eqos->reset_ctl);
1655 debug("%s: returns %d\n", __func__, ret);
1659 /* board-specific Ethernet Interface initializations. */
1660 __weak int board_interface_eth_init(struct udevice *dev,
1661 phy_interface_t interface_type)
1666 static int eqos_probe_resources_stm32(struct udevice *dev)
1668 struct eqos_priv *eqos = dev_get_priv(dev);
1670 phy_interface_t interface;
1671 struct ofnode_phandle_args phandle_args;
1673 debug("%s(dev=%p):\n", __func__, dev);
1675 interface = eqos->config->interface(dev);
1677 if (interface == PHY_INTERFACE_MODE_NONE) {
1678 pr_err("Invalid PHY interface\n");
1682 ret = board_interface_eth_init(dev, interface);
1686 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1688 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1690 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1694 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1696 pr_err("clk_get_by_name(rx) failed: %d", ret);
1697 goto err_free_clk_master_bus;
1700 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1702 pr_err("clk_get_by_name(tx) failed: %d", ret);
1703 goto err_free_clk_rx;
1706 /* Get ETH_CLK clocks (optional) */
1707 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1709 pr_warn("No phy clock provided %d", ret);
1712 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1715 /* search "reset-gpios" in phy node */
1716 ret = gpio_request_by_name_nodev(phandle_args.node,
1718 &eqos->phy_reset_gpio,
1720 GPIOD_IS_OUT_ACTIVE);
1722 pr_warn("gpio_request_by_name(phy reset) not provided %d",
1725 eqos->phyaddr = ofnode_read_u32_default(phandle_args.node,
1729 debug("%s: OK\n", __func__);
1733 clk_free(&eqos->clk_rx);
1734 err_free_clk_master_bus:
1735 clk_free(&eqos->clk_master_bus);
1738 debug("%s: returns %d\n", __func__, ret);
1742 static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
1744 const char *phy_mode;
1745 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
1747 debug("%s(dev=%p):\n", __func__, dev);
1749 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1752 interface = phy_get_interface_by_name(phy_mode);
1757 static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
1759 return PHY_INTERFACE_MODE_MII;
1762 static int eqos_remove_resources_tegra186(struct udevice *dev)
1764 struct eqos_priv *eqos = dev_get_priv(dev);
1766 debug("%s(dev=%p):\n", __func__, dev);
1768 clk_free(&eqos->clk_tx);
1769 clk_free(&eqos->clk_ptp_ref);
1770 clk_free(&eqos->clk_rx);
1771 clk_free(&eqos->clk_slave_bus);
1772 clk_free(&eqos->clk_master_bus);
1773 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1774 reset_free(&eqos->reset_ctl);
1776 debug("%s: OK\n", __func__);
1780 static int eqos_remove_resources_stm32(struct udevice *dev)
1782 struct eqos_priv *eqos = dev_get_priv(dev);
1784 debug("%s(dev=%p):\n", __func__, dev);
1786 clk_free(&eqos->clk_tx);
1787 clk_free(&eqos->clk_rx);
1788 clk_free(&eqos->clk_master_bus);
1789 if (clk_valid(&eqos->clk_ck))
1790 clk_free(&eqos->clk_ck);
1792 if (dm_gpio_is_valid(&eqos->phy_reset_gpio))
1793 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1795 debug("%s: OK\n", __func__);
1799 static int eqos_probe(struct udevice *dev)
1801 struct eqos_priv *eqos = dev_get_priv(dev);
1804 debug("%s(dev=%p):\n", __func__, dev);
1807 eqos->config = (void *)dev_get_driver_data(dev);
1809 eqos->regs = devfdt_get_addr(dev);
1810 if (eqos->regs == FDT_ADDR_T_NONE) {
1811 pr_err("devfdt_get_addr() failed");
1814 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1815 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1816 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1817 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1819 ret = eqos_probe_resources_core(dev);
1821 pr_err("eqos_probe_resources_core() failed: %d", ret);
1825 ret = eqos->config->ops->eqos_probe_resources(dev);
1827 pr_err("eqos_probe_resources() failed: %d", ret);
1828 goto err_remove_resources_core;
1831 #ifdef CONFIG_DM_ETH_PHY
1832 eqos->mii = eth_phy_get_mdio_bus(dev);
1835 eqos->mii = mdio_alloc();
1837 pr_err("mdio_alloc() failed");
1839 goto err_remove_resources_tegra;
1841 eqos->mii->read = eqos_mdio_read;
1842 eqos->mii->write = eqos_mdio_write;
1843 eqos->mii->priv = eqos;
1844 strcpy(eqos->mii->name, dev->name);
1846 ret = mdio_register(eqos->mii);
1848 pr_err("mdio_register() failed: %d", ret);
1853 #ifdef CONFIG_DM_ETH_PHY
1854 eth_phy_set_mdio_bus(dev, eqos->mii);
1857 debug("%s: OK\n", __func__);
1861 mdio_free(eqos->mii);
1862 err_remove_resources_tegra:
1863 eqos->config->ops->eqos_remove_resources(dev);
1864 err_remove_resources_core:
1865 eqos_remove_resources_core(dev);
1867 debug("%s: returns %d\n", __func__, ret);
1871 static int eqos_remove(struct udevice *dev)
1873 struct eqos_priv *eqos = dev_get_priv(dev);
1875 debug("%s(dev=%p):\n", __func__, dev);
1877 mdio_unregister(eqos->mii);
1878 mdio_free(eqos->mii);
1879 eqos->config->ops->eqos_remove_resources(dev);
1881 eqos_probe_resources_core(dev);
1883 debug("%s: OK\n", __func__);
1887 static const struct eth_ops eqos_ops = {
1888 .start = eqos_start,
1892 .free_pkt = eqos_free_pkt,
1893 .write_hwaddr = eqos_write_hwaddr,
1896 static struct eqos_ops eqos_tegra186_ops = {
1897 .eqos_inval_desc = eqos_inval_desc_tegra186,
1898 .eqos_flush_desc = eqos_flush_desc_tegra186,
1899 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1900 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1901 .eqos_probe_resources = eqos_probe_resources_tegra186,
1902 .eqos_remove_resources = eqos_remove_resources_tegra186,
1903 .eqos_stop_resets = eqos_stop_resets_tegra186,
1904 .eqos_start_resets = eqos_start_resets_tegra186,
1905 .eqos_stop_clks = eqos_stop_clks_tegra186,
1906 .eqos_start_clks = eqos_start_clks_tegra186,
1907 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1908 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1909 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
1910 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1913 static const struct eqos_config eqos_tegra186_config = {
1914 .reg_access_always_ok = false,
1917 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1918 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
1919 .interface = eqos_get_interface_tegra186,
1920 .ops = &eqos_tegra186_ops
1923 static struct eqos_ops eqos_stm32_ops = {
1924 .eqos_inval_desc = eqos_inval_desc_stm32,
1925 .eqos_flush_desc = eqos_flush_desc_stm32,
1926 .eqos_inval_buffer = eqos_inval_buffer_stm32,
1927 .eqos_flush_buffer = eqos_flush_buffer_stm32,
1928 .eqos_probe_resources = eqos_probe_resources_stm32,
1929 .eqos_remove_resources = eqos_remove_resources_stm32,
1930 .eqos_stop_resets = eqos_stop_resets_stm32,
1931 .eqos_start_resets = eqos_start_resets_stm32,
1932 .eqos_stop_clks = eqos_stop_clks_stm32,
1933 .eqos_start_clks = eqos_start_clks_stm32,
1934 .eqos_calibrate_pads = eqos_calibrate_pads_stm32,
1935 .eqos_disable_calibration = eqos_disable_calibration_stm32,
1936 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_stm32,
1937 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1940 static const struct eqos_config eqos_stm32_config = {
1941 .reg_access_always_ok = false,
1944 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1945 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
1946 .interface = eqos_get_interface_stm32,
1947 .ops = &eqos_stm32_ops
1950 static const struct udevice_id eqos_ids[] = {
1952 .compatible = "nvidia,tegra186-eqos",
1953 .data = (ulong)&eqos_tegra186_config
1956 .compatible = "snps,dwmac-4.20a",
1957 .data = (ulong)&eqos_stm32_config
1963 U_BOOT_DRIVER(eth_eqos) = {
1966 .of_match = eqos_ids,
1967 .probe = eqos_probe,
1968 .remove = eqos_remove,
1970 .priv_auto_alloc_size = sizeof(struct eqos_priv),
1971 .platdata_auto_alloc_size = sizeof(struct eth_pdata),