ar8031_driver.c
Go to the documentation of this file.
1 /**
2  * @file ar8031_driver.c
3  * @brief AR8031 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 AR8031 Ethernet PHY driver
42  **/
43 
45 {
46  ar8031Init,
47  ar8031Tick,
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief AR8031 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing AR8031...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = AR8031_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  //Chip configuration register
91 
92  //Basic mode control register
95 
96  //Auto-negotiation advertisement register
101 
102  //1000 BASE-T control register
104 
105  //Function control register
109 
110  //Dump PHY registers for debugging purpose
111  ar8031DumpPhyReg(interface);
112 
113  //The PHY will generate interrupts when link status changes are detected
116 
117  //Force the TCP/IP stack to poll the link state at startup
118  interface->phyEvent = TRUE;
119  //Notify the TCP/IP stack of the event
121 
122  //Successful initialization
123  return NO_ERROR;
124 }
125 
126 
127 /**
128  * @brief AR8031 timer handler
129  * @param[in] interface Underlying network interface
130  **/
131 
132 void ar8031Tick(NetInterface *interface)
133 {
134  uint16_t value;
135  bool_t linkState;
136 
137  //No external interrupt line driver?
138  if(interface->extIntDriver == NULL)
139  {
140  //Read basic status register
141  value = ar8031ReadPhyReg(interface, AR8031_BMSR);
142  //Retrieve current link state
143  linkState = (value & AR8031_BMSR_LINK_STATUS) ? TRUE : FALSE;
144 
145  //Link up event?
146  if(linkState && !interface->linkState)
147  {
148  //Set event flag
149  interface->phyEvent = TRUE;
150  //Notify the TCP/IP stack of the event
152  }
153  //Link down event?
154  else if(!linkState && interface->linkState)
155  {
156  //Set event flag
157  interface->phyEvent = TRUE;
158  //Notify the TCP/IP stack of the event
160  }
161  }
162 }
163 
164 
165 /**
166  * @brief Enable interrupts
167  * @param[in] interface Underlying network interface
168  **/
169 
171 {
172  //Enable PHY transceiver interrupts
173  if(interface->extIntDriver != NULL)
174  {
175  interface->extIntDriver->enableIrq();
176  }
177 }
178 
179 
180 /**
181  * @brief Disable interrupts
182  * @param[in] interface Underlying network interface
183  **/
184 
186 {
187  //Disable PHY transceiver interrupts
188  if(interface->extIntDriver != NULL)
189  {
190  interface->extIntDriver->disableIrq();
191  }
192 }
193 
194 
195 /**
196  * @brief AR8031 event handler
197  * @param[in] interface Underlying network interface
198  **/
199 
201 {
202  uint16_t status;
203 
204  //Read status register to acknowledge the interrupt
205  status = ar8031ReadPhyReg(interface, AR8031_INT_STATUS);
206 
207  //Link status change?
209  {
210  //Read PHY status register
211  status = ar8031ReadPhyReg(interface, AR8031_PHY_STATUS);
212 
213  //Link is up?
214  if(status & AR8031_PHY_STATUS_LINK)
215  {
216  //Check current speed
217  switch(status & AR8031_PHY_STATUS_SPEED)
218  {
219  //10BASE-T
221  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
222  break;
223  //100BASE-TX
225  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
226  break;
227  //1000BASE-T
229  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
230  break;
231  //Unknown speed
232  default:
233  //Debug message
234  TRACE_WARNING("Invalid speed\r\n");
235  break;
236  }
237 
238  //Check current duplex mode
239  if(status & AR8031_PHY_STATUS_DUPLEX)
240  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
241  else
242  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
243 
244  //Update link state
245  interface->linkState = TRUE;
246 
247  //Adjust MAC configuration parameters for proper operation
248  interface->nicDriver->updateMacConfig(interface);
249  }
250  else
251  {
252  //Update link state
253  interface->linkState = FALSE;
254  }
255 
256  //Process link state change event
257  nicNotifyLinkChange(interface);
258  }
259 }
260 
261 
262 /**
263  * @brief Write PHY register
264  * @param[in] interface Underlying network interface
265  * @param[in] address PHY register address
266  * @param[in] data Register value
267  **/
268 
269 void ar8031WritePhyReg(NetInterface *interface, uint8_t address,
270  uint16_t data)
271 {
272  //Write the specified PHY register
273  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
274  interface->phyAddr, address, data);
275 }
276 
277 
278 /**
279  * @brief Read PHY register
280  * @param[in] interface Underlying network interface
281  * @param[in] address PHY register address
282  * @return Register value
283  **/
284 
285 uint16_t ar8031ReadPhyReg(NetInterface *interface, uint8_t address)
286 {
287  //Read the specified PHY register
288  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
289  interface->phyAddr, address);
290 }
291 
292 
293 /**
294  * @brief Dump PHY registers for debugging purpose
295  * @param[in] interface Underlying network interface
296  **/
297 
299 {
300  uint8_t i;
301 
302  //Loop through PHY registers
303  for(i = 0; i < 32; i++)
304  {
305  //Display current PHY register
306  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
307  ar8031ReadPhyReg(interface, i));
308  }
309 
310  //Terminate with a line feed
311  TRACE_DEBUG("\r\n");
312 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:107
int bool_t
Definition: compiler_port.h:49
const PhyDriver ar8031PhyDriver
AR8031 Ethernet PHY driver.
Definition: ar8031_driver.c:44
void ar8031EventHandler(NetInterface *interface)
AR8031 event handler.
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
#define AR8031_ANAR
Definition: ar8031_driver.h:49
void ar8031DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define AR8031_INT_STATUS_LINK_FAIL
#define AR8031_PHY_STATUS_LINK
#define AR8031_PHY_STATUS_DUPLEX
#define TRUE
Definition: os_port.h:50
#define AR8031_ANAR_10BT_FD
PHY driver.
Definition: nic.h:214
#define AR8031_INT_STATUS_LINK_SUCCESS
#define AR8031_GBCR
Definition: ar8031_driver.h:54
#define AR8031_BMCR_DUPLEX_MODE
Definition: ar8031_driver.h:81
#define AR8031_INT_EN
Definition: ar8031_driver.h:61
error_t ar8031Init(NetInterface *interface)
AR8031 PHY transceiver initialization.
Definition: ar8031_driver.c:62
#define AR8031_PHY_STATUS_SPEED_1000MBPS
#define AR8031_PHY_STATUS
Definition: ar8031_driver.h:60
#define AR8031_ANAR_10BT_HD
void ar8031EnableIrq(NetInterface *interface)
Enable interrupts.
#define SMI_OPCODE_WRITE
Definition: nic.h:62
#define AR8031_BMCR_AN_EN
Definition: ar8031_driver.h:77
#define AR8031_INT_STATUS
Definition: ar8031_driver.h:62
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
#define AR8031_ANAR_100BTX_FD
#define AR8031_GBCR_1000BT_FD
void ar8031Tick(NetInterface *interface)
AR8031 timer handler.
#define AR8031_BMCR_RESET
Definition: ar8031_driver.h:74
#define AR8031_FUNC_CTRL_POLARITY_REVERSAL
void ar8031WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
#define AR8031_CHIP_CONF_BT_BX_REG_SEL
OsEvent netEvent
Definition: net.c:77
#define SMI_OPCODE_READ
Definition: nic.h:63
#define TRACE_INFO(...)
Definition: debug.h:94
#define AR8031_ANAR_SELECTOR_DEFAULT
#define AR8031_PHY_ADDR
Definition: ar8031_driver.h:39
#define TRACE_WARNING(...)
Definition: debug.h:84
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define AR8031_FUNC_CTRL
Definition: ar8031_driver.h:59
#define AR8031_BMSR_LINK_STATUS
Definition: ar8031_driver.h:98
#define AR8031_FUNC_CTRL_ASSERT_CRS_ON_TX
uint16_t ar8031ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define AR8031_BMSR
Definition: ar8031_driver.h:46
#define AR8031_CHIP_CONF
Definition: ar8031_driver.h:71
#define AR8031_ANAR_100BTX_HD
void ar8031DisableIrq(NetInterface *interface)
Disable interrupts.
#define AR8031_CHIP_CONF_PRIORITY_SEL
#define AR8031_ANAR_PAUSE
#define AR8031_ANAR_ASYM_PAUSE
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
#define AR8031_PHY_STATUS_SPEED_100MBPS
#define AR8031_FUNC_CTRL_MDIX_MODE_AUTO
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define AR8031_BMCR_SPEED_SEL_LSB
Definition: ar8031_driver.h:76
#define AR8031_PHY_STATUS_SPEED_10MBPS
@ 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
AR8031 Gigabit Ethernet PHY transceiver.
#define AR8031_PHY_STATUS_SPEED
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define AR8031_ANAR_XNP_ABLE
#define AR8031_BMCR
Definition: ar8031_driver.h:45