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

net: stmmac: unify mdio functions

stmmac_mdio_{read|write} and stmmac_mdio_{read|write}_gmac4 are not
enought different for being split.
The only differences between thoses two functions are shift/mask for
addr/reg/clk_csr.

This patch introduce a per platform set of variable for setting thoses
shift/mask and unify mdio read and write functions.

Signed-off-by: Corentin Labbe <clabbe.montjoie@gmail.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

LABBE Corentin and committed by
David S. Miller
b91dce4c 01f1f615

+48 -98
+6
drivers/net/ethernet/stmicro/stmmac/common.h
··· 507 507 struct mii_regs { 508 508 unsigned int addr; /* MII Address */ 509 509 unsigned int data; /* MII Data */ 510 + unsigned int addr_shift; /* MII address shift */ 511 + unsigned int reg_shift; /* MII reg shift */ 512 + unsigned int addr_mask; /* MII address mask */ 513 + unsigned int reg_mask; /* MII reg mask */ 514 + unsigned int clk_csr_shift; 515 + unsigned int clk_csr_mask; 510 516 }; 511 517 512 518 /* Helpers to manage the descriptors for chain and ring modes */
+6
drivers/net/ethernet/stmicro/stmmac/dwmac1000_core.c
··· 534 534 mac->link.speed = GMAC_CONTROL_FES; 535 535 mac->mii.addr = GMAC_MII_ADDR; 536 536 mac->mii.data = GMAC_MII_DATA; 537 + mac->mii.addr_shift = 11; 538 + mac->mii.addr_mask = 0x0000F800; 539 + mac->mii.reg_shift = 6; 540 + mac->mii.reg_mask = 0x000007C0; 541 + mac->mii.clk_csr_shift = 2; 542 + mac->mii.clk_csr_mask = 0xF; 537 543 538 544 /* Get and dump the chip ID */ 539 545 *synopsys_id = stmmac_get_synopsys_id(hwid);
+7
drivers/net/ethernet/stmicro/stmmac/dwmac100_core.c
··· 192 192 mac->link.speed = 0; 193 193 mac->mii.addr = MAC_MII_ADDR; 194 194 mac->mii.data = MAC_MII_DATA; 195 + mac->mii.addr_shift = 11; 196 + mac->mii.addr_mask = 0x0000F800; 197 + mac->mii.reg_shift = 6; 198 + mac->mii.reg_mask = 0x000007C0; 199 + mac->mii.clk_csr_shift = 2; 200 + mac->mii.clk_csr_mask = 0xF; 201 + 195 202 /* Synopsys Id is not available on old chips */ 196 203 *synopsys_id = 0; 197 204
+6
drivers/net/ethernet/stmicro/stmmac/dwmac4_core.c
··· 430 430 mac->link.speed = GMAC_CONFIG_FES; 431 431 mac->mii.addr = GMAC_MDIO_ADDR; 432 432 mac->mii.data = GMAC_MDIO_DATA; 433 + mac->mii.addr_shift = 21; 434 + mac->mii.addr_mask = GENMASK(25, 21); 435 + mac->mii.reg_shift = 16; 436 + mac->mii.reg_mask = GENMASK(20, 16); 437 + mac->mii.clk_csr_shift = 8; 438 + mac->mii.clk_csr_mask = GENMASK(11, 8); 433 439 434 440 /* Get and dump the chip ID */ 435 441 *synopsys_id = stmmac_get_synopsys_id(hwid);
+23 -98
drivers/net/ethernet/stmicro/stmmac/stmmac_mdio.c
··· 42 42 #define MII_GMAC4_WRITE (1 << MII_GMAC4_GOC_SHIFT) 43 43 #define MII_GMAC4_READ (3 << MII_GMAC4_GOC_SHIFT) 44 44 45 - #define MII_PHY_ADDR_GMAC4_SHIFT 21 46 - #define MII_PHY_ADDR_GMAC4_MASK GENMASK(25, 21) 47 - #define MII_PHY_REG_GMAC4_SHIFT 16 48 - #define MII_PHY_REG_GMAC4_MASK GENMASK(20, 16) 49 - #define MII_CSR_CLK_GMAC4_SHIFT 8 50 - #define MII_CSR_CLK_GMAC4_MASK GENMASK(11, 8) 51 - 52 45 static int stmmac_mdio_busy_wait(void __iomem *ioaddr, unsigned int mii_addr) 53 46 { 54 47 unsigned long curr; ··· 61 68 /** 62 69 * stmmac_mdio_read 63 70 * @bus: points to the mii_bus structure 64 - * @phyaddr: MII addr reg bits 15-11 65 - * @phyreg: MII addr reg bits 10-6 71 + * @phyaddr: MII addr 72 + * @phyreg: MII reg 66 73 * Description: it reads data from the MII register from within the phy device. 67 74 * For the 7111 GMAC, we must set the bit 0 in the MII address register while 68 75 * accessing the PHY registers. ··· 76 83 unsigned int mii_data = priv->hw->mii.data; 77 84 78 85 int data; 79 - u16 value = (((phyaddr << 11) & (0x0000F800)) | 80 - ((phyreg << 6) & (0x000007C0))); 81 - value |= MII_BUSY | ((priv->clk_csr & 0xF) << 2); 86 + u32 value = MII_BUSY; 87 + 88 + value |= (phyaddr << priv->hw->mii.addr_shift) 89 + & priv->hw->mii.addr_mask; 90 + value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask; 91 + value |= (priv->clk_csr & priv->hw->mii.clk_csr_mask) 92 + << priv->hw->mii.clk_csr_shift; 93 + if (priv->plat->has_gmac4) 94 + value |= MII_GMAC4_READ; 82 95 83 96 if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address)) 84 97 return -EBUSY; ··· 103 104 /** 104 105 * stmmac_mdio_write 105 106 * @bus: points to the mii_bus structure 106 - * @phyaddr: MII addr reg bits 15-11 107 - * @phyreg: MII addr reg bits 10-6 107 + * @phyaddr: MII addr 108 + * @phyreg: MII reg 108 109 * @phydata: phy data 109 110 * Description: it writes the data into the MII register from within the device. 110 111 */ ··· 116 117 unsigned int mii_address = priv->hw->mii.addr; 117 118 unsigned int mii_data = priv->hw->mii.data; 118 119 119 - u16 value = 120 - (((phyaddr << 11) & (0x0000F800)) | ((phyreg << 6) & (0x000007C0))) 121 - | MII_WRITE; 120 + u32 value = MII_WRITE | MII_BUSY; 122 121 123 - value |= MII_BUSY | ((priv->clk_csr & 0xF) << 2); 122 + value |= (phyaddr << priv->hw->mii.addr_shift) 123 + & priv->hw->mii.addr_mask; 124 + value |= (phyreg << priv->hw->mii.reg_shift) & priv->hw->mii.reg_mask; 124 125 125 - /* Wait until any existing MII operation is complete */ 126 - if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address)) 127 - return -EBUSY; 128 - 129 - /* Set the MII address register to write */ 130 - writel(phydata, priv->ioaddr + mii_data); 131 - writel(value, priv->ioaddr + mii_address); 132 - 133 - /* Wait until any existing MII operation is complete */ 134 - return stmmac_mdio_busy_wait(priv->ioaddr, mii_address); 135 - } 136 - 137 - /** 138 - * stmmac_mdio_read_gmac4 139 - * @bus: points to the mii_bus structure 140 - * @phyaddr: MII addr reg bits 25-21 141 - * @phyreg: MII addr reg bits 20-16 142 - * Description: it reads data from the MII register of GMAC4 from within 143 - * the phy device. 144 - */ 145 - static int stmmac_mdio_read_gmac4(struct mii_bus *bus, int phyaddr, int phyreg) 146 - { 147 - struct net_device *ndev = bus->priv; 148 - struct stmmac_priv *priv = netdev_priv(ndev); 149 - unsigned int mii_address = priv->hw->mii.addr; 150 - unsigned int mii_data = priv->hw->mii.data; 151 - int data; 152 - u32 value = (((phyaddr << MII_PHY_ADDR_GMAC4_SHIFT) & 153 - (MII_PHY_ADDR_GMAC4_MASK)) | 154 - ((phyreg << MII_PHY_REG_GMAC4_SHIFT) & 155 - (MII_PHY_REG_GMAC4_MASK))) | MII_GMAC4_READ; 156 - 157 - value |= MII_BUSY | ((priv->clk_csr & MII_CSR_CLK_GMAC4_MASK) 158 - << MII_CSR_CLK_GMAC4_SHIFT); 159 - 160 - if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address)) 161 - return -EBUSY; 162 - 163 - writel(value, priv->ioaddr + mii_address); 164 - 165 - if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address)) 166 - return -EBUSY; 167 - 168 - /* Read the data from the MII data register */ 169 - data = (int)readl(priv->ioaddr + mii_data); 170 - 171 - return data; 172 - } 173 - 174 - /** 175 - * stmmac_mdio_write_gmac4 176 - * @bus: points to the mii_bus structure 177 - * @phyaddr: MII addr reg bits 25-21 178 - * @phyreg: MII addr reg bits 20-16 179 - * @phydata: phy data 180 - * Description: it writes the data into the MII register of GMAC4 from within 181 - * the device. 182 - */ 183 - static int stmmac_mdio_write_gmac4(struct mii_bus *bus, int phyaddr, int phyreg, 184 - u16 phydata) 185 - { 186 - struct net_device *ndev = bus->priv; 187 - struct stmmac_priv *priv = netdev_priv(ndev); 188 - unsigned int mii_address = priv->hw->mii.addr; 189 - unsigned int mii_data = priv->hw->mii.data; 190 - 191 - u32 value = (((phyaddr << MII_PHY_ADDR_GMAC4_SHIFT) & 192 - (MII_PHY_ADDR_GMAC4_MASK)) | 193 - ((phyreg << MII_PHY_REG_GMAC4_SHIFT) & 194 - (MII_PHY_REG_GMAC4_MASK))) | MII_GMAC4_WRITE; 195 - 196 - value |= MII_BUSY | ((priv->clk_csr & MII_CSR_CLK_GMAC4_MASK) 197 - << MII_CSR_CLK_GMAC4_SHIFT); 126 + value |= ((priv->clk_csr & priv->hw->mii.clk_csr_mask) 127 + << priv->hw->mii.clk_csr_shift); 128 + if (priv->plat->has_gmac4) 129 + value |= MII_GMAC4_WRITE; 198 130 199 131 /* Wait until any existing MII operation is complete */ 200 132 if (stmmac_mdio_busy_wait(priv->ioaddr, mii_address)) ··· 235 305 #endif 236 306 237 307 new_bus->name = "stmmac"; 238 - if (priv->plat->has_gmac4) { 239 - new_bus->read = &stmmac_mdio_read_gmac4; 240 - new_bus->write = &stmmac_mdio_write_gmac4; 241 - } else { 242 - new_bus->read = &stmmac_mdio_read; 243 - new_bus->write = &stmmac_mdio_write; 244 - } 308 + new_bus->read = &stmmac_mdio_read; 309 + new_bus->write = &stmmac_mdio_write; 245 310 246 311 new_bus->reset = &stmmac_mdio_reset; 247 312 snprintf(new_bus->id, MII_BUS_ID_SIZE, "%s-%x",