xref: /rk3399_rockchip-uboot/drivers/net/mvneta.c (revision 19fc2eae6defbf1fba494a523749dd69a80cc58b)
1*19fc2eaeSStefan Roese /*
2*19fc2eaeSStefan Roese  * Driver for Marvell NETA network card for Armada XP and Armada 370 SoCs.
3*19fc2eaeSStefan Roese  *
4*19fc2eaeSStefan Roese  * U-Boot version:
5*19fc2eaeSStefan Roese  * Copyright (C) 2014 Stefan Roese <sr@denx.de>
6*19fc2eaeSStefan Roese  *
7*19fc2eaeSStefan Roese  * Based on the Linux version which is:
8*19fc2eaeSStefan Roese  * Copyright (C) 2012 Marvell
9*19fc2eaeSStefan Roese  *
10*19fc2eaeSStefan Roese  * Rami Rosen <rosenr@marvell.com>
11*19fc2eaeSStefan Roese  * Thomas Petazzoni <thomas.petazzoni@free-electrons.com>
12*19fc2eaeSStefan Roese  *
13*19fc2eaeSStefan Roese  * SPDX-License-Identifier:	GPL-2.0
14*19fc2eaeSStefan Roese  */
15*19fc2eaeSStefan Roese 
16*19fc2eaeSStefan Roese #include <common.h>
17*19fc2eaeSStefan Roese #include <net.h>
18*19fc2eaeSStefan Roese #include <netdev.h>
19*19fc2eaeSStefan Roese #include <config.h>
20*19fc2eaeSStefan Roese #include <malloc.h>
21*19fc2eaeSStefan Roese #include <asm/io.h>
22*19fc2eaeSStefan Roese #include <asm/errno.h>
23*19fc2eaeSStefan Roese #include <phy.h>
24*19fc2eaeSStefan Roese #include <miiphy.h>
25*19fc2eaeSStefan Roese #include <watchdog.h>
26*19fc2eaeSStefan Roese #include <asm/arch/cpu.h>
27*19fc2eaeSStefan Roese #include <asm/arch/soc.h>
28*19fc2eaeSStefan Roese #include <linux/compat.h>
29*19fc2eaeSStefan Roese #include <linux/mbus.h>
30*19fc2eaeSStefan Roese 
31*19fc2eaeSStefan Roese #if !defined(CONFIG_PHYLIB)
32*19fc2eaeSStefan Roese # error Marvell mvneta requires PHYLIB
33*19fc2eaeSStefan Roese #endif
34*19fc2eaeSStefan Roese 
35*19fc2eaeSStefan Roese /* Some linux -> U-Boot compatibility stuff */
36*19fc2eaeSStefan Roese #define netdev_err(dev, fmt, args...)		\
37*19fc2eaeSStefan Roese 	printf(fmt, ##args)
38*19fc2eaeSStefan Roese #define netdev_warn(dev, fmt, args...)		\
39*19fc2eaeSStefan Roese 	printf(fmt, ##args)
40*19fc2eaeSStefan Roese #define netdev_info(dev, fmt, args...)		\
41*19fc2eaeSStefan Roese 	printf(fmt, ##args)
42*19fc2eaeSStefan Roese 
43*19fc2eaeSStefan Roese #define CONFIG_NR_CPUS		1
44*19fc2eaeSStefan Roese #define BIT(nr)			(1UL << (nr))
45*19fc2eaeSStefan Roese #define ETH_HLEN		14	/* Total octets in header */
46*19fc2eaeSStefan Roese 
47*19fc2eaeSStefan Roese /* 2(HW hdr) 14(MAC hdr) 4(CRC) 32(extra for cache prefetch) */
48*19fc2eaeSStefan Roese #define WRAP			(2 + ETH_HLEN + 4 + 32)
49*19fc2eaeSStefan Roese #define MTU			1500
50*19fc2eaeSStefan Roese #define RX_BUFFER_SIZE		(ALIGN(MTU + WRAP, ARCH_DMA_MINALIGN))
51*19fc2eaeSStefan Roese 
52*19fc2eaeSStefan Roese #define MVNETA_SMI_TIMEOUT			10000
53*19fc2eaeSStefan Roese 
54*19fc2eaeSStefan Roese /* Registers */
55*19fc2eaeSStefan Roese #define MVNETA_RXQ_CONFIG_REG(q)                (0x1400 + ((q) << 2))
56*19fc2eaeSStefan Roese #define	     MVNETA_RXQ_HW_BUF_ALLOC            BIT(1)
57*19fc2eaeSStefan Roese #define      MVNETA_RXQ_PKT_OFFSET_ALL_MASK     (0xf    << 8)
58*19fc2eaeSStefan Roese #define      MVNETA_RXQ_PKT_OFFSET_MASK(offs)   ((offs) << 8)
59*19fc2eaeSStefan Roese #define MVNETA_RXQ_THRESHOLD_REG(q)             (0x14c0 + ((q) << 2))
60*19fc2eaeSStefan Roese #define      MVNETA_RXQ_NON_OCCUPIED(v)         ((v) << 16)
61*19fc2eaeSStefan Roese #define MVNETA_RXQ_BASE_ADDR_REG(q)             (0x1480 + ((q) << 2))
62*19fc2eaeSStefan Roese #define MVNETA_RXQ_SIZE_REG(q)                  (0x14a0 + ((q) << 2))
63*19fc2eaeSStefan Roese #define      MVNETA_RXQ_BUF_SIZE_SHIFT          19
64*19fc2eaeSStefan Roese #define      MVNETA_RXQ_BUF_SIZE_MASK           (0x1fff << 19)
65*19fc2eaeSStefan Roese #define MVNETA_RXQ_STATUS_REG(q)                (0x14e0 + ((q) << 2))
66*19fc2eaeSStefan Roese #define      MVNETA_RXQ_OCCUPIED_ALL_MASK       0x3fff
67*19fc2eaeSStefan Roese #define MVNETA_RXQ_STATUS_UPDATE_REG(q)         (0x1500 + ((q) << 2))
68*19fc2eaeSStefan Roese #define      MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT  16
69*19fc2eaeSStefan Roese #define      MVNETA_RXQ_ADD_NON_OCCUPIED_MAX    255
70*19fc2eaeSStefan Roese #define MVNETA_PORT_RX_RESET                    0x1cc0
71*19fc2eaeSStefan Roese #define      MVNETA_PORT_RX_DMA_RESET           BIT(0)
72*19fc2eaeSStefan Roese #define MVNETA_PHY_ADDR                         0x2000
73*19fc2eaeSStefan Roese #define      MVNETA_PHY_ADDR_MASK               0x1f
74*19fc2eaeSStefan Roese #define MVNETA_SMI                              0x2004
75*19fc2eaeSStefan Roese #define      MVNETA_PHY_REG_MASK                0x1f
76*19fc2eaeSStefan Roese /* SMI register fields */
77*19fc2eaeSStefan Roese #define     MVNETA_SMI_DATA_OFFS		0	/* Data */
78*19fc2eaeSStefan Roese #define     MVNETA_SMI_DATA_MASK		(0xffff << MVNETA_SMI_DATA_OFFS)
79*19fc2eaeSStefan Roese #define     MVNETA_SMI_DEV_ADDR_OFFS		16	/* PHY device address */
80*19fc2eaeSStefan Roese #define     MVNETA_SMI_REG_ADDR_OFFS		21	/* PHY device reg addr*/
81*19fc2eaeSStefan Roese #define     MVNETA_SMI_OPCODE_OFFS		26	/* Write/Read opcode */
82*19fc2eaeSStefan Roese #define     MVNETA_SMI_OPCODE_READ		(1 << MVNETA_SMI_OPCODE_OFFS)
83*19fc2eaeSStefan Roese #define     MVNETA_SMI_READ_VALID		(1 << 27)	/* Read Valid */
84*19fc2eaeSStefan Roese #define     MVNETA_SMI_BUSY			(1 << 28)	/* Busy */
85*19fc2eaeSStefan Roese #define MVNETA_MBUS_RETRY                       0x2010
86*19fc2eaeSStefan Roese #define MVNETA_UNIT_INTR_CAUSE                  0x2080
87*19fc2eaeSStefan Roese #define MVNETA_UNIT_CONTROL                     0x20B0
88*19fc2eaeSStefan Roese #define      MVNETA_PHY_POLLING_ENABLE          BIT(1)
89*19fc2eaeSStefan Roese #define MVNETA_WIN_BASE(w)                      (0x2200 + ((w) << 3))
90*19fc2eaeSStefan Roese #define MVNETA_WIN_SIZE(w)                      (0x2204 + ((w) << 3))
91*19fc2eaeSStefan Roese #define MVNETA_WIN_REMAP(w)                     (0x2280 + ((w) << 2))
92*19fc2eaeSStefan Roese #define MVNETA_BASE_ADDR_ENABLE                 0x2290
93*19fc2eaeSStefan Roese #define MVNETA_PORT_CONFIG                      0x2400
94*19fc2eaeSStefan Roese #define      MVNETA_UNI_PROMISC_MODE            BIT(0)
95*19fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ(q)                  ((q) << 1)
96*19fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_ARP(q)              ((q) << 4)
97*19fc2eaeSStefan Roese #define      MVNETA_TX_UNSET_ERR_SUM            BIT(12)
98*19fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_TCP(q)              ((q) << 16)
99*19fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_UDP(q)              ((q) << 19)
100*19fc2eaeSStefan Roese #define      MVNETA_DEF_RXQ_BPDU(q)             ((q) << 22)
101*19fc2eaeSStefan Roese #define      MVNETA_RX_CSUM_WITH_PSEUDO_HDR     BIT(25)
102*19fc2eaeSStefan Roese #define      MVNETA_PORT_CONFIG_DEFL_VALUE(q)   (MVNETA_DEF_RXQ(q)       | \
103*19fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_ARP(q)	 | \
104*19fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_TCP(q)	 | \
105*19fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_UDP(q)	 | \
106*19fc2eaeSStefan Roese 						 MVNETA_DEF_RXQ_BPDU(q)	 | \
107*19fc2eaeSStefan Roese 						 MVNETA_TX_UNSET_ERR_SUM | \
108*19fc2eaeSStefan Roese 						 MVNETA_RX_CSUM_WITH_PSEUDO_HDR)
109*19fc2eaeSStefan Roese #define MVNETA_PORT_CONFIG_EXTEND                0x2404
110*19fc2eaeSStefan Roese #define MVNETA_MAC_ADDR_LOW                      0x2414
111*19fc2eaeSStefan Roese #define MVNETA_MAC_ADDR_HIGH                     0x2418
112*19fc2eaeSStefan Roese #define MVNETA_SDMA_CONFIG                       0x241c
113*19fc2eaeSStefan Roese #define      MVNETA_SDMA_BRST_SIZE_16            4
114*19fc2eaeSStefan Roese #define      MVNETA_RX_BRST_SZ_MASK(burst)       ((burst) << 1)
115*19fc2eaeSStefan Roese #define      MVNETA_RX_NO_DATA_SWAP              BIT(4)
116*19fc2eaeSStefan Roese #define      MVNETA_TX_NO_DATA_SWAP              BIT(5)
117*19fc2eaeSStefan Roese #define      MVNETA_DESC_SWAP                    BIT(6)
118*19fc2eaeSStefan Roese #define      MVNETA_TX_BRST_SZ_MASK(burst)       ((burst) << 22)
119*19fc2eaeSStefan Roese #define MVNETA_PORT_STATUS                       0x2444
120*19fc2eaeSStefan Roese #define      MVNETA_TX_IN_PRGRS                  BIT(1)
121*19fc2eaeSStefan Roese #define      MVNETA_TX_FIFO_EMPTY                BIT(8)
122*19fc2eaeSStefan Roese #define MVNETA_RX_MIN_FRAME_SIZE                 0x247c
123*19fc2eaeSStefan Roese #define MVNETA_SERDES_CFG			 0x24A0
124*19fc2eaeSStefan Roese #define      MVNETA_SGMII_SERDES_PROTO		 0x0cc7
125*19fc2eaeSStefan Roese #define      MVNETA_QSGMII_SERDES_PROTO		 0x0667
126*19fc2eaeSStefan Roese #define MVNETA_TYPE_PRIO                         0x24bc
127*19fc2eaeSStefan Roese #define      MVNETA_FORCE_UNI                    BIT(21)
128*19fc2eaeSStefan Roese #define MVNETA_TXQ_CMD_1                         0x24e4
129*19fc2eaeSStefan Roese #define MVNETA_TXQ_CMD                           0x2448
130*19fc2eaeSStefan Roese #define      MVNETA_TXQ_DISABLE_SHIFT            8
131*19fc2eaeSStefan Roese #define      MVNETA_TXQ_ENABLE_MASK              0x000000ff
132*19fc2eaeSStefan Roese #define MVNETA_ACC_MODE                          0x2500
133*19fc2eaeSStefan Roese #define MVNETA_CPU_MAP(cpu)                      (0x2540 + ((cpu) << 2))
134*19fc2eaeSStefan Roese #define      MVNETA_CPU_RXQ_ACCESS_ALL_MASK      0x000000ff
135*19fc2eaeSStefan Roese #define      MVNETA_CPU_TXQ_ACCESS_ALL_MASK      0x0000ff00
136*19fc2eaeSStefan Roese #define MVNETA_RXQ_TIME_COAL_REG(q)              (0x2580 + ((q) << 2))
137*19fc2eaeSStefan Roese 
138*19fc2eaeSStefan Roese /* Exception Interrupt Port/Queue Cause register */
139*19fc2eaeSStefan Roese 
140*19fc2eaeSStefan Roese #define MVNETA_INTR_NEW_CAUSE                    0x25a0
141*19fc2eaeSStefan Roese #define MVNETA_INTR_NEW_MASK                     0x25a4
142*19fc2eaeSStefan Roese 
143*19fc2eaeSStefan Roese /* bits  0..7  = TXQ SENT, one bit per queue.
144*19fc2eaeSStefan Roese  * bits  8..15 = RXQ OCCUP, one bit per queue.
145*19fc2eaeSStefan Roese  * bits 16..23 = RXQ FREE, one bit per queue.
146*19fc2eaeSStefan Roese  * bit  29 = OLD_REG_SUM, see old reg ?
147*19fc2eaeSStefan Roese  * bit  30 = TX_ERR_SUM, one bit for 4 ports
148*19fc2eaeSStefan Roese  * bit  31 = MISC_SUM,   one bit for 4 ports
149*19fc2eaeSStefan Roese  */
150*19fc2eaeSStefan Roese #define      MVNETA_TX_INTR_MASK(nr_txqs)        (((1 << nr_txqs) - 1) << 0)
151*19fc2eaeSStefan Roese #define      MVNETA_TX_INTR_MASK_ALL             (0xff << 0)
152*19fc2eaeSStefan Roese #define      MVNETA_RX_INTR_MASK(nr_rxqs)        (((1 << nr_rxqs) - 1) << 8)
153*19fc2eaeSStefan Roese #define      MVNETA_RX_INTR_MASK_ALL             (0xff << 8)
154*19fc2eaeSStefan Roese 
155*19fc2eaeSStefan Roese #define MVNETA_INTR_OLD_CAUSE                    0x25a8
156*19fc2eaeSStefan Roese #define MVNETA_INTR_OLD_MASK                     0x25ac
157*19fc2eaeSStefan Roese 
158*19fc2eaeSStefan Roese /* Data Path Port/Queue Cause Register */
159*19fc2eaeSStefan Roese #define MVNETA_INTR_MISC_CAUSE                   0x25b0
160*19fc2eaeSStefan Roese #define MVNETA_INTR_MISC_MASK                    0x25b4
161*19fc2eaeSStefan Roese #define MVNETA_INTR_ENABLE                       0x25b8
162*19fc2eaeSStefan Roese 
163*19fc2eaeSStefan Roese #define MVNETA_RXQ_CMD                           0x2680
164*19fc2eaeSStefan Roese #define      MVNETA_RXQ_DISABLE_SHIFT            8
165*19fc2eaeSStefan Roese #define      MVNETA_RXQ_ENABLE_MASK              0x000000ff
166*19fc2eaeSStefan Roese #define MVETH_TXQ_TOKEN_COUNT_REG(q)             (0x2700 + ((q) << 4))
167*19fc2eaeSStefan Roese #define MVETH_TXQ_TOKEN_CFG_REG(q)               (0x2704 + ((q) << 4))
168*19fc2eaeSStefan Roese #define MVNETA_GMAC_CTRL_0                       0x2c00
169*19fc2eaeSStefan Roese #define      MVNETA_GMAC_MAX_RX_SIZE_SHIFT       2
170*19fc2eaeSStefan Roese #define      MVNETA_GMAC_MAX_RX_SIZE_MASK        0x7ffc
171*19fc2eaeSStefan Roese #define      MVNETA_GMAC0_PORT_ENABLE            BIT(0)
172*19fc2eaeSStefan Roese #define MVNETA_GMAC_CTRL_2                       0x2c08
173*19fc2eaeSStefan Roese #define      MVNETA_GMAC2_PCS_ENABLE             BIT(3)
174*19fc2eaeSStefan Roese #define      MVNETA_GMAC2_PORT_RGMII             BIT(4)
175*19fc2eaeSStefan Roese #define      MVNETA_GMAC2_PORT_RESET             BIT(6)
176*19fc2eaeSStefan Roese #define MVNETA_GMAC_STATUS                       0x2c10
177*19fc2eaeSStefan Roese #define      MVNETA_GMAC_LINK_UP                 BIT(0)
178*19fc2eaeSStefan Roese #define      MVNETA_GMAC_SPEED_1000              BIT(1)
179*19fc2eaeSStefan Roese #define      MVNETA_GMAC_SPEED_100               BIT(2)
180*19fc2eaeSStefan Roese #define      MVNETA_GMAC_FULL_DUPLEX             BIT(3)
181*19fc2eaeSStefan Roese #define      MVNETA_GMAC_RX_FLOW_CTRL_ENABLE     BIT(4)
182*19fc2eaeSStefan Roese #define      MVNETA_GMAC_TX_FLOW_CTRL_ENABLE     BIT(5)
183*19fc2eaeSStefan Roese #define      MVNETA_GMAC_RX_FLOW_CTRL_ACTIVE     BIT(6)
184*19fc2eaeSStefan Roese #define      MVNETA_GMAC_TX_FLOW_CTRL_ACTIVE     BIT(7)
185*19fc2eaeSStefan Roese #define MVNETA_GMAC_AUTONEG_CONFIG               0x2c0c
186*19fc2eaeSStefan Roese #define      MVNETA_GMAC_FORCE_LINK_DOWN         BIT(0)
187*19fc2eaeSStefan Roese #define      MVNETA_GMAC_FORCE_LINK_PASS         BIT(1)
188*19fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_MII_SPEED        BIT(5)
189*19fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_GMII_SPEED       BIT(6)
190*19fc2eaeSStefan Roese #define      MVNETA_GMAC_AN_SPEED_EN             BIT(7)
191*19fc2eaeSStefan Roese #define      MVNETA_GMAC_CONFIG_FULL_DUPLEX      BIT(12)
192*19fc2eaeSStefan Roese #define      MVNETA_GMAC_AN_DUPLEX_EN            BIT(13)
193*19fc2eaeSStefan Roese #define MVNETA_MIB_COUNTERS_BASE                 0x3080
194*19fc2eaeSStefan Roese #define      MVNETA_MIB_LATE_COLLISION           0x7c
195*19fc2eaeSStefan Roese #define MVNETA_DA_FILT_SPEC_MCAST                0x3400
196*19fc2eaeSStefan Roese #define MVNETA_DA_FILT_OTH_MCAST                 0x3500
197*19fc2eaeSStefan Roese #define MVNETA_DA_FILT_UCAST_BASE                0x3600
198*19fc2eaeSStefan Roese #define MVNETA_TXQ_BASE_ADDR_REG(q)              (0x3c00 + ((q) << 2))
199*19fc2eaeSStefan Roese #define MVNETA_TXQ_SIZE_REG(q)                   (0x3c20 + ((q) << 2))
200*19fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_THRESH_ALL_MASK     0x3fff0000
201*19fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_THRESH_MASK(coal)   ((coal) << 16)
202*19fc2eaeSStefan Roese #define MVNETA_TXQ_UPDATE_REG(q)                 (0x3c60 + ((q) << 2))
203*19fc2eaeSStefan Roese #define      MVNETA_TXQ_DEC_SENT_SHIFT           16
204*19fc2eaeSStefan Roese #define MVNETA_TXQ_STATUS_REG(q)                 (0x3c40 + ((q) << 2))
205*19fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_DESC_SHIFT          16
206*19fc2eaeSStefan Roese #define      MVNETA_TXQ_SENT_DESC_MASK           0x3fff0000
207*19fc2eaeSStefan Roese #define MVNETA_PORT_TX_RESET                     0x3cf0
208*19fc2eaeSStefan Roese #define      MVNETA_PORT_TX_DMA_RESET            BIT(0)
209*19fc2eaeSStefan Roese #define MVNETA_TX_MTU                            0x3e0c
210*19fc2eaeSStefan Roese #define MVNETA_TX_TOKEN_SIZE                     0x3e14
211*19fc2eaeSStefan Roese #define      MVNETA_TX_TOKEN_SIZE_MAX            0xffffffff
212*19fc2eaeSStefan Roese #define MVNETA_TXQ_TOKEN_SIZE_REG(q)             (0x3e40 + ((q) << 2))
213*19fc2eaeSStefan Roese #define      MVNETA_TXQ_TOKEN_SIZE_MAX           0x7fffffff
214*19fc2eaeSStefan Roese 
215*19fc2eaeSStefan Roese /* Descriptor ring Macros */
216*19fc2eaeSStefan Roese #define MVNETA_QUEUE_NEXT_DESC(q, index)	\
217*19fc2eaeSStefan Roese 	(((index) < (q)->last_desc) ? ((index) + 1) : 0)
218*19fc2eaeSStefan Roese 
219*19fc2eaeSStefan Roese /* Various constants */
220*19fc2eaeSStefan Roese 
221*19fc2eaeSStefan Roese /* Coalescing */
222*19fc2eaeSStefan Roese #define MVNETA_TXDONE_COAL_PKTS		16
223*19fc2eaeSStefan Roese #define MVNETA_RX_COAL_PKTS		32
224*19fc2eaeSStefan Roese #define MVNETA_RX_COAL_USEC		100
225*19fc2eaeSStefan Roese 
226*19fc2eaeSStefan Roese /* The two bytes Marvell header. Either contains a special value used
227*19fc2eaeSStefan Roese  * by Marvell switches when a specific hardware mode is enabled (not
228*19fc2eaeSStefan Roese  * supported by this driver) or is filled automatically by zeroes on
229*19fc2eaeSStefan Roese  * the RX side. Those two bytes being at the front of the Ethernet
230*19fc2eaeSStefan Roese  * header, they allow to have the IP header aligned on a 4 bytes
231*19fc2eaeSStefan Roese  * boundary automatically: the hardware skips those two bytes on its
232*19fc2eaeSStefan Roese  * own.
233*19fc2eaeSStefan Roese  */
234*19fc2eaeSStefan Roese #define MVNETA_MH_SIZE			2
235*19fc2eaeSStefan Roese 
236*19fc2eaeSStefan Roese #define MVNETA_VLAN_TAG_LEN             4
237*19fc2eaeSStefan Roese 
238*19fc2eaeSStefan Roese #define MVNETA_CPU_D_CACHE_LINE_SIZE    32
239*19fc2eaeSStefan Roese #define MVNETA_TX_CSUM_MAX_SIZE		9800
240*19fc2eaeSStefan Roese #define MVNETA_ACC_MODE_EXT		1
241*19fc2eaeSStefan Roese 
242*19fc2eaeSStefan Roese /* Timeout constants */
243*19fc2eaeSStefan Roese #define MVNETA_TX_DISABLE_TIMEOUT_MSEC	1000
244*19fc2eaeSStefan Roese #define MVNETA_RX_DISABLE_TIMEOUT_MSEC	1000
245*19fc2eaeSStefan Roese #define MVNETA_TX_FIFO_EMPTY_TIMEOUT	10000
246*19fc2eaeSStefan Roese 
247*19fc2eaeSStefan Roese #define MVNETA_TX_MTU_MAX		0x3ffff
248*19fc2eaeSStefan Roese 
249*19fc2eaeSStefan Roese /* Max number of Rx descriptors */
250*19fc2eaeSStefan Roese #define MVNETA_MAX_RXD 16
251*19fc2eaeSStefan Roese 
252*19fc2eaeSStefan Roese /* Max number of Tx descriptors */
253*19fc2eaeSStefan Roese #define MVNETA_MAX_TXD 16
254*19fc2eaeSStefan Roese 
255*19fc2eaeSStefan Roese /* descriptor aligned size */
256*19fc2eaeSStefan Roese #define MVNETA_DESC_ALIGNED_SIZE	32
257*19fc2eaeSStefan Roese 
258*19fc2eaeSStefan Roese struct mvneta_port {
259*19fc2eaeSStefan Roese 	void __iomem *base;
260*19fc2eaeSStefan Roese 	struct mvneta_rx_queue *rxqs;
261*19fc2eaeSStefan Roese 	struct mvneta_tx_queue *txqs;
262*19fc2eaeSStefan Roese 
263*19fc2eaeSStefan Roese 	u8 mcast_count[256];
264*19fc2eaeSStefan Roese 	u16 tx_ring_size;
265*19fc2eaeSStefan Roese 	u16 rx_ring_size;
266*19fc2eaeSStefan Roese 
267*19fc2eaeSStefan Roese 	phy_interface_t phy_interface;
268*19fc2eaeSStefan Roese 	unsigned int link;
269*19fc2eaeSStefan Roese 	unsigned int duplex;
270*19fc2eaeSStefan Roese 	unsigned int speed;
271*19fc2eaeSStefan Roese 
272*19fc2eaeSStefan Roese 	int init;
273*19fc2eaeSStefan Roese 	int phyaddr;
274*19fc2eaeSStefan Roese 	struct phy_device *phydev;
275*19fc2eaeSStefan Roese 	struct mii_dev *bus;
276*19fc2eaeSStefan Roese };
277*19fc2eaeSStefan Roese 
278*19fc2eaeSStefan Roese /* The mvneta_tx_desc and mvneta_rx_desc structures describe the
279*19fc2eaeSStefan Roese  * layout of the transmit and reception DMA descriptors, and their
280*19fc2eaeSStefan Roese  * layout is therefore defined by the hardware design
281*19fc2eaeSStefan Roese  */
282*19fc2eaeSStefan Roese 
283*19fc2eaeSStefan Roese #define MVNETA_TX_L3_OFF_SHIFT	0
284*19fc2eaeSStefan Roese #define MVNETA_TX_IP_HLEN_SHIFT	8
285*19fc2eaeSStefan Roese #define MVNETA_TX_L4_UDP	BIT(16)
286*19fc2eaeSStefan Roese #define MVNETA_TX_L3_IP6	BIT(17)
287*19fc2eaeSStefan Roese #define MVNETA_TXD_IP_CSUM	BIT(18)
288*19fc2eaeSStefan Roese #define MVNETA_TXD_Z_PAD	BIT(19)
289*19fc2eaeSStefan Roese #define MVNETA_TXD_L_DESC	BIT(20)
290*19fc2eaeSStefan Roese #define MVNETA_TXD_F_DESC	BIT(21)
291*19fc2eaeSStefan Roese #define MVNETA_TXD_FLZ_DESC	(MVNETA_TXD_Z_PAD  | \
292*19fc2eaeSStefan Roese 				 MVNETA_TXD_L_DESC | \
293*19fc2eaeSStefan Roese 				 MVNETA_TXD_F_DESC)
294*19fc2eaeSStefan Roese #define MVNETA_TX_L4_CSUM_FULL	BIT(30)
295*19fc2eaeSStefan Roese #define MVNETA_TX_L4_CSUM_NOT	BIT(31)
296*19fc2eaeSStefan Roese 
297*19fc2eaeSStefan Roese #define MVNETA_RXD_ERR_CRC		0x0
298*19fc2eaeSStefan Roese #define MVNETA_RXD_ERR_SUMMARY		BIT(16)
299*19fc2eaeSStefan Roese #define MVNETA_RXD_ERR_OVERRUN		BIT(17)
300*19fc2eaeSStefan Roese #define MVNETA_RXD_ERR_LEN		BIT(18)
301*19fc2eaeSStefan Roese #define MVNETA_RXD_ERR_RESOURCE		(BIT(17) | BIT(18))
302*19fc2eaeSStefan Roese #define MVNETA_RXD_ERR_CODE_MASK	(BIT(17) | BIT(18))
303*19fc2eaeSStefan Roese #define MVNETA_RXD_L3_IP4		BIT(25)
304*19fc2eaeSStefan Roese #define MVNETA_RXD_FIRST_LAST_DESC	(BIT(26) | BIT(27))
305*19fc2eaeSStefan Roese #define MVNETA_RXD_L4_CSUM_OK		BIT(30)
306*19fc2eaeSStefan Roese 
307*19fc2eaeSStefan Roese struct mvneta_tx_desc {
308*19fc2eaeSStefan Roese 	u32  command;		/* Options used by HW for packet transmitting.*/
309*19fc2eaeSStefan Roese 	u16  reserverd1;	/* csum_l4 (for future use)		*/
310*19fc2eaeSStefan Roese 	u16  data_size;		/* Data size of transmitted packet in bytes */
311*19fc2eaeSStefan Roese 	u32  buf_phys_addr;	/* Physical addr of transmitted buffer	*/
312*19fc2eaeSStefan Roese 	u32  reserved2;		/* hw_cmd - (for future use, PMT)	*/
313*19fc2eaeSStefan Roese 	u32  reserved3[4];	/* Reserved - (for future use)		*/
314*19fc2eaeSStefan Roese };
315*19fc2eaeSStefan Roese 
316*19fc2eaeSStefan Roese struct mvneta_rx_desc {
317*19fc2eaeSStefan Roese 	u32  status;		/* Info about received packet		*/
318*19fc2eaeSStefan Roese 	u16  reserved1;		/* pnc_info - (for future use, PnC)	*/
319*19fc2eaeSStefan Roese 	u16  data_size;		/* Size of received packet in bytes	*/
320*19fc2eaeSStefan Roese 
321*19fc2eaeSStefan Roese 	u32  buf_phys_addr;	/* Physical address of the buffer	*/
322*19fc2eaeSStefan Roese 	u32  reserved2;		/* pnc_flow_id  (for future use, PnC)	*/
323*19fc2eaeSStefan Roese 
324*19fc2eaeSStefan Roese 	u32  buf_cookie;	/* cookie for access to RX buffer in rx path */
325*19fc2eaeSStefan Roese 	u16  reserved3;		/* prefetch_cmd, for future use		*/
326*19fc2eaeSStefan Roese 	u16  reserved4;		/* csum_l4 - (for future use, PnC)	*/
327*19fc2eaeSStefan Roese 
328*19fc2eaeSStefan Roese 	u32  reserved5;		/* pnc_extra PnC (for future use, PnC)	*/
329*19fc2eaeSStefan Roese 	u32  reserved6;		/* hw_cmd (for future use, PnC and HWF)	*/
330*19fc2eaeSStefan Roese };
331*19fc2eaeSStefan Roese 
332*19fc2eaeSStefan Roese struct mvneta_tx_queue {
333*19fc2eaeSStefan Roese 	/* Number of this TX queue, in the range 0-7 */
334*19fc2eaeSStefan Roese 	u8 id;
335*19fc2eaeSStefan Roese 
336*19fc2eaeSStefan Roese 	/* Number of TX DMA descriptors in the descriptor ring */
337*19fc2eaeSStefan Roese 	int size;
338*19fc2eaeSStefan Roese 
339*19fc2eaeSStefan Roese 	/* Index of last TX DMA descriptor that was inserted */
340*19fc2eaeSStefan Roese 	int txq_put_index;
341*19fc2eaeSStefan Roese 
342*19fc2eaeSStefan Roese 	/* Index of the TX DMA descriptor to be cleaned up */
343*19fc2eaeSStefan Roese 	int txq_get_index;
344*19fc2eaeSStefan Roese 
345*19fc2eaeSStefan Roese 	/* Virtual address of the TX DMA descriptors array */
346*19fc2eaeSStefan Roese 	struct mvneta_tx_desc *descs;
347*19fc2eaeSStefan Roese 
348*19fc2eaeSStefan Roese 	/* DMA address of the TX DMA descriptors array */
349*19fc2eaeSStefan Roese 	dma_addr_t descs_phys;
350*19fc2eaeSStefan Roese 
351*19fc2eaeSStefan Roese 	/* Index of the last TX DMA descriptor */
352*19fc2eaeSStefan Roese 	int last_desc;
353*19fc2eaeSStefan Roese 
354*19fc2eaeSStefan Roese 	/* Index of the next TX DMA descriptor to process */
355*19fc2eaeSStefan Roese 	int next_desc_to_proc;
356*19fc2eaeSStefan Roese };
357*19fc2eaeSStefan Roese 
358*19fc2eaeSStefan Roese struct mvneta_rx_queue {
359*19fc2eaeSStefan Roese 	/* rx queue number, in the range 0-7 */
360*19fc2eaeSStefan Roese 	u8 id;
361*19fc2eaeSStefan Roese 
362*19fc2eaeSStefan Roese 	/* num of rx descriptors in the rx descriptor ring */
363*19fc2eaeSStefan Roese 	int size;
364*19fc2eaeSStefan Roese 
365*19fc2eaeSStefan Roese 	/* Virtual address of the RX DMA descriptors array */
366*19fc2eaeSStefan Roese 	struct mvneta_rx_desc *descs;
367*19fc2eaeSStefan Roese 
368*19fc2eaeSStefan Roese 	/* DMA address of the RX DMA descriptors array */
369*19fc2eaeSStefan Roese 	dma_addr_t descs_phys;
370*19fc2eaeSStefan Roese 
371*19fc2eaeSStefan Roese 	/* Index of the last RX DMA descriptor */
372*19fc2eaeSStefan Roese 	int last_desc;
373*19fc2eaeSStefan Roese 
374*19fc2eaeSStefan Roese 	/* Index of the next RX DMA descriptor to process */
375*19fc2eaeSStefan Roese 	int next_desc_to_proc;
376*19fc2eaeSStefan Roese };
377*19fc2eaeSStefan Roese 
378*19fc2eaeSStefan Roese /* U-Boot doesn't use the queues, so set the number to 1 */
379*19fc2eaeSStefan Roese static int rxq_number = 1;
380*19fc2eaeSStefan Roese static int txq_number = 1;
381*19fc2eaeSStefan Roese static int rxq_def;
382*19fc2eaeSStefan Roese 
383*19fc2eaeSStefan Roese struct buffer_location {
384*19fc2eaeSStefan Roese 	struct mvneta_tx_desc *tx_descs;
385*19fc2eaeSStefan Roese 	struct mvneta_rx_desc *rx_descs;
386*19fc2eaeSStefan Roese 	u32 rx_buffers;
387*19fc2eaeSStefan Roese };
388*19fc2eaeSStefan Roese 
389*19fc2eaeSStefan Roese /*
390*19fc2eaeSStefan Roese  * All 4 interfaces use the same global buffer, since only one interface
391*19fc2eaeSStefan Roese  * can be enabled at once
392*19fc2eaeSStefan Roese  */
393*19fc2eaeSStefan Roese static struct buffer_location buffer_loc;
394*19fc2eaeSStefan Roese 
395*19fc2eaeSStefan Roese /*
396*19fc2eaeSStefan Roese  * Page table entries are set to 1MB, or multiples of 1MB
397*19fc2eaeSStefan Roese  * (not < 1MB). driver uses less bd's so use 1MB bdspace.
398*19fc2eaeSStefan Roese  */
399*19fc2eaeSStefan Roese #define BD_SPACE	(1 << 20)
400*19fc2eaeSStefan Roese 
401*19fc2eaeSStefan Roese /* Utility/helper methods */
402*19fc2eaeSStefan Roese 
403*19fc2eaeSStefan Roese /* Write helper method */
404*19fc2eaeSStefan Roese static void mvreg_write(struct mvneta_port *pp, u32 offset, u32 data)
405*19fc2eaeSStefan Roese {
406*19fc2eaeSStefan Roese 	writel(data, pp->base + offset);
407*19fc2eaeSStefan Roese }
408*19fc2eaeSStefan Roese 
409*19fc2eaeSStefan Roese /* Read helper method */
410*19fc2eaeSStefan Roese static u32 mvreg_read(struct mvneta_port *pp, u32 offset)
411*19fc2eaeSStefan Roese {
412*19fc2eaeSStefan Roese 	return readl(pp->base + offset);
413*19fc2eaeSStefan Roese }
414*19fc2eaeSStefan Roese 
415*19fc2eaeSStefan Roese /* Clear all MIB counters */
416*19fc2eaeSStefan Roese static void mvneta_mib_counters_clear(struct mvneta_port *pp)
417*19fc2eaeSStefan Roese {
418*19fc2eaeSStefan Roese 	int i;
419*19fc2eaeSStefan Roese 
420*19fc2eaeSStefan Roese 	/* Perform dummy reads from MIB counters */
421*19fc2eaeSStefan Roese 	for (i = 0; i < MVNETA_MIB_LATE_COLLISION; i += 4)
422*19fc2eaeSStefan Roese 		mvreg_read(pp, (MVNETA_MIB_COUNTERS_BASE + i));
423*19fc2eaeSStefan Roese }
424*19fc2eaeSStefan Roese 
425*19fc2eaeSStefan Roese /* Rx descriptors helper methods */
426*19fc2eaeSStefan Roese 
427*19fc2eaeSStefan Roese /* Checks whether the RX descriptor having this status is both the first
428*19fc2eaeSStefan Roese  * and the last descriptor for the RX packet. Each RX packet is currently
429*19fc2eaeSStefan Roese  * received through a single RX descriptor, so not having each RX
430*19fc2eaeSStefan Roese  * descriptor with its first and last bits set is an error
431*19fc2eaeSStefan Roese  */
432*19fc2eaeSStefan Roese static int mvneta_rxq_desc_is_first_last(u32 status)
433*19fc2eaeSStefan Roese {
434*19fc2eaeSStefan Roese 	return (status & MVNETA_RXD_FIRST_LAST_DESC) ==
435*19fc2eaeSStefan Roese 		MVNETA_RXD_FIRST_LAST_DESC;
436*19fc2eaeSStefan Roese }
437*19fc2eaeSStefan Roese 
438*19fc2eaeSStefan Roese /* Add number of descriptors ready to receive new packets */
439*19fc2eaeSStefan Roese static void mvneta_rxq_non_occup_desc_add(struct mvneta_port *pp,
440*19fc2eaeSStefan Roese 					  struct mvneta_rx_queue *rxq,
441*19fc2eaeSStefan Roese 					  int ndescs)
442*19fc2eaeSStefan Roese {
443*19fc2eaeSStefan Roese 	/* Only MVNETA_RXQ_ADD_NON_OCCUPIED_MAX (255) descriptors can
444*19fc2eaeSStefan Roese 	 * be added at once
445*19fc2eaeSStefan Roese 	 */
446*19fc2eaeSStefan Roese 	while (ndescs > MVNETA_RXQ_ADD_NON_OCCUPIED_MAX) {
447*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
448*19fc2eaeSStefan Roese 			    (MVNETA_RXQ_ADD_NON_OCCUPIED_MAX <<
449*19fc2eaeSStefan Roese 			     MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
450*19fc2eaeSStefan Roese 		ndescs -= MVNETA_RXQ_ADD_NON_OCCUPIED_MAX;
451*19fc2eaeSStefan Roese 	}
452*19fc2eaeSStefan Roese 
453*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id),
454*19fc2eaeSStefan Roese 		    (ndescs << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT));
455*19fc2eaeSStefan Roese }
456*19fc2eaeSStefan Roese 
457*19fc2eaeSStefan Roese /* Get number of RX descriptors occupied by received packets */
458*19fc2eaeSStefan Roese static int mvneta_rxq_busy_desc_num_get(struct mvneta_port *pp,
459*19fc2eaeSStefan Roese 					struct mvneta_rx_queue *rxq)
460*19fc2eaeSStefan Roese {
461*19fc2eaeSStefan Roese 	u32 val;
462*19fc2eaeSStefan Roese 
463*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_STATUS_REG(rxq->id));
464*19fc2eaeSStefan Roese 	return val & MVNETA_RXQ_OCCUPIED_ALL_MASK;
465*19fc2eaeSStefan Roese }
466*19fc2eaeSStefan Roese 
467*19fc2eaeSStefan Roese /* Update num of rx desc called upon return from rx path or
468*19fc2eaeSStefan Roese  * from mvneta_rxq_drop_pkts().
469*19fc2eaeSStefan Roese  */
470*19fc2eaeSStefan Roese static void mvneta_rxq_desc_num_update(struct mvneta_port *pp,
471*19fc2eaeSStefan Roese 				       struct mvneta_rx_queue *rxq,
472*19fc2eaeSStefan Roese 				       int rx_done, int rx_filled)
473*19fc2eaeSStefan Roese {
474*19fc2eaeSStefan Roese 	u32 val;
475*19fc2eaeSStefan Roese 
476*19fc2eaeSStefan Roese 	if ((rx_done <= 0xff) && (rx_filled <= 0xff)) {
477*19fc2eaeSStefan Roese 		val = rx_done |
478*19fc2eaeSStefan Roese 		  (rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT);
479*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
480*19fc2eaeSStefan Roese 		return;
481*19fc2eaeSStefan Roese 	}
482*19fc2eaeSStefan Roese 
483*19fc2eaeSStefan Roese 	/* Only 255 descriptors can be added at once */
484*19fc2eaeSStefan Roese 	while ((rx_done > 0) || (rx_filled > 0)) {
485*19fc2eaeSStefan Roese 		if (rx_done <= 0xff) {
486*19fc2eaeSStefan Roese 			val = rx_done;
487*19fc2eaeSStefan Roese 			rx_done = 0;
488*19fc2eaeSStefan Roese 		} else {
489*19fc2eaeSStefan Roese 			val = 0xff;
490*19fc2eaeSStefan Roese 			rx_done -= 0xff;
491*19fc2eaeSStefan Roese 		}
492*19fc2eaeSStefan Roese 		if (rx_filled <= 0xff) {
493*19fc2eaeSStefan Roese 			val |= rx_filled << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
494*19fc2eaeSStefan Roese 			rx_filled = 0;
495*19fc2eaeSStefan Roese 		} else {
496*19fc2eaeSStefan Roese 			val |= 0xff << MVNETA_RXQ_ADD_NON_OCCUPIED_SHIFT;
497*19fc2eaeSStefan Roese 			rx_filled -= 0xff;
498*19fc2eaeSStefan Roese 		}
499*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_STATUS_UPDATE_REG(rxq->id), val);
500*19fc2eaeSStefan Roese 	}
501*19fc2eaeSStefan Roese }
502*19fc2eaeSStefan Roese 
503*19fc2eaeSStefan Roese /* Get pointer to next RX descriptor to be processed by SW */
504*19fc2eaeSStefan Roese static struct mvneta_rx_desc *
505*19fc2eaeSStefan Roese mvneta_rxq_next_desc_get(struct mvneta_rx_queue *rxq)
506*19fc2eaeSStefan Roese {
507*19fc2eaeSStefan Roese 	int rx_desc = rxq->next_desc_to_proc;
508*19fc2eaeSStefan Roese 
509*19fc2eaeSStefan Roese 	rxq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(rxq, rx_desc);
510*19fc2eaeSStefan Roese 	return rxq->descs + rx_desc;
511*19fc2eaeSStefan Roese }
512*19fc2eaeSStefan Roese 
513*19fc2eaeSStefan Roese /* Tx descriptors helper methods */
514*19fc2eaeSStefan Roese 
515*19fc2eaeSStefan Roese /* Update HW with number of TX descriptors to be sent */
516*19fc2eaeSStefan Roese static void mvneta_txq_pend_desc_add(struct mvneta_port *pp,
517*19fc2eaeSStefan Roese 				     struct mvneta_tx_queue *txq,
518*19fc2eaeSStefan Roese 				     int pend_desc)
519*19fc2eaeSStefan Roese {
520*19fc2eaeSStefan Roese 	u32 val;
521*19fc2eaeSStefan Roese 
522*19fc2eaeSStefan Roese 	/* Only 255 descriptors can be added at once ; Assume caller
523*19fc2eaeSStefan Roese 	 * process TX desriptors in quanta less than 256
524*19fc2eaeSStefan Roese 	 */
525*19fc2eaeSStefan Roese 	val = pend_desc;
526*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
527*19fc2eaeSStefan Roese }
528*19fc2eaeSStefan Roese 
529*19fc2eaeSStefan Roese /* Get pointer to next TX descriptor to be processed (send) by HW */
530*19fc2eaeSStefan Roese static struct mvneta_tx_desc *
531*19fc2eaeSStefan Roese mvneta_txq_next_desc_get(struct mvneta_tx_queue *txq)
532*19fc2eaeSStefan Roese {
533*19fc2eaeSStefan Roese 	int tx_desc = txq->next_desc_to_proc;
534*19fc2eaeSStefan Roese 
535*19fc2eaeSStefan Roese 	txq->next_desc_to_proc = MVNETA_QUEUE_NEXT_DESC(txq, tx_desc);
536*19fc2eaeSStefan Roese 	return txq->descs + tx_desc;
537*19fc2eaeSStefan Roese }
538*19fc2eaeSStefan Roese 
539*19fc2eaeSStefan Roese /* Set rxq buf size */
540*19fc2eaeSStefan Roese static void mvneta_rxq_buf_size_set(struct mvneta_port *pp,
541*19fc2eaeSStefan Roese 				    struct mvneta_rx_queue *rxq,
542*19fc2eaeSStefan Roese 				    int buf_size)
543*19fc2eaeSStefan Roese {
544*19fc2eaeSStefan Roese 	u32 val;
545*19fc2eaeSStefan Roese 
546*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_SIZE_REG(rxq->id));
547*19fc2eaeSStefan Roese 
548*19fc2eaeSStefan Roese 	val &= ~MVNETA_RXQ_BUF_SIZE_MASK;
549*19fc2eaeSStefan Roese 	val |= ((buf_size >> 3) << MVNETA_RXQ_BUF_SIZE_SHIFT);
550*19fc2eaeSStefan Roese 
551*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), val);
552*19fc2eaeSStefan Roese }
553*19fc2eaeSStefan Roese 
554*19fc2eaeSStefan Roese /* Start the Ethernet port RX and TX activity */
555*19fc2eaeSStefan Roese static void mvneta_port_up(struct mvneta_port *pp)
556*19fc2eaeSStefan Roese {
557*19fc2eaeSStefan Roese 	int queue;
558*19fc2eaeSStefan Roese 	u32 q_map;
559*19fc2eaeSStefan Roese 
560*19fc2eaeSStefan Roese 	/* Enable all initialized TXs. */
561*19fc2eaeSStefan Roese 	mvneta_mib_counters_clear(pp);
562*19fc2eaeSStefan Roese 	q_map = 0;
563*19fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
564*19fc2eaeSStefan Roese 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
565*19fc2eaeSStefan Roese 		if (txq->descs != NULL)
566*19fc2eaeSStefan Roese 			q_map |= (1 << queue);
567*19fc2eaeSStefan Roese 	}
568*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_CMD, q_map);
569*19fc2eaeSStefan Roese 
570*19fc2eaeSStefan Roese 	/* Enable all initialized RXQs. */
571*19fc2eaeSStefan Roese 	q_map = 0;
572*19fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
573*19fc2eaeSStefan Roese 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
574*19fc2eaeSStefan Roese 		if (rxq->descs != NULL)
575*19fc2eaeSStefan Roese 			q_map |= (1 << queue);
576*19fc2eaeSStefan Roese 	}
577*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_CMD, q_map);
578*19fc2eaeSStefan Roese }
579*19fc2eaeSStefan Roese 
580*19fc2eaeSStefan Roese /* Stop the Ethernet port activity */
581*19fc2eaeSStefan Roese static void mvneta_port_down(struct mvneta_port *pp)
582*19fc2eaeSStefan Roese {
583*19fc2eaeSStefan Roese 	u32 val;
584*19fc2eaeSStefan Roese 	int count;
585*19fc2eaeSStefan Roese 
586*19fc2eaeSStefan Roese 	/* Stop Rx port activity. Check port Rx activity. */
587*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_RXQ_CMD) & MVNETA_RXQ_ENABLE_MASK;
588*19fc2eaeSStefan Roese 
589*19fc2eaeSStefan Roese 	/* Issue stop command for active channels only */
590*19fc2eaeSStefan Roese 	if (val != 0)
591*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_RXQ_CMD,
592*19fc2eaeSStefan Roese 			    val << MVNETA_RXQ_DISABLE_SHIFT);
593*19fc2eaeSStefan Roese 
594*19fc2eaeSStefan Roese 	/* Wait for all Rx activity to terminate. */
595*19fc2eaeSStefan Roese 	count = 0;
596*19fc2eaeSStefan Roese 	do {
597*19fc2eaeSStefan Roese 		if (count++ >= MVNETA_RX_DISABLE_TIMEOUT_MSEC) {
598*19fc2eaeSStefan Roese 			netdev_warn(pp->dev,
599*19fc2eaeSStefan Roese 				    "TIMEOUT for RX stopped ! rx_queue_cmd: 0x08%x\n",
600*19fc2eaeSStefan Roese 				    val);
601*19fc2eaeSStefan Roese 			break;
602*19fc2eaeSStefan Roese 		}
603*19fc2eaeSStefan Roese 		mdelay(1);
604*19fc2eaeSStefan Roese 
605*19fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_RXQ_CMD);
606*19fc2eaeSStefan Roese 	} while (val & 0xff);
607*19fc2eaeSStefan Roese 
608*19fc2eaeSStefan Roese 	/* Stop Tx port activity. Check port Tx activity. Issue stop
609*19fc2eaeSStefan Roese 	 * command for active channels only
610*19fc2eaeSStefan Roese 	 */
611*19fc2eaeSStefan Roese 	val = (mvreg_read(pp, MVNETA_TXQ_CMD)) & MVNETA_TXQ_ENABLE_MASK;
612*19fc2eaeSStefan Roese 
613*19fc2eaeSStefan Roese 	if (val != 0)
614*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_TXQ_CMD,
615*19fc2eaeSStefan Roese 			    (val << MVNETA_TXQ_DISABLE_SHIFT));
616*19fc2eaeSStefan Roese 
617*19fc2eaeSStefan Roese 	/* Wait for all Tx activity to terminate. */
618*19fc2eaeSStefan Roese 	count = 0;
619*19fc2eaeSStefan Roese 	do {
620*19fc2eaeSStefan Roese 		if (count++ >= MVNETA_TX_DISABLE_TIMEOUT_MSEC) {
621*19fc2eaeSStefan Roese 			netdev_warn(pp->dev,
622*19fc2eaeSStefan Roese 				    "TIMEOUT for TX stopped status=0x%08x\n",
623*19fc2eaeSStefan Roese 				    val);
624*19fc2eaeSStefan Roese 			break;
625*19fc2eaeSStefan Roese 		}
626*19fc2eaeSStefan Roese 		mdelay(1);
627*19fc2eaeSStefan Roese 
628*19fc2eaeSStefan Roese 		/* Check TX Command reg that all Txqs are stopped */
629*19fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_TXQ_CMD);
630*19fc2eaeSStefan Roese 
631*19fc2eaeSStefan Roese 	} while (val & 0xff);
632*19fc2eaeSStefan Roese 
633*19fc2eaeSStefan Roese 	/* Double check to verify that TX FIFO is empty */
634*19fc2eaeSStefan Roese 	count = 0;
635*19fc2eaeSStefan Roese 	do {
636*19fc2eaeSStefan Roese 		if (count++ >= MVNETA_TX_FIFO_EMPTY_TIMEOUT) {
637*19fc2eaeSStefan Roese 			netdev_warn(pp->dev,
638*19fc2eaeSStefan Roese 				    "TX FIFO empty timeout status=0x08%x\n",
639*19fc2eaeSStefan Roese 				    val);
640*19fc2eaeSStefan Roese 			break;
641*19fc2eaeSStefan Roese 		}
642*19fc2eaeSStefan Roese 		mdelay(1);
643*19fc2eaeSStefan Roese 
644*19fc2eaeSStefan Roese 		val = mvreg_read(pp, MVNETA_PORT_STATUS);
645*19fc2eaeSStefan Roese 	} while (!(val & MVNETA_TX_FIFO_EMPTY) &&
646*19fc2eaeSStefan Roese 		 (val & MVNETA_TX_IN_PRGRS));
647*19fc2eaeSStefan Roese 
648*19fc2eaeSStefan Roese 	udelay(200);
649*19fc2eaeSStefan Roese }
650*19fc2eaeSStefan Roese 
651*19fc2eaeSStefan Roese /* Enable the port by setting the port enable bit of the MAC control register */
652*19fc2eaeSStefan Roese static void mvneta_port_enable(struct mvneta_port *pp)
653*19fc2eaeSStefan Roese {
654*19fc2eaeSStefan Roese 	u32 val;
655*19fc2eaeSStefan Roese 
656*19fc2eaeSStefan Roese 	/* Enable port */
657*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
658*19fc2eaeSStefan Roese 	val |= MVNETA_GMAC0_PORT_ENABLE;
659*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
660*19fc2eaeSStefan Roese }
661*19fc2eaeSStefan Roese 
662*19fc2eaeSStefan Roese /* Disable the port and wait for about 200 usec before retuning */
663*19fc2eaeSStefan Roese static void mvneta_port_disable(struct mvneta_port *pp)
664*19fc2eaeSStefan Roese {
665*19fc2eaeSStefan Roese 	u32 val;
666*19fc2eaeSStefan Roese 
667*19fc2eaeSStefan Roese 	/* Reset the Enable bit in the Serial Control Register */
668*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_GMAC_CTRL_0);
669*19fc2eaeSStefan Roese 	val &= ~MVNETA_GMAC0_PORT_ENABLE;
670*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_0, val);
671*19fc2eaeSStefan Roese 
672*19fc2eaeSStefan Roese 	udelay(200);
673*19fc2eaeSStefan Roese }
674*19fc2eaeSStefan Roese 
675*19fc2eaeSStefan Roese /* Multicast tables methods */
676*19fc2eaeSStefan Roese 
677*19fc2eaeSStefan Roese /* Set all entries in Unicast MAC Table; queue==-1 means reject all */
678*19fc2eaeSStefan Roese static void mvneta_set_ucast_table(struct mvneta_port *pp, int queue)
679*19fc2eaeSStefan Roese {
680*19fc2eaeSStefan Roese 	int offset;
681*19fc2eaeSStefan Roese 	u32 val;
682*19fc2eaeSStefan Roese 
683*19fc2eaeSStefan Roese 	if (queue == -1) {
684*19fc2eaeSStefan Roese 		val = 0;
685*19fc2eaeSStefan Roese 	} else {
686*19fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
687*19fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
688*19fc2eaeSStefan Roese 	}
689*19fc2eaeSStefan Roese 
690*19fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xc; offset += 4)
691*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_UCAST_BASE + offset, val);
692*19fc2eaeSStefan Roese }
693*19fc2eaeSStefan Roese 
694*19fc2eaeSStefan Roese /* Set all entries in Special Multicast MAC Table; queue==-1 means reject all */
695*19fc2eaeSStefan Roese static void mvneta_set_special_mcast_table(struct mvneta_port *pp, int queue)
696*19fc2eaeSStefan Roese {
697*19fc2eaeSStefan Roese 	int offset;
698*19fc2eaeSStefan Roese 	u32 val;
699*19fc2eaeSStefan Roese 
700*19fc2eaeSStefan Roese 	if (queue == -1) {
701*19fc2eaeSStefan Roese 		val = 0;
702*19fc2eaeSStefan Roese 	} else {
703*19fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
704*19fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
705*19fc2eaeSStefan Roese 	}
706*19fc2eaeSStefan Roese 
707*19fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xfc; offset += 4)
708*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_SPEC_MCAST + offset, val);
709*19fc2eaeSStefan Roese }
710*19fc2eaeSStefan Roese 
711*19fc2eaeSStefan Roese /* Set all entries in Other Multicast MAC Table. queue==-1 means reject all */
712*19fc2eaeSStefan Roese static void mvneta_set_other_mcast_table(struct mvneta_port *pp, int queue)
713*19fc2eaeSStefan Roese {
714*19fc2eaeSStefan Roese 	int offset;
715*19fc2eaeSStefan Roese 	u32 val;
716*19fc2eaeSStefan Roese 
717*19fc2eaeSStefan Roese 	if (queue == -1) {
718*19fc2eaeSStefan Roese 		memset(pp->mcast_count, 0, sizeof(pp->mcast_count));
719*19fc2eaeSStefan Roese 		val = 0;
720*19fc2eaeSStefan Roese 	} else {
721*19fc2eaeSStefan Roese 		memset(pp->mcast_count, 1, sizeof(pp->mcast_count));
722*19fc2eaeSStefan Roese 		val = 0x1 | (queue << 1);
723*19fc2eaeSStefan Roese 		val |= (val << 24) | (val << 16) | (val << 8);
724*19fc2eaeSStefan Roese 	}
725*19fc2eaeSStefan Roese 
726*19fc2eaeSStefan Roese 	for (offset = 0; offset <= 0xfc; offset += 4)
727*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_DA_FILT_OTH_MCAST + offset, val);
728*19fc2eaeSStefan Roese }
729*19fc2eaeSStefan Roese 
730*19fc2eaeSStefan Roese /* This method sets defaults to the NETA port:
731*19fc2eaeSStefan Roese  *	Clears interrupt Cause and Mask registers.
732*19fc2eaeSStefan Roese  *	Clears all MAC tables.
733*19fc2eaeSStefan Roese  *	Sets defaults to all registers.
734*19fc2eaeSStefan Roese  *	Resets RX and TX descriptor rings.
735*19fc2eaeSStefan Roese  *	Resets PHY.
736*19fc2eaeSStefan Roese  * This method can be called after mvneta_port_down() to return the port
737*19fc2eaeSStefan Roese  *	settings to defaults.
738*19fc2eaeSStefan Roese  */
739*19fc2eaeSStefan Roese static void mvneta_defaults_set(struct mvneta_port *pp)
740*19fc2eaeSStefan Roese {
741*19fc2eaeSStefan Roese 	int cpu;
742*19fc2eaeSStefan Roese 	int queue;
743*19fc2eaeSStefan Roese 	u32 val;
744*19fc2eaeSStefan Roese 
745*19fc2eaeSStefan Roese 	/* Clear all Cause registers */
746*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_NEW_CAUSE, 0);
747*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_OLD_CAUSE, 0);
748*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_MISC_CAUSE, 0);
749*19fc2eaeSStefan Roese 
750*19fc2eaeSStefan Roese 	/* Mask all interrupts */
751*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_NEW_MASK, 0);
752*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_OLD_MASK, 0);
753*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_MISC_MASK, 0);
754*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_INTR_ENABLE, 0);
755*19fc2eaeSStefan Roese 
756*19fc2eaeSStefan Roese 	/* Enable MBUS Retry bit16 */
757*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_MBUS_RETRY, 0x20);
758*19fc2eaeSStefan Roese 
759*19fc2eaeSStefan Roese 	/* Set CPU queue access map - all CPUs have access to all RX
760*19fc2eaeSStefan Roese 	 * queues and to all TX queues
761*19fc2eaeSStefan Roese 	 */
762*19fc2eaeSStefan Roese 	for (cpu = 0; cpu < CONFIG_NR_CPUS; cpu++)
763*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_CPU_MAP(cpu),
764*19fc2eaeSStefan Roese 			    (MVNETA_CPU_RXQ_ACCESS_ALL_MASK |
765*19fc2eaeSStefan Roese 			     MVNETA_CPU_TXQ_ACCESS_ALL_MASK));
766*19fc2eaeSStefan Roese 
767*19fc2eaeSStefan Roese 	/* Reset RX and TX DMAs */
768*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_RX_RESET, MVNETA_PORT_RX_DMA_RESET);
769*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_TX_RESET, MVNETA_PORT_TX_DMA_RESET);
770*19fc2eaeSStefan Roese 
771*19fc2eaeSStefan Roese 	/* Disable Legacy WRR, Disable EJP, Release from reset */
772*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_CMD_1, 0);
773*19fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
774*19fc2eaeSStefan Roese 		mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(queue), 0);
775*19fc2eaeSStefan Roese 		mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(queue), 0);
776*19fc2eaeSStefan Roese 	}
777*19fc2eaeSStefan Roese 
778*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_TX_RESET, 0);
779*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_RX_RESET, 0);
780*19fc2eaeSStefan Roese 
781*19fc2eaeSStefan Roese 	/* Set Port Acceleration Mode */
782*19fc2eaeSStefan Roese 	val = MVNETA_ACC_MODE_EXT;
783*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_ACC_MODE, val);
784*19fc2eaeSStefan Roese 
785*19fc2eaeSStefan Roese 	/* Update val of portCfg register accordingly with all RxQueue types */
786*19fc2eaeSStefan Roese 	val = MVNETA_PORT_CONFIG_DEFL_VALUE(rxq_def);
787*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_CONFIG, val);
788*19fc2eaeSStefan Roese 
789*19fc2eaeSStefan Roese 	val = 0;
790*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_PORT_CONFIG_EXTEND, val);
791*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RX_MIN_FRAME_SIZE, 64);
792*19fc2eaeSStefan Roese 
793*19fc2eaeSStefan Roese 	/* Build PORT_SDMA_CONFIG_REG */
794*19fc2eaeSStefan Roese 	val = 0;
795*19fc2eaeSStefan Roese 
796*19fc2eaeSStefan Roese 	/* Default burst size */
797*19fc2eaeSStefan Roese 	val |= MVNETA_TX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
798*19fc2eaeSStefan Roese 	val |= MVNETA_RX_BRST_SZ_MASK(MVNETA_SDMA_BRST_SIZE_16);
799*19fc2eaeSStefan Roese 	val |= MVNETA_RX_NO_DATA_SWAP | MVNETA_TX_NO_DATA_SWAP;
800*19fc2eaeSStefan Roese 
801*19fc2eaeSStefan Roese 	/* Assign port SDMA configuration */
802*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SDMA_CONFIG, val);
803*19fc2eaeSStefan Roese 
804*19fc2eaeSStefan Roese 	/* Enable PHY polling in hardware for U-Boot */
805*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_UNIT_CONTROL);
806*19fc2eaeSStefan Roese 	val |= MVNETA_PHY_POLLING_ENABLE;
807*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_UNIT_CONTROL, val);
808*19fc2eaeSStefan Roese 
809*19fc2eaeSStefan Roese 	mvneta_set_ucast_table(pp, -1);
810*19fc2eaeSStefan Roese 	mvneta_set_special_mcast_table(pp, -1);
811*19fc2eaeSStefan Roese 	mvneta_set_other_mcast_table(pp, -1);
812*19fc2eaeSStefan Roese }
813*19fc2eaeSStefan Roese 
814*19fc2eaeSStefan Roese /* Set unicast address */
815*19fc2eaeSStefan Roese static void mvneta_set_ucast_addr(struct mvneta_port *pp, u8 last_nibble,
816*19fc2eaeSStefan Roese 				  int queue)
817*19fc2eaeSStefan Roese {
818*19fc2eaeSStefan Roese 	unsigned int unicast_reg;
819*19fc2eaeSStefan Roese 	unsigned int tbl_offset;
820*19fc2eaeSStefan Roese 	unsigned int reg_offset;
821*19fc2eaeSStefan Roese 
822*19fc2eaeSStefan Roese 	/* Locate the Unicast table entry */
823*19fc2eaeSStefan Roese 	last_nibble = (0xf & last_nibble);
824*19fc2eaeSStefan Roese 
825*19fc2eaeSStefan Roese 	/* offset from unicast tbl base */
826*19fc2eaeSStefan Roese 	tbl_offset = (last_nibble / 4) * 4;
827*19fc2eaeSStefan Roese 
828*19fc2eaeSStefan Roese 	/* offset within the above reg  */
829*19fc2eaeSStefan Roese 	reg_offset = last_nibble % 4;
830*19fc2eaeSStefan Roese 
831*19fc2eaeSStefan Roese 	unicast_reg = mvreg_read(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset));
832*19fc2eaeSStefan Roese 
833*19fc2eaeSStefan Roese 	if (queue == -1) {
834*19fc2eaeSStefan Roese 		/* Clear accepts frame bit at specified unicast DA tbl entry */
835*19fc2eaeSStefan Roese 		unicast_reg &= ~(0xff << (8 * reg_offset));
836*19fc2eaeSStefan Roese 	} else {
837*19fc2eaeSStefan Roese 		unicast_reg &= ~(0xff << (8 * reg_offset));
838*19fc2eaeSStefan Roese 		unicast_reg |= ((0x01 | (queue << 1)) << (8 * reg_offset));
839*19fc2eaeSStefan Roese 	}
840*19fc2eaeSStefan Roese 
841*19fc2eaeSStefan Roese 	mvreg_write(pp, (MVNETA_DA_FILT_UCAST_BASE + tbl_offset), unicast_reg);
842*19fc2eaeSStefan Roese }
843*19fc2eaeSStefan Roese 
844*19fc2eaeSStefan Roese /* Set mac address */
845*19fc2eaeSStefan Roese static void mvneta_mac_addr_set(struct mvneta_port *pp, unsigned char *addr,
846*19fc2eaeSStefan Roese 				int queue)
847*19fc2eaeSStefan Roese {
848*19fc2eaeSStefan Roese 	unsigned int mac_h;
849*19fc2eaeSStefan Roese 	unsigned int mac_l;
850*19fc2eaeSStefan Roese 
851*19fc2eaeSStefan Roese 	if (queue != -1) {
852*19fc2eaeSStefan Roese 		mac_l = (addr[4] << 8) | (addr[5]);
853*19fc2eaeSStefan Roese 		mac_h = (addr[0] << 24) | (addr[1] << 16) |
854*19fc2eaeSStefan Roese 			(addr[2] << 8) | (addr[3] << 0);
855*19fc2eaeSStefan Roese 
856*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_MAC_ADDR_LOW, mac_l);
857*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_MAC_ADDR_HIGH, mac_h);
858*19fc2eaeSStefan Roese 	}
859*19fc2eaeSStefan Roese 
860*19fc2eaeSStefan Roese 	/* Accept frames of this address */
861*19fc2eaeSStefan Roese 	mvneta_set_ucast_addr(pp, addr[5], queue);
862*19fc2eaeSStefan Roese }
863*19fc2eaeSStefan Roese 
864*19fc2eaeSStefan Roese /* Handle rx descriptor fill by setting buf_cookie and buf_phys_addr */
865*19fc2eaeSStefan Roese static void mvneta_rx_desc_fill(struct mvneta_rx_desc *rx_desc,
866*19fc2eaeSStefan Roese 				u32 phys_addr, u32 cookie)
867*19fc2eaeSStefan Roese {
868*19fc2eaeSStefan Roese 	rx_desc->buf_cookie = cookie;
869*19fc2eaeSStefan Roese 	rx_desc->buf_phys_addr = phys_addr;
870*19fc2eaeSStefan Roese }
871*19fc2eaeSStefan Roese 
872*19fc2eaeSStefan Roese /* Decrement sent descriptors counter */
873*19fc2eaeSStefan Roese static void mvneta_txq_sent_desc_dec(struct mvneta_port *pp,
874*19fc2eaeSStefan Roese 				     struct mvneta_tx_queue *txq,
875*19fc2eaeSStefan Roese 				     int sent_desc)
876*19fc2eaeSStefan Roese {
877*19fc2eaeSStefan Roese 	u32 val;
878*19fc2eaeSStefan Roese 
879*19fc2eaeSStefan Roese 	/* Only 255 TX descriptors can be updated at once */
880*19fc2eaeSStefan Roese 	while (sent_desc > 0xff) {
881*19fc2eaeSStefan Roese 		val = 0xff << MVNETA_TXQ_DEC_SENT_SHIFT;
882*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
883*19fc2eaeSStefan Roese 		sent_desc = sent_desc - 0xff;
884*19fc2eaeSStefan Roese 	}
885*19fc2eaeSStefan Roese 
886*19fc2eaeSStefan Roese 	val = sent_desc << MVNETA_TXQ_DEC_SENT_SHIFT;
887*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_UPDATE_REG(txq->id), val);
888*19fc2eaeSStefan Roese }
889*19fc2eaeSStefan Roese 
890*19fc2eaeSStefan Roese /* Get number of TX descriptors already sent by HW */
891*19fc2eaeSStefan Roese static int mvneta_txq_sent_desc_num_get(struct mvneta_port *pp,
892*19fc2eaeSStefan Roese 					struct mvneta_tx_queue *txq)
893*19fc2eaeSStefan Roese {
894*19fc2eaeSStefan Roese 	u32 val;
895*19fc2eaeSStefan Roese 	int sent_desc;
896*19fc2eaeSStefan Roese 
897*19fc2eaeSStefan Roese 	val = mvreg_read(pp, MVNETA_TXQ_STATUS_REG(txq->id));
898*19fc2eaeSStefan Roese 	sent_desc = (val & MVNETA_TXQ_SENT_DESC_MASK) >>
899*19fc2eaeSStefan Roese 		MVNETA_TXQ_SENT_DESC_SHIFT;
900*19fc2eaeSStefan Roese 
901*19fc2eaeSStefan Roese 	return sent_desc;
902*19fc2eaeSStefan Roese }
903*19fc2eaeSStefan Roese 
904*19fc2eaeSStefan Roese /* Display more error info */
905*19fc2eaeSStefan Roese static void mvneta_rx_error(struct mvneta_port *pp,
906*19fc2eaeSStefan Roese 			    struct mvneta_rx_desc *rx_desc)
907*19fc2eaeSStefan Roese {
908*19fc2eaeSStefan Roese 	u32 status = rx_desc->status;
909*19fc2eaeSStefan Roese 
910*19fc2eaeSStefan Roese 	if (!mvneta_rxq_desc_is_first_last(status)) {
911*19fc2eaeSStefan Roese 		netdev_err(pp->dev,
912*19fc2eaeSStefan Roese 			   "bad rx status %08x (buffer oversize), size=%d\n",
913*19fc2eaeSStefan Roese 			   status, rx_desc->data_size);
914*19fc2eaeSStefan Roese 		return;
915*19fc2eaeSStefan Roese 	}
916*19fc2eaeSStefan Roese 
917*19fc2eaeSStefan Roese 	switch (status & MVNETA_RXD_ERR_CODE_MASK) {
918*19fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_CRC:
919*19fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (crc error), size=%d\n",
920*19fc2eaeSStefan Roese 			   status, rx_desc->data_size);
921*19fc2eaeSStefan Roese 		break;
922*19fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_OVERRUN:
923*19fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (overrun error), size=%d\n",
924*19fc2eaeSStefan Roese 			   status, rx_desc->data_size);
925*19fc2eaeSStefan Roese 		break;
926*19fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_LEN:
927*19fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (max frame length error), size=%d\n",
928*19fc2eaeSStefan Roese 			   status, rx_desc->data_size);
929*19fc2eaeSStefan Roese 		break;
930*19fc2eaeSStefan Roese 	case MVNETA_RXD_ERR_RESOURCE:
931*19fc2eaeSStefan Roese 		netdev_err(pp->dev, "bad rx status %08x (resource error), size=%d\n",
932*19fc2eaeSStefan Roese 			   status, rx_desc->data_size);
933*19fc2eaeSStefan Roese 		break;
934*19fc2eaeSStefan Roese 	}
935*19fc2eaeSStefan Roese }
936*19fc2eaeSStefan Roese 
937*19fc2eaeSStefan Roese static struct mvneta_rx_queue *mvneta_rxq_handle_get(struct mvneta_port *pp,
938*19fc2eaeSStefan Roese 						     int rxq)
939*19fc2eaeSStefan Roese {
940*19fc2eaeSStefan Roese 	return &pp->rxqs[rxq];
941*19fc2eaeSStefan Roese }
942*19fc2eaeSStefan Roese 
943*19fc2eaeSStefan Roese 
944*19fc2eaeSStefan Roese /* Drop packets received by the RXQ and free buffers */
945*19fc2eaeSStefan Roese static void mvneta_rxq_drop_pkts(struct mvneta_port *pp,
946*19fc2eaeSStefan Roese 				 struct mvneta_rx_queue *rxq)
947*19fc2eaeSStefan Roese {
948*19fc2eaeSStefan Roese 	int rx_done;
949*19fc2eaeSStefan Roese 
950*19fc2eaeSStefan Roese 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
951*19fc2eaeSStefan Roese 	if (rx_done)
952*19fc2eaeSStefan Roese 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
953*19fc2eaeSStefan Roese }
954*19fc2eaeSStefan Roese 
955*19fc2eaeSStefan Roese /* Handle rxq fill: allocates rxq skbs; called when initializing a port */
956*19fc2eaeSStefan Roese static int mvneta_rxq_fill(struct mvneta_port *pp, struct mvneta_rx_queue *rxq,
957*19fc2eaeSStefan Roese 			   int num)
958*19fc2eaeSStefan Roese {
959*19fc2eaeSStefan Roese 	int i;
960*19fc2eaeSStefan Roese 
961*19fc2eaeSStefan Roese 	for (i = 0; i < num; i++) {
962*19fc2eaeSStefan Roese 		u32 addr;
963*19fc2eaeSStefan Roese 
964*19fc2eaeSStefan Roese 		/* U-Boot special: Fill in the rx buffer addresses */
965*19fc2eaeSStefan Roese 		addr = buffer_loc.rx_buffers + (i * RX_BUFFER_SIZE);
966*19fc2eaeSStefan Roese 		mvneta_rx_desc_fill(rxq->descs + i, addr, addr);
967*19fc2eaeSStefan Roese 	}
968*19fc2eaeSStefan Roese 
969*19fc2eaeSStefan Roese 	/* Add this number of RX descriptors as non occupied (ready to
970*19fc2eaeSStefan Roese 	 * get packets)
971*19fc2eaeSStefan Roese 	 */
972*19fc2eaeSStefan Roese 	mvneta_rxq_non_occup_desc_add(pp, rxq, i);
973*19fc2eaeSStefan Roese 
974*19fc2eaeSStefan Roese 	return 0;
975*19fc2eaeSStefan Roese }
976*19fc2eaeSStefan Roese 
977*19fc2eaeSStefan Roese /* Rx/Tx queue initialization/cleanup methods */
978*19fc2eaeSStefan Roese 
979*19fc2eaeSStefan Roese /* Create a specified RX queue */
980*19fc2eaeSStefan Roese static int mvneta_rxq_init(struct mvneta_port *pp,
981*19fc2eaeSStefan Roese 			   struct mvneta_rx_queue *rxq)
982*19fc2eaeSStefan Roese 
983*19fc2eaeSStefan Roese {
984*19fc2eaeSStefan Roese 	rxq->size = pp->rx_ring_size;
985*19fc2eaeSStefan Roese 
986*19fc2eaeSStefan Roese 	/* Allocate memory for RX descriptors */
987*19fc2eaeSStefan Roese 	rxq->descs_phys = (dma_addr_t)rxq->descs;
988*19fc2eaeSStefan Roese 	if (rxq->descs == NULL)
989*19fc2eaeSStefan Roese 		return -ENOMEM;
990*19fc2eaeSStefan Roese 
991*19fc2eaeSStefan Roese 	rxq->last_desc = rxq->size - 1;
992*19fc2eaeSStefan Roese 
993*19fc2eaeSStefan Roese 	/* Set Rx descriptors queue starting address */
994*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_BASE_ADDR_REG(rxq->id), rxq->descs_phys);
995*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_RXQ_SIZE_REG(rxq->id), rxq->size);
996*19fc2eaeSStefan Roese 
997*19fc2eaeSStefan Roese 	/* Fill RXQ with buffers from RX pool */
998*19fc2eaeSStefan Roese 	mvneta_rxq_buf_size_set(pp, rxq, RX_BUFFER_SIZE);
999*19fc2eaeSStefan Roese 	mvneta_rxq_fill(pp, rxq, rxq->size);
1000*19fc2eaeSStefan Roese 
1001*19fc2eaeSStefan Roese 	return 0;
1002*19fc2eaeSStefan Roese }
1003*19fc2eaeSStefan Roese 
1004*19fc2eaeSStefan Roese /* Cleanup Rx queue */
1005*19fc2eaeSStefan Roese static void mvneta_rxq_deinit(struct mvneta_port *pp,
1006*19fc2eaeSStefan Roese 			      struct mvneta_rx_queue *rxq)
1007*19fc2eaeSStefan Roese {
1008*19fc2eaeSStefan Roese 	mvneta_rxq_drop_pkts(pp, rxq);
1009*19fc2eaeSStefan Roese 
1010*19fc2eaeSStefan Roese 	rxq->descs             = NULL;
1011*19fc2eaeSStefan Roese 	rxq->last_desc         = 0;
1012*19fc2eaeSStefan Roese 	rxq->next_desc_to_proc = 0;
1013*19fc2eaeSStefan Roese 	rxq->descs_phys        = 0;
1014*19fc2eaeSStefan Roese }
1015*19fc2eaeSStefan Roese 
1016*19fc2eaeSStefan Roese /* Create and initialize a tx queue */
1017*19fc2eaeSStefan Roese static int mvneta_txq_init(struct mvneta_port *pp,
1018*19fc2eaeSStefan Roese 			   struct mvneta_tx_queue *txq)
1019*19fc2eaeSStefan Roese {
1020*19fc2eaeSStefan Roese 	txq->size = pp->tx_ring_size;
1021*19fc2eaeSStefan Roese 
1022*19fc2eaeSStefan Roese 	/* Allocate memory for TX descriptors */
1023*19fc2eaeSStefan Roese 	txq->descs_phys = (u32)txq->descs;
1024*19fc2eaeSStefan Roese 	if (txq->descs == NULL)
1025*19fc2eaeSStefan Roese 		return -ENOMEM;
1026*19fc2eaeSStefan Roese 
1027*19fc2eaeSStefan Roese 	txq->last_desc = txq->size - 1;
1028*19fc2eaeSStefan Roese 
1029*19fc2eaeSStefan Roese 	/* Set maximum bandwidth for enabled TXQs */
1030*19fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0x03ffffff);
1031*19fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0x3fffffff);
1032*19fc2eaeSStefan Roese 
1033*19fc2eaeSStefan Roese 	/* Set Tx descriptors queue starting address */
1034*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), txq->descs_phys);
1035*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), txq->size);
1036*19fc2eaeSStefan Roese 
1037*19fc2eaeSStefan Roese 	return 0;
1038*19fc2eaeSStefan Roese }
1039*19fc2eaeSStefan Roese 
1040*19fc2eaeSStefan Roese /* Free allocated resources when mvneta_txq_init() fails to allocate memory*/
1041*19fc2eaeSStefan Roese static void mvneta_txq_deinit(struct mvneta_port *pp,
1042*19fc2eaeSStefan Roese 			      struct mvneta_tx_queue *txq)
1043*19fc2eaeSStefan Roese {
1044*19fc2eaeSStefan Roese 	txq->descs             = NULL;
1045*19fc2eaeSStefan Roese 	txq->last_desc         = 0;
1046*19fc2eaeSStefan Roese 	txq->next_desc_to_proc = 0;
1047*19fc2eaeSStefan Roese 	txq->descs_phys        = 0;
1048*19fc2eaeSStefan Roese 
1049*19fc2eaeSStefan Roese 	/* Set minimum bandwidth for disabled TXQs */
1050*19fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_CFG_REG(txq->id), 0);
1051*19fc2eaeSStefan Roese 	mvreg_write(pp, MVETH_TXQ_TOKEN_COUNT_REG(txq->id), 0);
1052*19fc2eaeSStefan Roese 
1053*19fc2eaeSStefan Roese 	/* Set Tx descriptors queue starting address and size */
1054*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_BASE_ADDR_REG(txq->id), 0);
1055*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_TXQ_SIZE_REG(txq->id), 0);
1056*19fc2eaeSStefan Roese }
1057*19fc2eaeSStefan Roese 
1058*19fc2eaeSStefan Roese /* Cleanup all Tx queues */
1059*19fc2eaeSStefan Roese static void mvneta_cleanup_txqs(struct mvneta_port *pp)
1060*19fc2eaeSStefan Roese {
1061*19fc2eaeSStefan Roese 	int queue;
1062*19fc2eaeSStefan Roese 
1063*19fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++)
1064*19fc2eaeSStefan Roese 		mvneta_txq_deinit(pp, &pp->txqs[queue]);
1065*19fc2eaeSStefan Roese }
1066*19fc2eaeSStefan Roese 
1067*19fc2eaeSStefan Roese /* Cleanup all Rx queues */
1068*19fc2eaeSStefan Roese static void mvneta_cleanup_rxqs(struct mvneta_port *pp)
1069*19fc2eaeSStefan Roese {
1070*19fc2eaeSStefan Roese 	int queue;
1071*19fc2eaeSStefan Roese 
1072*19fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++)
1073*19fc2eaeSStefan Roese 		mvneta_rxq_deinit(pp, &pp->rxqs[queue]);
1074*19fc2eaeSStefan Roese }
1075*19fc2eaeSStefan Roese 
1076*19fc2eaeSStefan Roese 
1077*19fc2eaeSStefan Roese /* Init all Rx queues */
1078*19fc2eaeSStefan Roese static int mvneta_setup_rxqs(struct mvneta_port *pp)
1079*19fc2eaeSStefan Roese {
1080*19fc2eaeSStefan Roese 	int queue;
1081*19fc2eaeSStefan Roese 
1082*19fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
1083*19fc2eaeSStefan Roese 		int err = mvneta_rxq_init(pp, &pp->rxqs[queue]);
1084*19fc2eaeSStefan Roese 		if (err) {
1085*19fc2eaeSStefan Roese 			netdev_err(pp->dev, "%s: can't create rxq=%d\n",
1086*19fc2eaeSStefan Roese 				   __func__, queue);
1087*19fc2eaeSStefan Roese 			mvneta_cleanup_rxqs(pp);
1088*19fc2eaeSStefan Roese 			return err;
1089*19fc2eaeSStefan Roese 		}
1090*19fc2eaeSStefan Roese 	}
1091*19fc2eaeSStefan Roese 
1092*19fc2eaeSStefan Roese 	return 0;
1093*19fc2eaeSStefan Roese }
1094*19fc2eaeSStefan Roese 
1095*19fc2eaeSStefan Roese /* Init all tx queues */
1096*19fc2eaeSStefan Roese static int mvneta_setup_txqs(struct mvneta_port *pp)
1097*19fc2eaeSStefan Roese {
1098*19fc2eaeSStefan Roese 	int queue;
1099*19fc2eaeSStefan Roese 
1100*19fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
1101*19fc2eaeSStefan Roese 		int err = mvneta_txq_init(pp, &pp->txqs[queue]);
1102*19fc2eaeSStefan Roese 		if (err) {
1103*19fc2eaeSStefan Roese 			netdev_err(pp->dev, "%s: can't create txq=%d\n",
1104*19fc2eaeSStefan Roese 				   __func__, queue);
1105*19fc2eaeSStefan Roese 			mvneta_cleanup_txqs(pp);
1106*19fc2eaeSStefan Roese 			return err;
1107*19fc2eaeSStefan Roese 		}
1108*19fc2eaeSStefan Roese 	}
1109*19fc2eaeSStefan Roese 
1110*19fc2eaeSStefan Roese 	return 0;
1111*19fc2eaeSStefan Roese }
1112*19fc2eaeSStefan Roese 
1113*19fc2eaeSStefan Roese static void mvneta_start_dev(struct mvneta_port *pp)
1114*19fc2eaeSStefan Roese {
1115*19fc2eaeSStefan Roese 	/* start the Rx/Tx activity */
1116*19fc2eaeSStefan Roese 	mvneta_port_enable(pp);
1117*19fc2eaeSStefan Roese }
1118*19fc2eaeSStefan Roese 
1119*19fc2eaeSStefan Roese static void mvneta_adjust_link(struct eth_device *dev)
1120*19fc2eaeSStefan Roese {
1121*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1122*19fc2eaeSStefan Roese 	struct phy_device *phydev = pp->phydev;
1123*19fc2eaeSStefan Roese 	int status_change = 0;
1124*19fc2eaeSStefan Roese 
1125*19fc2eaeSStefan Roese 	if (phydev->link) {
1126*19fc2eaeSStefan Roese 		if ((pp->speed != phydev->speed) ||
1127*19fc2eaeSStefan Roese 		    (pp->duplex != phydev->duplex)) {
1128*19fc2eaeSStefan Roese 			u32 val;
1129*19fc2eaeSStefan Roese 
1130*19fc2eaeSStefan Roese 			val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1131*19fc2eaeSStefan Roese 			val &= ~(MVNETA_GMAC_CONFIG_MII_SPEED |
1132*19fc2eaeSStefan Roese 				 MVNETA_GMAC_CONFIG_GMII_SPEED |
1133*19fc2eaeSStefan Roese 				 MVNETA_GMAC_CONFIG_FULL_DUPLEX |
1134*19fc2eaeSStefan Roese 				 MVNETA_GMAC_AN_SPEED_EN |
1135*19fc2eaeSStefan Roese 				 MVNETA_GMAC_AN_DUPLEX_EN);
1136*19fc2eaeSStefan Roese 
1137*19fc2eaeSStefan Roese 			if (phydev->duplex)
1138*19fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_FULL_DUPLEX;
1139*19fc2eaeSStefan Roese 
1140*19fc2eaeSStefan Roese 			if (phydev->speed == SPEED_1000)
1141*19fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_GMII_SPEED;
1142*19fc2eaeSStefan Roese 			else
1143*19fc2eaeSStefan Roese 				val |= MVNETA_GMAC_CONFIG_MII_SPEED;
1144*19fc2eaeSStefan Roese 
1145*19fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1146*19fc2eaeSStefan Roese 
1147*19fc2eaeSStefan Roese 			pp->duplex = phydev->duplex;
1148*19fc2eaeSStefan Roese 			pp->speed  = phydev->speed;
1149*19fc2eaeSStefan Roese 		}
1150*19fc2eaeSStefan Roese 	}
1151*19fc2eaeSStefan Roese 
1152*19fc2eaeSStefan Roese 	if (phydev->link != pp->link) {
1153*19fc2eaeSStefan Roese 		if (!phydev->link) {
1154*19fc2eaeSStefan Roese 			pp->duplex = -1;
1155*19fc2eaeSStefan Roese 			pp->speed = 0;
1156*19fc2eaeSStefan Roese 		}
1157*19fc2eaeSStefan Roese 
1158*19fc2eaeSStefan Roese 		pp->link = phydev->link;
1159*19fc2eaeSStefan Roese 		status_change = 1;
1160*19fc2eaeSStefan Roese 	}
1161*19fc2eaeSStefan Roese 
1162*19fc2eaeSStefan Roese 	if (status_change) {
1163*19fc2eaeSStefan Roese 		if (phydev->link) {
1164*19fc2eaeSStefan Roese 			u32 val = mvreg_read(pp, MVNETA_GMAC_AUTONEG_CONFIG);
1165*19fc2eaeSStefan Roese 			val |= (MVNETA_GMAC_FORCE_LINK_PASS |
1166*19fc2eaeSStefan Roese 				MVNETA_GMAC_FORCE_LINK_DOWN);
1167*19fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_GMAC_AUTONEG_CONFIG, val);
1168*19fc2eaeSStefan Roese 			mvneta_port_up(pp);
1169*19fc2eaeSStefan Roese 		} else {
1170*19fc2eaeSStefan Roese 			mvneta_port_down(pp);
1171*19fc2eaeSStefan Roese 		}
1172*19fc2eaeSStefan Roese 	}
1173*19fc2eaeSStefan Roese }
1174*19fc2eaeSStefan Roese 
1175*19fc2eaeSStefan Roese static int mvneta_open(struct eth_device *dev)
1176*19fc2eaeSStefan Roese {
1177*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1178*19fc2eaeSStefan Roese 	int ret;
1179*19fc2eaeSStefan Roese 
1180*19fc2eaeSStefan Roese 	ret = mvneta_setup_rxqs(pp);
1181*19fc2eaeSStefan Roese 	if (ret)
1182*19fc2eaeSStefan Roese 		return ret;
1183*19fc2eaeSStefan Roese 
1184*19fc2eaeSStefan Roese 	ret = mvneta_setup_txqs(pp);
1185*19fc2eaeSStefan Roese 	if (ret)
1186*19fc2eaeSStefan Roese 		return ret;
1187*19fc2eaeSStefan Roese 
1188*19fc2eaeSStefan Roese 	mvneta_adjust_link(dev);
1189*19fc2eaeSStefan Roese 
1190*19fc2eaeSStefan Roese 	mvneta_start_dev(pp);
1191*19fc2eaeSStefan Roese 
1192*19fc2eaeSStefan Roese 	return 0;
1193*19fc2eaeSStefan Roese }
1194*19fc2eaeSStefan Roese 
1195*19fc2eaeSStefan Roese /* Initialize hw */
1196*19fc2eaeSStefan Roese static int mvneta_init(struct mvneta_port *pp)
1197*19fc2eaeSStefan Roese {
1198*19fc2eaeSStefan Roese 	int queue;
1199*19fc2eaeSStefan Roese 
1200*19fc2eaeSStefan Roese 	/* Disable port */
1201*19fc2eaeSStefan Roese 	mvneta_port_disable(pp);
1202*19fc2eaeSStefan Roese 
1203*19fc2eaeSStefan Roese 	/* Set port default values */
1204*19fc2eaeSStefan Roese 	mvneta_defaults_set(pp);
1205*19fc2eaeSStefan Roese 
1206*19fc2eaeSStefan Roese 	pp->txqs = kzalloc(txq_number * sizeof(struct mvneta_tx_queue),
1207*19fc2eaeSStefan Roese 			   GFP_KERNEL);
1208*19fc2eaeSStefan Roese 	if (!pp->txqs)
1209*19fc2eaeSStefan Roese 		return -ENOMEM;
1210*19fc2eaeSStefan Roese 
1211*19fc2eaeSStefan Roese 	/* U-Boot special: use preallocated area */
1212*19fc2eaeSStefan Roese 	pp->txqs[0].descs = buffer_loc.tx_descs;
1213*19fc2eaeSStefan Roese 
1214*19fc2eaeSStefan Roese 	/* Initialize TX descriptor rings */
1215*19fc2eaeSStefan Roese 	for (queue = 0; queue < txq_number; queue++) {
1216*19fc2eaeSStefan Roese 		struct mvneta_tx_queue *txq = &pp->txqs[queue];
1217*19fc2eaeSStefan Roese 		txq->id = queue;
1218*19fc2eaeSStefan Roese 		txq->size = pp->tx_ring_size;
1219*19fc2eaeSStefan Roese 	}
1220*19fc2eaeSStefan Roese 
1221*19fc2eaeSStefan Roese 	pp->rxqs = kzalloc(rxq_number * sizeof(struct mvneta_rx_queue),
1222*19fc2eaeSStefan Roese 			   GFP_KERNEL);
1223*19fc2eaeSStefan Roese 	if (!pp->rxqs) {
1224*19fc2eaeSStefan Roese 		kfree(pp->txqs);
1225*19fc2eaeSStefan Roese 		return -ENOMEM;
1226*19fc2eaeSStefan Roese 	}
1227*19fc2eaeSStefan Roese 
1228*19fc2eaeSStefan Roese 	/* U-Boot special: use preallocated area */
1229*19fc2eaeSStefan Roese 	pp->rxqs[0].descs = buffer_loc.rx_descs;
1230*19fc2eaeSStefan Roese 
1231*19fc2eaeSStefan Roese 	/* Create Rx descriptor rings */
1232*19fc2eaeSStefan Roese 	for (queue = 0; queue < rxq_number; queue++) {
1233*19fc2eaeSStefan Roese 		struct mvneta_rx_queue *rxq = &pp->rxqs[queue];
1234*19fc2eaeSStefan Roese 		rxq->id = queue;
1235*19fc2eaeSStefan Roese 		rxq->size = pp->rx_ring_size;
1236*19fc2eaeSStefan Roese 	}
1237*19fc2eaeSStefan Roese 
1238*19fc2eaeSStefan Roese 	return 0;
1239*19fc2eaeSStefan Roese }
1240*19fc2eaeSStefan Roese 
1241*19fc2eaeSStefan Roese /* platform glue : initialize decoding windows */
1242*19fc2eaeSStefan Roese static void mvneta_conf_mbus_windows(struct mvneta_port *pp)
1243*19fc2eaeSStefan Roese {
1244*19fc2eaeSStefan Roese 	const struct mbus_dram_target_info *dram;
1245*19fc2eaeSStefan Roese 	u32 win_enable;
1246*19fc2eaeSStefan Roese 	u32 win_protect;
1247*19fc2eaeSStefan Roese 	int i;
1248*19fc2eaeSStefan Roese 
1249*19fc2eaeSStefan Roese 	dram = mvebu_mbus_dram_info();
1250*19fc2eaeSStefan Roese 	for (i = 0; i < 6; i++) {
1251*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_BASE(i), 0);
1252*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_SIZE(i), 0);
1253*19fc2eaeSStefan Roese 
1254*19fc2eaeSStefan Roese 		if (i < 4)
1255*19fc2eaeSStefan Roese 			mvreg_write(pp, MVNETA_WIN_REMAP(i), 0);
1256*19fc2eaeSStefan Roese 	}
1257*19fc2eaeSStefan Roese 
1258*19fc2eaeSStefan Roese 	win_enable = 0x3f;
1259*19fc2eaeSStefan Roese 	win_protect = 0;
1260*19fc2eaeSStefan Roese 
1261*19fc2eaeSStefan Roese 	for (i = 0; i < dram->num_cs; i++) {
1262*19fc2eaeSStefan Roese 		const struct mbus_dram_window *cs = dram->cs + i;
1263*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_BASE(i), (cs->base & 0xffff0000) |
1264*19fc2eaeSStefan Roese 			    (cs->mbus_attr << 8) | dram->mbus_dram_target_id);
1265*19fc2eaeSStefan Roese 
1266*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_WIN_SIZE(i),
1267*19fc2eaeSStefan Roese 			    (cs->size - 1) & 0xffff0000);
1268*19fc2eaeSStefan Roese 
1269*19fc2eaeSStefan Roese 		win_enable &= ~(1 << i);
1270*19fc2eaeSStefan Roese 		win_protect |= 3 << (2 * i);
1271*19fc2eaeSStefan Roese 	}
1272*19fc2eaeSStefan Roese 
1273*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_BASE_ADDR_ENABLE, win_enable);
1274*19fc2eaeSStefan Roese }
1275*19fc2eaeSStefan Roese 
1276*19fc2eaeSStefan Roese /* Power up the port */
1277*19fc2eaeSStefan Roese static int mvneta_port_power_up(struct mvneta_port *pp, int phy_mode)
1278*19fc2eaeSStefan Roese {
1279*19fc2eaeSStefan Roese 	u32 ctrl;
1280*19fc2eaeSStefan Roese 
1281*19fc2eaeSStefan Roese 	/* MAC Cause register should be cleared */
1282*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_UNIT_INTR_CAUSE, 0);
1283*19fc2eaeSStefan Roese 
1284*19fc2eaeSStefan Roese 	ctrl = mvreg_read(pp, MVNETA_GMAC_CTRL_2);
1285*19fc2eaeSStefan Roese 
1286*19fc2eaeSStefan Roese 	/* Even though it might look weird, when we're configured in
1287*19fc2eaeSStefan Roese 	 * SGMII or QSGMII mode, the RGMII bit needs to be set.
1288*19fc2eaeSStefan Roese 	 */
1289*19fc2eaeSStefan Roese 	switch (phy_mode) {
1290*19fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_QSGMII:
1291*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_QSGMII_SERDES_PROTO);
1292*19fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1293*19fc2eaeSStefan Roese 		break;
1294*19fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_SGMII:
1295*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_SERDES_CFG, MVNETA_SGMII_SERDES_PROTO);
1296*19fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PCS_ENABLE | MVNETA_GMAC2_PORT_RGMII;
1297*19fc2eaeSStefan Roese 		break;
1298*19fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_RGMII:
1299*19fc2eaeSStefan Roese 	case PHY_INTERFACE_MODE_RGMII_ID:
1300*19fc2eaeSStefan Roese 		ctrl |= MVNETA_GMAC2_PORT_RGMII;
1301*19fc2eaeSStefan Roese 		break;
1302*19fc2eaeSStefan Roese 	default:
1303*19fc2eaeSStefan Roese 		return -EINVAL;
1304*19fc2eaeSStefan Roese 	}
1305*19fc2eaeSStefan Roese 
1306*19fc2eaeSStefan Roese 	/* Cancel Port Reset */
1307*19fc2eaeSStefan Roese 	ctrl &= ~MVNETA_GMAC2_PORT_RESET;
1308*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_GMAC_CTRL_2, ctrl);
1309*19fc2eaeSStefan Roese 
1310*19fc2eaeSStefan Roese 	while ((mvreg_read(pp, MVNETA_GMAC_CTRL_2) &
1311*19fc2eaeSStefan Roese 		MVNETA_GMAC2_PORT_RESET) != 0)
1312*19fc2eaeSStefan Roese 		continue;
1313*19fc2eaeSStefan Roese 
1314*19fc2eaeSStefan Roese 	return 0;
1315*19fc2eaeSStefan Roese }
1316*19fc2eaeSStefan Roese 
1317*19fc2eaeSStefan Roese /* Device initialization routine */
1318*19fc2eaeSStefan Roese static int mvneta_probe(struct eth_device *dev)
1319*19fc2eaeSStefan Roese {
1320*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1321*19fc2eaeSStefan Roese 	int err;
1322*19fc2eaeSStefan Roese 
1323*19fc2eaeSStefan Roese 	pp->tx_ring_size = MVNETA_MAX_TXD;
1324*19fc2eaeSStefan Roese 	pp->rx_ring_size = MVNETA_MAX_RXD;
1325*19fc2eaeSStefan Roese 
1326*19fc2eaeSStefan Roese 	err = mvneta_init(pp);
1327*19fc2eaeSStefan Roese 	if (err < 0) {
1328*19fc2eaeSStefan Roese 		dev_err(&pdev->dev, "can't init eth hal\n");
1329*19fc2eaeSStefan Roese 		return err;
1330*19fc2eaeSStefan Roese 	}
1331*19fc2eaeSStefan Roese 
1332*19fc2eaeSStefan Roese 	mvneta_conf_mbus_windows(pp);
1333*19fc2eaeSStefan Roese 
1334*19fc2eaeSStefan Roese 	mvneta_mac_addr_set(pp, dev->enetaddr, rxq_def);
1335*19fc2eaeSStefan Roese 
1336*19fc2eaeSStefan Roese 	err = mvneta_port_power_up(pp, pp->phy_interface);
1337*19fc2eaeSStefan Roese 	if (err < 0) {
1338*19fc2eaeSStefan Roese 		dev_err(&pdev->dev, "can't power up port\n");
1339*19fc2eaeSStefan Roese 		return err;
1340*19fc2eaeSStefan Roese 	}
1341*19fc2eaeSStefan Roese 
1342*19fc2eaeSStefan Roese 	/* Call open() now as it needs to be done before runing send() */
1343*19fc2eaeSStefan Roese 	mvneta_open(dev);
1344*19fc2eaeSStefan Roese 
1345*19fc2eaeSStefan Roese 	return 0;
1346*19fc2eaeSStefan Roese }
1347*19fc2eaeSStefan Roese 
1348*19fc2eaeSStefan Roese /* U-Boot only functions follow here */
1349*19fc2eaeSStefan Roese 
1350*19fc2eaeSStefan Roese /* SMI / MDIO functions */
1351*19fc2eaeSStefan Roese 
1352*19fc2eaeSStefan Roese static int smi_wait_ready(struct mvneta_port *pp)
1353*19fc2eaeSStefan Roese {
1354*19fc2eaeSStefan Roese 	u32 timeout = MVNETA_SMI_TIMEOUT;
1355*19fc2eaeSStefan Roese 	u32 smi_reg;
1356*19fc2eaeSStefan Roese 
1357*19fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
1358*19fc2eaeSStefan Roese 	do {
1359*19fc2eaeSStefan Roese 		/* read smi register */
1360*19fc2eaeSStefan Roese 		smi_reg = mvreg_read(pp, MVNETA_SMI);
1361*19fc2eaeSStefan Roese 		if (timeout-- == 0) {
1362*19fc2eaeSStefan Roese 			printf("Error: SMI busy timeout\n");
1363*19fc2eaeSStefan Roese 			return -EFAULT;
1364*19fc2eaeSStefan Roese 		}
1365*19fc2eaeSStefan Roese 	} while (smi_reg & MVNETA_SMI_BUSY);
1366*19fc2eaeSStefan Roese 
1367*19fc2eaeSStefan Roese 	return 0;
1368*19fc2eaeSStefan Roese }
1369*19fc2eaeSStefan Roese 
1370*19fc2eaeSStefan Roese /*
1371*19fc2eaeSStefan Roese  * smi_reg_read - miiphy_read callback function.
1372*19fc2eaeSStefan Roese  *
1373*19fc2eaeSStefan Roese  * Returns 16bit phy register value, or 0xffff on error
1374*19fc2eaeSStefan Roese  */
1375*19fc2eaeSStefan Roese static int smi_reg_read(const char *devname, u8 phy_adr, u8 reg_ofs, u16 *data)
1376*19fc2eaeSStefan Roese {
1377*19fc2eaeSStefan Roese 	struct eth_device *dev = eth_get_dev_by_name(devname);
1378*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1379*19fc2eaeSStefan Roese 	u32 smi_reg;
1380*19fc2eaeSStefan Roese 	u32 timeout;
1381*19fc2eaeSStefan Roese 
1382*19fc2eaeSStefan Roese 	/* check parameters */
1383*19fc2eaeSStefan Roese 	if (phy_adr > MVNETA_PHY_ADDR_MASK) {
1384*19fc2eaeSStefan Roese 		printf("Error: Invalid PHY address %d\n", phy_adr);
1385*19fc2eaeSStefan Roese 		return -EFAULT;
1386*19fc2eaeSStefan Roese 	}
1387*19fc2eaeSStefan Roese 
1388*19fc2eaeSStefan Roese 	if (reg_ofs > MVNETA_PHY_REG_MASK) {
1389*19fc2eaeSStefan Roese 		printf("Err: Invalid register offset %d\n", reg_ofs);
1390*19fc2eaeSStefan Roese 		return -EFAULT;
1391*19fc2eaeSStefan Roese 	}
1392*19fc2eaeSStefan Roese 
1393*19fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
1394*19fc2eaeSStefan Roese 	if (smi_wait_ready(pp) < 0)
1395*19fc2eaeSStefan Roese 		return -EFAULT;
1396*19fc2eaeSStefan Roese 
1397*19fc2eaeSStefan Roese 	/* fill the phy address and regiser offset and read opcode */
1398*19fc2eaeSStefan Roese 	smi_reg = (phy_adr << MVNETA_SMI_DEV_ADDR_OFFS)
1399*19fc2eaeSStefan Roese 		| (reg_ofs << MVNETA_SMI_REG_ADDR_OFFS)
1400*19fc2eaeSStefan Roese 		| MVNETA_SMI_OPCODE_READ;
1401*19fc2eaeSStefan Roese 
1402*19fc2eaeSStefan Roese 	/* write the smi register */
1403*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SMI, smi_reg);
1404*19fc2eaeSStefan Roese 
1405*19fc2eaeSStefan Roese 	/*wait till read value is ready */
1406*19fc2eaeSStefan Roese 	timeout = MVNETA_SMI_TIMEOUT;
1407*19fc2eaeSStefan Roese 
1408*19fc2eaeSStefan Roese 	do {
1409*19fc2eaeSStefan Roese 		/* read smi register */
1410*19fc2eaeSStefan Roese 		smi_reg = mvreg_read(pp, MVNETA_SMI);
1411*19fc2eaeSStefan Roese 		if (timeout-- == 0) {
1412*19fc2eaeSStefan Roese 			printf("Err: SMI read ready timeout\n");
1413*19fc2eaeSStefan Roese 			return -EFAULT;
1414*19fc2eaeSStefan Roese 		}
1415*19fc2eaeSStefan Roese 	} while (!(smi_reg & MVNETA_SMI_READ_VALID));
1416*19fc2eaeSStefan Roese 
1417*19fc2eaeSStefan Roese 	/* Wait for the data to update in the SMI register */
1418*19fc2eaeSStefan Roese 	for (timeout = 0; timeout < MVNETA_SMI_TIMEOUT; timeout++)
1419*19fc2eaeSStefan Roese 		;
1420*19fc2eaeSStefan Roese 
1421*19fc2eaeSStefan Roese 	*data = (u16)(mvreg_read(pp, MVNETA_SMI) & MVNETA_SMI_DATA_MASK);
1422*19fc2eaeSStefan Roese 
1423*19fc2eaeSStefan Roese 	return 0;
1424*19fc2eaeSStefan Roese }
1425*19fc2eaeSStefan Roese 
1426*19fc2eaeSStefan Roese /*
1427*19fc2eaeSStefan Roese  * smi_reg_write - imiiphy_write callback function.
1428*19fc2eaeSStefan Roese  *
1429*19fc2eaeSStefan Roese  * Returns 0 if write succeed, -EINVAL on bad parameters
1430*19fc2eaeSStefan Roese  * -ETIME on timeout
1431*19fc2eaeSStefan Roese  */
1432*19fc2eaeSStefan Roese static int smi_reg_write(const char *devname, u8 phy_adr, u8 reg_ofs, u16 data)
1433*19fc2eaeSStefan Roese {
1434*19fc2eaeSStefan Roese 	struct eth_device *dev = eth_get_dev_by_name(devname);
1435*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1436*19fc2eaeSStefan Roese 	u32 smi_reg;
1437*19fc2eaeSStefan Roese 
1438*19fc2eaeSStefan Roese 	/* check parameters */
1439*19fc2eaeSStefan Roese 	if (phy_adr > MVNETA_PHY_ADDR_MASK) {
1440*19fc2eaeSStefan Roese 		printf("Error: Invalid PHY address %d\n", phy_adr);
1441*19fc2eaeSStefan Roese 		return -EFAULT;
1442*19fc2eaeSStefan Roese 	}
1443*19fc2eaeSStefan Roese 
1444*19fc2eaeSStefan Roese 	if (reg_ofs > MVNETA_PHY_REG_MASK) {
1445*19fc2eaeSStefan Roese 		printf("Err: Invalid register offset %d\n", reg_ofs);
1446*19fc2eaeSStefan Roese 		return -EFAULT;
1447*19fc2eaeSStefan Roese 	}
1448*19fc2eaeSStefan Roese 
1449*19fc2eaeSStefan Roese 	/* wait till the SMI is not busy */
1450*19fc2eaeSStefan Roese 	if (smi_wait_ready(pp) < 0)
1451*19fc2eaeSStefan Roese 		return -EFAULT;
1452*19fc2eaeSStefan Roese 
1453*19fc2eaeSStefan Roese 	/* fill the phy addr and reg offset and write opcode and data */
1454*19fc2eaeSStefan Roese 	smi_reg = (data << MVNETA_SMI_DATA_OFFS);
1455*19fc2eaeSStefan Roese 	smi_reg |= (phy_adr << MVNETA_SMI_DEV_ADDR_OFFS)
1456*19fc2eaeSStefan Roese 		| (reg_ofs << MVNETA_SMI_REG_ADDR_OFFS);
1457*19fc2eaeSStefan Roese 	smi_reg &= ~MVNETA_SMI_OPCODE_READ;
1458*19fc2eaeSStefan Roese 
1459*19fc2eaeSStefan Roese 	/* write the smi register */
1460*19fc2eaeSStefan Roese 	mvreg_write(pp, MVNETA_SMI, smi_reg);
1461*19fc2eaeSStefan Roese 
1462*19fc2eaeSStefan Roese 	return 0;
1463*19fc2eaeSStefan Roese }
1464*19fc2eaeSStefan Roese 
1465*19fc2eaeSStefan Roese static int mvneta_init_u_boot(struct eth_device *dev, bd_t *bis)
1466*19fc2eaeSStefan Roese {
1467*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1468*19fc2eaeSStefan Roese 	struct phy_device *phydev;
1469*19fc2eaeSStefan Roese 
1470*19fc2eaeSStefan Roese 	mvneta_port_power_up(pp, pp->phy_interface);
1471*19fc2eaeSStefan Roese 
1472*19fc2eaeSStefan Roese 	if (!pp->init || pp->link == 0) {
1473*19fc2eaeSStefan Roese 		/* Set phy address of the port */
1474*19fc2eaeSStefan Roese 		mvreg_write(pp, MVNETA_PHY_ADDR, pp->phyaddr);
1475*19fc2eaeSStefan Roese 		phydev = phy_connect(pp->bus, pp->phyaddr, dev,
1476*19fc2eaeSStefan Roese 				     pp->phy_interface);
1477*19fc2eaeSStefan Roese 
1478*19fc2eaeSStefan Roese 		pp->phydev = phydev;
1479*19fc2eaeSStefan Roese 		phy_config(phydev);
1480*19fc2eaeSStefan Roese 		phy_startup(phydev);
1481*19fc2eaeSStefan Roese 		if (!phydev->link) {
1482*19fc2eaeSStefan Roese 			printf("%s: No link.\n", phydev->dev->name);
1483*19fc2eaeSStefan Roese 			return -1;
1484*19fc2eaeSStefan Roese 		}
1485*19fc2eaeSStefan Roese 
1486*19fc2eaeSStefan Roese 		/* Full init on first call */
1487*19fc2eaeSStefan Roese 		mvneta_probe(dev);
1488*19fc2eaeSStefan Roese 		pp->init = 1;
1489*19fc2eaeSStefan Roese 	} else {
1490*19fc2eaeSStefan Roese 		/* Upon all following calls, this is enough */
1491*19fc2eaeSStefan Roese 		mvneta_port_up(pp);
1492*19fc2eaeSStefan Roese 		mvneta_port_enable(pp);
1493*19fc2eaeSStefan Roese 	}
1494*19fc2eaeSStefan Roese 
1495*19fc2eaeSStefan Roese 	return 0;
1496*19fc2eaeSStefan Roese }
1497*19fc2eaeSStefan Roese 
1498*19fc2eaeSStefan Roese static int mvneta_send(struct eth_device *dev, void *ptr, int len)
1499*19fc2eaeSStefan Roese {
1500*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1501*19fc2eaeSStefan Roese 	struct mvneta_tx_queue *txq = &pp->txqs[0];
1502*19fc2eaeSStefan Roese 	struct mvneta_tx_desc *tx_desc;
1503*19fc2eaeSStefan Roese 	int sent_desc;
1504*19fc2eaeSStefan Roese 	u32 timeout = 0;
1505*19fc2eaeSStefan Roese 
1506*19fc2eaeSStefan Roese 	/* Get a descriptor for the first part of the packet */
1507*19fc2eaeSStefan Roese 	tx_desc = mvneta_txq_next_desc_get(txq);
1508*19fc2eaeSStefan Roese 
1509*19fc2eaeSStefan Roese 	tx_desc->buf_phys_addr = (u32)ptr;
1510*19fc2eaeSStefan Roese 	tx_desc->data_size = len;
1511*19fc2eaeSStefan Roese 	flush_dcache_range((u32)ptr, (u32)ptr + len);
1512*19fc2eaeSStefan Roese 
1513*19fc2eaeSStefan Roese 	/* First and Last descriptor */
1514*19fc2eaeSStefan Roese 	tx_desc->command = MVNETA_TX_L4_CSUM_NOT | MVNETA_TXD_FLZ_DESC;
1515*19fc2eaeSStefan Roese 	mvneta_txq_pend_desc_add(pp, txq, 1);
1516*19fc2eaeSStefan Roese 
1517*19fc2eaeSStefan Roese 	/* Wait for packet to be sent (queue might help with speed here) */
1518*19fc2eaeSStefan Roese 	sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1519*19fc2eaeSStefan Roese 	while (!sent_desc) {
1520*19fc2eaeSStefan Roese 		if (timeout++ > 10000) {
1521*19fc2eaeSStefan Roese 			printf("timeout: packet not sent\n");
1522*19fc2eaeSStefan Roese 			return -1;
1523*19fc2eaeSStefan Roese 		}
1524*19fc2eaeSStefan Roese 		sent_desc = mvneta_txq_sent_desc_num_get(pp, txq);
1525*19fc2eaeSStefan Roese 	}
1526*19fc2eaeSStefan Roese 
1527*19fc2eaeSStefan Roese 	/* txDone has increased - hw sent packet */
1528*19fc2eaeSStefan Roese 	mvneta_txq_sent_desc_dec(pp, txq, sent_desc);
1529*19fc2eaeSStefan Roese 	return 0;
1530*19fc2eaeSStefan Roese 
1531*19fc2eaeSStefan Roese 	return 0;
1532*19fc2eaeSStefan Roese }
1533*19fc2eaeSStefan Roese 
1534*19fc2eaeSStefan Roese static int mvneta_recv(struct eth_device *dev)
1535*19fc2eaeSStefan Roese {
1536*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1537*19fc2eaeSStefan Roese 	int rx_done;
1538*19fc2eaeSStefan Roese 	int packets_done;
1539*19fc2eaeSStefan Roese 	struct mvneta_rx_queue *rxq;
1540*19fc2eaeSStefan Roese 
1541*19fc2eaeSStefan Roese 	/* get rx queue */
1542*19fc2eaeSStefan Roese 	rxq = mvneta_rxq_handle_get(pp, rxq_def);
1543*19fc2eaeSStefan Roese 	rx_done = mvneta_rxq_busy_desc_num_get(pp, rxq);
1544*19fc2eaeSStefan Roese 	packets_done = rx_done;
1545*19fc2eaeSStefan Roese 
1546*19fc2eaeSStefan Roese 	while (packets_done--) {
1547*19fc2eaeSStefan Roese 		struct mvneta_rx_desc *rx_desc;
1548*19fc2eaeSStefan Roese 		unsigned char *data;
1549*19fc2eaeSStefan Roese 		u32 rx_status;
1550*19fc2eaeSStefan Roese 		int rx_bytes;
1551*19fc2eaeSStefan Roese 
1552*19fc2eaeSStefan Roese 		/*
1553*19fc2eaeSStefan Roese 		 * No cache invalidation needed here, since the desc's are
1554*19fc2eaeSStefan Roese 		 * located in a uncached memory region
1555*19fc2eaeSStefan Roese 		 */
1556*19fc2eaeSStefan Roese 		rx_desc = mvneta_rxq_next_desc_get(rxq);
1557*19fc2eaeSStefan Roese 
1558*19fc2eaeSStefan Roese 		rx_status = rx_desc->status;
1559*19fc2eaeSStefan Roese 		if (!mvneta_rxq_desc_is_first_last(rx_status) ||
1560*19fc2eaeSStefan Roese 		    (rx_status & MVNETA_RXD_ERR_SUMMARY)) {
1561*19fc2eaeSStefan Roese 			mvneta_rx_error(pp, rx_desc);
1562*19fc2eaeSStefan Roese 			/* leave the descriptor untouched */
1563*19fc2eaeSStefan Roese 			continue;
1564*19fc2eaeSStefan Roese 		}
1565*19fc2eaeSStefan Roese 
1566*19fc2eaeSStefan Roese 		/* 2 bytes for marvell header. 4 bytes for crc */
1567*19fc2eaeSStefan Roese 		rx_bytes = rx_desc->data_size - 6;
1568*19fc2eaeSStefan Roese 
1569*19fc2eaeSStefan Roese 		/* give packet to stack - skip on first 2 bytes */
1570*19fc2eaeSStefan Roese 		data = (u8 *)rx_desc->buf_cookie + 2;
1571*19fc2eaeSStefan Roese 		/*
1572*19fc2eaeSStefan Roese 		 * No cache invalidation needed here, since the rx_buffer's are
1573*19fc2eaeSStefan Roese 		 * located in a uncached memory region
1574*19fc2eaeSStefan Roese 		 */
1575*19fc2eaeSStefan Roese 		NetReceive(data, rx_bytes);
1576*19fc2eaeSStefan Roese 	}
1577*19fc2eaeSStefan Roese 
1578*19fc2eaeSStefan Roese 	/* Update rxq management counters */
1579*19fc2eaeSStefan Roese 	if (rx_done)
1580*19fc2eaeSStefan Roese 		mvneta_rxq_desc_num_update(pp, rxq, rx_done, rx_done);
1581*19fc2eaeSStefan Roese 
1582*19fc2eaeSStefan Roese 	return 0;
1583*19fc2eaeSStefan Roese }
1584*19fc2eaeSStefan Roese 
1585*19fc2eaeSStefan Roese static void mvneta_halt(struct eth_device *dev)
1586*19fc2eaeSStefan Roese {
1587*19fc2eaeSStefan Roese 	struct mvneta_port *pp = dev->priv;
1588*19fc2eaeSStefan Roese 
1589*19fc2eaeSStefan Roese 	mvneta_port_down(pp);
1590*19fc2eaeSStefan Roese 	mvneta_port_disable(pp);
1591*19fc2eaeSStefan Roese }
1592*19fc2eaeSStefan Roese 
1593*19fc2eaeSStefan Roese int mvneta_initialize(bd_t *bis, int base_addr, int devnum, int phy_addr)
1594*19fc2eaeSStefan Roese {
1595*19fc2eaeSStefan Roese 	struct eth_device *dev;
1596*19fc2eaeSStefan Roese 	struct mvneta_port *pp;
1597*19fc2eaeSStefan Roese 	void *bd_space;
1598*19fc2eaeSStefan Roese 
1599*19fc2eaeSStefan Roese 	dev = calloc(1, sizeof(*dev));
1600*19fc2eaeSStefan Roese 	if (dev == NULL)
1601*19fc2eaeSStefan Roese 		return -ENOMEM;
1602*19fc2eaeSStefan Roese 
1603*19fc2eaeSStefan Roese 	pp = calloc(1, sizeof(*pp));
1604*19fc2eaeSStefan Roese 	if (pp == NULL)
1605*19fc2eaeSStefan Roese 		return -ENOMEM;
1606*19fc2eaeSStefan Roese 
1607*19fc2eaeSStefan Roese 	dev->priv = pp;
1608*19fc2eaeSStefan Roese 
1609*19fc2eaeSStefan Roese 	/*
1610*19fc2eaeSStefan Roese 	 * Allocate buffer area for descs and rx_buffers. This is only
1611*19fc2eaeSStefan Roese 	 * done once for all interfaces. As only one interface can
1612*19fc2eaeSStefan Roese 	 * be active. Make this area DMA save by disabling the D-cache
1613*19fc2eaeSStefan Roese 	 */
1614*19fc2eaeSStefan Roese 	if (!buffer_loc.tx_descs) {
1615*19fc2eaeSStefan Roese 		/* Align buffer area for descs and rx_buffers to 1MiB */
1616*19fc2eaeSStefan Roese 		bd_space = memalign(1 << MMU_SECTION_SHIFT, BD_SPACE);
1617*19fc2eaeSStefan Roese 		mmu_set_region_dcache_behaviour((u32)bd_space, BD_SPACE,
1618*19fc2eaeSStefan Roese 						DCACHE_OFF);
1619*19fc2eaeSStefan Roese 		buffer_loc.tx_descs = (struct mvneta_tx_desc *)bd_space;
1620*19fc2eaeSStefan Roese 		buffer_loc.rx_descs = (struct mvneta_rx_desc *)
1621*19fc2eaeSStefan Roese 			((u32)bd_space +
1622*19fc2eaeSStefan Roese 			 MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc));
1623*19fc2eaeSStefan Roese 		buffer_loc.rx_buffers = (u32)
1624*19fc2eaeSStefan Roese 			(bd_space +
1625*19fc2eaeSStefan Roese 			 MVNETA_MAX_TXD * sizeof(struct mvneta_tx_desc) +
1626*19fc2eaeSStefan Roese 			 MVNETA_MAX_RXD * sizeof(struct mvneta_rx_desc));
1627*19fc2eaeSStefan Roese 	}
1628*19fc2eaeSStefan Roese 
1629*19fc2eaeSStefan Roese 	sprintf(dev->name, "neta%d", devnum);
1630*19fc2eaeSStefan Roese 
1631*19fc2eaeSStefan Roese 	pp->base = (void __iomem *)base_addr;
1632*19fc2eaeSStefan Roese 	dev->iobase = base_addr;
1633*19fc2eaeSStefan Roese 	dev->init = mvneta_init_u_boot;
1634*19fc2eaeSStefan Roese 	dev->halt = mvneta_halt;
1635*19fc2eaeSStefan Roese 	dev->send = mvneta_send;
1636*19fc2eaeSStefan Roese 	dev->recv = mvneta_recv;
1637*19fc2eaeSStefan Roese 	dev->write_hwaddr = NULL;
1638*19fc2eaeSStefan Roese 
1639*19fc2eaeSStefan Roese 	/*
1640*19fc2eaeSStefan Roese 	 * The PHY interface type is configured via the
1641*19fc2eaeSStefan Roese 	 * board specific CONFIG_SYS_NETA_INTERFACE_TYPE
1642*19fc2eaeSStefan Roese 	 * define.
1643*19fc2eaeSStefan Roese 	 */
1644*19fc2eaeSStefan Roese 	pp->phy_interface = CONFIG_SYS_NETA_INTERFACE_TYPE;
1645*19fc2eaeSStefan Roese 
1646*19fc2eaeSStefan Roese 	eth_register(dev);
1647*19fc2eaeSStefan Roese 
1648*19fc2eaeSStefan Roese 	pp->phyaddr = phy_addr;
1649*19fc2eaeSStefan Roese 	miiphy_register(dev->name, smi_reg_read, smi_reg_write);
1650*19fc2eaeSStefan Roese 	pp->bus = miiphy_get_dev_by_name(dev->name);
1651*19fc2eaeSStefan Roese 
1652*19fc2eaeSStefan Roese 	return 1;
1653*19fc2eaeSStefan Roese }
1654