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