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