1 /*
2  *
3  * (C) Copyright 2003
4  * Author : Hamid Ikdoumi (Atmel)
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24 
25 /*
26  * Adapted for KwikByte KB920x board: 22APR2005
27  */
28 
29 #include <common.h>
30 #include <at91rm9200_net.h>
31 #include <net.h>
32 #include <miiphy.h>
33 #include <lxt971a.h>
34 
35 #ifdef CONFIG_DRIVER_ETHER
36 
37 #if defined(CONFIG_CMD_NET)
38 
39 /*
40  * Name:
41  *	lxt972_IsPhyConnected
42  * Description:
43  *	Reads the 2 PHY ID registers
44  * Arguments:
45  *	p_mac - pointer to AT91S_EMAC struct
46  * Return value:
47  *	TRUE - if id read successfully
48  *	FALSE- if error
49  */
lxt972_IsPhyConnected(AT91PS_EMAC p_mac)50 unsigned int lxt972_IsPhyConnected (AT91PS_EMAC p_mac)
51 {
52 	unsigned short Id1, Id2;
53 
54 	at91rm9200_EmacEnableMDIO (p_mac);
55 	at91rm9200_EmacReadPhy(p_mac, PHY_PHYIDR1, &Id1);
56 	at91rm9200_EmacReadPhy(p_mac, PHY_PHYIDR2, &Id2);
57 	at91rm9200_EmacDisableMDIO (p_mac);
58 
59 	if ((Id1 == (0x0013)) && ((Id2  & 0xFFF0) == 0x78E0))
60 		return TRUE;
61 
62 	return FALSE;
63 }
64 
65 /*
66  * Name:
67  *	lxt972_GetLinkSpeed
68  * Description:
69  *	Link parallel detection status of MAC is checked and set in the
70  *	MAC configuration registers
71  * Arguments:
72  *	p_mac - pointer to MAC
73  * Return value:
74  *	TRUE - if link status set succesfully
75  *	FALSE - if link status not set
76  */
lxt972_GetLinkSpeed(AT91PS_EMAC p_mac)77 UCHAR lxt972_GetLinkSpeed (AT91PS_EMAC p_mac)
78 {
79 	unsigned short stat1;
80 
81 	if (!at91rm9200_EmacReadPhy (p_mac, PHY_LXT971_STAT2, &stat1))
82 		return FALSE;
83 
84 	if (!(stat1 & PHY_LXT971_STAT2_LINK))	/* link status up? */
85 		return FALSE;
86 
87 	if (stat1 & PHY_LXT971_STAT2_100BTX) {
88 
89 		if (stat1 & PHY_LXT971_STAT2_DUPLEX_MODE) {
90 
91 			/*set Emac for 100BaseTX and Full Duplex  */
92 			p_mac->EMAC_CFG |= AT91C_EMAC_SPD | AT91C_EMAC_FD;
93 		} else {
94 
95 			/*set Emac for 100BaseTX and Half Duplex  */
96 			p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
97 					~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
98 					| AT91C_EMAC_SPD;
99 		}
100 
101 		return TRUE;
102 
103 	} else {
104 
105 		if (stat1 & PHY_LXT971_STAT2_DUPLEX_MODE) {
106 
107 			/*set MII for 10BaseT and Full Duplex  */
108 			p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
109 					~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
110 					| AT91C_EMAC_FD;
111 		} else {
112 
113 			/*set MII for 10BaseT and Half Duplex  */
114 			p_mac->EMAC_CFG &= ~(AT91C_EMAC_SPD | AT91C_EMAC_FD);
115 		}
116 
117 		return TRUE;
118 	}
119 
120 	return FALSE;
121 }
122 
123 
124 /*
125  * Name:
126  *	lxt972_InitPhy
127  * Description:
128  *	MAC starts checking its link by using parallel detection and
129  *	Autonegotiation and the same is set in the MAC configuration registers
130  * Arguments:
131  *	p_mac - pointer to struct AT91S_EMAC
132  * Return value:
133  *	TRUE - if link status set succesfully
134  *	FALSE - if link status not set
135  */
lxt972_InitPhy(AT91PS_EMAC p_mac)136 UCHAR lxt972_InitPhy (AT91PS_EMAC p_mac)
137 {
138 	UCHAR ret = TRUE;
139 
140 	at91rm9200_EmacEnableMDIO (p_mac);
141 
142 	if (!lxt972_GetLinkSpeed (p_mac)) {
143 		/* Try another time */
144 		ret = lxt972_GetLinkSpeed (p_mac);
145 	}
146 
147 	/* Disable PHY Interrupts */
148 	at91rm9200_EmacWritePhy (p_mac, PHY_LXT971_INT_ENABLE, 0);
149 
150 	at91rm9200_EmacDisableMDIO (p_mac);
151 
152 	return (ret);
153 }
154 
155 
156 /*
157  * Name:
158  *	lxt972_AutoNegotiate
159  * Description:
160  *	MAC Autonegotiates with the partner status of same is set in the
161  *	MAC configuration registers
162  * Arguments:
163  *	dev - pointer to struct net_device
164  * Return value:
165  *	TRUE - if link status set successfully
166  *	FALSE - if link status not set
167  */
lxt972_AutoNegotiate(AT91PS_EMAC p_mac,int * status)168 UCHAR lxt972_AutoNegotiate (AT91PS_EMAC p_mac, int *status)
169 {
170 	unsigned short value;
171 
172 	/* Set lxt972 control register */
173 	if (!at91rm9200_EmacReadPhy (p_mac, PHY_BMCR, &value))
174 		return FALSE;
175 
176 	/* Restart Auto_negotiation  */
177 	value |= PHY_BMCR_RST_NEG;
178 	if (!at91rm9200_EmacWritePhy (p_mac, PHY_BMCR, &value))
179 		return FALSE;
180 
181 	/*check AutoNegotiate complete */
182 	udelay (10000);
183 	at91rm9200_EmacReadPhy(p_mac, PHY_BMSR, &value);
184 	if (!(value & PHY_BMSR_AUTN_COMP))
185 		return FALSE;
186 
187 	return (lxt972_GetLinkSpeed (p_mac));
188 }
189 
190 #endif
191 
192 #endif	/* CONFIG_DRIVER_ETHER */
193