blob: 519e06ff0194e4f45e18d0d82b9cf4c8725ffbe0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese3e103812014-10-22 12:13:14 +02002/*
3 * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
4 *
5 * U-Boot version:
Stefan Roese05b38c12015-11-19 07:46:15 +01006 * Copyright (C) 2014-2015 Stefan Roese <sr@denx.de>
Stefan Roese3e103812014-10-22 12:13:14 +02007 *
8 * Based on the Linux version which is:
9 * Copyright (C) 2012 Marvell
10 *
11 * Rami Rosen <rosenr@marvell.com>
12 * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
Stefan Roese3e103812014-10-22 12:13:14 +020013 */
14
15#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070016#include <cpu_func.h>
Stefan Roese05b38c12015-11-19 07:46:15 +010017#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060018#include <log.h>
Stefan Roese3e103812014-10-22 12:13:14 +020019#include <net.h>
20#include <netdev.h>
21#include <config.h>
22#include <malloc.h>
Simon Glass274e0b02020-05-10 11:39:56 -060023#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060024#include <asm/global_data.h>
Stefan Roese3e103812014-10-22 12:13:14 +020025#include <asm/io.h>
Simon Glass9bc15642020-02-03 07:36:16 -070026#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060028#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060029#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060030#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090031#include <linux/errno.h>
Stefan Roese3e103812014-10-22 12:13:14 +020032#include <phy.h>
33#include <miiphy.h>
34#include <watchdog.h>
35#include <asm/arch/cpu.h>
36#include <asm/arch/soc.h>
37#include <linux/compat.h>
38#include <linux/mbus.h>
Aditya Prayogac9fe02a2018-12-05 00:39:23 +080039#include <asm-generic/gpio.h>
Stefan Roese3e103812014-10-22 12:13:14 +020040
Stefan Roese05b38c12015-11-19 07:46:15 +010041DECLARE_GLOBAL_DATA_PTR;
42
Marek Behún06c91f02022-04-27 12:41:57 +020043#define MVNETA_NR_CPUS 1
Stefan Roese3e103812014-10-22 12:13:14 +020044#define ETH_HLEN 14 /* Total octets in header */
45
46/* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
47#define WRAP (2 + ETH_HLEN + 4 + 32)
48#define MTU 1500
49#define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
50
51#define MVNETA_SMI_TIMEOUT 10000
52
53/* Registers */
54#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
55#define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
56#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
57#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
58#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
59#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
60#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
61#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
62#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
63#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
64#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
65#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
66#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
67#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
68#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
69#define MVNETA_PORT_RX_RESET 0x1cc0
70#define MVNETA_PORT_RX_DMA_RESET BIT(0)
71#define MVNETA_PHY_ADDR 0x2000
72#define MVNETA_PHY_ADDR_MASK 0x1f
73#define MVNETA_SMI 0x2004
74#define MVNETA_PHY_REG_MASK 0x1f
75/* SMI register fields */
76#define MVNETA_SMI_DATA_OFFS 0 /* Data */
77#define MVNETA_SMI_DATA_MASK (0xffff << MVNETA_SMI_DATA_OFFS)
78#define MVNETA_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
79#define MVNETA_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
80#define MVNETA_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
81#define MVNETA_SMI_OPCODE_READ (1 << MVNETA_SMI_OPCODE_OFFS)
82#define MVNETA_SMI_READ_VALID (1 << 27) /* Read Valid */
83#define MVNETA_SMI_BUSY (1 << 28) /* Busy */
84#define MVNETA_MBUS_RETRY 0x2010
85#define MVNETA_UNIT_INTR_CAUSE 0x2080
86#define MVNETA_UNIT_CONTROL 0x20B0
87#define MVNETA_PHY_POLLING_ENABLE BIT(1)
88#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
89#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
90#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
Stefan Roese572be4a2016-05-19 17:46:36 +020091#define MVNETA_WIN_SIZE_MASK (0xffff0000)
Stefan Roese3e103812014-10-22 12:13:14 +020092#define MVNETA_BASE_ADDR_ENABLE 0x2290
Stefan Roese572be4a2016-05-19 17:46:36 +020093#define MVNETA_BASE_ADDR_ENABLE_BIT 0x1
94#define MVNETA_PORT_ACCESS_PROTECT 0x2294
95#define MVNETA_PORT_ACCESS_PROTECT_WIN0_RW 0x3
Stefan Roese3e103812014-10-22 12:13:14 +020096#define MVNETA_PORT_CONFIG 0x2400
97#define MVNETA_UNI_PROMISC_MODE BIT(0)
98#define MVNETA_DEF_RXQ(q) ((q) << 1)
99#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
100#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
101#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
102#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
103#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
104#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
105#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
106 MVNETA_DEF_RXQ_ARP(q) | \
107 MVNETA_DEF_RXQ_TCP(q) | \
108 MVNETA_DEF_RXQ_UDP(q) | \
109 MVNETA_DEF_RXQ_BPDU(q) | \
110 MVNETA_TX_UNSET_ERR_SUM | \
111 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
112#define MVNETA_PORT_CONFIG_EXTEND 0x2404
113#define MVNETA_MAC_ADDR_LOW 0x2414
114#define MVNETA_MAC_ADDR_HIGH 0x2418
115#define MVNETA_SDMA_CONFIG 0x241c
116#define MVNETA_SDMA_BRST_SIZE_16 4
117#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
118#define MVNETA_RX_NO_DATA_SWAP BIT(4)
119#define MVNETA_TX_NO_DATA_SWAP BIT(5)
120#define MVNETA_DESC_SWAP BIT(6)
121#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
122#define MVNETA_PORT_STATUS 0x2444
123#define MVNETA_TX_IN_PRGRS BIT(1)
124#define MVNETA_TX_FIFO_EMPTY BIT(8)
125#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
126#define MVNETA_SERDES_CFG 0x24A0
127#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
128#define MVNETA_QSGMII_SERDES_PROTO 0x0667
129#define MVNETA_TYPE_PRIO 0x24bc
130#define MVNETA_FORCE_UNI BIT(21)
131#define MVNETA_TXQ_CMD_1 0x24e4
132#define MVNETA_TXQ_CMD 0x2448
133#define MVNETA_TXQ_DISABLE_SHIFT 8
134#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
135#define MVNETA_ACC_MODE 0x2500
136#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
137#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
138#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
139#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
140
141/* Exception Interrupt Port/Queue Cause register */
142
143#define MVNETA_INTR_NEW_CAUSE 0x25a0
144#define MVNETA_INTR_NEW_MASK 0x25a4
145
146/* bits 0..7 = TXQ SENT, one bit per queue.
147 * bits 8..15 = RXQ OCCUP, one bit per queue.
148 * bits 16..23 = RXQ FREE, one bit per queue.
149 * bit 29 = OLD_REG_SUM, see old reg ?
150 * bit 30 = TX_ERR_SUM, one bit for 4 ports
151 * bit 31 = MISC_SUM, one bit for 4 ports
152 */
153#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
154#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
155#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
156#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
157
158#define MVNETA_INTR_OLD_CAUSE 0x25a8
159#define MVNETA_INTR_OLD_MASK 0x25ac
160
161/* Data Path Port/Queue Cause Register */
162#define MVNETA_INTR_MISC_CAUSE 0x25b0
163#define MVNETA_INTR_MISC_MASK 0x25b4
164#define MVNETA_INTR_ENABLE 0x25b8
165
166#define MVNETA_RXQ_CMD 0x2680
167#define MVNETA_RXQ_DISABLE_SHIFT 8
168#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
169#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
170#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
171#define MVNETA_GMAC_CTRL_0 0x2c00
172#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
173#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
174#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
175#define MVNETA_GMAC_CTRL_2 0x2c08
176#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
177#define MVNETA_GMAC2_PORT_RGMII BIT(4)
178#define MVNETA_GMAC2_PORT_RESET BIT(6)
179#define MVNETA_GMAC_STATUS 0x2c10
180#define MVNETA_GMAC_LINK_UP BIT(0)
181#define MVNETA_GMAC_SPEED_1000 BIT(1)
182#define MVNETA_GMAC_SPEED_100 BIT(2)
183#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
184#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
185#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
186#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
187#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
188#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
189#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
190#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200191#define MVNETA_GMAC_FORCE_LINK_UP (BIT(0) | BIT(1))
192#define MVNETA_GMAC_IB_BYPASS_AN_EN BIT(3)
Stefan Roese3e103812014-10-22 12:13:14 +0200193#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
194#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
195#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200196#define MVNETA_GMAC_SET_FC_EN BIT(8)
197#define MVNETA_GMAC_ADVERT_FC_EN BIT(9)
Stefan Roese3e103812014-10-22 12:13:14 +0200198#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
199#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200200#define MVNETA_GMAC_SAMPLE_TX_CFG_EN BIT(15)
Stefan Roese3e103812014-10-22 12:13:14 +0200201#define MVNETA_MIB_COUNTERS_BASE 0x3080
202#define MVNETA_MIB_LATE_COLLISION 0x7c
203#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
204#define MVNETA_DA_FILT_OTH_MCAST 0x3500
205#define MVNETA_DA_FILT_UCAST_BASE 0x3600
206#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
207#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
208#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
209#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
210#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
211#define MVNETA_TXQ_DEC_SENT_SHIFT 16
212#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
213#define MVNETA_TXQ_SENT_DESC_SHIFT 16
214#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
215#define MVNETA_PORT_TX_RESET 0x3cf0
216#define MVNETA_PORT_TX_DMA_RESET BIT(0)
217#define MVNETA_TX_MTU 0x3e0c
218#define MVNETA_TX_TOKEN_SIZE 0x3e14
219#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
220#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
221#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
222
223/* Descriptor ring Macros */
224#define MVNETA_QUEUE_NEXT_DESC(q, index) \
225 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
226
227/* Various constants */
228
229/* Coalescing */
230#define MVNETA_TXDONE_COAL_PKTS 16
231#define MVNETA_RX_COAL_PKTS 32
232#define MVNETA_RX_COAL_USEC 100
233
234/* The two bytes Marvell header. Either contains a special value used
235 * by Marvell switches when a specific hardware mode is enabled (not
236 * supported by this driver) or is filled automatically by zeroes on
237 * the RX side. Those two bytes being at the front of the Ethernet
238 * header, they allow to have the IP header aligned on a 4 bytes
239 * boundary automatically: the hardware skips those two bytes on its
240 * own.
241 */
242#define MVNETA_MH_SIZE 2
243
244#define MVNETA_VLAN_TAG_LEN 4
245
246#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
247#define MVNETA_TX_CSUM_MAX_SIZE 9800
248#define MVNETA_ACC_MODE_EXT 1
249
250/* Timeout constants */
251#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
252#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
253#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
254
255#define MVNETA_TX_MTU_MAX 0x3ffff
256
257/* Max number of Rx descriptors */
258#define MVNETA_MAX_RXD 16
259
260/* Max number of Tx descriptors */
261#define MVNETA_MAX_TXD 16
262
263/* descriptor aligned size */
264#define MVNETA_DESC_ALIGNED_SIZE 32
265
266struct mvneta_port {
267 void __iomem *base;
268 struct mvneta_rx_queue *rxqs;
269 struct mvneta_tx_queue *txqs;
270
271 u8 mcast_count[256];
272 u16 tx_ring_size;
273 u16 rx_ring_size;
274
275 phy_interface_t phy_interface;
Marek Behúne942d8e2022-04-27 12:41:47 +0200276 bool fixed_link;
Stefan Roese3e103812014-10-22 12:13:14 +0200277 unsigned int link;
278 unsigned int duplex;
279 unsigned int speed;
280
281 int init;
Stefan Roese3e103812014-10-22 12:13:14 +0200282 struct phy_device *phydev;
Simon Glassfa4689a2019-12-06 21:41:35 -0700283#if CONFIG_IS_ENABLED(DM_GPIO)
Aditya Prayogac9fe02a2018-12-05 00:39:23 +0800284 struct gpio_desc phy_reset_gpio;
Robert Marko58c98732022-03-24 10:57:37 +0100285 struct gpio_desc sfp_tx_disable_gpio;
Aditya Prayogac9fe02a2018-12-05 00:39:23 +0800286#endif
Stefan Roese3e103812014-10-22 12:13:14 +0200287};
288
289/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
290 * layout of the transmit and reception DMA descriptors, and their
291 * layout is therefore defined by the hardware design
292 */
293
294#define MVNETA_TX_L3_OFF_SHIFT 0
295#define MVNETA_TX_IP_HLEN_SHIFT 8
296#define MVNETA_TX_L4_UDP BIT(16)
297#define MVNETA_TX_L3_IP6 BIT(17)
298#define MVNETA_TXD_IP_CSUM BIT(18)
299#define MVNETA_TXD_Z_PAD BIT(19)
300#define MVNETA_TXD_L_DESC BIT(20)
301#define MVNETA_TXD_F_DESC BIT(21)
302#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
303 MVNETA_TXD_L_DESC | \
304 MVNETA_TXD_F_DESC)
305#define MVNETA_TX_L4_CSUM_FULL BIT(30)
306#define MVNETA_TX_L4_CSUM_NOT BIT(31)
307
308#define MVNETA_RXD_ERR_CRC 0x0
309#define MVNETA_RXD_ERR_SUMMARY BIT(16)
310#define MVNETA_RXD_ERR_OVERRUN BIT(17)
311#define MVNETA_RXD_ERR_LEN BIT(18)
312#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
313#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
314#define MVNETA_RXD_L3_IP4 BIT(25)
315#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
316#define MVNETA_RXD_L4_CSUM_OK BIT(30)
317
318struct mvneta_tx_desc {
319 u32 command; /* Options used by HW for packet transmitting.*/
320 u16 reserverd1; /* csum_l4 (for future use) */
321 u16 data_size; /* Data size of transmitted packet in bytes */
322 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
323 u32 reserved2; /* hw_cmd - (for future use, PMT) */
324 u32 reserved3[4]; /* Reserved - (for future use) */
325};
326
327struct mvneta_rx_desc {
328 u32 status; /* Info about received packet */
329 u16 reserved1; /* pnc_info - (for future use, PnC) */
330 u16 data_size; /* Size of received packet in bytes */
331
332 u32 buf_phys_addr; /* Physical address of the buffer */
333 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
334
335 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
336 u16 reserved3; /* prefetch_cmd, for future use */
337 u16 reserved4; /* csum_l4 - (for future use, PnC) */
338
339 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
340 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
341};
342
343struct mvneta_tx_queue {
344 /* Number of this TX queue, in the range 0-7 */
345 u8 id;
346
347 /* Number of TX DMA descriptors in the descriptor ring */
348 int size;
349
350 /* Index of last TX DMA descriptor that was inserted */
351 int txq_put_index;
352
353 /* Index of the TX DMA descriptor to be cleaned up */
354 int txq_get_index;
355
356 /* Virtual address of the TX DMA descriptors array */
357 struct mvneta_tx_desc *descs;
358
359 /* DMA address of the TX DMA descriptors array */
360 dma_addr_t descs_phys;
361
362 /* Index of the last TX DMA descriptor */
363 int last_desc;
364
365 /* Index of the next TX DMA descriptor to process */
366 int next_desc_to_proc;
367};
368
369struct mvneta_rx_queue {
370 /* rx queue number, in the range 0-7 */
371 u8 id;
372
373 /* num of rx descriptors in the rx descriptor ring */
374 int size;
375
376 /* Virtual address of the RX DMA descriptors array */
377 struct mvneta_rx_desc *descs;
378
379 /* DMA address of the RX DMA descriptors array */
380 dma_addr_t descs_phys;
381
382 /* Index of the last RX DMA descriptor */
383 int last_desc;
384
385 /* Index of the next RX DMA descriptor to process */
386 int next_desc_to_proc;
387};
388
389/* U-Boot doesn't use the queues, so set the number to 1 */
390static int rxq_number = 1;
391static int txq_number = 1;
392static int rxq_def;
393
394struct buffer_location {
395 struct mvneta_tx_desc *tx_descs;
396 struct mvneta_rx_desc *rx_descs;
397 u32 rx_buffers;
398};
399
400/*
401 * All 4 interfaces use the same global buffer, since only one interface
402 * can be enabled at once
403 */
404static struct buffer_location buffer_loc;
405
406/*
407 * Page table entries are set to 1MB, or multiples of 1MB
408 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
409 */
410#define BD_SPACE (1 << 20)
411
412/* Utility/helper methods */
413
414/* Write helper method */
415static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
416{
417 writel(data, pp->base + offset);
418}
419
420/* Read helper method */
421static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
422{
423 return readl(pp->base + offset);
424}
425
426/* Clear all MIB counters */
427static void mvneta_mib_counters_clear(struct mvneta_port *pp)
428{
429 int i;
430
431 /* Perform dummy reads from MIB counters */
432 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
433 mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
434}
435
436/* Rx descriptors helper methods */
437
438/* Checks whether the RX descriptor having this status is both the first
439 * and the last descriptor for the RX packet. Each RX packet is currently
440 * received through a single RX descriptor, so not having each RX
441 * descriptor with its first and last bits set is an error
442 */
443static int mvneta_rxq_desc_is_first_last(u32 status)
444{
445 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
446 MVNETA_RXD_FIRST_LAST_DESC;
447}
448
449/* Add number of descriptors ready to receive new packets */
450static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
451 struct mvneta_rx_queue *rxq,
452 int ndescs)
453{
454 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
455 * be added at once
456 */
457 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
458 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
459 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
460 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
461 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
462 }
463
464 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
465 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
466}
467
468/* Get number of RX descriptors occupied by received packets */
469static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
470 struct mvneta_rx_queue *rxq)
471{
472 u32 val;
473
474 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
475 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
476}
477
478/* Update num of rx desc called upon return from rx path or
479 * from mvneta_rxq_drop_pkts().
480 */
481static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
482 struct mvneta_rx_queue *rxq,
483 int rx_done, int rx_filled)
484{
485 u32 val;
486
487 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
488 val = rx_done |
489 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
490 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
491 return;
492 }
493
494 /* Only 255 descriptors can be added at once */
495 while ((rx_done > 0) || (rx_filled > 0)) {
496 if (rx_done <= 0xff) {
497 val = rx_done;
498 rx_done = 0;
499 } else {
500 val = 0xff;
501 rx_done -= 0xff;
502 }
503 if (rx_filled <= 0xff) {
504 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
505 rx_filled = 0;
506 } else {
507 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
508 rx_filled -= 0xff;
509 }
510 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
511 }
512}
513
514/* Get pointer to next RX descriptor to be processed by SW */
515static struct mvneta_rx_desc *
516mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
517{
518 int rx_desc = rxq->next_desc_to_proc;
519
520 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
521 return rxq->descs + rx_desc;
522}
523
524/* Tx descriptors helper methods */
525
526/* Update HW with number of TX descriptors to be sent */
527static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
528 struct mvneta_tx_queue *txq,
529 int pend_desc)
530{
531 u32 val;
532
533 /* Only 255 descriptors can be added at once ; Assume caller
Heinrich Schuchardt42376962017-08-29 18:44:37 +0200534 * process TX descriptors in quanta less than 256
Stefan Roese3e103812014-10-22 12:13:14 +0200535 */
536 val = pend_desc;
537 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
538}
539
540/* Get pointer to next TX descriptor to be processed (send) by HW */
541static struct mvneta_tx_desc *
542mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
543{
544 int tx_desc = txq->next_desc_to_proc;
545
546 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
547 return txq->descs + tx_desc;
548}
549
550/* Set rxq buf size */
551static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
552 struct mvneta_rx_queue *rxq,
553 int buf_size)
554{
555 u32 val;
556
557 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
558
559 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
560 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
561
562 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
563}
564
565/* Start the Ethernet port RX and TX activity */
566static void mvneta_port_up(struct mvneta_port *pp)
567{
568 int queue;
569 u32 q_map;
570
571 /* Enable all initialized TXs. */
572 mvneta_mib_counters_clear(pp);
573 q_map = 0;
574 for (queue = 0; queue < txq_number; queue++) {
575 struct mvneta_tx_queue *txq = &pp->txqs[queue];
576 if (txq->descs != NULL)
577 q_map |= (1 << queue);
578 }
579 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
580
581 /* Enable all initialized RXQs. */
582 q_map = 0;
583 for (queue = 0; queue < rxq_number; queue++) {
584 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
585 if (rxq->descs != NULL)
586 q_map |= (1 << queue);
587 }
588 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
589}
590
591/* Stop the Ethernet port activity */
592static void mvneta_port_down(struct mvneta_port *pp)
593{
594 u32 val;
595 int count;
596
597 /* Stop Rx port activity. Check port Rx activity. */
598 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
599
600 /* Issue stop command for active channels only */
601 if (val != 0)
602 mvreg_write(pp, MVNETA_RXQ_CMD,
603 val << MVNETA_RXQ_DISABLE_SHIFT);
604
605 /* Wait for all Rx activity to terminate. */
606 count = 0;
607 do {
608 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400609 dev_warn(pp->phydev->dev,
610 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
611 val);
Stefan Roese3e103812014-10-22 12:13:14 +0200612 break;
613 }
614 mdelay(1);
615
616 val = mvreg_read(pp, MVNETA_RXQ_CMD);
617 } while (val & 0xff);
618
619 /* Stop Tx port activity. Check port Tx activity. Issue stop
620 * command for active channels only
621 */
622 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
623
624 if (val != 0)
625 mvreg_write(pp, MVNETA_TXQ_CMD,
626 (val << MVNETA_TXQ_DISABLE_SHIFT));
627
628 /* Wait for all Tx activity to terminate. */
629 count = 0;
630 do {
631 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400632 dev_warn(pp->phydev->dev,
633 "TIMEOUT for TX stopped status=0x%08x\n",
634 val);
Stefan Roese3e103812014-10-22 12:13:14 +0200635 break;
636 }
637 mdelay(1);
638
639 /* Check TX Command reg that all Txqs are stopped */
640 val = mvreg_read(pp, MVNETA_TXQ_CMD);
641
642 } while (val & 0xff);
643
644 /* Double check to verify that TX FIFO is empty */
645 count = 0;
646 do {
647 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400648 dev_warn(pp->phydev->dev,
649 "TX FIFO empty timeout status=0x08%x\n",
650 val);
Stefan Roese3e103812014-10-22 12:13:14 +0200651 break;
652 }
653 mdelay(1);
654
655 val = mvreg_read(pp, MVNETA_PORT_STATUS);
656 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
657 (val & MVNETA_TX_IN_PRGRS));
658
659 udelay(200);
660}
661
662/* Enable the port by setting the port enable bit of the MAC control register */
663static void mvneta_port_enable(struct mvneta_port *pp)
664{
665 u32 val;
666
667 /* Enable port */
668 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
669 val |= MVNETA_GMAC0_PORT_ENABLE;
670 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
671}
672
673/* Disable the port and wait for about 200 usec before retuning */
674static void mvneta_port_disable(struct mvneta_port *pp)
675{
676 u32 val;
677
678 /* Reset the Enable bit in the Serial Control Register */
679 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
680 val &= ~MVNETA_GMAC0_PORT_ENABLE;
681 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
682
683 udelay(200);
684}
685
686/* Multicast tables methods */
687
688/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
689static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
690{
691 int offset;
692 u32 val;
693
694 if (queue == -1) {
695 val = 0;
696 } else {
697 val = 0x1 | (queue << 1);
698 val |= (val << 24) | (val << 16) | (val << 8);
699 }
700
701 for (offset = 0; offset <= 0xc; offset += 4)
702 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
703}
704
705/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
706static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
707{
708 int offset;
709 u32 val;
710
711 if (queue == -1) {
712 val = 0;
713 } else {
714 val = 0x1 | (queue << 1);
715 val |= (val << 24) | (val << 16) | (val << 8);
716 }
717
718 for (offset = 0; offset <= 0xfc; offset += 4)
719 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
720}
721
722/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
723static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
724{
725 int offset;
726 u32 val;
727
728 if (queue == -1) {
729 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
730 val = 0;
731 } else {
732 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
733 val = 0x1 | (queue << 1);
734 val |= (val << 24) | (val << 16) | (val << 8);
735 }
736
737 for (offset = 0; offset <= 0xfc; offset += 4)
738 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
739}
740
741/* This method sets defaults to the NETA port:
742 * Clears interrupt Cause and Mask registers.
743 * Clears all MAC tables.
744 * Sets defaults to all registers.
745 * Resets RX and TX descriptor rings.
746 * Resets PHY.
747 * This method can be called after mvneta_port_down() to return the port
748 * settings to defaults.
749 */
750static void mvneta_defaults_set(struct mvneta_port *pp)
751{
752 int cpu;
753 int queue;
754 u32 val;
755
756 /* Clear all Cause registers */
757 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
758 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
759 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
760
761 /* Mask all interrupts */
762 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
763 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
764 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
765 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
766
767 /* Enable MBUS Retry bit16 */
768 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
769
770 /* Set CPU queue access map - all CPUs have access to all RX
771 * queues and to all TX queues
772 */
Marek Behún06c91f02022-04-27 12:41:57 +0200773 for (cpu = 0; cpu < MVNETA_NR_CPUS; cpu++)
Stefan Roese3e103812014-10-22 12:13:14 +0200774 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
775 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
776 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
777
778 /* Reset RX and TX DMAs */
779 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
780 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
781
782 /* Disable Legacy WRR, Disable EJP, Release from reset */
783 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
784 for (queue = 0; queue < txq_number; queue++) {
785 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
786 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
787 }
788
789 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
790 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
791
792 /* Set Port Acceleration Mode */
793 val = MVNETA_ACC_MODE_EXT;
794 mvreg_write(pp, MVNETA_ACC_MODE, val);
795
796 /* Update val of portCfg register accordingly with all RxQueue types */
797 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
798 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
799
800 val = 0;
801 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
802 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
803
804 /* Build PORT_SDMA_CONFIG_REG */
805 val = 0;
806
807 /* Default burst size */
808 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
809 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
810 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
811
812 /* Assign port SDMA configuration */
813 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
814
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200815 /* Enable PHY polling in hardware if not in fixed-link mode */
Marek Behúne942d8e2022-04-27 12:41:47 +0200816 if (!pp->fixed_link) {
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200817 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
818 val |= MVNETA_PHY_POLLING_ENABLE;
819 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
820 }
Stefan Roese3e103812014-10-22 12:13:14 +0200821
822 mvneta_set_ucast_table(pp, -1);
823 mvneta_set_special_mcast_table(pp, -1);
824 mvneta_set_other_mcast_table(pp, -1);
825}
826
827/* Set unicast address */
828static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
829 int queue)
830{
831 unsigned int unicast_reg;
832 unsigned int tbl_offset;
833 unsigned int reg_offset;
834
835 /* Locate the Unicast table entry */
836 last_nibble = (0xf & last_nibble);
837
838 /* offset from unicast tbl base */
839 tbl_offset = (last_nibble / 4) * 4;
840
841 /* offset within the above reg */
842 reg_offset = last_nibble % 4;
843
844 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
845
846 if (queue == -1) {
847 /* Clear accepts frame bit at specified unicast DA tbl entry */
848 unicast_reg &= ~(0xff << (8 * reg_offset));
849 } else {
850 unicast_reg &= ~(0xff << (8 * reg_offset));
851 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
852 }
853
854 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
855}
856
857/* Set mac address */
858static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
859 int queue)
860{
861 unsigned int mac_h;
862 unsigned int mac_l;
863
864 if (queue != -1) {
865 mac_l = (addr[4] << 8) | (addr[5]);
866 mac_h = (addr[0] << 24) | (addr[1] << 16) |
867 (addr[2] << 8) | (addr[3] << 0);
868
869 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
870 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
871 }
872
873 /* Accept frames of this address */
874 mvneta_set_ucast_addr(pp, addr[5], queue);
875}
876
Matt Pelland668a5f22018-03-27 13:18:25 -0400877static int mvneta_write_hwaddr(struct udevice *dev)
878{
879 mvneta_mac_addr_set(dev_get_priv(dev),
Simon Glassfa20e932020-12-03 16:55:20 -0700880 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr,
Matt Pelland668a5f22018-03-27 13:18:25 -0400881 rxq_def);
882
883 return 0;
884}
885
Stefan Roese3e103812014-10-22 12:13:14 +0200886/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
887static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
888 u32 phys_addr, u32 cookie)
889{
890 rx_desc->buf_cookie = cookie;
891 rx_desc->buf_phys_addr = phys_addr;
892}
893
894/* Decrement sent descriptors counter */
895static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
896 struct mvneta_tx_queue *txq,
897 int sent_desc)
898{
899 u32 val;
900
901 /* Only 255 TX descriptors can be updated at once */
902 while (sent_desc > 0xff) {
903 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
904 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
905 sent_desc = sent_desc - 0xff;
906 }
907
908 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
909 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
910}
911
912/* Get number of TX descriptors already sent by HW */
913static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
914 struct mvneta_tx_queue *txq)
915{
916 u32 val;
917 int sent_desc;
918
919 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
920 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
921 MVNETA_TXQ_SENT_DESC_SHIFT;
922
923 return sent_desc;
924}
925
926/* Display more error info */
927static void mvneta_rx_error(struct mvneta_port *pp,
928 struct mvneta_rx_desc *rx_desc)
929{
930 u32 status = rx_desc->status;
931
932 if (!mvneta_rxq_desc_is_first_last(status)) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400933 dev_err(pp->phydev->dev,
934 "bad rx status %08x (buffer oversize), size=%d\n",
935 status, rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200936 return;
937 }
938
939 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
940 case MVNETA_RXD_ERR_CRC:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400941 dev_err(pp->phydev->dev,
942 "bad rx status %08x (crc error), size=%d\n", status,
943 rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200944 break;
945 case MVNETA_RXD_ERR_OVERRUN:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400946 dev_err(pp->phydev->dev,
947 "bad rx status %08x (overrun error), size=%d\n", status,
948 rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200949 break;
950 case MVNETA_RXD_ERR_LEN:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400951 dev_err(pp->phydev->dev,
952 "bad rx status %08x (max frame length error), size=%d\n",
953 status, rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200954 break;
955 case MVNETA_RXD_ERR_RESOURCE:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400956 dev_err(pp->phydev->dev,
957 "bad rx status %08x (resource error), size=%d\n",
958 status, rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200959 break;
960 }
961}
962
963static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
964 int rxq)
965{
966 return &pp->rxqs[rxq];
967}
968
969
970/* Drop packets received by the RXQ and free buffers */
971static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
972 struct mvneta_rx_queue *rxq)
973{
974 int rx_done;
975
976 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
977 if (rx_done)
978 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
979}
980
981/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
982static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
983 int num)
984{
985 int i;
986
987 for (i = 0; i < num; i++) {
988 u32 addr;
989
990 /* U-Boot special: Fill in the rx buffer addresses */
991 addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
992 mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
993 }
994
995 /* Add this number of RX descriptors as non occupied (ready to
996 * get packets)
997 */
998 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
999
1000 return 0;
1001}
1002
1003/* Rx/Tx queue initialization/cleanup methods */
1004
1005/* Create a specified RX queue */
1006static int mvneta_rxq_init(struct mvneta_port *pp,
1007 struct mvneta_rx_queue *rxq)
1008
1009{
1010 rxq->size = pp->rx_ring_size;
1011
1012 /* Allocate memory for RX descriptors */
1013 rxq->descs_phys = (dma_addr_t)rxq->descs;
1014 if (rxq->descs == NULL)
1015 return -ENOMEM;
1016
Jon Nettleton543efd12018-05-30 08:52:29 +03001017 WARN_ON(rxq->descs != PTR_ALIGN(rxq->descs, ARCH_DMA_MINALIGN));
1018
Stefan Roese3e103812014-10-22 12:13:14 +02001019 rxq->last_desc = rxq->size - 1;
1020
1021 /* Set Rx descriptors queue starting address */
1022 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
1023 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
1024
1025 /* Fill RXQ with buffers from RX pool */
1026 mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
1027 mvneta_rxq_fill(pp, rxq, rxq->size);
1028
1029 return 0;
1030}
1031
1032/* Cleanup Rx queue */
1033static void mvneta_rxq_deinit(struct mvneta_port *pp,
1034 struct mvneta_rx_queue *rxq)
1035{
1036 mvneta_rxq_drop_pkts(pp, rxq);
1037
1038 rxq->descs = NULL;
1039 rxq->last_desc = 0;
1040 rxq->next_desc_to_proc = 0;
1041 rxq->descs_phys = 0;
1042}
1043
1044/* Create and initialize a tx queue */
1045static int mvneta_txq_init(struct mvneta_port *pp,
1046 struct mvneta_tx_queue *txq)
1047{
1048 txq->size = pp->tx_ring_size;
1049
1050 /* Allocate memory for TX descriptors */
Stefan Roese6564d992016-05-19 18:09:17 +02001051 txq->descs_phys = (dma_addr_t)txq->descs;
Stefan Roese3e103812014-10-22 12:13:14 +02001052 if (txq->descs == NULL)
1053 return -ENOMEM;
1054
Jon Nettleton543efd12018-05-30 08:52:29 +03001055 WARN_ON(txq->descs != PTR_ALIGN(txq->descs, ARCH_DMA_MINALIGN));
1056
Stefan Roese3e103812014-10-22 12:13:14 +02001057 txq->last_desc = txq->size - 1;
1058
1059 /* Set maximum bandwidth for enabled TXQs */
1060 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
1061 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
1062
1063 /* Set Tx descriptors queue starting address */
1064 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
1065 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
1066
1067 return 0;
1068}
1069
1070/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
1071static void mvneta_txq_deinit(struct mvneta_port *pp,
1072 struct mvneta_tx_queue *txq)
1073{
1074 txq->descs = NULL;
1075 txq->last_desc = 0;
1076 txq->next_desc_to_proc = 0;
1077 txq->descs_phys = 0;
1078
1079 /* Set minimum bandwidth for disabled TXQs */
1080 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
1081 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
1082
1083 /* Set Tx descriptors queue starting address and size */
1084 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
1085 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
1086}
1087
1088/* Cleanup all Tx queues */
1089static void mvneta_cleanup_txqs(struct mvneta_port *pp)
1090{
1091 int queue;
1092
1093 for (queue = 0; queue < txq_number; queue++)
1094 mvneta_txq_deinit(pp, &pp->txqs[queue]);
1095}
1096
1097/* Cleanup all Rx queues */
1098static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
1099{
1100 int queue;
1101
1102 for (queue = 0; queue < rxq_number; queue++)
1103 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
1104}
1105
1106
1107/* Init all Rx queues */
1108static int mvneta_setup_rxqs(struct mvneta_port *pp)
1109{
1110 int queue;
1111
1112 for (queue = 0; queue < rxq_number; queue++) {
1113 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
1114 if (err) {
Sean Andersonecbbddf2020-09-15 10:44:55 -04001115 dev_err(pp->phydev->dev, "%s: can't create rxq=%d\n",
1116 __func__, queue);
Stefan Roese3e103812014-10-22 12:13:14 +02001117 mvneta_cleanup_rxqs(pp);
1118 return err;
1119 }
1120 }
1121
1122 return 0;
1123}
1124
1125/* Init all tx queues */
1126static int mvneta_setup_txqs(struct mvneta_port *pp)
1127{
1128 int queue;
1129
1130 for (queue = 0; queue < txq_number; queue++) {
1131 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
1132 if (err) {
Sean Andersonecbbddf2020-09-15 10:44:55 -04001133 dev_err(pp->phydev->dev, "%s: can't create txq=%d\n",
1134 __func__, queue);
Stefan Roese3e103812014-10-22 12:13:14 +02001135 mvneta_cleanup_txqs(pp);
1136 return err;
1137 }
1138 }
1139
1140 return 0;
1141}
1142
1143static void mvneta_start_dev(struct mvneta_port *pp)
1144{
1145 /* start the Rx/Tx activity */
1146 mvneta_port_enable(pp);
1147}
1148
Stefan Roese05b38c12015-11-19 07:46:15 +01001149static void mvneta_adjust_link(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001150{
Stefan Roese05b38c12015-11-19 07:46:15 +01001151 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001152 struct phy_device *phydev = pp->phydev;
Marek Behúnb4d0ea72022-04-27 12:41:54 +02001153 bool status_change = false;
Stefan Roese3e103812014-10-22 12:13:14 +02001154
Marek Behúne942d8e2022-04-27 12:41:47 +02001155 if (pp->fixed_link) {
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001156 debug("Using fixed link, skip link adjust\n");
1157 return;
1158 }
1159
Marek Behún4c46dab2022-04-27 12:41:53 +02001160 if (phydev->link &&
1161 (pp->speed != phydev->speed || pp->duplex != phydev->duplex)) {
1162 u32 val;
Stefan Roese3e103812014-10-22 12:13:14 +02001163
Marek Behún4c46dab2022-04-27 12:41:53 +02001164 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1165 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
1166 MVNETA_GMAC_CONFIG_GMII_SPEED |
1167 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
1168 MVNETA_GMAC_AN_SPEED_EN |
1169 MVNETA_GMAC_AN_DUPLEX_EN);
Stefan Roese3e103812014-10-22 12:13:14 +02001170
Marek Behún4c46dab2022-04-27 12:41:53 +02001171 if (phydev->duplex)
1172 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
Stefan Roese3e103812014-10-22 12:13:14 +02001173
Marek Behún4c46dab2022-04-27 12:41:53 +02001174 if (phydev->speed == SPEED_1000)
1175 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1176 else if (pp->speed == SPEED_100)
1177 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
Stefan Roese3e103812014-10-22 12:13:14 +02001178
Marek Behún4c46dab2022-04-27 12:41:53 +02001179 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
Stefan Roese3e103812014-10-22 12:13:14 +02001180
Marek Behún4c46dab2022-04-27 12:41:53 +02001181 pp->duplex = phydev->duplex;
Marek Behún2db231e2022-04-27 12:41:55 +02001182 pp->speed = phydev->speed;
Stefan Roese3e103812014-10-22 12:13:14 +02001183 }
1184
1185 if (phydev->link != pp->link) {
1186 if (!phydev->link) {
1187 pp->duplex = -1;
1188 pp->speed = 0;
1189 }
1190
1191 pp->link = phydev->link;
Marek Behúnb4d0ea72022-04-27 12:41:54 +02001192 status_change = true;
Stefan Roese3e103812014-10-22 12:13:14 +02001193 }
1194
1195 if (status_change) {
1196 if (phydev->link) {
1197 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1198 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
1199 MVNETA_GMAC_FORCE_LINK_DOWN);
1200 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1201 mvneta_port_up(pp);
1202 } else {
1203 mvneta_port_down(pp);
1204 }
1205 }
1206}
1207
Stefan Roese05b38c12015-11-19 07:46:15 +01001208static int mvneta_open(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001209{
Stefan Roese05b38c12015-11-19 07:46:15 +01001210 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001211 int ret;
1212
1213 ret = mvneta_setup_rxqs(pp);
1214 if (ret)
1215 return ret;
1216
1217 ret = mvneta_setup_txqs(pp);
1218 if (ret)
1219 return ret;
1220
1221 mvneta_adjust_link(dev);
1222
1223 mvneta_start_dev(pp);
1224
1225 return 0;
1226}
1227
1228/* Initialize hw */
Stefan Roese05b38c12015-11-19 07:46:15 +01001229static int mvneta_init2(struct mvneta_port *pp)
Stefan Roese3e103812014-10-22 12:13:14 +02001230{
1231 int queue;
1232
1233 /* Disable port */
1234 mvneta_port_disable(pp);
1235
1236 /* Set port default values */
1237 mvneta_defaults_set(pp);
1238
1239 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
1240 GFP_KERNEL);
1241 if (!pp->txqs)
1242 return -ENOMEM;
1243
1244 /* U-Boot special: use preallocated area */
1245 pp->txqs[0].descs = buffer_loc.tx_descs;
1246
1247 /* Initialize TX descriptor rings */
1248 for (queue = 0; queue < txq_number; queue++) {
1249 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1250 txq->id = queue;
1251 txq->size = pp->tx_ring_size;
1252 }
1253
1254 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
1255 GFP_KERNEL);
1256 if (!pp->rxqs) {
1257 kfree(pp->txqs);
1258 return -ENOMEM;
1259 }
1260
1261 /* U-Boot special: use preallocated area */
1262 pp->rxqs[0].descs = buffer_loc.rx_descs;
1263
1264 /* Create Rx descriptor rings */
1265 for (queue = 0; queue < rxq_number; queue++) {
1266 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1267 rxq->id = queue;
1268 rxq->size = pp->rx_ring_size;
1269 }
1270
1271 return 0;
1272}
1273
1274/* platform glue : initialize decoding windows */
Stefan Roese572be4a2016-05-19 17:46:36 +02001275
1276/*
1277 * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1278 * First layer is: GbE Address window that resides inside the GBE unit,
1279 * Second layer is: Fabric address window which is located in the NIC400
1280 * (South Fabric).
1281 * To simplify the address decode configuration for Armada3700, we bypass the
1282 * first layer of GBE decode window by setting the first window to 4GB.
1283 */
1284static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1285{
1286 /*
1287 * Set window size to 4GB, to bypass GBE address decode, leave the
1288 * work to MBUS decode window
1289 */
1290 mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1291
1292 /* Enable GBE address decode window 0 by set bit 0 to 0 */
1293 clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1294 MVNETA_BASE_ADDR_ENABLE_BIT);
1295
1296 /* Set GBE address decode window 0 to full Access (read or write) */
1297 setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1298 MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1299}
1300
Stefan Roese3e103812014-10-22 12:13:14 +02001301static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
1302{
1303 const struct mbus_dram_target_info *dram;
1304 u32 win_enable;
1305 u32 win_protect;
1306 int i;
1307
1308 dram = mvebu_mbus_dram_info();
1309 for (i = 0; i < 6; i++) {
1310 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1311 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1312
1313 if (i < 4)
1314 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1315 }
1316
1317 win_enable = 0x3f;
1318 win_protect = 0;
1319
1320 for (i = 0; i < dram->num_cs; i++) {
1321 const struct mbus_dram_window *cs = dram->cs + i;
1322 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
1323 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
1324
1325 mvreg_write(pp, MVNETA_WIN_SIZE(i),
1326 (cs->size - 1) & 0xffff0000);
1327
1328 win_enable &= ~(1 << i);
1329 win_protect |= 3 << (2 * i);
1330 }
1331
1332 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1333}
1334
1335/* Power up the port */
1336static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
1337{
1338 u32 ctrl;
1339
1340 /* MAC Cause register should be cleared */
1341 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
1342
1343 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1344
1345 /* Even though it might look weird, when we're configured in
1346 * SGMII or QSGMII mode, the RGMII bit needs to be set.
1347 */
1348 switch (phy_mode) {
1349 case PHY_INTERFACE_MODE_QSGMII:
1350 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
1351 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1352 break;
1353 case PHY_INTERFACE_MODE_SGMII:
1354 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
1355 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1356 break;
1357 case PHY_INTERFACE_MODE_RGMII:
1358 case PHY_INTERFACE_MODE_RGMII_ID:
1359 ctrl |= MVNETA_GMAC2_PORT_RGMII;
1360 break;
1361 default:
1362 return -EINVAL;
1363 }
1364
1365 /* Cancel Port Reset */
1366 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
1367 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
1368
1369 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
1370 MVNETA_GMAC2_PORT_RESET) != 0)
1371 continue;
1372
1373 return 0;
1374}
1375
1376/* Device initialization routine */
Stefan Roese05b38c12015-11-19 07:46:15 +01001377static int mvneta_init(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001378{
Simon Glassfa20e932020-12-03 16:55:20 -07001379 struct eth_pdata *pdata = dev_get_plat(dev);
Stefan Roese05b38c12015-11-19 07:46:15 +01001380 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001381 int err;
1382
1383 pp->tx_ring_size = MVNETA_MAX_TXD;
1384 pp->rx_ring_size = MVNETA_MAX_RXD;
1385
Stefan Roese05b38c12015-11-19 07:46:15 +01001386 err = mvneta_init2(pp);
Stefan Roese3e103812014-10-22 12:13:14 +02001387 if (err < 0) {
Sean Andersone0d00042020-09-15 10:44:54 -04001388 dev_err(dev, "can't init eth hal\n");
Stefan Roese3e103812014-10-22 12:13:14 +02001389 return err;
1390 }
1391
Stefan Roese05b38c12015-11-19 07:46:15 +01001392 mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
Stefan Roese3e103812014-10-22 12:13:14 +02001393
1394 err = mvneta_port_power_up(pp, pp->phy_interface);
1395 if (err < 0) {
Sean Andersone0d00042020-09-15 10:44:54 -04001396 dev_err(dev, "can't power up port\n");
Stefan Roese3e103812014-10-22 12:13:14 +02001397 return err;
1398 }
1399
1400 /* Call open() now as it needs to be done before runing send() */
1401 mvneta_open(dev);
1402
1403 return 0;
1404}
1405
1406/* U-Boot only functions follow here */
1407
Stefan Roese05b38c12015-11-19 07:46:15 +01001408static int mvneta_start(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001409{
Stefan Roese05b38c12015-11-19 07:46:15 +01001410 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001411 struct phy_device *phydev;
1412
1413 mvneta_port_power_up(pp, pp->phy_interface);
1414
1415 if (!pp->init || pp->link == 0) {
Marek Behúne942d8e2022-04-27 12:41:47 +02001416 if (pp->fixed_link) {
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001417 u32 val;
Stefan Roese3e103812014-10-22 12:13:14 +02001418
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001419 pp->init = 1;
1420 pp->link = 1;
1421 mvneta_init(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001422
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001423 val = MVNETA_GMAC_FORCE_LINK_UP |
1424 MVNETA_GMAC_IB_BYPASS_AN_EN |
1425 MVNETA_GMAC_SET_FC_EN |
1426 MVNETA_GMAC_ADVERT_FC_EN |
1427 MVNETA_GMAC_SAMPLE_TX_CFG_EN;
1428
1429 if (pp->duplex)
1430 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1431
1432 if (pp->speed == SPEED_1000)
1433 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1434 else if (pp->speed == SPEED_100)
1435 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1436
1437 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1438 } else {
Marek Behún99e296f2022-04-27 12:41:46 +02001439 phydev = dm_eth_phy_connect(dev);
Marek Behún075ccb12018-04-24 17:21:29 +02001440 if (!phydev) {
Marek Behún99e296f2022-04-27 12:41:46 +02001441 printf("dm_eth_phy_connect failed\n");
Marek Behún075ccb12018-04-24 17:21:29 +02001442 return -ENODEV;
1443 }
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001444
Marek Behún99e296f2022-04-27 12:41:46 +02001445 /* Set PHY address in case we will enable HW polling */
1446 mvreg_write(pp, MVNETA_PHY_ADDR, phydev->addr);
1447
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001448 pp->phydev = phydev;
1449 phy_config(phydev);
1450 phy_startup(phydev);
1451 if (!phydev->link) {
1452 printf("%s: No link.\n", phydev->dev->name);
1453 return -1;
1454 }
1455
1456 /* Full init on first call */
1457 mvneta_init(dev);
1458 pp->init = 1;
1459 return 0;
1460 }
Stefan Roese3e103812014-10-22 12:13:14 +02001461 }
1462
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001463 /* Upon all following calls, this is enough */
1464 mvneta_port_up(pp);
1465 mvneta_port_enable(pp);
1466
Stefan Roese3e103812014-10-22 12:13:14 +02001467 return 0;
1468}
1469
Stefan Roese05b38c12015-11-19 07:46:15 +01001470static int mvneta_send(struct udevice *dev, void *packet, int length)
Stefan Roese3e103812014-10-22 12:13:14 +02001471{
Stefan Roese05b38c12015-11-19 07:46:15 +01001472 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001473 struct mvneta_tx_queue *txq = &pp->txqs[0];
1474 struct mvneta_tx_desc *tx_desc;
1475 int sent_desc;
1476 u32 timeout = 0;
1477
1478 /* Get a descriptor for the first part of the packet */
1479 tx_desc = mvneta_txq_next_desc_get(txq);
1480
Stefan Roese6564d992016-05-19 18:09:17 +02001481 tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
Stefan Roese05b38c12015-11-19 07:46:15 +01001482 tx_desc->data_size = length;
Stefan Roese6564d992016-05-19 18:09:17 +02001483 flush_dcache_range((ulong)packet,
1484 (ulong)packet + ALIGN(length, PKTALIGN));
Stefan Roese3e103812014-10-22 12:13:14 +02001485
1486 /* First and Last descriptor */
1487 tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
1488 mvneta_txq_pend_desc_add(pp, txq, 1);
1489
1490 /* Wait for packet to be sent (queue might help with speed here) */
1491 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1492 while (!sent_desc) {
1493 if (timeout++ > 10000) {
1494 printf("timeout: packet not sent\n");
1495 return -1;
1496 }
1497 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1498 }
1499
1500 /* txDone has increased - hw sent packet */
1501 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
Stefan Roese3e103812014-10-22 12:13:14 +02001502
1503 return 0;
1504}
1505
Stefan Roese05b38c12015-11-19 07:46:15 +01001506static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
Stefan Roese3e103812014-10-22 12:13:14 +02001507{
Stefan Roese05b38c12015-11-19 07:46:15 +01001508 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001509 int rx_done;
Stefan Roese3e103812014-10-22 12:13:14 +02001510 struct mvneta_rx_queue *rxq;
Stefan Roese05b38c12015-11-19 07:46:15 +01001511 int rx_bytes = 0;
Stefan Roese3e103812014-10-22 12:13:14 +02001512
1513 /* get rx queue */
1514 rxq = mvneta_rxq_handle_get(pp, rxq_def);
1515 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Stefan Roese3e103812014-10-22 12:13:14 +02001516
Stefan Roese05b38c12015-11-19 07:46:15 +01001517 if (rx_done) {
Stefan Roese3e103812014-10-22 12:13:14 +02001518 struct mvneta_rx_desc *rx_desc;
1519 unsigned char *data;
1520 u32 rx_status;
Stefan Roese3e103812014-10-22 12:13:14 +02001521
1522 /*
1523 * No cache invalidation needed here, since the desc's are
1524 * located in a uncached memory region
1525 */
1526 rx_desc = mvneta_rxq_next_desc_get(rxq);
1527
1528 rx_status = rx_desc->status;
1529 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1530 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1531 mvneta_rx_error(pp, rx_desc);
1532 /* leave the descriptor untouched */
Stefan Roese05b38c12015-11-19 07:46:15 +01001533 return -EIO;
Stefan Roese3e103812014-10-22 12:13:14 +02001534 }
1535
1536 /* 2 bytes for marvell header. 4 bytes for crc */
1537 rx_bytes = rx_desc->data_size - 6;
1538
1539 /* give packet to stack - skip on first 2 bytes */
Stefan Roese6564d992016-05-19 18:09:17 +02001540 data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
Stefan Roese3e103812014-10-22 12:13:14 +02001541 /*
1542 * No cache invalidation needed here, since the rx_buffer's are
1543 * located in a uncached memory region
1544 */
Stefan Roese05b38c12015-11-19 07:46:15 +01001545 *packetp = data;
Stefan Roese3e103812014-10-22 12:13:14 +02001546
Jason Brownc7bc1832017-11-28 11:12:43 -08001547 /*
1548 * Only mark one descriptor as free
1549 * since only one was processed
1550 */
1551 mvneta_rxq_desc_num_update(pp, rxq, 1, 1);
Stefan Roese05b38c12015-11-19 07:46:15 +01001552 }
Stefan Roese3e103812014-10-22 12:13:14 +02001553
Stefan Roese05b38c12015-11-19 07:46:15 +01001554 return rx_bytes;
Stefan Roese3e103812014-10-22 12:13:14 +02001555}
1556
Stefan Roese05b38c12015-11-19 07:46:15 +01001557static int mvneta_probe(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001558{
Stefan Roese05b38c12015-11-19 07:46:15 +01001559 struct mvneta_port *pp = dev_get_priv(dev);
Robert Marko58c98732022-03-24 10:57:37 +01001560#if CONFIG_IS_ENABLED(DM_GPIO)
1561 struct ofnode_phandle_args sfp_args;
1562#endif
Stefan Roese05b38c12015-11-19 07:46:15 +01001563 void *blob = (void *)gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001564 int node = dev_of_offset(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001565 void *bd_space;
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001566 int fl_node;
Stefan Roese3e103812014-10-22 12:13:14 +02001567
Stefan Roese3e103812014-10-22 12:13:14 +02001568 /*
1569 * Allocate buffer area for descs and rx_buffers. This is only
1570 * done once for all interfaces. As only one interface can
Chris Packham0f81d7a2016-08-29 20:54:02 +12001571 * be active. Make this area DMA safe by disabling the D-cache
Stefan Roese3e103812014-10-22 12:13:14 +02001572 */
1573 if (!buffer_loc.tx_descs) {
Jon Nettleton543efd12018-05-30 08:52:29 +03001574 u32 size;
1575
Stefan Roese3e103812014-10-22 12:13:14 +02001576 /* Align buffer area for descs and rx_buffers to 1MiB */
1577 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
Rabeeh Khoury31ad3ce2018-06-19 21:36:50 +03001578 flush_dcache_range((ulong)bd_space, (ulong)bd_space + BD_SPACE);
Stefan Roese6564d992016-05-19 18:09:17 +02001579 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
Stefan Roese3e103812014-10-22 12:13:14 +02001580 DCACHE_OFF);
1581 buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
Jon Nettleton543efd12018-05-30 08:52:29 +03001582 size = roundup(MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc),
1583 ARCH_DMA_MINALIGN);
Rabeeh Khouryf046bed2018-06-19 21:36:51 +03001584 memset(buffer_loc.tx_descs, 0, size);
Stefan Roese3e103812014-10-22 12:13:14 +02001585 buffer_loc.rx_descs = (struct mvneta_rx_desc *)
Jon Nettleton543efd12018-05-30 08:52:29 +03001586 ((phys_addr_t)bd_space + size);
1587 size += roundup(MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc),
1588 ARCH_DMA_MINALIGN);
1589 buffer_loc.rx_buffers = (phys_addr_t)(bd_space + size);
Stefan Roese3e103812014-10-22 12:13:14 +02001590 }
1591
Marek Behún645c7442022-04-27 12:41:44 +02001592 pp->base = dev_read_addr_ptr(dev);
1593 pp->phy_interface = dev_read_phy_mode(dev);
1594 if (pp->phy_interface == PHY_INTERFACE_MODE_NA)
1595 return -EINVAL;
Stefan Roese3e103812014-10-22 12:13:14 +02001596
Stefan Roese05b38c12015-11-19 07:46:15 +01001597 /* Configure MBUS address windows */
Simon Glass54cbcc82017-05-18 20:08:57 -06001598 if (device_is_compatible(dev, "marvell,armada-3700-neta"))
Stefan Roese572be4a2016-05-19 17:46:36 +02001599 mvneta_bypass_mbus_windows(pp);
1600 else
1601 mvneta_conf_mbus_windows(pp);
Stefan Roese3e103812014-10-22 12:13:14 +02001602
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001603 /* fetch 'fixed-link' property from 'neta' node */
1604 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1605 if (fl_node != -FDT_ERR_NOTFOUND) {
1606 /* set phy_addr to invalid value for fixed link */
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001607 pp->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1608 pp->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
Marek Behúne942d8e2022-04-27 12:41:47 +02001609 pp->fixed_link = true;
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001610 }
Stefan Roese3e103812014-10-22 12:13:14 +02001611
Simon Glassfa4689a2019-12-06 21:41:35 -07001612#if CONFIG_IS_ENABLED(DM_GPIO)
Marek Behún1cbd1042022-04-27 12:41:52 +02001613 if (!dev_read_phandle_with_args(dev, "sfp", NULL, 0, 0, &sfp_args) &&
1614 ofnode_is_enabled(sfp_args.node))
Robert Marko58c98732022-03-24 10:57:37 +01001615 gpio_request_by_name_nodev(sfp_args.node, "tx-disable-gpio", 0,
1616 &pp->sfp_tx_disable_gpio, GPIOD_IS_OUT);
1617
Aditya Prayogac9fe02a2018-12-05 00:39:23 +08001618 gpio_request_by_name(dev, "phy-reset-gpios", 0,
1619 &pp->phy_reset_gpio, GPIOD_IS_OUT);
1620
1621 if (dm_gpio_is_valid(&pp->phy_reset_gpio)) {
1622 dm_gpio_set_value(&pp->phy_reset_gpio, 1);
1623 mdelay(10);
1624 dm_gpio_set_value(&pp->phy_reset_gpio, 0);
1625 }
Robert Marko58c98732022-03-24 10:57:37 +01001626
1627 if (dm_gpio_is_valid(&pp->sfp_tx_disable_gpio))
1628 dm_gpio_set_value(&pp->sfp_tx_disable_gpio, 0);
Aditya Prayogac9fe02a2018-12-05 00:39:23 +08001629#endif
1630
Marek Behúnb407baa2022-04-27 12:41:51 +02001631 return 0;
Stefan Roese05b38c12015-11-19 07:46:15 +01001632}
1633
1634static void mvneta_stop(struct udevice *dev)
1635{
1636 struct mvneta_port *pp = dev_get_priv(dev);
1637
1638 mvneta_port_down(pp);
1639 mvneta_port_disable(pp);
1640}
1641
1642static const struct eth_ops mvneta_ops = {
1643 .start = mvneta_start,
1644 .send = mvneta_send,
1645 .recv = mvneta_recv,
1646 .stop = mvneta_stop,
Matt Pelland668a5f22018-03-27 13:18:25 -04001647 .write_hwaddr = mvneta_write_hwaddr,
Stefan Roese05b38c12015-11-19 07:46:15 +01001648};
1649
Stefan Roese05b38c12015-11-19 07:46:15 +01001650static const struct udevice_id mvneta_ids[] = {
1651 { .compatible = "marvell,armada-370-neta" },
1652 { .compatible = "marvell,armada-xp-neta" },
Stefan Roese572be4a2016-05-19 17:46:36 +02001653 { .compatible = "marvell,armada-3700-neta" },
Stefan Roese05b38c12015-11-19 07:46:15 +01001654 { }
1655};
1656
1657U_BOOT_DRIVER(mvneta) = {
1658 .name = "mvneta",
1659 .id = UCLASS_ETH,
1660 .of_match = mvneta_ids,
Stefan Roese05b38c12015-11-19 07:46:15 +01001661 .probe = mvneta_probe,
1662 .ops = &mvneta_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001663 .priv_auto = sizeof(struct mvneta_port),
Simon Glass71fa5b42020-12-03 16:55:18 -07001664 .plat_auto = sizeof(struct eth_pdata),
Stefan Roese05b38c12015-11-19 07:46:15 +01001665};