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