lan8742_driver.c
Go to the documentation of this file.
1 /**
2  * @file lan8742_driver.c
3  * @brief LAN8742 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 LAN8742 Ethernet PHY driver
42  **/
43 
45 {
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief LAN8742 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing LAN8742...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = LAN8742_PHY_ADDR;
72  }
73 
74  //Initialize external interrupt line driver
75  if(interface->extIntDriver != NULL)
76  {
77  interface->extIntDriver->init();
78  }
79 
80  //Reset PHY transceiver (soft reset)
82 
83  //Wait for the reset to complete
85  {
86  }
87 
88  //Restore default auto-negotiation advertisement parameters
92 
93  //Enable auto-negotiation
95 
96  //Dump PHY registers for debugging purpose
97  lan8742DumpPhyReg(interface);
98 
99  //The PHY will generate interrupts when link status changes are detected
102 
103  //Force the TCP/IP stack to poll the link state at startup
104  interface->phyEvent = TRUE;
105  //Notify the TCP/IP stack of the event
107 
108  //Successful initialization
109  return NO_ERROR;
110 }
111 
112 
113 /**
114  * @brief LAN8742 timer handler
115  * @param[in] interface Underlying network interface
116  **/
117 
118 void lan8742Tick(NetInterface *interface)
119 {
120  uint16_t value;
121  bool_t linkState;
122 
123  //No external interrupt line driver?
124  if(interface->extIntDriver == NULL)
125  {
126  //Read basic status register
127  value = lan8742ReadPhyReg(interface, LAN8742_BMSR);
128  //Retrieve current link state
129  linkState = (value & LAN8742_BMSR_LINK_STATUS) ? TRUE : FALSE;
130 
131  //Link up event?
132  if(linkState && !interface->linkState)
133  {
134  //Set event flag
135  interface->phyEvent = TRUE;
136  //Notify the TCP/IP stack of the event
138  }
139  //Link down event?
140  else if(!linkState && interface->linkState)
141  {
142  //Set event flag
143  interface->phyEvent = TRUE;
144  //Notify the TCP/IP stack of the event
146  }
147  }
148 }
149 
150 
151 /**
152  * @brief Enable interrupts
153  * @param[in] interface Underlying network interface
154  **/
155 
157 {
158  //Enable PHY transceiver interrupts
159  if(interface->extIntDriver != NULL)
160  {
161  interface->extIntDriver->enableIrq();
162  }
163 }
164 
165 
166 /**
167  * @brief Disable interrupts
168  * @param[in] interface Underlying network interface
169  **/
170 
172 {
173  //Disable PHY transceiver interrupts
174  if(interface->extIntDriver != NULL)
175  {
176  interface->extIntDriver->disableIrq();
177  }
178 }
179 
180 
181 /**
182  * @brief LAN8742 event handler
183  * @param[in] interface Underlying network interface
184  **/
185 
187 {
188  uint16_t value;
189 
190  //Read status register to acknowledge the interrupt
191  value = lan8742ReadPhyReg(interface, LAN8742_ISR);
192 
193  //Link status change?
195  {
196  //Any link failure condition is latched in the BMSR register. Reading
197  //the register twice will always return the actual link status
198  value = lan8742ReadPhyReg(interface, LAN8742_BMSR);
199  value = lan8742ReadPhyReg(interface, LAN8742_BMSR);
200 
201  //Link is up?
203  {
204  //Read PHY special control/status register
205  value = lan8742ReadPhyReg(interface, LAN8742_PSCSR);
206 
207  //Check current operation mode
208  switch(value & LAN8742_PSCSR_HCDSPEED)
209  {
210  //10BASE-T half-duplex
212  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
213  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
214  break;
215  //10BASE-T full-duplex
217  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
218  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
219  break;
220  //100BASE-TX half-duplex
222  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
223  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
224  break;
225  //100BASE-TX full-duplex
227  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
228  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
229  break;
230  //Unknown operation mode
231  default:
232  //Debug message
233  TRACE_WARNING("Invalid operation mode!\r\n");
234  break;
235  }
236 
237  //Update link state
238  interface->linkState = TRUE;
239 
240  //Adjust MAC configuration parameters for proper operation
241  interface->nicDriver->updateMacConfig(interface);
242  }
243  else
244  {
245  //Update link state
246  interface->linkState = FALSE;
247  }
248 
249  //Process link state change event
250  nicNotifyLinkChange(interface);
251  }
252 }
253 
254 
255 /**
256  * @brief Write PHY register
257  * @param[in] interface Underlying network interface
258  * @param[in] address PHY register address
259  * @param[in] data Register value
260  **/
261 
262 void lan8742WritePhyReg(NetInterface *interface, uint8_t address,
263  uint16_t data)
264 {
265  //Write the specified PHY register
266  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
267  interface->phyAddr, address, data);
268 }
269 
270 
271 /**
272  * @brief Read PHY register
273  * @param[in] interface Underlying network interface
274  * @param[in] address PHY register address
275  * @return Register value
276  **/
277 
278 uint16_t lan8742ReadPhyReg(NetInterface *interface, uint8_t address)
279 {
280  //Read the specified PHY register
281  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
282  interface->phyAddr, address);
283 }
284 
285 
286 /**
287  * @brief Dump PHY registers for debugging purpose
288  * @param[in] interface Underlying network interface
289  **/
290 
292 {
293  uint8_t i;
294 
295  //Loop through PHY registers
296  for(i = 0; i < 32; i++)
297  {
298  //Display current PHY register
299  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
300  lan8742ReadPhyReg(interface, i));
301  }
302 
303  //Terminate with a line feed
304  TRACE_DEBUG("\r\n");
305 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
#define LAN8742_PSCSR
#define LAN8742_ANAR_SELECTOR_DEFAULT
int bool_t
Definition: compiler_port.h:49
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
#define LAN8742_ISR
#define TRUE
Definition: os_port.h:50
PHY driver.
Definition: nic.h:214
#define LAN8742_PSCSR_HCDSPEED_100BTX_FD
void lan8742DisableIrq(NetInterface *interface)
Disable interrupts.
void lan8742EnableIrq(NetInterface *interface)
Enable interrupts.
#define LAN8742_BMCR_AN_EN
void lan8742EventHandler(NetInterface *interface)
LAN8742 event handler.
#define LAN8742_ANAR
#define SMI_OPCODE_WRITE
Definition: nic.h:62
#define LAN8742_BMSR_LINK_STATUS
#define FALSE
Definition: os_port.h:46
#define LAN8742_BMCR_RESET
uint16_t lan8742ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
error_t
Error codes.
Definition: error.h:42
#define LAN8742_PSCSR_HCDSPEED_10BT_HD
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
void lan8742Tick(NetInterface *interface)
LAN8742 timer handler.
#define LAN8742_BMSR
OsEvent netEvent
Definition: net.c:77
#define SMI_OPCODE_READ
Definition: nic.h:63
error_t lan8742Init(NetInterface *interface)
LAN8742 PHY transceiver initialization.
#define TRACE_INFO(...)
Definition: debug.h:94
void lan8742WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define LAN8742_ANAR_100BTX_HD
void lan8742DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define TRACE_WARNING(...)
Definition: debug.h:84
#define LAN8742_PHY_ADDR
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define LAN8742_PSCSR_HCDSPEED_100BTX_HD
#define LAN8742_ANAR_10BT_FD
const PhyDriver lan8742PhyDriver
LAN8742 Ethernet PHY driver.
LAN8742 Ethernet PHY transceiver.
#define LAN8742_IMR
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
#define LAN8742_IMR_AN_COMPLETE
#define LAN8742_ANAR_10BT_HD
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define LAN8742_ANAR_100BTX_FD
#define LAN8742_BMCR
#define LAN8742_PSCSR_HCDSPEED_10BT_FD
@ 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 LAN8742_PSCSR_HCDSPEED
#define LAN8742_IMR_LINK_DOWN
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.