blob: 7d13736b2dea87a11a783e540fe752829ece1016 [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.
Kumar Galac916d7c2011-04-13 08:37:44 -05004 * Dave Liu <daveliu@freescale.com>
Kumar Galac916d7c2011-04-13 08:37:44 -05005 */
6#include <common.h>
7#include <asm/io.h>
8#include <malloc.h>
9#include <net.h>
10#include <hwconfig.h>
11#include <fm_eth.h>
12#include <fsl_mdio.h>
13#include <miiphy.h>
14#include <phy.h>
Shaohui Xie8225b2f2015-10-26 19:47:47 +080015#include <fsl_dtsec.h>
16#include <fsl_tgec.h>
Shaohui Xiecd348ef2015-03-20 19:28:19 -070017#include <fsl_memac.h>
Kumar Galac916d7c2011-04-13 08:37:44 -050018
19#include "fm.h"
20
21static struct eth_device *devlist[NUM_FM_PORTS];
22static int num_controllers;
23
24#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII)
25
26#define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \
27 TBIANA_FULL_DUPLEX)
28
29#define TBIANA_SGMII_ACK 0x4001
30
31#define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \
32 TBICR_FULL_DUPLEX | TBICR_SPEED1_SET)
33
34/* Configure the TBI for SGMII operation */
Kim Phillips960d70c2012-10-29 13:34:34 +000035static void dtsec_configure_serdes(struct fm_eth *priv)
Kumar Galac916d7c2011-04-13 08:37:44 -050036{
Roy Zang111fd192012-10-08 07:44:21 +000037#ifdef CONFIG_SYS_FMAN_V3
38 u32 value;
39 struct mii_dev bus;
40 bus.priv = priv->mac->phyregs;
Shengzhou Liuc35f8692014-10-23 17:20:57 +080041 bool sgmii_2500 = (priv->enet_if ==
42 PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
Shaohui Xiebc246112015-10-26 19:47:48 +080043 int i = 0;
Roy Zang111fd192012-10-08 07:44:21 +000044
Shaohui Xiebc246112015-10-26 19:47:48 +080045qsgmii_loop:
Shengzhou Liuc35f8692014-10-23 17:20:57 +080046 /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
shaohui xiebead0882016-11-15 14:36:47 +080047 if (sgmii_2500)
48 value = PHY_SGMII_CR_PHY_RESET |
49 PHY_SGMII_IF_SPEED_GIGABIT |
50 PHY_SGMII_IF_MODE_SGMII;
51 else
52 value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN;
Shengzhou Liuc35f8692014-10-23 17:20:57 +080053
Shaohui Xiebc246112015-10-26 19:47:48 +080054 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
Roy Zang111fd192012-10-08 07:44:21 +000055
56 /* Dev ability according to SGMII specification */
57 value = PHY_SGMII_DEV_ABILITY_SGMII;
Shaohui Xiebc246112015-10-26 19:47:48 +080058 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
Roy Zang111fd192012-10-08 07:44:21 +000059
shaohui xiebead0882016-11-15 14:36:47 +080060 if (sgmii_2500) {
61 /* Adjust link timer for 2.5G SGMII,
62 * 1.6 ms in units of 3.2 ns:
63 * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120.
64 */
65 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007);
66 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120);
67 } else {
68 /* Adjust link timer for SGMII,
69 * 1.6 ms in units of 8 ns:
70 * 1.6ms / 8ns = 2 * 10^5 = 0x30d40.
71 */
72 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003);
73 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40);
74 }
Roy Zang111fd192012-10-08 07:44:21 +000075
76 /* Restart AN */
shaohui xiebead0882016-11-15 14:36:47 +080077 value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
Shaohui Xiebc246112015-10-26 19:47:48 +080078 memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value);
79
80 if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) {
81 i++;
82 goto qsgmii_loop;
83 }
Roy Zang111fd192012-10-08 07:44:21 +000084#else
Kumar Galac916d7c2011-04-13 08:37:44 -050085 struct dtsec *regs = priv->mac->base;
86 struct tsec_mii_mng *phyregs = priv->mac->phyregs;
87
88 /*
89 * Access TBI PHY registers at given TSEC register offset as
90 * opposed to the register offset used for external PHY accesses
91 */
92 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_TBICON,
93 TBICON_CLK_SELECT);
94 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0, TBI_ANA,
95 TBIANA_SGMII_ACK);
96 tsec_local_mdio_write(phyregs, in_be32(&regs->tbipa), 0,
97 TBI_CR, TBICR_SETTINGS);
Roy Zang111fd192012-10-08 07:44:21 +000098#endif
Kumar Galac916d7c2011-04-13 08:37:44 -050099}
100
Madalin Bucur8313cb02020-04-23 16:25:16 +0300101static void dtsec_init_phy(struct fm_eth *fm_eth)
Kumar Galac916d7c2011-04-13 08:37:44 -0500102{
Roy Zang111fd192012-10-08 07:44:21 +0000103#ifndef CONFIG_SYS_FMAN_V3
shaohui xie1f3bd3e2012-10-11 20:25:36 +0000104 struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
105
Kumar Galac916d7c2011-04-13 08:37:44 -0500106 /* Assign a Physical address to the TBI */
107 out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
Roy Zang111fd192012-10-08 07:44:21 +0000108#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500109
Shengzhou Liuc35f8692014-10-23 17:20:57 +0800110 if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
Shaohui Xiebc246112015-10-26 19:47:48 +0800111 fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
Shengzhou Liuc35f8692014-10-23 17:20:57 +0800112 fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
Kumar Galac916d7c2011-04-13 08:37:44 -0500113 dtsec_configure_serdes(fm_eth);
114}
115
Shaohui Xie29d8c812015-10-26 19:47:46 +0800116#ifdef CONFIG_PHYLIB
Madalin Bucur8313cb02020-04-23 16:25:16 +0300117static int tgec_is_fibre(struct fm_eth *fm)
Kumar Galac916d7c2011-04-13 08:37:44 -0500118{
Kumar Galac916d7c2011-04-13 08:37:44 -0500119 char phyopt[20];
120
121 sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1);
122
123 return hwconfig_arg_cmp(phyopt, "xfi");
124}
125#endif
Shaohui Xie29d8c812015-10-26 19:47:46 +0800126#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500127
128static u16 muram_readw(u16 *addr)
129{
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800130 ulong base = (ulong)addr & ~0x3UL;
131 u32 val32 = in_be32((void *)base);
Kumar Galac916d7c2011-04-13 08:37:44 -0500132 int byte_pos;
133 u16 ret;
134
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800135 byte_pos = (ulong)addr & 0x3UL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500136 if (byte_pos)
137 ret = (u16)(val32 & 0x0000ffff);
138 else
139 ret = (u16)((val32 & 0xffff0000) >> 16);
140
141 return ret;
142}
143
144static void muram_writew(u16 *addr, u16 val)
145{
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800146 ulong base = (ulong)addr & ~0x3UL;
147 u32 org32 = in_be32((void *)base);
Kumar Galac916d7c2011-04-13 08:37:44 -0500148 u32 val32;
149 int byte_pos;
150
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800151 byte_pos = (ulong)addr & 0x3UL;
Kumar Galac916d7c2011-04-13 08:37:44 -0500152 if (byte_pos)
153 val32 = (org32 & 0xffff0000) | val;
154 else
155 val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
156
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800157 out_be32((void *)base, val32);
Kumar Galac916d7c2011-04-13 08:37:44 -0500158}
159
160static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port)
161{
162 int timeout = 1000000;
163
164 clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN);
165
166 /* wait until the rx port is not busy */
167 while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--)
168 ;
169}
170
171static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port)
172{
173 /* set BMI to independent mode, Rx port disable */
174 out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM);
175 /* clear FOF in IM case */
176 out_be32(&rx_port->fmbm_rim, 0);
177 /* Rx frame next engine -RISC */
178 out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX);
179 /* Rx command attribute - no order, MR[3] = 1 */
180 clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK);
181 setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4));
182 /* enable Rx statistic counters */
183 out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN);
184 /* disable Rx performance counters */
185 out_be32(&rx_port->fmbm_rpc, 0);
186}
187
188static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port)
189{
190 int timeout = 1000000;
191
192 clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN);
193
194 /* wait until the tx port is not busy */
195 while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--)
196 ;
197}
198
199static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port)
200{
201 /* set BMI to independent mode, Tx port disable */
202 out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM);
203 /* Tx frame next engine -RISC */
204 out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
205 out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX);
206 /* Tx command attribute - no order, MR[3] = 1 */
207 clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK);
208 setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4));
209 /* enable Tx statistic counters */
210 out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN);
211 /* disable Tx performance counters */
212 out_be32(&tx_port->fmbm_tpc, 0);
213}
214
215static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth)
216{
217 struct fm_port_global_pram *pram;
218 u32 pram_page_offset;
219 void *rx_bd_ring_base;
220 void *rx_buf_pool;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800221 u32 bd_ring_base_lo, bd_ring_base_hi;
222 u32 buf_lo, buf_hi;
Kumar Galac916d7c2011-04-13 08:37:44 -0500223 struct fm_port_bd *rxbd;
224 struct fm_port_qd *rxqd;
225 struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port;
226 int i;
227
228 /* alloc global parameter ram at MURAM */
229 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
230 FM_PRAM_SIZE, FM_PRAM_ALIGN);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800231 if (!pram) {
232 printf("%s: No muram for Rx global parameter\n", __func__);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800233 return -ENOMEM;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800234 }
235
Kumar Galac916d7c2011-04-13 08:37:44 -0500236 fm_eth->rx_pram = pram;
237
238 /* parameter page offset to MURAM */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800239 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
Kumar Galac916d7c2011-04-13 08:37:44 -0500240
241 /* enable global mode- snooping data buffers and BDs */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800242 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
Kumar Galac916d7c2011-04-13 08:37:44 -0500243
244 /* init the Rx queue descriptor pionter */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800245 out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
Kumar Galac916d7c2011-04-13 08:37:44 -0500246
247 /* set the max receive buffer length, power of 2 */
248 muram_writew(&pram->mrblr, MAX_RXBUF_LOG2);
249
250 /* alloc Rx buffer descriptors from main memory */
251 rx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
252 * RX_BD_RING_SIZE);
253 if (!rx_bd_ring_base)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800254 return -ENOMEM;
255
Kumar Galac916d7c2011-04-13 08:37:44 -0500256 memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd)
257 * RX_BD_RING_SIZE);
258
259 /* alloc Rx buffer from main memory */
260 rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE);
261 if (!rx_buf_pool)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800262 return -ENOMEM;
263
Kumar Galac916d7c2011-04-13 08:37:44 -0500264 memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800265 debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
Kumar Galac916d7c2011-04-13 08:37:44 -0500266
267 /* save them to fm_eth */
268 fm_eth->rx_bd_ring = rx_bd_ring_base;
269 fm_eth->cur_rxbd = rx_bd_ring_base;
270 fm_eth->rx_buf = rx_buf_pool;
271
272 /* init Rx BDs ring */
273 rxbd = (struct fm_port_bd *)rx_bd_ring_base;
274 for (i = 0; i < RX_BD_RING_SIZE; i++) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800275 muram_writew(&rxbd->status, RxBD_EMPTY);
276 muram_writew(&rxbd->len, 0);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800277 buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool +
278 i * MAX_RXBUF_LEN));
279 buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool +
280 i * MAX_RXBUF_LEN));
281 muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi);
282 out_be32(&rxbd->buf_ptr_lo, buf_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500283 rxbd++;
284 }
285
286 /* set the Rx queue descriptor */
287 rxqd = &pram->rxqd;
288 muram_writew(&rxqd->gen, 0);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800289 bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base));
290 bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base));
291 muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
292 out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500293 muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd)
294 * RX_BD_RING_SIZE);
295 muram_writew(&rxqd->offset_in, 0);
296 muram_writew(&rxqd->offset_out, 0);
297
298 /* set IM parameter ram pointer to Rx Frame Queue ID */
299 out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset);
300
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800301 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500302}
303
304static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth)
305{
306 struct fm_port_global_pram *pram;
307 u32 pram_page_offset;
308 void *tx_bd_ring_base;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800309 u32 bd_ring_base_lo, bd_ring_base_hi;
Kumar Galac916d7c2011-04-13 08:37:44 -0500310 struct fm_port_bd *txbd;
311 struct fm_port_qd *txqd;
312 struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port;
313 int i;
314
315 /* alloc global parameter ram at MURAM */
316 pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index,
317 FM_PRAM_SIZE, FM_PRAM_ALIGN);
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800318 if (!pram) {
319 printf("%s: No muram for Tx global parameter\n", __func__);
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800320 return -ENOMEM;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800321 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500322 fm_eth->tx_pram = pram;
323
324 /* parameter page offset to MURAM */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800325 pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
Kumar Galac916d7c2011-04-13 08:37:44 -0500326
327 /* enable global mode- snooping data buffers and BDs */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800328 out_be32(&pram->mode, PRAM_MODE_GLOBAL);
Kumar Galac916d7c2011-04-13 08:37:44 -0500329
330 /* init the Tx queue descriptor pionter */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800331 out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
Kumar Galac916d7c2011-04-13 08:37:44 -0500332
333 /* alloc Tx buffer descriptors from main memory */
334 tx_bd_ring_base = malloc(sizeof(struct fm_port_bd)
335 * TX_BD_RING_SIZE);
336 if (!tx_bd_ring_base)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800337 return -ENOMEM;
338
Kumar Galac916d7c2011-04-13 08:37:44 -0500339 memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd)
340 * TX_BD_RING_SIZE);
341 /* save it to fm_eth */
342 fm_eth->tx_bd_ring = tx_bd_ring_base;
343 fm_eth->cur_txbd = tx_bd_ring_base;
344
345 /* init Tx BDs ring */
346 txbd = (struct fm_port_bd *)tx_bd_ring_base;
347 for (i = 0; i < TX_BD_RING_SIZE; i++) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800348 muram_writew(&txbd->status, TxBD_LAST);
349 muram_writew(&txbd->len, 0);
350 muram_writew(&txbd->buf_ptr_hi, 0);
351 out_be32(&txbd->buf_ptr_lo, 0);
352 txbd++;
Kumar Galac916d7c2011-04-13 08:37:44 -0500353 }
354
355 /* set the Tx queue decriptor */
356 txqd = &pram->txqd;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800357 bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base));
358 bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base));
359 muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi);
360 out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo);
Kumar Galac916d7c2011-04-13 08:37:44 -0500361 muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd)
362 * TX_BD_RING_SIZE);
363 muram_writew(&txqd->offset_in, 0);
364 muram_writew(&txqd->offset_out, 0);
365
366 /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */
367 out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset);
368
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800369 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500370}
371
372static int fm_eth_init(struct fm_eth *fm_eth)
373{
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800374 int ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500375
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800376 ret = fm_eth_rx_port_parameter_init(fm_eth);
377 if (ret)
378 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500379
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800380 ret = fm_eth_tx_port_parameter_init(fm_eth);
381 if (ret)
382 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500383
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800384 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500385}
386
387static int fm_eth_startup(struct fm_eth *fm_eth)
388{
389 struct fsl_enet_mac *mac;
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800390 int ret;
391
Kumar Galac916d7c2011-04-13 08:37:44 -0500392 mac = fm_eth->mac;
393
394 /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800395 ret = fm_eth_init(fm_eth);
396 if (ret)
397 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500398 /* setup the MAC controller */
399 mac->init_mac(mac);
400
401 /* For some reason we need to set SPEED_100 */
Shaohui Xie1c68d012013-08-19 18:58:52 +0800402 if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
shaohui xiebead0882016-11-15 14:36:47 +0800403 (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
Shaohui Xie1c68d012013-08-19 18:58:52 +0800404 (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
405 mac->set_if_mode)
Kumar Galac916d7c2011-04-13 08:37:44 -0500406 mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100);
407
408 /* init bmi rx port, IM mode and disable */
409 bmi_rx_port_init(fm_eth->rx_port);
410 /* init bmi tx port, IM mode and disable */
411 bmi_tx_port_init(fm_eth->tx_port);
412
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800413 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500414}
415
416static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth)
417{
418 struct fm_port_global_pram *pram;
419
420 pram = fm_eth->tx_pram;
421 /* graceful stop transmission of frames */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800422 setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
Kumar Galac916d7c2011-04-13 08:37:44 -0500423 sync();
424}
425
426static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth)
427{
428 struct fm_port_global_pram *pram;
429
430 pram = fm_eth->tx_pram;
431 /* re-enable transmission of frames */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800432 clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
Kumar Galac916d7c2011-04-13 08:37:44 -0500433 sync();
434}
435
436static int fm_eth_open(struct eth_device *dev, bd_t *bd)
437{
438 struct fm_eth *fm_eth;
439 struct fsl_enet_mac *mac;
Timur Tabi11af8d62012-07-09 08:52:43 +0000440#ifdef CONFIG_PHYLIB
441 int ret;
442#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500443
444 fm_eth = (struct fm_eth *)dev->priv;
445 mac = fm_eth->mac;
446
447 /* setup the MAC address */
448 if (dev->enetaddr[0] & 0x01) {
449 printf("%s: MacAddress is multcast address\n", __func__);
450 return 1;
451 }
452 mac->set_mac_addr(mac, dev->enetaddr);
453
454 /* enable bmi Rx port */
455 setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN);
456 /* enable MAC rx/tx port */
457 mac->enable_mac(mac);
458 /* enable bmi Tx port */
459 setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN);
460 /* re-enable transmission of frame */
461 fmc_tx_port_graceful_stop_disable(fm_eth);
462
463#ifdef CONFIG_PHYLIB
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200464 if (fm_eth->phydev) {
465 ret = phy_startup(fm_eth->phydev);
466 if (ret) {
467 printf("%s: Could not initialize\n",
468 fm_eth->phydev->dev->name);
469 return ret;
470 }
471 } else {
472 return 0;
Timur Tabi11af8d62012-07-09 08:52:43 +0000473 }
Kumar Galac916d7c2011-04-13 08:37:44 -0500474#else
475 fm_eth->phydev->speed = SPEED_1000;
476 fm_eth->phydev->link = 1;
477 fm_eth->phydev->duplex = DUPLEX_FULL;
478#endif
479
480 /* set the MAC-PHY mode */
481 mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed);
482
483 if (!fm_eth->phydev->link)
484 printf("%s: No link.\n", fm_eth->phydev->dev->name);
485
486 return fm_eth->phydev->link ? 0 : -1;
487}
488
489static void fm_eth_halt(struct eth_device *dev)
490{
491 struct fm_eth *fm_eth;
492 struct fsl_enet_mac *mac;
493
494 fm_eth = (struct fm_eth *)dev->priv;
495 mac = fm_eth->mac;
496
497 /* graceful stop the transmission of frames */
498 fmc_tx_port_graceful_stop_enable(fm_eth);
499 /* disable bmi Tx port */
500 bmi_tx_port_disable(fm_eth->tx_port);
501 /* disable MAC rx/tx port */
502 mac->disable_mac(mac);
503 /* disable bmi Rx port */
504 bmi_rx_port_disable(fm_eth->rx_port);
505
Shaohui Xie29d8c812015-10-26 19:47:46 +0800506#ifdef CONFIG_PHYLIB
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200507 if (fm_eth->phydev)
508 phy_shutdown(fm_eth->phydev);
Shaohui Xie29d8c812015-10-26 19:47:46 +0800509#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500510}
511
Joe Hershbergere9df2012012-05-22 07:56:15 +0000512static int fm_eth_send(struct eth_device *dev, void *buf, int len)
Kumar Galac916d7c2011-04-13 08:37:44 -0500513{
514 struct fm_eth *fm_eth;
515 struct fm_port_global_pram *pram;
516 struct fm_port_bd *txbd, *txbd_base;
517 u16 offset_in;
518 int i;
519
520 fm_eth = (struct fm_eth *)dev->priv;
521 pram = fm_eth->tx_pram;
522 txbd = fm_eth->cur_txbd;
523
524 /* find one empty TxBD */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800525 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500526 udelay(100);
527 if (i > 0x1000) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800528 printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
529 dev->name, muram_readw(&txbd->status));
Kumar Galac916d7c2011-04-13 08:37:44 -0500530 return 0;
531 }
532 }
533 /* setup TxBD */
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800534 muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf)));
535 out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf)));
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800536 muram_writew(&txbd->len, len);
Kumar Galac916d7c2011-04-13 08:37:44 -0500537 sync();
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800538 muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
Kumar Galac916d7c2011-04-13 08:37:44 -0500539 sync();
540
541 /* update TxQD, let RISC to send the packet */
542 offset_in = muram_readw(&pram->txqd.offset_in);
543 offset_in += sizeof(struct fm_port_bd);
544 if (offset_in >= muram_readw(&pram->txqd.bd_ring_size))
545 offset_in = 0;
546 muram_writew(&pram->txqd.offset_in, offset_in);
547 sync();
548
549 /* wait for buffer to be transmitted */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800550 for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
Kumar Galac916d7c2011-04-13 08:37:44 -0500551 udelay(100);
552 if (i > 0x10000) {
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800553 printf("%s: Tx error, txbd->status = 0x%x\n",
554 dev->name, muram_readw(&txbd->status));
Kumar Galac916d7c2011-04-13 08:37:44 -0500555 return 0;
556 }
557 }
558
559 /* advance the TxBD */
560 txbd++;
561 txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring;
562 if (txbd >= (txbd_base + TX_BD_RING_SIZE))
563 txbd = txbd_base;
564 /* update current txbd */
565 fm_eth->cur_txbd = (void *)txbd;
566
567 return 1;
568}
569
Madalin Bucur1c710182020-04-23 16:25:17 +0300570static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth,
571 struct fm_port_bd *rxbd)
572{
573 struct fm_port_global_pram *pram;
574 struct fm_port_bd *rxbd_base;
575 u16 offset_out;
576
577 pram = fm_eth->rx_pram;
578
579 /* clear the RxBDs */
580 muram_writew(&rxbd->status, RxBD_EMPTY);
581 muram_writew(&rxbd->len, 0);
582 sync();
583
584 /* advance RxBD */
585 rxbd++;
586 rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
587 if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
588 rxbd = rxbd_base;
589
590 /* update RxQD */
591 offset_out = muram_readw(&pram->rxqd.offset_out);
592 offset_out += sizeof(struct fm_port_bd);
593 if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
594 offset_out = 0;
595 muram_writew(&pram->rxqd.offset_out, offset_out);
596 sync();
597
598 return rxbd;
599}
600
Kumar Galac916d7c2011-04-13 08:37:44 -0500601static int fm_eth_recv(struct eth_device *dev)
602{
Madalin Bucur1c710182020-04-23 16:25:17 +0300603 struct fm_eth *fm_eth = (struct fm_eth *)dev->priv;
604 struct fm_port_bd *rxbd = fm_eth->cur_rxbd;
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800605 u32 buf_lo, buf_hi;
Madalin Bucur1c710182020-04-23 16:25:17 +0300606 u16 status, len;
607 int ret = -1;
Kumar Galac916d7c2011-04-13 08:37:44 -0500608 u8 *data;
Kumar Galac916d7c2011-04-13 08:37:44 -0500609
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800610 status = muram_readw(&rxbd->status);
Kumar Galac916d7c2011-04-13 08:37:44 -0500611
612 while (!(status & RxBD_EMPTY)) {
613 if (!(status & RxBD_ERROR)) {
Hou Zhiqiang9fc29db2015-10-26 19:47:44 +0800614 buf_hi = muram_readw(&rxbd->buf_ptr_hi);
615 buf_lo = in_be32(&rxbd->buf_ptr_lo);
616 data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800617 len = muram_readw(&rxbd->len);
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500618 net_process_received_packet(data, len);
Kumar Galac916d7c2011-04-13 08:37:44 -0500619 } else {
620 printf("%s: Rx error\n", dev->name);
Daniel Inderbitzin466f7752015-07-10 14:06:02 +0200621 ret = 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500622 }
623
Madalin Bucur1c710182020-04-23 16:25:17 +0300624 /* free current bd, advance to next one */
625 rxbd = fm_eth_free_one(fm_eth, rxbd);
Kumar Galac916d7c2011-04-13 08:37:44 -0500626
Kumar Galac916d7c2011-04-13 08:37:44 -0500627 /* read next status */
Hou Zhiqiang648bde62015-10-26 19:47:43 +0800628 status = muram_readw(&rxbd->status);
Kumar Galac916d7c2011-04-13 08:37:44 -0500629 }
630 fm_eth->cur_rxbd = (void *)rxbd;
631
Daniel Inderbitzin466f7752015-07-10 14:06:02 +0200632 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500633}
634
635static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
636{
637 struct fsl_enet_mac *mac;
638 int num;
639 void *base, *phyregs = NULL;
640
641 num = fm_eth->num;
642
Roy Zang111fd192012-10-08 07:44:21 +0000643#ifdef CONFIG_SYS_FMAN_V3
Shengzhou Liucc19c252014-11-24 17:11:57 +0800644#ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800645 if (fm_eth->type == FM_ETH_10G_E) {
Shengzhou Liucc19c252014-11-24 17:11:57 +0800646 /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
647 * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
648 * 10GEC1 uses mEMAC1 on T1024.
Shengzhou Liu82a55c12013-11-22 17:39:09 +0800649 * so it needs to change the num.
650 */
651 if (fm_eth->num >= 2)
652 num -= 2;
653 else
654 num += 8;
655 }
Shengzhou Liucc19c252014-11-24 17:11:57 +0800656#endif
Roy Zang111fd192012-10-08 07:44:21 +0000657 base = &reg->memac[num].fm_memac;
658 phyregs = &reg->memac[num].fm_memac_mdio;
659#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500660 /* Get the mac registers base address */
661 if (fm_eth->type == FM_ETH_1G_E) {
662 base = &reg->mac_1g[num].fm_dtesc;
Timur Tabi30381712011-10-04 16:44:43 -0500663 phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
Kumar Galac916d7c2011-04-13 08:37:44 -0500664 } else {
665 base = &reg->mac_10g[num].fm_10gec;
666 phyregs = &reg->mac_10g[num].fm_10gec_mdio;
667 }
Roy Zang111fd192012-10-08 07:44:21 +0000668#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500669
670 /* alloc mac controller */
671 mac = malloc(sizeof(struct fsl_enet_mac));
672 if (!mac)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800673 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500674 memset(mac, 0, sizeof(struct fsl_enet_mac));
675
676 /* save the mac to fm_eth struct */
677 fm_eth->mac = mac;
678
Roy Zang111fd192012-10-08 07:44:21 +0000679#ifdef CONFIG_SYS_FMAN_V3
680 init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
681#else
Kumar Galac916d7c2011-04-13 08:37:44 -0500682 if (fm_eth->type == FM_ETH_1G_E)
Timur Tabi30381712011-10-04 16:44:43 -0500683 init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
Kumar Galac916d7c2011-04-13 08:37:44 -0500684 else
685 init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
Roy Zang111fd192012-10-08 07:44:21 +0000686#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500687
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800688 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500689}
690
Madalin Bucur8313cb02020-04-23 16:25:16 +0300691static int init_phy(struct fm_eth *fm_eth)
Kumar Galac916d7c2011-04-13 08:37:44 -0500692{
Shaohui Xie29d8c812015-10-26 19:47:46 +0800693#ifdef CONFIG_PHYLIB
Kumar Galac916d7c2011-04-13 08:37:44 -0500694 struct phy_device *phydev = NULL;
695 u32 supported;
Shaohui Xie29d8c812015-10-26 19:47:46 +0800696#endif
Kumar Galac916d7c2011-04-13 08:37:44 -0500697
Kumar Galac916d7c2011-04-13 08:37:44 -0500698 if (fm_eth->type == FM_ETH_1G_E)
Madalin Bucur8313cb02020-04-23 16:25:16 +0300699 dtsec_init_phy(fm_eth);
Kumar Galac916d7c2011-04-13 08:37:44 -0500700
Shaohui Xie29d8c812015-10-26 19:47:46 +0800701#ifdef CONFIG_PHYLIB
Kumar Galac916d7c2011-04-13 08:37:44 -0500702 if (fm_eth->bus) {
Madalin Bucur8313cb02020-04-23 16:25:16 +0300703 phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, fm_eth->dev,
704 fm_eth->enet_if);
Codrin Ciubotariu6798c322015-01-12 14:08:29 +0200705 if (!phydev) {
706 printf("Failed to connect\n");
707 return -1;
708 }
709 } else {
710 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500711 }
712
713 if (fm_eth->type == FM_ETH_1G_E) {
714 supported = (SUPPORTED_10baseT_Half |
715 SUPPORTED_10baseT_Full |
716 SUPPORTED_100baseT_Half |
717 SUPPORTED_100baseT_Full |
718 SUPPORTED_1000baseT_Full);
719 } else {
720 supported = SUPPORTED_10000baseT_Full;
721
Madalin Bucur8313cb02020-04-23 16:25:16 +0300722 if (tgec_is_fibre(fm_eth))
Kumar Galac916d7c2011-04-13 08:37:44 -0500723 phydev->port = PORT_FIBRE;
724 }
725
726 phydev->supported &= supported;
727 phydev->advertising = phydev->supported;
728
729 fm_eth->phydev = phydev;
730
731 phy_config(phydev);
732#endif
733
734 return 0;
735}
736
737int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
738{
739 struct eth_device *dev;
740 struct fm_eth *fm_eth;
741 int i, num = info->num;
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800742 int ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500743
744 /* alloc eth device */
745 dev = (struct eth_device *)malloc(sizeof(struct eth_device));
746 if (!dev)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800747 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500748 memset(dev, 0, sizeof(struct eth_device));
749
750 /* alloc the FMan ethernet private struct */
751 fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
752 if (!fm_eth)
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800753 return -ENOMEM;
Kumar Galac916d7c2011-04-13 08:37:44 -0500754 memset(fm_eth, 0, sizeof(struct fm_eth));
755
756 /* save off some things we need from the info struct */
757 fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
758 fm_eth->num = num;
759 fm_eth->type = info->type;
760
761 fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
762 fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
763
764 /* set the ethernet max receive length */
765 fm_eth->max_rx_len = MAX_RXBUF_LEN;
766
767 /* init global mac structure */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800768 ret = fm_eth_init_mac(fm_eth, reg);
769 if (ret)
770 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500771
772 /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
773 if (fm_eth->type == FM_ETH_1G_E)
774 sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
775 else
776 sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
777
778 devlist[num_controllers++] = dev;
779 dev->iobase = 0;
780 dev->priv = (void *)fm_eth;
781 dev->init = fm_eth_open;
782 dev->halt = fm_eth_halt;
783 dev->send = fm_eth_send;
784 dev->recv = fm_eth_recv;
785 fm_eth->dev = dev;
786 fm_eth->bus = info->bus;
787 fm_eth->phyaddr = info->phy_addr;
788 fm_eth->enet_if = info->enet_if;
789
790 /* startup the FM im */
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800791 ret = fm_eth_startup(fm_eth);
792 if (ret)
793 return ret;
Kumar Galac916d7c2011-04-13 08:37:44 -0500794
Madalin Bucur8313cb02020-04-23 16:25:16 +0300795 init_phy(fm_eth);
Kumar Galac916d7c2011-04-13 08:37:44 -0500796
797 /* clear the ethernet address */
798 for (i = 0; i < 6; i++)
799 dev->enetaddr[i] = 0;
800 eth_register(dev);
801
Hou Zhiqiang0f2cb9f2015-10-26 19:47:45 +0800802 return 0;
Kumar Galac916d7c2011-04-13 08:37:44 -0500803}