target/linux/realtek/files-5.10/drivers/net/dsa/rtl83xx/rtl839x.c
int rtl839x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
{
	int err = 0;
	u32 v;

	// Take bug on RTL839x Rev <= C into account
	if (port >= RTL839X_CPU_PORT)
		return -EIO;

	mutex_lock(&smi_lock);

	// Set PHY to access
	rtl839x_set_port_reg_le(BIT_ULL(port), RTL839X_PHYREG_PORT_CTRL);

	// Set data to write
	sw_w32_mask(0xffff << 16, val << 16, RTL839X_PHYREG_DATA_CTRL);

	// Set MMD device number and register to write to
	sw_w32(devnum << 16 | (regnum & 0xffff), RTL839X_PHYREG_MMD_CTRL);

	v = BIT(3) | BIT(2) | BIT(0); // WRITE | MMD-access | EXEC
	sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);

	do {
		v = sw_r32(RTL839X_PHYREG_ACCESS_CTRL);
	} while (v & BIT(0));

	pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, val, err);
	mutex_unlock(&smi_lock);
	return err;
}
/*
 * Write to an mmd register of the PHY
 */
Write to an mmd register of the PHY
Write to an mmd register of the PHY
int rtl839x_write_mmd_phy(u32 port, u32 devnum, u32 regnum, u32 val)
{
	int err = 0;
	u32 v;
	if (port >= RTL839X_CPU_PORT)
		return -EIO;
	mutex_lock(&smi_lock);
	rtl839x_set_port_reg_le(BIT_ULL(port), RTL839X_PHYREG_PORT_CTRL);
	sw_w32_mask(0xffff << 16, val << 16, RTL839X_PHYREG_DATA_CTRL);
	sw_w32(devnum << 16 | (regnum & 0xffff), RTL839X_PHYREG_MMD_CTRL);
	v = BIT(3) | BIT(2) | BIT(0); 
	sw_w32(v, RTL839X_PHYREG_ACCESS_CTRL);
	do {
		v = sw_r32(RTL839X_PHYREG_ACCESS_CTRL);
	} while (v & BIT(0));
	pr_debug("%s: port %d, regnum: %x, val: %x (err %d)\n", __func__, port, regnum, val, err);
	mutex_unlock(&smi_lock);
	return err;
}
int rtl839x_write_mmd_phy ( u32 port , u32 devnum , u32 regnum , u32 val ) { int err = 0 ; u32 v ; if ( port >= RTL839X_CPU_PORT ) return - EIO ; mutex_lock ( & smi_lock ) ; rtl839x_set_port_reg_le ( BIT_ULL ( port ) , RTL839X_PHYREG_PORT_CTRL ) ; sw_w32_mask ( 0xffff << 16 , val << 16 , RTL839X_PHYREG_DATA_CTRL ) ; sw_w32 ( devnum << 16 | ( regnum & 0xffff ) , RTL839X_PHYREG_MMD_CTRL ) ; v = BIT ( 3 ) | BIT ( 2 ) | BIT ( 0 ) ; sw_w32 ( v , RTL839X_PHYREG_ACCESS_CTRL ) ; do { v = sw_r32 ( RTL839X_PHYREG_ACCESS_CTRL ) ; } while ( v & BIT ( 0 ) ) ; pr_debug ( " \n " , __func__ , port , regnum , val , err ) ; mutex_unlock ( & smi_lock ) ; return err ; }
Write to an mmd register of the PHY
Write to an mmd register of the PHY
// Take bug on RTL839x Rev <= C into account // Set PHY to access // Set data to write // Set MMD device number and register to write to // WRITE | MMD-access | EXEC