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.
47 #define EQOS_MAC_REGS_BASE 0x000
48 struct 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 */
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)
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)
90 #define EQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
92 #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT 0
93 #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK 0xff
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
99 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV 1
101 #define EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT 0
102 #define EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK 0xff
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
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
113 #define EQOS_MAC_MDIO_ADDRESS_CR_250_300 5
114 #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)
121 #define EQOS_MAC_MDIO_DATA_GD_MASK 0xffff
123 #define EQOS_MTL_REGS_BASE 0xd00
124 struct 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 */
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)
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
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)
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
162 #define EQOS_DMA_REGS_BASE 0x1000
163 struct 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 */
182 #define EQOS_DMA_MODE_SWR BIT(0)
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)
191 #define EQOS_DMA_CH0_CONTROL_PBLX8 BIT(16)
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)
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)
204 /* These registers are Tegra186-specific */
205 #define EQOS_TEGRA186_REGS_BASE 0x8800
206 struct 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 */
213 #define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
215 #define EQOS_AUTO_CAL_CONFIG_START BIT(31)
216 #define EQOS_AUTO_CAL_CONFIG_ENABLE BIT(29)
218 #define EQOS_AUTO_CAL_STATUS_ACTIVE BIT(31)
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)
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.
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.
245 #if EQOS_DESCRIPTOR_SIZE < ARCH_DMA_MINALIGN
246 #if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
247 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86)
248 #warning Cache line size is larger than descriptor size
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)
265 bool reg_access_always_ok;
270 phy_interface_t (*interface)(struct udevice *dev);
271 struct eqos_ops *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);
293 const struct eqos_config *config;
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;
303 struct clk clk_ptp_ref;
306 struct clk clk_slave_bus;
308 struct phy_device *phy;
312 struct eqos_desc *tx_descs;
313 struct eqos_desc *rx_descs;
314 int tx_desc_idx, rx_desc_idx;
323 * TX and RX descriptors are 16 bytes. This causes problems with the cache
324 * maintenance on CPUs where the cache-line size exceeds the size of these
325 * descriptors. What will happen is that when the driver receives a packet
326 * it will be immediately requeued for the hardware to reuse. The CPU will
327 * therefore need to flush the cache-line containing the descriptor, which
328 * will cause all other descriptors in the same cache-line to be flushed
329 * along with it. If one of those descriptors had been written to by the
330 * device those changes (and the associated packet) will be lost.
332 * To work around this, we make use of non-cached memory if available. If
333 * descriptors are mapped uncached there's no need to manually flush them
334 * or invalidate them.
336 * Note that this only applies to descriptors. The packet data buffers do
337 * not have the same constraints since they are 1536 bytes large, so they
338 * are unlikely to share cache-lines.
340 static void *eqos_alloc_descs(unsigned int num)
342 #ifdef CONFIG_SYS_NONCACHED_MEMORY
343 return (void *)noncached_alloc(EQOS_DESCRIPTORS_SIZE,
344 EQOS_DESCRIPTOR_ALIGN);
346 return memalign(EQOS_DESCRIPTOR_ALIGN, EQOS_DESCRIPTORS_SIZE);
350 static void eqos_free_descs(void *descs)
352 #ifdef CONFIG_SYS_NONCACHED_MEMORY
353 /* FIXME: noncached_alloc() has no opposite */
359 static void eqos_inval_desc_tegra186(void *desc)
361 #ifndef CONFIG_SYS_NONCACHED_MEMORY
362 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
363 unsigned long end = ALIGN(start + EQOS_DESCRIPTOR_SIZE,
366 invalidate_dcache_range(start, end);
370 static void eqos_inval_desc_stm32(void *desc)
372 #ifndef CONFIG_SYS_NONCACHED_MEMORY
373 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
374 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
377 invalidate_dcache_range(start, end);
381 static void eqos_flush_desc_tegra186(void *desc)
383 #ifndef CONFIG_SYS_NONCACHED_MEMORY
384 flush_cache((unsigned long)desc, EQOS_DESCRIPTOR_SIZE);
388 static void eqos_flush_desc_stm32(void *desc)
390 #ifndef CONFIG_SYS_NONCACHED_MEMORY
391 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
392 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
395 flush_dcache_range(start, end);
399 static void eqos_inval_buffer_tegra186(void *buf, size_t size)
401 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
402 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
404 invalidate_dcache_range(start, end);
407 static void eqos_inval_buffer_stm32(void *buf, size_t size)
409 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
410 unsigned long end = roundup((unsigned long)buf + size,
413 invalidate_dcache_range(start, end);
416 static void eqos_flush_buffer_tegra186(void *buf, size_t size)
418 flush_cache((unsigned long)buf, size);
421 static void eqos_flush_buffer_stm32(void *buf, size_t size)
423 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
424 unsigned long end = roundup((unsigned long)buf + size,
427 flush_dcache_range(start, end);
430 static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
432 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
433 EQOS_MAC_MDIO_ADDRESS_GB, false,
437 static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
440 struct eqos_priv *eqos = bus->priv;
444 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
447 ret = eqos_mdio_wait_idle(eqos);
449 pr_err("MDIO not idle at entry");
453 val = readl(&eqos->mac_regs->mdio_address);
454 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
455 EQOS_MAC_MDIO_ADDRESS_C45E;
456 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
457 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
458 (eqos->config->config_mac_mdio <<
459 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
460 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
461 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
462 EQOS_MAC_MDIO_ADDRESS_GB;
463 writel(val, &eqos->mac_regs->mdio_address);
465 udelay(eqos->config->mdio_wait);
467 ret = eqos_mdio_wait_idle(eqos);
469 pr_err("MDIO read didn't complete");
473 val = readl(&eqos->mac_regs->mdio_data);
474 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
476 debug("%s: val=%x\n", __func__, val);
481 static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
482 int mdio_reg, u16 mdio_val)
484 struct eqos_priv *eqos = bus->priv;
488 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
489 mdio_addr, mdio_reg, mdio_val);
491 ret = eqos_mdio_wait_idle(eqos);
493 pr_err("MDIO not idle at entry");
497 writel(mdio_val, &eqos->mac_regs->mdio_data);
499 val = readl(&eqos->mac_regs->mdio_address);
500 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
501 EQOS_MAC_MDIO_ADDRESS_C45E;
502 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
503 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
504 (eqos->config->config_mac_mdio <<
505 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
506 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
507 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
508 EQOS_MAC_MDIO_ADDRESS_GB;
509 writel(val, &eqos->mac_regs->mdio_address);
511 udelay(eqos->config->mdio_wait);
513 ret = eqos_mdio_wait_idle(eqos);
515 pr_err("MDIO read didn't complete");
522 static int eqos_start_clks_tegra186(struct udevice *dev)
524 struct eqos_priv *eqos = dev_get_priv(dev);
527 debug("%s(dev=%p):\n", __func__, dev);
529 ret = clk_enable(&eqos->clk_slave_bus);
531 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
535 ret = clk_enable(&eqos->clk_master_bus);
537 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
538 goto err_disable_clk_slave_bus;
541 ret = clk_enable(&eqos->clk_rx);
543 pr_err("clk_enable(clk_rx) failed: %d", ret);
544 goto err_disable_clk_master_bus;
547 ret = clk_enable(&eqos->clk_ptp_ref);
549 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
550 goto err_disable_clk_rx;
553 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
555 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
556 goto err_disable_clk_ptp_ref;
559 ret = clk_enable(&eqos->clk_tx);
561 pr_err("clk_enable(clk_tx) failed: %d", ret);
562 goto err_disable_clk_ptp_ref;
565 debug("%s: OK\n", __func__);
568 err_disable_clk_ptp_ref:
569 clk_disable(&eqos->clk_ptp_ref);
571 clk_disable(&eqos->clk_rx);
572 err_disable_clk_master_bus:
573 clk_disable(&eqos->clk_master_bus);
574 err_disable_clk_slave_bus:
575 clk_disable(&eqos->clk_slave_bus);
577 debug("%s: FAILED: %d\n", __func__, ret);
581 static int eqos_start_clks_stm32(struct udevice *dev)
583 struct eqos_priv *eqos = dev_get_priv(dev);
586 debug("%s(dev=%p):\n", __func__, dev);
588 ret = clk_enable(&eqos->clk_master_bus);
590 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
594 ret = clk_enable(&eqos->clk_rx);
596 pr_err("clk_enable(clk_rx) failed: %d", ret);
597 goto err_disable_clk_master_bus;
600 ret = clk_enable(&eqos->clk_tx);
602 pr_err("clk_enable(clk_tx) failed: %d", ret);
603 goto err_disable_clk_rx;
606 if (clk_valid(&eqos->clk_ck)) {
607 ret = clk_enable(&eqos->clk_ck);
609 pr_err("clk_enable(clk_ck) failed: %d", ret);
610 goto err_disable_clk_tx;
614 debug("%s: OK\n", __func__);
618 clk_disable(&eqos->clk_tx);
620 clk_disable(&eqos->clk_rx);
621 err_disable_clk_master_bus:
622 clk_disable(&eqos->clk_master_bus);
624 debug("%s: FAILED: %d\n", __func__, ret);
628 static void eqos_stop_clks_tegra186(struct udevice *dev)
630 struct eqos_priv *eqos = dev_get_priv(dev);
632 debug("%s(dev=%p):\n", __func__, dev);
634 clk_disable(&eqos->clk_tx);
635 clk_disable(&eqos->clk_ptp_ref);
636 clk_disable(&eqos->clk_rx);
637 clk_disable(&eqos->clk_master_bus);
638 clk_disable(&eqos->clk_slave_bus);
640 debug("%s: OK\n", __func__);
643 static void eqos_stop_clks_stm32(struct udevice *dev)
645 struct eqos_priv *eqos = dev_get_priv(dev);
647 debug("%s(dev=%p):\n", __func__, dev);
649 clk_disable(&eqos->clk_tx);
650 clk_disable(&eqos->clk_rx);
651 clk_disable(&eqos->clk_master_bus);
652 if (clk_valid(&eqos->clk_ck))
653 clk_disable(&eqos->clk_ck);
655 debug("%s: OK\n", __func__);
658 static int eqos_start_resets_tegra186(struct udevice *dev)
660 struct eqos_priv *eqos = dev_get_priv(dev);
663 debug("%s(dev=%p):\n", __func__, dev);
665 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
667 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
673 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
675 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
679 ret = reset_assert(&eqos->reset_ctl);
681 pr_err("reset_assert() failed: %d", ret);
687 ret = reset_deassert(&eqos->reset_ctl);
689 pr_err("reset_deassert() failed: %d", ret);
693 debug("%s: OK\n", __func__);
697 static int eqos_start_resets_stm32(struct udevice *dev)
699 struct eqos_priv *eqos = dev_get_priv(dev);
702 debug("%s(dev=%p):\n", __func__, dev);
703 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
704 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
706 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d",
713 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
715 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d",
720 debug("%s: OK\n", __func__);
725 static int eqos_stop_resets_tegra186(struct udevice *dev)
727 struct eqos_priv *eqos = dev_get_priv(dev);
729 reset_assert(&eqos->reset_ctl);
730 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
735 static int eqos_stop_resets_stm32(struct udevice *dev)
737 struct eqos_priv *eqos = dev_get_priv(dev);
740 if (dm_gpio_is_valid(&eqos->phy_reset_gpio)) {
741 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
743 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d",
752 static int eqos_calibrate_pads_tegra186(struct udevice *dev)
754 struct eqos_priv *eqos = dev_get_priv(dev);
757 debug("%s(dev=%p):\n", __func__, dev);
759 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
760 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
764 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
765 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
767 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
768 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
770 pr_err("calibrate didn't start");
774 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
775 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
777 pr_err("calibrate didn't finish");
784 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
785 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
787 debug("%s: returns %d\n", __func__, ret);
792 static int eqos_disable_calibration_tegra186(struct udevice *dev)
794 struct eqos_priv *eqos = dev_get_priv(dev);
796 debug("%s(dev=%p):\n", __func__, dev);
798 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
799 EQOS_AUTO_CAL_CONFIG_ENABLE);
804 static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
806 struct eqos_priv *eqos = dev_get_priv(dev);
808 return clk_get_rate(&eqos->clk_slave_bus);
811 static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
813 struct eqos_priv *eqos = dev_get_priv(dev);
815 return clk_get_rate(&eqos->clk_master_bus);
818 static int eqos_calibrate_pads_stm32(struct udevice *dev)
823 static int eqos_disable_calibration_stm32(struct udevice *dev)
828 static int eqos_set_full_duplex(struct udevice *dev)
830 struct eqos_priv *eqos = dev_get_priv(dev);
832 debug("%s(dev=%p):\n", __func__, dev);
834 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
839 static int eqos_set_half_duplex(struct udevice *dev)
841 struct eqos_priv *eqos = dev_get_priv(dev);
843 debug("%s(dev=%p):\n", __func__, dev);
845 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
847 /* WAR: Flush TX queue when switching to half-duplex */
848 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
849 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
854 static int eqos_set_gmii_speed(struct udevice *dev)
856 struct eqos_priv *eqos = dev_get_priv(dev);
858 debug("%s(dev=%p):\n", __func__, dev);
860 clrbits_le32(&eqos->mac_regs->configuration,
861 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
866 static int eqos_set_mii_speed_100(struct udevice *dev)
868 struct eqos_priv *eqos = dev_get_priv(dev);
870 debug("%s(dev=%p):\n", __func__, dev);
872 setbits_le32(&eqos->mac_regs->configuration,
873 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
878 static int eqos_set_mii_speed_10(struct udevice *dev)
880 struct eqos_priv *eqos = dev_get_priv(dev);
882 debug("%s(dev=%p):\n", __func__, dev);
884 clrsetbits_le32(&eqos->mac_regs->configuration,
885 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
890 static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
892 struct eqos_priv *eqos = dev_get_priv(dev);
896 debug("%s(dev=%p):\n", __func__, dev);
898 switch (eqos->phy->speed) {
900 rate = 125 * 1000 * 1000;
903 rate = 25 * 1000 * 1000;
906 rate = 2.5 * 1000 * 1000;
909 pr_err("invalid speed %d", eqos->phy->speed);
913 ret = clk_set_rate(&eqos->clk_tx, rate);
915 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
922 static int eqos_set_tx_clk_speed_stm32(struct udevice *dev)
927 static int eqos_adjust_link(struct udevice *dev)
929 struct eqos_priv *eqos = dev_get_priv(dev);
933 debug("%s(dev=%p):\n", __func__, dev);
935 if (eqos->phy->duplex)
936 ret = eqos_set_full_duplex(dev);
938 ret = eqos_set_half_duplex(dev);
940 pr_err("eqos_set_*_duplex() failed: %d", ret);
944 switch (eqos->phy->speed) {
946 en_calibration = true;
947 ret = eqos_set_gmii_speed(dev);
950 en_calibration = true;
951 ret = eqos_set_mii_speed_100(dev);
954 en_calibration = false;
955 ret = eqos_set_mii_speed_10(dev);
958 pr_err("invalid speed %d", eqos->phy->speed);
962 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
966 if (en_calibration) {
967 ret = eqos->config->ops->eqos_calibrate_pads(dev);
969 pr_err("eqos_calibrate_pads() failed: %d",
974 ret = eqos->config->ops->eqos_disable_calibration(dev);
976 pr_err("eqos_disable_calibration() failed: %d",
981 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
983 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
990 static int eqos_write_hwaddr(struct udevice *dev)
992 struct eth_pdata *plat = dev_get_platdata(dev);
993 struct eqos_priv *eqos = dev_get_priv(dev);
997 * This function may be called before start() or after stop(). At that
998 * time, on at least some configurations of the EQoS HW, all clocks to
999 * the EQoS HW block will be stopped, and a reset signal applied. If
1000 * any register access is attempted in this state, bus timeouts or CPU
1001 * hangs may occur. This check prevents that.
1003 * A simple solution to this problem would be to not implement
1004 * write_hwaddr(), since start() always writes the MAC address into HW
1005 * anyway. However, it is desirable to implement write_hwaddr() to
1006 * support the case of SW that runs subsequent to U-Boot which expects
1007 * the MAC address to already be programmed into the EQoS registers,
1008 * which must happen irrespective of whether the U-Boot user (or
1009 * scripts) actually made use of the EQoS device, and hence
1010 * irrespective of whether start() was ever called.
1012 * Note that this requirement by subsequent SW is not valid for
1013 * Tegra186, and is likely not valid for any non-PCI instantiation of
1014 * the EQoS HW block. This function is implemented solely as
1015 * future-proofing with the expectation the driver will eventually be
1016 * ported to some system where the expectation above is true.
1018 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
1021 /* Update the MAC address */
1022 val = (plat->enetaddr[5] << 8) |
1023 (plat->enetaddr[4]);
1024 writel(val, &eqos->mac_regs->address0_high);
1025 val = (plat->enetaddr[3] << 24) |
1026 (plat->enetaddr[2] << 16) |
1027 (plat->enetaddr[1] << 8) |
1028 (plat->enetaddr[0]);
1029 writel(val, &eqos->mac_regs->address0_low);
1034 static int eqos_start(struct udevice *dev)
1036 struct eqos_priv *eqos = dev_get_priv(dev);
1039 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
1042 debug("%s(dev=%p):\n", __func__, dev);
1044 eqos->tx_desc_idx = 0;
1045 eqos->rx_desc_idx = 0;
1047 ret = eqos->config->ops->eqos_start_clks(dev);
1049 pr_err("eqos_start_clks() failed: %d", ret);
1053 ret = eqos->config->ops->eqos_start_resets(dev);
1055 pr_err("eqos_start_resets() failed: %d", ret);
1061 eqos->reg_access_ok = true;
1063 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
1064 EQOS_DMA_MODE_SWR, false,
1065 eqos->config->swr_wait, false);
1067 pr_err("EQOS_DMA_MODE_SWR stuck");
1068 goto err_stop_resets;
1071 ret = eqos->config->ops->eqos_calibrate_pads(dev);
1073 pr_err("eqos_calibrate_pads() failed: %d", ret);
1074 goto err_stop_resets;
1076 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
1078 val = (rate / 1000000) - 1;
1079 writel(val, &eqos->mac_regs->us_tic_counter);
1082 * if PHY was already connected and configured,
1083 * don't need to reconnect/reconfigure again
1086 eqos->phy = phy_connect(eqos->mii, eqos->phyaddr, dev,
1087 eqos->config->interface(dev));
1089 pr_err("phy_connect() failed");
1090 goto err_stop_resets;
1093 if (eqos->max_speed) {
1094 ret = phy_set_supported(eqos->phy, eqos->max_speed);
1096 pr_err("phy_set_supported() failed: %d", ret);
1097 goto err_shutdown_phy;
1101 ret = phy_config(eqos->phy);
1103 pr_err("phy_config() failed: %d", ret);
1104 goto err_shutdown_phy;
1108 ret = phy_startup(eqos->phy);
1110 pr_err("phy_startup() failed: %d", ret);
1111 goto err_shutdown_phy;
1114 if (!eqos->phy->link) {
1116 goto err_shutdown_phy;
1119 ret = eqos_adjust_link(dev);
1121 pr_err("eqos_adjust_link() failed: %d", ret);
1122 goto err_shutdown_phy;
1127 /* Enable Store and Forward mode for TX */
1128 /* Program Tx operating mode */
1129 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1130 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
1131 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
1132 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
1134 /* Transmit Queue weight */
1135 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
1137 /* Enable Store and Forward mode for RX, since no jumbo frame */
1138 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1139 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
1141 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
1142 val = readl(&eqos->mac_regs->hw_feature1);
1143 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
1144 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
1145 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
1146 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
1149 * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
1150 * r/tqs is encoded as (n / 256) - 1.
1152 tqs = (128 << tx_fifo_sz) / 256 - 1;
1153 rqs = (128 << rx_fifo_sz) / 256 - 1;
1155 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1156 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
1157 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
1158 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
1159 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1160 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
1161 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
1162 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
1164 /* Flow control used only if each channel gets 4KB or more FIFO */
1165 if (rqs >= ((4096 / 256) - 1)) {
1168 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1169 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
1172 * Set Threshold for Activating Flow Contol space for min 2
1173 * frames ie, (1500 * 1) = 1500 bytes.
1175 * Set Threshold for Deactivating Flow Contol for space of
1176 * min 1 frame (frame size 1500bytes) in receive fifo
1178 if (rqs == ((4096 / 256) - 1)) {
1180 * This violates the above formula because of FIFO size
1181 * limit therefore overflow may occur inspite of this.
1183 rfd = 0x3; /* Full-3K */
1184 rfa = 0x1; /* Full-1.5K */
1185 } else if (rqs == ((8192 / 256) - 1)) {
1186 rfd = 0x6; /* Full-4K */
1187 rfa = 0xa; /* Full-6K */
1188 } else if (rqs == ((16384 / 256) - 1)) {
1189 rfd = 0x6; /* Full-4K */
1190 rfa = 0x12; /* Full-10K */
1192 rfd = 0x6; /* Full-4K */
1193 rfa = 0x1E; /* Full-16K */
1196 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1197 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
1198 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1199 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
1200 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
1202 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1204 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
1209 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
1210 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
1211 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
1212 eqos->config->config_mac <<
1213 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
1215 /* Set TX flow control parameters */
1216 /* Set Pause Time */
1217 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1218 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
1219 /* Assign priority for TX flow control */
1220 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
1221 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
1222 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
1223 /* Assign priority for RX flow control */
1224 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
1225 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
1226 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
1227 /* Enable flow control */
1228 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1229 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
1230 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
1231 EQOS_MAC_RX_FLOW_CTRL_RFE);
1233 clrsetbits_le32(&eqos->mac_regs->configuration,
1234 EQOS_MAC_CONFIGURATION_GPSLCE |
1235 EQOS_MAC_CONFIGURATION_WD |
1236 EQOS_MAC_CONFIGURATION_JD |
1237 EQOS_MAC_CONFIGURATION_JE,
1238 EQOS_MAC_CONFIGURATION_CST |
1239 EQOS_MAC_CONFIGURATION_ACS);
1241 eqos_write_hwaddr(dev);
1245 /* Enable OSP mode */
1246 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1247 EQOS_DMA_CH0_TX_CONTROL_OSP);
1249 /* RX buffer size. Must be a multiple of bus width */
1250 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1251 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
1252 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
1253 EQOS_MAX_PACKET_SIZE <<
1254 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
1256 setbits_le32(&eqos->dma_regs->ch0_control,
1257 EQOS_DMA_CH0_CONTROL_PBLX8);
1260 * Burst length must be < 1/2 FIFO size.
1261 * FIFO size in tqs is encoded as (n / 256) - 1.
1262 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1263 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1268 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1269 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1270 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1271 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1273 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1274 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1275 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1276 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1278 /* DMA performance configuration */
1279 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1280 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1281 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1282 writel(val, &eqos->dma_regs->sysbus_mode);
1284 /* Set up descriptors */
1286 memset(eqos->descs, 0, EQOS_DESCRIPTORS_SIZE);
1287 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
1288 struct eqos_desc *rx_desc = &(eqos->rx_descs[i]);
1289 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1290 (i * EQOS_MAX_PACKET_SIZE));
1291 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1292 eqos->config->ops->eqos_flush_desc(rx_desc);
1295 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
1296 writel((ulong)eqos->tx_descs, &eqos->dma_regs->ch0_txdesc_list_address);
1297 writel(EQOS_DESCRIPTORS_TX - 1,
1298 &eqos->dma_regs->ch0_txdesc_ring_length);
1300 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
1301 writel((ulong)eqos->rx_descs, &eqos->dma_regs->ch0_rxdesc_list_address);
1302 writel(EQOS_DESCRIPTORS_RX - 1,
1303 &eqos->dma_regs->ch0_rxdesc_ring_length);
1305 /* Enable everything */
1307 setbits_le32(&eqos->mac_regs->configuration,
1308 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1310 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1311 EQOS_DMA_CH0_TX_CONTROL_ST);
1312 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1313 EQOS_DMA_CH0_RX_CONTROL_SR);
1315 /* TX tail pointer not written until we need to TX a packet */
1317 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1318 * first descriptor, implying all descriptors were available. However,
1319 * that's not distinguishable from none of the descriptors being
1322 last_rx_desc = (ulong)&(eqos->rx_descs[(EQOS_DESCRIPTORS_RX - 1)]);
1323 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1325 eqos->started = true;
1327 debug("%s: OK\n", __func__);
1331 phy_shutdown(eqos->phy);
1333 eqos->config->ops->eqos_stop_resets(dev);
1335 eqos->config->ops->eqos_stop_clks(dev);
1337 pr_err("FAILED: %d", ret);
1341 static void eqos_stop(struct udevice *dev)
1343 struct eqos_priv *eqos = dev_get_priv(dev);
1346 debug("%s(dev=%p):\n", __func__, dev);
1350 eqos->started = false;
1351 eqos->reg_access_ok = false;
1353 /* Disable TX DMA */
1354 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1355 EQOS_DMA_CH0_TX_CONTROL_ST);
1357 /* Wait for TX all packets to drain out of MTL */
1358 for (i = 0; i < 1000000; i++) {
1359 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1360 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1361 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1362 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1363 if ((trcsts != 1) && (!txqsts))
1367 /* Turn off MAC TX and RX */
1368 clrbits_le32(&eqos->mac_regs->configuration,
1369 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1371 /* Wait for all RX packets to drain out of MTL */
1372 for (i = 0; i < 1000000; i++) {
1373 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1374 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1375 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1376 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1377 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1378 if ((!prxq) && (!rxqsts))
1382 /* Turn off RX DMA */
1383 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1384 EQOS_DMA_CH0_RX_CONTROL_SR);
1387 phy_shutdown(eqos->phy);
1389 eqos->config->ops->eqos_stop_resets(dev);
1390 eqos->config->ops->eqos_stop_clks(dev);
1392 debug("%s: OK\n", __func__);
1395 static int eqos_send(struct udevice *dev, void *packet, int length)
1397 struct eqos_priv *eqos = dev_get_priv(dev);
1398 struct eqos_desc *tx_desc;
1401 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1404 memcpy(eqos->tx_dma_buf, packet, length);
1405 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
1407 tx_desc = &(eqos->tx_descs[eqos->tx_desc_idx]);
1408 eqos->tx_desc_idx++;
1409 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1411 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1413 tx_desc->des2 = length;
1415 * Make sure that if HW sees the _OWN write below, it will see all the
1416 * writes to the rest of the descriptor too.
1419 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
1420 eqos->config->ops->eqos_flush_desc(tx_desc);
1422 writel((ulong)(&(eqos->tx_descs[eqos->tx_desc_idx])),
1423 &eqos->dma_regs->ch0_txdesc_tail_pointer);
1425 for (i = 0; i < 1000000; i++) {
1426 eqos->config->ops->eqos_inval_desc(tx_desc);
1427 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1432 debug("%s: TX timeout\n", __func__);
1437 static int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
1439 struct eqos_priv *eqos = dev_get_priv(dev);
1440 struct eqos_desc *rx_desc;
1443 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1445 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1446 eqos->config->ops->eqos_inval_desc(rx_desc);
1447 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1448 debug("%s: RX packet not available\n", __func__);
1452 *packetp = eqos->rx_dma_buf +
1453 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1454 length = rx_desc->des3 & 0x7fff;
1455 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1457 eqos->config->ops->eqos_inval_buffer(*packetp, length);
1462 static int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
1464 struct eqos_priv *eqos = dev_get_priv(dev);
1465 uchar *packet_expected;
1466 struct eqos_desc *rx_desc;
1468 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1470 packet_expected = eqos->rx_dma_buf +
1471 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1472 if (packet != packet_expected) {
1473 debug("%s: Unexpected packet (expected %p)\n", __func__,
1478 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1482 eqos->config->ops->eqos_flush_desc(rx_desc);
1483 eqos->config->ops->eqos_inval_buffer(packet, length);
1484 rx_desc->des0 = (u32)(ulong)packet;
1488 * Make sure that if HW sees the _OWN write below, it will see all the
1489 * writes to the rest of the descriptor too.
1492 rx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1493 eqos->config->ops->eqos_flush_desc(rx_desc);
1495 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1497 eqos->rx_desc_idx++;
1498 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1503 static int eqos_probe_resources_core(struct udevice *dev)
1505 struct eqos_priv *eqos = dev_get_priv(dev);
1508 debug("%s(dev=%p):\n", __func__, dev);
1510 eqos->descs = eqos_alloc_descs(EQOS_DESCRIPTORS_TX +
1511 EQOS_DESCRIPTORS_RX);
1513 debug("%s: eqos_alloc_descs() failed\n", __func__);
1517 eqos->tx_descs = (struct eqos_desc *)eqos->descs;
1518 eqos->rx_descs = (eqos->tx_descs + EQOS_DESCRIPTORS_TX);
1519 debug("%s: tx_descs=%p, rx_descs=%p\n", __func__, eqos->tx_descs,
1522 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1523 if (!eqos->tx_dma_buf) {
1524 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1526 goto err_free_descs;
1528 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
1530 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1531 if (!eqos->rx_dma_buf) {
1532 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1534 goto err_free_tx_dma_buf;
1536 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
1538 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1539 if (!eqos->rx_pkt) {
1540 debug("%s: malloc(rx_pkt) failed\n", __func__);
1542 goto err_free_rx_dma_buf;
1544 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1546 eqos->config->ops->eqos_inval_buffer(eqos->rx_dma_buf,
1547 EQOS_MAX_PACKET_SIZE * EQOS_DESCRIPTORS_RX);
1549 debug("%s: OK\n", __func__);
1552 err_free_rx_dma_buf:
1553 free(eqos->rx_dma_buf);
1554 err_free_tx_dma_buf:
1555 free(eqos->tx_dma_buf);
1557 eqos_free_descs(eqos->descs);
1560 debug("%s: returns %d\n", __func__, ret);
1564 static int eqos_remove_resources_core(struct udevice *dev)
1566 struct eqos_priv *eqos = dev_get_priv(dev);
1568 debug("%s(dev=%p):\n", __func__, dev);
1571 free(eqos->rx_dma_buf);
1572 free(eqos->tx_dma_buf);
1573 eqos_free_descs(eqos->descs);
1575 debug("%s: OK\n", __func__);
1579 static int eqos_probe_resources_tegra186(struct udevice *dev)
1581 struct eqos_priv *eqos = dev_get_priv(dev);
1584 debug("%s(dev=%p):\n", __func__, dev);
1586 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1588 pr_err("reset_get_by_name(rst) failed: %d", ret);
1592 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1593 &eqos->phy_reset_gpio,
1594 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1596 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
1597 goto err_free_reset_eqos;
1600 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1602 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
1603 goto err_free_gpio_phy_reset;
1606 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1608 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1609 goto err_free_clk_slave_bus;
1612 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1614 pr_err("clk_get_by_name(rx) failed: %d", ret);
1615 goto err_free_clk_master_bus;
1618 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1620 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
1621 goto err_free_clk_rx;
1625 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1627 pr_err("clk_get_by_name(tx) failed: %d", ret);
1628 goto err_free_clk_ptp_ref;
1631 debug("%s: OK\n", __func__);
1634 err_free_clk_ptp_ref:
1635 clk_free(&eqos->clk_ptp_ref);
1637 clk_free(&eqos->clk_rx);
1638 err_free_clk_master_bus:
1639 clk_free(&eqos->clk_master_bus);
1640 err_free_clk_slave_bus:
1641 clk_free(&eqos->clk_slave_bus);
1642 err_free_gpio_phy_reset:
1643 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1644 err_free_reset_eqos:
1645 reset_free(&eqos->reset_ctl);
1647 debug("%s: returns %d\n", __func__, ret);
1651 /* board-specific Ethernet Interface initializations. */
1652 __weak int board_interface_eth_init(struct udevice *dev,
1653 phy_interface_t interface_type)
1658 static int eqos_probe_resources_stm32(struct udevice *dev)
1660 struct eqos_priv *eqos = dev_get_priv(dev);
1662 phy_interface_t interface;
1663 struct ofnode_phandle_args phandle_args;
1665 debug("%s(dev=%p):\n", __func__, dev);
1667 interface = eqos->config->interface(dev);
1669 if (interface == PHY_INTERFACE_MODE_NONE) {
1670 pr_err("Invalid PHY interface\n");
1674 ret = board_interface_eth_init(dev, interface);
1678 eqos->max_speed = dev_read_u32_default(dev, "max-speed", 0);
1680 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1682 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1686 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1688 pr_err("clk_get_by_name(rx) failed: %d", ret);
1689 goto err_free_clk_master_bus;
1692 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1694 pr_err("clk_get_by_name(tx) failed: %d", ret);
1695 goto err_free_clk_rx;
1698 /* Get ETH_CLK clocks (optional) */
1699 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1701 pr_warn("No phy clock provided %d", ret);
1704 ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0,
1707 /* search "reset-gpios" in phy node */
1708 ret = gpio_request_by_name_nodev(phandle_args.node,
1710 &eqos->phy_reset_gpio,
1712 GPIOD_IS_OUT_ACTIVE);
1714 pr_warn("gpio_request_by_name(phy reset) not provided %d",
1717 eqos->phyaddr = ofnode_read_u32_default(phandle_args.node,
1721 debug("%s: OK\n", __func__);
1725 clk_free(&eqos->clk_rx);
1726 err_free_clk_master_bus:
1727 clk_free(&eqos->clk_master_bus);
1730 debug("%s: returns %d\n", __func__, ret);
1734 static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
1736 const char *phy_mode;
1737 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
1739 debug("%s(dev=%p):\n", __func__, dev);
1741 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1744 interface = phy_get_interface_by_name(phy_mode);
1749 static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
1751 return PHY_INTERFACE_MODE_MII;
1754 static int eqos_remove_resources_tegra186(struct udevice *dev)
1756 struct eqos_priv *eqos = dev_get_priv(dev);
1758 debug("%s(dev=%p):\n", __func__, dev);
1760 clk_free(&eqos->clk_tx);
1761 clk_free(&eqos->clk_ptp_ref);
1762 clk_free(&eqos->clk_rx);
1763 clk_free(&eqos->clk_slave_bus);
1764 clk_free(&eqos->clk_master_bus);
1765 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1766 reset_free(&eqos->reset_ctl);
1768 debug("%s: OK\n", __func__);
1772 static int eqos_remove_resources_stm32(struct udevice *dev)
1774 struct eqos_priv *eqos = dev_get_priv(dev);
1776 debug("%s(dev=%p):\n", __func__, dev);
1778 clk_free(&eqos->clk_tx);
1779 clk_free(&eqos->clk_rx);
1780 clk_free(&eqos->clk_master_bus);
1781 if (clk_valid(&eqos->clk_ck))
1782 clk_free(&eqos->clk_ck);
1784 if (dm_gpio_is_valid(&eqos->phy_reset_gpio))
1785 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1787 debug("%s: OK\n", __func__);
1791 static int eqos_probe(struct udevice *dev)
1793 struct eqos_priv *eqos = dev_get_priv(dev);
1796 debug("%s(dev=%p):\n", __func__, dev);
1799 eqos->config = (void *)dev_get_driver_data(dev);
1801 eqos->regs = devfdt_get_addr(dev);
1802 if (eqos->regs == FDT_ADDR_T_NONE) {
1803 pr_err("devfdt_get_addr() failed");
1806 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1807 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1808 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1809 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1811 ret = eqos_probe_resources_core(dev);
1813 pr_err("eqos_probe_resources_core() failed: %d", ret);
1817 ret = eqos->config->ops->eqos_probe_resources(dev);
1819 pr_err("eqos_probe_resources() failed: %d", ret);
1820 goto err_remove_resources_core;
1823 eqos->mii = mdio_alloc();
1825 pr_err("mdio_alloc() failed");
1827 goto err_remove_resources_tegra;
1829 eqos->mii->read = eqos_mdio_read;
1830 eqos->mii->write = eqos_mdio_write;
1831 eqos->mii->priv = eqos;
1832 strcpy(eqos->mii->name, dev->name);
1834 ret = mdio_register(eqos->mii);
1836 pr_err("mdio_register() failed: %d", ret);
1840 debug("%s: OK\n", __func__);
1844 mdio_free(eqos->mii);
1845 err_remove_resources_tegra:
1846 eqos->config->ops->eqos_remove_resources(dev);
1847 err_remove_resources_core:
1848 eqos_remove_resources_core(dev);
1850 debug("%s: returns %d\n", __func__, ret);
1854 static int eqos_remove(struct udevice *dev)
1856 struct eqos_priv *eqos = dev_get_priv(dev);
1858 debug("%s(dev=%p):\n", __func__, dev);
1860 mdio_unregister(eqos->mii);
1861 mdio_free(eqos->mii);
1862 eqos->config->ops->eqos_remove_resources(dev);
1864 eqos_probe_resources_core(dev);
1866 debug("%s: OK\n", __func__);
1870 static const struct eth_ops eqos_ops = {
1871 .start = eqos_start,
1875 .free_pkt = eqos_free_pkt,
1876 .write_hwaddr = eqos_write_hwaddr,
1879 static struct eqos_ops eqos_tegra186_ops = {
1880 .eqos_inval_desc = eqos_inval_desc_tegra186,
1881 .eqos_flush_desc = eqos_flush_desc_tegra186,
1882 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1883 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1884 .eqos_probe_resources = eqos_probe_resources_tegra186,
1885 .eqos_remove_resources = eqos_remove_resources_tegra186,
1886 .eqos_stop_resets = eqos_stop_resets_tegra186,
1887 .eqos_start_resets = eqos_start_resets_tegra186,
1888 .eqos_stop_clks = eqos_stop_clks_tegra186,
1889 .eqos_start_clks = eqos_start_clks_tegra186,
1890 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1891 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1892 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
1893 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1896 static const struct eqos_config eqos_tegra186_config = {
1897 .reg_access_always_ok = false,
1900 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1901 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
1902 .interface = eqos_get_interface_tegra186,
1903 .ops = &eqos_tegra186_ops
1906 static struct eqos_ops eqos_stm32_ops = {
1907 .eqos_inval_desc = eqos_inval_desc_stm32,
1908 .eqos_flush_desc = eqos_flush_desc_stm32,
1909 .eqos_inval_buffer = eqos_inval_buffer_stm32,
1910 .eqos_flush_buffer = eqos_flush_buffer_stm32,
1911 .eqos_probe_resources = eqos_probe_resources_stm32,
1912 .eqos_remove_resources = eqos_remove_resources_stm32,
1913 .eqos_stop_resets = eqos_stop_resets_stm32,
1914 .eqos_start_resets = eqos_start_resets_stm32,
1915 .eqos_stop_clks = eqos_stop_clks_stm32,
1916 .eqos_start_clks = eqos_start_clks_stm32,
1917 .eqos_calibrate_pads = eqos_calibrate_pads_stm32,
1918 .eqos_disable_calibration = eqos_disable_calibration_stm32,
1919 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_stm32,
1920 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1923 static const struct eqos_config eqos_stm32_config = {
1924 .reg_access_always_ok = false,
1927 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1928 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
1929 .interface = eqos_get_interface_stm32,
1930 .ops = &eqos_stm32_ops
1933 static const struct udevice_id eqos_ids[] = {
1935 .compatible = "nvidia,tegra186-eqos",
1936 .data = (ulong)&eqos_tegra186_config
1939 .compatible = "snps,dwmac-4.20a",
1940 .data = (ulong)&eqos_stm32_config
1946 U_BOOT_DRIVER(eth_eqos) = {
1949 .of_match = eqos_ids,
1950 .probe = eqos_probe,
1951 .remove = eqos_remove,
1953 .priv_auto_alloc_size = sizeof(struct eqos_priv),
1954 .platdata_auto_alloc_size = sizeof(struct eth_pdata),