Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 2 | /* |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 3 | * Copyright 2009-2012 Freescale Semiconductor, Inc. |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 4 | * Copyright 2020 NXP |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 5 | * Dave Liu <daveliu@freescale.com> |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 6 | */ |
| 7 | #include <common.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame^] | 8 | #include <log.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <part.h> |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 10 | #include <asm/io.h> |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 11 | #ifdef CONFIG_DM_ETH |
| 12 | #include <dm.h> |
| 13 | #include <dm/ofnode.h> |
| 14 | #include <linux/compat.h> |
| 15 | #include <phy_interface.h> |
| 16 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 17 | #include <malloc.h> |
| 18 | #include <net.h> |
| 19 | #include <hwconfig.h> |
| 20 | #include <fm_eth.h> |
| 21 | #include <fsl_mdio.h> |
| 22 | #include <miiphy.h> |
| 23 | #include <phy.h> |
Shaohui Xie | 8225b2f | 2015-10-26 19:47:47 +0800 | [diff] [blame] | 24 | #include <fsl_dtsec.h> |
| 25 | #include <fsl_tgec.h> |
Shaohui Xie | cd348ef | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 26 | #include <fsl_memac.h> |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 27 | |
| 28 | #include "fm.h" |
| 29 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 30 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 31 | static struct eth_device *devlist[NUM_FM_PORTS]; |
| 32 | static int num_controllers; |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 33 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 34 | |
| 35 | #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII) && !defined(BITBANGMII) |
| 36 | |
| 37 | #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \ |
| 38 | TBIANA_FULL_DUPLEX) |
| 39 | |
| 40 | #define TBIANA_SGMII_ACK 0x4001 |
| 41 | |
| 42 | #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \ |
| 43 | TBICR_FULL_DUPLEX | TBICR_SPEED1_SET) |
| 44 | |
| 45 | /* Configure the TBI for SGMII operation */ |
Kim Phillips | 960d70c | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 46 | static void dtsec_configure_serdes(struct fm_eth *priv) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 47 | { |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 48 | #ifdef CONFIG_SYS_FMAN_V3 |
| 49 | u32 value; |
| 50 | struct mii_dev bus; |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 51 | bool sgmii_2500 = (priv->enet_if == |
| 52 | PHY_INTERFACE_MODE_SGMII_2500) ? true : false; |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 53 | int i = 0, j; |
| 54 | |
| 55 | #ifndef CONFIG_DM_ETH |
| 56 | bus.priv = priv->mac->phyregs; |
| 57 | #else |
| 58 | bus.priv = priv->pcs_mdio; |
| 59 | #endif |
| 60 | bus.read = memac_mdio_read; |
| 61 | bus.write = memac_mdio_write; |
| 62 | bus.reset = memac_mdio_reset; |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 63 | |
Shaohui Xie | bc24611 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 64 | qsgmii_loop: |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 65 | /* 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] | 66 | if (sgmii_2500) |
| 67 | value = PHY_SGMII_CR_PHY_RESET | |
| 68 | PHY_SGMII_IF_SPEED_GIGABIT | |
| 69 | PHY_SGMII_IF_MODE_SGMII; |
| 70 | else |
| 71 | value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN; |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 72 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 73 | for (j = 0; j <= 3; j++) |
| 74 | debug("dump PCS reg %#x: %#x\n", j, |
| 75 | memac_mdio_read(&bus, i, MDIO_DEVAD_NONE, j)); |
| 76 | |
Shaohui Xie | bc24611 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 77 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 78 | |
| 79 | /* Dev ability according to SGMII specification */ |
| 80 | value = PHY_SGMII_DEV_ABILITY_SGMII; |
Shaohui Xie | bc24611 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 81 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 82 | |
shaohui xie | bead088 | 2016-11-15 14:36:47 +0800 | [diff] [blame] | 83 | if (sgmii_2500) { |
| 84 | /* Adjust link timer for 2.5G SGMII, |
| 85 | * 1.6 ms in units of 3.2 ns: |
| 86 | * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120. |
| 87 | */ |
| 88 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007); |
| 89 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120); |
| 90 | } else { |
| 91 | /* Adjust link timer for SGMII, |
| 92 | * 1.6 ms in units of 8 ns: |
| 93 | * 1.6ms / 8ns = 2 * 10^5 = 0x30d40. |
| 94 | */ |
| 95 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003); |
| 96 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40); |
| 97 | } |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 98 | |
| 99 | /* Restart AN */ |
shaohui xie | bead088 | 2016-11-15 14:36:47 +0800 | [diff] [blame] | 100 | value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN; |
Shaohui Xie | bc24611 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 101 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value); |
| 102 | |
| 103 | if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) { |
| 104 | i++; |
| 105 | goto qsgmii_loop; |
| 106 | } |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 107 | #else |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 108 | struct dtsec *regs = priv->mac->base; |
| 109 | struct tsec_mii_mng *phyregs = priv->mac->phyregs; |
| 110 | |
| 111 | /* |
| 112 | * Access TBI PHY registers at given TSEC register offset as |
| 113 | * opposed to the register offset used for external PHY accesses |
| 114 | */ |
| 115 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_TBICON, |
| 116 | TBICON_CLK_SELECT); |
| 117 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_ANA, |
| 118 | TBIANA_SGMII_ACK); |
| 119 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, |
| 120 | TBI_CR, TBICR_SETTINGS); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 121 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 124 | static void dtsec_init_phy(struct fm_eth *fm_eth) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 125 | { |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 126 | #ifndef CONFIG_SYS_FMAN_V3 |
shaohui xie | 1f3bd3e | 2012-10-11 20:25:36 +0000 | [diff] [blame] | 127 | struct dtsec *regs = (struct dtsec *)CONFIG_SYS_FSL_FM1_DTSEC1_ADDR; |
| 128 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 129 | /* Assign a Physical address to the TBI */ |
| 130 | out_be32(®s->tbipa, CONFIG_SYS_TBIPA_VALUE); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 131 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 132 | |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 133 | if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII || |
Shaohui Xie | bc24611 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 134 | fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII || |
Shengzhou Liu | c35f869 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 135 | fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 136 | dtsec_configure_serdes(fm_eth); |
| 137 | } |
| 138 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 139 | #ifndef CONFIG_DM_ETH |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 140 | #ifdef CONFIG_PHYLIB |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 141 | static int tgec_is_fibre(struct fm_eth *fm) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 142 | { |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 143 | char phyopt[20]; |
| 144 | |
| 145 | sprintf(phyopt, "fsl_fm%d_xaui_phy", fm->fm_index + 1); |
| 146 | |
| 147 | return hwconfig_arg_cmp(phyopt, "xfi"); |
| 148 | } |
| 149 | #endif |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 150 | #endif /* CONFIG_DM_ETH */ |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 151 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 152 | |
| 153 | static u16 muram_readw(u16 *addr) |
| 154 | { |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 155 | ulong base = (ulong)addr & ~0x3UL; |
| 156 | u32 val32 = in_be32((void *)base); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 157 | int byte_pos; |
| 158 | u16 ret; |
| 159 | |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 160 | byte_pos = (ulong)addr & 0x3UL; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 161 | if (byte_pos) |
| 162 | ret = (u16)(val32 & 0x0000ffff); |
| 163 | else |
| 164 | ret = (u16)((val32 & 0xffff0000) >> 16); |
| 165 | |
| 166 | return ret; |
| 167 | } |
| 168 | |
| 169 | static void muram_writew(u16 *addr, u16 val) |
| 170 | { |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 171 | ulong base = (ulong)addr & ~0x3UL; |
| 172 | u32 org32 = in_be32((void *)base); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 173 | u32 val32; |
| 174 | int byte_pos; |
| 175 | |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 176 | byte_pos = (ulong)addr & 0x3UL; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 177 | if (byte_pos) |
| 178 | val32 = (org32 & 0xffff0000) | val; |
| 179 | else |
| 180 | val32 = (org32 & 0x0000ffff) | ((u32)val << 16); |
| 181 | |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 182 | out_be32((void *)base, val32); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void bmi_rx_port_disable(struct fm_bmi_rx_port *rx_port) |
| 186 | { |
| 187 | int timeout = 1000000; |
| 188 | |
| 189 | clrbits_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_EN); |
| 190 | |
| 191 | /* wait until the rx port is not busy */ |
| 192 | while ((in_be32(&rx_port->fmbm_rst) & FMBM_RST_BSY) && timeout--) |
| 193 | ; |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 194 | if (!timeout) |
| 195 | printf("%s - timeout\n", __func__); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port) |
| 199 | { |
| 200 | /* set BMI to independent mode, Rx port disable */ |
| 201 | out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM); |
| 202 | /* clear FOF in IM case */ |
| 203 | out_be32(&rx_port->fmbm_rim, 0); |
| 204 | /* Rx frame next engine -RISC */ |
| 205 | out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX); |
| 206 | /* Rx command attribute - no order, MR[3] = 1 */ |
| 207 | clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK); |
| 208 | setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4)); |
| 209 | /* enable Rx statistic counters */ |
| 210 | out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN); |
| 211 | /* disable Rx performance counters */ |
| 212 | out_be32(&rx_port->fmbm_rpc, 0); |
| 213 | } |
| 214 | |
| 215 | static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port) |
| 216 | { |
| 217 | int timeout = 1000000; |
| 218 | |
| 219 | clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN); |
| 220 | |
| 221 | /* wait until the tx port is not busy */ |
| 222 | while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--) |
| 223 | ; |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 224 | if (!timeout) |
| 225 | printf("%s - timeout\n", __func__); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port) |
| 229 | { |
| 230 | /* set BMI to independent mode, Tx port disable */ |
| 231 | out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM); |
| 232 | /* Tx frame next engine -RISC */ |
| 233 | out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX); |
| 234 | out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX); |
| 235 | /* Tx command attribute - no order, MR[3] = 1 */ |
| 236 | clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK); |
| 237 | setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4)); |
| 238 | /* enable Tx statistic counters */ |
| 239 | out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN); |
| 240 | /* disable Tx performance counters */ |
| 241 | out_be32(&tx_port->fmbm_tpc, 0); |
| 242 | } |
| 243 | |
| 244 | static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth) |
| 245 | { |
| 246 | struct fm_port_global_pram *pram; |
| 247 | u32 pram_page_offset; |
| 248 | void *rx_bd_ring_base; |
| 249 | void *rx_buf_pool; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 250 | u32 bd_ring_base_lo, bd_ring_base_hi; |
| 251 | u32 buf_lo, buf_hi; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 252 | struct fm_port_bd *rxbd; |
| 253 | struct fm_port_qd *rxqd; |
| 254 | struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port; |
| 255 | int i; |
| 256 | |
| 257 | /* alloc global parameter ram at MURAM */ |
| 258 | pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index, |
| 259 | FM_PRAM_SIZE, FM_PRAM_ALIGN); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 260 | if (!pram) { |
| 261 | printf("%s: No muram for Rx global parameter\n", __func__); |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 262 | return -ENOMEM; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 263 | } |
| 264 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 265 | fm_eth->rx_pram = pram; |
| 266 | |
| 267 | /* parameter page offset to MURAM */ |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 268 | pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 269 | |
| 270 | /* enable global mode- snooping data buffers and BDs */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 271 | out_be32(&pram->mode, PRAM_MODE_GLOBAL); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 272 | |
| 273 | /* init the Rx queue descriptor pionter */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 274 | out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 275 | |
| 276 | /* set the max receive buffer length, power of 2 */ |
| 277 | muram_writew(&pram->mrblr, MAX_RXBUF_LOG2); |
| 278 | |
| 279 | /* alloc Rx buffer descriptors from main memory */ |
| 280 | rx_bd_ring_base = malloc(sizeof(struct fm_port_bd) |
| 281 | * RX_BD_RING_SIZE); |
| 282 | if (!rx_bd_ring_base) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 283 | return -ENOMEM; |
| 284 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 285 | memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd) |
| 286 | * RX_BD_RING_SIZE); |
| 287 | |
| 288 | /* alloc Rx buffer from main memory */ |
| 289 | rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE); |
| 290 | if (!rx_buf_pool) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 291 | return -ENOMEM; |
| 292 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 293 | memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 294 | debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 295 | |
| 296 | /* save them to fm_eth */ |
| 297 | fm_eth->rx_bd_ring = rx_bd_ring_base; |
| 298 | fm_eth->cur_rxbd = rx_bd_ring_base; |
| 299 | fm_eth->rx_buf = rx_buf_pool; |
| 300 | |
| 301 | /* init Rx BDs ring */ |
| 302 | rxbd = (struct fm_port_bd *)rx_bd_ring_base; |
| 303 | for (i = 0; i < RX_BD_RING_SIZE; i++) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 304 | muram_writew(&rxbd->status, RxBD_EMPTY); |
| 305 | muram_writew(&rxbd->len, 0); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 306 | buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool + |
| 307 | i * MAX_RXBUF_LEN)); |
| 308 | buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool + |
| 309 | i * MAX_RXBUF_LEN)); |
| 310 | muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi); |
| 311 | out_be32(&rxbd->buf_ptr_lo, buf_lo); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 312 | rxbd++; |
| 313 | } |
| 314 | |
| 315 | /* set the Rx queue descriptor */ |
| 316 | rxqd = &pram->rxqd; |
| 317 | muram_writew(&rxqd->gen, 0); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 318 | bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base)); |
| 319 | bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base)); |
| 320 | muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi); |
| 321 | out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 322 | muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd) |
| 323 | * RX_BD_RING_SIZE); |
| 324 | muram_writew(&rxqd->offset_in, 0); |
| 325 | muram_writew(&rxqd->offset_out, 0); |
| 326 | |
| 327 | /* set IM parameter ram pointer to Rx Frame Queue ID */ |
| 328 | out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset); |
| 329 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 330 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth) |
| 334 | { |
| 335 | struct fm_port_global_pram *pram; |
| 336 | u32 pram_page_offset; |
| 337 | void *tx_bd_ring_base; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 338 | u32 bd_ring_base_lo, bd_ring_base_hi; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 339 | struct fm_port_bd *txbd; |
| 340 | struct fm_port_qd *txqd; |
| 341 | struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port; |
| 342 | int i; |
| 343 | |
| 344 | /* alloc global parameter ram at MURAM */ |
| 345 | pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index, |
| 346 | FM_PRAM_SIZE, FM_PRAM_ALIGN); |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 347 | if (!pram) { |
| 348 | printf("%s: No muram for Tx global parameter\n", __func__); |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 349 | return -ENOMEM; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 350 | } |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 351 | fm_eth->tx_pram = pram; |
| 352 | |
| 353 | /* parameter page offset to MURAM */ |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 354 | pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 355 | |
| 356 | /* enable global mode- snooping data buffers and BDs */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 357 | out_be32(&pram->mode, PRAM_MODE_GLOBAL); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 358 | |
| 359 | /* init the Tx queue descriptor pionter */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 360 | out_be32(&pram->txqd_ptr, pram_page_offset + 0x40); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 361 | |
| 362 | /* alloc Tx buffer descriptors from main memory */ |
| 363 | tx_bd_ring_base = malloc(sizeof(struct fm_port_bd) |
| 364 | * TX_BD_RING_SIZE); |
| 365 | if (!tx_bd_ring_base) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 366 | return -ENOMEM; |
| 367 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 368 | memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd) |
| 369 | * TX_BD_RING_SIZE); |
| 370 | /* save it to fm_eth */ |
| 371 | fm_eth->tx_bd_ring = tx_bd_ring_base; |
| 372 | fm_eth->cur_txbd = tx_bd_ring_base; |
| 373 | |
| 374 | /* init Tx BDs ring */ |
| 375 | txbd = (struct fm_port_bd *)tx_bd_ring_base; |
| 376 | for (i = 0; i < TX_BD_RING_SIZE; i++) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 377 | muram_writew(&txbd->status, TxBD_LAST); |
| 378 | muram_writew(&txbd->len, 0); |
| 379 | muram_writew(&txbd->buf_ptr_hi, 0); |
| 380 | out_be32(&txbd->buf_ptr_lo, 0); |
| 381 | txbd++; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* set the Tx queue decriptor */ |
| 385 | txqd = &pram->txqd; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 386 | bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base)); |
| 387 | bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base)); |
| 388 | muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi); |
| 389 | out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 390 | muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd) |
| 391 | * TX_BD_RING_SIZE); |
| 392 | muram_writew(&txqd->offset_in, 0); |
| 393 | muram_writew(&txqd->offset_out, 0); |
| 394 | |
| 395 | /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */ |
| 396 | out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset); |
| 397 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 398 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | static int fm_eth_init(struct fm_eth *fm_eth) |
| 402 | { |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 403 | int ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 404 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 405 | ret = fm_eth_rx_port_parameter_init(fm_eth); |
| 406 | if (ret) |
| 407 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 408 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 409 | ret = fm_eth_tx_port_parameter_init(fm_eth); |
| 410 | if (ret) |
| 411 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 412 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 413 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int fm_eth_startup(struct fm_eth *fm_eth) |
| 417 | { |
| 418 | struct fsl_enet_mac *mac; |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 419 | int ret; |
| 420 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 421 | mac = fm_eth->mac; |
| 422 | |
| 423 | /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */ |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 424 | ret = fm_eth_init(fm_eth); |
| 425 | if (ret) |
| 426 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 427 | /* setup the MAC controller */ |
| 428 | mac->init_mac(mac); |
| 429 | |
| 430 | /* For some reason we need to set SPEED_100 */ |
Shaohui Xie | 1c68d01 | 2013-08-19 18:58:52 +0800 | [diff] [blame] | 431 | if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) || |
shaohui xie | bead088 | 2016-11-15 14:36:47 +0800 | [diff] [blame] | 432 | (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) || |
Shaohui Xie | 1c68d01 | 2013-08-19 18:58:52 +0800 | [diff] [blame] | 433 | (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) && |
| 434 | mac->set_if_mode) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 435 | mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100); |
| 436 | |
| 437 | /* init bmi rx port, IM mode and disable */ |
| 438 | bmi_rx_port_init(fm_eth->rx_port); |
| 439 | /* init bmi tx port, IM mode and disable */ |
| 440 | bmi_tx_port_init(fm_eth->tx_port); |
| 441 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 442 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth) |
| 446 | { |
| 447 | struct fm_port_global_pram *pram; |
| 448 | |
| 449 | pram = fm_eth->tx_pram; |
| 450 | /* graceful stop transmission of frames */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 451 | setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 452 | sync(); |
| 453 | } |
| 454 | |
| 455 | static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth) |
| 456 | { |
| 457 | struct fm_port_global_pram *pram; |
| 458 | |
| 459 | pram = fm_eth->tx_pram; |
| 460 | /* re-enable transmission of frames */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 461 | clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 462 | sync(); |
| 463 | } |
| 464 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 465 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 466 | static int fm_eth_open(struct eth_device *dev, bd_t *bd) |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 467 | #else |
| 468 | static int fm_eth_open(struct udevice *dev) |
| 469 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 470 | { |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 471 | #ifndef CONFIG_DM_ETH |
| 472 | struct fm_eth *fm_eth = dev->priv; |
| 473 | #else |
| 474 | struct eth_pdata *pdata = dev_get_platdata(dev); |
| 475 | struct fm_eth *fm_eth = dev_get_priv(dev); |
| 476 | #endif |
| 477 | unsigned char *enetaddr; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 478 | struct fsl_enet_mac *mac; |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 479 | #ifdef CONFIG_PHYLIB |
| 480 | int ret; |
| 481 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 482 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 483 | mac = fm_eth->mac; |
| 484 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 485 | #ifndef CONFIG_DM_ETH |
| 486 | enetaddr = &dev->enetaddr[0]; |
| 487 | #else |
| 488 | enetaddr = pdata->enetaddr; |
| 489 | #endif |
| 490 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 491 | /* setup the MAC address */ |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 492 | if (enetaddr[0] & 0x01) { |
| 493 | printf("%s: MacAddress is multicast address\n", __func__); |
| 494 | enetaddr[0] = 0; |
| 495 | enetaddr[5] = fm_eth->num; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 496 | } |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 497 | mac->set_mac_addr(mac, enetaddr); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 498 | |
| 499 | /* enable bmi Rx port */ |
| 500 | setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN); |
| 501 | /* enable MAC rx/tx port */ |
| 502 | mac->enable_mac(mac); |
| 503 | /* enable bmi Tx port */ |
| 504 | setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN); |
| 505 | /* re-enable transmission of frame */ |
| 506 | fmc_tx_port_graceful_stop_disable(fm_eth); |
| 507 | |
| 508 | #ifdef CONFIG_PHYLIB |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 509 | if (fm_eth->phydev) { |
| 510 | ret = phy_startup(fm_eth->phydev); |
| 511 | if (ret) { |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 512 | #ifndef CONFIG_DM_ETH |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 513 | printf("%s: Could not initialize\n", |
| 514 | fm_eth->phydev->dev->name); |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 515 | #else |
| 516 | printf("%s: Could not initialize\n", dev->name); |
| 517 | #endif |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 518 | return ret; |
| 519 | } |
| 520 | } else { |
| 521 | return 0; |
Timur Tabi | 11af8d6 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 522 | } |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 523 | #else |
| 524 | fm_eth->phydev->speed = SPEED_1000; |
| 525 | fm_eth->phydev->link = 1; |
| 526 | fm_eth->phydev->duplex = DUPLEX_FULL; |
| 527 | #endif |
| 528 | |
| 529 | /* set the MAC-PHY mode */ |
| 530 | mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed); |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 531 | debug("MAC IF mode %d, speed %d, link %d\n", fm_eth->enet_if, |
| 532 | fm_eth->phydev->speed, fm_eth->phydev->link); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 533 | |
| 534 | if (!fm_eth->phydev->link) |
| 535 | printf("%s: No link.\n", fm_eth->phydev->dev->name); |
| 536 | |
| 537 | return fm_eth->phydev->link ? 0 : -1; |
| 538 | } |
| 539 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 540 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 541 | static void fm_eth_halt(struct eth_device *dev) |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 542 | #else |
| 543 | static void fm_eth_halt(struct udevice *dev) |
| 544 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 545 | { |
| 546 | struct fm_eth *fm_eth; |
| 547 | struct fsl_enet_mac *mac; |
| 548 | |
| 549 | fm_eth = (struct fm_eth *)dev->priv; |
| 550 | mac = fm_eth->mac; |
| 551 | |
| 552 | /* graceful stop the transmission of frames */ |
| 553 | fmc_tx_port_graceful_stop_enable(fm_eth); |
| 554 | /* disable bmi Tx port */ |
| 555 | bmi_tx_port_disable(fm_eth->tx_port); |
| 556 | /* disable MAC rx/tx port */ |
| 557 | mac->disable_mac(mac); |
| 558 | /* disable bmi Rx port */ |
| 559 | bmi_rx_port_disable(fm_eth->rx_port); |
| 560 | |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 561 | #ifdef CONFIG_PHYLIB |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 562 | if (fm_eth->phydev) |
| 563 | phy_shutdown(fm_eth->phydev); |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 564 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 565 | } |
| 566 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 567 | #ifndef CONFIG_DM_ETH |
Joe Hershberger | e9df201 | 2012-05-22 07:56:15 +0000 | [diff] [blame] | 568 | static int fm_eth_send(struct eth_device *dev, void *buf, int len) |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 569 | #else |
| 570 | static int fm_eth_send(struct udevice *dev, void *buf, int len) |
| 571 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 572 | { |
| 573 | struct fm_eth *fm_eth; |
| 574 | struct fm_port_global_pram *pram; |
| 575 | struct fm_port_bd *txbd, *txbd_base; |
| 576 | u16 offset_in; |
| 577 | int i; |
| 578 | |
| 579 | fm_eth = (struct fm_eth *)dev->priv; |
| 580 | pram = fm_eth->tx_pram; |
| 581 | txbd = fm_eth->cur_txbd; |
| 582 | |
| 583 | /* find one empty TxBD */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 584 | for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) { |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 585 | udelay(100); |
| 586 | if (i > 0x1000) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 587 | printf("%s: Tx buffer not ready, txbd->status = 0x%x\n", |
| 588 | dev->name, muram_readw(&txbd->status)); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | } |
| 592 | /* setup TxBD */ |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 593 | muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf))); |
| 594 | 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] | 595 | muram_writew(&txbd->len, len); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 596 | sync(); |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 597 | muram_writew(&txbd->status, TxBD_READY | TxBD_LAST); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 598 | sync(); |
| 599 | |
| 600 | /* update TxQD, let RISC to send the packet */ |
| 601 | offset_in = muram_readw(&pram->txqd.offset_in); |
| 602 | offset_in += sizeof(struct fm_port_bd); |
| 603 | if (offset_in >= muram_readw(&pram->txqd.bd_ring_size)) |
| 604 | offset_in = 0; |
| 605 | muram_writew(&pram->txqd.offset_in, offset_in); |
| 606 | sync(); |
| 607 | |
| 608 | /* wait for buffer to be transmitted */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 609 | for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) { |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 610 | udelay(100); |
| 611 | if (i > 0x10000) { |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 612 | printf("%s: Tx error, txbd->status = 0x%x\n", |
| 613 | dev->name, muram_readw(&txbd->status)); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 614 | return 0; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | /* advance the TxBD */ |
| 619 | txbd++; |
| 620 | txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring; |
| 621 | if (txbd >= (txbd_base + TX_BD_RING_SIZE)) |
| 622 | txbd = txbd_base; |
| 623 | /* update current txbd */ |
| 624 | fm_eth->cur_txbd = (void *)txbd; |
| 625 | |
| 626 | return 1; |
| 627 | } |
| 628 | |
Madalin Bucur | 1c71018 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 629 | static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth, |
| 630 | struct fm_port_bd *rxbd) |
| 631 | { |
| 632 | struct fm_port_global_pram *pram; |
| 633 | struct fm_port_bd *rxbd_base; |
| 634 | u16 offset_out; |
| 635 | |
| 636 | pram = fm_eth->rx_pram; |
| 637 | |
| 638 | /* clear the RxBDs */ |
| 639 | muram_writew(&rxbd->status, RxBD_EMPTY); |
| 640 | muram_writew(&rxbd->len, 0); |
| 641 | sync(); |
| 642 | |
| 643 | /* advance RxBD */ |
| 644 | rxbd++; |
| 645 | rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring; |
| 646 | if (rxbd >= (rxbd_base + RX_BD_RING_SIZE)) |
| 647 | rxbd = rxbd_base; |
| 648 | |
| 649 | /* update RxQD */ |
| 650 | offset_out = muram_readw(&pram->rxqd.offset_out); |
| 651 | offset_out += sizeof(struct fm_port_bd); |
| 652 | if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size)) |
| 653 | offset_out = 0; |
| 654 | muram_writew(&pram->rxqd.offset_out, offset_out); |
| 655 | sync(); |
| 656 | |
| 657 | return rxbd; |
| 658 | } |
| 659 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 660 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 661 | static int fm_eth_recv(struct eth_device *dev) |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 662 | #else |
| 663 | static int fm_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 664 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 665 | { |
Madalin Bucur | 1c71018 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 666 | struct fm_eth *fm_eth = (struct fm_eth *)dev->priv; |
| 667 | struct fm_port_bd *rxbd = fm_eth->cur_rxbd; |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 668 | u32 buf_lo, buf_hi; |
Madalin Bucur | 1c71018 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 669 | u16 status, len; |
| 670 | int ret = -1; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 671 | u8 *data; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 672 | |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 673 | status = muram_readw(&rxbd->status); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 674 | |
| 675 | while (!(status & RxBD_EMPTY)) { |
| 676 | if (!(status & RxBD_ERROR)) { |
Hou Zhiqiang | 9fc29db | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 677 | buf_hi = muram_readw(&rxbd->buf_ptr_hi); |
| 678 | buf_lo = in_be32(&rxbd->buf_ptr_lo); |
| 679 | data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo); |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 680 | len = muram_readw(&rxbd->len); |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 681 | #ifndef CONFIG_DM_ETH |
Joe Hershberger | 1fd92db | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 682 | net_process_received_packet(data, len); |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 683 | #else |
| 684 | *packetp = data; |
| 685 | return len; |
| 686 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 687 | } else { |
| 688 | printf("%s: Rx error\n", dev->name); |
Daniel Inderbitzin | 466f775 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 689 | ret = 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 690 | } |
| 691 | |
Madalin Bucur | 1c71018 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 692 | /* free current bd, advance to next one */ |
| 693 | rxbd = fm_eth_free_one(fm_eth, rxbd); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 694 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 695 | /* read next status */ |
Hou Zhiqiang | 648bde6 | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 696 | status = muram_readw(&rxbd->status); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 697 | } |
| 698 | fm_eth->cur_rxbd = (void *)rxbd; |
| 699 | |
Daniel Inderbitzin | 466f775 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 700 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 701 | } |
| 702 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 703 | #ifdef CONFIG_DM_ETH |
| 704 | static int fm_eth_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 705 | { |
| 706 | struct fm_eth *fm_eth = (struct fm_eth *)dev->priv; |
| 707 | |
| 708 | fm_eth->cur_rxbd = fm_eth_free_one(fm_eth, fm_eth->cur_rxbd); |
| 709 | |
| 710 | return 0; |
| 711 | } |
| 712 | #endif /* CONFIG_DM_ETH */ |
| 713 | |
| 714 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 715 | static int fm_eth_init_mac(struct fm_eth *fm_eth, struct ccsr_fman *reg) |
| 716 | { |
| 717 | struct fsl_enet_mac *mac; |
| 718 | int num; |
| 719 | void *base, *phyregs = NULL; |
| 720 | |
| 721 | num = fm_eth->num; |
| 722 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 723 | #ifdef CONFIG_SYS_FMAN_V3 |
Shengzhou Liu | cc19c25 | 2014-11-24 17:11:57 +0800 | [diff] [blame] | 724 | #ifndef CONFIG_FSL_FM_10GEC_REGULAR_NOTATION |
Shengzhou Liu | 82a55c1 | 2013-11-22 17:39:09 +0800 | [diff] [blame] | 725 | if (fm_eth->type == FM_ETH_10G_E) { |
Shengzhou Liu | cc19c25 | 2014-11-24 17:11:57 +0800 | [diff] [blame] | 726 | /* 10GEC1/10GEC2 use mEMAC9/mEMAC10 on T2080/T4240. |
| 727 | * 10GEC3/10GEC4 use mEMAC1/mEMAC2 on T2080. |
| 728 | * 10GEC1 uses mEMAC1 on T1024. |
Shengzhou Liu | 82a55c1 | 2013-11-22 17:39:09 +0800 | [diff] [blame] | 729 | * so it needs to change the num. |
| 730 | */ |
| 731 | if (fm_eth->num >= 2) |
| 732 | num -= 2; |
| 733 | else |
| 734 | num += 8; |
| 735 | } |
Shengzhou Liu | cc19c25 | 2014-11-24 17:11:57 +0800 | [diff] [blame] | 736 | #endif |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 737 | base = ®->memac[num].fm_memac; |
| 738 | phyregs = ®->memac[num].fm_memac_mdio; |
| 739 | #else |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 740 | /* Get the mac registers base address */ |
| 741 | if (fm_eth->type == FM_ETH_1G_E) { |
| 742 | base = ®->mac_1g[num].fm_dtesc; |
Timur Tabi | 3038171 | 2011-10-04 16:44:43 -0500 | [diff] [blame] | 743 | phyregs = ®->mac_1g[num].fm_mdio.miimcfg; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 744 | } else { |
| 745 | base = ®->mac_10g[num].fm_10gec; |
| 746 | phyregs = ®->mac_10g[num].fm_10gec_mdio; |
| 747 | } |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 748 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 749 | |
| 750 | /* alloc mac controller */ |
| 751 | mac = malloc(sizeof(struct fsl_enet_mac)); |
| 752 | if (!mac) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 753 | return -ENOMEM; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 754 | memset(mac, 0, sizeof(struct fsl_enet_mac)); |
| 755 | |
| 756 | /* save the mac to fm_eth struct */ |
| 757 | fm_eth->mac = mac; |
| 758 | |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 759 | #ifdef CONFIG_SYS_FMAN_V3 |
| 760 | init_memac(mac, base, phyregs, MAX_RXBUF_LEN); |
| 761 | #else |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 762 | if (fm_eth->type == FM_ETH_1G_E) |
Timur Tabi | 3038171 | 2011-10-04 16:44:43 -0500 | [diff] [blame] | 763 | init_dtsec(mac, base, phyregs, MAX_RXBUF_LEN); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 764 | else |
| 765 | init_tgec(mac, base, phyregs, MAX_RXBUF_LEN); |
Roy Zang | 111fd19 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 766 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 767 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 768 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 769 | } |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 770 | #else /* CONFIG_DM_ETH */ |
| 771 | static int fm_eth_init_mac(struct fm_eth *fm_eth, void *reg) |
| 772 | { |
| 773 | #ifndef CONFIG_SYS_FMAN_V3 |
| 774 | void *mdio; |
| 775 | #endif |
| 776 | |
| 777 | fm_eth->mac = kzalloc(sizeof(*fm_eth->mac), GFP_KERNEL); |
| 778 | if (!fm_eth->mac) |
| 779 | return -ENOMEM; |
| 780 | |
| 781 | #ifndef CONFIG_SYS_FMAN_V3 |
| 782 | mdio = fman_mdio(fm_eth->dev->parent, fm_eth->mac_type, fm_eth->num); |
| 783 | debug("MDIO %d @ %p\n", fm_eth->num, mdio); |
| 784 | #endif |
| 785 | |
| 786 | switch (fm_eth->mac_type) { |
| 787 | #ifdef CONFIG_SYS_FMAN_V3 |
| 788 | case FM_MEMAC: |
| 789 | init_memac(fm_eth->mac, reg, NULL, MAX_RXBUF_LEN); |
| 790 | break; |
| 791 | #else |
| 792 | case FM_DTSEC: |
| 793 | init_dtsec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN); |
| 794 | break; |
| 795 | case FM_TGEC: |
| 796 | init_tgec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN); |
| 797 | break; |
| 798 | #endif |
| 799 | } |
| 800 | |
| 801 | return 0; |
| 802 | } |
| 803 | #endif /* CONFIG_DM_ETH */ |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 804 | |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 805 | static int init_phy(struct fm_eth *fm_eth) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 806 | { |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 807 | #ifdef CONFIG_PHYLIB |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 808 | u32 supported = PHY_GBIT_FEATURES; |
| 809 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 810 | struct phy_device *phydev = NULL; |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 811 | #endif |
| 812 | |
| 813 | if (fm_eth->type == FM_ETH_10G_E) |
| 814 | supported = PHY_10G_FEATURES; |
| 815 | if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII_2500) |
| 816 | supported |= SUPPORTED_2500baseX_Full; |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 817 | #endif |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 818 | |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 819 | if (fm_eth->type == FM_ETH_1G_E) |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 820 | dtsec_init_phy(fm_eth); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 821 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 822 | #ifdef CONFIG_DM_ETH |
| 823 | #ifdef CONFIG_PHYLIB |
| 824 | #ifdef CONFIG_DM_MDIO |
| 825 | fm_eth->phydev = dm_eth_phy_connect(fm_eth->dev); |
| 826 | if (!fm_eth->phydev) |
| 827 | return -ENODEV; |
| 828 | #endif |
| 829 | fm_eth->phydev->advertising &= supported; |
| 830 | fm_eth->phydev->supported &= supported; |
| 831 | |
| 832 | phy_config(fm_eth->phydev); |
| 833 | #endif |
| 834 | #else /* CONFIG_DM_ETH */ |
Shaohui Xie | 29d8c81 | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 835 | #ifdef CONFIG_PHYLIB |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 836 | if (fm_eth->bus) { |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 837 | phydev = phy_connect(fm_eth->bus, fm_eth->phyaddr, fm_eth->dev, |
| 838 | fm_eth->enet_if); |
Codrin Ciubotariu | 6798c32 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 839 | if (!phydev) { |
| 840 | printf("Failed to connect\n"); |
| 841 | return -1; |
| 842 | } |
| 843 | } else { |
| 844 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | if (fm_eth->type == FM_ETH_1G_E) { |
| 848 | supported = (SUPPORTED_10baseT_Half | |
| 849 | SUPPORTED_10baseT_Full | |
| 850 | SUPPORTED_100baseT_Half | |
| 851 | SUPPORTED_100baseT_Full | |
| 852 | SUPPORTED_1000baseT_Full); |
| 853 | } else { |
| 854 | supported = SUPPORTED_10000baseT_Full; |
| 855 | |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 856 | if (tgec_is_fibre(fm_eth)) |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 857 | phydev->port = PORT_FIBRE; |
| 858 | } |
| 859 | |
| 860 | phydev->supported &= supported; |
| 861 | phydev->advertising = phydev->supported; |
| 862 | |
| 863 | fm_eth->phydev = phydev; |
| 864 | |
| 865 | phy_config(phydev); |
| 866 | #endif |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 867 | #endif /* CONFIG_DM_ETH */ |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 868 | return 0; |
| 869 | } |
| 870 | |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 871 | #ifndef CONFIG_DM_ETH |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 872 | int fm_eth_initialize(struct ccsr_fman *reg, struct fm_eth_info *info) |
| 873 | { |
| 874 | struct eth_device *dev; |
| 875 | struct fm_eth *fm_eth; |
| 876 | int i, num = info->num; |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 877 | int ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 878 | |
| 879 | /* alloc eth device */ |
| 880 | dev = (struct eth_device *)malloc(sizeof(struct eth_device)); |
| 881 | if (!dev) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 882 | return -ENOMEM; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 883 | memset(dev, 0, sizeof(struct eth_device)); |
| 884 | |
| 885 | /* alloc the FMan ethernet private struct */ |
| 886 | fm_eth = (struct fm_eth *)malloc(sizeof(struct fm_eth)); |
| 887 | if (!fm_eth) |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 888 | return -ENOMEM; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 889 | memset(fm_eth, 0, sizeof(struct fm_eth)); |
| 890 | |
| 891 | /* save off some things we need from the info struct */ |
| 892 | fm_eth->fm_index = info->index - 1; /* keep as 0 based for muram */ |
| 893 | fm_eth->num = num; |
| 894 | fm_eth->type = info->type; |
| 895 | |
| 896 | fm_eth->rx_port = (void *)®->port[info->rx_port_id - 1].fm_bmi; |
| 897 | fm_eth->tx_port = (void *)®->port[info->tx_port_id - 1].fm_bmi; |
| 898 | |
| 899 | /* set the ethernet max receive length */ |
| 900 | fm_eth->max_rx_len = MAX_RXBUF_LEN; |
| 901 | |
| 902 | /* init global mac structure */ |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 903 | ret = fm_eth_init_mac(fm_eth, reg); |
| 904 | if (ret) |
| 905 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 906 | |
| 907 | /* keep same as the manual, we call FMAN1, FMAN2, DTSEC1, DTSEC2, etc */ |
| 908 | if (fm_eth->type == FM_ETH_1G_E) |
| 909 | sprintf(dev->name, "FM%d@DTSEC%d", info->index, num + 1); |
| 910 | else |
| 911 | sprintf(dev->name, "FM%d@TGEC%d", info->index, num + 1); |
| 912 | |
| 913 | devlist[num_controllers++] = dev; |
| 914 | dev->iobase = 0; |
| 915 | dev->priv = (void *)fm_eth; |
| 916 | dev->init = fm_eth_open; |
| 917 | dev->halt = fm_eth_halt; |
| 918 | dev->send = fm_eth_send; |
| 919 | dev->recv = fm_eth_recv; |
| 920 | fm_eth->dev = dev; |
| 921 | fm_eth->bus = info->bus; |
| 922 | fm_eth->phyaddr = info->phy_addr; |
| 923 | fm_eth->enet_if = info->enet_if; |
| 924 | |
| 925 | /* startup the FM im */ |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 926 | ret = fm_eth_startup(fm_eth); |
| 927 | if (ret) |
| 928 | return ret; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 929 | |
Madalin Bucur | 8313cb0 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 930 | init_phy(fm_eth); |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 931 | |
| 932 | /* clear the ethernet address */ |
| 933 | for (i = 0; i < 6; i++) |
| 934 | dev->enetaddr[i] = 0; |
| 935 | eth_register(dev); |
| 936 | |
Hou Zhiqiang | 0f2cb9f | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 937 | return 0; |
Kumar Gala | c916d7c | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 938 | } |
Madalin Bucur | 6eb32a0 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 939 | #else /* CONFIG_DM_ETH */ |
| 940 | #ifdef CONFIG_PHYLIB |
| 941 | phy_interface_t fman_read_sys_if(struct udevice *dev) |
| 942 | { |
| 943 | const char *if_str; |
| 944 | |
| 945 | if_str = ofnode_read_string(dev->node, "phy-connection-type"); |
| 946 | debug("MAC system interface mode %s\n", if_str); |
| 947 | |
| 948 | return phy_get_interface_by_name(if_str); |
| 949 | } |
| 950 | #endif |
| 951 | |
| 952 | static int fm_eth_bind(struct udevice *dev) |
| 953 | { |
| 954 | char mac_name[11]; |
| 955 | u32 fm, num; |
| 956 | |
| 957 | if (ofnode_read_u32(ofnode_get_parent(dev->node), "cell-index", &fm)) { |
| 958 | printf("FMan node property cell-index missing\n"); |
| 959 | return -EINVAL; |
| 960 | } |
| 961 | |
| 962 | if (dev && dev_read_u32(dev, "cell-index", &num)) { |
| 963 | printf("FMan MAC node property cell-index missing\n"); |
| 964 | return -EINVAL; |
| 965 | } |
| 966 | |
| 967 | sprintf(mac_name, "fm%d-mac%d", fm + 1, num + 1); |
| 968 | device_set_name(dev, mac_name); |
| 969 | |
| 970 | debug("%s - binding %s\n", __func__, mac_name); |
| 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static struct udevice *fm_get_internal_mdio(struct udevice *dev) |
| 976 | { |
| 977 | struct ofnode_phandle_args phandle = {.node = ofnode_null()}; |
| 978 | struct udevice *mdiodev; |
| 979 | |
| 980 | if (dev_read_phandle_with_args(dev, "pcsphy-handle", NULL, |
| 981 | 0, 0, &phandle) || |
| 982 | !ofnode_valid(phandle.node)) { |
| 983 | if (dev_read_phandle_with_args(dev, "tbi-handle", NULL, |
| 984 | 0, 0, &phandle) || |
| 985 | !ofnode_valid(phandle.node)) { |
| 986 | printf("Issue reading pcsphy-handle/tbi-handle for MAC %s\n", |
| 987 | dev->name); |
| 988 | return NULL; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
| 993 | ofnode_get_parent(phandle.node), |
| 994 | &mdiodev)) { |
| 995 | printf("can't find MDIO bus for node %s\n", |
| 996 | ofnode_get_name(ofnode_get_parent(phandle.node))); |
| 997 | return NULL; |
| 998 | } |
| 999 | debug("Found internal MDIO bus %p\n", mdiodev); |
| 1000 | |
| 1001 | return mdiodev; |
| 1002 | } |
| 1003 | |
| 1004 | static int fm_eth_probe(struct udevice *dev) |
| 1005 | { |
| 1006 | struct fm_eth *fm_eth = (struct fm_eth *)dev->priv; |
| 1007 | struct ofnode_phandle_args args; |
| 1008 | void *reg; |
| 1009 | int ret, index; |
| 1010 | |
| 1011 | debug("%s enter for dev %p fm_eth %p - %s\n", __func__, dev, fm_eth, |
| 1012 | (dev) ? dev->name : "-"); |
| 1013 | |
| 1014 | if (fm_eth->dev) { |
| 1015 | printf("%s already probed, exit\n", (dev) ? dev->name : "-"); |
| 1016 | return 0; |
| 1017 | } |
| 1018 | |
| 1019 | fm_eth->dev = dev; |
| 1020 | fm_eth->fm_index = fman_id(dev->parent); |
| 1021 | reg = (void *)(uintptr_t)dev_read_addr(dev); |
| 1022 | fm_eth->mac_type = dev_get_driver_data(dev); |
| 1023 | #ifdef CONFIG_PHYLIB |
| 1024 | fm_eth->enet_if = fman_read_sys_if(dev); |
| 1025 | #else |
| 1026 | fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII; |
| 1027 | printf("%s: warning - unable to determine interface type\n", __func__); |
| 1028 | #endif |
| 1029 | switch (fm_eth->mac_type) { |
| 1030 | #ifndef CONFIG_SYS_FMAN_V3 |
| 1031 | case FM_TGEC: |
| 1032 | fm_eth->type = FM_ETH_10G_E; |
| 1033 | break; |
| 1034 | case FM_DTSEC: |
| 1035 | #else |
| 1036 | case FM_MEMAC: |
| 1037 | /* default to 1G, 10G is indicated by port property in dts */ |
| 1038 | #endif |
| 1039 | fm_eth->type = FM_ETH_1G_E; |
| 1040 | break; |
| 1041 | } |
| 1042 | |
| 1043 | if (dev_read_u32(dev, "cell-index", &fm_eth->num)) { |
| 1044 | printf("FMan MAC node property cell-index missing\n"); |
| 1045 | return -EINVAL; |
| 1046 | } |
| 1047 | |
| 1048 | if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL, |
| 1049 | 0, 0, &args)) |
| 1050 | goto ports_ref_failure; |
| 1051 | index = ofnode_read_u32_default(args.node, "cell-index", 0); |
| 1052 | if (index <= 0) |
| 1053 | goto ports_ref_failure; |
| 1054 | fm_eth->rx_port = fman_port(dev->parent, index); |
| 1055 | |
| 1056 | if (ofnode_read_bool(args.node, "fsl,fman-10g-port")) |
| 1057 | fm_eth->type = FM_ETH_10G_E; |
| 1058 | |
| 1059 | if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL, |
| 1060 | 0, 1, &args)) |
| 1061 | goto ports_ref_failure; |
| 1062 | index = ofnode_read_u32_default(args.node, "cell-index", 0); |
| 1063 | if (index <= 0) |
| 1064 | goto ports_ref_failure; |
| 1065 | fm_eth->tx_port = fman_port(dev->parent, index); |
| 1066 | |
| 1067 | /* set the ethernet max receive length */ |
| 1068 | fm_eth->max_rx_len = MAX_RXBUF_LEN; |
| 1069 | |
| 1070 | switch (fm_eth->enet_if) { |
| 1071 | case PHY_INTERFACE_MODE_QSGMII: |
| 1072 | /* all PCS blocks are accessed on one controller */ |
| 1073 | if (fm_eth->num != 0) |
| 1074 | break; |
| 1075 | case PHY_INTERFACE_MODE_SGMII: |
| 1076 | case PHY_INTERFACE_MODE_SGMII_2500: |
| 1077 | fm_eth->pcs_mdio = fm_get_internal_mdio(dev); |
| 1078 | break; |
| 1079 | default: |
| 1080 | break; |
| 1081 | } |
| 1082 | |
| 1083 | /* init global mac structure */ |
| 1084 | ret = fm_eth_init_mac(fm_eth, reg); |
| 1085 | if (ret) |
| 1086 | return ret; |
| 1087 | |
| 1088 | /* startup the FM im */ |
| 1089 | ret = fm_eth_startup(fm_eth); |
| 1090 | |
| 1091 | if (!ret) |
| 1092 | ret = init_phy(fm_eth); |
| 1093 | |
| 1094 | return ret; |
| 1095 | |
| 1096 | ports_ref_failure: |
| 1097 | printf("Issue reading fsl,fman-ports for MAC %s\n", dev->name); |
| 1098 | return -ENOENT; |
| 1099 | } |
| 1100 | |
| 1101 | static int fm_eth_remove(struct udevice *dev) |
| 1102 | { |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
| 1106 | static const struct eth_ops fm_eth_ops = { |
| 1107 | .start = fm_eth_open, |
| 1108 | .send = fm_eth_send, |
| 1109 | .recv = fm_eth_recv, |
| 1110 | .free_pkt = fm_eth_free_pkt, |
| 1111 | .stop = fm_eth_halt, |
| 1112 | }; |
| 1113 | |
| 1114 | static const struct udevice_id fm_eth_ids[] = { |
| 1115 | #ifdef CONFIG_SYS_FMAN_V3 |
| 1116 | { .compatible = "fsl,fman-memac", .data = FM_MEMAC }, |
| 1117 | #else |
| 1118 | { .compatible = "fsl,fman-dtsec", .data = FM_DTSEC }, |
| 1119 | { .compatible = "fsl,fman-xgec", .data = FM_TGEC }, |
| 1120 | #endif |
| 1121 | {} |
| 1122 | }; |
| 1123 | |
| 1124 | U_BOOT_DRIVER(eth_fman) = { |
| 1125 | .name = "eth_fman", |
| 1126 | .id = UCLASS_ETH, |
| 1127 | .of_match = fm_eth_ids, |
| 1128 | .bind = fm_eth_bind, |
| 1129 | .probe = fm_eth_probe, |
| 1130 | .remove = fm_eth_remove, |
| 1131 | .ops = &fm_eth_ops, |
| 1132 | .priv_auto_alloc_size = sizeof(struct fm_eth), |
| 1133 | .platdata_auto_alloc_size = sizeof(struct eth_pdata), |
| 1134 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 1135 | }; |
| 1136 | #endif /* CONFIG_DM_ETH */ |