rtl8211f_driver.c
Go to the documentation of this file.
1 /**
2  * @file rtl8211f_driver.c
3  * @brief RTL8211F Gigabit Ethernet PHY driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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 2.4.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 };
52 
53 
54 /**
55  * @brief RTL8211F PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing RTL8211F...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = RTL8211F_PHY_ADDR;
70  }
71 
72  //Initialize serial management interface
73  if(interface->smiDriver != NULL)
74  {
75  interface->smiDriver->init();
76  }
77 
78  //Initialize external interrupt line driver
79  if(interface->extIntDriver != NULL)
80  {
81  interface->extIntDriver->init();
82  }
83 
84  //Reset PHY transceiver
86 
87  //Wait for the reset to complete
89  {
90  }
91 
92  //Dump PHY registers for debugging purpose
93  rtl8211fDumpPhyReg(interface);
94 
95  //The PHY will generate interrupts when link status changes are detected
98 
99  //Perform custom configuration
100  rtl8211fInitHook(interface);
101 
102  //Force the TCP/IP stack to poll the link state at startup
103  interface->phyEvent = TRUE;
104  //Notify the TCP/IP stack of the event
106 
107  //Successful initialization
108  return NO_ERROR;
109 }
110 
111 
112 /**
113  * @brief RTL8211F custom configuration
114  * @param[in] interface Underlying network interface
115  **/
116 
117 __weak_func void rtl8211fInitHook(NetInterface *interface)
118 {
119 }
120 
121 
122 /**
123  * @brief RTL8211F timer handler
124  * @param[in] interface Underlying network interface
125  **/
126 
127 void rtl8211fTick(NetInterface *interface)
128 {
129  uint16_t value;
130  bool_t linkState;
131 
132  //No external interrupt line driver?
133  if(interface->extIntDriver == NULL)
134  {
135  //Read basic status register
136  value = rtl8211fReadPhyReg(interface, RTL8211F_BMSR);
137  //Retrieve current link state
138  linkState = (value & RTL8211F_BMSR_LINK_STATUS) ? TRUE : FALSE;
139 
140  //Link up event?
141  if(linkState && !interface->linkState)
142  {
143  //Set event flag
144  interface->phyEvent = TRUE;
145  //Notify the TCP/IP stack of the event
147  }
148  //Link down event?
149  else if(!linkState && interface->linkState)
150  {
151  //Set event flag
152  interface->phyEvent = TRUE;
153  //Notify the TCP/IP stack of the event
155  }
156  }
157 }
158 
159 
160 /**
161  * @brief Enable interrupts
162  * @param[in] interface Underlying network interface
163  **/
164 
166 {
167  //Enable PHY transceiver interrupts
168  if(interface->extIntDriver != NULL)
169  {
170  interface->extIntDriver->enableIrq();
171  }
172 }
173 
174 
175 /**
176  * @brief Disable interrupts
177  * @param[in] interface Underlying network interface
178  **/
179 
181 {
182  //Disable PHY transceiver interrupts
183  if(interface->extIntDriver != NULL)
184  {
185  interface->extIntDriver->disableIrq();
186  }
187 }
188 
189 
190 /**
191  * @brief RTL8211F event handler
192  * @param[in] interface Underlying network interface
193  **/
194 
196 {
197  uint16_t status;
198 
199  //Read status register to acknowledge the interrupt
200  status = rtl8211fReadPhyReg(interface, RTL8211F_INSR);
201 
202  //Link status change?
204  {
205  //Any link failure condition is latched in the BMSR register. Reading
206  //the register twice will always return the actual link status
207  status = rtl8211fReadPhyReg(interface, RTL8211F_BMSR);
208  status = rtl8211fReadPhyReg(interface, RTL8211F_BMSR);
209 
210  //Link is up?
211  if((status & RTL8211F_BMSR_LINK_STATUS) != 0)
212  {
213  //Read PHY status register
214  status = rtl8211fReadPhyReg(interface, RTL8211F_PHYSR);
215 
216  //Check current speed
217  switch(status & RTL8211F_PHYSR_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 & RTL8211F_PHYSR_DUPLEX) != 0)
240  {
241  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
242  }
243  else
244  {
245  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
246  }
247 
248  //Update link state
249  interface->linkState = TRUE;
250 
251  //Adjust MAC configuration parameters for proper operation
252  interface->nicDriver->updateMacConfig(interface);
253  }
254  else
255  {
256  //Update link state
257  interface->linkState = FALSE;
258  }
259 
260  //Process link state change event
261  nicNotifyLinkChange(interface);
262  }
263 }
264 
265 
266 /**
267  * @brief Write PHY register
268  * @param[in] interface Underlying network interface
269  * @param[in] address PHY register address
270  * @param[in] data Register value
271  **/
272 
273 void rtl8211fWritePhyReg(NetInterface *interface, uint8_t address,
274  uint16_t data)
275 {
276  //Write the specified PHY register
277  if(interface->smiDriver != NULL)
278  {
279  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
280  interface->phyAddr, address, data);
281  }
282  else
283  {
284  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
285  interface->phyAddr, address, data);
286  }
287 }
288 
289 
290 /**
291  * @brief Read PHY register
292  * @param[in] interface Underlying network interface
293  * @param[in] address PHY register address
294  * @return Register value
295  **/
296 
297 uint16_t rtl8211fReadPhyReg(NetInterface *interface, uint8_t address)
298 {
299  uint16_t data;
300 
301  //Read the specified PHY register
302  if(interface->smiDriver != NULL)
303  {
304  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
305  interface->phyAddr, address);
306  }
307  else
308  {
309  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
310  interface->phyAddr, address);
311  }
312 
313  //Return the value of the PHY register
314  return data;
315 }
316 
317 
318 /**
319  * @brief Dump PHY registers for debugging purpose
320  * @param[in] interface Underlying network interface
321  **/
322 
324 {
325  uint8_t i;
326 
327  //Loop through PHY registers
328  for(i = 0; i < 32; i++)
329  {
330  //Display current PHY register
331  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
332  rtl8211fReadPhyReg(interface, i));
333  }
334 
335  //Terminate with a line feed
336  TRACE_DEBUG("\r\n");
337 }
338 
339 
340 /**
341  * @brief Write MMD register
342  * @param[in] interface Underlying network interface
343  * @param[in] devAddr Device address
344  * @param[in] regAddr Register address
345  * @param[in] data MMD register value
346  **/
347 
348 void rtl8211fWriteMmdReg(NetInterface *interface, uint8_t devAddr,
349  uint16_t regAddr, uint16_t data)
350 {
351  //Select register operation
354 
355  //Write MMD register address
357 
358  //Select data operation
361 
362  //Write the content of the MMD register
364 }
365 
366 
367 /**
368  * @brief Read MMD register
369  * @param[in] interface Underlying network interface
370  * @param[in] devAddr Device address
371  * @param[in] regAddr Register address
372  * @return MMD register value
373  **/
374 
375 uint16_t rtl8211fReadMmdReg(NetInterface *interface, uint8_t devAddr,
376  uint16_t regAddr)
377 {
378  //Select register operation
381 
382  //Write MMD register address
384 
385  //Select data operation
388 
389  //Read the content of the MMD register
390  return rtl8211fReadPhyReg(interface, RTL8211F_MMDAADR);
391 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
void rtl8211fEnableIrq(NetInterface *interface)
Enable interrupts.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define RTL8211F_MMDACR_DEVAD
#define RTL8211F_MMDACR_FUNC_DATA_NO_POST_INC
#define RTL8211F_PHYSR_SPEED_10MBPS
#define RTL8211F_MMDACR_FUNC_ADDR
void rtl8211fDisableIrq(NetInterface *interface)
Disable interrupts.
#define RTL8211F_PHYSR
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define RTL8211F_INER
#define RTL8211F_PHY_ADDR
#define FALSE
Definition: os_port.h:46
void rtl8211fEventHandler(NetInterface *interface)
RTL8211F event handler.
error_t
Error codes.
Definition: error.h:43
RTL8211F Gigabit Ethernet PHY driver.
#define RTL8211F_INSR_LINK_STATUS
#define NetInterface
Definition: net.h:36
#define RTL8211F_MMDAADR
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define RTL8211F_PHYSR_SPEED_1000MBPS
#define RTL8211F_INER_LINK_STATUS
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
#define RTL8211F_INSR_AN_COMPLETE
#define RTL8211F_INER_AN_COMPLETE
__weak_func void rtl8211fInitHook(NetInterface *interface)
RTL8211F custom configuration.
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
uint16_t rtl8211fReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
uint16_t regAddr
#define RTL8211F_BMSR
void rtl8211fDumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
Ipv6Addr address[]
Definition: ipv6.h:325
uint16_t rtl8211fReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define RTL8211F_BMSR_LINK_STATUS
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
error_t rtl8211fInit(NetInterface *interface)
RTL8211F PHY transceiver initialization.
void rtl8211fWriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void rtl8211fWritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define RTL8211F_BMCR
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
void rtl8211fTick(NetInterface *interface)
RTL8211F timer handler.
#define RTL8211F_PHYSR_SPEED
#define RTL8211F_INSR
#define RTL8211F_MMDACR
TCP/IP stack core.
#define RTL8211F_BMCR_RESET
#define RTL8211F_PHYSR_DUPLEX
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define RTL8211F_PHYSR_SPEED_100MBPS
const PhyDriver rtl8211fPhyDriver
RTL8211F Ethernet PHY driver.