lan8720_driver.c
Go to the documentation of this file.
1 /**
2  * @file lan8720_driver.c
3  * @brief LAN8720 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.4
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 LAN8720 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief LAN8720 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing LAN8720...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = LAN8720_PHY_ADDR;
70  }
71 
72  //Initialize serial management interface
73  if(interface->smiDriver != NULL)
74  {
75  interface->smiDriver->init();
76  }
77 
78  //Initialize external interrupt line driver
79  if(interface->extIntDriver != NULL)
80  {
81  interface->extIntDriver->init();
82  }
83 
84  //Reset PHY transceiver (soft reset)
86 
87  //Wait for the reset to complete
89  {
90  }
91 
92  //Dump PHY registers for debugging purpose
93  lan8720DumpPhyReg(interface);
94 
95  //Restore default auto-negotiation advertisement parameters
99 
100  //Enable auto-negotiation
102 
103  //The PHY will generate interrupts when link status changes are detected
106 
107  //Perform custom configuration
108  lan8720InitHook(interface);
109 
110  //Force the TCP/IP stack to poll the link state at startup
111  interface->phyEvent = TRUE;
112  //Notify the TCP/IP stack of the event
114 
115  //Successful initialization
116  return NO_ERROR;
117 }
118 
119 
120 /**
121  * @brief LAN8720 custom configuration
122  * @param[in] interface Underlying network interface
123  **/
124 
125 __weak_func void lan8720InitHook(NetInterface *interface)
126 {
127 }
128 
129 
130 /**
131  * @brief LAN8720 timer handler
132  * @param[in] interface Underlying network interface
133  **/
134 
135 void lan8720Tick(NetInterface *interface)
136 {
137  uint16_t value;
138  bool_t linkState;
139 
140  //No external interrupt line driver?
141  if(interface->extIntDriver == NULL)
142  {
143  //Read basic status register
144  value = lan8720ReadPhyReg(interface, LAN8720_BMSR);
145  //Retrieve current link state
146  linkState = (value & LAN8720_BMSR_LINK_STATUS) ? TRUE : FALSE;
147 
148  //Link up event?
149  if(linkState && !interface->linkState)
150  {
151  //Set event flag
152  interface->phyEvent = TRUE;
153  //Notify the TCP/IP stack of the event
155  }
156  //Link down event?
157  else if(!linkState && interface->linkState)
158  {
159  //Set event flag
160  interface->phyEvent = TRUE;
161  //Notify the TCP/IP stack of the event
163  }
164  }
165 }
166 
167 
168 /**
169  * @brief Enable interrupts
170  * @param[in] interface Underlying network interface
171  **/
172 
174 {
175  //Enable PHY transceiver interrupts
176  if(interface->extIntDriver != NULL)
177  {
178  interface->extIntDriver->enableIrq();
179  }
180 }
181 
182 
183 /**
184  * @brief Disable interrupts
185  * @param[in] interface Underlying network interface
186  **/
187 
189 {
190  //Disable PHY transceiver interrupts
191  if(interface->extIntDriver != NULL)
192  {
193  interface->extIntDriver->disableIrq();
194  }
195 }
196 
197 
198 /**
199  * @brief LAN8720 event handler
200  * @param[in] interface Underlying network interface
201  **/
202 
204 {
205  uint16_t value;
206 
207  //Read status register to acknowledge the interrupt
208  value = lan8720ReadPhyReg(interface, LAN8720_ISR);
209 
210  //Link status change?
212  {
213  //Any link failure condition is latched in the BMSR register. Reading
214  //the register twice will always return the actual link status
215  value = lan8720ReadPhyReg(interface, LAN8720_BMSR);
216  value = lan8720ReadPhyReg(interface, LAN8720_BMSR);
217 
218  //Link is up?
219  if((value & LAN8720_BMSR_LINK_STATUS) != 0)
220  {
221  //Read PHY special control/status register
222  value = lan8720ReadPhyReg(interface, LAN8720_PSCSR);
223 
224  //Check current operation mode
225  switch(value & LAN8720_PSCSR_HCDSPEED)
226  {
227  //10BASE-T half-duplex
229  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
230  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
231  break;
232 
233  //10BASE-T full-duplex
235  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
236  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
237  break;
238 
239  //100BASE-TX half-duplex
241  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
242  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
243  break;
244 
245  //100BASE-TX full-duplex
247  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
248  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
249  break;
250 
251  //Unknown operation mode
252  default:
253  //Debug message
254  TRACE_WARNING("Invalid operation mode!\r\n");
255  break;
256  }
257 
258  //Update link state
259  interface->linkState = TRUE;
260 
261  //Adjust MAC configuration parameters for proper operation
262  interface->nicDriver->updateMacConfig(interface);
263  }
264  else
265  {
266  //Update link state
267  interface->linkState = FALSE;
268  }
269 
270  //Process link state change event
271  nicNotifyLinkChange(interface);
272  }
273 }
274 
275 
276 /**
277  * @brief Write PHY register
278  * @param[in] interface Underlying network interface
279  * @param[in] address PHY register address
280  * @param[in] data Register value
281  **/
282 
283 void lan8720WritePhyReg(NetInterface *interface, uint8_t address,
284  uint16_t data)
285 {
286  //Write the specified PHY register
287  if(interface->smiDriver != NULL)
288  {
289  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
290  interface->phyAddr, address, data);
291  }
292  else
293  {
294  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
295  interface->phyAddr, address, data);
296  }
297 }
298 
299 
300 /**
301  * @brief Read PHY register
302  * @param[in] interface Underlying network interface
303  * @param[in] address PHY register address
304  * @return Register value
305  **/
306 
307 uint16_t lan8720ReadPhyReg(NetInterface *interface, uint8_t address)
308 {
309  uint16_t data;
310 
311  //Read the specified PHY register
312  if(interface->smiDriver != NULL)
313  {
314  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
315  interface->phyAddr, address);
316  }
317  else
318  {
319  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
320  interface->phyAddr, address);
321  }
322 
323  //Return the value of the PHY register
324  return data;
325 }
326 
327 
328 /**
329  * @brief Dump PHY registers for debugging purpose
330  * @param[in] interface Underlying network interface
331  **/
332 
334 {
335  uint8_t i;
336 
337  //Loop through PHY registers
338  for(i = 0; i < 32; i++)
339  {
340  //Display current PHY register
341  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
342  lan8720ReadPhyReg(interface, i));
343  }
344 
345  //Terminate with a line feed
346  TRACE_DEBUG("\r\n");
347 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
#define LAN8720_ANAR_100BTX_HD
#define LAN8720_PSCSR
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
void lan8720DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define LAN8720_IMR_AN_COMPLETE
#define LAN8720_ANAR
#define LAN8720_BMCR_AN_EN
const PhyDriver lan8720PhyDriver
LAN8720 Ethernet PHY driver.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define LAN8720_BMCR
void lan8720EnableIrq(NetInterface *interface)
Enable interrupts.
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
#define LAN8720_PHY_ADDR
#define LAN8720_PSCSR_HCDSPEED_10BT_FD
void lan8720WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define LAN8720_BMSR_LINK_STATUS
#define LAN8720_ISR
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
LAN8720 Ethernet PHY driver.
#define LAN8720_PSCSR_HCDSPEED_10BT_HD
#define SMI_OPCODE_READ
Definition: nic.h:67
#define LAN8720_IMR_LINK_DOWN
#define LAN8720_PSCSR_HCDSPEED
#define TRACE_INFO(...)
Definition: debug.h:95
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define LAN8720_BMCR_RESET
#define LAN8720_PSCSR_HCDSPEED_100BTX_HD
Ipv6Addr address[]
Definition: ipv6.h:325
#define LAN8720_IMR
#define LAN8720_ANAR_SELECTOR_DEFAULT
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
#define LAN8720_ANAR_10BT_FD
void lan8720DisableIrq(NetInterface *interface)
Disable interrupts.
#define LAN8720_PSCSR_HCDSPEED_100BTX_FD
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define LAN8720_BMSR
void lan8720Tick(NetInterface *interface)
LAN8720 timer handler.
TCP/IP stack core.
uint16_t lan8720ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void lan8720EventHandler(NetInterface *interface)
LAN8720 event handler.
#define LAN8720_ANAR_100BTX_FD
error_t lan8720Init(NetInterface *interface)
LAN8720 PHY transceiver initialization.
#define LAN8720_ANAR_10BT_HD
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
__weak_func void lan8720InitHook(NetInterface *interface)
LAN8720 custom configuration.