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