xref: /rk3399_rockchip-uboot/drivers/net/mvneta.c (revision ec6bc928bbe852a5dae242d24cfd28f868f6286c)
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))
94544eefe0SStefan Roese #define MVNETA_WIN_SIZE_MASK			(0xffff0000)
9519fc2eaeSStefan Roese #define MVNETA_BASE_ADDR_ENABLE                 0x2290
96544eefe0SStefan Roese #define      MVNETA_BASE_ADDR_ENABLE_BIT	0x1
97544eefe0SStefan Roese #define MVNETA_PORT_ACCESS_PROTECT              0x2294
98544eefe0SStefan 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)
194278d30c8SKonstantin Porotchkin #define      MVNETA_GMAC_FORCE_LINK_UP           (BIT(0) | BIT(1))
195278d30c8SKonstantin Porotchkin #define      MVNETA_GMAC_IB_BYPASS_AN_EN         BIT(3)
19619fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
19719fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
19819fc2eaeSStefan Roese #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
199278d30c8SKonstantin Porotchkin #define      MVNETA_GMAC_SET_FC_EN               BIT(8)
200278d30c8SKonstantin Porotchkin #define      MVNETA_GMAC_ADVERT_FC_EN            BIT(9)
20119fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
20219fc2eaeSStefan Roese #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
203278d30c8SKonstantin Porotchkin #define      MVNETA_GMAC_SAMPLE_TX_CFG_EN        BIT(15)
20419fc2eaeSStefan Roese #define MVNETA_MIB_COUNTERS_BASE                 0x3080
20519fc2eaeSStefan Roese #define      MVNETA_MIB_LATE_COLLISION           0x7c
20619fc2eaeSStefan Roese #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
20719fc2eaeSStefan Roese #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
20819fc2eaeSStefan Roese #define MVNETA_DA_FILT_UCAST_BASE                0x3600
20919fc2eaeSStefan Roese #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
21019fc2eaeSStefan Roese #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
21119fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
21219fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
21319fc2eaeSStefan Roese #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
21419fc2eaeSStefan Roese #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
21519fc2eaeSStefan Roese #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
21619fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
21719fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
21819fc2eaeSStefan Roese #define MVNETA_PORT_TX_RESET                     0x3cf0
21919fc2eaeSStefan Roese #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
22019fc2eaeSStefan Roese #define MVNETA_TX_MTU                            0x3e0c
22119fc2eaeSStefan Roese #define MVNETA_TX_TOKEN_SIZE                     0x3e14
22219fc2eaeSStefan Roese #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
22319fc2eaeSStefan Roese #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
22419fc2eaeSStefan Roese #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
22519fc2eaeSStefan Roese 
22619fc2eaeSStefan Roese /* Descriptor ring Macros */
22719fc2eaeSStefan Roese #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
22819fc2eaeSStefan Roese 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
22919fc2eaeSStefan Roese 
23019fc2eaeSStefan Roese /* Various constants */
23119fc2eaeSStefan Roese 
23219fc2eaeSStefan Roese /* Coalescing */
23319fc2eaeSStefan Roese #define MVNETA_TXDONE_COAL_PKTS		16
23419fc2eaeSStefan Roese #define MVNETA_RX_COAL_PKTS		32
23519fc2eaeSStefan Roese #define MVNETA_RX_COAL_USEC		100
23619fc2eaeSStefan Roese 
23719fc2eaeSStefan Roese /* The two bytes Marvell header. Either contains a special value used
23819fc2eaeSStefan Roese  * by Marvell switches when a specific hardware mode is enabled (not
23919fc2eaeSStefan Roese  * supported by this driver) or is filled automatically by zeroes on
24019fc2eaeSStefan Roese  * the RX side. Those two bytes being at the front of the Ethernet
24119fc2eaeSStefan Roese  * header, they allow to have the IP header aligned on a 4 bytes
24219fc2eaeSStefan Roese  * boundary automatically: the hardware skips those two bytes on its
24319fc2eaeSStefan Roese  * own.
24419fc2eaeSStefan Roese  */
24519fc2eaeSStefan Roese #define MVNETA_MH_SIZE			2
24619fc2eaeSStefan Roese 
24719fc2eaeSStefan Roese #define MVNETA_VLAN_TAG_LEN             4
24819fc2eaeSStefan Roese 
24919fc2eaeSStefan Roese #define MVNETA_CPU_D_CACHE_LINE_SIZE    32
25019fc2eaeSStefan Roese #define MVNETA_TX_CSUM_MAX_SIZE		9800
25119fc2eaeSStefan Roese #define MVNETA_ACC_MODE_EXT		1
25219fc2eaeSStefan Roese 
25319fc2eaeSStefan Roese /* Timeout constants */
25419fc2eaeSStefan Roese #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
25519fc2eaeSStefan Roese #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
25619fc2eaeSStefan Roese #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
25719fc2eaeSStefan Roese 
25819fc2eaeSStefan Roese #define MVNETA_TX_MTU_MAX		0x3ffff
25919fc2eaeSStefan Roese 
26019fc2eaeSStefan Roese /* Max number of Rx descriptors */
26119fc2eaeSStefan Roese #define MVNETA_MAX_RXD 16
26219fc2eaeSStefan Roese 
26319fc2eaeSStefan Roese /* Max number of Tx descriptors */
26419fc2eaeSStefan Roese #define MVNETA_MAX_TXD 16
26519fc2eaeSStefan Roese 
26619fc2eaeSStefan Roese /* descriptor aligned size */
26719fc2eaeSStefan Roese #define MVNETA_DESC_ALIGNED_SIZE	32
26819fc2eaeSStefan Roese 
26919fc2eaeSStefan Roese struct mvneta_port {
27019fc2eaeSStefan Roese 	void __iomem *base;
27119fc2eaeSStefan Roese 	struct mvneta_rx_queue *rxqs;
27219fc2eaeSStefan Roese 	struct mvneta_tx_queue *txqs;
27319fc2eaeSStefan Roese 
27419fc2eaeSStefan Roese 	u8 mcast_count[256];
27519fc2eaeSStefan Roese 	u16 tx_ring_size;
27619fc2eaeSStefan Roese 	u16 rx_ring_size;
27719fc2eaeSStefan Roese 
27819fc2eaeSStefan Roese 	phy_interface_t phy_interface;
27919fc2eaeSStefan Roese 	unsigned int link;
28019fc2eaeSStefan Roese 	unsigned int duplex;
28119fc2eaeSStefan Roese 	unsigned int speed;
28219fc2eaeSStefan Roese 
28319fc2eaeSStefan Roese 	int init;
28419fc2eaeSStefan Roese 	int phyaddr;
28519fc2eaeSStefan Roese 	struct phy_device *phydev;
28619fc2eaeSStefan Roese 	struct mii_dev *bus;
28719fc2eaeSStefan Roese };
28819fc2eaeSStefan Roese 
28919fc2eaeSStefan Roese /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
29019fc2eaeSStefan Roese  * layout of the transmit and reception DMA descriptors, and their
29119fc2eaeSStefan Roese  * layout is therefore defined by the hardware design
29219fc2eaeSStefan Roese  */
29319fc2eaeSStefan Roese 
29419fc2eaeSStefan Roese #define MVNETA_TX_L3_OFF_SHIFT	0
29519fc2eaeSStefan Roese #define MVNETA_TX_IP_HLEN_SHIFT	8
29619fc2eaeSStefan Roese #define MVNETA_TX_L4_UDP	BIT(16)
29719fc2eaeSStefan Roese #define MVNETA_TX_L3_IP6	BIT(17)
29819fc2eaeSStefan Roese #define MVNETA_TXD_IP_CSUM	BIT(18)
29919fc2eaeSStefan Roese #define MVNETA_TXD_Z_PAD	BIT(19)
30019fc2eaeSStefan Roese #define MVNETA_TXD_L_DESC	BIT(20)
30119fc2eaeSStefan Roese #define MVNETA_TXD_F_DESC	BIT(21)
30219fc2eaeSStefan Roese #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
30319fc2eaeSStefan Roese 				 MVNETA_TXD_L_DESC | \
30419fc2eaeSStefan Roese 				 MVNETA_TXD_F_DESC)
30519fc2eaeSStefan Roese #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
30619fc2eaeSStefan Roese #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
30719fc2eaeSStefan Roese 
30819fc2eaeSStefan Roese #define MVNETA_RXD_ERR_CRC		0x0
30919fc2eaeSStefan Roese #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
31019fc2eaeSStefan Roese #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
31119fc2eaeSStefan Roese #define MVNETA_RXD_ERR_LEN		BIT(18)
31219fc2eaeSStefan Roese #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
31319fc2eaeSStefan Roese #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
31419fc2eaeSStefan Roese #define MVNETA_RXD_L3_IP4		BIT(25)
31519fc2eaeSStefan Roese #define MVNETA_RXD_FIRST_LAST_DESC	(BIT(26) | BIT(27))
31619fc2eaeSStefan Roese #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
31719fc2eaeSStefan Roese 
31819fc2eaeSStefan Roese struct mvneta_tx_desc {
31919fc2eaeSStefan Roese 	u32  command;		/* Options used by HW for packet transmitting.*/
32019fc2eaeSStefan Roese 	u16  reserverd1;	/* csum_l4 (for future use)		*/
32119fc2eaeSStefan Roese 	u16  data_size;		/* Data size of transmitted packet in bytes */
32219fc2eaeSStefan Roese 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
32319fc2eaeSStefan Roese 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
32419fc2eaeSStefan Roese 	u32  reserved3[4];	/* Reserved - (for future use)		*/
32519fc2eaeSStefan Roese };
32619fc2eaeSStefan Roese 
32719fc2eaeSStefan Roese struct mvneta_rx_desc {
32819fc2eaeSStefan Roese 	u32  status;		/* Info about received packet		*/
32919fc2eaeSStefan Roese 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
33019fc2eaeSStefan Roese 	u16  data_size;		/* Size of received packet in bytes	*/
33119fc2eaeSStefan Roese 
33219fc2eaeSStefan Roese 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
33319fc2eaeSStefan Roese 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
33419fc2eaeSStefan Roese 
33519fc2eaeSStefan Roese 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
33619fc2eaeSStefan Roese 	u16  reserved3;		/* prefetch_cmd, for future use		*/
33719fc2eaeSStefan Roese 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
33819fc2eaeSStefan Roese 
33919fc2eaeSStefan Roese 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
34019fc2eaeSStefan Roese 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
34119fc2eaeSStefan Roese };
34219fc2eaeSStefan Roese 
34319fc2eaeSStefan Roese struct mvneta_tx_queue {
34419fc2eaeSStefan Roese 	/* Number of this TX queue, in the range 0-7 */
34519fc2eaeSStefan Roese 	u8 id;
34619fc2eaeSStefan Roese 
34719fc2eaeSStefan Roese 	/* Number of TX DMA descriptors in the descriptor ring */
34819fc2eaeSStefan Roese 	int size;
34919fc2eaeSStefan Roese 
35019fc2eaeSStefan Roese 	/* Index of last TX DMA descriptor that was inserted */
35119fc2eaeSStefan Roese 	int txq_put_index;
35219fc2eaeSStefan Roese 
35319fc2eaeSStefan Roese 	/* Index of the TX DMA descriptor to be cleaned up */
35419fc2eaeSStefan Roese 	int txq_get_index;
35519fc2eaeSStefan Roese 
35619fc2eaeSStefan Roese 	/* Virtual address of the TX DMA descriptors array */
35719fc2eaeSStefan Roese 	struct mvneta_tx_desc *descs;
35819fc2eaeSStefan Roese 
35919fc2eaeSStefan Roese 	/* DMA address of the TX DMA descriptors array */
36019fc2eaeSStefan Roese 	dma_addr_t descs_phys;
36119fc2eaeSStefan Roese 
36219fc2eaeSStefan Roese 	/* Index of the last TX DMA descriptor */
36319fc2eaeSStefan Roese 	int last_desc;
36419fc2eaeSStefan Roese 
36519fc2eaeSStefan Roese 	/* Index of the next TX DMA descriptor to process */
36619fc2eaeSStefan Roese 	int next_desc_to_proc;
36719fc2eaeSStefan Roese };
36819fc2eaeSStefan Roese 
36919fc2eaeSStefan Roese struct mvneta_rx_queue {
37019fc2eaeSStefan Roese 	/* rx queue number, in the range 0-7 */
37119fc2eaeSStefan Roese 	u8 id;
37219fc2eaeSStefan Roese 
37319fc2eaeSStefan Roese 	/* num of rx descriptors in the rx descriptor ring */
37419fc2eaeSStefan Roese 	int size;
37519fc2eaeSStefan Roese 
37619fc2eaeSStefan Roese 	/* Virtual address of the RX DMA descriptors array */
37719fc2eaeSStefan Roese 	struct mvneta_rx_desc *descs;
37819fc2eaeSStefan Roese 
37919fc2eaeSStefan Roese 	/* DMA address of the RX DMA descriptors array */
38019fc2eaeSStefan Roese 	dma_addr_t descs_phys;
38119fc2eaeSStefan Roese 
38219fc2eaeSStefan Roese 	/* Index of the last RX DMA descriptor */
38319fc2eaeSStefan Roese 	int last_desc;
38419fc2eaeSStefan Roese 
38519fc2eaeSStefan Roese 	/* Index of the next RX DMA descriptor to process */
38619fc2eaeSStefan Roese 	int next_desc_to_proc;
38719fc2eaeSStefan Roese };
38819fc2eaeSStefan Roese 
38919fc2eaeSStefan Roese /* U-Boot doesn't use the queues, so set the number to 1 */
39019fc2eaeSStefan Roese static int rxq_number = 1;
39119fc2eaeSStefan Roese static int txq_number = 1;
39219fc2eaeSStefan Roese static int rxq_def;
39319fc2eaeSStefan Roese 
39419fc2eaeSStefan Roese struct buffer_location {
39519fc2eaeSStefan Roese 	struct mvneta_tx_desc *tx_descs;
39619fc2eaeSStefan Roese 	struct mvneta_rx_desc *rx_descs;
39719fc2eaeSStefan Roese 	u32 rx_buffers;
39819fc2eaeSStefan Roese };
39919fc2eaeSStefan Roese 
40019fc2eaeSStefan Roese /*
40119fc2eaeSStefan Roese  * All 4 interfaces use the same global buffer, since only one interface
40219fc2eaeSStefan Roese  * can be enabled at once
40319fc2eaeSStefan Roese  */
40419fc2eaeSStefan Roese static struct buffer_location buffer_loc;
40519fc2eaeSStefan Roese 
40619fc2eaeSStefan Roese /*
40719fc2eaeSStefan Roese  * Page table entries are set to 1MB, or multiples of 1MB
40819fc2eaeSStefan Roese  * (not < 1MB). driver uses less bd's so use 1MB bdspace.
40919fc2eaeSStefan Roese  */
41019fc2eaeSStefan Roese #define BD_SPACE	(1 << 20)
41119fc2eaeSStefan Roese 
412976feda2SKonstantin Porotchkin /*
413976feda2SKonstantin Porotchkin  * Dummy implementation that can be overwritten by a board
414976feda2SKonstantin Porotchkin  * specific function
415976feda2SKonstantin Porotchkin  */
board_network_enable(struct mii_dev * bus)416976feda2SKonstantin Porotchkin __weak int board_network_enable(struct mii_dev *bus)
417976feda2SKonstantin Porotchkin {
418976feda2SKonstantin Porotchkin 	return 0;
419976feda2SKonstantin Porotchkin }
420976feda2SKonstantin Porotchkin 
42119fc2eaeSStefan Roese /* Utility/helper methods */
42219fc2eaeSStefan Roese 
42319fc2eaeSStefan Roese /* Write helper method */
mvreg_write(struct mvneta_port * pp,u32 offset,u32 data)42419fc2eaeSStefan Roese static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
42519fc2eaeSStefan Roese {
42619fc2eaeSStefan Roese 	writel(data, pp->base + offset);
42719fc2eaeSStefan Roese }
42819fc2eaeSStefan Roese 
42919fc2eaeSStefan Roese /* Read helper method */
mvreg_read(struct mvneta_port * pp,u32 offset)43019fc2eaeSStefan Roese static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
43119fc2eaeSStefan Roese {
43219fc2eaeSStefan Roese 	return readl(pp->base + offset);
43319fc2eaeSStefan Roese }
43419fc2eaeSStefan Roese 
43519fc2eaeSStefan Roese /* Clear all MIB counters */
mvneta_mib_counters_clear(struct mvneta_port * pp)43619fc2eaeSStefan Roese static void mvneta_mib_counters_clear(struct mvneta_port *pp)
43719fc2eaeSStefan Roese {
43819fc2eaeSStefan Roese 	int i;
43919fc2eaeSStefan Roese 
44019fc2eaeSStefan Roese 	/* Perform dummy reads from MIB counters */
44119fc2eaeSStefan Roese 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
44219fc2eaeSStefan Roese 		mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
44319fc2eaeSStefan Roese }
44419fc2eaeSStefan Roese 
44519fc2eaeSStefan Roese /* Rx descriptors helper methods */
44619fc2eaeSStefan Roese 
44719fc2eaeSStefan Roese /* Checks whether the RX descriptor having this status is both the first
44819fc2eaeSStefan Roese  * and the last descriptor for the RX packet. Each RX packet is currently
44919fc2eaeSStefan Roese  * received through a single RX descriptor, so not having each RX
45019fc2eaeSStefan Roese  * descriptor with its first and last bits set is an error
45119fc2eaeSStefan Roese  */
mvneta_rxq_desc_is_first_last(u32 status)45219fc2eaeSStefan Roese static int mvneta_rxq_desc_is_first_last(u32 status)
45319fc2eaeSStefan Roese {
45419fc2eaeSStefan Roese 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
45519fc2eaeSStefan Roese 		MVNETA_RXD_FIRST_LAST_DESC;
45619fc2eaeSStefan Roese }
45719fc2eaeSStefan Roese 
45819fc2eaeSStefan Roese /* Add number of descriptors ready to receive new packets */
mvneta_rxq_non_occup_desc_add(struct mvneta_port * pp,struct mvneta_rx_queue * rxq,int ndescs)45919fc2eaeSStefan Roese static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
46019fc2eaeSStefan Roese 					  struct mvneta_rx_queue *rxq,
46119fc2eaeSStefan Roese 					  int ndescs)
46219fc2eaeSStefan Roese {
46319fc2eaeSStefan Roese 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
46419fc2eaeSStefan Roese 	 * be added at once
46519fc2eaeSStefan Roese 	 */
46619fc2eaeSStefan Roese 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
46719fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
46819fc2eaeSStefan Roese 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
46919fc2eaeSStefan Roese 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
47019fc2eaeSStefan Roese 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
47119fc2eaeSStefan Roese 	}
47219fc2eaeSStefan Roese 
47319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
47419fc2eaeSStefan Roese 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
47519fc2eaeSStefan Roese }
47619fc2eaeSStefan Roese 
47719fc2eaeSStefan Roese /* Get number of RX descriptors occupied by received packets */
mvneta_rxq_busy_desc_num_get(struct mvneta_port * pp,struct mvneta_rx_queue * rxq)47819fc2eaeSStefan Roese static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
47919fc2eaeSStefan Roese 					struct mvneta_rx_queue *rxq)
48019fc2eaeSStefan Roese {
48119fc2eaeSStefan Roese 	u32 val;
48219fc2eaeSStefan Roese 
48319fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
48419fc2eaeSStefan Roese 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
48519fc2eaeSStefan Roese }
48619fc2eaeSStefan Roese 
48719fc2eaeSStefan Roese /* Update num of rx desc called upon return from rx path or
48819fc2eaeSStefan Roese  * from mvneta_rxq_drop_pkts().
48919fc2eaeSStefan Roese  */
mvneta_rxq_desc_num_update(struct mvneta_port * pp,struct mvneta_rx_queue * rxq,int rx_done,int rx_filled)49019fc2eaeSStefan Roese static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
49119fc2eaeSStefan Roese 				       struct mvneta_rx_queue *rxq,
49219fc2eaeSStefan Roese 				       int rx_done, int rx_filled)
49319fc2eaeSStefan Roese {
49419fc2eaeSStefan Roese 	u32 val;
49519fc2eaeSStefan Roese 
49619fc2eaeSStefan Roese 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
49719fc2eaeSStefan Roese 		val = rx_done |
49819fc2eaeSStefan Roese 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
49919fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
50019fc2eaeSStefan Roese 		return;
50119fc2eaeSStefan Roese 	}
50219fc2eaeSStefan Roese 
50319fc2eaeSStefan Roese 	/* Only 255 descriptors can be added at once */
50419fc2eaeSStefan Roese 	while ((rx_done > 0) || (rx_filled > 0)) {
50519fc2eaeSStefan Roese 		if (rx_done <= 0xff) {
50619fc2eaeSStefan Roese 			val = rx_done;
50719fc2eaeSStefan Roese 			rx_done = 0;
50819fc2eaeSStefan Roese 		} else {
50919fc2eaeSStefan Roese 			val = 0xff;
51019fc2eaeSStefan Roese 			rx_done -= 0xff;
51119fc2eaeSStefan Roese 		}
51219fc2eaeSStefan Roese 		if (rx_filled <= 0xff) {
51319fc2eaeSStefan Roese 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
51419fc2eaeSStefan Roese 			rx_filled = 0;
51519fc2eaeSStefan Roese 		} else {
51619fc2eaeSStefan Roese 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
51719fc2eaeSStefan Roese 			rx_filled -= 0xff;
51819fc2eaeSStefan Roese 		}
51919fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
52019fc2eaeSStefan Roese 	}
52119fc2eaeSStefan Roese }
52219fc2eaeSStefan Roese 
52319fc2eaeSStefan Roese /* Get pointer to next RX descriptor to be processed by SW */
52419fc2eaeSStefan Roese static struct mvneta_rx_desc *
mvneta_rxq_next_desc_get(struct mvneta_rx_queue * rxq)52519fc2eaeSStefan Roese mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
52619fc2eaeSStefan Roese {
52719fc2eaeSStefan Roese 	int rx_desc = rxq->next_desc_to_proc;
52819fc2eaeSStefan Roese 
52919fc2eaeSStefan Roese 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
53019fc2eaeSStefan Roese 	return rxq->descs + rx_desc;
53119fc2eaeSStefan Roese }
53219fc2eaeSStefan Roese 
53319fc2eaeSStefan Roese /* Tx descriptors helper methods */
53419fc2eaeSStefan Roese 
53519fc2eaeSStefan Roese /* Update HW with number of TX descriptors to be sent */
mvneta_txq_pend_desc_add(struct mvneta_port * pp,struct mvneta_tx_queue * txq,int pend_desc)53619fc2eaeSStefan Roese static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
53719fc2eaeSStefan Roese 				     struct mvneta_tx_queue *txq,
53819fc2eaeSStefan Roese 				     int pend_desc)
53919fc2eaeSStefan Roese {
54019fc2eaeSStefan Roese 	u32 val;
54119fc2eaeSStefan Roese 
54219fc2eaeSStefan Roese 	/* Only 255 descriptors can be added at once ; Assume caller
543*e4691564SHeinrich Schuchardt 	 * process TX descriptors in quanta less than 256
54419fc2eaeSStefan Roese 	 */
54519fc2eaeSStefan Roese 	val = pend_desc;
54619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
54719fc2eaeSStefan Roese }
54819fc2eaeSStefan Roese 
54919fc2eaeSStefan Roese /* Get pointer to next TX descriptor to be processed (send) by HW */
55019fc2eaeSStefan Roese static struct mvneta_tx_desc *
mvneta_txq_next_desc_get(struct mvneta_tx_queue * txq)55119fc2eaeSStefan Roese mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
55219fc2eaeSStefan Roese {
55319fc2eaeSStefan Roese 	int tx_desc = txq->next_desc_to_proc;
55419fc2eaeSStefan Roese 
55519fc2eaeSStefan Roese 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
55619fc2eaeSStefan Roese 	return txq->descs + tx_desc;
55719fc2eaeSStefan Roese }
55819fc2eaeSStefan Roese 
55919fc2eaeSStefan Roese /* Set rxq buf size */
mvneta_rxq_buf_size_set(struct mvneta_port * pp,struct mvneta_rx_queue * rxq,int buf_size)56019fc2eaeSStefan Roese static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
56119fc2eaeSStefan Roese 				    struct mvneta_rx_queue *rxq,
56219fc2eaeSStefan Roese 				    int buf_size)
56319fc2eaeSStefan Roese {
56419fc2eaeSStefan Roese 	u32 val;
56519fc2eaeSStefan Roese 
56619fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
56719fc2eaeSStefan Roese 
56819fc2eaeSStefan Roese 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
56919fc2eaeSStefan Roese 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
57019fc2eaeSStefan Roese 
57119fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
57219fc2eaeSStefan Roese }
57319fc2eaeSStefan Roese 
mvneta_port_is_fixed_link(struct mvneta_port * pp)574278d30c8SKonstantin Porotchkin static int mvneta_port_is_fixed_link(struct mvneta_port *pp)
575278d30c8SKonstantin Porotchkin {
576278d30c8SKonstantin Porotchkin 	/* phy_addr is set to invalid value for fixed link */
577278d30c8SKonstantin Porotchkin 	return pp->phyaddr > PHY_MAX_ADDR;
578278d30c8SKonstantin Porotchkin }
579278d30c8SKonstantin Porotchkin 
580278d30c8SKonstantin Porotchkin 
58119fc2eaeSStefan Roese /* Start the Ethernet port RX and TX activity */
mvneta_port_up(struct mvneta_port * pp)58219fc2eaeSStefan Roese static void mvneta_port_up(struct mvneta_port *pp)
58319fc2eaeSStefan Roese {
58419fc2eaeSStefan Roese 	int queue;
58519fc2eaeSStefan Roese 	u32 q_map;
58619fc2eaeSStefan Roese 
58719fc2eaeSStefan Roese 	/* Enable all initialized TXs. */
58819fc2eaeSStefan Roese 	mvneta_mib_counters_clear(pp);
58919fc2eaeSStefan Roese 	q_map = 0;
59019fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
59119fc2eaeSStefan Roese 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
59219fc2eaeSStefan Roese 		if (txq->descs != NULL)
59319fc2eaeSStefan Roese 			q_map |= (1 << queue);
59419fc2eaeSStefan Roese 	}
59519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
59619fc2eaeSStefan Roese 
59719fc2eaeSStefan Roese 	/* Enable all initialized RXQs. */
59819fc2eaeSStefan Roese 	q_map = 0;
59919fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
60019fc2eaeSStefan Roese 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
60119fc2eaeSStefan Roese 		if (rxq->descs != NULL)
60219fc2eaeSStefan Roese 			q_map |= (1 << queue);
60319fc2eaeSStefan Roese 	}
60419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
60519fc2eaeSStefan Roese }
60619fc2eaeSStefan Roese 
60719fc2eaeSStefan Roese /* Stop the Ethernet port activity */
mvneta_port_down(struct mvneta_port * pp)60819fc2eaeSStefan Roese static void mvneta_port_down(struct mvneta_port *pp)
60919fc2eaeSStefan Roese {
61019fc2eaeSStefan Roese 	u32 val;
61119fc2eaeSStefan Roese 	int count;
61219fc2eaeSStefan Roese 
61319fc2eaeSStefan Roese 	/* Stop Rx port activity. Check port Rx activity. */
61419fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
61519fc2eaeSStefan Roese 
61619fc2eaeSStefan Roese 	/* Issue stop command for active channels only */
61719fc2eaeSStefan Roese 	if (val != 0)
61819fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_CMD,
61919fc2eaeSStefan Roese 			    val << MVNETA_RXQ_DISABLE_SHIFT);
62019fc2eaeSStefan Roese 
62119fc2eaeSStefan Roese 	/* Wait for all Rx activity to terminate. */
62219fc2eaeSStefan Roese 	count = 0;
62319fc2eaeSStefan Roese 	do {
62419fc2eaeSStefan Roese 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
62519fc2eaeSStefan Roese 			netdev_warn(pp->dev,
62619fc2eaeSStefan Roese 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
62719fc2eaeSStefan Roese 				    val);
62819fc2eaeSStefan Roese 			break;
62919fc2eaeSStefan Roese 		}
63019fc2eaeSStefan Roese 		mdelay(1);
63119fc2eaeSStefan Roese 
63219fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
63319fc2eaeSStefan Roese 	} while (val & 0xff);
63419fc2eaeSStefan Roese 
63519fc2eaeSStefan Roese 	/* Stop Tx port activity. Check port Tx activity. Issue stop
63619fc2eaeSStefan Roese 	 * command for active channels only
63719fc2eaeSStefan Roese 	 */
63819fc2eaeSStefan Roese 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
63919fc2eaeSStefan Roese 
64019fc2eaeSStefan Roese 	if (val != 0)
64119fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_TXQ_CMD,
64219fc2eaeSStefan Roese 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
64319fc2eaeSStefan Roese 
64419fc2eaeSStefan Roese 	/* Wait for all Tx activity to terminate. */
64519fc2eaeSStefan Roese 	count = 0;
64619fc2eaeSStefan Roese 	do {
64719fc2eaeSStefan Roese 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
64819fc2eaeSStefan Roese 			netdev_warn(pp->dev,
64919fc2eaeSStefan Roese 				    "TIMEOUT for TX stopped status=0x%08x\n",
65019fc2eaeSStefan Roese 				    val);
65119fc2eaeSStefan Roese 			break;
65219fc2eaeSStefan Roese 		}
65319fc2eaeSStefan Roese 		mdelay(1);
65419fc2eaeSStefan Roese 
65519fc2eaeSStefan Roese 		/* Check TX Command reg that all Txqs are stopped */
65619fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
65719fc2eaeSStefan Roese 
65819fc2eaeSStefan Roese 	} while (val & 0xff);
65919fc2eaeSStefan Roese 
66019fc2eaeSStefan Roese 	/* Double check to verify that TX FIFO is empty */
66119fc2eaeSStefan Roese 	count = 0;
66219fc2eaeSStefan Roese 	do {
66319fc2eaeSStefan Roese 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
66419fc2eaeSStefan Roese 			netdev_warn(pp->dev,
66519fc2eaeSStefan Roese 				    "TX FIFO empty timeout status=0x08%x\n",
66619fc2eaeSStefan Roese 				    val);
66719fc2eaeSStefan Roese 			break;
66819fc2eaeSStefan Roese 		}
66919fc2eaeSStefan Roese 		mdelay(1);
67019fc2eaeSStefan Roese 
67119fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
67219fc2eaeSStefan Roese 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
67319fc2eaeSStefan Roese 		 (val & MVNETA_TX_IN_PRGRS));
67419fc2eaeSStefan Roese 
67519fc2eaeSStefan Roese 	udelay(200);
67619fc2eaeSStefan Roese }
67719fc2eaeSStefan Roese 
67819fc2eaeSStefan Roese /* Enable the port by setting the port enable bit of the MAC control register */
mvneta_port_enable(struct mvneta_port * pp)67919fc2eaeSStefan Roese static void mvneta_port_enable(struct mvneta_port *pp)
68019fc2eaeSStefan Roese {
68119fc2eaeSStefan Roese 	u32 val;
68219fc2eaeSStefan Roese 
68319fc2eaeSStefan Roese 	/* Enable port */
68419fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
68519fc2eaeSStefan Roese 	val |= MVNETA_GMAC0_PORT_ENABLE;
68619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
68719fc2eaeSStefan Roese }
68819fc2eaeSStefan Roese 
68919fc2eaeSStefan Roese /* Disable the port and wait for about 200 usec before retuning */
mvneta_port_disable(struct mvneta_port * pp)69019fc2eaeSStefan Roese static void mvneta_port_disable(struct mvneta_port *pp)
69119fc2eaeSStefan Roese {
69219fc2eaeSStefan Roese 	u32 val;
69319fc2eaeSStefan Roese 
69419fc2eaeSStefan Roese 	/* Reset the Enable bit in the Serial Control Register */
69519fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
69619fc2eaeSStefan Roese 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
69719fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
69819fc2eaeSStefan Roese 
69919fc2eaeSStefan Roese 	udelay(200);
70019fc2eaeSStefan Roese }
70119fc2eaeSStefan Roese 
70219fc2eaeSStefan Roese /* Multicast tables methods */
70319fc2eaeSStefan Roese 
70419fc2eaeSStefan Roese /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
mvneta_set_ucast_table(struct mvneta_port * pp,int queue)70519fc2eaeSStefan Roese static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
70619fc2eaeSStefan Roese {
70719fc2eaeSStefan Roese 	int offset;
70819fc2eaeSStefan Roese 	u32 val;
70919fc2eaeSStefan Roese 
71019fc2eaeSStefan Roese 	if (queue == -1) {
71119fc2eaeSStefan Roese 		val = 0;
71219fc2eaeSStefan Roese 	} else {
71319fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
71419fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
71519fc2eaeSStefan Roese 	}
71619fc2eaeSStefan Roese 
71719fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xc; offset += 4)
71819fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
71919fc2eaeSStefan Roese }
72019fc2eaeSStefan Roese 
72119fc2eaeSStefan Roese /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
mvneta_set_special_mcast_table(struct mvneta_port * pp,int queue)72219fc2eaeSStefan Roese static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
72319fc2eaeSStefan Roese {
72419fc2eaeSStefan Roese 	int offset;
72519fc2eaeSStefan Roese 	u32 val;
72619fc2eaeSStefan Roese 
72719fc2eaeSStefan Roese 	if (queue == -1) {
72819fc2eaeSStefan Roese 		val = 0;
72919fc2eaeSStefan Roese 	} else {
73019fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
73119fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
73219fc2eaeSStefan Roese 	}
73319fc2eaeSStefan Roese 
73419fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xfc; offset += 4)
73519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
73619fc2eaeSStefan Roese }
73719fc2eaeSStefan Roese 
73819fc2eaeSStefan Roese /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
mvneta_set_other_mcast_table(struct mvneta_port * pp,int queue)73919fc2eaeSStefan Roese static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
74019fc2eaeSStefan Roese {
74119fc2eaeSStefan Roese 	int offset;
74219fc2eaeSStefan Roese 	u32 val;
74319fc2eaeSStefan Roese 
74419fc2eaeSStefan Roese 	if (queue == -1) {
74519fc2eaeSStefan Roese 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
74619fc2eaeSStefan Roese 		val = 0;
74719fc2eaeSStefan Roese 	} else {
74819fc2eaeSStefan Roese 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
74919fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
75019fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
75119fc2eaeSStefan Roese 	}
75219fc2eaeSStefan Roese 
75319fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xfc; offset += 4)
75419fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
75519fc2eaeSStefan Roese }
75619fc2eaeSStefan Roese 
75719fc2eaeSStefan Roese /* This method sets defaults to the NETA port:
75819fc2eaeSStefan Roese  *	Clears interrupt Cause and Mask registers.
75919fc2eaeSStefan Roese  *	Clears all MAC tables.
76019fc2eaeSStefan Roese  *	Sets defaults to all registers.
76119fc2eaeSStefan Roese  *	Resets RX and TX descriptor rings.
76219fc2eaeSStefan Roese  *	Resets PHY.
76319fc2eaeSStefan Roese  * This method can be called after mvneta_port_down() to return the port
76419fc2eaeSStefan Roese  *	settings to defaults.
76519fc2eaeSStefan Roese  */
mvneta_defaults_set(struct mvneta_port * pp)76619fc2eaeSStefan Roese static void mvneta_defaults_set(struct mvneta_port *pp)
76719fc2eaeSStefan Roese {
76819fc2eaeSStefan Roese 	int cpu;
76919fc2eaeSStefan Roese 	int queue;
77019fc2eaeSStefan Roese 	u32 val;
77119fc2eaeSStefan Roese 
77219fc2eaeSStefan Roese 	/* Clear all Cause registers */
77319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
77419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
77519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
77619fc2eaeSStefan Roese 
77719fc2eaeSStefan Roese 	/* Mask all interrupts */
77819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
77919fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
78019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
78119fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
78219fc2eaeSStefan Roese 
78319fc2eaeSStefan Roese 	/* Enable MBUS Retry bit16 */
78419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
78519fc2eaeSStefan Roese 
78619fc2eaeSStefan Roese 	/* Set CPU queue access map - all CPUs have access to all RX
78719fc2eaeSStefan Roese 	 * queues and to all TX queues
78819fc2eaeSStefan Roese 	 */
78919fc2eaeSStefan Roese 	for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
79019fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_CPU_MAP(cpu),
79119fc2eaeSStefan Roese 			    (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
79219fc2eaeSStefan Roese 			     MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
79319fc2eaeSStefan Roese 
79419fc2eaeSStefan Roese 	/* Reset RX and TX DMAs */
79519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
79619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
79719fc2eaeSStefan Roese 
79819fc2eaeSStefan Roese 	/* Disable Legacy WRR, Disable EJP, Release from reset */
79919fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
80019fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
80119fc2eaeSStefan Roese 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
80219fc2eaeSStefan Roese 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
80319fc2eaeSStefan Roese 	}
80419fc2eaeSStefan Roese 
80519fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
80619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
80719fc2eaeSStefan Roese 
80819fc2eaeSStefan Roese 	/* Set Port Acceleration Mode */
80919fc2eaeSStefan Roese 	val = MVNETA_ACC_MODE_EXT;
81019fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_ACC_MODE, val);
81119fc2eaeSStefan Roese 
81219fc2eaeSStefan Roese 	/* Update val of portCfg register accordingly with all RxQueue types */
81319fc2eaeSStefan Roese 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
81419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
81519fc2eaeSStefan Roese 
81619fc2eaeSStefan Roese 	val = 0;
81719fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
81819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
81919fc2eaeSStefan Roese 
82019fc2eaeSStefan Roese 	/* Build PORT_SDMA_CONFIG_REG */
82119fc2eaeSStefan Roese 	val = 0;
82219fc2eaeSStefan Roese 
82319fc2eaeSStefan Roese 	/* Default burst size */
82419fc2eaeSStefan Roese 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
82519fc2eaeSStefan Roese 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
82619fc2eaeSStefan Roese 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
82719fc2eaeSStefan Roese 
82819fc2eaeSStefan Roese 	/* Assign port SDMA configuration */
82919fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
83019fc2eaeSStefan Roese 
831278d30c8SKonstantin Porotchkin 	/* Enable PHY polling in hardware if not in fixed-link mode */
832278d30c8SKonstantin Porotchkin 	if (!mvneta_port_is_fixed_link(pp)) {
83319fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
83419fc2eaeSStefan Roese 		val |= MVNETA_PHY_POLLING_ENABLE;
83519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
836278d30c8SKonstantin Porotchkin 	}
83719fc2eaeSStefan Roese 
83819fc2eaeSStefan Roese 	mvneta_set_ucast_table(pp, -1);
83919fc2eaeSStefan Roese 	mvneta_set_special_mcast_table(pp, -1);
84019fc2eaeSStefan Roese 	mvneta_set_other_mcast_table(pp, -1);
84119fc2eaeSStefan Roese }
84219fc2eaeSStefan Roese 
84319fc2eaeSStefan Roese /* Set unicast address */
mvneta_set_ucast_addr(struct mvneta_port * pp,u8 last_nibble,int queue)84419fc2eaeSStefan Roese static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
84519fc2eaeSStefan Roese 				  int queue)
84619fc2eaeSStefan Roese {
84719fc2eaeSStefan Roese 	unsigned int unicast_reg;
84819fc2eaeSStefan Roese 	unsigned int tbl_offset;
84919fc2eaeSStefan Roese 	unsigned int reg_offset;
85019fc2eaeSStefan Roese 
85119fc2eaeSStefan Roese 	/* Locate the Unicast table entry */
85219fc2eaeSStefan Roese 	last_nibble = (0xf & last_nibble);
85319fc2eaeSStefan Roese 
85419fc2eaeSStefan Roese 	/* offset from unicast tbl base */
85519fc2eaeSStefan Roese 	tbl_offset = (last_nibble / 4) * 4;
85619fc2eaeSStefan Roese 
85719fc2eaeSStefan Roese 	/* offset within the above reg  */
85819fc2eaeSStefan Roese 	reg_offset = last_nibble % 4;
85919fc2eaeSStefan Roese 
86019fc2eaeSStefan Roese 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
86119fc2eaeSStefan Roese 
86219fc2eaeSStefan Roese 	if (queue == -1) {
86319fc2eaeSStefan Roese 		/* Clear accepts frame bit at specified unicast DA tbl entry */
86419fc2eaeSStefan Roese 		unicast_reg &= ~(0xff << (8 * reg_offset));
86519fc2eaeSStefan Roese 	} else {
86619fc2eaeSStefan Roese 		unicast_reg &= ~(0xff << (8 * reg_offset));
86719fc2eaeSStefan Roese 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
86819fc2eaeSStefan Roese 	}
86919fc2eaeSStefan Roese 
87019fc2eaeSStefan Roese 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
87119fc2eaeSStefan Roese }
87219fc2eaeSStefan Roese 
87319fc2eaeSStefan Roese /* Set mac address */
mvneta_mac_addr_set(struct mvneta_port * pp,unsigned char * addr,int queue)87419fc2eaeSStefan Roese static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
87519fc2eaeSStefan Roese 				int queue)
87619fc2eaeSStefan Roese {
87719fc2eaeSStefan Roese 	unsigned int mac_h;
87819fc2eaeSStefan Roese 	unsigned int mac_l;
87919fc2eaeSStefan Roese 
88019fc2eaeSStefan Roese 	if (queue != -1) {
88119fc2eaeSStefan Roese 		mac_l = (addr[4] << 8) | (addr[5]);
88219fc2eaeSStefan Roese 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
88319fc2eaeSStefan Roese 			(addr[2] << 8) | (addr[3] << 0);
88419fc2eaeSStefan Roese 
88519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
88619fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
88719fc2eaeSStefan Roese 	}
88819fc2eaeSStefan Roese 
88919fc2eaeSStefan Roese 	/* Accept frames of this address */
89019fc2eaeSStefan Roese 	mvneta_set_ucast_addr(pp, addr[5], queue);
89119fc2eaeSStefan Roese }
89219fc2eaeSStefan Roese 
89319fc2eaeSStefan Roese /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
mvneta_rx_desc_fill(struct mvneta_rx_desc * rx_desc,u32 phys_addr,u32 cookie)89419fc2eaeSStefan Roese static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
89519fc2eaeSStefan Roese 				u32 phys_addr, u32 cookie)
89619fc2eaeSStefan Roese {
89719fc2eaeSStefan Roese 	rx_desc->buf_cookie = cookie;
89819fc2eaeSStefan Roese 	rx_desc->buf_phys_addr = phys_addr;
89919fc2eaeSStefan Roese }
90019fc2eaeSStefan Roese 
90119fc2eaeSStefan Roese /* Decrement sent descriptors counter */
mvneta_txq_sent_desc_dec(struct mvneta_port * pp,struct mvneta_tx_queue * txq,int sent_desc)90219fc2eaeSStefan Roese static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
90319fc2eaeSStefan Roese 				     struct mvneta_tx_queue *txq,
90419fc2eaeSStefan Roese 				     int sent_desc)
90519fc2eaeSStefan Roese {
90619fc2eaeSStefan Roese 	u32 val;
90719fc2eaeSStefan Roese 
90819fc2eaeSStefan Roese 	/* Only 255 TX descriptors can be updated at once */
90919fc2eaeSStefan Roese 	while (sent_desc > 0xff) {
91019fc2eaeSStefan Roese 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
91119fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
91219fc2eaeSStefan Roese 		sent_desc = sent_desc - 0xff;
91319fc2eaeSStefan Roese 	}
91419fc2eaeSStefan Roese 
91519fc2eaeSStefan Roese 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
91619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
91719fc2eaeSStefan Roese }
91819fc2eaeSStefan Roese 
91919fc2eaeSStefan Roese /* Get number of TX descriptors already sent by HW */
mvneta_txq_sent_desc_num_get(struct mvneta_port * pp,struct mvneta_tx_queue * txq)92019fc2eaeSStefan Roese static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
92119fc2eaeSStefan Roese 					struct mvneta_tx_queue *txq)
92219fc2eaeSStefan Roese {
92319fc2eaeSStefan Roese 	u32 val;
92419fc2eaeSStefan Roese 	int sent_desc;
92519fc2eaeSStefan Roese 
92619fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
92719fc2eaeSStefan Roese 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
92819fc2eaeSStefan Roese 		MVNETA_TXQ_SENT_DESC_SHIFT;
92919fc2eaeSStefan Roese 
93019fc2eaeSStefan Roese 	return sent_desc;
93119fc2eaeSStefan Roese }
93219fc2eaeSStefan Roese 
93319fc2eaeSStefan Roese /* Display more error info */
mvneta_rx_error(struct mvneta_port * pp,struct mvneta_rx_desc * rx_desc)93419fc2eaeSStefan Roese static void mvneta_rx_error(struct mvneta_port *pp,
93519fc2eaeSStefan Roese 			    struct mvneta_rx_desc *rx_desc)
93619fc2eaeSStefan Roese {
93719fc2eaeSStefan Roese 	u32 status = rx_desc->status;
93819fc2eaeSStefan Roese 
93919fc2eaeSStefan Roese 	if (!mvneta_rxq_desc_is_first_last(status)) {
94019fc2eaeSStefan Roese 		netdev_err(pp->dev,
94119fc2eaeSStefan Roese 			   "bad rx status %08x (buffer oversize), size=%d\n",
94219fc2eaeSStefan Roese 			   status, rx_desc->data_size);
94319fc2eaeSStefan Roese 		return;
94419fc2eaeSStefan Roese 	}
94519fc2eaeSStefan Roese 
94619fc2eaeSStefan Roese 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
94719fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_CRC:
94819fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
94919fc2eaeSStefan Roese 			   status, rx_desc->data_size);
95019fc2eaeSStefan Roese 		break;
95119fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_OVERRUN:
95219fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
95319fc2eaeSStefan Roese 			   status, rx_desc->data_size);
95419fc2eaeSStefan Roese 		break;
95519fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_LEN:
95619fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
95719fc2eaeSStefan Roese 			   status, rx_desc->data_size);
95819fc2eaeSStefan Roese 		break;
95919fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_RESOURCE:
96019fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
96119fc2eaeSStefan Roese 			   status, rx_desc->data_size);
96219fc2eaeSStefan Roese 		break;
96319fc2eaeSStefan Roese 	}
96419fc2eaeSStefan Roese }
96519fc2eaeSStefan Roese 
mvneta_rxq_handle_get(struct mvneta_port * pp,int rxq)96619fc2eaeSStefan Roese static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
96719fc2eaeSStefan Roese 						     int rxq)
96819fc2eaeSStefan Roese {
96919fc2eaeSStefan Roese 	return &pp->rxqs[rxq];
97019fc2eaeSStefan Roese }
97119fc2eaeSStefan Roese 
97219fc2eaeSStefan Roese 
97319fc2eaeSStefan Roese /* Drop packets received by the RXQ and free buffers */
mvneta_rxq_drop_pkts(struct mvneta_port * pp,struct mvneta_rx_queue * rxq)97419fc2eaeSStefan Roese static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
97519fc2eaeSStefan Roese 				 struct mvneta_rx_queue *rxq)
97619fc2eaeSStefan Roese {
97719fc2eaeSStefan Roese 	int rx_done;
97819fc2eaeSStefan Roese 
97919fc2eaeSStefan Roese 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
98019fc2eaeSStefan Roese 	if (rx_done)
98119fc2eaeSStefan Roese 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
98219fc2eaeSStefan Roese }
98319fc2eaeSStefan Roese 
98419fc2eaeSStefan Roese /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
mvneta_rxq_fill(struct mvneta_port * pp,struct mvneta_rx_queue * rxq,int num)98519fc2eaeSStefan Roese static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
98619fc2eaeSStefan Roese 			   int num)
98719fc2eaeSStefan Roese {
98819fc2eaeSStefan Roese 	int i;
98919fc2eaeSStefan Roese 
99019fc2eaeSStefan Roese 	for (i = 0; i < num; i++) {
99119fc2eaeSStefan Roese 		u32 addr;
99219fc2eaeSStefan Roese 
99319fc2eaeSStefan Roese 		/* U-Boot special: Fill in the rx buffer addresses */
99419fc2eaeSStefan Roese 		addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
99519fc2eaeSStefan Roese 		mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
99619fc2eaeSStefan Roese 	}
99719fc2eaeSStefan Roese 
99819fc2eaeSStefan Roese 	/* Add this number of RX descriptors as non occupied (ready to
99919fc2eaeSStefan Roese 	 * get packets)
100019fc2eaeSStefan Roese 	 */
100119fc2eaeSStefan Roese 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
100219fc2eaeSStefan Roese 
100319fc2eaeSStefan Roese 	return 0;
100419fc2eaeSStefan Roese }
100519fc2eaeSStefan Roese 
100619fc2eaeSStefan Roese /* Rx/Tx queue initialization/cleanup methods */
100719fc2eaeSStefan Roese 
100819fc2eaeSStefan Roese /* Create a specified RX queue */
mvneta_rxq_init(struct mvneta_port * pp,struct mvneta_rx_queue * rxq)100919fc2eaeSStefan Roese static int mvneta_rxq_init(struct mvneta_port *pp,
101019fc2eaeSStefan Roese 			   struct mvneta_rx_queue *rxq)
101119fc2eaeSStefan Roese 
101219fc2eaeSStefan Roese {
101319fc2eaeSStefan Roese 	rxq->size = pp->rx_ring_size;
101419fc2eaeSStefan Roese 
101519fc2eaeSStefan Roese 	/* Allocate memory for RX descriptors */
101619fc2eaeSStefan Roese 	rxq->descs_phys = (dma_addr_t)rxq->descs;
101719fc2eaeSStefan Roese 	if (rxq->descs == NULL)
101819fc2eaeSStefan Roese 		return -ENOMEM;
101919fc2eaeSStefan Roese 
102019fc2eaeSStefan Roese 	rxq->last_desc = rxq->size - 1;
102119fc2eaeSStefan Roese 
102219fc2eaeSStefan Roese 	/* Set Rx descriptors queue starting address */
102319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
102419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
102519fc2eaeSStefan Roese 
102619fc2eaeSStefan Roese 	/* Fill RXQ with buffers from RX pool */
102719fc2eaeSStefan Roese 	mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
102819fc2eaeSStefan Roese 	mvneta_rxq_fill(pp, rxq, rxq->size);
102919fc2eaeSStefan Roese 
103019fc2eaeSStefan Roese 	return 0;
103119fc2eaeSStefan Roese }
103219fc2eaeSStefan Roese 
103319fc2eaeSStefan Roese /* Cleanup Rx queue */
mvneta_rxq_deinit(struct mvneta_port * pp,struct mvneta_rx_queue * rxq)103419fc2eaeSStefan Roese static void mvneta_rxq_deinit(struct mvneta_port *pp,
103519fc2eaeSStefan Roese 			      struct mvneta_rx_queue *rxq)
103619fc2eaeSStefan Roese {
103719fc2eaeSStefan Roese 	mvneta_rxq_drop_pkts(pp, rxq);
103819fc2eaeSStefan Roese 
103919fc2eaeSStefan Roese 	rxq->descs             = NULL;
104019fc2eaeSStefan Roese 	rxq->last_desc         = 0;
104119fc2eaeSStefan Roese 	rxq->next_desc_to_proc = 0;
104219fc2eaeSStefan Roese 	rxq->descs_phys        = 0;
104319fc2eaeSStefan Roese }
104419fc2eaeSStefan Roese 
104519fc2eaeSStefan Roese /* Create and initialize a tx queue */
mvneta_txq_init(struct mvneta_port * pp,struct mvneta_tx_queue * txq)104619fc2eaeSStefan Roese static int mvneta_txq_init(struct mvneta_port *pp,
104719fc2eaeSStefan Roese 			   struct mvneta_tx_queue *txq)
104819fc2eaeSStefan Roese {
104919fc2eaeSStefan Roese 	txq->size = pp->tx_ring_size;
105019fc2eaeSStefan Roese 
105119fc2eaeSStefan Roese 	/* Allocate memory for TX descriptors */
10523cbc11daSStefan Roese 	txq->descs_phys = (dma_addr_t)txq->descs;
105319fc2eaeSStefan Roese 	if (txq->descs == NULL)
105419fc2eaeSStefan Roese 		return -ENOMEM;
105519fc2eaeSStefan Roese 
105619fc2eaeSStefan Roese 	txq->last_desc = txq->size - 1;
105719fc2eaeSStefan Roese 
105819fc2eaeSStefan Roese 	/* Set maximum bandwidth for enabled TXQs */
105919fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
106019fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
106119fc2eaeSStefan Roese 
106219fc2eaeSStefan Roese 	/* Set Tx descriptors queue starting address */
106319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
106419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
106519fc2eaeSStefan Roese 
106619fc2eaeSStefan Roese 	return 0;
106719fc2eaeSStefan Roese }
106819fc2eaeSStefan Roese 
106919fc2eaeSStefan Roese /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
mvneta_txq_deinit(struct mvneta_port * pp,struct mvneta_tx_queue * txq)107019fc2eaeSStefan Roese static void mvneta_txq_deinit(struct mvneta_port *pp,
107119fc2eaeSStefan Roese 			      struct mvneta_tx_queue *txq)
107219fc2eaeSStefan Roese {
107319fc2eaeSStefan Roese 	txq->descs             = NULL;
107419fc2eaeSStefan Roese 	txq->last_desc         = 0;
107519fc2eaeSStefan Roese 	txq->next_desc_to_proc = 0;
107619fc2eaeSStefan Roese 	txq->descs_phys        = 0;
107719fc2eaeSStefan Roese 
107819fc2eaeSStefan Roese 	/* Set minimum bandwidth for disabled TXQs */
107919fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
108019fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
108119fc2eaeSStefan Roese 
108219fc2eaeSStefan Roese 	/* Set Tx descriptors queue starting address and size */
108319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
108419fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
108519fc2eaeSStefan Roese }
108619fc2eaeSStefan Roese 
108719fc2eaeSStefan Roese /* Cleanup all Tx queues */
mvneta_cleanup_txqs(struct mvneta_port * pp)108819fc2eaeSStefan Roese static void mvneta_cleanup_txqs(struct mvneta_port *pp)
108919fc2eaeSStefan Roese {
109019fc2eaeSStefan Roese 	int queue;
109119fc2eaeSStefan Roese 
109219fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++)
109319fc2eaeSStefan Roese 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
109419fc2eaeSStefan Roese }
109519fc2eaeSStefan Roese 
109619fc2eaeSStefan Roese /* Cleanup all Rx queues */
mvneta_cleanup_rxqs(struct mvneta_port * pp)109719fc2eaeSStefan Roese static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
109819fc2eaeSStefan Roese {
109919fc2eaeSStefan Roese 	int queue;
110019fc2eaeSStefan Roese 
110119fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++)
110219fc2eaeSStefan Roese 		mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
110319fc2eaeSStefan Roese }
110419fc2eaeSStefan Roese 
110519fc2eaeSStefan Roese 
110619fc2eaeSStefan Roese /* Init all Rx queues */
mvneta_setup_rxqs(struct mvneta_port * pp)110719fc2eaeSStefan Roese static int mvneta_setup_rxqs(struct mvneta_port *pp)
110819fc2eaeSStefan Roese {
110919fc2eaeSStefan Roese 	int queue;
111019fc2eaeSStefan Roese 
111119fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
111219fc2eaeSStefan Roese 		int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
111319fc2eaeSStefan Roese 		if (err) {
111419fc2eaeSStefan Roese 			netdev_err(pp->dev, "%s: can't create rxq=%d\n",
111519fc2eaeSStefan Roese 				   __func__, queue);
111619fc2eaeSStefan Roese 			mvneta_cleanup_rxqs(pp);
111719fc2eaeSStefan Roese 			return err;
111819fc2eaeSStefan Roese 		}
111919fc2eaeSStefan Roese 	}
112019fc2eaeSStefan Roese 
112119fc2eaeSStefan Roese 	return 0;
112219fc2eaeSStefan Roese }
112319fc2eaeSStefan Roese 
112419fc2eaeSStefan Roese /* Init all tx queues */
mvneta_setup_txqs(struct mvneta_port * pp)112519fc2eaeSStefan Roese static int mvneta_setup_txqs(struct mvneta_port *pp)
112619fc2eaeSStefan Roese {
112719fc2eaeSStefan Roese 	int queue;
112819fc2eaeSStefan Roese 
112919fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
113019fc2eaeSStefan Roese 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
113119fc2eaeSStefan Roese 		if (err) {
113219fc2eaeSStefan Roese 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
113319fc2eaeSStefan Roese 				   __func__, queue);
113419fc2eaeSStefan Roese 			mvneta_cleanup_txqs(pp);
113519fc2eaeSStefan Roese 			return err;
113619fc2eaeSStefan Roese 		}
113719fc2eaeSStefan Roese 	}
113819fc2eaeSStefan Roese 
113919fc2eaeSStefan Roese 	return 0;
114019fc2eaeSStefan Roese }
114119fc2eaeSStefan Roese 
mvneta_start_dev(struct mvneta_port * pp)114219fc2eaeSStefan Roese static void mvneta_start_dev(struct mvneta_port *pp)
114319fc2eaeSStefan Roese {
114419fc2eaeSStefan Roese 	/* start the Rx/Tx activity */
114519fc2eaeSStefan Roese 	mvneta_port_enable(pp);
114619fc2eaeSStefan Roese }
114719fc2eaeSStefan Roese 
mvneta_adjust_link(struct udevice * dev)1148e3b9c98aSStefan Roese static void mvneta_adjust_link(struct udevice *dev)
114919fc2eaeSStefan Roese {
1150e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
115119fc2eaeSStefan Roese 	struct phy_device *phydev = pp->phydev;
115219fc2eaeSStefan Roese 	int status_change = 0;
115319fc2eaeSStefan Roese 
1154278d30c8SKonstantin Porotchkin 	if (mvneta_port_is_fixed_link(pp)) {
1155278d30c8SKonstantin Porotchkin 		debug("Using fixed link, skip link adjust\n");
1156278d30c8SKonstantin Porotchkin 		return;
1157278d30c8SKonstantin Porotchkin 	}
1158278d30c8SKonstantin Porotchkin 
115919fc2eaeSStefan Roese 	if (phydev->link) {
116019fc2eaeSStefan Roese 		if ((pp->speed != phydev->speed) ||
116119fc2eaeSStefan Roese 		    (pp->duplex != phydev->duplex)) {
116219fc2eaeSStefan Roese 			u32 val;
116319fc2eaeSStefan Roese 
116419fc2eaeSStefan Roese 			val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
116519fc2eaeSStefan Roese 			val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
116619fc2eaeSStefan Roese 				 MVNETA_GMAC_CONFIG_GMII_SPEED |
116719fc2eaeSStefan Roese 				 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
116819fc2eaeSStefan Roese 				 MVNETA_GMAC_AN_SPEED_EN |
116919fc2eaeSStefan Roese 				 MVNETA_GMAC_AN_DUPLEX_EN);
117019fc2eaeSStefan Roese 
117119fc2eaeSStefan Roese 			if (phydev->duplex)
117219fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
117319fc2eaeSStefan Roese 
117419fc2eaeSStefan Roese 			if (phydev->speed == SPEED_1000)
117519fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
117619fc2eaeSStefan Roese 			else
117719fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
117819fc2eaeSStefan Roese 
117919fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
118019fc2eaeSStefan Roese 
118119fc2eaeSStefan Roese 			pp->duplex = phydev->duplex;
118219fc2eaeSStefan Roese 			pp->speed  = phydev->speed;
118319fc2eaeSStefan Roese 		}
118419fc2eaeSStefan Roese 	}
118519fc2eaeSStefan Roese 
118619fc2eaeSStefan Roese 	if (phydev->link != pp->link) {
118719fc2eaeSStefan Roese 		if (!phydev->link) {
118819fc2eaeSStefan Roese 			pp->duplex = -1;
118919fc2eaeSStefan Roese 			pp->speed = 0;
119019fc2eaeSStefan Roese 		}
119119fc2eaeSStefan Roese 
119219fc2eaeSStefan Roese 		pp->link = phydev->link;
119319fc2eaeSStefan Roese 		status_change = 1;
119419fc2eaeSStefan Roese 	}
119519fc2eaeSStefan Roese 
119619fc2eaeSStefan Roese 	if (status_change) {
119719fc2eaeSStefan Roese 		if (phydev->link) {
119819fc2eaeSStefan Roese 			u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
119919fc2eaeSStefan Roese 			val |= (MVNETA_GMAC_FORCE_LINK_PASS |
120019fc2eaeSStefan Roese 				MVNETA_GMAC_FORCE_LINK_DOWN);
120119fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
120219fc2eaeSStefan Roese 			mvneta_port_up(pp);
120319fc2eaeSStefan Roese 		} else {
120419fc2eaeSStefan Roese 			mvneta_port_down(pp);
120519fc2eaeSStefan Roese 		}
120619fc2eaeSStefan Roese 	}
120719fc2eaeSStefan Roese }
120819fc2eaeSStefan Roese 
mvneta_open(struct udevice * dev)1209e3b9c98aSStefan Roese static int mvneta_open(struct udevice *dev)
121019fc2eaeSStefan Roese {
1211e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
121219fc2eaeSStefan Roese 	int ret;
121319fc2eaeSStefan Roese 
121419fc2eaeSStefan Roese 	ret = mvneta_setup_rxqs(pp);
121519fc2eaeSStefan Roese 	if (ret)
121619fc2eaeSStefan Roese 		return ret;
121719fc2eaeSStefan Roese 
121819fc2eaeSStefan Roese 	ret = mvneta_setup_txqs(pp);
121919fc2eaeSStefan Roese 	if (ret)
122019fc2eaeSStefan Roese 		return ret;
122119fc2eaeSStefan Roese 
122219fc2eaeSStefan Roese 	mvneta_adjust_link(dev);
122319fc2eaeSStefan Roese 
122419fc2eaeSStefan Roese 	mvneta_start_dev(pp);
122519fc2eaeSStefan Roese 
122619fc2eaeSStefan Roese 	return 0;
122719fc2eaeSStefan Roese }
122819fc2eaeSStefan Roese 
122919fc2eaeSStefan Roese /* Initialize hw */
mvneta_init2(struct mvneta_port * pp)1230e3b9c98aSStefan Roese static int mvneta_init2(struct mvneta_port *pp)
123119fc2eaeSStefan Roese {
123219fc2eaeSStefan Roese 	int queue;
123319fc2eaeSStefan Roese 
123419fc2eaeSStefan Roese 	/* Disable port */
123519fc2eaeSStefan Roese 	mvneta_port_disable(pp);
123619fc2eaeSStefan Roese 
123719fc2eaeSStefan Roese 	/* Set port default values */
123819fc2eaeSStefan Roese 	mvneta_defaults_set(pp);
123919fc2eaeSStefan Roese 
124019fc2eaeSStefan Roese 	pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
124119fc2eaeSStefan Roese 			   GFP_KERNEL);
124219fc2eaeSStefan Roese 	if (!pp->txqs)
124319fc2eaeSStefan Roese 		return -ENOMEM;
124419fc2eaeSStefan Roese 
124519fc2eaeSStefan Roese 	/* U-Boot special: use preallocated area */
124619fc2eaeSStefan Roese 	pp->txqs[0].descs = buffer_loc.tx_descs;
124719fc2eaeSStefan Roese 
124819fc2eaeSStefan Roese 	/* Initialize TX descriptor rings */
124919fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
125019fc2eaeSStefan Roese 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
125119fc2eaeSStefan Roese 		txq->id = queue;
125219fc2eaeSStefan Roese 		txq->size = pp->tx_ring_size;
125319fc2eaeSStefan Roese 	}
125419fc2eaeSStefan Roese 
125519fc2eaeSStefan Roese 	pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
125619fc2eaeSStefan Roese 			   GFP_KERNEL);
125719fc2eaeSStefan Roese 	if (!pp->rxqs) {
125819fc2eaeSStefan Roese 		kfree(pp->txqs);
125919fc2eaeSStefan Roese 		return -ENOMEM;
126019fc2eaeSStefan Roese 	}
126119fc2eaeSStefan Roese 
126219fc2eaeSStefan Roese 	/* U-Boot special: use preallocated area */
126319fc2eaeSStefan Roese 	pp->rxqs[0].descs = buffer_loc.rx_descs;
126419fc2eaeSStefan Roese 
126519fc2eaeSStefan Roese 	/* Create Rx descriptor rings */
126619fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
126719fc2eaeSStefan Roese 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
126819fc2eaeSStefan Roese 		rxq->id = queue;
126919fc2eaeSStefan Roese 		rxq->size = pp->rx_ring_size;
127019fc2eaeSStefan Roese 	}
127119fc2eaeSStefan Roese 
127219fc2eaeSStefan Roese 	return 0;
127319fc2eaeSStefan Roese }
127419fc2eaeSStefan Roese 
127519fc2eaeSStefan Roese /* platform glue : initialize decoding windows */
1276544eefe0SStefan Roese 
1277544eefe0SStefan Roese /*
1278544eefe0SStefan Roese  * Not like A380, in Armada3700, there are two layers of decode windows for GBE:
1279544eefe0SStefan Roese  * First layer is:  GbE Address window that resides inside the GBE unit,
1280544eefe0SStefan Roese  * Second layer is: Fabric address window which is located in the NIC400
1281544eefe0SStefan Roese  *                  (South Fabric).
1282544eefe0SStefan Roese  * To simplify the address decode configuration for Armada3700, we bypass the
1283544eefe0SStefan Roese  * first layer of GBE decode window by setting the first window to 4GB.
1284544eefe0SStefan Roese  */
mvneta_bypass_mbus_windows(struct mvneta_port * pp)1285544eefe0SStefan Roese static void mvneta_bypass_mbus_windows(struct mvneta_port *pp)
1286544eefe0SStefan Roese {
1287544eefe0SStefan Roese 	/*
1288544eefe0SStefan Roese 	 * Set window size to 4GB, to bypass GBE address decode, leave the
1289544eefe0SStefan Roese 	 * work to MBUS decode window
1290544eefe0SStefan Roese 	 */
1291544eefe0SStefan Roese 	mvreg_write(pp, MVNETA_WIN_SIZE(0), MVNETA_WIN_SIZE_MASK);
1292544eefe0SStefan Roese 
1293544eefe0SStefan Roese 	/* Enable GBE address decode window 0 by set bit 0 to 0 */
1294544eefe0SStefan Roese 	clrbits_le32(pp->base + MVNETA_BASE_ADDR_ENABLE,
1295544eefe0SStefan Roese 		     MVNETA_BASE_ADDR_ENABLE_BIT);
1296544eefe0SStefan Roese 
1297544eefe0SStefan Roese 	/* Set GBE address decode window 0 to full Access (read or write) */
1298544eefe0SStefan Roese 	setbits_le32(pp->base + MVNETA_PORT_ACCESS_PROTECT,
1299544eefe0SStefan Roese 		     MVNETA_PORT_ACCESS_PROTECT_WIN0_RW);
1300544eefe0SStefan Roese }
1301544eefe0SStefan Roese 
mvneta_conf_mbus_windows(struct mvneta_port * pp)130219fc2eaeSStefan Roese static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
130319fc2eaeSStefan Roese {
130419fc2eaeSStefan Roese 	const struct mbus_dram_target_info *dram;
130519fc2eaeSStefan Roese 	u32 win_enable;
130619fc2eaeSStefan Roese 	u32 win_protect;
130719fc2eaeSStefan Roese 	int i;
130819fc2eaeSStefan Roese 
130919fc2eaeSStefan Roese 	dram = mvebu_mbus_dram_info();
131019fc2eaeSStefan Roese 	for (i = 0; i < 6; i++) {
131119fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
131219fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
131319fc2eaeSStefan Roese 
131419fc2eaeSStefan Roese 		if (i < 4)
131519fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
131619fc2eaeSStefan Roese 	}
131719fc2eaeSStefan Roese 
131819fc2eaeSStefan Roese 	win_enable = 0x3f;
131919fc2eaeSStefan Roese 	win_protect = 0;
132019fc2eaeSStefan Roese 
132119fc2eaeSStefan Roese 	for (i = 0; i < dram->num_cs; i++) {
132219fc2eaeSStefan Roese 		const struct mbus_dram_window *cs = dram->cs + i;
132319fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
132419fc2eaeSStefan Roese 			    (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
132519fc2eaeSStefan Roese 
132619fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_SIZE(i),
132719fc2eaeSStefan Roese 			    (cs->size - 1) & 0xffff0000);
132819fc2eaeSStefan Roese 
132919fc2eaeSStefan Roese 		win_enable &= ~(1 << i);
133019fc2eaeSStefan Roese 		win_protect |= 3 << (2 * i);
133119fc2eaeSStefan Roese 	}
133219fc2eaeSStefan Roese 
133319fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
133419fc2eaeSStefan Roese }
133519fc2eaeSStefan Roese 
133619fc2eaeSStefan Roese /* Power up the port */
mvneta_port_power_up(struct mvneta_port * pp,int phy_mode)133719fc2eaeSStefan Roese static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
133819fc2eaeSStefan Roese {
133919fc2eaeSStefan Roese 	u32 ctrl;
134019fc2eaeSStefan Roese 
134119fc2eaeSStefan Roese 	/* MAC Cause register should be cleared */
134219fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
134319fc2eaeSStefan Roese 
134419fc2eaeSStefan Roese 	ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
134519fc2eaeSStefan Roese 
134619fc2eaeSStefan Roese 	/* Even though it might look weird, when we're configured in
134719fc2eaeSStefan Roese 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
134819fc2eaeSStefan Roese 	 */
134919fc2eaeSStefan Roese 	switch (phy_mode) {
135019fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_QSGMII:
135119fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
135219fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
135319fc2eaeSStefan Roese 		break;
135419fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_SGMII:
135519fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
135619fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
135719fc2eaeSStefan Roese 		break;
135819fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_RGMII:
135919fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_RGMII_ID:
136019fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PORT_RGMII;
136119fc2eaeSStefan Roese 		break;
136219fc2eaeSStefan Roese 	default:
136319fc2eaeSStefan Roese 		return -EINVAL;
136419fc2eaeSStefan Roese 	}
136519fc2eaeSStefan Roese 
136619fc2eaeSStefan Roese 	/* Cancel Port Reset */
136719fc2eaeSStefan Roese 	ctrl &= ~MVNETA_GMAC2_PORT_RESET;
136819fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
136919fc2eaeSStefan Roese 
137019fc2eaeSStefan Roese 	while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
137119fc2eaeSStefan Roese 		MVNETA_GMAC2_PORT_RESET) != 0)
137219fc2eaeSStefan Roese 		continue;
137319fc2eaeSStefan Roese 
137419fc2eaeSStefan Roese 	return 0;
137519fc2eaeSStefan Roese }
137619fc2eaeSStefan Roese 
137719fc2eaeSStefan Roese /* Device initialization routine */
mvneta_init(struct udevice * dev)1378e3b9c98aSStefan Roese static int mvneta_init(struct udevice *dev)
137919fc2eaeSStefan Roese {
1380e3b9c98aSStefan Roese 	struct eth_pdata *pdata = dev_get_platdata(dev);
1381e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
138219fc2eaeSStefan Roese 	int err;
138319fc2eaeSStefan Roese 
138419fc2eaeSStefan Roese 	pp->tx_ring_size = MVNETA_MAX_TXD;
138519fc2eaeSStefan Roese 	pp->rx_ring_size = MVNETA_MAX_RXD;
138619fc2eaeSStefan Roese 
1387e3b9c98aSStefan Roese 	err = mvneta_init2(pp);
138819fc2eaeSStefan Roese 	if (err < 0) {
138919fc2eaeSStefan Roese 		dev_err(&pdev->dev, "can't init eth hal\n");
139019fc2eaeSStefan Roese 		return err;
139119fc2eaeSStefan Roese 	}
139219fc2eaeSStefan Roese 
1393e3b9c98aSStefan Roese 	mvneta_mac_addr_set(pp, pdata->enetaddr, rxq_def);
139419fc2eaeSStefan Roese 
139519fc2eaeSStefan Roese 	err = mvneta_port_power_up(pp, pp->phy_interface);
139619fc2eaeSStefan Roese 	if (err < 0) {
139719fc2eaeSStefan Roese 		dev_err(&pdev->dev, "can't power up port\n");
139819fc2eaeSStefan Roese 		return err;
139919fc2eaeSStefan Roese 	}
140019fc2eaeSStefan Roese 
140119fc2eaeSStefan Roese 	/* Call open() now as it needs to be done before runing send() */
140219fc2eaeSStefan Roese 	mvneta_open(dev);
140319fc2eaeSStefan Roese 
140419fc2eaeSStefan Roese 	return 0;
140519fc2eaeSStefan Roese }
140619fc2eaeSStefan Roese 
140719fc2eaeSStefan Roese /* U-Boot only functions follow here */
140819fc2eaeSStefan Roese 
140919fc2eaeSStefan Roese /* SMI / MDIO functions */
141019fc2eaeSStefan Roese 
smi_wait_ready(struct mvneta_port * pp)141119fc2eaeSStefan Roese static int smi_wait_ready(struct mvneta_port *pp)
141219fc2eaeSStefan Roese {
141319fc2eaeSStefan Roese 	u32 timeout = MVNETA_SMI_TIMEOUT;
141419fc2eaeSStefan Roese 	u32 smi_reg;
141519fc2eaeSStefan Roese 
141619fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
141719fc2eaeSStefan Roese 	do {
141819fc2eaeSStefan Roese 		/* read smi register */
141919fc2eaeSStefan Roese 		smi_reg = mvreg_read(pp, MVNETA_SMI);
142019fc2eaeSStefan Roese 		if (timeout-- == 0) {
142119fc2eaeSStefan Roese 			printf("Error: SMI busy timeout\n");
142219fc2eaeSStefan Roese 			return -EFAULT;
142319fc2eaeSStefan Roese 		}
142419fc2eaeSStefan Roese 	} while (smi_reg & MVNETA_SMI_BUSY);
142519fc2eaeSStefan Roese 
142619fc2eaeSStefan Roese 	return 0;
142719fc2eaeSStefan Roese }
142819fc2eaeSStefan Roese 
142919fc2eaeSStefan Roese /*
1430e3b9c98aSStefan Roese  * mvneta_mdio_read - miiphy_read callback function.
143119fc2eaeSStefan Roese  *
143219fc2eaeSStefan Roese  * Returns 16bit phy register value, or 0xffff on error
143319fc2eaeSStefan Roese  */
mvneta_mdio_read(struct mii_dev * bus,int addr,int devad,int reg)1434e3b9c98aSStefan Roese static int mvneta_mdio_read(struct mii_dev *bus, int addr, int devad, int reg)
143519fc2eaeSStefan Roese {
1436e3b9c98aSStefan Roese 	struct mvneta_port *pp = bus->priv;
143719fc2eaeSStefan Roese 	u32 smi_reg;
143819fc2eaeSStefan Roese 	u32 timeout;
143919fc2eaeSStefan Roese 
144019fc2eaeSStefan Roese 	/* check parameters */
1441e3b9c98aSStefan Roese 	if (addr > MVNETA_PHY_ADDR_MASK) {
1442e3b9c98aSStefan Roese 		printf("Error: Invalid PHY address %d\n", addr);
144319fc2eaeSStefan Roese 		return -EFAULT;
144419fc2eaeSStefan Roese 	}
144519fc2eaeSStefan Roese 
1446e3b9c98aSStefan Roese 	if (reg > MVNETA_PHY_REG_MASK) {
1447e3b9c98aSStefan Roese 		printf("Err: Invalid register offset %d\n", reg);
144819fc2eaeSStefan Roese 		return -EFAULT;
144919fc2eaeSStefan Roese 	}
145019fc2eaeSStefan Roese 
145119fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
145219fc2eaeSStefan Roese 	if (smi_wait_ready(pp) < 0)
145319fc2eaeSStefan Roese 		return -EFAULT;
145419fc2eaeSStefan Roese 
145519fc2eaeSStefan Roese 	/* fill the phy address and regiser offset and read opcode */
1456e3b9c98aSStefan Roese 	smi_reg = (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1457e3b9c98aSStefan Roese 		| (reg << MVNETA_SMI_REG_ADDR_OFFS)
145819fc2eaeSStefan Roese 		| MVNETA_SMI_OPCODE_READ;
145919fc2eaeSStefan Roese 
146019fc2eaeSStefan Roese 	/* write the smi register */
146119fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SMI, smi_reg);
146219fc2eaeSStefan Roese 
146319fc2eaeSStefan Roese 	/* wait till read value is ready */
146419fc2eaeSStefan Roese 	timeout = MVNETA_SMI_TIMEOUT;
146519fc2eaeSStefan Roese 
146619fc2eaeSStefan Roese 	do {
146719fc2eaeSStefan Roese 		/* read smi register */
146819fc2eaeSStefan Roese 		smi_reg = mvreg_read(pp, MVNETA_SMI);
146919fc2eaeSStefan Roese 		if (timeout-- == 0) {
147019fc2eaeSStefan Roese 			printf("Err: SMI read ready timeout\n");
147119fc2eaeSStefan Roese 			return -EFAULT;
147219fc2eaeSStefan Roese 		}
147319fc2eaeSStefan Roese 	} while (!(smi_reg & MVNETA_SMI_READ_VALID));
147419fc2eaeSStefan Roese 
147519fc2eaeSStefan Roese 	/* Wait for the data to update in the SMI register */
147619fc2eaeSStefan Roese 	for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
147719fc2eaeSStefan Roese 		;
147819fc2eaeSStefan Roese 
1479e3b9c98aSStefan Roese 	return mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK;
148019fc2eaeSStefan Roese }
148119fc2eaeSStefan Roese 
148219fc2eaeSStefan Roese /*
1483e3b9c98aSStefan Roese  * mvneta_mdio_write - miiphy_write callback function.
148419fc2eaeSStefan Roese  *
148519fc2eaeSStefan Roese  * Returns 0 if write succeed, -EINVAL on bad parameters
148619fc2eaeSStefan Roese  * -ETIME on timeout
148719fc2eaeSStefan Roese  */
mvneta_mdio_write(struct mii_dev * bus,int addr,int devad,int reg,u16 value)1488e3b9c98aSStefan Roese static int mvneta_mdio_write(struct mii_dev *bus, int addr, int devad, int reg,
1489e3b9c98aSStefan Roese 			     u16 value)
149019fc2eaeSStefan Roese {
1491e3b9c98aSStefan Roese 	struct mvneta_port *pp = bus->priv;
149219fc2eaeSStefan Roese 	u32 smi_reg;
149319fc2eaeSStefan Roese 
149419fc2eaeSStefan Roese 	/* check parameters */
1495e3b9c98aSStefan Roese 	if (addr > MVNETA_PHY_ADDR_MASK) {
1496e3b9c98aSStefan Roese 		printf("Error: Invalid PHY address %d\n", addr);
149719fc2eaeSStefan Roese 		return -EFAULT;
149819fc2eaeSStefan Roese 	}
149919fc2eaeSStefan Roese 
1500e3b9c98aSStefan Roese 	if (reg > MVNETA_PHY_REG_MASK) {
1501e3b9c98aSStefan Roese 		printf("Err: Invalid register offset %d\n", reg);
150219fc2eaeSStefan Roese 		return -EFAULT;
150319fc2eaeSStefan Roese 	}
150419fc2eaeSStefan Roese 
150519fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
150619fc2eaeSStefan Roese 	if (smi_wait_ready(pp) < 0)
150719fc2eaeSStefan Roese 		return -EFAULT;
150819fc2eaeSStefan Roese 
150919fc2eaeSStefan Roese 	/* fill the phy addr and reg offset and write opcode and data */
1510e3b9c98aSStefan Roese 	smi_reg = value << MVNETA_SMI_DATA_OFFS;
1511e3b9c98aSStefan Roese 	smi_reg |= (addr << MVNETA_SMI_DEV_ADDR_OFFS)
1512e3b9c98aSStefan Roese 		| (reg << MVNETA_SMI_REG_ADDR_OFFS);
151319fc2eaeSStefan Roese 	smi_reg &= ~MVNETA_SMI_OPCODE_READ;
151419fc2eaeSStefan Roese 
151519fc2eaeSStefan Roese 	/* write the smi register */
151619fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SMI, smi_reg);
151719fc2eaeSStefan Roese 
151819fc2eaeSStefan Roese 	return 0;
151919fc2eaeSStefan Roese }
152019fc2eaeSStefan Roese 
mvneta_start(struct udevice * dev)1521e3b9c98aSStefan Roese static int mvneta_start(struct udevice *dev)
152219fc2eaeSStefan Roese {
1523e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
152419fc2eaeSStefan Roese 	struct phy_device *phydev;
152519fc2eaeSStefan Roese 
152619fc2eaeSStefan Roese 	mvneta_port_power_up(pp, pp->phy_interface);
152719fc2eaeSStefan Roese 
152819fc2eaeSStefan Roese 	if (!pp->init || pp->link == 0) {
1529278d30c8SKonstantin Porotchkin 		if (mvneta_port_is_fixed_link(pp)) {
1530278d30c8SKonstantin Porotchkin 			u32 val;
1531278d30c8SKonstantin Porotchkin 
1532278d30c8SKonstantin Porotchkin 			pp->init = 1;
1533278d30c8SKonstantin Porotchkin 			pp->link = 1;
1534278d30c8SKonstantin Porotchkin 			mvneta_init(dev);
1535278d30c8SKonstantin Porotchkin 
1536278d30c8SKonstantin Porotchkin 			val = MVNETA_GMAC_FORCE_LINK_UP |
1537278d30c8SKonstantin Porotchkin 			      MVNETA_GMAC_IB_BYPASS_AN_EN |
1538278d30c8SKonstantin Porotchkin 			      MVNETA_GMAC_SET_FC_EN |
1539278d30c8SKonstantin Porotchkin 			      MVNETA_GMAC_ADVERT_FC_EN |
1540278d30c8SKonstantin Porotchkin 			      MVNETA_GMAC_SAMPLE_TX_CFG_EN;
1541278d30c8SKonstantin Porotchkin 
1542278d30c8SKonstantin Porotchkin 			if (pp->duplex)
1543278d30c8SKonstantin Porotchkin 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1544278d30c8SKonstantin Porotchkin 
1545278d30c8SKonstantin Porotchkin 			if (pp->speed == SPEED_1000)
1546278d30c8SKonstantin Porotchkin 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1547278d30c8SKonstantin Porotchkin 			else if (pp->speed == SPEED_100)
1548278d30c8SKonstantin Porotchkin 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1549278d30c8SKonstantin Porotchkin 
1550278d30c8SKonstantin Porotchkin 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1551278d30c8SKonstantin Porotchkin 		} else {
155219fc2eaeSStefan Roese 			/* Set phy address of the port */
155319fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_PHY_ADDR, pp->phyaddr);
1554278d30c8SKonstantin Porotchkin 
155519fc2eaeSStefan Roese 			phydev = phy_connect(pp->bus, pp->phyaddr, dev,
155619fc2eaeSStefan Roese 					     pp->phy_interface);
155719fc2eaeSStefan Roese 
155819fc2eaeSStefan Roese 			pp->phydev = phydev;
155919fc2eaeSStefan Roese 			phy_config(phydev);
156019fc2eaeSStefan Roese 			phy_startup(phydev);
156119fc2eaeSStefan Roese 			if (!phydev->link) {
156219fc2eaeSStefan Roese 				printf("%s: No link.\n", phydev->dev->name);
156319fc2eaeSStefan Roese 				return -1;
156419fc2eaeSStefan Roese 			}
156519fc2eaeSStefan Roese 
156619fc2eaeSStefan Roese 			/* Full init on first call */
1567e3b9c98aSStefan Roese 			mvneta_init(dev);
156819fc2eaeSStefan Roese 			pp->init = 1;
1569278d30c8SKonstantin Porotchkin 			return 0;
1570278d30c8SKonstantin Porotchkin 		}
1571278d30c8SKonstantin Porotchkin 	}
1572278d30c8SKonstantin Porotchkin 
157319fc2eaeSStefan Roese 	/* Upon all following calls, this is enough */
157419fc2eaeSStefan Roese 	mvneta_port_up(pp);
157519fc2eaeSStefan Roese 	mvneta_port_enable(pp);
157619fc2eaeSStefan Roese 
157719fc2eaeSStefan Roese 	return 0;
157819fc2eaeSStefan Roese }
157919fc2eaeSStefan Roese 
mvneta_send(struct udevice * dev,void * packet,int length)1580e3b9c98aSStefan Roese static int mvneta_send(struct udevice *dev, void *packet, int length)
158119fc2eaeSStefan Roese {
1582e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
158319fc2eaeSStefan Roese 	struct mvneta_tx_queue *txq = &pp->txqs[0];
158419fc2eaeSStefan Roese 	struct mvneta_tx_desc *tx_desc;
158519fc2eaeSStefan Roese 	int sent_desc;
158619fc2eaeSStefan Roese 	u32 timeout = 0;
158719fc2eaeSStefan Roese 
158819fc2eaeSStefan Roese 	/* Get a descriptor for the first part of the packet */
158919fc2eaeSStefan Roese 	tx_desc = mvneta_txq_next_desc_get(txq);
159019fc2eaeSStefan Roese 
15913cbc11daSStefan Roese 	tx_desc->buf_phys_addr = (u32)(uintptr_t)packet;
1592e3b9c98aSStefan Roese 	tx_desc->data_size = length;
15933cbc11daSStefan Roese 	flush_dcache_range((ulong)packet,
15943cbc11daSStefan Roese 			   (ulong)packet + ALIGN(length, PKTALIGN));
159519fc2eaeSStefan Roese 
159619fc2eaeSStefan Roese 	/* First and Last descriptor */
159719fc2eaeSStefan Roese 	tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
159819fc2eaeSStefan Roese 	mvneta_txq_pend_desc_add(pp, txq, 1);
159919fc2eaeSStefan Roese 
160019fc2eaeSStefan Roese 	/* Wait for packet to be sent (queue might help with speed here) */
160119fc2eaeSStefan Roese 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
160219fc2eaeSStefan Roese 	while (!sent_desc) {
160319fc2eaeSStefan Roese 		if (timeout++ > 10000) {
160419fc2eaeSStefan Roese 			printf("timeout: packet not sent\n");
160519fc2eaeSStefan Roese 			return -1;
160619fc2eaeSStefan Roese 		}
160719fc2eaeSStefan Roese 		sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
160819fc2eaeSStefan Roese 	}
160919fc2eaeSStefan Roese 
161019fc2eaeSStefan Roese 	/* txDone has increased - hw sent packet */
161119fc2eaeSStefan Roese 	mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
161219fc2eaeSStefan Roese 
161319fc2eaeSStefan Roese 	return 0;
161419fc2eaeSStefan Roese }
161519fc2eaeSStefan Roese 
mvneta_recv(struct udevice * dev,int flags,uchar ** packetp)1616e3b9c98aSStefan Roese static int mvneta_recv(struct udevice *dev, int flags, uchar **packetp)
161719fc2eaeSStefan Roese {
1618e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
161919fc2eaeSStefan Roese 	int rx_done;
162019fc2eaeSStefan Roese 	struct mvneta_rx_queue *rxq;
1621e3b9c98aSStefan Roese 	int rx_bytes = 0;
162219fc2eaeSStefan Roese 
162319fc2eaeSStefan Roese 	/* get rx queue */
162419fc2eaeSStefan Roese 	rxq = mvneta_rxq_handle_get(pp, rxq_def);
162519fc2eaeSStefan Roese 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
162619fc2eaeSStefan Roese 
1627e3b9c98aSStefan Roese 	if (rx_done) {
162819fc2eaeSStefan Roese 		struct mvneta_rx_desc *rx_desc;
162919fc2eaeSStefan Roese 		unsigned char *data;
163019fc2eaeSStefan Roese 		u32 rx_status;
163119fc2eaeSStefan Roese 
163219fc2eaeSStefan Roese 		/*
163319fc2eaeSStefan Roese 		 * No cache invalidation needed here, since the desc's are
163419fc2eaeSStefan Roese 		 * located in a uncached memory region
163519fc2eaeSStefan Roese 		 */
163619fc2eaeSStefan Roese 		rx_desc = mvneta_rxq_next_desc_get(rxq);
163719fc2eaeSStefan Roese 
163819fc2eaeSStefan Roese 		rx_status = rx_desc->status;
163919fc2eaeSStefan Roese 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
164019fc2eaeSStefan Roese 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
164119fc2eaeSStefan Roese 			mvneta_rx_error(pp, rx_desc);
164219fc2eaeSStefan Roese 			/* leave the descriptor untouched */
1643e3b9c98aSStefan Roese 			return -EIO;
164419fc2eaeSStefan Roese 		}
164519fc2eaeSStefan Roese 
164619fc2eaeSStefan Roese 		/* 2 bytes for marvell header. 4 bytes for crc */
164719fc2eaeSStefan Roese 		rx_bytes = rx_desc->data_size - 6;
164819fc2eaeSStefan Roese 
164919fc2eaeSStefan Roese 		/* give packet to stack - skip on first 2 bytes */
16503cbc11daSStefan Roese 		data = (u8 *)(uintptr_t)rx_desc->buf_cookie + 2;
165119fc2eaeSStefan Roese 		/*
165219fc2eaeSStefan Roese 		 * No cache invalidation needed here, since the rx_buffer's are
165319fc2eaeSStefan Roese 		 * located in a uncached memory region
165419fc2eaeSStefan Roese 		 */
1655e3b9c98aSStefan Roese 		*packetp = data;
165619fc2eaeSStefan Roese 
165719fc2eaeSStefan Roese 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
165819fc2eaeSStefan Roese 	}
165919fc2eaeSStefan Roese 
1660e3b9c98aSStefan Roese 	return rx_bytes;
166119fc2eaeSStefan Roese }
166219fc2eaeSStefan Roese 
mvneta_probe(struct udevice * dev)1663e3b9c98aSStefan Roese static int mvneta_probe(struct udevice *dev)
166419fc2eaeSStefan Roese {
1665e3b9c98aSStefan Roese 	struct eth_pdata *pdata = dev_get_platdata(dev);
1666e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
1667e3b9c98aSStefan Roese 	void *blob = (void *)gd->fdt_blob;
1668e160f7d4SSimon Glass 	int node = dev_of_offset(dev);
1669e3b9c98aSStefan Roese 	struct mii_dev *bus;
1670e3b9c98aSStefan Roese 	unsigned long addr;
167119fc2eaeSStefan Roese 	void *bd_space;
1672976feda2SKonstantin Porotchkin 	int ret;
1673278d30c8SKonstantin Porotchkin 	int fl_node;
167419fc2eaeSStefan Roese 
167519fc2eaeSStefan Roese 	/*
167619fc2eaeSStefan Roese 	 * Allocate buffer area for descs and rx_buffers. This is only
167719fc2eaeSStefan Roese 	 * done once for all interfaces. As only one interface can
16786723b235SChris Packham 	 * be active. Make this area DMA safe by disabling the D-cache
167919fc2eaeSStefan Roese 	 */
168019fc2eaeSStefan Roese 	if (!buffer_loc.tx_descs) {
168119fc2eaeSStefan Roese 		/* Align buffer area for descs and rx_buffers to 1MiB */
168219fc2eaeSStefan Roese 		bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
16833cbc11daSStefan Roese 		mmu_set_region_dcache_behaviour((phys_addr_t)bd_space, BD_SPACE,
168419fc2eaeSStefan Roese 						DCACHE_OFF);
168519fc2eaeSStefan Roese 		buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
168619fc2eaeSStefan Roese 		buffer_loc.rx_descs = (struct mvneta_rx_desc *)
16873cbc11daSStefan Roese 			((phys_addr_t)bd_space +
168819fc2eaeSStefan Roese 			 MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc));
16893cbc11daSStefan Roese 		buffer_loc.rx_buffers = (phys_addr_t)
169019fc2eaeSStefan Roese 			(bd_space +
169119fc2eaeSStefan Roese 			 MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc) +
169219fc2eaeSStefan Roese 			 MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc));
169319fc2eaeSStefan Roese 	}
169419fc2eaeSStefan Roese 
1695e3b9c98aSStefan Roese 	pp->base = (void __iomem *)pdata->iobase;
169619fc2eaeSStefan Roese 
1697e3b9c98aSStefan Roese 	/* Configure MBUS address windows */
1698911f3aefSSimon Glass 	if (device_is_compatible(dev, "marvell,armada-3700-neta"))
1699544eefe0SStefan Roese 		mvneta_bypass_mbus_windows(pp);
1700544eefe0SStefan Roese 	else
1701e3b9c98aSStefan Roese 		mvneta_conf_mbus_windows(pp);
170219fc2eaeSStefan Roese 
1703e3b9c98aSStefan Roese 	/* PHY interface is already decoded in mvneta_ofdata_to_platdata() */
1704e3b9c98aSStefan Roese 	pp->phy_interface = pdata->phy_interface;
170519fc2eaeSStefan Roese 
1706278d30c8SKonstantin Porotchkin 	/* fetch 'fixed-link' property from 'neta' node */
1707278d30c8SKonstantin Porotchkin 	fl_node = fdt_subnode_offset(blob, node, "fixed-link");
1708278d30c8SKonstantin Porotchkin 	if (fl_node != -FDT_ERR_NOTFOUND) {
1709278d30c8SKonstantin Porotchkin 		/* set phy_addr to invalid value for fixed link */
1710278d30c8SKonstantin Porotchkin 		pp->phyaddr = PHY_MAX_ADDR + 1;
1711278d30c8SKonstantin Porotchkin 		pp->duplex = fdtdec_get_bool(blob, fl_node, "full-duplex");
1712278d30c8SKonstantin Porotchkin 		pp->speed = fdtdec_get_int(blob, fl_node, "speed", 0);
1713278d30c8SKonstantin Porotchkin 	} else {
1714e3b9c98aSStefan Roese 		/* Now read phyaddr from DT */
1715e3b9c98aSStefan Roese 		addr = fdtdec_get_int(blob, node, "phy", 0);
1716e3b9c98aSStefan Roese 		addr = fdt_node_offset_by_phandle(blob, addr);
1717e3b9c98aSStefan Roese 		pp->phyaddr = fdtdec_get_int(blob, addr, "reg", 0);
1718278d30c8SKonstantin Porotchkin 	}
171919fc2eaeSStefan Roese 
1720e3b9c98aSStefan Roese 	bus = mdio_alloc();
1721e3b9c98aSStefan Roese 	if (!bus) {
1722e3b9c98aSStefan Roese 		printf("Failed to allocate MDIO bus\n");
1723e3b9c98aSStefan Roese 		return -ENOMEM;
172419fc2eaeSStefan Roese 	}
1725e3b9c98aSStefan Roese 
1726e3b9c98aSStefan Roese 	bus->read = mvneta_mdio_read;
1727e3b9c98aSStefan Roese 	bus->write = mvneta_mdio_write;
1728e3b9c98aSStefan Roese 	snprintf(bus->name, sizeof(bus->name), dev->name);
1729e3b9c98aSStefan Roese 	bus->priv = (void *)pp;
1730e3b9c98aSStefan Roese 	pp->bus = bus;
1731e3b9c98aSStefan Roese 
1732976feda2SKonstantin Porotchkin 	ret = mdio_register(bus);
1733976feda2SKonstantin Porotchkin 	if (ret)
1734976feda2SKonstantin Porotchkin 		return ret;
1735976feda2SKonstantin Porotchkin 
1736976feda2SKonstantin Porotchkin 	return board_network_enable(bus);
1737e3b9c98aSStefan Roese }
1738e3b9c98aSStefan Roese 
mvneta_stop(struct udevice * dev)1739e3b9c98aSStefan Roese static void mvneta_stop(struct udevice *dev)
1740e3b9c98aSStefan Roese {
1741e3b9c98aSStefan Roese 	struct mvneta_port *pp = dev_get_priv(dev);
1742e3b9c98aSStefan Roese 
1743e3b9c98aSStefan Roese 	mvneta_port_down(pp);
1744e3b9c98aSStefan Roese 	mvneta_port_disable(pp);
1745e3b9c98aSStefan Roese }
1746e3b9c98aSStefan Roese 
1747e3b9c98aSStefan Roese static const struct eth_ops mvneta_ops = {
1748e3b9c98aSStefan Roese 	.start		= mvneta_start,
1749e3b9c98aSStefan Roese 	.send		= mvneta_send,
1750e3b9c98aSStefan Roese 	.recv		= mvneta_recv,
1751e3b9c98aSStefan Roese 	.stop		= mvneta_stop,
1752e3b9c98aSStefan Roese };
1753e3b9c98aSStefan Roese 
mvneta_ofdata_to_platdata(struct udevice * dev)1754e3b9c98aSStefan Roese static int mvneta_ofdata_to_platdata(struct udevice *dev)
1755e3b9c98aSStefan Roese {
1756e3b9c98aSStefan Roese 	struct eth_pdata *pdata = dev_get_platdata(dev);
1757e3b9c98aSStefan Roese 	const char *phy_mode;
1758e3b9c98aSStefan Roese 
1759a821c4afSSimon Glass 	pdata->iobase = devfdt_get_addr(dev);
1760e3b9c98aSStefan Roese 
1761e3b9c98aSStefan Roese 	/* Get phy-mode / phy_interface from DT */
1762e3b9c98aSStefan Roese 	pdata->phy_interface = -1;
1763e160f7d4SSimon Glass 	phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
1764e160f7d4SSimon Glass 			       NULL);
1765e3b9c98aSStefan Roese 	if (phy_mode)
1766e3b9c98aSStefan Roese 		pdata->phy_interface = phy_get_interface_by_name(phy_mode);
1767e3b9c98aSStefan Roese 	if (pdata->phy_interface == -1) {
1768e3b9c98aSStefan Roese 		debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
1769e3b9c98aSStefan Roese 		return -EINVAL;
1770e3b9c98aSStefan Roese 	}
1771e3b9c98aSStefan Roese 
1772e3b9c98aSStefan Roese 	return 0;
1773e3b9c98aSStefan Roese }
1774e3b9c98aSStefan Roese 
1775e3b9c98aSStefan Roese static const struct udevice_id mvneta_ids[] = {
1776e3b9c98aSStefan Roese 	{ .compatible = "marvell,armada-370-neta" },
1777e3b9c98aSStefan Roese 	{ .compatible = "marvell,armada-xp-neta" },
1778544eefe0SStefan Roese 	{ .compatible = "marvell,armada-3700-neta" },
1779e3b9c98aSStefan Roese 	{ }
1780e3b9c98aSStefan Roese };
1781e3b9c98aSStefan Roese 
1782e3b9c98aSStefan Roese U_BOOT_DRIVER(mvneta) = {
1783e3b9c98aSStefan Roese 	.name	= "mvneta",
1784e3b9c98aSStefan Roese 	.id	= UCLASS_ETH,
1785e3b9c98aSStefan Roese 	.of_match = mvneta_ids,
1786e3b9c98aSStefan Roese 	.ofdata_to_platdata = mvneta_ofdata_to_platdata,
1787e3b9c98aSStefan Roese 	.probe	= mvneta_probe,
1788e3b9c98aSStefan Roese 	.ops	= &mvneta_ops,
1789e3b9c98aSStefan Roese 	.priv_auto_alloc_size = sizeof(struct mvneta_port),
1790e3b9c98aSStefan Roese 	.platdata_auto_alloc_size = sizeof(struct eth_pdata),
1791e3b9c98aSStefan Roese };
1792