rtl8211f_driver.c
Go to the documentation of this file.
1 /**
2  * @file rtl8211f_driver.c
3  * @brief RTL8211F 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.4
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 RTL8211F Ethernet PHY driver
42  **/
43 
45 {
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief RTL8211F PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing RTL8211F...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = RTL8211F_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  //Dump PHY registers for debugging purpose
89  rtl8211fDumpPhyReg(interface);
90 
91  //The PHY will generate interrupts when link status changes are detected
94 
95  //Force the TCP/IP stack to poll the link state at startup
96  interface->phyEvent = TRUE;
97  //Notify the TCP/IP stack of the event
99 
100  //Successful initialization
101  return NO_ERROR;
102 }
103 
104 
105 /**
106  * @brief RTL8211F timer handler
107  * @param[in] interface Underlying network interface
108  **/
109 
110 void rtl8211fTick(NetInterface *interface)
111 {
112  uint16_t value;
113  bool_t linkState;
114 
115  //No external interrupt line driver?
116  if(interface->extIntDriver == NULL)
117  {
118  //Read basic status register
119  value = rtl8211fReadPhyReg(interface, RTL8211F_BMSR);
120  //Retrieve current link state
121  linkState = (value & RTL8211F_BMSR_LINK_STATUS) ? TRUE : FALSE;
122 
123  //Link up event?
124  if(linkState && !interface->linkState)
125  {
126  //Set event flag
127  interface->phyEvent = TRUE;
128  //Notify the TCP/IP stack of the event
130  }
131  //Link down event?
132  else if(!linkState && interface->linkState)
133  {
134  //Set event flag
135  interface->phyEvent = TRUE;
136  //Notify the TCP/IP stack of the event
138  }
139  }
140 }
141 
142 
143 /**
144  * @brief Enable interrupts
145  * @param[in] interface Underlying network interface
146  **/
147 
149 {
150  //Enable PHY transceiver interrupts
151  if(interface->extIntDriver != NULL)
152  {
153  interface->extIntDriver->enableIrq();
154  }
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  {
168  interface->extIntDriver->disableIrq();
169  }
170 }
171 
172 
173 /**
174  * @brief RTL8211F event handler
175  * @param[in] interface Underlying network interface
176  **/
177 
179 {
180  uint16_t status;
181 
182  //Read status register to acknowledge the interrupt
183  status = rtl8211fReadPhyReg(interface, RTL8211F_INSR);
184 
185  //Link status change?
187  {
188  //Any link failure condition is latched in the BMSR register. Reading
189  //the register twice will always return the actual link status
190  status = rtl8211fReadPhyReg(interface, RTL8211F_BMSR);
191  status = rtl8211fReadPhyReg(interface, RTL8211F_BMSR);
192 
193  //Link is up?
194  if(status & RTL8211F_BMSR_LINK_STATUS)
195  {
196  //Read PHY status register
197  status = rtl8211fReadPhyReg(interface, RTL8211F_PHYSR);
198 
199  //Check current speed
200  switch(status & RTL8211F_PHYSR_SPEED)
201  {
202  //10BASE-T
204  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
205  break;
206  //100BASE-TX
208  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
209  break;
210  //1000BASE-T
212  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
213  break;
214  //Unknown speed
215  default:
216  //Debug message
217  TRACE_WARNING("Invalid speed\r\n");
218  break;
219  }
220 
221  //Check current duplex mode
222  if(status & RTL8211F_PHYSR_DUPLEX)
223  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
224  else
225  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
226 
227  //Update link state
228  interface->linkState = TRUE;
229 
230  //Adjust MAC configuration parameters for proper operation
231  interface->nicDriver->updateMacConfig(interface);
232  }
233  else
234  {
235  //Update link state
236  interface->linkState = FALSE;
237  }
238 
239  //Process link state change event
240  nicNotifyLinkChange(interface);
241  }
242 }
243 
244 
245 /**
246  * @brief Write PHY register
247  * @param[in] interface Underlying network interface
248  * @param[in] address PHY register address
249  * @param[in] data Register value
250  **/
251 
252 void rtl8211fWritePhyReg(NetInterface *interface, uint8_t address,
253  uint16_t data)
254 {
255  //Write the specified PHY register
256  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
257  interface->phyAddr, address, data);
258 }
259 
260 
261 /**
262  * @brief Read PHY register
263  * @param[in] interface Underlying network interface
264  * @param[in] address PHY register address
265  * @return Register value
266  **/
267 
268 uint16_t rtl8211fReadPhyReg(NetInterface *interface, uint8_t address)
269 {
270  //Read the specified PHY register
271  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
272  interface->phyAddr, address);
273 }
274 
275 
276 /**
277  * @brief Dump PHY registers for debugging purpose
278  * @param[in] interface Underlying network interface
279  **/
280 
282 {
283  uint8_t i;
284 
285  //Loop through PHY registers
286  for(i = 0; i < 32; i++)
287  {
288  //Display current PHY register
289  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
290  rtl8211fReadPhyReg(interface, i));
291  }
292 
293  //Terminate with a line feed
294  TRACE_DEBUG("\r\n");
295 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:536
#define RTL8211F_PHYSR_SPEED
TCP/IP stack core.
void rtl8211fDumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define RTL8211F_INSR
Debugging facilities.
#define RTL8211F_PHYSR_SPEED_10MBPS
error_t rtl8211fInit(NetInterface *interface)
RTL8211F PHY transceiver initialization.
#define RTL8211F_BMSR
#define RTL8211F_INSR_AN_COMPLETE
#define SMI_OPCODE_READ
Definition: nic.h:63
#define RTL8211F_BMCR_RESET
#define RTL8211F_INER_AN_COMPLETE
#define TRUE
Definition: os_port.h:50
#define SMI_OPCODE_WRITE
Definition: nic.h:62
#define RTL8211F_BMSR_LINK_STATUS
PHY driver.
Definition: nic.h:214
const PhyDriver rtl8211fPhyDriver
RTL8211F Ethernet PHY driver.
void rtl8211fWritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define RTL8211F_INSR_LINK_STATUS
RTL8211F Gigabit Ethernet PHY transceiver.
#define RTL8211F_PHYSR_DUPLEX
void rtl8211fDisableIrq(NetInterface *interface)
Disable interrupts.
void rtl8211fEnableIrq(NetInterface *interface)
Enable interrupts.
uint16_t rtl8211fReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void rtl8211fTick(NetInterface *interface)
RTL8211F timer handler.
#define RTL8211F_INER_LINK_STATUS
#define TRACE_INFO(...)
Definition: debug.h:94
Success.
Definition: error.h:44
Ipv6Addr address
OsEvent netEvent
Definition: net.c:74
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t
Error codes.
Definition: error.h:42
#define TRACE_WARNING(...)
Definition: debug.h:84
#define RTL8211F_PHYSR_SPEED_1000MBPS
uint8_t data[]
Definition: dtls_misc.h:169
#define NetInterface
Definition: net.h:36
uint8_t value[]
Definition: dtls_misc.h:143
#define RTL8211F_PHYSR
#define RTL8211F_INER
#define RTL8211F_BMCR
#define RTL8211F_PHY_ADDR
#define RTL8211F_PHYSR_SPEED_100MBPS
#define FALSE
Definition: os_port.h:46
int bool_t
Definition: compiler_port.h:49
void rtl8211fEventHandler(NetInterface *interface)
RTL8211F event handler.
#define TRACE_DEBUG(...)
Definition: debug.h:106