1*83d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+
2c916d7c9SKumar Gala /*
3111fd19eSRoy Zang * Copyright 2009-2012 Freescale Semiconductor, Inc.
4c916d7c9SKumar Gala * Dave Liu <daveliu@freescale.com>
5c916d7c9SKumar Gala */
6c916d7c9SKumar Gala #include <common.h>
7c916d7c9SKumar Gala #include <asm/io.h>
8c916d7c9SKumar Gala #include <malloc.h>
9c916d7c9SKumar Gala #include <net.h>
10c916d7c9SKumar Gala #include <hwconfig.h>
11c916d7c9SKumar Gala #include <fm_eth.h>
12c916d7c9SKumar Gala #include <fsl_mdio.h>
13c916d7c9SKumar Gala #include <miiphy.h>
14c916d7c9SKumar Gala #include <phy.h>
158225b2fdSShaohui Xie #include <fsl_dtsec.h>
168225b2fdSShaohui Xie #include <fsl_tgec.h>
17cd348efaSShaohui Xie #include <fsl_memac.h>
18c916d7c9SKumar Gala
19c916d7c9SKumar Gala #include "fm.h"
20c916d7c9SKumar Gala
21c916d7c9SKumar Gala static struct eth_device *devlist[NUM_FM_PORTS];
22c916d7c9SKumar Gala static int num_controllers;
23c916d7c9SKumar Gala
24c916d7c9SKumar Gala #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
25c916d7c9SKumar Gala
26c916d7c9SKumar Gala #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
27c916d7c9SKumar Gala TBIANA_FULL_DUPLEX)
28c916d7c9SKumar Gala
29c916d7c9SKumar Gala #define TBIANA_SGMII_ACK 0x4001
30c916d7c9SKumar Gala
31c916d7c9SKumar Gala #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
32c916d7c9SKumar Gala TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
33c916d7c9SKumar Gala
34c916d7c9SKumar Gala /* Configure the TBI for SGMII operation */
dtsec_configure_serdes(struct fm_eth * priv)35960d70c6SKim Phillips static void dtsec_configure_serdes(struct fm_eth *priv)
36c916d7c9SKumar Gala {
37111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
38111fd19eSRoy Zang u32 value;
39111fd19eSRoy Zang struct mii_dev bus;
40111fd19eSRoy Zang bus.priv = priv->mac->phyregs;
41c35f8693SShengzhou Liu bool sgmii_2500 = (priv->enet_if ==
42c35f8693SShengzhou Liu PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
43bc24611cSShaohui Xie int i = 0;
44111fd19eSRoy Zang
45bc24611cSShaohui Xie qsgmii_loop:
46c35f8693SShengzhou Liu /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
47bead0880Sshaohui xie if (sgmii_2500)
48bead0880Sshaohui xie value = PHY_SGMII_CR_PHY_RESET |
49bead0880Sshaohui xie PHY_SGMII_IF_SPEED_GIGABIT |
50bead0880Sshaohui xie PHY_SGMII_IF_MODE_SGMII;
51bead0880Sshaohui xie else
52bead0880Sshaohui xie value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
53c35f8693SShengzhou Liu
54bc24611cSShaohui Xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
55111fd19eSRoy Zang
56111fd19eSRoy Zang /* Dev ability according to SGMII specification */
57111fd19eSRoy Zang value = PHY_SGMII_DEV_ABILITY_SGMII;
58bc24611cSShaohui Xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
59111fd19eSRoy Zang
60bead0880Sshaohui xie if (sgmii_2500) {
61bead0880Sshaohui xie /* Adjust link timer for 2.5G SGMII,
62bead0880Sshaohui xie * 1.6 ms in units of 3.2 ns:
63bead0880Sshaohui xie * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
64bead0880Sshaohui xie */
65bead0880Sshaohui xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
66bead0880Sshaohui xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
67bead0880Sshaohui xie } else {
68bead0880Sshaohui xie /* Adjust link timer for SGMII,
69bead0880Sshaohui xie * 1.6 ms in units of 8 ns:
70bead0880Sshaohui xie * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
71bead0880Sshaohui xie */
72bead0880Sshaohui xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
73bead0880Sshaohui xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
74bead0880Sshaohui xie }
75111fd19eSRoy Zang
76111fd19eSRoy Zang /* Restart AN */
77bead0880Sshaohui xie value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
78bc24611cSShaohui Xie memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
79bc24611cSShaohui Xie
80bc24611cSShaohui Xie if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
81bc24611cSShaohui Xie i++;
82bc24611cSShaohui Xie goto qsgmii_loop;
83bc24611cSShaohui Xie }
84111fd19eSRoy Zang #else
85c916d7c9SKumar Gala struct dtsec *regs = priv->mac->base;
86c916d7c9SKumar Gala struct tsec_mii_mng *phyregs = priv->mac->phyregs;
87c916d7c9SKumar Gala
88c916d7c9SKumar Gala /*
89c916d7c9SKumar Gala * Access TBI PHY registers at given TSEC register offset as
90c916d7c9SKumar Gala * opposed to the register offset used for external PHY accesses
91c916d7c9SKumar Gala */
92c916d7c9SKumar Gala tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_TBICON,
93c916d7c9SKumar Gala TBICON_CLK_SELECT);
94c916d7c9SKumar Gala tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_ANA,
95c916d7c9SKumar Gala TBIANA_SGMII_ACK);
96c916d7c9SKumar Gala tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0,
97c916d7c9SKumar Gala TBI_CR, TBICR_SETTINGS);
98111fd19eSRoy Zang #endif
99c916d7c9SKumar Gala }
100c916d7c9SKumar Gala
dtsec_init_phy(struct eth_device * dev)101c916d7c9SKumar Gala static void dtsec_init_phy(struct eth_device *dev)
102c916d7c9SKumar Gala {
103c916d7c9SKumar Gala struct fm_eth *fm_eth = dev->priv;
104111fd19eSRoy Zang #ifndef CONFIG_SYS_FMAN_V3
1051f3bd3e2Sshaohui xie struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
1061f3bd3e2Sshaohui xie
107c916d7c9SKumar Gala /* Assign a Physical address to the TBI */
108c916d7c9SKumar Gala out_be32(®s->tbipa, CONFIG_SYS_TBIPA_VALUE);
109111fd19eSRoy Zang #endif
110c916d7c9SKumar Gala
111c35f8693SShengzhou Liu if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
112bc24611cSShaohui Xie fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
113c35f8693SShengzhou Liu fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
114c916d7c9SKumar Gala dtsec_configure_serdes(fm_eth);
115c916d7c9SKumar Gala }
116c916d7c9SKumar Gala
11729d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
tgec_is_fibre(struct eth_device * dev)118c916d7c9SKumar Gala static int tgec_is_fibre(struct eth_device *dev)
119c916d7c9SKumar Gala {
120c916d7c9SKumar Gala struct fm_eth *fm = dev->priv;
121c916d7c9SKumar Gala char phyopt[20];
122c916d7c9SKumar Gala
123c916d7c9SKumar Gala sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
124c916d7c9SKumar Gala
125c916d7c9SKumar Gala return hwconfig_arg_cmp(phyopt, "xfi");
126c916d7c9SKumar Gala }
127c916d7c9SKumar Gala #endif
12829d8c814SShaohui Xie #endif
129c916d7c9SKumar Gala
muram_readw(u16 * addr)130c916d7c9SKumar Gala static u16 muram_readw(u16 *addr)
131c916d7c9SKumar Gala {
1329fc29db1SHou Zhiqiang ulong base = (ulong)addr & ~0x3UL;
1339fc29db1SHou Zhiqiang u32 val32 = in_be32((void *)base);
134c916d7c9SKumar Gala int byte_pos;
135c916d7c9SKumar Gala u16 ret;
136c916d7c9SKumar Gala
1379fc29db1SHou Zhiqiang byte_pos = (ulong)addr & 0x3UL;
138c916d7c9SKumar Gala if (byte_pos)
139c916d7c9SKumar Gala ret = (u16)(val32 & 0x0000ffff);
140c916d7c9SKumar Gala else
141c916d7c9SKumar Gala ret = (u16)((val32 & 0xffff0000) >> 16);
142c916d7c9SKumar Gala
143c916d7c9SKumar Gala return ret;
144c916d7c9SKumar Gala }
145c916d7c9SKumar Gala
muram_writew(u16 * addr,u16 val)146c916d7c9SKumar Gala static void muram_writew(u16 *addr, u16 val)
147c916d7c9SKumar Gala {
1489fc29db1SHou Zhiqiang ulong base = (ulong)addr & ~0x3UL;
1499fc29db1SHou Zhiqiang u32 org32 = in_be32((void *)base);
150c916d7c9SKumar Gala u32 val32;
151c916d7c9SKumar Gala int byte_pos;
152c916d7c9SKumar Gala
1539fc29db1SHou Zhiqiang byte_pos = (ulong)addr & 0x3UL;
154c916d7c9SKumar Gala if (byte_pos)
155c916d7c9SKumar Gala val32 = (org32 & 0xffff0000) | val;
156c916d7c9SKumar Gala else
157c916d7c9SKumar Gala val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
158c916d7c9SKumar Gala
1599fc29db1SHou Zhiqiang out_be32((void *)base, val32);
160c916d7c9SKumar Gala }
161c916d7c9SKumar Gala
bmi_rx_port_disable(struct fm_bmi_rx_port * rx_port)162c916d7c9SKumar Gala static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
163c916d7c9SKumar Gala {
164c916d7c9SKumar Gala int timeout = 1000000;
165c916d7c9SKumar Gala
166c916d7c9SKumar Gala clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
167c916d7c9SKumar Gala
168c916d7c9SKumar Gala /* wait until the rx port is not busy */
169c916d7c9SKumar Gala while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
170c916d7c9SKumar Gala ;
171c916d7c9SKumar Gala }
172c916d7c9SKumar Gala
bmi_rx_port_init(struct fm_bmi_rx_port * rx_port)173c916d7c9SKumar Gala static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
174c916d7c9SKumar Gala {
175c916d7c9SKumar Gala /* set BMI to independent mode, Rx port disable */
176c916d7c9SKumar Gala out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
177c916d7c9SKumar Gala /* clear FOF in IM case */
178c916d7c9SKumar Gala out_be32(&rx_port->fmbm_rim, 0);
179c916d7c9SKumar Gala /* Rx frame next engine -RISC */
180c916d7c9SKumar Gala out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
181c916d7c9SKumar Gala /* Rx command attribute - no order, MR[3] = 1 */
182c916d7c9SKumar Gala clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
183c916d7c9SKumar Gala setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
184c916d7c9SKumar Gala /* enable Rx statistic counters */
185c916d7c9SKumar Gala out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
186c916d7c9SKumar Gala /* disable Rx performance counters */
187c916d7c9SKumar Gala out_be32(&rx_port->fmbm_rpc, 0);
188c916d7c9SKumar Gala }
189c916d7c9SKumar Gala
bmi_tx_port_disable(struct fm_bmi_tx_port * tx_port)190c916d7c9SKumar Gala static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
191c916d7c9SKumar Gala {
192c916d7c9SKumar Gala int timeout = 1000000;
193c916d7c9SKumar Gala
194c916d7c9SKumar Gala clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
195c916d7c9SKumar Gala
196c916d7c9SKumar Gala /* wait until the tx port is not busy */
197c916d7c9SKumar Gala while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
198c916d7c9SKumar Gala ;
199c916d7c9SKumar Gala }
200c916d7c9SKumar Gala
bmi_tx_port_init(struct fm_bmi_tx_port * tx_port)201c916d7c9SKumar Gala static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
202c916d7c9SKumar Gala {
203c916d7c9SKumar Gala /* set BMI to independent mode, Tx port disable */
204c916d7c9SKumar Gala out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
205c916d7c9SKumar Gala /* Tx frame next engine -RISC */
206c916d7c9SKumar Gala out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
207c916d7c9SKumar Gala out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
208c916d7c9SKumar Gala /* Tx command attribute - no order, MR[3] = 1 */
209c916d7c9SKumar Gala clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
210c916d7c9SKumar Gala setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
211c916d7c9SKumar Gala /* enable Tx statistic counters */
212c916d7c9SKumar Gala out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
213c916d7c9SKumar Gala /* disable Tx performance counters */
214c916d7c9SKumar Gala out_be32(&tx_port->fmbm_tpc, 0);
215c916d7c9SKumar Gala }
216c916d7c9SKumar Gala
fm_eth_rx_port_parameter_init(struct fm_eth * fm_eth)217c916d7c9SKumar Gala static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
218c916d7c9SKumar Gala {
219c916d7c9SKumar Gala struct fm_port_global_pram *pram;
220c916d7c9SKumar Gala u32 pram_page_offset;
221c916d7c9SKumar Gala void *rx_bd_ring_base;
222c916d7c9SKumar Gala void *rx_buf_pool;
2239fc29db1SHou Zhiqiang u32 bd_ring_base_lo, bd_ring_base_hi;
2249fc29db1SHou Zhiqiang u32 buf_lo, buf_hi;
225c916d7c9SKumar Gala struct fm_port_bd *rxbd;
226c916d7c9SKumar Gala struct fm_port_qd *rxqd;
227c916d7c9SKumar Gala struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
228c916d7c9SKumar Gala int i;
229c916d7c9SKumar Gala
230c916d7c9SKumar Gala /* alloc global parameter ram at MURAM */
231c916d7c9SKumar Gala pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
232c916d7c9SKumar Gala FM_PRAM_SIZE, FM_PRAM_ALIGN);
2339fc29db1SHou Zhiqiang if (!pram) {
2349fc29db1SHou Zhiqiang printf("%s: No muram for Rx global parameter\n", __func__);
2350f2cb9f5SHou Zhiqiang return -ENOMEM;
2369fc29db1SHou Zhiqiang }
2379fc29db1SHou Zhiqiang
238c916d7c9SKumar Gala fm_eth->rx_pram = pram;
239c916d7c9SKumar Gala
240c916d7c9SKumar Gala /* parameter page offset to MURAM */
2419fc29db1SHou Zhiqiang pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
242c916d7c9SKumar Gala
243c916d7c9SKumar Gala /* enable global mode- snooping data buffers and BDs */
244648bde6dSHou Zhiqiang out_be32(&pram->mode, PRAM_MODE_GLOBAL);
245c916d7c9SKumar Gala
246c916d7c9SKumar Gala /* init the Rx queue descriptor pionter */
247648bde6dSHou Zhiqiang out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
248c916d7c9SKumar Gala
249c916d7c9SKumar Gala /* set the max receive buffer length, power of 2 */
250c916d7c9SKumar Gala muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
251c916d7c9SKumar Gala
252c916d7c9SKumar Gala /* alloc Rx buffer descriptors from main memory */
253c916d7c9SKumar Gala rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
254c916d7c9SKumar Gala * RX_BD_RING_SIZE);
255c916d7c9SKumar Gala if (!rx_bd_ring_base)
2560f2cb9f5SHou Zhiqiang return -ENOMEM;
2570f2cb9f5SHou Zhiqiang
258c916d7c9SKumar Gala memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
259c916d7c9SKumar Gala * RX_BD_RING_SIZE);
260c916d7c9SKumar Gala
261c916d7c9SKumar Gala /* alloc Rx buffer from main memory */
262c916d7c9SKumar Gala rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
263c916d7c9SKumar Gala if (!rx_buf_pool)
2640f2cb9f5SHou Zhiqiang return -ENOMEM;
2650f2cb9f5SHou Zhiqiang
266c916d7c9SKumar Gala memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
2679fc29db1SHou Zhiqiang debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
268c916d7c9SKumar Gala
269c916d7c9SKumar Gala /* save them to fm_eth */
270c916d7c9SKumar Gala fm_eth->rx_bd_ring = rx_bd_ring_base;
271c916d7c9SKumar Gala fm_eth->cur_rxbd = rx_bd_ring_base;
272c916d7c9SKumar Gala fm_eth->rx_buf = rx_buf_pool;
273c916d7c9SKumar Gala
274c916d7c9SKumar Gala /* init Rx BDs ring */
275c916d7c9SKumar Gala rxbd = (struct fm_port_bd *)rx_bd_ring_base;
276c916d7c9SKumar Gala for (i = 0; i < RX_BD_RING_SIZE; i++) {
277648bde6dSHou Zhiqiang muram_writew(&rxbd->status, RxBD_EMPTY);
278648bde6dSHou Zhiqiang muram_writew(&rxbd->len, 0);
2799fc29db1SHou Zhiqiang buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
2809fc29db1SHou Zhiqiang i * MAX_RXBUF_LEN));
2819fc29db1SHou Zhiqiang buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
2829fc29db1SHou Zhiqiang i * MAX_RXBUF_LEN));
2839fc29db1SHou Zhiqiang muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
2849fc29db1SHou Zhiqiang out_be32(&rxbd->buf_ptr_lo, buf_lo);
285c916d7c9SKumar Gala rxbd++;
286c916d7c9SKumar Gala }
287c916d7c9SKumar Gala
288c916d7c9SKumar Gala /* set the Rx queue descriptor */
289c916d7c9SKumar Gala rxqd = &pram->rxqd;
290c916d7c9SKumar Gala muram_writew(&rxqd->gen, 0);
2919fc29db1SHou Zhiqiang bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
2929fc29db1SHou Zhiqiang bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
2939fc29db1SHou Zhiqiang muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
2949fc29db1SHou Zhiqiang out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
295c916d7c9SKumar Gala muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
296c916d7c9SKumar Gala * RX_BD_RING_SIZE);
297c916d7c9SKumar Gala muram_writew(&rxqd->offset_in, 0);
298c916d7c9SKumar Gala muram_writew(&rxqd->offset_out, 0);
299c916d7c9SKumar Gala
300c916d7c9SKumar Gala /* set IM parameter ram pointer to Rx Frame Queue ID */
301c916d7c9SKumar Gala out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
302c916d7c9SKumar Gala
3030f2cb9f5SHou Zhiqiang return 0;
304c916d7c9SKumar Gala }
305c916d7c9SKumar Gala
fm_eth_tx_port_parameter_init(struct fm_eth * fm_eth)306c916d7c9SKumar Gala static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
307c916d7c9SKumar Gala {
308c916d7c9SKumar Gala struct fm_port_global_pram *pram;
309c916d7c9SKumar Gala u32 pram_page_offset;
310c916d7c9SKumar Gala void *tx_bd_ring_base;
3119fc29db1SHou Zhiqiang u32 bd_ring_base_lo, bd_ring_base_hi;
312c916d7c9SKumar Gala struct fm_port_bd *txbd;
313c916d7c9SKumar Gala struct fm_port_qd *txqd;
314c916d7c9SKumar Gala struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
315c916d7c9SKumar Gala int i;
316c916d7c9SKumar Gala
317c916d7c9SKumar Gala /* alloc global parameter ram at MURAM */
318c916d7c9SKumar Gala pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
319c916d7c9SKumar Gala FM_PRAM_SIZE, FM_PRAM_ALIGN);
3209fc29db1SHou Zhiqiang if (!pram) {
3219fc29db1SHou Zhiqiang printf("%s: No muram for Tx global parameter\n", __func__);
3220f2cb9f5SHou Zhiqiang return -ENOMEM;
3239fc29db1SHou Zhiqiang }
324c916d7c9SKumar Gala fm_eth->tx_pram = pram;
325c916d7c9SKumar Gala
326c916d7c9SKumar Gala /* parameter page offset to MURAM */
3279fc29db1SHou Zhiqiang pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
328c916d7c9SKumar Gala
329c916d7c9SKumar Gala /* enable global mode- snooping data buffers and BDs */
330648bde6dSHou Zhiqiang out_be32(&pram->mode, PRAM_MODE_GLOBAL);
331c916d7c9SKumar Gala
332c916d7c9SKumar Gala /* init the Tx queue descriptor pionter */
333648bde6dSHou Zhiqiang out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
334c916d7c9SKumar Gala
335c916d7c9SKumar Gala /* alloc Tx buffer descriptors from main memory */
336c916d7c9SKumar Gala tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
337c916d7c9SKumar Gala * TX_BD_RING_SIZE);
338c916d7c9SKumar Gala if (!tx_bd_ring_base)
3390f2cb9f5SHou Zhiqiang return -ENOMEM;
3400f2cb9f5SHou Zhiqiang
341c916d7c9SKumar Gala memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
342c916d7c9SKumar Gala * TX_BD_RING_SIZE);
343c916d7c9SKumar Gala /* save it to fm_eth */
344c916d7c9SKumar Gala fm_eth->tx_bd_ring = tx_bd_ring_base;
345c916d7c9SKumar Gala fm_eth->cur_txbd = tx_bd_ring_base;
346c916d7c9SKumar Gala
347c916d7c9SKumar Gala /* init Tx BDs ring */
348c916d7c9SKumar Gala txbd = (struct fm_port_bd *)tx_bd_ring_base;
349c916d7c9SKumar Gala for (i = 0; i < TX_BD_RING_SIZE; i++) {
350648bde6dSHou Zhiqiang muram_writew(&txbd->status, TxBD_LAST);
351648bde6dSHou Zhiqiang muram_writew(&txbd->len, 0);
352648bde6dSHou Zhiqiang muram_writew(&txbd->buf_ptr_hi, 0);
353648bde6dSHou Zhiqiang out_be32(&txbd->buf_ptr_lo, 0);
354648bde6dSHou Zhiqiang txbd++;
355c916d7c9SKumar Gala }
356c916d7c9SKumar Gala
357c916d7c9SKumar Gala /* set the Tx queue decriptor */
358c916d7c9SKumar Gala txqd = &pram->txqd;
3599fc29db1SHou Zhiqiang bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
3609fc29db1SHou Zhiqiang bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
3619fc29db1SHou Zhiqiang muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
3629fc29db1SHou Zhiqiang out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
363c916d7c9SKumar Gala muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
364c916d7c9SKumar Gala * TX_BD_RING_SIZE);
365c916d7c9SKumar Gala muram_writew(&txqd->offset_in, 0);
366c916d7c9SKumar Gala muram_writew(&txqd->offset_out, 0);
367c916d7c9SKumar Gala
368c916d7c9SKumar Gala /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
369c916d7c9SKumar Gala out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
370c916d7c9SKumar Gala
3710f2cb9f5SHou Zhiqiang return 0;
372c916d7c9SKumar Gala }
373c916d7c9SKumar Gala
fm_eth_init(struct fm_eth * fm_eth)374c916d7c9SKumar Gala static int fm_eth_init(struct fm_eth *fm_eth)
375c916d7c9SKumar Gala {
3760f2cb9f5SHou Zhiqiang int ret;
377c916d7c9SKumar Gala
3780f2cb9f5SHou Zhiqiang ret = fm_eth_rx_port_parameter_init(fm_eth);
3790f2cb9f5SHou Zhiqiang if (ret)
3800f2cb9f5SHou Zhiqiang return ret;
3810f2cb9f5SHou Zhiqiang
3820f2cb9f5SHou Zhiqiang ret = fm_eth_tx_port_parameter_init(fm_eth);
3830f2cb9f5SHou Zhiqiang if (ret)
3840f2cb9f5SHou Zhiqiang return ret;
3850f2cb9f5SHou Zhiqiang
386c916d7c9SKumar Gala return 0;
387c916d7c9SKumar Gala }
388c916d7c9SKumar Gala
fm_eth_startup(struct fm_eth * fm_eth)389c916d7c9SKumar Gala static int fm_eth_startup(struct fm_eth *fm_eth)
390c916d7c9SKumar Gala {
391c916d7c9SKumar Gala struct fsl_enet_mac *mac;
3920f2cb9f5SHou Zhiqiang int ret;
3930f2cb9f5SHou Zhiqiang
394c916d7c9SKumar Gala mac = fm_eth->mac;
395c916d7c9SKumar Gala
396c916d7c9SKumar Gala /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
3970f2cb9f5SHou Zhiqiang ret = fm_eth_init(fm_eth);
3980f2cb9f5SHou Zhiqiang if (ret)
3990f2cb9f5SHou Zhiqiang return ret;
400c916d7c9SKumar Gala /* setup the MAC controller */
401c916d7c9SKumar Gala mac->init_mac(mac);
402c916d7c9SKumar Gala
403c916d7c9SKumar Gala /* For some reason we need to set SPEED_100 */
4041c68d01eSShaohui Xie if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
405bead0880Sshaohui xie (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
4061c68d01eSShaohui Xie (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
4071c68d01eSShaohui Xie mac->set_if_mode)
408c916d7c9SKumar Gala mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
409c916d7c9SKumar Gala
410c916d7c9SKumar Gala /* init bmi rx port, IM mode and disable */
411c916d7c9SKumar Gala bmi_rx_port_init(fm_eth->rx_port);
412c916d7c9SKumar Gala /* init bmi tx port, IM mode and disable */
413c916d7c9SKumar Gala bmi_tx_port_init(fm_eth->tx_port);
414c916d7c9SKumar Gala
4150f2cb9f5SHou Zhiqiang return 0;
416c916d7c9SKumar Gala }
417c916d7c9SKumar Gala
fmc_tx_port_graceful_stop_enable(struct fm_eth * fm_eth)418c916d7c9SKumar Gala static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
419c916d7c9SKumar Gala {
420c916d7c9SKumar Gala struct fm_port_global_pram *pram;
421c916d7c9SKumar Gala
422c916d7c9SKumar Gala pram = fm_eth->tx_pram;
423c916d7c9SKumar Gala /* graceful stop transmission of frames */
424648bde6dSHou Zhiqiang setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
425c916d7c9SKumar Gala sync();
426c916d7c9SKumar Gala }
427c916d7c9SKumar Gala
fmc_tx_port_graceful_stop_disable(struct fm_eth * fm_eth)428c916d7c9SKumar Gala static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
429c916d7c9SKumar Gala {
430c916d7c9SKumar Gala struct fm_port_global_pram *pram;
431c916d7c9SKumar Gala
432c916d7c9SKumar Gala pram = fm_eth->tx_pram;
433c916d7c9SKumar Gala /* re-enable transmission of frames */
434648bde6dSHou Zhiqiang clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
435c916d7c9SKumar Gala sync();
436c916d7c9SKumar Gala }
437c916d7c9SKumar Gala
fm_eth_open(struct eth_device * dev,bd_t * bd)438c916d7c9SKumar Gala static int fm_eth_open(struct eth_device *dev, bd_t *bd)
439c916d7c9SKumar Gala {
440c916d7c9SKumar Gala struct fm_eth *fm_eth;
441c916d7c9SKumar Gala struct fsl_enet_mac *mac;
44211af8d65STimur Tabi #ifdef CONFIG_PHYLIB
44311af8d65STimur Tabi int ret;
44411af8d65STimur Tabi #endif
445c916d7c9SKumar Gala
446c916d7c9SKumar Gala fm_eth = (struct fm_eth *)dev->priv;
447c916d7c9SKumar Gala mac = fm_eth->mac;
448c916d7c9SKumar Gala
449c916d7c9SKumar Gala /* setup the MAC address */
450c916d7c9SKumar Gala if (dev->enetaddr[0] & 0x01) {
451c916d7c9SKumar Gala printf("%s: MacAddress is multcast address\n", __func__);
452c916d7c9SKumar Gala return 1;
453c916d7c9SKumar Gala }
454c916d7c9SKumar Gala mac->set_mac_addr(mac, dev->enetaddr);
455c916d7c9SKumar Gala
456c916d7c9SKumar Gala /* enable bmi Rx port */
457c916d7c9SKumar Gala setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
458c916d7c9SKumar Gala /* enable MAC rx/tx port */
459c916d7c9SKumar Gala mac->enable_mac(mac);
460c916d7c9SKumar Gala /* enable bmi Tx port */
461c916d7c9SKumar Gala setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
462c916d7c9SKumar Gala /* re-enable transmission of frame */
463c916d7c9SKumar Gala fmc_tx_port_graceful_stop_disable(fm_eth);
464c916d7c9SKumar Gala
465c916d7c9SKumar Gala #ifdef CONFIG_PHYLIB
4666798c324SCodrin Ciubotariu if (fm_eth->phydev) {
46711af8d65STimur Tabi ret = phy_startup(fm_eth->phydev);
46811af8d65STimur Tabi if (ret) {
4696798c324SCodrin Ciubotariu printf("%s: Could not initialize\n",
4706798c324SCodrin Ciubotariu fm_eth->phydev->dev->name);
47111af8d65STimur Tabi return ret;
47211af8d65STimur Tabi }
4736798c324SCodrin Ciubotariu } else {
4746798c324SCodrin Ciubotariu return 0;
4756798c324SCodrin Ciubotariu }
476c916d7c9SKumar Gala #else
477c916d7c9SKumar Gala fm_eth->phydev->speed = SPEED_1000;
478c916d7c9SKumar Gala fm_eth->phydev->link = 1;
479c916d7c9SKumar Gala fm_eth->phydev->duplex = DUPLEX_FULL;
480c916d7c9SKumar Gala #endif
481c916d7c9SKumar Gala
482c916d7c9SKumar Gala /* set the MAC-PHY mode */
483c916d7c9SKumar Gala mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
484c916d7c9SKumar Gala
485c916d7c9SKumar Gala if (!fm_eth->phydev->link)
486c916d7c9SKumar Gala printf("%s: No link.\n", fm_eth->phydev->dev->name);
487c916d7c9SKumar Gala
488c916d7c9SKumar Gala return fm_eth->phydev->link ? 0 : -1;
489c916d7c9SKumar Gala }
490c916d7c9SKumar Gala
fm_eth_halt(struct eth_device * dev)491c916d7c9SKumar Gala static void fm_eth_halt(struct eth_device *dev)
492c916d7c9SKumar Gala {
493c916d7c9SKumar Gala struct fm_eth *fm_eth;
494c916d7c9SKumar Gala struct fsl_enet_mac *mac;
495c916d7c9SKumar Gala
496c916d7c9SKumar Gala fm_eth = (struct fm_eth *)dev->priv;
497c916d7c9SKumar Gala mac = fm_eth->mac;
498c916d7c9SKumar Gala
499c916d7c9SKumar Gala /* graceful stop the transmission of frames */
500c916d7c9SKumar Gala fmc_tx_port_graceful_stop_enable(fm_eth);
501c916d7c9SKumar Gala /* disable bmi Tx port */
502c916d7c9SKumar Gala bmi_tx_port_disable(fm_eth->tx_port);
503c916d7c9SKumar Gala /* disable MAC rx/tx port */
504c916d7c9SKumar Gala mac->disable_mac(mac);
505c916d7c9SKumar Gala /* disable bmi Rx port */
506c916d7c9SKumar Gala bmi_rx_port_disable(fm_eth->rx_port);
507c916d7c9SKumar Gala
50829d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
5096798c324SCodrin Ciubotariu if (fm_eth->phydev)
510c916d7c9SKumar Gala phy_shutdown(fm_eth->phydev);
51129d8c814SShaohui Xie #endif
512c916d7c9SKumar Gala }
513c916d7c9SKumar Gala
fm_eth_send(struct eth_device * dev,void * buf,int len)514e9df2018SJoe Hershberger static int fm_eth_send(struct eth_device *dev, void *buf, int len)
515c916d7c9SKumar Gala {
516c916d7c9SKumar Gala struct fm_eth *fm_eth;
517c916d7c9SKumar Gala struct fm_port_global_pram *pram;
518c916d7c9SKumar Gala struct fm_port_bd *txbd, *txbd_base;
519c916d7c9SKumar Gala u16 offset_in;
520c916d7c9SKumar Gala int i;
521c916d7c9SKumar Gala
522c916d7c9SKumar Gala fm_eth = (struct fm_eth *)dev->priv;
523c916d7c9SKumar Gala pram = fm_eth->tx_pram;
524c916d7c9SKumar Gala txbd = fm_eth->cur_txbd;
525c916d7c9SKumar Gala
526c916d7c9SKumar Gala /* find one empty TxBD */
527648bde6dSHou Zhiqiang for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
528c916d7c9SKumar Gala udelay(100);
529c916d7c9SKumar Gala if (i > 0x1000) {
530648bde6dSHou Zhiqiang printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
531648bde6dSHou Zhiqiang dev->name, muram_readw(&txbd->status));
532c916d7c9SKumar Gala return 0;
533c916d7c9SKumar Gala }
534c916d7c9SKumar Gala }
535c916d7c9SKumar Gala /* setup TxBD */
5369fc29db1SHou Zhiqiang muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
5379fc29db1SHou Zhiqiang out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
538648bde6dSHou Zhiqiang muram_writew(&txbd->len, len);
539c916d7c9SKumar Gala sync();
540648bde6dSHou Zhiqiang muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
541c916d7c9SKumar Gala sync();
542c916d7c9SKumar Gala
543c916d7c9SKumar Gala /* update TxQD, let RISC to send the packet */
544c916d7c9SKumar Gala offset_in = muram_readw(&pram->txqd.offset_in);
545c916d7c9SKumar Gala offset_in += sizeof(struct fm_port_bd);
546c916d7c9SKumar Gala if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
547c916d7c9SKumar Gala offset_in = 0;
548c916d7c9SKumar Gala muram_writew(&pram->txqd.offset_in, offset_in);
549c916d7c9SKumar Gala sync();
550c916d7c9SKumar Gala
551c916d7c9SKumar Gala /* wait for buffer to be transmitted */
552648bde6dSHou Zhiqiang for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
553c916d7c9SKumar Gala udelay(100);
554c916d7c9SKumar Gala if (i > 0x10000) {
555648bde6dSHou Zhiqiang printf("%s: Tx error, txbd->status = 0x%x\n",
556648bde6dSHou Zhiqiang dev->name, muram_readw(&txbd->status));
557c916d7c9SKumar Gala return 0;
558c916d7c9SKumar Gala }
559c916d7c9SKumar Gala }
560c916d7c9SKumar Gala
561c916d7c9SKumar Gala /* advance the TxBD */
562c916d7c9SKumar Gala txbd++;
563c916d7c9SKumar Gala txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
564c916d7c9SKumar Gala if (txbd >= (txbd_base + TX_BD_RING_SIZE))
565c916d7c9SKumar Gala txbd = txbd_base;
566c916d7c9SKumar Gala /* update current txbd */
567c916d7c9SKumar Gala fm_eth->cur_txbd = (void *)txbd;
568c916d7c9SKumar Gala
569c916d7c9SKumar Gala return 1;
570c916d7c9SKumar Gala }
571c916d7c9SKumar Gala
fm_eth_recv(struct eth_device * dev)572c916d7c9SKumar Gala static int fm_eth_recv(struct eth_device *dev)
573c916d7c9SKumar Gala {
574c916d7c9SKumar Gala struct fm_eth *fm_eth;
575c916d7c9SKumar Gala struct fm_port_global_pram *pram;
576c916d7c9SKumar Gala struct fm_port_bd *rxbd, *rxbd_base;
577c916d7c9SKumar Gala u16 status, len;
5789fc29db1SHou Zhiqiang u32 buf_lo, buf_hi;
579c916d7c9SKumar Gala u8 *data;
580c916d7c9SKumar Gala u16 offset_out;
581466f775eSDaniel Inderbitzin int ret = 1;
582c916d7c9SKumar Gala
583c916d7c9SKumar Gala fm_eth = (struct fm_eth *)dev->priv;
584c916d7c9SKumar Gala pram = fm_eth->rx_pram;
585c916d7c9SKumar Gala rxbd = fm_eth->cur_rxbd;
586648bde6dSHou Zhiqiang status = muram_readw(&rxbd->status);
587c916d7c9SKumar Gala
588c916d7c9SKumar Gala while (!(status & RxBD_EMPTY)) {
589c916d7c9SKumar Gala if (!(status & RxBD_ERROR)) {
5909fc29db1SHou Zhiqiang buf_hi = muram_readw(&rxbd->buf_ptr_hi);
5919fc29db1SHou Zhiqiang buf_lo = in_be32(&rxbd->buf_ptr_lo);
5929fc29db1SHou Zhiqiang data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
593648bde6dSHou Zhiqiang len = muram_readw(&rxbd->len);
5941fd92db8SJoe Hershberger net_process_received_packet(data, len);
595c916d7c9SKumar Gala } else {
596c916d7c9SKumar Gala printf("%s: Rx error\n", dev->name);
597466f775eSDaniel Inderbitzin ret = 0;
598c916d7c9SKumar Gala }
599c916d7c9SKumar Gala
600c916d7c9SKumar Gala /* clear the RxBDs */
601648bde6dSHou Zhiqiang muram_writew(&rxbd->status, RxBD_EMPTY);
602648bde6dSHou Zhiqiang muram_writew(&rxbd->len, 0);
603c916d7c9SKumar Gala sync();
604c916d7c9SKumar Gala
605c916d7c9SKumar Gala /* advance RxBD */
606c916d7c9SKumar Gala rxbd++;
607c916d7c9SKumar Gala rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
608c916d7c9SKumar Gala if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
609c916d7c9SKumar Gala rxbd = rxbd_base;
610c916d7c9SKumar Gala /* read next status */
611648bde6dSHou Zhiqiang status = muram_readw(&rxbd->status);
612c916d7c9SKumar Gala
613c916d7c9SKumar Gala /* update RxQD */
614c916d7c9SKumar Gala offset_out = muram_readw(&pram->rxqd.offset_out);
615c916d7c9SKumar Gala offset_out += sizeof(struct fm_port_bd);
616c916d7c9SKumar Gala if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
617c916d7c9SKumar Gala offset_out = 0;
618c916d7c9SKumar Gala muram_writew(&pram->rxqd.offset_out, offset_out);
619c916d7c9SKumar Gala sync();
620c916d7c9SKumar Gala }
621c916d7c9SKumar Gala fm_eth->cur_rxbd = (void *)rxbd;
622c916d7c9SKumar Gala
623466f775eSDaniel Inderbitzin return ret;
624c916d7c9SKumar Gala }
625c916d7c9SKumar Gala
fm_eth_init_mac(struct fm_eth * fm_eth,struct ccsr_fman * reg)626c916d7c9SKumar Gala static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
627c916d7c9SKumar Gala {
628c916d7c9SKumar Gala struct fsl_enet_mac *mac;
629c916d7c9SKumar Gala int num;
630c916d7c9SKumar Gala void *base, *phyregs = NULL;
631c916d7c9SKumar Gala
632c916d7c9SKumar Gala num = fm_eth->num;
633c916d7c9SKumar Gala
634111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
635cc19c25eSShengzhou Liu #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
63682a55c1eSShengzhou Liu if (fm_eth->type == FM_ETH_10G_E) {
637cc19c25eSShengzhou Liu /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
638cc19c25eSShengzhou Liu * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
639cc19c25eSShengzhou Liu * 10GEC1 uses mEMAC1 on T1024.
64082a55c1eSShengzhou Liu * so it needs to change the num.
64182a55c1eSShengzhou Liu */
64282a55c1eSShengzhou Liu if (fm_eth->num >= 2)
64382a55c1eSShengzhou Liu num -= 2;
64482a55c1eSShengzhou Liu else
645944b6ccfSShaohui Xie num += 8;
64682a55c1eSShengzhou Liu }
647cc19c25eSShengzhou Liu #endif
648111fd19eSRoy Zang base = ®->memac[num].fm_memac;
649111fd19eSRoy Zang phyregs = ®->memac[num].fm_memac_mdio;
650111fd19eSRoy Zang #else
651c916d7c9SKumar Gala /* Get the mac registers base address */
652c916d7c9SKumar Gala if (fm_eth->type == FM_ETH_1G_E) {
653c916d7c9SKumar Gala base = ®->mac_1g[num].fm_dtesc;
65430381716STimur Tabi phyregs = ®->mac_1g[num].fm_mdio.miimcfg;
655c916d7c9SKumar Gala } else {
656c916d7c9SKumar Gala base = ®->mac_10g[num].fm_10gec;
657c916d7c9SKumar Gala phyregs = ®->mac_10g[num].fm_10gec_mdio;
658c916d7c9SKumar Gala }
659111fd19eSRoy Zang #endif
660c916d7c9SKumar Gala
661c916d7c9SKumar Gala /* alloc mac controller */
662c916d7c9SKumar Gala mac = malloc(sizeof(struct fsl_enet_mac));
663c916d7c9SKumar Gala if (!mac)
6640f2cb9f5SHou Zhiqiang return -ENOMEM;
665c916d7c9SKumar Gala memset(mac, 0, sizeof(struct fsl_enet_mac));
666c916d7c9SKumar Gala
667c916d7c9SKumar Gala /* save the mac to fm_eth struct */
668c916d7c9SKumar Gala fm_eth->mac = mac;
669c916d7c9SKumar Gala
670111fd19eSRoy Zang #ifdef CONFIG_SYS_FMAN_V3
671111fd19eSRoy Zang init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
672111fd19eSRoy Zang #else
673c916d7c9SKumar Gala if (fm_eth->type == FM_ETH_1G_E)
67430381716STimur Tabi init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
675c916d7c9SKumar Gala else
676c916d7c9SKumar Gala init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
677111fd19eSRoy Zang #endif
678c916d7c9SKumar Gala
6790f2cb9f5SHou Zhiqiang return 0;
680c916d7c9SKumar Gala }
681c916d7c9SKumar Gala
init_phy(struct eth_device * dev)682c916d7c9SKumar Gala static int init_phy(struct eth_device *dev)
683c916d7c9SKumar Gala {
684c916d7c9SKumar Gala struct fm_eth *fm_eth = dev->priv;
68529d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
686c916d7c9SKumar Gala struct phy_device *phydev = NULL;
687c916d7c9SKumar Gala u32 supported;
68829d8c814SShaohui Xie #endif
689c916d7c9SKumar Gala
690c916d7c9SKumar Gala if (fm_eth->type == FM_ETH_1G_E)
691c916d7c9SKumar Gala dtsec_init_phy(dev);
692c916d7c9SKumar Gala
69329d8c814SShaohui Xie #ifdef CONFIG_PHYLIB
694c916d7c9SKumar Gala if (fm_eth->bus) {
695c916d7c9SKumar Gala phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, dev,
696c916d7c9SKumar Gala fm_eth->enet_if);
697c916d7c9SKumar Gala if (!phydev) {
698c916d7c9SKumar Gala printf("Failed to connect\n");
699c916d7c9SKumar Gala return -1;
700c916d7c9SKumar Gala }
7016798c324SCodrin Ciubotariu } else {
7026798c324SCodrin Ciubotariu return 0;
7036798c324SCodrin Ciubotariu }
704c916d7c9SKumar Gala
705c916d7c9SKumar Gala if (fm_eth->type == FM_ETH_1G_E) {
706c916d7c9SKumar Gala supported = (SUPPORTED_10baseT_Half |
707c916d7c9SKumar Gala SUPPORTED_10baseT_Full |
708c916d7c9SKumar Gala SUPPORTED_100baseT_Half |
709c916d7c9SKumar Gala SUPPORTED_100baseT_Full |
710c916d7c9SKumar Gala SUPPORTED_1000baseT_Full);
711c916d7c9SKumar Gala } else {
712c916d7c9SKumar Gala supported = SUPPORTED_10000baseT_Full;
713c916d7c9SKumar Gala
714c916d7c9SKumar Gala if (tgec_is_fibre(dev))
715c916d7c9SKumar Gala phydev->port = PORT_FIBRE;
716c916d7c9SKumar Gala }
717c916d7c9SKumar Gala
718c916d7c9SKumar Gala phydev->supported &= supported;
719c916d7c9SKumar Gala phydev->advertising = phydev->supported;
720c916d7c9SKumar Gala
721c916d7c9SKumar Gala fm_eth->phydev = phydev;
722c916d7c9SKumar Gala
723c916d7c9SKumar Gala phy_config(phydev);
724c916d7c9SKumar Gala #endif
725c916d7c9SKumar Gala
726c916d7c9SKumar Gala return 0;
727c916d7c9SKumar Gala }
728c916d7c9SKumar Gala
fm_eth_initialize(struct ccsr_fman * reg,struct fm_eth_info * info)729c916d7c9SKumar Gala int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
730c916d7c9SKumar Gala {
731c916d7c9SKumar Gala struct eth_device *dev;
732c916d7c9SKumar Gala struct fm_eth *fm_eth;
733c916d7c9SKumar Gala int i, num = info->num;
7340f2cb9f5SHou Zhiqiang int ret;
735c916d7c9SKumar Gala
736c916d7c9SKumar Gala /* alloc eth device */
737c916d7c9SKumar Gala dev = (struct eth_device *)malloc(sizeof(struct eth_device));
738c916d7c9SKumar Gala if (!dev)
7390f2cb9f5SHou Zhiqiang return -ENOMEM;
740c916d7c9SKumar Gala memset(dev, 0, sizeof(struct eth_device));
741c916d7c9SKumar Gala
742c916d7c9SKumar Gala /* alloc the FMan ethernet private struct */
743c916d7c9SKumar Gala fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
744c916d7c9SKumar Gala if (!fm_eth)
7450f2cb9f5SHou Zhiqiang return -ENOMEM;
746c916d7c9SKumar Gala memset(fm_eth, 0, sizeof(struct fm_eth));
747c916d7c9SKumar Gala
748c916d7c9SKumar Gala /* save off some things we need from the info struct */
749c916d7c9SKumar Gala fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
750c916d7c9SKumar Gala fm_eth->num = num;
751c916d7c9SKumar Gala fm_eth->type = info->type;
752c916d7c9SKumar Gala
753c916d7c9SKumar Gala fm_eth->rx_port = (void *)®->port[info->rx_port_id - 1].fm_bmi;
754c916d7c9SKumar Gala fm_eth->tx_port = (void *)®->port[info->tx_port_id - 1].fm_bmi;
755c916d7c9SKumar Gala
756c916d7c9SKumar Gala /* set the ethernet max receive length */
757c916d7c9SKumar Gala fm_eth->max_rx_len = MAX_RXBUF_LEN;
758c916d7c9SKumar Gala
759c916d7c9SKumar Gala /* init global mac structure */
7600f2cb9f5SHou Zhiqiang ret = fm_eth_init_mac(fm_eth, reg);
7610f2cb9f5SHou Zhiqiang if (ret)
7620f2cb9f5SHou Zhiqiang return ret;
763c916d7c9SKumar Gala
764c916d7c9SKumar Gala /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
765c916d7c9SKumar Gala if (fm_eth->type == FM_ETH_1G_E)
766c916d7c9SKumar Gala sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
767c916d7c9SKumar Gala else
768c916d7c9SKumar Gala sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
769c916d7c9SKumar Gala
770c916d7c9SKumar Gala devlist[num_controllers++] = dev;
771c916d7c9SKumar Gala dev->iobase = 0;
772c916d7c9SKumar Gala dev->priv = (void *)fm_eth;
773c916d7c9SKumar Gala dev->init = fm_eth_open;
774c916d7c9SKumar Gala dev->halt = fm_eth_halt;
775c916d7c9SKumar Gala dev->send = fm_eth_send;
776c916d7c9SKumar Gala dev->recv = fm_eth_recv;
777c916d7c9SKumar Gala fm_eth->dev = dev;
778c916d7c9SKumar Gala fm_eth->bus = info->bus;
779c916d7c9SKumar Gala fm_eth->phyaddr = info->phy_addr;
780c916d7c9SKumar Gala fm_eth->enet_if = info->enet_if;
781c916d7c9SKumar Gala
782c916d7c9SKumar Gala /* startup the FM im */
7830f2cb9f5SHou Zhiqiang ret = fm_eth_startup(fm_eth);
7840f2cb9f5SHou Zhiqiang if (ret)
7850f2cb9f5SHou Zhiqiang return ret;
786c916d7c9SKumar Gala
7876798c324SCodrin Ciubotariu init_phy(dev);
788c916d7c9SKumar Gala
789c916d7c9SKumar Gala /* clear the ethernet address */
790c916d7c9SKumar Gala for (i = 0; i < 6; i++)
791c916d7c9SKumar Gala dev->enetaddr[i] = 0;
792c916d7c9SKumar Gala eth_register(dev);
793c916d7c9SKumar Gala
7940f2cb9f5SHou Zhiqiang return 0;
795c916d7c9SKumar Gala }
796