xref: /rk3399_rockchip-uboot/drivers/net/mvneta.c (revision 544eefe084661d5ee486aca0cd0ec03801bfced9)
119fc2eaeSStefan Roese /*
219fc2eaeSStefan Roese  * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
319fc2eaeSStefan Roese  *
419fc2eaeSStefan Roese  * U-Boot version:
5e3b9c98aSStefan Roese  * Copyright (C) 2014-2015 Stefan Roese <sr@denx.de>
619fc2eaeSStefan Roese  *
719fc2eaeSStefan Roese  * Based on the Linux version which is:
819fc2eaeSStefan Roese  * Copyright (C) 2012 Marvell
919fc2eaeSStefan Roese  *
1019fc2eaeSStefan Roese  * Rami Rosen <rosenr@marvell.com>
1119fc2eaeSStefan Roese  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
1219fc2eaeSStefan Roese  *
1319fc2eaeSStefan Roese  * SPDX-License-Identifier:	GPL-2.0
1419fc2eaeSStefan Roese  */
1519fc2eaeSStefan Roese 
1619fc2eaeSStefan Roese #include <common.h>
17e3b9c98aSStefan Roese #include <dm.h>
1819fc2eaeSStefan Roese #include <net.h>
1919fc2eaeSStefan Roese #include <netdev.h>
2019fc2eaeSStefan Roese #include <config.h>
2119fc2eaeSStefan Roese #include <malloc.h>
2219fc2eaeSStefan Roese #include <asm/io.h>
231221ce45SMasahiro Yamada #include <linux/errno.h>
2419fc2eaeSStefan Roese #include <phy.h>
2519fc2eaeSStefan Roese #include <miiphy.h>
2619fc2eaeSStefan Roese #include <watchdog.h>
2719fc2eaeSStefan Roese #include <asm/arch/cpu.h>
2819fc2eaeSStefan Roese #include <asm/arch/soc.h>
2919fc2eaeSStefan Roese #include <linux/compat.h>
3019fc2eaeSStefan Roese #include <linux/mbus.h>
3119fc2eaeSStefan Roese 
32e3b9c98aSStefan Roese DECLARE_GLOBAL_DATA_PTR;
33e3b9c98aSStefan Roese 
3419fc2eaeSStefan Roese #if !defined(CONFIG_PHYLIB)
3519fc2eaeSStefan Roese # error Marvell mvneta requires PHYLIB
3619fc2eaeSStefan Roese #endif
3719fc2eaeSStefan Roese 
3819fc2eaeSStefan Roese /* Some linux -> U-Boot compatibility stuff */
3919fc2eaeSStefan Roese #define netdev_err(dev, fmt, args...)		\
4019fc2eaeSStefan Roese 	printf(fmt, ##args)
4119fc2eaeSStefan Roese #define netdev_warn(dev, fmt, args...)		\
4219fc2eaeSStefan Roese 	printf(fmt, ##args)
4319fc2eaeSStefan Roese #define netdev_info(dev, fmt, args...)		\
4419fc2eaeSStefan Roese 	printf(fmt, ##args)
4519fc2eaeSStefan Roese 
4619fc2eaeSStefan Roese #define CONFIG_NR_CPUS		1
4719fc2eaeSStefan Roese #define ETH_HLEN		14	/* Total octets in header */
4819fc2eaeSStefan Roese 
4919fc2eaeSStefan Roese /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
5019fc2eaeSStefan Roese #define WRAP			(2 + ETH_HLEN + 4 + 32)
5119fc2eaeSStefan Roese #define MTU			1500
5219fc2eaeSStefan Roese #define RX_BUFFER_SIZE		(ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
5319fc2eaeSStefan Roese 
5419fc2eaeSStefan Roese #define MVNETA_SMI_TIMEOUT			10000
5519fc2eaeSStefan Roese 
5619fc2eaeSStefan Roese /* Registers */
5719fc2eaeSStefan Roese #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
5819fc2eaeSStefan Roese #define	     MVNETA_RXQ_HW_BUF_ALLOC            BIT(1)
5919fc2eaeSStefan Roese #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
6019fc2eaeSStefan Roese #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
6119fc2eaeSStefan Roese #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
6219fc2eaeSStefan Roese #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
6319fc2eaeSStefan Roese #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
6419fc2eaeSStefan Roese #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
6519fc2eaeSStefan Roese #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
6619fc2eaeSStefan Roese #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
6719fc2eaeSStefan Roese #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
6819fc2eaeSStefan Roese #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
6919fc2eaeSStefan Roese #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
7019fc2eaeSStefan Roese #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
7119fc2eaeSStefan Roese #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
7219fc2eaeSStefan Roese #define MVNETA_PORT_RX_RESET                    0x1cc0
7319fc2eaeSStefan Roese #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
7419fc2eaeSStefan Roese #define MVNETA_PHY_ADDR                         0x2000
7519fc2eaeSStefan Roese #define      MVNETA_PHY_ADDR_MASK               0x1f
7619fc2eaeSStefan Roese #define MVNETA_SMI                              0x2004
7719fc2eaeSStefan Roese #define      MVNETA_PHY_REG_MASK                0x1f
7819fc2eaeSStefan Roese /* SMI register fields */
7919fc2eaeSStefan Roese #define     MVNETA_SMI_DATA_OFFS		0	/* Data */
8019fc2eaeSStefan Roese #define     MVNETA_SMI_DATA_MASK		(0xffff << MVNETA_SMI_DATA_OFFS)
8119fc2eaeSStefan Roese #define     MVNETA_SMI_DEV_ADDR_OFFS		16	/* PHY device address */
8219fc2eaeSStefan Roese #define     MVNETA_SMI_REG_ADDR_OFFS		21	/* PHY device reg addr*/
8319fc2eaeSStefan Roese #define     MVNETA_SMI_OPCODE_OFFS		26	/* Write/Read opcode */
8419fc2eaeSStefan Roese #define     MVNETA_SMI_OPCODE_READ		(1 << MVNETA_SMI_OPCODE_OFFS)
8519fc2eaeSStefan Roese #define     MVNETA_SMI_READ_VALID		(1 << 27)	/* Read Valid */
8619fc2eaeSStefan Roese #define     MVNETA_SMI_BUSY			(1 << 28)	/* Busy */
8719fc2eaeSStefan Roese #define MVNETA_MBUS_RETRY                       0x2010
8819fc2eaeSStefan Roese #define MVNETA_UNIT_INTR_CAUSE                  0x2080
8919fc2eaeSStefan Roese #define MVNETA_UNIT_CONTROL                     0x20B0
9019fc2eaeSStefan Roese #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
9119fc2eaeSStefan Roese #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
9219fc2eaeSStefan Roese #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
9319fc2eaeSStefan Roese #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
94*544eefe0SStefan Roese #define MVNETA_WIN_SIZE_MASK			(0xffff0000)
9519fc2eaeSStefan Roese #define MVNETA_BASE_ADDR_ENABLE                 0x2290
96*544eefe0SStefan Roese #define      MVNETA_BASE_ADDR_ENABLE_BIT	0x1
97*544eefe0SStefan Roese #define MVNETA_PORT_ACCESS_PROTECT              0x2294
98*544eefe0SStefan Roese #define      MVNETA_PORT_ACCESS_PROTECT_WIN0_RW	0x3
9919fc2eaeSStefan Roese #define MVNETA_PORT_CONFIG                      0x2400
10019fc2eaeSStefan Roese #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
10119fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
10219fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
10319fc2eaeSStefan Roese #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
10419fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
10519fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
10619fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
10719fc2eaeSStefan Roese #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
10819fc2eaeSStefan Roese #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
10919fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_ARP(q)	 | \
11019fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_TCP(q)	 | \
11119fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_UDP(q)	 | \
11219fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_BPDU(q)	 | \
11319fc2eaeSStefan Roese 						 MVNETA_TX_UNSET_ERR_SUM | \
11419fc2eaeSStefan Roese 						 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
11519fc2eaeSStefan Roese #define MVNETA_PORT_CONFIG_EXTEND                0x2404
11619fc2eaeSStefan Roese #define MVNETA_MAC_ADDR_LOW                      0x2414
11719fc2eaeSStefan Roese #define MVNETA_MAC_ADDR_HIGH                     0x2418
11819fc2eaeSStefan Roese #define MVNETA_SDMA_CONFIG                       0x241c
11919fc2eaeSStefan Roese #define      MVNETA_SDMA_BRST_SIZE_16            4
12019fc2eaeSStefan Roese #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
12119fc2eaeSStefan Roese #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
12219fc2eaeSStefan Roese #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
12319fc2eaeSStefan Roese #define      MVNETA_DESC_SWAP                    BIT(6)
12419fc2eaeSStefan Roese #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
12519fc2eaeSStefan Roese #define MVNETA_PORT_STATUS                       0x2444
12619fc2eaeSStefan Roese #define      MVNETA_TX_IN_PRGRS                  BIT(1)
12719fc2eaeSStefan Roese #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
12819fc2eaeSStefan Roese #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
12919fc2eaeSStefan Roese #define MVNETA_SERDES_CFG			 0x24A0
13019fc2eaeSStefan Roese #define      MVNETA_SGMII_SERDES_PROTO		 0x0cc7
13119fc2eaeSStefan Roese #define      MVNETA_QSGMII_SERDES_PROTO		 0x0667
13219fc2eaeSStefan Roese #define MVNETA_TYPE_PRIO                         0x24bc
13319fc2eaeSStefan Roese #define      MVNETA_FORCE_UNI                    BIT(21)
13419fc2eaeSStefan Roese #define MVNETA_TXQ_CMD_1                         0x24e4
13519fc2eaeSStefan Roese #define MVNETA_TXQ_CMD                           0x2448
13619fc2eaeSStefan Roese #define      MVNETA_TXQ_DISABLE_SHIFT            8
13719fc2eaeSStefan Roese #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
13819fc2eaeSStefan Roese #define MVNETA_ACC_MODE                          0x2500
13919fc2eaeSStefan Roese #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
14019fc2eaeSStefan Roese #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
14119fc2eaeSStefan Roese #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
14219fc2eaeSStefan Roese #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
14319fc2eaeSStefan Roese 
14419fc2eaeSStefan Roese /* Exception Interrupt Port/Queue Cause register */
14519fc2eaeSStefan Roese 
14619fc2eaeSStefan Roese #define MVNETA_INTR_NEW_CAUSE                    0x25a0
14719fc2eaeSStefan Roese #define MVNETA_INTR_NEW_MASK                     0x25a4
14819fc2eaeSStefan Roese 
14919fc2eaeSStefan Roese /* bits  0..7  = TXQ SENT, one bit per queue.
15019fc2eaeSStefan Roese  * bits  8..15 = RXQ OCCUP, one bit per queue.
15119fc2eaeSStefan Roese  * bits 16..23 = RXQ FREE, one bit per queue.
15219fc2eaeSStefan Roese  * bit  29 = OLD_REG_SUM, see old reg ?
15319fc2eaeSStefan Roese  * bit  30 = TX_ERR_SUM, one bit for 4 ports
15419fc2eaeSStefan Roese  * bit  31 = MISC_SUM,   one bit for 4 ports
15519fc2eaeSStefan Roese  */
15619fc2eaeSStefan Roese #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
15719fc2eaeSStefan Roese #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
15819fc2eaeSStefan Roese #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
15919fc2eaeSStefan Roese #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
16019fc2eaeSStefan Roese 
16119fc2eaeSStefan Roese #define MVNETA_INTR_OLD_CAUSE                    0x25a8
16219fc2eaeSStefan Roese #define MVNETA_INTR_OLD_MASK                     0x25ac
16319fc2eaeSStefan Roese 
16419fc2eaeSStefan Roese /* Data Path Port/Queue Cause Register */
16519fc2eaeSStefan Roese #define MVNETA_INTR_MISC_CAUSE                   0x25b0
16619fc2eaeSStefan Roese #define MVNETA_INTR_MISC_MASK                    0x25b4
16719fc2eaeSStefan Roese #define MVNETA_INTR_ENABLE                       0x25b8
16819fc2eaeSStefan Roese 
16919fc2eaeSStefan Roese #define MVNETA_RXQ_CMD                           0x2680
17019fc2eaeSStefan Roese #define      MVNETA_RXQ_DISABLE_SHIFT            8
17119fc2eaeSStefan Roese #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
17219fc2eaeSStefan Roese #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
17319fc2eaeSStefan Roese #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
17419fc2eaeSStefan Roese #define MVNETA_GMAC_CTRL_0                       0x2c00
17519fc2eaeSStefan Roese #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
17619fc2eaeSStefan Roese #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
17719fc2eaeSStefan Roese #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
17819fc2eaeSStefan Roese #define MVNETA_GMAC_CTRL_2                       0x2c08
17919fc2eaeSStefan Roese #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
18019fc2eaeSStefan Roese #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
18119fc2eaeSStefan Roese #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
18219fc2eaeSStefan Roese #define MVNETA_GMAC_STATUS                       0x2c10
18319fc2eaeSStefan Roese #define      MVNETA_GMAC_LINK_UP                 BIT(0)
18419fc2eaeSStefan Roese #define      MVNETA_GMAC_SPEED_1000              BIT(1)
18519fc2eaeSStefan Roese #define      MVNETA_GMAC_SPEED_100               BIT(2)
18619fc2eaeSStefan Roese #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
18719fc2eaeSStefan Roese #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
18819fc2eaeSStefan Roese #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
18919fc2eaeSStefan Roese #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
19019fc2eaeSStefan Roese #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
19119fc2eaeSStefan Roese #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
19219fc2eaeSStefan Roese #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
19319fc2eaeSStefan Roese #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
19419fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
19519fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
19619fc2eaeSStefan Roese #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
19719fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
19819fc2eaeSStefan Roese #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
19919fc2eaeSStefan Roese #define MVNETA_MIB_COUNTERS_BASE                 0x3080
20019fc2eaeSStefan Roese #define      MVNETA_MIB_LATE_COLLISION           0x7c
20119fc2eaeSStefan Roese #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
20219fc2eaeSStefan Roese #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
20319fc2eaeSStefan Roese #define MVNETA_DA_FILT_UCAST_BASE                0x3600
20419fc2eaeSStefan Roese #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
20519fc2eaeSStefan Roese #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
20619fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
20719fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
20819fc2eaeSStefan Roese #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
20919fc2eaeSStefan Roese #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
21019fc2eaeSStefan Roese #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
21119fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
21219fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
21319fc2eaeSStefan Roese #define MVNETA_PORT_TX_RESET                     0x3cf0
21419fc2eaeSStefan Roese #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
21519fc2eaeSStefan Roese #define MVNETA_TX_MTU                            0x3e0c
21619fc2eaeSStefan Roese #define MVNETA_TX_TOKEN_SIZE                     0x3e14
21719fc2eaeSStefan Roese #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
21819fc2eaeSStefan Roese #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
21919fc2eaeSStefan Roese #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
22019fc2eaeSStefan Roese 
22119fc2eaeSStefan Roese /* Descriptor ring Macros */
22219fc2eaeSStefan Roese #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
22319fc2eaeSStefan Roese 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
22419fc2eaeSStefan Roese 
22519fc2eaeSStefan Roese /* Various constants */
22619fc2eaeSStefan Roese 
22719fc2eaeSStefan Roese /* Coalescing */
22819fc2eaeSStefan Roese #define MVNETA_TXDONE_COAL_PKTS		16
22919fc2eaeSStefan Roese #define MVNETA_RX_COAL_PKTS		32
23019fc2eaeSStefan Roese #define MVNETA_RX_COAL_USEC		100
23119fc2eaeSStefan Roese 
23219fc2eaeSStefan Roese /* The two bytes Marvell header. Either contains a special value used
23319fc2eaeSStefan Roese  * by Marvell switches when a specific hardware mode is enabled (not
23419fc2eaeSStefan Roese  * supported by this driver) or is filled automatically by zeroes on
23519fc2eaeSStefan Roese  * the RX side. Those two bytes being at the front of the Ethernet
23619fc2eaeSStefan Roese  * header, they allow to have the IP header aligned on a 4 bytes
23719fc2eaeSStefan Roese  * boundary automatically: the hardware skips those two bytes on its
23819fc2eaeSStefan Roese  * own.
23919fc2eaeSStefan Roese  */
24019fc2eaeSStefan Roese #define MVNETA_MH_SIZE			2
24119fc2eaeSStefan Roese 
24219fc2eaeSStefan Roese #define MVNETA_VLAN_TAG_LEN             4
24319fc2eaeSStefan Roese 
24419fc2eaeSStefan Roese #define MVNETA_CPU_D_CACHE_LINE_SIZE    32
24519fc2eaeSStefan Roese #define MVNETA_TX_CSUM_MAX_SIZE		9800
24619fc2eaeSStefan Roese #define MVNETA_ACC_MODE_EXT		1
24719fc2eaeSStefan Roese 
24819fc2eaeSStefan Roese /* Timeout constants */
24919fc2eaeSStefan Roese #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
25019fc2eaeSStefan Roese #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
25119fc2eaeSStefan Roese #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
25219fc2eaeSStefan Roese 
25319fc2eaeSStefan Roese #define MVNETA_TX_MTU_MAX		0x3ffff
25419fc2eaeSStefan Roese 
25519fc2eaeSStefan Roese /* Max number of Rx descriptors */
25619fc2eaeSStefan Roese #define MVNETA_MAX_RXD 16
25719fc2eaeSStefan Roese 
25819fc2eaeSStefan Roese /* Max number of Tx descriptors */
25919fc2eaeSStefan Roese #define MVNETA_MAX_TXD 16
26019fc2eaeSStefan Roese 
26119fc2eaeSStefan Roese /* descriptor aligned size */
26219fc2eaeSStefan Roese #define MVNETA_DESC_ALIGNED_SIZE	32
26319fc2eaeSStefan Roese 
26419fc2eaeSStefan Roese struct mvneta_port {
26519fc2eaeSStefan Roese 	void __iomem *base;
26619fc2eaeSStefan Roese 	struct mvneta_rx_queue *rxqs;
26719fc2eaeSStefan Roese 	struct mvneta_tx_queue *txqs;
26819fc2eaeSStefan Roese 
26919fc2eaeSStefan Roese 	u8 mcast_count[256];
27019fc2eaeSStefan Roese 	u16 tx_ring_size;
27119fc2eaeSStefan Roese 	u16 rx_ring_size;
27219fc2eaeSStefan Roese 
27319fc2eaeSStefan Roese 	phy_interface_t phy_interface;
27419fc2eaeSStefan Roese 	unsigned int link;
27519fc2eaeSStefan Roese 	unsigned int duplex;
27619fc2eaeSStefan Roese 	unsigned int speed;
27719fc2eaeSStefan Roese 
27819fc2eaeSStefan Roese 	int init;
27919fc2eaeSStefan Roese 	int phyaddr;
28019fc2eaeSStefan Roese 	struct phy_device *phydev;
28119fc2eaeSStefan Roese 	struct mii_dev *bus;
28219fc2eaeSStefan Roese };
28319fc2eaeSStefan Roese 
28419fc2eaeSStefan Roese /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
28519fc2eaeSStefan Roese  * layout of the transmit and reception DMA descriptors, and their
28619fc2eaeSStefan Roese  * layout is therefore defined by the hardware design
28719fc2eaeSStefan Roese  */
28819fc2eaeSStefan Roese 
28919fc2eaeSStefan Roese #define MVNETA_TX_L3_OFF_SHIFT	0
29019fc2eaeSStefan Roese #define MVNETA_TX_IP_HLEN_SHIFT	8
29119fc2eaeSStefan Roese #define MVNETA_TX_L4_UDP	BIT(16)
29219fc2eaeSStefan Roese #define MVNETA_TX_L3_IP6	BIT(17)
29319fc2eaeSStefan Roese #define MVNETA_TXD_IP_CSUM	BIT(18)
29419fc2eaeSStefan Roese #define MVNETA_TXD_Z_PAD	BIT(19)
29519fc2eaeSStefan Roese #define MVNETA_TXD_L_DESC	BIT(20)
29619fc2eaeSStefan Roese #define MVNETA_TXD_F_DESC	BIT(21)
29719fc2eaeSStefan Roese #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
29819fc2eaeSStefan Roese 				 MVNETA_TXD_L_DESC | \
29919fc2eaeSStefan Roese 				 MVNETA_TXD_F_DESC)
30019fc2eaeSStefan Roese #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
30119fc2eaeSStefan Roese #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
30219fc2eaeSStefan Roese 
30319fc2eaeSStefan Roese #define MVNETA_RXD_ERR_CRC		0x0
30419fc2eaeSStefan Roese #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
30519fc2eaeSStefan Roese #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
30619fc2eaeSStefan Roese #define MVNETA_RXD_ERR_LEN		BIT(18)
30719fc2eaeSStefan Roese #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
30819fc2eaeSStefan Roese #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
30919fc2eaeSStefan Roese #define MVNETA_RXD_L3_IP4		BIT(25)
31019fc2eaeSStefan Roese #define MVNETA_RXD_FIRST_LAST_DESC	(BIT(26) | BIT(27))
31119fc2eaeSStefan Roese #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
31219fc2eaeSStefan Roese 
31319fc2eaeSStefan Roese struct mvneta_tx_desc {
31419fc2eaeSStefan Roese 	u32  command;		/* Options used by HW for packet transmitting.*/
31519fc2eaeSStefan Roese 	u16  reserverd1;	/* csum_l4 (for future use)		*/
31619fc2eaeSStefan Roese 	u16  data_size;		/* Data size of transmitted packet in bytes */
31719fc2eaeSStefan Roese 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
31819fc2eaeSStefan Roese 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
31919fc2eaeSStefan Roese 	u32  reserved3[4];	/* Reserved - (for future use)		*/
32019fc2eaeSStefan Roese };
32119fc2eaeSStefan Roese 
32219fc2eaeSStefan Roese struct mvneta_rx_desc {
32319fc2eaeSStefan Roese 	u32  status;		/* Info about received packet		*/
32419fc2eaeSStefan Roese 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
32519fc2eaeSStefan Roese 	u16  data_size;		/* Size of received packet in bytes	*/
32619fc2eaeSStefan Roese 
32719fc2eaeSStefan Roese 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
32819fc2eaeSStefan Roese 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
32919fc2eaeSStefan Roese 
33019fc2eaeSStefan Roese 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
33119fc2eaeSStefan Roese 	u16  reserved3;		/* prefetch_cmd, for future use		*/
33219fc2eaeSStefan Roese 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
33319fc2eaeSStefan Roese 
33419fc2eaeSStefan Roese 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
33519fc2eaeSStefan Roese 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
33619fc2eaeSStefan Roese };
33719fc2eaeSStefan Roese 
33819fc2eaeSStefan Roese struct mvneta_tx_queue {
33919fc2eaeSStefan Roese 	/* Number of this TX queue, in the range 0-7 */
34019fc2eaeSStefan Roese 	u8 id;
34119fc2eaeSStefan Roese 
34219fc2eaeSStefan Roese 	/* Number of TX DMA descriptors in the descriptor ring */
34319fc2eaeSStefan Roese 	int size;
34419fc2eaeSStefan Roese 
34519fc2eaeSStefan Roese 	/* Index of last TX DMA descriptor that was inserted */
34619fc2eaeSStefan Roese 	int txq_put_index;
34719fc2eaeSStefan Roese 
34819fc2eaeSStefan Roese 	/* Index of the TX DMA descriptor to be cleaned up */
34919fc2eaeSStefan Roese 	int txq_get_index;
35019fc2eaeSStefan Roese 
35119fc2eaeSStefan Roese 	/* Virtual address of the TX DMA descriptors array */
35219fc2eaeSStefan Roese 	struct mvneta_tx_desc *descs;
35319fc2eaeSStefan Roese 
35419fc2eaeSStefan Roese 	/* DMA address of the TX DMA descriptors array */
35519fc2eaeSStefan Roese 	dma_addr_t descs_phys;
35619fc2eaeSStefan Roese 
35719fc2eaeSStefan Roese 	/* Index of the last TX DMA descriptor */
35819fc2eaeSStefan Roese 	int last_desc;
35919fc2eaeSStefan Roese 
36019fc2eaeSStefan Roese 	/* Index of the next TX DMA descriptor to process */
36119fc2eaeSStefan Roese 	int next_desc_to_proc;
36219fc2eaeSStefan Roese };
36319fc2eaeSStefan Roese 
36419fc2eaeSStefan Roese struct mvneta_rx_queue {
36519fc2eaeSStefan Roese 	/* rx queue number, in the range 0-7 */
36619fc2eaeSStefan Roese 	u8 id;
36719fc2eaeSStefan Roese 
36819fc2eaeSStefan Roese 	/* num of rx descriptors in the rx descriptor ring */
36919fc2eaeSStefan Roese 	int size;
37019fc2eaeSStefan Roese 
37119fc2eaeSStefan Roese 	/* Virtual address of the RX DMA descriptors array */
37219fc2eaeSStefan Roese 	struct mvneta_rx_desc *descs;
37319fc2eaeSStefan Roese 
37419fc2eaeSStefan Roese 	/* DMA address of the RX DMA descriptors array */
37519fc2eaeSStefan Roese 	dma_addr_t descs_phys;
37619fc2eaeSStefan Roese 
37719fc2eaeSStefan Roese 	/* Index of the last RX DMA descriptor */
37819fc2eaeSStefan Roese 	int last_desc;
37919fc2eaeSStefan Roese 
38019fc2eaeSStefan Roese 	/* Index of the next RX DMA descriptor to process */
38119fc2eaeSStefan Roese 	int next_desc_to_proc;
38219fc2eaeSStefan Roese };
38319fc2eaeSStefan Roese 
38419fc2eaeSStefan Roese /* U-Boot doesn't use the queues, so set the number to 1 */
38519fc2eaeSStefan Roese static int rxq_number = 1;
38619fc2eaeSStefan Roese static int txq_number = 1;
38719fc2eaeSStefan Roese static int rxq_def;
38819fc2eaeSStefan Roese 
38919fc2eaeSStefan Roese struct buffer_location {
39019fc2eaeSStefan Roese 	struct mvneta_tx_desc *tx_descs;
39119fc2eaeSStefan Roese 	struct mvneta_rx_desc *rx_descs;
39219fc2eaeSStefan Roese 	u32 rx_buffers;
39319fc2eaeSStefan Roese };
39419fc2eaeSStefan Roese 
39519fc2eaeSStefan Roese /*
39619fc2eaeSStefan Roese  * All 4 interfaces use the same global buffer, since only one interface
39719fc2eaeSStefan Roese  * can be enabled at once
39819fc2eaeSStefan Roese  */
39919fc2eaeSStefan Roese static struct buffer_location buffer_loc;
40019fc2eaeSStefan Roese 
40119fc2eaeSStefan Roese /*
40219fc2eaeSStefan Roese  * Page table entries are set to 1MB, or multiples of 1MB
40319fc2eaeSStefan Roese  * (not < 1MB). driver uses less bd's so use 1MB bdspace.
40419fc2eaeSStefan Roese  */
40519fc2eaeSStefan Roese #define BD_SPACE	(1 << 20)
40619fc2eaeSStefan Roese 
40719fc2eaeSStefan Roese /* Utility/helper methods */
40819fc2eaeSStefan Roese 
40919fc2eaeSStefan Roese /* Write helper method */
41019fc2eaeSStefan Roese static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
41119fc2eaeSStefan Roese {
41219fc2eaeSStefan Roese 	writel(data, pp->base + offset);
41319fc2eaeSStefan Roese }
41419fc2eaeSStefan Roese 
41519fc2eaeSStefan Roese /* Read helper method */
41619fc2eaeSStefan Roese static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
41719fc2eaeSStefan Roese {
41819fc2eaeSStefan Roese 	return readl(pp->base + offset);
41919fc2eaeSStefan Roese }
42019fc2eaeSStefan Roese 
42119fc2eaeSStefan Roese /* Clear all MIB counters */
42219fc2eaeSStefan Roese static void mvneta_mib_counters_clear(struct mvneta_port *pp)
42319fc2eaeSStefan Roese {
42419fc2eaeSStefan Roese 	int i;
42519fc2eaeSStefan Roese 
42619fc2eaeSStefan Roese 	/* Perform dummy reads from MIB counters */
42719fc2eaeSStefan Roese 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
42819fc2eaeSStefan Roese 		mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
42919fc2eaeSStefan Roese }
43019fc2eaeSStefan Roese 
43119fc2eaeSStefan Roese /* Rx descriptors helper methods */
43219fc2eaeSStefan Roese 
43319fc2eaeSStefan Roese /* Checks whether the RX descriptor having this status is both the first
43419fc2eaeSStefan Roese  * and the last descriptor for the RX packet. Each RX packet is currently
43519fc2eaeSStefan Roese  * received through a single RX descriptor, so not having each RX
43619fc2eaeSStefan Roese  * descriptor with its first and last bits set is an error
43719fc2eaeSStefan Roese  */
43819fc2eaeSStefan Roese static int mvneta_rxq_desc_is_first_last(u32 status)
43919fc2eaeSStefan Roese {
44019fc2eaeSStefan Roese 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
44119fc2eaeSStefan Roese 		MVNETA_RXD_FIRST_LAST_DESC;
44219fc2eaeSStefan Roese }
44319fc2eaeSStefan Roese 
44419fc2eaeSStefan Roese /* Add number of descriptors ready to receive new packets */
44519fc2eaeSStefan Roese static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
44619fc2eaeSStefan Roese 					  struct mvneta_rx_queue *rxq,
44719fc2eaeSStefan Roese 					  int ndescs)
44819fc2eaeSStefan Roese {
44919fc2eaeSStefan Roese 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
45019fc2eaeSStefan Roese 	 * be added at once
45119fc2eaeSStefan Roese 	 */
45219fc2eaeSStefan Roese 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
45319fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
45419fc2eaeSStefan Roese 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
45519fc2eaeSStefan Roese 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
45619fc2eaeSStefan Roese 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
45719fc2eaeSStefan Roese 	}
45819fc2eaeSStefan Roese 
45919fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
46019fc2eaeSStefan Roese 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
46119fc2eaeSStefan Roese }
46219fc2eaeSStefan Roese 
46319fc2eaeSStefan Roese /* Get number of RX descriptors occupied by received packets */
46419fc2eaeSStefan Roese static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
46519fc2eaeSStefan Roese 					struct mvneta_rx_queue *rxq)
46619fc2eaeSStefan Roese {
46719fc2eaeSStefan Roese 	u32 val;
46819fc2eaeSStefan Roese 
46919fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
47019fc2eaeSStefan Roese 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
47119fc2eaeSStefan Roese }
47219fc2eaeSStefan Roese 
47319fc2eaeSStefan Roese /* Update num of rx desc called upon return from rx path or
47419fc2eaeSStefan Roese  * from mvneta_rxq_drop_pkts().
47519fc2eaeSStefan Roese  */
47619fc2eaeSStefan Roese static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
47719fc2eaeSStefan Roese 				       struct mvneta_rx_queue *rxq,
47819fc2eaeSStefan Roese 				       int rx_done, int rx_filled)
47919fc2eaeSStefan Roese {
48019fc2eaeSStefan Roese 	u32 val;
48119fc2eaeSStefan Roese 
48219fc2eaeSStefan Roese 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
48319fc2eaeSStefan Roese 		val = rx_done |
48419fc2eaeSStefan Roese 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
48519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
48619fc2eaeSStefan Roese 		return;
48719fc2eaeSStefan Roese 	}
48819fc2eaeSStefan Roese 
48919fc2eaeSStefan Roese 	/* Only 255 descriptors can be added at once */
49019fc2eaeSStefan Roese 	while ((rx_done > 0) || (rx_filled > 0)) {
49119fc2eaeSStefan Roese 		if (rx_done <= 0xff) {
49219fc2eaeSStefan Roese 			val = rx_done;
49319fc2eaeSStefan Roese 			rx_done = 0;
49419fc2eaeSStefan Roese 		} else {
49519fc2eaeSStefan Roese 			val = 0xff;
49619fc2eaeSStefan Roese 			rx_done -= 0xff;
49719fc2eaeSStefan Roese 		}
49819fc2eaeSStefan Roese 		if (rx_filled <= 0xff) {
49919fc2eaeSStefan Roese 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
50019fc2eaeSStefan Roese 			rx_filled = 0;
50119fc2eaeSStefan Roese 		} else {
50219fc2eaeSStefan Roese 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
50319fc2eaeSStefan Roese 			rx_filled -= 0xff;
50419fc2eaeSStefan Roese 		}
50519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
50619fc2eaeSStefan Roese 	}
50719fc2eaeSStefan Roese }
50819fc2eaeSStefan Roese 
50919fc2eaeSStefan Roese /* Get pointer to next RX descriptor to be processed by SW */
51019fc2eaeSStefan Roese static struct mvneta_rx_desc *
51119fc2eaeSStefan Roese mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
51219fc2eaeSStefan Roese {
51319fc2eaeSStefan Roese 	int rx_desc = rxq->next_desc_to_proc;
51419fc2eaeSStefan Roese 
51519fc2eaeSStefan Roese 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
51619fc2eaeSStefan Roese 	return rxq->descs + rx_desc;
51719fc2eaeSStefan Roese }
51819fc2eaeSStefan Roese 
51919fc2eaeSStefan Roese /* Tx descriptors helper methods */
52019fc2eaeSStefan Roese 
52119fc2eaeSStefan Roese /* Update HW with number of TX descriptors to be sent */
52219fc2eaeSStefan Roese static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
52319fc2eaeSStefan Roese 				     struct mvneta_tx_queue *txq,
52419fc2eaeSStefan Roese 				     int pend_desc)
52519fc2eaeSStefan Roese {
52619fc2eaeSStefan Roese 	u32 val;
52719fc2eaeSStefan Roese 
52819fc2eaeSStefan Roese 	/* Only 255 descriptors can be added at once ; Assume caller
52919fc2eaeSStefan Roese 	 * process TX desriptors in quanta less than 256
53019fc2eaeSStefan Roese 	 */
53119fc2eaeSStefan Roese 	val = pend_desc;
53219fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
53319fc2eaeSStefan Roese }
53419fc2eaeSStefan Roese 
53519fc2eaeSStefan Roese /* Get pointer to next TX descriptor to be processed (send) by HW */
53619fc2eaeSStefan Roese static struct mvneta_tx_desc *
53719fc2eaeSStefan Roese mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
53819fc2eaeSStefan Roese {
53919fc2eaeSStefan Roese 	int tx_desc = txq->next_desc_to_proc;
54019fc2eaeSStefan Roese 
54119fc2eaeSStefan Roese 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
54219fc2eaeSStefan Roese 	return txq->descs + tx_desc;
54319fc2eaeSStefan Roese }
54419fc2eaeSStefan Roese 
54519fc2eaeSStefan Roese /* Set rxq buf size */
54619fc2eaeSStefan Roese static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
54719fc2eaeSStefan Roese 				    struct mvneta_rx_queue *rxq,
54819fc2eaeSStefan Roese 				    int buf_size)
54919fc2eaeSStefan Roese {
55019fc2eaeSStefan Roese 	u32 val;
55119fc2eaeSStefan Roese 
55219fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
55319fc2eaeSStefan Roese 
55419fc2eaeSStefan Roese 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
55519fc2eaeSStefan Roese 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
55619fc2eaeSStefan Roese 
55719fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
55819fc2eaeSStefan Roese }
55919fc2eaeSStefan Roese 
56019fc2eaeSStefan Roese /* Start the Ethernet port RX and TX activity */
56119fc2eaeSStefan Roese static void mvneta_port_up(struct mvneta_port *pp)
56219fc2eaeSStefan Roese {
56319fc2eaeSStefan Roese 	int queue;
56419fc2eaeSStefan Roese 	u32 q_map;
56519fc2eaeSStefan Roese 
56619fc2eaeSStefan Roese 	/* Enable all initialized TXs. */
56719fc2eaeSStefan Roese 	mvneta_mib_counters_clear(pp);
56819fc2eaeSStefan Roese 	q_map = 0;
56919fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
57019fc2eaeSStefan Roese 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
57119fc2eaeSStefan Roese 		if (txq->descs != NULL)
57219fc2eaeSStefan Roese 			q_map |= (1 << queue);
57319fc2eaeSStefan Roese 	}
57419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
57519fc2eaeSStefan Roese 
57619fc2eaeSStefan Roese 	/* Enable all initialized RXQs. */
57719fc2eaeSStefan Roese 	q_map = 0;
57819fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
57919fc2eaeSStefan Roese 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
58019fc2eaeSStefan Roese 		if (rxq->descs != NULL)
58119fc2eaeSStefan Roese 			q_map |= (1 << queue);
58219fc2eaeSStefan Roese 	}
58319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
58419fc2eaeSStefan Roese }
58519fc2eaeSStefan Roese 
58619fc2eaeSStefan Roese /* Stop the Ethernet port activity */
58719fc2eaeSStefan Roese static void mvneta_port_down(struct mvneta_port *pp)
58819fc2eaeSStefan Roese {
58919fc2eaeSStefan Roese 	u32 val;
59019fc2eaeSStefan Roese 	int count;
59119fc2eaeSStefan Roese 
59219fc2eaeSStefan Roese 	/* Stop Rx port activity. Check port Rx activity. */
59319fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
59419fc2eaeSStefan Roese 
59519fc2eaeSStefan Roese 	/* Issue stop command for active channels only */
59619fc2eaeSStefan Roese 	if (val != 0)
59719fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_CMD,
59819fc2eaeSStefan Roese 			    val << MVNETA_RXQ_DISABLE_SHIFT);
59919fc2eaeSStefan Roese 
60019fc2eaeSStefan Roese 	/* Wait for all Rx activity to terminate. */
60119fc2eaeSStefan Roese 	count = 0;
60219fc2eaeSStefan Roese 	do {
60319fc2eaeSStefan Roese 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
60419fc2eaeSStefan Roese 			netdev_warn(pp->dev,
60519fc2eaeSStefan Roese 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
60619fc2eaeSStefan Roese 				    val);
60719fc2eaeSStefan Roese 			break;
60819fc2eaeSStefan Roese 		}
60919fc2eaeSStefan Roese 		mdelay(1);
61019fc2eaeSStefan Roese 
61119fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
61219fc2eaeSStefan Roese 	} while (val & 0xff);
61319fc2eaeSStefan Roese 
61419fc2eaeSStefan Roese 	/* Stop Tx port activity. Check port Tx activity. Issue stop
61519fc2eaeSStefan Roese 	 * command for active channels only
61619fc2eaeSStefan Roese 	 */
61719fc2eaeSStefan Roese 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
61819fc2eaeSStefan Roese 
61919fc2eaeSStefan Roese 	if (val != 0)
62019fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_TXQ_CMD,
62119fc2eaeSStefan Roese 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
62219fc2eaeSStefan Roese 
62319fc2eaeSStefan Roese 	/* Wait for all Tx activity to terminate. */
62419fc2eaeSStefan Roese 	count = 0;
62519fc2eaeSStefan Roese 	do {
62619fc2eaeSStefan Roese 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
62719fc2eaeSStefan Roese 			netdev_warn(pp->dev,
62819fc2eaeSStefan Roese 				    "TIMEOUT for TX stopped status=0x%08x\n",
62919fc2eaeSStefan Roese 				    val);
63019fc2eaeSStefan Roese 			break;
63119fc2eaeSStefan Roese 		}
63219fc2eaeSStefan Roese 		mdelay(1);
63319fc2eaeSStefan Roese 
63419fc2eaeSStefan Roese 		/* Check TX Command reg that all Txqs are stopped */
63519fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
63619fc2eaeSStefan Roese 
63719fc2eaeSStefan Roese 	} while (val & 0xff);
63819fc2eaeSStefan Roese 
63919fc2eaeSStefan Roese 	/* Double check to verify that TX FIFO is empty */
64019fc2eaeSStefan Roese 	count = 0;
64119fc2eaeSStefan Roese 	do {
64219fc2eaeSStefan Roese 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
64319fc2eaeSStefan Roese 			netdev_warn(pp->dev,
64419fc2eaeSStefan Roese 				    "TX FIFO empty timeout status=0x08%x\n",
64519fc2eaeSStefan Roese 				    val);
64619fc2eaeSStefan Roese 			break;
64719fc2eaeSStefan Roese 		}
64819fc2eaeSStefan Roese 		mdelay(1);
64919fc2eaeSStefan Roese 
65019fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
65119fc2eaeSStefan Roese 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
65219fc2eaeSStefan Roese 		 (val & MVNETA_TX_IN_PRGRS));
65319fc2eaeSStefan Roese 
65419fc2eaeSStefan Roese 	udelay(200);
65519fc2eaeSStefan Roese }
65619fc2eaeSStefan Roese 
65719fc2eaeSStefan Roese /* Enable the port by setting the port enable bit of the MAC control register */
65819fc2eaeSStefan Roese static void mvneta_port_enable(struct mvneta_port *pp)
65919fc2eaeSStefan Roese {
66019fc2eaeSStefan Roese 	u32 val;
66119fc2eaeSStefan Roese 
66219fc2eaeSStefan Roese 	/* Enable port */
66319fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
66419fc2eaeSStefan Roese 	val |= MVNETA_GMAC0_PORT_ENABLE;
66519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
66619fc2eaeSStefan Roese }
66719fc2eaeSStefan Roese 
66819fc2eaeSStefan Roese /* Disable the port and wait for about 200 usec before retuning */
66919fc2eaeSStefan Roese static void mvneta_port_disable(struct mvneta_port *pp)
67019fc2eaeSStefan Roese {
67119fc2eaeSStefan Roese 	u32 val;
67219fc2eaeSStefan Roese 
67319fc2eaeSStefan Roese 	/* Reset the Enable bit in the Serial Control Register */
67419fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
67519fc2eaeSStefan Roese 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
67619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
67719fc2eaeSStefan Roese 
67819fc2eaeSStefan Roese 	udelay(200);
67919fc2eaeSStefan Roese }
68019fc2eaeSStefan Roese 
68119fc2eaeSStefan Roese /* Multicast tables methods */
68219fc2eaeSStefan Roese 
68319fc2eaeSStefan Roese /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
68419fc2eaeSStefan Roese static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
68519fc2eaeSStefan Roese {
68619fc2eaeSStefan Roese 	int offset;
68719fc2eaeSStefan Roese 	u32 val;
68819fc2eaeSStefan Roese 
68919fc2eaeSStefan Roese 	if (queue == -1) {
69019fc2eaeSStefan Roese 		val = 0;
69119fc2eaeSStefan Roese 	} else {
69219fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
69319fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
69419fc2eaeSStefan Roese 	}
69519fc2eaeSStefan Roese 
69619fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xc; offset += 4)
69719fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
69819fc2eaeSStefan Roese }
69919fc2eaeSStefan Roese 
70019fc2eaeSStefan Roese /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
70119fc2eaeSStefan Roese static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
70219fc2eaeSStefan Roese {
70319fc2eaeSStefan Roese 	int offset;
70419fc2eaeSStefan Roese 	u32 val;
70519fc2eaeSStefan Roese 
70619fc2eaeSStefan Roese 	if (queue == -1) {
70719fc2eaeSStefan Roese 		val = 0;
70819fc2eaeSStefan Roese 	} else {
70919fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
71019fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
71119fc2eaeSStefan Roese 	}
71219fc2eaeSStefan Roese 
71319fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xfc; offset += 4)
71419fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
71519fc2eaeSStefan Roese }
71619fc2eaeSStefan Roese 
71719fc2eaeSStefan Roese /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
71819fc2eaeSStefan Roese static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
71919fc2eaeSStefan Roese {
72019fc2eaeSStefan Roese 	int offset;
72119fc2eaeSStefan Roese 	u32 val;
72219fc2eaeSStefan Roese 
72319fc2eaeSStefan Roese 	if (queue == -1) {
72419fc2eaeSStefan Roese 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
72519fc2eaeSStefan Roese 		val = 0;
72619fc2eaeSStefan Roese 	} else {
72719fc2eaeSStefan Roese 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
72819fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
72919fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
73019fc2eaeSStefan Roese 	}
73119fc2eaeSStefan Roese 
73219fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xfc; offset += 4)
73319fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
73419fc2eaeSStefan Roese }
73519fc2eaeSStefan Roese 
73619fc2eaeSStefan Roese /* This method sets defaults to the NETA port:
73719fc2eaeSStefan Roese  *	Clears interrupt Cause and Mask registers.
73819fc2eaeSStefan Roese  *	Clears all MAC tables.
73919fc2eaeSStefan Roese  *	Sets defaults to all registers.
74019fc2eaeSStefan Roese  *	Resets RX and TX descriptor rings.
74119fc2eaeSStefan Roese  *	Resets PHY.
74219fc2eaeSStefan Roese  * This method can be called after mvneta_port_down() to return the port
74319fc2eaeSStefan Roese  *	settings to defaults.
74419fc2eaeSStefan Roese  */
74519fc2eaeSStefan Roese static void mvneta_defaults_set(struct mvneta_port *pp)
74619fc2eaeSStefan Roese {
74719fc2eaeSStefan Roese 	int cpu;
74819fc2eaeSStefan Roese 	int queue;
74919fc2eaeSStefan Roese 	u32 val;
75019fc2eaeSStefan Roese 
75119fc2eaeSStefan Roese 	/* Clear all Cause registers */
75219fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
75319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
75419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
75519fc2eaeSStefan Roese 
75619fc2eaeSStefan Roese 	/* Mask all interrupts */
75719fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
75819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
75919fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
76019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
76119fc2eaeSStefan Roese 
76219fc2eaeSStefan Roese 	/* Enable MBUS Retry bit16 */
76319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
76419fc2eaeSStefan Roese 
76519fc2eaeSStefan Roese 	/* Set CPU queue access map - all CPUs have access to all RX
76619fc2eaeSStefan Roese 	 * queues and to all TX queues
76719fc2eaeSStefan Roese 	 */
76819fc2eaeSStefan Roese 	for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
76919fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_CPU_MAP(cpu),
77019fc2eaeSStefan Roese 			    (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
77119fc2eaeSStefan Roese 			     MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
77219fc2eaeSStefan Roese 
77319fc2eaeSStefan Roese 	/* Reset RX and TX DMAs */
77419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
77519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
77619fc2eaeSStefan Roese 
77719fc2eaeSStefan Roese 	/* Disable Legacy WRR, Disable EJP, Release from reset */
77819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
77919fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
78019fc2eaeSStefan Roese 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
78119fc2eaeSStefan Roese 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
78219fc2eaeSStefan Roese 	}
78319fc2eaeSStefan Roese 
78419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
78519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
78619fc2eaeSStefan Roese 
78719fc2eaeSStefan Roese 	/* Set Port Acceleration Mode */
78819fc2eaeSStefan Roese 	val = MVNETA_ACC_MODE_EXT;
78919fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_ACC_MODE, val);
79019fc2eaeSStefan Roese 
79119fc2eaeSStefan Roese 	/* Update val of portCfg register accordingly with all RxQueue types */
79219fc2eaeSStefan Roese 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
79319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
79419fc2eaeSStefan Roese 
79519fc2eaeSStefan Roese 	val = 0;
79619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
79719fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
79819fc2eaeSStefan Roese 
79919fc2eaeSStefan Roese 	/* Build PORT_SDMA_CONFIG_REG */
80019fc2eaeSStefan Roese 	val = 0;
80119fc2eaeSStefan Roese 
80219fc2eaeSStefan Roese 	/* Default burst size */
80319fc2eaeSStefan Roese 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
80419fc2eaeSStefan Roese 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
80519fc2eaeSStefan Roese 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
80619fc2eaeSStefan Roese 
80719fc2eaeSStefan Roese 	/* Assign port SDMA configuration */
80819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
80919fc2eaeSStefan Roese 
81019fc2eaeSStefan Roese 	/* Enable PHY polling in hardware for U-Boot */
81119fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
81219fc2eaeSStefan Roese 	val |= MVNETA_PHY_POLLING_ENABLE;
81319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
81419fc2eaeSStefan Roese 
81519fc2eaeSStefan Roese 	mvneta_set_ucast_table(pp, -1);
81619fc2eaeSStefan Roese 	mvneta_set_special_mcast_table(pp, -1);
81719fc2eaeSStefan Roese 	mvneta_set_other_mcast_table(pp, -1);
81819fc2eaeSStefan Roese }
81919fc2eaeSStefan Roese 
82019fc2eaeSStefan Roese /* Set unicast address */
82119fc2eaeSStefan Roese static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
82219fc2eaeSStefan Roese 				  int queue)
82319fc2eaeSStefan Roese {
82419fc2eaeSStefan Roese 	unsigned int unicast_reg;
82519fc2eaeSStefan Roese 	unsigned int tbl_offset;
82619fc2eaeSStefan Roese 	unsigned int reg_offset;
82719fc2eaeSStefan Roese 
82819fc2eaeSStefan Roese 	/* Locate the Unicast table entry */
82919fc2eaeSStefan Roese 	last_nibble = (0xf & last_nibble);
83019fc2eaeSStefan Roese 
83119fc2eaeSStefan Roese 	/* offset from unicast tbl base */
83219fc2eaeSStefan Roese 	tbl_offset = (last_nibble / 4) * 4;
83319fc2eaeSStefan Roese 
83419fc2eaeSStefan Roese 	/* offset within the above reg  */
83519fc2eaeSStefan Roese 	reg_offset = last_nibble % 4;
83619fc2eaeSStefan Roese 
83719fc2eaeSStefan Roese 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
83819fc2eaeSStefan Roese 
83919fc2eaeSStefan Roese 	if (queue == -1) {
84019fc2eaeSStefan Roese 		/* Clear accepts frame bit at specified unicast DA tbl entry */
84119fc2eaeSStefan Roese 		unicast_reg &= ~(0xff << (8 * reg_offset));
84219fc2eaeSStefan Roese 	} else {
84319fc2eaeSStefan Roese 		unicast_reg &= ~(0xff << (8 * reg_offset));
84419fc2eaeSStefan Roese 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
84519fc2eaeSStefan Roese 	}
84619fc2eaeSStefan Roese 
84719fc2eaeSStefan Roese 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
84819fc2eaeSStefan Roese }
84919fc2eaeSStefan Roese 
85019fc2eaeSStefan Roese /* Set mac address */
85119fc2eaeSStefan Roese static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
85219fc2eaeSStefan Roese 				int queue)
85319fc2eaeSStefan Roese {
85419fc2eaeSStefan Roese 	unsigned int mac_h;
85519fc2eaeSStefan Roese 	unsigned int mac_l;
85619fc2eaeSStefan Roese 
85719fc2eaeSStefan Roese 	if (queue != -1) {
85819fc2eaeSStefan Roese 		mac_l = (addr[4] << 8) | (addr[5]);
85919fc2eaeSStefan Roese 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
86019fc2eaeSStefan Roese 			(addr[2] << 8) | (addr[3] << 0);
86119fc2eaeSStefan Roese 
86219fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
86319fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
86419fc2eaeSStefan Roese 	}
86519fc2eaeSStefan Roese 
86619fc2eaeSStefan Roese 	/* Accept frames of this address */
86719fc2eaeSStefan Roese 	mvneta_set_ucast_addr(pp, addr[5], queue);
86819fc2eaeSStefan Roese }
86919fc2eaeSStefan Roese 
87019fc2eaeSStefan Roese /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
87119fc2eaeSStefan Roese static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
87219fc2eaeSStefan Roese 				u32 phys_addr, u32 cookie)
87319fc2eaeSStefan Roese {
87419fc2eaeSStefan Roese 	rx_desc->buf_cookie = cookie;
87519fc2eaeSStefan Roese 	rx_desc->buf_phys_addr = phys_addr;
87619fc2eaeSStefan Roese }
87719fc2eaeSStefan Roese 
87819fc2eaeSStefan Roese /* Decrement sent descriptors counter */
87919fc2eaeSStefan Roese static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
88019fc2eaeSStefan Roese 				     struct mvneta_tx_queue *txq,
88119fc2eaeSStefan Roese 				     int sent_desc)
88219fc2eaeSStefan Roese {
88319fc2eaeSStefan Roese 	u32 val;
88419fc2eaeSStefan Roese 
88519fc2eaeSStefan Roese 	/* Only 255 TX descriptors can be updated at once */
88619fc2eaeSStefan Roese 	while (sent_desc > 0xff) {
88719fc2eaeSStefan Roese 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
88819fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
88919fc2eaeSStefan Roese 		sent_desc = sent_desc - 0xff;
89019fc2eaeSStefan Roese 	}
89119fc2eaeSStefan Roese 
89219fc2eaeSStefan Roese 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
89319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
89419fc2eaeSStefan Roese }
89519fc2eaeSStefan Roese 
89619fc2eaeSStefan Roese /* Get number of TX descriptors already sent by HW */
89719fc2eaeSStefan Roese static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
89819fc2eaeSStefan Roese 					struct mvneta_tx_queue *txq)
89919fc2eaeSStefan Roese {
90019fc2eaeSStefan Roese 	u32 val;
90119fc2eaeSStefan Roese 	int sent_desc;
90219fc2eaeSStefan Roese 
90319fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
90419fc2eaeSStefan Roese 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
90519fc2eaeSStefan Roese 		MVNETA_TXQ_SENT_DESC_SHIFT;
90619fc2eaeSStefan Roese 
90719fc2eaeSStefan Roese 	return sent_desc;
90819fc2eaeSStefan Roese }
90919fc2eaeSStefan Roese 
91019fc2eaeSStefan Roese /* Display more error info */
91119fc2eaeSStefan Roese static void mvneta_rx_error(struct mvneta_port *pp,
91219fc2eaeSStefan Roese 			    struct mvneta_rx_desc *rx_desc)
91319fc2eaeSStefan Roese {
91419fc2eaeSStefan Roese 	u32 status = rx_desc->status;
91519fc2eaeSStefan Roese 
91619fc2eaeSStefan Roese 	if (!mvneta_rxq_desc_is_first_last(status)) {
91719fc2eaeSStefan Roese 		netdev_err(pp->dev,
91819fc2eaeSStefan Roese 			   "bad rx status %08x (buffer oversize), size=%d\n",
91919fc2eaeSStefan Roese 			   status, rx_desc->data_size);
92019fc2eaeSStefan Roese 		return;
92119fc2eaeSStefan Roese 	}
92219fc2eaeSStefan Roese 
92319fc2eaeSStefan Roese 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
92419fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_CRC:
92519fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
92619fc2eaeSStefan Roese 			   status, rx_desc->data_size);
92719fc2eaeSStefan Roese 		break;
92819fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_OVERRUN:
92919fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
93019fc2eaeSStefan Roese 			   status, rx_desc->data_size);
93119fc2eaeSStefan Roese 		break;
93219fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_LEN:
93319fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
93419fc2eaeSStefan Roese 			   status, rx_desc->data_size);
93519fc2eaeSStefan Roese 		break;
93619fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_RESOURCE:
93719fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
93819fc2eaeSStefan Roese 			   status, rx_desc->data_size);
93919fc2eaeSStefan Roese 		break;
94019fc2eaeSStefan Roese 	}
94119fc2eaeSStefan Roese }
94219fc2eaeSStefan Roese 
94319fc2eaeSStefan Roese static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
94419fc2eaeSStefan Roese 						     int rxq)
94519fc2eaeSStefan Roese {
94619fc2eaeSStefan Roese 	return &pp->rxqs[rxq];
94719fc2eaeSStefan Roese }
94819fc2eaeSStefan Roese 
94919fc2eaeSStefan Roese 
95019fc2eaeSStefan Roese /* Drop packets received by the RXQ and free buffers */
95119fc2eaeSStefan Roese static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
95219fc2eaeSStefan Roese 				 struct mvneta_rx_queue *rxq)
95319fc2eaeSStefan Roese {
95419fc2eaeSStefan Roese 	int rx_done;
95519fc2eaeSStefan Roese 
95619fc2eaeSStefan Roese 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
95719fc2eaeSStefan Roese 	if (rx_done)
95819fc2eaeSStefan Roese 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
95919fc2eaeSStefan Roese }
96019fc2eaeSStefan Roese 
96119fc2eaeSStefan Roese /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
96219fc2eaeSStefan Roese static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
96319fc2eaeSStefan Roese 			   int num)
96419fc2eaeSStefan Roese {
96519fc2eaeSStefan Roese 	int i;
96619fc2eaeSStefan Roese 
96719fc2eaeSStefan Roese 	for (i = 0; i < num; i++) {
96819fc2eaeSStefan Roese 		u32 addr;
96919fc2eaeSStefan Roese 
97019fc2eaeSStefan Roese 		/* U-Boot special: Fill in the rx buffer addresses */
97119fc2eaeSStefan Roese 		addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
97219fc2eaeSStefan Roese 		mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
97319fc2eaeSStefan Roese 	}
97419fc2eaeSStefan Roese 
97519fc2eaeSStefan Roese 	/* Add this number of RX descriptors as non occupied (ready to
97619fc2eaeSStefan Roese 	 * get packets)
97719fc2eaeSStefan Roese 	 */
97819fc2eaeSStefan Roese 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
97919fc2eaeSStefan Roese 
98019fc2eaeSStefan Roese 	return 0;
98119fc2eaeSStefan Roese }
98219fc2eaeSStefan Roese 
98319fc2eaeSStefan Roese /* Rx/Tx queue initialization/cleanup methods */
98419fc2eaeSStefan Roese 
98519fc2eaeSStefan Roese /* Create a specified RX queue */
98619fc2eaeSStefan Roese static int mvneta_rxq_init(struct mvneta_port *pp,
98719fc2eaeSStefan Roese 			   struct mvneta_rx_queue *rxq)
98819fc2eaeSStefan Roese 
98919fc2eaeSStefan Roese {
99019fc2eaeSStefan Roese 	rxq->size = pp->rx_ring_size;
99119fc2eaeSStefan Roese 
99219fc2eaeSStefan Roese 	/* Allocate memory for RX descriptors */
99319fc2eaeSStefan Roese 	rxq->descs_phys = (dma_addr_t)rxq->descs;
99419fc2eaeSStefan Roese 	if (rxq->descs == NULL)
99519fc2eaeSStefan Roese 		return -ENOMEM;
99619fc2eaeSStefan Roese 
99719fc2eaeSStefan Roese 	rxq->last_desc = rxq->size - 1;
99819fc2eaeSStefan Roese 
99919fc2eaeSStefan Roese 	/* Set Rx descriptors queue starting address */
100019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
100119fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
100219fc2eaeSStefan Roese 
100319fc2eaeSStefan Roese 	/* Fill RXQ with buffers from RX pool */
100419fc2eaeSStefan Roese 	mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
100519fc2eaeSStefan Roese 	mvneta_rxq_fill(pp, rxq, rxq->size);
100619fc2eaeSStefan Roese 
100719fc2eaeSStefan Roese 	return 0;
100819fc2eaeSStefan Roese }
100919fc2eaeSStefan Roese 
101019fc2eaeSStefan Roese /* Cleanup Rx queue */
101119fc2eaeSStefan Roese static void mvneta_rxq_deinit(struct mvneta_port *pp,
101219fc2eaeSStefan Roese 			      struct mvneta_rx_queue *rxq)
101319fc2eaeSStefan Roese {
101419fc2eaeSStefan Roese 	mvneta_rxq_drop_pkts(pp, rxq);
101519fc2eaeSStefan Roese 
101619fc2eaeSStefan Roese 	rxq->descs             = NULL;
101719fc2eaeSStefan Roese 	rxq->last_desc         = 0;
101819fc2eaeSStefan Roese 	rxq->next_desc_to_proc = 0;
101919fc2eaeSStefan Roese 	rxq->descs_phys        = 0;
102019fc2eaeSStefan Roese }
102119fc2eaeSStefan Roese 
102219fc2eaeSStefan Roese /* Create and initialize a tx queue */
102319fc2eaeSStefan Roese static int mvneta_txq_init(struct mvneta_port *pp,
102419fc2eaeSStefan Roese 			   struct mvneta_tx_queue *txq)
102519fc2eaeSStefan Roese {
102619fc2eaeSStefan Roese 	txq->size = pp->tx_ring_size;
102719fc2eaeSStefan Roese 
102819fc2eaeSStefan Roese 	/* Allocate memory for TX descriptors */
10293cbc11daSStefan Roese 	txq->descs_phys = (dma_addr_t)txq->descs;
103019fc2eaeSStefan Roese 	if (txq->descs == NULL)
103119fc2eaeSStefan Roese 		return -ENOMEM;
103219fc2eaeSStefan Roese 
103319fc2eaeSStefan Roese 	txq->last_desc = txq->size - 1;
103419fc2eaeSStefan Roese 
103519fc2eaeSStefan Roese 	/* Set maximum bandwidth for enabled TXQs */
103619fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
103719fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
103819fc2eaeSStefan Roese 
103919fc2eaeSStefan Roese 	/* Set Tx descriptors queue starting address */
104019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
104119fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
104219fc2eaeSStefan Roese 
104319fc2eaeSStefan Roese 	return 0;
104419fc2eaeSStefan Roese }
104519fc2eaeSStefan Roese 
104619fc2eaeSStefan Roese /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
104719fc2eaeSStefan Roese static void mvneta_txq_deinit(struct mvneta_port *pp,
104819fc2eaeSStefan Roese 			      struct mvneta_tx_queue *txq)
104919fc2eaeSStefan Roese {
105019fc2eaeSStefan Roese 	txq->descs             = NULL;
105119fc2eaeSStefan Roese 	txq->last_desc         = 0;
105219fc2eaeSStefan Roese 	txq->next_desc_to_proc = 0;
105319fc2eaeSStefan Roese 	txq->descs_phys        = 0;
105419fc2eaeSStefan Roese 
105519fc2eaeSStefan Roese 	/* Set minimum bandwidth for disabled TXQs */
105619fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
105719fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
105819fc2eaeSStefan Roese 
105919fc2eaeSStefan Roese 	/* Set Tx descriptors queue starting address and size */
106019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
106119fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
106219fc2eaeSStefan Roese }
106319fc2eaeSStefan Roese 
106419fc2eaeSStefan Roese /* Cleanup all Tx queues */
106519fc2eaeSStefan Roese static void mvneta_cleanup_txqs(struct mvneta_port *pp)
106619fc2eaeSStefan Roese {
106719fc2eaeSStefan Roese 	int queue;
106819fc2eaeSStefan Roese 
106919fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++)
107019fc2eaeSStefan Roese 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
107119fc2eaeSStefan Roese }
107219fc2eaeSStefan Roese 
107319fc2eaeSStefan Roese /* Cleanup all Rx queues */
107419fc2eaeSStefan Roese static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
107519fc2eaeSStefan Roese {
107619fc2eaeSStefan Roese 	int queue;
107719fc2eaeSStefan Roese 
107819fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++)
107919fc2eaeSStefan Roese 		mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
108019fc2eaeSStefan Roese }
108119fc2eaeSStefan Roese 
108219fc2eaeSStefan Roese 
108319fc2eaeSStefan Roese /* Init all Rx queues */
108419fc2eaeSStefan Roese static int mvneta_setup_rxqs(struct mvneta_port *pp)
108519fc2eaeSStefan Roese {
108619fc2eaeSStefan Roese 	int queue;
108719fc2eaeSStefan Roese 
108819fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
108919fc2eaeSStefan Roese 		int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
109019fc2eaeSStefan Roese 		if (err) {
109119fc2eaeSStefan Roese 			netdev_err(pp->dev, "%s: can't create rxq=%d\n",
109219fc2eaeSStefan Roese 				   __func__, queue);
109319fc2eaeSStefan Roese 			mvneta_cleanup_rxqs(pp);
109419fc2eaeSStefan Roese 			return err;
109519fc2eaeSStefan Roese 		}
109619fc2eaeSStefan Roese 	}
109719fc2eaeSStefan Roese 
109819fc2eaeSStefan Roese 	return 0;
109919fc2eaeSStefan Roese }
110019fc2eaeSStefan Roese 
110119fc2eaeSStefan Roese /* Init all tx queues */
110219fc2eaeSStefan Roese static int mvneta_setup_txqs(struct mvneta_port *pp)
110319fc2eaeSStefan Roese {
110419fc2eaeSStefan Roese 	int queue;
110519fc2eaeSStefan Roese 
110619fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
110719fc2eaeSStefan Roese 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
110819fc2eaeSStefan Roese 		if (err) {
110919fc2eaeSStefan Roese 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
111019fc2eaeSStefan Roese 				   __func__, queue);
111119fc2eaeSStefan Roese 			mvneta_cleanup_txqs(pp);
111219fc2eaeSStefan Roese 			return err;
111319fc2eaeSStefan Roese 		}
111419fc2eaeSStefan Roese 	}
111519fc2eaeSStefan Roese 
111619fc2eaeSStefan Roese 	return 0;
111719fc2eaeSStefan Roese }
111819fc2eaeSStefan Roese 
111919fc2eaeSStefan Roese static void mvneta_start_dev(struct mvneta_port *pp)
112019fc2eaeSStefan Roese {
112119fc2eaeSStefan Roese 	/* start the Rx/Tx activity */
112219fc2eaeSStefan Roese 	mvneta_port_enable(pp);
112319fc2eaeSStefan Roese }
112419fc2eaeSStefan Roese 
1125e3b9c98aSStefan Roese static void mvneta_adjust_link(struct udevice *dev)
112619fc2eaeSStefan Roese {
1127e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
112819fc2eaeSStefan Roese 	struct phy_device *phydev = pp->phydev;
112919fc2eaeSStefan Roese 	int status_change = 0;
113019fc2eaeSStefan Roese 
113119fc2eaeSStefan Roese 	if (phydev->link) {
113219fc2eaeSStefan Roese 		if ((pp->speed != phydev->speed) ||
113319fc2eaeSStefan Roese 		    (pp->duplex != phydev->duplex)) {
113419fc2eaeSStefan Roese 			u32 val;
113519fc2eaeSStefan Roese 
113619fc2eaeSStefan Roese 			val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
113719fc2eaeSStefan Roese 			val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
113819fc2eaeSStefan Roese 				 MVNETA_GMAC_CONFIG_GMII_SPEED |
113919fc2eaeSStefan Roese 				 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
114019fc2eaeSStefan Roese 				 MVNETA_GMAC_AN_SPEED_EN |
114119fc2eaeSStefan Roese 				 MVNETA_GMAC_AN_DUPLEX_EN);
114219fc2eaeSStefan Roese 
114319fc2eaeSStefan Roese 			if (phydev->duplex)
114419fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
114519fc2eaeSStefan Roese 
114619fc2eaeSStefan Roese 			if (phydev->speed == SPEED_1000)
114719fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
114819fc2eaeSStefan Roese 			else
114919fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
115019fc2eaeSStefan Roese 
115119fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
115219fc2eaeSStefan Roese 
115319fc2eaeSStefan Roese 			pp->duplex = phydev->duplex;
115419fc2eaeSStefan Roese 			pp->speed  = phydev->speed;
115519fc2eaeSStefan Roese 		}
115619fc2eaeSStefan Roese 	}
115719fc2eaeSStefan Roese 
115819fc2eaeSStefan Roese 	if (phydev->link != pp->link) {
115919fc2eaeSStefan Roese 		if (!phydev->link) {
116019fc2eaeSStefan Roese 			pp->duplex = -1;
116119fc2eaeSStefan Roese 			pp->speed = 0;
116219fc2eaeSStefan Roese 		}
116319fc2eaeSStefan Roese 
116419fc2eaeSStefan Roese 		pp->link = phydev->link;
116519fc2eaeSStefan Roese 		status_change = 1;
116619fc2eaeSStefan Roese 	}
116719fc2eaeSStefan Roese 
116819fc2eaeSStefan Roese 	if (status_change) {
116919fc2eaeSStefan Roese 		if (phydev->link) {
117019fc2eaeSStefan Roese 			u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
117119fc2eaeSStefan Roese 			val |= (MVNETA_GMAC_FORCE_LINK_PASS |
117219fc2eaeSStefan Roese 				MVNETA_GMAC_FORCE_LINK_DOWN);
117319fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
117419fc2eaeSStefan Roese 			mvneta_port_up(pp);
117519fc2eaeSStefan Roese 		} else {
117619fc2eaeSStefan Roese 			mvneta_port_down(pp);
117719fc2eaeSStefan Roese 		}
117819fc2eaeSStefan Roese 	}
117919fc2eaeSStefan Roese }
118019fc2eaeSStefan Roese 
1181e3b9c98aSStefan Roese static int mvneta_open(struct udevice *dev)
118219fc2eaeSStefan Roese {
1183e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
118419fc2eaeSStefan Roese 	int ret;
118519fc2eaeSStefan Roese 
118619fc2eaeSStefan Roese 	ret = mvneta_setup_rxqs(pp);
118719fc2eaeSStefan Roese 	if (ret)
118819fc2eaeSStefan Roese 		return ret;
118919fc2eaeSStefan Roese 
119019fc2eaeSStefan Roese 	ret = mvneta_setup_txqs(pp);
119119fc2eaeSStefan Roese 	if (ret)
119219fc2eaeSStefan Roese 		return ret;
119319fc2eaeSStefan Roese 
119419fc2eaeSStefan Roese 	mvneta_adjust_link(dev);
119519fc2eaeSStefan Roese 
119619fc2eaeSStefan Roese 	mvneta_start_dev(pp);
119719fc2eaeSStefan Roese 
119819fc2eaeSStefan Roese 	return 0;
119919fc2eaeSStefan Roese }
120019fc2eaeSStefan Roese 
120119fc2eaeSStefan Roese /* Initialize hw */
1202e3b9c98aSStefan Roese static int mvneta_init2(struct mvneta_port *pp)
120319fc2eaeSStefan Roese {
120419fc2eaeSStefan Roese 	int queue;
120519fc2eaeSStefan Roese 
120619fc2eaeSStefan Roese 	/* Disable port */
120719fc2eaeSStefan Roese 	mvneta_port_disable(pp);
120819fc2eaeSStefan Roese 
120919fc2eaeSStefan Roese 	/* Set port default values */
121019fc2eaeSStefan Roese 	mvneta_defaults_set(pp);
121119fc2eaeSStefan Roese 
121219fc2eaeSStefan Roese 	pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
121319fc2eaeSStefan Roese 			   GFP_KERNEL);
121419fc2eaeSStefan Roese 	if (!pp->txqs)
121519fc2eaeSStefan Roese 		return -ENOMEM;
121619fc2eaeSStefan Roese 
121719fc2eaeSStefan Roese 	/* U-Boot special: use preallocated area */
121819fc2eaeSStefan Roese 	pp->txqs[0].descs = buffer_loc.tx_descs;
121919fc2eaeSStefan Roese 
122019fc2eaeSStefan Roese 	/* Initialize TX descriptor rings */
122119fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
122219fc2eaeSStefan Roese 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
122319fc2eaeSStefan Roese 		txq->id = queue;
122419fc2eaeSStefan Roese 		txq->size = pp->tx_ring_size;
122519fc2eaeSStefan Roese 	}
122619fc2eaeSStefan Roese 
122719fc2eaeSStefan Roese 	pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
122819fc2eaeSStefan Roese 			   GFP_KERNEL);
122919fc2eaeSStefan Roese 	if (!pp->rxqs) {
123019fc2eaeSStefan Roese 		kfree(pp->txqs);
123119fc2eaeSStefan Roese 		return -ENOMEM;
123219fc2eaeSStefan Roese 	}
123319fc2eaeSStefan Roese 
123419fc2eaeSStefan Roese 	/* U-Boot special: use preallocated area */
123519fc2eaeSStefan Roese 	pp->rxqs[0].descs = buffer_loc.rx_descs;
123619fc2eaeSStefan Roese 
123719fc2eaeSStefan Roese 	/* Create Rx descriptor rings */
123819fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
123919fc2eaeSStefan Roese 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
124019fc2eaeSStefan Roese 		rxq->id = queue;
124119fc2eaeSStefan Roese 		rxq->size = pp->rx_ring_size;
124219fc2eaeSStefan Roese 	}
124319fc2eaeSStefan Roese 
124419fc2eaeSStefan Roese 	return 0;
124519fc2eaeSStefan Roese }
124619fc2eaeSStefan Roese 
124719fc2eaeSStefan Roese /* platform glue : initialize decoding windows */
1248*544eefe0SStefan Roese 
1249*544eefe0SStefan Roese /*
1250*544eefe0SStefan Roese  * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1251*544eefe0SStefan Roese  * First layer is:  GbE Address window that resides inside the GBE unit,
1252*544eefe0SStefan Roese  * Second layer is: Fabric address window which is located in the NIC400
1253*544eefe0SStefan Roese  *                  (South Fabric).
1254*544eefe0SStefan Roese  * To simplify the address decode configuration for Armada3700, we bypass the
1255*544eefe0SStefan Roese  * first layer of GBE decode window by setting the first window to 4GB.
1256*544eefe0SStefan Roese  */
1257*544eefe0SStefan Roese static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1258*544eefe0SStefan Roese {
1259*544eefe0SStefan Roese 	/*
1260*544eefe0SStefan Roese 	 * Set window size to 4GB, to bypass GBE address decode, leave the
1261*544eefe0SStefan Roese 	 * work to MBUS decode window
1262*544eefe0SStefan Roese 	 */
1263*544eefe0SStefan Roese 	mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1264*544eefe0SStefan Roese 
1265*544eefe0SStefan Roese 	/* Enable GBE address decode window 0 by set bit 0 to 0 */
1266*544eefe0SStefan Roese 	clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1267*544eefe0SStefan Roese 		     MVNETA_BASE_ADDR_ENABLE_BIT);
1268*544eefe0SStefan Roese 
1269*544eefe0SStefan Roese 	/* Set GBE address decode window 0 to full Access (read or write) */
1270*544eefe0SStefan Roese 	setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1271*544eefe0SStefan Roese 		     MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1272*544eefe0SStefan Roese }
1273*544eefe0SStefan Roese 
127419fc2eaeSStefan Roese static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
127519fc2eaeSStefan Roese {
127619fc2eaeSStefan Roese 	const struct mbus_dram_target_info *dram;
127719fc2eaeSStefan Roese 	u32 win_enable;
127819fc2eaeSStefan Roese 	u32 win_protect;
127919fc2eaeSStefan Roese 	int i;
128019fc2eaeSStefan Roese 
128119fc2eaeSStefan Roese 	dram = mvebu_mbus_dram_info();
128219fc2eaeSStefan Roese 	for (i = 0; i < 6; i++) {
128319fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
128419fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
128519fc2eaeSStefan Roese 
128619fc2eaeSStefan Roese 		if (i < 4)
128719fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
128819fc2eaeSStefan Roese 	}
128919fc2eaeSStefan Roese 
129019fc2eaeSStefan Roese 	win_enable = 0x3f;
129119fc2eaeSStefan Roese 	win_protect = 0;
129219fc2eaeSStefan Roese 
129319fc2eaeSStefan Roese 	for (i = 0; i < dram->num_cs; i++) {
129419fc2eaeSStefan Roese 		const struct mbus_dram_window *cs = dram->cs + i;
129519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
129619fc2eaeSStefan Roese 			    (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
129719fc2eaeSStefan Roese 
129819fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_SIZE(i),
129919fc2eaeSStefan Roese 			    (cs->size - 1) & 0xffff0000);
130019fc2eaeSStefan Roese 
130119fc2eaeSStefan Roese 		win_enable &= ~(1 << i);
130219fc2eaeSStefan Roese 		win_protect |= 3 << (2 * i);
130319fc2eaeSStefan Roese 	}
130419fc2eaeSStefan Roese 
130519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
130619fc2eaeSStefan Roese }
130719fc2eaeSStefan Roese 
130819fc2eaeSStefan Roese /* Power up the port */
130919fc2eaeSStefan Roese static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
131019fc2eaeSStefan Roese {
131119fc2eaeSStefan Roese 	u32 ctrl;
131219fc2eaeSStefan Roese 
131319fc2eaeSStefan Roese 	/* MAC Cause register should be cleared */
131419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
131519fc2eaeSStefan Roese 
131619fc2eaeSStefan Roese 	ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
131719fc2eaeSStefan Roese 
131819fc2eaeSStefan Roese 	/* Even though it might look weird, when we're configured in
131919fc2eaeSStefan Roese 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
132019fc2eaeSStefan Roese 	 */
132119fc2eaeSStefan Roese 	switch (phy_mode) {
132219fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_QSGMII:
132319fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
132419fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
132519fc2eaeSStefan Roese 		break;
132619fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_SGMII:
132719fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
132819fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
132919fc2eaeSStefan Roese 		break;
133019fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_RGMII:
133119fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_RGMII_ID:
133219fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PORT_RGMII;
133319fc2eaeSStefan Roese 		break;
133419fc2eaeSStefan Roese 	default:
133519fc2eaeSStefan Roese 		return -EINVAL;
133619fc2eaeSStefan Roese 	}
133719fc2eaeSStefan Roese 
133819fc2eaeSStefan Roese 	/* Cancel Port Reset */
133919fc2eaeSStefan Roese 	ctrl &= ~MVNETA_GMAC2_PORT_RESET;
134019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
134119fc2eaeSStefan Roese 
134219fc2eaeSStefan Roese 	while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
134319fc2eaeSStefan Roese 		MVNETA_GMAC2_PORT_RESET) != 0)
134419fc2eaeSStefan Roese 		continue;
134519fc2eaeSStefan Roese 
134619fc2eaeSStefan Roese 	return 0;
134719fc2eaeSStefan Roese }
134819fc2eaeSStefan Roese 
134919fc2eaeSStefan Roese /* Device initialization routine */
1350e3b9c98aSStefan Roese static int mvneta_init(struct udevice *dev)
135119fc2eaeSStefan Roese {
1352e3b9c98aSStefan Roese 	struct eth_pdata *pdata = dev_get_platdata(dev);
1353e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
135419fc2eaeSStefan Roese 	int err;
135519fc2eaeSStefan Roese 
135619fc2eaeSStefan Roese 	pp->tx_ring_size = MVNETA_MAX_TXD;
135719fc2eaeSStefan Roese 	pp->rx_ring_size = MVNETA_MAX_RXD;
135819fc2eaeSStefan Roese 
1359e3b9c98aSStefan Roese 	err = mvneta_init2(pp);
136019fc2eaeSStefan Roese 	if (err < 0) {
136119fc2eaeSStefan Roese 		dev_err(&pdev->dev, "can't init eth hal\n");
136219fc2eaeSStefan Roese 		return err;
136319fc2eaeSStefan Roese 	}
136419fc2eaeSStefan Roese 
1365e3b9c98aSStefan Roese 	mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
136619fc2eaeSStefan Roese 
136719fc2eaeSStefan Roese 	err = mvneta_port_power_up(pp, pp->phy_interface);
136819fc2eaeSStefan Roese 	if (err < 0) {
136919fc2eaeSStefan Roese 		dev_err(&pdev->dev, "can't power up port\n");
137019fc2eaeSStefan Roese 		return err;
137119fc2eaeSStefan Roese 	}
137219fc2eaeSStefan Roese 
137319fc2eaeSStefan Roese 	/* Call open() now as it needs to be done before runing send() */
137419fc2eaeSStefan Roese 	mvneta_open(dev);
137519fc2eaeSStefan Roese 
137619fc2eaeSStefan Roese 	return 0;
137719fc2eaeSStefan Roese }
137819fc2eaeSStefan Roese 
137919fc2eaeSStefan Roese /* U-Boot only functions follow here */
138019fc2eaeSStefan Roese 
138119fc2eaeSStefan Roese /* SMI / MDIO functions */
138219fc2eaeSStefan Roese 
138319fc2eaeSStefan Roese static int smi_wait_ready(struct mvneta_port *pp)
138419fc2eaeSStefan Roese {
138519fc2eaeSStefan Roese 	u32 timeout = MVNETA_SMI_TIMEOUT;
138619fc2eaeSStefan Roese 	u32 smi_reg;
138719fc2eaeSStefan Roese 
138819fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
138919fc2eaeSStefan Roese 	do {
139019fc2eaeSStefan Roese 		/* read smi register */
139119fc2eaeSStefan Roese 		smi_reg = mvreg_read(pp, MVNETA_SMI);
139219fc2eaeSStefan Roese 		if (timeout-- == 0) {
139319fc2eaeSStefan Roese 			printf("Error: SMI busy timeout\n");
139419fc2eaeSStefan Roese 			return -EFAULT;
139519fc2eaeSStefan Roese 		}
139619fc2eaeSStefan Roese 	} while (smi_reg & MVNETA_SMI_BUSY);
139719fc2eaeSStefan Roese 
139819fc2eaeSStefan Roese 	return 0;
139919fc2eaeSStefan Roese }
140019fc2eaeSStefan Roese 
140119fc2eaeSStefan Roese /*
1402e3b9c98aSStefan Roese  * mvneta_mdio_read - miiphy_read callback function.
140319fc2eaeSStefan Roese  *
140419fc2eaeSStefan Roese  * Returns 16bit phy register value, or 0xffff on error
140519fc2eaeSStefan Roese  */
1406e3b9c98aSStefan Roese static int mvneta_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
140719fc2eaeSStefan Roese {
1408e3b9c98aSStefan Roese 	struct mvneta_port *pp = bus->priv;
140919fc2eaeSStefan Roese 	u32 smi_reg;
141019fc2eaeSStefan Roese 	u32 timeout;
141119fc2eaeSStefan Roese 
141219fc2eaeSStefan Roese 	/* check parameters */
1413e3b9c98aSStefan Roese 	if (addr > MVNETA_PHY_ADDR_MASK) {
1414e3b9c98aSStefan Roese 		printf("Error: Invalid PHY address %d\n", addr);
141519fc2eaeSStefan Roese 		return -EFAULT;
141619fc2eaeSStefan Roese 	}
141719fc2eaeSStefan Roese 
1418e3b9c98aSStefan Roese 	if (reg > MVNETA_PHY_REG_MASK) {
1419e3b9c98aSStefan Roese 		printf("Err: Invalid register offset %d\n", reg);
142019fc2eaeSStefan Roese 		return -EFAULT;
142119fc2eaeSStefan Roese 	}
142219fc2eaeSStefan Roese 
142319fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
142419fc2eaeSStefan Roese 	if (smi_wait_ready(pp) < 0)
142519fc2eaeSStefan Roese 		return -EFAULT;
142619fc2eaeSStefan Roese 
142719fc2eaeSStefan Roese 	/* fill the phy address and regiser offset and read opcode */
1428e3b9c98aSStefan Roese 	smi_reg = (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1429e3b9c98aSStefan Roese 		| (reg << MVNETA_SMI_REG_ADDR_OFFS)
143019fc2eaeSStefan Roese 		| MVNETA_SMI_OPCODE_READ;
143119fc2eaeSStefan Roese 
143219fc2eaeSStefan Roese 	/* write the smi register */
143319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SMI, smi_reg);
143419fc2eaeSStefan Roese 
143519fc2eaeSStefan Roese 	/* wait till read value is ready */
143619fc2eaeSStefan Roese 	timeout = MVNETA_SMI_TIMEOUT;
143719fc2eaeSStefan Roese 
143819fc2eaeSStefan Roese 	do {
143919fc2eaeSStefan Roese 		/* read smi register */
144019fc2eaeSStefan Roese 		smi_reg = mvreg_read(pp, MVNETA_SMI);
144119fc2eaeSStefan Roese 		if (timeout-- == 0) {
144219fc2eaeSStefan Roese 			printf("Err: SMI read ready timeout\n");
144319fc2eaeSStefan Roese 			return -EFAULT;
144419fc2eaeSStefan Roese 		}
144519fc2eaeSStefan Roese 	} while (!(smi_reg & MVNETA_SMI_READ_VALID));
144619fc2eaeSStefan Roese 
144719fc2eaeSStefan Roese 	/* Wait for the data to update in the SMI register */
144819fc2eaeSStefan Roese 	for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
144919fc2eaeSStefan Roese 		;
145019fc2eaeSStefan Roese 
1451e3b9c98aSStefan Roese 	return mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK;
145219fc2eaeSStefan Roese }
145319fc2eaeSStefan Roese 
145419fc2eaeSStefan Roese /*
1455e3b9c98aSStefan Roese  * mvneta_mdio_write - miiphy_write callback function.
145619fc2eaeSStefan Roese  *
145719fc2eaeSStefan Roese  * Returns 0 if write succeed, -EINVAL on bad parameters
145819fc2eaeSStefan Roese  * -ETIME on timeout
145919fc2eaeSStefan Roese  */
1460e3b9c98aSStefan Roese static int mvneta_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
1461e3b9c98aSStefan Roese 			     u16 value)
146219fc2eaeSStefan Roese {
1463e3b9c98aSStefan Roese 	struct mvneta_port *pp = bus->priv;
146419fc2eaeSStefan Roese 	u32 smi_reg;
146519fc2eaeSStefan Roese 
146619fc2eaeSStefan Roese 	/* check parameters */
1467e3b9c98aSStefan Roese 	if (addr > MVNETA_PHY_ADDR_MASK) {
1468e3b9c98aSStefan Roese 		printf("Error: Invalid PHY address %d\n", addr);
146919fc2eaeSStefan Roese 		return -EFAULT;
147019fc2eaeSStefan Roese 	}
147119fc2eaeSStefan Roese 
1472e3b9c98aSStefan Roese 	if (reg > MVNETA_PHY_REG_MASK) {
1473e3b9c98aSStefan Roese 		printf("Err: Invalid register offset %d\n", reg);
147419fc2eaeSStefan Roese 		return -EFAULT;
147519fc2eaeSStefan Roese 	}
147619fc2eaeSStefan Roese 
147719fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
147819fc2eaeSStefan Roese 	if (smi_wait_ready(pp) < 0)
147919fc2eaeSStefan Roese 		return -EFAULT;
148019fc2eaeSStefan Roese 
148119fc2eaeSStefan Roese 	/* fill the phy addr and reg offset and write opcode and data */
1482e3b9c98aSStefan Roese 	smi_reg = value << MVNETA_SMI_DATA_OFFS;
1483e3b9c98aSStefan Roese 	smi_reg |= (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1484e3b9c98aSStefan Roese 		| (reg << MVNETA_SMI_REG_ADDR_OFFS);
148519fc2eaeSStefan Roese 	smi_reg &= ~MVNETA_SMI_OPCODE_READ;
148619fc2eaeSStefan Roese 
148719fc2eaeSStefan Roese 	/* write the smi register */
148819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SMI, smi_reg);
148919fc2eaeSStefan Roese 
149019fc2eaeSStefan Roese 	return 0;
149119fc2eaeSStefan Roese }
149219fc2eaeSStefan Roese 
1493e3b9c98aSStefan Roese static int mvneta_start(struct udevice *dev)
149419fc2eaeSStefan Roese {
1495e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
149619fc2eaeSStefan Roese 	struct phy_device *phydev;
149719fc2eaeSStefan Roese 
149819fc2eaeSStefan Roese 	mvneta_port_power_up(pp, pp->phy_interface);
149919fc2eaeSStefan Roese 
150019fc2eaeSStefan Roese 	if (!pp->init || pp->link == 0) {
150119fc2eaeSStefan Roese 		/* Set phy address of the port */
150219fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_PHY_ADDR, pp->phyaddr);
150319fc2eaeSStefan Roese 		phydev = phy_connect(pp->bus, pp->phyaddr, dev,
150419fc2eaeSStefan Roese 				     pp->phy_interface);
150519fc2eaeSStefan Roese 
150619fc2eaeSStefan Roese 		pp->phydev = phydev;
150719fc2eaeSStefan Roese 		phy_config(phydev);
150819fc2eaeSStefan Roese 		phy_startup(phydev);
150919fc2eaeSStefan Roese 		if (!phydev->link) {
151019fc2eaeSStefan Roese 			printf("%s: No link.\n", phydev->dev->name);
151119fc2eaeSStefan Roese 			return -1;
151219fc2eaeSStefan Roese 		}
151319fc2eaeSStefan Roese 
151419fc2eaeSStefan Roese 		/* Full init on first call */
1515e3b9c98aSStefan Roese 		mvneta_init(dev);
151619fc2eaeSStefan Roese 		pp->init = 1;
151719fc2eaeSStefan Roese 	} else {
151819fc2eaeSStefan Roese 		/* Upon all following calls, this is enough */
151919fc2eaeSStefan Roese 		mvneta_port_up(pp);
152019fc2eaeSStefan Roese 		mvneta_port_enable(pp);
152119fc2eaeSStefan Roese 	}
152219fc2eaeSStefan Roese 
152319fc2eaeSStefan Roese 	return 0;
152419fc2eaeSStefan Roese }
152519fc2eaeSStefan Roese 
1526e3b9c98aSStefan Roese static int mvneta_send(struct udevice *dev, void *packet, int length)
152719fc2eaeSStefan Roese {
1528e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
152919fc2eaeSStefan Roese 	struct mvneta_tx_queue *txq = &pp->txqs[0];
153019fc2eaeSStefan Roese 	struct mvneta_tx_desc *tx_desc;
153119fc2eaeSStefan Roese 	int sent_desc;
153219fc2eaeSStefan Roese 	u32 timeout = 0;
153319fc2eaeSStefan Roese 
153419fc2eaeSStefan Roese 	/* Get a descriptor for the first part of the packet */
153519fc2eaeSStefan Roese 	tx_desc = mvneta_txq_next_desc_get(txq);
153619fc2eaeSStefan Roese 
15373cbc11daSStefan Roese 	tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
1538e3b9c98aSStefan Roese 	tx_desc->data_size = length;
15393cbc11daSStefan Roese 	flush_dcache_range((ulong)packet,
15403cbc11daSStefan Roese 			   (ulong)packet + ALIGN(length, PKTALIGN));
154119fc2eaeSStefan Roese 
154219fc2eaeSStefan Roese 	/* First and Last descriptor */
154319fc2eaeSStefan Roese 	tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
154419fc2eaeSStefan Roese 	mvneta_txq_pend_desc_add(pp, txq, 1);
154519fc2eaeSStefan Roese 
154619fc2eaeSStefan Roese 	/* Wait for packet to be sent (queue might help with speed here) */
154719fc2eaeSStefan Roese 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
154819fc2eaeSStefan Roese 	while (!sent_desc) {
154919fc2eaeSStefan Roese 		if (timeout++ > 10000) {
155019fc2eaeSStefan Roese 			printf("timeout: packet not sent\n");
155119fc2eaeSStefan Roese 			return -1;
155219fc2eaeSStefan Roese 		}
155319fc2eaeSStefan Roese 		sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
155419fc2eaeSStefan Roese 	}
155519fc2eaeSStefan Roese 
155619fc2eaeSStefan Roese 	/* txDone has increased - hw sent packet */
155719fc2eaeSStefan Roese 	mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
155819fc2eaeSStefan Roese 
155919fc2eaeSStefan Roese 	return 0;
156019fc2eaeSStefan Roese }
156119fc2eaeSStefan Roese 
1562e3b9c98aSStefan Roese static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
156319fc2eaeSStefan Roese {
1564e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
156519fc2eaeSStefan Roese 	int rx_done;
156619fc2eaeSStefan Roese 	struct mvneta_rx_queue *rxq;
1567e3b9c98aSStefan Roese 	int rx_bytes = 0;
156819fc2eaeSStefan Roese 
156919fc2eaeSStefan Roese 	/* get rx queue */
157019fc2eaeSStefan Roese 	rxq = mvneta_rxq_handle_get(pp, rxq_def);
157119fc2eaeSStefan Roese 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
157219fc2eaeSStefan Roese 
1573e3b9c98aSStefan Roese 	if (rx_done) {
157419fc2eaeSStefan Roese 		struct mvneta_rx_desc *rx_desc;
157519fc2eaeSStefan Roese 		unsigned char *data;
157619fc2eaeSStefan Roese 		u32 rx_status;
157719fc2eaeSStefan Roese 
157819fc2eaeSStefan Roese 		/*
157919fc2eaeSStefan Roese 		 * No cache invalidation needed here, since the desc's are
158019fc2eaeSStefan Roese 		 * located in a uncached memory region
158119fc2eaeSStefan Roese 		 */
158219fc2eaeSStefan Roese 		rx_desc = mvneta_rxq_next_desc_get(rxq);
158319fc2eaeSStefan Roese 
158419fc2eaeSStefan Roese 		rx_status = rx_desc->status;
158519fc2eaeSStefan Roese 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
158619fc2eaeSStefan Roese 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
158719fc2eaeSStefan Roese 			mvneta_rx_error(pp, rx_desc);
158819fc2eaeSStefan Roese 			/* leave the descriptor untouched */
1589e3b9c98aSStefan Roese 			return -EIO;
159019fc2eaeSStefan Roese 		}
159119fc2eaeSStefan Roese 
159219fc2eaeSStefan Roese 		/* 2 bytes for marvell header. 4 bytes for crc */
159319fc2eaeSStefan Roese 		rx_bytes = rx_desc->data_size - 6;
159419fc2eaeSStefan Roese 
159519fc2eaeSStefan Roese 		/* give packet to stack - skip on first 2 bytes */
15963cbc11daSStefan Roese 		data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
159719fc2eaeSStefan Roese 		/*
159819fc2eaeSStefan Roese 		 * No cache invalidation needed here, since the rx_buffer's are
159919fc2eaeSStefan Roese 		 * located in a uncached memory region
160019fc2eaeSStefan Roese 		 */
1601e3b9c98aSStefan Roese 		*packetp = data;
160219fc2eaeSStefan Roese 
160319fc2eaeSStefan Roese 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
160419fc2eaeSStefan Roese 	}
160519fc2eaeSStefan Roese 
1606e3b9c98aSStefan Roese 	return rx_bytes;
160719fc2eaeSStefan Roese }
160819fc2eaeSStefan Roese 
1609e3b9c98aSStefan Roese static int mvneta_probe(struct udevice *dev)
161019fc2eaeSStefan Roese {
1611e3b9c98aSStefan Roese 	struct eth_pdata *pdata = dev_get_platdata(dev);
1612e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
1613e3b9c98aSStefan Roese 	void *blob = (void *)gd->fdt_blob;
1614e3b9c98aSStefan Roese 	int node = dev->of_offset;
1615e3b9c98aSStefan Roese 	struct mii_dev *bus;
1616e3b9c98aSStefan Roese 	unsigned long addr;
161719fc2eaeSStefan Roese 	void *bd_space;
161819fc2eaeSStefan Roese 
161919fc2eaeSStefan Roese 	/*
162019fc2eaeSStefan Roese 	 * Allocate buffer area for descs and rx_buffers. This is only
162119fc2eaeSStefan Roese 	 * done once for all interfaces. As only one interface can
162219fc2eaeSStefan Roese 	 * be active. Make this area DMA save by disabling the D-cache
162319fc2eaeSStefan Roese 	 */
162419fc2eaeSStefan Roese 	if (!buffer_loc.tx_descs) {
162519fc2eaeSStefan Roese 		/* Align buffer area for descs and rx_buffers to 1MiB */
162619fc2eaeSStefan Roese 		bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
16273cbc11daSStefan Roese 		mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
162819fc2eaeSStefan Roese 						DCACHE_OFF);
162919fc2eaeSStefan Roese 		buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
163019fc2eaeSStefan Roese 		buffer_loc.rx_descs = (struct mvneta_rx_desc *)
16313cbc11daSStefan Roese 			((phys_addr_t)bd_space +
163219fc2eaeSStefan Roese 			 MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc));
16333cbc11daSStefan Roese 		buffer_loc.rx_buffers = (phys_addr_t)
163419fc2eaeSStefan Roese 			(bd_space +
163519fc2eaeSStefan Roese 			 MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc) +
163619fc2eaeSStefan Roese 			 MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc));
163719fc2eaeSStefan Roese 	}
163819fc2eaeSStefan Roese 
1639e3b9c98aSStefan Roese 	pp->base = (void __iomem *)pdata->iobase;
164019fc2eaeSStefan Roese 
1641e3b9c98aSStefan Roese 	/* Configure MBUS address windows */
1642*544eefe0SStefan Roese 	if (of_device_is_compatible(dev, "marvell,armada-3700-neta"))
1643*544eefe0SStefan Roese 		mvneta_bypass_mbus_windows(pp);
1644*544eefe0SStefan Roese 	else
1645e3b9c98aSStefan Roese 		mvneta_conf_mbus_windows(pp);
164619fc2eaeSStefan Roese 
1647e3b9c98aSStefan Roese 	/* PHY interface is already decoded in mvneta_ofdata_to_platdata() */
1648e3b9c98aSStefan Roese 	pp->phy_interface = pdata->phy_interface;
164919fc2eaeSStefan Roese 
1650e3b9c98aSStefan Roese 	/* Now read phyaddr from DT */
1651e3b9c98aSStefan Roese 	addr = fdtdec_get_int(blob, node, "phy", 0);
1652e3b9c98aSStefan Roese 	addr = fdt_node_offset_by_phandle(blob, addr);
1653e3b9c98aSStefan Roese 	pp->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
165419fc2eaeSStefan Roese 
1655e3b9c98aSStefan Roese 	bus = mdio_alloc();
1656e3b9c98aSStefan Roese 	if (!bus) {
1657e3b9c98aSStefan Roese 		printf("Failed to allocate MDIO bus\n");
1658e3b9c98aSStefan Roese 		return -ENOMEM;
165919fc2eaeSStefan Roese 	}
1660e3b9c98aSStefan Roese 
1661e3b9c98aSStefan Roese 	bus->read = mvneta_mdio_read;
1662e3b9c98aSStefan Roese 	bus->write = mvneta_mdio_write;
1663e3b9c98aSStefan Roese 	snprintf(bus->name, sizeof(bus->name), dev->name);
1664e3b9c98aSStefan Roese 	bus->priv = (void *)pp;
1665e3b9c98aSStefan Roese 	pp->bus = bus;
1666e3b9c98aSStefan Roese 
1667e3b9c98aSStefan Roese 	return mdio_register(bus);
1668e3b9c98aSStefan Roese }
1669e3b9c98aSStefan Roese 
1670e3b9c98aSStefan Roese static void mvneta_stop(struct udevice *dev)
1671e3b9c98aSStefan Roese {
1672e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
1673e3b9c98aSStefan Roese 
1674e3b9c98aSStefan Roese 	mvneta_port_down(pp);
1675e3b9c98aSStefan Roese 	mvneta_port_disable(pp);
1676e3b9c98aSStefan Roese }
1677e3b9c98aSStefan Roese 
1678e3b9c98aSStefan Roese static const struct eth_ops mvneta_ops = {
1679e3b9c98aSStefan Roese 	.start		= mvneta_start,
1680e3b9c98aSStefan Roese 	.send		= mvneta_send,
1681e3b9c98aSStefan Roese 	.recv		= mvneta_recv,
1682e3b9c98aSStefan Roese 	.stop		= mvneta_stop,
1683e3b9c98aSStefan Roese };
1684e3b9c98aSStefan Roese 
1685e3b9c98aSStefan Roese static int mvneta_ofdata_to_platdata(struct udevice *dev)
1686e3b9c98aSStefan Roese {
1687e3b9c98aSStefan Roese 	struct eth_pdata *pdata = dev_get_platdata(dev);
1688e3b9c98aSStefan Roese 	const char *phy_mode;
1689e3b9c98aSStefan Roese 
1690e3b9c98aSStefan Roese 	pdata->iobase = dev_get_addr(dev);
1691e3b9c98aSStefan Roese 
1692e3b9c98aSStefan Roese 	/* Get phy-mode / phy_interface from DT */
1693e3b9c98aSStefan Roese 	pdata->phy_interface = -1;
1694e3b9c98aSStefan Roese 	phy_mode = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy-mode", NULL);
1695e3b9c98aSStefan Roese 	if (phy_mode)
1696e3b9c98aSStefan Roese 		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1697e3b9c98aSStefan Roese 	if (pdata->phy_interface == -1) {
1698e3b9c98aSStefan Roese 		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1699e3b9c98aSStefan Roese 		return -EINVAL;
1700e3b9c98aSStefan Roese 	}
1701e3b9c98aSStefan Roese 
1702e3b9c98aSStefan Roese 	return 0;
1703e3b9c98aSStefan Roese }
1704e3b9c98aSStefan Roese 
1705e3b9c98aSStefan Roese static const struct udevice_id mvneta_ids[] = {
1706e3b9c98aSStefan Roese 	{ .compatible = "marvell,armada-370-neta" },
1707e3b9c98aSStefan Roese 	{ .compatible = "marvell,armada-xp-neta" },
1708*544eefe0SStefan Roese 	{ .compatible = "marvell,armada-3700-neta" },
1709e3b9c98aSStefan Roese 	{ }
1710e3b9c98aSStefan Roese };
1711e3b9c98aSStefan Roese 
1712e3b9c98aSStefan Roese U_BOOT_DRIVER(mvneta) = {
1713e3b9c98aSStefan Roese 	.name	= "mvneta",
1714e3b9c98aSStefan Roese 	.id	= UCLASS_ETH,
1715e3b9c98aSStefan Roese 	.of_match = mvneta_ids,
1716e3b9c98aSStefan Roese 	.ofdata_to_platdata = mvneta_ofdata_to_platdata,
1717e3b9c98aSStefan Roese 	.probe	= mvneta_probe,
1718e3b9c98aSStefan Roese 	.ops	= &mvneta_ops,
1719e3b9c98aSStefan Roese 	.priv_auto_alloc_size = sizeof(struct mvneta_port),
1720e3b9c98aSStefan Roese 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
1721e3b9c98aSStefan Roese };
1722