blob: 4a97024061e530f84f3c2cac3d4015d8925e2086 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Eugene Surovegin37448f72005-10-10 16:58:14 -07002 * drivers/net/ibm_emac/ibm_emac_phy.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Eugene Surovegin37448f72005-10-10 16:58:14 -07004 * Driver for PowerPC 4xx on-chip ethernet controller, PHY support.
5 * Borrowed from sungem_phy.c, though I only kept the generic MII
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * driver for now.
7 *
8 * This file should be shared with other drivers or eventually
9 * merged as the "low level" part of miilib
10 *
11 * (c) 2003, Benjamin Herrenscmidt (benh@kernel.crashing.org)
Eugene Surovegin37448f72005-10-10 16:58:14 -070012 * (c) 2004-2005, Eugene Surovegin <ebs@ebshome.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
14 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/types.h>
18#include <linux/netdevice.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/mii.h>
20#include <linux/ethtool.h>
21#include <linux/delay.h>
22
Eugene Surovegin37448f72005-10-10 16:58:14 -070023#include <asm/ocp.h>
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "ibm_emac_phy.h"
26
Eugene Surovegin37448f72005-10-10 16:58:14 -070027static inline int phy_read(struct mii_phy *phy, int reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Eugene Surovegin37448f72005-10-10 16:58:14 -070029 return phy->mdio_read(phy->dev, phy->address, reg);
30}
31
32static inline void phy_write(struct mii_phy *phy, int reg, int val)
33{
34 phy->mdio_write(phy->dev, phy->address, reg, val);
35}
36
37int mii_reset_phy(struct mii_phy *phy)
38{
39 int val;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 int limit = 10000;
41
Eugene Surovegin37448f72005-10-10 16:58:14 -070042 val = phy_read(phy, MII_BMCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 val &= ~BMCR_ISOLATE;
44 val |= BMCR_RESET;
Eugene Surovegin37448f72005-10-10 16:58:14 -070045 phy_write(phy, MII_BMCR, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Eugene Surovegin37448f72005-10-10 16:58:14 -070047 udelay(300);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 while (limit--) {
Eugene Surovegin37448f72005-10-10 16:58:14 -070050 val = phy_read(phy, MII_BMCR);
51 if (val >= 0 && (val & BMCR_RESET) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 break;
53 udelay(10);
54 }
55 if ((val & BMCR_ISOLATE) && limit > 0)
Eugene Surovegin37448f72005-10-10 16:58:14 -070056 phy_write(phy, MII_BMCR, val & ~BMCR_ISOLATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Eugene Surovegin37448f72005-10-10 16:58:14 -070058 return limit <= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Eugene Surovegin37448f72005-10-10 16:58:14 -070061static int genmii_setup_aneg(struct mii_phy *phy, u32 advertise)
62{
63 int ctl, adv;
64
65 phy->autoneg = AUTONEG_ENABLE;
66 phy->speed = SPEED_10;
67 phy->duplex = DUPLEX_HALF;
68 phy->pause = phy->asym_pause = 0;
69 phy->advertising = advertise;
70
71 /* Setup standard advertise */
72 adv = phy_read(phy, MII_ADVERTISE);
73 if (adv < 0)
74 return adv;
75 adv &= ~(ADVERTISE_ALL | ADVERTISE_100BASE4 | ADVERTISE_PAUSE_CAP |
76 ADVERTISE_PAUSE_ASYM);
77 if (advertise & ADVERTISED_10baseT_Half)
78 adv |= ADVERTISE_10HALF;
79 if (advertise & ADVERTISED_10baseT_Full)
80 adv |= ADVERTISE_10FULL;
81 if (advertise & ADVERTISED_100baseT_Half)
82 adv |= ADVERTISE_100HALF;
83 if (advertise & ADVERTISED_100baseT_Full)
84 adv |= ADVERTISE_100FULL;
85 if (advertise & ADVERTISED_Pause)
86 adv |= ADVERTISE_PAUSE_CAP;
87 if (advertise & ADVERTISED_Asym_Pause)
88 adv |= ADVERTISE_PAUSE_ASYM;
89 phy_write(phy, MII_ADVERTISE, adv);
90
91 if (phy->features &
92 (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half)) {
93 adv = phy_read(phy, MII_CTRL1000);
94 if (adv < 0)
95 return adv;
96 adv &= ~(ADVERTISE_1000FULL | ADVERTISE_1000HALF);
97 if (advertise & ADVERTISED_1000baseT_Full)
98 adv |= ADVERTISE_1000FULL;
99 if (advertise & ADVERTISED_1000baseT_Half)
100 adv |= ADVERTISE_1000HALF;
101 phy_write(phy, MII_CTRL1000, adv);
102 }
103
104 /* Start/Restart aneg */
105 ctl = phy_read(phy, MII_BMCR);
106 ctl |= (BMCR_ANENABLE | BMCR_ANRESTART);
107 phy_write(phy, MII_BMCR, ctl);
108
109 return 0;
110}
111
112static int genmii_setup_forced(struct mii_phy *phy, int speed, int fd)
113{
114 int ctl;
115
116 phy->autoneg = AUTONEG_DISABLE;
117 phy->speed = speed;
118 phy->duplex = fd;
119 phy->pause = phy->asym_pause = 0;
120
121 ctl = phy_read(phy, MII_BMCR);
122 if (ctl < 0)
123 return ctl;
124 ctl &= ~(BMCR_FULLDPLX | BMCR_SPEED100 | BMCR_ANENABLE);
125
126 /* First reset the PHY */
127 phy_write(phy, MII_BMCR, ctl | BMCR_RESET);
128
129 /* Select speed & duplex */
130 switch (speed) {
131 case SPEED_10:
132 break;
133 case SPEED_100:
134 ctl |= BMCR_SPEED100;
135 break;
136 case SPEED_1000:
137 ctl |= BMCR_SPEED1000;
138 break;
139 default:
140 return -EINVAL;
141 }
142 if (fd == DUPLEX_FULL)
143 ctl |= BMCR_FULLDPLX;
144 phy_write(phy, MII_BMCR, ctl);
145
146 return 0;
147}
148
149static int genmii_poll_link(struct mii_phy *phy)
150{
151 int status;
152
153 /* Clear latched value with dummy read */
154 phy_read(phy, MII_BMSR);
155 status = phy_read(phy, MII_BMSR);
156 if (status < 0 || (status & BMSR_LSTATUS) == 0)
157 return 0;
158 if (phy->autoneg == AUTONEG_ENABLE && !(status & BMSR_ANEGCOMPLETE))
159 return 0;
160 return 1;
161}
162
163static int genmii_read_link(struct mii_phy *phy)
164{
165 if (phy->autoneg == AUTONEG_ENABLE) {
166 int glpa = 0;
167 int lpa = phy_read(phy, MII_LPA) & phy_read(phy, MII_ADVERTISE);
168 if (lpa < 0)
169 return lpa;
170
171 if (phy->features &
172 (SUPPORTED_1000baseT_Full | SUPPORTED_1000baseT_Half)) {
173 int adv = phy_read(phy, MII_CTRL1000);
174 glpa = phy_read(phy, MII_STAT1000);
175
176 if (glpa < 0 || adv < 0)
177 return adv;
178
179 glpa &= adv << 2;
180 }
181
182 phy->speed = SPEED_10;
183 phy->duplex = DUPLEX_HALF;
184 phy->pause = phy->asym_pause = 0;
185
186 if (glpa & (LPA_1000FULL | LPA_1000HALF)) {
187 phy->speed = SPEED_1000;
188 if (glpa & LPA_1000FULL)
189 phy->duplex = DUPLEX_FULL;
190 } else if (lpa & (LPA_100FULL | LPA_100HALF)) {
191 phy->speed = SPEED_100;
192 if (lpa & LPA_100FULL)
193 phy->duplex = DUPLEX_FULL;
194 } else if (lpa & LPA_10FULL)
195 phy->duplex = DUPLEX_FULL;
196
197 if (phy->duplex == DUPLEX_FULL) {
198 phy->pause = lpa & LPA_PAUSE_CAP ? 1 : 0;
199 phy->asym_pause = lpa & LPA_PAUSE_ASYM ? 1 : 0;
200 }
201 } else {
202 int bmcr = phy_read(phy, MII_BMCR);
203 if (bmcr < 0)
204 return bmcr;
205
206 if (bmcr & BMCR_FULLDPLX)
207 phy->duplex = DUPLEX_FULL;
208 else
209 phy->duplex = DUPLEX_HALF;
210 if (bmcr & BMCR_SPEED1000)
211 phy->speed = SPEED_1000;
212 else if (bmcr & BMCR_SPEED100)
213 phy->speed = SPEED_100;
214 else
215 phy->speed = SPEED_10;
216
217 phy->pause = phy->asym_pause = 0;
218 }
219 return 0;
220}
221
222/* Generic implementation for most 10/100/1000 PHYs */
223static struct mii_phy_ops generic_phy_ops = {
224 .setup_aneg = genmii_setup_aneg,
225 .setup_forced = genmii_setup_forced,
226 .poll_link = genmii_poll_link,
227 .read_link = genmii_read_link
228};
229
230static struct mii_phy_def genmii_phy_def = {
231 .phy_id = 0x00000000,
232 .phy_id_mask = 0x00000000,
233 .name = "Generic MII",
234 .ops = &generic_phy_ops
235};
236
237/* CIS8201 */
Eugene Surovegin86e7fe72005-10-29 12:47:41 -0700238#define MII_CIS8201_10BTCSR 0x16
239#define TENBTCSR_ECHO_DISABLE 0x2000
Eugene Surovegin37448f72005-10-10 16:58:14 -0700240#define MII_CIS8201_EPCR 0x17
241#define EPCR_MODE_MASK 0x3000
242#define EPCR_GMII_MODE 0x0000
243#define EPCR_RGMII_MODE 0x1000
244#define EPCR_TBI_MODE 0x2000
245#define EPCR_RTBI_MODE 0x3000
Eugene Surovegin86e7fe72005-10-29 12:47:41 -0700246#define MII_CIS8201_ACSR 0x1c
247#define ACSR_PIN_PRIO_SELECT 0x0004
Eugene Surovegin37448f72005-10-10 16:58:14 -0700248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static int cis8201_init(struct mii_phy *phy)
250{
Eugene Surovegin37448f72005-10-10 16:58:14 -0700251 int epcr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 epcr = phy_read(phy, MII_CIS8201_EPCR);
Eugene Surovegin37448f72005-10-10 16:58:14 -0700254 if (epcr < 0)
255 return epcr;
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 epcr &= ~EPCR_MODE_MASK;
258
259 switch (phy->mode) {
260 case PHY_MODE_TBI:
261 epcr |= EPCR_TBI_MODE;
262 break;
263 case PHY_MODE_RTBI:
264 epcr |= EPCR_RTBI_MODE;
265 break;
266 case PHY_MODE_GMII:
267 epcr |= EPCR_GMII_MODE;
268 break;
269 case PHY_MODE_RGMII:
270 default:
271 epcr |= EPCR_RGMII_MODE;
272 }
273
274 phy_write(phy, MII_CIS8201_EPCR, epcr);
Eugene Surovegin86e7fe72005-10-29 12:47:41 -0700275
276 /* MII regs override strap pins */
277 phy_write(phy, MII_CIS8201_ACSR,
278 phy_read(phy, MII_CIS8201_ACSR) | ACSR_PIN_PRIO_SELECT);
279
280 /* Disable TX_EN -> CRS echo mode, otherwise 10/HDX doesn't work */
281 phy_write(phy, MII_CIS8201_10BTCSR,
282 phy_read(phy, MII_CIS8201_10BTCSR) | TENBTCSR_ECHO_DISABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
284 return 0;
285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287static struct mii_phy_ops cis8201_phy_ops = {
Eugene Surovegin37448f72005-10-10 16:58:14 -0700288 .init = cis8201_init,
289 .setup_aneg = genmii_setup_aneg,
290 .setup_forced = genmii_setup_forced,
291 .poll_link = genmii_poll_link,
292 .read_link = genmii_read_link
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293};
294
295static struct mii_phy_def cis8201_phy_def = {
Eugene Surovegin37448f72005-10-10 16:58:14 -0700296 .phy_id = 0x000fc410,
297 .phy_id_mask = 0x000ffff0,
298 .name = "CIS8201 Gigabit Ethernet",
299 .ops = &cis8201_phy_ops
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300};
301
302static struct mii_phy_def *mii_phy_table[] = {
303 &cis8201_phy_def,
304 &genmii_phy_def,
305 NULL
306};
307
Eugene Surovegin37448f72005-10-10 16:58:14 -0700308int mii_phy_probe(struct mii_phy *phy, int address)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct mii_phy_def *def;
311 int i;
Eugene Surovegin37448f72005-10-10 16:58:14 -0700312 u32 id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Eugene Surovegin37448f72005-10-10 16:58:14 -0700314 phy->autoneg = AUTONEG_DISABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 phy->advertising = 0;
Eugene Surovegin37448f72005-10-10 16:58:14 -0700316 phy->address = address;
317 phy->speed = SPEED_10;
318 phy->duplex = DUPLEX_HALF;
319 phy->pause = phy->asym_pause = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Eugene Surovegin37448f72005-10-10 16:58:14 -0700321 /* Take PHY out of isolate mode and reset it. */
322 if (mii_reset_phy(phy))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return -ENODEV;
324
325 /* Read ID and find matching entry */
Eugene Surovegin37448f72005-10-10 16:58:14 -0700326 id = (phy_read(phy, MII_PHYSID1) << 16) | phy_read(phy, MII_PHYSID2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 for (i = 0; (def = mii_phy_table[i]) != NULL; i++)
328 if ((id & def->phy_id_mask) == def->phy_id)
329 break;
330 /* Should never be NULL (we have a generic entry), but... */
Eugene Surovegin37448f72005-10-10 16:58:14 -0700331 if (!def)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return -ENODEV;
333
334 phy->def = def;
335
Eugene Surovegin37448f72005-10-10 16:58:14 -0700336 /* Determine PHY features if needed */
337 phy->features = def->features;
338 if (!phy->features) {
339 u16 bmsr = phy_read(phy, MII_BMSR);
340 if (bmsr & BMSR_ANEGCAPABLE)
341 phy->features |= SUPPORTED_Autoneg;
342 if (bmsr & BMSR_10HALF)
343 phy->features |= SUPPORTED_10baseT_Half;
344 if (bmsr & BMSR_10FULL)
345 phy->features |= SUPPORTED_10baseT_Full;
346 if (bmsr & BMSR_100HALF)
347 phy->features |= SUPPORTED_100baseT_Half;
348 if (bmsr & BMSR_100FULL)
349 phy->features |= SUPPORTED_100baseT_Full;
350 if (bmsr & BMSR_ESTATEN) {
351 u16 esr = phy_read(phy, MII_ESTATUS);
352 if (esr & ESTATUS_1000_TFULL)
353 phy->features |= SUPPORTED_1000baseT_Full;
354 if (esr & ESTATUS_1000_THALF)
355 phy->features |= SUPPORTED_1000baseT_Half;
356 }
357 phy->features |= SUPPORTED_MII;
358 }
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 /* Setup default advertising */
Eugene Surovegin37448f72005-10-10 16:58:14 -0700361 phy->advertising = phy->features;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
363 return 0;
364}
365
366MODULE_LICENSE("GPL");