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-2020 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.8
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  //Force the TCP/IP stack to poll the link state at startup
90  interface->phyEvent = TRUE;
91  //Notify the TCP/IP stack of the event
93 
94  //Successful initialization
95  return NO_ERROR;
96 }
97 
98 
99 /**
100  * @brief uPD60611 timer handler
101  * @param[in] interface Underlying network interface
102  **/
103 
104 void upd60611Tick(NetInterface *interface)
105 {
106  uint16_t value;
107  bool_t linkState;
108 
109  //Read basic status register
110  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
111  //Retrieve current link state
112  linkState = (value & UPD60611_BMSR_LINK_STATUS) ? TRUE : FALSE;
113 
114  //Link up event?
115  if(linkState && !interface->linkState)
116  {
117  //Set event flag
118  interface->phyEvent = TRUE;
119  //Notify the TCP/IP stack of the event
121  }
122  //Link down event?
123  else if(!linkState && interface->linkState)
124  {
125  //Set event flag
126  interface->phyEvent = TRUE;
127  //Notify the TCP/IP stack of the event
129  }
130 }
131 
132 
133 /**
134  * @brief Enable interrupts
135  * @param[in] interface Underlying network interface
136  **/
137 
139 {
140 }
141 
142 
143 /**
144  * @brief Disable interrupts
145  * @param[in] interface Underlying network interface
146  **/
147 
149 {
150 }
151 
152 
153 /**
154  * @brief uPD60611 event handler
155  * @param[in] interface Underlying network interface
156  **/
157 
159 {
160  uint16_t value;
161  bool_t linkState;
162 
163  //Any link failure condition is latched in the BMSR register. Reading
164  //the register twice will always return the actual link status
165  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
166  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
167 
168  //Retrieve current link state
169  linkState = (value & UPD60611_BMSR_LINK_STATUS) ? TRUE : FALSE;
170 
171  //Link is up?
172  if(linkState && !interface->linkState)
173  {
174  //Read PHY special control/status register
176 
177  //Check current operation mode
179  {
180  //10BASE-T half-duplex
182  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
183  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
184  break;
185  //10BASE-T full-duplex
187  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
188  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
189  break;
190  //100BASE-TX half-duplex
192  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
193  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
194  break;
195  //100BASE-TX full-duplex
197  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
198  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
199  break;
200  //Unknown operation mode
201  default:
202  //Debug message
203  TRACE_WARNING("Invalid operation mode!\r\n");
204  break;
205  }
206 
207  //Update link state
208  interface->linkState = TRUE;
209 
210  //Adjust MAC configuration parameters for proper operation
211  interface->nicDriver->updateMacConfig(interface);
212 
213  //Process link state change event
214  nicNotifyLinkChange(interface);
215  }
216  //Link is down?
217  else if(!linkState && interface->linkState)
218  {
219  //Update link state
220  interface->linkState = FALSE;
221 
222  //Process link state change event
223  nicNotifyLinkChange(interface);
224  }
225 }
226 
227 
228 /**
229  * @brief Write PHY register
230  * @param[in] interface Underlying network interface
231  * @param[in] address PHY register address
232  * @param[in] data Register value
233  **/
234 
235 void upd60611WritePhyReg(NetInterface *interface, uint8_t address,
236  uint16_t data)
237 {
238  //Write the specified PHY register
239  if(interface->smiDriver != NULL)
240  {
241  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
242  interface->phyAddr, address, data);
243  }
244  else
245  {
246  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
247  interface->phyAddr, address, data);
248  }
249 }
250 
251 
252 /**
253  * @brief Read PHY register
254  * @param[in] interface Underlying network interface
255  * @param[in] address PHY register address
256  * @return Register value
257  **/
258 
259 uint16_t upd60611ReadPhyReg(NetInterface *interface, uint8_t address)
260 {
261  uint16_t data;
262 
263  //Read the specified PHY register
264  if(interface->smiDriver != NULL)
265  {
266  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
267  interface->phyAddr, address);
268  }
269  else
270  {
271  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
272  interface->phyAddr, address);
273  }
274 
275  //Return the value of the PHY register
276  return data;
277 }
278 
279 
280 /**
281  * @brief Dump PHY registers for debugging purpose
282  * @param[in] interface Underlying network interface
283  **/
284 
286 {
287  uint8_t i;
288 
289  //Loop through PHY registers
290  for(i = 0; i < 32; i++)
291  {
292  //Display current PHY register
293  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
294  upd60611ReadPhyReg(interface, i));
295  }
296 
297  //Terminate with a line feed
298  TRACE_DEBUG("\r\n");
299 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
int bool_t
Definition: compiler_port.h:49
void upd60611DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define netEvent
Definition: net_legacy.h:267
uint8_t data[]
Definition: ethernet.h:209
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:292
const PhyDriver upd60611PhyDriver
uPD60611 Ethernet PHY driver
error_t upd60611Init(NetInterface *interface)
uPD60611 PHY transceiver initialization
#define UPD60611_BMSR_LINK_STATUS
void upd60611Tick(NetInterface *interface)
uPD60611 timer handler
uint16_t upd60611ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define UPD60611_PSCSR_HCDSPEED
#define SMI_OPCODE_WRITE
Definition: nic.h:65
#define FALSE
Definition: os_port.h:46
uPD60611 Ethernet PHY driver
error_t
Error codes.
Definition: error.h:42
#define UPD60611_PHY_ADDR
#define UPD60611_BMCR_RESET
uint8_t value[]
Definition: tcp.h:332
#define NetInterface
Definition: net.h:36
#define UPD60611_PSCSR_HCDSPEED_100BTX_FD
#define SMI_OPCODE_READ
Definition: nic.h:66
#define UPD60611_BMCR
#define TRACE_INFO(...)
Definition: debug.h:95
#define UPD60611_PSCSR_HCDSPEED_10BT_HD
#define UPD60611_PSCSR_HCDSPEED_10BT_FD
void upd60611EnableIrq(NetInterface *interface)
Enable interrupts.
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define UPD60611_BMSR
void upd60611WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define UPD60611_PSCSR_HCDSPEED_100BTX_HD
void upd60611EventHandler(NetInterface *interface)
uPD60611 event handler
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void upd60611DisableIrq(NetInterface *interface)
Disable interrupts.
TCP/IP stack core.
#define UPD60611_PSCSR
Success.
Definition: error.h:44
Debugging facilities.