dp83865_driver.c
Go to the documentation of this file.
1 /**
2  * @file dp83865_driver.c
3  * @brief DP83865 Gigabit Ethernet PHY driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.0
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 DP83865 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief DP83865 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  uint16_t temp;
63 
64  //Debug message
65  TRACE_INFO("Initializing DP83865...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = DP83865_PHY_ADDR;
72  }
73 
74  //Initialize serial management interface
75  if(interface->smiDriver != NULL)
76  {
77  interface->smiDriver->init();
78  }
79 
80  //Initialize external interrupt line driver
81  if(interface->extIntDriver != NULL)
82  {
83  interface->extIntDriver->init();
84  }
85 
86  //Reset PHY transceiver
88 
89  //Wait for the reset to complete
91  {
92  }
93 
94  //Dump PHY registers for debugging purpose
95  dp83865DumpPhyReg(interface);
96 
97  //Select RGMII mode
98  temp = dp83865ReadPhyReg(interface, DP83865_AUX_CTRL);
101  dp83865WritePhyReg(interface, DP83865_AUX_CTRL, temp);
102 
103  //Perform custom configuration
104  dp83865InitHook(interface);
105 
106  //Force the TCP/IP stack to poll the link state at startup
107  interface->phyEvent = TRUE;
108  //Notify the TCP/IP stack of the event
109  osSetEvent(&interface->netContext->event);
110 
111  //Successful initialization
112  return NO_ERROR;
113 }
114 
115 
116 /**
117  * @brief DP83865 custom configuration
118  * @param[in] interface Underlying network interface
119  **/
120 
121 __weak_func void dp83865InitHook(NetInterface *interface)
122 {
123 }
124 
125 
126 /**
127  * @brief DP83865 timer handler
128  * @param[in] interface Underlying network interface
129  **/
130 
131 void dp83865Tick(NetInterface *interface)
132 {
133  uint16_t value;
134  bool_t linkState;
135 
136  //Read basic status register
137  value = dp83865ReadPhyReg(interface, DP83865_BMSR);
138  //Retrieve current link state
139  linkState = (value & DP83865_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
147  osSetEvent(&interface->netContext->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
155  osSetEvent(&interface->netContext->event);
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 DP83865 event handler
192  * @param[in] interface Underlying network interface
193  **/
194 
196 {
197  uint16_t status;
198  bool_t linkState;
199 
200  //Read Link and Auto-Negotiation Status register
201  status = dp83865ReadPhyReg(interface, DP83865_LINK_AN);
202  //Retrieve current link state
203  linkState = (status & DP83865_LINK_AN_LINK_STATUS) ? TRUE : FALSE;
204 
205  //Link up event?
206  if(linkState && !interface->linkState)
207  {
208  //Check current speed
209  switch(status & DP83865_LINK_AN_SPEED_STATUS)
210  {
211  //10BASE-T
213  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
214  break;
215  //100BASE-TX
217  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
218  break;
219  //1000BASE-T
221  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
222  break;
223  //Unknown speed
224  default:
225  //Debug message
226  TRACE_WARNING("Invalid speed\r\n");
227  break;
228  }
229 
230  //Check duplex mode
231  if((status & DP83865_LINK_AN_DUPLEX_STATUS) != 0)
232  {
233  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
234  }
235  else
236  {
237  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
238  }
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  //Process link state change event
247  nicNotifyLinkChange(interface);
248  }
249  //Link down event?
250  else if(!linkState && interface->linkState)
251  {
252  //Update link state
253  interface->linkState = FALSE;
254 
255  //Process link state change event
256  nicNotifyLinkChange(interface);
257  }
258 }
259 
260 
261 /**
262  * @brief Write PHY register
263  * @param[in] interface Underlying network interface
264  * @param[in] address PHY register address
265  * @param[in] data Register value
266  **/
267 
268 void dp83865WritePhyReg(NetInterface *interface, uint8_t address,
269  uint16_t data)
270 {
271  //Write the specified PHY register
272  if(interface->smiDriver != NULL)
273  {
274  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
275  interface->phyAddr, address, data);
276  }
277  else
278  {
279  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
280  interface->phyAddr, address, data);
281  }
282 }
283 
284 
285 /**
286  * @brief Read PHY register
287  * @param[in] interface Underlying network interface
288  * @param[in] address PHY register address
289  * @return Register value
290  **/
291 
292 uint16_t dp83865ReadPhyReg(NetInterface *interface, uint8_t address)
293 {
294  uint16_t data;
295 
296  //Read the specified PHY register
297  if(interface->smiDriver != NULL)
298  {
299  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
300  interface->phyAddr, address);
301  }
302  else
303  {
304  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
305  interface->phyAddr, address);
306  }
307 
308  //Return the value of the PHY register
309  return data;
310 }
311 
312 
313 /**
314  * @brief Dump PHY registers for debugging purpose
315  * @param[in] interface Underlying network interface
316  **/
317 
319 {
320  uint8_t i;
321 
322  //Loop through PHY registers
323  for(i = 0; i < 32; i++)
324  {
325  //Display current PHY register
326  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
327  dp83865ReadPhyReg(interface, i));
328  }
329 
330  //Terminate with a line feed
331  TRACE_DEBUG("\r\n");
332 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:601
#define DP83865_BMCR_RESET
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define DP83865_AUX_CTRL_RGMII_EN
#define DP83865_BMSR_LINK_STATUS
int bool_t
Definition: compiler_port.h:63
const PhyDriver dp83865PhyDriver
DP83865 Ethernet PHY driver.
#define DP83865_LINK_AN
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
DP83865 Gigabit Ethernet PHY driver.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:224
#define DP83865_LINK_AN_LINK_STATUS
#define SMI_OPCODE_WRITE
Definition: nic.h:66
void dp83865DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define DP83865_LINK_AN_SPEED_STATUS
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
void dp83865WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define NetInterface
Definition: net.h:40
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
void dp83865Tick(NetInterface *interface)
DP83865 timer handler.
#define DP83865_LINK_AN_SPEED_STATUS_100MBPS
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:105
void dp83865DisableIrq(NetInterface *interface)
Disable interrupts.
#define DP83865_BMCR
#define DP83865_AUX_CTRL
uint16_t dp83865ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define DP83865_LINK_AN_SPEED_STATUS_1000MBPS
#define TRACE_WARNING(...)
Definition: debug.h:93
#define TRACE_DEBUG(...)
Definition: debug.h:119
#define DP83865_LINK_AN_DUPLEX_STATUS
#define DP83865_AUX_CTRL_RGMII_EN_RGMII_3COM_MODE
Ipv6Addr address[]
Definition: ipv6.h:345
error_t dp83865Init(NetInterface *interface)
DP83865 PHY transceiver initialization.
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:376
#define DP83865_BMSR
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
__weak_func void dp83865InitHook(NetInterface *interface)
DP83865 custom configuration.
#define DP83865_PHY_ADDR
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
TCP/IP stack core.
#define DP83865_LINK_AN_SPEED_STATUS_10MBPS
void dp83865EventHandler(NetInterface *interface)
DP83865 event handler.
void dp83865EnableIrq(NetInterface *interface)
Enable interrupts.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.