xref: /openbmc/linux/drivers/net/phy/national.c (revision 1953feb0)
1a2443fd1SAndrew Lunn // SPDX-License-Identifier: GPL-2.0+
24621bf12SDavid S. Miller /*
34621bf12SDavid S. Miller  * drivers/net/phy/national.c
44621bf12SDavid S. Miller  *
54621bf12SDavid S. Miller  * Driver for National Semiconductor PHYs
64621bf12SDavid S. Miller  *
74621bf12SDavid S. Miller  * Author: Stuart Menefy <stuart.menefy@st.com>
84621bf12SDavid S. Miller  * Maintainer: Giuseppe Cavallaro <peppe.cavallaro@st.com>
94621bf12SDavid S. Miller  *
104621bf12SDavid S. Miller  * Copyright (c) 2008 STMicroelectronics Limited
114621bf12SDavid S. Miller  */
124621bf12SDavid S. Miller 
138d242488SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
148d242488SJoe Perches 
154621bf12SDavid S. Miller #include <linux/kernel.h>
164621bf12SDavid S. Miller #include <linux/module.h>
174621bf12SDavid S. Miller #include <linux/mii.h>
184621bf12SDavid S. Miller #include <linux/ethtool.h>
194621bf12SDavid S. Miller #include <linux/phy.h>
204621bf12SDavid S. Miller #include <linux/netdevice.h>
214621bf12SDavid S. Miller 
224621bf12SDavid S. Miller /* DP83865 phy identifier values */
234621bf12SDavid S. Miller #define DP83865_PHY_ID	0x20005c7a
244621bf12SDavid S. Miller 
256e6f400fSGiuseppe CAVALLARO #define DP83865_INT_STATUS	0x14
266e6f400fSGiuseppe CAVALLARO #define DP83865_INT_MASK	0x15
276e6f400fSGiuseppe CAVALLARO #define DP83865_INT_CLEAR	0x17
284621bf12SDavid S. Miller 
294621bf12SDavid S. Miller #define DP83865_INT_REMOTE_FAULT 0x0008
304621bf12SDavid S. Miller #define DP83865_INT_ANE_COMPLETED 0x0010
314621bf12SDavid S. Miller #define DP83865_INT_LINK_CHANGE	0xe000
324621bf12SDavid S. Miller #define DP83865_INT_MASK_DEFAULT (DP83865_INT_REMOTE_FAULT | \
334621bf12SDavid S. Miller 				DP83865_INT_ANE_COMPLETED | \
344621bf12SDavid S. Miller 				DP83865_INT_LINK_CHANGE)
354621bf12SDavid S. Miller 
364621bf12SDavid S. Miller /* Advanced proprietary configuration */
374621bf12SDavid S. Miller #define NS_EXP_MEM_CTL	0x16
384621bf12SDavid S. Miller #define NS_EXP_MEM_DATA	0x1d
394621bf12SDavid S. Miller #define NS_EXP_MEM_ADD	0x1e
404621bf12SDavid S. Miller 
414621bf12SDavid S. Miller #define LED_CTRL_REG 0x13
424621bf12SDavid S. Miller #define AN_FALLBACK_AN 0x0001
434621bf12SDavid S. Miller #define AN_FALLBACK_CRC 0x0002
444621bf12SDavid S. Miller #define AN_FALLBACK_IE 0x0004
454621bf12SDavid S. Miller #define ALL_FALLBACK_ON (AN_FALLBACK_AN |  AN_FALLBACK_CRC | AN_FALLBACK_IE)
464621bf12SDavid S. Miller 
474621bf12SDavid S. Miller enum hdx_loopback {
484621bf12SDavid S. Miller 	hdx_loopback_on = 0,
494621bf12SDavid S. Miller 	hdx_loopback_off = 1,
504621bf12SDavid S. Miller };
514621bf12SDavid S. Miller 
ns_exp_read(struct phy_device * phydev,u16 reg)524621bf12SDavid S. Miller static u8 ns_exp_read(struct phy_device *phydev, u16 reg)
534621bf12SDavid S. Miller {
544621bf12SDavid S. Miller 	phy_write(phydev, NS_EXP_MEM_ADD, reg);
554621bf12SDavid S. Miller 	return phy_read(phydev, NS_EXP_MEM_DATA);
564621bf12SDavid S. Miller }
574621bf12SDavid S. Miller 
ns_exp_write(struct phy_device * phydev,u16 reg,u8 data)584621bf12SDavid S. Miller static void ns_exp_write(struct phy_device *phydev, u16 reg, u8 data)
594621bf12SDavid S. Miller {
604621bf12SDavid S. Miller 	phy_write(phydev, NS_EXP_MEM_ADD, reg);
614621bf12SDavid S. Miller 	phy_write(phydev, NS_EXP_MEM_DATA, data);
624621bf12SDavid S. Miller }
634621bf12SDavid S. Miller 
ns_ack_interrupt(struct phy_device * phydev)644621bf12SDavid S. Miller static int ns_ack_interrupt(struct phy_device *phydev)
654621bf12SDavid S. Miller {
666e6f400fSGiuseppe CAVALLARO 	int ret = phy_read(phydev, DP83865_INT_STATUS);
674621bf12SDavid S. Miller 	if (ret < 0)
684621bf12SDavid S. Miller 		return ret;
694621bf12SDavid S. Miller 
706e6f400fSGiuseppe CAVALLARO 	/* Clear the interrupt status bit by writing a “1”
71*1953feb0SWenpeng Liang 	 * to the corresponding bit in INT_CLEAR (2:0 are reserved)
72*1953feb0SWenpeng Liang 	 */
736e6f400fSGiuseppe CAVALLARO 	ret = phy_write(phydev, DP83865_INT_CLEAR, ret & ~0x7);
746e6f400fSGiuseppe CAVALLARO 
756e6f400fSGiuseppe CAVALLARO 	return ret;
764621bf12SDavid S. Miller }
774621bf12SDavid S. Miller 
ns_handle_interrupt(struct phy_device * phydev)786571b455SIoana Ciornei static irqreturn_t ns_handle_interrupt(struct phy_device *phydev)
796571b455SIoana Ciornei {
806571b455SIoana Ciornei 	int irq_status;
816571b455SIoana Ciornei 
826571b455SIoana Ciornei 	irq_status = phy_read(phydev, DP83865_INT_STATUS);
836571b455SIoana Ciornei 	if (irq_status < 0) {
846571b455SIoana Ciornei 		phy_error(phydev);
856571b455SIoana Ciornei 		return IRQ_NONE;
866571b455SIoana Ciornei 	}
876571b455SIoana Ciornei 
886571b455SIoana Ciornei 	if (!(irq_status & DP83865_INT_MASK_DEFAULT))
896571b455SIoana Ciornei 		return IRQ_NONE;
906571b455SIoana Ciornei 
916571b455SIoana Ciornei 	/* clear the interrupt */
926571b455SIoana Ciornei 	phy_write(phydev, DP83865_INT_CLEAR, irq_status & ~0x7);
936571b455SIoana Ciornei 
946571b455SIoana Ciornei 	phy_trigger_machine(phydev);
956571b455SIoana Ciornei 
966571b455SIoana Ciornei 	return IRQ_HANDLED;
976571b455SIoana Ciornei }
986571b455SIoana Ciornei 
ns_config_intr(struct phy_device * phydev)99a4d77421SIoana Ciornei static int ns_config_intr(struct phy_device *phydev)
100a4d77421SIoana Ciornei {
101a4d77421SIoana Ciornei 	int err;
102a4d77421SIoana Ciornei 
103a4d77421SIoana Ciornei 	if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
104a4d77421SIoana Ciornei 		err = ns_ack_interrupt(phydev);
105a4d77421SIoana Ciornei 		if (err)
106a4d77421SIoana Ciornei 			return err;
107a4d77421SIoana Ciornei 
108a4d77421SIoana Ciornei 		err = phy_write(phydev, DP83865_INT_MASK,
109a4d77421SIoana Ciornei 				DP83865_INT_MASK_DEFAULT);
110a4d77421SIoana Ciornei 	} else {
111a4d77421SIoana Ciornei 		err = phy_write(phydev, DP83865_INT_MASK, 0);
112a4d77421SIoana Ciornei 		if (err)
113a4d77421SIoana Ciornei 			return err;
114a4d77421SIoana Ciornei 
115a4d77421SIoana Ciornei 		err = ns_ack_interrupt(phydev);
116a4d77421SIoana Ciornei 	}
117a4d77421SIoana Ciornei 
118a4d77421SIoana Ciornei 	return err;
119a4d77421SIoana Ciornei }
120a4d77421SIoana Ciornei 
ns_giga_speed_fallback(struct phy_device * phydev,int mode)1214621bf12SDavid S. Miller static void ns_giga_speed_fallback(struct phy_device *phydev, int mode)
1224621bf12SDavid S. Miller {
1234621bf12SDavid S. Miller 	int bmcr = phy_read(phydev, MII_BMCR);
1244621bf12SDavid S. Miller 
1254621bf12SDavid S. Miller 	phy_write(phydev, MII_BMCR, (bmcr | BMCR_PDOWN));
1264621bf12SDavid S. Miller 
1274621bf12SDavid S. Miller 	/* Enable 8 bit expended memory read/write (no auto increment) */
1284621bf12SDavid S. Miller 	phy_write(phydev, NS_EXP_MEM_CTL, 0);
1294621bf12SDavid S. Miller 	phy_write(phydev, NS_EXP_MEM_ADD, 0x1C0);
1304621bf12SDavid S. Miller 	phy_write(phydev, NS_EXP_MEM_DATA, 0x0008);
1314621bf12SDavid S. Miller 	phy_write(phydev, MII_BMCR, (bmcr & ~BMCR_PDOWN));
1324621bf12SDavid S. Miller 	phy_write(phydev, LED_CTRL_REG, mode);
1334621bf12SDavid S. Miller }
1344621bf12SDavid S. Miller 
ns_10_base_t_hdx_loopack(struct phy_device * phydev,int disable)1354621bf12SDavid S. Miller static void ns_10_base_t_hdx_loopack(struct phy_device *phydev, int disable)
1364621bf12SDavid S. Miller {
137e47488b2SPeter Mamonov 	u16 lb_dis = BIT(1);
138e47488b2SPeter Mamonov 
1394621bf12SDavid S. Miller 	if (disable)
140e47488b2SPeter Mamonov 		ns_exp_write(phydev, 0x1c0,
141e47488b2SPeter Mamonov 			     ns_exp_read(phydev, 0x1c0) | lb_dis);
1424621bf12SDavid S. Miller 	else
1434621bf12SDavid S. Miller 		ns_exp_write(phydev, 0x1c0,
144e47488b2SPeter Mamonov 			     ns_exp_read(phydev, 0x1c0) & ~lb_dis);
1454621bf12SDavid S. Miller 
1468d242488SJoe Perches 	pr_debug("10BASE-T HDX loopback %s\n",
147e47488b2SPeter Mamonov 		 (ns_exp_read(phydev, 0x1c0) & lb_dis) ? "off" : "on");
1484621bf12SDavid S. Miller }
1494621bf12SDavid S. Miller 
ns_config_init(struct phy_device * phydev)1504621bf12SDavid S. Miller static int ns_config_init(struct phy_device *phydev)
1514621bf12SDavid S. Miller {
1524621bf12SDavid S. Miller 	ns_giga_speed_fallback(phydev, ALL_FALLBACK_ON);
1534621bf12SDavid S. Miller 	/* In the latest MAC or switches design, the 10 Mbps loopback
154*1953feb0SWenpeng Liang 	 * is desired to be turned off.
155*1953feb0SWenpeng Liang 	 */
1564621bf12SDavid S. Miller 	ns_10_base_t_hdx_loopack(phydev, hdx_loopback_off);
1574621bf12SDavid S. Miller 	return ns_ack_interrupt(phydev);
1584621bf12SDavid S. Miller }
1594621bf12SDavid S. Miller 
160116dffa0SJohan Hovold static struct phy_driver dp83865_driver[] = { {
1614621bf12SDavid S. Miller 	.phy_id = DP83865_PHY_ID,
1624621bf12SDavid S. Miller 	.phy_id_mask = 0xfffffff0,
1634621bf12SDavid S. Miller 	.name = "NatSemi DP83865",
164dcdecdcfSHeiner Kallweit 	/* PHY_GBIT_FEATURES */
1654621bf12SDavid S. Miller 	.config_init = ns_config_init,
1664621bf12SDavid S. Miller 	.config_intr = ns_config_intr,
1676571b455SIoana Ciornei 	.handle_interrupt = ns_handle_interrupt,
168116dffa0SJohan Hovold } };
1694621bf12SDavid S. Miller 
170116dffa0SJohan Hovold module_phy_driver(dp83865_driver);
1714621bf12SDavid S. Miller 
1724621bf12SDavid S. Miller MODULE_DESCRIPTION("NatSemi PHY driver");
1734621bf12SDavid S. Miller MODULE_AUTHOR("Stuart Menefy");
1744621bf12SDavid S. Miller MODULE_LICENSE("GPL");
1754621bf12SDavid S. Miller 
176cf93c945SUwe Kleine-König static struct mdio_device_id __maybe_unused ns_tbl[] = {
1774e4f10f6SDavid Woodhouse 	{ DP83865_PHY_ID, 0xfffffff0 },
1784e4f10f6SDavid Woodhouse 	{ }
1794e4f10f6SDavid Woodhouse };
1804e4f10f6SDavid Woodhouse 
1814e4f10f6SDavid Woodhouse MODULE_DEVICE_TABLE(mdio, ns_tbl);
182