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