xref: /rk3399_rockchip-uboot/drivers/net/fm/eth.c (revision f2465934b46235287e07473fa4919035ba1a2b68)
1c916d7c9SKumar Gala /*
2111fd19eSRoy Zang  * Copyright 2009-2012 Freescale Semiconductor, Inc.
3c916d7c9SKumar Gala  *	Dave Liu <daveliu@freescale.com>
4c916d7c9SKumar Gala  *
51a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
6c916d7c9SKumar Gala  */
7c916d7c9SKumar Gala #include <common.h>
8c916d7c9SKumar Gala #include <asm/io.h>
9c916d7c9SKumar Gala #include <malloc.h>
10c916d7c9SKumar Gala #include <net.h>
11c916d7c9SKumar Gala #include <hwconfig.h>
12c916d7c9SKumar Gala #include <fm_eth.h>
13c916d7c9SKumar Gala #include <fsl_mdio.h>
14c916d7c9SKumar Gala #include <miiphy.h>
15c916d7c9SKumar Gala #include <phy.h>
168225b2fdSShaohui Xie #include <fsl_dtsec.h>
178225b2fdSShaohui Xie #include <fsl_tgec.h>
18cd348efaSShaohui Xie #include <fsl_memac.h>
19c916d7c9SKumar Gala 
20c916d7c9SKumar Gala #include "fm.h"
21c916d7c9SKumar Gala 
22c916d7c9SKumar Gala static struct eth_device *devlist[NUM_FM_PORTS];
23c916d7c9SKumar Gala static int num_controllers;
24c916d7c9SKumar Gala 
25c916d7c9SKumar Gala #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
26c916d7c9SKumar Gala 
27c916d7c9SKumar Gala #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
28c916d7c9SKumar Gala 			 TBIANA_FULL_DUPLEX)
29c916d7c9SKumar Gala 
30c916d7c9SKumar Gala #define TBIANA_SGMII_ACK 0x4001
31c916d7c9SKumar Gala 
32c916d7c9SKumar Gala #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
33c916d7c9SKumar Gala 			TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
34c916d7c9SKumar Gala 
35c916d7c9SKumar Gala /* Configure the TBI for SGMII operation */
dtsec_configure_serdes(struct fm_eth * priv)36960d70c6SKim Phillips static void dtsec_configure_serdes(struct fm_eth *priv)
37c916d7c9SKumar Gala {
38111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
39111fd19eSRoy Zang 	u32 value;
40111fd19eSRoy Zang 	struct mii_dev bus;
41111fd19eSRoy Zang 	bus.priv = priv->mac->phyregs;
42c35f8693SShengzhou Liu 	bool sgmii_2500 = (priv->enet_if ==
43c35f8693SShengzhou Liu 			PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
44bc24611cSShaohui Xie 	int i = 0;
45111fd19eSRoy Zang 
46bc24611cSShaohui Xie qsgmii_loop:
47c35f8693SShengzhou Liu 	/* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
48*bead0880Sshaohui xie 	if (sgmii_2500)
49*bead0880Sshaohui xie 		value = PHY_SGMII_CR_PHY_RESET |
50*bead0880Sshaohui xie 			PHY_SGMII_IF_SPEED_GIGABIT |
51*bead0880Sshaohui xie 			PHY_SGMII_IF_MODE_SGMII;
52*bead0880Sshaohui xie 	else
53*bead0880Sshaohui xie 		value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
54c35f8693SShengzhou Liu 
55bc24611cSShaohui Xie 	memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
56111fd19eSRoy Zang 
57111fd19eSRoy Zang 	/* Dev ability according to SGMII specification */
58111fd19eSRoy Zang 	value = PHY_SGMII_DEV_ABILITY_SGMII;
59bc24611cSShaohui Xie 	memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
60111fd19eSRoy Zang 
61*bead0880Sshaohui xie 	if (sgmii_2500) {
62*bead0880Sshaohui xie 		/* Adjust link timer for 2.5G SGMII,
63*bead0880Sshaohui xie 		 * 1.6 ms in units of 3.2 ns:
64*bead0880Sshaohui xie 		 * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
65*bead0880Sshaohui xie 		 */
66*bead0880Sshaohui xie 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
67*bead0880Sshaohui xie 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
68*bead0880Sshaohui xie 	} else {
69*bead0880Sshaohui xie 		/* Adjust link timer for SGMII,
70*bead0880Sshaohui xie 		 * 1.6 ms in units of 8 ns:
71*bead0880Sshaohui xie 		 * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
72*bead0880Sshaohui xie 		 */
73*bead0880Sshaohui xie 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
74*bead0880Sshaohui xie 		memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
75*bead0880Sshaohui xie 	}
76111fd19eSRoy Zang 
77111fd19eSRoy Zang 	/* Restart AN */
78*bead0880Sshaohui xie 	value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
79bc24611cSShaohui Xie 	memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
80bc24611cSShaohui Xie 
81bc24611cSShaohui Xie 	if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
82bc24611cSShaohui Xie 		i++;
83bc24611cSShaohui Xie 		goto qsgmii_loop;
84bc24611cSShaohui Xie 	}
85111fd19eSRoy Zang #else
86c916d7c9SKumar Gala 	struct dtsec *regs = priv->mac->base;
87c916d7c9SKumar Gala 	struct tsec_mii_mng *phyregs = priv->mac->phyregs;
88c916d7c9SKumar Gala 
89c916d7c9SKumar Gala 	/*
90c916d7c9SKumar Gala 	 * Access TBI PHY registers at given TSEC register offset as
91c916d7c9SKumar Gala 	 * opposed to the register offset used for external PHY accesses
92c916d7c9SKumar Gala 	 */
93c916d7c9SKumar Gala 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
94c916d7c9SKumar Gala 			TBICON_CLK_SELECT);
95c916d7c9SKumar Gala 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
96c916d7c9SKumar Gala 			TBIANA_SGMII_ACK);
97c916d7c9SKumar Gala 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
98c916d7c9SKumar Gala 			TBI_CR, TBICR_SETTINGS);
99111fd19eSRoy Zang #endif
100c916d7c9SKumar Gala }
101c916d7c9SKumar Gala 
dtsec_init_phy(struct eth_device * dev)102c916d7c9SKumar Gala static void dtsec_init_phy(struct eth_device *dev)
103c916d7c9SKumar Gala {
104c916d7c9SKumar Gala 	struct fm_eth *fm_eth = dev->priv;
105111fd19eSRoy Zang #ifndef CONFIG_SYS_FMAN_V3
1061f3bd3e2Sshaohui xie 	struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
1071f3bd3e2Sshaohui xie 
108c916d7c9SKumar Gala 	/* Assign a Physical address to the TBI */
109c916d7c9SKumar Gala 	out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
110111fd19eSRoy Zang #endif
111c916d7c9SKumar Gala 
112c35f8693SShengzhou Liu 	if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
113bc24611cSShaohui Xie 	    fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
114c35f8693SShengzhou Liu 	    fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
115c916d7c9SKumar Gala 		dtsec_configure_serdes(fm_eth);
116c916d7c9SKumar Gala }
117c916d7c9SKumar Gala 
11829d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
tgec_is_fibre(struct eth_device * dev)119c916d7c9SKumar Gala static int tgec_is_fibre(struct eth_device *dev)
120c916d7c9SKumar Gala {
121c916d7c9SKumar Gala 	struct fm_eth *fm = dev->priv;
122c916d7c9SKumar Gala 	char phyopt[20];
123c916d7c9SKumar Gala 
124c916d7c9SKumar Gala 	sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
125c916d7c9SKumar Gala 
126c916d7c9SKumar Gala 	return hwconfig_arg_cmp(phyopt, "xfi");
127c916d7c9SKumar Gala }
128c916d7c9SKumar Gala #endif
12929d8c814SShaohui Xie #endif
130c916d7c9SKumar Gala 
muram_readw(u16 * addr)131c916d7c9SKumar Gala static u16 muram_readw(u16 *addr)
132c916d7c9SKumar Gala {
1339fc29db1SHou Zhiqiang 	ulong base = (ulong)addr & ~0x3UL;
1349fc29db1SHou Zhiqiang 	u32 val32 = in_be32((void *)base);
135c916d7c9SKumar Gala 	int byte_pos;
136c916d7c9SKumar Gala 	u16 ret;
137c916d7c9SKumar Gala 
1389fc29db1SHou Zhiqiang 	byte_pos = (ulong)addr & 0x3UL;
139c916d7c9SKumar Gala 	if (byte_pos)
140c916d7c9SKumar Gala 		ret = (u16)(val32 & 0x0000ffff);
141c916d7c9SKumar Gala 	else
142c916d7c9SKumar Gala 		ret = (u16)((val32 & 0xffff0000) >> 16);
143c916d7c9SKumar Gala 
144c916d7c9SKumar Gala 	return ret;
145c916d7c9SKumar Gala }
146c916d7c9SKumar Gala 
muram_writew(u16 * addr,u16 val)147c916d7c9SKumar Gala static void muram_writew(u16 *addr, u16 val)
148c916d7c9SKumar Gala {
1499fc29db1SHou Zhiqiang 	ulong base = (ulong)addr & ~0x3UL;
1509fc29db1SHou Zhiqiang 	u32 org32 = in_be32((void *)base);
151c916d7c9SKumar Gala 	u32 val32;
152c916d7c9SKumar Gala 	int byte_pos;
153c916d7c9SKumar Gala 
1549fc29db1SHou Zhiqiang 	byte_pos = (ulong)addr & 0x3UL;
155c916d7c9SKumar Gala 	if (byte_pos)
156c916d7c9SKumar Gala 		val32 = (org32 & 0xffff0000) | val;
157c916d7c9SKumar Gala 	else
158c916d7c9SKumar Gala 		val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
159c916d7c9SKumar Gala 
1609fc29db1SHou Zhiqiang 	out_be32((void *)base, val32);
161c916d7c9SKumar Gala }
162c916d7c9SKumar Gala 
bmi_rx_port_disable(struct fm_bmi_rx_port * rx_port)163c916d7c9SKumar Gala static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
164c916d7c9SKumar Gala {
165c916d7c9SKumar Gala 	int timeout = 1000000;
166c916d7c9SKumar Gala 
167c916d7c9SKumar Gala 	clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
168c916d7c9SKumar Gala 
169c916d7c9SKumar Gala 	/* wait until the rx port is not busy */
170c916d7c9SKumar Gala 	while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
171c916d7c9SKumar Gala 		;
172c916d7c9SKumar Gala }
173c916d7c9SKumar Gala 
bmi_rx_port_init(struct fm_bmi_rx_port * rx_port)174c916d7c9SKumar Gala static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
175c916d7c9SKumar Gala {
176c916d7c9SKumar Gala 	/* set BMI to independent mode, Rx port disable */
177c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
178c916d7c9SKumar Gala 	/* clear FOF in IM case */
179c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rim, 0);
180c916d7c9SKumar Gala 	/* Rx frame next engine -RISC */
181c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
182c916d7c9SKumar Gala 	/* Rx command attribute - no order, MR[3] = 1 */
183c916d7c9SKumar Gala 	clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
184c916d7c9SKumar Gala 	setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
185c916d7c9SKumar Gala 	/* enable Rx statistic counters */
186c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
187c916d7c9SKumar Gala 	/* disable Rx performance counters */
188c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rpc, 0);
189c916d7c9SKumar Gala }
190c916d7c9SKumar Gala 
bmi_tx_port_disable(struct fm_bmi_tx_port * tx_port)191c916d7c9SKumar Gala static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
192c916d7c9SKumar Gala {
193c916d7c9SKumar Gala 	int timeout = 1000000;
194c916d7c9SKumar Gala 
195c916d7c9SKumar Gala 	clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
196c916d7c9SKumar Gala 
197c916d7c9SKumar Gala 	/* wait until the tx port is not busy */
198c916d7c9SKumar Gala 	while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
199c916d7c9SKumar Gala 		;
200c916d7c9SKumar Gala }
201c916d7c9SKumar Gala 
bmi_tx_port_init(struct fm_bmi_tx_port * tx_port)202c916d7c9SKumar Gala static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
203c916d7c9SKumar Gala {
204c916d7c9SKumar Gala 	/* set BMI to independent mode, Tx port disable */
205c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
206c916d7c9SKumar Gala 	/* Tx frame next engine -RISC */
207c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
208c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
209c916d7c9SKumar Gala 	/* Tx command attribute - no order, MR[3] = 1 */
210c916d7c9SKumar Gala 	clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
211c916d7c9SKumar Gala 	setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
212c916d7c9SKumar Gala 	/* enable Tx statistic counters */
213c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
214c916d7c9SKumar Gala 	/* disable Tx performance counters */
215c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tpc, 0);
216c916d7c9SKumar Gala }
217c916d7c9SKumar Gala 
fm_eth_rx_port_parameter_init(struct fm_eth * fm_eth)218c916d7c9SKumar Gala static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
219c916d7c9SKumar Gala {
220c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
221c916d7c9SKumar Gala 	u32 pram_page_offset;
222c916d7c9SKumar Gala 	void *rx_bd_ring_base;
223c916d7c9SKumar Gala 	void *rx_buf_pool;
2249fc29db1SHou Zhiqiang 	u32 bd_ring_base_lo, bd_ring_base_hi;
2259fc29db1SHou Zhiqiang 	u32 buf_lo, buf_hi;
226c916d7c9SKumar Gala 	struct fm_port_bd *rxbd;
227c916d7c9SKumar Gala 	struct fm_port_qd *rxqd;
228c916d7c9SKumar Gala 	struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
229c916d7c9SKumar Gala 	int i;
230c916d7c9SKumar Gala 
231c916d7c9SKumar Gala 	/* alloc global parameter ram at MURAM */
232c916d7c9SKumar Gala 	pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
233c916d7c9SKumar Gala 		FM_PRAM_SIZE, FM_PRAM_ALIGN);
2349fc29db1SHou Zhiqiang 	if (!pram) {
2359fc29db1SHou Zhiqiang 		printf("%s: No muram for Rx global parameter\n", __func__);
2360f2cb9f5SHou Zhiqiang 		return -ENOMEM;
2379fc29db1SHou Zhiqiang 	}
2389fc29db1SHou Zhiqiang 
239c916d7c9SKumar Gala 	fm_eth->rx_pram = pram;
240c916d7c9SKumar Gala 
241c916d7c9SKumar Gala 	/* parameter page offset to MURAM */
2429fc29db1SHou Zhiqiang 	pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
243c916d7c9SKumar Gala 
244c916d7c9SKumar Gala 	/* enable global mode- snooping data buffers and BDs */
245648bde6dSHou Zhiqiang 	out_be32(&pram->mode, PRAM_MODE_GLOBAL);
246c916d7c9SKumar Gala 
247c916d7c9SKumar Gala 	/* init the Rx queue descriptor pionter */
248648bde6dSHou Zhiqiang 	out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
249c916d7c9SKumar Gala 
250c916d7c9SKumar Gala 	/* set the max receive buffer length, power of 2 */
251c916d7c9SKumar Gala 	muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
252c916d7c9SKumar Gala 
253c916d7c9SKumar Gala 	/* alloc Rx buffer descriptors from main memory */
254c916d7c9SKumar Gala 	rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
255c916d7c9SKumar Gala 			* RX_BD_RING_SIZE);
256c916d7c9SKumar Gala 	if (!rx_bd_ring_base)
2570f2cb9f5SHou Zhiqiang 		return -ENOMEM;
2580f2cb9f5SHou Zhiqiang 
259c916d7c9SKumar Gala 	memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
260c916d7c9SKumar Gala 			* RX_BD_RING_SIZE);
261c916d7c9SKumar Gala 
262c916d7c9SKumar Gala 	/* alloc Rx buffer from main memory */
263c916d7c9SKumar Gala 	rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
264c916d7c9SKumar Gala 	if (!rx_buf_pool)
2650f2cb9f5SHou Zhiqiang 		return -ENOMEM;
2660f2cb9f5SHou Zhiqiang 
267c916d7c9SKumar Gala 	memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
2689fc29db1SHou Zhiqiang 	debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
269c916d7c9SKumar Gala 
270c916d7c9SKumar Gala 	/* save them to fm_eth */
271c916d7c9SKumar Gala 	fm_eth->rx_bd_ring = rx_bd_ring_base;
272c916d7c9SKumar Gala 	fm_eth->cur_rxbd = rx_bd_ring_base;
273c916d7c9SKumar Gala 	fm_eth->rx_buf = rx_buf_pool;
274c916d7c9SKumar Gala 
275c916d7c9SKumar Gala 	/* init Rx BDs ring */
276c916d7c9SKumar Gala 	rxbd = (struct fm_port_bd *)rx_bd_ring_base;
277c916d7c9SKumar Gala 	for (i = 0; i < RX_BD_RING_SIZE; i++) {
278648bde6dSHou Zhiqiang 		muram_writew(&rxbd->status, RxBD_EMPTY);
279648bde6dSHou Zhiqiang 		muram_writew(&rxbd->len, 0);
2809fc29db1SHou Zhiqiang 		buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
2819fc29db1SHou Zhiqiang 					i * MAX_RXBUF_LEN));
2829fc29db1SHou Zhiqiang 		buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
2839fc29db1SHou Zhiqiang 					i * MAX_RXBUF_LEN));
2849fc29db1SHou Zhiqiang 		muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
2859fc29db1SHou Zhiqiang 		out_be32(&rxbd->buf_ptr_lo, buf_lo);
286c916d7c9SKumar Gala 		rxbd++;
287c916d7c9SKumar Gala 	}
288c916d7c9SKumar Gala 
289c916d7c9SKumar Gala 	/* set the Rx queue descriptor */
290c916d7c9SKumar Gala 	rxqd = &pram->rxqd;
291c916d7c9SKumar Gala 	muram_writew(&rxqd->gen, 0);
2929fc29db1SHou Zhiqiang 	bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
2939fc29db1SHou Zhiqiang 	bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
2949fc29db1SHou Zhiqiang 	muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
2959fc29db1SHou Zhiqiang 	out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
296c916d7c9SKumar Gala 	muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
297c916d7c9SKumar Gala 			* RX_BD_RING_SIZE);
298c916d7c9SKumar Gala 	muram_writew(&rxqd->offset_in, 0);
299c916d7c9SKumar Gala 	muram_writew(&rxqd->offset_out, 0);
300c916d7c9SKumar Gala 
301c916d7c9SKumar Gala 	/* set IM parameter ram pointer to Rx Frame Queue ID */
302c916d7c9SKumar Gala 	out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
303c916d7c9SKumar Gala 
3040f2cb9f5SHou Zhiqiang 	return 0;
305c916d7c9SKumar Gala }
306c916d7c9SKumar Gala 
fm_eth_tx_port_parameter_init(struct fm_eth * fm_eth)307c916d7c9SKumar Gala static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
308c916d7c9SKumar Gala {
309c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
310c916d7c9SKumar Gala 	u32 pram_page_offset;
311c916d7c9SKumar Gala 	void *tx_bd_ring_base;
3129fc29db1SHou Zhiqiang 	u32 bd_ring_base_lo, bd_ring_base_hi;
313c916d7c9SKumar Gala 	struct fm_port_bd *txbd;
314c916d7c9SKumar Gala 	struct fm_port_qd *txqd;
315c916d7c9SKumar Gala 	struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
316c916d7c9SKumar Gala 	int i;
317c916d7c9SKumar Gala 
318c916d7c9SKumar Gala 	/* alloc global parameter ram at MURAM */
319c916d7c9SKumar Gala 	pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
320c916d7c9SKumar Gala 		FM_PRAM_SIZE, FM_PRAM_ALIGN);
3219fc29db1SHou Zhiqiang 	if (!pram) {
3229fc29db1SHou Zhiqiang 		printf("%s: No muram for Tx global parameter\n", __func__);
3230f2cb9f5SHou Zhiqiang 		return -ENOMEM;
3249fc29db1SHou Zhiqiang 	}
325c916d7c9SKumar Gala 	fm_eth->tx_pram = pram;
326c916d7c9SKumar Gala 
327c916d7c9SKumar Gala 	/* parameter page offset to MURAM */
3289fc29db1SHou Zhiqiang 	pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
329c916d7c9SKumar Gala 
330c916d7c9SKumar Gala 	/* enable global mode- snooping data buffers and BDs */
331648bde6dSHou Zhiqiang 	out_be32(&pram->mode, PRAM_MODE_GLOBAL);
332c916d7c9SKumar Gala 
333c916d7c9SKumar Gala 	/* init the Tx queue descriptor pionter */
334648bde6dSHou Zhiqiang 	out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
335c916d7c9SKumar Gala 
336c916d7c9SKumar Gala 	/* alloc Tx buffer descriptors from main memory */
337c916d7c9SKumar Gala 	tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
338c916d7c9SKumar Gala 			* TX_BD_RING_SIZE);
339c916d7c9SKumar Gala 	if (!tx_bd_ring_base)
3400f2cb9f5SHou Zhiqiang 		return -ENOMEM;
3410f2cb9f5SHou Zhiqiang 
342c916d7c9SKumar Gala 	memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
343c916d7c9SKumar Gala 			* TX_BD_RING_SIZE);
344c916d7c9SKumar Gala 	/* save it to fm_eth */
345c916d7c9SKumar Gala 	fm_eth->tx_bd_ring = tx_bd_ring_base;
346c916d7c9SKumar Gala 	fm_eth->cur_txbd = tx_bd_ring_base;
347c916d7c9SKumar Gala 
348c916d7c9SKumar Gala 	/* init Tx BDs ring */
349c916d7c9SKumar Gala 	txbd = (struct fm_port_bd *)tx_bd_ring_base;
350c916d7c9SKumar Gala 	for (i = 0; i < TX_BD_RING_SIZE; i++) {
351648bde6dSHou Zhiqiang 		muram_writew(&txbd->status, TxBD_LAST);
352648bde6dSHou Zhiqiang 		muram_writew(&txbd->len, 0);
353648bde6dSHou Zhiqiang 		muram_writew(&txbd->buf_ptr_hi, 0);
354648bde6dSHou Zhiqiang 		out_be32(&txbd->buf_ptr_lo, 0);
355648bde6dSHou Zhiqiang 		txbd++;
356c916d7c9SKumar Gala 	}
357c916d7c9SKumar Gala 
358c916d7c9SKumar Gala 	/* set the Tx queue decriptor */
359c916d7c9SKumar Gala 	txqd = &pram->txqd;
3609fc29db1SHou Zhiqiang 	bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
3619fc29db1SHou Zhiqiang 	bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
3629fc29db1SHou Zhiqiang 	muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
3639fc29db1SHou Zhiqiang 	out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
364c916d7c9SKumar Gala 	muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
365c916d7c9SKumar Gala 			* TX_BD_RING_SIZE);
366c916d7c9SKumar Gala 	muram_writew(&txqd->offset_in, 0);
367c916d7c9SKumar Gala 	muram_writew(&txqd->offset_out, 0);
368c916d7c9SKumar Gala 
369c916d7c9SKumar Gala 	/* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
370c916d7c9SKumar Gala 	out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
371c916d7c9SKumar Gala 
3720f2cb9f5SHou Zhiqiang 	return 0;
373c916d7c9SKumar Gala }
374c916d7c9SKumar Gala 
fm_eth_init(struct fm_eth * fm_eth)375c916d7c9SKumar Gala static int fm_eth_init(struct fm_eth *fm_eth)
376c916d7c9SKumar Gala {
3770f2cb9f5SHou Zhiqiang 	int ret;
378c916d7c9SKumar Gala 
3790f2cb9f5SHou Zhiqiang 	ret = fm_eth_rx_port_parameter_init(fm_eth);
3800f2cb9f5SHou Zhiqiang 	if (ret)
3810f2cb9f5SHou Zhiqiang 		return ret;
3820f2cb9f5SHou Zhiqiang 
3830f2cb9f5SHou Zhiqiang 	ret = fm_eth_tx_port_parameter_init(fm_eth);
3840f2cb9f5SHou Zhiqiang 	if (ret)
3850f2cb9f5SHou Zhiqiang 		return ret;
3860f2cb9f5SHou Zhiqiang 
387c916d7c9SKumar Gala 	return 0;
388c916d7c9SKumar Gala }
389c916d7c9SKumar Gala 
fm_eth_startup(struct fm_eth * fm_eth)390c916d7c9SKumar Gala static int fm_eth_startup(struct fm_eth *fm_eth)
391c916d7c9SKumar Gala {
392c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
3930f2cb9f5SHou Zhiqiang 	int ret;
3940f2cb9f5SHou Zhiqiang 
395c916d7c9SKumar Gala 	mac = fm_eth->mac;
396c916d7c9SKumar Gala 
397c916d7c9SKumar Gala 	/* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
3980f2cb9f5SHou Zhiqiang 	ret = fm_eth_init(fm_eth);
3990f2cb9f5SHou Zhiqiang 	if (ret)
4000f2cb9f5SHou Zhiqiang 		return ret;
401c916d7c9SKumar Gala 	/* setup the MAC controller */
402c916d7c9SKumar Gala 	mac->init_mac(mac);
403c916d7c9SKumar Gala 
404c916d7c9SKumar Gala 	/* For some reason we need to set SPEED_100 */
4051c68d01eSShaohui Xie 	if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
406*bead0880Sshaohui xie 	     (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
4071c68d01eSShaohui Xie 	     (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
4081c68d01eSShaohui Xie 	      mac->set_if_mode)
409c916d7c9SKumar Gala 		mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
410c916d7c9SKumar Gala 
411c916d7c9SKumar Gala 	/* init bmi rx port, IM mode and disable */
412c916d7c9SKumar Gala 	bmi_rx_port_init(fm_eth->rx_port);
413c916d7c9SKumar Gala 	/* init bmi tx port, IM mode and disable */
414c916d7c9SKumar Gala 	bmi_tx_port_init(fm_eth->tx_port);
415c916d7c9SKumar Gala 
4160f2cb9f5SHou Zhiqiang 	return 0;
417c916d7c9SKumar Gala }
418c916d7c9SKumar Gala 
fmc_tx_port_graceful_stop_enable(struct fm_eth * fm_eth)419c916d7c9SKumar Gala static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
420c916d7c9SKumar Gala {
421c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
422c916d7c9SKumar Gala 
423c916d7c9SKumar Gala 	pram = fm_eth->tx_pram;
424c916d7c9SKumar Gala 	/* graceful stop transmission of frames */
425648bde6dSHou Zhiqiang 	setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
426c916d7c9SKumar Gala 	sync();
427c916d7c9SKumar Gala }
428c916d7c9SKumar Gala 
fmc_tx_port_graceful_stop_disable(struct fm_eth * fm_eth)429c916d7c9SKumar Gala static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
430c916d7c9SKumar Gala {
431c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
432c916d7c9SKumar Gala 
433c916d7c9SKumar Gala 	pram = fm_eth->tx_pram;
434c916d7c9SKumar Gala 	/* re-enable transmission of frames */
435648bde6dSHou Zhiqiang 	clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
436c916d7c9SKumar Gala 	sync();
437c916d7c9SKumar Gala }
438c916d7c9SKumar Gala 
fm_eth_open(struct eth_device * dev,bd_t * bd)439c916d7c9SKumar Gala static int fm_eth_open(struct eth_device *dev, bd_t *bd)
440c916d7c9SKumar Gala {
441c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
442c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
44311af8d65STimur Tabi #ifdef CONFIG_PHYLIB
44411af8d65STimur Tabi 	int ret;
44511af8d65STimur Tabi #endif
446c916d7c9SKumar Gala 
447c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
448c916d7c9SKumar Gala 	mac = fm_eth->mac;
449c916d7c9SKumar Gala 
450c916d7c9SKumar Gala 	/* setup the MAC address */
451c916d7c9SKumar Gala 	if (dev->enetaddr[0] & 0x01) {
452c916d7c9SKumar Gala 		printf("%s: MacAddress is multcast address\n",	__func__);
453c916d7c9SKumar Gala 		return 1;
454c916d7c9SKumar Gala 	}
455c916d7c9SKumar Gala 	mac->set_mac_addr(mac, dev->enetaddr);
456c916d7c9SKumar Gala 
457c916d7c9SKumar Gala 	/* enable bmi Rx port */
458c916d7c9SKumar Gala 	setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
459c916d7c9SKumar Gala 	/* enable MAC rx/tx port */
460c916d7c9SKumar Gala 	mac->enable_mac(mac);
461c916d7c9SKumar Gala 	/* enable bmi Tx port */
462c916d7c9SKumar Gala 	setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
463c916d7c9SKumar Gala 	/* re-enable transmission of frame */
464c916d7c9SKumar Gala 	fmc_tx_port_graceful_stop_disable(fm_eth);
465c916d7c9SKumar Gala 
466c916d7c9SKumar Gala #ifdef CONFIG_PHYLIB
4676798c324SCodrin Ciubotariu 	if (fm_eth->phydev) {
46811af8d65STimur Tabi 		ret = phy_startup(fm_eth->phydev);
46911af8d65STimur Tabi 		if (ret) {
4706798c324SCodrin Ciubotariu 			printf("%s: Could not initialize\n",
4716798c324SCodrin Ciubotariu 			       fm_eth->phydev->dev->name);
47211af8d65STimur Tabi 			return ret;
47311af8d65STimur Tabi 		}
4746798c324SCodrin Ciubotariu 	} else {
4756798c324SCodrin Ciubotariu 		return 0;
4766798c324SCodrin Ciubotariu 	}
477c916d7c9SKumar Gala #else
478c916d7c9SKumar Gala 	fm_eth->phydev->speed = SPEED_1000;
479c916d7c9SKumar Gala 	fm_eth->phydev->link = 1;
480c916d7c9SKumar Gala 	fm_eth->phydev->duplex = DUPLEX_FULL;
481c916d7c9SKumar Gala #endif
482c916d7c9SKumar Gala 
483c916d7c9SKumar Gala 	/* set the MAC-PHY mode */
484c916d7c9SKumar Gala 	mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
485c916d7c9SKumar Gala 
486c916d7c9SKumar Gala 	if (!fm_eth->phydev->link)
487c916d7c9SKumar Gala 		printf("%s: No link.\n", fm_eth->phydev->dev->name);
488c916d7c9SKumar Gala 
489c916d7c9SKumar Gala 	return fm_eth->phydev->link ? 0 : -1;
490c916d7c9SKumar Gala }
491c916d7c9SKumar Gala 
fm_eth_halt(struct eth_device * dev)492c916d7c9SKumar Gala static void fm_eth_halt(struct eth_device *dev)
493c916d7c9SKumar Gala {
494c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
495c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
496c916d7c9SKumar Gala 
497c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
498c916d7c9SKumar Gala 	mac = fm_eth->mac;
499c916d7c9SKumar Gala 
500c916d7c9SKumar Gala 	/* graceful stop the transmission of frames */
501c916d7c9SKumar Gala 	fmc_tx_port_graceful_stop_enable(fm_eth);
502c916d7c9SKumar Gala 	/* disable bmi Tx port */
503c916d7c9SKumar Gala 	bmi_tx_port_disable(fm_eth->tx_port);
504c916d7c9SKumar Gala 	/* disable MAC rx/tx port */
505c916d7c9SKumar Gala 	mac->disable_mac(mac);
506c916d7c9SKumar Gala 	/* disable bmi Rx port */
507c916d7c9SKumar Gala 	bmi_rx_port_disable(fm_eth->rx_port);
508c916d7c9SKumar Gala 
50929d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
5106798c324SCodrin Ciubotariu 	if (fm_eth->phydev)
511c916d7c9SKumar Gala 		phy_shutdown(fm_eth->phydev);
51229d8c814SShaohui Xie #endif
513c916d7c9SKumar Gala }
514c916d7c9SKumar Gala 
fm_eth_send(struct eth_device * dev,void * buf,int len)515e9df2018SJoe Hershberger static int fm_eth_send(struct eth_device *dev, void *buf, int len)
516c916d7c9SKumar Gala {
517c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
518c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
519c916d7c9SKumar Gala 	struct fm_port_bd *txbd, *txbd_base;
520c916d7c9SKumar Gala 	u16 offset_in;
521c916d7c9SKumar Gala 	int i;
522c916d7c9SKumar Gala 
523c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
524c916d7c9SKumar Gala 	pram = fm_eth->tx_pram;
525c916d7c9SKumar Gala 	txbd = fm_eth->cur_txbd;
526c916d7c9SKumar Gala 
527c916d7c9SKumar Gala 	/* find one empty TxBD */
528648bde6dSHou Zhiqiang 	for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
529c916d7c9SKumar Gala 		udelay(100);
530c916d7c9SKumar Gala 		if (i > 0x1000) {
531648bde6dSHou Zhiqiang 			printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
532648bde6dSHou Zhiqiang 			       dev->name, muram_readw(&txbd->status));
533c916d7c9SKumar Gala 			return 0;
534c916d7c9SKumar Gala 		}
535c916d7c9SKumar Gala 	}
536c916d7c9SKumar Gala 	/* setup TxBD */
5379fc29db1SHou Zhiqiang 	muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
5389fc29db1SHou Zhiqiang 	out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
539648bde6dSHou Zhiqiang 	muram_writew(&txbd->len, len);
540c916d7c9SKumar Gala 	sync();
541648bde6dSHou Zhiqiang 	muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
542c916d7c9SKumar Gala 	sync();
543c916d7c9SKumar Gala 
544c916d7c9SKumar Gala 	/* update TxQD, let RISC to send the packet */
545c916d7c9SKumar Gala 	offset_in = muram_readw(&pram->txqd.offset_in);
546c916d7c9SKumar Gala 	offset_in += sizeof(struct fm_port_bd);
547c916d7c9SKumar Gala 	if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
548c916d7c9SKumar Gala 		offset_in = 0;
549c916d7c9SKumar Gala 	muram_writew(&pram->txqd.offset_in, offset_in);
550c916d7c9SKumar Gala 	sync();
551c916d7c9SKumar Gala 
552c916d7c9SKumar Gala 	/* wait for buffer to be transmitted */
553648bde6dSHou Zhiqiang 	for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
554c916d7c9SKumar Gala 		udelay(100);
555c916d7c9SKumar Gala 		if (i > 0x10000) {
556648bde6dSHou Zhiqiang 			printf("%s: Tx error, txbd->status = 0x%x\n",
557648bde6dSHou Zhiqiang 			       dev->name, muram_readw(&txbd->status));
558c916d7c9SKumar Gala 			return 0;
559c916d7c9SKumar Gala 		}
560c916d7c9SKumar Gala 	}
561c916d7c9SKumar Gala 
562c916d7c9SKumar Gala 	/* advance the TxBD */
563c916d7c9SKumar Gala 	txbd++;
564c916d7c9SKumar Gala 	txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
565c916d7c9SKumar Gala 	if (txbd >= (txbd_base + TX_BD_RING_SIZE))
566c916d7c9SKumar Gala 		txbd = txbd_base;
567c916d7c9SKumar Gala 	/* update current txbd */
568c916d7c9SKumar Gala 	fm_eth->cur_txbd = (void *)txbd;
569c916d7c9SKumar Gala 
570c916d7c9SKumar Gala 	return 1;
571c916d7c9SKumar Gala }
572c916d7c9SKumar Gala 
fm_eth_recv(struct eth_device * dev)573c916d7c9SKumar Gala static int fm_eth_recv(struct eth_device *dev)
574c916d7c9SKumar Gala {
575c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
576c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
577c916d7c9SKumar Gala 	struct fm_port_bd *rxbd, *rxbd_base;
578c916d7c9SKumar Gala 	u16 status, len;
5799fc29db1SHou Zhiqiang 	u32 buf_lo, buf_hi;
580c916d7c9SKumar Gala 	u8 *data;
581c916d7c9SKumar Gala 	u16 offset_out;
582466f775eSDaniel Inderbitzin 	int ret = 1;
583c916d7c9SKumar Gala 
584c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
585c916d7c9SKumar Gala 	pram = fm_eth->rx_pram;
586c916d7c9SKumar Gala 	rxbd = fm_eth->cur_rxbd;
587648bde6dSHou Zhiqiang 	status = muram_readw(&rxbd->status);
588c916d7c9SKumar Gala 
589c916d7c9SKumar Gala 	while (!(status & RxBD_EMPTY)) {
590c916d7c9SKumar Gala 		if (!(status & RxBD_ERROR)) {
5919fc29db1SHou Zhiqiang 			buf_hi = muram_readw(&rxbd->buf_ptr_hi);
5929fc29db1SHou Zhiqiang 			buf_lo = in_be32(&rxbd->buf_ptr_lo);
5939fc29db1SHou Zhiqiang 			data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
594648bde6dSHou Zhiqiang 			len = muram_readw(&rxbd->len);
5951fd92db8SJoe Hershberger 			net_process_received_packet(data, len);
596c916d7c9SKumar Gala 		} else {
597c916d7c9SKumar Gala 			printf("%s: Rx error\n", dev->name);
598466f775eSDaniel Inderbitzin 			ret = 0;
599c916d7c9SKumar Gala 		}
600c916d7c9SKumar Gala 
601c916d7c9SKumar Gala 		/* clear the RxBDs */
602648bde6dSHou Zhiqiang 		muram_writew(&rxbd->status, RxBD_EMPTY);
603648bde6dSHou Zhiqiang 		muram_writew(&rxbd->len, 0);
604c916d7c9SKumar Gala 		sync();
605c916d7c9SKumar Gala 
606c916d7c9SKumar Gala 		/* advance RxBD */
607c916d7c9SKumar Gala 		rxbd++;
608c916d7c9SKumar Gala 		rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
609c916d7c9SKumar Gala 		if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
610c916d7c9SKumar Gala 			rxbd = rxbd_base;
611c916d7c9SKumar Gala 		/* read next status */
612648bde6dSHou Zhiqiang 		status = muram_readw(&rxbd->status);
613c916d7c9SKumar Gala 
614c916d7c9SKumar Gala 		/* update RxQD */
615c916d7c9SKumar Gala 		offset_out = muram_readw(&pram->rxqd.offset_out);
616c916d7c9SKumar Gala 		offset_out += sizeof(struct fm_port_bd);
617c916d7c9SKumar Gala 		if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
618c916d7c9SKumar Gala 			offset_out = 0;
619c916d7c9SKumar Gala 		muram_writew(&pram->rxqd.offset_out, offset_out);
620c916d7c9SKumar Gala 		sync();
621c916d7c9SKumar Gala 	}
622c916d7c9SKumar Gala 	fm_eth->cur_rxbd = (void *)rxbd;
623c916d7c9SKumar Gala 
624466f775eSDaniel Inderbitzin 	return ret;
625c916d7c9SKumar Gala }
626c916d7c9SKumar Gala 
fm_eth_init_mac(struct fm_eth * fm_eth,struct ccsr_fman * reg)627c916d7c9SKumar Gala static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
628c916d7c9SKumar Gala {
629c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
630c916d7c9SKumar Gala 	int num;
631c916d7c9SKumar Gala 	void *base, *phyregs = NULL;
632c916d7c9SKumar Gala 
633c916d7c9SKumar Gala 	num = fm_eth->num;
634c916d7c9SKumar Gala 
635111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
636cc19c25eSShengzhou Liu #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
63782a55c1eSShengzhou Liu 	if (fm_eth->type == FM_ETH_10G_E) {
638cc19c25eSShengzhou Liu 		/* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
639cc19c25eSShengzhou Liu 		 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
640cc19c25eSShengzhou Liu 		 * 10GEC1 uses mEMAC1 on T1024.
64182a55c1eSShengzhou Liu 		 * so it needs to change the num.
64282a55c1eSShengzhou Liu 		 */
64382a55c1eSShengzhou Liu 		if (fm_eth->num >= 2)
64482a55c1eSShengzhou Liu 			num -= 2;
64582a55c1eSShengzhou Liu 		else
646944b6ccfSShaohui Xie 			num += 8;
64782a55c1eSShengzhou Liu 	}
648cc19c25eSShengzhou Liu #endif
649111fd19eSRoy Zang 	base = &reg->memac[num].fm_memac;
650111fd19eSRoy Zang 	phyregs = &reg->memac[num].fm_memac_mdio;
651111fd19eSRoy Zang #else
652c916d7c9SKumar Gala 	/* Get the mac registers base address */
653c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E) {
654c916d7c9SKumar Gala 		base = &reg->mac_1g[num].fm_dtesc;
65530381716STimur Tabi 		phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
656c916d7c9SKumar Gala 	} else {
657c916d7c9SKumar Gala 		base = &reg->mac_10g[num].fm_10gec;
658c916d7c9SKumar Gala 		phyregs = &reg->mac_10g[num].fm_10gec_mdio;
659c916d7c9SKumar Gala 	}
660111fd19eSRoy Zang #endif
661c916d7c9SKumar Gala 
662c916d7c9SKumar Gala 	/* alloc mac controller */
663c916d7c9SKumar Gala 	mac = malloc(sizeof(struct fsl_enet_mac));
664c916d7c9SKumar Gala 	if (!mac)
6650f2cb9f5SHou Zhiqiang 		return -ENOMEM;
666c916d7c9SKumar Gala 	memset(mac, 0, sizeof(struct fsl_enet_mac));
667c916d7c9SKumar Gala 
668c916d7c9SKumar Gala 	/* save the mac to fm_eth struct */
669c916d7c9SKumar Gala 	fm_eth->mac = mac;
670c916d7c9SKumar Gala 
671111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
672111fd19eSRoy Zang 	init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
673111fd19eSRoy Zang #else
674c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E)
67530381716STimur Tabi 		init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
676c916d7c9SKumar Gala 	else
677c916d7c9SKumar Gala 		init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
678111fd19eSRoy Zang #endif
679c916d7c9SKumar Gala 
6800f2cb9f5SHou Zhiqiang 	return 0;
681c916d7c9SKumar Gala }
682c916d7c9SKumar Gala 
init_phy(struct eth_device * dev)683c916d7c9SKumar Gala static int init_phy(struct eth_device *dev)
684c916d7c9SKumar Gala {
685c916d7c9SKumar Gala 	struct fm_eth *fm_eth = dev->priv;
68629d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
687c916d7c9SKumar Gala 	struct phy_device *phydev = NULL;
688c916d7c9SKumar Gala 	u32 supported;
68929d8c814SShaohui Xie #endif
690c916d7c9SKumar Gala 
691c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E)
692c916d7c9SKumar Gala 		dtsec_init_phy(dev);
693c916d7c9SKumar Gala 
69429d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
695c916d7c9SKumar Gala 	if (fm_eth->bus) {
696c916d7c9SKumar Gala 		phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, dev,
697c916d7c9SKumar Gala 					fm_eth->enet_if);
698c916d7c9SKumar Gala 		if (!phydev) {
699c916d7c9SKumar Gala 			printf("Failed to connect\n");
700c916d7c9SKumar Gala 			return -1;
701c916d7c9SKumar Gala 		}
7026798c324SCodrin Ciubotariu 	} else {
7036798c324SCodrin Ciubotariu 		return 0;
7046798c324SCodrin Ciubotariu 	}
705c916d7c9SKumar Gala 
706c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E) {
707c916d7c9SKumar Gala 		supported = (SUPPORTED_10baseT_Half |
708c916d7c9SKumar Gala 				SUPPORTED_10baseT_Full |
709c916d7c9SKumar Gala 				SUPPORTED_100baseT_Half |
710c916d7c9SKumar Gala 				SUPPORTED_100baseT_Full |
711c916d7c9SKumar Gala 				SUPPORTED_1000baseT_Full);
712c916d7c9SKumar Gala 	} else {
713c916d7c9SKumar Gala 		supported = SUPPORTED_10000baseT_Full;
714c916d7c9SKumar Gala 
715c916d7c9SKumar Gala 		if (tgec_is_fibre(dev))
716c916d7c9SKumar Gala 			phydev->port = PORT_FIBRE;
717c916d7c9SKumar Gala 	}
718c916d7c9SKumar Gala 
719c916d7c9SKumar Gala 	phydev->supported &= supported;
720c916d7c9SKumar Gala 	phydev->advertising = phydev->supported;
721c916d7c9SKumar Gala 
722c916d7c9SKumar Gala 	fm_eth->phydev = phydev;
723c916d7c9SKumar Gala 
724c916d7c9SKumar Gala 	phy_config(phydev);
725c916d7c9SKumar Gala #endif
726c916d7c9SKumar Gala 
727c916d7c9SKumar Gala 	return 0;
728c916d7c9SKumar Gala }
729c916d7c9SKumar Gala 
fm_eth_initialize(struct ccsr_fman * reg,struct fm_eth_info * info)730c916d7c9SKumar Gala int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
731c916d7c9SKumar Gala {
732c916d7c9SKumar Gala 	struct eth_device *dev;
733c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
734c916d7c9SKumar Gala 	int i, num = info->num;
7350f2cb9f5SHou Zhiqiang 	int ret;
736c916d7c9SKumar Gala 
737c916d7c9SKumar Gala 	/* alloc eth device */
738c916d7c9SKumar Gala 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
739c916d7c9SKumar Gala 	if (!dev)
7400f2cb9f5SHou Zhiqiang 		return -ENOMEM;
741c916d7c9SKumar Gala 	memset(dev, 0, sizeof(struct eth_device));
742c916d7c9SKumar Gala 
743c916d7c9SKumar Gala 	/* alloc the FMan ethernet private struct */
744c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
745c916d7c9SKumar Gala 	if (!fm_eth)
7460f2cb9f5SHou Zhiqiang 		return -ENOMEM;
747c916d7c9SKumar Gala 	memset(fm_eth, 0, sizeof(struct fm_eth));
748c916d7c9SKumar Gala 
749c916d7c9SKumar Gala 	/* save off some things we need from the info struct */
750c916d7c9SKumar Gala 	fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
751c916d7c9SKumar Gala 	fm_eth->num = num;
752c916d7c9SKumar Gala 	fm_eth->type = info->type;
753c916d7c9SKumar Gala 
754c916d7c9SKumar Gala 	fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
755c916d7c9SKumar Gala 	fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
756c916d7c9SKumar Gala 
757c916d7c9SKumar Gala 	/* set the ethernet max receive length */
758c916d7c9SKumar Gala 	fm_eth->max_rx_len = MAX_RXBUF_LEN;
759c916d7c9SKumar Gala 
760c916d7c9SKumar Gala 	/* init global mac structure */
7610f2cb9f5SHou Zhiqiang 	ret = fm_eth_init_mac(fm_eth, reg);
7620f2cb9f5SHou Zhiqiang 	if (ret)
7630f2cb9f5SHou Zhiqiang 		return ret;
764c916d7c9SKumar Gala 
765c916d7c9SKumar Gala 	/* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
766c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E)
767c916d7c9SKumar Gala 		sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
768c916d7c9SKumar Gala 	else
769c916d7c9SKumar Gala 		sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
770c916d7c9SKumar Gala 
771c916d7c9SKumar Gala 	devlist[num_controllers++] = dev;
772c916d7c9SKumar Gala 	dev->iobase = 0;
773c916d7c9SKumar Gala 	dev->priv = (void *)fm_eth;
774c916d7c9SKumar Gala 	dev->init = fm_eth_open;
775c916d7c9SKumar Gala 	dev->halt = fm_eth_halt;
776c916d7c9SKumar Gala 	dev->send = fm_eth_send;
777c916d7c9SKumar Gala 	dev->recv = fm_eth_recv;
778c916d7c9SKumar Gala 	fm_eth->dev = dev;
779c916d7c9SKumar Gala 	fm_eth->bus = info->bus;
780c916d7c9SKumar Gala 	fm_eth->phyaddr = info->phy_addr;
781c916d7c9SKumar Gala 	fm_eth->enet_if = info->enet_if;
782c916d7c9SKumar Gala 
783c916d7c9SKumar Gala 	/* startup the FM im */
7840f2cb9f5SHou Zhiqiang 	ret = fm_eth_startup(fm_eth);
7850f2cb9f5SHou Zhiqiang 	if (ret)
7860f2cb9f5SHou Zhiqiang 		return ret;
787c916d7c9SKumar Gala 
7886798c324SCodrin Ciubotariu 	init_phy(dev);
789c916d7c9SKumar Gala 
790c916d7c9SKumar Gala 	/* clear the ethernet address */
791c916d7c9SKumar Gala 	for (i = 0; i < 6; i++)
792c916d7c9SKumar Gala 		dev->enetaddr[i] = 0;
793c916d7c9SKumar Gala 	eth_register(dev);
794c916d7c9SKumar Gala 
7950f2cb9f5SHou Zhiqiang 	return 0;
796c916d7c9SKumar Gala }
797