xref: /OK3568_Linux_fs/u-boot/drivers/net/fm/eth.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * Copyright 2009-2012 Freescale Semiconductor, Inc.
3*4882a593Smuzhiyun  *	Dave Liu <daveliu@freescale.com>
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * SPDX-License-Identifier:	GPL-2.0+
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <common.h>
8*4882a593Smuzhiyun #include <asm/io.h>
9*4882a593Smuzhiyun #include <malloc.h>
10*4882a593Smuzhiyun #include <net.h>
11*4882a593Smuzhiyun #include <hwconfig.h>
12*4882a593Smuzhiyun #include <fm_eth.h>
13*4882a593Smuzhiyun #include <fsl_mdio.h>
14*4882a593Smuzhiyun #include <miiphy.h>
15*4882a593Smuzhiyun #include <phy.h>
16*4882a593Smuzhiyun #include <fsl_dtsec.h>
17*4882a593Smuzhiyun #include <fsl_tgec.h>
18*4882a593Smuzhiyun #include <fsl_memac.h>
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun #include "fm.h"
21*4882a593Smuzhiyun 
22*4882a593Smuzhiyun static struct eth_device *devlist[NUM_FM_PORTS];
23*4882a593Smuzhiyun static int num_controllers;
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
28*4882a593Smuzhiyun 			 TBIANA_FULL_DUPLEX)
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define TBIANA_SGMII_ACK 0x4001
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
33*4882a593Smuzhiyun 			TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun /* Configure the TBI for SGMII operation */
dtsec_configure_serdes(struct fm_eth * priv)36*4882a593Smuzhiyun static void dtsec_configure_serdes(struct fm_eth *priv)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun #ifdef CONFIG_SYS_FMAN_V3
39*4882a593Smuzhiyun 	u32 value;
40*4882a593Smuzhiyun 	struct mii_dev bus;
41*4882a593Smuzhiyun 	bus.priv = priv->mac->phyregs;
42*4882a593Smuzhiyun 	bool sgmii_2500 = (priv->enet_if ==
43*4882a593Smuzhiyun 			PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
44*4882a593Smuzhiyun 	int i = 0;
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun qsgmii_loop:
47*4882a593Smuzhiyun 	/* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
48*4882a593Smuzhiyun 	if (sgmii_2500)
49*4882a593Smuzhiyun 		value = PHY_SGMII_CR_PHY_RESET |
50*4882a593Smuzhiyun 			PHY_SGMII_IF_SPEED_GIGABIT |
51*4882a593Smuzhiyun 			PHY_SGMII_IF_MODE_SGMII;
52*4882a593Smuzhiyun 	else
53*4882a593Smuzhiyun 		value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	/* Dev ability according to SGMII specification */
58*4882a593Smuzhiyun 	value = PHY_SGMII_DEV_ABILITY_SGMII;
59*4882a593Smuzhiyun 	memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	if (sgmii_2500) {
62*4882a593Smuzhiyun 		/* Adjust link timer for 2.5G SGMII,
63*4882a593Smuzhiyun 		 * 1.6 ms in units of 3.2 ns:
64*4882a593Smuzhiyun 		 * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
65*4882a593Smuzhiyun 		 */
66*4882a593Smuzhiyun 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
67*4882a593Smuzhiyun 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
68*4882a593Smuzhiyun 	} else {
69*4882a593Smuzhiyun 		/* Adjust link timer for SGMII,
70*4882a593Smuzhiyun 		 * 1.6 ms in units of 8 ns:
71*4882a593Smuzhiyun 		 * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
72*4882a593Smuzhiyun 		 */
73*4882a593Smuzhiyun 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
74*4882a593Smuzhiyun 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	/* Restart AN */
78*4882a593Smuzhiyun 	value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
79*4882a593Smuzhiyun 	memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
82*4882a593Smuzhiyun 		i++;
83*4882a593Smuzhiyun 		goto qsgmii_loop;
84*4882a593Smuzhiyun 	}
85*4882a593Smuzhiyun #else
86*4882a593Smuzhiyun 	struct dtsec *regs = priv->mac->base;
87*4882a593Smuzhiyun 	struct tsec_mii_mng *phyregs = priv->mac->phyregs;
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun 	/*
90*4882a593Smuzhiyun 	 * Access TBI PHY registers at given TSEC register offset as
91*4882a593Smuzhiyun 	 * opposed to the register offset used for external PHY accesses
92*4882a593Smuzhiyun 	 */
93*4882a593Smuzhiyun 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
94*4882a593Smuzhiyun 			TBICON_CLK_SELECT);
95*4882a593Smuzhiyun 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
96*4882a593Smuzhiyun 			TBIANA_SGMII_ACK);
97*4882a593Smuzhiyun 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
98*4882a593Smuzhiyun 			TBI_CR, TBICR_SETTINGS);
99*4882a593Smuzhiyun #endif
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
dtsec_init_phy(struct eth_device * dev)102*4882a593Smuzhiyun static void dtsec_init_phy(struct eth_device *dev)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	struct fm_eth *fm_eth = dev->priv;
105*4882a593Smuzhiyun #ifndef CONFIG_SYS_FMAN_V3
106*4882a593Smuzhiyun 	struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
107*4882a593Smuzhiyun 
108*4882a593Smuzhiyun 	/* Assign a Physical address to the TBI */
109*4882a593Smuzhiyun 	out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
110*4882a593Smuzhiyun #endif
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
113*4882a593Smuzhiyun 	    fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
114*4882a593Smuzhiyun 	    fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
115*4882a593Smuzhiyun 		dtsec_configure_serdes(fm_eth);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun #ifdef CONFIG_PHYLIB
tgec_is_fibre(struct eth_device * dev)119*4882a593Smuzhiyun static int tgec_is_fibre(struct eth_device *dev)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct fm_eth *fm = dev->priv;
122*4882a593Smuzhiyun 	char phyopt[20];
123*4882a593Smuzhiyun 
124*4882a593Smuzhiyun 	sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
125*4882a593Smuzhiyun 
126*4882a593Smuzhiyun 	return hwconfig_arg_cmp(phyopt, "xfi");
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun #endif
129*4882a593Smuzhiyun #endif
130*4882a593Smuzhiyun 
muram_readw(u16 * addr)131*4882a593Smuzhiyun static u16 muram_readw(u16 *addr)
132*4882a593Smuzhiyun {
133*4882a593Smuzhiyun 	ulong base = (ulong)addr & ~0x3UL;
134*4882a593Smuzhiyun 	u32 val32 = in_be32((void *)base);
135*4882a593Smuzhiyun 	int byte_pos;
136*4882a593Smuzhiyun 	u16 ret;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	byte_pos = (ulong)addr & 0x3UL;
139*4882a593Smuzhiyun 	if (byte_pos)
140*4882a593Smuzhiyun 		ret = (u16)(val32 & 0x0000ffff);
141*4882a593Smuzhiyun 	else
142*4882a593Smuzhiyun 		ret = (u16)((val32 & 0xffff0000) >> 16);
143*4882a593Smuzhiyun 
144*4882a593Smuzhiyun 	return ret;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
muram_writew(u16 * addr,u16 val)147*4882a593Smuzhiyun static void muram_writew(u16 *addr, u16 val)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	ulong base = (ulong)addr & ~0x3UL;
150*4882a593Smuzhiyun 	u32 org32 = in_be32((void *)base);
151*4882a593Smuzhiyun 	u32 val32;
152*4882a593Smuzhiyun 	int byte_pos;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	byte_pos = (ulong)addr & 0x3UL;
155*4882a593Smuzhiyun 	if (byte_pos)
156*4882a593Smuzhiyun 		val32 = (org32 & 0xffff0000) | val;
157*4882a593Smuzhiyun 	else
158*4882a593Smuzhiyun 		val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	out_be32((void *)base, val32);
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun 
bmi_rx_port_disable(struct fm_bmi_rx_port * rx_port)163*4882a593Smuzhiyun static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
164*4882a593Smuzhiyun {
165*4882a593Smuzhiyun 	int timeout = 1000000;
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun 	clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	/* wait until the rx port is not busy */
170*4882a593Smuzhiyun 	while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
171*4882a593Smuzhiyun 		;
172*4882a593Smuzhiyun }
173*4882a593Smuzhiyun 
bmi_rx_port_init(struct fm_bmi_rx_port * rx_port)174*4882a593Smuzhiyun static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun 	/* set BMI to independent mode, Rx port disable */
177*4882a593Smuzhiyun 	out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
178*4882a593Smuzhiyun 	/* clear FOF in IM case */
179*4882a593Smuzhiyun 	out_be32(&rx_port->fmbm_rim, 0);
180*4882a593Smuzhiyun 	/* Rx frame next engine -RISC */
181*4882a593Smuzhiyun 	out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
182*4882a593Smuzhiyun 	/* Rx command attribute - no order, MR[3] = 1 */
183*4882a593Smuzhiyun 	clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
184*4882a593Smuzhiyun 	setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
185*4882a593Smuzhiyun 	/* enable Rx statistic counters */
186*4882a593Smuzhiyun 	out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
187*4882a593Smuzhiyun 	/* disable Rx performance counters */
188*4882a593Smuzhiyun 	out_be32(&rx_port->fmbm_rpc, 0);
189*4882a593Smuzhiyun }
190*4882a593Smuzhiyun 
bmi_tx_port_disable(struct fm_bmi_tx_port * tx_port)191*4882a593Smuzhiyun static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
192*4882a593Smuzhiyun {
193*4882a593Smuzhiyun 	int timeout = 1000000;
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun 	clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 	/* wait until the tx port is not busy */
198*4882a593Smuzhiyun 	while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
199*4882a593Smuzhiyun 		;
200*4882a593Smuzhiyun }
201*4882a593Smuzhiyun 
bmi_tx_port_init(struct fm_bmi_tx_port * tx_port)202*4882a593Smuzhiyun static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
203*4882a593Smuzhiyun {
204*4882a593Smuzhiyun 	/* set BMI to independent mode, Tx port disable */
205*4882a593Smuzhiyun 	out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
206*4882a593Smuzhiyun 	/* Tx frame next engine -RISC */
207*4882a593Smuzhiyun 	out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
208*4882a593Smuzhiyun 	out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
209*4882a593Smuzhiyun 	/* Tx command attribute - no order, MR[3] = 1 */
210*4882a593Smuzhiyun 	clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
211*4882a593Smuzhiyun 	setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
212*4882a593Smuzhiyun 	/* enable Tx statistic counters */
213*4882a593Smuzhiyun 	out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
214*4882a593Smuzhiyun 	/* disable Tx performance counters */
215*4882a593Smuzhiyun 	out_be32(&tx_port->fmbm_tpc, 0);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
fm_eth_rx_port_parameter_init(struct fm_eth * fm_eth)218*4882a593Smuzhiyun static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun 	struct fm_port_global_pram *pram;
221*4882a593Smuzhiyun 	u32 pram_page_offset;
222*4882a593Smuzhiyun 	void *rx_bd_ring_base;
223*4882a593Smuzhiyun 	void *rx_buf_pool;
224*4882a593Smuzhiyun 	u32 bd_ring_base_lo, bd_ring_base_hi;
225*4882a593Smuzhiyun 	u32 buf_lo, buf_hi;
226*4882a593Smuzhiyun 	struct fm_port_bd *rxbd;
227*4882a593Smuzhiyun 	struct fm_port_qd *rxqd;
228*4882a593Smuzhiyun 	struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
229*4882a593Smuzhiyun 	int i;
230*4882a593Smuzhiyun 
231*4882a593Smuzhiyun 	/* alloc global parameter ram at MURAM */
232*4882a593Smuzhiyun 	pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
233*4882a593Smuzhiyun 		FM_PRAM_SIZE, FM_PRAM_ALIGN);
234*4882a593Smuzhiyun 	if (!pram) {
235*4882a593Smuzhiyun 		printf("%s: No muram for Rx global parameter\n", __func__);
236*4882a593Smuzhiyun 		return -ENOMEM;
237*4882a593Smuzhiyun 	}
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	fm_eth->rx_pram = pram;
240*4882a593Smuzhiyun 
241*4882a593Smuzhiyun 	/* parameter page offset to MURAM */
242*4882a593Smuzhiyun 	pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 	/* enable global mode- snooping data buffers and BDs */
245*4882a593Smuzhiyun 	out_be32(&pram->mode, PRAM_MODE_GLOBAL);
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	/* init the Rx queue descriptor pionter */
248*4882a593Smuzhiyun 	out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	/* set the max receive buffer length, power of 2 */
251*4882a593Smuzhiyun 	muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	/* alloc Rx buffer descriptors from main memory */
254*4882a593Smuzhiyun 	rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
255*4882a593Smuzhiyun 			* RX_BD_RING_SIZE);
256*4882a593Smuzhiyun 	if (!rx_bd_ring_base)
257*4882a593Smuzhiyun 		return -ENOMEM;
258*4882a593Smuzhiyun 
259*4882a593Smuzhiyun 	memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
260*4882a593Smuzhiyun 			* RX_BD_RING_SIZE);
261*4882a593Smuzhiyun 
262*4882a593Smuzhiyun 	/* alloc Rx buffer from main memory */
263*4882a593Smuzhiyun 	rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
264*4882a593Smuzhiyun 	if (!rx_buf_pool)
265*4882a593Smuzhiyun 		return -ENOMEM;
266*4882a593Smuzhiyun 
267*4882a593Smuzhiyun 	memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
268*4882a593Smuzhiyun 	debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	/* save them to fm_eth */
271*4882a593Smuzhiyun 	fm_eth->rx_bd_ring = rx_bd_ring_base;
272*4882a593Smuzhiyun 	fm_eth->cur_rxbd = rx_bd_ring_base;
273*4882a593Smuzhiyun 	fm_eth->rx_buf = rx_buf_pool;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	/* init Rx BDs ring */
276*4882a593Smuzhiyun 	rxbd = (struct fm_port_bd *)rx_bd_ring_base;
277*4882a593Smuzhiyun 	for (i = 0; i < RX_BD_RING_SIZE; i++) {
278*4882a593Smuzhiyun 		muram_writew(&rxbd->status, RxBD_EMPTY);
279*4882a593Smuzhiyun 		muram_writew(&rxbd->len, 0);
280*4882a593Smuzhiyun 		buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
281*4882a593Smuzhiyun 					i * MAX_RXBUF_LEN));
282*4882a593Smuzhiyun 		buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
283*4882a593Smuzhiyun 					i * MAX_RXBUF_LEN));
284*4882a593Smuzhiyun 		muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
285*4882a593Smuzhiyun 		out_be32(&rxbd->buf_ptr_lo, buf_lo);
286*4882a593Smuzhiyun 		rxbd++;
287*4882a593Smuzhiyun 	}
288*4882a593Smuzhiyun 
289*4882a593Smuzhiyun 	/* set the Rx queue descriptor */
290*4882a593Smuzhiyun 	rxqd = &pram->rxqd;
291*4882a593Smuzhiyun 	muram_writew(&rxqd->gen, 0);
292*4882a593Smuzhiyun 	bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
293*4882a593Smuzhiyun 	bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
294*4882a593Smuzhiyun 	muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
295*4882a593Smuzhiyun 	out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
296*4882a593Smuzhiyun 	muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
297*4882a593Smuzhiyun 			* RX_BD_RING_SIZE);
298*4882a593Smuzhiyun 	muram_writew(&rxqd->offset_in, 0);
299*4882a593Smuzhiyun 	muram_writew(&rxqd->offset_out, 0);
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	/* set IM parameter ram pointer to Rx Frame Queue ID */
302*4882a593Smuzhiyun 	out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
303*4882a593Smuzhiyun 
304*4882a593Smuzhiyun 	return 0;
305*4882a593Smuzhiyun }
306*4882a593Smuzhiyun 
fm_eth_tx_port_parameter_init(struct fm_eth * fm_eth)307*4882a593Smuzhiyun static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun 	struct fm_port_global_pram *pram;
310*4882a593Smuzhiyun 	u32 pram_page_offset;
311*4882a593Smuzhiyun 	void *tx_bd_ring_base;
312*4882a593Smuzhiyun 	u32 bd_ring_base_lo, bd_ring_base_hi;
313*4882a593Smuzhiyun 	struct fm_port_bd *txbd;
314*4882a593Smuzhiyun 	struct fm_port_qd *txqd;
315*4882a593Smuzhiyun 	struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
316*4882a593Smuzhiyun 	int i;
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	/* alloc global parameter ram at MURAM */
319*4882a593Smuzhiyun 	pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
320*4882a593Smuzhiyun 		FM_PRAM_SIZE, FM_PRAM_ALIGN);
321*4882a593Smuzhiyun 	if (!pram) {
322*4882a593Smuzhiyun 		printf("%s: No muram for Tx global parameter\n", __func__);
323*4882a593Smuzhiyun 		return -ENOMEM;
324*4882a593Smuzhiyun 	}
325*4882a593Smuzhiyun 	fm_eth->tx_pram = pram;
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun 	/* parameter page offset to MURAM */
328*4882a593Smuzhiyun 	pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
329*4882a593Smuzhiyun 
330*4882a593Smuzhiyun 	/* enable global mode- snooping data buffers and BDs */
331*4882a593Smuzhiyun 	out_be32(&pram->mode, PRAM_MODE_GLOBAL);
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	/* init the Tx queue descriptor pionter */
334*4882a593Smuzhiyun 	out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	/* alloc Tx buffer descriptors from main memory */
337*4882a593Smuzhiyun 	tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
338*4882a593Smuzhiyun 			* TX_BD_RING_SIZE);
339*4882a593Smuzhiyun 	if (!tx_bd_ring_base)
340*4882a593Smuzhiyun 		return -ENOMEM;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
343*4882a593Smuzhiyun 			* TX_BD_RING_SIZE);
344*4882a593Smuzhiyun 	/* save it to fm_eth */
345*4882a593Smuzhiyun 	fm_eth->tx_bd_ring = tx_bd_ring_base;
346*4882a593Smuzhiyun 	fm_eth->cur_txbd = tx_bd_ring_base;
347*4882a593Smuzhiyun 
348*4882a593Smuzhiyun 	/* init Tx BDs ring */
349*4882a593Smuzhiyun 	txbd = (struct fm_port_bd *)tx_bd_ring_base;
350*4882a593Smuzhiyun 	for (i = 0; i < TX_BD_RING_SIZE; i++) {
351*4882a593Smuzhiyun 		muram_writew(&txbd->status, TxBD_LAST);
352*4882a593Smuzhiyun 		muram_writew(&txbd->len, 0);
353*4882a593Smuzhiyun 		muram_writew(&txbd->buf_ptr_hi, 0);
354*4882a593Smuzhiyun 		out_be32(&txbd->buf_ptr_lo, 0);
355*4882a593Smuzhiyun 		txbd++;
356*4882a593Smuzhiyun 	}
357*4882a593Smuzhiyun 
358*4882a593Smuzhiyun 	/* set the Tx queue decriptor */
359*4882a593Smuzhiyun 	txqd = &pram->txqd;
360*4882a593Smuzhiyun 	bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
361*4882a593Smuzhiyun 	bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
362*4882a593Smuzhiyun 	muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
363*4882a593Smuzhiyun 	out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
364*4882a593Smuzhiyun 	muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
365*4882a593Smuzhiyun 			* TX_BD_RING_SIZE);
366*4882a593Smuzhiyun 	muram_writew(&txqd->offset_in, 0);
367*4882a593Smuzhiyun 	muram_writew(&txqd->offset_out, 0);
368*4882a593Smuzhiyun 
369*4882a593Smuzhiyun 	/* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
370*4882a593Smuzhiyun 	out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	return 0;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun 
fm_eth_init(struct fm_eth * fm_eth)375*4882a593Smuzhiyun static int fm_eth_init(struct fm_eth *fm_eth)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun 	int ret;
378*4882a593Smuzhiyun 
379*4882a593Smuzhiyun 	ret = fm_eth_rx_port_parameter_init(fm_eth);
380*4882a593Smuzhiyun 	if (ret)
381*4882a593Smuzhiyun 		return ret;
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	ret = fm_eth_tx_port_parameter_init(fm_eth);
384*4882a593Smuzhiyun 	if (ret)
385*4882a593Smuzhiyun 		return ret;
386*4882a593Smuzhiyun 
387*4882a593Smuzhiyun 	return 0;
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun 
fm_eth_startup(struct fm_eth * fm_eth)390*4882a593Smuzhiyun static int fm_eth_startup(struct fm_eth *fm_eth)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	struct fsl_enet_mac *mac;
393*4882a593Smuzhiyun 	int ret;
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun 	mac = fm_eth->mac;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	/* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
398*4882a593Smuzhiyun 	ret = fm_eth_init(fm_eth);
399*4882a593Smuzhiyun 	if (ret)
400*4882a593Smuzhiyun 		return ret;
401*4882a593Smuzhiyun 	/* setup the MAC controller */
402*4882a593Smuzhiyun 	mac->init_mac(mac);
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun 	/* For some reason we need to set SPEED_100 */
405*4882a593Smuzhiyun 	if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
406*4882a593Smuzhiyun 	     (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
407*4882a593Smuzhiyun 	     (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
408*4882a593Smuzhiyun 	      mac->set_if_mode)
409*4882a593Smuzhiyun 		mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	/* init bmi rx port, IM mode and disable */
412*4882a593Smuzhiyun 	bmi_rx_port_init(fm_eth->rx_port);
413*4882a593Smuzhiyun 	/* init bmi tx port, IM mode and disable */
414*4882a593Smuzhiyun 	bmi_tx_port_init(fm_eth->tx_port);
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	return 0;
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun 
fmc_tx_port_graceful_stop_enable(struct fm_eth * fm_eth)419*4882a593Smuzhiyun static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
420*4882a593Smuzhiyun {
421*4882a593Smuzhiyun 	struct fm_port_global_pram *pram;
422*4882a593Smuzhiyun 
423*4882a593Smuzhiyun 	pram = fm_eth->tx_pram;
424*4882a593Smuzhiyun 	/* graceful stop transmission of frames */
425*4882a593Smuzhiyun 	setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
426*4882a593Smuzhiyun 	sync();
427*4882a593Smuzhiyun }
428*4882a593Smuzhiyun 
fmc_tx_port_graceful_stop_disable(struct fm_eth * fm_eth)429*4882a593Smuzhiyun static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
430*4882a593Smuzhiyun {
431*4882a593Smuzhiyun 	struct fm_port_global_pram *pram;
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	pram = fm_eth->tx_pram;
434*4882a593Smuzhiyun 	/* re-enable transmission of frames */
435*4882a593Smuzhiyun 	clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
436*4882a593Smuzhiyun 	sync();
437*4882a593Smuzhiyun }
438*4882a593Smuzhiyun 
fm_eth_open(struct eth_device * dev,bd_t * bd)439*4882a593Smuzhiyun static int fm_eth_open(struct eth_device *dev, bd_t *bd)
440*4882a593Smuzhiyun {
441*4882a593Smuzhiyun 	struct fm_eth *fm_eth;
442*4882a593Smuzhiyun 	struct fsl_enet_mac *mac;
443*4882a593Smuzhiyun #ifdef CONFIG_PHYLIB
444*4882a593Smuzhiyun 	int ret;
445*4882a593Smuzhiyun #endif
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	fm_eth = (struct fm_eth *)dev->priv;
448*4882a593Smuzhiyun 	mac = fm_eth->mac;
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	/* setup the MAC address */
451*4882a593Smuzhiyun 	if (dev->enetaddr[0] & 0x01) {
452*4882a593Smuzhiyun 		printf("%s: MacAddress is multcast address\n",	__func__);
453*4882a593Smuzhiyun 		return 1;
454*4882a593Smuzhiyun 	}
455*4882a593Smuzhiyun 	mac->set_mac_addr(mac, dev->enetaddr);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	/* enable bmi Rx port */
458*4882a593Smuzhiyun 	setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
459*4882a593Smuzhiyun 	/* enable MAC rx/tx port */
460*4882a593Smuzhiyun 	mac->enable_mac(mac);
461*4882a593Smuzhiyun 	/* enable bmi Tx port */
462*4882a593Smuzhiyun 	setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
463*4882a593Smuzhiyun 	/* re-enable transmission of frame */
464*4882a593Smuzhiyun 	fmc_tx_port_graceful_stop_disable(fm_eth);
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun #ifdef CONFIG_PHYLIB
467*4882a593Smuzhiyun 	if (fm_eth->phydev) {
468*4882a593Smuzhiyun 		ret = phy_startup(fm_eth->phydev);
469*4882a593Smuzhiyun 		if (ret) {
470*4882a593Smuzhiyun 			printf("%s: Could not initialize\n",
471*4882a593Smuzhiyun 			       fm_eth->phydev->dev->name);
472*4882a593Smuzhiyun 			return ret;
473*4882a593Smuzhiyun 		}
474*4882a593Smuzhiyun 	} else {
475*4882a593Smuzhiyun 		return 0;
476*4882a593Smuzhiyun 	}
477*4882a593Smuzhiyun #else
478*4882a593Smuzhiyun 	fm_eth->phydev->speed = SPEED_1000;
479*4882a593Smuzhiyun 	fm_eth->phydev->link = 1;
480*4882a593Smuzhiyun 	fm_eth->phydev->duplex = DUPLEX_FULL;
481*4882a593Smuzhiyun #endif
482*4882a593Smuzhiyun 
483*4882a593Smuzhiyun 	/* set the MAC-PHY mode */
484*4882a593Smuzhiyun 	mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	if (!fm_eth->phydev->link)
487*4882a593Smuzhiyun 		printf("%s: No link.\n", fm_eth->phydev->dev->name);
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun 	return fm_eth->phydev->link ? 0 : -1;
490*4882a593Smuzhiyun }
491*4882a593Smuzhiyun 
fm_eth_halt(struct eth_device * dev)492*4882a593Smuzhiyun static void fm_eth_halt(struct eth_device *dev)
493*4882a593Smuzhiyun {
494*4882a593Smuzhiyun 	struct fm_eth *fm_eth;
495*4882a593Smuzhiyun 	struct fsl_enet_mac *mac;
496*4882a593Smuzhiyun 
497*4882a593Smuzhiyun 	fm_eth = (struct fm_eth *)dev->priv;
498*4882a593Smuzhiyun 	mac = fm_eth->mac;
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	/* graceful stop the transmission of frames */
501*4882a593Smuzhiyun 	fmc_tx_port_graceful_stop_enable(fm_eth);
502*4882a593Smuzhiyun 	/* disable bmi Tx port */
503*4882a593Smuzhiyun 	bmi_tx_port_disable(fm_eth->tx_port);
504*4882a593Smuzhiyun 	/* disable MAC rx/tx port */
505*4882a593Smuzhiyun 	mac->disable_mac(mac);
506*4882a593Smuzhiyun 	/* disable bmi Rx port */
507*4882a593Smuzhiyun 	bmi_rx_port_disable(fm_eth->rx_port);
508*4882a593Smuzhiyun 
509*4882a593Smuzhiyun #ifdef CONFIG_PHYLIB
510*4882a593Smuzhiyun 	if (fm_eth->phydev)
511*4882a593Smuzhiyun 		phy_shutdown(fm_eth->phydev);
512*4882a593Smuzhiyun #endif
513*4882a593Smuzhiyun }
514*4882a593Smuzhiyun 
fm_eth_send(struct eth_device * dev,void * buf,int len)515*4882a593Smuzhiyun static int fm_eth_send(struct eth_device *dev, void *buf, int len)
516*4882a593Smuzhiyun {
517*4882a593Smuzhiyun 	struct fm_eth *fm_eth;
518*4882a593Smuzhiyun 	struct fm_port_global_pram *pram;
519*4882a593Smuzhiyun 	struct fm_port_bd *txbd, *txbd_base;
520*4882a593Smuzhiyun 	u16 offset_in;
521*4882a593Smuzhiyun 	int i;
522*4882a593Smuzhiyun 
523*4882a593Smuzhiyun 	fm_eth = (struct fm_eth *)dev->priv;
524*4882a593Smuzhiyun 	pram = fm_eth->tx_pram;
525*4882a593Smuzhiyun 	txbd = fm_eth->cur_txbd;
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	/* find one empty TxBD */
528*4882a593Smuzhiyun 	for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
529*4882a593Smuzhiyun 		udelay(100);
530*4882a593Smuzhiyun 		if (i > 0x1000) {
531*4882a593Smuzhiyun 			printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
532*4882a593Smuzhiyun 			       dev->name, muram_readw(&txbd->status));
533*4882a593Smuzhiyun 			return 0;
534*4882a593Smuzhiyun 		}
535*4882a593Smuzhiyun 	}
536*4882a593Smuzhiyun 	/* setup TxBD */
537*4882a593Smuzhiyun 	muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
538*4882a593Smuzhiyun 	out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
539*4882a593Smuzhiyun 	muram_writew(&txbd->len, len);
540*4882a593Smuzhiyun 	sync();
541*4882a593Smuzhiyun 	muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
542*4882a593Smuzhiyun 	sync();
543*4882a593Smuzhiyun 
544*4882a593Smuzhiyun 	/* update TxQD, let RISC to send the packet */
545*4882a593Smuzhiyun 	offset_in = muram_readw(&pram->txqd.offset_in);
546*4882a593Smuzhiyun 	offset_in += sizeof(struct fm_port_bd);
547*4882a593Smuzhiyun 	if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
548*4882a593Smuzhiyun 		offset_in = 0;
549*4882a593Smuzhiyun 	muram_writew(&pram->txqd.offset_in, offset_in);
550*4882a593Smuzhiyun 	sync();
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	/* wait for buffer to be transmitted */
553*4882a593Smuzhiyun 	for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
554*4882a593Smuzhiyun 		udelay(100);
555*4882a593Smuzhiyun 		if (i > 0x10000) {
556*4882a593Smuzhiyun 			printf("%s: Tx error, txbd->status = 0x%x\n",
557*4882a593Smuzhiyun 			       dev->name, muram_readw(&txbd->status));
558*4882a593Smuzhiyun 			return 0;
559*4882a593Smuzhiyun 		}
560*4882a593Smuzhiyun 	}
561*4882a593Smuzhiyun 
562*4882a593Smuzhiyun 	/* advance the TxBD */
563*4882a593Smuzhiyun 	txbd++;
564*4882a593Smuzhiyun 	txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
565*4882a593Smuzhiyun 	if (txbd >= (txbd_base + TX_BD_RING_SIZE))
566*4882a593Smuzhiyun 		txbd = txbd_base;
567*4882a593Smuzhiyun 	/* update current txbd */
568*4882a593Smuzhiyun 	fm_eth->cur_txbd = (void *)txbd;
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	return 1;
571*4882a593Smuzhiyun }
572*4882a593Smuzhiyun 
fm_eth_recv(struct eth_device * dev)573*4882a593Smuzhiyun static int fm_eth_recv(struct eth_device *dev)
574*4882a593Smuzhiyun {
575*4882a593Smuzhiyun 	struct fm_eth *fm_eth;
576*4882a593Smuzhiyun 	struct fm_port_global_pram *pram;
577*4882a593Smuzhiyun 	struct fm_port_bd *rxbd, *rxbd_base;
578*4882a593Smuzhiyun 	u16 status, len;
579*4882a593Smuzhiyun 	u32 buf_lo, buf_hi;
580*4882a593Smuzhiyun 	u8 *data;
581*4882a593Smuzhiyun 	u16 offset_out;
582*4882a593Smuzhiyun 	int ret = 1;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	fm_eth = (struct fm_eth *)dev->priv;
585*4882a593Smuzhiyun 	pram = fm_eth->rx_pram;
586*4882a593Smuzhiyun 	rxbd = fm_eth->cur_rxbd;
587*4882a593Smuzhiyun 	status = muram_readw(&rxbd->status);
588*4882a593Smuzhiyun 
589*4882a593Smuzhiyun 	while (!(status & RxBD_EMPTY)) {
590*4882a593Smuzhiyun 		if (!(status & RxBD_ERROR)) {
591*4882a593Smuzhiyun 			buf_hi = muram_readw(&rxbd->buf_ptr_hi);
592*4882a593Smuzhiyun 			buf_lo = in_be32(&rxbd->buf_ptr_lo);
593*4882a593Smuzhiyun 			data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
594*4882a593Smuzhiyun 			len = muram_readw(&rxbd->len);
595*4882a593Smuzhiyun 			net_process_received_packet(data, len);
596*4882a593Smuzhiyun 		} else {
597*4882a593Smuzhiyun 			printf("%s: Rx error\n", dev->name);
598*4882a593Smuzhiyun 			ret = 0;
599*4882a593Smuzhiyun 		}
600*4882a593Smuzhiyun 
601*4882a593Smuzhiyun 		/* clear the RxBDs */
602*4882a593Smuzhiyun 		muram_writew(&rxbd->status, RxBD_EMPTY);
603*4882a593Smuzhiyun 		muram_writew(&rxbd->len, 0);
604*4882a593Smuzhiyun 		sync();
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 		/* advance RxBD */
607*4882a593Smuzhiyun 		rxbd++;
608*4882a593Smuzhiyun 		rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
609*4882a593Smuzhiyun 		if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
610*4882a593Smuzhiyun 			rxbd = rxbd_base;
611*4882a593Smuzhiyun 		/* read next status */
612*4882a593Smuzhiyun 		status = muram_readw(&rxbd->status);
613*4882a593Smuzhiyun 
614*4882a593Smuzhiyun 		/* update RxQD */
615*4882a593Smuzhiyun 		offset_out = muram_readw(&pram->rxqd.offset_out);
616*4882a593Smuzhiyun 		offset_out += sizeof(struct fm_port_bd);
617*4882a593Smuzhiyun 		if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
618*4882a593Smuzhiyun 			offset_out = 0;
619*4882a593Smuzhiyun 		muram_writew(&pram->rxqd.offset_out, offset_out);
620*4882a593Smuzhiyun 		sync();
621*4882a593Smuzhiyun 	}
622*4882a593Smuzhiyun 	fm_eth->cur_rxbd = (void *)rxbd;
623*4882a593Smuzhiyun 
624*4882a593Smuzhiyun 	return ret;
625*4882a593Smuzhiyun }
626*4882a593Smuzhiyun 
fm_eth_init_mac(struct fm_eth * fm_eth,struct ccsr_fman * reg)627*4882a593Smuzhiyun static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
628*4882a593Smuzhiyun {
629*4882a593Smuzhiyun 	struct fsl_enet_mac *mac;
630*4882a593Smuzhiyun 	int num;
631*4882a593Smuzhiyun 	void *base, *phyregs = NULL;
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	num = fm_eth->num;
634*4882a593Smuzhiyun 
635*4882a593Smuzhiyun #ifdef CONFIG_SYS_FMAN_V3
636*4882a593Smuzhiyun #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
637*4882a593Smuzhiyun 	if (fm_eth->type == FM_ETH_10G_E) {
638*4882a593Smuzhiyun 		/* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
639*4882a593Smuzhiyun 		 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
640*4882a593Smuzhiyun 		 * 10GEC1 uses mEMAC1 on T1024.
641*4882a593Smuzhiyun 		 * so it needs to change the num.
642*4882a593Smuzhiyun 		 */
643*4882a593Smuzhiyun 		if (fm_eth->num >= 2)
644*4882a593Smuzhiyun 			num -= 2;
645*4882a593Smuzhiyun 		else
646*4882a593Smuzhiyun 			num += 8;
647*4882a593Smuzhiyun 	}
648*4882a593Smuzhiyun #endif
649*4882a593Smuzhiyun 	base = &reg->memac[num].fm_memac;
650*4882a593Smuzhiyun 	phyregs = &reg->memac[num].fm_memac_mdio;
651*4882a593Smuzhiyun #else
652*4882a593Smuzhiyun 	/* Get the mac registers base address */
653*4882a593Smuzhiyun 	if (fm_eth->type == FM_ETH_1G_E) {
654*4882a593Smuzhiyun 		base = &reg->mac_1g[num].fm_dtesc;
655*4882a593Smuzhiyun 		phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
656*4882a593Smuzhiyun 	} else {
657*4882a593Smuzhiyun 		base = &reg->mac_10g[num].fm_10gec;
658*4882a593Smuzhiyun 		phyregs = &reg->mac_10g[num].fm_10gec_mdio;
659*4882a593Smuzhiyun 	}
660*4882a593Smuzhiyun #endif
661*4882a593Smuzhiyun 
662*4882a593Smuzhiyun 	/* alloc mac controller */
663*4882a593Smuzhiyun 	mac = malloc(sizeof(struct fsl_enet_mac));
664*4882a593Smuzhiyun 	if (!mac)
665*4882a593Smuzhiyun 		return -ENOMEM;
666*4882a593Smuzhiyun 	memset(mac, 0, sizeof(struct fsl_enet_mac));
667*4882a593Smuzhiyun 
668*4882a593Smuzhiyun 	/* save the mac to fm_eth struct */
669*4882a593Smuzhiyun 	fm_eth->mac = mac;
670*4882a593Smuzhiyun 
671*4882a593Smuzhiyun #ifdef CONFIG_SYS_FMAN_V3
672*4882a593Smuzhiyun 	init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
673*4882a593Smuzhiyun #else
674*4882a593Smuzhiyun 	if (fm_eth->type == FM_ETH_1G_E)
675*4882a593Smuzhiyun 		init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
676*4882a593Smuzhiyun 	else
677*4882a593Smuzhiyun 		init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
678*4882a593Smuzhiyun #endif
679*4882a593Smuzhiyun 
680*4882a593Smuzhiyun 	return 0;
681*4882a593Smuzhiyun }
682*4882a593Smuzhiyun 
init_phy(struct eth_device * dev)683*4882a593Smuzhiyun static int init_phy(struct eth_device *dev)
684*4882a593Smuzhiyun {
685*4882a593Smuzhiyun 	struct fm_eth *fm_eth = dev->priv;
686*4882a593Smuzhiyun #ifdef CONFIG_PHYLIB
687*4882a593Smuzhiyun 	struct phy_device *phydev = NULL;
688*4882a593Smuzhiyun 	u32 supported;
689*4882a593Smuzhiyun #endif
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	if (fm_eth->type == FM_ETH_1G_E)
692*4882a593Smuzhiyun 		dtsec_init_phy(dev);
693*4882a593Smuzhiyun 
694*4882a593Smuzhiyun #ifdef CONFIG_PHYLIB
695*4882a593Smuzhiyun 	if (fm_eth->bus) {
696*4882a593Smuzhiyun 		phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, dev,
697*4882a593Smuzhiyun 					fm_eth->enet_if);
698*4882a593Smuzhiyun 		if (!phydev) {
699*4882a593Smuzhiyun 			printf("Failed to connect\n");
700*4882a593Smuzhiyun 			return -1;
701*4882a593Smuzhiyun 		}
702*4882a593Smuzhiyun 	} else {
703*4882a593Smuzhiyun 		return 0;
704*4882a593Smuzhiyun 	}
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	if (fm_eth->type == FM_ETH_1G_E) {
707*4882a593Smuzhiyun 		supported = (SUPPORTED_10baseT_Half |
708*4882a593Smuzhiyun 				SUPPORTED_10baseT_Full |
709*4882a593Smuzhiyun 				SUPPORTED_100baseT_Half |
710*4882a593Smuzhiyun 				SUPPORTED_100baseT_Full |
711*4882a593Smuzhiyun 				SUPPORTED_1000baseT_Full);
712*4882a593Smuzhiyun 	} else {
713*4882a593Smuzhiyun 		supported = SUPPORTED_10000baseT_Full;
714*4882a593Smuzhiyun 
715*4882a593Smuzhiyun 		if (tgec_is_fibre(dev))
716*4882a593Smuzhiyun 			phydev->port = PORT_FIBRE;
717*4882a593Smuzhiyun 	}
718*4882a593Smuzhiyun 
719*4882a593Smuzhiyun 	phydev->supported &= supported;
720*4882a593Smuzhiyun 	phydev->advertising = phydev->supported;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	fm_eth->phydev = phydev;
723*4882a593Smuzhiyun 
724*4882a593Smuzhiyun 	phy_config(phydev);
725*4882a593Smuzhiyun #endif
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	return 0;
728*4882a593Smuzhiyun }
729*4882a593Smuzhiyun 
fm_eth_initialize(struct ccsr_fman * reg,struct fm_eth_info * info)730*4882a593Smuzhiyun int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
731*4882a593Smuzhiyun {
732*4882a593Smuzhiyun 	struct eth_device *dev;
733*4882a593Smuzhiyun 	struct fm_eth *fm_eth;
734*4882a593Smuzhiyun 	int i, num = info->num;
735*4882a593Smuzhiyun 	int ret;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	/* alloc eth device */
738*4882a593Smuzhiyun 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
739*4882a593Smuzhiyun 	if (!dev)
740*4882a593Smuzhiyun 		return -ENOMEM;
741*4882a593Smuzhiyun 	memset(dev, 0, sizeof(struct eth_device));
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	/* alloc the FMan ethernet private struct */
744*4882a593Smuzhiyun 	fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
745*4882a593Smuzhiyun 	if (!fm_eth)
746*4882a593Smuzhiyun 		return -ENOMEM;
747*4882a593Smuzhiyun 	memset(fm_eth, 0, sizeof(struct fm_eth));
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun 	/* save off some things we need from the info struct */
750*4882a593Smuzhiyun 	fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
751*4882a593Smuzhiyun 	fm_eth->num = num;
752*4882a593Smuzhiyun 	fm_eth->type = info->type;
753*4882a593Smuzhiyun 
754*4882a593Smuzhiyun 	fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
755*4882a593Smuzhiyun 	fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
756*4882a593Smuzhiyun 
757*4882a593Smuzhiyun 	/* set the ethernet max receive length */
758*4882a593Smuzhiyun 	fm_eth->max_rx_len = MAX_RXBUF_LEN;
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun 	/* init global mac structure */
761*4882a593Smuzhiyun 	ret = fm_eth_init_mac(fm_eth, reg);
762*4882a593Smuzhiyun 	if (ret)
763*4882a593Smuzhiyun 		return ret;
764*4882a593Smuzhiyun 
765*4882a593Smuzhiyun 	/* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
766*4882a593Smuzhiyun 	if (fm_eth->type == FM_ETH_1G_E)
767*4882a593Smuzhiyun 		sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
768*4882a593Smuzhiyun 	else
769*4882a593Smuzhiyun 		sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
770*4882a593Smuzhiyun 
771*4882a593Smuzhiyun 	devlist[num_controllers++] = dev;
772*4882a593Smuzhiyun 	dev->iobase = 0;
773*4882a593Smuzhiyun 	dev->priv = (void *)fm_eth;
774*4882a593Smuzhiyun 	dev->init = fm_eth_open;
775*4882a593Smuzhiyun 	dev->halt = fm_eth_halt;
776*4882a593Smuzhiyun 	dev->send = fm_eth_send;
777*4882a593Smuzhiyun 	dev->recv = fm_eth_recv;
778*4882a593Smuzhiyun 	fm_eth->dev = dev;
779*4882a593Smuzhiyun 	fm_eth->bus = info->bus;
780*4882a593Smuzhiyun 	fm_eth->phyaddr = info->phy_addr;
781*4882a593Smuzhiyun 	fm_eth->enet_if = info->enet_if;
782*4882a593Smuzhiyun 
783*4882a593Smuzhiyun 	/* startup the FM im */
784*4882a593Smuzhiyun 	ret = fm_eth_startup(fm_eth);
785*4882a593Smuzhiyun 	if (ret)
786*4882a593Smuzhiyun 		return ret;
787*4882a593Smuzhiyun 
788*4882a593Smuzhiyun 	init_phy(dev);
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun 	/* clear the ethernet address */
791*4882a593Smuzhiyun 	for (i = 0; i < 6; i++)
792*4882a593Smuzhiyun 		dev->enetaddr[i] = 0;
793*4882a593Smuzhiyun 	eth_register(dev);
794*4882a593Smuzhiyun 
795*4882a593Smuzhiyun 	return 0;
796*4882a593Smuzhiyun }
797