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-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 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  //The PHY will generate interrupts when link status changes are detected
98 
99  //Force the TCP/IP stack to poll the link state at startup
100  interface->phyEvent = TRUE;
101  //Notify the TCP/IP stack of the event
103 
104  //Successful initialization
105  return NO_ERROR;
106 }
107 
108 
109 /**
110  * @brief LAN8720 timer handler
111  * @param[in] interface Underlying network interface
112  **/
113 
114 void lan8720Tick(NetInterface *interface)
115 {
116  uint16_t value;
117  bool_t linkState;
118 
119  //No external interrupt line driver?
120  if(interface->extIntDriver == NULL)
121  {
122  //Read basic status register
123  value = lan8720ReadPhyReg(interface, LAN8720_BMSR);
124  //Retrieve current link state
125  linkState = (value & LAN8720_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 /**
148  * @brief Enable interrupts
149  * @param[in] interface Underlying network interface
150  **/
151 
153 {
154  //Enable PHY transceiver interrupts
155  if(interface->extIntDriver != NULL)
156  {
157  interface->extIntDriver->enableIrq();
158  }
159 }
160 
161 
162 /**
163  * @brief Disable interrupts
164  * @param[in] interface Underlying network interface
165  **/
166 
168 {
169  //Disable PHY transceiver interrupts
170  if(interface->extIntDriver != NULL)
171  {
172  interface->extIntDriver->disableIrq();
173  }
174 }
175 
176 
177 /**
178  * @brief LAN8720 event handler
179  * @param[in] interface Underlying network interface
180  **/
181 
183 {
184  uint16_t value;
185 
186  //Read status register to acknowledge the interrupt
187  value = lan8720ReadPhyReg(interface, LAN8720_ISR);
188 
189  //Link status change?
191  {
192  //Any link failure condition is latched in the BMSR register. Reading
193  //the register twice will always return the actual link status
194  value = lan8720ReadPhyReg(interface, LAN8720_BMSR);
195  value = lan8720ReadPhyReg(interface, LAN8720_BMSR);
196 
197  //Link is up?
198  if((value & LAN8720_BMSR_LINK_STATUS) != 0)
199  {
200  //Read PHY special control/status register
201  value = lan8720ReadPhyReg(interface, LAN8720_PSCSR);
202 
203  //Check current operation mode
204  switch(value & LAN8720_PSCSR_HCDSPEED)
205  {
206  //10BASE-T half-duplex
208  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
209  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
210  break;
211  //10BASE-T full-duplex
213  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
214  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
215  break;
216  //100BASE-TX half-duplex
218  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
219  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
220  break;
221  //100BASE-TX full-duplex
223  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
224  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
225  break;
226  //Unknown operation mode
227  default:
228  //Debug message
229  TRACE_WARNING("Invalid operation mode!\r\n");
230  break;
231  }
232 
233  //Update link state
234  interface->linkState = TRUE;
235 
236  //Adjust MAC configuration parameters for proper operation
237  interface->nicDriver->updateMacConfig(interface);
238  }
239  else
240  {
241  //Update link state
242  interface->linkState = FALSE;
243  }
244 
245  //Process link state change event
246  nicNotifyLinkChange(interface);
247  }
248 }
249 
250 
251 /**
252  * @brief Write PHY register
253  * @param[in] interface Underlying network interface
254  * @param[in] address PHY register address
255  * @param[in] data Register value
256  **/
257 
258 void lan8720WritePhyReg(NetInterface *interface, uint8_t address,
259  uint16_t data)
260 {
261  //Write the specified PHY register
262  if(interface->smiDriver != NULL)
263  {
264  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
265  interface->phyAddr, address, data);
266  }
267  else
268  {
269  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
270  interface->phyAddr, address, data);
271  }
272 }
273 
274 
275 /**
276  * @brief Read PHY register
277  * @param[in] interface Underlying network interface
278  * @param[in] address PHY register address
279  * @return Register value
280  **/
281 
282 uint16_t lan8720ReadPhyReg(NetInterface *interface, uint8_t address)
283 {
284  uint16_t data;
285 
286  //Read the specified PHY register
287  if(interface->smiDriver != NULL)
288  {
289  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
290  interface->phyAddr, address);
291  }
292  else
293  {
294  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
295  interface->phyAddr, address);
296  }
297 
298  //Return the value of the PHY register
299  return data;
300 }
301 
302 
303 /**
304  * @brief Dump PHY registers for debugging purpose
305  * @param[in] interface Underlying network interface
306  **/
307 
309 {
310  uint8_t i;
311 
312  //Loop through PHY registers
313  for(i = 0; i < 32; i++)
314  {
315  //Display current PHY register
316  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
317  lan8720ReadPhyReg(interface, i));
318  }
319 
320  //Terminate with a line feed
321  TRACE_DEBUG("\r\n");
322 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
#define LAN8720_PSCSR
int bool_t
Definition: compiler_port.h:49
#define netEvent
Definition: net_legacy.h:267
uint8_t data[]
Definition: ethernet.h:209
void lan8720DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:292
#define LAN8720_IMR_AN_COMPLETE
const PhyDriver lan8720PhyDriver
LAN8720 Ethernet PHY driver.
#define SMI_OPCODE_WRITE
Definition: nic.h:65
#define LAN8720_BMCR
void lan8720EnableIrq(NetInterface *interface)
Enable interrupts.
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
#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
uint8_t value[]
Definition: tcp.h:332
#define LAN8720_ISR
#define NetInterface
Definition: net.h:36
LAN8720 Ethernet PHY driver.
#define LAN8720_PSCSR_HCDSPEED_10BT_HD
#define SMI_OPCODE_READ
Definition: nic.h:66
#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
#define LAN8720_IMR
void lan8720DisableIrq(NetInterface *interface)
Disable interrupts.
#define LAN8720_PSCSR_HCDSPEED_100BTX_FD
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#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.
error_t lan8720Init(NetInterface *interface)
LAN8720 PHY transceiver initialization.
Success.
Definition: error.h:44
Debugging facilities.