dp83640_driver.c
Go to the documentation of this file.
1 /**
2  * @file dp83640_driver.c
3  * @brief DP83640 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.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 DP83640 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief DP83640 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  uint16_t value;
63 
64  //Debug message
65  TRACE_INFO("Initializing DP83640...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = DP83640_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  //A software reset is accomplished by setting the RESET bit of the BMCR register
88 
89  //Wait for the reset to complete
91  {
92  }
93 
94  //Dump PHY registers for debugging purpose
95  dp83640DumpPhyReg(interface);
96 
97  //Configure PWR_DOWN/INT pin as an interrupt output
100 
101  //The PHY will generate interrupts when link status changes are detected
103 
104  //Select page 6
105  dp83640WritePhyReg(interface, DP83640_PAGSR, 6);
106 
107  //If the CLK_OUT pin is to be used as a 50 MHz RMII clock, the default PTP
108  //clock output function must be disabled
112 
113  //Select page 0
114  dp83640WritePhyReg(interface, DP83640_PAGSR, 0);
115 
116  //Perform custom configuration
117  dp83640InitHook(interface);
118 
119  //Force the TCP/IP stack to poll the link state at startup
120  interface->phyEvent = TRUE;
121  //Notify the TCP/IP stack of the event
123 
124  //Successful initialization
125  return NO_ERROR;
126 }
127 
128 
129 /**
130  * @brief DP83640 custom configuration
131  * @param[in] interface Underlying network interface
132  **/
133 
134 __weak_func void dp83640InitHook(NetInterface *interface)
135 {
136 }
137 
138 
139 /**
140  * @brief DP83640 timer handler
141  * @param[in] interface Underlying network interface
142  **/
143 
144 void dp83640Tick(NetInterface *interface)
145 {
146  uint16_t value;
147  bool_t linkState;
148 
149  //No external interrupt line driver?
150  if(interface->extIntDriver == NULL)
151  {
152  //Read basic status register
153  value = dp83640ReadPhyReg(interface, DP83640_BMSR);
154  //Retrieve current link state
155  linkState = (value & DP83640_BMSR_LINK_STATUS) ? TRUE : FALSE;
156 
157  //Link up event?
158  if(linkState && !interface->linkState)
159  {
160  //Set event flag
161  interface->phyEvent = TRUE;
162  //Notify the TCP/IP stack of the event
164  }
165  //Link down event?
166  else if(!linkState && interface->linkState)
167  {
168  //Set event flag
169  interface->phyEvent = TRUE;
170  //Notify the TCP/IP stack of the event
172  }
173  }
174 }
175 
176 
177 /**
178  * @brief Enable interrupts
179  * @param[in] interface Underlying network interface
180  **/
181 
183 {
184  //Enable PHY transceiver interrupts
185  if(interface->extIntDriver != NULL)
186  {
187  interface->extIntDriver->enableIrq();
188  }
189 }
190 
191 
192 /**
193  * @brief Disable interrupts
194  * @param[in] interface Underlying network interface
195  **/
196 
198 {
199  //Disable PHY transceiver interrupts
200  if(interface->extIntDriver != NULL)
201  {
202  interface->extIntDriver->disableIrq();
203  }
204 }
205 
206 
207 /**
208  * @brief DP83640 event handler
209  * @param[in] interface Underlying network interface
210  **/
211 
213 {
214  uint16_t status;
215 
216  //Read status register to acknowledge the interrupt
217  status = dp83640ReadPhyReg(interface, DP83640_MISR);
218 
219  //Link status change?
220  if((status & DP83640_MISR_LINK_INT) != 0)
221  {
222  //Read PHY status register
223  status = dp83640ReadPhyReg(interface, DP83640_PHYSTS);
224 
225  //Link is up?
226  if((status & DP83640_PHYSTS_LINK_STATUS) != 0)
227  {
228  //Check current speed
229  if((status & DP83640_PHYSTS_SPEED_STATUS) != 0)
230  {
231  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
232  }
233  else
234  {
235  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
236  }
237 
238  //Check duplex mode
239  if((status & DP83640_PHYSTS_DUPLEX_STATUS) != 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 dp83640WritePhyReg(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 dp83640ReadPhyReg(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  dp83640ReadPhyReg(interface, i));
333  }
334 
335  //Terminate with a line feed
336  TRACE_DEBUG("\r\n");
337 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
void dp83640WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
__weak_func void dp83640InitHook(NetInterface *interface)
DP83640 custom configuration.
uint16_t dp83640ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
error_t dp83640Init(NetInterface *interface)
DP83640 PHY transceiver initialization.
const PhyDriver dp83640PhyDriver
DP83640 Ethernet PHY driver.
void dp83640DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void dp83640Tick(NetInterface *interface)
DP83640 timer handler.
void dp83640EnableIrq(NetInterface *interface)
Enable interrupts.
void dp83640DisableIrq(NetInterface *interface)
Disable interrupts.
void dp83640EventHandler(NetInterface *interface)
DP83640 event handler.
DP83640 Ethernet PHY driver.
#define DP83640_BMSR_LINK_STATUS
#define DP83640_MISR
#define DP83640_MISR_LINK_INT
#define DP83640_MICR_INTEN
#define DP83640_MICR
#define DP83640_PHYSTS_DUPLEX_STATUS
#define DP83640_PHYSTS
#define DP83640_PHYSTS_SPEED_STATUS
#define DP83640_MICR_INT_OE
#define DP83640_MISR_LINK_INT_EN
#define DP83640_BMSR
#define DP83640_PAGSR
#define DP83640_PHY_ADDR
#define DP83640_PHYSTS_LINK_STATUS
#define DP83640_PTP_COC
#define DP83640_BMCR
#define DP83640_PTP_COC_PTP_CLKOUT_EN
#define DP83640_BMCR_RESET
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
Ipv6Addr address[]
Definition: ipv6.h:316
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Ethernet PHY driver.
Definition: nic.h:308
uint8_t value[]
Definition: tcp.h:369