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.
45 #define EQOS_MAC_REGS_BASE 0x000
46 struct eqos_mac_regs {
47 uint32_t configuration; /* 0x000 */
48 uint32_t unused_004[(0x070 - 0x004) / 4]; /* 0x004 */
49 uint32_t q0_tx_flow_ctrl; /* 0x070 */
50 uint32_t unused_070[(0x090 - 0x074) / 4]; /* 0x074 */
51 uint32_t rx_flow_ctrl; /* 0x090 */
52 uint32_t unused_094; /* 0x094 */
53 uint32_t txq_prty_map0; /* 0x098 */
54 uint32_t unused_09c; /* 0x09c */
55 uint32_t rxq_ctrl0; /* 0x0a0 */
56 uint32_t unused_0a4; /* 0x0a4 */
57 uint32_t rxq_ctrl2; /* 0x0a8 */
58 uint32_t unused_0ac[(0x0dc - 0x0ac) / 4]; /* 0x0ac */
59 uint32_t us_tic_counter; /* 0x0dc */
60 uint32_t unused_0e0[(0x11c - 0x0e0) / 4]; /* 0x0e0 */
61 uint32_t hw_feature0; /* 0x11c */
62 uint32_t hw_feature1; /* 0x120 */
63 uint32_t hw_feature2; /* 0x124 */
64 uint32_t unused_128[(0x200 - 0x128) / 4]; /* 0x128 */
65 uint32_t mdio_address; /* 0x200 */
66 uint32_t mdio_data; /* 0x204 */
67 uint32_t unused_208[(0x300 - 0x208) / 4]; /* 0x208 */
68 uint32_t address0_high; /* 0x300 */
69 uint32_t address0_low; /* 0x304 */
72 #define EQOS_MAC_CONFIGURATION_GPSLCE BIT(23)
73 #define EQOS_MAC_CONFIGURATION_CST BIT(21)
74 #define EQOS_MAC_CONFIGURATION_ACS BIT(20)
75 #define EQOS_MAC_CONFIGURATION_WD BIT(19)
76 #define EQOS_MAC_CONFIGURATION_JD BIT(17)
77 #define EQOS_MAC_CONFIGURATION_JE BIT(16)
78 #define EQOS_MAC_CONFIGURATION_PS BIT(15)
79 #define EQOS_MAC_CONFIGURATION_FES BIT(14)
80 #define EQOS_MAC_CONFIGURATION_DM BIT(13)
81 #define EQOS_MAC_CONFIGURATION_TE BIT(1)
82 #define EQOS_MAC_CONFIGURATION_RE BIT(0)
84 #define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT 16
85 #define EQOS_MAC_Q0_TX_FLOW_CTRL_PT_MASK 0xffff
86 #define EQOS_MAC_Q0_TX_FLOW_CTRL_TFE BIT(1)
88 #define EQOS_MAC_RX_FLOW_CTRL_RFE BIT(0)
90 #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT 0
91 #define EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK 0xff
93 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT 0
94 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK 3
95 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_NOT_ENABLED 0
96 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB 2
97 #define EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV 1
99 #define EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT 0
100 #define EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK 0xff
102 #define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT 6
103 #define EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK 0x1f
104 #define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT 0
105 #define EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK 0x1f
107 #define EQOS_MAC_MDIO_ADDRESS_PA_SHIFT 21
108 #define EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT 16
109 #define EQOS_MAC_MDIO_ADDRESS_CR_SHIFT 8
110 #define EQOS_MAC_MDIO_ADDRESS_CR_20_35 2
111 #define EQOS_MAC_MDIO_ADDRESS_CR_250_300 5
112 #define EQOS_MAC_MDIO_ADDRESS_SKAP BIT(4)
113 #define EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT 2
114 #define EQOS_MAC_MDIO_ADDRESS_GOC_READ 3
115 #define EQOS_MAC_MDIO_ADDRESS_GOC_WRITE 1
116 #define EQOS_MAC_MDIO_ADDRESS_C45E BIT(1)
117 #define EQOS_MAC_MDIO_ADDRESS_GB BIT(0)
119 #define EQOS_MAC_MDIO_DATA_GD_MASK 0xffff
121 #define EQOS_MTL_REGS_BASE 0xd00
122 struct eqos_mtl_regs {
123 uint32_t txq0_operation_mode; /* 0xd00 */
124 uint32_t unused_d04; /* 0xd04 */
125 uint32_t txq0_debug; /* 0xd08 */
126 uint32_t unused_d0c[(0xd18 - 0xd0c) / 4]; /* 0xd0c */
127 uint32_t txq0_quantum_weight; /* 0xd18 */
128 uint32_t unused_d1c[(0xd30 - 0xd1c) / 4]; /* 0xd1c */
129 uint32_t rxq0_operation_mode; /* 0xd30 */
130 uint32_t unused_d34; /* 0xd34 */
131 uint32_t rxq0_debug; /* 0xd38 */
134 #define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT 16
135 #define EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK 0x1ff
136 #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT 2
137 #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_MASK 3
138 #define EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED 2
139 #define EQOS_MTL_TXQ0_OPERATION_MODE_TSF BIT(1)
140 #define EQOS_MTL_TXQ0_OPERATION_MODE_FTQ BIT(0)
142 #define EQOS_MTL_TXQ0_DEBUG_TXQSTS BIT(4)
143 #define EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT 1
144 #define EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK 3
146 #define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT 20
147 #define EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK 0x3ff
148 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT 14
149 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK 0x3f
150 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT 8
151 #define EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK 0x3f
152 #define EQOS_MTL_RXQ0_OPERATION_MODE_EHFC BIT(7)
153 #define EQOS_MTL_RXQ0_OPERATION_MODE_RSF BIT(5)
155 #define EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT 16
156 #define EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK 0x7fff
157 #define EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT 4
158 #define EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK 3
160 #define EQOS_DMA_REGS_BASE 0x1000
161 struct eqos_dma_regs {
162 uint32_t mode; /* 0x1000 */
163 uint32_t sysbus_mode; /* 0x1004 */
164 uint32_t unused_1008[(0x1100 - 0x1008) / 4]; /* 0x1008 */
165 uint32_t ch0_control; /* 0x1100 */
166 uint32_t ch0_tx_control; /* 0x1104 */
167 uint32_t ch0_rx_control; /* 0x1108 */
168 uint32_t unused_110c; /* 0x110c */
169 uint32_t ch0_txdesc_list_haddress; /* 0x1110 */
170 uint32_t ch0_txdesc_list_address; /* 0x1114 */
171 uint32_t ch0_rxdesc_list_haddress; /* 0x1118 */
172 uint32_t ch0_rxdesc_list_address; /* 0x111c */
173 uint32_t ch0_txdesc_tail_pointer; /* 0x1120 */
174 uint32_t unused_1124; /* 0x1124 */
175 uint32_t ch0_rxdesc_tail_pointer; /* 0x1128 */
176 uint32_t ch0_txdesc_ring_length; /* 0x112c */
177 uint32_t ch0_rxdesc_ring_length; /* 0x1130 */
180 #define EQOS_DMA_MODE_SWR BIT(0)
182 #define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT 16
183 #define EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_MASK 0xf
184 #define EQOS_DMA_SYSBUS_MODE_EAME BIT(11)
185 #define EQOS_DMA_SYSBUS_MODE_BLEN16 BIT(3)
186 #define EQOS_DMA_SYSBUS_MODE_BLEN8 BIT(2)
187 #define EQOS_DMA_SYSBUS_MODE_BLEN4 BIT(1)
189 #define EQOS_DMA_CH0_CONTROL_PBLX8 BIT(16)
191 #define EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT 16
192 #define EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK 0x3f
193 #define EQOS_DMA_CH0_TX_CONTROL_OSP BIT(4)
194 #define EQOS_DMA_CH0_TX_CONTROL_ST BIT(0)
196 #define EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT 16
197 #define EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK 0x3f
198 #define EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT 1
199 #define EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK 0x3fff
200 #define EQOS_DMA_CH0_RX_CONTROL_SR BIT(0)
202 /* These registers are Tegra186-specific */
203 #define EQOS_TEGRA186_REGS_BASE 0x8800
204 struct eqos_tegra186_regs {
205 uint32_t sdmemcomppadctrl; /* 0x8800 */
206 uint32_t auto_cal_config; /* 0x8804 */
207 uint32_t unused_8808; /* 0x8808 */
208 uint32_t auto_cal_status; /* 0x880c */
211 #define EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD BIT(31)
213 #define EQOS_AUTO_CAL_CONFIG_START BIT(31)
214 #define EQOS_AUTO_CAL_CONFIG_ENABLE BIT(29)
216 #define EQOS_AUTO_CAL_STATUS_ACTIVE BIT(31)
220 #define EQOS_DESCRIPTOR_WORDS 4
221 #define EQOS_DESCRIPTOR_SIZE (EQOS_DESCRIPTOR_WORDS * 4)
222 /* We assume ARCH_DMA_MINALIGN >= 16; 16 is the EQOS HW minimum */
223 #define EQOS_DESCRIPTOR_ALIGN ARCH_DMA_MINALIGN
224 #define EQOS_DESCRIPTORS_TX 4
225 #define EQOS_DESCRIPTORS_RX 4
226 #define EQOS_DESCRIPTORS_NUM (EQOS_DESCRIPTORS_TX + EQOS_DESCRIPTORS_RX)
227 #define EQOS_DESCRIPTORS_SIZE ALIGN(EQOS_DESCRIPTORS_NUM * \
228 EQOS_DESCRIPTOR_SIZE, ARCH_DMA_MINALIGN)
229 #define EQOS_BUFFER_ALIGN ARCH_DMA_MINALIGN
230 #define EQOS_MAX_PACKET_SIZE ALIGN(1568, ARCH_DMA_MINALIGN)
231 #define EQOS_RX_BUFFER_SIZE (EQOS_DESCRIPTORS_RX * EQOS_MAX_PACKET_SIZE)
234 * Warn if the cache-line size is larger than the descriptor size. In such
235 * cases the driver will likely fail because the CPU needs to flush the cache
236 * when requeuing RX buffers, therefore descriptors written by the hardware
237 * may be discarded. Architectures with full IO coherence, such as x86, do not
238 * experience this issue, and hence are excluded from this condition.
240 * This can be fixed by defining CONFIG_SYS_NONCACHED_MEMORY which will cause
241 * the driver to allocate descriptors from a pool of non-cached memory.
243 #if EQOS_DESCRIPTOR_SIZE < ARCH_DMA_MINALIGN
244 #if !defined(CONFIG_SYS_NONCACHED_MEMORY) && \
245 !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) && !defined(CONFIG_X86)
246 #warning Cache line size is larger than descriptor size
257 #define EQOS_DESC3_OWN BIT(31)
258 #define EQOS_DESC3_FD BIT(29)
259 #define EQOS_DESC3_LD BIT(28)
260 #define EQOS_DESC3_BUF1V BIT(24)
263 bool reg_access_always_ok;
268 phy_interface_t (*interface)(struct udevice *dev);
269 struct eqos_ops *ops;
273 void (*eqos_inval_desc)(void *desc);
274 void (*eqos_flush_desc)(void *desc);
275 void (*eqos_inval_buffer)(void *buf, size_t size);
276 void (*eqos_flush_buffer)(void *buf, size_t size);
277 int (*eqos_probe_resources)(struct udevice *dev);
278 int (*eqos_remove_resources)(struct udevice *dev);
279 int (*eqos_stop_resets)(struct udevice *dev);
280 int (*eqos_start_resets)(struct udevice *dev);
281 void (*eqos_stop_clks)(struct udevice *dev);
282 int (*eqos_start_clks)(struct udevice *dev);
283 int (*eqos_calibrate_pads)(struct udevice *dev);
284 int (*eqos_disable_calibration)(struct udevice *dev);
285 int (*eqos_set_tx_clk_speed)(struct udevice *dev);
286 ulong (*eqos_get_tick_clk_rate)(struct udevice *dev);
291 const struct eqos_config *config;
293 struct eqos_mac_regs *mac_regs;
294 struct eqos_mtl_regs *mtl_regs;
295 struct eqos_dma_regs *dma_regs;
296 struct eqos_tegra186_regs *tegra186_regs;
297 struct reset_ctl reset_ctl;
298 struct gpio_desc phy_reset_gpio;
299 struct clk clk_master_bus;
301 struct clk clk_ptp_ref;
304 struct clk clk_slave_bus;
306 struct phy_device *phy;
308 struct eqos_desc *tx_descs;
309 struct eqos_desc *rx_descs;
310 int tx_desc_idx, rx_desc_idx;
319 * TX and RX descriptors are 16 bytes. This causes problems with the cache
320 * maintenance on CPUs where the cache-line size exceeds the size of these
321 * descriptors. What will happen is that when the driver receives a packet
322 * it will be immediately requeued for the hardware to reuse. The CPU will
323 * therefore need to flush the cache-line containing the descriptor, which
324 * will cause all other descriptors in the same cache-line to be flushed
325 * along with it. If one of those descriptors had been written to by the
326 * device those changes (and the associated packet) will be lost.
328 * To work around this, we make use of non-cached memory if available. If
329 * descriptors are mapped uncached there's no need to manually flush them
330 * or invalidate them.
332 * Note that this only applies to descriptors. The packet data buffers do
333 * not have the same constraints since they are 1536 bytes large, so they
334 * are unlikely to share cache-lines.
336 static void *eqos_alloc_descs(unsigned int num)
338 #ifdef CONFIG_SYS_NONCACHED_MEMORY
339 return (void *)noncached_alloc(EQOS_DESCRIPTORS_SIZE,
340 EQOS_DESCRIPTOR_ALIGN);
342 return memalign(EQOS_DESCRIPTOR_ALIGN, EQOS_DESCRIPTORS_SIZE);
346 static void eqos_free_descs(void *descs)
348 #ifdef CONFIG_SYS_NONCACHED_MEMORY
349 /* FIXME: noncached_alloc() has no opposite */
355 static void eqos_inval_desc_tegra186(void *desc)
357 #ifndef CONFIG_SYS_NONCACHED_MEMORY
358 unsigned long start = (unsigned long)desc & ~(ARCH_DMA_MINALIGN - 1);
359 unsigned long end = ALIGN(start + EQOS_DESCRIPTOR_SIZE,
362 invalidate_dcache_range(start, end);
366 static void eqos_inval_desc_stm32(void *desc)
368 #ifndef CONFIG_SYS_NONCACHED_MEMORY
369 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
370 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
373 invalidate_dcache_range(start, end);
377 static void eqos_flush_desc_tegra186(void *desc)
379 #ifndef CONFIG_SYS_NONCACHED_MEMORY
380 flush_cache((unsigned long)desc, EQOS_DESCRIPTOR_SIZE);
384 static void eqos_flush_desc_stm32(void *desc)
386 #ifndef CONFIG_SYS_NONCACHED_MEMORY
387 unsigned long start = rounddown((unsigned long)desc, ARCH_DMA_MINALIGN);
388 unsigned long end = roundup((unsigned long)desc + EQOS_DESCRIPTOR_SIZE,
391 flush_dcache_range(start, end);
395 static void eqos_inval_buffer_tegra186(void *buf, size_t size)
397 unsigned long start = (unsigned long)buf & ~(ARCH_DMA_MINALIGN - 1);
398 unsigned long end = ALIGN(start + size, ARCH_DMA_MINALIGN);
400 invalidate_dcache_range(start, end);
403 static void eqos_inval_buffer_stm32(void *buf, size_t size)
405 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
406 unsigned long end = roundup((unsigned long)buf + size,
409 invalidate_dcache_range(start, end);
412 static void eqos_flush_buffer_tegra186(void *buf, size_t size)
414 flush_cache((unsigned long)buf, size);
417 static void eqos_flush_buffer_stm32(void *buf, size_t size)
419 unsigned long start = rounddown((unsigned long)buf, ARCH_DMA_MINALIGN);
420 unsigned long end = roundup((unsigned long)buf + size,
423 flush_dcache_range(start, end);
426 static int eqos_mdio_wait_idle(struct eqos_priv *eqos)
428 return wait_for_bit_le32(&eqos->mac_regs->mdio_address,
429 EQOS_MAC_MDIO_ADDRESS_GB, false,
433 static int eqos_mdio_read(struct mii_dev *bus, int mdio_addr, int mdio_devad,
436 struct eqos_priv *eqos = bus->priv;
440 debug("%s(dev=%p, addr=%x, reg=%d):\n", __func__, eqos->dev, mdio_addr,
443 ret = eqos_mdio_wait_idle(eqos);
445 pr_err("MDIO not idle at entry");
449 val = readl(&eqos->mac_regs->mdio_address);
450 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
451 EQOS_MAC_MDIO_ADDRESS_C45E;
452 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
453 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
454 (eqos->config->config_mac_mdio <<
455 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
456 (EQOS_MAC_MDIO_ADDRESS_GOC_READ <<
457 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
458 EQOS_MAC_MDIO_ADDRESS_GB;
459 writel(val, &eqos->mac_regs->mdio_address);
461 udelay(eqos->config->mdio_wait);
463 ret = eqos_mdio_wait_idle(eqos);
465 pr_err("MDIO read didn't complete");
469 val = readl(&eqos->mac_regs->mdio_data);
470 val &= EQOS_MAC_MDIO_DATA_GD_MASK;
472 debug("%s: val=%x\n", __func__, val);
477 static int eqos_mdio_write(struct mii_dev *bus, int mdio_addr, int mdio_devad,
478 int mdio_reg, u16 mdio_val)
480 struct eqos_priv *eqos = bus->priv;
484 debug("%s(dev=%p, addr=%x, reg=%d, val=%x):\n", __func__, eqos->dev,
485 mdio_addr, mdio_reg, mdio_val);
487 ret = eqos_mdio_wait_idle(eqos);
489 pr_err("MDIO not idle at entry");
493 writel(mdio_val, &eqos->mac_regs->mdio_data);
495 val = readl(&eqos->mac_regs->mdio_address);
496 val &= EQOS_MAC_MDIO_ADDRESS_SKAP |
497 EQOS_MAC_MDIO_ADDRESS_C45E;
498 val |= (mdio_addr << EQOS_MAC_MDIO_ADDRESS_PA_SHIFT) |
499 (mdio_reg << EQOS_MAC_MDIO_ADDRESS_RDA_SHIFT) |
500 (eqos->config->config_mac_mdio <<
501 EQOS_MAC_MDIO_ADDRESS_CR_SHIFT) |
502 (EQOS_MAC_MDIO_ADDRESS_GOC_WRITE <<
503 EQOS_MAC_MDIO_ADDRESS_GOC_SHIFT) |
504 EQOS_MAC_MDIO_ADDRESS_GB;
505 writel(val, &eqos->mac_regs->mdio_address);
507 udelay(eqos->config->mdio_wait);
509 ret = eqos_mdio_wait_idle(eqos);
511 pr_err("MDIO read didn't complete");
518 static int eqos_start_clks_tegra186(struct udevice *dev)
520 struct eqos_priv *eqos = dev_get_priv(dev);
523 debug("%s(dev=%p):\n", __func__, dev);
525 ret = clk_enable(&eqos->clk_slave_bus);
527 pr_err("clk_enable(clk_slave_bus) failed: %d", ret);
531 ret = clk_enable(&eqos->clk_master_bus);
533 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
534 goto err_disable_clk_slave_bus;
537 ret = clk_enable(&eqos->clk_rx);
539 pr_err("clk_enable(clk_rx) failed: %d", ret);
540 goto err_disable_clk_master_bus;
543 ret = clk_enable(&eqos->clk_ptp_ref);
545 pr_err("clk_enable(clk_ptp_ref) failed: %d", ret);
546 goto err_disable_clk_rx;
549 ret = clk_set_rate(&eqos->clk_ptp_ref, 125 * 1000 * 1000);
551 pr_err("clk_set_rate(clk_ptp_ref) failed: %d", ret);
552 goto err_disable_clk_ptp_ref;
555 ret = clk_enable(&eqos->clk_tx);
557 pr_err("clk_enable(clk_tx) failed: %d", ret);
558 goto err_disable_clk_ptp_ref;
561 debug("%s: OK\n", __func__);
564 err_disable_clk_ptp_ref:
565 clk_disable(&eqos->clk_ptp_ref);
567 clk_disable(&eqos->clk_rx);
568 err_disable_clk_master_bus:
569 clk_disable(&eqos->clk_master_bus);
570 err_disable_clk_slave_bus:
571 clk_disable(&eqos->clk_slave_bus);
573 debug("%s: FAILED: %d\n", __func__, ret);
577 static int eqos_start_clks_stm32(struct udevice *dev)
579 struct eqos_priv *eqos = dev_get_priv(dev);
582 debug("%s(dev=%p):\n", __func__, dev);
584 ret = clk_enable(&eqos->clk_master_bus);
586 pr_err("clk_enable(clk_master_bus) failed: %d", ret);
590 ret = clk_enable(&eqos->clk_rx);
592 pr_err("clk_enable(clk_rx) failed: %d", ret);
593 goto err_disable_clk_master_bus;
596 ret = clk_enable(&eqos->clk_tx);
598 pr_err("clk_enable(clk_tx) failed: %d", ret);
599 goto err_disable_clk_rx;
602 if (clk_valid(&eqos->clk_ck)) {
603 ret = clk_enable(&eqos->clk_ck);
605 pr_err("clk_enable(clk_ck) failed: %d", ret);
606 goto err_disable_clk_tx;
610 debug("%s: OK\n", __func__);
614 clk_disable(&eqos->clk_tx);
616 clk_disable(&eqos->clk_rx);
617 err_disable_clk_master_bus:
618 clk_disable(&eqos->clk_master_bus);
620 debug("%s: FAILED: %d\n", __func__, ret);
624 void eqos_stop_clks_tegra186(struct udevice *dev)
626 struct eqos_priv *eqos = dev_get_priv(dev);
628 debug("%s(dev=%p):\n", __func__, dev);
630 clk_disable(&eqos->clk_tx);
631 clk_disable(&eqos->clk_ptp_ref);
632 clk_disable(&eqos->clk_rx);
633 clk_disable(&eqos->clk_master_bus);
634 clk_disable(&eqos->clk_slave_bus);
636 debug("%s: OK\n", __func__);
639 void eqos_stop_clks_stm32(struct udevice *dev)
641 struct eqos_priv *eqos = dev_get_priv(dev);
643 debug("%s(dev=%p):\n", __func__, dev);
645 clk_disable(&eqos->clk_tx);
646 clk_disable(&eqos->clk_rx);
647 clk_disable(&eqos->clk_master_bus);
648 if (clk_valid(&eqos->clk_ck))
649 clk_disable(&eqos->clk_ck);
651 debug("%s: OK\n", __func__);
654 static int eqos_start_resets_tegra186(struct udevice *dev)
656 struct eqos_priv *eqos = dev_get_priv(dev);
659 debug("%s(dev=%p):\n", __func__, dev);
661 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
663 pr_err("dm_gpio_set_value(phy_reset, assert) failed: %d", ret);
669 ret = dm_gpio_set_value(&eqos->phy_reset_gpio, 0);
671 pr_err("dm_gpio_set_value(phy_reset, deassert) failed: %d", ret);
675 ret = reset_assert(&eqos->reset_ctl);
677 pr_err("reset_assert() failed: %d", ret);
683 ret = reset_deassert(&eqos->reset_ctl);
685 pr_err("reset_deassert() failed: %d", ret);
689 debug("%s: OK\n", __func__);
693 static int eqos_start_resets_stm32(struct udevice *dev)
698 static int eqos_stop_resets_tegra186(struct udevice *dev)
700 struct eqos_priv *eqos = dev_get_priv(dev);
702 reset_assert(&eqos->reset_ctl);
703 dm_gpio_set_value(&eqos->phy_reset_gpio, 1);
708 static int eqos_stop_resets_stm32(struct udevice *dev)
713 static int eqos_calibrate_pads_tegra186(struct udevice *dev)
715 struct eqos_priv *eqos = dev_get_priv(dev);
718 debug("%s(dev=%p):\n", __func__, dev);
720 setbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
721 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
725 setbits_le32(&eqos->tegra186_regs->auto_cal_config,
726 EQOS_AUTO_CAL_CONFIG_START | EQOS_AUTO_CAL_CONFIG_ENABLE);
728 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
729 EQOS_AUTO_CAL_STATUS_ACTIVE, true, 10, false);
731 pr_err("calibrate didn't start");
735 ret = wait_for_bit_le32(&eqos->tegra186_regs->auto_cal_status,
736 EQOS_AUTO_CAL_STATUS_ACTIVE, false, 10, false);
738 pr_err("calibrate didn't finish");
745 clrbits_le32(&eqos->tegra186_regs->sdmemcomppadctrl,
746 EQOS_SDMEMCOMPPADCTRL_PAD_E_INPUT_OR_E_PWRD);
748 debug("%s: returns %d\n", __func__, ret);
753 static int eqos_disable_calibration_tegra186(struct udevice *dev)
755 struct eqos_priv *eqos = dev_get_priv(dev);
757 debug("%s(dev=%p):\n", __func__, dev);
759 clrbits_le32(&eqos->tegra186_regs->auto_cal_config,
760 EQOS_AUTO_CAL_CONFIG_ENABLE);
765 static ulong eqos_get_tick_clk_rate_tegra186(struct udevice *dev)
767 struct eqos_priv *eqos = dev_get_priv(dev);
769 return clk_get_rate(&eqos->clk_slave_bus);
772 static ulong eqos_get_tick_clk_rate_stm32(struct udevice *dev)
774 struct eqos_priv *eqos = dev_get_priv(dev);
776 return clk_get_rate(&eqos->clk_master_bus);
779 static int eqos_calibrate_pads_stm32(struct udevice *dev)
784 static int eqos_disable_calibration_stm32(struct udevice *dev)
789 static int eqos_set_full_duplex(struct udevice *dev)
791 struct eqos_priv *eqos = dev_get_priv(dev);
793 debug("%s(dev=%p):\n", __func__, dev);
795 setbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
800 static int eqos_set_half_duplex(struct udevice *dev)
802 struct eqos_priv *eqos = dev_get_priv(dev);
804 debug("%s(dev=%p):\n", __func__, dev);
806 clrbits_le32(&eqos->mac_regs->configuration, EQOS_MAC_CONFIGURATION_DM);
808 /* WAR: Flush TX queue when switching to half-duplex */
809 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
810 EQOS_MTL_TXQ0_OPERATION_MODE_FTQ);
815 static int eqos_set_gmii_speed(struct udevice *dev)
817 struct eqos_priv *eqos = dev_get_priv(dev);
819 debug("%s(dev=%p):\n", __func__, dev);
821 clrbits_le32(&eqos->mac_regs->configuration,
822 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
827 static int eqos_set_mii_speed_100(struct udevice *dev)
829 struct eqos_priv *eqos = dev_get_priv(dev);
831 debug("%s(dev=%p):\n", __func__, dev);
833 setbits_le32(&eqos->mac_regs->configuration,
834 EQOS_MAC_CONFIGURATION_PS | EQOS_MAC_CONFIGURATION_FES);
839 static int eqos_set_mii_speed_10(struct udevice *dev)
841 struct eqos_priv *eqos = dev_get_priv(dev);
843 debug("%s(dev=%p):\n", __func__, dev);
845 clrsetbits_le32(&eqos->mac_regs->configuration,
846 EQOS_MAC_CONFIGURATION_FES, EQOS_MAC_CONFIGURATION_PS);
851 static int eqos_set_tx_clk_speed_tegra186(struct udevice *dev)
853 struct eqos_priv *eqos = dev_get_priv(dev);
857 debug("%s(dev=%p):\n", __func__, dev);
859 switch (eqos->phy->speed) {
861 rate = 125 * 1000 * 1000;
864 rate = 25 * 1000 * 1000;
867 rate = 2.5 * 1000 * 1000;
870 pr_err("invalid speed %d", eqos->phy->speed);
874 ret = clk_set_rate(&eqos->clk_tx, rate);
876 pr_err("clk_set_rate(tx_clk, %lu) failed: %d", rate, ret);
883 static int eqos_set_tx_clk_speed_stm32(struct udevice *dev)
888 static int eqos_adjust_link(struct udevice *dev)
890 struct eqos_priv *eqos = dev_get_priv(dev);
894 debug("%s(dev=%p):\n", __func__, dev);
896 if (eqos->phy->duplex)
897 ret = eqos_set_full_duplex(dev);
899 ret = eqos_set_half_duplex(dev);
901 pr_err("eqos_set_*_duplex() failed: %d", ret);
905 switch (eqos->phy->speed) {
907 en_calibration = true;
908 ret = eqos_set_gmii_speed(dev);
911 en_calibration = true;
912 ret = eqos_set_mii_speed_100(dev);
915 en_calibration = false;
916 ret = eqos_set_mii_speed_10(dev);
919 pr_err("invalid speed %d", eqos->phy->speed);
923 pr_err("eqos_set_*mii_speed*() failed: %d", ret);
927 if (en_calibration) {
928 ret = eqos->config->ops->eqos_calibrate_pads(dev);
930 pr_err("eqos_calibrate_pads() failed: %d",
935 ret = eqos->config->ops->eqos_disable_calibration(dev);
937 pr_err("eqos_disable_calibration() failed: %d",
942 ret = eqos->config->ops->eqos_set_tx_clk_speed(dev);
944 pr_err("eqos_set_tx_clk_speed() failed: %d", ret);
951 static int eqos_write_hwaddr(struct udevice *dev)
953 struct eth_pdata *plat = dev_get_platdata(dev);
954 struct eqos_priv *eqos = dev_get_priv(dev);
958 * This function may be called before start() or after stop(). At that
959 * time, on at least some configurations of the EQoS HW, all clocks to
960 * the EQoS HW block will be stopped, and a reset signal applied. If
961 * any register access is attempted in this state, bus timeouts or CPU
962 * hangs may occur. This check prevents that.
964 * A simple solution to this problem would be to not implement
965 * write_hwaddr(), since start() always writes the MAC address into HW
966 * anyway. However, it is desirable to implement write_hwaddr() to
967 * support the case of SW that runs subsequent to U-Boot which expects
968 * the MAC address to already be programmed into the EQoS registers,
969 * which must happen irrespective of whether the U-Boot user (or
970 * scripts) actually made use of the EQoS device, and hence
971 * irrespective of whether start() was ever called.
973 * Note that this requirement by subsequent SW is not valid for
974 * Tegra186, and is likely not valid for any non-PCI instantiation of
975 * the EQoS HW block. This function is implemented solely as
976 * future-proofing with the expectation the driver will eventually be
977 * ported to some system where the expectation above is true.
979 if (!eqos->config->reg_access_always_ok && !eqos->reg_access_ok)
982 /* Update the MAC address */
983 val = (plat->enetaddr[5] << 8) |
985 writel(val, &eqos->mac_regs->address0_high);
986 val = (plat->enetaddr[3] << 24) |
987 (plat->enetaddr[2] << 16) |
988 (plat->enetaddr[1] << 8) |
990 writel(val, &eqos->mac_regs->address0_low);
995 static int eqos_start(struct udevice *dev)
997 struct eqos_priv *eqos = dev_get_priv(dev);
1000 u32 val, tx_fifo_sz, rx_fifo_sz, tqs, rqs, pbl;
1003 debug("%s(dev=%p):\n", __func__, dev);
1005 eqos->tx_desc_idx = 0;
1006 eqos->rx_desc_idx = 0;
1008 ret = eqos->config->ops->eqos_start_clks(dev);
1010 pr_err("eqos_start_clks() failed: %d", ret);
1014 ret = eqos->config->ops->eqos_start_resets(dev);
1016 pr_err("eqos_start_resets() failed: %d", ret);
1022 eqos->reg_access_ok = true;
1024 ret = wait_for_bit_le32(&eqos->dma_regs->mode,
1025 EQOS_DMA_MODE_SWR, false,
1026 eqos->config->swr_wait, false);
1028 pr_err("EQOS_DMA_MODE_SWR stuck");
1029 goto err_stop_resets;
1032 ret = eqos->config->ops->eqos_calibrate_pads(dev);
1034 pr_err("eqos_calibrate_pads() failed: %d", ret);
1035 goto err_stop_resets;
1037 rate = eqos->config->ops->eqos_get_tick_clk_rate(dev);
1039 val = (rate / 1000000) - 1;
1040 writel(val, &eqos->mac_regs->us_tic_counter);
1043 * if PHY was already connected and configured,
1044 * don't need to reconnect/reconfigure again
1047 eqos->phy = phy_connect(eqos->mii, 0, dev,
1048 eqos->config->interface(dev));
1050 pr_err("phy_connect() failed");
1051 goto err_stop_resets;
1053 ret = phy_config(eqos->phy);
1055 pr_err("phy_config() failed: %d", ret);
1056 goto err_shutdown_phy;
1060 ret = phy_startup(eqos->phy);
1062 pr_err("phy_startup() failed: %d", ret);
1063 goto err_shutdown_phy;
1066 if (!eqos->phy->link) {
1068 goto err_shutdown_phy;
1071 ret = eqos_adjust_link(dev);
1073 pr_err("eqos_adjust_link() failed: %d", ret);
1074 goto err_shutdown_phy;
1079 /* Enable Store and Forward mode for TX */
1080 /* Program Tx operating mode */
1081 setbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1082 EQOS_MTL_TXQ0_OPERATION_MODE_TSF |
1083 (EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_ENABLED <<
1084 EQOS_MTL_TXQ0_OPERATION_MODE_TXQEN_SHIFT));
1086 /* Transmit Queue weight */
1087 writel(0x10, &eqos->mtl_regs->txq0_quantum_weight);
1089 /* Enable Store and Forward mode for RX, since no jumbo frame */
1090 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1091 EQOS_MTL_RXQ0_OPERATION_MODE_RSF);
1093 /* Transmit/Receive queue fifo size; use all RAM for 1 queue */
1094 val = readl(&eqos->mac_regs->hw_feature1);
1095 tx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_SHIFT) &
1096 EQOS_MAC_HW_FEATURE1_TXFIFOSIZE_MASK;
1097 rx_fifo_sz = (val >> EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_SHIFT) &
1098 EQOS_MAC_HW_FEATURE1_RXFIFOSIZE_MASK;
1101 * r/tx_fifo_sz is encoded as log2(n / 128). Undo that by shifting.
1102 * r/tqs is encoded as (n / 256) - 1.
1104 tqs = (128 << tx_fifo_sz) / 256 - 1;
1105 rqs = (128 << rx_fifo_sz) / 256 - 1;
1107 clrsetbits_le32(&eqos->mtl_regs->txq0_operation_mode,
1108 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_MASK <<
1109 EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT,
1110 tqs << EQOS_MTL_TXQ0_OPERATION_MODE_TQS_SHIFT);
1111 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1112 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_MASK <<
1113 EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT,
1114 rqs << EQOS_MTL_RXQ0_OPERATION_MODE_RQS_SHIFT);
1116 /* Flow control used only if each channel gets 4KB or more FIFO */
1117 if (rqs >= ((4096 / 256) - 1)) {
1120 setbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1121 EQOS_MTL_RXQ0_OPERATION_MODE_EHFC);
1124 * Set Threshold for Activating Flow Contol space for min 2
1125 * frames ie, (1500 * 1) = 1500 bytes.
1127 * Set Threshold for Deactivating Flow Contol for space of
1128 * min 1 frame (frame size 1500bytes) in receive fifo
1130 if (rqs == ((4096 / 256) - 1)) {
1132 * This violates the above formula because of FIFO size
1133 * limit therefore overflow may occur inspite of this.
1135 rfd = 0x3; /* Full-3K */
1136 rfa = 0x1; /* Full-1.5K */
1137 } else if (rqs == ((8192 / 256) - 1)) {
1138 rfd = 0x6; /* Full-4K */
1139 rfa = 0xa; /* Full-6K */
1140 } else if (rqs == ((16384 / 256) - 1)) {
1141 rfd = 0x6; /* Full-4K */
1142 rfa = 0x12; /* Full-10K */
1144 rfd = 0x6; /* Full-4K */
1145 rfa = 0x1E; /* Full-16K */
1148 clrsetbits_le32(&eqos->mtl_regs->rxq0_operation_mode,
1149 (EQOS_MTL_RXQ0_OPERATION_MODE_RFD_MASK <<
1150 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1151 (EQOS_MTL_RXQ0_OPERATION_MODE_RFA_MASK <<
1152 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT),
1154 EQOS_MTL_RXQ0_OPERATION_MODE_RFD_SHIFT) |
1156 EQOS_MTL_RXQ0_OPERATION_MODE_RFA_SHIFT));
1161 clrsetbits_le32(&eqos->mac_regs->rxq_ctrl0,
1162 EQOS_MAC_RXQ_CTRL0_RXQ0EN_MASK <<
1163 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT,
1164 eqos->config->config_mac <<
1165 EQOS_MAC_RXQ_CTRL0_RXQ0EN_SHIFT);
1167 /* Set TX flow control parameters */
1168 /* Set Pause Time */
1169 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1170 0xffff << EQOS_MAC_Q0_TX_FLOW_CTRL_PT_SHIFT);
1171 /* Assign priority for TX flow control */
1172 clrbits_le32(&eqos->mac_regs->txq_prty_map0,
1173 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_MASK <<
1174 EQOS_MAC_TXQ_PRTY_MAP0_PSTQ0_SHIFT);
1175 /* Assign priority for RX flow control */
1176 clrbits_le32(&eqos->mac_regs->rxq_ctrl2,
1177 EQOS_MAC_RXQ_CTRL2_PSRQ0_MASK <<
1178 EQOS_MAC_RXQ_CTRL2_PSRQ0_SHIFT);
1179 /* Enable flow control */
1180 setbits_le32(&eqos->mac_regs->q0_tx_flow_ctrl,
1181 EQOS_MAC_Q0_TX_FLOW_CTRL_TFE);
1182 setbits_le32(&eqos->mac_regs->rx_flow_ctrl,
1183 EQOS_MAC_RX_FLOW_CTRL_RFE);
1185 clrsetbits_le32(&eqos->mac_regs->configuration,
1186 EQOS_MAC_CONFIGURATION_GPSLCE |
1187 EQOS_MAC_CONFIGURATION_WD |
1188 EQOS_MAC_CONFIGURATION_JD |
1189 EQOS_MAC_CONFIGURATION_JE,
1190 EQOS_MAC_CONFIGURATION_CST |
1191 EQOS_MAC_CONFIGURATION_ACS);
1193 eqos_write_hwaddr(dev);
1197 /* Enable OSP mode */
1198 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1199 EQOS_DMA_CH0_TX_CONTROL_OSP);
1201 /* RX buffer size. Must be a multiple of bus width */
1202 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1203 EQOS_DMA_CH0_RX_CONTROL_RBSZ_MASK <<
1204 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT,
1205 EQOS_MAX_PACKET_SIZE <<
1206 EQOS_DMA_CH0_RX_CONTROL_RBSZ_SHIFT);
1208 setbits_le32(&eqos->dma_regs->ch0_control,
1209 EQOS_DMA_CH0_CONTROL_PBLX8);
1212 * Burst length must be < 1/2 FIFO size.
1213 * FIFO size in tqs is encoded as (n / 256) - 1.
1214 * Each burst is n * 8 (PBLX8) * 16 (AXI width) == 128 bytes.
1215 * Half of n * 256 is n * 128, so pbl == tqs, modulo the -1.
1220 clrsetbits_le32(&eqos->dma_regs->ch0_tx_control,
1221 EQOS_DMA_CH0_TX_CONTROL_TXPBL_MASK <<
1222 EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT,
1223 pbl << EQOS_DMA_CH0_TX_CONTROL_TXPBL_SHIFT);
1225 clrsetbits_le32(&eqos->dma_regs->ch0_rx_control,
1226 EQOS_DMA_CH0_RX_CONTROL_RXPBL_MASK <<
1227 EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT,
1228 8 << EQOS_DMA_CH0_RX_CONTROL_RXPBL_SHIFT);
1230 /* DMA performance configuration */
1231 val = (2 << EQOS_DMA_SYSBUS_MODE_RD_OSR_LMT_SHIFT) |
1232 EQOS_DMA_SYSBUS_MODE_EAME | EQOS_DMA_SYSBUS_MODE_BLEN16 |
1233 EQOS_DMA_SYSBUS_MODE_BLEN8 | EQOS_DMA_SYSBUS_MODE_BLEN4;
1234 writel(val, &eqos->dma_regs->sysbus_mode);
1236 /* Set up descriptors */
1238 memset(eqos->descs, 0, EQOS_DESCRIPTORS_SIZE);
1239 for (i = 0; i < EQOS_DESCRIPTORS_RX; i++) {
1240 struct eqos_desc *rx_desc = &(eqos->rx_descs[i]);
1241 rx_desc->des0 = (u32)(ulong)(eqos->rx_dma_buf +
1242 (i * EQOS_MAX_PACKET_SIZE));
1243 rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1245 eqos->config->ops->eqos_flush_desc(eqos->descs);
1247 writel(0, &eqos->dma_regs->ch0_txdesc_list_haddress);
1248 writel((ulong)eqos->tx_descs, &eqos->dma_regs->ch0_txdesc_list_address);
1249 writel(EQOS_DESCRIPTORS_TX - 1,
1250 &eqos->dma_regs->ch0_txdesc_ring_length);
1252 writel(0, &eqos->dma_regs->ch0_rxdesc_list_haddress);
1253 writel((ulong)eqos->rx_descs, &eqos->dma_regs->ch0_rxdesc_list_address);
1254 writel(EQOS_DESCRIPTORS_RX - 1,
1255 &eqos->dma_regs->ch0_rxdesc_ring_length);
1257 /* Enable everything */
1259 setbits_le32(&eqos->mac_regs->configuration,
1260 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1262 setbits_le32(&eqos->dma_regs->ch0_tx_control,
1263 EQOS_DMA_CH0_TX_CONTROL_ST);
1264 setbits_le32(&eqos->dma_regs->ch0_rx_control,
1265 EQOS_DMA_CH0_RX_CONTROL_SR);
1267 /* TX tail pointer not written until we need to TX a packet */
1269 * Point RX tail pointer at last descriptor. Ideally, we'd point at the
1270 * first descriptor, implying all descriptors were available. However,
1271 * that's not distinguishable from none of the descriptors being
1274 last_rx_desc = (ulong)&(eqos->rx_descs[(EQOS_DESCRIPTORS_RX - 1)]);
1275 writel(last_rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1277 eqos->started = true;
1279 debug("%s: OK\n", __func__);
1283 phy_shutdown(eqos->phy);
1285 eqos->config->ops->eqos_stop_resets(dev);
1287 eqos->config->ops->eqos_stop_clks(dev);
1289 pr_err("FAILED: %d", ret);
1293 void eqos_stop(struct udevice *dev)
1295 struct eqos_priv *eqos = dev_get_priv(dev);
1298 debug("%s(dev=%p):\n", __func__, dev);
1302 eqos->started = false;
1303 eqos->reg_access_ok = false;
1305 /* Disable TX DMA */
1306 clrbits_le32(&eqos->dma_regs->ch0_tx_control,
1307 EQOS_DMA_CH0_TX_CONTROL_ST);
1309 /* Wait for TX all packets to drain out of MTL */
1310 for (i = 0; i < 1000000; i++) {
1311 u32 val = readl(&eqos->mtl_regs->txq0_debug);
1312 u32 trcsts = (val >> EQOS_MTL_TXQ0_DEBUG_TRCSTS_SHIFT) &
1313 EQOS_MTL_TXQ0_DEBUG_TRCSTS_MASK;
1314 u32 txqsts = val & EQOS_MTL_TXQ0_DEBUG_TXQSTS;
1315 if ((trcsts != 1) && (!txqsts))
1319 /* Turn off MAC TX and RX */
1320 clrbits_le32(&eqos->mac_regs->configuration,
1321 EQOS_MAC_CONFIGURATION_TE | EQOS_MAC_CONFIGURATION_RE);
1323 /* Wait for all RX packets to drain out of MTL */
1324 for (i = 0; i < 1000000; i++) {
1325 u32 val = readl(&eqos->mtl_regs->rxq0_debug);
1326 u32 prxq = (val >> EQOS_MTL_RXQ0_DEBUG_PRXQ_SHIFT) &
1327 EQOS_MTL_RXQ0_DEBUG_PRXQ_MASK;
1328 u32 rxqsts = (val >> EQOS_MTL_RXQ0_DEBUG_RXQSTS_SHIFT) &
1329 EQOS_MTL_RXQ0_DEBUG_RXQSTS_MASK;
1330 if ((!prxq) && (!rxqsts))
1334 /* Turn off RX DMA */
1335 clrbits_le32(&eqos->dma_regs->ch0_rx_control,
1336 EQOS_DMA_CH0_RX_CONTROL_SR);
1339 phy_shutdown(eqos->phy);
1341 eqos->config->ops->eqos_stop_resets(dev);
1342 eqos->config->ops->eqos_stop_clks(dev);
1344 debug("%s: OK\n", __func__);
1347 int eqos_send(struct udevice *dev, void *packet, int length)
1349 struct eqos_priv *eqos = dev_get_priv(dev);
1350 struct eqos_desc *tx_desc;
1353 debug("%s(dev=%p, packet=%p, length=%d):\n", __func__, dev, packet,
1356 memcpy(eqos->tx_dma_buf, packet, length);
1357 eqos->config->ops->eqos_flush_buffer(eqos->tx_dma_buf, length);
1359 tx_desc = &(eqos->tx_descs[eqos->tx_desc_idx]);
1360 eqos->tx_desc_idx++;
1361 eqos->tx_desc_idx %= EQOS_DESCRIPTORS_TX;
1363 tx_desc->des0 = (ulong)eqos->tx_dma_buf;
1365 tx_desc->des2 = length;
1367 * Make sure that if HW sees the _OWN write below, it will see all the
1368 * writes to the rest of the descriptor too.
1371 tx_desc->des3 = EQOS_DESC3_OWN | EQOS_DESC3_FD | EQOS_DESC3_LD | length;
1372 eqos->config->ops->eqos_flush_desc(tx_desc);
1374 writel((ulong)(tx_desc + 1), &eqos->dma_regs->ch0_txdesc_tail_pointer);
1376 for (i = 0; i < 1000000; i++) {
1377 eqos->config->ops->eqos_inval_desc(tx_desc);
1378 if (!(readl(&tx_desc->des3) & EQOS_DESC3_OWN))
1383 debug("%s: TX timeout\n", __func__);
1388 int eqos_recv(struct udevice *dev, int flags, uchar **packetp)
1390 struct eqos_priv *eqos = dev_get_priv(dev);
1391 struct eqos_desc *rx_desc;
1394 debug("%s(dev=%p, flags=%x):\n", __func__, dev, flags);
1396 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1397 if (rx_desc->des3 & EQOS_DESC3_OWN) {
1398 debug("%s: RX packet not available\n", __func__);
1402 *packetp = eqos->rx_dma_buf +
1403 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1404 length = rx_desc->des3 & 0x7fff;
1405 debug("%s: *packetp=%p, length=%d\n", __func__, *packetp, length);
1407 eqos->config->ops->eqos_inval_buffer(*packetp, length);
1412 int eqos_free_pkt(struct udevice *dev, uchar *packet, int length)
1414 struct eqos_priv *eqos = dev_get_priv(dev);
1415 uchar *packet_expected;
1416 struct eqos_desc *rx_desc;
1418 debug("%s(packet=%p, length=%d)\n", __func__, packet, length);
1420 packet_expected = eqos->rx_dma_buf +
1421 (eqos->rx_desc_idx * EQOS_MAX_PACKET_SIZE);
1422 if (packet != packet_expected) {
1423 debug("%s: Unexpected packet (expected %p)\n", __func__,
1428 rx_desc = &(eqos->rx_descs[eqos->rx_desc_idx]);
1429 rx_desc->des0 = (u32)(ulong)packet;
1433 * Make sure that if HW sees the _OWN write below, it will see all the
1434 * writes to the rest of the descriptor too.
1437 rx_desc->des3 |= EQOS_DESC3_OWN | EQOS_DESC3_BUF1V;
1438 eqos->config->ops->eqos_flush_desc(rx_desc);
1440 writel((ulong)rx_desc, &eqos->dma_regs->ch0_rxdesc_tail_pointer);
1442 eqos->rx_desc_idx++;
1443 eqos->rx_desc_idx %= EQOS_DESCRIPTORS_RX;
1448 static int eqos_probe_resources_core(struct udevice *dev)
1450 struct eqos_priv *eqos = dev_get_priv(dev);
1453 debug("%s(dev=%p):\n", __func__, dev);
1455 eqos->descs = eqos_alloc_descs(EQOS_DESCRIPTORS_TX +
1456 EQOS_DESCRIPTORS_RX);
1458 debug("%s: eqos_alloc_descs() failed\n", __func__);
1462 eqos->tx_descs = (struct eqos_desc *)eqos->descs;
1463 eqos->rx_descs = (eqos->tx_descs + EQOS_DESCRIPTORS_TX);
1464 debug("%s: tx_descs=%p, rx_descs=%p\n", __func__, eqos->tx_descs,
1467 eqos->tx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_MAX_PACKET_SIZE);
1468 if (!eqos->tx_dma_buf) {
1469 debug("%s: memalign(tx_dma_buf) failed\n", __func__);
1471 goto err_free_descs;
1473 debug("%s: tx_dma_buf=%p\n", __func__, eqos->tx_dma_buf);
1475 eqos->rx_dma_buf = memalign(EQOS_BUFFER_ALIGN, EQOS_RX_BUFFER_SIZE);
1476 if (!eqos->rx_dma_buf) {
1477 debug("%s: memalign(rx_dma_buf) failed\n", __func__);
1479 goto err_free_tx_dma_buf;
1481 debug("%s: rx_dma_buf=%p\n", __func__, eqos->rx_dma_buf);
1483 eqos->rx_pkt = malloc(EQOS_MAX_PACKET_SIZE);
1484 if (!eqos->rx_pkt) {
1485 debug("%s: malloc(rx_pkt) failed\n", __func__);
1487 goto err_free_rx_dma_buf;
1489 debug("%s: rx_pkt=%p\n", __func__, eqos->rx_pkt);
1491 debug("%s: OK\n", __func__);
1494 err_free_rx_dma_buf:
1495 free(eqos->rx_dma_buf);
1496 err_free_tx_dma_buf:
1497 free(eqos->tx_dma_buf);
1499 eqos_free_descs(eqos->descs);
1502 debug("%s: returns %d\n", __func__, ret);
1506 static int eqos_remove_resources_core(struct udevice *dev)
1508 struct eqos_priv *eqos = dev_get_priv(dev);
1510 debug("%s(dev=%p):\n", __func__, dev);
1513 free(eqos->rx_dma_buf);
1514 free(eqos->tx_dma_buf);
1515 eqos_free_descs(eqos->descs);
1517 debug("%s: OK\n", __func__);
1521 static int eqos_probe_resources_tegra186(struct udevice *dev)
1523 struct eqos_priv *eqos = dev_get_priv(dev);
1526 debug("%s(dev=%p):\n", __func__, dev);
1528 ret = reset_get_by_name(dev, "eqos", &eqos->reset_ctl);
1530 pr_err("reset_get_by_name(rst) failed: %d", ret);
1534 ret = gpio_request_by_name(dev, "phy-reset-gpios", 0,
1535 &eqos->phy_reset_gpio,
1536 GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
1538 pr_err("gpio_request_by_name(phy reset) failed: %d", ret);
1539 goto err_free_reset_eqos;
1542 ret = clk_get_by_name(dev, "slave_bus", &eqos->clk_slave_bus);
1544 pr_err("clk_get_by_name(slave_bus) failed: %d", ret);
1545 goto err_free_gpio_phy_reset;
1548 ret = clk_get_by_name(dev, "master_bus", &eqos->clk_master_bus);
1550 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1551 goto err_free_clk_slave_bus;
1554 ret = clk_get_by_name(dev, "rx", &eqos->clk_rx);
1556 pr_err("clk_get_by_name(rx) failed: %d", ret);
1557 goto err_free_clk_master_bus;
1560 ret = clk_get_by_name(dev, "ptp_ref", &eqos->clk_ptp_ref);
1562 pr_err("clk_get_by_name(ptp_ref) failed: %d", ret);
1563 goto err_free_clk_rx;
1567 ret = clk_get_by_name(dev, "tx", &eqos->clk_tx);
1569 pr_err("clk_get_by_name(tx) failed: %d", ret);
1570 goto err_free_clk_ptp_ref;
1573 debug("%s: OK\n", __func__);
1576 err_free_clk_ptp_ref:
1577 clk_free(&eqos->clk_ptp_ref);
1579 clk_free(&eqos->clk_rx);
1580 err_free_clk_master_bus:
1581 clk_free(&eqos->clk_master_bus);
1582 err_free_clk_slave_bus:
1583 clk_free(&eqos->clk_slave_bus);
1584 err_free_gpio_phy_reset:
1585 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1586 err_free_reset_eqos:
1587 reset_free(&eqos->reset_ctl);
1589 debug("%s: returns %d\n", __func__, ret);
1593 /* board-specific Ethernet Interface initializations. */
1594 __weak int board_interface_eth_init(int interface_type, bool eth_clk_sel_reg,
1595 bool eth_ref_clk_sel_reg)
1600 static int eqos_probe_resources_stm32(struct udevice *dev)
1602 struct eqos_priv *eqos = dev_get_priv(dev);
1604 phy_interface_t interface;
1605 bool eth_clk_sel_reg = false;
1606 bool eth_ref_clk_sel_reg = false;
1608 debug("%s(dev=%p):\n", __func__, dev);
1610 interface = eqos->config->interface(dev);
1612 if (interface == PHY_INTERFACE_MODE_NONE) {
1613 pr_err("Invalid PHY interface\n");
1617 /* Gigabit Ethernet 125MHz clock selection. */
1618 eth_clk_sel_reg = dev_read_bool(dev, "st,eth_clk_sel");
1620 /* Ethernet 50Mhz RMII clock selection */
1621 eth_ref_clk_sel_reg =
1622 dev_read_bool(dev, "st,eth_ref_clk_sel");
1624 ret = board_interface_eth_init(interface, eth_clk_sel_reg,
1625 eth_ref_clk_sel_reg);
1629 ret = clk_get_by_name(dev, "stmmaceth", &eqos->clk_master_bus);
1631 pr_err("clk_get_by_name(master_bus) failed: %d", ret);
1635 ret = clk_get_by_name(dev, "mac-clk-rx", &eqos->clk_rx);
1637 pr_err("clk_get_by_name(rx) failed: %d", ret);
1638 goto err_free_clk_master_bus;
1641 ret = clk_get_by_name(dev, "mac-clk-tx", &eqos->clk_tx);
1643 pr_err("clk_get_by_name(tx) failed: %d", ret);
1644 goto err_free_clk_rx;
1647 /* Get ETH_CLK clocks (optional) */
1648 ret = clk_get_by_name(dev, "eth-ck", &eqos->clk_ck);
1650 pr_warn("No phy clock provided %d", ret);
1652 debug("%s: OK\n", __func__);
1656 clk_free(&eqos->clk_rx);
1657 err_free_clk_master_bus:
1658 clk_free(&eqos->clk_master_bus);
1661 debug("%s: returns %d\n", __func__, ret);
1665 static phy_interface_t eqos_get_interface_stm32(struct udevice *dev)
1667 const char *phy_mode;
1668 phy_interface_t interface = PHY_INTERFACE_MODE_NONE;
1670 debug("%s(dev=%p):\n", __func__, dev);
1672 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1675 interface = phy_get_interface_by_name(phy_mode);
1680 static phy_interface_t eqos_get_interface_tegra186(struct udevice *dev)
1682 return PHY_INTERFACE_MODE_MII;
1685 static int eqos_remove_resources_tegra186(struct udevice *dev)
1687 struct eqos_priv *eqos = dev_get_priv(dev);
1689 debug("%s(dev=%p):\n", __func__, dev);
1691 clk_free(&eqos->clk_tx);
1692 clk_free(&eqos->clk_ptp_ref);
1693 clk_free(&eqos->clk_rx);
1694 clk_free(&eqos->clk_slave_bus);
1695 clk_free(&eqos->clk_master_bus);
1696 dm_gpio_free(dev, &eqos->phy_reset_gpio);
1697 reset_free(&eqos->reset_ctl);
1699 debug("%s: OK\n", __func__);
1703 static int eqos_remove_resources_stm32(struct udevice *dev)
1705 struct eqos_priv *eqos = dev_get_priv(dev);
1707 debug("%s(dev=%p):\n", __func__, dev);
1709 clk_free(&eqos->clk_tx);
1710 clk_free(&eqos->clk_rx);
1711 clk_free(&eqos->clk_master_bus);
1712 if (clk_valid(&eqos->clk_ck))
1713 clk_free(&eqos->clk_ck);
1715 debug("%s: OK\n", __func__);
1719 static int eqos_probe(struct udevice *dev)
1721 struct eqos_priv *eqos = dev_get_priv(dev);
1724 debug("%s(dev=%p):\n", __func__, dev);
1727 eqos->config = (void *)dev_get_driver_data(dev);
1729 eqos->regs = devfdt_get_addr(dev);
1730 if (eqos->regs == FDT_ADDR_T_NONE) {
1731 pr_err("devfdt_get_addr() failed");
1734 eqos->mac_regs = (void *)(eqos->regs + EQOS_MAC_REGS_BASE);
1735 eqos->mtl_regs = (void *)(eqos->regs + EQOS_MTL_REGS_BASE);
1736 eqos->dma_regs = (void *)(eqos->regs + EQOS_DMA_REGS_BASE);
1737 eqos->tegra186_regs = (void *)(eqos->regs + EQOS_TEGRA186_REGS_BASE);
1739 ret = eqos_probe_resources_core(dev);
1741 pr_err("eqos_probe_resources_core() failed: %d", ret);
1745 ret = eqos->config->ops->eqos_probe_resources(dev);
1747 pr_err("eqos_probe_resources() failed: %d", ret);
1748 goto err_remove_resources_core;
1751 eqos->mii = mdio_alloc();
1753 pr_err("mdio_alloc() failed");
1755 goto err_remove_resources_tegra;
1757 eqos->mii->read = eqos_mdio_read;
1758 eqos->mii->write = eqos_mdio_write;
1759 eqos->mii->priv = eqos;
1760 strcpy(eqos->mii->name, dev->name);
1762 ret = mdio_register(eqos->mii);
1764 pr_err("mdio_register() failed: %d", ret);
1768 debug("%s: OK\n", __func__);
1772 mdio_free(eqos->mii);
1773 err_remove_resources_tegra:
1774 eqos->config->ops->eqos_remove_resources(dev);
1775 err_remove_resources_core:
1776 eqos_remove_resources_core(dev);
1778 debug("%s: returns %d\n", __func__, ret);
1782 static int eqos_remove(struct udevice *dev)
1784 struct eqos_priv *eqos = dev_get_priv(dev);
1786 debug("%s(dev=%p):\n", __func__, dev);
1788 mdio_unregister(eqos->mii);
1789 mdio_free(eqos->mii);
1790 eqos->config->ops->eqos_remove_resources(dev);
1792 eqos_probe_resources_core(dev);
1794 debug("%s: OK\n", __func__);
1798 static const struct eth_ops eqos_ops = {
1799 .start = eqos_start,
1803 .free_pkt = eqos_free_pkt,
1804 .write_hwaddr = eqos_write_hwaddr,
1807 static struct eqos_ops eqos_tegra186_ops = {
1808 .eqos_inval_desc = eqos_inval_desc_tegra186,
1809 .eqos_flush_desc = eqos_flush_desc_tegra186,
1810 .eqos_inval_buffer = eqos_inval_buffer_tegra186,
1811 .eqos_flush_buffer = eqos_flush_buffer_tegra186,
1812 .eqos_probe_resources = eqos_probe_resources_tegra186,
1813 .eqos_remove_resources = eqos_remove_resources_tegra186,
1814 .eqos_stop_resets = eqos_stop_resets_tegra186,
1815 .eqos_start_resets = eqos_start_resets_tegra186,
1816 .eqos_stop_clks = eqos_stop_clks_tegra186,
1817 .eqos_start_clks = eqos_start_clks_tegra186,
1818 .eqos_calibrate_pads = eqos_calibrate_pads_tegra186,
1819 .eqos_disable_calibration = eqos_disable_calibration_tegra186,
1820 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_tegra186,
1821 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_tegra186
1824 static const struct eqos_config eqos_tegra186_config = {
1825 .reg_access_always_ok = false,
1828 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_DCB,
1829 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_20_35,
1830 .interface = eqos_get_interface_tegra186,
1831 .ops = &eqos_tegra186_ops
1834 static struct eqos_ops eqos_stm32_ops = {
1835 .eqos_inval_desc = eqos_inval_desc_stm32,
1836 .eqos_flush_desc = eqos_flush_desc_stm32,
1837 .eqos_inval_buffer = eqos_inval_buffer_stm32,
1838 .eqos_flush_buffer = eqos_flush_buffer_stm32,
1839 .eqos_probe_resources = eqos_probe_resources_stm32,
1840 .eqos_remove_resources = eqos_remove_resources_stm32,
1841 .eqos_stop_resets = eqos_stop_resets_stm32,
1842 .eqos_start_resets = eqos_start_resets_stm32,
1843 .eqos_stop_clks = eqos_stop_clks_stm32,
1844 .eqos_start_clks = eqos_start_clks_stm32,
1845 .eqos_calibrate_pads = eqos_calibrate_pads_stm32,
1846 .eqos_disable_calibration = eqos_disable_calibration_stm32,
1847 .eqos_set_tx_clk_speed = eqos_set_tx_clk_speed_stm32,
1848 .eqos_get_tick_clk_rate = eqos_get_tick_clk_rate_stm32
1851 static const struct eqos_config eqos_stm32_config = {
1852 .reg_access_always_ok = false,
1855 .config_mac = EQOS_MAC_RXQ_CTRL0_RXQ0EN_ENABLED_AV,
1856 .config_mac_mdio = EQOS_MAC_MDIO_ADDRESS_CR_250_300,
1857 .interface = eqos_get_interface_stm32,
1858 .ops = &eqos_stm32_ops
1861 static const struct udevice_id eqos_ids[] = {
1863 .compatible = "nvidia,tegra186-eqos",
1864 .data = (ulong)&eqos_tegra186_config
1867 .compatible = "snps,dwmac-4.20a",
1868 .data = (ulong)&eqos_stm32_config
1874 U_BOOT_DRIVER(eth_eqos) = {
1877 .of_match = eqos_ids,
1878 .probe = eqos_probe,
1879 .remove = eqos_remove,
1881 .priv_auto_alloc_size = sizeof(struct eqos_priv),
1882 .platdata_auto_alloc_size = sizeof(struct eth_pdata),