18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci// Copyright (C) 2018 Microchip Technology
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/kernel.h>
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/delay.h>
78c2ecf20Sopenharmony_ci#include <linux/mii.h>
88c2ecf20Sopenharmony_ci#include <linux/phy.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/* External Register Control Register */
118c2ecf20Sopenharmony_ci#define LAN87XX_EXT_REG_CTL                     (0x14)
128c2ecf20Sopenharmony_ci#define LAN87XX_EXT_REG_CTL_RD_CTL              (0x1000)
138c2ecf20Sopenharmony_ci#define LAN87XX_EXT_REG_CTL_WR_CTL              (0x0800)
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci/* External Register Read Data Register */
168c2ecf20Sopenharmony_ci#define LAN87XX_EXT_REG_RD_DATA                 (0x15)
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci/* External Register Write Data Register */
198c2ecf20Sopenharmony_ci#define LAN87XX_EXT_REG_WR_DATA                 (0x16)
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci/* Interrupt Source Register */
228c2ecf20Sopenharmony_ci#define LAN87XX_INTERRUPT_SOURCE                (0x18)
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci/* Interrupt Mask Register */
258c2ecf20Sopenharmony_ci#define LAN87XX_INTERRUPT_MASK                  (0x19)
268c2ecf20Sopenharmony_ci#define LAN87XX_MASK_LINK_UP                    (0x0004)
278c2ecf20Sopenharmony_ci#define LAN87XX_MASK_LINK_DOWN                  (0x0002)
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* phyaccess nested types */
308c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_MODE_READ		0
318c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_MODE_WRITE		1
328c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_MODE_MODIFY		2
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_BANK_SMI		0
358c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_BANK_MISC		1
368c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_BANK_PCS		2
378c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_BANK_AFE		3
388c2ecf20Sopenharmony_ci#define	PHYACC_ATTR_BANK_MAX		7
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define DRIVER_AUTHOR	"Nisar Sayed <nisar.sayed@microchip.com>"
418c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Microchip LAN87XX T1 PHY driver"
428c2ecf20Sopenharmony_ci
438c2ecf20Sopenharmony_cistruct access_ereg_val {
448c2ecf20Sopenharmony_ci	u8  mode;
458c2ecf20Sopenharmony_ci	u8  bank;
468c2ecf20Sopenharmony_ci	u8  offset;
478c2ecf20Sopenharmony_ci	u16 val;
488c2ecf20Sopenharmony_ci	u16 mask;
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int access_ereg(struct phy_device *phydev, u8 mode, u8 bank,
528c2ecf20Sopenharmony_ci		       u8 offset, u16 val)
538c2ecf20Sopenharmony_ci{
548c2ecf20Sopenharmony_ci	u16 ereg = 0;
558c2ecf20Sopenharmony_ci	int rc = 0;
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	if (mode > PHYACC_ATTR_MODE_WRITE || bank > PHYACC_ATTR_BANK_MAX)
588c2ecf20Sopenharmony_ci		return -EINVAL;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	if (bank == PHYACC_ATTR_BANK_SMI) {
618c2ecf20Sopenharmony_ci		if (mode == PHYACC_ATTR_MODE_WRITE)
628c2ecf20Sopenharmony_ci			rc = phy_write(phydev, offset, val);
638c2ecf20Sopenharmony_ci		else
648c2ecf20Sopenharmony_ci			rc = phy_read(phydev, offset);
658c2ecf20Sopenharmony_ci		return rc;
668c2ecf20Sopenharmony_ci	}
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci	if (mode == PHYACC_ATTR_MODE_WRITE) {
698c2ecf20Sopenharmony_ci		ereg = LAN87XX_EXT_REG_CTL_WR_CTL;
708c2ecf20Sopenharmony_ci		rc = phy_write(phydev, LAN87XX_EXT_REG_WR_DATA, val);
718c2ecf20Sopenharmony_ci		if (rc < 0)
728c2ecf20Sopenharmony_ci			return rc;
738c2ecf20Sopenharmony_ci	} else {
748c2ecf20Sopenharmony_ci		ereg = LAN87XX_EXT_REG_CTL_RD_CTL;
758c2ecf20Sopenharmony_ci	}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	ereg |= (bank << 8) | offset;
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	rc = phy_write(phydev, LAN87XX_EXT_REG_CTL, ereg);
808c2ecf20Sopenharmony_ci	if (rc < 0)
818c2ecf20Sopenharmony_ci		return rc;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (mode == PHYACC_ATTR_MODE_READ)
848c2ecf20Sopenharmony_ci		rc = phy_read(phydev, LAN87XX_EXT_REG_RD_DATA);
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	return rc;
878c2ecf20Sopenharmony_ci}
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistatic int access_ereg_modify_changed(struct phy_device *phydev,
908c2ecf20Sopenharmony_ci				      u8 bank, u8 offset, u16 val, u16 mask)
918c2ecf20Sopenharmony_ci{
928c2ecf20Sopenharmony_ci	int new = 0, rc = 0;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	if (bank > PHYACC_ATTR_BANK_MAX)
958c2ecf20Sopenharmony_ci		return -EINVAL;
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci	rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ, bank, offset, val);
988c2ecf20Sopenharmony_ci	if (rc < 0)
998c2ecf20Sopenharmony_ci		return rc;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	new = val | (rc & (mask ^ 0xFFFF));
1028c2ecf20Sopenharmony_ci	rc = access_ereg(phydev, PHYACC_ATTR_MODE_WRITE, bank, offset, new);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	return rc;
1058c2ecf20Sopenharmony_ci}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cistatic int lan87xx_phy_init(struct phy_device *phydev)
1088c2ecf20Sopenharmony_ci{
1098c2ecf20Sopenharmony_ci	static const struct access_ereg_val init[] = {
1108c2ecf20Sopenharmony_ci		/* TX Amplitude = 5 */
1118c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_AFE, 0x0B,
1128c2ecf20Sopenharmony_ci		 0x000A, 0x001E},
1138c2ecf20Sopenharmony_ci		/* Clear SMI interrupts */
1148c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_SMI, 0x18,
1158c2ecf20Sopenharmony_ci		 0, 0},
1168c2ecf20Sopenharmony_ci		/* Clear MISC interrupts */
1178c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_READ, PHYACC_ATTR_BANK_MISC, 0x08,
1188c2ecf20Sopenharmony_ci		 0, 0},
1198c2ecf20Sopenharmony_ci		/* Turn on TC10 Ring Oscillator (ROSC) */
1208c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_MISC, 0x20,
1218c2ecf20Sopenharmony_ci		 0x0020, 0x0020},
1228c2ecf20Sopenharmony_ci		/* WUR Detect Length to 1.2uS, LPC Detect Length to 1.09uS */
1238c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_PCS, 0x20,
1248c2ecf20Sopenharmony_ci		 0x283C, 0},
1258c2ecf20Sopenharmony_ci		/* Wake_In Debounce Length to 39uS, Wake_Out Length to 79uS */
1268c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x21,
1278c2ecf20Sopenharmony_ci		 0x274F, 0},
1288c2ecf20Sopenharmony_ci		/* Enable Auto Wake Forward to Wake_Out, ROSC on, Sleep,
1298c2ecf20Sopenharmony_ci		 * and Wake_In to wake PHY
1308c2ecf20Sopenharmony_ci		 */
1318c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x20,
1328c2ecf20Sopenharmony_ci		 0x80A7, 0},
1338c2ecf20Sopenharmony_ci		/* Enable WUP Auto Fwd, Enable Wake on MDI, Wakeup Debouncer
1348c2ecf20Sopenharmony_ci		 * to 128 uS
1358c2ecf20Sopenharmony_ci		 */
1368c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_WRITE, PHYACC_ATTR_BANK_MISC, 0x24,
1378c2ecf20Sopenharmony_ci		 0xF110, 0},
1388c2ecf20Sopenharmony_ci		/* Enable HW Init */
1398c2ecf20Sopenharmony_ci		{PHYACC_ATTR_MODE_MODIFY, PHYACC_ATTR_BANK_SMI, 0x1A,
1408c2ecf20Sopenharmony_ci		 0x0100, 0x0100},
1418c2ecf20Sopenharmony_ci	};
1428c2ecf20Sopenharmony_ci	int rc, i;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	/* Start manual initialization procedures in Managed Mode */
1458c2ecf20Sopenharmony_ci	rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
1468c2ecf20Sopenharmony_ci					0x1a, 0x0000, 0x0100);
1478c2ecf20Sopenharmony_ci	if (rc < 0)
1488c2ecf20Sopenharmony_ci		return rc;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	/* Soft Reset the SMI block */
1518c2ecf20Sopenharmony_ci	rc = access_ereg_modify_changed(phydev, PHYACC_ATTR_BANK_SMI,
1528c2ecf20Sopenharmony_ci					0x00, 0x8000, 0x8000);
1538c2ecf20Sopenharmony_ci	if (rc < 0)
1548c2ecf20Sopenharmony_ci		return rc;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/* Check to see if the self-clearing bit is cleared */
1578c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
1588c2ecf20Sopenharmony_ci	rc = access_ereg(phydev, PHYACC_ATTR_MODE_READ,
1598c2ecf20Sopenharmony_ci			 PHYACC_ATTR_BANK_SMI, 0x00, 0);
1608c2ecf20Sopenharmony_ci	if (rc < 0)
1618c2ecf20Sopenharmony_ci		return rc;
1628c2ecf20Sopenharmony_ci	if ((rc & 0x8000) != 0)
1638c2ecf20Sopenharmony_ci		return -ETIMEDOUT;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	/* PHY Initialization */
1668c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(init); i++) {
1678c2ecf20Sopenharmony_ci		if (init[i].mode == PHYACC_ATTR_MODE_MODIFY) {
1688c2ecf20Sopenharmony_ci			rc = access_ereg_modify_changed(phydev, init[i].bank,
1698c2ecf20Sopenharmony_ci							init[i].offset,
1708c2ecf20Sopenharmony_ci							init[i].val,
1718c2ecf20Sopenharmony_ci							init[i].mask);
1728c2ecf20Sopenharmony_ci		} else {
1738c2ecf20Sopenharmony_ci			rc = access_ereg(phydev, init[i].mode, init[i].bank,
1748c2ecf20Sopenharmony_ci					 init[i].offset, init[i].val);
1758c2ecf20Sopenharmony_ci		}
1768c2ecf20Sopenharmony_ci		if (rc < 0)
1778c2ecf20Sopenharmony_ci			return rc;
1788c2ecf20Sopenharmony_ci	}
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	return 0;
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic int lan87xx_phy_config_intr(struct phy_device *phydev)
1848c2ecf20Sopenharmony_ci{
1858c2ecf20Sopenharmony_ci	int rc, val = 0;
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
1888c2ecf20Sopenharmony_ci		/* unmask all source and clear them before enable */
1898c2ecf20Sopenharmony_ci		rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, 0x7FFF);
1908c2ecf20Sopenharmony_ci		rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
1918c2ecf20Sopenharmony_ci		val = LAN87XX_MASK_LINK_UP | LAN87XX_MASK_LINK_DOWN;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	rc = phy_write(phydev, LAN87XX_INTERRUPT_MASK, val);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	return rc < 0 ? rc : 0;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int lan87xx_phy_ack_interrupt(struct phy_device *phydev)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	int rc = phy_read(phydev, LAN87XX_INTERRUPT_SOURCE);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	return rc < 0 ? rc : 0;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int lan87xx_config_init(struct phy_device *phydev)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	int rc = lan87xx_phy_init(phydev);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return rc < 0 ? rc : 0;
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic struct phy_driver microchip_t1_phy_driver[] = {
2148c2ecf20Sopenharmony_ci	{
2158c2ecf20Sopenharmony_ci		.phy_id         = 0x0007c150,
2168c2ecf20Sopenharmony_ci		.phy_id_mask    = 0xfffffff0,
2178c2ecf20Sopenharmony_ci		.name           = "Microchip LAN87xx T1",
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci		.features       = PHY_BASIC_T1_FEATURES,
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		.config_init	= lan87xx_config_init,
2228c2ecf20Sopenharmony_ci		.config_aneg    = genphy_config_aneg,
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci		.ack_interrupt  = lan87xx_phy_ack_interrupt,
2258c2ecf20Sopenharmony_ci		.config_intr    = lan87xx_phy_config_intr,
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_ci		.suspend        = genphy_suspend,
2288c2ecf20Sopenharmony_ci		.resume         = genphy_resume,
2298c2ecf20Sopenharmony_ci	}
2308c2ecf20Sopenharmony_ci};
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_cimodule_phy_driver(microchip_t1_phy_driver);
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic struct mdio_device_id __maybe_unused microchip_t1_tbl[] = {
2358c2ecf20Sopenharmony_ci	{ 0x0007c150, 0xfffffff0 },
2368c2ecf20Sopenharmony_ci	{ }
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(mdio, microchip_t1_tbl);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRIVER_AUTHOR);
2428c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
2438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
244