upd60611_driver.c
Go to the documentation of this file.
1 /**
2  * @file upd60611_driver.c
3  * @brief uPD60611 Ethernet PHY transceiver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 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.6
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  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief uPD60611 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing uPD60611...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = UPD60611_PHY_ADDR;
72  }
73 
74  //Reset PHY transceiver
76 
77  //Wait for the reset to complete
79  {
80  }
81 
82  //Dump PHY registers for debugging purpose
83  upd60611DumpPhyReg(interface);
84 
85  //Force the TCP/IP stack to poll the link state at startup
86  interface->phyEvent = TRUE;
87  //Notify the TCP/IP stack of the event
89 
90  //Successful initialization
91  return NO_ERROR;
92 }
93 
94 
95 /**
96  * @brief uPD60611 timer handler
97  * @param[in] interface Underlying network interface
98  **/
99 
100 void upd60611Tick(NetInterface *interface)
101 {
102  uint16_t value;
103  bool_t linkState;
104 
105  //Read basic status register
106  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
107  //Retrieve current link state
108  linkState = (value & UPD60611_BMSR_LINK_STATUS) ? TRUE : FALSE;
109 
110  //Link up event?
111  if(linkState && !interface->linkState)
112  {
113  //Set event flag
114  interface->phyEvent = TRUE;
115  //Notify the TCP/IP stack of the event
117  }
118  //Link down event?
119  else if(!linkState && interface->linkState)
120  {
121  //Set event flag
122  interface->phyEvent = TRUE;
123  //Notify the TCP/IP stack of the event
125  }
126 }
127 
128 
129 /**
130  * @brief Enable interrupts
131  * @param[in] interface Underlying network interface
132  **/
133 
135 {
136 }
137 
138 
139 /**
140  * @brief Disable interrupts
141  * @param[in] interface Underlying network interface
142  **/
143 
145 {
146 }
147 
148 
149 /**
150  * @brief uPD60611 event handler
151  * @param[in] interface Underlying network interface
152  **/
153 
155 {
156  uint16_t value;
157  bool_t linkState;
158 
159  //Any link failure condition is latched in the BMSR register. Reading
160  //the register twice will always return the actual link status
161  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
162  value = upd60611ReadPhyReg(interface, UPD60611_BMSR);
163 
164  //Retrieve current link state
165  linkState = (value & UPD60611_BMSR_LINK_STATUS) ? TRUE : FALSE;
166 
167  //Link is up?
168  if(linkState && !interface->linkState)
169  {
170  //Read PHY special control/status register
172 
173  //Check current operation mode
175  {
176  //10BASE-T half-duplex
178  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
179  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
180  break;
181  //10BASE-T full-duplex
183  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
184  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
185  break;
186  //100BASE-TX half-duplex
188  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
189  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
190  break;
191  //100BASE-TX full-duplex
193  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
194  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
195  break;
196  //Unknown operation mode
197  default:
198  //Debug message
199  TRACE_WARNING("Invalid operation mode!\r\n");
200  break;
201  }
202 
203  //Update link state
204  interface->linkState = TRUE;
205 
206  //Adjust MAC configuration parameters for proper operation
207  interface->nicDriver->updateMacConfig(interface);
208 
209  //Process link state change event
210  nicNotifyLinkChange(interface);
211  }
212  //Link is down?
213  else if(!linkState && interface->linkState)
214  {
215  //Update link state
216  interface->linkState = FALSE;
217 
218  //Process link state change event
219  nicNotifyLinkChange(interface);
220  }
221 }
222 
223 
224 /**
225  * @brief Write PHY register
226  * @param[in] interface Underlying network interface
227  * @param[in] address PHY register address
228  * @param[in] data Register value
229  **/
230 
231 void upd60611WritePhyReg(NetInterface *interface, uint8_t address,
232  uint16_t data)
233 {
234  //Write the specified PHY register
235  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
236  interface->phyAddr, address, data);
237 }
238 
239 
240 /**
241  * @brief Read PHY register
242  * @param[in] interface Underlying network interface
243  * @param[in] address PHY register address
244  * @return Register value
245  **/
246 
247 uint16_t upd60611ReadPhyReg(NetInterface *interface, uint8_t address)
248 {
249  //Read the specified PHY register
250  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
251  interface->phyAddr, address);
252 }
253 
254 
255 /**
256  * @brief Dump PHY registers for debugging purpose
257  * @param[in] interface Underlying network interface
258  **/
259 
261 {
262  uint8_t i;
263 
264  //Loop through PHY registers
265  for(i = 0; i < 32; i++)
266  {
267  //Display current PHY register
268  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
269  upd60611ReadPhyReg(interface, i));
270  }
271 
272  //Terminate with a line feed
273  TRACE_DEBUG("\r\n");
274 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
int bool_t
Definition: compiler_port.h:49
void upd60611DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
#define TRUE
Definition: os_port.h:50
PHY driver.
Definition: nic.h:214
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:62
#define FALSE
Definition: os_port.h:46
uPD60611 Ethernet PHY transceiver
error_t
Error codes.
Definition: error.h:42
#define UPD60611_PHY_ADDR
#define UPD60611_BMCR_RESET
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
#define UPD60611_PSCSR_HCDSPEED_100BTX_FD
OsEvent netEvent
Definition: net.c:77
#define SMI_OPCODE_READ
Definition: nic.h:63
#define UPD60611_BMCR
#define TRACE_INFO(...)
Definition: debug.h:94
#define UPD60611_PSCSR_HCDSPEED_10BT_HD
#define UPD60611_PSCSR_HCDSPEED_10BT_FD
void upd60611EnableIrq(NetInterface *interface)
Enable interrupts.
#define TRACE_WARNING(...)
Definition: debug.h:84
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define UPD60611_BMSR
void upd60611WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define UPD60611_PSCSR_HCDSPEED_100BTX_HD
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
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.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:106
uint8_t value[]
Definition: dtls_misc.h:150
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
#define UPD60611_PSCSR
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.