1 // SPDX-License-Identifier: GPL-2.0+
3 * drivers/net/phy/lxt.c
5 * Driver for Intel LXT PHYs
9 * Copyright (c) 2004 Freescale Semiconductor, Inc.
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/errno.h>
14 #include <linux/unistd.h>
15 #include <linux/interrupt.h>
16 #include <linux/init.h>
17 #include <linux/delay.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
20 #include <linux/skbuff.h>
21 #include <linux/spinlock.h>
23 #include <linux/module.h>
24 #include <linux/mii.h>
25 #include <linux/ethtool.h>
26 #include <linux/phy.h>
30 #include <linux/uaccess.h>
32 /* The Level one LXT970 is used by many boards */
34 #define MII_LXT970_IER 17 /* Interrupt Enable Register */
36 #define MII_LXT970_IER_IEN 0x0002
38 #define MII_LXT970_ISR 18 /* Interrupt Status Register */
40 #define MII_LXT970_IRS_MINT BIT(15)
42 #define MII_LXT970_CONFIG 19 /* Configuration Register */
44 /* ------------------------------------------------------------------------- */
45 /* The Level one LXT971 is used on some of my custom boards */
47 /* register definitions for the 971 */
48 #define MII_LXT971_IER 18 /* Interrupt Enable Register */
49 #define MII_LXT971_IER_IEN 0x00f2
51 #define MII_LXT971_ISR 19 /* Interrupt Status Register */
52 #define MII_LXT971_ISR_MASK 0x00f0
54 /* register definitions for the 973 */
55 #define MII_LXT973_PCR 16 /* Port Configuration Register */
56 #define PCR_FIBER_SELECT 1
58 MODULE_DESCRIPTION("Intel LXT PHY driver");
59 MODULE_AUTHOR("Andy Fleming");
60 MODULE_LICENSE("GPL");
62 static int lxt970_ack_interrupt(struct phy_device
*phydev
)
66 err
= phy_read(phydev
, MII_BMSR
);
71 err
= phy_read(phydev
, MII_LXT970_ISR
);
79 static int lxt970_config_intr(struct phy_device
*phydev
)
83 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
84 err
= lxt970_ack_interrupt(phydev
);
88 err
= phy_write(phydev
, MII_LXT970_IER
, MII_LXT970_IER_IEN
);
90 err
= phy_write(phydev
, MII_LXT970_IER
, 0);
94 err
= lxt970_ack_interrupt(phydev
);
100 static irqreturn_t
lxt970_handle_interrupt(struct phy_device
*phydev
)
104 /* The interrupt status register is cleared by reading BMSR
105 * followed by MII_LXT970_ISR
107 irq_status
= phy_read(phydev
, MII_BMSR
);
108 if (irq_status
< 0) {
113 irq_status
= phy_read(phydev
, MII_LXT970_ISR
);
114 if (irq_status
< 0) {
119 if (!(irq_status
& MII_LXT970_IRS_MINT
))
122 phy_trigger_machine(phydev
);
127 static int lxt970_config_init(struct phy_device
*phydev
)
129 return phy_write(phydev
, MII_LXT970_CONFIG
, 0);
133 static int lxt971_ack_interrupt(struct phy_device
*phydev
)
135 int err
= phy_read(phydev
, MII_LXT971_ISR
);
143 static int lxt971_config_intr(struct phy_device
*phydev
)
147 if (phydev
->interrupts
== PHY_INTERRUPT_ENABLED
) {
148 err
= lxt971_ack_interrupt(phydev
);
152 err
= phy_write(phydev
, MII_LXT971_IER
, MII_LXT971_IER_IEN
);
154 err
= phy_write(phydev
, MII_LXT971_IER
, 0);
158 err
= lxt971_ack_interrupt(phydev
);
164 static irqreturn_t
lxt971_handle_interrupt(struct phy_device
*phydev
)
168 irq_status
= phy_read(phydev
, MII_LXT971_ISR
);
169 if (irq_status
< 0) {
174 if (!(irq_status
& MII_LXT971_ISR_MASK
))
177 phy_trigger_machine(phydev
);
183 * A2 version of LXT973 chip has an ERRATA: it randomly return the contents
184 * of the previous even register when you read a odd register regularly
187 static int lxt973a2_update_link(struct phy_device
*phydev
)
191 int retry
= 8; /* we try 8 times */
194 status
= phy_read(phydev
, MII_BMSR
);
199 control
= phy_read(phydev
, MII_BMCR
);
204 /* Read link and autonegotiation status */
205 status
= phy_read(phydev
, MII_BMSR
);
206 } while (status
>= 0 && retry
-- && status
== control
);
211 if ((status
& BMSR_LSTATUS
) == 0)
219 static int lxt973a2_read_status(struct phy_device
*phydev
)
225 /* Update the link, but return if there was an error */
226 err
= lxt973a2_update_link(phydev
);
230 if (AUTONEG_ENABLE
== phydev
->autoneg
) {
233 adv
= phy_read(phydev
, MII_ADVERTISE
);
239 lpa
= phy_read(phydev
, MII_LPA
);
244 /* If both registers are equal, it is suspect but not
245 * impossible, hence a new try
247 } while (lpa
== adv
&& retry
--);
249 mii_lpa_to_linkmode_lpa_t(phydev
->lp_advertising
, lpa
);
253 phydev
->speed
= SPEED_10
;
254 phydev
->duplex
= DUPLEX_HALF
;
255 phydev
->pause
= phydev
->asym_pause
= 0;
257 if (lpa
& (LPA_100FULL
| LPA_100HALF
)) {
258 phydev
->speed
= SPEED_100
;
260 if (lpa
& LPA_100FULL
)
261 phydev
->duplex
= DUPLEX_FULL
;
263 if (lpa
& LPA_10FULL
)
264 phydev
->duplex
= DUPLEX_FULL
;
267 phy_resolve_aneg_pause(phydev
);
269 err
= genphy_read_status_fixed(phydev
);
273 phydev
->pause
= phydev
->asym_pause
= 0;
274 linkmode_zero(phydev
->lp_advertising
);
280 static int lxt973_probe(struct phy_device
*phydev
)
282 int val
= phy_read(phydev
, MII_LXT973_PCR
);
284 if (val
& PCR_FIBER_SELECT
) {
286 * If fiber is selected, then the only correct setting
287 * is 100Mbps, full duplex, and auto negotiation off.
289 val
= phy_read(phydev
, MII_BMCR
);
290 val
|= (BMCR_SPEED100
| BMCR_FULLDPLX
);
291 val
&= ~BMCR_ANENABLE
;
292 phy_write(phydev
, MII_BMCR
, val
);
293 /* Remember that the port is in fiber mode. */
294 phydev
->priv
= lxt973_probe
;
301 static int lxt973_config_aneg(struct phy_device
*phydev
)
303 /* Do nothing if port is in fiber mode. */
304 return phydev
->priv
? 0 : genphy_config_aneg(phydev
);
307 static struct phy_driver lxt97x_driver
[] = {
309 .phy_id
= 0x78100000,
311 .phy_id_mask
= 0xfffffff0,
312 /* PHY_BASIC_FEATURES */
313 .config_init
= lxt970_config_init
,
314 .config_intr
= lxt970_config_intr
,
315 .handle_interrupt
= lxt970_handle_interrupt
,
317 .phy_id
= 0x001378e0,
319 .phy_id_mask
= 0xfffffff0,
320 /* PHY_BASIC_FEATURES */
321 .config_intr
= lxt971_config_intr
,
322 .handle_interrupt
= lxt971_handle_interrupt
,
323 .suspend
= genphy_suspend
,
324 .resume
= genphy_resume
,
326 .phy_id
= 0x00137a10,
328 .phy_id_mask
= 0xffffffff,
329 /* PHY_BASIC_FEATURES */
331 .probe
= lxt973_probe
,
332 .config_aneg
= lxt973_config_aneg
,
333 .read_status
= lxt973a2_read_status
,
334 .suspend
= genphy_suspend
,
335 .resume
= genphy_resume
,
337 .phy_id
= 0x00137a10,
339 .phy_id_mask
= 0xfffffff0,
340 /* PHY_BASIC_FEATURES */
342 .probe
= lxt973_probe
,
343 .config_aneg
= lxt973_config_aneg
,
344 .suspend
= genphy_suspend
,
345 .resume
= genphy_resume
,
348 module_phy_driver(lxt97x_driver
);
350 static struct mdio_device_id __maybe_unused lxt_tbl
[] = {
351 { 0x78100000, 0xfffffff0 },
352 { 0x001378e0, 0xfffffff0 },
353 { 0x00137a10, 0xfffffff0 },
357 MODULE_DEVICE_TABLE(mdio
, lxt_tbl
);