blob: 24a491dcde67a726413d69a16c1685b224dab540 [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
Stefan Roese3e103812014-10-22 12:13:14 +020043#if !defined(CONFIG_PHYLIB)
44# error Marvell mvneta requires PHYLIB
45#endif
46
Stefan Roese3e103812014-10-22 12:13:14 +020047#define CONFIG_NR_CPUS 1
Stefan Roese3e103812014-10-22 12:13:14 +020048#define ETH_HLEN 14 /* Total octets in header */
49
50/* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
51#define WRAP (2 + ETH_HLEN + 4 + 32)
52#define MTU 1500
53#define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
54
55#define MVNETA_SMI_TIMEOUT 10000
56
57/* Registers */
58#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
59#define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
60#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
61#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
62#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
63#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
64#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
65#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
66#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
67#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
68#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
69#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
70#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
71#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
72#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
73#define MVNETA_PORT_RX_RESET 0x1cc0
74#define MVNETA_PORT_RX_DMA_RESET BIT(0)
75#define MVNETA_PHY_ADDR 0x2000
76#define MVNETA_PHY_ADDR_MASK 0x1f
77#define MVNETA_SMI 0x2004
78#define MVNETA_PHY_REG_MASK 0x1f
79/* SMI register fields */
80#define MVNETA_SMI_DATA_OFFS 0 /* Data */
81#define MVNETA_SMI_DATA_MASK (0xffff << MVNETA_SMI_DATA_OFFS)
82#define MVNETA_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
83#define MVNETA_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
84#define MVNETA_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
85#define MVNETA_SMI_OPCODE_READ (1 << MVNETA_SMI_OPCODE_OFFS)
86#define MVNETA_SMI_READ_VALID (1 << 27) /* Read Valid */
87#define MVNETA_SMI_BUSY (1 << 28) /* Busy */
88#define MVNETA_MBUS_RETRY 0x2010
89#define MVNETA_UNIT_INTR_CAUSE 0x2080
90#define MVNETA_UNIT_CONTROL 0x20B0
91#define MVNETA_PHY_POLLING_ENABLE BIT(1)
92#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
93#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
94#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
Stefan Roese572be4a2016-05-19 17:46:36 +020095#define MVNETA_WIN_SIZE_MASK (0xffff0000)
Stefan Roese3e103812014-10-22 12:13:14 +020096#define MVNETA_BASE_ADDR_ENABLE 0x2290
Stefan Roese572be4a2016-05-19 17:46:36 +020097#define MVNETA_BASE_ADDR_ENABLE_BIT 0x1
98#define MVNETA_PORT_ACCESS_PROTECT 0x2294
99#define MVNETA_PORT_ACCESS_PROTECT_WIN0_RW 0x3
Stefan Roese3e103812014-10-22 12:13:14 +0200100#define MVNETA_PORT_CONFIG 0x2400
101#define MVNETA_UNI_PROMISC_MODE BIT(0)
102#define MVNETA_DEF_RXQ(q) ((q) << 1)
103#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
104#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
105#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
106#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
107#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
108#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
109#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
110 MVNETA_DEF_RXQ_ARP(q) | \
111 MVNETA_DEF_RXQ_TCP(q) | \
112 MVNETA_DEF_RXQ_UDP(q) | \
113 MVNETA_DEF_RXQ_BPDU(q) | \
114 MVNETA_TX_UNSET_ERR_SUM | \
115 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
116#define MVNETA_PORT_CONFIG_EXTEND 0x2404
117#define MVNETA_MAC_ADDR_LOW 0x2414
118#define MVNETA_MAC_ADDR_HIGH 0x2418
119#define MVNETA_SDMA_CONFIG 0x241c
120#define MVNETA_SDMA_BRST_SIZE_16 4
121#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
122#define MVNETA_RX_NO_DATA_SWAP BIT(4)
123#define MVNETA_TX_NO_DATA_SWAP BIT(5)
124#define MVNETA_DESC_SWAP BIT(6)
125#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
126#define MVNETA_PORT_STATUS 0x2444
127#define MVNETA_TX_IN_PRGRS BIT(1)
128#define MVNETA_TX_FIFO_EMPTY BIT(8)
129#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
130#define MVNETA_SERDES_CFG 0x24A0
131#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
132#define MVNETA_QSGMII_SERDES_PROTO 0x0667
133#define MVNETA_TYPE_PRIO 0x24bc
134#define MVNETA_FORCE_UNI BIT(21)
135#define MVNETA_TXQ_CMD_1 0x24e4
136#define MVNETA_TXQ_CMD 0x2448
137#define MVNETA_TXQ_DISABLE_SHIFT 8
138#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
139#define MVNETA_ACC_MODE 0x2500
140#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
141#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
142#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
143#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
144
145/* Exception Interrupt Port/Queue Cause register */
146
147#define MVNETA_INTR_NEW_CAUSE 0x25a0
148#define MVNETA_INTR_NEW_MASK 0x25a4
149
150/* bits 0..7 = TXQ SENT, one bit per queue.
151 * bits 8..15 = RXQ OCCUP, one bit per queue.
152 * bits 16..23 = RXQ FREE, one bit per queue.
153 * bit 29 = OLD_REG_SUM, see old reg ?
154 * bit 30 = TX_ERR_SUM, one bit for 4 ports
155 * bit 31 = MISC_SUM, one bit for 4 ports
156 */
157#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
158#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
159#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
160#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
161
162#define MVNETA_INTR_OLD_CAUSE 0x25a8
163#define MVNETA_INTR_OLD_MASK 0x25ac
164
165/* Data Path Port/Queue Cause Register */
166#define MVNETA_INTR_MISC_CAUSE 0x25b0
167#define MVNETA_INTR_MISC_MASK 0x25b4
168#define MVNETA_INTR_ENABLE 0x25b8
169
170#define MVNETA_RXQ_CMD 0x2680
171#define MVNETA_RXQ_DISABLE_SHIFT 8
172#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
173#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
174#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
175#define MVNETA_GMAC_CTRL_0 0x2c00
176#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
177#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
178#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
179#define MVNETA_GMAC_CTRL_2 0x2c08
180#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
181#define MVNETA_GMAC2_PORT_RGMII BIT(4)
182#define MVNETA_GMAC2_PORT_RESET BIT(6)
183#define MVNETA_GMAC_STATUS 0x2c10
184#define MVNETA_GMAC_LINK_UP BIT(0)
185#define MVNETA_GMAC_SPEED_1000 BIT(1)
186#define MVNETA_GMAC_SPEED_100 BIT(2)
187#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
188#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
189#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
190#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
191#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
192#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
193#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
194#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200195#define MVNETA_GMAC_FORCE_LINK_UP (BIT(0) | BIT(1))
196#define MVNETA_GMAC_IB_BYPASS_AN_EN BIT(3)
Stefan Roese3e103812014-10-22 12:13:14 +0200197#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
198#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
199#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200200#define MVNETA_GMAC_SET_FC_EN BIT(8)
201#define MVNETA_GMAC_ADVERT_FC_EN BIT(9)
Stefan Roese3e103812014-10-22 12:13:14 +0200202#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
203#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200204#define MVNETA_GMAC_SAMPLE_TX_CFG_EN BIT(15)
Stefan Roese3e103812014-10-22 12:13:14 +0200205#define MVNETA_MIB_COUNTERS_BASE 0x3080
206#define MVNETA_MIB_LATE_COLLISION 0x7c
207#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
208#define MVNETA_DA_FILT_OTH_MCAST 0x3500
209#define MVNETA_DA_FILT_UCAST_BASE 0x3600
210#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
211#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
212#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
213#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
214#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
215#define MVNETA_TXQ_DEC_SENT_SHIFT 16
216#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
217#define MVNETA_TXQ_SENT_DESC_SHIFT 16
218#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
219#define MVNETA_PORT_TX_RESET 0x3cf0
220#define MVNETA_PORT_TX_DMA_RESET BIT(0)
221#define MVNETA_TX_MTU 0x3e0c
222#define MVNETA_TX_TOKEN_SIZE 0x3e14
223#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
224#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
225#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
226
227/* Descriptor ring Macros */
228#define MVNETA_QUEUE_NEXT_DESC(q, index) \
229 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
230
231/* Various constants */
232
233/* Coalescing */
234#define MVNETA_TXDONE_COAL_PKTS 16
235#define MVNETA_RX_COAL_PKTS 32
236#define MVNETA_RX_COAL_USEC 100
237
238/* The two bytes Marvell header. Either contains a special value used
239 * by Marvell switches when a specific hardware mode is enabled (not
240 * supported by this driver) or is filled automatically by zeroes on
241 * the RX side. Those two bytes being at the front of the Ethernet
242 * header, they allow to have the IP header aligned on a 4 bytes
243 * boundary automatically: the hardware skips those two bytes on its
244 * own.
245 */
246#define MVNETA_MH_SIZE 2
247
248#define MVNETA_VLAN_TAG_LEN 4
249
250#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
251#define MVNETA_TX_CSUM_MAX_SIZE 9800
252#define MVNETA_ACC_MODE_EXT 1
253
254/* Timeout constants */
255#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
256#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
257#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
258
259#define MVNETA_TX_MTU_MAX 0x3ffff
260
261/* Max number of Rx descriptors */
262#define MVNETA_MAX_RXD 16
263
264/* Max number of Tx descriptors */
265#define MVNETA_MAX_TXD 16
266
267/* descriptor aligned size */
268#define MVNETA_DESC_ALIGNED_SIZE 32
269
270struct mvneta_port {
271 void __iomem *base;
272 struct mvneta_rx_queue *rxqs;
273 struct mvneta_tx_queue *txqs;
274
275 u8 mcast_count[256];
276 u16 tx_ring_size;
277 u16 rx_ring_size;
278
279 phy_interface_t phy_interface;
280 unsigned int link;
281 unsigned int duplex;
282 unsigned int speed;
283
284 int init;
285 int phyaddr;
286 struct phy_device *phydev;
Simon Glassfa4689a2019-12-06 21:41:35 -0700287#if CONFIG_IS_ENABLED(DM_GPIO)
Aditya Prayogac9fe02a2018-12-05 00:39:23 +0800288 struct gpio_desc phy_reset_gpio;
Robert Marko58c98732022-03-24 10:57:37 +0100289 struct gpio_desc sfp_tx_disable_gpio;
Aditya Prayogac9fe02a2018-12-05 00:39:23 +0800290#endif
Stefan Roese3e103812014-10-22 12:13:14 +0200291 struct mii_dev *bus;
292};
293
294/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
295 * layout of the transmit and reception DMA descriptors, and their
296 * layout is therefore defined by the hardware design
297 */
298
299#define MVNETA_TX_L3_OFF_SHIFT 0
300#define MVNETA_TX_IP_HLEN_SHIFT 8
301#define MVNETA_TX_L4_UDP BIT(16)
302#define MVNETA_TX_L3_IP6 BIT(17)
303#define MVNETA_TXD_IP_CSUM BIT(18)
304#define MVNETA_TXD_Z_PAD BIT(19)
305#define MVNETA_TXD_L_DESC BIT(20)
306#define MVNETA_TXD_F_DESC BIT(21)
307#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
308 MVNETA_TXD_L_DESC | \
309 MVNETA_TXD_F_DESC)
310#define MVNETA_TX_L4_CSUM_FULL BIT(30)
311#define MVNETA_TX_L4_CSUM_NOT BIT(31)
312
313#define MVNETA_RXD_ERR_CRC 0x0
314#define MVNETA_RXD_ERR_SUMMARY BIT(16)
315#define MVNETA_RXD_ERR_OVERRUN BIT(17)
316#define MVNETA_RXD_ERR_LEN BIT(18)
317#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
318#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
319#define MVNETA_RXD_L3_IP4 BIT(25)
320#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
321#define MVNETA_RXD_L4_CSUM_OK BIT(30)
322
323struct mvneta_tx_desc {
324 u32 command; /* Options used by HW for packet transmitting.*/
325 u16 reserverd1; /* csum_l4 (for future use) */
326 u16 data_size; /* Data size of transmitted packet in bytes */
327 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
328 u32 reserved2; /* hw_cmd - (for future use, PMT) */
329 u32 reserved3[4]; /* Reserved - (for future use) */
330};
331
332struct mvneta_rx_desc {
333 u32 status; /* Info about received packet */
334 u16 reserved1; /* pnc_info - (for future use, PnC) */
335 u16 data_size; /* Size of received packet in bytes */
336
337 u32 buf_phys_addr; /* Physical address of the buffer */
338 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
339
340 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
341 u16 reserved3; /* prefetch_cmd, for future use */
342 u16 reserved4; /* csum_l4 - (for future use, PnC) */
343
344 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
345 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
346};
347
348struct mvneta_tx_queue {
349 /* Number of this TX queue, in the range 0-7 */
350 u8 id;
351
352 /* Number of TX DMA descriptors in the descriptor ring */
353 int size;
354
355 /* Index of last TX DMA descriptor that was inserted */
356 int txq_put_index;
357
358 /* Index of the TX DMA descriptor to be cleaned up */
359 int txq_get_index;
360
361 /* Virtual address of the TX DMA descriptors array */
362 struct mvneta_tx_desc *descs;
363
364 /* DMA address of the TX DMA descriptors array */
365 dma_addr_t descs_phys;
366
367 /* Index of the last TX DMA descriptor */
368 int last_desc;
369
370 /* Index of the next TX DMA descriptor to process */
371 int next_desc_to_proc;
372};
373
374struct mvneta_rx_queue {
375 /* rx queue number, in the range 0-7 */
376 u8 id;
377
378 /* num of rx descriptors in the rx descriptor ring */
379 int size;
380
381 /* Virtual address of the RX DMA descriptors array */
382 struct mvneta_rx_desc *descs;
383
384 /* DMA address of the RX DMA descriptors array */
385 dma_addr_t descs_phys;
386
387 /* Index of the last RX DMA descriptor */
388 int last_desc;
389
390 /* Index of the next RX DMA descriptor to process */
391 int next_desc_to_proc;
392};
393
394/* U-Boot doesn't use the queues, so set the number to 1 */
395static int rxq_number = 1;
396static int txq_number = 1;
397static int rxq_def;
398
399struct buffer_location {
400 struct mvneta_tx_desc *tx_descs;
401 struct mvneta_rx_desc *rx_descs;
402 u32 rx_buffers;
403};
404
405/*
406 * All 4 interfaces use the same global buffer, since only one interface
407 * can be enabled at once
408 */
409static struct buffer_location buffer_loc;
410
411/*
412 * Page table entries are set to 1MB, or multiples of 1MB
413 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
414 */
415#define BD_SPACE (1 << 20)
416
Konstantin Porotchkinfb8e2022017-02-16 13:52:27 +0200417/*
418 * Dummy implementation that can be overwritten by a board
419 * specific function
420 */
421__weak int board_network_enable(struct mii_dev *bus)
422{
423 return 0;
424}
425
Stefan Roese3e103812014-10-22 12:13:14 +0200426/* Utility/helper methods */
427
428/* Write helper method */
429static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
430{
431 writel(data, pp->base + offset);
432}
433
434/* Read helper method */
435static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
436{
437 return readl(pp->base + offset);
438}
439
440/* Clear all MIB counters */
441static void mvneta_mib_counters_clear(struct mvneta_port *pp)
442{
443 int i;
444
445 /* Perform dummy reads from MIB counters */
446 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
447 mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
448}
449
450/* Rx descriptors helper methods */
451
452/* Checks whether the RX descriptor having this status is both the first
453 * and the last descriptor for the RX packet. Each RX packet is currently
454 * received through a single RX descriptor, so not having each RX
455 * descriptor with its first and last bits set is an error
456 */
457static int mvneta_rxq_desc_is_first_last(u32 status)
458{
459 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
460 MVNETA_RXD_FIRST_LAST_DESC;
461}
462
463/* Add number of descriptors ready to receive new packets */
464static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
465 struct mvneta_rx_queue *rxq,
466 int ndescs)
467{
468 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
469 * be added at once
470 */
471 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
472 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
473 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
474 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
475 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
476 }
477
478 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
479 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
480}
481
482/* Get number of RX descriptors occupied by received packets */
483static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
484 struct mvneta_rx_queue *rxq)
485{
486 u32 val;
487
488 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
489 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
490}
491
492/* Update num of rx desc called upon return from rx path or
493 * from mvneta_rxq_drop_pkts().
494 */
495static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
496 struct mvneta_rx_queue *rxq,
497 int rx_done, int rx_filled)
498{
499 u32 val;
500
501 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
502 val = rx_done |
503 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
504 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
505 return;
506 }
507
508 /* Only 255 descriptors can be added at once */
509 while ((rx_done > 0) || (rx_filled > 0)) {
510 if (rx_done <= 0xff) {
511 val = rx_done;
512 rx_done = 0;
513 } else {
514 val = 0xff;
515 rx_done -= 0xff;
516 }
517 if (rx_filled <= 0xff) {
518 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
519 rx_filled = 0;
520 } else {
521 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
522 rx_filled -= 0xff;
523 }
524 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
525 }
526}
527
528/* Get pointer to next RX descriptor to be processed by SW */
529static struct mvneta_rx_desc *
530mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
531{
532 int rx_desc = rxq->next_desc_to_proc;
533
534 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
535 return rxq->descs + rx_desc;
536}
537
538/* Tx descriptors helper methods */
539
540/* Update HW with number of TX descriptors to be sent */
541static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
542 struct mvneta_tx_queue *txq,
543 int pend_desc)
544{
545 u32 val;
546
547 /* Only 255 descriptors can be added at once ; Assume caller
Heinrich Schuchardt42376962017-08-29 18:44:37 +0200548 * process TX descriptors in quanta less than 256
Stefan Roese3e103812014-10-22 12:13:14 +0200549 */
550 val = pend_desc;
551 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
552}
553
554/* Get pointer to next TX descriptor to be processed (send) by HW */
555static struct mvneta_tx_desc *
556mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
557{
558 int tx_desc = txq->next_desc_to_proc;
559
560 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
561 return txq->descs + tx_desc;
562}
563
564/* Set rxq buf size */
565static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
566 struct mvneta_rx_queue *rxq,
567 int buf_size)
568{
569 u32 val;
570
571 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
572
573 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
574 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
575
576 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
577}
578
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200579static int mvneta_port_is_fixed_link(struct mvneta_port *pp)
580{
581 /* phy_addr is set to invalid value for fixed link */
582 return pp->phyaddr > PHY_MAX_ADDR;
583}
584
585
Stefan Roese3e103812014-10-22 12:13:14 +0200586/* Start the Ethernet port RX and TX activity */
587static void mvneta_port_up(struct mvneta_port *pp)
588{
589 int queue;
590 u32 q_map;
591
592 /* Enable all initialized TXs. */
593 mvneta_mib_counters_clear(pp);
594 q_map = 0;
595 for (queue = 0; queue < txq_number; queue++) {
596 struct mvneta_tx_queue *txq = &pp->txqs[queue];
597 if (txq->descs != NULL)
598 q_map |= (1 << queue);
599 }
600 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
601
602 /* Enable all initialized RXQs. */
603 q_map = 0;
604 for (queue = 0; queue < rxq_number; queue++) {
605 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
606 if (rxq->descs != NULL)
607 q_map |= (1 << queue);
608 }
609 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
610}
611
612/* Stop the Ethernet port activity */
613static void mvneta_port_down(struct mvneta_port *pp)
614{
615 u32 val;
616 int count;
617
618 /* Stop Rx port activity. Check port Rx activity. */
619 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
620
621 /* Issue stop command for active channels only */
622 if (val != 0)
623 mvreg_write(pp, MVNETA_RXQ_CMD,
624 val << MVNETA_RXQ_DISABLE_SHIFT);
625
626 /* Wait for all Rx activity to terminate. */
627 count = 0;
628 do {
629 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400630 dev_warn(pp->phydev->dev,
631 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
632 val);
Stefan Roese3e103812014-10-22 12:13:14 +0200633 break;
634 }
635 mdelay(1);
636
637 val = mvreg_read(pp, MVNETA_RXQ_CMD);
638 } while (val & 0xff);
639
640 /* Stop Tx port activity. Check port Tx activity. Issue stop
641 * command for active channels only
642 */
643 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
644
645 if (val != 0)
646 mvreg_write(pp, MVNETA_TXQ_CMD,
647 (val << MVNETA_TXQ_DISABLE_SHIFT));
648
649 /* Wait for all Tx activity to terminate. */
650 count = 0;
651 do {
652 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400653 dev_warn(pp->phydev->dev,
654 "TIMEOUT for TX stopped status=0x%08x\n",
655 val);
Stefan Roese3e103812014-10-22 12:13:14 +0200656 break;
657 }
658 mdelay(1);
659
660 /* Check TX Command reg that all Txqs are stopped */
661 val = mvreg_read(pp, MVNETA_TXQ_CMD);
662
663 } while (val & 0xff);
664
665 /* Double check to verify that TX FIFO is empty */
666 count = 0;
667 do {
668 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400669 dev_warn(pp->phydev->dev,
670 "TX FIFO empty timeout status=0x08%x\n",
671 val);
Stefan Roese3e103812014-10-22 12:13:14 +0200672 break;
673 }
674 mdelay(1);
675
676 val = mvreg_read(pp, MVNETA_PORT_STATUS);
677 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
678 (val & MVNETA_TX_IN_PRGRS));
679
680 udelay(200);
681}
682
683/* Enable the port by setting the port enable bit of the MAC control register */
684static void mvneta_port_enable(struct mvneta_port *pp)
685{
686 u32 val;
687
688 /* Enable port */
689 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
690 val |= MVNETA_GMAC0_PORT_ENABLE;
691 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
692}
693
694/* Disable the port and wait for about 200 usec before retuning */
695static void mvneta_port_disable(struct mvneta_port *pp)
696{
697 u32 val;
698
699 /* Reset the Enable bit in the Serial Control Register */
700 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
701 val &= ~MVNETA_GMAC0_PORT_ENABLE;
702 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
703
704 udelay(200);
705}
706
707/* Multicast tables methods */
708
709/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
710static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
711{
712 int offset;
713 u32 val;
714
715 if (queue == -1) {
716 val = 0;
717 } else {
718 val = 0x1 | (queue << 1);
719 val |= (val << 24) | (val << 16) | (val << 8);
720 }
721
722 for (offset = 0; offset <= 0xc; offset += 4)
723 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
724}
725
726/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
727static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
728{
729 int offset;
730 u32 val;
731
732 if (queue == -1) {
733 val = 0;
734 } else {
735 val = 0x1 | (queue << 1);
736 val |= (val << 24) | (val << 16) | (val << 8);
737 }
738
739 for (offset = 0; offset <= 0xfc; offset += 4)
740 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
741}
742
743/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
744static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
745{
746 int offset;
747 u32 val;
748
749 if (queue == -1) {
750 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
751 val = 0;
752 } else {
753 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
754 val = 0x1 | (queue << 1);
755 val |= (val << 24) | (val << 16) | (val << 8);
756 }
757
758 for (offset = 0; offset <= 0xfc; offset += 4)
759 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
760}
761
762/* This method sets defaults to the NETA port:
763 * Clears interrupt Cause and Mask registers.
764 * Clears all MAC tables.
765 * Sets defaults to all registers.
766 * Resets RX and TX descriptor rings.
767 * Resets PHY.
768 * This method can be called after mvneta_port_down() to return the port
769 * settings to defaults.
770 */
771static void mvneta_defaults_set(struct mvneta_port *pp)
772{
773 int cpu;
774 int queue;
775 u32 val;
776
777 /* Clear all Cause registers */
778 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
779 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
780 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
781
782 /* Mask all interrupts */
783 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
784 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
785 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
786 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
787
788 /* Enable MBUS Retry bit16 */
789 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
790
791 /* Set CPU queue access map - all CPUs have access to all RX
792 * queues and to all TX queues
793 */
794 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
795 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
796 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
797 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
798
799 /* Reset RX and TX DMAs */
800 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
801 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
802
803 /* Disable Legacy WRR, Disable EJP, Release from reset */
804 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
805 for (queue = 0; queue < txq_number; queue++) {
806 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
807 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
808 }
809
810 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
811 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
812
813 /* Set Port Acceleration Mode */
814 val = MVNETA_ACC_MODE_EXT;
815 mvreg_write(pp, MVNETA_ACC_MODE, val);
816
817 /* Update val of portCfg register accordingly with all RxQueue types */
818 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
819 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
820
821 val = 0;
822 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
823 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
824
825 /* Build PORT_SDMA_CONFIG_REG */
826 val = 0;
827
828 /* Default burst size */
829 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
830 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
831 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
832
833 /* Assign port SDMA configuration */
834 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
835
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +0200836 /* Enable PHY polling in hardware if not in fixed-link mode */
837 if (!mvneta_port_is_fixed_link(pp)) {
838 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
839 val |= MVNETA_PHY_POLLING_ENABLE;
840 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
841 }
Stefan Roese3e103812014-10-22 12:13:14 +0200842
843 mvneta_set_ucast_table(pp, -1);
844 mvneta_set_special_mcast_table(pp, -1);
845 mvneta_set_other_mcast_table(pp, -1);
846}
847
848/* Set unicast address */
849static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
850 int queue)
851{
852 unsigned int unicast_reg;
853 unsigned int tbl_offset;
854 unsigned int reg_offset;
855
856 /* Locate the Unicast table entry */
857 last_nibble = (0xf & last_nibble);
858
859 /* offset from unicast tbl base */
860 tbl_offset = (last_nibble / 4) * 4;
861
862 /* offset within the above reg */
863 reg_offset = last_nibble % 4;
864
865 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
866
867 if (queue == -1) {
868 /* Clear accepts frame bit at specified unicast DA tbl entry */
869 unicast_reg &= ~(0xff << (8 * reg_offset));
870 } else {
871 unicast_reg &= ~(0xff << (8 * reg_offset));
872 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
873 }
874
875 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
876}
877
878/* Set mac address */
879static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
880 int queue)
881{
882 unsigned int mac_h;
883 unsigned int mac_l;
884
885 if (queue != -1) {
886 mac_l = (addr[4] << 8) | (addr[5]);
887 mac_h = (addr[0] << 24) | (addr[1] << 16) |
888 (addr[2] << 8) | (addr[3] << 0);
889
890 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
891 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
892 }
893
894 /* Accept frames of this address */
895 mvneta_set_ucast_addr(pp, addr[5], queue);
896}
897
Matt Pelland668a5f22018-03-27 13:18:25 -0400898static int mvneta_write_hwaddr(struct udevice *dev)
899{
900 mvneta_mac_addr_set(dev_get_priv(dev),
Simon Glassfa20e932020-12-03 16:55:20 -0700901 ((struct eth_pdata *)dev_get_plat(dev))->enetaddr,
Matt Pelland668a5f22018-03-27 13:18:25 -0400902 rxq_def);
903
904 return 0;
905}
906
Stefan Roese3e103812014-10-22 12:13:14 +0200907/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
908static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
909 u32 phys_addr, u32 cookie)
910{
911 rx_desc->buf_cookie = cookie;
912 rx_desc->buf_phys_addr = phys_addr;
913}
914
915/* Decrement sent descriptors counter */
916static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
917 struct mvneta_tx_queue *txq,
918 int sent_desc)
919{
920 u32 val;
921
922 /* Only 255 TX descriptors can be updated at once */
923 while (sent_desc > 0xff) {
924 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
925 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
926 sent_desc = sent_desc - 0xff;
927 }
928
929 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
930 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
931}
932
933/* Get number of TX descriptors already sent by HW */
934static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
935 struct mvneta_tx_queue *txq)
936{
937 u32 val;
938 int sent_desc;
939
940 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
941 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
942 MVNETA_TXQ_SENT_DESC_SHIFT;
943
944 return sent_desc;
945}
946
947/* Display more error info */
948static void mvneta_rx_error(struct mvneta_port *pp,
949 struct mvneta_rx_desc *rx_desc)
950{
951 u32 status = rx_desc->status;
952
953 if (!mvneta_rxq_desc_is_first_last(status)) {
Sean Andersonecbbddf2020-09-15 10:44:55 -0400954 dev_err(pp->phydev->dev,
955 "bad rx status %08x (buffer oversize), size=%d\n",
956 status, rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200957 return;
958 }
959
960 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
961 case MVNETA_RXD_ERR_CRC:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400962 dev_err(pp->phydev->dev,
963 "bad rx status %08x (crc error), size=%d\n", status,
964 rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200965 break;
966 case MVNETA_RXD_ERR_OVERRUN:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400967 dev_err(pp->phydev->dev,
968 "bad rx status %08x (overrun error), size=%d\n", status,
969 rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200970 break;
971 case MVNETA_RXD_ERR_LEN:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400972 dev_err(pp->phydev->dev,
973 "bad rx status %08x (max frame length error), size=%d\n",
974 status, rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200975 break;
976 case MVNETA_RXD_ERR_RESOURCE:
Sean Andersonecbbddf2020-09-15 10:44:55 -0400977 dev_err(pp->phydev->dev,
978 "bad rx status %08x (resource error), size=%d\n",
979 status, rx_desc->data_size);
Stefan Roese3e103812014-10-22 12:13:14 +0200980 break;
981 }
982}
983
984static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
985 int rxq)
986{
987 return &pp->rxqs[rxq];
988}
989
990
991/* Drop packets received by the RXQ and free buffers */
992static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
993 struct mvneta_rx_queue *rxq)
994{
995 int rx_done;
996
997 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
998 if (rx_done)
999 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1000}
1001
1002/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
1003static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
1004 int num)
1005{
1006 int i;
1007
1008 for (i = 0; i < num; i++) {
1009 u32 addr;
1010
1011 /* U-Boot special: Fill in the rx buffer addresses */
1012 addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
1013 mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
1014 }
1015
1016 /* Add this number of RX descriptors as non occupied (ready to
1017 * get packets)
1018 */
1019 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
1020
1021 return 0;
1022}
1023
1024/* Rx/Tx queue initialization/cleanup methods */
1025
1026/* Create a specified RX queue */
1027static int mvneta_rxq_init(struct mvneta_port *pp,
1028 struct mvneta_rx_queue *rxq)
1029
1030{
1031 rxq->size = pp->rx_ring_size;
1032
1033 /* Allocate memory for RX descriptors */
1034 rxq->descs_phys = (dma_addr_t)rxq->descs;
1035 if (rxq->descs == NULL)
1036 return -ENOMEM;
1037
Jon Nettleton543efd12018-05-30 08:52:29 +03001038 WARN_ON(rxq->descs != PTR_ALIGN(rxq->descs, ARCH_DMA_MINALIGN));
1039
Stefan Roese3e103812014-10-22 12:13:14 +02001040 rxq->last_desc = rxq->size - 1;
1041
1042 /* Set Rx descriptors queue starting address */
1043 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
1044 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
1045
1046 /* Fill RXQ with buffers from RX pool */
1047 mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
1048 mvneta_rxq_fill(pp, rxq, rxq->size);
1049
1050 return 0;
1051}
1052
1053/* Cleanup Rx queue */
1054static void mvneta_rxq_deinit(struct mvneta_port *pp,
1055 struct mvneta_rx_queue *rxq)
1056{
1057 mvneta_rxq_drop_pkts(pp, rxq);
1058
1059 rxq->descs = NULL;
1060 rxq->last_desc = 0;
1061 rxq->next_desc_to_proc = 0;
1062 rxq->descs_phys = 0;
1063}
1064
1065/* Create and initialize a tx queue */
1066static int mvneta_txq_init(struct mvneta_port *pp,
1067 struct mvneta_tx_queue *txq)
1068{
1069 txq->size = pp->tx_ring_size;
1070
1071 /* Allocate memory for TX descriptors */
Stefan Roese6564d992016-05-19 18:09:17 +02001072 txq->descs_phys = (dma_addr_t)txq->descs;
Stefan Roese3e103812014-10-22 12:13:14 +02001073 if (txq->descs == NULL)
1074 return -ENOMEM;
1075
Jon Nettleton543efd12018-05-30 08:52:29 +03001076 WARN_ON(txq->descs != PTR_ALIGN(txq->descs, ARCH_DMA_MINALIGN));
1077
Stefan Roese3e103812014-10-22 12:13:14 +02001078 txq->last_desc = txq->size - 1;
1079
1080 /* Set maximum bandwidth for enabled TXQs */
1081 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
1082 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
1083
1084 /* Set Tx descriptors queue starting address */
1085 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
1086 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
1087
1088 return 0;
1089}
1090
1091/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
1092static void mvneta_txq_deinit(struct mvneta_port *pp,
1093 struct mvneta_tx_queue *txq)
1094{
1095 txq->descs = NULL;
1096 txq->last_desc = 0;
1097 txq->next_desc_to_proc = 0;
1098 txq->descs_phys = 0;
1099
1100 /* Set minimum bandwidth for disabled TXQs */
1101 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
1102 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
1103
1104 /* Set Tx descriptors queue starting address and size */
1105 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
1106 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
1107}
1108
1109/* Cleanup all Tx queues */
1110static void mvneta_cleanup_txqs(struct mvneta_port *pp)
1111{
1112 int queue;
1113
1114 for (queue = 0; queue < txq_number; queue++)
1115 mvneta_txq_deinit(pp, &pp->txqs[queue]);
1116}
1117
1118/* Cleanup all Rx queues */
1119static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
1120{
1121 int queue;
1122
1123 for (queue = 0; queue < rxq_number; queue++)
1124 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
1125}
1126
1127
1128/* Init all Rx queues */
1129static int mvneta_setup_rxqs(struct mvneta_port *pp)
1130{
1131 int queue;
1132
1133 for (queue = 0; queue < rxq_number; queue++) {
1134 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
1135 if (err) {
Sean Andersonecbbddf2020-09-15 10:44:55 -04001136 dev_err(pp->phydev->dev, "%s: can't create rxq=%d\n",
1137 __func__, queue);
Stefan Roese3e103812014-10-22 12:13:14 +02001138 mvneta_cleanup_rxqs(pp);
1139 return err;
1140 }
1141 }
1142
1143 return 0;
1144}
1145
1146/* Init all tx queues */
1147static int mvneta_setup_txqs(struct mvneta_port *pp)
1148{
1149 int queue;
1150
1151 for (queue = 0; queue < txq_number; queue++) {
1152 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
1153 if (err) {
Sean Andersonecbbddf2020-09-15 10:44:55 -04001154 dev_err(pp->phydev->dev, "%s: can't create txq=%d\n",
1155 __func__, queue);
Stefan Roese3e103812014-10-22 12:13:14 +02001156 mvneta_cleanup_txqs(pp);
1157 return err;
1158 }
1159 }
1160
1161 return 0;
1162}
1163
1164static void mvneta_start_dev(struct mvneta_port *pp)
1165{
1166 /* start the Rx/Tx activity */
1167 mvneta_port_enable(pp);
1168}
1169
Stefan Roese05b38c12015-11-19 07:46:15 +01001170static void mvneta_adjust_link(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001171{
Stefan Roese05b38c12015-11-19 07:46:15 +01001172 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001173 struct phy_device *phydev = pp->phydev;
1174 int status_change = 0;
1175
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001176 if (mvneta_port_is_fixed_link(pp)) {
1177 debug("Using fixed link, skip link adjust\n");
1178 return;
1179 }
1180
Stefan Roese3e103812014-10-22 12:13:14 +02001181 if (phydev->link) {
1182 if ((pp->speed != phydev->speed) ||
1183 (pp->duplex != phydev->duplex)) {
1184 u32 val;
1185
1186 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1187 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
1188 MVNETA_GMAC_CONFIG_GMII_SPEED |
1189 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
1190 MVNETA_GMAC_AN_SPEED_EN |
1191 MVNETA_GMAC_AN_DUPLEX_EN);
1192
1193 if (phydev->duplex)
1194 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1195
1196 if (phydev->speed == SPEED_1000)
1197 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
Marek BehĂșn0250f262022-04-27 12:41:45 +02001198 else if (pp->speed == SPEED_100)
Stefan Roese3e103812014-10-22 12:13:14 +02001199 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1200
1201 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1202
1203 pp->duplex = phydev->duplex;
1204 pp->speed = phydev->speed;
1205 }
1206 }
1207
1208 if (phydev->link != pp->link) {
1209 if (!phydev->link) {
1210 pp->duplex = -1;
1211 pp->speed = 0;
1212 }
1213
1214 pp->link = phydev->link;
1215 status_change = 1;
1216 }
1217
1218 if (status_change) {
1219 if (phydev->link) {
1220 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1221 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
1222 MVNETA_GMAC_FORCE_LINK_DOWN);
1223 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1224 mvneta_port_up(pp);
1225 } else {
1226 mvneta_port_down(pp);
1227 }
1228 }
1229}
1230
Stefan Roese05b38c12015-11-19 07:46:15 +01001231static int mvneta_open(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001232{
Stefan Roese05b38c12015-11-19 07:46:15 +01001233 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001234 int ret;
1235
1236 ret = mvneta_setup_rxqs(pp);
1237 if (ret)
1238 return ret;
1239
1240 ret = mvneta_setup_txqs(pp);
1241 if (ret)
1242 return ret;
1243
1244 mvneta_adjust_link(dev);
1245
1246 mvneta_start_dev(pp);
1247
1248 return 0;
1249}
1250
1251/* Initialize hw */
Stefan Roese05b38c12015-11-19 07:46:15 +01001252static int mvneta_init2(struct mvneta_port *pp)
Stefan Roese3e103812014-10-22 12:13:14 +02001253{
1254 int queue;
1255
1256 /* Disable port */
1257 mvneta_port_disable(pp);
1258
1259 /* Set port default values */
1260 mvneta_defaults_set(pp);
1261
1262 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
1263 GFP_KERNEL);
1264 if (!pp->txqs)
1265 return -ENOMEM;
1266
1267 /* U-Boot special: use preallocated area */
1268 pp->txqs[0].descs = buffer_loc.tx_descs;
1269
1270 /* Initialize TX descriptor rings */
1271 for (queue = 0; queue < txq_number; queue++) {
1272 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1273 txq->id = queue;
1274 txq->size = pp->tx_ring_size;
1275 }
1276
1277 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
1278 GFP_KERNEL);
1279 if (!pp->rxqs) {
1280 kfree(pp->txqs);
1281 return -ENOMEM;
1282 }
1283
1284 /* U-Boot special: use preallocated area */
1285 pp->rxqs[0].descs = buffer_loc.rx_descs;
1286
1287 /* Create Rx descriptor rings */
1288 for (queue = 0; queue < rxq_number; queue++) {
1289 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1290 rxq->id = queue;
1291 rxq->size = pp->rx_ring_size;
1292 }
1293
1294 return 0;
1295}
1296
1297/* platform glue : initialize decoding windows */
Stefan Roese572be4a2016-05-19 17:46:36 +02001298
1299/*
1300 * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1301 * First layer is: GbE Address window that resides inside the GBE unit,
1302 * Second layer is: Fabric address window which is located in the NIC400
1303 * (South Fabric).
1304 * To simplify the address decode configuration for Armada3700, we bypass the
1305 * first layer of GBE decode window by setting the first window to 4GB.
1306 */
1307static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1308{
1309 /*
1310 * Set window size to 4GB, to bypass GBE address decode, leave the
1311 * work to MBUS decode window
1312 */
1313 mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1314
1315 /* Enable GBE address decode window 0 by set bit 0 to 0 */
1316 clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1317 MVNETA_BASE_ADDR_ENABLE_BIT);
1318
1319 /* Set GBE address decode window 0 to full Access (read or write) */
1320 setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1321 MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1322}
1323
Stefan Roese3e103812014-10-22 12:13:14 +02001324static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
1325{
1326 const struct mbus_dram_target_info *dram;
1327 u32 win_enable;
1328 u32 win_protect;
1329 int i;
1330
1331 dram = mvebu_mbus_dram_info();
1332 for (i = 0; i < 6; i++) {
1333 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1334 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1335
1336 if (i < 4)
1337 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1338 }
1339
1340 win_enable = 0x3f;
1341 win_protect = 0;
1342
1343 for (i = 0; i < dram->num_cs; i++) {
1344 const struct mbus_dram_window *cs = dram->cs + i;
1345 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
1346 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
1347
1348 mvreg_write(pp, MVNETA_WIN_SIZE(i),
1349 (cs->size - 1) & 0xffff0000);
1350
1351 win_enable &= ~(1 << i);
1352 win_protect |= 3 << (2 * i);
1353 }
1354
1355 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1356}
1357
1358/* Power up the port */
1359static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
1360{
1361 u32 ctrl;
1362
1363 /* MAC Cause register should be cleared */
1364 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
1365
1366 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1367
1368 /* Even though it might look weird, when we're configured in
1369 * SGMII or QSGMII mode, the RGMII bit needs to be set.
1370 */
1371 switch (phy_mode) {
1372 case PHY_INTERFACE_MODE_QSGMII:
1373 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
1374 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1375 break;
1376 case PHY_INTERFACE_MODE_SGMII:
1377 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
1378 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1379 break;
1380 case PHY_INTERFACE_MODE_RGMII:
1381 case PHY_INTERFACE_MODE_RGMII_ID:
1382 ctrl |= MVNETA_GMAC2_PORT_RGMII;
1383 break;
1384 default:
1385 return -EINVAL;
1386 }
1387
1388 /* Cancel Port Reset */
1389 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
1390 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
1391
1392 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
1393 MVNETA_GMAC2_PORT_RESET) != 0)
1394 continue;
1395
1396 return 0;
1397}
1398
1399/* Device initialization routine */
Stefan Roese05b38c12015-11-19 07:46:15 +01001400static int mvneta_init(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001401{
Simon Glassfa20e932020-12-03 16:55:20 -07001402 struct eth_pdata *pdata = dev_get_plat(dev);
Stefan Roese05b38c12015-11-19 07:46:15 +01001403 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001404 int err;
1405
1406 pp->tx_ring_size = MVNETA_MAX_TXD;
1407 pp->rx_ring_size = MVNETA_MAX_RXD;
1408
Stefan Roese05b38c12015-11-19 07:46:15 +01001409 err = mvneta_init2(pp);
Stefan Roese3e103812014-10-22 12:13:14 +02001410 if (err < 0) {
Sean Andersone0d00042020-09-15 10:44:54 -04001411 dev_err(dev, "can't init eth hal\n");
Stefan Roese3e103812014-10-22 12:13:14 +02001412 return err;
1413 }
1414
Stefan Roese05b38c12015-11-19 07:46:15 +01001415 mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
Stefan Roese3e103812014-10-22 12:13:14 +02001416
1417 err = mvneta_port_power_up(pp, pp->phy_interface);
1418 if (err < 0) {
Sean Andersone0d00042020-09-15 10:44:54 -04001419 dev_err(dev, "can't power up port\n");
Stefan Roese3e103812014-10-22 12:13:14 +02001420 return err;
1421 }
1422
1423 /* Call open() now as it needs to be done before runing send() */
1424 mvneta_open(dev);
1425
1426 return 0;
1427}
1428
1429/* U-Boot only functions follow here */
1430
1431/* SMI / MDIO functions */
1432
1433static int smi_wait_ready(struct mvneta_port *pp)
1434{
1435 u32 timeout = MVNETA_SMI_TIMEOUT;
1436 u32 smi_reg;
1437
1438 /* wait till the SMI is not busy */
1439 do {
1440 /* read smi register */
1441 smi_reg = mvreg_read(pp, MVNETA_SMI);
1442 if (timeout-- == 0) {
1443 printf("Error: SMI busy timeout\n");
1444 return -EFAULT;
1445 }
1446 } while (smi_reg & MVNETA_SMI_BUSY);
1447
1448 return 0;
1449}
1450
1451/*
Stefan Roese05b38c12015-11-19 07:46:15 +01001452 * mvneta_mdio_read - miiphy_read callback function.
Stefan Roese3e103812014-10-22 12:13:14 +02001453 *
1454 * Returns 16bit phy register value, or 0xffff on error
1455 */
Stefan Roese05b38c12015-11-19 07:46:15 +01001456static int mvneta_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
Stefan Roese3e103812014-10-22 12:13:14 +02001457{
Stefan Roese05b38c12015-11-19 07:46:15 +01001458 struct mvneta_port *pp = bus->priv;
Stefan Roese3e103812014-10-22 12:13:14 +02001459 u32 smi_reg;
1460 u32 timeout;
1461
1462 /* check parameters */
Stefan Roese05b38c12015-11-19 07:46:15 +01001463 if (addr > MVNETA_PHY_ADDR_MASK) {
1464 printf("Error: Invalid PHY address %d\n", addr);
Stefan Roese3e103812014-10-22 12:13:14 +02001465 return -EFAULT;
1466 }
1467
Stefan Roese05b38c12015-11-19 07:46:15 +01001468 if (reg > MVNETA_PHY_REG_MASK) {
1469 printf("Err: Invalid register offset %d\n", reg);
Stefan Roese3e103812014-10-22 12:13:14 +02001470 return -EFAULT;
1471 }
1472
1473 /* wait till the SMI is not busy */
1474 if (smi_wait_ready(pp) < 0)
1475 return -EFAULT;
1476
1477 /* fill the phy address and regiser offset and read opcode */
Stefan Roese05b38c12015-11-19 07:46:15 +01001478 smi_reg = (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1479 | (reg << MVNETA_SMI_REG_ADDR_OFFS)
Stefan Roese3e103812014-10-22 12:13:14 +02001480 | MVNETA_SMI_OPCODE_READ;
1481
1482 /* write the smi register */
1483 mvreg_write(pp, MVNETA_SMI, smi_reg);
1484
Stefan Roese05b38c12015-11-19 07:46:15 +01001485 /* wait till read value is ready */
Stefan Roese3e103812014-10-22 12:13:14 +02001486 timeout = MVNETA_SMI_TIMEOUT;
1487
1488 do {
1489 /* read smi register */
1490 smi_reg = mvreg_read(pp, MVNETA_SMI);
1491 if (timeout-- == 0) {
1492 printf("Err: SMI read ready timeout\n");
1493 return -EFAULT;
1494 }
1495 } while (!(smi_reg & MVNETA_SMI_READ_VALID));
1496
1497 /* Wait for the data to update in the SMI register */
1498 for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
1499 ;
1500
Stefan Roese05b38c12015-11-19 07:46:15 +01001501 return mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK;
Stefan Roese3e103812014-10-22 12:13:14 +02001502}
1503
1504/*
Stefan Roese05b38c12015-11-19 07:46:15 +01001505 * mvneta_mdio_write - miiphy_write callback function.
Stefan Roese3e103812014-10-22 12:13:14 +02001506 *
1507 * Returns 0 if write succeed, -EINVAL on bad parameters
1508 * -ETIME on timeout
1509 */
Stefan Roese05b38c12015-11-19 07:46:15 +01001510static int mvneta_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
1511 u16 value)
Stefan Roese3e103812014-10-22 12:13:14 +02001512{
Stefan Roese05b38c12015-11-19 07:46:15 +01001513 struct mvneta_port *pp = bus->priv;
Stefan Roese3e103812014-10-22 12:13:14 +02001514 u32 smi_reg;
1515
1516 /* check parameters */
Stefan Roese05b38c12015-11-19 07:46:15 +01001517 if (addr > MVNETA_PHY_ADDR_MASK) {
1518 printf("Error: Invalid PHY address %d\n", addr);
Stefan Roese3e103812014-10-22 12:13:14 +02001519 return -EFAULT;
1520 }
1521
Stefan Roese05b38c12015-11-19 07:46:15 +01001522 if (reg > MVNETA_PHY_REG_MASK) {
1523 printf("Err: Invalid register offset %d\n", reg);
Stefan Roese3e103812014-10-22 12:13:14 +02001524 return -EFAULT;
1525 }
1526
1527 /* wait till the SMI is not busy */
1528 if (smi_wait_ready(pp) < 0)
1529 return -EFAULT;
1530
1531 /* fill the phy addr and reg offset and write opcode and data */
Stefan Roese05b38c12015-11-19 07:46:15 +01001532 smi_reg = value << MVNETA_SMI_DATA_OFFS;
1533 smi_reg |= (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1534 | (reg << MVNETA_SMI_REG_ADDR_OFFS);
Stefan Roese3e103812014-10-22 12:13:14 +02001535 smi_reg &= ~MVNETA_SMI_OPCODE_READ;
1536
1537 /* write the smi register */
1538 mvreg_write(pp, MVNETA_SMI, smi_reg);
1539
1540 return 0;
1541}
1542
Stefan Roese05b38c12015-11-19 07:46:15 +01001543static int mvneta_start(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001544{
Stefan Roese05b38c12015-11-19 07:46:15 +01001545 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001546 struct phy_device *phydev;
1547
1548 mvneta_port_power_up(pp, pp->phy_interface);
1549
1550 if (!pp->init || pp->link == 0) {
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001551 if (mvneta_port_is_fixed_link(pp)) {
1552 u32 val;
Stefan Roese3e103812014-10-22 12:13:14 +02001553
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001554 pp->init = 1;
1555 pp->link = 1;
1556 mvneta_init(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001557
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001558 val = MVNETA_GMAC_FORCE_LINK_UP |
1559 MVNETA_GMAC_IB_BYPASS_AN_EN |
1560 MVNETA_GMAC_SET_FC_EN |
1561 MVNETA_GMAC_ADVERT_FC_EN |
1562 MVNETA_GMAC_SAMPLE_TX_CFG_EN;
1563
1564 if (pp->duplex)
1565 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1566
1567 if (pp->speed == SPEED_1000)
1568 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1569 else if (pp->speed == SPEED_100)
1570 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1571
1572 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1573 } else {
Marek BehĂșn99e296f2022-04-27 12:41:46 +02001574 phydev = dm_eth_phy_connect(dev);
Marek BehĂșn075ccb12018-04-24 17:21:29 +02001575 if (!phydev) {
Marek BehĂșn99e296f2022-04-27 12:41:46 +02001576 printf("dm_eth_phy_connect failed\n");
Marek BehĂșn075ccb12018-04-24 17:21:29 +02001577 return -ENODEV;
1578 }
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001579
Marek BehĂșn99e296f2022-04-27 12:41:46 +02001580 /* Set PHY address in case we will enable HW polling */
1581 mvreg_write(pp, MVNETA_PHY_ADDR, phydev->addr);
1582
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001583 pp->phydev = phydev;
1584 phy_config(phydev);
1585 phy_startup(phydev);
1586 if (!phydev->link) {
1587 printf("%s: No link.\n", phydev->dev->name);
1588 return -1;
1589 }
1590
1591 /* Full init on first call */
1592 mvneta_init(dev);
1593 pp->init = 1;
1594 return 0;
1595 }
Stefan Roese3e103812014-10-22 12:13:14 +02001596 }
1597
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001598 /* Upon all following calls, this is enough */
1599 mvneta_port_up(pp);
1600 mvneta_port_enable(pp);
1601
Stefan Roese3e103812014-10-22 12:13:14 +02001602 return 0;
1603}
1604
Stefan Roese05b38c12015-11-19 07:46:15 +01001605static int mvneta_send(struct udevice *dev, void *packet, int length)
Stefan Roese3e103812014-10-22 12:13:14 +02001606{
Stefan Roese05b38c12015-11-19 07:46:15 +01001607 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001608 struct mvneta_tx_queue *txq = &pp->txqs[0];
1609 struct mvneta_tx_desc *tx_desc;
1610 int sent_desc;
1611 u32 timeout = 0;
1612
1613 /* Get a descriptor for the first part of the packet */
1614 tx_desc = mvneta_txq_next_desc_get(txq);
1615
Stefan Roese6564d992016-05-19 18:09:17 +02001616 tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
Stefan Roese05b38c12015-11-19 07:46:15 +01001617 tx_desc->data_size = length;
Stefan Roese6564d992016-05-19 18:09:17 +02001618 flush_dcache_range((ulong)packet,
1619 (ulong)packet + ALIGN(length, PKTALIGN));
Stefan Roese3e103812014-10-22 12:13:14 +02001620
1621 /* First and Last descriptor */
1622 tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
1623 mvneta_txq_pend_desc_add(pp, txq, 1);
1624
1625 /* Wait for packet to be sent (queue might help with speed here) */
1626 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1627 while (!sent_desc) {
1628 if (timeout++ > 10000) {
1629 printf("timeout: packet not sent\n");
1630 return -1;
1631 }
1632 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1633 }
1634
1635 /* txDone has increased - hw sent packet */
1636 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
Stefan Roese3e103812014-10-22 12:13:14 +02001637
1638 return 0;
1639}
1640
Stefan Roese05b38c12015-11-19 07:46:15 +01001641static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
Stefan Roese3e103812014-10-22 12:13:14 +02001642{
Stefan Roese05b38c12015-11-19 07:46:15 +01001643 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese3e103812014-10-22 12:13:14 +02001644 int rx_done;
Stefan Roese3e103812014-10-22 12:13:14 +02001645 struct mvneta_rx_queue *rxq;
Stefan Roese05b38c12015-11-19 07:46:15 +01001646 int rx_bytes = 0;
Stefan Roese3e103812014-10-22 12:13:14 +02001647
1648 /* get rx queue */
1649 rxq = mvneta_rxq_handle_get(pp, rxq_def);
1650 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Stefan Roese3e103812014-10-22 12:13:14 +02001651
Stefan Roese05b38c12015-11-19 07:46:15 +01001652 if (rx_done) {
Stefan Roese3e103812014-10-22 12:13:14 +02001653 struct mvneta_rx_desc *rx_desc;
1654 unsigned char *data;
1655 u32 rx_status;
Stefan Roese3e103812014-10-22 12:13:14 +02001656
1657 /*
1658 * No cache invalidation needed here, since the desc's are
1659 * located in a uncached memory region
1660 */
1661 rx_desc = mvneta_rxq_next_desc_get(rxq);
1662
1663 rx_status = rx_desc->status;
1664 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1665 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1666 mvneta_rx_error(pp, rx_desc);
1667 /* leave the descriptor untouched */
Stefan Roese05b38c12015-11-19 07:46:15 +01001668 return -EIO;
Stefan Roese3e103812014-10-22 12:13:14 +02001669 }
1670
1671 /* 2 bytes for marvell header. 4 bytes for crc */
1672 rx_bytes = rx_desc->data_size - 6;
1673
1674 /* give packet to stack - skip on first 2 bytes */
Stefan Roese6564d992016-05-19 18:09:17 +02001675 data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
Stefan Roese3e103812014-10-22 12:13:14 +02001676 /*
1677 * No cache invalidation needed here, since the rx_buffer's are
1678 * located in a uncached memory region
1679 */
Stefan Roese05b38c12015-11-19 07:46:15 +01001680 *packetp = data;
Stefan Roese3e103812014-10-22 12:13:14 +02001681
Jason Brownc7bc1832017-11-28 11:12:43 -08001682 /*
1683 * Only mark one descriptor as free
1684 * since only one was processed
1685 */
1686 mvneta_rxq_desc_num_update(pp, rxq, 1, 1);
Stefan Roese05b38c12015-11-19 07:46:15 +01001687 }
Stefan Roese3e103812014-10-22 12:13:14 +02001688
Stefan Roese05b38c12015-11-19 07:46:15 +01001689 return rx_bytes;
Stefan Roese3e103812014-10-22 12:13:14 +02001690}
1691
Stefan Roese05b38c12015-11-19 07:46:15 +01001692static int mvneta_probe(struct udevice *dev)
Stefan Roese3e103812014-10-22 12:13:14 +02001693{
Stefan Roese05b38c12015-11-19 07:46:15 +01001694 struct mvneta_port *pp = dev_get_priv(dev);
Robert Marko58c98732022-03-24 10:57:37 +01001695#if CONFIG_IS_ENABLED(DM_GPIO)
1696 struct ofnode_phandle_args sfp_args;
1697#endif
Stefan Roese05b38c12015-11-19 07:46:15 +01001698 void *blob = (void *)gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -07001699 int node = dev_of_offset(dev);
Stefan Roese05b38c12015-11-19 07:46:15 +01001700 struct mii_dev *bus;
1701 unsigned long addr;
Stefan Roese3e103812014-10-22 12:13:14 +02001702 void *bd_space;
Konstantin Porotchkinfb8e2022017-02-16 13:52:27 +02001703 int ret;
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001704 int fl_node;
Stefan Roese3e103812014-10-22 12:13:14 +02001705
Stefan Roese3e103812014-10-22 12:13:14 +02001706 /*
1707 * Allocate buffer area for descs and rx_buffers. This is only
1708 * done once for all interfaces. As only one interface can
Chris Packham0f81d7a2016-08-29 20:54:02 +12001709 * be active. Make this area DMA safe by disabling the D-cache
Stefan Roese3e103812014-10-22 12:13:14 +02001710 */
1711 if (!buffer_loc.tx_descs) {
Jon Nettleton543efd12018-05-30 08:52:29 +03001712 u32 size;
1713
Stefan Roese3e103812014-10-22 12:13:14 +02001714 /* Align buffer area for descs and rx_buffers to 1MiB */
1715 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
Rabeeh Khoury31ad3ce2018-06-19 21:36:50 +03001716 flush_dcache_range((ulong)bd_space, (ulong)bd_space + BD_SPACE);
Stefan Roese6564d992016-05-19 18:09:17 +02001717 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
Stefan Roese3e103812014-10-22 12:13:14 +02001718 DCACHE_OFF);
1719 buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
Jon Nettleton543efd12018-05-30 08:52:29 +03001720 size = roundup(MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc),
1721 ARCH_DMA_MINALIGN);
Rabeeh Khouryf046bed2018-06-19 21:36:51 +03001722 memset(buffer_loc.tx_descs, 0, size);
Stefan Roese3e103812014-10-22 12:13:14 +02001723 buffer_loc.rx_descs = (struct mvneta_rx_desc *)
Jon Nettleton543efd12018-05-30 08:52:29 +03001724 ((phys_addr_t)bd_space + size);
1725 size += roundup(MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc),
1726 ARCH_DMA_MINALIGN);
1727 buffer_loc.rx_buffers = (phys_addr_t)(bd_space + size);
Stefan Roese3e103812014-10-22 12:13:14 +02001728 }
1729
Marek BehĂșn645c7442022-04-27 12:41:44 +02001730 pp->base = dev_read_addr_ptr(dev);
1731 pp->phy_interface = dev_read_phy_mode(dev);
1732 if (pp->phy_interface == PHY_INTERFACE_MODE_NA)
1733 return -EINVAL;
Stefan Roese3e103812014-10-22 12:13:14 +02001734
Stefan Roese05b38c12015-11-19 07:46:15 +01001735 /* Configure MBUS address windows */
Simon Glass54cbcc82017-05-18 20:08:57 -06001736 if (device_is_compatible(dev, "marvell,armada-3700-neta"))
Stefan Roese572be4a2016-05-19 17:46:36 +02001737 mvneta_bypass_mbus_windows(pp);
1738 else
1739 mvneta_conf_mbus_windows(pp);
Stefan Roese3e103812014-10-22 12:13:14 +02001740
Konstantin Porotchkin95d0af32017-02-16 13:52:28 +02001741 /* fetch 'fixed-link' property from 'neta' node */
1742 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1743 if (fl_node != -FDT_ERR_NOTFOUND) {
1744 /* set phy_addr to invalid value for fixed link */
1745 pp->phyaddr = PHY_MAX_ADDR + 1;
1746 pp->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1747 pp->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1748 } else {
1749 /* Now read phyaddr from DT */
1750 addr = fdtdec_get_int(blob, node, "phy", 0);
1751 addr = fdt_node_offset_by_phandle(blob, addr);
1752 pp->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1753 }
Stefan Roese3e103812014-10-22 12:13:14 +02001754
Stefan Roese05b38c12015-11-19 07:46:15 +01001755 bus = mdio_alloc();
1756 if (!bus) {
1757 printf("Failed to allocate MDIO bus\n");
1758 return -ENOMEM;
1759 }
1760
1761 bus->read = mvneta_mdio_read;
1762 bus->write = mvneta_mdio_write;
1763 snprintf(bus->name, sizeof(bus->name), dev->name);
1764 bus->priv = (void *)pp;
1765 pp->bus = bus;
1766
Konstantin Porotchkinfb8e2022017-02-16 13:52:27 +02001767 ret = mdio_register(bus);
1768 if (ret)
1769 return ret;
1770
Simon Glassfa4689a2019-12-06 21:41:35 -07001771#if CONFIG_IS_ENABLED(DM_GPIO)
Robert Marko58c98732022-03-24 10:57:37 +01001772 ret = dev_read_phandle_with_args(dev, "sfp", NULL, 0, 0, &sfp_args);
1773 if (!ret && ofnode_is_enabled(sfp_args.node))
1774 gpio_request_by_name_nodev(sfp_args.node, "tx-disable-gpio", 0,
1775 &pp->sfp_tx_disable_gpio, GPIOD_IS_OUT);
1776
Aditya Prayogac9fe02a2018-12-05 00:39:23 +08001777 gpio_request_by_name(dev, "phy-reset-gpios", 0,
1778 &pp->phy_reset_gpio, GPIOD_IS_OUT);
1779
1780 if (dm_gpio_is_valid(&pp->phy_reset_gpio)) {
1781 dm_gpio_set_value(&pp->phy_reset_gpio, 1);
1782 mdelay(10);
1783 dm_gpio_set_value(&pp->phy_reset_gpio, 0);
1784 }
Robert Marko58c98732022-03-24 10:57:37 +01001785
1786 if (dm_gpio_is_valid(&pp->sfp_tx_disable_gpio))
1787 dm_gpio_set_value(&pp->sfp_tx_disable_gpio, 0);
Aditya Prayogac9fe02a2018-12-05 00:39:23 +08001788#endif
1789
Konstantin Porotchkinfb8e2022017-02-16 13:52:27 +02001790 return board_network_enable(bus);
Stefan Roese05b38c12015-11-19 07:46:15 +01001791}
1792
1793static void mvneta_stop(struct udevice *dev)
1794{
1795 struct mvneta_port *pp = dev_get_priv(dev);
1796
1797 mvneta_port_down(pp);
1798 mvneta_port_disable(pp);
1799}
1800
1801static const struct eth_ops mvneta_ops = {
1802 .start = mvneta_start,
1803 .send = mvneta_send,
1804 .recv = mvneta_recv,
1805 .stop = mvneta_stop,
Matt Pelland668a5f22018-03-27 13:18:25 -04001806 .write_hwaddr = mvneta_write_hwaddr,
Stefan Roese05b38c12015-11-19 07:46:15 +01001807};
1808
Stefan Roese05b38c12015-11-19 07:46:15 +01001809static const struct udevice_id mvneta_ids[] = {
1810 { .compatible = "marvell,armada-370-neta" },
1811 { .compatible = "marvell,armada-xp-neta" },
Stefan Roese572be4a2016-05-19 17:46:36 +02001812 { .compatible = "marvell,armada-3700-neta" },
Stefan Roese05b38c12015-11-19 07:46:15 +01001813 { }
1814};
1815
1816U_BOOT_DRIVER(mvneta) = {
1817 .name = "mvneta",
1818 .id = UCLASS_ETH,
1819 .of_match = mvneta_ids,
Stefan Roese05b38c12015-11-19 07:46:15 +01001820 .probe = mvneta_probe,
1821 .ops = &mvneta_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -07001822 .priv_auto = sizeof(struct mvneta_port),
Simon Glass71fa5b42020-12-03 16:55:18 -07001823 .plat_auto = sizeof(struct eth_pdata),
Stefan Roese05b38c12015-11-19 07:46:15 +01001824};