blob: d737400a20d93f6dc65865c608ad300cebd6d969 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
Stefan Roese19fc2ea2014-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 Roesee3b9c982015-11-19 07:46:15 +01006 * Copyright (C) 2014-2015 Stefan Roese <sr@denx.de>
Stefan Roese19fc2ea2014-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 Roese19fc2ea2014-10-22 12:13:14 +020013 */
14
15#include <common.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -070016#include <cpu_func.h>
Stefan Roesee3b9c982015-11-19 07:46:15 +010017#include <dm.h>
Stefan Roese19fc2ea2014-10-22 12:13:14 +020018#include <net.h>
19#include <netdev.h>
20#include <config.h>
21#include <malloc.h>
22#include <asm/io.h>
Simon Glass336d4612020-02-03 07:36:16 -070023#include <dm/device_compat.h>
Simon Glass61b29b82020-02-03 07:36:15 -070024#include <dm/devres.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090025#include <linux/errno.h>
Stefan Roese19fc2ea2014-10-22 12:13:14 +020026#include <phy.h>
27#include <miiphy.h>
28#include <watchdog.h>
29#include <asm/arch/cpu.h>
30#include <asm/arch/soc.h>
31#include <linux/compat.h>
32#include <linux/mbus.h>
Aditya Prayoga18bfc8f2018-12-05 00:39:23 +080033#include <asm-generic/gpio.h>
Stefan Roese19fc2ea2014-10-22 12:13:14 +020034
Stefan Roesee3b9c982015-11-19 07:46:15 +010035DECLARE_GLOBAL_DATA_PTR;
36
Stefan Roese19fc2ea2014-10-22 12:13:14 +020037#if !defined(CONFIG_PHYLIB)
38# error Marvell mvneta requires PHYLIB
39#endif
40
Stefan Roese19fc2ea2014-10-22 12:13:14 +020041#define CONFIG_NR_CPUS 1
Stefan Roese19fc2ea2014-10-22 12:13:14 +020042#define ETH_HLEN 14 /* Total octets in header */
43
44/* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
45#define WRAP (2 + ETH_HLEN + 4 + 32)
46#define MTU 1500
47#define RX_BUFFER_SIZE (ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
48
49#define MVNETA_SMI_TIMEOUT 10000
50
51/* Registers */
52#define MVNETA_RXQ_CONFIG_REG(q) (0x1400 + ((q) << 2))
53#define MVNETA_RXQ_HW_BUF_ALLOC BIT(1)
54#define MVNETA_RXQ_PKT_OFFSET_ALL_MASK (0xf << 8)
55#define MVNETA_RXQ_PKT_OFFSET_MASK(offs) ((offs) << 8)
56#define MVNETA_RXQ_THRESHOLD_REG(q) (0x14c0 + ((q) << 2))
57#define MVNETA_RXQ_NON_OCCUPIED(v) ((v) << 16)
58#define MVNETA_RXQ_BASE_ADDR_REG(q) (0x1480 + ((q) << 2))
59#define MVNETA_RXQ_SIZE_REG(q) (0x14a0 + ((q) << 2))
60#define MVNETA_RXQ_BUF_SIZE_SHIFT 19
61#define MVNETA_RXQ_BUF_SIZE_MASK (0x1fff << 19)
62#define MVNETA_RXQ_STATUS_REG(q) (0x14e0 + ((q) << 2))
63#define MVNETA_RXQ_OCCUPIED_ALL_MASK 0x3fff
64#define MVNETA_RXQ_STATUS_UPDATE_REG(q) (0x1500 + ((q) << 2))
65#define MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT 16
66#define MVNETA_RXQ_ADD_NON_OCCUPIED_MAX 255
67#define MVNETA_PORT_RX_RESET 0x1cc0
68#define MVNETA_PORT_RX_DMA_RESET BIT(0)
69#define MVNETA_PHY_ADDR 0x2000
70#define MVNETA_PHY_ADDR_MASK 0x1f
71#define MVNETA_SMI 0x2004
72#define MVNETA_PHY_REG_MASK 0x1f
73/* SMI register fields */
74#define MVNETA_SMI_DATA_OFFS 0 /* Data */
75#define MVNETA_SMI_DATA_MASK (0xffff << MVNETA_SMI_DATA_OFFS)
76#define MVNETA_SMI_DEV_ADDR_OFFS 16 /* PHY device address */
77#define MVNETA_SMI_REG_ADDR_OFFS 21 /* PHY device reg addr*/
78#define MVNETA_SMI_OPCODE_OFFS 26 /* Write/Read opcode */
79#define MVNETA_SMI_OPCODE_READ (1 << MVNETA_SMI_OPCODE_OFFS)
80#define MVNETA_SMI_READ_VALID (1 << 27) /* Read Valid */
81#define MVNETA_SMI_BUSY (1 << 28) /* Busy */
82#define MVNETA_MBUS_RETRY 0x2010
83#define MVNETA_UNIT_INTR_CAUSE 0x2080
84#define MVNETA_UNIT_CONTROL 0x20B0
85#define MVNETA_PHY_POLLING_ENABLE BIT(1)
86#define MVNETA_WIN_BASE(w) (0x2200 + ((w) << 3))
87#define MVNETA_WIN_SIZE(w) (0x2204 + ((w) << 3))
88#define MVNETA_WIN_REMAP(w) (0x2280 + ((w) << 2))
Stefan Roese544eefe2016-05-19 17:46:36 +020089#define MVNETA_WIN_SIZE_MASK (0xffff0000)
Stefan Roese19fc2ea2014-10-22 12:13:14 +020090#define MVNETA_BASE_ADDR_ENABLE 0x2290
Stefan Roese544eefe2016-05-19 17:46:36 +020091#define MVNETA_BASE_ADDR_ENABLE_BIT 0x1
92#define MVNETA_PORT_ACCESS_PROTECT 0x2294
93#define MVNETA_PORT_ACCESS_PROTECT_WIN0_RW 0x3
Stefan Roese19fc2ea2014-10-22 12:13:14 +020094#define MVNETA_PORT_CONFIG 0x2400
95#define MVNETA_UNI_PROMISC_MODE BIT(0)
96#define MVNETA_DEF_RXQ(q) ((q) << 1)
97#define MVNETA_DEF_RXQ_ARP(q) ((q) << 4)
98#define MVNETA_TX_UNSET_ERR_SUM BIT(12)
99#define MVNETA_DEF_RXQ_TCP(q) ((q) << 16)
100#define MVNETA_DEF_RXQ_UDP(q) ((q) << 19)
101#define MVNETA_DEF_RXQ_BPDU(q) ((q) << 22)
102#define MVNETA_RX_CSUM_WITH_PSEUDO_HDR BIT(25)
103#define MVNETA_PORT_CONFIG_DEFL_VALUE(q) (MVNETA_DEF_RXQ(q) | \
104 MVNETA_DEF_RXQ_ARP(q) | \
105 MVNETA_DEF_RXQ_TCP(q) | \
106 MVNETA_DEF_RXQ_UDP(q) | \
107 MVNETA_DEF_RXQ_BPDU(q) | \
108 MVNETA_TX_UNSET_ERR_SUM | \
109 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
110#define MVNETA_PORT_CONFIG_EXTEND 0x2404
111#define MVNETA_MAC_ADDR_LOW 0x2414
112#define MVNETA_MAC_ADDR_HIGH 0x2418
113#define MVNETA_SDMA_CONFIG 0x241c
114#define MVNETA_SDMA_BRST_SIZE_16 4
115#define MVNETA_RX_BRST_SZ_MASK(burst) ((burst) << 1)
116#define MVNETA_RX_NO_DATA_SWAP BIT(4)
117#define MVNETA_TX_NO_DATA_SWAP BIT(5)
118#define MVNETA_DESC_SWAP BIT(6)
119#define MVNETA_TX_BRST_SZ_MASK(burst) ((burst) << 22)
120#define MVNETA_PORT_STATUS 0x2444
121#define MVNETA_TX_IN_PRGRS BIT(1)
122#define MVNETA_TX_FIFO_EMPTY BIT(8)
123#define MVNETA_RX_MIN_FRAME_SIZE 0x247c
124#define MVNETA_SERDES_CFG 0x24A0
125#define MVNETA_SGMII_SERDES_PROTO 0x0cc7
126#define MVNETA_QSGMII_SERDES_PROTO 0x0667
127#define MVNETA_TYPE_PRIO 0x24bc
128#define MVNETA_FORCE_UNI BIT(21)
129#define MVNETA_TXQ_CMD_1 0x24e4
130#define MVNETA_TXQ_CMD 0x2448
131#define MVNETA_TXQ_DISABLE_SHIFT 8
132#define MVNETA_TXQ_ENABLE_MASK 0x000000ff
133#define MVNETA_ACC_MODE 0x2500
134#define MVNETA_CPU_MAP(cpu) (0x2540 + ((cpu) << 2))
135#define MVNETA_CPU_RXQ_ACCESS_ALL_MASK 0x000000ff
136#define MVNETA_CPU_TXQ_ACCESS_ALL_MASK 0x0000ff00
137#define MVNETA_RXQ_TIME_COAL_REG(q) (0x2580 + ((q) << 2))
138
139/* Exception Interrupt Port/Queue Cause register */
140
141#define MVNETA_INTR_NEW_CAUSE 0x25a0
142#define MVNETA_INTR_NEW_MASK 0x25a4
143
144/* bits 0..7 = TXQ SENT, one bit per queue.
145 * bits 8..15 = RXQ OCCUP, one bit per queue.
146 * bits 16..23 = RXQ FREE, one bit per queue.
147 * bit 29 = OLD_REG_SUM, see old reg ?
148 * bit 30 = TX_ERR_SUM, one bit for 4 ports
149 * bit 31 = MISC_SUM, one bit for 4 ports
150 */
151#define MVNETA_TX_INTR_MASK(nr_txqs) (((1 << nr_txqs) - 1) << 0)
152#define MVNETA_TX_INTR_MASK_ALL (0xff << 0)
153#define MVNETA_RX_INTR_MASK(nr_rxqs) (((1 << nr_rxqs) - 1) << 8)
154#define MVNETA_RX_INTR_MASK_ALL (0xff << 8)
155
156#define MVNETA_INTR_OLD_CAUSE 0x25a8
157#define MVNETA_INTR_OLD_MASK 0x25ac
158
159/* Data Path Port/Queue Cause Register */
160#define MVNETA_INTR_MISC_CAUSE 0x25b0
161#define MVNETA_INTR_MISC_MASK 0x25b4
162#define MVNETA_INTR_ENABLE 0x25b8
163
164#define MVNETA_RXQ_CMD 0x2680
165#define MVNETA_RXQ_DISABLE_SHIFT 8
166#define MVNETA_RXQ_ENABLE_MASK 0x000000ff
167#define MVETH_TXQ_TOKEN_COUNT_REG(q) (0x2700 + ((q) << 4))
168#define MVETH_TXQ_TOKEN_CFG_REG(q) (0x2704 + ((q) << 4))
169#define MVNETA_GMAC_CTRL_0 0x2c00
170#define MVNETA_GMAC_MAX_RX_SIZE_SHIFT 2
171#define MVNETA_GMAC_MAX_RX_SIZE_MASK 0x7ffc
172#define MVNETA_GMAC0_PORT_ENABLE BIT(0)
173#define MVNETA_GMAC_CTRL_2 0x2c08
174#define MVNETA_GMAC2_PCS_ENABLE BIT(3)
175#define MVNETA_GMAC2_PORT_RGMII BIT(4)
176#define MVNETA_GMAC2_PORT_RESET BIT(6)
177#define MVNETA_GMAC_STATUS 0x2c10
178#define MVNETA_GMAC_LINK_UP BIT(0)
179#define MVNETA_GMAC_SPEED_1000 BIT(1)
180#define MVNETA_GMAC_SPEED_100 BIT(2)
181#define MVNETA_GMAC_FULL_DUPLEX BIT(3)
182#define MVNETA_GMAC_RX_FLOW_CTRL_ENABLE BIT(4)
183#define MVNETA_GMAC_TX_FLOW_CTRL_ENABLE BIT(5)
184#define MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE BIT(6)
185#define MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE BIT(7)
186#define MVNETA_GMAC_AUTONEG_CONFIG 0x2c0c
187#define MVNETA_GMAC_FORCE_LINK_DOWN BIT(0)
188#define MVNETA_GMAC_FORCE_LINK_PASS BIT(1)
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +0200189#define MVNETA_GMAC_FORCE_LINK_UP (BIT(0) | BIT(1))
190#define MVNETA_GMAC_IB_BYPASS_AN_EN BIT(3)
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200191#define MVNETA_GMAC_CONFIG_MII_SPEED BIT(5)
192#define MVNETA_GMAC_CONFIG_GMII_SPEED BIT(6)
193#define MVNETA_GMAC_AN_SPEED_EN BIT(7)
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +0200194#define MVNETA_GMAC_SET_FC_EN BIT(8)
195#define MVNETA_GMAC_ADVERT_FC_EN BIT(9)
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200196#define MVNETA_GMAC_CONFIG_FULL_DUPLEX BIT(12)
197#define MVNETA_GMAC_AN_DUPLEX_EN BIT(13)
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +0200198#define MVNETA_GMAC_SAMPLE_TX_CFG_EN BIT(15)
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200199#define MVNETA_MIB_COUNTERS_BASE 0x3080
200#define MVNETA_MIB_LATE_COLLISION 0x7c
201#define MVNETA_DA_FILT_SPEC_MCAST 0x3400
202#define MVNETA_DA_FILT_OTH_MCAST 0x3500
203#define MVNETA_DA_FILT_UCAST_BASE 0x3600
204#define MVNETA_TXQ_BASE_ADDR_REG(q) (0x3c00 + ((q) << 2))
205#define MVNETA_TXQ_SIZE_REG(q) (0x3c20 + ((q) << 2))
206#define MVNETA_TXQ_SENT_THRESH_ALL_MASK 0x3fff0000
207#define MVNETA_TXQ_SENT_THRESH_MASK(coal) ((coal) << 16)
208#define MVNETA_TXQ_UPDATE_REG(q) (0x3c60 + ((q) << 2))
209#define MVNETA_TXQ_DEC_SENT_SHIFT 16
210#define MVNETA_TXQ_STATUS_REG(q) (0x3c40 + ((q) << 2))
211#define MVNETA_TXQ_SENT_DESC_SHIFT 16
212#define MVNETA_TXQ_SENT_DESC_MASK 0x3fff0000
213#define MVNETA_PORT_TX_RESET 0x3cf0
214#define MVNETA_PORT_TX_DMA_RESET BIT(0)
215#define MVNETA_TX_MTU 0x3e0c
216#define MVNETA_TX_TOKEN_SIZE 0x3e14
217#define MVNETA_TX_TOKEN_SIZE_MAX 0xffffffff
218#define MVNETA_TXQ_TOKEN_SIZE_REG(q) (0x3e40 + ((q) << 2))
219#define MVNETA_TXQ_TOKEN_SIZE_MAX 0x7fffffff
220
221/* Descriptor ring Macros */
222#define MVNETA_QUEUE_NEXT_DESC(q, index) \
223 (((index) < (q)->last_desc) ? ((index) + 1) : 0)
224
225/* Various constants */
226
227/* Coalescing */
228#define MVNETA_TXDONE_COAL_PKTS 16
229#define MVNETA_RX_COAL_PKTS 32
230#define MVNETA_RX_COAL_USEC 100
231
232/* The two bytes Marvell header. Either contains a special value used
233 * by Marvell switches when a specific hardware mode is enabled (not
234 * supported by this driver) or is filled automatically by zeroes on
235 * the RX side. Those two bytes being at the front of the Ethernet
236 * header, they allow to have the IP header aligned on a 4 bytes
237 * boundary automatically: the hardware skips those two bytes on its
238 * own.
239 */
240#define MVNETA_MH_SIZE 2
241
242#define MVNETA_VLAN_TAG_LEN 4
243
244#define MVNETA_CPU_D_CACHE_LINE_SIZE 32
245#define MVNETA_TX_CSUM_MAX_SIZE 9800
246#define MVNETA_ACC_MODE_EXT 1
247
248/* Timeout constants */
249#define MVNETA_TX_DISABLE_TIMEOUT_MSEC 1000
250#define MVNETA_RX_DISABLE_TIMEOUT_MSEC 1000
251#define MVNETA_TX_FIFO_EMPTY_TIMEOUT 10000
252
253#define MVNETA_TX_MTU_MAX 0x3ffff
254
255/* Max number of Rx descriptors */
256#define MVNETA_MAX_RXD 16
257
258/* Max number of Tx descriptors */
259#define MVNETA_MAX_TXD 16
260
261/* descriptor aligned size */
262#define MVNETA_DESC_ALIGNED_SIZE 32
263
264struct mvneta_port {
265 void __iomem *base;
266 struct mvneta_rx_queue *rxqs;
267 struct mvneta_tx_queue *txqs;
268
269 u8 mcast_count[256];
270 u16 tx_ring_size;
271 u16 rx_ring_size;
272
273 phy_interface_t phy_interface;
274 unsigned int link;
275 unsigned int duplex;
276 unsigned int speed;
277
278 int init;
279 int phyaddr;
280 struct phy_device *phydev;
Simon Glassbcee8d62019-12-06 21:41:35 -0700281#if CONFIG_IS_ENABLED(DM_GPIO)
Aditya Prayoga18bfc8f2018-12-05 00:39:23 +0800282 struct gpio_desc phy_reset_gpio;
283#endif
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200284 struct mii_dev *bus;
285};
286
287/* The mvneta_tx_desc and mvneta_rx_desc structures describe the
288 * layout of the transmit and reception DMA descriptors, and their
289 * layout is therefore defined by the hardware design
290 */
291
292#define MVNETA_TX_L3_OFF_SHIFT 0
293#define MVNETA_TX_IP_HLEN_SHIFT 8
294#define MVNETA_TX_L4_UDP BIT(16)
295#define MVNETA_TX_L3_IP6 BIT(17)
296#define MVNETA_TXD_IP_CSUM BIT(18)
297#define MVNETA_TXD_Z_PAD BIT(19)
298#define MVNETA_TXD_L_DESC BIT(20)
299#define MVNETA_TXD_F_DESC BIT(21)
300#define MVNETA_TXD_FLZ_DESC (MVNETA_TXD_Z_PAD | \
301 MVNETA_TXD_L_DESC | \
302 MVNETA_TXD_F_DESC)
303#define MVNETA_TX_L4_CSUM_FULL BIT(30)
304#define MVNETA_TX_L4_CSUM_NOT BIT(31)
305
306#define MVNETA_RXD_ERR_CRC 0x0
307#define MVNETA_RXD_ERR_SUMMARY BIT(16)
308#define MVNETA_RXD_ERR_OVERRUN BIT(17)
309#define MVNETA_RXD_ERR_LEN BIT(18)
310#define MVNETA_RXD_ERR_RESOURCE (BIT(17) | BIT(18))
311#define MVNETA_RXD_ERR_CODE_MASK (BIT(17) | BIT(18))
312#define MVNETA_RXD_L3_IP4 BIT(25)
313#define MVNETA_RXD_FIRST_LAST_DESC (BIT(26) | BIT(27))
314#define MVNETA_RXD_L4_CSUM_OK BIT(30)
315
316struct mvneta_tx_desc {
317 u32 command; /* Options used by HW for packet transmitting.*/
318 u16 reserverd1; /* csum_l4 (for future use) */
319 u16 data_size; /* Data size of transmitted packet in bytes */
320 u32 buf_phys_addr; /* Physical addr of transmitted buffer */
321 u32 reserved2; /* hw_cmd - (for future use, PMT) */
322 u32 reserved3[4]; /* Reserved - (for future use) */
323};
324
325struct mvneta_rx_desc {
326 u32 status; /* Info about received packet */
327 u16 reserved1; /* pnc_info - (for future use, PnC) */
328 u16 data_size; /* Size of received packet in bytes */
329
330 u32 buf_phys_addr; /* Physical address of the buffer */
331 u32 reserved2; /* pnc_flow_id (for future use, PnC) */
332
333 u32 buf_cookie; /* cookie for access to RX buffer in rx path */
334 u16 reserved3; /* prefetch_cmd, for future use */
335 u16 reserved4; /* csum_l4 - (for future use, PnC) */
336
337 u32 reserved5; /* pnc_extra PnC (for future use, PnC) */
338 u32 reserved6; /* hw_cmd (for future use, PnC and HWF) */
339};
340
341struct mvneta_tx_queue {
342 /* Number of this TX queue, in the range 0-7 */
343 u8 id;
344
345 /* Number of TX DMA descriptors in the descriptor ring */
346 int size;
347
348 /* Index of last TX DMA descriptor that was inserted */
349 int txq_put_index;
350
351 /* Index of the TX DMA descriptor to be cleaned up */
352 int txq_get_index;
353
354 /* Virtual address of the TX DMA descriptors array */
355 struct mvneta_tx_desc *descs;
356
357 /* DMA address of the TX DMA descriptors array */
358 dma_addr_t descs_phys;
359
360 /* Index of the last TX DMA descriptor */
361 int last_desc;
362
363 /* Index of the next TX DMA descriptor to process */
364 int next_desc_to_proc;
365};
366
367struct mvneta_rx_queue {
368 /* rx queue number, in the range 0-7 */
369 u8 id;
370
371 /* num of rx descriptors in the rx descriptor ring */
372 int size;
373
374 /* Virtual address of the RX DMA descriptors array */
375 struct mvneta_rx_desc *descs;
376
377 /* DMA address of the RX DMA descriptors array */
378 dma_addr_t descs_phys;
379
380 /* Index of the last RX DMA descriptor */
381 int last_desc;
382
383 /* Index of the next RX DMA descriptor to process */
384 int next_desc_to_proc;
385};
386
387/* U-Boot doesn't use the queues, so set the number to 1 */
388static int rxq_number = 1;
389static int txq_number = 1;
390static int rxq_def;
391
392struct buffer_location {
393 struct mvneta_tx_desc *tx_descs;
394 struct mvneta_rx_desc *rx_descs;
395 u32 rx_buffers;
396};
397
398/*
399 * All 4 interfaces use the same global buffer, since only one interface
400 * can be enabled at once
401 */
402static struct buffer_location buffer_loc;
403
404/*
405 * Page table entries are set to 1MB, or multiples of 1MB
406 * (not < 1MB). driver uses less bd's so use 1MB bdspace.
407 */
408#define BD_SPACE (1 << 20)
409
Konstantin Porotchkin976feda2017-02-16 13:52:27 +0200410/*
411 * Dummy implementation that can be overwritten by a board
412 * specific function
413 */
414__weak int board_network_enable(struct mii_dev *bus)
415{
416 return 0;
417}
418
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200419/* Utility/helper methods */
420
421/* Write helper method */
422static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
423{
424 writel(data, pp->base + offset);
425}
426
427/* Read helper method */
428static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
429{
430 return readl(pp->base + offset);
431}
432
433/* Clear all MIB counters */
434static void mvneta_mib_counters_clear(struct mvneta_port *pp)
435{
436 int i;
437
438 /* Perform dummy reads from MIB counters */
439 for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
440 mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
441}
442
443/* Rx descriptors helper methods */
444
445/* Checks whether the RX descriptor having this status is both the first
446 * and the last descriptor for the RX packet. Each RX packet is currently
447 * received through a single RX descriptor, so not having each RX
448 * descriptor with its first and last bits set is an error
449 */
450static int mvneta_rxq_desc_is_first_last(u32 status)
451{
452 return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
453 MVNETA_RXD_FIRST_LAST_DESC;
454}
455
456/* Add number of descriptors ready to receive new packets */
457static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
458 struct mvneta_rx_queue *rxq,
459 int ndescs)
460{
461 /* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
462 * be added at once
463 */
464 while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
465 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
466 (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
467 MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
468 ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
469 }
470
471 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
472 (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
473}
474
475/* Get number of RX descriptors occupied by received packets */
476static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
477 struct mvneta_rx_queue *rxq)
478{
479 u32 val;
480
481 val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
482 return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
483}
484
485/* Update num of rx desc called upon return from rx path or
486 * from mvneta_rxq_drop_pkts().
487 */
488static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
489 struct mvneta_rx_queue *rxq,
490 int rx_done, int rx_filled)
491{
492 u32 val;
493
494 if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
495 val = rx_done |
496 (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
497 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
498 return;
499 }
500
501 /* Only 255 descriptors can be added at once */
502 while ((rx_done > 0) || (rx_filled > 0)) {
503 if (rx_done <= 0xff) {
504 val = rx_done;
505 rx_done = 0;
506 } else {
507 val = 0xff;
508 rx_done -= 0xff;
509 }
510 if (rx_filled <= 0xff) {
511 val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
512 rx_filled = 0;
513 } else {
514 val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
515 rx_filled -= 0xff;
516 }
517 mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
518 }
519}
520
521/* Get pointer to next RX descriptor to be processed by SW */
522static struct mvneta_rx_desc *
523mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
524{
525 int rx_desc = rxq->next_desc_to_proc;
526
527 rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
528 return rxq->descs + rx_desc;
529}
530
531/* Tx descriptors helper methods */
532
533/* Update HW with number of TX descriptors to be sent */
534static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
535 struct mvneta_tx_queue *txq,
536 int pend_desc)
537{
538 u32 val;
539
540 /* Only 255 descriptors can be added at once ; Assume caller
Heinrich Schuchardte4691562017-08-29 18:44:37 +0200541 * process TX descriptors in quanta less than 256
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200542 */
543 val = pend_desc;
544 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
545}
546
547/* Get pointer to next TX descriptor to be processed (send) by HW */
548static struct mvneta_tx_desc *
549mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
550{
551 int tx_desc = txq->next_desc_to_proc;
552
553 txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
554 return txq->descs + tx_desc;
555}
556
557/* Set rxq buf size */
558static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
559 struct mvneta_rx_queue *rxq,
560 int buf_size)
561{
562 u32 val;
563
564 val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
565
566 val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
567 val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
568
569 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
570}
571
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +0200572static int mvneta_port_is_fixed_link(struct mvneta_port *pp)
573{
574 /* phy_addr is set to invalid value for fixed link */
575 return pp->phyaddr > PHY_MAX_ADDR;
576}
577
578
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200579/* Start the Ethernet port RX and TX activity */
580static void mvneta_port_up(struct mvneta_port *pp)
581{
582 int queue;
583 u32 q_map;
584
585 /* Enable all initialized TXs. */
586 mvneta_mib_counters_clear(pp);
587 q_map = 0;
588 for (queue = 0; queue < txq_number; queue++) {
589 struct mvneta_tx_queue *txq = &pp->txqs[queue];
590 if (txq->descs != NULL)
591 q_map |= (1 << queue);
592 }
593 mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
594
595 /* Enable all initialized RXQs. */
596 q_map = 0;
597 for (queue = 0; queue < rxq_number; queue++) {
598 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
599 if (rxq->descs != NULL)
600 q_map |= (1 << queue);
601 }
602 mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
603}
604
605/* Stop the Ethernet port activity */
606static void mvneta_port_down(struct mvneta_port *pp)
607{
608 u32 val;
609 int count;
610
611 /* Stop Rx port activity. Check port Rx activity. */
612 val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
613
614 /* Issue stop command for active channels only */
615 if (val != 0)
616 mvreg_write(pp, MVNETA_RXQ_CMD,
617 val << MVNETA_RXQ_DISABLE_SHIFT);
618
619 /* Wait for all Rx activity to terminate. */
620 count = 0;
621 do {
622 if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
623 netdev_warn(pp->dev,
624 "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
625 val);
626 break;
627 }
628 mdelay(1);
629
630 val = mvreg_read(pp, MVNETA_RXQ_CMD);
631 } while (val & 0xff);
632
633 /* Stop Tx port activity. Check port Tx activity. Issue stop
634 * command for active channels only
635 */
636 val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
637
638 if (val != 0)
639 mvreg_write(pp, MVNETA_TXQ_CMD,
640 (val << MVNETA_TXQ_DISABLE_SHIFT));
641
642 /* Wait for all Tx activity to terminate. */
643 count = 0;
644 do {
645 if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
646 netdev_warn(pp->dev,
647 "TIMEOUT for TX stopped status=0x%08x\n",
648 val);
649 break;
650 }
651 mdelay(1);
652
653 /* Check TX Command reg that all Txqs are stopped */
654 val = mvreg_read(pp, MVNETA_TXQ_CMD);
655
656 } while (val & 0xff);
657
658 /* Double check to verify that TX FIFO is empty */
659 count = 0;
660 do {
661 if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
662 netdev_warn(pp->dev,
663 "TX FIFO empty timeout status=0x08%x\n",
664 val);
665 break;
666 }
667 mdelay(1);
668
669 val = mvreg_read(pp, MVNETA_PORT_STATUS);
670 } while (!(val & MVNETA_TX_FIFO_EMPTY) &&
671 (val & MVNETA_TX_IN_PRGRS));
672
673 udelay(200);
674}
675
676/* Enable the port by setting the port enable bit of the MAC control register */
677static void mvneta_port_enable(struct mvneta_port *pp)
678{
679 u32 val;
680
681 /* Enable port */
682 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
683 val |= MVNETA_GMAC0_PORT_ENABLE;
684 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
685}
686
687/* Disable the port and wait for about 200 usec before retuning */
688static void mvneta_port_disable(struct mvneta_port *pp)
689{
690 u32 val;
691
692 /* Reset the Enable bit in the Serial Control Register */
693 val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
694 val &= ~MVNETA_GMAC0_PORT_ENABLE;
695 mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
696
697 udelay(200);
698}
699
700/* Multicast tables methods */
701
702/* Set all entries in Unicast MAC Table; queue==-1 means reject all */
703static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
704{
705 int offset;
706 u32 val;
707
708 if (queue == -1) {
709 val = 0;
710 } else {
711 val = 0x1 | (queue << 1);
712 val |= (val << 24) | (val << 16) | (val << 8);
713 }
714
715 for (offset = 0; offset <= 0xc; offset += 4)
716 mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
717}
718
719/* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
720static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
721{
722 int offset;
723 u32 val;
724
725 if (queue == -1) {
726 val = 0;
727 } else {
728 val = 0x1 | (queue << 1);
729 val |= (val << 24) | (val << 16) | (val << 8);
730 }
731
732 for (offset = 0; offset <= 0xfc; offset += 4)
733 mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
734}
735
736/* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
737static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
738{
739 int offset;
740 u32 val;
741
742 if (queue == -1) {
743 memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
744 val = 0;
745 } else {
746 memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
747 val = 0x1 | (queue << 1);
748 val |= (val << 24) | (val << 16) | (val << 8);
749 }
750
751 for (offset = 0; offset <= 0xfc; offset += 4)
752 mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
753}
754
755/* This method sets defaults to the NETA port:
756 * Clears interrupt Cause and Mask registers.
757 * Clears all MAC tables.
758 * Sets defaults to all registers.
759 * Resets RX and TX descriptor rings.
760 * Resets PHY.
761 * This method can be called after mvneta_port_down() to return the port
762 * settings to defaults.
763 */
764static void mvneta_defaults_set(struct mvneta_port *pp)
765{
766 int cpu;
767 int queue;
768 u32 val;
769
770 /* Clear all Cause registers */
771 mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
772 mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
773 mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
774
775 /* Mask all interrupts */
776 mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
777 mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
778 mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
779 mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
780
781 /* Enable MBUS Retry bit16 */
782 mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
783
784 /* Set CPU queue access map - all CPUs have access to all RX
785 * queues and to all TX queues
786 */
787 for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
788 mvreg_write(pp, MVNETA_CPU_MAP(cpu),
789 (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
790 MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
791
792 /* Reset RX and TX DMAs */
793 mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
794 mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
795
796 /* Disable Legacy WRR, Disable EJP, Release from reset */
797 mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
798 for (queue = 0; queue < txq_number; queue++) {
799 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
800 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
801 }
802
803 mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
804 mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
805
806 /* Set Port Acceleration Mode */
807 val = MVNETA_ACC_MODE_EXT;
808 mvreg_write(pp, MVNETA_ACC_MODE, val);
809
810 /* Update val of portCfg register accordingly with all RxQueue types */
811 val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
812 mvreg_write(pp, MVNETA_PORT_CONFIG, val);
813
814 val = 0;
815 mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
816 mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
817
818 /* Build PORT_SDMA_CONFIG_REG */
819 val = 0;
820
821 /* Default burst size */
822 val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
823 val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
824 val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
825
826 /* Assign port SDMA configuration */
827 mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
828
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +0200829 /* Enable PHY polling in hardware if not in fixed-link mode */
830 if (!mvneta_port_is_fixed_link(pp)) {
831 val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
832 val |= MVNETA_PHY_POLLING_ENABLE;
833 mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
834 }
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200835
836 mvneta_set_ucast_table(pp, -1);
837 mvneta_set_special_mcast_table(pp, -1);
838 mvneta_set_other_mcast_table(pp, -1);
839}
840
841/* Set unicast address */
842static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
843 int queue)
844{
845 unsigned int unicast_reg;
846 unsigned int tbl_offset;
847 unsigned int reg_offset;
848
849 /* Locate the Unicast table entry */
850 last_nibble = (0xf & last_nibble);
851
852 /* offset from unicast tbl base */
853 tbl_offset = (last_nibble / 4) * 4;
854
855 /* offset within the above reg */
856 reg_offset = last_nibble % 4;
857
858 unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
859
860 if (queue == -1) {
861 /* Clear accepts frame bit at specified unicast DA tbl entry */
862 unicast_reg &= ~(0xff << (8 * reg_offset));
863 } else {
864 unicast_reg &= ~(0xff << (8 * reg_offset));
865 unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
866 }
867
868 mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
869}
870
871/* Set mac address */
872static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
873 int queue)
874{
875 unsigned int mac_h;
876 unsigned int mac_l;
877
878 if (queue != -1) {
879 mac_l = (addr[4] << 8) | (addr[5]);
880 mac_h = (addr[0] << 24) | (addr[1] << 16) |
881 (addr[2] << 8) | (addr[3] << 0);
882
883 mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
884 mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
885 }
886
887 /* Accept frames of this address */
888 mvneta_set_ucast_addr(pp, addr[5], queue);
889}
890
Matt Pelland0a85f022018-03-27 13:18:25 -0400891static int mvneta_write_hwaddr(struct udevice *dev)
892{
893 mvneta_mac_addr_set(dev_get_priv(dev),
894 ((struct eth_pdata *)dev_get_platdata(dev))->enetaddr,
895 rxq_def);
896
897 return 0;
898}
899
Stefan Roese19fc2ea2014-10-22 12:13:14 +0200900/* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
901static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
902 u32 phys_addr, u32 cookie)
903{
904 rx_desc->buf_cookie = cookie;
905 rx_desc->buf_phys_addr = phys_addr;
906}
907
908/* Decrement sent descriptors counter */
909static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
910 struct mvneta_tx_queue *txq,
911 int sent_desc)
912{
913 u32 val;
914
915 /* Only 255 TX descriptors can be updated at once */
916 while (sent_desc > 0xff) {
917 val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
918 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
919 sent_desc = sent_desc - 0xff;
920 }
921
922 val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
923 mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
924}
925
926/* Get number of TX descriptors already sent by HW */
927static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
928 struct mvneta_tx_queue *txq)
929{
930 u32 val;
931 int sent_desc;
932
933 val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
934 sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
935 MVNETA_TXQ_SENT_DESC_SHIFT;
936
937 return sent_desc;
938}
939
940/* Display more error info */
941static void mvneta_rx_error(struct mvneta_port *pp,
942 struct mvneta_rx_desc *rx_desc)
943{
944 u32 status = rx_desc->status;
945
946 if (!mvneta_rxq_desc_is_first_last(status)) {
947 netdev_err(pp->dev,
948 "bad rx status %08x (buffer oversize), size=%d\n",
949 status, rx_desc->data_size);
950 return;
951 }
952
953 switch (status & MVNETA_RXD_ERR_CODE_MASK) {
954 case MVNETA_RXD_ERR_CRC:
955 netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
956 status, rx_desc->data_size);
957 break;
958 case MVNETA_RXD_ERR_OVERRUN:
959 netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
960 status, rx_desc->data_size);
961 break;
962 case MVNETA_RXD_ERR_LEN:
963 netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
964 status, rx_desc->data_size);
965 break;
966 case MVNETA_RXD_ERR_RESOURCE:
967 netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
968 status, rx_desc->data_size);
969 break;
970 }
971}
972
973static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
974 int rxq)
975{
976 return &pp->rxqs[rxq];
977}
978
979
980/* Drop packets received by the RXQ and free buffers */
981static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
982 struct mvneta_rx_queue *rxq)
983{
984 int rx_done;
985
986 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
987 if (rx_done)
988 mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
989}
990
991/* Handle rxq fill: allocates rxq skbs; called when initializing a port */
992static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
993 int num)
994{
995 int i;
996
997 for (i = 0; i < num; i++) {
998 u32 addr;
999
1000 /* U-Boot special: Fill in the rx buffer addresses */
1001 addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
1002 mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
1003 }
1004
1005 /* Add this number of RX descriptors as non occupied (ready to
1006 * get packets)
1007 */
1008 mvneta_rxq_non_occup_desc_add(pp, rxq, i);
1009
1010 return 0;
1011}
1012
1013/* Rx/Tx queue initialization/cleanup methods */
1014
1015/* Create a specified RX queue */
1016static int mvneta_rxq_init(struct mvneta_port *pp,
1017 struct mvneta_rx_queue *rxq)
1018
1019{
1020 rxq->size = pp->rx_ring_size;
1021
1022 /* Allocate memory for RX descriptors */
1023 rxq->descs_phys = (dma_addr_t)rxq->descs;
1024 if (rxq->descs == NULL)
1025 return -ENOMEM;
1026
Jon Nettleton199b27b2018-05-30 08:52:29 +03001027 WARN_ON(rxq->descs != PTR_ALIGN(rxq->descs, ARCH_DMA_MINALIGN));
1028
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001029 rxq->last_desc = rxq->size - 1;
1030
1031 /* Set Rx descriptors queue starting address */
1032 mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
1033 mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
1034
1035 /* Fill RXQ with buffers from RX pool */
1036 mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
1037 mvneta_rxq_fill(pp, rxq, rxq->size);
1038
1039 return 0;
1040}
1041
1042/* Cleanup Rx queue */
1043static void mvneta_rxq_deinit(struct mvneta_port *pp,
1044 struct mvneta_rx_queue *rxq)
1045{
1046 mvneta_rxq_drop_pkts(pp, rxq);
1047
1048 rxq->descs = NULL;
1049 rxq->last_desc = 0;
1050 rxq->next_desc_to_proc = 0;
1051 rxq->descs_phys = 0;
1052}
1053
1054/* Create and initialize a tx queue */
1055static int mvneta_txq_init(struct mvneta_port *pp,
1056 struct mvneta_tx_queue *txq)
1057{
1058 txq->size = pp->tx_ring_size;
1059
1060 /* Allocate memory for TX descriptors */
Stefan Roese3cbc11d2016-05-19 18:09:17 +02001061 txq->descs_phys = (dma_addr_t)txq->descs;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001062 if (txq->descs == NULL)
1063 return -ENOMEM;
1064
Jon Nettleton199b27b2018-05-30 08:52:29 +03001065 WARN_ON(txq->descs != PTR_ALIGN(txq->descs, ARCH_DMA_MINALIGN));
1066
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001067 txq->last_desc = txq->size - 1;
1068
1069 /* Set maximum bandwidth for enabled TXQs */
1070 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
1071 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
1072
1073 /* Set Tx descriptors queue starting address */
1074 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
1075 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
1076
1077 return 0;
1078}
1079
1080/* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
1081static void mvneta_txq_deinit(struct mvneta_port *pp,
1082 struct mvneta_tx_queue *txq)
1083{
1084 txq->descs = NULL;
1085 txq->last_desc = 0;
1086 txq->next_desc_to_proc = 0;
1087 txq->descs_phys = 0;
1088
1089 /* Set minimum bandwidth for disabled TXQs */
1090 mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
1091 mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
1092
1093 /* Set Tx descriptors queue starting address and size */
1094 mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
1095 mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
1096}
1097
1098/* Cleanup all Tx queues */
1099static void mvneta_cleanup_txqs(struct mvneta_port *pp)
1100{
1101 int queue;
1102
1103 for (queue = 0; queue < txq_number; queue++)
1104 mvneta_txq_deinit(pp, &pp->txqs[queue]);
1105}
1106
1107/* Cleanup all Rx queues */
1108static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
1109{
1110 int queue;
1111
1112 for (queue = 0; queue < rxq_number; queue++)
1113 mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
1114}
1115
1116
1117/* Init all Rx queues */
1118static int mvneta_setup_rxqs(struct mvneta_port *pp)
1119{
1120 int queue;
1121
1122 for (queue = 0; queue < rxq_number; queue++) {
1123 int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
1124 if (err) {
1125 netdev_err(pp->dev, "%s: can't create rxq=%d\n",
1126 __func__, queue);
1127 mvneta_cleanup_rxqs(pp);
1128 return err;
1129 }
1130 }
1131
1132 return 0;
1133}
1134
1135/* Init all tx queues */
1136static int mvneta_setup_txqs(struct mvneta_port *pp)
1137{
1138 int queue;
1139
1140 for (queue = 0; queue < txq_number; queue++) {
1141 int err = mvneta_txq_init(pp, &pp->txqs[queue]);
1142 if (err) {
1143 netdev_err(pp->dev, "%s: can't create txq=%d\n",
1144 __func__, queue);
1145 mvneta_cleanup_txqs(pp);
1146 return err;
1147 }
1148 }
1149
1150 return 0;
1151}
1152
1153static void mvneta_start_dev(struct mvneta_port *pp)
1154{
1155 /* start the Rx/Tx activity */
1156 mvneta_port_enable(pp);
1157}
1158
Stefan Roesee3b9c982015-11-19 07:46:15 +01001159static void mvneta_adjust_link(struct udevice *dev)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001160{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001161 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001162 struct phy_device *phydev = pp->phydev;
1163 int status_change = 0;
1164
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001165 if (mvneta_port_is_fixed_link(pp)) {
1166 debug("Using fixed link, skip link adjust\n");
1167 return;
1168 }
1169
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001170 if (phydev->link) {
1171 if ((pp->speed != phydev->speed) ||
1172 (pp->duplex != phydev->duplex)) {
1173 u32 val;
1174
1175 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1176 val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
1177 MVNETA_GMAC_CONFIG_GMII_SPEED |
1178 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
1179 MVNETA_GMAC_AN_SPEED_EN |
1180 MVNETA_GMAC_AN_DUPLEX_EN);
1181
1182 if (phydev->duplex)
1183 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1184
1185 if (phydev->speed == SPEED_1000)
1186 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1187 else
1188 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1189
1190 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1191
1192 pp->duplex = phydev->duplex;
1193 pp->speed = phydev->speed;
1194 }
1195 }
1196
1197 if (phydev->link != pp->link) {
1198 if (!phydev->link) {
1199 pp->duplex = -1;
1200 pp->speed = 0;
1201 }
1202
1203 pp->link = phydev->link;
1204 status_change = 1;
1205 }
1206
1207 if (status_change) {
1208 if (phydev->link) {
1209 u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1210 val |= (MVNETA_GMAC_FORCE_LINK_PASS |
1211 MVNETA_GMAC_FORCE_LINK_DOWN);
1212 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1213 mvneta_port_up(pp);
1214 } else {
1215 mvneta_port_down(pp);
1216 }
1217 }
1218}
1219
Stefan Roesee3b9c982015-11-19 07:46:15 +01001220static int mvneta_open(struct udevice *dev)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001221{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001222 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001223 int ret;
1224
1225 ret = mvneta_setup_rxqs(pp);
1226 if (ret)
1227 return ret;
1228
1229 ret = mvneta_setup_txqs(pp);
1230 if (ret)
1231 return ret;
1232
1233 mvneta_adjust_link(dev);
1234
1235 mvneta_start_dev(pp);
1236
1237 return 0;
1238}
1239
1240/* Initialize hw */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001241static int mvneta_init2(struct mvneta_port *pp)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001242{
1243 int queue;
1244
1245 /* Disable port */
1246 mvneta_port_disable(pp);
1247
1248 /* Set port default values */
1249 mvneta_defaults_set(pp);
1250
1251 pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
1252 GFP_KERNEL);
1253 if (!pp->txqs)
1254 return -ENOMEM;
1255
1256 /* U-Boot special: use preallocated area */
1257 pp->txqs[0].descs = buffer_loc.tx_descs;
1258
1259 /* Initialize TX descriptor rings */
1260 for (queue = 0; queue < txq_number; queue++) {
1261 struct mvneta_tx_queue *txq = &pp->txqs[queue];
1262 txq->id = queue;
1263 txq->size = pp->tx_ring_size;
1264 }
1265
1266 pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
1267 GFP_KERNEL);
1268 if (!pp->rxqs) {
1269 kfree(pp->txqs);
1270 return -ENOMEM;
1271 }
1272
1273 /* U-Boot special: use preallocated area */
1274 pp->rxqs[0].descs = buffer_loc.rx_descs;
1275
1276 /* Create Rx descriptor rings */
1277 for (queue = 0; queue < rxq_number; queue++) {
1278 struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1279 rxq->id = queue;
1280 rxq->size = pp->rx_ring_size;
1281 }
1282
1283 return 0;
1284}
1285
1286/* platform glue : initialize decoding windows */
Stefan Roese544eefe2016-05-19 17:46:36 +02001287
1288/*
1289 * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1290 * First layer is: GbE Address window that resides inside the GBE unit,
1291 * Second layer is: Fabric address window which is located in the NIC400
1292 * (South Fabric).
1293 * To simplify the address decode configuration for Armada3700, we bypass the
1294 * first layer of GBE decode window by setting the first window to 4GB.
1295 */
1296static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1297{
1298 /*
1299 * Set window size to 4GB, to bypass GBE address decode, leave the
1300 * work to MBUS decode window
1301 */
1302 mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1303
1304 /* Enable GBE address decode window 0 by set bit 0 to 0 */
1305 clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1306 MVNETA_BASE_ADDR_ENABLE_BIT);
1307
1308 /* Set GBE address decode window 0 to full Access (read or write) */
1309 setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1310 MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1311}
1312
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001313static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
1314{
1315 const struct mbus_dram_target_info *dram;
1316 u32 win_enable;
1317 u32 win_protect;
1318 int i;
1319
1320 dram = mvebu_mbus_dram_info();
1321 for (i = 0; i < 6; i++) {
1322 mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1323 mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1324
1325 if (i < 4)
1326 mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1327 }
1328
1329 win_enable = 0x3f;
1330 win_protect = 0;
1331
1332 for (i = 0; i < dram->num_cs; i++) {
1333 const struct mbus_dram_window *cs = dram->cs + i;
1334 mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
1335 (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
1336
1337 mvreg_write(pp, MVNETA_WIN_SIZE(i),
1338 (cs->size - 1) & 0xffff0000);
1339
1340 win_enable &= ~(1 << i);
1341 win_protect |= 3 << (2 * i);
1342 }
1343
1344 mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1345}
1346
1347/* Power up the port */
1348static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
1349{
1350 u32 ctrl;
1351
1352 /* MAC Cause register should be cleared */
1353 mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
1354
1355 ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1356
1357 /* Even though it might look weird, when we're configured in
1358 * SGMII or QSGMII mode, the RGMII bit needs to be set.
1359 */
1360 switch (phy_mode) {
1361 case PHY_INTERFACE_MODE_QSGMII:
1362 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
1363 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1364 break;
1365 case PHY_INTERFACE_MODE_SGMII:
1366 mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
1367 ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1368 break;
1369 case PHY_INTERFACE_MODE_RGMII:
1370 case PHY_INTERFACE_MODE_RGMII_ID:
1371 ctrl |= MVNETA_GMAC2_PORT_RGMII;
1372 break;
1373 default:
1374 return -EINVAL;
1375 }
1376
1377 /* Cancel Port Reset */
1378 ctrl &= ~MVNETA_GMAC2_PORT_RESET;
1379 mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
1380
1381 while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
1382 MVNETA_GMAC2_PORT_RESET) != 0)
1383 continue;
1384
1385 return 0;
1386}
1387
1388/* Device initialization routine */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001389static int mvneta_init(struct udevice *dev)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001390{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001391 struct eth_pdata *pdata = dev_get_platdata(dev);
1392 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001393 int err;
1394
1395 pp->tx_ring_size = MVNETA_MAX_TXD;
1396 pp->rx_ring_size = MVNETA_MAX_RXD;
1397
Stefan Roesee3b9c982015-11-19 07:46:15 +01001398 err = mvneta_init2(pp);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001399 if (err < 0) {
1400 dev_err(&pdev->dev, "can't init eth hal\n");
1401 return err;
1402 }
1403
Stefan Roesee3b9c982015-11-19 07:46:15 +01001404 mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001405
1406 err = mvneta_port_power_up(pp, pp->phy_interface);
1407 if (err < 0) {
1408 dev_err(&pdev->dev, "can't power up port\n");
1409 return err;
1410 }
1411
1412 /* Call open() now as it needs to be done before runing send() */
1413 mvneta_open(dev);
1414
1415 return 0;
1416}
1417
1418/* U-Boot only functions follow here */
1419
1420/* SMI / MDIO functions */
1421
1422static int smi_wait_ready(struct mvneta_port *pp)
1423{
1424 u32 timeout = MVNETA_SMI_TIMEOUT;
1425 u32 smi_reg;
1426
1427 /* wait till the SMI is not busy */
1428 do {
1429 /* read smi register */
1430 smi_reg = mvreg_read(pp, MVNETA_SMI);
1431 if (timeout-- == 0) {
1432 printf("Error: SMI busy timeout\n");
1433 return -EFAULT;
1434 }
1435 } while (smi_reg & MVNETA_SMI_BUSY);
1436
1437 return 0;
1438}
1439
1440/*
Stefan Roesee3b9c982015-11-19 07:46:15 +01001441 * mvneta_mdio_read - miiphy_read callback function.
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001442 *
1443 * Returns 16bit phy register value, or 0xffff on error
1444 */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001445static int mvneta_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001446{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001447 struct mvneta_port *pp = bus->priv;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001448 u32 smi_reg;
1449 u32 timeout;
1450
1451 /* check parameters */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001452 if (addr > MVNETA_PHY_ADDR_MASK) {
1453 printf("Error: Invalid PHY address %d\n", addr);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001454 return -EFAULT;
1455 }
1456
Stefan Roesee3b9c982015-11-19 07:46:15 +01001457 if (reg > MVNETA_PHY_REG_MASK) {
1458 printf("Err: Invalid register offset %d\n", reg);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001459 return -EFAULT;
1460 }
1461
1462 /* wait till the SMI is not busy */
1463 if (smi_wait_ready(pp) < 0)
1464 return -EFAULT;
1465
1466 /* fill the phy address and regiser offset and read opcode */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001467 smi_reg = (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1468 | (reg << MVNETA_SMI_REG_ADDR_OFFS)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001469 | MVNETA_SMI_OPCODE_READ;
1470
1471 /* write the smi register */
1472 mvreg_write(pp, MVNETA_SMI, smi_reg);
1473
Stefan Roesee3b9c982015-11-19 07:46:15 +01001474 /* wait till read value is ready */
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001475 timeout = MVNETA_SMI_TIMEOUT;
1476
1477 do {
1478 /* read smi register */
1479 smi_reg = mvreg_read(pp, MVNETA_SMI);
1480 if (timeout-- == 0) {
1481 printf("Err: SMI read ready timeout\n");
1482 return -EFAULT;
1483 }
1484 } while (!(smi_reg & MVNETA_SMI_READ_VALID));
1485
1486 /* Wait for the data to update in the SMI register */
1487 for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
1488 ;
1489
Stefan Roesee3b9c982015-11-19 07:46:15 +01001490 return mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001491}
1492
1493/*
Stefan Roesee3b9c982015-11-19 07:46:15 +01001494 * mvneta_mdio_write - miiphy_write callback function.
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001495 *
1496 * Returns 0 if write succeed, -EINVAL on bad parameters
1497 * -ETIME on timeout
1498 */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001499static int mvneta_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
1500 u16 value)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001501{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001502 struct mvneta_port *pp = bus->priv;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001503 u32 smi_reg;
1504
1505 /* check parameters */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001506 if (addr > MVNETA_PHY_ADDR_MASK) {
1507 printf("Error: Invalid PHY address %d\n", addr);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001508 return -EFAULT;
1509 }
1510
Stefan Roesee3b9c982015-11-19 07:46:15 +01001511 if (reg > MVNETA_PHY_REG_MASK) {
1512 printf("Err: Invalid register offset %d\n", reg);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001513 return -EFAULT;
1514 }
1515
1516 /* wait till the SMI is not busy */
1517 if (smi_wait_ready(pp) < 0)
1518 return -EFAULT;
1519
1520 /* fill the phy addr and reg offset and write opcode and data */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001521 smi_reg = value << MVNETA_SMI_DATA_OFFS;
1522 smi_reg |= (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1523 | (reg << MVNETA_SMI_REG_ADDR_OFFS);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001524 smi_reg &= ~MVNETA_SMI_OPCODE_READ;
1525
1526 /* write the smi register */
1527 mvreg_write(pp, MVNETA_SMI, smi_reg);
1528
1529 return 0;
1530}
1531
Stefan Roesee3b9c982015-11-19 07:46:15 +01001532static int mvneta_start(struct udevice *dev)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001533{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001534 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001535 struct phy_device *phydev;
1536
1537 mvneta_port_power_up(pp, pp->phy_interface);
1538
1539 if (!pp->init || pp->link == 0) {
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001540 if (mvneta_port_is_fixed_link(pp)) {
1541 u32 val;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001542
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001543 pp->init = 1;
1544 pp->link = 1;
1545 mvneta_init(dev);
1546
1547 val = MVNETA_GMAC_FORCE_LINK_UP |
1548 MVNETA_GMAC_IB_BYPASS_AN_EN |
1549 MVNETA_GMAC_SET_FC_EN |
1550 MVNETA_GMAC_ADVERT_FC_EN |
1551 MVNETA_GMAC_SAMPLE_TX_CFG_EN;
1552
1553 if (pp->duplex)
1554 val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1555
1556 if (pp->speed == SPEED_1000)
1557 val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1558 else if (pp->speed == SPEED_100)
1559 val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1560
1561 mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1562 } else {
1563 /* Set phy address of the port */
1564 mvreg_write(pp, MVNETA_PHY_ADDR, pp->phyaddr);
1565
1566 phydev = phy_connect(pp->bus, pp->phyaddr, dev,
1567 pp->phy_interface);
Marek Behúncf2cf852018-04-24 17:21:29 +02001568 if (!phydev) {
1569 printf("phy_connect failed\n");
1570 return -ENODEV;
1571 }
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001572
1573 pp->phydev = phydev;
1574 phy_config(phydev);
1575 phy_startup(phydev);
1576 if (!phydev->link) {
1577 printf("%s: No link.\n", phydev->dev->name);
1578 return -1;
1579 }
1580
1581 /* Full init on first call */
1582 mvneta_init(dev);
1583 pp->init = 1;
1584 return 0;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001585 }
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001586 }
1587
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001588 /* Upon all following calls, this is enough */
1589 mvneta_port_up(pp);
1590 mvneta_port_enable(pp);
1591
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001592 return 0;
1593}
1594
Stefan Roesee3b9c982015-11-19 07:46:15 +01001595static int mvneta_send(struct udevice *dev, void *packet, int length)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001596{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001597 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001598 struct mvneta_tx_queue *txq = &pp->txqs[0];
1599 struct mvneta_tx_desc *tx_desc;
1600 int sent_desc;
1601 u32 timeout = 0;
1602
1603 /* Get a descriptor for the first part of the packet */
1604 tx_desc = mvneta_txq_next_desc_get(txq);
1605
Stefan Roese3cbc11d2016-05-19 18:09:17 +02001606 tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
Stefan Roesee3b9c982015-11-19 07:46:15 +01001607 tx_desc->data_size = length;
Stefan Roese3cbc11d2016-05-19 18:09:17 +02001608 flush_dcache_range((ulong)packet,
1609 (ulong)packet + ALIGN(length, PKTALIGN));
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001610
1611 /* First and Last descriptor */
1612 tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
1613 mvneta_txq_pend_desc_add(pp, txq, 1);
1614
1615 /* Wait for packet to be sent (queue might help with speed here) */
1616 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1617 while (!sent_desc) {
1618 if (timeout++ > 10000) {
1619 printf("timeout: packet not sent\n");
1620 return -1;
1621 }
1622 sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1623 }
1624
1625 /* txDone has increased - hw sent packet */
1626 mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001627
1628 return 0;
1629}
1630
Stefan Roesee3b9c982015-11-19 07:46:15 +01001631static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001632{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001633 struct mvneta_port *pp = dev_get_priv(dev);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001634 int rx_done;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001635 struct mvneta_rx_queue *rxq;
Stefan Roesee3b9c982015-11-19 07:46:15 +01001636 int rx_bytes = 0;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001637
1638 /* get rx queue */
1639 rxq = mvneta_rxq_handle_get(pp, rxq_def);
1640 rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001641
Stefan Roesee3b9c982015-11-19 07:46:15 +01001642 if (rx_done) {
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001643 struct mvneta_rx_desc *rx_desc;
1644 unsigned char *data;
1645 u32 rx_status;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001646
1647 /*
1648 * No cache invalidation needed here, since the desc's are
1649 * located in a uncached memory region
1650 */
1651 rx_desc = mvneta_rxq_next_desc_get(rxq);
1652
1653 rx_status = rx_desc->status;
1654 if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1655 (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1656 mvneta_rx_error(pp, rx_desc);
1657 /* leave the descriptor untouched */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001658 return -EIO;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001659 }
1660
1661 /* 2 bytes for marvell header. 4 bytes for crc */
1662 rx_bytes = rx_desc->data_size - 6;
1663
1664 /* give packet to stack - skip on first 2 bytes */
Stefan Roese3cbc11d2016-05-19 18:09:17 +02001665 data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001666 /*
1667 * No cache invalidation needed here, since the rx_buffer's are
1668 * located in a uncached memory region
1669 */
Stefan Roesee3b9c982015-11-19 07:46:15 +01001670 *packetp = data;
1671
Jason Brown32ac8b02017-11-28 11:12:43 -08001672 /*
1673 * Only mark one descriptor as free
1674 * since only one was processed
1675 */
1676 mvneta_rxq_desc_num_update(pp, rxq, 1, 1);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001677 }
1678
Stefan Roesee3b9c982015-11-19 07:46:15 +01001679 return rx_bytes;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001680}
1681
Stefan Roesee3b9c982015-11-19 07:46:15 +01001682static int mvneta_probe(struct udevice *dev)
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001683{
Stefan Roesee3b9c982015-11-19 07:46:15 +01001684 struct eth_pdata *pdata = dev_get_platdata(dev);
1685 struct mvneta_port *pp = dev_get_priv(dev);
1686 void *blob = (void *)gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -07001687 int node = dev_of_offset(dev);
Stefan Roesee3b9c982015-11-19 07:46:15 +01001688 struct mii_dev *bus;
1689 unsigned long addr;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001690 void *bd_space;
Konstantin Porotchkin976feda2017-02-16 13:52:27 +02001691 int ret;
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001692 int fl_node;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001693
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001694 /*
1695 * Allocate buffer area for descs and rx_buffers. This is only
1696 * done once for all interfaces. As only one interface can
Chris Packham6723b232016-08-29 20:54:02 +12001697 * be active. Make this area DMA safe by disabling the D-cache
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001698 */
1699 if (!buffer_loc.tx_descs) {
Jon Nettleton199b27b2018-05-30 08:52:29 +03001700 u32 size;
1701
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001702 /* Align buffer area for descs and rx_buffers to 1MiB */
1703 bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
Rabeeh Khoury0f8888b2018-06-19 21:36:50 +03001704 flush_dcache_range((ulong)bd_space, (ulong)bd_space + BD_SPACE);
Stefan Roese3cbc11d2016-05-19 18:09:17 +02001705 mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001706 DCACHE_OFF);
1707 buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
Jon Nettleton199b27b2018-05-30 08:52:29 +03001708 size = roundup(MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc),
1709 ARCH_DMA_MINALIGN);
Rabeeh Khoury318b5d72018-06-19 21:36:51 +03001710 memset(buffer_loc.tx_descs, 0, size);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001711 buffer_loc.rx_descs = (struct mvneta_rx_desc *)
Jon Nettleton199b27b2018-05-30 08:52:29 +03001712 ((phys_addr_t)bd_space + size);
1713 size += roundup(MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc),
1714 ARCH_DMA_MINALIGN);
1715 buffer_loc.rx_buffers = (phys_addr_t)(bd_space + size);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001716 }
1717
Stefan Roesee3b9c982015-11-19 07:46:15 +01001718 pp->base = (void __iomem *)pdata->iobase;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001719
Stefan Roesee3b9c982015-11-19 07:46:15 +01001720 /* Configure MBUS address windows */
Simon Glass911f3ae2017-05-18 20:08:57 -06001721 if (device_is_compatible(dev, "marvell,armada-3700-neta"))
Stefan Roese544eefe2016-05-19 17:46:36 +02001722 mvneta_bypass_mbus_windows(pp);
1723 else
1724 mvneta_conf_mbus_windows(pp);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001725
Stefan Roesee3b9c982015-11-19 07:46:15 +01001726 /* PHY interface is already decoded in mvneta_ofdata_to_platdata() */
1727 pp->phy_interface = pdata->phy_interface;
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001728
Konstantin Porotchkin278d30c2017-02-16 13:52:28 +02001729 /* fetch 'fixed-link' property from 'neta' node */
1730 fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1731 if (fl_node != -FDT_ERR_NOTFOUND) {
1732 /* set phy_addr to invalid value for fixed link */
1733 pp->phyaddr = PHY_MAX_ADDR + 1;
1734 pp->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1735 pp->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1736 } else {
1737 /* Now read phyaddr from DT */
1738 addr = fdtdec_get_int(blob, node, "phy", 0);
1739 addr = fdt_node_offset_by_phandle(blob, addr);
1740 pp->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1741 }
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001742
Stefan Roesee3b9c982015-11-19 07:46:15 +01001743 bus = mdio_alloc();
1744 if (!bus) {
1745 printf("Failed to allocate MDIO bus\n");
1746 return -ENOMEM;
1747 }
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001748
Stefan Roesee3b9c982015-11-19 07:46:15 +01001749 bus->read = mvneta_mdio_read;
1750 bus->write = mvneta_mdio_write;
1751 snprintf(bus->name, sizeof(bus->name), dev->name);
1752 bus->priv = (void *)pp;
1753 pp->bus = bus;
1754
Konstantin Porotchkin976feda2017-02-16 13:52:27 +02001755 ret = mdio_register(bus);
1756 if (ret)
1757 return ret;
1758
Simon Glassbcee8d62019-12-06 21:41:35 -07001759#if CONFIG_IS_ENABLED(DM_GPIO)
Aditya Prayoga18bfc8f2018-12-05 00:39:23 +08001760 gpio_request_by_name(dev, "phy-reset-gpios", 0,
1761 &pp->phy_reset_gpio, GPIOD_IS_OUT);
1762
1763 if (dm_gpio_is_valid(&pp->phy_reset_gpio)) {
1764 dm_gpio_set_value(&pp->phy_reset_gpio, 1);
1765 mdelay(10);
1766 dm_gpio_set_value(&pp->phy_reset_gpio, 0);
1767 }
1768#endif
1769
Konstantin Porotchkin976feda2017-02-16 13:52:27 +02001770 return board_network_enable(bus);
Stefan Roese19fc2ea2014-10-22 12:13:14 +02001771}
Stefan Roesee3b9c982015-11-19 07:46:15 +01001772
1773static void mvneta_stop(struct udevice *dev)
1774{
1775 struct mvneta_port *pp = dev_get_priv(dev);
1776
1777 mvneta_port_down(pp);
1778 mvneta_port_disable(pp);
1779}
1780
1781static const struct eth_ops mvneta_ops = {
1782 .start = mvneta_start,
1783 .send = mvneta_send,
1784 .recv = mvneta_recv,
1785 .stop = mvneta_stop,
Matt Pelland0a85f022018-03-27 13:18:25 -04001786 .write_hwaddr = mvneta_write_hwaddr,
Stefan Roesee3b9c982015-11-19 07:46:15 +01001787};
1788
1789static int mvneta_ofdata_to_platdata(struct udevice *dev)
1790{
1791 struct eth_pdata *pdata = dev_get_platdata(dev);
1792 const char *phy_mode;
1793
Simon Glassa821c4a2017-05-17 17:18:05 -06001794 pdata->iobase = devfdt_get_addr(dev);
Stefan Roesee3b9c982015-11-19 07:46:15 +01001795
1796 /* Get phy-mode / phy_interface from DT */
1797 pdata->phy_interface = -1;
Simon Glasse160f7d2017-01-17 16:52:55 -07001798 phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1799 NULL);
Stefan Roesee3b9c982015-11-19 07:46:15 +01001800 if (phy_mode)
1801 pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1802 if (pdata->phy_interface == -1) {
1803 debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1804 return -EINVAL;
1805 }
1806
1807 return 0;
1808}
1809
1810static const struct udevice_id mvneta_ids[] = {
1811 { .compatible = "marvell,armada-370-neta" },
1812 { .compatible = "marvell,armada-xp-neta" },
Stefan Roese544eefe2016-05-19 17:46:36 +02001813 { .compatible = "marvell,armada-3700-neta" },
Stefan Roesee3b9c982015-11-19 07:46:15 +01001814 { }
1815};
1816
1817U_BOOT_DRIVER(mvneta) = {
1818 .name = "mvneta",
1819 .id = UCLASS_ETH,
1820 .of_match = mvneta_ids,
1821 .ofdata_to_platdata = mvneta_ofdata_to_platdata,
1822 .probe = mvneta_probe,
1823 .ops = &mvneta_ops,
1824 .priv_auto_alloc_size = sizeof(struct mvneta_port),
1825 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1826};