Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 1 | /* |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 2 | * Copyright 2009-2012 Freescale Semiconductor, Inc. |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 3 | * Dave Liu <daveliu@freescale.com> |
| 4 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <malloc.h> |
| 10 | #include <net.h> |
| 11 | #include <hwconfig.h> |
| 12 | #include <fm_eth.h> |
| 13 | #include <fsl_mdio.h> |
| 14 | #include <miiphy.h> |
| 15 | #include <phy.h> |
Shaohui Xie | 8225b2f | 2015-10-26 19:47:47 +0800 | [diff] [blame^] | 16 | #include <fsl_dtsec.h> |
| 17 | #include <fsl_tgec.h> |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 18 | #include <fsl_memac.h> |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 19 | |
| 20 | #include "fm.h" |
| 21 | |
| 22 | static struct eth_device *devlist[NUM_FM_PORTS]; |
| 23 | static int num_controllers; |
| 24 | |
| 25 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII) |
| 26 | |
| 27 | #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \ |
| 28 | TBIANA_FULL_DUPLEX) |
| 29 | |
| 30 | #define TBIANA_SGMII_ACK 0x4001 |
| 31 | |
| 32 | #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \ |
| 33 | TBICR_FULL_DUPLEX | TBICR_SPEED1_SET) |
| 34 | |
| 35 | /* Configure the TBI for SGMII operation */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 36 | static void dtsec_configure_serdes(struct fm_eth *priv) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 37 | { |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 38 | #ifdef CONFIG_SYS_FMAN_V3 |
| 39 | u32 value; |
| 40 | struct mii_dev bus; |
| 41 | bus.priv = priv->mac->phyregs; |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 42 | bool sgmii_2500 = (priv->enet_if == |
| 43 | PHY_INTERFACE_MODE_SGMII_2500) ? true : false; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 44 | |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 45 | /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */ |
| 46 | value = PHY_SGMII_IF_MODE_SGMII; |
| 47 | if (!sgmii_2500) |
| 48 | value |= PHY_SGMII_IF_MODE_AN; |
| 49 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 50 | memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x14, value); |
| 51 | |
| 52 | /* Dev ability according to SGMII specification */ |
| 53 | value = PHY_SGMII_DEV_ABILITY_SGMII; |
| 54 | memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x4, value); |
| 55 | |
| 56 | /* Adjust link timer for SGMII - |
| 57 | 1.6 ms in units of 8 ns = 2 * 10^5 = 0x30d40 */ |
| 58 | memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x13, 0x3); |
| 59 | memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0x12, 0xd40); |
| 60 | |
| 61 | /* Restart AN */ |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 62 | value = PHY_SGMII_CR_DEF_VAL; |
| 63 | if (!sgmii_2500) |
| 64 | value |= PHY_SGMII_CR_RESET_AN; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 65 | memac_mdio_write(&bus, 0, MDIO_DEVAD_NONE, 0, value); |
| 66 | #else |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 67 | struct dtsec *regs = priv->mac->base; |
| 68 | struct tsec_mii_mng *phyregs = priv->mac->phyregs; |
| 69 | |
| 70 | /* |
| 71 | * Access TBI PHY registers at given TSEC register offset as |
| 72 | * opposed to the register offset used for external PHY accesses |
| 73 | */ |
| 74 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_TBICON, |
| 75 | TBICON_CLK_SELECT); |
| 76 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_ANA, |
| 77 | TBIANA_SGMII_ACK); |
| 78 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, |
| 79 | TBI_CR, TBICR_SETTINGS); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 80 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void dtsec_init_phy(struct eth_device *dev) |
| 84 | { |
| 85 | struct fm_eth *fm_eth = dev->priv; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 86 | #ifndef CONFIG_SYS_FMAN_V3 |
shaohui xie | 1f3bd3e | 2012-10-11 20:25:36 +0000 | [diff] [blame] | 87 | struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR; |
| 88 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 89 | /* Assign a Physical address to the TBI */ |
| 90 | out_be32(®s->tbipa, CONFIG_SYS_TBIPA_VALUE); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 91 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 92 | |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 93 | if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII || |
| 94 | fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 95 | dtsec_configure_serdes(fm_eth); |
| 96 | } |
| 97 | |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 98 | #ifdef CONFIG_PHYLIB |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 99 | static int tgec_is_fibre(struct eth_device *dev) |
| 100 | { |
| 101 | struct fm_eth *fm = dev->priv; |
| 102 | char phyopt[20]; |
| 103 | |
| 104 | sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1); |
| 105 | |
| 106 | return hwconfig_arg_cmp(phyopt, "xfi"); |
| 107 | } |
| 108 | #endif |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 109 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 110 | |
| 111 | static u16 muram_readw(u16 *addr) |
| 112 | { |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 113 | ulong base = (ulong)addr & ~0x3UL; |
| 114 | u32 val32 = in_be32((void *)base); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 115 | int byte_pos; |
| 116 | u16 ret; |
| 117 | |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 118 | byte_pos = (ulong)addr & 0x3UL; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 119 | if (byte_pos) |
| 120 | ret = (u16)(val32 & 0x0000ffff); |
| 121 | else |
| 122 | ret = (u16)((val32 & 0xffff0000) >> 16); |
| 123 | |
| 124 | return ret; |
| 125 | } |
| 126 | |
| 127 | static void muram_writew(u16 *addr, u16 val) |
| 128 | { |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 129 | ulong base = (ulong)addr & ~0x3UL; |
| 130 | u32 org32 = in_be32((void *)base); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 131 | u32 val32; |
| 132 | int byte_pos; |
| 133 | |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 134 | byte_pos = (ulong)addr & 0x3UL; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 135 | if (byte_pos) |
| 136 | val32 = (org32 & 0xffff0000) | val; |
| 137 | else |
| 138 | val32 = (org32 & 0x0000ffff) | ((u32)val << 16); |
| 139 | |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 140 | out_be32((void *)base, val32); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port) |
| 144 | { |
| 145 | int timeout = 1000000; |
| 146 | |
| 147 | clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN); |
| 148 | |
| 149 | /* wait until the rx port is not busy */ |
| 150 | while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--) |
| 151 | ; |
| 152 | } |
| 153 | |
| 154 | static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port) |
| 155 | { |
| 156 | /* set BMI to independent mode, Rx port disable */ |
| 157 | out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM); |
| 158 | /* clear FOF in IM case */ |
| 159 | out_be32(&rx_port->fmbm_rim, 0); |
| 160 | /* Rx frame next engine -RISC */ |
| 161 | out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX); |
| 162 | /* Rx command attribute - no order, MR[3] = 1 */ |
| 163 | clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK); |
| 164 | setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4)); |
| 165 | /* enable Rx statistic counters */ |
| 166 | out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN); |
| 167 | /* disable Rx performance counters */ |
| 168 | out_be32(&rx_port->fmbm_rpc, 0); |
| 169 | } |
| 170 | |
| 171 | static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port) |
| 172 | { |
| 173 | int timeout = 1000000; |
| 174 | |
| 175 | clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN); |
| 176 | |
| 177 | /* wait until the tx port is not busy */ |
| 178 | while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--) |
| 179 | ; |
| 180 | } |
| 181 | |
| 182 | static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port) |
| 183 | { |
| 184 | /* set BMI to independent mode, Tx port disable */ |
| 185 | out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM); |
| 186 | /* Tx frame next engine -RISC */ |
| 187 | out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX); |
| 188 | out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX); |
| 189 | /* Tx command attribute - no order, MR[3] = 1 */ |
| 190 | clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK); |
| 191 | setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4)); |
| 192 | /* enable Tx statistic counters */ |
| 193 | out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN); |
| 194 | /* disable Tx performance counters */ |
| 195 | out_be32(&tx_port->fmbm_tpc, 0); |
| 196 | } |
| 197 | |
| 198 | static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth) |
| 199 | { |
| 200 | struct fm_port_global_pram *pram; |
| 201 | u32 pram_page_offset; |
| 202 | void *rx_bd_ring_base; |
| 203 | void *rx_buf_pool; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 204 | u32 bd_ring_base_lo, bd_ring_base_hi; |
| 205 | u32 buf_lo, buf_hi; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 206 | struct fm_port_bd *rxbd; |
| 207 | struct fm_port_qd *rxqd; |
| 208 | struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port; |
| 209 | int i; |
| 210 | |
| 211 | /* alloc global parameter ram at MURAM */ |
| 212 | pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index, |
| 213 | FM_PRAM_SIZE, FM_PRAM_ALIGN); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 214 | if (!pram) { |
| 215 | printf("%s: No muram for Rx global parameter\n", __func__); |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 216 | return -ENOMEM; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 217 | } |
| 218 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 219 | fm_eth->rx_pram = pram; |
| 220 | |
| 221 | /* parameter page offset to MURAM */ |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 222 | pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 223 | |
| 224 | /* enable global mode- snooping data buffers and BDs */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 225 | out_be32(&pram->mode, PRAM_MODE_GLOBAL); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 226 | |
| 227 | /* init the Rx queue descriptor pionter */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 228 | out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 229 | |
| 230 | /* set the max receive buffer length, power of 2 */ |
| 231 | muram_writew(&pram->mrblr, MAX_RXBUF_LOG2); |
| 232 | |
| 233 | /* alloc Rx buffer descriptors from main memory */ |
| 234 | rx_bd_ring_base = malloc(sizeof(struct fm_port_bd) |
| 235 | * RX_BD_RING_SIZE); |
| 236 | if (!rx_bd_ring_base) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 237 | return -ENOMEM; |
| 238 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 239 | memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd) |
| 240 | * RX_BD_RING_SIZE); |
| 241 | |
| 242 | /* alloc Rx buffer from main memory */ |
| 243 | rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE); |
| 244 | if (!rx_buf_pool) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 245 | return -ENOMEM; |
| 246 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 247 | memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 248 | debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 249 | |
| 250 | /* save them to fm_eth */ |
| 251 | fm_eth->rx_bd_ring = rx_bd_ring_base; |
| 252 | fm_eth->cur_rxbd = rx_bd_ring_base; |
| 253 | fm_eth->rx_buf = rx_buf_pool; |
| 254 | |
| 255 | /* init Rx BDs ring */ |
| 256 | rxbd = (struct fm_port_bd *)rx_bd_ring_base; |
| 257 | for (i = 0; i < RX_BD_RING_SIZE; i++) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 258 | muram_writew(&rxbd->status, RxBD_EMPTY); |
| 259 | muram_writew(&rxbd->len, 0); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 260 | buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool + |
| 261 | i * MAX_RXBUF_LEN)); |
| 262 | buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool + |
| 263 | i * MAX_RXBUF_LEN)); |
| 264 | muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi); |
| 265 | out_be32(&rxbd->buf_ptr_lo, buf_lo); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 266 | rxbd++; |
| 267 | } |
| 268 | |
| 269 | /* set the Rx queue descriptor */ |
| 270 | rxqd = &pram->rxqd; |
| 271 | muram_writew(&rxqd->gen, 0); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 272 | bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base)); |
| 273 | bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base)); |
| 274 | muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi); |
| 275 | out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 276 | muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd) |
| 277 | * RX_BD_RING_SIZE); |
| 278 | muram_writew(&rxqd->offset_in, 0); |
| 279 | muram_writew(&rxqd->offset_out, 0); |
| 280 | |
| 281 | /* set IM parameter ram pointer to Rx Frame Queue ID */ |
| 282 | out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset); |
| 283 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 284 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth) |
| 288 | { |
| 289 | struct fm_port_global_pram *pram; |
| 290 | u32 pram_page_offset; |
| 291 | void *tx_bd_ring_base; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 292 | u32 bd_ring_base_lo, bd_ring_base_hi; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 293 | struct fm_port_bd *txbd; |
| 294 | struct fm_port_qd *txqd; |
| 295 | struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port; |
| 296 | int i; |
| 297 | |
| 298 | /* alloc global parameter ram at MURAM */ |
| 299 | pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index, |
| 300 | FM_PRAM_SIZE, FM_PRAM_ALIGN); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 301 | if (!pram) { |
| 302 | printf("%s: No muram for Tx global parameter\n", __func__); |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 303 | return -ENOMEM; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 304 | } |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 305 | fm_eth->tx_pram = pram; |
| 306 | |
| 307 | /* parameter page offset to MURAM */ |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 308 | pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 309 | |
| 310 | /* enable global mode- snooping data buffers and BDs */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 311 | out_be32(&pram->mode, PRAM_MODE_GLOBAL); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 312 | |
| 313 | /* init the Tx queue descriptor pionter */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 314 | out_be32(&pram->txqd_ptr, pram_page_offset + 0x40); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 315 | |
| 316 | /* alloc Tx buffer descriptors from main memory */ |
| 317 | tx_bd_ring_base = malloc(sizeof(struct fm_port_bd) |
| 318 | * TX_BD_RING_SIZE); |
| 319 | if (!tx_bd_ring_base) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 320 | return -ENOMEM; |
| 321 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 322 | memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd) |
| 323 | * TX_BD_RING_SIZE); |
| 324 | /* save it to fm_eth */ |
| 325 | fm_eth->tx_bd_ring = tx_bd_ring_base; |
| 326 | fm_eth->cur_txbd = tx_bd_ring_base; |
| 327 | |
| 328 | /* init Tx BDs ring */ |
| 329 | txbd = (struct fm_port_bd *)tx_bd_ring_base; |
| 330 | for (i = 0; i < TX_BD_RING_SIZE; i++) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 331 | muram_writew(&txbd->status, TxBD_LAST); |
| 332 | muram_writew(&txbd->len, 0); |
| 333 | muram_writew(&txbd->buf_ptr_hi, 0); |
| 334 | out_be32(&txbd->buf_ptr_lo, 0); |
| 335 | txbd++; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | /* set the Tx queue decriptor */ |
| 339 | txqd = &pram->txqd; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 340 | bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base)); |
| 341 | bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base)); |
| 342 | muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi); |
| 343 | out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 344 | muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd) |
| 345 | * TX_BD_RING_SIZE); |
| 346 | muram_writew(&txqd->offset_in, 0); |
| 347 | muram_writew(&txqd->offset_out, 0); |
| 348 | |
| 349 | /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */ |
| 350 | out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset); |
| 351 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 352 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | static int fm_eth_init(struct fm_eth *fm_eth) |
| 356 | { |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 357 | int ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 358 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 359 | ret = fm_eth_rx_port_parameter_init(fm_eth); |
| 360 | if (ret) |
| 361 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 362 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 363 | ret = fm_eth_tx_port_parameter_init(fm_eth); |
| 364 | if (ret) |
| 365 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 366 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 367 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static int fm_eth_startup(struct fm_eth *fm_eth) |
| 371 | { |
| 372 | struct fsl_enet_mac *mac; |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 373 | int ret; |
| 374 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 375 | mac = fm_eth->mac; |
| 376 | |
| 377 | /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */ |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 378 | ret = fm_eth_init(fm_eth); |
| 379 | if (ret) |
| 380 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 381 | /* setup the MAC controller */ |
| 382 | mac->init_mac(mac); |
| 383 | |
| 384 | /* For some reason we need to set SPEED_100 */ |
Shaohui Xie | 1c68d01 | 2013-08-19 18:58:52 +0800 | [diff] [blame] | 385 | if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) || |
| 386 | (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) && |
| 387 | mac->set_if_mode) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 388 | mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100); |
| 389 | |
| 390 | /* init bmi rx port, IM mode and disable */ |
| 391 | bmi_rx_port_init(fm_eth->rx_port); |
| 392 | /* init bmi tx port, IM mode and disable */ |
| 393 | bmi_tx_port_init(fm_eth->tx_port); |
| 394 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 395 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth) |
| 399 | { |
| 400 | struct fm_port_global_pram *pram; |
| 401 | |
| 402 | pram = fm_eth->tx_pram; |
| 403 | /* graceful stop transmission of frames */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 404 | setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 405 | sync(); |
| 406 | } |
| 407 | |
| 408 | static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth) |
| 409 | { |
| 410 | struct fm_port_global_pram *pram; |
| 411 | |
| 412 | pram = fm_eth->tx_pram; |
| 413 | /* re-enable transmission of frames */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 414 | clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 415 | sync(); |
| 416 | } |
| 417 | |
| 418 | static int fm_eth_open(struct eth_device *dev, bd_t *bd) |
| 419 | { |
| 420 | struct fm_eth *fm_eth; |
| 421 | struct fsl_enet_mac *mac; |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 422 | #ifdef CONFIG_PHYLIB |
| 423 | int ret; |
| 424 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 425 | |
| 426 | fm_eth = (struct fm_eth *)dev->priv; |
| 427 | mac = fm_eth->mac; |
| 428 | |
| 429 | /* setup the MAC address */ |
| 430 | if (dev->enetaddr[0] & 0x01) { |
| 431 | printf("%s: MacAddress is multcast address\n", __func__); |
| 432 | return 1; |
| 433 | } |
| 434 | mac->set_mac_addr(mac, dev->enetaddr); |
| 435 | |
| 436 | /* enable bmi Rx port */ |
| 437 | setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN); |
| 438 | /* enable MAC rx/tx port */ |
| 439 | mac->enable_mac(mac); |
| 440 | /* enable bmi Tx port */ |
| 441 | setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN); |
| 442 | /* re-enable transmission of frame */ |
| 443 | fmc_tx_port_graceful_stop_disable(fm_eth); |
| 444 | |
| 445 | #ifdef CONFIG_PHYLIB |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 446 | if (fm_eth->phydev) { |
| 447 | ret = phy_startup(fm_eth->phydev); |
| 448 | if (ret) { |
| 449 | printf("%s: Could not initialize\n", |
| 450 | fm_eth->phydev->dev->name); |
| 451 | return ret; |
| 452 | } |
| 453 | } else { |
| 454 | return 0; |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 455 | } |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 456 | #else |
| 457 | fm_eth->phydev->speed = SPEED_1000; |
| 458 | fm_eth->phydev->link = 1; |
| 459 | fm_eth->phydev->duplex = DUPLEX_FULL; |
| 460 | #endif |
| 461 | |
| 462 | /* set the MAC-PHY mode */ |
| 463 | mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed); |
| 464 | |
| 465 | if (!fm_eth->phydev->link) |
| 466 | printf("%s: No link.\n", fm_eth->phydev->dev->name); |
| 467 | |
| 468 | return fm_eth->phydev->link ? 0 : -1; |
| 469 | } |
| 470 | |
| 471 | static void fm_eth_halt(struct eth_device *dev) |
| 472 | { |
| 473 | struct fm_eth *fm_eth; |
| 474 | struct fsl_enet_mac *mac; |
| 475 | |
| 476 | fm_eth = (struct fm_eth *)dev->priv; |
| 477 | mac = fm_eth->mac; |
| 478 | |
| 479 | /* graceful stop the transmission of frames */ |
| 480 | fmc_tx_port_graceful_stop_enable(fm_eth); |
| 481 | /* disable bmi Tx port */ |
| 482 | bmi_tx_port_disable(fm_eth->tx_port); |
| 483 | /* disable MAC rx/tx port */ |
| 484 | mac->disable_mac(mac); |
| 485 | /* disable bmi Rx port */ |
| 486 | bmi_rx_port_disable(fm_eth->rx_port); |
| 487 | |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 488 | #ifdef CONFIG_PHYLIB |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 489 | if (fm_eth->phydev) |
| 490 | phy_shutdown(fm_eth->phydev); |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 491 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 492 | } |
| 493 | |
Joe Hershberger | e9df201 | 2012-05-22 07:56:15 +0000 | [diff] [blame] | 494 | static int fm_eth_send(struct eth_device *dev, void *buf, int len) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 495 | { |
| 496 | struct fm_eth *fm_eth; |
| 497 | struct fm_port_global_pram *pram; |
| 498 | struct fm_port_bd *txbd, *txbd_base; |
| 499 | u16 offset_in; |
| 500 | int i; |
| 501 | |
| 502 | fm_eth = (struct fm_eth *)dev->priv; |
| 503 | pram = fm_eth->tx_pram; |
| 504 | txbd = fm_eth->cur_txbd; |
| 505 | |
| 506 | /* find one empty TxBD */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 507 | for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) { |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 508 | udelay(100); |
| 509 | if (i > 0x1000) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 510 | printf("%s: Tx buffer not ready, txbd->status = 0x%x\n", |
| 511 | dev->name, muram_readw(&txbd->status)); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 512 | return 0; |
| 513 | } |
| 514 | } |
| 515 | /* setup TxBD */ |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 516 | muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf))); |
| 517 | out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf))); |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 518 | muram_writew(&txbd->len, len); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 519 | sync(); |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 520 | muram_writew(&txbd->status, TxBD_READY | TxBD_LAST); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 521 | sync(); |
| 522 | |
| 523 | /* update TxQD, let RISC to send the packet */ |
| 524 | offset_in = muram_readw(&pram->txqd.offset_in); |
| 525 | offset_in += sizeof(struct fm_port_bd); |
| 526 | if (offset_in >= muram_readw(&pram->txqd.bd_ring_size)) |
| 527 | offset_in = 0; |
| 528 | muram_writew(&pram->txqd.offset_in, offset_in); |
| 529 | sync(); |
| 530 | |
| 531 | /* wait for buffer to be transmitted */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 532 | for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) { |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 533 | udelay(100); |
| 534 | if (i > 0x10000) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 535 | printf("%s: Tx error, txbd->status = 0x%x\n", |
| 536 | dev->name, muram_readw(&txbd->status)); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 537 | return 0; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | /* advance the TxBD */ |
| 542 | txbd++; |
| 543 | txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring; |
| 544 | if (txbd >= (txbd_base + TX_BD_RING_SIZE)) |
| 545 | txbd = txbd_base; |
| 546 | /* update current txbd */ |
| 547 | fm_eth->cur_txbd = (void *)txbd; |
| 548 | |
| 549 | return 1; |
| 550 | } |
| 551 | |
| 552 | static int fm_eth_recv(struct eth_device *dev) |
| 553 | { |
| 554 | struct fm_eth *fm_eth; |
| 555 | struct fm_port_global_pram *pram; |
| 556 | struct fm_port_bd *rxbd, *rxbd_base; |
| 557 | u16 status, len; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 558 | u32 buf_lo, buf_hi; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 559 | u8 *data; |
| 560 | u16 offset_out; |
Daniel Inderbitzin | 466f775 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 561 | int ret = 1; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 562 | |
| 563 | fm_eth = (struct fm_eth *)dev->priv; |
| 564 | pram = fm_eth->rx_pram; |
| 565 | rxbd = fm_eth->cur_rxbd; |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 566 | status = muram_readw(&rxbd->status); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 567 | |
| 568 | while (!(status & RxBD_EMPTY)) { |
| 569 | if (!(status & RxBD_ERROR)) { |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 570 | buf_hi = muram_readw(&rxbd->buf_ptr_hi); |
| 571 | buf_lo = in_be32(&rxbd->buf_ptr_lo); |
| 572 | data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo); |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 573 | len = muram_readw(&rxbd->len); |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 574 | net_process_received_packet(data, len); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 575 | } else { |
| 576 | printf("%s: Rx error\n", dev->name); |
Daniel Inderbitzin | 466f775 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 577 | ret = 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | /* clear the RxBDs */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 581 | muram_writew(&rxbd->status, RxBD_EMPTY); |
| 582 | muram_writew(&rxbd->len, 0); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 583 | sync(); |
| 584 | |
| 585 | /* advance RxBD */ |
| 586 | rxbd++; |
| 587 | rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring; |
| 588 | if (rxbd >= (rxbd_base + RX_BD_RING_SIZE)) |
| 589 | rxbd = rxbd_base; |
| 590 | /* read next status */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 591 | status = muram_readw(&rxbd->status); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 592 | |
| 593 | /* update RxQD */ |
| 594 | offset_out = muram_readw(&pram->rxqd.offset_out); |
| 595 | offset_out += sizeof(struct fm_port_bd); |
| 596 | if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size)) |
| 597 | offset_out = 0; |
| 598 | muram_writew(&pram->rxqd.offset_out, offset_out); |
| 599 | sync(); |
| 600 | } |
| 601 | fm_eth->cur_rxbd = (void *)rxbd; |
| 602 | |
Daniel Inderbitzin | 466f775 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 603 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg) |
| 607 | { |
| 608 | struct fsl_enet_mac *mac; |
| 609 | int num; |
| 610 | void *base, *phyregs = NULL; |
| 611 | |
| 612 | num = fm_eth->num; |
| 613 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 614 | #ifdef CONFIG_SYS_FMAN_V3 |
Shengzhou Liu | cc19c25 | 2014-11-24 17:11:57 +0800 | [diff] [blame] | 615 | #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION |
Shengzhou Liu | 82a55c1 | 2013-11-22 17:39:09 +0800 | [diff] [blame] | 616 | if (fm_eth->type == FM_ETH_10G_E) { |
Shengzhou Liu | cc19c25 | 2014-11-24 17:11:57 +0800 | [diff] [blame] | 617 | /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240. |
| 618 | * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080. |
| 619 | * 10GEC1 uses mEMAC1 on T1024. |
Shengzhou Liu | 82a55c1 | 2013-11-22 17:39:09 +0800 | [diff] [blame] | 620 | * so it needs to change the num. |
| 621 | */ |
| 622 | if (fm_eth->num >= 2) |
| 623 | num -= 2; |
| 624 | else |
| 625 | num += 8; |
| 626 | } |
Shengzhou Liu | cc19c25 | 2014-11-24 17:11:57 +0800 | [diff] [blame] | 627 | #endif |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 628 | base = ®->memac[num].fm_memac; |
| 629 | phyregs = ®->memac[num].fm_memac_mdio; |
| 630 | #else |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 631 | /* Get the mac registers base address */ |
| 632 | if (fm_eth->type == FM_ETH_1G_E) { |
| 633 | base = ®->mac_1g[num].fm_dtesc; |
Timur Tabi | 3038171 | 2011-10-04 16:44:43 -0500 | [diff] [blame] | 634 | phyregs = ®->mac_1g[num].fm_mdio.miimcfg; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 635 | } else { |
| 636 | base = ®->mac_10g[num].fm_10gec; |
| 637 | phyregs = ®->mac_10g[num].fm_10gec_mdio; |
| 638 | } |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 639 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 640 | |
| 641 | /* alloc mac controller */ |
| 642 | mac = malloc(sizeof(struct fsl_enet_mac)); |
| 643 | if (!mac) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 644 | return -ENOMEM; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 645 | memset(mac, 0, sizeof(struct fsl_enet_mac)); |
| 646 | |
| 647 | /* save the mac to fm_eth struct */ |
| 648 | fm_eth->mac = mac; |
| 649 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 650 | #ifdef CONFIG_SYS_FMAN_V3 |
| 651 | init_memac(mac, base, phyregs, MAX_RXBUF_LEN); |
| 652 | #else |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 653 | if (fm_eth->type == FM_ETH_1G_E) |
Timur Tabi | 3038171 | 2011-10-04 16:44:43 -0500 | [diff] [blame] | 654 | init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 655 | else |
| 656 | init_tgec(mac, base, phyregs, MAX_RXBUF_LEN); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 657 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 658 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 659 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | static int init_phy(struct eth_device *dev) |
| 663 | { |
| 664 | struct fm_eth *fm_eth = dev->priv; |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 665 | #ifdef CONFIG_PHYLIB |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 666 | struct phy_device *phydev = NULL; |
| 667 | u32 supported; |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 668 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 669 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 670 | if (fm_eth->type == FM_ETH_1G_E) |
| 671 | dtsec_init_phy(dev); |
| 672 | |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 673 | #ifdef CONFIG_PHYLIB |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 674 | if (fm_eth->bus) { |
| 675 | phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, dev, |
| 676 | fm_eth->enet_if); |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 677 | if (!phydev) { |
| 678 | printf("Failed to connect\n"); |
| 679 | return -1; |
| 680 | } |
| 681 | } else { |
| 682 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | if (fm_eth->type == FM_ETH_1G_E) { |
| 686 | supported = (SUPPORTED_10baseT_Half | |
| 687 | SUPPORTED_10baseT_Full | |
| 688 | SUPPORTED_100baseT_Half | |
| 689 | SUPPORTED_100baseT_Full | |
| 690 | SUPPORTED_1000baseT_Full); |
| 691 | } else { |
| 692 | supported = SUPPORTED_10000baseT_Full; |
| 693 | |
| 694 | if (tgec_is_fibre(dev)) |
| 695 | phydev->port = PORT_FIBRE; |
| 696 | } |
| 697 | |
| 698 | phydev->supported &= supported; |
| 699 | phydev->advertising = phydev->supported; |
| 700 | |
| 701 | fm_eth->phydev = phydev; |
| 702 | |
| 703 | phy_config(phydev); |
| 704 | #endif |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info) |
| 710 | { |
| 711 | struct eth_device *dev; |
| 712 | struct fm_eth *fm_eth; |
| 713 | int i, num = info->num; |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 714 | int ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 715 | |
| 716 | /* alloc eth device */ |
| 717 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
| 718 | if (!dev) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 719 | return -ENOMEM; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 720 | memset(dev, 0, sizeof(struct eth_device)); |
| 721 | |
| 722 | /* alloc the FMan ethernet private struct */ |
| 723 | fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth)); |
| 724 | if (!fm_eth) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 725 | return -ENOMEM; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 726 | memset(fm_eth, 0, sizeof(struct fm_eth)); |
| 727 | |
| 728 | /* save off some things we need from the info struct */ |
| 729 | fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */ |
| 730 | fm_eth->num = num; |
| 731 | fm_eth->type = info->type; |
| 732 | |
| 733 | fm_eth->rx_port = (void *)®->port[info->rx_port_id - 1].fm_bmi; |
| 734 | fm_eth->tx_port = (void *)®->port[info->tx_port_id - 1].fm_bmi; |
| 735 | |
| 736 | /* set the ethernet max receive length */ |
| 737 | fm_eth->max_rx_len = MAX_RXBUF_LEN; |
| 738 | |
| 739 | /* init global mac structure */ |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 740 | ret = fm_eth_init_mac(fm_eth, reg); |
| 741 | if (ret) |
| 742 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 743 | |
| 744 | /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */ |
| 745 | if (fm_eth->type == FM_ETH_1G_E) |
| 746 | sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1); |
| 747 | else |
| 748 | sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1); |
| 749 | |
| 750 | devlist[num_controllers++] = dev; |
| 751 | dev->iobase = 0; |
| 752 | dev->priv = (void *)fm_eth; |
| 753 | dev->init = fm_eth_open; |
| 754 | dev->halt = fm_eth_halt; |
| 755 | dev->send = fm_eth_send; |
| 756 | dev->recv = fm_eth_recv; |
| 757 | fm_eth->dev = dev; |
| 758 | fm_eth->bus = info->bus; |
| 759 | fm_eth->phyaddr = info->phy_addr; |
| 760 | fm_eth->enet_if = info->enet_if; |
| 761 | |
| 762 | /* startup the FM im */ |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 763 | ret = fm_eth_startup(fm_eth); |
| 764 | if (ret) |
| 765 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 766 | |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 767 | init_phy(dev); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 768 | |
| 769 | /* clear the ethernet address */ |
| 770 | for (i = 0; i < 6; i++) |
| 771 | dev->enetaddr[i] = 0; |
| 772 | eth_register(dev); |
| 773 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 774 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 775 | } |