162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci#include <linux/bitfield.h>
362306a36Sopenharmony_ci#include <linux/module.h>
462306a36Sopenharmony_ci#include <linux/phy.h>
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#define MTK_EXT_PAGE_ACCESS		0x1f
762306a36Sopenharmony_ci#define MTK_PHY_PAGE_STANDARD		0x0000
862306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED		0x0001
962306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_2		0x0002
1062306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_3		0x0003
1162306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_2A30	0x2a30
1262306a36Sopenharmony_ci#define MTK_PHY_PAGE_EXTENDED_52B5	0x52b5
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_cistatic int mtk_gephy_read_page(struct phy_device *phydev)
1562306a36Sopenharmony_ci{
1662306a36Sopenharmony_ci	return __phy_read(phydev, MTK_EXT_PAGE_ACCESS);
1762306a36Sopenharmony_ci}
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_cistatic int mtk_gephy_write_page(struct phy_device *phydev, int page)
2062306a36Sopenharmony_ci{
2162306a36Sopenharmony_ci	return __phy_write(phydev, MTK_EXT_PAGE_ACCESS, page);
2262306a36Sopenharmony_ci}
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_cistatic void mtk_gephy_config_init(struct phy_device *phydev)
2562306a36Sopenharmony_ci{
2662306a36Sopenharmony_ci	/* Disable EEE */
2762306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_AN, MDIO_AN_EEE_ADV, 0);
2862306a36Sopenharmony_ci
2962306a36Sopenharmony_ci	/* Enable HW auto downshift */
3062306a36Sopenharmony_ci	phy_modify_paged(phydev, MTK_PHY_PAGE_EXTENDED, 0x14, 0, BIT(4));
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci	/* Increase SlvDPSready time */
3362306a36Sopenharmony_ci	phy_select_page(phydev, MTK_PHY_PAGE_EXTENDED_52B5);
3462306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0xafae);
3562306a36Sopenharmony_ci	__phy_write(phydev, 0x12, 0x2f);
3662306a36Sopenharmony_ci	__phy_write(phydev, 0x10, 0x8fae);
3762306a36Sopenharmony_ci	phy_restore_page(phydev, MTK_PHY_PAGE_STANDARD, 0);
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci	/* Adjust 100_mse_threshold */
4062306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, 0x123, 0xffff);
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	/* Disable mcc */
4362306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, 0xa6, 0x300);
4462306a36Sopenharmony_ci}
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic int mt7530_phy_config_init(struct phy_device *phydev)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	mtk_gephy_config_init(phydev);
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	/* Increase post_update_timer */
5162306a36Sopenharmony_ci	phy_write_paged(phydev, MTK_PHY_PAGE_EXTENDED_3, 0x11, 0x4b);
5262306a36Sopenharmony_ci
5362306a36Sopenharmony_ci	return 0;
5462306a36Sopenharmony_ci}
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_cistatic int mt7531_phy_config_init(struct phy_device *phydev)
5762306a36Sopenharmony_ci{
5862306a36Sopenharmony_ci	mtk_gephy_config_init(phydev);
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci	/* PHY link down power saving enable */
6162306a36Sopenharmony_ci	phy_set_bits(phydev, 0x17, BIT(4));
6262306a36Sopenharmony_ci	phy_clear_bits_mmd(phydev, MDIO_MMD_VEND1, 0xc6, 0x300);
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci	/* Set TX Pair delay selection */
6562306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, 0x13, 0x404);
6662306a36Sopenharmony_ci	phy_write_mmd(phydev, MDIO_MMD_VEND1, 0x14, 0x404);
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci	return 0;
6962306a36Sopenharmony_ci}
7062306a36Sopenharmony_ci
7162306a36Sopenharmony_cistatic struct phy_driver mtk_gephy_driver[] = {
7262306a36Sopenharmony_ci	{
7362306a36Sopenharmony_ci		PHY_ID_MATCH_EXACT(0x03a29412),
7462306a36Sopenharmony_ci		.name		= "MediaTek MT7530 PHY",
7562306a36Sopenharmony_ci		.config_init	= mt7530_phy_config_init,
7662306a36Sopenharmony_ci		/* Interrupts are handled by the switch, not the PHY
7762306a36Sopenharmony_ci		 * itself.
7862306a36Sopenharmony_ci		 */
7962306a36Sopenharmony_ci		.config_intr	= genphy_no_config_intr,
8062306a36Sopenharmony_ci		.handle_interrupt = genphy_handle_interrupt_no_ack,
8162306a36Sopenharmony_ci		.suspend	= genphy_suspend,
8262306a36Sopenharmony_ci		.resume		= genphy_resume,
8362306a36Sopenharmony_ci		.read_page	= mtk_gephy_read_page,
8462306a36Sopenharmony_ci		.write_page	= mtk_gephy_write_page,
8562306a36Sopenharmony_ci	},
8662306a36Sopenharmony_ci	{
8762306a36Sopenharmony_ci		PHY_ID_MATCH_EXACT(0x03a29441),
8862306a36Sopenharmony_ci		.name		= "MediaTek MT7531 PHY",
8962306a36Sopenharmony_ci		.config_init	= mt7531_phy_config_init,
9062306a36Sopenharmony_ci		/* Interrupts are handled by the switch, not the PHY
9162306a36Sopenharmony_ci		 * itself.
9262306a36Sopenharmony_ci		 */
9362306a36Sopenharmony_ci		.config_intr	= genphy_no_config_intr,
9462306a36Sopenharmony_ci		.handle_interrupt = genphy_handle_interrupt_no_ack,
9562306a36Sopenharmony_ci		.suspend	= genphy_suspend,
9662306a36Sopenharmony_ci		.resume		= genphy_resume,
9762306a36Sopenharmony_ci		.read_page	= mtk_gephy_read_page,
9862306a36Sopenharmony_ci		.write_page	= mtk_gephy_write_page,
9962306a36Sopenharmony_ci	},
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cimodule_phy_driver(mtk_gephy_driver);
10362306a36Sopenharmony_ci
10462306a36Sopenharmony_cistatic struct mdio_device_id __maybe_unused mtk_gephy_tbl[] = {
10562306a36Sopenharmony_ci	{ PHY_ID_MATCH_EXACT(0x03a29441) },
10662306a36Sopenharmony_ci	{ PHY_ID_MATCH_EXACT(0x03a29412) },
10762306a36Sopenharmony_ci	{ }
10862306a36Sopenharmony_ci};
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ciMODULE_DESCRIPTION("MediaTek Gigabit Ethernet PHY driver");
11162306a36Sopenharmony_ciMODULE_AUTHOR("DENG, Qingfang <dqfext@gmail.com>");
11262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
11362306a36Sopenharmony_ci
11462306a36Sopenharmony_ciMODULE_DEVICE_TABLE(mdio, mtk_gephy_tbl);
115