Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'net-stmmac-hwif-c-cleanups'

Russell King says:

====================
net: stmmac: hwif.c cleanups

This series cleans up hwif.c:

- move the reading of the version information out of stmmac_hwif_init()
into its own function, stmmac_get_version(), storing the result in a
new struct.

- simplify stmmac_get_version().

- read the version register once, passing it to stmmac_get_id() and
stmmac_get_dev_id().

- move stmmac_get_id() and stmmac_get_dev_id() into
stmmac_get_version()

- define version register fields and use FIELD_GET() to decode

- start tackling the big loop in stmmac_hwif_init() - provide a
function, stmmac_hwif_find(), which looks up the hwif entry, thus
making a much smaller loop, which improves readability of this code.

- change the use of '^' to '!=' when comparing the dev_id, which is
what is really meant here.

- reorganise the test after calling stmmac_hwif_init() so that we
handle the error case in the indented code, and the success case
with no indent, which is the classical arrangement.
====================

Link: https://patch.msgid.link/aQFZVSGJuv8-_DIo@shell.armlinux.org.uk
Signed-off-by: Jakub Kicinski <kuba@kernel.org>

+101 -74
+3
drivers/net/ethernet/stmicro/stmmac/common.h
··· 26 26 #include "hwif.h" 27 27 #include "mmc.h" 28 28 29 + #define DWMAC_SNPSVER GENMASK_U32(7, 0) 30 + #define DWMAC_USERVER GENMASK_U32(15, 8) 31 + 29 32 /* Synopsys Core versions */ 30 33 #define DWMAC_CORE_3_40 0x34 31 34 #define DWMAC_CORE_3_50 0x35
+98 -74
drivers/net/ethernet/stmicro/stmmac/hwif.c
··· 13 13 #include "dwmac4_descs.h" 14 14 #include "dwxgmac2.h" 15 15 16 - static u32 stmmac_get_id(struct stmmac_priv *priv, u32 id_reg) 17 - { 18 - u32 reg = readl(priv->ioaddr + id_reg); 16 + struct stmmac_version { 17 + u8 snpsver; 18 + u8 dev_id; 19 + }; 19 20 20 - if (!reg) { 21 + static void stmmac_get_version(struct stmmac_priv *priv, 22 + struct stmmac_version *ver) 23 + { 24 + enum dwmac_core_type core_type = priv->plat->core_type; 25 + unsigned int version_offset; 26 + u32 version; 27 + 28 + ver->snpsver = 0; 29 + ver->dev_id = 0; 30 + 31 + if (core_type == DWMAC_CORE_MAC100) 32 + return; 33 + 34 + if (core_type == DWMAC_CORE_GMAC) 35 + version_offset = GMAC_VERSION; 36 + else 37 + version_offset = GMAC4_VERSION; 38 + 39 + version = readl(priv->ioaddr + version_offset); 40 + if (version == 0) { 21 41 dev_info(priv->device, "Version ID not available\n"); 22 - return 0x0; 42 + return; 23 43 } 24 44 25 45 dev_info(priv->device, "User ID: 0x%x, Synopsys ID: 0x%x\n", 26 - (unsigned int)(reg & GENMASK(15, 8)) >> 8, 27 - (unsigned int)(reg & GENMASK(7, 0))); 28 - return reg & GENMASK(7, 0); 29 - } 46 + FIELD_GET(DWMAC_USERVER, version), 47 + FIELD_GET(DWMAC_SNPSVER, version)); 30 48 31 - static u32 stmmac_get_dev_id(struct stmmac_priv *priv, u32 id_reg) 32 - { 33 - u32 reg = readl(priv->ioaddr + id_reg); 34 - 35 - if (!reg) { 36 - dev_info(priv->device, "Version ID not available\n"); 37 - return 0x0; 38 - } 39 - 40 - return (reg & GENMASK(15, 8)) >> 8; 49 + ver->snpsver = FIELD_GET(DWMAC_SNPSVER, version); 50 + if (core_type == DWMAC_CORE_XGMAC) 51 + ver->dev_id = FIELD_GET(DWMAC_USERVER, version); 41 52 } 42 53 43 54 static void stmmac_dwmac_mode_quirk(struct stmmac_priv *priv) ··· 299 288 }, 300 289 }; 301 290 291 + static const struct stmmac_hwif_entry * 292 + stmmac_hwif_find(enum dwmac_core_type core_type, u8 snpsver, u8 dev_id) 293 + { 294 + const struct stmmac_hwif_entry *entry; 295 + int i; 296 + 297 + for (i = ARRAY_SIZE(stmmac_hw) - 1; i >= 0; i--) { 298 + entry = &stmmac_hw[i]; 299 + 300 + if (core_type != entry->core_type) 301 + continue; 302 + /* Use synopsys_id var because some setups can override this */ 303 + if (snpsver < entry->min_id) 304 + continue; 305 + if (core_type == DWMAC_CORE_XGMAC && 306 + dev_id != entry->dev_id) 307 + continue; 308 + 309 + return entry; 310 + } 311 + 312 + return NULL; 313 + } 314 + 302 315 int stmmac_hwif_init(struct stmmac_priv *priv) 303 316 { 304 317 enum dwmac_core_type core_type = priv->plat->core_type; 305 318 const struct stmmac_hwif_entry *entry; 319 + struct stmmac_version version; 306 320 struct mac_device_info *mac; 307 321 bool needs_setup = true; 308 - u32 id, dev_id = 0; 309 - int i, ret; 322 + int ret; 310 323 311 - if (core_type == DWMAC_CORE_GMAC) { 312 - id = stmmac_get_id(priv, GMAC_VERSION); 313 - } else if (dwmac_is_xmac(core_type)) { 314 - id = stmmac_get_id(priv, GMAC4_VERSION); 315 - if (core_type == DWMAC_CORE_XGMAC) 316 - dev_id = stmmac_get_dev_id(priv, GMAC4_VERSION); 317 - } else { 318 - id = 0; 319 - } 324 + stmmac_get_version(priv, &version); 320 325 321 326 /* Save ID for later use */ 322 - priv->synopsys_id = id; 327 + priv->synopsys_id = version.snpsver; 323 328 324 329 /* Lets assume some safe values first */ 325 330 if (core_type == DWMAC_CORE_GMAC4) { ··· 363 336 spin_lock_init(&mac->irq_ctrl_lock); 364 337 365 338 /* Fallback to generic HW */ 366 - for (i = ARRAY_SIZE(stmmac_hw) - 1; i >= 0; i--) { 367 - entry = &stmmac_hw[i]; 368 339 369 - if (core_type != entry->core_type) 370 - continue; 371 - /* Use synopsys_id var because some setups can override this */ 372 - if (priv->synopsys_id < entry->min_id) 373 - continue; 374 - if (core_type == DWMAC_CORE_XGMAC && (dev_id ^ entry->dev_id)) 375 - continue; 340 + /* Use synopsys_id var because some setups can override this */ 341 + entry = stmmac_hwif_find(core_type, priv->synopsys_id, version.dev_id); 342 + if (!entry) { 343 + dev_err(priv->device, 344 + "Failed to find HW IF (id=0x%x, gmac=%d/%d)\n", 345 + version.snpsver, core_type == DWMAC_CORE_GMAC, 346 + core_type == DWMAC_CORE_GMAC4); 376 347 377 - /* Only use generic HW helpers if needed */ 378 - mac->desc = mac->desc ? : entry->desc; 379 - mac->dma = mac->dma ? : entry->dma; 380 - mac->mac = mac->mac ? : entry->mac; 381 - mac->ptp = mac->ptp ? : entry->hwtimestamp; 382 - mac->mode = mac->mode ? : entry->mode; 383 - mac->tc = mac->tc ? : entry->tc; 384 - mac->mmc = mac->mmc ? : entry->mmc; 385 - mac->est = mac->est ? : entry->est; 386 - mac->vlan = mac->vlan ? : entry->vlan; 387 - 388 - priv->hw = mac; 389 - priv->fpe_cfg.reg = entry->regs.fpe_reg; 390 - priv->ptpaddr = priv->ioaddr + entry->regs.ptp_off; 391 - priv->mmcaddr = priv->ioaddr + entry->regs.mmc_off; 392 - memcpy(&priv->ptp_clock_ops, entry->ptp, 393 - sizeof(struct ptp_clock_info)); 394 - if (entry->est) 395 - priv->estaddr = priv->ioaddr + entry->regs.est_off; 396 - 397 - /* Entry found */ 398 - if (needs_setup) { 399 - ret = entry->setup(priv); 400 - if (ret) 401 - return ret; 402 - } 403 - 404 - /* Save quirks, if needed for posterior use */ 405 - priv->hwif_quirks = entry->quirks; 406 - return 0; 348 + return -EINVAL; 407 349 } 408 350 409 - dev_err(priv->device, "Failed to find HW IF (id=0x%x, gmac=%d/%d)\n", 410 - id, core_type == DWMAC_CORE_GMAC, 411 - core_type == DWMAC_CORE_GMAC4); 412 - return -EINVAL; 351 + /* Only use generic HW helpers if needed */ 352 + mac->desc = mac->desc ? : entry->desc; 353 + mac->dma = mac->dma ? : entry->dma; 354 + mac->mac = mac->mac ? : entry->mac; 355 + mac->ptp = mac->ptp ? : entry->hwtimestamp; 356 + mac->mode = mac->mode ? : entry->mode; 357 + mac->tc = mac->tc ? : entry->tc; 358 + mac->mmc = mac->mmc ? : entry->mmc; 359 + mac->est = mac->est ? : entry->est; 360 + mac->vlan = mac->vlan ? : entry->vlan; 361 + 362 + priv->hw = mac; 363 + priv->fpe_cfg.reg = entry->regs.fpe_reg; 364 + priv->ptpaddr = priv->ioaddr + entry->regs.ptp_off; 365 + priv->mmcaddr = priv->ioaddr + entry->regs.mmc_off; 366 + memcpy(&priv->ptp_clock_ops, entry->ptp, 367 + sizeof(struct ptp_clock_info)); 368 + 369 + if (entry->est) 370 + priv->estaddr = priv->ioaddr + entry->regs.est_off; 371 + 372 + /* Entry found */ 373 + if (needs_setup) { 374 + ret = entry->setup(priv); 375 + if (ret) 376 + return ret; 377 + } 378 + 379 + /* Save quirks, if needed for posterior use */ 380 + priv->hwif_quirks = entry->quirks; 381 + 382 + return 0; 413 383 }