Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Texas Instruments K3 AM65 Ethernet Switch SubSystem Driver |
| 4 | * |
| 5 | * Copyright (C) 2019, Texas Instruments, Incorporated |
| 6 | * |
| 7 | */ |
| 8 | |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 9 | #include <malloc.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <asm/cache.h> |
Suman Anna | 18e40be | 2023-08-02 13:47:26 +0530 | [diff] [blame] | 11 | #include <asm/gpio.h> |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 12 | #include <asm/io.h> |
| 13 | #include <asm/processor.h> |
| 14 | #include <clk.h> |
| 15 | #include <dm.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 16 | #include <dm/device_compat.h> |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 17 | #include <dm/lists.h> |
Maxime Ripard | 028849d | 2023-07-24 15:57:30 +0200 | [diff] [blame] | 18 | #include <dm/pinctrl.h> |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 19 | #include <dma-uclass.h> |
| 20 | #include <dm/of_access.h> |
| 21 | #include <miiphy.h> |
| 22 | #include <net.h> |
| 23 | #include <phy.h> |
| 24 | #include <power-domain.h> |
Roger Quadros | cb8f8ad | 2023-07-22 22:31:48 +0300 | [diff] [blame] | 25 | #include <regmap.h> |
Ravi Gunasekaran | 1eb6191 | 2022-09-22 15:21:24 +0530 | [diff] [blame] | 26 | #include <soc.h> |
Roger Quadros | cb8f8ad | 2023-07-22 22:31:48 +0300 | [diff] [blame] | 27 | #include <syscon.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 28 | #include <linux/bitops.h> |
Suman Anna | 18e40be | 2023-08-02 13:47:26 +0530 | [diff] [blame] | 29 | #include <linux/delay.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 30 | #include <linux/printk.h> |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 31 | #include <linux/soc/ti/ti-udma.h> |
| 32 | |
Vignesh Raghavendra | c5a6613 | 2021-05-10 20:06:09 +0530 | [diff] [blame] | 33 | #define AM65_CPSW_CPSWNU_MAX_PORTS 9 |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 34 | |
| 35 | #define AM65_CPSW_SS_BASE 0x0 |
| 36 | #define AM65_CPSW_SGMII_BASE 0x100 |
| 37 | #define AM65_CPSW_MDIO_BASE 0xf00 |
| 38 | #define AM65_CPSW_XGMII_BASE 0x2100 |
| 39 | #define AM65_CPSW_CPSW_NU_BASE 0x20000 |
| 40 | #define AM65_CPSW_CPSW_NU_ALE_BASE 0x1e000 |
| 41 | |
| 42 | #define AM65_CPSW_CPSW_NU_PORTS_OFFSET 0x1000 |
| 43 | #define AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET 0x330 |
| 44 | |
| 45 | #define AM65_CPSW_MDIO_BUS_FREQ_DEF 1000000 |
| 46 | |
| 47 | #define AM65_CPSW_CTL_REG 0x4 |
| 48 | #define AM65_CPSW_STAT_PORT_EN_REG 0x14 |
| 49 | #define AM65_CPSW_PTYPE_REG 0x18 |
| 50 | |
| 51 | #define AM65_CPSW_CTL_REG_P0_ENABLE BIT(2) |
| 52 | #define AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE BIT(13) |
| 53 | #define AM65_CPSW_CTL_REG_P0_RX_PAD BIT(14) |
| 54 | |
| 55 | #define AM65_CPSW_P0_FLOW_ID_REG 0x8 |
| 56 | #define AM65_CPSW_PN_RX_MAXLEN_REG 0x24 |
| 57 | #define AM65_CPSW_PN_REG_SA_L 0x308 |
| 58 | #define AM65_CPSW_PN_REG_SA_H 0x30c |
| 59 | |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 60 | #define AM65_CPSW_SGMII_CONTROL_REG 0x010 |
| 61 | #define AM65_CPSW_SGMII_MR_ADV_ABILITY_REG 0x018 |
| 62 | #define AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE BIT(0) |
| 63 | |
| 64 | #define ADVERTISE_SGMII 0x1 |
| 65 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 66 | #define AM65_CPSW_ALE_CTL_REG 0x8 |
| 67 | #define AM65_CPSW_ALE_CTL_REG_ENABLE BIT(31) |
| 68 | #define AM65_CPSW_ALE_CTL_REG_RESET_TBL BIT(30) |
| 69 | #define AM65_CPSW_ALE_CTL_REG_BYPASS BIT(4) |
| 70 | #define AM65_CPSW_ALE_PN_CTL_REG(x) (0x40 + (x) * 4) |
| 71 | #define AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD 0x3 |
| 72 | #define AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY BIT(11) |
| 73 | |
Vignesh Raghavendra | 5cb8a0f | 2020-07-06 13:36:53 +0530 | [diff] [blame] | 74 | #define AM65_CPSW_ALE_THREADMAPDEF_REG 0x134 |
| 75 | #define AM65_CPSW_ALE_DEFTHREAD_EN BIT(15) |
| 76 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 77 | #define AM65_CPSW_MACSL_CTL_REG 0x0 |
| 78 | #define AM65_CPSW_MACSL_CTL_REG_IFCTL_A BIT(15) |
Murali Karicheri | 6565e90 | 2020-04-17 11:12:09 -0400 | [diff] [blame] | 79 | #define AM65_CPSW_MACSL_CTL_EXT_EN BIT(18) |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 80 | #define AM65_CPSW_MACSL_CTL_REG_GIG BIT(7) |
| 81 | #define AM65_CPSW_MACSL_CTL_REG_GMII_EN BIT(5) |
| 82 | #define AM65_CPSW_MACSL_CTL_REG_LOOPBACK BIT(1) |
| 83 | #define AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX BIT(0) |
| 84 | #define AM65_CPSW_MACSL_RESET_REG 0x8 |
| 85 | #define AM65_CPSW_MACSL_RESET_REG_RESET BIT(0) |
| 86 | #define AM65_CPSW_MACSL_STATUS_REG 0x4 |
| 87 | #define AM65_CPSW_MACSL_RESET_REG_PN_IDLE BIT(31) |
| 88 | #define AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE BIT(30) |
| 89 | #define AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE BIT(29) |
| 90 | #define AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE BIT(28) |
| 91 | #define AM65_CPSW_MACSL_RESET_REG_IDLE_MASK \ |
| 92 | (AM65_CPSW_MACSL_RESET_REG_PN_IDLE | \ |
| 93 | AM65_CPSW_MACSL_RESET_REG_PN_E_IDLE | \ |
| 94 | AM65_CPSW_MACSL_RESET_REG_PN_P_IDLE | \ |
| 95 | AM65_CPSW_MACSL_RESET_REG_PN_TX_IDLE) |
| 96 | |
| 97 | #define AM65_CPSW_CPPI_PKT_TYPE 0x7 |
| 98 | |
Suman Anna | 18e40be | 2023-08-02 13:47:26 +0530 | [diff] [blame] | 99 | #define DEFAULT_GPIO_RESET_DELAY 10 |
| 100 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 101 | struct am65_cpsw_port { |
| 102 | fdt_addr_t port_base; |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 103 | fdt_addr_t port_sgmii_base; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 104 | fdt_addr_t macsl_base; |
| 105 | bool disabled; |
| 106 | u32 mac_control; |
| 107 | }; |
| 108 | |
| 109 | struct am65_cpsw_common { |
| 110 | struct udevice *dev; |
| 111 | fdt_addr_t ss_base; |
| 112 | fdt_addr_t cpsw_base; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 113 | fdt_addr_t ale_base; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 114 | |
| 115 | struct clk fclk; |
| 116 | struct power_domain pwrdmn; |
| 117 | |
| 118 | u32 port_num; |
| 119 | struct am65_cpsw_port ports[AM65_CPSW_CPSWNU_MAX_PORTS]; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 120 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 121 | u32 bus_freq; |
| 122 | |
| 123 | struct dma dma_tx; |
| 124 | struct dma dma_rx; |
| 125 | u32 rx_next; |
| 126 | u32 rx_pend; |
| 127 | bool started; |
| 128 | }; |
| 129 | |
| 130 | struct am65_cpsw_priv { |
| 131 | struct udevice *dev; |
| 132 | struct am65_cpsw_common *cpsw_common; |
| 133 | u32 port_id; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 134 | struct phy_device *phydev; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 135 | }; |
| 136 | |
| 137 | #ifdef PKTSIZE_ALIGN |
| 138 | #define UDMA_RX_BUF_SIZE PKTSIZE_ALIGN |
| 139 | #else |
| 140 | #define UDMA_RX_BUF_SIZE ALIGN(1522, ARCH_DMA_MINALIGN) |
| 141 | #endif |
| 142 | |
| 143 | #ifdef PKTBUFSRX |
| 144 | #define UDMA_RX_DESC_NUM PKTBUFSRX |
| 145 | #else |
| 146 | #define UDMA_RX_DESC_NUM 4 |
| 147 | #endif |
| 148 | |
| 149 | #define mac_hi(mac) (((mac)[0] << 0) | ((mac)[1] << 8) | \ |
| 150 | ((mac)[2] << 16) | ((mac)[3] << 24)) |
| 151 | #define mac_lo(mac) (((mac)[4] << 0) | ((mac)[5] << 8)) |
| 152 | |
| 153 | static void am65_cpsw_set_sl_mac(struct am65_cpsw_port *slave, |
| 154 | unsigned char *addr) |
| 155 | { |
| 156 | writel(mac_hi(addr), |
| 157 | slave->port_base + AM65_CPSW_PN_REG_SA_H); |
| 158 | writel(mac_lo(addr), |
| 159 | slave->port_base + AM65_CPSW_PN_REG_SA_L); |
| 160 | } |
| 161 | |
| 162 | int am65_cpsw_macsl_reset(struct am65_cpsw_port *slave) |
| 163 | { |
| 164 | u32 i = 100; |
| 165 | |
| 166 | /* Set the soft reset bit */ |
| 167 | writel(AM65_CPSW_MACSL_RESET_REG_RESET, |
| 168 | slave->macsl_base + AM65_CPSW_MACSL_RESET_REG); |
| 169 | |
| 170 | while ((readl(slave->macsl_base + AM65_CPSW_MACSL_RESET_REG) & |
| 171 | AM65_CPSW_MACSL_RESET_REG_RESET) && i--) |
| 172 | cpu_relax(); |
| 173 | |
| 174 | /* Timeout on the reset */ |
| 175 | return i; |
| 176 | } |
| 177 | |
| 178 | static int am65_cpsw_macsl_wait_for_idle(struct am65_cpsw_port *slave) |
| 179 | { |
| 180 | u32 i = 100; |
| 181 | |
| 182 | while ((readl(slave->macsl_base + AM65_CPSW_MACSL_STATUS_REG) & |
| 183 | AM65_CPSW_MACSL_RESET_REG_IDLE_MASK) && i--) |
| 184 | cpu_relax(); |
| 185 | |
| 186 | return i; |
| 187 | } |
| 188 | |
| 189 | static int am65_cpsw_update_link(struct am65_cpsw_priv *priv) |
| 190 | { |
| 191 | struct am65_cpsw_common *common = priv->cpsw_common; |
| 192 | struct am65_cpsw_port *port = &common->ports[priv->port_id]; |
| 193 | struct phy_device *phy = priv->phydev; |
| 194 | u32 mac_control = 0; |
| 195 | |
| 196 | if (phy->link) { /* link up */ |
| 197 | mac_control = /*AM65_CPSW_MACSL_CTL_REG_LOOPBACK |*/ |
| 198 | AM65_CPSW_MACSL_CTL_REG_GMII_EN; |
| 199 | if (phy->speed == 1000) |
| 200 | mac_control |= AM65_CPSW_MACSL_CTL_REG_GIG; |
Murali Karicheri | 6565e90 | 2020-04-17 11:12:09 -0400 | [diff] [blame] | 201 | if (phy->speed == 10 && phy_interface_is_rgmii(phy)) |
| 202 | /* Can be used with in band mode only */ |
| 203 | mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 204 | if (phy->duplex == DUPLEX_FULL) |
| 205 | mac_control |= AM65_CPSW_MACSL_CTL_REG_FULL_DUPLEX; |
| 206 | if (phy->speed == 100) |
| 207 | mac_control |= AM65_CPSW_MACSL_CTL_REG_IFCTL_A; |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 208 | if (phy->interface == PHY_INTERFACE_MODE_SGMII) |
| 209 | mac_control |= AM65_CPSW_MACSL_CTL_EXT_EN; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | if (mac_control == port->mac_control) |
| 213 | goto out; |
| 214 | |
| 215 | if (mac_control) { |
| 216 | printf("link up on port %d, speed %d, %s duplex\n", |
| 217 | priv->port_id, phy->speed, |
| 218 | (phy->duplex == DUPLEX_FULL) ? "full" : "half"); |
| 219 | } else { |
| 220 | printf("link down on port %d\n", priv->port_id); |
| 221 | } |
| 222 | |
| 223 | writel(mac_control, port->macsl_base + AM65_CPSW_MACSL_CTL_REG); |
| 224 | port->mac_control = mac_control; |
| 225 | |
| 226 | out: |
| 227 | return phy->link; |
| 228 | } |
| 229 | |
Andreas Dannenberg | 1dc2ee6 | 2023-06-14 17:28:53 -0500 | [diff] [blame] | 230 | #define AM65_GMII_SEL_PORT_OFFS(x) (0x4 * ((x) - 1)) |
| 231 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 232 | #define AM65_GMII_SEL_MODE_MII 0 |
| 233 | #define AM65_GMII_SEL_MODE_RMII 1 |
| 234 | #define AM65_GMII_SEL_MODE_RGMII 2 |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 235 | #define AM65_GMII_SEL_MODE_SGMII 3 |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 236 | |
| 237 | #define AM65_GMII_SEL_RGMII_IDMODE BIT(4) |
| 238 | |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 239 | static int am65_cpsw_gmii_sel_k3(struct am65_cpsw_priv *priv, |
| 240 | phy_interface_t phy_mode) |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 241 | { |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 242 | struct udevice *dev = priv->dev; |
| 243 | u32 offset, reg, phandle; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 244 | bool rgmii_id = false; |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 245 | fdt_addr_t gmii_sel; |
| 246 | u32 mode = 0; |
| 247 | ofnode node; |
| 248 | int ret; |
| 249 | |
| 250 | ret = ofnode_read_u32(dev_ofnode(dev), "phys", &phandle); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | |
| 254 | ret = ofnode_read_u32_index(dev_ofnode(dev), "phys", 1, &offset); |
| 255 | if (ret) |
| 256 | return ret; |
| 257 | |
| 258 | node = ofnode_get_by_phandle(phandle); |
| 259 | if (!ofnode_valid(node)) |
| 260 | return -ENODEV; |
| 261 | |
| 262 | gmii_sel = ofnode_get_addr(node); |
| 263 | if (gmii_sel == FDT_ADDR_T_NONE) |
| 264 | return -ENODEV; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 265 | |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 266 | gmii_sel += AM65_GMII_SEL_PORT_OFFS(offset); |
Andreas Dannenberg | 1dc2ee6 | 2023-06-14 17:28:53 -0500 | [diff] [blame] | 267 | reg = readl(gmii_sel); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 268 | |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 269 | dev_dbg(dev, "old gmii_sel: %08x\n", reg); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 270 | |
| 271 | switch (phy_mode) { |
| 272 | case PHY_INTERFACE_MODE_RMII: |
| 273 | mode = AM65_GMII_SEL_MODE_RMII; |
| 274 | break; |
| 275 | |
| 276 | case PHY_INTERFACE_MODE_RGMII: |
Grygorii Strashko | bf45d9b | 2019-09-19 11:16:41 +0300 | [diff] [blame] | 277 | case PHY_INTERFACE_MODE_RGMII_RXID: |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 278 | mode = AM65_GMII_SEL_MODE_RGMII; |
| 279 | break; |
| 280 | |
| 281 | case PHY_INTERFACE_MODE_RGMII_ID: |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 282 | case PHY_INTERFACE_MODE_RGMII_TXID: |
| 283 | mode = AM65_GMII_SEL_MODE_RGMII; |
| 284 | rgmii_id = true; |
| 285 | break; |
| 286 | |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 287 | case PHY_INTERFACE_MODE_SGMII: |
| 288 | mode = AM65_GMII_SEL_MODE_SGMII; |
| 289 | break; |
| 290 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 291 | default: |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 292 | dev_warn(dev, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 293 | "Unsupported PHY mode: %u. Defaulting to MII.\n", |
| 294 | phy_mode); |
| 295 | /* fallthrough */ |
| 296 | case PHY_INTERFACE_MODE_MII: |
| 297 | mode = AM65_GMII_SEL_MODE_MII; |
| 298 | break; |
| 299 | }; |
| 300 | |
| 301 | if (rgmii_id) |
| 302 | mode |= AM65_GMII_SEL_RGMII_IDMODE; |
| 303 | |
| 304 | reg = mode; |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 305 | dev_dbg(dev, "gmii_sel PHY mode: %u, new gmii_sel: %08x\n", |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 306 | phy_mode, reg); |
Andreas Dannenberg | 1dc2ee6 | 2023-06-14 17:28:53 -0500 | [diff] [blame] | 307 | writel(reg, gmii_sel); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 308 | |
Andreas Dannenberg | 1dc2ee6 | 2023-06-14 17:28:53 -0500 | [diff] [blame] | 309 | reg = readl(gmii_sel); |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 310 | if (reg != mode) { |
| 311 | dev_err(dev, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 312 | "gmii_sel PHY mode NOT SET!: requested: %08x, gmii_sel: %08x\n", |
| 313 | mode, reg); |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 314 | return 0; |
| 315 | } |
| 316 | |
| 317 | return 0; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | static int am65_cpsw_start(struct udevice *dev) |
| 321 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 322 | struct eth_pdata *pdata = dev_get_plat(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 323 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
| 324 | struct am65_cpsw_common *common = priv->cpsw_common; |
| 325 | struct am65_cpsw_port *port = &common->ports[priv->port_id]; |
| 326 | struct am65_cpsw_port *port0 = &common->ports[0]; |
Vignesh Raghavendra | 462ff04 | 2019-12-04 22:17:22 +0530 | [diff] [blame] | 327 | struct ti_udma_drv_chan_cfg_data *dma_rx_cfg_data; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 328 | int ret, i; |
| 329 | |
Matthias Schiffer | ac70b87 | 2024-04-26 10:02:25 +0200 | [diff] [blame] | 330 | if (common->started) |
| 331 | return 0; |
| 332 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 333 | ret = power_domain_on(&common->pwrdmn); |
| 334 | if (ret) { |
| 335 | dev_err(dev, "power_domain_on() failed %d\n", ret); |
| 336 | goto out; |
| 337 | } |
| 338 | |
| 339 | ret = clk_enable(&common->fclk); |
| 340 | if (ret) { |
| 341 | dev_err(dev, "clk enabled failed %d\n", ret); |
| 342 | goto err_off_pwrdm; |
| 343 | } |
| 344 | |
| 345 | common->rx_next = 0; |
| 346 | common->rx_pend = 0; |
| 347 | ret = dma_get_by_name(common->dev, "tx0", &common->dma_tx); |
| 348 | if (ret) { |
| 349 | dev_err(dev, "TX dma get failed %d\n", ret); |
| 350 | goto err_off_clk; |
| 351 | } |
| 352 | ret = dma_get_by_name(common->dev, "rx", &common->dma_rx); |
| 353 | if (ret) { |
| 354 | dev_err(dev, "RX dma get failed %d\n", ret); |
| 355 | goto err_free_tx; |
| 356 | } |
| 357 | |
| 358 | for (i = 0; i < UDMA_RX_DESC_NUM; i++) { |
| 359 | ret = dma_prepare_rcv_buf(&common->dma_rx, |
| 360 | net_rx_packets[i], |
| 361 | UDMA_RX_BUF_SIZE); |
| 362 | if (ret) { |
| 363 | dev_err(dev, "RX dma add buf failed %d\n", ret); |
Matthias Schiffer | 9ee004a | 2024-04-26 10:02:26 +0200 | [diff] [blame^] | 364 | goto err_free_rx; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
| 368 | ret = dma_enable(&common->dma_tx); |
| 369 | if (ret) { |
| 370 | dev_err(dev, "TX dma_enable failed %d\n", ret); |
| 371 | goto err_free_rx; |
| 372 | } |
| 373 | ret = dma_enable(&common->dma_rx); |
| 374 | if (ret) { |
| 375 | dev_err(dev, "RX dma_enable failed %d\n", ret); |
| 376 | goto err_dis_tx; |
| 377 | } |
| 378 | |
| 379 | /* Control register */ |
| 380 | writel(AM65_CPSW_CTL_REG_P0_ENABLE | |
| 381 | AM65_CPSW_CTL_REG_P0_TX_CRC_REMOVE | |
| 382 | AM65_CPSW_CTL_REG_P0_RX_PAD, |
| 383 | common->cpsw_base + AM65_CPSW_CTL_REG); |
| 384 | |
| 385 | /* disable priority elevation */ |
| 386 | writel(0, common->cpsw_base + AM65_CPSW_PTYPE_REG); |
| 387 | |
| 388 | /* enable statistics */ |
| 389 | writel(BIT(0) | BIT(priv->port_id), |
| 390 | common->cpsw_base + AM65_CPSW_STAT_PORT_EN_REG); |
| 391 | |
| 392 | /* Port 0 length register */ |
| 393 | writel(PKTSIZE_ALIGN, port0->port_base + AM65_CPSW_PN_RX_MAXLEN_REG); |
| 394 | |
| 395 | /* set base flow_id */ |
Vignesh Raghavendra | 462ff04 | 2019-12-04 22:17:22 +0530 | [diff] [blame] | 396 | dma_get_cfg(&common->dma_rx, 0, (void **)&dma_rx_cfg_data); |
| 397 | writel(dma_rx_cfg_data->flow_id_base, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 398 | port0->port_base + AM65_CPSW_P0_FLOW_ID_REG); |
Vignesh Raghavendra | 462ff04 | 2019-12-04 22:17:22 +0530 | [diff] [blame] | 399 | dev_info(dev, "K3 CPSW: rflow_id_base: %u\n", |
| 400 | dma_rx_cfg_data->flow_id_base); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 401 | |
| 402 | /* Reset and enable the ALE */ |
| 403 | writel(AM65_CPSW_ALE_CTL_REG_ENABLE | AM65_CPSW_ALE_CTL_REG_RESET_TBL | |
| 404 | AM65_CPSW_ALE_CTL_REG_BYPASS, |
| 405 | common->ale_base + AM65_CPSW_ALE_CTL_REG); |
| 406 | |
| 407 | /* port 0 put into forward mode */ |
| 408 | writel(AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD, |
| 409 | common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0)); |
| 410 | |
Vignesh Raghavendra | 5cb8a0f | 2020-07-06 13:36:53 +0530 | [diff] [blame] | 411 | writel(AM65_CPSW_ALE_DEFTHREAD_EN, |
| 412 | common->ale_base + AM65_CPSW_ALE_THREADMAPDEF_REG); |
| 413 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 414 | /* PORT x configuration */ |
| 415 | |
| 416 | /* Port x Max length register */ |
| 417 | writel(PKTSIZE_ALIGN, port->port_base + AM65_CPSW_PN_RX_MAXLEN_REG); |
| 418 | |
| 419 | /* Port x set mac */ |
| 420 | am65_cpsw_set_sl_mac(port, pdata->enetaddr); |
| 421 | |
| 422 | /* Port x ALE: mac_only, Forwarding */ |
| 423 | writel(AM65_CPSW_ALE_PN_CTL_REG_MAC_ONLY | |
| 424 | AM65_CPSW_ALE_PN_CTL_REG_MODE_FORWARD, |
| 425 | common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id)); |
| 426 | |
| 427 | port->mac_control = 0; |
| 428 | if (!am65_cpsw_macsl_reset(port)) { |
| 429 | dev_err(dev, "mac_sl reset failed\n"); |
| 430 | ret = -EFAULT; |
| 431 | goto err_dis_rx; |
| 432 | } |
| 433 | |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 434 | if (priv->phydev->interface == PHY_INTERFACE_MODE_SGMII) { |
| 435 | writel(ADVERTISE_SGMII, |
| 436 | port->port_sgmii_base + AM65_CPSW_SGMII_MR_ADV_ABILITY_REG); |
| 437 | writel(AM65_CPSW_SGMII_CONTROL_MR_AN_ENABLE, |
| 438 | port->port_sgmii_base + AM65_CPSW_SGMII_CONTROL_REG); |
| 439 | } |
| 440 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 441 | ret = phy_startup(priv->phydev); |
| 442 | if (ret) { |
| 443 | dev_err(dev, "phy_startup failed\n"); |
| 444 | goto err_dis_rx; |
| 445 | } |
| 446 | |
| 447 | ret = am65_cpsw_update_link(priv); |
| 448 | if (!ret) { |
| 449 | ret = -ENODEV; |
| 450 | goto err_phy_shutdown; |
| 451 | } |
| 452 | |
| 453 | common->started = true; |
| 454 | |
| 455 | return 0; |
| 456 | |
| 457 | err_phy_shutdown: |
| 458 | phy_shutdown(priv->phydev); |
| 459 | err_dis_rx: |
| 460 | /* disable ports */ |
| 461 | writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id)); |
| 462 | writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0)); |
| 463 | if (!am65_cpsw_macsl_wait_for_idle(port)) |
| 464 | dev_err(dev, "mac_sl idle timeout\n"); |
| 465 | writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG); |
| 466 | writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG); |
| 467 | writel(0, common->cpsw_base + AM65_CPSW_CTL_REG); |
| 468 | |
| 469 | dma_disable(&common->dma_rx); |
| 470 | err_dis_tx: |
| 471 | dma_disable(&common->dma_tx); |
| 472 | err_free_rx: |
| 473 | dma_free(&common->dma_rx); |
| 474 | err_free_tx: |
| 475 | dma_free(&common->dma_tx); |
| 476 | err_off_clk: |
| 477 | clk_disable(&common->fclk); |
| 478 | err_off_pwrdm: |
| 479 | power_domain_off(&common->pwrdmn); |
| 480 | out: |
| 481 | dev_err(dev, "%s end error\n", __func__); |
| 482 | |
| 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | static int am65_cpsw_send(struct udevice *dev, void *packet, int length) |
| 487 | { |
| 488 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
| 489 | struct am65_cpsw_common *common = priv->cpsw_common; |
| 490 | struct ti_udma_drv_packet_data packet_data; |
| 491 | int ret; |
| 492 | |
Matthias Schiffer | ac70b87 | 2024-04-26 10:02:25 +0200 | [diff] [blame] | 493 | if (!common->started) |
| 494 | return -ENETDOWN; |
| 495 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 496 | packet_data.pkt_type = AM65_CPSW_CPPI_PKT_TYPE; |
| 497 | packet_data.dest_tag = priv->port_id; |
| 498 | ret = dma_send(&common->dma_tx, packet, length, &packet_data); |
| 499 | if (ret) { |
| 500 | dev_err(dev, "TX dma_send failed %d\n", ret); |
| 501 | return ret; |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static int am65_cpsw_recv(struct udevice *dev, int flags, uchar **packetp) |
| 508 | { |
| 509 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
| 510 | struct am65_cpsw_common *common = priv->cpsw_common; |
| 511 | |
Matthias Schiffer | ac70b87 | 2024-04-26 10:02:25 +0200 | [diff] [blame] | 512 | if (!common->started) |
| 513 | return -ENETDOWN; |
| 514 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 515 | /* try to receive a new packet */ |
| 516 | return dma_receive(&common->dma_rx, (void **)packetp, NULL); |
| 517 | } |
| 518 | |
| 519 | static int am65_cpsw_free_pkt(struct udevice *dev, uchar *packet, int length) |
| 520 | { |
| 521 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
| 522 | struct am65_cpsw_common *common = priv->cpsw_common; |
| 523 | int ret; |
| 524 | |
| 525 | if (length > 0) { |
| 526 | u32 pkt = common->rx_next % UDMA_RX_DESC_NUM; |
| 527 | |
| 528 | ret = dma_prepare_rcv_buf(&common->dma_rx, |
| 529 | net_rx_packets[pkt], |
| 530 | UDMA_RX_BUF_SIZE); |
| 531 | if (ret) |
| 532 | dev_err(dev, "RX dma free_pkt failed %d\n", ret); |
| 533 | common->rx_next++; |
| 534 | } |
| 535 | |
| 536 | return 0; |
| 537 | } |
| 538 | |
| 539 | static void am65_cpsw_stop(struct udevice *dev) |
| 540 | { |
| 541 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
| 542 | struct am65_cpsw_common *common = priv->cpsw_common; |
| 543 | struct am65_cpsw_port *port = &common->ports[priv->port_id]; |
| 544 | |
| 545 | if (!common->started) |
| 546 | return; |
| 547 | |
| 548 | phy_shutdown(priv->phydev); |
| 549 | |
| 550 | writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(priv->port_id)); |
| 551 | writel(0, common->ale_base + AM65_CPSW_ALE_PN_CTL_REG(0)); |
| 552 | if (!am65_cpsw_macsl_wait_for_idle(port)) |
| 553 | dev_err(dev, "mac_sl idle timeout\n"); |
| 554 | writel(0, port->macsl_base + AM65_CPSW_MACSL_CTL_REG); |
| 555 | writel(0, common->ale_base + AM65_CPSW_ALE_CTL_REG); |
| 556 | writel(0, common->cpsw_base + AM65_CPSW_CTL_REG); |
| 557 | |
| 558 | dma_disable(&common->dma_tx); |
| 559 | dma_free(&common->dma_tx); |
| 560 | |
| 561 | dma_disable(&common->dma_rx); |
| 562 | dma_free(&common->dma_rx); |
| 563 | |
| 564 | common->started = false; |
| 565 | } |
| 566 | |
Roger Quadros | cb8f8ad | 2023-07-22 22:31:48 +0300 | [diff] [blame] | 567 | static int am65_cpsw_am654_get_efuse_macid(struct udevice *dev, |
| 568 | int slave, u8 *mac_addr) |
| 569 | { |
| 570 | u32 mac_lo, mac_hi, offset; |
| 571 | struct regmap *syscon; |
| 572 | int ret; |
| 573 | |
| 574 | syscon = syscon_regmap_lookup_by_phandle(dev, "ti,syscon-efuse"); |
| 575 | if (IS_ERR(syscon)) { |
| 576 | if (PTR_ERR(syscon) == -ENODEV) |
| 577 | return 0; |
| 578 | return PTR_ERR(syscon); |
| 579 | } |
| 580 | |
| 581 | ret = dev_read_u32_index(dev, "ti,syscon-efuse", 1, &offset); |
| 582 | if (ret) |
| 583 | return ret; |
| 584 | |
| 585 | regmap_read(syscon, offset, &mac_lo); |
| 586 | regmap_read(syscon, offset + 4, &mac_hi); |
| 587 | |
| 588 | mac_addr[0] = (mac_hi >> 8) & 0xff; |
| 589 | mac_addr[1] = mac_hi & 0xff; |
| 590 | mac_addr[2] = (mac_lo >> 24) & 0xff; |
| 591 | mac_addr[3] = (mac_lo >> 16) & 0xff; |
| 592 | mac_addr[4] = (mac_lo >> 8) & 0xff; |
| 593 | mac_addr[5] = mac_lo & 0xff; |
| 594 | |
| 595 | return 0; |
| 596 | } |
| 597 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 598 | static int am65_cpsw_read_rom_hwaddr(struct udevice *dev) |
| 599 | { |
| 600 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 601 | struct eth_pdata *pdata = dev_get_plat(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 602 | |
Roger Quadros | cb8f8ad | 2023-07-22 22:31:48 +0300 | [diff] [blame] | 603 | am65_cpsw_am654_get_efuse_macid(dev, |
| 604 | priv->port_id, |
| 605 | pdata->enetaddr); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 606 | |
| 607 | return 0; |
| 608 | } |
| 609 | |
| 610 | static const struct eth_ops am65_cpsw_ops = { |
| 611 | .start = am65_cpsw_start, |
| 612 | .send = am65_cpsw_send, |
| 613 | .recv = am65_cpsw_recv, |
| 614 | .free_pkt = am65_cpsw_free_pkt, |
| 615 | .stop = am65_cpsw_stop, |
| 616 | .read_rom_hwaddr = am65_cpsw_read_rom_hwaddr, |
| 617 | }; |
| 618 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 619 | static int am65_cpsw_phy_init(struct udevice *dev) |
| 620 | { |
| 621 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 622 | struct eth_pdata *pdata = dev_get_plat(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 623 | struct phy_device *phydev; |
| 624 | u32 supported = PHY_GBIT_FEATURES; |
| 625 | int ret; |
| 626 | |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 627 | phydev = dm_eth_phy_connect(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 628 | if (!phydev) { |
| 629 | dev_err(dev, "phy_connect() failed\n"); |
| 630 | return -ENODEV; |
| 631 | } |
| 632 | |
| 633 | phydev->supported &= supported; |
| 634 | if (pdata->max_speed) { |
| 635 | ret = phy_set_supported(phydev, pdata->max_speed); |
| 636 | if (ret) |
| 637 | return ret; |
| 638 | } |
| 639 | phydev->advertising = phydev->supported; |
| 640 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 641 | priv->phydev = phydev; |
| 642 | ret = phy_config(phydev); |
| 643 | if (ret < 0) |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 644 | dev_err(dev, "phy_config() failed: %d", ret); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 645 | |
| 646 | return ret; |
| 647 | } |
| 648 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 649 | static int am65_cpsw_ofdata_parse_phy(struct udevice *dev) |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 650 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 651 | struct eth_pdata *pdata = dev_get_plat(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 652 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 653 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 654 | dev_read_u32(dev, "reg", &priv->port_id); |
| 655 | |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 656 | pdata->phy_interface = dev_read_phy_mode(dev); |
Marek Behún | 48631e4 | 2022-04-07 00:33:03 +0200 | [diff] [blame] | 657 | if (pdata->phy_interface == PHY_INTERFACE_MODE_NA) { |
Marek Behún | bc19477 | 2022-04-07 00:33:01 +0200 | [diff] [blame] | 658 | dev_err(dev, "Invalid PHY mode, port %u\n", priv->port_id); |
| 659 | return -EINVAL; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 660 | } |
| 661 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 662 | dev_read_u32(dev, "max-speed", (u32 *)&pdata->max_speed); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 663 | if (pdata->max_speed) |
| 664 | dev_err(dev, "Port %u speed froced to %uMbit\n", |
| 665 | priv->port_id, pdata->max_speed); |
| 666 | |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 667 | return 0; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 668 | } |
| 669 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 670 | static int am65_cpsw_port_probe(struct udevice *dev) |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 671 | { |
| 672 | struct am65_cpsw_priv *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 673 | struct eth_pdata *pdata = dev_get_plat(dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 674 | struct am65_cpsw_common *cpsw_common; |
Michael Walle | 2e9cacb | 2024-04-03 16:31:55 +0200 | [diff] [blame] | 675 | char portname[32]; |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 676 | int ret; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 677 | |
| 678 | priv->dev = dev; |
| 679 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 680 | cpsw_common = dev_get_priv(dev->parent); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 681 | priv->cpsw_common = cpsw_common; |
| 682 | |
Michael Walle | 2e9cacb | 2024-04-03 16:31:55 +0200 | [diff] [blame] | 683 | snprintf(portname, sizeof(portname), "%s%s", dev->parent->name, dev->name); |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 684 | device_set_name(dev, portname); |
| 685 | |
| 686 | ret = am65_cpsw_ofdata_parse_phy(dev); |
| 687 | if (ret) |
| 688 | goto out; |
| 689 | |
Roger Quadros | be0619b | 2023-07-22 22:31:49 +0300 | [diff] [blame] | 690 | ret = am65_cpsw_gmii_sel_k3(priv, pdata->phy_interface); |
| 691 | if (ret) |
| 692 | goto out; |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 693 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 694 | ret = am65_cpsw_phy_init(dev); |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 695 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 696 | out: |
| 697 | return ret; |
| 698 | } |
| 699 | |
| 700 | static int am65_cpsw_probe_nuss(struct udevice *dev) |
| 701 | { |
| 702 | struct am65_cpsw_common *cpsw_common = dev_get_priv(dev); |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 703 | ofnode ports_np, node; |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 704 | int ret, i; |
| 705 | struct udevice *port_dev; |
| 706 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 707 | cpsw_common->dev = dev; |
| 708 | cpsw_common->ss_base = dev_read_addr(dev); |
| 709 | if (cpsw_common->ss_base == FDT_ADDR_T_NONE) |
| 710 | return -EINVAL; |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 711 | |
| 712 | ret = power_domain_get_by_index(dev, &cpsw_common->pwrdmn, 0); |
| 713 | if (ret) { |
| 714 | dev_err(dev, "failed to get pwrdmn: %d\n", ret); |
| 715 | return ret; |
| 716 | } |
| 717 | |
| 718 | ret = clk_get_by_name(dev, "fck", &cpsw_common->fclk); |
| 719 | if (ret) { |
| 720 | power_domain_free(&cpsw_common->pwrdmn); |
| 721 | dev_err(dev, "failed to get clock %d\n", ret); |
| 722 | return ret; |
| 723 | } |
| 724 | |
| 725 | cpsw_common->cpsw_base = cpsw_common->ss_base + AM65_CPSW_CPSW_NU_BASE; |
| 726 | cpsw_common->ale_base = cpsw_common->cpsw_base + |
| 727 | AM65_CPSW_CPSW_NU_ALE_BASE; |
Suman Anna | 18e40be | 2023-08-02 13:47:26 +0530 | [diff] [blame] | 728 | |
Vignesh Raghavendra | 2b834d0 | 2020-07-06 13:36:54 +0530 | [diff] [blame] | 729 | ports_np = dev_read_subnode(dev, "ethernet-ports"); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 730 | if (!ofnode_valid(ports_np)) { |
| 731 | ret = -ENOENT; |
| 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | ofnode_for_each_subnode(node, ports_np) { |
| 736 | const char *node_name; |
| 737 | u32 port_id; |
| 738 | bool disabled; |
| 739 | |
| 740 | node_name = ofnode_get_name(node); |
| 741 | |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 742 | disabled = !ofnode_is_enabled(node); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 743 | |
| 744 | ret = ofnode_read_u32(node, "reg", &port_id); |
| 745 | if (ret) { |
| 746 | dev_err(dev, "%s: failed to get port_id (%d)\n", |
| 747 | node_name, ret); |
| 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | if (port_id >= AM65_CPSW_CPSWNU_MAX_PORTS) { |
| 752 | dev_err(dev, "%s: invalid port_id (%d)\n", |
| 753 | node_name, port_id); |
| 754 | ret = -EINVAL; |
| 755 | goto out; |
| 756 | } |
| 757 | cpsw_common->port_num++; |
| 758 | |
| 759 | if (!port_id) |
| 760 | continue; |
| 761 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 762 | cpsw_common->ports[port_id].disabled = disabled; |
| 763 | if (disabled) |
| 764 | continue; |
| 765 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 766 | ret = device_bind_driver_to_node(dev, "am65_cpsw_nuss_port", ofnode_get_name(node), node, &port_dev); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 767 | if (ret) |
Vignesh Raghavendra | d26ac2e | 2022-01-21 12:47:51 +0530 | [diff] [blame] | 768 | dev_err(dev, "Failed to bind to %s node\n", ofnode_get_name(node)); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | for (i = 0; i < AM65_CPSW_CPSWNU_MAX_PORTS; i++) { |
| 772 | struct am65_cpsw_port *port = &cpsw_common->ports[i]; |
| 773 | |
| 774 | port->port_base = cpsw_common->cpsw_base + |
| 775 | AM65_CPSW_CPSW_NU_PORTS_OFFSET + |
| 776 | (i * AM65_CPSW_CPSW_NU_PORTS_OFFSET); |
Siddharth Vadapalli | 726fc0a | 2023-08-02 13:47:25 +0530 | [diff] [blame] | 777 | port->port_sgmii_base = cpsw_common->ss_base + |
| 778 | (i * AM65_CPSW_SGMII_BASE); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 779 | port->macsl_base = port->port_base + |
| 780 | AM65_CPSW_CPSW_NU_PORT_MACSL_OFFSET; |
| 781 | } |
| 782 | |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 783 | cpsw_common->bus_freq = |
| 784 | dev_read_u32_default(dev, "bus_freq", |
| 785 | AM65_CPSW_MDIO_BUS_FREQ_DEF); |
| 786 | |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 787 | dev_info(dev, "K3 CPSW: nuss_ver: 0x%08X cpsw_ver: 0x%08X ale_ver: 0x%08X Ports:%u\n", |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 788 | readl(cpsw_common->ss_base), |
| 789 | readl(cpsw_common->cpsw_base), |
| 790 | readl(cpsw_common->ale_base), |
Roger Quadros | fced6b6 | 2024-02-28 12:35:27 +0200 | [diff] [blame] | 791 | cpsw_common->port_num); |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 792 | |
| 793 | out: |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 794 | power_domain_free(&cpsw_common->pwrdmn); |
| 795 | return ret; |
| 796 | } |
| 797 | |
| 798 | static const struct udevice_id am65_cpsw_nuss_ids[] = { |
| 799 | { .compatible = "ti,am654-cpsw-nuss" }, |
Vignesh Raghavendra | 30bc6ea | 2019-12-04 22:17:23 +0530 | [diff] [blame] | 800 | { .compatible = "ti,j721e-cpsw-nuss" }, |
Vignesh Raghavendra | 1cc3562 | 2021-05-10 20:06:11 +0530 | [diff] [blame] | 801 | { .compatible = "ti,am642-cpsw-nuss" }, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 802 | { } |
| 803 | }; |
| 804 | |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 805 | U_BOOT_DRIVER(am65_cpsw_nuss) = { |
| 806 | .name = "am65_cpsw_nuss", |
| 807 | .id = UCLASS_MISC, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 808 | .of_match = am65_cpsw_nuss_ids, |
Vignesh Raghavendra | bbedbbb | 2021-12-24 12:55:30 +0530 | [diff] [blame] | 809 | .probe = am65_cpsw_probe_nuss, |
| 810 | .priv_auto = sizeof(struct am65_cpsw_common), |
| 811 | }; |
| 812 | |
| 813 | U_BOOT_DRIVER(am65_cpsw_nuss_port) = { |
| 814 | .name = "am65_cpsw_nuss_port", |
| 815 | .id = UCLASS_ETH, |
| 816 | .probe = am65_cpsw_port_probe, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 817 | .ops = &am65_cpsw_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 818 | .priv_auto = sizeof(struct am65_cpsw_priv), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 819 | .plat_auto = sizeof(struct eth_pdata), |
Vignesh Raghavendra | 198bbb1 | 2022-01-28 11:21:19 +0530 | [diff] [blame] | 820 | .flags = DM_FLAG_ALLOC_PRIV_DMA | DM_FLAG_OS_PREPARE, |
Keerthy | a00b95c | 2019-07-09 10:30:34 +0530 | [diff] [blame] | 821 | }; |