| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* Copyright Altera Corporation (C) 2014. All rights reserved. |
| 3 |
* |
| 4 |
* Adopted from dwmac-sti.c |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <linux/mfd/altera-sysmgr.h> |
| 8 |
#include <linux/clocksource_ids.h> |
| 9 |
#include <linux/of.h> |
| 10 |
#include <linux/of_address.h> |
| 11 |
#include <linux/of_net.h> |
| 12 |
#include <linux/phy.h> |
| 13 |
#include <linux/regmap.h> |
| 14 |
#include <linux/mdio/mdio-regmap.h> |
| 15 |
#include <linux/pcs-lynx.h> |
| 16 |
#include <linux/reset.h> |
| 17 |
#include <linux/stmmac.h> |
| 18 |
|
| 19 |
#include "dwxgmac2.h" |
| 20 |
#include "stmmac.h" |
| 21 |
#include "stmmac_platform.h" |
| 22 |
#include "stmmac_ptp.h" |
| 23 |
|
| 24 |
#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII 0x0 |
| 25 |
#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII 0x1 |
| 26 |
#define SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII 0x2 |
| 27 |
#define SYSMGR_EMACGRP_CTRL_PHYSEL_WIDTH 2 |
| 28 |
#define SYSMGR_EMACGRP_CTRL_PHYSEL_MASK 0x00000003 |
| 29 |
#define SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK 0x00000010 |
| 30 |
#define SYSMGR_GEN10_EMACGRP_CTRL_PTP_REF_CLK_MASK 0x00000100 |
| 31 |
|
| 32 |
#define SYSMGR_FPGAGRP_MODULE_REG 0x00000028 |
| 33 |
#define SYSMGR_FPGAGRP_MODULE_EMAC 0x00000004 |
| 34 |
#define SYSMGR_FPGAINTF_EMAC_REG 0x00000070 |
| 35 |
#define SYSMGR_FPGAINTF_EMAC_BIT 0x1 |
| 36 |
|
| 37 |
#define EMAC_SPLITTER_CTRL_REG 0x0 |
| 38 |
#define EMAC_SPLITTER_CTRL_SPEED_MASK 0x3 |
| 39 |
#define EMAC_SPLITTER_CTRL_SPEED_10 0x2 |
| 40 |
#define EMAC_SPLITTER_CTRL_SPEED_100 0x3 |
| 41 |
#define EMAC_SPLITTER_CTRL_SPEED_1000 0x0 |
| 42 |
|
| 43 |
#define SGMII_ADAPTER_CTRL_REG 0x00 |
| 44 |
#define SGMII_ADAPTER_ENABLE 0x0000 |
| 45 |
#define SGMII_ADAPTER_DISABLE 0x0001 |
| 46 |
|
| 47 |
#define SMTG_MDIO_ADDR 0x15 |
| 48 |
#define SMTG_TSC_WORD0 0xC |
| 49 |
#define SMTG_TSC_WORD1 0xD |
| 50 |
#define SMTG_TSC_WORD2 0xE |
| 51 |
#define SMTG_TSC_WORD3 0xF |
| 52 |
#define SMTG_TSC_SHIFT 16 |
| 53 |
|
| 54 |
struct socfpga_dwmac; |
| 55 |
struct socfpga_dwmac_ops { |
| 56 |
int (*set_phy_mode)(struct socfpga_dwmac *dwmac_priv); |
| 57 |
void (*setup_plat_dat)(struct socfpga_dwmac *dwmac_priv); |
| 58 |
}; |
| 59 |
|
| 60 |
struct socfpga_dwmac { |
| 61 |
u32 reg_offset; |
| 62 |
u32 reg_shift; |
| 63 |
struct device *dev; |
| 64 |
struct plat_stmmacenet_data *plat_dat; |
| 65 |
struct regmap *sys_mgr_base_addr; |
| 66 |
struct reset_control *stmmac_rst; |
| 67 |
struct reset_control *stmmac_ocp_rst; |
| 68 |
void __iomem *splitter_base; |
| 69 |
void __iomem *tse_pcs_base; |
| 70 |
void __iomem *sgmii_adapter_base; |
| 71 |
bool f2h_ptp_ref_clk; |
| 72 |
const struct socfpga_dwmac_ops *ops; |
| 73 |
}; |
| 74 |
|
| 75 |
static void socfpga_dwmac_fix_mac_speed(void *bsp_priv, int speed, |
| 76 |
unsigned int mode) |
| 77 |
{ |
| 78 |
struct socfpga_dwmac *dwmac = (struct socfpga_dwmac *)bsp_priv; |
| 79 |
struct stmmac_priv *priv = netdev_priv(dev_get_drvdata(dwmac->dev)); |
| 80 |
void __iomem *splitter_base = dwmac->splitter_base; |
| 81 |
void __iomem *sgmii_adapter_base = dwmac->sgmii_adapter_base; |
| 82 |
u32 val; |
| 83 |
|
| 84 |
if (sgmii_adapter_base) |
| 85 |
writew(SGMII_ADAPTER_DISABLE, |
| 86 |
sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG); |
| 87 |
|
| 88 |
if (splitter_base) { |
| 89 |
val = readl(splitter_base + EMAC_SPLITTER_CTRL_REG); |
| 90 |
val &= ~EMAC_SPLITTER_CTRL_SPEED_MASK; |
| 91 |
|
| 92 |
switch (speed) { |
| 93 |
case 1000: |
| 94 |
val |= EMAC_SPLITTER_CTRL_SPEED_1000; |
| 95 |
break; |
| 96 |
case 100: |
| 97 |
val |= EMAC_SPLITTER_CTRL_SPEED_100; |
| 98 |
break; |
| 99 |
case 10: |
| 100 |
val |= EMAC_SPLITTER_CTRL_SPEED_10; |
| 101 |
break; |
| 102 |
default: |
| 103 |
return; |
| 104 |
} |
| 105 |
writel(val, splitter_base + EMAC_SPLITTER_CTRL_REG); |
| 106 |
} |
| 107 |
|
| 108 |
if ((priv->plat->phy_interface == PHY_INTERFACE_MODE_SGMII || |
| 109 |
priv->plat->phy_interface == PHY_INTERFACE_MODE_1000BASEX) && |
| 110 |
sgmii_adapter_base) |
| 111 |
writew(SGMII_ADAPTER_ENABLE, |
| 112 |
sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG); |
| 113 |
} |
| 114 |
|
| 115 |
static int socfpga_dwmac_parse_data(struct socfpga_dwmac *dwmac, struct device *dev) |
| 116 |
{ |
| 117 |
struct device_node *np = dev->of_node; |
| 118 |
struct regmap *sys_mgr_base_addr; |
| 119 |
u32 reg_offset, reg_shift; |
| 120 |
int ret, index; |
| 121 |
struct device_node *np_splitter = NULL; |
| 122 |
struct device_node *np_sgmii_adapter = NULL; |
| 123 |
struct resource res_splitter; |
| 124 |
struct resource res_tse_pcs; |
| 125 |
struct resource res_sgmii_adapter; |
| 126 |
|
| 127 |
sys_mgr_base_addr = |
| 128 |
altr_sysmgr_regmap_lookup_by_phandle(np, "altr,sysmgr-syscon"); |
| 129 |
if (IS_ERR(sys_mgr_base_addr)) { |
| 130 |
dev_info(dev, "No sysmgr-syscon node found\n"); |
| 131 |
return PTR_ERR(sys_mgr_base_addr); |
| 132 |
} |
| 133 |
|
| 134 |
ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 1, ®_offset); |
| 135 |
if (ret) { |
| 136 |
dev_info(dev, "Could not read reg_offset from sysmgr-syscon!\n"); |
| 137 |
return -EINVAL; |
| 138 |
} |
| 139 |
|
| 140 |
ret = of_property_read_u32_index(np, "altr,sysmgr-syscon", 2, ®_shift); |
| 141 |
if (ret) { |
| 142 |
dev_info(dev, "Could not read reg_shift from sysmgr-syscon!\n"); |
| 143 |
return -EINVAL; |
| 144 |
} |
| 145 |
|
| 146 |
dwmac->f2h_ptp_ref_clk = of_property_read_bool(np, "altr,f2h_ptp_ref_clk"); |
| 147 |
|
| 148 |
np_splitter = of_parse_phandle(np, "altr,emac-splitter", 0); |
| 149 |
if (np_splitter) { |
| 150 |
ret = of_address_to_resource(np_splitter, 0, &res_splitter); |
| 151 |
of_node_put(np_splitter); |
| 152 |
if (ret) { |
| 153 |
dev_info(dev, "Missing emac splitter address\n"); |
| 154 |
return -EINVAL; |
| 155 |
} |
| 156 |
|
| 157 |
dwmac->splitter_base = devm_ioremap_resource(dev, &res_splitter); |
| 158 |
if (IS_ERR(dwmac->splitter_base)) { |
| 159 |
dev_info(dev, "Failed to mapping emac splitter\n"); |
| 160 |
return PTR_ERR(dwmac->splitter_base); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
np_sgmii_adapter = of_parse_phandle(np, |
| 165 |
"altr,gmii-to-sgmii-converter", 0); |
| 166 |
if (np_sgmii_adapter) { |
| 167 |
index = of_property_match_string(np_sgmii_adapter, "reg-names", |
| 168 |
"hps_emac_interface_splitter_avalon_slave"); |
| 169 |
|
| 170 |
if (index >= 0) { |
| 171 |
if (of_address_to_resource(np_sgmii_adapter, index, |
| 172 |
&res_splitter)) { |
| 173 |
dev_err(dev, |
| 174 |
"%s: ERROR: missing emac splitter address\n", |
| 175 |
__func__); |
| 176 |
ret = -EINVAL; |
| 177 |
goto err_node_put; |
| 178 |
} |
| 179 |
|
| 180 |
dwmac->splitter_base = |
| 181 |
devm_ioremap_resource(dev, &res_splitter); |
| 182 |
|
| 183 |
if (IS_ERR(dwmac->splitter_base)) { |
| 184 |
ret = PTR_ERR(dwmac->splitter_base); |
| 185 |
goto err_node_put; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
index = of_property_match_string(np_sgmii_adapter, "reg-names", |
| 190 |
"gmii_to_sgmii_adapter_avalon_slave"); |
| 191 |
|
| 192 |
if (index >= 0) { |
| 193 |
if (of_address_to_resource(np_sgmii_adapter, index, |
| 194 |
&res_sgmii_adapter)) { |
| 195 |
dev_err(dev, |
| 196 |
"%s: ERROR: failed mapping adapter\n", |
| 197 |
__func__); |
| 198 |
ret = -EINVAL; |
| 199 |
goto err_node_put; |
| 200 |
} |
| 201 |
|
| 202 |
dwmac->sgmii_adapter_base = |
| 203 |
devm_ioremap_resource(dev, &res_sgmii_adapter); |
| 204 |
|
| 205 |
if (IS_ERR(dwmac->sgmii_adapter_base)) { |
| 206 |
ret = PTR_ERR(dwmac->sgmii_adapter_base); |
| 207 |
goto err_node_put; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
index = of_property_match_string(np_sgmii_adapter, "reg-names", |
| 212 |
"eth_tse_control_port"); |
| 213 |
|
| 214 |
if (index >= 0) { |
| 215 |
if (of_address_to_resource(np_sgmii_adapter, index, |
| 216 |
&res_tse_pcs)) { |
| 217 |
dev_err(dev, |
| 218 |
"%s: ERROR: failed mapping tse control port\n", |
| 219 |
__func__); |
| 220 |
ret = -EINVAL; |
| 221 |
goto err_node_put; |
| 222 |
} |
| 223 |
|
| 224 |
dwmac->tse_pcs_base = |
| 225 |
devm_ioremap_resource(dev, &res_tse_pcs); |
| 226 |
|
| 227 |
if (IS_ERR(dwmac->tse_pcs_base)) { |
| 228 |
ret = PTR_ERR(dwmac->tse_pcs_base); |
| 229 |
goto err_node_put; |
| 230 |
} |
| 231 |
} |
| 232 |
} |
| 233 |
dwmac->reg_offset = reg_offset; |
| 234 |
dwmac->reg_shift = reg_shift; |
| 235 |
dwmac->sys_mgr_base_addr = sys_mgr_base_addr; |
| 236 |
dwmac->dev = dev; |
| 237 |
of_node_put(np_sgmii_adapter); |
| 238 |
|
| 239 |
return 0; |
| 240 |
|
| 241 |
err_node_put: |
| 242 |
of_node_put(np_sgmii_adapter); |
| 243 |
return ret; |
| 244 |
} |
| 245 |
|
| 246 |
static int socfpga_get_plat_phymode(struct socfpga_dwmac *dwmac) |
| 247 |
{ |
| 248 |
return dwmac->plat_dat->phy_interface; |
| 249 |
} |
| 250 |
|
| 251 |
static void socfpga_sgmii_config(struct socfpga_dwmac *dwmac, bool enable) |
| 252 |
{ |
| 253 |
u16 val = enable ? SGMII_ADAPTER_ENABLE : SGMII_ADAPTER_DISABLE; |
| 254 |
|
| 255 |
writew(val, dwmac->sgmii_adapter_base + SGMII_ADAPTER_CTRL_REG); |
| 256 |
} |
| 257 |
|
| 258 |
static int socfpga_set_phy_mode_common(int phymode, u32 *val) |
| 259 |
{ |
| 260 |
switch (phymode) { |
| 261 |
case PHY_INTERFACE_MODE_RGMII: |
| 262 |
case PHY_INTERFACE_MODE_RGMII_ID: |
| 263 |
case PHY_INTERFACE_MODE_RGMII_RXID: |
| 264 |
case PHY_INTERFACE_MODE_RGMII_TXID: |
| 265 |
*val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII; |
| 266 |
break; |
| 267 |
case PHY_INTERFACE_MODE_MII: |
| 268 |
case PHY_INTERFACE_MODE_GMII: |
| 269 |
case PHY_INTERFACE_MODE_SGMII: |
| 270 |
case PHY_INTERFACE_MODE_1000BASEX: |
| 271 |
*val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII; |
| 272 |
break; |
| 273 |
case PHY_INTERFACE_MODE_RMII: |
| 274 |
*val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII; |
| 275 |
break; |
| 276 |
default: |
| 277 |
return -EINVAL; |
| 278 |
} |
| 279 |
return 0; |
| 280 |
} |
| 281 |
|
| 282 |
static void get_smtgtime(struct mii_bus *mii, int smtg_addr, u64 *smtg_time) |
| 283 |
{ |
| 284 |
u64 ns; |
| 285 |
|
| 286 |
ns = mdiobus_read(mii, smtg_addr, SMTG_TSC_WORD3); |
| 287 |
ns <<= SMTG_TSC_SHIFT; |
| 288 |
ns |= mdiobus_read(mii, smtg_addr, SMTG_TSC_WORD2); |
| 289 |
ns <<= SMTG_TSC_SHIFT; |
| 290 |
ns |= mdiobus_read(mii, smtg_addr, SMTG_TSC_WORD1); |
| 291 |
ns <<= SMTG_TSC_SHIFT; |
| 292 |
ns |= mdiobus_read(mii, smtg_addr, SMTG_TSC_WORD0); |
| 293 |
|
| 294 |
*smtg_time = ns; |
| 295 |
} |
| 296 |
|
| 297 |
static int smtg_crosststamp(ktime_t *device, struct system_counterval_t *system, |
| 298 |
void *ctx) |
| 299 |
{ |
| 300 |
struct stmmac_priv *priv = (struct stmmac_priv *)ctx; |
| 301 |
u32 num_snapshot, gpio_value, acr_value; |
| 302 |
void __iomem *ptpaddr = priv->ptpaddr; |
| 303 |
void __iomem *ioaddr = priv->hw->pcsr; |
| 304 |
unsigned long flags; |
| 305 |
u64 smtg_time = 0; |
| 306 |
u64 ptp_time = 0; |
| 307 |
int i, ret; |
| 308 |
u32 v; |
| 309 |
|
| 310 |
/* Both internal crosstimestamping and external triggered event |
| 311 |
* timestamping cannot be run concurrently. |
| 312 |
*/ |
| 313 |
if (priv->plat->flags & STMMAC_FLAG_EXT_SNAPSHOT_EN) |
| 314 |
return -EBUSY; |
| 315 |
|
| 316 |
mutex_lock(&priv->aux_ts_lock); |
| 317 |
/* Enable Internal snapshot trigger */ |
| 318 |
acr_value = readl(ptpaddr + PTP_ACR); |
| 319 |
acr_value &= ~PTP_ACR_MASK; |
| 320 |
switch (priv->plat->int_snapshot_num) { |
| 321 |
case AUX_SNAPSHOT0: |
| 322 |
acr_value |= PTP_ACR_ATSEN0; |
| 323 |
break; |
| 324 |
case AUX_SNAPSHOT1: |
| 325 |
acr_value |= PTP_ACR_ATSEN1; |
| 326 |
break; |
| 327 |
case AUX_SNAPSHOT2: |
| 328 |
acr_value |= PTP_ACR_ATSEN2; |
| 329 |
break; |
| 330 |
case AUX_SNAPSHOT3: |
| 331 |
acr_value |= PTP_ACR_ATSEN3; |
| 332 |
break; |
| 333 |
default: |
| 334 |
mutex_unlock(&priv->aux_ts_lock); |
| 335 |
return -EINVAL; |
| 336 |
} |
| 337 |
writel(acr_value, ptpaddr + PTP_ACR); |
| 338 |
|
| 339 |
/* Clear FIFO */ |
| 340 |
acr_value = readl(ptpaddr + PTP_ACR); |
| 341 |
acr_value |= PTP_ACR_ATSFC; |
| 342 |
writel(acr_value, ptpaddr + PTP_ACR); |
| 343 |
/* Release the mutex */ |
| 344 |
mutex_unlock(&priv->aux_ts_lock); |
| 345 |
|
| 346 |
/* Trigger Internal snapshot signal. Create a rising edge by just toggle |
| 347 |
* the GPO0 to low and back to high. |
| 348 |
*/ |
| 349 |
gpio_value = readl(ioaddr + XGMAC_GPIO_STATUS); |
| 350 |
gpio_value &= ~XGMAC_GPIO_GPO0; |
| 351 |
writel(gpio_value, ioaddr + XGMAC_GPIO_STATUS); |
| 352 |
gpio_value |= XGMAC_GPIO_GPO0; |
| 353 |
writel(gpio_value, ioaddr + XGMAC_GPIO_STATUS); |
| 354 |
|
| 355 |
/* Poll for time sync operation done */ |
| 356 |
ret = readl_poll_timeout(priv->ioaddr + XGMAC_INT_STATUS, v, |
| 357 |
(v & XGMAC_INT_TSIS), 100, 10000); |
| 358 |
if (ret) { |
| 359 |
netdev_err(priv->dev, "%s: Wait for time sync operation timeout\n", |
| 360 |
__func__); |
| 361 |
return ret; |
| 362 |
} |
| 363 |
|
| 364 |
*system = (struct system_counterval_t) { |
| 365 |
.cycles = 0, |
| 366 |
.cs_id = CSID_ARM_ARCH_COUNTER, |
| 367 |
.use_nsecs = false, |
| 368 |
}; |
| 369 |
|
| 370 |
num_snapshot = FIELD_GET(XGMAC_TIMESTAMP_ATSNS_MASK, |
| 371 |
readl(ioaddr + XGMAC_TIMESTAMP_STATUS)); |
| 372 |
|
| 373 |
/* Repeat until the timestamps are from the FIFO last segment */ |
| 374 |
for (i = 0; i < num_snapshot; i++) { |
| 375 |
read_lock_irqsave(&priv->ptp_lock, flags); |
| 376 |
stmmac_get_ptptime(priv, ptpaddr, &ptp_time); |
| 377 |
*device = ns_to_ktime(ptp_time); |
| 378 |
read_unlock_irqrestore(&priv->ptp_lock, flags); |
| 379 |
} |
| 380 |
|
| 381 |
get_smtgtime(priv->mii, SMTG_MDIO_ADDR, &smtg_time); |
| 382 |
system->cycles = smtg_time; |
| 383 |
|
| 384 |
return 0; |
| 385 |
} |
| 386 |
|
| 387 |
static int socfpga_gen5_set_phy_mode(struct socfpga_dwmac *dwmac) |
| 388 |
{ |
| 389 |
struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr; |
| 390 |
int phymode = socfpga_get_plat_phymode(dwmac); |
| 391 |
u32 reg_offset = dwmac->reg_offset; |
| 392 |
u32 reg_shift = dwmac->reg_shift; |
| 393 |
u32 ctrl, val, module; |
| 394 |
|
| 395 |
if (socfpga_set_phy_mode_common(phymode, &val)) { |
| 396 |
dev_err(dwmac->dev, "bad phy mode %d\n", phymode); |
| 397 |
return -EINVAL; |
| 398 |
} |
| 399 |
|
| 400 |
/* Overwrite val to GMII if splitter core is enabled. The phymode here |
| 401 |
* is the actual phy mode on phy hardware, but phy interface from |
| 402 |
* EMAC core is GMII. |
| 403 |
*/ |
| 404 |
if (dwmac->splitter_base) |
| 405 |
val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII; |
| 406 |
|
| 407 |
/* Assert reset to the enet controller before changing the phy mode */ |
| 408 |
reset_control_assert(dwmac->stmmac_ocp_rst); |
| 409 |
reset_control_assert(dwmac->stmmac_rst); |
| 410 |
|
| 411 |
regmap_read(sys_mgr_base_addr, reg_offset, &ctrl); |
| 412 |
ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK << reg_shift); |
| 413 |
ctrl |= val << reg_shift; |
| 414 |
|
| 415 |
if (dwmac->f2h_ptp_ref_clk || |
| 416 |
phymode == PHY_INTERFACE_MODE_MII || |
| 417 |
phymode == PHY_INTERFACE_MODE_GMII || |
| 418 |
phymode == PHY_INTERFACE_MODE_SGMII) { |
| 419 |
regmap_read(sys_mgr_base_addr, SYSMGR_FPGAGRP_MODULE_REG, |
| 420 |
&module); |
| 421 |
module |= (SYSMGR_FPGAGRP_MODULE_EMAC << (reg_shift / 2)); |
| 422 |
regmap_write(sys_mgr_base_addr, SYSMGR_FPGAGRP_MODULE_REG, |
| 423 |
module); |
| 424 |
} |
| 425 |
|
| 426 |
if (dwmac->f2h_ptp_ref_clk) |
| 427 |
ctrl |= SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK << (reg_shift / 2); |
| 428 |
else |
| 429 |
ctrl &= ~(SYSMGR_EMACGRP_CTRL_PTP_REF_CLK_MASK << |
| 430 |
(reg_shift / 2)); |
| 431 |
|
| 432 |
regmap_write(sys_mgr_base_addr, reg_offset, ctrl); |
| 433 |
|
| 434 |
/* Deassert reset for the phy configuration to be sampled by |
| 435 |
* the enet controller, and operation to start in requested mode |
| 436 |
*/ |
| 437 |
reset_control_deassert(dwmac->stmmac_ocp_rst); |
| 438 |
reset_control_deassert(dwmac->stmmac_rst); |
| 439 |
if (phymode == PHY_INTERFACE_MODE_SGMII) |
| 440 |
socfpga_sgmii_config(dwmac, true); |
| 441 |
|
| 442 |
return 0; |
| 443 |
} |
| 444 |
|
| 445 |
static int socfpga_gen10_set_phy_mode(struct socfpga_dwmac *dwmac) |
| 446 |
{ |
| 447 |
struct regmap *sys_mgr_base_addr = dwmac->sys_mgr_base_addr; |
| 448 |
int phymode = socfpga_get_plat_phymode(dwmac); |
| 449 |
u32 reg_offset = dwmac->reg_offset; |
| 450 |
u32 reg_shift = dwmac->reg_shift; |
| 451 |
u32 ctrl, val, module; |
| 452 |
|
| 453 |
if (socfpga_set_phy_mode_common(phymode, &val)) |
| 454 |
return -EINVAL; |
| 455 |
|
| 456 |
/* Overwrite val to GMII if splitter core is enabled. The phymode here |
| 457 |
* is the actual phy mode on phy hardware, but phy interface from |
| 458 |
* EMAC core is GMII. |
| 459 |
*/ |
| 460 |
if (dwmac->splitter_base) |
| 461 |
val = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII; |
| 462 |
|
| 463 |
/* Assert reset to the enet controller before changing the phy mode */ |
| 464 |
reset_control_assert(dwmac->stmmac_ocp_rst); |
| 465 |
reset_control_assert(dwmac->stmmac_rst); |
| 466 |
|
| 467 |
regmap_read(sys_mgr_base_addr, reg_offset, &ctrl); |
| 468 |
ctrl &= ~(SYSMGR_EMACGRP_CTRL_PHYSEL_MASK); |
| 469 |
ctrl |= val; |
| 470 |
|
| 471 |
if (dwmac->f2h_ptp_ref_clk || |
| 472 |
phymode == PHY_INTERFACE_MODE_MII || |
| 473 |
phymode == PHY_INTERFACE_MODE_GMII || |
| 474 |
phymode == PHY_INTERFACE_MODE_SGMII) { |
| 475 |
ctrl |= SYSMGR_GEN10_EMACGRP_CTRL_PTP_REF_CLK_MASK; |
| 476 |
regmap_read(sys_mgr_base_addr, SYSMGR_FPGAINTF_EMAC_REG, |
| 477 |
&module); |
| 478 |
module |= (SYSMGR_FPGAINTF_EMAC_BIT << reg_shift); |
| 479 |
regmap_write(sys_mgr_base_addr, SYSMGR_FPGAINTF_EMAC_REG, |
| 480 |
module); |
| 481 |
} else { |
| 482 |
ctrl &= ~SYSMGR_GEN10_EMACGRP_CTRL_PTP_REF_CLK_MASK; |
| 483 |
} |
| 484 |
|
| 485 |
regmap_write(sys_mgr_base_addr, reg_offset, ctrl); |
| 486 |
|
| 487 |
/* Deassert reset for the phy configuration to be sampled by |
| 488 |
* the enet controller, and operation to start in requested mode |
| 489 |
*/ |
| 490 |
reset_control_deassert(dwmac->stmmac_ocp_rst); |
| 491 |
reset_control_deassert(dwmac->stmmac_rst); |
| 492 |
if (phymode == PHY_INTERFACE_MODE_SGMII) |
| 493 |
socfpga_sgmii_config(dwmac, true); |
| 494 |
return 0; |
| 495 |
} |
| 496 |
|
| 497 |
static int socfpga_dwmac_pcs_init(struct stmmac_priv *priv) |
| 498 |
{ |
| 499 |
struct socfpga_dwmac *dwmac = priv->plat->bsp_priv; |
| 500 |
struct regmap_config pcs_regmap_cfg = { |
| 501 |
.reg_bits = 16, |
| 502 |
.val_bits = 16, |
| 503 |
.reg_shift = REGMAP_UPSHIFT(1), |
| 504 |
}; |
| 505 |
struct mdio_regmap_config mrc; |
| 506 |
struct regmap *pcs_regmap; |
| 507 |
struct phylink_pcs *pcs; |
| 508 |
struct mii_bus *pcs_bus; |
| 509 |
|
| 510 |
if (!dwmac->tse_pcs_base) |
| 511 |
return 0; |
| 512 |
|
| 513 |
pcs_regmap = devm_regmap_init_mmio(priv->device, dwmac->tse_pcs_base, |
| 514 |
&pcs_regmap_cfg); |
| 515 |
if (IS_ERR(pcs_regmap)) |
| 516 |
return PTR_ERR(pcs_regmap); |
| 517 |
|
| 518 |
memset(&mrc, 0, sizeof(mrc)); |
| 519 |
mrc.regmap = pcs_regmap; |
| 520 |
mrc.parent = priv->device; |
| 521 |
mrc.valid_addr = 0x0; |
| 522 |
mrc.autoscan = false; |
| 523 |
|
| 524 |
/* Can't use ndev->name here because it will not have been initialised, |
| 525 |
* and in any case, the user can rename network interfaces at runtime. |
| 526 |
*/ |
| 527 |
snprintf(mrc.name, MII_BUS_ID_SIZE, "%s-pcs-mii", |
| 528 |
dev_name(priv->device)); |
| 529 |
pcs_bus = devm_mdio_regmap_register(priv->device, &mrc); |
| 530 |
if (IS_ERR(pcs_bus)) |
| 531 |
return PTR_ERR(pcs_bus); |
| 532 |
|
| 533 |
pcs = lynx_pcs_create_mdiodev(pcs_bus, 0); |
| 534 |
if (IS_ERR(pcs)) |
| 535 |
return PTR_ERR(pcs); |
| 536 |
|
| 537 |
priv->hw->phylink_pcs = pcs; |
| 538 |
return 0; |
| 539 |
} |
| 540 |
|
| 541 |
static void socfpga_dwmac_pcs_exit(struct stmmac_priv *priv) |
| 542 |
{ |
| 543 |
if (priv->hw->phylink_pcs) |
| 544 |
lynx_pcs_destroy(priv->hw->phylink_pcs); |
| 545 |
} |
| 546 |
|
| 547 |
static struct phylink_pcs *socfpga_dwmac_select_pcs(struct stmmac_priv *priv, |
| 548 |
phy_interface_t interface) |
| 549 |
{ |
| 550 |
return priv->hw->phylink_pcs; |
| 551 |
} |
| 552 |
|
| 553 |
static int socfpga_dwmac_init(struct device *dev, void *bsp_priv) |
| 554 |
{ |
| 555 |
struct socfpga_dwmac *dwmac = bsp_priv; |
| 556 |
|
| 557 |
return dwmac->ops->set_phy_mode(dwmac); |
| 558 |
} |
| 559 |
|
| 560 |
static void socfpga_gen5_setup_plat_dat(struct socfpga_dwmac *dwmac) |
| 561 |
{ |
| 562 |
struct plat_stmmacenet_data *plat_dat = dwmac->plat_dat; |
| 563 |
|
| 564 |
plat_dat->core_type = DWMAC_CORE_GMAC; |
| 565 |
|
| 566 |
/* Rx watchdog timer in dwmac is buggy in this hw */ |
| 567 |
plat_dat->riwt_off = 1; |
| 568 |
} |
| 569 |
|
| 570 |
static void socfpga_agilex5_setup_plat_dat(struct socfpga_dwmac *dwmac) |
| 571 |
{ |
| 572 |
struct plat_stmmacenet_data *plat_dat = dwmac->plat_dat; |
| 573 |
|
| 574 |
plat_dat->core_type = DWMAC_CORE_XGMAC; |
| 575 |
|
| 576 |
/* Enable TSO */ |
| 577 |
plat_dat->flags |= STMMAC_FLAG_TSO_EN; |
| 578 |
|
| 579 |
/* Enable TBS */ |
| 580 |
switch (plat_dat->tx_queues_to_use) { |
| 581 |
case 8: |
| 582 |
plat_dat->tx_queues_cfg[7].tbs_en = true; |
| 583 |
fallthrough; |
| 584 |
case 7: |
| 585 |
plat_dat->tx_queues_cfg[6].tbs_en = true; |
| 586 |
break; |
| 587 |
default: |
| 588 |
/* Tx Queues 0 - 5 doesn't support TBS on Agilex5 */ |
| 589 |
break; |
| 590 |
} |
| 591 |
|
| 592 |
/* Hw supported cross-timestamp */ |
| 593 |
plat_dat->int_snapshot_num = AUX_SNAPSHOT0; |
| 594 |
plat_dat->crosststamp = smtg_crosststamp; |
| 595 |
} |
| 596 |
|
| 597 |
static int socfpga_dwmac_probe(struct platform_device *pdev) |
| 598 |
{ |
| 599 |
struct plat_stmmacenet_data *plat_dat; |
| 600 |
struct stmmac_resources stmmac_res; |
| 601 |
struct device *dev = &pdev->dev; |
| 602 |
int ret; |
| 603 |
struct socfpga_dwmac *dwmac; |
| 604 |
const struct socfpga_dwmac_ops *ops; |
| 605 |
|
| 606 |
ops = device_get_match_data(&pdev->dev); |
| 607 |
if (!ops) { |
| 608 |
dev_err(&pdev->dev, "no of match data provided\n"); |
| 609 |
return -EINVAL; |
| 610 |
} |
| 611 |
|
| 612 |
ret = stmmac_get_platform_resources(pdev, &stmmac_res); |
| 613 |
if (ret) |
| 614 |
return ret; |
| 615 |
|
| 616 |
plat_dat = devm_stmmac_probe_config_dt(pdev, stmmac_res.mac); |
| 617 |
if (IS_ERR(plat_dat)) |
| 618 |
return PTR_ERR(plat_dat); |
| 619 |
|
| 620 |
dwmac = devm_kzalloc(dev, sizeof(*dwmac), GFP_KERNEL); |
| 621 |
if (!dwmac) |
| 622 |
return -ENOMEM; |
| 623 |
|
| 624 |
dwmac->stmmac_ocp_rst = devm_reset_control_get_optional(dev, "stmmaceth-ocp"); |
| 625 |
if (IS_ERR(dwmac->stmmac_ocp_rst)) { |
| 626 |
ret = PTR_ERR(dwmac->stmmac_ocp_rst); |
| 627 |
dev_err(dev, "error getting reset control of ocp %d\n", ret); |
| 628 |
return ret; |
| 629 |
} |
| 630 |
|
| 631 |
reset_control_deassert(dwmac->stmmac_ocp_rst); |
| 632 |
|
| 633 |
ret = socfpga_dwmac_parse_data(dwmac, dev); |
| 634 |
if (ret) { |
| 635 |
dev_err(dev, "Unable to parse OF data\n"); |
| 636 |
return ret; |
| 637 |
} |
| 638 |
|
| 639 |
/* The socfpga driver needs to control the stmmac reset to set the phy |
| 640 |
* mode. Create a copy of the core reset handle so it can be used by |
| 641 |
* the driver later. |
| 642 |
*/ |
| 643 |
dwmac->stmmac_rst = plat_dat->stmmac_rst; |
| 644 |
dwmac->ops = ops; |
| 645 |
dwmac->plat_dat = plat_dat; |
| 646 |
|
| 647 |
plat_dat->bsp_priv = dwmac; |
| 648 |
plat_dat->fix_mac_speed = socfpga_dwmac_fix_mac_speed; |
| 649 |
plat_dat->init = socfpga_dwmac_init; |
| 650 |
plat_dat->pcs_init = socfpga_dwmac_pcs_init; |
| 651 |
plat_dat->pcs_exit = socfpga_dwmac_pcs_exit; |
| 652 |
plat_dat->select_pcs = socfpga_dwmac_select_pcs; |
| 653 |
|
| 654 |
ops->setup_plat_dat(dwmac); |
| 655 |
|
| 656 |
return devm_stmmac_pltfr_probe(pdev, plat_dat, &stmmac_res); |
| 657 |
} |
| 658 |
|
| 659 |
static const struct socfpga_dwmac_ops socfpga_gen5_ops = { |
| 660 |
.set_phy_mode = socfpga_gen5_set_phy_mode, |
| 661 |
.setup_plat_dat = socfpga_gen5_setup_plat_dat, |
| 662 |
}; |
| 663 |
|
| 664 |
static const struct socfpga_dwmac_ops socfpga_gen10_ops = { |
| 665 |
.set_phy_mode = socfpga_gen10_set_phy_mode, |
| 666 |
.setup_plat_dat = socfpga_gen5_setup_plat_dat, |
| 667 |
}; |
| 668 |
|
| 669 |
static const struct socfpga_dwmac_ops socfpga_agilex5_ops = { |
| 670 |
.set_phy_mode = socfpga_gen10_set_phy_mode, |
| 671 |
.setup_plat_dat = socfpga_agilex5_setup_plat_dat, |
| 672 |
}; |
| 673 |
|
| 674 |
static const struct of_device_id socfpga_dwmac_match[] = { |
| 675 |
{ .compatible = "altr,socfpga-stmmac", .data = &socfpga_gen5_ops }, |
| 676 |
{ .compatible = "altr,socfpga-stmmac-a10-s10", .data = &socfpga_gen10_ops }, |
| 677 |
{ .compatible = "altr,socfpga-stmmac-agilex5", .data = &socfpga_agilex5_ops }, |
| 678 |
{ } |
| 679 |
}; |
| 680 |
MODULE_DEVICE_TABLE(of, socfpga_dwmac_match); |
| 681 |
|
| 682 |
static struct platform_driver socfpga_dwmac_driver = { |
| 683 |
.probe = socfpga_dwmac_probe, |
| 684 |
.driver = { |
| 685 |
.name = "socfpga-dwmac", |
| 686 |
.pm = &stmmac_pltfr_pm_ops, |
| 687 |
.of_match_table = socfpga_dwmac_match, |
| 688 |
}, |
| 689 |
}; |
| 690 |
module_platform_driver(socfpga_dwmac_driver); |
| 691 |
|
| 692 |
MODULE_DESCRIPTION("Altera SOC DWMAC Specific Glue layer"); |
| 693 |
MODULE_LICENSE("GPL v2"); |