lan8742_driver.c
Go to the documentation of this file.
1 /**
2  * @file lan8742_driver.c
3  * @brief LAN8742 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 LAN8742 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief LAN8742 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing LAN8742...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = LAN8742_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  //Restore default auto-negotiation advertisement parameters
96 
97  //Enable auto-negotiation
99 
100  //Dump PHY registers for debugging purpose
101  lan8742DumpPhyReg(interface);
102 
103  //The PHY will generate interrupts when link status changes are detected
106 
107  //Perform custom configuration
108  lan8742InitHook(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 LAN8742 custom configuration
122  * @param[in] interface Underlying network interface
123  **/
124 
125 __weak_func void lan8742InitHook(NetInterface *interface)
126 {
127 }
128 
129 
130 /**
131  * @brief LAN8742 timer handler
132  * @param[in] interface Underlying network interface
133  **/
134 
135 void lan8742Tick(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 = lan8742ReadPhyReg(interface, LAN8742_BMSR);
145  //Retrieve current link state
146  linkState = (value & LAN8742_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 LAN8742 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 = lan8742ReadPhyReg(interface, LAN8742_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 = lan8742ReadPhyReg(interface, LAN8742_BMSR);
216  value = lan8742ReadPhyReg(interface, LAN8742_BMSR);
217 
218  //Link is up?
219  if((value & LAN8742_BMSR_LINK_STATUS) != 0)
220  {
221  //Read PHY special control/status register
222  value = lan8742ReadPhyReg(interface, LAN8742_PSCSR);
223 
224  //Check current operation mode
225  switch(value & LAN8742_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 lan8742WritePhyReg(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 lan8742ReadPhyReg(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  lan8742ReadPhyReg(interface, i));
343  }
344 
345  //Terminate with a line feed
346  TRACE_DEBUG("\r\n");
347 }
348 
349 
350 /**
351  * @brief Write MMD register
352  * @param[in] interface Underlying network interface
353  * @param[in] devAddr Device address
354  * @param[in] regAddr Register address
355  * @param[in] data MMD register value
356  **/
357 
358 void lan8742WriteMmdReg(NetInterface *interface, uint8_t devAddr,
359  uint16_t regAddr, uint16_t data)
360 {
361  //Select register operation
364 
365  //Write MMD register address
367 
368  //Select data operation
371 
372  //Write the content of the MMD register
374 }
375 
376 
377 /**
378  * @brief Read MMD register
379  * @param[in] interface Underlying network interface
380  * @param[in] devAddr Device address
381  * @param[in] regAddr Register address
382  * @return MMD register value
383  **/
384 
385 uint16_t lan8742ReadMmdReg(NetInterface *interface, uint8_t devAddr,
386  uint16_t regAddr)
387 {
388  //Select register operation
391 
392  //Write MMD register address
394 
395  //Select data operation
398 
399  //Read the content of the MMD register
400  return lan8742ReadPhyReg(interface, LAN8742_MMDAADR);
401 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
#define LAN8742_PSCSR
#define LAN8742_ANAR_SELECTOR_DEFAULT
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define LAN8742_ISR
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
#define LAN8742_PSCSR_HCDSPEED_100BTX_FD
uint8_t data[]
Definition: ethernet.h:222
void lan8742DisableIrq(NetInterface *interface)
Disable interrupts.
void lan8742EnableIrq(NetInterface *interface)
Enable interrupts.
#define LAN8742_MMDAADR
#define LAN8742_BMCR_AN_EN
void lan8742EventHandler(NetInterface *interface)
LAN8742 event handler.
#define LAN8742_ANAR
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define LAN8742_BMSR_LINK_STATUS
#define FALSE
Definition: os_port.h:46
#define LAN8742_MMDACR_FUNC_DATA_NO_POST_INC
#define LAN8742_BMCR_RESET
uint16_t lan8742ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define LAN8742_MMDACR_FUNC_ADDR
error_t
Error codes.
Definition: error.h:43
#define LAN8742_PSCSR_HCDSPEED_10BT_HD
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
void lan8742Tick(NetInterface *interface)
LAN8742 timer handler.
#define LAN8742_BMSR
#define SMI_OPCODE_READ
Definition: nic.h:67
error_t lan8742Init(NetInterface *interface)
LAN8742 PHY transceiver initialization.
#define TRACE_INFO(...)
Definition: debug.h:95
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:85
#define LAN8742_PHY_ADDR
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define LAN8742_MMDACR_DEVAD
uint16_t regAddr
#define LAN8742_PSCSR_HCDSPEED_100BTX_HD
#define LAN8742_ANAR_10BT_FD
Ipv6Addr address[]
Definition: ipv6.h:325
const PhyDriver lan8742PhyDriver
LAN8742 Ethernet PHY driver.
LAN8742 Ethernet PHY driver.
uint16_t lan8742ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
#define LAN8742_IMR
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
#define LAN8742_MMDACR
#define LAN8742_IMR_AN_COMPLETE
#define LAN8742_ANAR_10BT_HD
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:112
TCP/IP stack core.
void lan8742WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
#define LAN8742_PSCSR_HCDSPEED
#define LAN8742_IMR_LINK_DOWN
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
__weak_func void lan8742InitHook(NetInterface *interface)
LAN8742 custom configuration.