driver: net: fm: change init_phy() param
[oweals/u-boot.git] / drivers / net / fm / eth.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2009-2012 Freescale Semiconductor, Inc.
4  *      Dave Liu <daveliu@freescale.com>
5  */
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>
15 #include <fsl_dtsec.h>
16 #include <fsl_tgec.h>
17 #include <fsl_memac.h>
18
19 #include "fm.h"
20
21 static struct eth_device *devlist[NUM_FM_PORTS];
22 static 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 */
35 static void dtsec_configure_serdes(struct fm_eth *priv)
36 {
37 #ifdef CONFIG_SYS_FMAN_V3
38         u32 value;
39         struct mii_dev bus;
40         bus.priv = priv->mac->phyregs;
41         bool sgmii_2500 = (priv->enet_if ==
42                         PHY_INTERFACE_MODE_SGMII_2500) ? true : false;
43         int i = 0;
44
45 qsgmii_loop:
46         /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */
47         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;
53
54         memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value);
55
56         /* Dev ability according to SGMII specification */
57         value = PHY_SGMII_DEV_ABILITY_SGMII;
58         memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value);
59
60         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         }
75
76         /* Restart AN */
77         value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN;
78         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         }
84 #else
85         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);
98 #endif
99 }
100
101 static void dtsec_init_phy(struct fm_eth *fm_eth)
102 {
103 #ifndef CONFIG_SYS_FMAN_V3
104         struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR;
105
106         /* Assign a Physical address to the TBI */
107         out_be32(&regs->tbipa, CONFIG_SYS_TBIPA_VALUE);
108 #endif
109
110         if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII ||
111             fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII ||
112             fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500)
113                 dtsec_configure_serdes(fm_eth);
114 }
115
116 #ifdef CONFIG_PHYLIB
117 static int tgec_is_fibre(struct fm_eth *fm)
118 {
119         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
126 #endif
127
128 static u16 muram_readw(u16 *addr)
129 {
130         ulong base = (ulong)addr & ~0x3UL;
131         u32 val32 = in_be32((void *)base);
132         int byte_pos;
133         u16 ret;
134
135         byte_pos = (ulong)addr & 0x3UL;
136         if (byte_pos)
137                 ret = (u16)(val32 & 0x0000ffff);
138         else
139                 ret = (u16)((val32 & 0xffff0000) >> 16);
140
141         return ret;
142 }
143
144 static void muram_writew(u16 *addr, u16 val)
145 {
146         ulong base = (ulong)addr & ~0x3UL;
147         u32 org32 = in_be32((void *)base);
148         u32 val32;
149         int byte_pos;
150
151         byte_pos = (ulong)addr & 0x3UL;
152         if (byte_pos)
153                 val32 = (org32 & 0xffff0000) | val;
154         else
155                 val32 = (org32 & 0x0000ffff) | ((u32)val << 16);
156
157         out_be32((void *)base, val32);
158 }
159
160 static 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
171 static 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
188 static 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
199 static 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
215 static 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;
221         u32 bd_ring_base_lo, bd_ring_base_hi;
222         u32 buf_lo, buf_hi;
223         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);
231         if (!pram) {
232                 printf("%s: No muram for Rx global parameter\n", __func__);
233                 return -ENOMEM;
234         }
235
236         fm_eth->rx_pram = pram;
237
238         /* parameter page offset to MURAM */
239         pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
240
241         /* enable global mode- snooping data buffers and BDs */
242         out_be32(&pram->mode, PRAM_MODE_GLOBAL);
243
244         /* init the Rx queue descriptor pionter */
245         out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20);
246
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)
254                 return -ENOMEM;
255
256         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)
262                 return -ENOMEM;
263
264         memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE);
265         debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool);
266
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++) {
275                 muram_writew(&rxbd->status, RxBD_EMPTY);
276                 muram_writew(&rxbd->len, 0);
277                 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);
283                 rxbd++;
284         }
285
286         /* set the Rx queue descriptor */
287         rxqd = &pram->rxqd;
288         muram_writew(&rxqd->gen, 0);
289         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);
293         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
301         return 0;
302 }
303
304 static 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;
309         u32 bd_ring_base_lo, bd_ring_base_hi;
310         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);
318         if (!pram) {
319                 printf("%s: No muram for Tx global parameter\n", __func__);
320                 return -ENOMEM;
321         }
322         fm_eth->tx_pram = pram;
323
324         /* parameter page offset to MURAM */
325         pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index);
326
327         /* enable global mode- snooping data buffers and BDs */
328         out_be32(&pram->mode, PRAM_MODE_GLOBAL);
329
330         /* init the Tx queue descriptor pionter */
331         out_be32(&pram->txqd_ptr, pram_page_offset + 0x40);
332
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)
337                 return -ENOMEM;
338
339         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++) {
348                 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++;
353         }
354
355         /* set the Tx queue decriptor */
356         txqd = &pram->txqd;
357         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);
361         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
369         return 0;
370 }
371
372 static int fm_eth_init(struct fm_eth *fm_eth)
373 {
374         int ret;
375
376         ret = fm_eth_rx_port_parameter_init(fm_eth);
377         if (ret)
378                 return ret;
379
380         ret = fm_eth_tx_port_parameter_init(fm_eth);
381         if (ret)
382                 return ret;
383
384         return 0;
385 }
386
387 static int fm_eth_startup(struct fm_eth *fm_eth)
388 {
389         struct fsl_enet_mac *mac;
390         int ret;
391
392         mac = fm_eth->mac;
393
394         /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */
395         ret = fm_eth_init(fm_eth);
396         if (ret)
397                 return ret;
398         /* setup the MAC controller */
399         mac->init_mac(mac);
400
401         /* For some reason we need to set SPEED_100 */
402         if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) ||
403              (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) ||
404              (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) &&
405               mac->set_if_mode)
406                 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
413         return 0;
414 }
415
416 static 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 */
422         setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
423         sync();
424 }
425
426 static 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 */
432         clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP);
433         sync();
434 }
435
436 static int fm_eth_open(struct eth_device *dev, bd_t *bd)
437 {
438         struct fm_eth *fm_eth;
439         struct fsl_enet_mac *mac;
440 #ifdef CONFIG_PHYLIB
441         int ret;
442 #endif
443
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
464         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;
473         }
474 #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
489 static 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
506 #ifdef CONFIG_PHYLIB
507         if (fm_eth->phydev)
508                 phy_shutdown(fm_eth->phydev);
509 #endif
510 }
511
512 static int fm_eth_send(struct eth_device *dev, void *buf, int len)
513 {
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 */
525         for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
526                 udelay(100);
527                 if (i > 0x1000) {
528                         printf("%s: Tx buffer not ready, txbd->status = 0x%x\n",
529                                dev->name, muram_readw(&txbd->status));
530                         return 0;
531                 }
532         }
533         /* setup TxBD */
534         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)));
536         muram_writew(&txbd->len, len);
537         sync();
538         muram_writew(&txbd->status, TxBD_READY | TxBD_LAST);
539         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 */
550         for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) {
551                 udelay(100);
552                 if (i > 0x10000) {
553                         printf("%s: Tx error, txbd->status = 0x%x\n",
554                                dev->name, muram_readw(&txbd->status));
555                         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
570 static int fm_eth_recv(struct eth_device *dev)
571 {
572         struct fm_eth *fm_eth;
573         struct fm_port_global_pram *pram;
574         struct fm_port_bd *rxbd, *rxbd_base;
575         u16 status, len;
576         u32 buf_lo, buf_hi;
577         u8 *data;
578         u16 offset_out;
579         int ret = 1;
580
581         fm_eth = (struct fm_eth *)dev->priv;
582         pram = fm_eth->rx_pram;
583         rxbd = fm_eth->cur_rxbd;
584         status = muram_readw(&rxbd->status);
585
586         while (!(status & RxBD_EMPTY)) {
587                 if (!(status & RxBD_ERROR)) {
588                         buf_hi = muram_readw(&rxbd->buf_ptr_hi);
589                         buf_lo = in_be32(&rxbd->buf_ptr_lo);
590                         data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo);
591                         len = muram_readw(&rxbd->len);
592                         net_process_received_packet(data, len);
593                 } else {
594                         printf("%s: Rx error\n", dev->name);
595                         ret = 0;
596                 }
597
598                 /* clear the RxBDs */
599                 muram_writew(&rxbd->status, RxBD_EMPTY);
600                 muram_writew(&rxbd->len, 0);
601                 sync();
602
603                 /* advance RxBD */
604                 rxbd++;
605                 rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring;
606                 if (rxbd >= (rxbd_base + RX_BD_RING_SIZE))
607                         rxbd = rxbd_base;
608                 /* read next status */
609                 status = muram_readw(&rxbd->status);
610
611                 /* update RxQD */
612                 offset_out = muram_readw(&pram->rxqd.offset_out);
613                 offset_out += sizeof(struct fm_port_bd);
614                 if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size))
615                         offset_out = 0;
616                 muram_writew(&pram->rxqd.offset_out, offset_out);
617                 sync();
618         }
619         fm_eth->cur_rxbd = (void *)rxbd;
620
621         return ret;
622 }
623
624 static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg)
625 {
626         struct fsl_enet_mac *mac;
627         int num;
628         void *base, *phyregs = NULL;
629
630         num = fm_eth->num;
631
632 #ifdef CONFIG_SYS_FMAN_V3
633 #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION
634         if (fm_eth->type == FM_ETH_10G_E) {
635                 /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240.
636                  * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080.
637                  * 10GEC1 uses mEMAC1 on T1024.
638                  * so it needs to change the num.
639                  */
640                 if (fm_eth->num >= 2)
641                         num -= 2;
642                 else
643                         num += 8;
644         }
645 #endif
646         base = &reg->memac[num].fm_memac;
647         phyregs = &reg->memac[num].fm_memac_mdio;
648 #else
649         /* Get the mac registers base address */
650         if (fm_eth->type == FM_ETH_1G_E) {
651                 base = &reg->mac_1g[num].fm_dtesc;
652                 phyregs = &reg->mac_1g[num].fm_mdio.miimcfg;
653         } else {
654                 base = &reg->mac_10g[num].fm_10gec;
655                 phyregs = &reg->mac_10g[num].fm_10gec_mdio;
656         }
657 #endif
658
659         /* alloc mac controller */
660         mac = malloc(sizeof(struct fsl_enet_mac));
661         if (!mac)
662                 return -ENOMEM;
663         memset(mac, 0, sizeof(struct fsl_enet_mac));
664
665         /* save the mac to fm_eth struct */
666         fm_eth->mac = mac;
667
668 #ifdef CONFIG_SYS_FMAN_V3
669         init_memac(mac, base, phyregs, MAX_RXBUF_LEN);
670 #else
671         if (fm_eth->type == FM_ETH_1G_E)
672                 init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN);
673         else
674                 init_tgec(mac, base, phyregs, MAX_RXBUF_LEN);
675 #endif
676
677         return 0;
678 }
679
680 static int init_phy(struct fm_eth *fm_eth)
681 {
682 #ifdef CONFIG_PHYLIB
683         struct phy_device *phydev = NULL;
684         u32 supported;
685 #endif
686
687         if (fm_eth->type == FM_ETH_1G_E)
688                 dtsec_init_phy(fm_eth);
689
690 #ifdef CONFIG_PHYLIB
691         if (fm_eth->bus) {
692                 phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, fm_eth->dev,
693                                      fm_eth->enet_if);
694                 if (!phydev) {
695                         printf("Failed to connect\n");
696                         return -1;
697                 }
698         } else {
699                 return 0;
700         }
701
702         if (fm_eth->type == FM_ETH_1G_E) {
703                 supported = (SUPPORTED_10baseT_Half |
704                                 SUPPORTED_10baseT_Full |
705                                 SUPPORTED_100baseT_Half |
706                                 SUPPORTED_100baseT_Full |
707                                 SUPPORTED_1000baseT_Full);
708         } else {
709                 supported = SUPPORTED_10000baseT_Full;
710
711                 if (tgec_is_fibre(fm_eth))
712                         phydev->port = PORT_FIBRE;
713         }
714
715         phydev->supported &= supported;
716         phydev->advertising = phydev->supported;
717
718         fm_eth->phydev = phydev;
719
720         phy_config(phydev);
721 #endif
722
723         return 0;
724 }
725
726 int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info)
727 {
728         struct eth_device *dev;
729         struct fm_eth *fm_eth;
730         int i, num = info->num;
731         int ret;
732
733         /* alloc eth device */
734         dev = (struct eth_device *)malloc(sizeof(struct eth_device));
735         if (!dev)
736                 return -ENOMEM;
737         memset(dev, 0, sizeof(struct eth_device));
738
739         /* alloc the FMan ethernet private struct */
740         fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth));
741         if (!fm_eth)
742                 return -ENOMEM;
743         memset(fm_eth, 0, sizeof(struct fm_eth));
744
745         /* save off some things we need from the info struct */
746         fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */
747         fm_eth->num = num;
748         fm_eth->type = info->type;
749
750         fm_eth->rx_port = (void *)&reg->port[info->rx_port_id - 1].fm_bmi;
751         fm_eth->tx_port = (void *)&reg->port[info->tx_port_id - 1].fm_bmi;
752
753         /* set the ethernet max receive length */
754         fm_eth->max_rx_len = MAX_RXBUF_LEN;
755
756         /* init global mac structure */
757         ret = fm_eth_init_mac(fm_eth, reg);
758         if (ret)
759                 return ret;
760
761         /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */
762         if (fm_eth->type == FM_ETH_1G_E)
763                 sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1);
764         else
765                 sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1);
766
767         devlist[num_controllers++] = dev;
768         dev->iobase = 0;
769         dev->priv = (void *)fm_eth;
770         dev->init = fm_eth_open;
771         dev->halt = fm_eth_halt;
772         dev->send = fm_eth_send;
773         dev->recv = fm_eth_recv;
774         fm_eth->dev = dev;
775         fm_eth->bus = info->bus;
776         fm_eth->phyaddr = info->phy_addr;
777         fm_eth->enet_if = info->enet_if;
778
779         /* startup the FM im */
780         ret = fm_eth_startup(fm_eth);
781         if (ret)
782                 return ret;
783
784         init_phy(fm_eth);
785
786         /* clear the ethernet address */
787         for (i = 0; i < 6; i++)
788                 dev->enetaddr[i] = 0;
789         eth_register(dev);
790
791         return 0;
792 }