b707a9b27847107d71da2f81cf6db0188212e0ac
[linux-2.6-microblaze.git] / drivers / net / phy / dp83848.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the Texas Instruments DP83848 PHY
4  *
5  * Copyright (C) 2015-2016 Texas Instruments Incorporated - http://www.ti.com/
6  */
7
8 #include <linux/module.h>
9 #include <linux/phy.h>
10
11 #define TI_DP83848C_PHY_ID              0x20005ca0
12 #define TI_DP83620_PHY_ID               0x20005ce0
13 #define NS_DP83848C_PHY_ID              0x20005c90
14 #define TLK10X_PHY_ID                   0x2000a210
15
16 /* Registers */
17 #define DP83848_MICR                    0x11 /* MII Interrupt Control Register */
18 #define DP83848_MISR                    0x12 /* MII Interrupt Status Register */
19
20 /* MICR Register Fields */
21 #define DP83848_MICR_INT_OE             BIT(0) /* Interrupt Output Enable */
22 #define DP83848_MICR_INTEN              BIT(1) /* Interrupt Enable */
23
24 /* MISR Register Fields */
25 #define DP83848_MISR_RHF_INT_EN         BIT(0) /* Receive Error Counter */
26 #define DP83848_MISR_FHF_INT_EN         BIT(1) /* False Carrier Counter */
27 #define DP83848_MISR_ANC_INT_EN         BIT(2) /* Auto-negotiation complete */
28 #define DP83848_MISR_DUP_INT_EN         BIT(3) /* Duplex Status */
29 #define DP83848_MISR_SPD_INT_EN         BIT(4) /* Speed status */
30 #define DP83848_MISR_LINK_INT_EN        BIT(5) /* Link status */
31 #define DP83848_MISR_ED_INT_EN          BIT(6) /* Energy detect */
32 #define DP83848_MISR_LQM_INT_EN         BIT(7) /* Link Quality Monitor */
33
34 #define DP83848_INT_EN_MASK             \
35         (DP83848_MISR_ANC_INT_EN |      \
36          DP83848_MISR_DUP_INT_EN |      \
37          DP83848_MISR_SPD_INT_EN |      \
38          DP83848_MISR_LINK_INT_EN)
39
40 #define DP83848_MISR_RHF_INT            BIT(8)
41 #define DP83848_MISR_FHF_INT            BIT(9)
42 #define DP83848_MISR_ANC_INT            BIT(10)
43 #define DP83848_MISR_DUP_INT            BIT(11)
44 #define DP83848_MISR_SPD_INT            BIT(12)
45 #define DP83848_MISR_LINK_INT           BIT(13)
46 #define DP83848_MISR_ED_INT             BIT(14)
47
48 #define DP83848_INT_MASK                \
49         (DP83848_MISR_ANC_INT | \
50          DP83848_MISR_DUP_INT | \
51          DP83848_MISR_SPD_INT | \
52          DP83848_MISR_LINK_INT)
53
54 static int dp83848_ack_interrupt(struct phy_device *phydev)
55 {
56         int err = phy_read(phydev, DP83848_MISR);
57
58         return err < 0 ? err : 0;
59 }
60
61 static int dp83848_config_intr(struct phy_device *phydev)
62 {
63         int control, ret;
64
65         control = phy_read(phydev, DP83848_MICR);
66         if (control < 0)
67                 return control;
68
69         if (phydev->interrupts == PHY_INTERRUPT_ENABLED) {
70                 control |= DP83848_MICR_INT_OE;
71                 control |= DP83848_MICR_INTEN;
72
73                 ret = phy_write(phydev, DP83848_MISR, DP83848_INT_EN_MASK);
74                 if (ret < 0)
75                         return ret;
76         } else {
77                 control &= ~DP83848_MICR_INTEN;
78         }
79
80         return phy_write(phydev, DP83848_MICR, control);
81 }
82
83 static irqreturn_t dp83848_handle_interrupt(struct phy_device *phydev)
84 {
85         int irq_status;
86
87         irq_status = phy_read(phydev, DP83848_MISR);
88         if (irq_status < 0) {
89                 phy_error(phydev);
90                 return IRQ_NONE;
91         }
92
93         if (!(irq_status & DP83848_INT_MASK))
94                 return IRQ_NONE;
95
96         phy_trigger_machine(phydev);
97
98         return IRQ_HANDLED;
99 }
100
101 static int dp83848_config_init(struct phy_device *phydev)
102 {
103         int val;
104
105         /* DP83620 always reports Auto Negotiation Ability on BMSR. Instead,
106          * we check initial value of BMCR Auto negotiation enable bit
107          */
108         val = phy_read(phydev, MII_BMCR);
109         if (!(val & BMCR_ANENABLE))
110                 phydev->autoneg = AUTONEG_DISABLE;
111
112         return 0;
113 }
114
115 static struct mdio_device_id __maybe_unused dp83848_tbl[] = {
116         { TI_DP83848C_PHY_ID, 0xfffffff0 },
117         { NS_DP83848C_PHY_ID, 0xfffffff0 },
118         { TI_DP83620_PHY_ID, 0xfffffff0 },
119         { TLK10X_PHY_ID, 0xfffffff0 },
120         { }
121 };
122 MODULE_DEVICE_TABLE(mdio, dp83848_tbl);
123
124 #define DP83848_PHY_DRIVER(_id, _name, _config_init)            \
125         {                                                       \
126                 .phy_id         = _id,                          \
127                 .phy_id_mask    = 0xfffffff0,                   \
128                 .name           = _name,                        \
129                 /* PHY_BASIC_FEATURES */                        \
130                                                                 \
131                 .soft_reset     = genphy_soft_reset,            \
132                 .config_init    = _config_init,                 \
133                 .suspend        = genphy_suspend,               \
134                 .resume         = genphy_resume,                \
135                                                                 \
136                 /* IRQ related */                               \
137                 .ack_interrupt  = dp83848_ack_interrupt,        \
138                 .config_intr    = dp83848_config_intr,          \
139                 .handle_interrupt = dp83848_handle_interrupt,   \
140         }
141
142 static struct phy_driver dp83848_driver[] = {
143         DP83848_PHY_DRIVER(TI_DP83848C_PHY_ID, "TI DP83848C 10/100 Mbps PHY",
144                            NULL),
145         DP83848_PHY_DRIVER(NS_DP83848C_PHY_ID, "NS DP83848C 10/100 Mbps PHY",
146                            NULL),
147         DP83848_PHY_DRIVER(TI_DP83620_PHY_ID, "TI DP83620 10/100 Mbps PHY",
148                            dp83848_config_init),
149         DP83848_PHY_DRIVER(TLK10X_PHY_ID, "TI TLK10X 10/100 Mbps PHY",
150                            NULL),
151 };
152 module_phy_driver(dp83848_driver);
153
154 MODULE_DESCRIPTION("Texas Instruments DP83848 PHY driver");
155 MODULE_AUTHOR("Andrew F. Davis <afd@ti.com>");
156 MODULE_LICENSE("GPL v2");