lan8841_driver.c
Go to the documentation of this file.
1 /**
2  * @file lan8841_driver.c
3  * @brief LAN8841 Gigabit 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.0
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 LAN8841 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief LAN8841 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing LAN8841...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = LAN8841_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
86 
87  //Wait for the reset to complete
89  {
90  }
91 
92  //Dump PHY registers for debugging purpose
93  lan8841DumpPhyReg(interface);
94 
95  //Perform custom configuration
96  lan8841InitHook(interface);
97 
98  //Force the TCP/IP stack to poll the link state at startup
99  interface->phyEvent = TRUE;
100  //Notify the TCP/IP stack of the event
102 
103  //Successful initialization
104  return NO_ERROR;
105 }
106 
107 
108 /**
109  * @brief LAN8841 custom configuration
110  * @param[in] interface Underlying network interface
111  **/
112 
113 __weak_func void lan8841InitHook(NetInterface *interface)
114 {
115  uint16_t value;
116 
117  //If MAC does not provide any delay for the TXC, the device may add a fixed
118  //2ns delay to the TXC input
122 }
123 
124 
125 /**
126  * @brief LAN8841 timer handler
127  * @param[in] interface Underlying network interface
128  **/
129 
130 void lan8841Tick(NetInterface *interface)
131 {
132  uint16_t value;
133  bool_t linkState;
134 
135  //No external interrupt line driver?
136  if(interface->extIntDriver == NULL)
137  {
138  //Read basic status register
139  value = lan8841ReadPhyReg(interface, LAN8841_BMSR);
140  //Retrieve current link state
141  linkState = (value & LAN8841_BMSR_LINK_STATUS) ? TRUE : FALSE;
142 
143  //Link up event?
144  if(linkState && !interface->linkState)
145  {
146  //Set event flag
147  interface->phyEvent = TRUE;
148  //Notify the TCP/IP stack of the event
150  }
151  //Link down event?
152  else if(!linkState && interface->linkState)
153  {
154  //Set event flag
155  interface->phyEvent = TRUE;
156  //Notify the TCP/IP stack of the event
158  }
159  }
160 }
161 
162 
163 /**
164  * @brief Enable interrupts
165  * @param[in] interface Underlying network interface
166  **/
167 
169 {
170  //Enable PHY transceiver interrupts
171  if(interface->extIntDriver != NULL)
172  {
173  interface->extIntDriver->enableIrq();
174  }
175 }
176 
177 
178 /**
179  * @brief Disable interrupts
180  * @param[in] interface Underlying network interface
181  **/
182 
184 {
185  //Disable PHY transceiver interrupts
186  if(interface->extIntDriver != NULL)
187  {
188  interface->extIntDriver->disableIrq();
189  }
190 }
191 
192 
193 /**
194  * @brief LAN8841 event handler
195  * @param[in] interface Underlying network interface
196  **/
197 
199 {
200  uint16_t value;
201 
202  //Read status register to acknowledge the interrupt
203  value = lan8841ReadPhyReg(interface, LAN8841_ISR);
204 
205  //Link status change?
207  {
208  //Any link failure condition is latched in the BMSR register. Reading
209  //the register twice will always return the actual link status
210  value = lan8841ReadPhyReg(interface, LAN8841_BMSR);
211  value = lan8841ReadPhyReg(interface, LAN8841_BMSR);
212 
213  //Link is up?
214  if((value & LAN8841_BMSR_LINK_STATUS) != 0)
215  {
216  //Read PHY control register
217  value = lan8841ReadPhyReg(interface, LAN8841_PHYCON);
218 
219  //Check current speed
221  {
222  //1000BASE-T
223  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
224  }
225  else if((value & LAN8841_PHYCON_SPEED_100BTX) != 0)
226  {
227  //100BASE-TX
228  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
229  }
230  else if((value & LAN8841_PHYCON_SPEED_10BT) != 0)
231  {
232  //10BASE-T
233  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
234  }
235  else
236  {
237  //Debug message
238  TRACE_WARNING("Invalid speed!\r\n");
239  }
240 
241  //Check current duplex mode
243  {
244  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
245  }
246  else
247  {
248  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
249  }
250 
251  //Update link state
252  interface->linkState = TRUE;
253 
254  //Adjust MAC configuration parameters for proper operation
255  interface->nicDriver->updateMacConfig(interface);
256  }
257  else
258  {
259  //Update link state
260  interface->linkState = FALSE;
261  }
262 
263  //Process link state change event
264  nicNotifyLinkChange(interface);
265  }
266 }
267 
268 
269 /**
270  * @brief Write PHY register
271  * @param[in] interface Underlying network interface
272  * @param[in] address PHY register address
273  * @param[in] data Register value
274  **/
275 
276 void lan8841WritePhyReg(NetInterface *interface, uint8_t address,
277  uint16_t data)
278 {
279  //Write the specified PHY register
280  if(interface->smiDriver != NULL)
281  {
282  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
283  interface->phyAddr, address, data);
284  }
285  else
286  {
287  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
288  interface->phyAddr, address, data);
289  }
290 }
291 
292 
293 /**
294  * @brief Read PHY register
295  * @param[in] interface Underlying network interface
296  * @param[in] address PHY register address
297  * @return Register value
298  **/
299 
300 uint16_t lan8841ReadPhyReg(NetInterface *interface, uint8_t address)
301 {
302  uint16_t data;
303 
304  //Read the specified PHY register
305  if(interface->smiDriver != NULL)
306  {
307  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
308  interface->phyAddr, address);
309  }
310  else
311  {
312  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
313  interface->phyAddr, address);
314  }
315 
316  //Return the value of the PHY register
317  return data;
318 }
319 
320 
321 /**
322  * @brief Dump PHY registers for debugging purpose
323  * @param[in] interface Underlying network interface
324  **/
325 
327 {
328  uint8_t i;
329 
330  //Loop through PHY registers
331  for(i = 0; i < 32; i++)
332  {
333  //Display current PHY register
334  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
335  lan8841ReadPhyReg(interface, i));
336  }
337 
338  //Terminate with a line feed
339  TRACE_DEBUG("\r\n");
340 }
341 
342 
343 /**
344  * @brief Write MMD register
345  * @param[in] interface Underlying network interface
346  * @param[in] devAddr Device address
347  * @param[in] regAddr Register address
348  * @param[in] data MMD register value
349  **/
350 
351 void lan8841WriteMmdReg(NetInterface *interface, uint8_t devAddr,
352  uint16_t regAddr, uint16_t data)
353 {
354  //Select register operation
357 
358  //Write MMD register address
360 
361  //Select data operation
364 
365  //Write the content of the MMD register
367 }
368 
369 
370 /**
371  * @brief Read MMD register
372  * @param[in] interface Underlying network interface
373  * @param[in] devAddr Device address
374  * @param[in] regAddr Register address
375  * @return MMD register value
376  **/
377 
378 uint16_t lan8841ReadMmdReg(NetInterface *interface, uint8_t devAddr,
379  uint16_t regAddr)
380 {
381  //Select register operation
384 
385  //Write MMD register address
387 
388  //Select data operation
391 
392  //Read the content of the MMD register
393  return lan8841ReadPhyReg(interface, LAN8841_MMDAADR);
394 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_INFO(...)
Definition: debug.h:95
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
Ipv6Addr address[]
Definition: ipv6.h:316
uint16_t lan8841ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void lan8841DisableIrq(NetInterface *interface)
Disable interrupts.
void lan8841DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void lan8841EventHandler(NetInterface *interface)
LAN8841 event handler.
uint16_t lan8841ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
__weak_func void lan8841InitHook(NetInterface *interface)
LAN8841 custom configuration.
error_t lan8841Init(NetInterface *interface)
LAN8841 PHY transceiver initialization.
const PhyDriver lan8841PhyDriver
LAN8841 Ethernet PHY driver.
void lan8841EnableIrq(NetInterface *interface)
Enable interrupts.
void lan8841WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
void lan8841Tick(NetInterface *interface)
LAN8841 timer handler.
void lan8841WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
LAN8841 Gigabit Ethernet PHY driver.
#define LAN8841_ISR_LINK_DOWN
#define LAN8841_MMDACR_FUNC_DATA_NO_POST_INC
#define LAN8841_PHYCON_SPEED_10BT
#define LAN8841_BMSR_LINK_STATUS
#define LAN8841_MMDACR
#define LAN8841_MMDACR_DEVAD
#define LAN8841_PHY_ADDR
#define LAN8841_TX_DLL_CTRL_BYPASS_TXDLL
#define LAN8841_BMCR
#define LAN8841_TX_DLL_CTRL
#define LAN8841_PHYCON_DUPLEX_STATUS
#define LAN8841_MMDAADR
#define LAN8841_ISR_LINK_UP
#define LAN8841_BMCR_RESET
#define LAN8841_MMDACR_FUNC_ADDR
#define LAN8841_PHYCON
#define LAN8841_BMSR
#define LAN8841_PHYCON_SPEED_1000BT
#define LAN8841_ISR
#define LAN8841_PHYCON_SPEED_100BTX
uint16_t regAddr
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Ethernet PHY driver.
Definition: nic.h:308
uint8_t value[]
Definition: tcp.h:369