1f7917c00SJeff Kirsher /*
2f7917c00SJeff Kirsher  * Copyright (c) 2005-2008 Chelsio, Inc. All rights reserved.
3f7917c00SJeff Kirsher  *
4f7917c00SJeff Kirsher  * This software is available to you under a choice of one of two
5f7917c00SJeff Kirsher  * licenses.  You may choose to be licensed under the terms of the GNU
6f7917c00SJeff Kirsher  * General Public License (GPL) Version 2, available from the file
7f7917c00SJeff Kirsher  * COPYING in the main directory of this source tree, or the
8f7917c00SJeff Kirsher  * OpenIB.org BSD license below:
9f7917c00SJeff Kirsher  *
10f7917c00SJeff Kirsher  *     Redistribution and use in source and binary forms, with or
11f7917c00SJeff Kirsher  *     without modification, are permitted provided that the following
12f7917c00SJeff Kirsher  *     conditions are met:
13f7917c00SJeff Kirsher  *
14f7917c00SJeff Kirsher  *      - Redistributions of source code must retain the above
15f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
16f7917c00SJeff Kirsher  *        disclaimer.
17f7917c00SJeff Kirsher  *
18f7917c00SJeff Kirsher  *      - Redistributions in binary form must reproduce the above
19f7917c00SJeff Kirsher  *        copyright notice, this list of conditions and the following
20f7917c00SJeff Kirsher  *        disclaimer in the documentation and/or other materials
21f7917c00SJeff Kirsher  *        provided with the distribution.
22f7917c00SJeff Kirsher  *
23f7917c00SJeff Kirsher  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24f7917c00SJeff Kirsher  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25f7917c00SJeff Kirsher  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26f7917c00SJeff Kirsher  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27f7917c00SJeff Kirsher  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28f7917c00SJeff Kirsher  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29f7917c00SJeff Kirsher  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30f7917c00SJeff Kirsher  * SOFTWARE.
31f7917c00SJeff Kirsher  */
32f7917c00SJeff Kirsher #include "common.h"
33f7917c00SJeff Kirsher 
34f7917c00SJeff Kirsher /* VSC8211 PHY specific registers. */
35f7917c00SJeff Kirsher enum {
36f7917c00SJeff Kirsher 	VSC8211_SIGDET_CTRL = 19,
37f7917c00SJeff Kirsher 	VSC8211_EXT_CTRL = 23,
38f7917c00SJeff Kirsher 	VSC8211_INTR_ENABLE = 25,
39f7917c00SJeff Kirsher 	VSC8211_INTR_STATUS = 26,
40f7917c00SJeff Kirsher 	VSC8211_LED_CTRL = 27,
41f7917c00SJeff Kirsher 	VSC8211_AUX_CTRL_STAT = 28,
42f7917c00SJeff Kirsher 	VSC8211_EXT_PAGE_AXS = 31,
43f7917c00SJeff Kirsher };
44f7917c00SJeff Kirsher 
45f7917c00SJeff Kirsher enum {
46f7917c00SJeff Kirsher 	VSC_INTR_RX_ERR = 1 << 0,
47f7917c00SJeff Kirsher 	VSC_INTR_MS_ERR = 1 << 1,  /* master/slave resolution error */
48f7917c00SJeff Kirsher 	VSC_INTR_CABLE = 1 << 2,  /* cable impairment */
49f7917c00SJeff Kirsher 	VSC_INTR_FALSE_CARR = 1 << 3,  /* false carrier */
50f7917c00SJeff Kirsher 	VSC_INTR_MEDIA_CHG = 1 << 4,  /* AMS media change */
51f7917c00SJeff Kirsher 	VSC_INTR_RX_FIFO = 1 << 5,  /* Rx FIFO over/underflow */
52f7917c00SJeff Kirsher 	VSC_INTR_TX_FIFO = 1 << 6,  /* Tx FIFO over/underflow */
53f7917c00SJeff Kirsher 	VSC_INTR_DESCRAMBL = 1 << 7,  /* descrambler lock-lost */
54f7917c00SJeff Kirsher 	VSC_INTR_SYMBOL_ERR = 1 << 8,  /* symbol error */
55f7917c00SJeff Kirsher 	VSC_INTR_NEG_DONE = 1 << 10, /* autoneg done */
56f7917c00SJeff Kirsher 	VSC_INTR_NEG_ERR = 1 << 11, /* autoneg error */
57f7917c00SJeff Kirsher 	VSC_INTR_DPLX_CHG = 1 << 12, /* duplex change */
58f7917c00SJeff Kirsher 	VSC_INTR_LINK_CHG = 1 << 13, /* link change */
59f7917c00SJeff Kirsher 	VSC_INTR_SPD_CHG = 1 << 14, /* speed change */
60f7917c00SJeff Kirsher 	VSC_INTR_ENABLE = 1 << 15, /* interrupt enable */
61f7917c00SJeff Kirsher };
62f7917c00SJeff Kirsher 
63f7917c00SJeff Kirsher enum {
64f7917c00SJeff Kirsher 	VSC_CTRL_CLAUSE37_VIEW = 1 << 4,   /* Switch to Clause 37 view */
65f7917c00SJeff Kirsher 	VSC_CTRL_MEDIA_MODE_HI = 0xf000    /* High part of media mode select */
66f7917c00SJeff Kirsher };
67f7917c00SJeff Kirsher 
68f7917c00SJeff Kirsher #define CFG_CHG_INTR_MASK (VSC_INTR_LINK_CHG | VSC_INTR_NEG_ERR | \
69f7917c00SJeff Kirsher 			   VSC_INTR_DPLX_CHG | VSC_INTR_SPD_CHG | \
70f7917c00SJeff Kirsher 	 		   VSC_INTR_NEG_DONE)
71f7917c00SJeff Kirsher #define INTR_MASK (CFG_CHG_INTR_MASK | VSC_INTR_TX_FIFO | VSC_INTR_RX_FIFO | \
72f7917c00SJeff Kirsher 		   VSC_INTR_ENABLE)
73f7917c00SJeff Kirsher 
74f7917c00SJeff Kirsher /* PHY specific auxiliary control & status register fields */
75f7917c00SJeff Kirsher #define S_ACSR_ACTIPHY_TMR    0
76f7917c00SJeff Kirsher #define M_ACSR_ACTIPHY_TMR    0x3
77f7917c00SJeff Kirsher #define V_ACSR_ACTIPHY_TMR(x) ((x) << S_ACSR_ACTIPHY_TMR)
78f7917c00SJeff Kirsher 
79f7917c00SJeff Kirsher #define S_ACSR_SPEED    3
80f7917c00SJeff Kirsher #define M_ACSR_SPEED    0x3
81f7917c00SJeff Kirsher #define G_ACSR_SPEED(x) (((x) >> S_ACSR_SPEED) & M_ACSR_SPEED)
82f7917c00SJeff Kirsher 
83f7917c00SJeff Kirsher #define S_ACSR_DUPLEX 5
84f7917c00SJeff Kirsher #define F_ACSR_DUPLEX (1 << S_ACSR_DUPLEX)
85f7917c00SJeff Kirsher 
86f7917c00SJeff Kirsher #define S_ACSR_ACTIPHY 6
87f7917c00SJeff Kirsher #define F_ACSR_ACTIPHY (1 << S_ACSR_ACTIPHY)
88f7917c00SJeff Kirsher 
89f7917c00SJeff Kirsher /*
90f7917c00SJeff Kirsher  * Reset the PHY.  This PHY completes reset immediately so we never wait.
91f7917c00SJeff Kirsher  */
vsc8211_reset(struct cphy * cphy,int wait)92f7917c00SJeff Kirsher static int vsc8211_reset(struct cphy *cphy, int wait)
93f7917c00SJeff Kirsher {
94f7917c00SJeff Kirsher 	return t3_phy_reset(cphy, MDIO_DEVAD_NONE, 0);
95f7917c00SJeff Kirsher }
96f7917c00SJeff Kirsher 
vsc8211_intr_enable(struct cphy * cphy)97f7917c00SJeff Kirsher static int vsc8211_intr_enable(struct cphy *cphy)
98f7917c00SJeff Kirsher {
99f7917c00SJeff Kirsher 	return t3_mdio_write(cphy, MDIO_DEVAD_NONE, VSC8211_INTR_ENABLE,
100f7917c00SJeff Kirsher 			     INTR_MASK);
101f7917c00SJeff Kirsher }
102f7917c00SJeff Kirsher 
vsc8211_intr_disable(struct cphy * cphy)103f7917c00SJeff Kirsher static int vsc8211_intr_disable(struct cphy *cphy)
104f7917c00SJeff Kirsher {
105f7917c00SJeff Kirsher 	return t3_mdio_write(cphy, MDIO_DEVAD_NONE, VSC8211_INTR_ENABLE, 0);
106f7917c00SJeff Kirsher }
107f7917c00SJeff Kirsher 
vsc8211_intr_clear(struct cphy * cphy)108f7917c00SJeff Kirsher static int vsc8211_intr_clear(struct cphy *cphy)
109f7917c00SJeff Kirsher {
110f7917c00SJeff Kirsher 	u32 val;
111f7917c00SJeff Kirsher 
112f7917c00SJeff Kirsher 	/* Clear PHY interrupts by reading the register. */
113f7917c00SJeff Kirsher 	return t3_mdio_read(cphy, MDIO_DEVAD_NONE, VSC8211_INTR_STATUS, &val);
114f7917c00SJeff Kirsher }
115f7917c00SJeff Kirsher 
vsc8211_autoneg_enable(struct cphy * cphy)116f7917c00SJeff Kirsher static int vsc8211_autoneg_enable(struct cphy *cphy)
117f7917c00SJeff Kirsher {
118f7917c00SJeff Kirsher 	return t3_mdio_change_bits(cphy, MDIO_DEVAD_NONE, MII_BMCR,
119f7917c00SJeff Kirsher 				   BMCR_PDOWN | BMCR_ISOLATE,
120f7917c00SJeff Kirsher 				   BMCR_ANENABLE | BMCR_ANRESTART);
121f7917c00SJeff Kirsher }
122f7917c00SJeff Kirsher 
vsc8211_autoneg_restart(struct cphy * cphy)123f7917c00SJeff Kirsher static int vsc8211_autoneg_restart(struct cphy *cphy)
124f7917c00SJeff Kirsher {
125f7917c00SJeff Kirsher 	return t3_mdio_change_bits(cphy, MDIO_DEVAD_NONE, MII_BMCR,
126f7917c00SJeff Kirsher 				   BMCR_PDOWN | BMCR_ISOLATE,
127f7917c00SJeff Kirsher 				   BMCR_ANRESTART);
128f7917c00SJeff Kirsher }
129f7917c00SJeff Kirsher 
vsc8211_get_link_status(struct cphy * cphy,int * link_ok,int * speed,int * duplex,int * fc)130f7917c00SJeff Kirsher static int vsc8211_get_link_status(struct cphy *cphy, int *link_ok,
131f7917c00SJeff Kirsher 				   int *speed, int *duplex, int *fc)
132f7917c00SJeff Kirsher {
133f7917c00SJeff Kirsher 	unsigned int bmcr, status, lpa, adv;
134f7917c00SJeff Kirsher 	int err, sp = -1, dplx = -1, pause = 0;
135f7917c00SJeff Kirsher 
136f7917c00SJeff Kirsher 	err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_BMCR, &bmcr);
137f7917c00SJeff Kirsher 	if (!err)
138f7917c00SJeff Kirsher 		err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_BMSR, &status);
139f7917c00SJeff Kirsher 	if (err)
140f7917c00SJeff Kirsher 		return err;
141f7917c00SJeff Kirsher 
142f7917c00SJeff Kirsher 	if (link_ok) {
143f7917c00SJeff Kirsher 		/*
144f7917c00SJeff Kirsher 		 * BMSR_LSTATUS is latch-low, so if it is 0 we need to read it
145f7917c00SJeff Kirsher 		 * once more to get the current link state.
146f7917c00SJeff Kirsher 		 */
147f7917c00SJeff Kirsher 		if (!(status & BMSR_LSTATUS))
148f7917c00SJeff Kirsher 			err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_BMSR,
149f7917c00SJeff Kirsher 					   &status);
150f7917c00SJeff Kirsher 		if (err)
151f7917c00SJeff Kirsher 			return err;
152f7917c00SJeff Kirsher 		*link_ok = (status & BMSR_LSTATUS) != 0;
153f7917c00SJeff Kirsher 	}
154f7917c00SJeff Kirsher 	if (!(bmcr & BMCR_ANENABLE)) {
155f7917c00SJeff Kirsher 		dplx = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
156f7917c00SJeff Kirsher 		if (bmcr & BMCR_SPEED1000)
157f7917c00SJeff Kirsher 			sp = SPEED_1000;
158f7917c00SJeff Kirsher 		else if (bmcr & BMCR_SPEED100)
159f7917c00SJeff Kirsher 			sp = SPEED_100;
160f7917c00SJeff Kirsher 		else
161f7917c00SJeff Kirsher 			sp = SPEED_10;
162f7917c00SJeff Kirsher 	} else if (status & BMSR_ANEGCOMPLETE) {
163f7917c00SJeff Kirsher 		err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, VSC8211_AUX_CTRL_STAT,
164f7917c00SJeff Kirsher 				   &status);
165f7917c00SJeff Kirsher 		if (err)
166f7917c00SJeff Kirsher 			return err;
167f7917c00SJeff Kirsher 
168f7917c00SJeff Kirsher 		dplx = (status & F_ACSR_DUPLEX) ? DUPLEX_FULL : DUPLEX_HALF;
169f7917c00SJeff Kirsher 		sp = G_ACSR_SPEED(status);
170f7917c00SJeff Kirsher 		if (sp == 0)
171f7917c00SJeff Kirsher 			sp = SPEED_10;
172f7917c00SJeff Kirsher 		else if (sp == 1)
173f7917c00SJeff Kirsher 			sp = SPEED_100;
174f7917c00SJeff Kirsher 		else
175f7917c00SJeff Kirsher 			sp = SPEED_1000;
176f7917c00SJeff Kirsher 
177f7917c00SJeff Kirsher 		if (fc && dplx == DUPLEX_FULL) {
178f7917c00SJeff Kirsher 			err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_LPA,
179f7917c00SJeff Kirsher 					   &lpa);
180f7917c00SJeff Kirsher 			if (!err)
181f7917c00SJeff Kirsher 				err = t3_mdio_read(cphy, MDIO_DEVAD_NONE,
182f7917c00SJeff Kirsher 						   MII_ADVERTISE, &adv);
183f7917c00SJeff Kirsher 			if (err)
184f7917c00SJeff Kirsher 				return err;
185f7917c00SJeff Kirsher 
186f7917c00SJeff Kirsher 			if (lpa & adv & ADVERTISE_PAUSE_CAP)
187f7917c00SJeff Kirsher 				pause = PAUSE_RX | PAUSE_TX;
188f7917c00SJeff Kirsher 			else if ((lpa & ADVERTISE_PAUSE_CAP) &&
189f7917c00SJeff Kirsher 				 (lpa & ADVERTISE_PAUSE_ASYM) &&
190f7917c00SJeff Kirsher 				 (adv & ADVERTISE_PAUSE_ASYM))
191f7917c00SJeff Kirsher 				pause = PAUSE_TX;
192f7917c00SJeff Kirsher 			else if ((lpa & ADVERTISE_PAUSE_ASYM) &&
193f7917c00SJeff Kirsher 				 (adv & ADVERTISE_PAUSE_CAP))
194f7917c00SJeff Kirsher 				pause = PAUSE_RX;
195f7917c00SJeff Kirsher 		}
196f7917c00SJeff Kirsher 	}
197f7917c00SJeff Kirsher 	if (speed)
198f7917c00SJeff Kirsher 		*speed = sp;
199f7917c00SJeff Kirsher 	if (duplex)
200f7917c00SJeff Kirsher 		*duplex = dplx;
201f7917c00SJeff Kirsher 	if (fc)
202f7917c00SJeff Kirsher 		*fc = pause;
203f7917c00SJeff Kirsher 	return 0;
204f7917c00SJeff Kirsher }
205f7917c00SJeff Kirsher 
vsc8211_get_link_status_fiber(struct cphy * cphy,int * link_ok,int * speed,int * duplex,int * fc)206f7917c00SJeff Kirsher static int vsc8211_get_link_status_fiber(struct cphy *cphy, int *link_ok,
207f7917c00SJeff Kirsher 					 int *speed, int *duplex, int *fc)
208f7917c00SJeff Kirsher {
209f7917c00SJeff Kirsher 	unsigned int bmcr, status, lpa, adv;
210f7917c00SJeff Kirsher 	int err, sp = -1, dplx = -1, pause = 0;
211f7917c00SJeff Kirsher 
212f7917c00SJeff Kirsher 	err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_BMCR, &bmcr);
213f7917c00SJeff Kirsher 	if (!err)
214f7917c00SJeff Kirsher 		err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_BMSR, &status);
215f7917c00SJeff Kirsher 	if (err)
216f7917c00SJeff Kirsher 		return err;
217f7917c00SJeff Kirsher 
218f7917c00SJeff Kirsher 	if (link_ok) {
219f7917c00SJeff Kirsher 		/*
220f7917c00SJeff Kirsher 		 * BMSR_LSTATUS is latch-low, so if it is 0 we need to read it
221f7917c00SJeff Kirsher 		 * once more to get the current link state.
222f7917c00SJeff Kirsher 		 */
223f7917c00SJeff Kirsher 		if (!(status & BMSR_LSTATUS))
224f7917c00SJeff Kirsher 			err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_BMSR,
225f7917c00SJeff Kirsher 					   &status);
226f7917c00SJeff Kirsher 		if (err)
227f7917c00SJeff Kirsher 			return err;
228f7917c00SJeff Kirsher 		*link_ok = (status & BMSR_LSTATUS) != 0;
229f7917c00SJeff Kirsher 	}
230f7917c00SJeff Kirsher 	if (!(bmcr & BMCR_ANENABLE)) {
231f7917c00SJeff Kirsher 		dplx = (bmcr & BMCR_FULLDPLX) ? DUPLEX_FULL : DUPLEX_HALF;
232f7917c00SJeff Kirsher 		if (bmcr & BMCR_SPEED1000)
233f7917c00SJeff Kirsher 			sp = SPEED_1000;
234f7917c00SJeff Kirsher 		else if (bmcr & BMCR_SPEED100)
235f7917c00SJeff Kirsher 			sp = SPEED_100;
236f7917c00SJeff Kirsher 		else
237f7917c00SJeff Kirsher 			sp = SPEED_10;
238f7917c00SJeff Kirsher 	} else if (status & BMSR_ANEGCOMPLETE) {
239f7917c00SJeff Kirsher 		err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_LPA, &lpa);
240f7917c00SJeff Kirsher 		if (!err)
241f7917c00SJeff Kirsher 			err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, MII_ADVERTISE,
242f7917c00SJeff Kirsher 					   &adv);
243f7917c00SJeff Kirsher 		if (err)
244f7917c00SJeff Kirsher 			return err;
245f7917c00SJeff Kirsher 
246f7917c00SJeff Kirsher 		if (adv & lpa & ADVERTISE_1000XFULL) {
247f7917c00SJeff Kirsher 			dplx = DUPLEX_FULL;
248f7917c00SJeff Kirsher 			sp = SPEED_1000;
249f7917c00SJeff Kirsher 		} else if (adv & lpa & ADVERTISE_1000XHALF) {
250f7917c00SJeff Kirsher 			dplx = DUPLEX_HALF;
251f7917c00SJeff Kirsher 			sp = SPEED_1000;
252f7917c00SJeff Kirsher 		}
253f7917c00SJeff Kirsher 
254f7917c00SJeff Kirsher 		if (fc && dplx == DUPLEX_FULL) {
255f7917c00SJeff Kirsher 			if (lpa & adv & ADVERTISE_1000XPAUSE)
256f7917c00SJeff Kirsher 				pause = PAUSE_RX | PAUSE_TX;
257f7917c00SJeff Kirsher 			else if ((lpa & ADVERTISE_1000XPAUSE) &&
258f7917c00SJeff Kirsher 				 (adv & lpa & ADVERTISE_1000XPSE_ASYM))
259f7917c00SJeff Kirsher 				pause = PAUSE_TX;
260f7917c00SJeff Kirsher 			else if ((lpa & ADVERTISE_1000XPSE_ASYM) &&
261f7917c00SJeff Kirsher 				 (adv & ADVERTISE_1000XPAUSE))
262f7917c00SJeff Kirsher 				pause = PAUSE_RX;
263f7917c00SJeff Kirsher 		}
264f7917c00SJeff Kirsher 	}
265f7917c00SJeff Kirsher 	if (speed)
266f7917c00SJeff Kirsher 		*speed = sp;
267f7917c00SJeff Kirsher 	if (duplex)
268f7917c00SJeff Kirsher 		*duplex = dplx;
269f7917c00SJeff Kirsher 	if (fc)
270f7917c00SJeff Kirsher 		*fc = pause;
271f7917c00SJeff Kirsher 	return 0;
272f7917c00SJeff Kirsher }
273f7917c00SJeff Kirsher 
274f7917c00SJeff Kirsher #ifdef UNUSED
275f7917c00SJeff Kirsher /*
276f7917c00SJeff Kirsher  * Enable/disable auto MDI/MDI-X in forced link speed mode.
277f7917c00SJeff Kirsher  */
vsc8211_set_automdi(struct cphy * phy,int enable)278f7917c00SJeff Kirsher static int vsc8211_set_automdi(struct cphy *phy, int enable)
279f7917c00SJeff Kirsher {
280f7917c00SJeff Kirsher 	int err;
281f7917c00SJeff Kirsher 
282f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_EXT_PAGE_AXS, 0x52b5);
283f7917c00SJeff Kirsher 	if (err)
284f7917c00SJeff Kirsher 		return err;
285f7917c00SJeff Kirsher 
286f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, 18, 0x12);
287f7917c00SJeff Kirsher 	if (err)
288f7917c00SJeff Kirsher 		return err;
289f7917c00SJeff Kirsher 
290f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, 17, enable ? 0x2803 : 0x3003);
291f7917c00SJeff Kirsher 	if (err)
292f7917c00SJeff Kirsher 		return err;
293f7917c00SJeff Kirsher 
294f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, 16, 0x87fa);
295f7917c00SJeff Kirsher 	if (err)
296f7917c00SJeff Kirsher 		return err;
297f7917c00SJeff Kirsher 
298f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_EXT_PAGE_AXS, 0);
299f7917c00SJeff Kirsher 	if (err)
300f7917c00SJeff Kirsher 		return err;
301f7917c00SJeff Kirsher 
302f7917c00SJeff Kirsher 	return 0;
303f7917c00SJeff Kirsher }
304f7917c00SJeff Kirsher 
vsc8211_set_speed_duplex(struct cphy * phy,int speed,int duplex)305f7917c00SJeff Kirsher int vsc8211_set_speed_duplex(struct cphy *phy, int speed, int duplex)
306f7917c00SJeff Kirsher {
307f7917c00SJeff Kirsher 	int err;
308f7917c00SJeff Kirsher 
309f7917c00SJeff Kirsher 	err = t3_set_phy_speed_duplex(phy, speed, duplex);
310f7917c00SJeff Kirsher 	if (!err)
311f7917c00SJeff Kirsher 		err = vsc8211_set_automdi(phy, 1);
312f7917c00SJeff Kirsher 	return err;
313f7917c00SJeff Kirsher }
314f7917c00SJeff Kirsher #endif /* UNUSED */
315f7917c00SJeff Kirsher 
vsc8211_power_down(struct cphy * cphy,int enable)316f7917c00SJeff Kirsher static int vsc8211_power_down(struct cphy *cphy, int enable)
317f7917c00SJeff Kirsher {
318f7917c00SJeff Kirsher 	return t3_mdio_change_bits(cphy, 0, MII_BMCR, BMCR_PDOWN,
319f7917c00SJeff Kirsher 				   enable ? BMCR_PDOWN : 0);
320f7917c00SJeff Kirsher }
321f7917c00SJeff Kirsher 
vsc8211_intr_handler(struct cphy * cphy)322f7917c00SJeff Kirsher static int vsc8211_intr_handler(struct cphy *cphy)
323f7917c00SJeff Kirsher {
324f7917c00SJeff Kirsher 	unsigned int cause;
325f7917c00SJeff Kirsher 	int err, cphy_cause = 0;
326f7917c00SJeff Kirsher 
327f7917c00SJeff Kirsher 	err = t3_mdio_read(cphy, MDIO_DEVAD_NONE, VSC8211_INTR_STATUS, &cause);
328f7917c00SJeff Kirsher 	if (err)
329f7917c00SJeff Kirsher 		return err;
330f7917c00SJeff Kirsher 
331f7917c00SJeff Kirsher 	cause &= INTR_MASK;
332f7917c00SJeff Kirsher 	if (cause & CFG_CHG_INTR_MASK)
333f7917c00SJeff Kirsher 		cphy_cause |= cphy_cause_link_change;
334f7917c00SJeff Kirsher 	if (cause & (VSC_INTR_RX_FIFO | VSC_INTR_TX_FIFO))
335f7917c00SJeff Kirsher 		cphy_cause |= cphy_cause_fifo_error;
336f7917c00SJeff Kirsher 	return cphy_cause;
337f7917c00SJeff Kirsher }
338f7917c00SJeff Kirsher 
33946f85a92SJulia Lawall static const struct cphy_ops vsc8211_ops = {
340f7917c00SJeff Kirsher 	.reset = vsc8211_reset,
341f7917c00SJeff Kirsher 	.intr_enable = vsc8211_intr_enable,
342f7917c00SJeff Kirsher 	.intr_disable = vsc8211_intr_disable,
343f7917c00SJeff Kirsher 	.intr_clear = vsc8211_intr_clear,
344f7917c00SJeff Kirsher 	.intr_handler = vsc8211_intr_handler,
345f7917c00SJeff Kirsher 	.autoneg_enable = vsc8211_autoneg_enable,
346f7917c00SJeff Kirsher 	.autoneg_restart = vsc8211_autoneg_restart,
347f7917c00SJeff Kirsher 	.advertise = t3_phy_advertise,
348f7917c00SJeff Kirsher 	.set_speed_duplex = t3_set_phy_speed_duplex,
349f7917c00SJeff Kirsher 	.get_link_status = vsc8211_get_link_status,
350f7917c00SJeff Kirsher 	.power_down = vsc8211_power_down,
351f7917c00SJeff Kirsher };
352f7917c00SJeff Kirsher 
35346f85a92SJulia Lawall static const struct cphy_ops vsc8211_fiber_ops = {
354f7917c00SJeff Kirsher 	.reset = vsc8211_reset,
355f7917c00SJeff Kirsher 	.intr_enable = vsc8211_intr_enable,
356f7917c00SJeff Kirsher 	.intr_disable = vsc8211_intr_disable,
357f7917c00SJeff Kirsher 	.intr_clear = vsc8211_intr_clear,
358f7917c00SJeff Kirsher 	.intr_handler = vsc8211_intr_handler,
359f7917c00SJeff Kirsher 	.autoneg_enable = vsc8211_autoneg_enable,
360f7917c00SJeff Kirsher 	.autoneg_restart = vsc8211_autoneg_restart,
361f7917c00SJeff Kirsher 	.advertise = t3_phy_advertise_fiber,
362f7917c00SJeff Kirsher 	.set_speed_duplex = t3_set_phy_speed_duplex,
363f7917c00SJeff Kirsher 	.get_link_status = vsc8211_get_link_status_fiber,
364f7917c00SJeff Kirsher 	.power_down = vsc8211_power_down,
365f7917c00SJeff Kirsher };
366f7917c00SJeff Kirsher 
t3_vsc8211_phy_prep(struct cphy * phy,struct adapter * adapter,int phy_addr,const struct mdio_ops * mdio_ops)367f7917c00SJeff Kirsher int t3_vsc8211_phy_prep(struct cphy *phy, struct adapter *adapter,
368f7917c00SJeff Kirsher 			int phy_addr, const struct mdio_ops *mdio_ops)
369f7917c00SJeff Kirsher {
370f7917c00SJeff Kirsher 	int err;
371f7917c00SJeff Kirsher 	unsigned int val;
372f7917c00SJeff Kirsher 
373f7917c00SJeff Kirsher 	cphy_init(phy, adapter, phy_addr, &vsc8211_ops, mdio_ops,
374f7917c00SJeff Kirsher 		  SUPPORTED_10baseT_Full | SUPPORTED_100baseT_Full |
375f7917c00SJeff Kirsher 		  SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg | SUPPORTED_MII |
376f7917c00SJeff Kirsher 		  SUPPORTED_TP | SUPPORTED_IRQ, "10/100/1000BASE-T");
377f7917c00SJeff Kirsher 	msleep(20);       /* PHY needs ~10ms to start responding to MDIO */
378f7917c00SJeff Kirsher 
379f7917c00SJeff Kirsher 	err = t3_mdio_read(phy, MDIO_DEVAD_NONE, VSC8211_EXT_CTRL, &val);
380f7917c00SJeff Kirsher 	if (err)
381f7917c00SJeff Kirsher 		return err;
382f7917c00SJeff Kirsher 	if (val & VSC_CTRL_MEDIA_MODE_HI) {
383f7917c00SJeff Kirsher 		/* copper interface, just need to configure the LEDs */
384f7917c00SJeff Kirsher 		return t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_LED_CTRL,
385f7917c00SJeff Kirsher 				     0x100);
386f7917c00SJeff Kirsher 	}
387f7917c00SJeff Kirsher 
388f7917c00SJeff Kirsher 	phy->caps = SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
389f7917c00SJeff Kirsher 		    SUPPORTED_MII | SUPPORTED_FIBRE | SUPPORTED_IRQ;
390f7917c00SJeff Kirsher 	phy->desc = "1000BASE-X";
391f7917c00SJeff Kirsher 	phy->ops = &vsc8211_fiber_ops;
392f7917c00SJeff Kirsher 
393f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_EXT_PAGE_AXS, 1);
394f7917c00SJeff Kirsher 	if (err)
395f7917c00SJeff Kirsher 		return err;
396f7917c00SJeff Kirsher 
397f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_SIGDET_CTRL, 1);
398f7917c00SJeff Kirsher 	if (err)
399f7917c00SJeff Kirsher 		return err;
400f7917c00SJeff Kirsher 
401f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_EXT_PAGE_AXS, 0);
402f7917c00SJeff Kirsher 	if (err)
403f7917c00SJeff Kirsher 		return err;
404f7917c00SJeff Kirsher 
405f7917c00SJeff Kirsher 	err = t3_mdio_write(phy, MDIO_DEVAD_NONE, VSC8211_EXT_CTRL,
406f7917c00SJeff Kirsher 			    val | VSC_CTRL_CLAUSE37_VIEW);
407f7917c00SJeff Kirsher 	if (err)
408f7917c00SJeff Kirsher 		return err;
409f7917c00SJeff Kirsher 
410f7917c00SJeff Kirsher 	err = vsc8211_reset(phy, 0);
411f7917c00SJeff Kirsher 	if (err)
412f7917c00SJeff Kirsher 		return err;
413f7917c00SJeff Kirsher 
414f7917c00SJeff Kirsher 	udelay(5); /* delay after reset before next SMI */
415f7917c00SJeff Kirsher 	return 0;
416f7917c00SJeff Kirsher }
417