Skip to content

phy-realtek: add rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b - #20450

Closed
balika011 wants to merge 1 commit into
openwrt:mainfrom
balika011:common_phy_realtek
Closed

balika011 wants to merge 1 commit into
openwrt:mainfrom
balika011:common_phy_realtek

Conversation

@balika011

Copy link
Copy Markdown
Contributor

This change adds/moves support for rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b phys into common realtek phy driver.

Also removes phy-rtl8261n as it's no longer needed.

note: this pr supersedes #20392

@abajk

abajk commented Oct 18, 2025

Copy link
Copy Markdown
Contributor

Do you have plans to send patches upstream?

@balika011

Copy link
Copy Markdown
Contributor Author

Do you have plans to send patches upstream?

I wasn't thinking about that, at least not yet.

@balika011
balika011 force-pushed the common_phy_realtek branch 2 times, most recently from 5d62647 to 0ae4f4f Compare October 18, 2025 17:55
@balika011
balika011 requested a review from abajk October 18, 2025 17:59
@github-actions github-actions Bot added kernel pull request/issue with Linux kernel related changes core packages pull request/issue for core (in-tree) packages target/mediatek pull request/issue for mediatek target target/realtek pull request/issue for realtek target target/airoha pull request/issue for airoha target target/qualcommbe pull request/issue for qualcommbe target labels Oct 18, 2025
hurrian added a commit to hurrian/openwrt-w1700k that referenced this pull request Oct 18, 2025
Adds support for the `RTL8261N` NBASE-T PHY to the Gemtek W1700K.

Depends: openwrt#20450 (phy-realtek: add rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b)
Signed-off-by: Kenneth Kasilag <[email protected]>
hurrian pushed a commit to hurrian/openwrt-w1700k that referenced this pull request Oct 18, 2025
Based on disassembly of the OEM driver, and from logic analyzer measurement, the
following needs to be configued for the RTL8261N PHY of Gemtek W1700K:
- UXSGMII HSO/HSI polarity swap
- Device specific quirk (realtek,patch-gemtek-top)

Depends: openwrt#20450 (phy-realtek: add rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b)
Signed-off-by: Richard Huynh <[email protected]>
Signed-off-by: Kenneth Kasilag <[email protected]>
hurrian added a commit to hurrian/openwrt-w1700k that referenced this pull request Oct 19, 2025
Adds support for the `RTL8261N` NBASE-T PHY to the Gemtek W1700K.

Depends: openwrt#20450 (phy-realtek: add rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b)
Signed-off-by: Kenneth Kasilag <[email protected]>
hurrian pushed a commit to hurrian/openwrt-w1700k that referenced this pull request Oct 19, 2025
Based on disassembly of the OEM driver, and from logic analyzer measurement, the
following needs to be configued for the RTL8261N PHY of Gemtek W1700K:
- UXSGMII HSO/HSI polarity swap
- Device specific quirk (realtek,patch-gemtek-top)

Depends: openwrt#20450 (phy-realtek: add rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b)
Signed-off-by: Richard Huynh <[email protected]>
Signed-off-by: Kenneth Kasilag <[email protected]>
@hurrian

hurrian commented Oct 19, 2025

Copy link
Copy Markdown
Contributor

Aside from the issue with the 737 patch on 6.12, tested this to work on Gemtek W1700K #17869

Only issue I'm having now is that ethtool seemingly isn't exporting info about the PHY - likewise, /sys/class/net/{eth1,eth2}/speed comes up empty.

@balika011

Copy link
Copy Markdown
Contributor Author

Only issue I'm having now is that ethtool seemingly isn't exporting info about the PHY - likewise, /sys/class/net/{eth1,eth2}/speed comes up empty.

Huh. It works fine for me on ST5008

hurrian added a commit to hurrian/openwrt-w1700k that referenced this pull request Oct 19, 2025
Adds support for the `RTL8261N` NBASE-T PHY to the Gemtek W1700K.

Depends: openwrt#20450 (phy-realtek: add rtl8251l, rtl8254b, rtl8261be, rtl8261n, rtl8264 and rtl8264b)
Signed-off-by: Kenneth Kasilag <[email protected]>

@OpenWRT-fanboy OpenWRT-fanboy left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works great on W1700k without the 737 hack.

@kirdesde

Copy link
Copy Markdown

Tested on: TP-Link BE450 (Mediatek MT7988d, RTL8261N, Device Support PR pending #20398)

@dangowrt

Copy link
Copy Markdown
Member

I took the freedom to take care of the rebasing

@dangowrt

Copy link
Copy Markdown
Member

Imho this looks ready to be sent upstream, so next step would be to squash all files in target/linux/generic/files/ into the respective patches and complete the patch descriptions. Then it's ready to go to netdev list. And as the firmware files are generated from source files under GPL it should be no problem to include them in linux-firmware even if submitted from an email address which isn't @realtek.com.tw.

@balika011

Copy link
Copy Markdown
Contributor Author

I took the freedom to take care of the rebasing

i also pushed the new firmwares: https://github.com/balika011/realtek_phy_firmware/releases

@Djfe

Djfe commented Jan 24, 2026

Copy link
Copy Markdown
Contributor

Sorry, I don't want to delay this further since we finally are on the finish line, but according to @hauke we should clear up the licensing situation before it can be sent upstream, no?
#20450 (comment)

And as the firmware files are generated from source files under GPL it should be no problem to include them in linux-firmware

@dangowrt @balika011 which GPL version were the original source files for each of these? copyright/licensing should not be swept under the carpet when upstreaming

@dangowrt

Copy link
Copy Markdown
Member

The source files released in various GPL drops under GPLv2 contain huge numeral arrays. In the repo you can see how the binaries are generated from the GPLv2-only licenced source files. I suppose GPLv3 applies to the script generating the binaries from the source files, as the resulting binaries have the identical content of the GPLv2 sources, just in different encoding (binary instead of an array of numbers in a C source file)
See https://github.com/balika011/realtek_phy_firmware

@Djfe

Djfe commented Jan 24, 2026 •

Copy link
Copy Markdown
Contributor

great, now that that is answered who upstreams this to netdev after this step:

so next step would be to squash all files in target/linux/generic/files/ into the respective patches and complete the patch descriptions.

@balika011

Copy link
Copy Markdown
Contributor Author

Adding the RTL8224 without having one I can't grantee that it will work. So I rather not add that unless someone donates one.

@abajk

abajk commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

Adding the RTL8224 without having one I can't grantee that it will work. So I rather not add that unless someone donates one.

RTL8224 is supported upstream. I wouldn't link it to this PR.

@balika011

Copy link
Copy Markdown
Contributor Author

Adding the RTL8224 without having one I can't grantee that it will work. So I rather not add that unless someone donates one.

RTL8224 is supported upstream. I wouldn't link it to this PR.

Does the upstream supports the patching too?

@abajk

abajk commented Jan 25, 2026

Copy link
Copy Markdown
Contributor

Does the upstream supports the patching too?

The upstream driver doesn't support patching. I think it's even better that it works correctly without patches. Some patches simply set registers, the functions of which we don't yet know. Unless the patched firmware for RTL8224 has some new features, I don't think it's worth adding.

int ret = 0, us_diff;
struct timespec64 start, now;

ktime_get_real_ts64(&start);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you need the real time? This looks like it would break when somebody changes the system clock in the middle.

Maybe you wanted to use a monotonic time like ktime_get_ts64. Or just use ktime_get in both situations (start + current time) and then directly calculate your usecs via something like uint32_t us_diff = ktime_to_us(ktime_sub(now, start));

@ecsv ecsv left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many of these things can directly be found when running checkpatch (from lets say Linux 7.0) against the newly introduced directory:

./scripts/checkpatch.pl --strict -f /home/..../target/linux/generic/files/drivers/net/phy/realtek/*

Other things were just noticed while scrolling through the changes

@@ -0,0 +1,25 @@
#include "realtek.h"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include "realtek.h"
// SPDX-License-Identifier: GPL-2.0
#include "phy_patch.h"
#include <linux/errno.h>
#include <linux/phy.h>
#include "realtek.h"

{
struct rtl826x_priv *priv = phydev->priv;
struct rt_phy_patch_db *patch_db = priv->patch;
int ret = 0, i = 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int ret = 0, i = 0;
int ret = 0
size_t i;

struct rt_phy_patch_db *patch_db = priv->patch;
int ret = 0, i = 0;

if (patch_db == NULL || patch_db->patch_op == NULL) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (patch_db == NULL || patch_db->patch_op == NULL) {
if (!patch_db || !patch_db->patch_op) {

@@ -0,0 +1,40 @@
#ifndef __PHY_PATCH_H__

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#ifndef __PHY_PATCH_H__
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __PHY_PATCH_H__

} __packed;

struct rt_phy_patch_db {
int (*patch_op)(struct phy_device *phydev, struct rtk_hwpatch *pPatch_data);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
int (*patch_op)(struct phy_device *phydev, struct rtk_hwpatch *pPatch_data);
int (*patch_op)(struct phy_device *phydev, const struct rtk_hwpatch *patch);

I don't like that there are two things called patch_op. One is the something which stores a value from rtk_phypatch_op and the other one is a pointer to executing the op. Maybe we can call this one patch_op_exec or something like this?


data = (data >> 8) & 0x1f;
for (cnt = 0; cnt <= data; cnt++) {
ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2, 0xbc62, GENMASK(12, 8), cnt << 8);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2, 0xbc62, GENMASK(12, 8), cnt << 8);
ret = phy_modify_mmd(phydev, MDIO_MMD_VEND2, 0xbc62,
GENMASK(12, 8), cnt << 8);

case RTK_PATCH_OP_TOP:
reg = (patch->pagemmd * 8) + (patch->addr - 16);
if (mask != GENMASK(15, 0)) {
ret = phy_modify_mmd(phydev, MDIO_MMD_VEND1, reg, mask, patch->data << patch->lsb);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret = phy_modify_mmd(phydev, MDIO_MMD_VEND1, reg, mask, patch->data << patch->lsb);
ret = phy_modify_mmd(phydev, MDIO_MMD_VEND1, reg, mask,
patch->data << patch->lsb);

break;

case RTK_PATCH_OP_PSDS0:
ret = _phy_patch_rtl826x_sds_modify(phydev, patch->pagemmd, patch->addr, mask, patch->data << patch->lsb);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ret = _phy_patch_rtl826x_sds_modify(phydev, patch->pagemmd, patch->addr, mask, patch->data << patch->lsb);
ret = _phy_patch_rtl826x_sds_modify(phydev, patch->pagemmd, patch->addr, mask,
patch->data << patch->lsb);

Comment on lines +32 to +36
struct rt_phy_patch_db {
int (*patch_op)(struct phy_device *phydev, struct rtk_hwpatch *pPatch_data);
struct rtk_hwpatch *conf;
u32 size;
};

@ecsv ecsv Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, this can be written also as a flex array:

Suggested change
struct rt_phy_patch_db {
int (*patch_op)(struct phy_device *phydev, struct rtk_hwpatch *pPatch_data);
struct rtk_hwpatch *conf;
u32 size;
};
struct rt_phy_patch_db {
int (*patch_op)(struct phy_device *phydev, struct rtk_hwpatch *pPatch_data);
size_t count;
struct rtk_hwpatch conf[] __counted_by(count);
};

Of course, then the size would be the number of entries in conf and no longer the raw size of conf. The loops must therefore be adjusted to not divide but directly use count as upper bound check.

The allocation of struct rt_phy_patch_db would look then like:

size_t count;

count = fw->size / sizeof(struct rtk_hwpatch);
patch_db = devm_kzalloc(struct_size(patch_db, conf, count), GFP_KERNEL);
if (!patch_db) {
	...
}

patch_db->count = count;

or with Linux 7.0:

size_t count;

count = fw->size / sizeof(struct rtk_hwpatch);
patch_db = kzalloc_flex(*patch_db, conf, count, GFP_ATOMIC);
if (!patch_db) {
	...
}

patch_db->count = count;

This is preferred in the kernel because you can then utilize the already existing infrastructure better. Things like CONFIG_FORTIFY_SOURCE or CONFIG_UBSAN_BOUNDS

devm_kzalloc_flex would even be better because you don't free the allocated memory in your code. devm_*alloc* can help you with this because it is then freed automatically when the device is freed

Comment on lines +253 to +261
for (i = 0; i < fw->size / sizeof(struct rtk_hwpatch); i++) {
patch_db->conf[i].patch_op = patches[i].patch_op;
patch_db->conf[i].portmask = patches[i].portmask;
patch_db->conf[i].pagemmd = le16_to_cpu(patches[i].pagemmd);
patch_db->conf[i].addr = le16_to_cpu(patches[i].addr);
patch_db->conf[i].msb = patches[i].msb;
patch_db->conf[i].lsb = patches[i].lsb;
patch_db->conf[i].data = le16_to_cpu(patches[i].data);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is slightly ugly because the le16_to_cpu converts a __le16 to a u16. For sparse/cgcc, this is in different address spaces and you will therefore get errors because of this.

For now, it works. But when you want to upstream it, you need to check your code by building it with make C=1 - and then you have to fix it.

One way to handle it is to have a file representation structure and a in-memory representation structure (which is used in the patch_db)

struct rtk_hwpatch {
	u8 patch_op;
	u8 portmask;
	__le16 pagemmd;
	__le16 addr;
	u8 msb;
	u8 lsb;
	__le16 data;
} __packed;

struct rtk_patch_op {
	u8 patch_op;
	u8 portmask;
	u16 pagemmd;
	u16 addr;
	u8 msb;
	u8 lsb;
	u16 data;
};

Or you always store the in-file representation (__le16) and then on-demand convert the __le16 to u16.

@ecsv

ecsv commented Feb 24, 2026 •

Copy link
Copy Markdown
Contributor

Just to document it: this can also be used in u-boot to get RTL8261BE working on something like MT7988. I've just did a really rough PoC which uses a slightly different format for the patch file. uboot-mediatek-rtl8261-1.patch

SIZE="$(stat -c%s rtl8261n.bin)"
python3 -c "import struct,sys; sys.stdout.buffer.write(struct.pack('<I', $SIZE))" > rtl8261n-fw.bin
cat rtl8261n.bin >> rtl8261n-fw.bin

Which can then be loaded from a MTD partition (in my case 0x10000 bytes large) when it is registered in the devicetree:

&eth2 {
	status = "okay";
	pinctrl-names = "default";
	pinctrl-0 = <&mdio0_pins>;
	phy-mode = "usxgmii";
	phy-handle = <&phy8>;

	nvmem-cell-names = "mac-address";
	nvmem-cells = <&gmac2_mac>;

	phy8: ethernet-phy@8 {
		compatible = "ethernet-phy-id001c.caf3";
		reg = <8>;

		reset-gpios = <&pio 3 GPIO_ACTIVE_LOW>;
		reset-delay-us = <100000>;
		reset-deassert-us = <100000>;

		realtek,pnswap-rx;
		realtek,pnswap-tx;
	};
};

&pio {
	mdio0_pins: mdio0-pins {
		mux {
			function = "eth";
			groups = "mdc_mdio0";
		};

		conf {
			groups = "mdc_mdio0";
			drive-strength = <MTK_DRIVE_8mA>;
		};
	};
};

The loader in board/mediatek/mt7988/mt7988_rfb.c could then for example be:

#define MT7988_RTL8261N_FW_SIZE                0x10000

int mt7988_rtl8261n_get_fw(void **fw, size_t *size)
{
	struct mtd_info *mtd;
	uint32_t real_size;
	size_t retlen;
	uint8_t *data;
	int ret;

	data = malloc(MT7988_RTL8261N_FW_SIZE);
	if (!data)
		return -ENOMEM;

	mtd = get_mtd_device_nm("rtl8261-fw");
	if (!mtd) {
		printf("[RTL8261N] unable to find firmware partition.\n");
		free(data);
		return -ENODEV;
	}

	ret = mtd_read(mtd, 0, MT7988_RTL8261N_FW_SIZE, &retlen, data);
	if (ret || retlen != MT7988_RTL8261N_FW_SIZE) {
		printf("[RTL8261N] read firmware from MTD failed.\n");
		free(data);
		put_mtd_device(mtd);
		return -EIO;
	}

	real_size = 0;
	real_size |= data[0] << 0;
	real_size |= data[1] << 8;
	real_size |= data[2] << 16;
	real_size |= data[3] << 24;

	if (real_size  > MT7988_RTL8261N_FW_SIZE - 4) {
		printf("[RTL8261N] found size larger than read size\n");
		free(data);
		put_mtd_device(mtd);
		return -EINVAL;
	}

	*size = real_size;

	put_mtd_device(mtd);

	*fw = data;

	return 1;
}

Of course, CONFIG_PHYLIB_10G=y and CONFIG_PHY_REALTEK=y must be enabled in the board configuration.

cmonroe referenced this pull request in cmonroe/target-cascade Mar 17, 2026
The RTL8261C/D-CG is a Realtek 10Gbps copper PHY supporting 100M/1G/2.5G/5G/10G
speeds via USXGMII interface. Requires firmware patching at init via patch
mode entry/exit sequence. Work around MMD7 AN STAT1 not reporting
autonegotiation capability correctly by forcing the Autoneg bit in
get_features.
@andrewjlamarche

Copy link
Copy Markdown
Contributor

@balika011 do you plan on addressing the comments here? I think this change would still be received well by all the new gear using 10G RTL PHYs.

@xvolte

xvolte commented Mar 20, 2026

Copy link
Copy Markdown

hello.
is there anything missing for this to be merged to main. ?
thanks in advance,

@ecsv

ecsv commented Mar 21, 2026

Copy link
Copy Markdown
Contributor

is there anything missing for this to be merged to main. ?

@xvolte there is:

@OpenWRT-fanboy

Copy link
Copy Markdown

It would be a real shame if so much effort goes to waste. If this were to be committed, we should expect continued improvement and refinement. Just look at the near daily committing of realtek driver fixes.

If (as seems likely) @balika011 has abandoned this PR, maybe someone else can slightly clean this up, submit a new PR and get it done. @ecsv?

Sometimes the committers let perfect be the enemy of the good. Certainly seems so here.

@lukasz1992

Copy link
Copy Markdown

@OpenWRT-fanboy
We can just try to upgrade construct/* files in OpenWRT very easily. Maybe that helps with the issues of current driver?

https://git.no.mesh.fail/mirror/realtek-doc/src/branch/main/sources/rtk-gp3000/src/hal/phy/construct have updated values

@ecsv

ecsv commented Mar 22, 2026 •

Copy link
Copy Markdown
Contributor

If (as seems likely) @balika011 has abandoned this PR, maybe someone else can slightly clean this up, submit a new PR and get it done. @ecsv?

I find this rather sub-optimal. I've prepared all these changes already in github but now need to redo them because I can't apply my own suggestions from this PR.

But yes, I think this PR is unmanageable and the original author is unavailable. This thing has to be splitted up in multiple commits (firmware, patching, features, ...) and PRs (adding of new things, removal of the old driver)

I don't have the HW with me at the moment, but I can start thinking about the "mechanical" cleanups.

I will definitely not do anything which broaden the scope of this PR. And will also not assist with debugging or adding of features.

EDIT: See #22563

@ecsv

ecsv commented May 18, 2026

Copy link
Copy Markdown
Contributor

@dangowrt also close this one (see #23427)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core packages pull request/issue for core (in-tree) packages kernel pull request/issue with Linux kernel related changes target/airoha pull request/issue for airoha target target/mediatek pull request/issue for mediatek target target/qualcommbe pull request/issue for qualcommbe target target/realtek pull request/issue for realtek target target/siflower pull request/issue for siflower target

Projects

None yet

Development

Successfully merging this pull request may close these issues.