blob: 6d4384f41a0a57c9b7192d6d0a6b0a0a630dbd97 [file] [log] [blame]
wdenk429168e2004-08-02 23:39:03 +00001/*
2 * (C) Copyright 2003
3 * Author : Hamid Ikdoumi (Atmel)
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
24#include <at91rm9200_net.h>
25#include <net.h>
wdenk429168e2004-08-02 23:39:03 +000026#ifdef CONFIG_DRIVER_ETHER
Jens Scharsigc041e9d2010-01-23 12:03:45 +010027#include <dm9161.h>
wdenk429168e2004-08-02 23:39:03 +000028
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -050029#if defined(CONFIG_CMD_NET)
wdenk429168e2004-08-02 23:39:03 +000030
31/*
32 * Name:
33 * dm9161_IsPhyConnected
34 * Description:
35 * Reads the 2 PHY ID registers
36 * Arguments:
37 * p_mac - pointer to AT91S_EMAC struct
38 * Return value:
39 * TRUE - if id read successfully
40 * FALSE- if error
41 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +020042unsigned int dm9161_IsPhyConnected (AT91PS_EMAC p_mac)
wdenk429168e2004-08-02 23:39:03 +000043{
44 unsigned short Id1, Id2;
45
46 at91rm9200_EmacEnableMDIO (p_mac);
47 at91rm9200_EmacReadPhy (p_mac, DM9161_PHYID1, &Id1);
48 at91rm9200_EmacReadPhy (p_mac, DM9161_PHYID2, &Id2);
49 at91rm9200_EmacDisableMDIO (p_mac);
50
51 if ((Id1 == (DM9161_PHYID1_OUI >> 6)) &&
52 ((Id2 >> 10) == (DM9161_PHYID1_OUI & DM9161_LSB_MASK)))
53 return TRUE;
54
55 return FALSE;
56}
57
58/*
59 * Name:
60 * dm9161_GetLinkSpeed
61 * Description:
62 * Link parallel detection status of MAC is checked and set in the
63 * MAC configuration registers
64 * Arguments:
65 * p_mac - pointer to MAC
66 * Return value:
67 * TRUE - if link status set succesfully
68 * FALSE - if link status not set
69 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +020070UCHAR dm9161_GetLinkSpeed (AT91PS_EMAC p_mac)
wdenk429168e2004-08-02 23:39:03 +000071{
72 unsigned short stat1, stat2;
73
74 if (!at91rm9200_EmacReadPhy (p_mac, DM9161_BMSR, &stat1))
75 return FALSE;
76
77 if (!(stat1 & DM9161_LINK_STATUS)) /* link status up? */
78 return FALSE;
79
80 if (!at91rm9200_EmacReadPhy (p_mac, DM9161_DSCSR, &stat2))
81 return FALSE;
82
83 if ((stat1 & DM9161_100BASE_TX_FD) && (stat2 & DM9161_100FDX)) {
84 /*set Emac for 100BaseTX and Full Duplex */
85 p_mac->EMAC_CFG |= AT91C_EMAC_SPD | AT91C_EMAC_FD;
86 return TRUE;
87 }
88
89 if ((stat1 & DM9161_10BASE_T_FD) && (stat2 & DM9161_10FDX)) {
90 /*set MII for 10BaseT and Full Duplex */
91 p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
92 ~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
93 | AT91C_EMAC_FD;
94 return TRUE;
95 }
96
Peter Pearsed4fc6012007-08-14 10:10:52 +010097 if ((stat1 & DM9161_100BASE_TX_HD) && (stat2 & DM9161_100HDX)) {
wdenk429168e2004-08-02 23:39:03 +000098 /*set MII for 100BaseTX and Half Duplex */
99 p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
100 ~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
101 | AT91C_EMAC_SPD;
102 return TRUE;
103 }
104
105 if ((stat1 & DM9161_10BASE_T_HD) && (stat2 & DM9161_10HDX)) {
106 /*set MII for 10BaseT and Half Duplex */
107 p_mac->EMAC_CFG &= ~(AT91C_EMAC_SPD | AT91C_EMAC_FD);
108 return TRUE;
109 }
110 return FALSE;
111}
112
113
114/*
115 * Name:
116 * dm9161_InitPhy
117 * Description:
118 * MAC starts checking its link by using parallel detection and
119 * Autonegotiation and the same is set in the MAC configuration registers
120 * Arguments:
121 * p_mac - pointer to struct AT91S_EMAC
122 * Return value:
123 * TRUE - if link status set succesfully
124 * FALSE - if link status not set
125 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200126UCHAR dm9161_InitPhy (AT91PS_EMAC p_mac)
wdenk429168e2004-08-02 23:39:03 +0000127{
128 UCHAR ret = TRUE;
129 unsigned short IntValue;
130
131 at91rm9200_EmacEnableMDIO (p_mac);
132
133 if (!dm9161_GetLinkSpeed (p_mac)) {
134 /* Try another time */
135 ret = dm9161_GetLinkSpeed (p_mac);
136 }
137
138 /* Disable PHY Interrupts */
139 at91rm9200_EmacReadPhy (p_mac, DM9161_MDINTR, &IntValue);
Wolfgang Denkfef636b2005-10-05 01:54:04 +0200140 /* set FDX, SPD, Link, INTR masks */
141 IntValue |= (DM9161_FDX_MASK | DM9161_SPD_MASK |
Peter Pearsed4fc6012007-08-14 10:10:52 +0100142 DM9161_LINK_MASK | DM9161_INTR_MASK);
wdenk429168e2004-08-02 23:39:03 +0000143 at91rm9200_EmacWritePhy (p_mac, DM9161_MDINTR, &IntValue);
144 at91rm9200_EmacDisableMDIO (p_mac);
145
146 return (ret);
147}
148
149
150/*
151 * Name:
152 * dm9161_AutoNegotiate
153 * Description:
154 * MAC Autonegotiates with the partner status of same is set in the
155 * MAC configuration registers
156 * Arguments:
157 * dev - pointer to struct net_device
158 * Return value:
159 * TRUE - if link status set successfully
160 * FALSE - if link status not set
161 */
Wolfgang Denk080bdb72005-10-05 01:51:29 +0200162UCHAR dm9161_AutoNegotiate (AT91PS_EMAC p_mac, int *status)
wdenk429168e2004-08-02 23:39:03 +0000163{
164 unsigned short value;
165 unsigned short PhyAnar;
166 unsigned short PhyAnalpar;
167
168 /* Set dm9161 control register */
169 if (!at91rm9200_EmacReadPhy (p_mac, DM9161_BMCR, &value))
170 return FALSE;
171 value &= ~DM9161_AUTONEG; /* remove autonegotiation enable */
172 value |= DM9161_ISOLATE; /* Electrically isolate PHY */
173 if (!at91rm9200_EmacWritePhy (p_mac, DM9161_BMCR, &value))
174 return FALSE;
175
Peter Pearsed4fc6012007-08-14 10:10:52 +0100176 /* Set the Auto_negotiation Advertisement Register */
177 /* MII advertising for Next page, 100BaseTxFD and HD, */
178 /* 10BaseTFD and HD, IEEE 802.3 */
wdenk429168e2004-08-02 23:39:03 +0000179 PhyAnar = DM9161_NP | DM9161_TX_FDX | DM9161_TX_HDX |
Peter Pearsed4fc6012007-08-14 10:10:52 +0100180 DM9161_10_FDX | DM9161_10_HDX | DM9161_AN_IEEE_802_3;
wdenk429168e2004-08-02 23:39:03 +0000181 if (!at91rm9200_EmacWritePhy (p_mac, DM9161_ANAR, &PhyAnar))
182 return FALSE;
183
184 /* Read the Control Register */
185 if (!at91rm9200_EmacReadPhy (p_mac, DM9161_BMCR, &value))
186 return FALSE;
187
188 value |= DM9161_SPEED_SELECT | DM9161_AUTONEG | DM9161_DUPLEX_MODE;
189 if (!at91rm9200_EmacWritePhy (p_mac, DM9161_BMCR, &value))
190 return FALSE;
191 /* Restart Auto_negotiation */
192 value |= DM9161_RESTART_AUTONEG;
Wolfgang Denkfef636b2005-10-05 01:54:04 +0200193 value &= ~DM9161_ISOLATE;
wdenk429168e2004-08-02 23:39:03 +0000194 if (!at91rm9200_EmacWritePhy (p_mac, DM9161_BMCR, &value))
195 return FALSE;
196
197 /*check AutoNegotiate complete */
198 udelay (10000);
199 at91rm9200_EmacReadPhy (p_mac, DM9161_BMSR, &value);
200 if (!(value & DM9161_AUTONEG_COMP))
201 return FALSE;
202
203 /* Get the AutoNeg Link partner base page */
204 if (!at91rm9200_EmacReadPhy (p_mac, DM9161_ANLPAR, &PhyAnalpar))
205 return FALSE;
206
207 if ((PhyAnar & DM9161_TX_FDX) && (PhyAnalpar & DM9161_TX_FDX)) {
208 /*set MII for 100BaseTX and Full Duplex */
209 p_mac->EMAC_CFG |= AT91C_EMAC_SPD | AT91C_EMAC_FD;
210 return TRUE;
211 }
212
213 if ((PhyAnar & DM9161_10_FDX) && (PhyAnalpar & DM9161_10_FDX)) {
214 /*set MII for 10BaseT and Full Duplex */
215 p_mac->EMAC_CFG = (p_mac->EMAC_CFG &
216 ~(AT91C_EMAC_SPD | AT91C_EMAC_FD))
217 | AT91C_EMAC_FD;
218 return TRUE;
219 }
220 return FALSE;
221}
222
Jon Loeliger3a1ed1e2007-07-09 18:57:22 -0500223#endif
wdenk429168e2004-08-02 23:39:03 +0000224
225#endif /* CONFIG_DRIVER_ETHER */