blob: 7c23ccc1f0e6e3405d3ab3db8cca90bb2012d880 [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 Glassf7ae49f2020-05-10 11:40:05 -06008#include <log.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -06009#include <part.h>
Kumar Galac916d7c2011-04-13 08:37:44 -050010#include <asm/io.h>
Madalin Bucur6eb32a02020-04-23 16:25:19 +030011#ifdef CONFIG_DM_ETH
12#include <dm.h>
13#include <dm/ofnode.h>
14#include <linux/compat.h>
15#include <phy_interface.h>
16#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050017#include <malloc.h>
18#include <net.h>
19#include <hwconfig.h>
20#include <fm_eth.h>
21#include <fsl_mdio.h>
22#include <miiphy.h>
23#include <phy.h>
Shaohui Xie8225b2f2015-10-26 19:47:47 +080024#include <fsl_dtsec.h>
25#include <fsl_tgec.h>
Shaohui Xiecd348ef2015-03-20 19:28:19 -070026#include <fsl_memac.h>
Simon Glassc05ed002020-05-10 11:40:11 -060027#include <linux/delay.h>
Kumar Galac916d7c2011-04-13 08:37:44 -050028
29#include "fm.h"
30
Madalin Bucur6eb32a02020-04-23 16:25:19 +030031#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -050032static struct eth_device *devlist[NUM_FM_PORTS];
33static int num_controllers;
Madalin Bucur6eb32a02020-04-23 16:25:19 +030034#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050035
36#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
37
38#define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
39 TBIANA_FULL_DUPLEX)
40
41#define TBIANA_SGMII_ACK 0x4001
42
43#define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
44 TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
45
46/* Configure the TBI for SGMII operation */
Kim Phillips960d70c2012-10-29 13:34:34 +000047static void dtsec_configure_serdes(struct fm_eth *priv)
Kumar Galac916d7c2011-04-13 08:37:44 -050048{
Roy Zang111fd192012-10-08 07:44:21 +000049#ifdef CONFIG_SYS_FMAN_V3
50 u32 value;
51 struct mii_dev bus;
Shengzhou Liuc35f8692014-10-23 17:20:57 +080052 bool sgmii_2500 = (priv->enet_if ==
53 PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
Madalin Bucur6eb32a02020-04-23 16:25:19 +030054 int i = 0, j;
55
56#ifndef CONFIG_DM_ETH
57 bus.priv = priv->mac->phyregs;
58#else
59 bus.priv = priv->pcs_mdio;
Madalin Bucur6eb32a02020-04-23 16:25:19 +030060 bus.read = memac_mdio_read;
61 bus.write = memac_mdio_write;
62 bus.reset = memac_mdio_reset;
Madalin Bucur9b2e8922020-05-04 13:09:12 +030063#endif
Roy Zang111fd192012-10-08 07:44:21 +000064
Shaohui Xiebc246112015-10-26 19:47:48 +080065qsgmii_loop:
Shengzhou Liuc35f8692014-10-23 17:20:57 +080066 /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
shaohui xiebead0882016-11-15 14:36:47 +080067 if (sgmii_2500)
68 value = PHY_SGMII_CR_PHY_RESET |
69 PHY_SGMII_IF_SPEED_GIGABIT |
70 PHY_SGMII_IF_MODE_SGMII;
71 else
72 value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
Shengzhou Liuc35f8692014-10-23 17:20:57 +080073
Madalin Bucur6eb32a02020-04-23 16:25:19 +030074 for (j = 0; j <= 3; j++)
75 debug("dump PCS reg %#x: %#x\n", j,
76 memac_mdio_read(&bus, i, MDIO_DEVAD_NONE, j));
77
Shaohui Xiebc246112015-10-26 19:47:48 +080078 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
Roy Zang111fd192012-10-08 07:44:21 +000079
80 /* Dev ability according to SGMII specification */
81 value = PHY_SGMII_DEV_ABILITY_SGMII;
Shaohui Xiebc246112015-10-26 19:47:48 +080082 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
Roy Zang111fd192012-10-08 07:44:21 +000083
shaohui xiebead0882016-11-15 14:36:47 +080084 if (sgmii_2500) {
85 /* Adjust link timer for 2.5G SGMII,
86 * 1.6 ms in units of 3.2 ns:
87 * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
88 */
89 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
90 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
91 } else {
92 /* Adjust link timer for SGMII,
93 * 1.6 ms in units of 8 ns:
94 * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
95 */
96 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
97 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
98 }
Roy Zang111fd192012-10-08 07:44:21 +000099
100 /* Restart AN */
shaohui xiebead0882016-11-15 14:36:47 +0800101 value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
Shaohui Xiebc246112015-10-26 19:47:48 +0800102 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
103
104 if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
105 i++;
106 goto qsgmii_loop;
107 }
Roy Zang111fd192012-10-08 07:44:21 +0000108#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500109 struct dtsec *regs = priv->mac->base;
110 struct tsec_mii_mng *phyregs = priv->mac->phyregs;
111
112 /*
113 * Access TBI PHY registers at given TSEC register offset as
114 * opposed to the register offset used for external PHY accesses
115 */
116 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
117 TBICON_CLK_SELECT);
118 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
119 TBIANA_SGMII_ACK);
120 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
121 TBI_CR, TBICR_SETTINGS);
Roy Zang111fd192012-10-08 07:44:21 +0000122#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500123}
124
Madalin Bucur8313cb02020-04-23 16:25:16 +0300125static void dtsec_init_phy(struct fm_eth *fm_eth)
Kumar Galac916d7c2011-04-13 08:37:44 -0500126{
Roy Zang111fd192012-10-08 07:44:21 +0000127#ifndef CONFIG_SYS_FMAN_V3
shaohui xie1f3bd3e2012-10-11 20:25:36 +0000128 struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
129
Kumar Galac916d7c2011-04-13 08:37:44 -0500130 /* Assign a Physical address to the TBI */
131 out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
Roy Zang111fd192012-10-08 07:44:21 +0000132#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500133
Shengzhou Liuc35f8692014-10-23 17:20:57 +0800134 if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
Shaohui Xiebc246112015-10-26 19:47:48 +0800135 fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
Shengzhou Liuc35f8692014-10-23 17:20:57 +0800136 fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
Kumar Galac916d7c2011-04-13 08:37:44 -0500137 dtsec_configure_serdes(fm_eth);
138}
139
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300140#ifndef CONFIG_DM_ETH
Shaohui Xie29d8c812015-10-26 19:47:46 +0800141#ifdef CONFIG_PHYLIB
Madalin Bucur8313cb02020-04-23 16:25:16 +0300142static int tgec_is_fibre(struct fm_eth *fm)
Kumar Galac916d7c2011-04-13 08:37:44 -0500143{
Kumar Galac916d7c2011-04-13 08:37:44 -0500144 char phyopt[20];
145
146 sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
147
148 return hwconfig_arg_cmp(phyopt, "xfi");
149}
150#endif
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300151#endif /* CONFIG_DM_ETH */
Shaohui Xie29d8c812015-10-26 19:47:46 +0800152#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500153
154static u16 muram_readw(u16 *addr)
155{
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800156 ulong base = (ulong)addr & ~0x3UL;
157 u32 val32 = in_be32((void *)base);
Kumar Galac916d7c2011-04-13 08:37:44 -0500158 int byte_pos;
159 u16 ret;
160
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800161 byte_pos = (ulong)addr & 0x3UL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500162 if (byte_pos)
163 ret = (u16)(val32 & 0x0000ffff);
164 else
165 ret = (u16)((val32 & 0xffff0000) >> 16);
166
167 return ret;
168}
169
170static void muram_writew(u16 *addr, u16 val)
171{
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800172 ulong base = (ulong)addr & ~0x3UL;
173 u32 org32 = in_be32((void *)base);
Kumar Galac916d7c2011-04-13 08:37:44 -0500174 u32 val32;
175 int byte_pos;
176
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800177 byte_pos = (ulong)addr & 0x3UL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500178 if (byte_pos)
179 val32 = (org32 & 0xffff0000) | val;
180 else
181 val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
182
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800183 out_be32((void *)base, val32);
Kumar Galac916d7c2011-04-13 08:37:44 -0500184}
185
186static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
187{
188 int timeout = 1000000;
189
190 clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
191
192 /* wait until the rx port is not busy */
193 while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
194 ;
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300195 if (!timeout)
196 printf("%s - timeout\n", __func__);
Kumar Galac916d7c2011-04-13 08:37:44 -0500197}
198
199static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
200{
201 /* set BMI to independent mode, Rx port disable */
202 out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
203 /* clear FOF in IM case */
204 out_be32(&rx_port->fmbm_rim, 0);
205 /* Rx frame next engine -RISC */
206 out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
207 /* Rx command attribute - no order, MR[3] = 1 */
208 clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
209 setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
210 /* enable Rx statistic counters */
211 out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
212 /* disable Rx performance counters */
213 out_be32(&rx_port->fmbm_rpc, 0);
214}
215
216static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
217{
218 int timeout = 1000000;
219
220 clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
221
222 /* wait until the tx port is not busy */
223 while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
224 ;
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300225 if (!timeout)
226 printf("%s - timeout\n", __func__);
Kumar Galac916d7c2011-04-13 08:37:44 -0500227}
228
229static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
230{
231 /* set BMI to independent mode, Tx port disable */
232 out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
233 /* Tx frame next engine -RISC */
234 out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
235 out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
236 /* Tx command attribute - no order, MR[3] = 1 */
237 clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
238 setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
239 /* enable Tx statistic counters */
240 out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
241 /* disable Tx performance counters */
242 out_be32(&tx_port->fmbm_tpc, 0);
243}
244
245static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
246{
247 struct fm_port_global_pram *pram;
248 u32 pram_page_offset;
249 void *rx_bd_ring_base;
250 void *rx_buf_pool;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800251 u32 bd_ring_base_lo, bd_ring_base_hi;
252 u32 buf_lo, buf_hi;
Kumar Galac916d7c2011-04-13 08:37:44 -0500253 struct fm_port_bd *rxbd;
254 struct fm_port_qd *rxqd;
255 struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
256 int i;
257
258 /* alloc global parameter ram at MURAM */
259 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
260 FM_PRAM_SIZE, FM_PRAM_ALIGN);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800261 if (!pram) {
262 printf("%s: No muram for Rx global parameter\n", __func__);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800263 return -ENOMEM;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800264 }
265
Kumar Galac916d7c2011-04-13 08:37:44 -0500266 fm_eth->rx_pram = pram;
267
268 /* parameter page offset to MURAM */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800269 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
Kumar Galac916d7c2011-04-13 08:37:44 -0500270
271 /* enable global mode- snooping data buffers and BDs */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800272 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
Kumar Galac916d7c2011-04-13 08:37:44 -0500273
274 /* init the Rx queue descriptor pionter */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800275 out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
Kumar Galac916d7c2011-04-13 08:37:44 -0500276
277 /* set the max receive buffer length, power of 2 */
278 muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
279
280 /* alloc Rx buffer descriptors from main memory */
281 rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
282 * RX_BD_RING_SIZE);
283 if (!rx_bd_ring_base)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800284 return -ENOMEM;
285
Kumar Galac916d7c2011-04-13 08:37:44 -0500286 memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
287 * RX_BD_RING_SIZE);
288
289 /* alloc Rx buffer from main memory */
290 rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
Hou Zhiqiang3e184872021-06-12 21:15:41 +0300291 if (!rx_buf_pool) {
292 free(rx_bd_ring_base);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800293 return -ENOMEM;
Hou Zhiqiang3e184872021-06-12 21:15:41 +0300294 }
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800295
Kumar Galac916d7c2011-04-13 08:37:44 -0500296 memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800297 debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
Kumar Galac916d7c2011-04-13 08:37:44 -0500298
299 /* save them to fm_eth */
300 fm_eth->rx_bd_ring = rx_bd_ring_base;
301 fm_eth->cur_rxbd = rx_bd_ring_base;
302 fm_eth->rx_buf = rx_buf_pool;
303
304 /* init Rx BDs ring */
305 rxbd = (struct fm_port_bd *)rx_bd_ring_base;
306 for (i = 0; i < RX_BD_RING_SIZE; i++) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800307 muram_writew(&rxbd->status, RxBD_EMPTY);
308 muram_writew(&rxbd->len, 0);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800309 buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
310 i * MAX_RXBUF_LEN));
311 buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
312 i * MAX_RXBUF_LEN));
313 muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
314 out_be32(&rxbd->buf_ptr_lo, buf_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500315 rxbd++;
316 }
317
318 /* set the Rx queue descriptor */
319 rxqd = &pram->rxqd;
320 muram_writew(&rxqd->gen, 0);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800321 bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
322 bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
323 muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
324 out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500325 muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
326 * RX_BD_RING_SIZE);
327 muram_writew(&rxqd->offset_in, 0);
328 muram_writew(&rxqd->offset_out, 0);
329
330 /* set IM parameter ram pointer to Rx Frame Queue ID */
331 out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
332
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800333 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500334}
335
336static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
337{
338 struct fm_port_global_pram *pram;
339 u32 pram_page_offset;
340 void *tx_bd_ring_base;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800341 u32 bd_ring_base_lo, bd_ring_base_hi;
Kumar Galac916d7c2011-04-13 08:37:44 -0500342 struct fm_port_bd *txbd;
343 struct fm_port_qd *txqd;
344 struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
345 int i;
346
347 /* alloc global parameter ram at MURAM */
348 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
349 FM_PRAM_SIZE, FM_PRAM_ALIGN);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800350 if (!pram) {
351 printf("%s: No muram for Tx global parameter\n", __func__);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800352 return -ENOMEM;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800353 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500354 fm_eth->tx_pram = pram;
355
356 /* parameter page offset to MURAM */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800357 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
Kumar Galac916d7c2011-04-13 08:37:44 -0500358
359 /* enable global mode- snooping data buffers and BDs */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800360 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
Kumar Galac916d7c2011-04-13 08:37:44 -0500361
362 /* init the Tx queue descriptor pionter */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800363 out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
Kumar Galac916d7c2011-04-13 08:37:44 -0500364
365 /* alloc Tx buffer descriptors from main memory */
366 tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
367 * TX_BD_RING_SIZE);
368 if (!tx_bd_ring_base)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800369 return -ENOMEM;
370
Kumar Galac916d7c2011-04-13 08:37:44 -0500371 memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
372 * TX_BD_RING_SIZE);
373 /* save it to fm_eth */
374 fm_eth->tx_bd_ring = tx_bd_ring_base;
375 fm_eth->cur_txbd = tx_bd_ring_base;
376
377 /* init Tx BDs ring */
378 txbd = (struct fm_port_bd *)tx_bd_ring_base;
379 for (i = 0; i < TX_BD_RING_SIZE; i++) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800380 muram_writew(&txbd->status, TxBD_LAST);
381 muram_writew(&txbd->len, 0);
382 muram_writew(&txbd->buf_ptr_hi, 0);
383 out_be32(&txbd->buf_ptr_lo, 0);
384 txbd++;
Kumar Galac916d7c2011-04-13 08:37:44 -0500385 }
386
387 /* set the Tx queue decriptor */
388 txqd = &pram->txqd;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800389 bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
390 bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
391 muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
392 out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500393 muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
394 * TX_BD_RING_SIZE);
395 muram_writew(&txqd->offset_in, 0);
396 muram_writew(&txqd->offset_out, 0);
397
398 /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
399 out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
400
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800401 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500402}
403
404static int fm_eth_init(struct fm_eth *fm_eth)
405{
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800406 int ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500407
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800408 ret = fm_eth_rx_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 ret = fm_eth_tx_port_parameter_init(fm_eth);
413 if (ret)
414 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500415
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800416 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500417}
418
419static int fm_eth_startup(struct fm_eth *fm_eth)
420{
421 struct fsl_enet_mac *mac;
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800422 int ret;
423
Kumar Galac916d7c2011-04-13 08:37:44 -0500424 mac = fm_eth->mac;
425
426 /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800427 ret = fm_eth_init(fm_eth);
428 if (ret)
429 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500430 /* setup the MAC controller */
431 mac->init_mac(mac);
432
433 /* For some reason we need to set SPEED_100 */
Shaohui Xie1c68d012013-08-19 18:58:52 +0800434 if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
shaohui xiebead0882016-11-15 14:36:47 +0800435 (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
Shaohui Xie1c68d012013-08-19 18:58:52 +0800436 (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
437 mac->set_if_mode)
Kumar Galac916d7c2011-04-13 08:37:44 -0500438 mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
439
440 /* init bmi rx port, IM mode and disable */
441 bmi_rx_port_init(fm_eth->rx_port);
442 /* init bmi tx port, IM mode and disable */
443 bmi_tx_port_init(fm_eth->tx_port);
444
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800445 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500446}
447
448static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
449{
450 struct fm_port_global_pram *pram;
451
452 pram = fm_eth->tx_pram;
453 /* graceful stop transmission of frames */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800454 setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
Kumar Galac916d7c2011-04-13 08:37:44 -0500455 sync();
456}
457
458static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
459{
460 struct fm_port_global_pram *pram;
461
462 pram = fm_eth->tx_pram;
463 /* re-enable transmission of frames */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800464 clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
Kumar Galac916d7c2011-04-13 08:37:44 -0500465 sync();
466}
467
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300468#ifndef CONFIG_DM_ETH
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900469static int fm_eth_open(struct eth_device *dev, struct bd_info *bd)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300470#else
471static int fm_eth_open(struct udevice *dev)
472#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500473{
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300474#ifndef CONFIG_DM_ETH
475 struct fm_eth *fm_eth = dev->priv;
476#else
Simon Glassc69cda22020-12-03 16:55:20 -0700477 struct eth_pdata *pdata = dev_get_plat(dev);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300478 struct fm_eth *fm_eth = dev_get_priv(dev);
479#endif
480 unsigned char *enetaddr;
Kumar Galac916d7c2011-04-13 08:37:44 -0500481 struct fsl_enet_mac *mac;
Timur Tabi11af8d62012-07-09 08:52:43 +0000482#ifdef CONFIG_PHYLIB
483 int ret;
484#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500485
Kumar Galac916d7c2011-04-13 08:37:44 -0500486 mac = fm_eth->mac;
487
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300488#ifndef CONFIG_DM_ETH
489 enetaddr = &dev->enetaddr[0];
490#else
491 enetaddr = pdata->enetaddr;
492#endif
493
Kumar Galac916d7c2011-04-13 08:37:44 -0500494 /* setup the MAC address */
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300495 if (enetaddr[0] & 0x01) {
496 printf("%s: MacAddress is multicast address\n", __func__);
497 enetaddr[0] = 0;
498 enetaddr[5] = fm_eth->num;
Kumar Galac916d7c2011-04-13 08:37:44 -0500499 }
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300500 mac->set_mac_addr(mac, enetaddr);
Kumar Galac916d7c2011-04-13 08:37:44 -0500501
502 /* enable bmi Rx port */
503 setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
504 /* enable MAC rx/tx port */
505 mac->enable_mac(mac);
506 /* enable bmi Tx port */
507 setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
508 /* re-enable transmission of frame */
509 fmc_tx_port_graceful_stop_disable(fm_eth);
510
511#ifdef CONFIG_PHYLIB
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200512 if (fm_eth->phydev) {
513 ret = phy_startup(fm_eth->phydev);
514 if (ret) {
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300515#ifndef CONFIG_DM_ETH
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200516 printf("%s: Could not initialize\n",
517 fm_eth->phydev->dev->name);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300518#else
519 printf("%s: Could not initialize\n", dev->name);
520#endif
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200521 return ret;
522 }
523 } else {
524 return 0;
Timur Tabi11af8d62012-07-09 08:52:43 +0000525 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500526#else
527 fm_eth->phydev->speed = SPEED_1000;
528 fm_eth->phydev->link = 1;
529 fm_eth->phydev->duplex = DUPLEX_FULL;
530#endif
531
532 /* set the MAC-PHY mode */
533 mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300534 debug("MAC IF mode %d, speed %d, link %d\n", fm_eth->enet_if,
535 fm_eth->phydev->speed, fm_eth->phydev->link);
Kumar Galac916d7c2011-04-13 08:37:44 -0500536
537 if (!fm_eth->phydev->link)
538 printf("%s: No link.\n", fm_eth->phydev->dev->name);
539
540 return fm_eth->phydev->link ? 0 : -1;
541}
542
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300543#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500544static void fm_eth_halt(struct eth_device *dev)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300545#else
546static void fm_eth_halt(struct udevice *dev)
547#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500548{
549 struct fm_eth *fm_eth;
550 struct fsl_enet_mac *mac;
551
Simon Glass0fd3d912020-12-22 19:30:28 -0700552#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500553 fm_eth = (struct fm_eth *)dev->priv;
Simon Glass0fd3d912020-12-22 19:30:28 -0700554#else
555 fm_eth = dev_get_priv(dev);
556#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500557 mac = fm_eth->mac;
558
559 /* graceful stop the transmission of frames */
560 fmc_tx_port_graceful_stop_enable(fm_eth);
561 /* disable bmi Tx port */
562 bmi_tx_port_disable(fm_eth->tx_port);
563 /* disable MAC rx/tx port */
564 mac->disable_mac(mac);
565 /* disable bmi Rx port */
566 bmi_rx_port_disable(fm_eth->rx_port);
567
Shaohui Xie29d8c812015-10-26 19:47:46 +0800568#ifdef CONFIG_PHYLIB
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200569 if (fm_eth->phydev)
570 phy_shutdown(fm_eth->phydev);
Shaohui Xie29d8c812015-10-26 19:47:46 +0800571#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500572}
573
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300574#ifndef CONFIG_DM_ETH
Joe Hershbergere9df2012012-05-22 07:56:15 +0000575static int fm_eth_send(struct eth_device *dev, void *buf, int len)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300576#else
577static int fm_eth_send(struct udevice *dev, void *buf, int len)
578#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500579{
580 struct fm_eth *fm_eth;
581 struct fm_port_global_pram *pram;
582 struct fm_port_bd *txbd, *txbd_base;
583 u16 offset_in;
584 int i;
585
Simon Glass0fd3d912020-12-22 19:30:28 -0700586#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500587 fm_eth = (struct fm_eth *)dev->priv;
Simon Glass0fd3d912020-12-22 19:30:28 -0700588#else
589 fm_eth = dev_get_priv(dev);
590#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500591 pram = fm_eth->tx_pram;
592 txbd = fm_eth->cur_txbd;
593
594 /* find one empty TxBD */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800595 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500596 udelay(100);
597 if (i > 0x1000) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800598 printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
599 dev->name, muram_readw(&txbd->status));
Kumar Galac916d7c2011-04-13 08:37:44 -0500600 return 0;
601 }
602 }
603 /* setup TxBD */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800604 muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
605 out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800606 muram_writew(&txbd->len, len);
Kumar Galac916d7c2011-04-13 08:37:44 -0500607 sync();
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800608 muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
Kumar Galac916d7c2011-04-13 08:37:44 -0500609 sync();
610
611 /* update TxQD, let RISC to send the packet */
612 offset_in = muram_readw(&pram->txqd.offset_in);
613 offset_in += sizeof(struct fm_port_bd);
614 if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
615 offset_in = 0;
616 muram_writew(&pram->txqd.offset_in, offset_in);
617 sync();
618
619 /* wait for buffer to be transmitted */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800620 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500621 udelay(100);
622 if (i > 0x10000) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800623 printf("%s: Tx error, txbd->status = 0x%x\n",
624 dev->name, muram_readw(&txbd->status));
Kumar Galac916d7c2011-04-13 08:37:44 -0500625 return 0;
626 }
627 }
628
629 /* advance the TxBD */
630 txbd++;
631 txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
632 if (txbd >= (txbd_base + TX_BD_RING_SIZE))
633 txbd = txbd_base;
634 /* update current txbd */
635 fm_eth->cur_txbd = (void *)txbd;
636
637 return 1;
638}
639
Madalin Bucur1c710182020-04-23 16:25:17 +0300640static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth,
641 struct fm_port_bd *rxbd)
642{
643 struct fm_port_global_pram *pram;
644 struct fm_port_bd *rxbd_base;
645 u16 offset_out;
646
647 pram = fm_eth->rx_pram;
648
649 /* clear the RxBDs */
650 muram_writew(&rxbd->status, RxBD_EMPTY);
651 muram_writew(&rxbd->len, 0);
652 sync();
653
654 /* advance RxBD */
655 rxbd++;
656 rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
657 if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
658 rxbd = rxbd_base;
659
660 /* update RxQD */
661 offset_out = muram_readw(&pram->rxqd.offset_out);
662 offset_out += sizeof(struct fm_port_bd);
663 if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
664 offset_out = 0;
665 muram_writew(&pram->rxqd.offset_out, offset_out);
666 sync();
667
668 return rxbd;
669}
670
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300671#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500672static int fm_eth_recv(struct eth_device *dev)
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300673#else
674static int fm_eth_recv(struct udevice *dev, int flags, uchar **packetp)
675#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500676{
Simon Glass0fd3d912020-12-22 19:30:28 -0700677 struct fm_eth *fm_eth;
678 struct fm_port_bd *rxbd;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800679 u32 buf_lo, buf_hi;
Madalin Bucur1c710182020-04-23 16:25:17 +0300680 u16 status, len;
681 int ret = -1;
Kumar Galac916d7c2011-04-13 08:37:44 -0500682 u8 *data;
Kumar Galac916d7c2011-04-13 08:37:44 -0500683
Simon Glass0fd3d912020-12-22 19:30:28 -0700684#ifndef CONFIG_DM_ETH
685 fm_eth = (struct fm_eth *)dev->priv;
686#else
687 fm_eth = dev_get_priv(dev);
688#endif
689 rxbd = fm_eth->cur_rxbd;
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800690 status = muram_readw(&rxbd->status);
Kumar Galac916d7c2011-04-13 08:37:44 -0500691
692 while (!(status & RxBD_EMPTY)) {
693 if (!(status & RxBD_ERROR)) {
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800694 buf_hi = muram_readw(&rxbd->buf_ptr_hi);
695 buf_lo = in_be32(&rxbd->buf_ptr_lo);
696 data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800697 len = muram_readw(&rxbd->len);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300698#ifndef CONFIG_DM_ETH
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500699 net_process_received_packet(data, len);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300700#else
701 *packetp = data;
702 return len;
703#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500704 } else {
705 printf("%s: Rx error\n", dev->name);
Daniel Inderbitzin466f7752015-07-10 14:06:02 +0200706 ret = 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500707 }
708
Madalin Bucur1c710182020-04-23 16:25:17 +0300709 /* free current bd, advance to next one */
710 rxbd = fm_eth_free_one(fm_eth, rxbd);
Kumar Galac916d7c2011-04-13 08:37:44 -0500711
Kumar Galac916d7c2011-04-13 08:37:44 -0500712 /* read next status */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800713 status = muram_readw(&rxbd->status);
Kumar Galac916d7c2011-04-13 08:37:44 -0500714 }
715 fm_eth->cur_rxbd = (void *)rxbd;
716
Daniel Inderbitzin466f7752015-07-10 14:06:02 +0200717 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500718}
719
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300720#ifdef CONFIG_DM_ETH
721static int fm_eth_free_pkt(struct udevice *dev, uchar *packet, int length)
722{
Simon Glass0fd3d912020-12-22 19:30:28 -0700723 struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev);
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300724
725 fm_eth->cur_rxbd = fm_eth_free_one(fm_eth, fm_eth->cur_rxbd);
726
727 return 0;
728}
729#endif /* CONFIG_DM_ETH */
730
731#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500732static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
733{
734 struct fsl_enet_mac *mac;
735 int num;
736 void *base, *phyregs = NULL;
737
738 num = fm_eth->num;
739
Roy Zang111fd192012-10-08 07:44:21 +0000740#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liucc19c252014-11-24 17:11:57 +0800741#ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800742 if (fm_eth->type == FM_ETH_10G_E) {
Shengzhou Liucc19c252014-11-24 17:11:57 +0800743 /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
744 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
745 * 10GEC1 uses mEMAC1 on T1024.
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800746 * so it needs to change the num.
747 */
748 if (fm_eth->num >= 2)
749 num -= 2;
750 else
751 num += 8;
752 }
Shengzhou Liucc19c252014-11-24 17:11:57 +0800753#endif
Roy Zang111fd192012-10-08 07:44:21 +0000754 base = &reg->memac[num].fm_memac;
755 phyregs = &reg->memac[num].fm_memac_mdio;
756#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500757 /* Get the mac registers base address */
758 if (fm_eth->type == FM_ETH_1G_E) {
759 base = &reg->mac_1g[num].fm_dtesc;
Timur Tabi30381712011-10-04 16:44:43 -0500760 phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
Kumar Galac916d7c2011-04-13 08:37:44 -0500761 } else {
762 base = &reg->mac_10g[num].fm_10gec;
763 phyregs = &reg->mac_10g[num].fm_10gec_mdio;
764 }
Roy Zang111fd192012-10-08 07:44:21 +0000765#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500766
767 /* alloc mac controller */
768 mac = malloc(sizeof(struct fsl_enet_mac));
769 if (!mac)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800770 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500771 memset(mac, 0, sizeof(struct fsl_enet_mac));
772
773 /* save the mac to fm_eth struct */
774 fm_eth->mac = mac;
775
Roy Zang111fd192012-10-08 07:44:21 +0000776#ifdef CONFIG_SYS_FMAN_V3
777 init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
778#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500779 if (fm_eth->type == FM_ETH_1G_E)
Timur Tabi30381712011-10-04 16:44:43 -0500780 init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
Kumar Galac916d7c2011-04-13 08:37:44 -0500781 else
782 init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
Roy Zang111fd192012-10-08 07:44:21 +0000783#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500784
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800785 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500786}
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300787#else /* CONFIG_DM_ETH */
788static int fm_eth_init_mac(struct fm_eth *fm_eth, void *reg)
789{
790#ifndef CONFIG_SYS_FMAN_V3
791 void *mdio;
792#endif
793
794 fm_eth->mac = kzalloc(sizeof(*fm_eth->mac), GFP_KERNEL);
795 if (!fm_eth->mac)
796 return -ENOMEM;
797
798#ifndef CONFIG_SYS_FMAN_V3
799 mdio = fman_mdio(fm_eth->dev->parent, fm_eth->mac_type, fm_eth->num);
800 debug("MDIO %d @ %p\n", fm_eth->num, mdio);
801#endif
802
803 switch (fm_eth->mac_type) {
804#ifdef CONFIG_SYS_FMAN_V3
805 case FM_MEMAC:
806 init_memac(fm_eth->mac, reg, NULL, MAX_RXBUF_LEN);
807 break;
808#else
809 case FM_DTSEC:
810 init_dtsec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
811 break;
812 case FM_TGEC:
813 init_tgec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN);
814 break;
815#endif
816 }
817
818 return 0;
819}
820#endif /* CONFIG_DM_ETH */
Kumar Galac916d7c2011-04-13 08:37:44 -0500821
Madalin Bucur8313cb02020-04-23 16:25:16 +0300822static int init_phy(struct fm_eth *fm_eth)
Kumar Galac916d7c2011-04-13 08:37:44 -0500823{
Shaohui Xie29d8c812015-10-26 19:47:46 +0800824#ifdef CONFIG_PHYLIB
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300825 u32 supported = PHY_GBIT_FEATURES;
826#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500827 struct phy_device *phydev = NULL;
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300828#endif
829
830 if (fm_eth->type == FM_ETH_10G_E)
831 supported = PHY_10G_FEATURES;
832 if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
833 supported |= SUPPORTED_2500baseX_Full;
Shaohui Xie29d8c812015-10-26 19:47:46 +0800834#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500835
Kumar Galac916d7c2011-04-13 08:37:44 -0500836 if (fm_eth->type == FM_ETH_1G_E)
Madalin Bucur8313cb02020-04-23 16:25:16 +0300837 dtsec_init_phy(fm_eth);
Kumar Galac916d7c2011-04-13 08:37:44 -0500838
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300839#ifdef CONFIG_DM_ETH
840#ifdef CONFIG_PHYLIB
841#ifdef CONFIG_DM_MDIO
842 fm_eth->phydev = dm_eth_phy_connect(fm_eth->dev);
843 if (!fm_eth->phydev)
844 return -ENODEV;
845#endif
846 fm_eth->phydev->advertising &= supported;
847 fm_eth->phydev->supported &= supported;
848
849 phy_config(fm_eth->phydev);
850#endif
851#else /* CONFIG_DM_ETH */
Shaohui Xie29d8c812015-10-26 19:47:46 +0800852#ifdef CONFIG_PHYLIB
Kumar Galac916d7c2011-04-13 08:37:44 -0500853 if (fm_eth->bus) {
Madalin Bucur8313cb02020-04-23 16:25:16 +0300854 phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, fm_eth->dev,
855 fm_eth->enet_if);
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200856 if (!phydev) {
857 printf("Failed to connect\n");
858 return -1;
859 }
860 } else {
861 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500862 }
863
864 if (fm_eth->type == FM_ETH_1G_E) {
865 supported = (SUPPORTED_10baseT_Half |
866 SUPPORTED_10baseT_Full |
867 SUPPORTED_100baseT_Half |
868 SUPPORTED_100baseT_Full |
869 SUPPORTED_1000baseT_Full);
870 } else {
871 supported = SUPPORTED_10000baseT_Full;
872
Madalin Bucur8313cb02020-04-23 16:25:16 +0300873 if (tgec_is_fibre(fm_eth))
Kumar Galac916d7c2011-04-13 08:37:44 -0500874 phydev->port = PORT_FIBRE;
875 }
876
877 phydev->supported &= supported;
878 phydev->advertising = phydev->supported;
879
880 fm_eth->phydev = phydev;
881
882 phy_config(phydev);
883#endif
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300884#endif /* CONFIG_DM_ETH */
Kumar Galac916d7c2011-04-13 08:37:44 -0500885 return 0;
886}
887
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300888#ifndef CONFIG_DM_ETH
Kumar Galac916d7c2011-04-13 08:37:44 -0500889int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
890{
891 struct eth_device *dev;
892 struct fm_eth *fm_eth;
893 int i, num = info->num;
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800894 int ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500895
896 /* alloc eth device */
897 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
898 if (!dev)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800899 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500900 memset(dev, 0, sizeof(struct eth_device));
901
902 /* alloc the FMan ethernet private struct */
903 fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
904 if (!fm_eth)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800905 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500906 memset(fm_eth, 0, sizeof(struct fm_eth));
907
908 /* save off some things we need from the info struct */
909 fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
910 fm_eth->num = num;
911 fm_eth->type = info->type;
912
913 fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
914 fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
915
916 /* set the ethernet max receive length */
917 fm_eth->max_rx_len = MAX_RXBUF_LEN;
918
919 /* init global mac structure */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800920 ret = fm_eth_init_mac(fm_eth, reg);
921 if (ret)
922 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500923
924 /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
925 if (fm_eth->type == FM_ETH_1G_E)
926 sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
927 else
928 sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
929
930 devlist[num_controllers++] = dev;
931 dev->iobase = 0;
932 dev->priv = (void *)fm_eth;
933 dev->init = fm_eth_open;
934 dev->halt = fm_eth_halt;
935 dev->send = fm_eth_send;
936 dev->recv = fm_eth_recv;
937 fm_eth->dev = dev;
938 fm_eth->bus = info->bus;
939 fm_eth->phyaddr = info->phy_addr;
940 fm_eth->enet_if = info->enet_if;
941
942 /* startup the FM im */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800943 ret = fm_eth_startup(fm_eth);
944 if (ret)
945 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500946
Madalin Bucur8313cb02020-04-23 16:25:16 +0300947 init_phy(fm_eth);
Kumar Galac916d7c2011-04-13 08:37:44 -0500948
949 /* clear the ethernet address */
950 for (i = 0; i < 6; i++)
951 dev->enetaddr[i] = 0;
952 eth_register(dev);
953
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800954 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500955}
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300956#else /* CONFIG_DM_ETH */
957#ifdef CONFIG_PHYLIB
958phy_interface_t fman_read_sys_if(struct udevice *dev)
959{
960 const char *if_str;
961
Simon Glassf10643c2020-12-19 10:40:14 -0700962 if_str = ofnode_read_string(dev_ofnode(dev), "phy-connection-type");
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300963 debug("MAC system interface mode %s\n", if_str);
964
965 return phy_get_interface_by_name(if_str);
966}
967#endif
968
969static int fm_eth_bind(struct udevice *dev)
970{
971 char mac_name[11];
972 u32 fm, num;
973
Simon Glassf10643c2020-12-19 10:40:14 -0700974 if (ofnode_read_u32(ofnode_get_parent(dev_ofnode(dev)), "cell-index", &fm)) {
Madalin Bucur6eb32a02020-04-23 16:25:19 +0300975 printf("FMan node property cell-index missing\n");
976 return -EINVAL;
977 }
978
979 if (dev && dev_read_u32(dev, "cell-index", &num)) {
980 printf("FMan MAC node property cell-index missing\n");
981 return -EINVAL;
982 }
983
984 sprintf(mac_name, "fm%d-mac%d", fm + 1, num + 1);
985 device_set_name(dev, mac_name);
986
987 debug("%s - binding %s\n", __func__, mac_name);
988
989 return 0;
990}
991
992static struct udevice *fm_get_internal_mdio(struct udevice *dev)
993{
994 struct ofnode_phandle_args phandle = {.node = ofnode_null()};
995 struct udevice *mdiodev;
996
997 if (dev_read_phandle_with_args(dev, "pcsphy-handle", NULL,
998 0, 0, &phandle) ||
999 !ofnode_valid(phandle.node)) {
1000 if (dev_read_phandle_with_args(dev, "tbi-handle", NULL,
1001 0, 0, &phandle) ||
1002 !ofnode_valid(phandle.node)) {
1003 printf("Issue reading pcsphy-handle/tbi-handle for MAC %s\n",
1004 dev->name);
1005 return NULL;
1006 }
1007 }
1008
1009 if (uclass_get_device_by_ofnode(UCLASS_MDIO,
1010 ofnode_get_parent(phandle.node),
1011 &mdiodev)) {
1012 printf("can't find MDIO bus for node %s\n",
1013 ofnode_get_name(ofnode_get_parent(phandle.node)));
1014 return NULL;
1015 }
1016 debug("Found internal MDIO bus %p\n", mdiodev);
1017
1018 return mdiodev;
1019}
1020
1021static int fm_eth_probe(struct udevice *dev)
1022{
Simon Glass0fd3d912020-12-22 19:30:28 -07001023 struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev);
Madalin Bucur6eb32a02020-04-23 16:25:19 +03001024 struct ofnode_phandle_args args;
1025 void *reg;
1026 int ret, index;
1027
1028 debug("%s enter for dev %p fm_eth %p - %s\n", __func__, dev, fm_eth,
1029 (dev) ? dev->name : "-");
1030
1031 if (fm_eth->dev) {
1032 printf("%s already probed, exit\n", (dev) ? dev->name : "-");
1033 return 0;
1034 }
1035
1036 fm_eth->dev = dev;
1037 fm_eth->fm_index = fman_id(dev->parent);
1038 reg = (void *)(uintptr_t)dev_read_addr(dev);
1039 fm_eth->mac_type = dev_get_driver_data(dev);
1040#ifdef CONFIG_PHYLIB
1041 fm_eth->enet_if = fman_read_sys_if(dev);
1042#else
1043 fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII;
1044 printf("%s: warning - unable to determine interface type\n", __func__);
1045#endif
1046 switch (fm_eth->mac_type) {
1047#ifndef CONFIG_SYS_FMAN_V3
1048 case FM_TGEC:
1049 fm_eth->type = FM_ETH_10G_E;
1050 break;
1051 case FM_DTSEC:
1052#else
1053 case FM_MEMAC:
1054 /* default to 1G, 10G is indicated by port property in dts */
1055#endif
1056 fm_eth->type = FM_ETH_1G_E;
1057 break;
1058 }
1059
1060 if (dev_read_u32(dev, "cell-index", &fm_eth->num)) {
1061 printf("FMan MAC node property cell-index missing\n");
1062 return -EINVAL;
1063 }
1064
1065 if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
1066 0, 0, &args))
1067 goto ports_ref_failure;
1068 index = ofnode_read_u32_default(args.node, "cell-index", 0);
1069 if (index <= 0)
1070 goto ports_ref_failure;
1071 fm_eth->rx_port = fman_port(dev->parent, index);
1072
1073 if (ofnode_read_bool(args.node, "fsl,fman-10g-port"))
1074 fm_eth->type = FM_ETH_10G_E;
1075
1076 if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL,
1077 0, 1, &args))
1078 goto ports_ref_failure;
1079 index = ofnode_read_u32_default(args.node, "cell-index", 0);
1080 if (index <= 0)
1081 goto ports_ref_failure;
1082 fm_eth->tx_port = fman_port(dev->parent, index);
1083
1084 /* set the ethernet max receive length */
1085 fm_eth->max_rx_len = MAX_RXBUF_LEN;
1086
1087 switch (fm_eth->enet_if) {
1088 case PHY_INTERFACE_MODE_QSGMII:
1089 /* all PCS blocks are accessed on one controller */
1090 if (fm_eth->num != 0)
1091 break;
1092 case PHY_INTERFACE_MODE_SGMII:
1093 case PHY_INTERFACE_MODE_SGMII_2500:
1094 fm_eth->pcs_mdio = fm_get_internal_mdio(dev);
1095 break;
1096 default:
1097 break;
1098 }
1099
1100 /* init global mac structure */
1101 ret = fm_eth_init_mac(fm_eth, reg);
1102 if (ret)
1103 return ret;
1104
1105 /* startup the FM im */
1106 ret = fm_eth_startup(fm_eth);
1107
1108 if (!ret)
1109 ret = init_phy(fm_eth);
1110
1111 return ret;
1112
1113ports_ref_failure:
1114 printf("Issue reading fsl,fman-ports for MAC %s\n", dev->name);
1115 return -ENOENT;
1116}
1117
1118static int fm_eth_remove(struct udevice *dev)
1119{
1120 return 0;
1121}
1122
1123static const struct eth_ops fm_eth_ops = {
1124 .start = fm_eth_open,
1125 .send = fm_eth_send,
1126 .recv = fm_eth_recv,
1127 .free_pkt = fm_eth_free_pkt,
1128 .stop = fm_eth_halt,
1129};
1130
1131static const struct udevice_id fm_eth_ids[] = {
1132#ifdef CONFIG_SYS_FMAN_V3
1133 { .compatible = "fsl,fman-memac", .data = FM_MEMAC },
1134#else
1135 { .compatible = "fsl,fman-dtsec", .data = FM_DTSEC },
1136 { .compatible = "fsl,fman-xgec", .data = FM_TGEC },
1137#endif
1138 {}
1139};
1140
1141U_BOOT_DRIVER(eth_fman) = {
1142 .name = "eth_fman",
1143 .id = UCLASS_ETH,
1144 .of_match = fm_eth_ids,
1145 .bind = fm_eth_bind,
1146 .probe = fm_eth_probe,
1147 .remove = fm_eth_remove,
1148 .ops = &fm_eth_ops,
Simon Glass41575d82020-12-03 16:55:17 -07001149 .priv_auto = sizeof(struct fm_eth),
Simon Glasscaa4daa2020-12-03 16:55:18 -07001150 .plat_auto = sizeof(struct eth_pdata),
Madalin Bucur6eb32a02020-04-23 16:25:19 +03001151 .flags = DM_FLAG_ALLOC_PRIV_DMA,
1152};
1153#endif /* CONFIG_DM_ETH */