rtl8211_driver.c
Go to the documentation of this file.
1 /**
2  * @file rtl8211_driver.c
3  * @brief RTL8211 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 RTL8211 Ethernet PHY driver
40  **/
41 
43 {
49 };
50 
51 
52 /**
53  * @brief RTL8211 PHY transceiver initialization
54  * @param[in] interface Underlying network interface
55  * @return Error code
56  **/
57 
59 {
60  //Debug message
61  TRACE_INFO("Initializing RTL8211...\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
71 
72  //Dump PHY registers for debugging purpose
73  rtl8211DumpPhyReg(interface);
74 
75  //The PHY will generate interrupts when link status changes are detected
77 
78  //Force the TCP/IP stack to poll the link state at startup
79  interface->phyEvent = TRUE;
80  //Notify the TCP/IP stack of the event
82 
83  //Successful initialization
84  return NO_ERROR;
85 }
86 
87 
88 /**
89  * @brief RTL8211 timer handler
90  * @param[in] interface Underlying network interface
91  **/
92 
93 void rtl8211Tick(NetInterface *interface)
94 {
95  uint16_t value;
96  bool_t linkState;
97 
98  //No external interrupt line driver?
99  if(interface->extIntDriver == NULL)
100  {
101  //Read basic status register
103  //Retrieve current link state
104  linkState = (value & BMSR_LINK_STATUS) ? TRUE : FALSE;
105 
106  //Link up event?
107  if(linkState && !interface->linkState)
108  {
109  //Set event flag
110  interface->phyEvent = TRUE;
111  //Notify the TCP/IP stack of the event
113  }
114  //Link down event?
115  else if(!linkState && interface->linkState)
116  {
117  //Set event flag
118  interface->phyEvent = TRUE;
119  //Notify the TCP/IP stack of the event
121  }
122  }
123 }
124 
125 
126 /**
127  * @brief Enable interrupts
128  * @param[in] interface Underlying network interface
129  **/
130 
132 {
133  //Enable PHY transceiver interrupts
134  if(interface->extIntDriver != NULL)
135  interface->extIntDriver->enableIrq();
136 }
137 
138 
139 /**
140  * @brief Disable interrupts
141  * @param[in] interface Underlying network interface
142  **/
143 
145 {
146  //Disable PHY transceiver interrupts
147  if(interface->extIntDriver != NULL)
148  interface->extIntDriver->disableIrq();
149 }
150 
151 
152 /**
153  * @brief RTL8211 event handler
154  * @param[in] interface Underlying network interface
155  **/
156 
158 {
159  uint16_t status;
160 
161  //Read status register to acknowledge the interrupt
162  status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_INSR);
163 
164  //Link status change?
165  if(status & (INSR_AN_COMPLETE | INSR_LINK_STATUS))
166  {
167  //Any link failure condition is latched in the BMSR register... Reading
168  //the register twice will always return the actual link status
169  status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_BMSR);
170  status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_BMSR);
171 
172  //Link is up?
173  if(status & BMSR_LINK_STATUS)
174  {
175  //Read PHY status register
176  status = rtl8211ReadPhyReg(interface, RTL8211_PHY_REG_PHYSR);
177 
178  //Check current speed
179  switch(status & PHYSR_SPEED_MASK)
180  {
181  //10BASE-T
182  case PHYSR_SPEED_10:
183  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
184  break;
185  //100BASE-TX
186  case PHYSR_SPEED_100:
187  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
188  break;
189  //1000BASE-T
190  case PHYSR_SPEED_1000:
191  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
192  break;
193  //Unknown speed
194  default:
195  //Debug message
196  TRACE_WARNING("Invalid speed\r\n");
197  break;
198  }
199 
200  //Check current duplex mode
201  if(status & PHYSR_DUPLEX)
202  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
203  else
204  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
205 
206  //Update link state
207  interface->linkState = TRUE;
208 
209  //Adjust MAC configuration parameters for proper operation
210  interface->nicDriver->updateMacConfig(interface);
211  }
212  else
213  {
214  //Update link state
215  interface->linkState = FALSE;
216  }
217 
218  //Process link state change event
219  nicNotifyLinkChange(interface);
220  }
221 }
222 
223 
224 /**
225  * @brief Write PHY register
226  * @param[in] interface Underlying network interface
227  * @param[in] address PHY register address
228  * @param[in] data Register value
229  **/
230 
231 void rtl8211WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
232 {
233  uint8_t phyAddr;
234 
235  //Get the address of the PHY transceiver
236  if(interface->phyAddr < 32)
237  phyAddr = interface->phyAddr;
238  else
239  phyAddr = RTL8211_PHY_ADDR;
240 
241  //Write the specified PHY register
242  interface->nicDriver->writePhyReg(phyAddr, address, data);
243 }
244 
245 
246 /**
247  * @brief Read PHY register
248  * @param[in] interface Underlying network interface
249  * @param[in] address PHY register address
250  * @return Register value
251  **/
252 
253 uint16_t rtl8211ReadPhyReg(NetInterface *interface, uint8_t address)
254 {
255  uint8_t phyAddr;
256 
257  //Get the address of the PHY transceiver
258  if(interface->phyAddr < 32)
259  phyAddr = interface->phyAddr;
260  else
261  phyAddr = RTL8211_PHY_ADDR;
262 
263  //Read the specified PHY register
264  return interface->nicDriver->readPhyReg(phyAddr, address);
265 }
266 
267 
268 /**
269  * @brief Dump PHY registers for debugging purpose
270  * @param[in] interface Underlying network interface
271  **/
272 
274 {
275  uint8_t i;
276 
277  //Loop through PHY registers
278  for(i = 0; i < 32; i++)
279  {
280  //Display current PHY register
281  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, rtl8211ReadPhyReg(interface, i));
282  }
283 
284  //Terminate with a line feed
285  TRACE_DEBUG("\r\n");
286 }
const PhyDriver rtl8211PhyDriver
RTL8211 Ethernet PHY driver.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
#define RTL8211_PHY_ADDR
#define RTL8211_PHY_REG_INSR
TCP/IP stack core.
Debugging facilities.
void rtl8211EventHandler(NetInterface *interface)
RTL8211 event handler.
#define BMSR_LINK_STATUS
Definition: ar8031_driver.h:95
error_t rtl8211Init(NetInterface *interface)
RTL8211 PHY transceiver initialization.
#define TRUE
Definition: os_port.h:48
#define PHYSR_DUPLEX
void rtl8211EnableIrq(NetInterface *interface)
Enable interrupts.
#define INSR_LINK_STATUS
PHY driver.
Definition: nic.h:196
uint16_t rtl8211ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define RTL8211_PHY_REG_BMSR
#define PHYSR_SPEED_1000
#define INER_AN_COMPLETE
#define BMCR_RESET
Definition: ar8031_driver.h:71
void rtl8211Tick(NetInterface *interface)
RTL8211 timer handler.
#define RTL8211_PHY_REG_PHYSR
#define PHYSR_SPEED_100
#define TRACE_INFO(...)
Definition: debug.h:86
Success.
Definition: error.h:42
#define PHYSR_SPEED_10
Ipv6Addr address
void rtl8211DisableIrq(NetInterface *interface)
Disable interrupts.
#define INSR_AN_COMPLETE
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 RTL8211_PHY_REG_BMCR
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
uint8_t value[]
Definition: dtls_misc.h:141
#define PHYSR_SPEED_MASK
void rtl8211WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define INER_LINK_STATUS
RTL8211 Ethernet PHY transceiver.
#define RTL8211_PHY_REG_INER
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
void rtl8211DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define TRACE_DEBUG(...)
Definition: debug.h:98