upd60611_driver.c
Go to the documentation of this file.
1 /**
2  * @file upd60611_driver.c
3  * @brief uPD60611 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 uPD60611 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief uPD60611 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing uPD60611...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = UPD60611_PHY_ADDR;
70  }
71 
72  //Initialize serial management interface
73  if(interface->smiDriver != NULL)
74  {
75  interface->smiDriver->init();
76  }
77 
78  //Reset PHY transceiver
80 
81  //Wait for the reset to complete
83  {
84  }
85 
86  //Dump PHY registers for debugging purpose
87  upd60611DumpPhyReg(interface);
88 
89  //Perform custom configuration
90  upd60611InitHook(interface);
91 
92  //Force the TCP/IP stack to poll the link state at startup
93  interface->phyEvent = TRUE;
94  //Notify the TCP/IP stack of the event
96 
97  //Successful initialization
98  return NO_ERROR;
99 }
100 
101 
102 /**
103  * @brief uPD60611 custom configuration
104  * @param[in] interface Underlying network interface
105  **/
106 
107 __weak_func void upd60611InitHook(NetInterface *interface)
108 {
109 }
110 
111 
112 /**
113  * @brief uPD60611 timer handler
114  * @param[in] interface Underlying network interface
115  **/
116 
117 void upd60611Tick(NetInterface *interface)
118 {
119  uint16_t value;
120  bool_t linkState;
121 
122  //Read basic status register
123  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
124  //Retrieve current link state
125  linkState = (value & UPD60611_BMSR_LINK_STATUS) ? TRUE : FALSE;
126 
127  //Link up event?
128  if(linkState && !interface->linkState)
129  {
130  //Set event flag
131  interface->phyEvent = TRUE;
132  //Notify the TCP/IP stack of the event
134  }
135  //Link down event?
136  else if(!linkState && interface->linkState)
137  {
138  //Set event flag
139  interface->phyEvent = TRUE;
140  //Notify the TCP/IP stack of the event
142  }
143 }
144 
145 
146 /**
147  * @brief Enable interrupts
148  * @param[in] interface Underlying network interface
149  **/
150 
152 {
153 }
154 
155 
156 /**
157  * @brief Disable interrupts
158  * @param[in] interface Underlying network interface
159  **/
160 
162 {
163 }
164 
165 
166 /**
167  * @brief uPD60611 event handler
168  * @param[in] interface Underlying network interface
169  **/
170 
172 {
173  uint16_t value;
174  bool_t linkState;
175 
176  //Any link failure condition is latched in the BMSR register. Reading
177  //the register twice will always return the actual link status
178  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
179  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
180 
181  //Retrieve current link state
182  linkState = (value & UPD60611_BMSR_LINK_STATUS) ? TRUE : FALSE;
183 
184  //Link is up?
185  if(linkState && !interface->linkState)
186  {
187  //Read PHY special control/status register
189 
190  //Check current operation mode
192  {
193  //10BASE-T half-duplex
195  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
196  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
197  break;
198 
199  //10BASE-T full-duplex
201  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
202  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
203  break;
204 
205  //100BASE-TX half-duplex
207  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
208  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
209  break;
210 
211  //100BASE-TX full-duplex
213  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
214  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
215  break;
216 
217  //Unknown operation mode
218  default:
219  //Debug message
220  TRACE_WARNING("Invalid operation mode!\r\n");
221  break;
222  }
223 
224  //Update link state
225  interface->linkState = TRUE;
226 
227  //Adjust MAC configuration parameters for proper operation
228  interface->nicDriver->updateMacConfig(interface);
229 
230  //Process link state change event
231  nicNotifyLinkChange(interface);
232  }
233  //Link is down?
234  else if(!linkState && interface->linkState)
235  {
236  //Update link state
237  interface->linkState = FALSE;
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 upd60611WritePhyReg(NetInterface *interface, uint8_t address,
253  uint16_t data)
254 {
255  //Write the specified PHY register
256  if(interface->smiDriver != NULL)
257  {
258  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
259  interface->phyAddr, address, data);
260  }
261  else
262  {
263  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
264  interface->phyAddr, address, data);
265  }
266 }
267 
268 
269 /**
270  * @brief Read PHY register
271  * @param[in] interface Underlying network interface
272  * @param[in] address PHY register address
273  * @return Register value
274  **/
275 
276 uint16_t upd60611ReadPhyReg(NetInterface *interface, uint8_t address)
277 {
278  uint16_t data;
279 
280  //Read the specified PHY register
281  if(interface->smiDriver != NULL)
282  {
283  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
284  interface->phyAddr, address);
285  }
286  else
287  {
288  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
289  interface->phyAddr, address);
290  }
291 
292  //Return the value of the PHY register
293  return data;
294 }
295 
296 
297 /**
298  * @brief Dump PHY registers for debugging purpose
299  * @param[in] interface Underlying network interface
300  **/
301 
303 {
304  uint8_t i;
305 
306  //Loop through PHY registers
307  for(i = 0; i < 32; i++)
308  {
309  //Display current PHY register
310  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
311  upd60611ReadPhyReg(interface, i));
312  }
313 
314  //Terminate with a line feed
315  TRACE_DEBUG("\r\n");
316 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_INFO(...)
Definition: debug.h:95
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
void upd60611EnableIrq(NetInterface *interface)
Enable interrupts.
void upd60611DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void upd60611Tick(NetInterface *interface)
uPD60611 timer handler
uint16_t upd60611ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void upd60611DisableIrq(NetInterface *interface)
Disable interrupts.
const PhyDriver upd60611PhyDriver
uPD60611 Ethernet PHY driver
__weak_func void upd60611InitHook(NetInterface *interface)
uPD60611 custom configuration
void upd60611WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
void upd60611EventHandler(NetInterface *interface)
uPD60611 event handler
error_t upd60611Init(NetInterface *interface)
uPD60611 PHY transceiver initialization
uPD60611 Ethernet PHY driver
#define UPD60611_PSCSR
#define UPD60611_BMCR_RESET
#define UPD60611_PSCSR_HCDSPEED
#define UPD60611_PHY_ADDR
#define UPD60611_PSCSR_HCDSPEED_10BT_FD
#define UPD60611_BMCR
#define UPD60611_PSCSR_HCDSPEED_10BT_HD
#define UPD60611_PSCSR_HCDSPEED_100BTX_HD
#define UPD60611_BMSR_LINK_STATUS
#define UPD60611_PSCSR_HCDSPEED_100BTX_FD
#define UPD60611_BMSR