xref: /rk3399_rockchip-uboot/drivers/net/fm/eth.c (revision 0f2cb9f5a0743f19a4c1d0f7e6506537dd6ef78d)
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>
16c916d7c9SKumar Gala #include <asm/fsl_dtsec.h>
17c916d7c9SKumar Gala #include <asm/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 */
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;
44111fd19eSRoy Zang 
45c35f8693SShengzhou Liu 	/* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
46c35f8693SShengzhou Liu 	value = PHY_SGMII_IF_MODE_SGMII;
47c35f8693SShengzhou Liu 	if (!sgmii_2500)
48c35f8693SShengzhou Liu 		value |= PHY_SGMII_IF_MODE_AN;
49c35f8693SShengzhou Liu 
50111fd19eSRoy Zang 	memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x14, value);
51111fd19eSRoy Zang 
52111fd19eSRoy Zang 	/* Dev ability according to SGMII specification */
53111fd19eSRoy Zang 	value = PHY_SGMII_DEV_ABILITY_SGMII;
54111fd19eSRoy Zang 	memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x4, value);
55111fd19eSRoy Zang 
56111fd19eSRoy Zang 	/* Adjust link timer for SGMII  -
57111fd19eSRoy Zang 	1.6 ms in units of 8 ns = 2 * 10^5 = 0x30d40 */
58111fd19eSRoy Zang 	memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x13, 0x3);
59111fd19eSRoy Zang 	memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x12, 0xd40);
60111fd19eSRoy Zang 
61111fd19eSRoy Zang 	/* Restart AN */
62c35f8693SShengzhou Liu 	value = PHY_SGMII_CR_DEF_VAL;
63c35f8693SShengzhou Liu 	if (!sgmii_2500)
64c35f8693SShengzhou Liu 		value |= PHY_SGMII_CR_RESET_AN;
65111fd19eSRoy Zang 	memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0, value);
66111fd19eSRoy Zang #else
67c916d7c9SKumar Gala 	struct dtsec *regs = priv->mac->base;
68c916d7c9SKumar Gala 	struct tsec_mii_mng *phyregs = priv->mac->phyregs;
69c916d7c9SKumar Gala 
70c916d7c9SKumar Gala 	/*
71c916d7c9SKumar Gala 	 * Access TBI PHY registers at given TSEC register offset as
72c916d7c9SKumar Gala 	 * opposed to the register offset used for external PHY accesses
73c916d7c9SKumar Gala 	 */
74c916d7c9SKumar Gala 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
75c916d7c9SKumar Gala 			TBICON_CLK_SELECT);
76c916d7c9SKumar Gala 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
77c916d7c9SKumar Gala 			TBIANA_SGMII_ACK);
78c916d7c9SKumar Gala 	tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
79c916d7c9SKumar Gala 			TBI_CR, TBICR_SETTINGS);
80111fd19eSRoy Zang #endif
81c916d7c9SKumar Gala }
82c916d7c9SKumar Gala 
83c916d7c9SKumar Gala static void dtsec_init_phy(struct eth_device *dev)
84c916d7c9SKumar Gala {
85c916d7c9SKumar Gala 	struct fm_eth *fm_eth = dev->priv;
86111fd19eSRoy Zang #ifndef CONFIG_SYS_FMAN_V3
871f3bd3e2Sshaohui xie 	struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
881f3bd3e2Sshaohui xie 
89c916d7c9SKumar Gala 	/* Assign a Physical address to the TBI */
90c916d7c9SKumar Gala 	out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
91111fd19eSRoy Zang #endif
92c916d7c9SKumar Gala 
93c35f8693SShengzhou Liu 	if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
94c35f8693SShengzhou Liu 	    fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
95c916d7c9SKumar Gala 		dtsec_configure_serdes(fm_eth);
96c916d7c9SKumar Gala }
97c916d7c9SKumar Gala 
98c916d7c9SKumar Gala static int tgec_is_fibre(struct eth_device *dev)
99c916d7c9SKumar Gala {
100c916d7c9SKumar Gala 	struct fm_eth *fm = dev->priv;
101c916d7c9SKumar Gala 	char phyopt[20];
102c916d7c9SKumar Gala 
103c916d7c9SKumar Gala 	sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
104c916d7c9SKumar Gala 
105c916d7c9SKumar Gala 	return hwconfig_arg_cmp(phyopt, "xfi");
106c916d7c9SKumar Gala }
107c916d7c9SKumar Gala #endif
108c916d7c9SKumar Gala 
109c916d7c9SKumar Gala static u16 muram_readw(u16 *addr)
110c916d7c9SKumar Gala {
1119fc29db1SHou Zhiqiang 	ulong base = (ulong)addr & ~0x3UL;
1129fc29db1SHou Zhiqiang 	u32 val32 = in_be32((void *)base);
113c916d7c9SKumar Gala 	int byte_pos;
114c916d7c9SKumar Gala 	u16 ret;
115c916d7c9SKumar Gala 
1169fc29db1SHou Zhiqiang 	byte_pos = (ulong)addr & 0x3UL;
117c916d7c9SKumar Gala 	if (byte_pos)
118c916d7c9SKumar Gala 		ret = (u16)(val32 & 0x0000ffff);
119c916d7c9SKumar Gala 	else
120c916d7c9SKumar Gala 		ret = (u16)((val32 & 0xffff0000) >> 16);
121c916d7c9SKumar Gala 
122c916d7c9SKumar Gala 	return ret;
123c916d7c9SKumar Gala }
124c916d7c9SKumar Gala 
125c916d7c9SKumar Gala static void muram_writew(u16 *addr, u16 val)
126c916d7c9SKumar Gala {
1279fc29db1SHou Zhiqiang 	ulong base = (ulong)addr & ~0x3UL;
1289fc29db1SHou Zhiqiang 	u32 org32 = in_be32((void *)base);
129c916d7c9SKumar Gala 	u32 val32;
130c916d7c9SKumar Gala 	int byte_pos;
131c916d7c9SKumar Gala 
1329fc29db1SHou Zhiqiang 	byte_pos = (ulong)addr & 0x3UL;
133c916d7c9SKumar Gala 	if (byte_pos)
134c916d7c9SKumar Gala 		val32 = (org32 & 0xffff0000) | val;
135c916d7c9SKumar Gala 	else
136c916d7c9SKumar Gala 		val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
137c916d7c9SKumar Gala 
1389fc29db1SHou Zhiqiang 	out_be32((void *)base, val32);
139c916d7c9SKumar Gala }
140c916d7c9SKumar Gala 
141c916d7c9SKumar Gala static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
142c916d7c9SKumar Gala {
143c916d7c9SKumar Gala 	int timeout = 1000000;
144c916d7c9SKumar Gala 
145c916d7c9SKumar Gala 	clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
146c916d7c9SKumar Gala 
147c916d7c9SKumar Gala 	/* wait until the rx port is not busy */
148c916d7c9SKumar Gala 	while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
149c916d7c9SKumar Gala 		;
150c916d7c9SKumar Gala }
151c916d7c9SKumar Gala 
152c916d7c9SKumar Gala static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
153c916d7c9SKumar Gala {
154c916d7c9SKumar Gala 	/* set BMI to independent mode, Rx port disable */
155c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
156c916d7c9SKumar Gala 	/* clear FOF in IM case */
157c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rim, 0);
158c916d7c9SKumar Gala 	/* Rx frame next engine -RISC */
159c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
160c916d7c9SKumar Gala 	/* Rx command attribute - no order, MR[3] = 1 */
161c916d7c9SKumar Gala 	clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
162c916d7c9SKumar Gala 	setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
163c916d7c9SKumar Gala 	/* enable Rx statistic counters */
164c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
165c916d7c9SKumar Gala 	/* disable Rx performance counters */
166c916d7c9SKumar Gala 	out_be32(&rx_port->fmbm_rpc, 0);
167c916d7c9SKumar Gala }
168c916d7c9SKumar Gala 
169c916d7c9SKumar Gala static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
170c916d7c9SKumar Gala {
171c916d7c9SKumar Gala 	int timeout = 1000000;
172c916d7c9SKumar Gala 
173c916d7c9SKumar Gala 	clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
174c916d7c9SKumar Gala 
175c916d7c9SKumar Gala 	/* wait until the tx port is not busy */
176c916d7c9SKumar Gala 	while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
177c916d7c9SKumar Gala 		;
178c916d7c9SKumar Gala }
179c916d7c9SKumar Gala 
180c916d7c9SKumar Gala static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
181c916d7c9SKumar Gala {
182c916d7c9SKumar Gala 	/* set BMI to independent mode, Tx port disable */
183c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
184c916d7c9SKumar Gala 	/* Tx frame next engine -RISC */
185c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
186c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
187c916d7c9SKumar Gala 	/* Tx command attribute - no order, MR[3] = 1 */
188c916d7c9SKumar Gala 	clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
189c916d7c9SKumar Gala 	setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
190c916d7c9SKumar Gala 	/* enable Tx statistic counters */
191c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
192c916d7c9SKumar Gala 	/* disable Tx performance counters */
193c916d7c9SKumar Gala 	out_be32(&tx_port->fmbm_tpc, 0);
194c916d7c9SKumar Gala }
195c916d7c9SKumar Gala 
196c916d7c9SKumar Gala static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
197c916d7c9SKumar Gala {
198c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
199c916d7c9SKumar Gala 	u32 pram_page_offset;
200c916d7c9SKumar Gala 	void *rx_bd_ring_base;
201c916d7c9SKumar Gala 	void *rx_buf_pool;
2029fc29db1SHou Zhiqiang 	u32 bd_ring_base_lo, bd_ring_base_hi;
2039fc29db1SHou Zhiqiang 	u32 buf_lo, buf_hi;
204c916d7c9SKumar Gala 	struct fm_port_bd *rxbd;
205c916d7c9SKumar Gala 	struct fm_port_qd *rxqd;
206c916d7c9SKumar Gala 	struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
207c916d7c9SKumar Gala 	int i;
208c916d7c9SKumar Gala 
209c916d7c9SKumar Gala 	/* alloc global parameter ram at MURAM */
210c916d7c9SKumar Gala 	pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
211c916d7c9SKumar Gala 		FM_PRAM_SIZE, FM_PRAM_ALIGN);
2129fc29db1SHou Zhiqiang 	if (!pram) {
2139fc29db1SHou Zhiqiang 		printf("%s: No muram for Rx global parameter\n", __func__);
214*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
2159fc29db1SHou Zhiqiang 	}
2169fc29db1SHou Zhiqiang 
217c916d7c9SKumar Gala 	fm_eth->rx_pram = pram;
218c916d7c9SKumar Gala 
219c916d7c9SKumar Gala 	/* parameter page offset to MURAM */
2209fc29db1SHou Zhiqiang 	pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
221c916d7c9SKumar Gala 
222c916d7c9SKumar Gala 	/* enable global mode- snooping data buffers and BDs */
223648bde6dSHou Zhiqiang 	out_be32(&pram->mode, PRAM_MODE_GLOBAL);
224c916d7c9SKumar Gala 
225c916d7c9SKumar Gala 	/* init the Rx queue descriptor pionter */
226648bde6dSHou Zhiqiang 	out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
227c916d7c9SKumar Gala 
228c916d7c9SKumar Gala 	/* set the max receive buffer length, power of 2 */
229c916d7c9SKumar Gala 	muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
230c916d7c9SKumar Gala 
231c916d7c9SKumar Gala 	/* alloc Rx buffer descriptors from main memory */
232c916d7c9SKumar Gala 	rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
233c916d7c9SKumar Gala 			* RX_BD_RING_SIZE);
234c916d7c9SKumar Gala 	if (!rx_bd_ring_base)
235*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
236*0f2cb9f5SHou Zhiqiang 
237c916d7c9SKumar Gala 	memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
238c916d7c9SKumar Gala 			* RX_BD_RING_SIZE);
239c916d7c9SKumar Gala 
240c916d7c9SKumar Gala 	/* alloc Rx buffer from main memory */
241c916d7c9SKumar Gala 	rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
242c916d7c9SKumar Gala 	if (!rx_buf_pool)
243*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
244*0f2cb9f5SHou Zhiqiang 
245c916d7c9SKumar Gala 	memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
2469fc29db1SHou Zhiqiang 	debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
247c916d7c9SKumar Gala 
248c916d7c9SKumar Gala 	/* save them to fm_eth */
249c916d7c9SKumar Gala 	fm_eth->rx_bd_ring = rx_bd_ring_base;
250c916d7c9SKumar Gala 	fm_eth->cur_rxbd = rx_bd_ring_base;
251c916d7c9SKumar Gala 	fm_eth->rx_buf = rx_buf_pool;
252c916d7c9SKumar Gala 
253c916d7c9SKumar Gala 	/* init Rx BDs ring */
254c916d7c9SKumar Gala 	rxbd = (struct fm_port_bd *)rx_bd_ring_base;
255c916d7c9SKumar Gala 	for (i = 0; i < RX_BD_RING_SIZE; i++) {
256648bde6dSHou Zhiqiang 		muram_writew(&rxbd->status, RxBD_EMPTY);
257648bde6dSHou Zhiqiang 		muram_writew(&rxbd->len, 0);
2589fc29db1SHou Zhiqiang 		buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
2599fc29db1SHou Zhiqiang 					i * MAX_RXBUF_LEN));
2609fc29db1SHou Zhiqiang 		buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
2619fc29db1SHou Zhiqiang 					i * MAX_RXBUF_LEN));
2629fc29db1SHou Zhiqiang 		muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
2639fc29db1SHou Zhiqiang 		out_be32(&rxbd->buf_ptr_lo, buf_lo);
264c916d7c9SKumar Gala 		rxbd++;
265c916d7c9SKumar Gala 	}
266c916d7c9SKumar Gala 
267c916d7c9SKumar Gala 	/* set the Rx queue descriptor */
268c916d7c9SKumar Gala 	rxqd = &pram->rxqd;
269c916d7c9SKumar Gala 	muram_writew(&rxqd->gen, 0);
2709fc29db1SHou Zhiqiang 	bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
2719fc29db1SHou Zhiqiang 	bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
2729fc29db1SHou Zhiqiang 	muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
2739fc29db1SHou Zhiqiang 	out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
274c916d7c9SKumar Gala 	muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
275c916d7c9SKumar Gala 			* RX_BD_RING_SIZE);
276c916d7c9SKumar Gala 	muram_writew(&rxqd->offset_in, 0);
277c916d7c9SKumar Gala 	muram_writew(&rxqd->offset_out, 0);
278c916d7c9SKumar Gala 
279c916d7c9SKumar Gala 	/* set IM parameter ram pointer to Rx Frame Queue ID */
280c916d7c9SKumar Gala 	out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
281c916d7c9SKumar Gala 
282*0f2cb9f5SHou Zhiqiang 	return 0;
283c916d7c9SKumar Gala }
284c916d7c9SKumar Gala 
285c916d7c9SKumar Gala static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
286c916d7c9SKumar Gala {
287c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
288c916d7c9SKumar Gala 	u32 pram_page_offset;
289c916d7c9SKumar Gala 	void *tx_bd_ring_base;
2909fc29db1SHou Zhiqiang 	u32 bd_ring_base_lo, bd_ring_base_hi;
291c916d7c9SKumar Gala 	struct fm_port_bd *txbd;
292c916d7c9SKumar Gala 	struct fm_port_qd *txqd;
293c916d7c9SKumar Gala 	struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
294c916d7c9SKumar Gala 	int i;
295c916d7c9SKumar Gala 
296c916d7c9SKumar Gala 	/* alloc global parameter ram at MURAM */
297c916d7c9SKumar Gala 	pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
298c916d7c9SKumar Gala 		FM_PRAM_SIZE, FM_PRAM_ALIGN);
2999fc29db1SHou Zhiqiang 	if (!pram) {
3009fc29db1SHou Zhiqiang 		printf("%s: No muram for Tx global parameter\n", __func__);
301*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
3029fc29db1SHou Zhiqiang 	}
303c916d7c9SKumar Gala 	fm_eth->tx_pram = pram;
304c916d7c9SKumar Gala 
305c916d7c9SKumar Gala 	/* parameter page offset to MURAM */
3069fc29db1SHou Zhiqiang 	pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
307c916d7c9SKumar Gala 
308c916d7c9SKumar Gala 	/* enable global mode- snooping data buffers and BDs */
309648bde6dSHou Zhiqiang 	out_be32(&pram->mode, PRAM_MODE_GLOBAL);
310c916d7c9SKumar Gala 
311c916d7c9SKumar Gala 	/* init the Tx queue descriptor pionter */
312648bde6dSHou Zhiqiang 	out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
313c916d7c9SKumar Gala 
314c916d7c9SKumar Gala 	/* alloc Tx buffer descriptors from main memory */
315c916d7c9SKumar Gala 	tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
316c916d7c9SKumar Gala 			* TX_BD_RING_SIZE);
317c916d7c9SKumar Gala 	if (!tx_bd_ring_base)
318*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
319*0f2cb9f5SHou Zhiqiang 
320c916d7c9SKumar Gala 	memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
321c916d7c9SKumar Gala 			* TX_BD_RING_SIZE);
322c916d7c9SKumar Gala 	/* save it to fm_eth */
323c916d7c9SKumar Gala 	fm_eth->tx_bd_ring = tx_bd_ring_base;
324c916d7c9SKumar Gala 	fm_eth->cur_txbd = tx_bd_ring_base;
325c916d7c9SKumar Gala 
326c916d7c9SKumar Gala 	/* init Tx BDs ring */
327c916d7c9SKumar Gala 	txbd = (struct fm_port_bd *)tx_bd_ring_base;
328c916d7c9SKumar Gala 	for (i = 0; i < TX_BD_RING_SIZE; i++) {
329648bde6dSHou Zhiqiang 		muram_writew(&txbd->status, TxBD_LAST);
330648bde6dSHou Zhiqiang 		muram_writew(&txbd->len, 0);
331648bde6dSHou Zhiqiang 		muram_writew(&txbd->buf_ptr_hi, 0);
332648bde6dSHou Zhiqiang 		out_be32(&txbd->buf_ptr_lo, 0);
333648bde6dSHou Zhiqiang 		txbd++;
334c916d7c9SKumar Gala 	}
335c916d7c9SKumar Gala 
336c916d7c9SKumar Gala 	/* set the Tx queue decriptor */
337c916d7c9SKumar Gala 	txqd = &pram->txqd;
3389fc29db1SHou Zhiqiang 	bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
3399fc29db1SHou Zhiqiang 	bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
3409fc29db1SHou Zhiqiang 	muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
3419fc29db1SHou Zhiqiang 	out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
342c916d7c9SKumar Gala 	muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
343c916d7c9SKumar Gala 			* TX_BD_RING_SIZE);
344c916d7c9SKumar Gala 	muram_writew(&txqd->offset_in, 0);
345c916d7c9SKumar Gala 	muram_writew(&txqd->offset_out, 0);
346c916d7c9SKumar Gala 
347c916d7c9SKumar Gala 	/* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
348c916d7c9SKumar Gala 	out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
349c916d7c9SKumar Gala 
350*0f2cb9f5SHou Zhiqiang 	return 0;
351c916d7c9SKumar Gala }
352c916d7c9SKumar Gala 
353c916d7c9SKumar Gala static int fm_eth_init(struct fm_eth *fm_eth)
354c916d7c9SKumar Gala {
355*0f2cb9f5SHou Zhiqiang 	int ret;
356c916d7c9SKumar Gala 
357*0f2cb9f5SHou Zhiqiang 	ret = fm_eth_rx_port_parameter_init(fm_eth);
358*0f2cb9f5SHou Zhiqiang 	if (ret)
359*0f2cb9f5SHou Zhiqiang 		return ret;
360*0f2cb9f5SHou Zhiqiang 
361*0f2cb9f5SHou Zhiqiang 	ret = fm_eth_tx_port_parameter_init(fm_eth);
362*0f2cb9f5SHou Zhiqiang 	if (ret)
363*0f2cb9f5SHou Zhiqiang 		return ret;
364*0f2cb9f5SHou Zhiqiang 
365c916d7c9SKumar Gala 	return 0;
366c916d7c9SKumar Gala }
367c916d7c9SKumar Gala 
368c916d7c9SKumar Gala static int fm_eth_startup(struct fm_eth *fm_eth)
369c916d7c9SKumar Gala {
370c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
371*0f2cb9f5SHou Zhiqiang 	int ret;
372*0f2cb9f5SHou Zhiqiang 
373c916d7c9SKumar Gala 	mac = fm_eth->mac;
374c916d7c9SKumar Gala 
375c916d7c9SKumar Gala 	/* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
376*0f2cb9f5SHou Zhiqiang 	ret = fm_eth_init(fm_eth);
377*0f2cb9f5SHou Zhiqiang 	if (ret)
378*0f2cb9f5SHou Zhiqiang 		return ret;
379c916d7c9SKumar Gala 	/* setup the MAC controller */
380c916d7c9SKumar Gala 	mac->init_mac(mac);
381c916d7c9SKumar Gala 
382c916d7c9SKumar Gala 	/* For some reason we need to set SPEED_100 */
3831c68d01eSShaohui Xie 	if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
3841c68d01eSShaohui Xie 	     (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
3851c68d01eSShaohui Xie 	      mac->set_if_mode)
386c916d7c9SKumar Gala 		mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
387c916d7c9SKumar Gala 
388c916d7c9SKumar Gala 	/* init bmi rx port, IM mode and disable */
389c916d7c9SKumar Gala 	bmi_rx_port_init(fm_eth->rx_port);
390c916d7c9SKumar Gala 	/* init bmi tx port, IM mode and disable */
391c916d7c9SKumar Gala 	bmi_tx_port_init(fm_eth->tx_port);
392c916d7c9SKumar Gala 
393*0f2cb9f5SHou Zhiqiang 	return 0;
394c916d7c9SKumar Gala }
395c916d7c9SKumar Gala 
396c916d7c9SKumar Gala static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
397c916d7c9SKumar Gala {
398c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
399c916d7c9SKumar Gala 
400c916d7c9SKumar Gala 	pram = fm_eth->tx_pram;
401c916d7c9SKumar Gala 	/* graceful stop transmission of frames */
402648bde6dSHou Zhiqiang 	setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
403c916d7c9SKumar Gala 	sync();
404c916d7c9SKumar Gala }
405c916d7c9SKumar Gala 
406c916d7c9SKumar Gala static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
407c916d7c9SKumar Gala {
408c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
409c916d7c9SKumar Gala 
410c916d7c9SKumar Gala 	pram = fm_eth->tx_pram;
411c916d7c9SKumar Gala 	/* re-enable transmission of frames */
412648bde6dSHou Zhiqiang 	clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
413c916d7c9SKumar Gala 	sync();
414c916d7c9SKumar Gala }
415c916d7c9SKumar Gala 
416c916d7c9SKumar Gala static int fm_eth_open(struct eth_device *dev, bd_t *bd)
417c916d7c9SKumar Gala {
418c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
419c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
42011af8d65STimur Tabi #ifdef CONFIG_PHYLIB
42111af8d65STimur Tabi 	int ret;
42211af8d65STimur Tabi #endif
423c916d7c9SKumar Gala 
424c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
425c916d7c9SKumar Gala 	mac = fm_eth->mac;
426c916d7c9SKumar Gala 
427c916d7c9SKumar Gala 	/* setup the MAC address */
428c916d7c9SKumar Gala 	if (dev->enetaddr[0] & 0x01) {
429c916d7c9SKumar Gala 		printf("%s: MacAddress is multcast address\n",	__func__);
430c916d7c9SKumar Gala 		return 1;
431c916d7c9SKumar Gala 	}
432c916d7c9SKumar Gala 	mac->set_mac_addr(mac, dev->enetaddr);
433c916d7c9SKumar Gala 
434c916d7c9SKumar Gala 	/* enable bmi Rx port */
435c916d7c9SKumar Gala 	setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
436c916d7c9SKumar Gala 	/* enable MAC rx/tx port */
437c916d7c9SKumar Gala 	mac->enable_mac(mac);
438c916d7c9SKumar Gala 	/* enable bmi Tx port */
439c916d7c9SKumar Gala 	setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
440c916d7c9SKumar Gala 	/* re-enable transmission of frame */
441c916d7c9SKumar Gala 	fmc_tx_port_graceful_stop_disable(fm_eth);
442c916d7c9SKumar Gala 
443c916d7c9SKumar Gala #ifdef CONFIG_PHYLIB
4446798c324SCodrin Ciubotariu 	if (fm_eth->phydev) {
44511af8d65STimur Tabi 		ret = phy_startup(fm_eth->phydev);
44611af8d65STimur Tabi 		if (ret) {
4476798c324SCodrin Ciubotariu 			printf("%s: Could not initialize\n",
4486798c324SCodrin Ciubotariu 			       fm_eth->phydev->dev->name);
44911af8d65STimur Tabi 			return ret;
45011af8d65STimur Tabi 		}
4516798c324SCodrin Ciubotariu 	} else {
4526798c324SCodrin Ciubotariu 		return 0;
4536798c324SCodrin Ciubotariu 	}
454c916d7c9SKumar Gala #else
455c916d7c9SKumar Gala 	fm_eth->phydev->speed = SPEED_1000;
456c916d7c9SKumar Gala 	fm_eth->phydev->link = 1;
457c916d7c9SKumar Gala 	fm_eth->phydev->duplex = DUPLEX_FULL;
458c916d7c9SKumar Gala #endif
459c916d7c9SKumar Gala 
460c916d7c9SKumar Gala 	/* set the MAC-PHY mode */
461c916d7c9SKumar Gala 	mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
462c916d7c9SKumar Gala 
463c916d7c9SKumar Gala 	if (!fm_eth->phydev->link)
464c916d7c9SKumar Gala 		printf("%s: No link.\n", fm_eth->phydev->dev->name);
465c916d7c9SKumar Gala 
466c916d7c9SKumar Gala 	return fm_eth->phydev->link ? 0 : -1;
467c916d7c9SKumar Gala }
468c916d7c9SKumar Gala 
469c916d7c9SKumar Gala static void fm_eth_halt(struct eth_device *dev)
470c916d7c9SKumar Gala {
471c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
472c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
473c916d7c9SKumar Gala 
474c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
475c916d7c9SKumar Gala 	mac = fm_eth->mac;
476c916d7c9SKumar Gala 
477c916d7c9SKumar Gala 	/* graceful stop the transmission of frames */
478c916d7c9SKumar Gala 	fmc_tx_port_graceful_stop_enable(fm_eth);
479c916d7c9SKumar Gala 	/* disable bmi Tx port */
480c916d7c9SKumar Gala 	bmi_tx_port_disable(fm_eth->tx_port);
481c916d7c9SKumar Gala 	/* disable MAC rx/tx port */
482c916d7c9SKumar Gala 	mac->disable_mac(mac);
483c916d7c9SKumar Gala 	/* disable bmi Rx port */
484c916d7c9SKumar Gala 	bmi_rx_port_disable(fm_eth->rx_port);
485c916d7c9SKumar Gala 
4866798c324SCodrin Ciubotariu 	if (fm_eth->phydev)
487c916d7c9SKumar Gala 		phy_shutdown(fm_eth->phydev);
488c916d7c9SKumar Gala }
489c916d7c9SKumar Gala 
490e9df2018SJoe Hershberger static int fm_eth_send(struct eth_device *dev, void *buf, int len)
491c916d7c9SKumar Gala {
492c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
493c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
494c916d7c9SKumar Gala 	struct fm_port_bd *txbd, *txbd_base;
495c916d7c9SKumar Gala 	u16 offset_in;
496c916d7c9SKumar Gala 	int i;
497c916d7c9SKumar Gala 
498c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
499c916d7c9SKumar Gala 	pram = fm_eth->tx_pram;
500c916d7c9SKumar Gala 	txbd = fm_eth->cur_txbd;
501c916d7c9SKumar Gala 
502c916d7c9SKumar Gala 	/* find one empty TxBD */
503648bde6dSHou Zhiqiang 	for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
504c916d7c9SKumar Gala 		udelay(100);
505c916d7c9SKumar Gala 		if (i > 0x1000) {
506648bde6dSHou Zhiqiang 			printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
507648bde6dSHou Zhiqiang 			       dev->name, muram_readw(&txbd->status));
508c916d7c9SKumar Gala 			return 0;
509c916d7c9SKumar Gala 		}
510c916d7c9SKumar Gala 	}
511c916d7c9SKumar Gala 	/* setup TxBD */
5129fc29db1SHou Zhiqiang 	muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
5139fc29db1SHou Zhiqiang 	out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
514648bde6dSHou Zhiqiang 	muram_writew(&txbd->len, len);
515c916d7c9SKumar Gala 	sync();
516648bde6dSHou Zhiqiang 	muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
517c916d7c9SKumar Gala 	sync();
518c916d7c9SKumar Gala 
519c916d7c9SKumar Gala 	/* update TxQD, let RISC to send the packet */
520c916d7c9SKumar Gala 	offset_in = muram_readw(&pram->txqd.offset_in);
521c916d7c9SKumar Gala 	offset_in += sizeof(struct fm_port_bd);
522c916d7c9SKumar Gala 	if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
523c916d7c9SKumar Gala 		offset_in = 0;
524c916d7c9SKumar Gala 	muram_writew(&pram->txqd.offset_in, offset_in);
525c916d7c9SKumar Gala 	sync();
526c916d7c9SKumar Gala 
527c916d7c9SKumar Gala 	/* wait for buffer to be transmitted */
528648bde6dSHou Zhiqiang 	for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
529c916d7c9SKumar Gala 		udelay(100);
530c916d7c9SKumar Gala 		if (i > 0x10000) {
531648bde6dSHou Zhiqiang 			printf("%s: Tx error, txbd->status = 0x%x\n",
532648bde6dSHou Zhiqiang 			       dev->name, muram_readw(&txbd->status));
533c916d7c9SKumar Gala 			return 0;
534c916d7c9SKumar Gala 		}
535c916d7c9SKumar Gala 	}
536c916d7c9SKumar Gala 
537c916d7c9SKumar Gala 	/* advance the TxBD */
538c916d7c9SKumar Gala 	txbd++;
539c916d7c9SKumar Gala 	txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
540c916d7c9SKumar Gala 	if (txbd >= (txbd_base + TX_BD_RING_SIZE))
541c916d7c9SKumar Gala 		txbd = txbd_base;
542c916d7c9SKumar Gala 	/* update current txbd */
543c916d7c9SKumar Gala 	fm_eth->cur_txbd = (void *)txbd;
544c916d7c9SKumar Gala 
545c916d7c9SKumar Gala 	return 1;
546c916d7c9SKumar Gala }
547c916d7c9SKumar Gala 
548c916d7c9SKumar Gala static int fm_eth_recv(struct eth_device *dev)
549c916d7c9SKumar Gala {
550c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
551c916d7c9SKumar Gala 	struct fm_port_global_pram *pram;
552c916d7c9SKumar Gala 	struct fm_port_bd *rxbd, *rxbd_base;
553c916d7c9SKumar Gala 	u16 status, len;
5549fc29db1SHou Zhiqiang 	u32 buf_lo, buf_hi;
555c916d7c9SKumar Gala 	u8 *data;
556c916d7c9SKumar Gala 	u16 offset_out;
557466f775eSDaniel Inderbitzin 	int ret = 1;
558c916d7c9SKumar Gala 
559c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)dev->priv;
560c916d7c9SKumar Gala 	pram = fm_eth->rx_pram;
561c916d7c9SKumar Gala 	rxbd = fm_eth->cur_rxbd;
562648bde6dSHou Zhiqiang 	status = muram_readw(&rxbd->status);
563c916d7c9SKumar Gala 
564c916d7c9SKumar Gala 	while (!(status & RxBD_EMPTY)) {
565c916d7c9SKumar Gala 		if (!(status & RxBD_ERROR)) {
5669fc29db1SHou Zhiqiang 			buf_hi = muram_readw(&rxbd->buf_ptr_hi);
5679fc29db1SHou Zhiqiang 			buf_lo = in_be32(&rxbd->buf_ptr_lo);
5689fc29db1SHou Zhiqiang 			data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
569648bde6dSHou Zhiqiang 			len = muram_readw(&rxbd->len);
5701fd92db8SJoe Hershberger 			net_process_received_packet(data, len);
571c916d7c9SKumar Gala 		} else {
572c916d7c9SKumar Gala 			printf("%s: Rx error\n", dev->name);
573466f775eSDaniel Inderbitzin 			ret = 0;
574c916d7c9SKumar Gala 		}
575c916d7c9SKumar Gala 
576c916d7c9SKumar Gala 		/* clear the RxBDs */
577648bde6dSHou Zhiqiang 		muram_writew(&rxbd->status, RxBD_EMPTY);
578648bde6dSHou Zhiqiang 		muram_writew(&rxbd->len, 0);
579c916d7c9SKumar Gala 		sync();
580c916d7c9SKumar Gala 
581c916d7c9SKumar Gala 		/* advance RxBD */
582c916d7c9SKumar Gala 		rxbd++;
583c916d7c9SKumar Gala 		rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
584c916d7c9SKumar Gala 		if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
585c916d7c9SKumar Gala 			rxbd = rxbd_base;
586c916d7c9SKumar Gala 		/* read next status */
587648bde6dSHou Zhiqiang 		status = muram_readw(&rxbd->status);
588c916d7c9SKumar Gala 
589c916d7c9SKumar Gala 		/* update RxQD */
590c916d7c9SKumar Gala 		offset_out = muram_readw(&pram->rxqd.offset_out);
591c916d7c9SKumar Gala 		offset_out += sizeof(struct fm_port_bd);
592c916d7c9SKumar Gala 		if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
593c916d7c9SKumar Gala 			offset_out = 0;
594c916d7c9SKumar Gala 		muram_writew(&pram->rxqd.offset_out, offset_out);
595c916d7c9SKumar Gala 		sync();
596c916d7c9SKumar Gala 	}
597c916d7c9SKumar Gala 	fm_eth->cur_rxbd = (void *)rxbd;
598c916d7c9SKumar Gala 
599466f775eSDaniel Inderbitzin 	return ret;
600c916d7c9SKumar Gala }
601c916d7c9SKumar Gala 
602c916d7c9SKumar Gala static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
603c916d7c9SKumar Gala {
604c916d7c9SKumar Gala 	struct fsl_enet_mac *mac;
605c916d7c9SKumar Gala 	int num;
606c916d7c9SKumar Gala 	void *base, *phyregs = NULL;
607c916d7c9SKumar Gala 
608c916d7c9SKumar Gala 	num = fm_eth->num;
609c916d7c9SKumar Gala 
610111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
611cc19c25eSShengzhou Liu #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
61282a55c1eSShengzhou Liu 	if (fm_eth->type == FM_ETH_10G_E) {
613cc19c25eSShengzhou Liu 		/* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
614cc19c25eSShengzhou Liu 		 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
615cc19c25eSShengzhou Liu 		 * 10GEC1 uses mEMAC1 on T1024.
61682a55c1eSShengzhou Liu 		 * so it needs to change the num.
61782a55c1eSShengzhou Liu 		 */
61882a55c1eSShengzhou Liu 		if (fm_eth->num >= 2)
61982a55c1eSShengzhou Liu 			num -= 2;
62082a55c1eSShengzhou Liu 		else
621944b6ccfSShaohui Xie 			num += 8;
62282a55c1eSShengzhou Liu 	}
623cc19c25eSShengzhou Liu #endif
624111fd19eSRoy Zang 	base = &reg->memac[num].fm_memac;
625111fd19eSRoy Zang 	phyregs = &reg->memac[num].fm_memac_mdio;
626111fd19eSRoy Zang #else
627c916d7c9SKumar Gala 	/* Get the mac registers base address */
628c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E) {
629c916d7c9SKumar Gala 		base = &reg->mac_1g[num].fm_dtesc;
63030381716STimur Tabi 		phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
631c916d7c9SKumar Gala 	} else {
632c916d7c9SKumar Gala 		base = &reg->mac_10g[num].fm_10gec;
633c916d7c9SKumar Gala 		phyregs = &reg->mac_10g[num].fm_10gec_mdio;
634c916d7c9SKumar Gala 	}
635111fd19eSRoy Zang #endif
636c916d7c9SKumar Gala 
637c916d7c9SKumar Gala 	/* alloc mac controller */
638c916d7c9SKumar Gala 	mac = malloc(sizeof(struct fsl_enet_mac));
639c916d7c9SKumar Gala 	if (!mac)
640*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
641c916d7c9SKumar Gala 	memset(mac, 0, sizeof(struct fsl_enet_mac));
642c916d7c9SKumar Gala 
643c916d7c9SKumar Gala 	/* save the mac to fm_eth struct */
644c916d7c9SKumar Gala 	fm_eth->mac = mac;
645c916d7c9SKumar Gala 
646111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
647111fd19eSRoy Zang 	init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
648111fd19eSRoy Zang #else
649c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E)
65030381716STimur Tabi 		init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
651c916d7c9SKumar Gala 	else
652c916d7c9SKumar Gala 		init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
653111fd19eSRoy Zang #endif
654c916d7c9SKumar Gala 
655*0f2cb9f5SHou Zhiqiang 	return 0;
656c916d7c9SKumar Gala }
657c916d7c9SKumar Gala 
658c916d7c9SKumar Gala static int init_phy(struct eth_device *dev)
659c916d7c9SKumar Gala {
660c916d7c9SKumar Gala 	struct fm_eth *fm_eth = dev->priv;
661c916d7c9SKumar Gala 	struct phy_device *phydev = NULL;
662c916d7c9SKumar Gala 	u32 supported;
663c916d7c9SKumar Gala 
664c916d7c9SKumar Gala #ifdef CONFIG_PHYLIB
665c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E)
666c916d7c9SKumar Gala 		dtsec_init_phy(dev);
667c916d7c9SKumar Gala 
668c916d7c9SKumar Gala 	if (fm_eth->bus) {
669c916d7c9SKumar Gala 		phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, dev,
670c916d7c9SKumar Gala 					fm_eth->enet_if);
671c916d7c9SKumar Gala 		if (!phydev) {
672c916d7c9SKumar Gala 			printf("Failed to connect\n");
673c916d7c9SKumar Gala 			return -1;
674c916d7c9SKumar Gala 		}
6756798c324SCodrin Ciubotariu 	} else {
6766798c324SCodrin Ciubotariu 		return 0;
6776798c324SCodrin Ciubotariu 	}
678c916d7c9SKumar Gala 
679c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E) {
680c916d7c9SKumar Gala 		supported = (SUPPORTED_10baseT_Half |
681c916d7c9SKumar Gala 				SUPPORTED_10baseT_Full |
682c916d7c9SKumar Gala 				SUPPORTED_100baseT_Half |
683c916d7c9SKumar Gala 				SUPPORTED_100baseT_Full |
684c916d7c9SKumar Gala 				SUPPORTED_1000baseT_Full);
685c916d7c9SKumar Gala 	} else {
686c916d7c9SKumar Gala 		supported = SUPPORTED_10000baseT_Full;
687c916d7c9SKumar Gala 
688c916d7c9SKumar Gala 		if (tgec_is_fibre(dev))
689c916d7c9SKumar Gala 			phydev->port = PORT_FIBRE;
690c916d7c9SKumar Gala 	}
691c916d7c9SKumar Gala 
692c916d7c9SKumar Gala 	phydev->supported &= supported;
693c916d7c9SKumar Gala 	phydev->advertising = phydev->supported;
694c916d7c9SKumar Gala 
695c916d7c9SKumar Gala 	fm_eth->phydev = phydev;
696c916d7c9SKumar Gala 
697c916d7c9SKumar Gala 	phy_config(phydev);
698c916d7c9SKumar Gala #endif
699c916d7c9SKumar Gala 
700c916d7c9SKumar Gala 	return 0;
701c916d7c9SKumar Gala }
702c916d7c9SKumar Gala 
703c916d7c9SKumar Gala int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
704c916d7c9SKumar Gala {
705c916d7c9SKumar Gala 	struct eth_device *dev;
706c916d7c9SKumar Gala 	struct fm_eth *fm_eth;
707c916d7c9SKumar Gala 	int i, num = info->num;
708*0f2cb9f5SHou Zhiqiang 	int ret;
709c916d7c9SKumar Gala 
710c916d7c9SKumar Gala 	/* alloc eth device */
711c916d7c9SKumar Gala 	dev = (struct eth_device *)malloc(sizeof(struct eth_device));
712c916d7c9SKumar Gala 	if (!dev)
713*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
714c916d7c9SKumar Gala 	memset(dev, 0, sizeof(struct eth_device));
715c916d7c9SKumar Gala 
716c916d7c9SKumar Gala 	/* alloc the FMan ethernet private struct */
717c916d7c9SKumar Gala 	fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
718c916d7c9SKumar Gala 	if (!fm_eth)
719*0f2cb9f5SHou Zhiqiang 		return -ENOMEM;
720c916d7c9SKumar Gala 	memset(fm_eth, 0, sizeof(struct fm_eth));
721c916d7c9SKumar Gala 
722c916d7c9SKumar Gala 	/* save off some things we need from the info struct */
723c916d7c9SKumar Gala 	fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
724c916d7c9SKumar Gala 	fm_eth->num = num;
725c916d7c9SKumar Gala 	fm_eth->type = info->type;
726c916d7c9SKumar Gala 
727c916d7c9SKumar Gala 	fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
728c916d7c9SKumar Gala 	fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
729c916d7c9SKumar Gala 
730c916d7c9SKumar Gala 	/* set the ethernet max receive length */
731c916d7c9SKumar Gala 	fm_eth->max_rx_len = MAX_RXBUF_LEN;
732c916d7c9SKumar Gala 
733c916d7c9SKumar Gala 	/* init global mac structure */
734*0f2cb9f5SHou Zhiqiang 	ret = fm_eth_init_mac(fm_eth, reg);
735*0f2cb9f5SHou Zhiqiang 	if (ret)
736*0f2cb9f5SHou Zhiqiang 		return ret;
737c916d7c9SKumar Gala 
738c916d7c9SKumar Gala 	/* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
739c916d7c9SKumar Gala 	if (fm_eth->type == FM_ETH_1G_E)
740c916d7c9SKumar Gala 		sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
741c916d7c9SKumar Gala 	else
742c916d7c9SKumar Gala 		sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
743c916d7c9SKumar Gala 
744c916d7c9SKumar Gala 	devlist[num_controllers++] = dev;
745c916d7c9SKumar Gala 	dev->iobase = 0;
746c916d7c9SKumar Gala 	dev->priv = (void *)fm_eth;
747c916d7c9SKumar Gala 	dev->init = fm_eth_open;
748c916d7c9SKumar Gala 	dev->halt = fm_eth_halt;
749c916d7c9SKumar Gala 	dev->send = fm_eth_send;
750c916d7c9SKumar Gala 	dev->recv = fm_eth_recv;
751c916d7c9SKumar Gala 	fm_eth->dev = dev;
752c916d7c9SKumar Gala 	fm_eth->bus = info->bus;
753c916d7c9SKumar Gala 	fm_eth->phyaddr = info->phy_addr;
754c916d7c9SKumar Gala 	fm_eth->enet_if = info->enet_if;
755c916d7c9SKumar Gala 
756c916d7c9SKumar Gala 	/* startup the FM im */
757*0f2cb9f5SHou Zhiqiang 	ret = fm_eth_startup(fm_eth);
758*0f2cb9f5SHou Zhiqiang 	if (ret)
759*0f2cb9f5SHou Zhiqiang 		return ret;
760c916d7c9SKumar Gala 
7616798c324SCodrin Ciubotariu 	init_phy(dev);
762c916d7c9SKumar Gala 
763c916d7c9SKumar Gala 	/* clear the ethernet address */
764c916d7c9SKumar Gala 	for (i = 0; i < 6; i++)
765c916d7c9SKumar Gala 		dev->enetaddr[i] = 0;
766c916d7c9SKumar Gala 	eth_register(dev);
767c916d7c9SKumar Gala 
768*0f2cb9f5SHou Zhiqiang 	return 0;
769c916d7c9SKumar Gala }
770