Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 2 | /* |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 3 | * Copyright 2009-2012 Freescale Semiconductor, Inc. |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 4 | * Copyright 2020 NXP |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 5 | * Dave Liu <daveliu@freescale.com> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 6 | */ |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 7 | #include <config.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 8 | #include <log.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 9 | #include <part.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 10 | #include <asm/io.h> |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <dm/ofnode.h> |
| 13 | #include <linux/compat.h> |
| 14 | #include <phy_interface.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 15 | #include <malloc.h> |
| 16 | #include <net.h> |
| 17 | #include <hwconfig.h> |
| 18 | #include <fm_eth.h> |
| 19 | #include <fsl_mdio.h> |
| 20 | #include <miiphy.h> |
| 21 | #include <phy.h> |
Shaohui Xie | 513eaf2 | 2015-10-26 19:47:47 +0800 | [diff] [blame] | 22 | #include <fsl_dtsec.h> |
| 23 | #include <fsl_tgec.h> |
Shaohui Xie | 835c72b | 2015-03-20 19:28:19 -0700 | [diff] [blame] | 24 | #include <fsl_memac.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 25 | #include <linux/delay.h> |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 26 | |
| 27 | #include "fm.h" |
| 28 | |
Jerome Forissier | edb410b | 2024-09-11 11:58:18 +0200 | [diff] [blame^] | 29 | #if ((defined(CONFIG_MII) || defined(CONFIG_CMD_MII)) && \ |
| 30 | !defined(CONFIG_BITBANGMII)) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 31 | |
| 32 | #define TBIANA_SETTINGS (TBIANA_ASYMMETRIC_PAUSE | TBIANA_SYMMETRIC_PAUSE | \ |
| 33 | TBIANA_FULL_DUPLEX) |
| 34 | |
| 35 | #define TBIANA_SGMII_ACK 0x4001 |
| 36 | |
| 37 | #define TBICR_SETTINGS (TBICR_ANEG_ENABLE | TBICR_RESTART_ANEG | \ |
| 38 | TBICR_FULL_DUPLEX | TBICR_SPEED1_SET) |
| 39 | |
| 40 | /* Configure the TBI for SGMII operation */ |
Kim Phillips | 914b078 | 2012-10-29 13:34:34 +0000 | [diff] [blame] | 41 | static void dtsec_configure_serdes(struct fm_eth *priv) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 42 | { |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 43 | #ifdef CONFIG_SYS_FMAN_V3 |
| 44 | u32 value; |
| 45 | struct mii_dev bus; |
Shengzhou Liu | 9540368 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 46 | bool sgmii_2500 = (priv->enet_if == |
Vladimir Oltean | 6caef97 | 2021-09-18 15:32:35 +0300 | [diff] [blame] | 47 | PHY_INTERFACE_MODE_2500BASEX) ? true : false; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 48 | int i = 0, j; |
| 49 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 50 | bus.priv = priv->pcs_mdio; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 51 | bus.read = memac_mdio_read; |
| 52 | bus.write = memac_mdio_write; |
| 53 | bus.reset = memac_mdio_reset; |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 54 | |
Shaohui Xie | 1b29512 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 55 | qsgmii_loop: |
Shengzhou Liu | 9540368 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 56 | /* SGMII IF mode + AN enable only for 1G SGMII, not for 2.5G */ |
shaohui xie | a3384a1 | 2016-11-15 14:36:47 +0800 | [diff] [blame] | 57 | if (sgmii_2500) |
| 58 | value = PHY_SGMII_CR_PHY_RESET | |
| 59 | PHY_SGMII_IF_SPEED_GIGABIT | |
| 60 | PHY_SGMII_IF_MODE_SGMII; |
| 61 | else |
| 62 | value = PHY_SGMII_IF_MODE_SGMII | PHY_SGMII_IF_MODE_AN; |
Shengzhou Liu | 9540368 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 63 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 64 | for (j = 0; j <= 3; j++) |
| 65 | debug("dump PCS reg %#x: %#x\n", j, |
| 66 | memac_mdio_read(&bus, i, MDIO_DEVAD_NONE, j)); |
| 67 | |
Shaohui Xie | 1b29512 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 68 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x14, value); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 69 | |
| 70 | /* Dev ability according to SGMII specification */ |
| 71 | value = PHY_SGMII_DEV_ABILITY_SGMII; |
Shaohui Xie | 1b29512 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 72 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x4, value); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 73 | |
shaohui xie | a3384a1 | 2016-11-15 14:36:47 +0800 | [diff] [blame] | 74 | if (sgmii_2500) { |
| 75 | /* Adjust link timer for 2.5G SGMII, |
| 76 | * 1.6 ms in units of 3.2 ns: |
| 77 | * 1.6ms / 3.2ns = 5 * 10^5 = 0x7a120. |
| 78 | */ |
| 79 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0007); |
| 80 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0xa120); |
| 81 | } else { |
| 82 | /* Adjust link timer for SGMII, |
| 83 | * 1.6 ms in units of 8 ns: |
| 84 | * 1.6ms / 8ns = 2 * 10^5 = 0x30d40. |
| 85 | */ |
| 86 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x13, 0x0003); |
| 87 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0x12, 0x0d40); |
| 88 | } |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 89 | |
| 90 | /* Restart AN */ |
shaohui xie | a3384a1 | 2016-11-15 14:36:47 +0800 | [diff] [blame] | 91 | value = PHY_SGMII_CR_DEF_VAL | PHY_SGMII_CR_RESET_AN; |
Shaohui Xie | 1b29512 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 92 | memac_mdio_write(&bus, i, MDIO_DEVAD_NONE, 0, value); |
| 93 | |
| 94 | if ((priv->enet_if == PHY_INTERFACE_MODE_QSGMII) && (i < 3)) { |
| 95 | i++; |
| 96 | goto qsgmii_loop; |
| 97 | } |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 98 | #else |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 99 | struct dtsec *regs = priv->mac->base; |
| 100 | struct tsec_mii_mng *phyregs = priv->mac->phyregs; |
| 101 | |
| 102 | /* |
| 103 | * Access TBI PHY registers at given TSEC register offset as |
| 104 | * opposed to the register offset used for external PHY accesses |
| 105 | */ |
| 106 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_TBICON, |
| 107 | TBICON_CLK_SELECT); |
| 108 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, TBI_ANA, |
| 109 | TBIANA_SGMII_ACK); |
| 110 | tsec_local_mdio_write(phyregs, in_be32(®s->tbipa), 0, |
| 111 | TBI_CR, TBICR_SETTINGS); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 112 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Madalin Bucur | 11fa7a3 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 115 | static void dtsec_init_phy(struct fm_eth *fm_eth) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 116 | { |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 117 | #ifndef CONFIG_SYS_FMAN_V3 |
Tom Rini | 376b88a | 2022-10-28 20:27:13 -0400 | [diff] [blame] | 118 | struct dtsec *regs = (struct dtsec *)CFG_SYS_FSL_FM1_DTSEC1_ADDR; |
shaohui xie | 842e59e | 2012-10-11 20:25:36 +0000 | [diff] [blame] | 119 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 120 | /* Assign a Physical address to the TBI */ |
Tom Rini | 6a5dccc | 2022-11-16 13:10:41 -0500 | [diff] [blame] | 121 | out_be32(®s->tbipa, CFG_SYS_TBIPA_VALUE); |
Roy Zang | bafd803 | 2012-10-08 07:44:21 +0000 | [diff] [blame] | 122 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 123 | |
Shengzhou Liu | 9540368 | 2014-10-23 17:20:57 +0800 | [diff] [blame] | 124 | if (fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII || |
Shaohui Xie | 1b29512 | 2015-10-26 19:47:48 +0800 | [diff] [blame] | 125 | fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII || |
Vladimir Oltean | 6caef97 | 2021-09-18 15:32:35 +0300 | [diff] [blame] | 126 | fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 127 | dtsec_configure_serdes(fm_eth); |
| 128 | } |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 129 | #endif |
| 130 | |
| 131 | static u16 muram_readw(u16 *addr) |
| 132 | { |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 133 | ulong base = (ulong)addr & ~0x3UL; |
| 134 | u32 val32 = in_be32((void *)base); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 135 | int byte_pos; |
| 136 | u16 ret; |
| 137 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 138 | byte_pos = (ulong)addr & 0x3UL; |
Kumar Gala | 2683c53 | 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 | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 149 | ulong base = (ulong)addr & ~0x3UL; |
| 150 | u32 org32 = in_be32((void *)base); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 151 | u32 val32; |
| 152 | int byte_pos; |
| 153 | |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 154 | byte_pos = (ulong)addr & 0x3UL; |
Kumar Gala | 2683c53 | 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 | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 160 | out_be32((void *)base, val32); |
Kumar Gala | 2683c53 | 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 | ; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 172 | if (!timeout) |
| 173 | printf("%s - timeout\n", __func__); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static void bmi_rx_port_init(struct fm_bmi_rx_port *rx_port) |
| 177 | { |
| 178 | /* set BMI to independent mode, Rx port disable */ |
| 179 | out_be32(&rx_port->fmbm_rcfg, FMBM_RCFG_IM); |
| 180 | /* clear FOF in IM case */ |
| 181 | out_be32(&rx_port->fmbm_rim, 0); |
| 182 | /* Rx frame next engine -RISC */ |
| 183 | out_be32(&rx_port->fmbm_rfne, NIA_ENG_RISC | NIA_RISC_AC_IM_RX); |
| 184 | /* Rx command attribute - no order, MR[3] = 1 */ |
| 185 | clrbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_ORDER | FMBM_RFCA_MR_MASK); |
| 186 | setbits_be32(&rx_port->fmbm_rfca, FMBM_RFCA_MR(4)); |
| 187 | /* enable Rx statistic counters */ |
| 188 | out_be32(&rx_port->fmbm_rstc, FMBM_RSTC_EN); |
| 189 | /* disable Rx performance counters */ |
| 190 | out_be32(&rx_port->fmbm_rpc, 0); |
| 191 | } |
| 192 | |
| 193 | static void bmi_tx_port_disable(struct fm_bmi_tx_port *tx_port) |
| 194 | { |
| 195 | int timeout = 1000000; |
| 196 | |
| 197 | clrbits_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_EN); |
| 198 | |
| 199 | /* wait until the tx port is not busy */ |
| 200 | while ((in_be32(&tx_port->fmbm_tst) & FMBM_TST_BSY) && timeout--) |
| 201 | ; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 202 | if (!timeout) |
| 203 | printf("%s - timeout\n", __func__); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | static void bmi_tx_port_init(struct fm_bmi_tx_port *tx_port) |
| 207 | { |
| 208 | /* set BMI to independent mode, Tx port disable */ |
| 209 | out_be32(&tx_port->fmbm_tcfg, FMBM_TCFG_IM); |
| 210 | /* Tx frame next engine -RISC */ |
| 211 | out_be32(&tx_port->fmbm_tfne, NIA_ENG_RISC | NIA_RISC_AC_IM_TX); |
| 212 | out_be32(&tx_port->fmbm_tfene, NIA_ENG_RISC | NIA_RISC_AC_IM_TX); |
| 213 | /* Tx command attribute - no order, MR[3] = 1 */ |
| 214 | clrbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_ORDER | FMBM_TFCA_MR_MASK); |
| 215 | setbits_be32(&tx_port->fmbm_tfca, FMBM_TFCA_MR(4)); |
| 216 | /* enable Tx statistic counters */ |
| 217 | out_be32(&tx_port->fmbm_tstc, FMBM_TSTC_EN); |
| 218 | /* disable Tx performance counters */ |
| 219 | out_be32(&tx_port->fmbm_tpc, 0); |
| 220 | } |
| 221 | |
| 222 | static int fm_eth_rx_port_parameter_init(struct fm_eth *fm_eth) |
| 223 | { |
| 224 | struct fm_port_global_pram *pram; |
| 225 | u32 pram_page_offset; |
| 226 | void *rx_bd_ring_base; |
| 227 | void *rx_buf_pool; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 228 | u32 bd_ring_base_lo, bd_ring_base_hi; |
| 229 | u32 buf_lo, buf_hi; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 230 | struct fm_port_bd *rxbd; |
| 231 | struct fm_port_qd *rxqd; |
| 232 | struct fm_bmi_rx_port *bmi_rx_port = fm_eth->rx_port; |
| 233 | int i; |
| 234 | |
| 235 | /* alloc global parameter ram at MURAM */ |
| 236 | pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index, |
| 237 | FM_PRAM_SIZE, FM_PRAM_ALIGN); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 238 | if (!pram) { |
| 239 | printf("%s: No muram for Rx global parameter\n", __func__); |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 240 | return -ENOMEM; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 241 | } |
| 242 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 243 | fm_eth->rx_pram = pram; |
| 244 | |
| 245 | /* parameter page offset to MURAM */ |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 246 | pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 247 | |
| 248 | /* enable global mode- snooping data buffers and BDs */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 249 | out_be32(&pram->mode, PRAM_MODE_GLOBAL); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 250 | |
| 251 | /* init the Rx queue descriptor pionter */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 252 | out_be32(&pram->rxqd_ptr, pram_page_offset + 0x20); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 253 | |
| 254 | /* set the max receive buffer length, power of 2 */ |
| 255 | muram_writew(&pram->mrblr, MAX_RXBUF_LOG2); |
| 256 | |
| 257 | /* alloc Rx buffer descriptors from main memory */ |
| 258 | rx_bd_ring_base = malloc(sizeof(struct fm_port_bd) |
| 259 | * RX_BD_RING_SIZE); |
| 260 | if (!rx_bd_ring_base) |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 261 | return -ENOMEM; |
| 262 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 263 | memset(rx_bd_ring_base, 0, sizeof(struct fm_port_bd) |
| 264 | * RX_BD_RING_SIZE); |
| 265 | |
| 266 | /* alloc Rx buffer from main memory */ |
| 267 | rx_buf_pool = malloc(MAX_RXBUF_LEN * RX_BD_RING_SIZE); |
Hou Zhiqiang | 46bcbeb | 2021-06-12 21:15:41 +0300 | [diff] [blame] | 268 | if (!rx_buf_pool) { |
| 269 | free(rx_bd_ring_base); |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 270 | return -ENOMEM; |
Hou Zhiqiang | 46bcbeb | 2021-06-12 21:15:41 +0300 | [diff] [blame] | 271 | } |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 272 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 273 | memset(rx_buf_pool, 0, MAX_RXBUF_LEN * RX_BD_RING_SIZE); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 274 | debug("%s: rx_buf_pool = %p\n", __func__, rx_buf_pool); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 275 | |
| 276 | /* save them to fm_eth */ |
| 277 | fm_eth->rx_bd_ring = rx_bd_ring_base; |
| 278 | fm_eth->cur_rxbd = rx_bd_ring_base; |
| 279 | fm_eth->rx_buf = rx_buf_pool; |
| 280 | |
| 281 | /* init Rx BDs ring */ |
| 282 | rxbd = (struct fm_port_bd *)rx_bd_ring_base; |
| 283 | for (i = 0; i < RX_BD_RING_SIZE; i++) { |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 284 | muram_writew(&rxbd->status, RxBD_EMPTY); |
| 285 | muram_writew(&rxbd->len, 0); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 286 | buf_hi = upper_32_bits(virt_to_phys(rx_buf_pool + |
| 287 | i * MAX_RXBUF_LEN)); |
| 288 | buf_lo = lower_32_bits(virt_to_phys(rx_buf_pool + |
| 289 | i * MAX_RXBUF_LEN)); |
| 290 | muram_writew(&rxbd->buf_ptr_hi, (u16)buf_hi); |
| 291 | out_be32(&rxbd->buf_ptr_lo, buf_lo); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 292 | rxbd++; |
| 293 | } |
| 294 | |
| 295 | /* set the Rx queue descriptor */ |
| 296 | rxqd = &pram->rxqd; |
| 297 | muram_writew(&rxqd->gen, 0); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 298 | bd_ring_base_hi = upper_32_bits(virt_to_phys(rx_bd_ring_base)); |
| 299 | bd_ring_base_lo = lower_32_bits(virt_to_phys(rx_bd_ring_base)); |
| 300 | muram_writew(&rxqd->bd_ring_base_hi, (u16)bd_ring_base_hi); |
| 301 | out_be32(&rxqd->bd_ring_base_lo, bd_ring_base_lo); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 302 | muram_writew(&rxqd->bd_ring_size, sizeof(struct fm_port_bd) |
| 303 | * RX_BD_RING_SIZE); |
| 304 | muram_writew(&rxqd->offset_in, 0); |
| 305 | muram_writew(&rxqd->offset_out, 0); |
| 306 | |
| 307 | /* set IM parameter ram pointer to Rx Frame Queue ID */ |
| 308 | out_be32(&bmi_rx_port->fmbm_rfqid, pram_page_offset); |
| 309 | |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 310 | return 0; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int fm_eth_tx_port_parameter_init(struct fm_eth *fm_eth) |
| 314 | { |
| 315 | struct fm_port_global_pram *pram; |
| 316 | u32 pram_page_offset; |
| 317 | void *tx_bd_ring_base; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 318 | u32 bd_ring_base_lo, bd_ring_base_hi; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 319 | struct fm_port_bd *txbd; |
| 320 | struct fm_port_qd *txqd; |
| 321 | struct fm_bmi_tx_port *bmi_tx_port = fm_eth->tx_port; |
| 322 | int i; |
| 323 | |
| 324 | /* alloc global parameter ram at MURAM */ |
| 325 | pram = (struct fm_port_global_pram *)fm_muram_alloc(fm_eth->fm_index, |
| 326 | FM_PRAM_SIZE, FM_PRAM_ALIGN); |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 327 | if (!pram) { |
| 328 | printf("%s: No muram for Tx global parameter\n", __func__); |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 329 | return -ENOMEM; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 330 | } |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 331 | fm_eth->tx_pram = pram; |
| 332 | |
| 333 | /* parameter page offset to MURAM */ |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 334 | pram_page_offset = (void *)pram - fm_muram_base(fm_eth->fm_index); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 335 | |
| 336 | /* enable global mode- snooping data buffers and BDs */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 337 | out_be32(&pram->mode, PRAM_MODE_GLOBAL); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 338 | |
| 339 | /* init the Tx queue descriptor pionter */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 340 | out_be32(&pram->txqd_ptr, pram_page_offset + 0x40); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 341 | |
| 342 | /* alloc Tx buffer descriptors from main memory */ |
| 343 | tx_bd_ring_base = malloc(sizeof(struct fm_port_bd) |
| 344 | * TX_BD_RING_SIZE); |
| 345 | if (!tx_bd_ring_base) |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 346 | return -ENOMEM; |
| 347 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 348 | memset(tx_bd_ring_base, 0, sizeof(struct fm_port_bd) |
| 349 | * TX_BD_RING_SIZE); |
| 350 | /* save it to fm_eth */ |
| 351 | fm_eth->tx_bd_ring = tx_bd_ring_base; |
| 352 | fm_eth->cur_txbd = tx_bd_ring_base; |
| 353 | |
| 354 | /* init Tx BDs ring */ |
| 355 | txbd = (struct fm_port_bd *)tx_bd_ring_base; |
| 356 | for (i = 0; i < TX_BD_RING_SIZE; i++) { |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 357 | muram_writew(&txbd->status, TxBD_LAST); |
| 358 | muram_writew(&txbd->len, 0); |
| 359 | muram_writew(&txbd->buf_ptr_hi, 0); |
| 360 | out_be32(&txbd->buf_ptr_lo, 0); |
| 361 | txbd++; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* set the Tx queue decriptor */ |
| 365 | txqd = &pram->txqd; |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 366 | bd_ring_base_hi = upper_32_bits(virt_to_phys(tx_bd_ring_base)); |
| 367 | bd_ring_base_lo = lower_32_bits(virt_to_phys(tx_bd_ring_base)); |
| 368 | muram_writew(&txqd->bd_ring_base_hi, (u16)bd_ring_base_hi); |
| 369 | out_be32(&txqd->bd_ring_base_lo, bd_ring_base_lo); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 370 | muram_writew(&txqd->bd_ring_size, sizeof(struct fm_port_bd) |
| 371 | * TX_BD_RING_SIZE); |
| 372 | muram_writew(&txqd->offset_in, 0); |
| 373 | muram_writew(&txqd->offset_out, 0); |
| 374 | |
| 375 | /* set IM parameter ram pointer to Tx Confirmation Frame Queue ID */ |
| 376 | out_be32(&bmi_tx_port->fmbm_tcfqid, pram_page_offset); |
| 377 | |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 378 | return 0; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static int fm_eth_init(struct fm_eth *fm_eth) |
| 382 | { |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 383 | int ret; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 384 | |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 385 | ret = fm_eth_rx_port_parameter_init(fm_eth); |
| 386 | if (ret) |
| 387 | return ret; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 388 | |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 389 | ret = fm_eth_tx_port_parameter_init(fm_eth); |
| 390 | if (ret) |
| 391 | return ret; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 392 | |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 393 | return 0; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static int fm_eth_startup(struct fm_eth *fm_eth) |
| 397 | { |
| 398 | struct fsl_enet_mac *mac; |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 399 | int ret; |
| 400 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 401 | mac = fm_eth->mac; |
| 402 | |
| 403 | /* Rx/TxBDs, Rx/TxQDs, Rx buff and parameter ram init */ |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 404 | ret = fm_eth_init(fm_eth); |
| 405 | if (ret) |
| 406 | return ret; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 407 | /* setup the MAC controller */ |
| 408 | mac->init_mac(mac); |
| 409 | |
| 410 | /* For some reason we need to set SPEED_100 */ |
Shaohui Xie | c218d29 | 2013-08-19 18:58:52 +0800 | [diff] [blame] | 411 | if (((fm_eth->enet_if == PHY_INTERFACE_MODE_SGMII) || |
Vladimir Oltean | 6caef97 | 2021-09-18 15:32:35 +0300 | [diff] [blame] | 412 | (fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX) || |
Shaohui Xie | c218d29 | 2013-08-19 18:58:52 +0800 | [diff] [blame] | 413 | (fm_eth->enet_if == PHY_INTERFACE_MODE_QSGMII)) && |
| 414 | mac->set_if_mode) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 415 | mac->set_if_mode(mac, fm_eth->enet_if, SPEED_100); |
| 416 | |
| 417 | /* init bmi rx port, IM mode and disable */ |
| 418 | bmi_rx_port_init(fm_eth->rx_port); |
| 419 | /* init bmi tx port, IM mode and disable */ |
| 420 | bmi_tx_port_init(fm_eth->tx_port); |
| 421 | |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 422 | return 0; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 423 | } |
| 424 | |
| 425 | static void fmc_tx_port_graceful_stop_enable(struct fm_eth *fm_eth) |
| 426 | { |
| 427 | struct fm_port_global_pram *pram; |
| 428 | |
| 429 | pram = fm_eth->tx_pram; |
| 430 | /* graceful stop transmission of frames */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 431 | setbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 432 | sync(); |
| 433 | } |
| 434 | |
| 435 | static void fmc_tx_port_graceful_stop_disable(struct fm_eth *fm_eth) |
| 436 | { |
| 437 | struct fm_port_global_pram *pram; |
| 438 | |
| 439 | pram = fm_eth->tx_pram; |
| 440 | /* re-enable transmission of frames */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 441 | clrbits_be32(&pram->mode, PRAM_MODE_GRACEFUL_STOP); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 442 | sync(); |
| 443 | } |
| 444 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 445 | static int fm_eth_open(struct udevice *dev) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 446 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 447 | struct eth_pdata *pdata = dev_get_plat(dev); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 448 | struct fm_eth *fm_eth = dev_get_priv(dev); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 449 | unsigned char *enetaddr; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 450 | struct fsl_enet_mac *mac; |
Timur Tabi | 4238746 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 451 | #ifdef CONFIG_PHYLIB |
| 452 | int ret; |
| 453 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 454 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 455 | mac = fm_eth->mac; |
| 456 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 457 | enetaddr = pdata->enetaddr; |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 458 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 459 | /* setup the MAC address */ |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 460 | if (enetaddr[0] & 0x01) { |
| 461 | printf("%s: MacAddress is multicast address\n", __func__); |
| 462 | enetaddr[0] = 0; |
| 463 | enetaddr[5] = fm_eth->num; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 464 | } |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 465 | mac->set_mac_addr(mac, enetaddr); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 466 | |
| 467 | /* enable bmi Rx port */ |
| 468 | setbits_be32(&fm_eth->rx_port->fmbm_rcfg, FMBM_RCFG_EN); |
| 469 | /* enable MAC rx/tx port */ |
| 470 | mac->enable_mac(mac); |
| 471 | /* enable bmi Tx port */ |
| 472 | setbits_be32(&fm_eth->tx_port->fmbm_tcfg, FMBM_TCFG_EN); |
| 473 | /* re-enable transmission of frame */ |
| 474 | fmc_tx_port_graceful_stop_disable(fm_eth); |
| 475 | |
| 476 | #ifdef CONFIG_PHYLIB |
Codrin Ciubotariu | 1ee90f9 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 477 | if (fm_eth->phydev) { |
| 478 | ret = phy_startup(fm_eth->phydev); |
| 479 | if (ret) { |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 480 | printf("%s: Could not initialize\n", dev->name); |
Codrin Ciubotariu | 1ee90f9 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 481 | return ret; |
| 482 | } |
| 483 | } else { |
| 484 | return 0; |
Timur Tabi | 4238746 | 2012-07-09 08:52:43 +0000 | [diff] [blame] | 485 | } |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 486 | #else |
| 487 | fm_eth->phydev->speed = SPEED_1000; |
| 488 | fm_eth->phydev->link = 1; |
| 489 | fm_eth->phydev->duplex = DUPLEX_FULL; |
| 490 | #endif |
| 491 | |
| 492 | /* set the MAC-PHY mode */ |
| 493 | mac->set_if_mode(mac, fm_eth->enet_if, fm_eth->phydev->speed); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 494 | debug("MAC IF mode %d, speed %d, link %d\n", fm_eth->enet_if, |
| 495 | fm_eth->phydev->speed, fm_eth->phydev->link); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 496 | |
| 497 | if (!fm_eth->phydev->link) |
| 498 | printf("%s: No link.\n", fm_eth->phydev->dev->name); |
| 499 | |
| 500 | return fm_eth->phydev->link ? 0 : -1; |
| 501 | } |
| 502 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 503 | static void fm_eth_halt(struct udevice *dev) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 504 | { |
| 505 | struct fm_eth *fm_eth; |
| 506 | struct fsl_enet_mac *mac; |
| 507 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 508 | fm_eth = dev_get_priv(dev); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 509 | mac = fm_eth->mac; |
| 510 | |
| 511 | /* graceful stop the transmission of frames */ |
| 512 | fmc_tx_port_graceful_stop_enable(fm_eth); |
| 513 | /* disable bmi Tx port */ |
| 514 | bmi_tx_port_disable(fm_eth->tx_port); |
| 515 | /* disable MAC rx/tx port */ |
| 516 | mac->disable_mac(mac); |
| 517 | /* disable bmi Rx port */ |
| 518 | bmi_rx_port_disable(fm_eth->rx_port); |
| 519 | |
Shaohui Xie | ab687cc | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 520 | #ifdef CONFIG_PHYLIB |
Codrin Ciubotariu | 1ee90f9 | 2015-01-12 14:08:29 +0200 | [diff] [blame] | 521 | if (fm_eth->phydev) |
| 522 | phy_shutdown(fm_eth->phydev); |
Shaohui Xie | ab687cc | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 523 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 524 | } |
| 525 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 526 | static int fm_eth_send(struct udevice *dev, void *buf, int len) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 527 | { |
| 528 | struct fm_eth *fm_eth; |
| 529 | struct fm_port_global_pram *pram; |
| 530 | struct fm_port_bd *txbd, *txbd_base; |
| 531 | u16 offset_in; |
| 532 | int i; |
| 533 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 534 | fm_eth = dev_get_priv(dev); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 535 | pram = fm_eth->tx_pram; |
| 536 | txbd = fm_eth->cur_txbd; |
| 537 | |
| 538 | /* find one empty TxBD */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 539 | for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 540 | udelay(100); |
| 541 | if (i > 0x1000) { |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 542 | printf("%s: Tx buffer not ready, txbd->status = 0x%x\n", |
| 543 | dev->name, muram_readw(&txbd->status)); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | } |
| 547 | /* setup TxBD */ |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 548 | muram_writew(&txbd->buf_ptr_hi, (u16)upper_32_bits(virt_to_phys(buf))); |
| 549 | out_be32(&txbd->buf_ptr_lo, lower_32_bits(virt_to_phys(buf))); |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 550 | muram_writew(&txbd->len, len); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 551 | sync(); |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 552 | muram_writew(&txbd->status, TxBD_READY | TxBD_LAST); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 553 | sync(); |
| 554 | |
| 555 | /* update TxQD, let RISC to send the packet */ |
| 556 | offset_in = muram_readw(&pram->txqd.offset_in); |
| 557 | offset_in += sizeof(struct fm_port_bd); |
| 558 | if (offset_in >= muram_readw(&pram->txqd.bd_ring_size)) |
| 559 | offset_in = 0; |
| 560 | muram_writew(&pram->txqd.offset_in, offset_in); |
| 561 | sync(); |
| 562 | |
| 563 | /* wait for buffer to be transmitted */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 564 | for (i = 0; muram_readw(&txbd->status) & TxBD_READY; i++) { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 565 | udelay(100); |
| 566 | if (i > 0x10000) { |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 567 | printf("%s: Tx error, txbd->status = 0x%x\n", |
| 568 | dev->name, muram_readw(&txbd->status)); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 569 | return 0; |
| 570 | } |
| 571 | } |
| 572 | |
| 573 | /* advance the TxBD */ |
| 574 | txbd++; |
| 575 | txbd_base = (struct fm_port_bd *)fm_eth->tx_bd_ring; |
| 576 | if (txbd >= (txbd_base + TX_BD_RING_SIZE)) |
| 577 | txbd = txbd_base; |
| 578 | /* update current txbd */ |
| 579 | fm_eth->cur_txbd = (void *)txbd; |
| 580 | |
| 581 | return 1; |
| 582 | } |
| 583 | |
Madalin Bucur | 248f920 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 584 | static struct fm_port_bd *fm_eth_free_one(struct fm_eth *fm_eth, |
| 585 | struct fm_port_bd *rxbd) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 586 | { |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 587 | struct fm_port_global_pram *pram; |
Madalin Bucur | 248f920 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 588 | struct fm_port_bd *rxbd_base; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 589 | u16 offset_out; |
| 590 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 591 | pram = fm_eth->rx_pram; |
Madalin Bucur | 248f920 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 592 | |
| 593 | /* clear the RxBDs */ |
| 594 | muram_writew(&rxbd->status, RxBD_EMPTY); |
| 595 | muram_writew(&rxbd->len, 0); |
| 596 | sync(); |
| 597 | |
| 598 | /* advance RxBD */ |
| 599 | rxbd++; |
| 600 | rxbd_base = (struct fm_port_bd *)fm_eth->rx_bd_ring; |
| 601 | if (rxbd >= (rxbd_base + RX_BD_RING_SIZE)) |
| 602 | rxbd = rxbd_base; |
| 603 | |
| 604 | /* update RxQD */ |
| 605 | offset_out = muram_readw(&pram->rxqd.offset_out); |
| 606 | offset_out += sizeof(struct fm_port_bd); |
| 607 | if (offset_out >= muram_readw(&pram->rxqd.bd_ring_size)) |
| 608 | offset_out = 0; |
| 609 | muram_writew(&pram->rxqd.offset_out, offset_out); |
| 610 | sync(); |
| 611 | |
| 612 | return rxbd; |
| 613 | } |
| 614 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 615 | static int fm_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
Madalin Bucur | 248f920 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 616 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 617 | struct fm_eth *fm_eth; |
| 618 | struct fm_port_bd *rxbd; |
Madalin Bucur | 248f920 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 619 | u32 buf_lo, buf_hi; |
| 620 | u16 status, len; |
| 621 | int ret = -1; |
| 622 | u8 *data; |
| 623 | |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 624 | fm_eth = dev_get_priv(dev); |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 625 | rxbd = fm_eth->cur_rxbd; |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 626 | status = muram_readw(&rxbd->status); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 627 | |
| 628 | while (!(status & RxBD_EMPTY)) { |
| 629 | if (!(status & RxBD_ERROR)) { |
Hou Zhiqiang | ea52d33 | 2015-10-26 19:47:44 +0800 | [diff] [blame] | 630 | buf_hi = muram_readw(&rxbd->buf_ptr_hi); |
| 631 | buf_lo = in_be32(&rxbd->buf_ptr_lo); |
| 632 | data = (u8 *)((ulong)(buf_hi << 16) << 16 | buf_lo); |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 633 | len = muram_readw(&rxbd->len); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 634 | *packetp = data; |
| 635 | return len; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 636 | } else { |
| 637 | printf("%s: Rx error\n", dev->name); |
Daniel Inderbitzin | fdbc5c7 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 638 | ret = 0; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 639 | } |
| 640 | |
Madalin Bucur | 248f920 | 2020-04-23 16:25:17 +0300 | [diff] [blame] | 641 | /* free current bd, advance to next one */ |
| 642 | rxbd = fm_eth_free_one(fm_eth, rxbd); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 643 | |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 644 | /* read next status */ |
Hou Zhiqiang | 3a25ece | 2015-10-26 19:47:43 +0800 | [diff] [blame] | 645 | status = muram_readw(&rxbd->status); |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 646 | } |
| 647 | fm_eth->cur_rxbd = (void *)rxbd; |
| 648 | |
Daniel Inderbitzin | fdbc5c7 | 2015-07-10 14:06:02 +0200 | [diff] [blame] | 649 | return ret; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 650 | } |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 651 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 652 | static int fm_eth_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 653 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 654 | struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 655 | |
| 656 | fm_eth->cur_rxbd = fm_eth_free_one(fm_eth, fm_eth->cur_rxbd); |
| 657 | |
| 658 | return 0; |
| 659 | } |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 660 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 661 | static int fm_eth_init_mac(struct fm_eth *fm_eth, void *reg) |
| 662 | { |
| 663 | #ifndef CONFIG_SYS_FMAN_V3 |
| 664 | void *mdio; |
| 665 | #endif |
| 666 | |
| 667 | fm_eth->mac = kzalloc(sizeof(*fm_eth->mac), GFP_KERNEL); |
| 668 | if (!fm_eth->mac) |
| 669 | return -ENOMEM; |
| 670 | |
| 671 | #ifndef CONFIG_SYS_FMAN_V3 |
| 672 | mdio = fman_mdio(fm_eth->dev->parent, fm_eth->mac_type, fm_eth->num); |
| 673 | debug("MDIO %d @ %p\n", fm_eth->num, mdio); |
| 674 | #endif |
| 675 | |
| 676 | switch (fm_eth->mac_type) { |
| 677 | #ifdef CONFIG_SYS_FMAN_V3 |
| 678 | case FM_MEMAC: |
| 679 | init_memac(fm_eth->mac, reg, NULL, MAX_RXBUF_LEN); |
| 680 | break; |
| 681 | #else |
| 682 | case FM_DTSEC: |
| 683 | init_dtsec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN); |
| 684 | break; |
| 685 | case FM_TGEC: |
| 686 | init_tgec(fm_eth->mac, reg, mdio, MAX_RXBUF_LEN); |
| 687 | break; |
| 688 | #endif |
| 689 | } |
| 690 | |
| 691 | return 0; |
| 692 | } |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 693 | |
Madalin Bucur | 11fa7a3 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 694 | static int init_phy(struct fm_eth *fm_eth) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 695 | { |
Shaohui Xie | ab687cc | 2015-10-26 19:47:46 +0800 | [diff] [blame] | 696 | #ifdef CONFIG_PHYLIB |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 697 | u32 supported = PHY_GBIT_FEATURES; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 698 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 699 | if (fm_eth->type == FM_ETH_10G_E) |
| 700 | supported = PHY_10G_FEATURES; |
Vladimir Oltean | 6caef97 | 2021-09-18 15:32:35 +0300 | [diff] [blame] | 701 | if (fm_eth->enet_if == PHY_INTERFACE_MODE_2500BASEX) |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 702 | supported |= SUPPORTED_2500baseX_Full; |
| 703 | #endif |
| 704 | |
Jerome Forissier | edb410b | 2024-09-11 11:58:18 +0200 | [diff] [blame^] | 705 | #if (CONFIG_IS_ENABLED(MII) || CONFIG_IS_ENABLED(CMD_MII)) && \ |
| 706 | !CONFIG_IS_ENABLED(BITBANGMII) |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 707 | if (fm_eth->type == FM_ETH_1G_E) |
Madalin Bucur | 11fa7a3 | 2020-04-23 16:25:16 +0300 | [diff] [blame] | 708 | dtsec_init_phy(fm_eth); |
Jerome Forissier | edb410b | 2024-09-11 11:58:18 +0200 | [diff] [blame^] | 709 | #endif |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 710 | |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 711 | #ifdef CONFIG_PHYLIB |
| 712 | #ifdef CONFIG_DM_MDIO |
| 713 | fm_eth->phydev = dm_eth_phy_connect(fm_eth->dev); |
| 714 | if (!fm_eth->phydev) |
| 715 | return -ENODEV; |
| 716 | #endif |
| 717 | fm_eth->phydev->advertising &= supported; |
| 718 | fm_eth->phydev->supported &= supported; |
| 719 | |
| 720 | phy_config(fm_eth->phydev); |
| 721 | #endif |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 722 | return 0; |
| 723 | } |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 724 | |
| 725 | static int fm_eth_bind(struct udevice *dev) |
| 726 | { |
| 727 | char mac_name[11]; |
| 728 | u32 fm, num; |
| 729 | |
Simon Glass | a7ece58 | 2020-12-19 10:40:14 -0700 | [diff] [blame] | 730 | if (ofnode_read_u32(ofnode_get_parent(dev_ofnode(dev)), "cell-index", &fm)) { |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 731 | printf("FMan node property cell-index missing\n"); |
| 732 | return -EINVAL; |
| 733 | } |
| 734 | |
| 735 | if (dev && dev_read_u32(dev, "cell-index", &num)) { |
| 736 | printf("FMan MAC node property cell-index missing\n"); |
| 737 | return -EINVAL; |
| 738 | } |
| 739 | |
| 740 | sprintf(mac_name, "fm%d-mac%d", fm + 1, num + 1); |
| 741 | device_set_name(dev, mac_name); |
| 742 | |
| 743 | debug("%s - binding %s\n", __func__, mac_name); |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | |
| 748 | static struct udevice *fm_get_internal_mdio(struct udevice *dev) |
| 749 | { |
| 750 | struct ofnode_phandle_args phandle = {.node = ofnode_null()}; |
| 751 | struct udevice *mdiodev; |
| 752 | |
| 753 | if (dev_read_phandle_with_args(dev, "pcsphy-handle", NULL, |
| 754 | 0, 0, &phandle) || |
| 755 | !ofnode_valid(phandle.node)) { |
| 756 | if (dev_read_phandle_with_args(dev, "tbi-handle", NULL, |
| 757 | 0, 0, &phandle) || |
| 758 | !ofnode_valid(phandle.node)) { |
| 759 | printf("Issue reading pcsphy-handle/tbi-handle for MAC %s\n", |
| 760 | dev->name); |
| 761 | return NULL; |
| 762 | } |
| 763 | } |
| 764 | |
| 765 | if (uclass_get_device_by_ofnode(UCLASS_MDIO, |
| 766 | ofnode_get_parent(phandle.node), |
| 767 | &mdiodev)) { |
| 768 | printf("can't find MDIO bus for node %s\n", |
| 769 | ofnode_get_name(ofnode_get_parent(phandle.node))); |
| 770 | return NULL; |
| 771 | } |
| 772 | debug("Found internal MDIO bus %p\n", mdiodev); |
| 773 | |
| 774 | return mdiodev; |
| 775 | } |
| 776 | |
| 777 | static int fm_eth_probe(struct udevice *dev) |
| 778 | { |
Simon Glass | 9558862 | 2020-12-22 19:30:28 -0700 | [diff] [blame] | 779 | struct fm_eth *fm_eth = (struct fm_eth *)dev_get_priv(dev); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 780 | struct ofnode_phandle_args args; |
| 781 | void *reg; |
| 782 | int ret, index; |
| 783 | |
| 784 | debug("%s enter for dev %p fm_eth %p - %s\n", __func__, dev, fm_eth, |
| 785 | (dev) ? dev->name : "-"); |
| 786 | |
| 787 | if (fm_eth->dev) { |
| 788 | printf("%s already probed, exit\n", (dev) ? dev->name : "-"); |
| 789 | return 0; |
| 790 | } |
| 791 | |
| 792 | fm_eth->dev = dev; |
| 793 | fm_eth->fm_index = fman_id(dev->parent); |
| 794 | reg = (void *)(uintptr_t)dev_read_addr(dev); |
| 795 | fm_eth->mac_type = dev_get_driver_data(dev); |
| 796 | #ifdef CONFIG_PHYLIB |
Marek BehĂșn | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 797 | fm_eth->enet_if = dev_read_phy_mode(dev); |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 798 | #else |
| 799 | fm_eth->enet_if = PHY_INTERFACE_MODE_SGMII; |
| 800 | printf("%s: warning - unable to determine interface type\n", __func__); |
| 801 | #endif |
| 802 | switch (fm_eth->mac_type) { |
| 803 | #ifndef CONFIG_SYS_FMAN_V3 |
| 804 | case FM_TGEC: |
| 805 | fm_eth->type = FM_ETH_10G_E; |
| 806 | break; |
| 807 | case FM_DTSEC: |
| 808 | #else |
| 809 | case FM_MEMAC: |
| 810 | /* default to 1G, 10G is indicated by port property in dts */ |
| 811 | #endif |
| 812 | fm_eth->type = FM_ETH_1G_E; |
| 813 | break; |
| 814 | } |
| 815 | |
| 816 | if (dev_read_u32(dev, "cell-index", &fm_eth->num)) { |
| 817 | printf("FMan MAC node property cell-index missing\n"); |
| 818 | return -EINVAL; |
| 819 | } |
| 820 | |
| 821 | if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL, |
| 822 | 0, 0, &args)) |
| 823 | goto ports_ref_failure; |
| 824 | index = ofnode_read_u32_default(args.node, "cell-index", 0); |
| 825 | if (index <= 0) |
| 826 | goto ports_ref_failure; |
| 827 | fm_eth->rx_port = fman_port(dev->parent, index); |
| 828 | |
| 829 | if (ofnode_read_bool(args.node, "fsl,fman-10g-port")) |
| 830 | fm_eth->type = FM_ETH_10G_E; |
| 831 | |
| 832 | if (dev_read_phandle_with_args(dev, "fsl,fman-ports", NULL, |
| 833 | 0, 1, &args)) |
| 834 | goto ports_ref_failure; |
| 835 | index = ofnode_read_u32_default(args.node, "cell-index", 0); |
| 836 | if (index <= 0) |
| 837 | goto ports_ref_failure; |
| 838 | fm_eth->tx_port = fman_port(dev->parent, index); |
| 839 | |
| 840 | /* set the ethernet max receive length */ |
| 841 | fm_eth->max_rx_len = MAX_RXBUF_LEN; |
| 842 | |
| 843 | switch (fm_eth->enet_if) { |
| 844 | case PHY_INTERFACE_MODE_QSGMII: |
| 845 | /* all PCS blocks are accessed on one controller */ |
| 846 | if (fm_eth->num != 0) |
| 847 | break; |
| 848 | case PHY_INTERFACE_MODE_SGMII: |
Vladimir Oltean | 6caef97 | 2021-09-18 15:32:35 +0300 | [diff] [blame] | 849 | case PHY_INTERFACE_MODE_2500BASEX: |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 850 | fm_eth->pcs_mdio = fm_get_internal_mdio(dev); |
| 851 | break; |
| 852 | default: |
| 853 | break; |
| 854 | } |
| 855 | |
| 856 | /* init global mac structure */ |
| 857 | ret = fm_eth_init_mac(fm_eth, reg); |
| 858 | if (ret) |
| 859 | return ret; |
| 860 | |
| 861 | /* startup the FM im */ |
| 862 | ret = fm_eth_startup(fm_eth); |
| 863 | |
| 864 | if (!ret) |
| 865 | ret = init_phy(fm_eth); |
| 866 | |
| 867 | return ret; |
| 868 | |
| 869 | ports_ref_failure: |
| 870 | printf("Issue reading fsl,fman-ports for MAC %s\n", dev->name); |
| 871 | return -ENOENT; |
| 872 | } |
| 873 | |
| 874 | static int fm_eth_remove(struct udevice *dev) |
| 875 | { |
Hou Zhiqiang | a02fc98 | 2015-10-26 19:47:45 +0800 | [diff] [blame] | 876 | return 0; |
Kumar Gala | 2683c53 | 2011-04-13 08:37:44 -0500 | [diff] [blame] | 877 | } |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 878 | |
| 879 | static const struct eth_ops fm_eth_ops = { |
| 880 | .start = fm_eth_open, |
| 881 | .send = fm_eth_send, |
| 882 | .recv = fm_eth_recv, |
| 883 | .free_pkt = fm_eth_free_pkt, |
| 884 | .stop = fm_eth_halt, |
| 885 | }; |
| 886 | |
| 887 | static const struct udevice_id fm_eth_ids[] = { |
| 888 | #ifdef CONFIG_SYS_FMAN_V3 |
| 889 | { .compatible = "fsl,fman-memac", .data = FM_MEMAC }, |
| 890 | #else |
| 891 | { .compatible = "fsl,fman-dtsec", .data = FM_DTSEC }, |
| 892 | { .compatible = "fsl,fman-xgec", .data = FM_TGEC }, |
| 893 | #endif |
| 894 | {} |
| 895 | }; |
| 896 | |
| 897 | U_BOOT_DRIVER(eth_fman) = { |
| 898 | .name = "eth_fman", |
| 899 | .id = UCLASS_ETH, |
| 900 | .of_match = fm_eth_ids, |
| 901 | .bind = fm_eth_bind, |
| 902 | .probe = fm_eth_probe, |
| 903 | .remove = fm_eth_remove, |
| 904 | .ops = &fm_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 905 | .priv_auto = sizeof(struct fm_eth), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 906 | .plat_auto = sizeof(struct eth_pdata), |
Madalin Bucur | b76b0a6 | 2020-04-23 16:25:19 +0300 | [diff] [blame] | 907 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 908 | }; |