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  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 1.9.6
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL NIC_TRACE_LEVEL
33 
34 //Dependencies
35 #include "core/net.h"
37 #include "debug.h"
38 
39 
40 /**
41  * @brief AR8035 Ethernet PHY driver
42  **/
43 
45 {
46  ar8035Init,
47  ar8035Tick,
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief AR8035 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing AR8035...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = AR8035_PHY_ADDR;
72  }
73 
74  //Initialize external interrupt line driver
75  if(interface->extIntDriver != NULL)
76  {
77  interface->extIntDriver->init();
78  }
79 
80  //Reset PHY transceiver
82 
83  //Wait for the reset to complete
85  {
86  }
87 
88  //Basic mode control register
91 
92  //Auto-negotiation advertisement register
97 
98  //1000 BASE-T control register
100 
101  //Function control register
105 
106  //Dump PHY registers for debugging purpose
107  ar8035DumpPhyReg(interface);
108 
109  //The PHY will generate interrupts when link status changes are detected
112 
113  //Force the TCP/IP stack to poll the link state at startup
114  interface->phyEvent = TRUE;
115  //Notify the TCP/IP stack of the event
117 
118  //Successful initialization
119  return NO_ERROR;
120 }
121 
122 
123 /**
124  * @brief AR8035 timer handler
125  * @param[in] interface Underlying network interface
126  **/
127 
128 void ar8035Tick(NetInterface *interface)
129 {
130  uint16_t value;
131  bool_t linkState;
132 
133  //No external interrupt line driver?
134  if(interface->extIntDriver == NULL)
135  {
136  //Read basic status register
137  value = ar8035ReadPhyReg(interface, AR8035_BMSR);
138  //Retrieve current link state
139  linkState = (value & AR8035_BMSR_LINK_STATUS) ? TRUE : FALSE;
140 
141  //Link up event?
142  if(linkState && !interface->linkState)
143  {
144  //Set event flag
145  interface->phyEvent = TRUE;
146  //Notify the TCP/IP stack of the event
148  }
149  //Link down event?
150  else if(!linkState && interface->linkState)
151  {
152  //Set event flag
153  interface->phyEvent = TRUE;
154  //Notify the TCP/IP stack of the event
156  }
157  }
158 }
159 
160 
161 /**
162  * @brief Enable interrupts
163  * @param[in] interface Underlying network interface
164  **/
165 
167 {
168  //Enable PHY transceiver interrupts
169  if(interface->extIntDriver != NULL)
170  {
171  interface->extIntDriver->enableIrq();
172  }
173 }
174 
175 
176 /**
177  * @brief Disable interrupts
178  * @param[in] interface Underlying network interface
179  **/
180 
182 {
183  //Disable PHY transceiver interrupts
184  if(interface->extIntDriver != NULL)
185  {
186  interface->extIntDriver->disableIrq();
187  }
188 }
189 
190 
191 /**
192  * @brief AR8035 event handler
193  * @param[in] interface Underlying network interface
194  **/
195 
197 {
198  uint16_t status;
199 
200  //Read status register to acknowledge the interrupt
201  status = ar8035ReadPhyReg(interface, AR8035_INT_STATUS);
202 
203  //Link status change?
205  {
206  //Read PHY status register
207  status = ar8035ReadPhyReg(interface, AR8035_PHY_STATUS);
208 
209  //Link is up?
210  if(status & AR8035_PHY_STATUS_LINK)
211  {
212  //Check current speed
213  switch(status & AR8035_PHY_STATUS_SPEED)
214  {
215  //10BASE-T
217  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
218  break;
219  //100BASE-TX
221  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
222  break;
223  //1000BASE-T
225  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
226  break;
227  //Unknown speed
228  default:
229  //Debug message
230  TRACE_WARNING("Invalid speed\r\n");
231  break;
232  }
233 
234  //Check current duplex mode
235  if(status & AR8035_PHY_STATUS_DUPLEX)
236  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
237  else
238  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
239 
240  //Update link state
241  interface->linkState = TRUE;
242 
243  //Adjust MAC configuration parameters for proper operation
244  interface->nicDriver->updateMacConfig(interface);
245  }
246  else
247  {
248  //Update link state
249  interface->linkState = FALSE;
250  }
251 
252  //Process link state change event
253  nicNotifyLinkChange(interface);
254  }
255 }
256 
257 
258 /**
259  * @brief Write PHY register
260  * @param[in] interface Underlying network interface
261  * @param[in] address PHY register address
262  * @param[in] data Register value
263  **/
264 
265 void ar8035WritePhyReg(NetInterface *interface, uint8_t address,
266  uint16_t data)
267 {
268  //Write the specified PHY register
269  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
270  interface->phyAddr, address, data);
271 }
272 
273 
274 /**
275  * @brief Read PHY register
276  * @param[in] interface Underlying network interface
277  * @param[in] address PHY register address
278  * @return Register value
279  **/
280 
281 uint16_t ar8035ReadPhyReg(NetInterface *interface, uint8_t address)
282 {
283  //Read the specified PHY register
284  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
285  interface->phyAddr, address);
286 }
287 
288 
289 /**
290  * @brief Dump PHY registers for debugging purpose
291  * @param[in] interface Underlying network interface
292  **/
293 
295 {
296  uint8_t i;
297 
298  //Loop through PHY registers
299  for(i = 0; i < 32; i++)
300  {
301  //Display current PHY register
302  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
303  ar8035ReadPhyReg(interface, i));
304  }
305 
306  //Terminate with a line feed
307  TRACE_DEBUG("\r\n");
308 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
#define AR8035_BMCR_SPEED_SEL_LSB
Definition: ar8035_driver.h:73
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:107
#define AR8035_BMCR_RESET
Definition: ar8035_driver.h:71
int bool_t
Definition: compiler_port.h:49
#define AR8035_PHY_STATUS_SPEED_100MBPS
#define AR8035_INT_STATUS_LINK_SUCCESS
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
#define AR8035_ANAR_10BT_HD
#define AR8035_ANAR_ASYM_PAUSE
#define TRUE
Definition: os_port.h:50
PHY driver.
Definition: nic.h:214
void ar8035DisableIrq(NetInterface *interface)
Disable interrupts.
#define AR8035_ANAR_10BT_FD
#define AR8035_GBCR
Definition: ar8035_driver.h:54
#define AR8035_INT_EN
Definition: ar8035_driver.h:61
#define AR8035_ANAR_PAUSE
#define AR8035_FUNC_CTRL
Definition: ar8035_driver.h:59
#define AR8035_FUNC_CTRL_ASSERT_CRS_ON_TX
void ar8035EventHandler(NetInterface *interface)
AR8035 event handler.
#define AR8035_PHY_STATUS_SPEED_1000MBPS
#define SMI_OPCODE_WRITE
Definition: nic.h:62
#define AR8035_BMCR_DUPLEX_MODE
Definition: ar8035_driver.h:78
#define AR8035_BMSR_LINK_STATUS
Definition: ar8035_driver.h:95
#define FALSE
Definition: os_port.h:46
error_t ar8035Init(NetInterface *interface)
AR8035 PHY transceiver initialization.
Definition: ar8035_driver.c:62
error_t
Error codes.
Definition: error.h:42
void ar8035Tick(NetInterface *interface)
AR8035 timer handler.
#define AR8035_ANAR_SELECTOR_DEFAULT
#define AR8035_INT_STATUS
Definition: ar8035_driver.h:62
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
#define AR8035_BMCR
Definition: ar8035_driver.h:45
const PhyDriver ar8035PhyDriver
AR8035 Ethernet PHY driver.
Definition: ar8035_driver.c:44
OsEvent netEvent
Definition: net.c:77
#define AR8035_INT_STATUS_LINK_FAIL
#define SMI_OPCODE_READ
Definition: nic.h:63
#define TRACE_INFO(...)
Definition: debug.h:94
#define AR8035_ANAR_XNP_ABLE
#define AR8035_PHY_STATUS_SPEED_10MBPS
#define AR8035_ANAR
Definition: ar8035_driver.h:49
#define AR8035_PHY_ADDR
Definition: ar8035_driver.h:39
void ar8035EnableIrq(NetInterface *interface)
Enable interrupts.
#define AR8035_BMSR
Definition: ar8035_driver.h:46
#define AR8035_PHY_STATUS_SPEED
#define AR8035_ANAR_100BTX_FD
#define TRACE_WARNING(...)
Definition: debug.h:84
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define AR8035_GBCR_1000BT_FD
uint16_t ar8035ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define AR8035_FUNC_CTRL_MDIX_MODE_AUTO
void ar8035WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
AR8035 Gigabit Ethernet PHY transceiver.
#define AR8035_ANAR_100BTX_HD
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
#define AR8035_PHY_STATUS_LINK
#define AR8035_BMCR_AN_EN
Definition: ar8035_driver.h:74
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define AR8035_PHY_STATUS_DUPLEX
#define AR8035_FUNC_CTRL_POLARITY_REVERSAL
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:106
uint8_t value[]
Definition: dtls_misc.h:150
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
@ NO_ERROR
Success.
Definition: error.h:44
#define AR8035_PHY_STATUS
Definition: ar8035_driver.h:60
Debugging facilities.
void ar8035DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.