blob: 1d0c9988b1e778ca3f7140b5e8793467af9bb041 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kumar Galac916d7c2011-04-13 08:37:44 -05002/*
Roy Zang111fd192012-10-08 07:44:21 +00003 * Copyright 2009-2012 Freescale Semiconductor, Inc.
Madalin Bucur6eb32a02020-04-23 16:25:19 +03004 * Copyright 2020 NXP
Kumar Galac916d7c2011-04-13 08:37:44 -05005 * Dave Liu <daveliu@freescale.com>
Kumar Galac916d7c2011-04-13 08:37:44 -05006 */
7#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06008#include <part.h>
Kumar Galac916d7c2011-04-13 08:37:44 -05009#include <asm/io.h>
Madalin Bucur6eb32a02020-04-23 16:25:19 +030010#ifdef CONFIG_DM_ETH
11#include <dm.h>
12#include <dm/ofnode.h>
13#include <linux/compat.h>
14#include <phy_interface.h>
15#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050016#include <malloc.h>
17#include <net.h>
18#include <hwconfig.h>
19#include <fm_eth.h>
20#include <fsl_mdio.h>
21#include <miiphy.h>
22#include <phy.h>
Shaohui Xie8225b2f2015-10-26 19:47:47 +080023#include <fsl_dtsec.h>
24#include <fsl_tgec.h>
Shaohui Xiecd348ef2015-03-20 19:28:19 -070025#include <fsl_memac.h>
Kumar Galac916d7c2011-04-13 08:37:44 -050026
27#include "fm.h"
28
Madalin Bucur6eb32a02020-04-23 16:25:19 +030029#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -050030static struct eth_device *devlist[NUM_FM_PORTS];
31static int num_controllers;
Madalin Bucur6eb32a02020-04-23 16:25:19 +030032#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050033
34#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
35
36#define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
37 TBIANA_FULL_DUPLEX)
38
39#define TBIANA_SGMII_ACK 0x4001
40
41#define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
42 TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
43
44/* Configure the TBI for SGMII operation */
Kim Phillips960d70c2012-10-29 13:34:34 +000045static void dtsec_configure_serdes(struct fm_eth *priv)
Kumar Galac916d7c2011-04-13 08:37:44 -050046{
Roy Zang111fd192012-10-08 07:44:21 +000047#ifdef CONFIG_SYS_FMAN_V3
48 u32 value;
49 struct mii_dev bus;
Shengzhou Liuc35f8692014-10-23 17:20:57 +080050 bool sgmii_2500 = (priv->enet_if ==
51 PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
Madalin Bucur6eb32a02020-04-23 16:25:19 +030052 int i = 0, j;
53
54#ifndef CONFIG_DM_ETH
55 bus.priv = priv->mac->phyregs;
56#else
57 bus.priv = priv->pcs_mdio;
58#endif
59 bus.read = memac_mdio_read;
60 bus.write = memac_mdio_write;
61 bus.reset = memac_mdio_reset;
Roy Zang111fd192012-10-08 07:44:21 +000062
Shaohui Xiebc246112015-10-26 19:47:48 +080063qsgmii_loop:
Shengzhou Liuc35f8692014-10-23 17:20:57 +080064 /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
shaohui xiebead0882016-11-15 14:36:47 +080065 if (sgmii_2500)
66 value = PHY_SGMII_CR_PHY_RESET |
67 PHY_SGMII_IF_SPEED_GIGABIT |
68 PHY_SGMII_IF_MODE_SGMII;
69 else
70 value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
Shengzhou Liuc35f8692014-10-23 17:20:57 +080071
Madalin Bucur6eb32a02020-04-23 16:25:19 +030072 for (j = 0; j <= 3; j++)
73 debug("dump PCS reg %#x: %#x\n", j,
74 memac_mdio_read(&bus, i, MDIO_DEVAD_NONE, j));
75
Shaohui Xiebc246112015-10-26 19:47:48 +080076 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
Roy Zang111fd192012-10-08 07:44:21 +000077
78 /* Dev ability according to SGMII specification */
79 value = PHY_SGMII_DEV_ABILITY_SGMII;
Shaohui Xiebc246112015-10-26 19:47:48 +080080 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
Roy Zang111fd192012-10-08 07:44:21 +000081
shaohui xiebead0882016-11-15 14:36:47 +080082 if (sgmii_2500) {
83 /* Adjust link timer for 2.5G SGMII,
84 * 1.6 ms in units of 3.2 ns:
85 * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
86 */
87 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
88 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
89 } else {
90 /* Adjust link timer for SGMII,
91 * 1.6 ms in units of 8 ns:
92 * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
93 */
94 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
95 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
96 }
Roy Zang111fd192012-10-08 07:44:21 +000097
98 /* Restart AN */
shaohui xiebead0882016-11-15 14:36:47 +080099 value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
Shaohui Xiebc246112015-10-26 19:47:48 +0800100 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
101
102 if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
103 i++;
104 goto qsgmii_loop;
105 }
Roy Zang111fd192012-10-08 07:44:21 +0000106#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500107 struct dtsec *regs = priv->mac->base;
108 struct tsec_mii_mng *phyregs = priv->mac->phyregs;
109
110 /*
111 * Access TBI PHY registers at given TSEC register offset as
112 * opposed to the register offset used for external PHY accesses
113 */
114 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
115 TBICON_CLK_SELECT);
116 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
117 TBIANA_SGMII_ACK);
118 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
119 TBI_CR, TBICR_SETTINGS);
Roy Zang111fd192012-10-08 07:44:21 +0000120#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500121}
122
Madalin Bucur8313cb02020-04-23 16:25:16 +0300123static void dtsec_init_phy(struct fm_eth *fm_eth)
Kumar Galac916d7c2011-04-13 08:37:44 -0500124{
Roy Zang111fd192012-10-08 07:44:21 +0000125#ifndef CONFIG_SYS_FMAN_V3
shaohui xie1f3bd3e2012-10-11 20:25:36 +0000126 struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
127
Kumar Galac916d7c2011-04-13 08:37:44 -0500128 /* Assign a Physical address to the TBI */
129 out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
Roy Zang111fd192012-10-08 07:44:21 +0000130#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500131
Shengzhou Liuc35f8692014-10-23 17:20:57 +0800132 if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
Shaohui Xiebc246112015-10-26 19:47:48 +0800133 fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
Shengzhou Liuc35f8692014-10-23 17:20:57 +0800134 fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
Kumar Galac916d7c2011-04-13 08:37:44 -0500135 dtsec_configure_serdes(fm_eth);
136}
137
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300138#ifndef CONFIG_DM_ETH
Shaohui Xie29d8c812015-10-26 19:47:46 +0800139#ifdef CONFIG_PHYLIB
Madalin Bucur8313cb02020-04-23 16:25:16 +0300140static int tgec_is_fibre(struct fm_eth *fm)
Kumar Galac916d7c2011-04-13 08:37:44 -0500141{
Kumar Galac916d7c2011-04-13 08:37:44 -0500142 char phyopt[20];
143
144 sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
145
146 return hwconfig_arg_cmp(phyopt, "xfi");
147}
148#endif
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300149#endif /* CONFIG_DM_ETH */
Shaohui Xie29d8c812015-10-26 19:47:46 +0800150#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500151
152static u16 muram_readw(u16 *addr)
153{
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800154 ulong base = (ulong)addr & ~0x3UL;
155 u32 val32 = in_be32((void *)base);
Kumar Galac916d7c2011-04-13 08:37:44 -0500156 int byte_pos;
157 u16 ret;
158
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800159 byte_pos = (ulong)addr & 0x3UL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500160 if (byte_pos)
161 ret = (u16)(val32 & 0x0000ffff);
162 else
163 ret = (u16)((val32 & 0xffff0000) >> 16);
164
165 return ret;
166}
167
168static void muram_writew(u16 *addr, u16 val)
169{
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800170 ulong base = (ulong)addr & ~0x3UL;
171 u32 org32 = in_be32((void *)base);
Kumar Galac916d7c2011-04-13 08:37:44 -0500172 u32 val32;
173 int byte_pos;
174
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800175 byte_pos = (ulong)addr & 0x3UL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500176 if (byte_pos)
177 val32 = (org32 & 0xffff0000) | val;
178 else
179 val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
180
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800181 out_be32((void *)base, val32);
Kumar Galac916d7c2011-04-13 08:37:44 -0500182}
183
184static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
185{
186 int timeout = 1000000;
187
188 clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
189
190 /* wait until the rx port is not busy */
191 while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
192 ;
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300193 if (!timeout)
194 printf("%s - timeout\n", __func__);
Kumar Galac916d7c2011-04-13 08:37:44 -0500195}
196
197static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
198{
199 /* set BMI to independent mode, Rx port disable */
200 out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
201 /* clear FOF in IM case */
202 out_be32(&rx_port->fmbm_rim, 0);
203 /* Rx frame next engine -RISC */
204 out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
205 /* Rx command attribute - no order, MR[3] = 1 */
206 clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
207 setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
208 /* enable Rx statistic counters */
209 out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
210 /* disable Rx performance counters */
211 out_be32(&rx_port->fmbm_rpc, 0);
212}
213
214static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
215{
216 int timeout = 1000000;
217
218 clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
219
220 /* wait until the tx port is not busy */
221 while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
222 ;
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300223 if (!timeout)
224 printf("%s - timeout\n", __func__);
Kumar Galac916d7c2011-04-13 08:37:44 -0500225}
226
227static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
228{
229 /* set BMI to independent mode, Tx port disable */
230 out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
231 /* Tx frame next engine -RISC */
232 out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
233 out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
234 /* Tx command attribute - no order, MR[3] = 1 */
235 clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
236 setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
237 /* enable Tx statistic counters */
238 out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
239 /* disable Tx performance counters */
240 out_be32(&tx_port->fmbm_tpc, 0);
241}
242
243static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
244{
245 struct fm_port_global_pram *pram;
246 u32 pram_page_offset;
247 void *rx_bd_ring_base;
248 void *rx_buf_pool;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800249 u32 bd_ring_base_lo, bd_ring_base_hi;
250 u32 buf_lo, buf_hi;
Kumar Galac916d7c2011-04-13 08:37:44 -0500251 struct fm_port_bd *rxbd;
252 struct fm_port_qd *rxqd;
253 struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
254 int i;
255
256 /* alloc global parameter ram at MURAM */
257 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
258 FM_PRAM_SIZE, FM_PRAM_ALIGN);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800259 if (!pram) {
260 printf("%s: No muram for Rx global parameter\n", __func__);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800261 return -ENOMEM;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800262 }
263
Kumar Galac916d7c2011-04-13 08:37:44 -0500264 fm_eth->rx_pram = pram;
265
266 /* parameter page offset to MURAM */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800267 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
Kumar Galac916d7c2011-04-13 08:37:44 -0500268
269 /* enable global mode- snooping data buffers and BDs */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800270 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
Kumar Galac916d7c2011-04-13 08:37:44 -0500271
272 /* init the Rx queue descriptor pionter */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800273 out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
Kumar Galac916d7c2011-04-13 08:37:44 -0500274
275 /* set the max receive buffer length, power of 2 */
276 muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
277
278 /* alloc Rx buffer descriptors from main memory */
279 rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
280 * RX_BD_RING_SIZE);
281 if (!rx_bd_ring_base)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800282 return -ENOMEM;
283
Kumar Galac916d7c2011-04-13 08:37:44 -0500284 memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
285 * RX_BD_RING_SIZE);
286
287 /* alloc Rx buffer from main memory */
288 rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
289 if (!rx_buf_pool)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800290 return -ENOMEM;
291
Kumar Galac916d7c2011-04-13 08:37:44 -0500292 memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800293 debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
Kumar Galac916d7c2011-04-13 08:37:44 -0500294
295 /* save them to fm_eth */
296 fm_eth->rx_bd_ring = rx_bd_ring_base;
297 fm_eth->cur_rxbd = rx_bd_ring_base;
298 fm_eth->rx_buf = rx_buf_pool;
299
300 /* init Rx BDs ring */
301 rxbd = (struct fm_port_bd *)rx_bd_ring_base;
302 for (i = 0; i < RX_BD_RING_SIZE; i++) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800303 muram_writew(&rxbd->status, RxBD_EMPTY);
304 muram_writew(&rxbd->len, 0);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800305 buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
306 i * MAX_RXBUF_LEN));
307 buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
308 i * MAX_RXBUF_LEN));
309 muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
310 out_be32(&rxbd->buf_ptr_lo, buf_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500311 rxbd++;
312 }
313
314 /* set the Rx queue descriptor */
315 rxqd = &pram->rxqd;
316 muram_writew(&rxqd->gen, 0);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800317 bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
318 bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
319 muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
320 out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500321 muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
322 * RX_BD_RING_SIZE);
323 muram_writew(&rxqd->offset_in, 0);
324 muram_writew(&rxqd->offset_out, 0);
325
326 /* set IM parameter ram pointer to Rx Frame Queue ID */
327 out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
328
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800329 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500330}
331
332static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
333{
334 struct fm_port_global_pram *pram;
335 u32 pram_page_offset;
336 void *tx_bd_ring_base;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800337 u32 bd_ring_base_lo, bd_ring_base_hi;
Kumar Galac916d7c2011-04-13 08:37:44 -0500338 struct fm_port_bd *txbd;
339 struct fm_port_qd *txqd;
340 struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
341 int i;
342
343 /* alloc global parameter ram at MURAM */
344 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
345 FM_PRAM_SIZE, FM_PRAM_ALIGN);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800346 if (!pram) {
347 printf("%s: No muram for Tx global parameter\n", __func__);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800348 return -ENOMEM;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800349 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500350 fm_eth->tx_pram = pram;
351
352 /* parameter page offset to MURAM */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800353 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
Kumar Galac916d7c2011-04-13 08:37:44 -0500354
355 /* enable global mode- snooping data buffers and BDs */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800356 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
Kumar Galac916d7c2011-04-13 08:37:44 -0500357
358 /* init the Tx queue descriptor pionter */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800359 out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
Kumar Galac916d7c2011-04-13 08:37:44 -0500360
361 /* alloc Tx buffer descriptors from main memory */
362 tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
363 * TX_BD_RING_SIZE);
364 if (!tx_bd_ring_base)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800365 return -ENOMEM;
366
Kumar Galac916d7c2011-04-13 08:37:44 -0500367 memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
368 * TX_BD_RING_SIZE);
369 /* save it to fm_eth */
370 fm_eth->tx_bd_ring = tx_bd_ring_base;
371 fm_eth->cur_txbd = tx_bd_ring_base;
372
373 /* init Tx BDs ring */
374 txbd = (struct fm_port_bd *)tx_bd_ring_base;
375 for (i = 0; i < TX_BD_RING_SIZE; i++) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800376 muram_writew(&txbd->status, TxBD_LAST);
377 muram_writew(&txbd->len, 0);
378 muram_writew(&txbd->buf_ptr_hi, 0);
379 out_be32(&txbd->buf_ptr_lo, 0);
380 txbd++;
Kumar Galac916d7c2011-04-13 08:37:44 -0500381 }
382
383 /* set the Tx queue decriptor */
384 txqd = &pram->txqd;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800385 bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
386 bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
387 muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
388 out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500389 muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
390 * TX_BD_RING_SIZE);
391 muram_writew(&txqd->offset_in, 0);
392 muram_writew(&txqd->offset_out, 0);
393
394 /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
395 out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
396
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800397 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500398}
399
400static int fm_eth_init(struct fm_eth *fm_eth)
401{
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800402 int ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500403
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800404 ret = fm_eth_rx_port_parameter_init(fm_eth);
405 if (ret)
406 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500407
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800408 ret = fm_eth_tx_port_parameter_init(fm_eth);
409 if (ret)
410 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500411
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800412 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500413}
414
415static int fm_eth_startup(struct fm_eth *fm_eth)
416{
417 struct fsl_enet_mac *mac;
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800418 int ret;
419
Kumar Galac916d7c2011-04-13 08:37:44 -0500420 mac = fm_eth->mac;
421
422 /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800423 ret = fm_eth_init(fm_eth);
424 if (ret)
425 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500426 /* setup the MAC controller */
427 mac->init_mac(mac);
428
429 /* For some reason we need to set SPEED_100 */
Shaohui Xie1c68d012013-08-19 18:58:52 +0800430 if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
shaohui xiebead0882016-11-15 14:36:47 +0800431 (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
Shaohui Xie1c68d012013-08-19 18:58:52 +0800432 (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
433 mac->set_if_mode)
Kumar Galac916d7c2011-04-13 08:37:44 -0500434 mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
435
436 /* init bmi rx port, IM mode and disable */
437 bmi_rx_port_init(fm_eth->rx_port);
438 /* init bmi tx port, IM mode and disable */
439 bmi_tx_port_init(fm_eth->tx_port);
440
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800441 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500442}
443
444static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
445{
446 struct fm_port_global_pram *pram;
447
448 pram = fm_eth->tx_pram;
449 /* graceful stop transmission of frames */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800450 setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
Kumar Galac916d7c2011-04-13 08:37:44 -0500451 sync();
452}
453
454static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
455{
456 struct fm_port_global_pram *pram;
457
458 pram = fm_eth->tx_pram;
459 /* re-enable transmission of frames */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800460 clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
Kumar Galac916d7c2011-04-13 08:37:44 -0500461 sync();
462}
463
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300464#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500465static int fm_eth_open(struct eth_device *dev, bd_t *bd)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300466#else
467static int fm_eth_open(struct udevice *dev)
468#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500469{
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300470#ifndef CONFIG_DM_ETH
471 struct fm_eth *fm_eth = dev->priv;
472#else
473 struct eth_pdata *pdata = dev_get_platdata(dev);
474 struct fm_eth *fm_eth = dev_get_priv(dev);
475#endif
476 unsigned char *enetaddr;
Kumar Galac916d7c2011-04-13 08:37:44 -0500477 struct fsl_enet_mac *mac;
Timur Tabi11af8d62012-07-09 08:52:43 +0000478#ifdef CONFIG_PHYLIB
479 int ret;
480#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500481
Kumar Galac916d7c2011-04-13 08:37:44 -0500482 mac = fm_eth->mac;
483
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300484#ifndef CONFIG_DM_ETH
485 enetaddr = &dev->enetaddr[0];
486#else
487 enetaddr = pdata->enetaddr;
488#endif
489
Kumar Galac916d7c2011-04-13 08:37:44 -0500490 /* setup the MAC address */
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300491 if (enetaddr[0] & 0x01) {
492 printf("%s: MacAddress is multicast address\n", __func__);
493 enetaddr[0] = 0;
494 enetaddr[5] = fm_eth->num;
Kumar Galac916d7c2011-04-13 08:37:44 -0500495 }
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300496 mac->set_mac_addr(mac, enetaddr);
Kumar Galac916d7c2011-04-13 08:37:44 -0500497
498 /* enable bmi Rx port */
499 setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
500 /* enable MAC rx/tx port */
501 mac->enable_mac(mac);
502 /* enable bmi Tx port */
503 setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
504 /* re-enable transmission of frame */
505 fmc_tx_port_graceful_stop_disable(fm_eth);
506
507#ifdef CONFIG_PHYLIB
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200508 if (fm_eth->phydev) {
509 ret = phy_startup(fm_eth->phydev);
510 if (ret) {
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300511#ifndef CONFIG_DM_ETH
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200512 printf("%s: Could not initialize\n",
513 fm_eth->phydev->dev->name);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300514#else
515 printf("%s: Could not initialize\n", dev->name);
516#endif
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200517 return ret;
518 }
519 } else {
520 return 0;
Timur Tabi11af8d62012-07-09 08:52:43 +0000521 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500522#else
523 fm_eth->phydev->speed = SPEED_1000;
524 fm_eth->phydev->link = 1;
525 fm_eth->phydev->duplex = DUPLEX_FULL;
526#endif
527
528 /* set the MAC-PHY mode */
529 mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300530 debug("MAC IF mode %d, speed %d, link %d\n", fm_eth->enet_if,
531 fm_eth->phydev->speed, fm_eth->phydev->link);
Kumar Galac916d7c2011-04-13 08:37:44 -0500532
533 if (!fm_eth->phydev->link)
534 printf("%s: No link.\n", fm_eth->phydev->dev->name);
535
536 return fm_eth->phydev->link ? 0 : -1;
537}
538
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300539#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500540static void fm_eth_halt(struct eth_device *dev)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300541#else
542static void fm_eth_halt(struct udevice *dev)
543#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500544{
545 struct fm_eth *fm_eth;
546 struct fsl_enet_mac *mac;
547
548 fm_eth = (struct fm_eth *)dev->priv;
549 mac = fm_eth->mac;
550
551 /* graceful stop the transmission of frames */
552 fmc_tx_port_graceful_stop_enable(fm_eth);
553 /* disable bmi Tx port */
554 bmi_tx_port_disable(fm_eth->tx_port);
555 /* disable MAC rx/tx port */
556 mac->disable_mac(mac);
557 /* disable bmi Rx port */
558 bmi_rx_port_disable(fm_eth->rx_port);
559
Shaohui Xie29d8c812015-10-26 19:47:46 +0800560#ifdef CONFIG_PHYLIB
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200561 if (fm_eth->phydev)
562 phy_shutdown(fm_eth->phydev);
Shaohui Xie29d8c812015-10-26 19:47:46 +0800563#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500564}
565
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300566#ifndef CONFIG_DM_ETH
Joe Hershbergere9df2012012-05-22 07:56:15 +0000567static int fm_eth_send(struct eth_device *dev, void *buf, int len)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300568#else
569static int fm_eth_send(struct udevice *dev, void *buf, int len)
570#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500571{
572 struct fm_eth *fm_eth;
573 struct fm_port_global_pram *pram;
574 struct fm_port_bd *txbd, *txbd_base;
575 u16 offset_in;
576 int i;
577
578 fm_eth = (struct fm_eth *)dev->priv;
579 pram = fm_eth->tx_pram;
580 txbd = fm_eth->cur_txbd;
581
582 /* find one empty TxBD */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800583 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500584 udelay(100);
585 if (i > 0x1000) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800586 printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
587 dev->name, muram_readw(&txbd->status));
Kumar Galac916d7c2011-04-13 08:37:44 -0500588 return 0;
589 }
590 }
591 /* setup TxBD */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800592 muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
593 out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800594 muram_writew(&txbd->len, len);
Kumar Galac916d7c2011-04-13 08:37:44 -0500595 sync();
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800596 muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
Kumar Galac916d7c2011-04-13 08:37:44 -0500597 sync();
598
599 /* update TxQD, let RISC to send the packet */
600 offset_in = muram_readw(&pram->txqd.offset_in);
601 offset_in += sizeof(struct fm_port_bd);
602 if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
603 offset_in = 0;
604 muram_writew(&pram->txqd.offset_in, offset_in);
605 sync();
606
607 /* wait for buffer to be transmitted */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800608 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500609 udelay(100);
610 if (i > 0x10000) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800611 printf("%s: Tx error, txbd->status = 0x%x\n",
612 dev->name, muram_readw(&txbd->status));
Kumar Galac916d7c2011-04-13 08:37:44 -0500613 return 0;
614 }
615 }
616
617 /* advance the TxBD */
618 txbd++;
619 txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
620 if (txbd >= (txbd_base + TX_BD_RING_SIZE))
621 txbd = txbd_base;
622 /* update current txbd */
623 fm_eth->cur_txbd = (void *)txbd;
624
625 return 1;
626}
627
Madalin Bucur1c710182020-04-23 16:25:17 +0300628static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth,
629 struct fm_port_bd *rxbd)
630{
631 struct fm_port_global_pram *pram;
632 struct fm_port_bd *rxbd_base;
633 u16 offset_out;
634
635 pram = fm_eth->rx_pram;
636
637 /* clear the RxBDs */
638 muram_writew(&rxbd->status, RxBD_EMPTY);
639 muram_writew(&rxbd->len, 0);
640 sync();
641
642 /* advance RxBD */
643 rxbd++;
644 rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
645 if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
646 rxbd = rxbd_base;
647
648 /* update RxQD */
649 offset_out = muram_readw(&pram->rxqd.offset_out);
650 offset_out += sizeof(struct fm_port_bd);
651 if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
652 offset_out = 0;
653 muram_writew(&pram->rxqd.offset_out, offset_out);
654 sync();
655
656 return rxbd;
657}
658
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300659#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500660static int fm_eth_recv(struct eth_device *dev)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300661#else
662static int fm_eth_recv(struct udevice *dev, int flags, uchar **packetp)
663#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500664{
Madalin Bucur1c710182020-04-23 16:25:17 +0300665 struct fm_eth *fm_eth = (struct fm_eth *)dev->priv;
666 struct fm_port_bd *rxbd = fm_eth->cur_rxbd;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800667 u32 buf_lo, buf_hi;
Madalin Bucur1c710182020-04-23 16:25:17 +0300668 u16 status, len;
669 int ret = -1;
Kumar Galac916d7c2011-04-13 08:37:44 -0500670 u8 *data;
Kumar Galac916d7c2011-04-13 08:37:44 -0500671
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800672 status = muram_readw(&rxbd->status);
Kumar Galac916d7c2011-04-13 08:37:44 -0500673
674 while (!(status & RxBD_EMPTY)) {
675 if (!(status & RxBD_ERROR)) {
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800676 buf_hi = muram_readw(&rxbd->buf_ptr_hi);
677 buf_lo = in_be32(&rxbd->buf_ptr_lo);
678 data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800679 len = muram_readw(&rxbd->len);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300680#ifndef CONFIG_DM_ETH
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500681 net_process_received_packet(data, len);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300682#else
683 *packetp = data;
684 return len;
685#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500686 } else {
687 printf("%s: Rx error\n", dev->name);
Daniel Inderbitzin466f7752015-07-10 14:06:02 +0200688 ret = 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500689 }
690
Madalin Bucur1c710182020-04-23 16:25:17 +0300691 /* free current bd, advance to next one */
692 rxbd = fm_eth_free_one(fm_eth, rxbd);
Kumar Galac916d7c2011-04-13 08:37:44 -0500693
Kumar Galac916d7c2011-04-13 08:37:44 -0500694 /* read next status */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800695 status = muram_readw(&rxbd->status);
Kumar Galac916d7c2011-04-13 08:37:44 -0500696 }
697 fm_eth->cur_rxbd = (void *)rxbd;
698
Daniel Inderbitzin466f7752015-07-10 14:06:02 +0200699 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500700}
701
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300702#ifdef CONFIG_DM_ETH
703static int fm_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
704{
705 struct fm_eth *fm_eth = (struct fm_eth *)dev->priv;
706
707 fm_eth->cur_rxbd = fm_eth_free_one(fm_eth, fm_eth->cur_rxbd);
708
709 return 0;
710}
711#endif /* CONFIG_DM_ETH */
712
713#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500714static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
715{
716 struct fsl_enet_mac *mac;
717 int num;
718 void *base, *phyregs = NULL;
719
720 num = fm_eth->num;
721
Roy Zang111fd192012-10-08 07:44:21 +0000722#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liucc19c252014-11-24 17:11:57 +0800723#ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800724 if (fm_eth->type == FM_ETH_10G_E) {
Shengzhou Liucc19c252014-11-24 17:11:57 +0800725 /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
726 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
727 * 10GEC1 uses mEMAC1 on T1024.
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800728 * so it needs to change the num.
729 */
730 if (fm_eth->num >= 2)
731 num -= 2;
732 else
733 num += 8;
734 }
Shengzhou Liucc19c252014-11-24 17:11:57 +0800735#endif
Roy Zang111fd192012-10-08 07:44:21 +0000736 base = &reg->memac[num].fm_memac;
737 phyregs = &reg->memac[num].fm_memac_mdio;
738#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500739 /* Get the mac registers base address */
740 if (fm_eth->type == FM_ETH_1G_E) {
741 base = &reg->mac_1g[num].fm_dtesc;
Timur Tabi30381712011-10-04 16:44:43 -0500742 phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
Kumar Galac916d7c2011-04-13 08:37:44 -0500743 } else {
744 base = &reg->mac_10g[num].fm_10gec;
745 phyregs = &reg->mac_10g[num].fm_10gec_mdio;
746 }
Roy Zang111fd192012-10-08 07:44:21 +0000747#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500748
749 /* alloc mac controller */
750 mac = malloc(sizeof(struct fsl_enet_mac));
751 if (!mac)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800752 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500753 memset(mac, 0, sizeof(struct fsl_enet_mac));
754
755 /* save the mac to fm_eth struct */
756 fm_eth->mac = mac;
757
Roy Zang111fd192012-10-08 07:44:21 +0000758#ifdef CONFIG_SYS_FMAN_V3
759 init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
760#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500761 if (fm_eth->type == FM_ETH_1G_E)
Timur Tabi30381712011-10-04 16:44:43 -0500762 init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
Kumar Galac916d7c2011-04-13 08:37:44 -0500763 else
764 init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
Roy Zang111fd192012-10-08 07:44:21 +0000765#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500766
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800767 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500768}
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300769#else /* CONFIG_DM_ETH */
770static int fm_eth_init_mac(struct fm_eth *fm_eth, void *reg)
771{
772#ifndef CONFIG_SYS_FMAN_V3
773 void *mdio;
774#endif
775
776 fm_eth->mac = kzalloc(sizeof(*fm_eth->mac), GFP_KERNEL);
777 if (!fm_eth->mac)
778 return -ENOMEM;
779
780#ifndef CONFIG_SYS_FMAN_V3
781 mdio = fman_mdio(fm_eth->dev->parent, fm_eth->mac_type, fm_eth->num);
782 debug("MDIO %d @ %p\n", fm_eth->num, mdio);
783#endif
784
785 switch (fm_eth->mac_type) {
786#ifdef CONFIG_SYS_FMAN_V3
787 case FM_MEMAC:
788 init_memac(fm_eth->mac, reg, NULL, MAX_RXBUF_LEN);
789 break;
790#else
791 case FM_DTSEC:
792 init_dtsec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
793 break;
794 case FM_TGEC:
795 init_tgec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
796 break;
797#endif
798 }
799
800 return 0;
801}
802#endif /* CONFIG_DM_ETH */
Kumar Galac916d7c2011-04-13 08:37:44 -0500803
Madalin Bucur8313cb02020-04-23 16:25:16 +0300804static int init_phy(struct fm_eth *fm_eth)
Kumar Galac916d7c2011-04-13 08:37:44 -0500805{
Shaohui Xie29d8c812015-10-26 19:47:46 +0800806#ifdef CONFIG_PHYLIB
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300807 u32 supported = PHY_GBIT_FEATURES;
808#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500809 struct phy_device *phydev = NULL;
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300810#endif
811
812 if (fm_eth->type == FM_ETH_10G_E)
813 supported = PHY_10G_FEATURES;
814 if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
815 supported |= SUPPORTED_2500baseX_Full;
Shaohui Xie29d8c812015-10-26 19:47:46 +0800816#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500817
Kumar Galac916d7c2011-04-13 08:37:44 -0500818 if (fm_eth->type == FM_ETH_1G_E)
Madalin Bucur8313cb02020-04-23 16:25:16 +0300819 dtsec_init_phy(fm_eth);
Kumar Galac916d7c2011-04-13 08:37:44 -0500820
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300821#ifdef CONFIG_DM_ETH
822#ifdef CONFIG_PHYLIB
823#ifdef CONFIG_DM_MDIO
824 fm_eth->phydev = dm_eth_phy_connect(fm_eth->dev);
825 if (!fm_eth->phydev)
826 return -ENODEV;
827#endif
828 fm_eth->phydev->advertising &= supported;
829 fm_eth->phydev->supported &= supported;
830
831 phy_config(fm_eth->phydev);
832#endif
833#else /* CONFIG_DM_ETH */
Shaohui Xie29d8c812015-10-26 19:47:46 +0800834#ifdef CONFIG_PHYLIB
Kumar Galac916d7c2011-04-13 08:37:44 -0500835 if (fm_eth->bus) {
Madalin Bucur8313cb02020-04-23 16:25:16 +0300836 phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, fm_eth->dev,
837 fm_eth->enet_if);
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200838 if (!phydev) {
839 printf("Failed to connect\n");
840 return -1;
841 }
842 } else {
843 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500844 }
845
846 if (fm_eth->type == FM_ETH_1G_E) {
847 supported = (SUPPORTED_10baseT_Half |
848 SUPPORTED_10baseT_Full |
849 SUPPORTED_100baseT_Half |
850 SUPPORTED_100baseT_Full |
851 SUPPORTED_1000baseT_Full);
852 } else {
853 supported = SUPPORTED_10000baseT_Full;
854
Madalin Bucur8313cb02020-04-23 16:25:16 +0300855 if (tgec_is_fibre(fm_eth))
Kumar Galac916d7c2011-04-13 08:37:44 -0500856 phydev->port = PORT_FIBRE;
857 }
858
859 phydev->supported &= supported;
860 phydev->advertising = phydev->supported;
861
862 fm_eth->phydev = phydev;
863
864 phy_config(phydev);
865#endif
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300866#endif /* CONFIG_DM_ETH */
Kumar Galac916d7c2011-04-13 08:37:44 -0500867 return 0;
868}
869
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300870#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500871int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
872{
873 struct eth_device *dev;
874 struct fm_eth *fm_eth;
875 int i, num = info->num;
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800876 int ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500877
878 /* alloc eth device */
879 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
880 if (!dev)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800881 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500882 memset(dev, 0, sizeof(struct eth_device));
883
884 /* alloc the FMan ethernet private struct */
885 fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
886 if (!fm_eth)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800887 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500888 memset(fm_eth, 0, sizeof(struct fm_eth));
889
890 /* save off some things we need from the info struct */
891 fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
892 fm_eth->num = num;
893 fm_eth->type = info->type;
894
895 fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
896 fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
897
898 /* set the ethernet max receive length */
899 fm_eth->max_rx_len = MAX_RXBUF_LEN;
900
901 /* init global mac structure */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800902 ret = fm_eth_init_mac(fm_eth, reg);
903 if (ret)
904 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500905
906 /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
907 if (fm_eth->type == FM_ETH_1G_E)
908 sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
909 else
910 sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
911
912 devlist[num_controllers++] = dev;
913 dev->iobase = 0;
914 dev->priv = (void *)fm_eth;
915 dev->init = fm_eth_open;
916 dev->halt = fm_eth_halt;
917 dev->send = fm_eth_send;
918 dev->recv = fm_eth_recv;
919 fm_eth->dev = dev;
920 fm_eth->bus = info->bus;
921 fm_eth->phyaddr = info->phy_addr;
922 fm_eth->enet_if = info->enet_if;
923
924 /* startup the FM im */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800925 ret = fm_eth_startup(fm_eth);
926 if (ret)
927 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500928
Madalin Bucur8313cb02020-04-23 16:25:16 +0300929 init_phy(fm_eth);
Kumar Galac916d7c2011-04-13 08:37:44 -0500930
931 /* clear the ethernet address */
932 for (i = 0; i < 6; i++)
933 dev->enetaddr[i] = 0;
934 eth_register(dev);
935
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800936 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500937}
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300938#else /* CONFIG_DM_ETH */
939#ifdef CONFIG_PHYLIB
940phy_interface_t fman_read_sys_if(struct udevice *dev)
941{
942 const char *if_str;
943
944 if_str = ofnode_read_string(dev->node, "phy-connection-type");
945 debug("MAC system interface mode %s\n", if_str);
946
947 return phy_get_interface_by_name(if_str);
948}
949#endif
950
951static int fm_eth_bind(struct udevice *dev)
952{
953 char mac_name[11];
954 u32 fm, num;
955
956 if (ofnode_read_u32(ofnode_get_parent(dev->node), "cell-index", &fm)) {
957 printf("FMan node property cell-index missing\n");
958 return -EINVAL;
959 }
960
961 if (dev && dev_read_u32(dev, "cell-index", &num)) {
962 printf("FMan MAC node property cell-index missing\n");
963 return -EINVAL;
964 }
965
966 sprintf(mac_name, "fm%d-mac%d", fm + 1, num + 1);
967 device_set_name(dev, mac_name);
968
969 debug("%s - binding %s\n", __func__, mac_name);
970
971 return 0;
972}
973
974static struct udevice *fm_get_internal_mdio(struct udevice *dev)
975{
976 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
977 struct udevice *mdiodev;
978
979 if (dev_read_phandle_with_args(dev, "pcsphy-handle", NULL,
980 0, 0, &phandle) ||
981 !ofnode_valid(phandle.node)) {
982 if (dev_read_phandle_with_args(dev, "tbi-handle", NULL,
983 0, 0, &phandle) ||
984 !ofnode_valid(phandle.node)) {
985 printf("Issue reading pcsphy-handle/tbi-handle for MAC %s\n",
986 dev->name);
987 return NULL;
988 }
989 }
990
991 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
992 ofnode_get_parent(phandle.node),
993 &mdiodev)) {
994 printf("can't find MDIO bus for node %s\n",
995 ofnode_get_name(ofnode_get_parent(phandle.node)));
996 return NULL;
997 }
998 debug("Found internal MDIO bus %p\n", mdiodev);
999
1000 return mdiodev;
1001}
1002
1003static int fm_eth_probe(struct udevice *dev)
1004{
1005 struct fm_eth *fm_eth = (struct fm_eth *)dev->priv;
1006 struct ofnode_phandle_args args;
1007 void *reg;
1008 int ret, index;
1009
1010 debug("%s enter for dev %p fm_eth %p - %s\n", __func__, dev, fm_eth,
1011 (dev) ? dev->name : "-");
1012
1013 if (fm_eth->dev) {
1014 printf("%s already probed, exit\n", (dev) ? dev->name : "-");
1015 return 0;
1016 }
1017
1018 fm_eth->dev = dev;
1019 fm_eth->fm_index = fman_id(dev->parent);
1020 reg = (void *)(uintptr_t)dev_read_addr(dev);
1021 fm_eth->mac_type = dev_get_driver_data(dev);
1022#ifdef CONFIG_PHYLIB
1023 fm_eth->enet_if = fman_read_sys_if(dev);
1024#else
1025 fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII;
1026 printf("%s: warning - unable to determine interface type\n", __func__);
1027#endif
1028 switch (fm_eth->mac_type) {
1029#ifndef CONFIG_SYS_FMAN_V3
1030 case FM_TGEC:
1031 fm_eth->type = FM_ETH_10G_E;
1032 break;
1033 case FM_DTSEC:
1034#else
1035 case FM_MEMAC:
1036 /* default to 1G, 10G is indicated by port property in dts */
1037#endif
1038 fm_eth->type = FM_ETH_1G_E;
1039 break;
1040 }
1041
1042 if (dev_read_u32(dev, "cell-index", &fm_eth->num)) {
1043 printf("FMan MAC node property cell-index missing\n");
1044 return -EINVAL;
1045 }
1046
1047 if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
1048 0, 0, &args))
1049 goto ports_ref_failure;
1050 index = ofnode_read_u32_default(args.node, "cell-index", 0);
1051 if (index <= 0)
1052 goto ports_ref_failure;
1053 fm_eth->rx_port = fman_port(dev->parent, index);
1054
1055 if (ofnode_read_bool(args.node, "fsl,fman-10g-port"))
1056 fm_eth->type = FM_ETH_10G_E;
1057
1058 if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
1059 0, 1, &args))
1060 goto ports_ref_failure;
1061 index = ofnode_read_u32_default(args.node, "cell-index", 0);
1062 if (index <= 0)
1063 goto ports_ref_failure;
1064 fm_eth->tx_port = fman_port(dev->parent, index);
1065
1066 /* set the ethernet max receive length */
1067 fm_eth->max_rx_len = MAX_RXBUF_LEN;
1068
1069 switch (fm_eth->enet_if) {
1070 case PHY_INTERFACE_MODE_QSGMII:
1071 /* all PCS blocks are accessed on one controller */
1072 if (fm_eth->num != 0)
1073 break;
1074 case PHY_INTERFACE_MODE_SGMII:
1075 case PHY_INTERFACE_MODE_SGMII_2500:
1076 fm_eth->pcs_mdio = fm_get_internal_mdio(dev);
1077 break;
1078 default:
1079 break;
1080 }
1081
1082 /* init global mac structure */
1083 ret = fm_eth_init_mac(fm_eth, reg);
1084 if (ret)
1085 return ret;
1086
1087 /* startup the FM im */
1088 ret = fm_eth_startup(fm_eth);
1089
1090 if (!ret)
1091 ret = init_phy(fm_eth);
1092
1093 return ret;
1094
1095ports_ref_failure:
1096 printf("Issue reading fsl,fman-ports for MAC %s\n", dev->name);
1097 return -ENOENT;
1098}
1099
1100static int fm_eth_remove(struct udevice *dev)
1101{
1102 return 0;
1103}
1104
1105static const struct eth_ops fm_eth_ops = {
1106 .start = fm_eth_open,
1107 .send = fm_eth_send,
1108 .recv = fm_eth_recv,
1109 .free_pkt = fm_eth_free_pkt,
1110 .stop = fm_eth_halt,
1111};
1112
1113static const struct udevice_id fm_eth_ids[] = {
1114#ifdef CONFIG_SYS_FMAN_V3
1115 { .compatible = "fsl,fman-memac", .data = FM_MEMAC },
1116#else
1117 { .compatible = "fsl,fman-dtsec", .data = FM_DTSEC },
1118 { .compatible = "fsl,fman-xgec", .data = FM_TGEC },
1119#endif
1120 {}
1121};
1122
1123U_BOOT_DRIVER(eth_fman) = {
1124 .name = "eth_fman",
1125 .id = UCLASS_ETH,
1126 .of_match = fm_eth_ids,
1127 .bind = fm_eth_bind,
1128 .probe = fm_eth_probe,
1129 .remove = fm_eth_remove,
1130 .ops = &fm_eth_ops,
1131 .priv_auto_alloc_size = sizeof(struct fm_eth),
1132 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1133 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1134};
1135#endif /* CONFIG_DM_ETH */