ar8035_driver.c
Go to the documentation of this file.
1 /**
2  * @file ar8035_driver.c
3  * @brief AR8035 Gigabit Ethernet PHY transceiver
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL NIC_TRACE_LEVEL
31 
32 //Dependencies
33 #include "core/net.h"
35 #include "debug.h"
36 
37 
38 /**
39  * @brief AR8035 Ethernet PHY driver
40  **/
41 
43 {
44  ar8035Init,
45  ar8035Tick,
49 };
50 
51 
52 /**
53  * @brief AR8035 PHY transceiver initialization
54  * @param[in] interface Underlying network interface
55  * @return Error code
56  **/
57 
59 {
60  //Debug message
61  TRACE_INFO("Initializing AR8035...\r\n");
62 
63  //Initialize external interrupt line driver
64  if(interface->extIntDriver != NULL)
65  interface->extIntDriver->init();
66 
67  //Reset PHY transceiver
69  //Wait for the reset to complete
70  while(ar8035ReadPhyReg(interface, AR8035_PHY_REG_BMCR) & BMCR_RESET);
71 
72  //Basic mode control register
75 
76  //Auto-negotiation advertisement register
80 
81  //1000 BASE-T control register
84 
85  //Function control register
89 
90  //Dump PHY registers for debugging purpose
91  ar8035DumpPhyReg(interface);
92 
93  //The PHY will generate interrupts when link status changes are detected
96 
97  //Force the TCP/IP stack to poll the link state at startup
98  interface->phyEvent = TRUE;
99  //Notify the TCP/IP stack of the event
101 
102  //Successful initialization
103  return NO_ERROR;
104 }
105 
106 
107 /**
108  * @brief AR8035 timer handler
109  * @param[in] interface Underlying network interface
110  **/
111 
112 void ar8035Tick(NetInterface *interface)
113 {
114  uint16_t value;
115  bool_t linkState;
116 
117  //No external interrupt line driver?
118  if(interface->extIntDriver == NULL)
119  {
120  //Read basic status register
122  //Retrieve current link state
123  linkState = (value & BMSR_LINK_STATUS) ? TRUE : FALSE;
124 
125  //Link up event?
126  if(linkState && !interface->linkState)
127  {
128  //Set event flag
129  interface->phyEvent = TRUE;
130  //Notify the TCP/IP stack of the event
132  }
133  //Link down event?
134  else if(!linkState && interface->linkState)
135  {
136  //Set event flag
137  interface->phyEvent = TRUE;
138  //Notify the TCP/IP stack of the event
140  }
141  }
142 }
143 
144 
145 /**
146  * @brief Enable interrupts
147  * @param[in] interface Underlying network interface
148  **/
149 
151 {
152  //Enable PHY transceiver interrupts
153  if(interface->extIntDriver != NULL)
154  interface->extIntDriver->enableIrq();
155 }
156 
157 
158 /**
159  * @brief Disable interrupts
160  * @param[in] interface Underlying network interface
161  **/
162 
164 {
165  //Disable PHY transceiver interrupts
166  if(interface->extIntDriver != NULL)
167  interface->extIntDriver->disableIrq();
168 }
169 
170 
171 /**
172  * @brief AR8035 event handler
173  * @param[in] interface Underlying network interface
174  **/
175 
177 {
178  uint16_t status;
179 
180  //Read status register to acknowledge the interrupt
181  status = ar8035ReadPhyReg(interface, AR8035_PHY_REG_INT_STATUS);
182 
183  //Link status change?
185  {
186  //Read PHY status register
187  status = ar8035ReadPhyReg(interface, AR8035_PHY_REG_PHY_STATUS);
188 
189  //Link is up?
190  if(status & PHY_STATUS_LINK)
191  {
192  //Check current speed
193  switch(status & PHY_STATUS_SPEED_MASK)
194  {
195  //10BASE-T
196  case PHY_STATUS_SPEED_10:
197  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
198  break;
199  //100BASE-TX
201  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
202  break;
203  //1000BASE-T
205  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
206  break;
207  //Unknown speed
208  default:
209  //Debug message
210  TRACE_WARNING("Invalid speed\r\n");
211  break;
212  }
213 
214  //Check current duplex mode
215  if(status & PHY_STATUS_DUPLEX)
216  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
217  else
218  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
219 
220  //Update link state
221  interface->linkState = TRUE;
222 
223  //Adjust MAC configuration parameters for proper operation
224  interface->nicDriver->updateMacConfig(interface);
225  }
226  else
227  {
228  //Update link state
229  interface->linkState = FALSE;
230  }
231 
232  //Process link state change event
233  nicNotifyLinkChange(interface);
234  }
235 }
236 
237 
238 /**
239  * @brief Write PHY register
240  * @param[in] interface Underlying network interface
241  * @param[in] address PHY register address
242  * @param[in] data Register value
243  **/
244 
245 void ar8035WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
246 {
247  uint8_t phyAddr;
248 
249  //Get the address of the PHY transceiver
250  if(interface->phyAddr < 32)
251  phyAddr = interface->phyAddr;
252  else
253  phyAddr = AR8035_PHY_ADDR;
254 
255  //Write the specified PHY register
256  interface->nicDriver->writePhyReg(phyAddr, address, data);
257 }
258 
259 
260 /**
261  * @brief Read PHY register
262  * @param[in] interface Underlying network interface
263  * @param[in] address PHY register address
264  * @return Register value
265  **/
266 
267 uint16_t ar8035ReadPhyReg(NetInterface *interface, uint8_t address)
268 {
269  uint8_t phyAddr;
270 
271  //Get the address of the PHY transceiver
272  if(interface->phyAddr < 32)
273  phyAddr = interface->phyAddr;
274  else
275  phyAddr = AR8035_PHY_ADDR;
276 
277  //Read the specified PHY register
278  return interface->nicDriver->readPhyReg(phyAddr, address);
279 }
280 
281 
282 /**
283  * @brief Dump PHY registers for debugging purpose
284  * @param[in] interface Underlying network interface
285  **/
286 
288 {
289  uint8_t i;
290 
291  //Loop through PHY registers
292  for(i = 0; i < 32; i++)
293  {
294  //Display current PHY register
295  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, ar8035ReadPhyReg(interface, i));
296  }
297 
298  //Terminate with a line feed
299  TRACE_DEBUG("\r\n");
300 }
void ar8035WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
#define _1000BT_CTRL_1000BT_FD
#define ANAR_10BT_FD
#define AR8035_PHY_REG_ANAR
Definition: ar8035_driver.h:47
#define ANAR_SELECTOR0
TCP/IP stack core.
#define AR8035_PHY_REG_INT_EN
Definition: ar8035_driver.h:59
Debugging facilities.
#define INT_STATUS_LINK_FAIL
#define AR8035_PHY_REG_BMSR
Definition: ar8035_driver.h:44
uint16_t ar8035ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define BMSR_LINK_STATUS
Definition: ar8031_driver.h:95
#define ANAR_PAUSE
#define PHY_STATUS_SPEED_1000
const PhyDriver ar8035PhyDriver
AR8035 Ethernet PHY driver.
Definition: ar8035_driver.c:42
#define ANAR_ASYMMETRIC_PAUSE
void ar8035DisableIrq(NetInterface *interface)
Disable interrupts.
void ar8035Tick(NetInterface *interface)
AR8035 timer handler.
#define PHY_STATUS_SPEED_MASK
#define TRUE
Definition: os_port.h:48
#define FUNCTION_MDI_CROSSOVER_MODE0
#define AR8035_PHY_ADDR
Definition: ar8035_driver.h:37
#define ANAR_10BT_HD
PHY driver.
Definition: nic.h:196
#define AR8035_PHY_REG_1000BT_CTRL
Definition: ar8035_driver.h:52
error_t ar8035Init(NetInterface *interface)
AR8035 PHY transceiver initialization.
Definition: ar8035_driver.c:58
#define PHY_STATUS_DUPLEX
#define FUNCTION_ASSERT_CRS_ON_TX
#define BMCR_RESET
Definition: ar8031_driver.h:71
AR8035 Gigabit Ethernet PHY transceiver.
#define AR8035_PHY_REG_BMCR
Definition: ar8035_driver.h:43
#define PHY_STATUS_SPEED_10
#define TRACE_INFO(...)
Definition: debug.h:86
Success.
Definition: error.h:42
Ipv6Addr address
OsEvent netEvent
Definition: net.c:72
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t
Error codes.
Definition: error.h:40
#define TRACE_WARNING(...)
Definition: debug.h:78
#define ANAR_100BTX_FD
#define ANAR_XNP_ABLE
#define AR8035_PHY_REG_INT_STATUS
Definition: ar8035_driver.h:60
#define PHY_STATUS_LINK
void ar8035DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
void ar8035EventHandler(NetInterface *interface)
AR8035 event handler.
#define BMCR_AN_EN
#define ANAR_100BTX_HD
uint8_t value[]
Definition: dtls_misc.h:141
#define AR8035_PHY_REG_FUNCTION_CTRL
Definition: ar8035_driver.h:57
void ar8035EnableIrq(NetInterface *interface)
Enable interrupts.
#define INT_STATUS_LINK_SUCCESS
#define BMCR_DUPLEX_MODE
#define FUNCTION_MDI_CROSSOVER_MODE1
#define BMCR_SPEED_SEL_LSB
Definition: ar8031_driver.h:73
#define AR8035_PHY_REG_PHY_STATUS
Definition: ar8035_driver.h:58
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
#define PHY_STATUS_SPEED_100
#define FUNCTION_POLARITY_REVERSAL
#define TRACE_DEBUG(...)
Definition: debug.h:98