lan8770_driver.c
Go to the documentation of this file.
1 /**
2  * @file lan8770_driver.c
3  * @brief LAN8770 100Base-T1 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 LAN8770 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief LAN8770 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  uint16_t temp;
63 
64  //Debug message
65  TRACE_INFO("Initializing LAN8770...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = LAN8770_PHY_ADDR;
72  }
73 
74  //Initialize serial management interface
75  if(interface->smiDriver != NULL)
76  {
77  interface->smiDriver->init();
78  }
79 
80  //Initialize external interrupt line driver
81  if(interface->extIntDriver != NULL)
82  {
83  interface->extIntDriver->init();
84  }
85 
86  //Hardware initialization sequence is suspended to enable pre-configuration
87  //by software
91 
92  //Reset PHY transceiver
95 
96  //Wait for the reset to complete
97  while(lan8770ReadPhyReg(interface, LAN8770_BASIC_CONTROL) &
99  {
100  }
101 
102  //Dump PHY registers for debugging purpose
103  lan8770DumpPhyReg(interface);
104 
105  //Set TX amplitude
106  temp = lan8770ReadExtReg(interface, LAN8770_AFE_PORT_CFG1);
109  lan8770WriteExtReg(interface, LAN8770_AFE_PORT_CFG1, temp);
110 
111  //Clear SMI interrupts
112  temp = lan8770ReadPhyReg(interface, LAN8770_INTERRUPT_SOURCE);
113  //Clear MISC interrupts
114  temp = lan8770ReadExtReg(interface, LAN8770_INTERRUPT2_SOURCE);
115 
116  //Enable ring oscillator
117  temp = lan8770ReadExtReg(interface, LAN8770_WKP_COM_CTL0);
119  lan8770WriteExtReg(interface, LAN8770_WKP_COM_CTL0, temp);
120 
121  //Adjust WUR detect length and LPS detect length
125 
126  //Configure MISC registers
127  lan8770WriteExtReg(interface, LAN8770_WKP_COM_CTL1, 0x274F);
128  lan8770WriteExtReg(interface, LAN8770_WKP_COM_CTL0, 0x80A7);
129  lan8770WriteExtReg(interface, LAN8770_WKP_PRT_CTL, 0xF110);
130 
131  //Enable hardware initialization sequence
135 
136  //Perform custom configuration
137  lan8770InitHook(interface);
138 
139  //Force the TCP/IP stack to poll the link state at startup
140  interface->phyEvent = TRUE;
141  //Notify the TCP/IP stack of the event
143 
144  //Successful initialization
145  return NO_ERROR;
146 }
147 
148 
149 /**
150  * @brief LAN8770 custom configuration
151  * @param[in] interface Underlying network interface
152  **/
153 
154 __weak_func void lan8770InitHook(NetInterface *interface)
155 {
156 }
157 
158 
159 /**
160  * @brief LAN8770 timer handler
161  * @param[in] interface Underlying network interface
162  **/
163 
164 void lan8770Tick(NetInterface *interface)
165 {
166  uint16_t value;
167  bool_t linkState;
168 
169  //No external interrupt line driver?
170  if(interface->extIntDriver == NULL)
171  {
172  //Read status register
174  //Retrieve current link state
175  linkState = (value & LAN8770_BASIC_STATUS_LINK_STATUS) ? TRUE : FALSE;
176 
177  //Link up event?
178  if(linkState && !interface->linkState)
179  {
180  //Set event flag
181  interface->phyEvent = TRUE;
182  //Notify the TCP/IP stack of the event
184  }
185  //Link down event?
186  else if(!linkState && interface->linkState)
187  {
188  //Set event flag
189  interface->phyEvent = TRUE;
190  //Notify the TCP/IP stack of the event
192  }
193  }
194 }
195 
196 
197 /**
198  * @brief Enable interrupts
199  * @param[in] interface Underlying network interface
200  **/
201 
203 {
204  //Enable PHY transceiver interrupts
205  if(interface->extIntDriver != NULL)
206  {
207  interface->extIntDriver->enableIrq();
208  }
209 }
210 
211 
212 /**
213  * @brief Disable interrupts
214  * @param[in] interface Underlying network interface
215  **/
216 
218 {
219  //Disable PHY transceiver interrupts
220  if(interface->extIntDriver != NULL)
221  {
222  interface->extIntDriver->disableIrq();
223  }
224 }
225 
226 
227 /**
228  * @brief LAN8770 event handler
229  * @param[in] interface Underlying network interface
230  **/
231 
233 {
234  uint16_t value;
235 
236  //Read status register
238 
239  //Link is up?
241  {
242  //The PHY is only able to operate in 100 Mbps mode
243  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
244  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
245 
246  //Adjust MAC configuration parameters for proper operation
247  interface->nicDriver->updateMacConfig(interface);
248 
249  //Update link state
250  interface->linkState = TRUE;
251  }
252  else
253  {
254  //Update link state
255  interface->linkState = FALSE;
256  }
257 
258  //Process link state change event
259  nicNotifyLinkChange(interface);
260 }
261 
262 
263 /**
264  * @brief Write PHY register
265  * @param[in] interface Underlying network interface
266  * @param[in] address PHY register address
267  * @param[in] data Register value
268  **/
269 
270 void lan8770WritePhyReg(NetInterface *interface, uint8_t address,
271  uint16_t data)
272 {
273  //Write the specified PHY register
274  if(interface->smiDriver != NULL)
275  {
276  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
277  interface->phyAddr, address, data);
278  }
279  else
280  {
281  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
282  interface->phyAddr, address, data);
283  }
284 }
285 
286 
287 /**
288  * @brief Read PHY register
289  * @param[in] interface Underlying network interface
290  * @param[in] address PHY register address
291  * @return Register value
292  **/
293 
294 uint16_t lan8770ReadPhyReg(NetInterface *interface, uint8_t address)
295 {
296  uint16_t data;
297 
298  //Read the specified PHY register
299  if(interface->smiDriver != NULL)
300  {
301  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
302  interface->phyAddr, address);
303  }
304  else
305  {
306  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
307  interface->phyAddr, address);
308  }
309 
310  //Return the value of the PHY register
311  return data;
312 }
313 
314 
315 /**
316  * @brief Dump PHY registers for debugging purpose
317  * @param[in] interface Underlying network interface
318  **/
319 
321 {
322  uint8_t i;
323 
324  //Loop through PHY registers
325  for(i = 0; i < 32; i++)
326  {
327  //Display current PHY register
328  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
329  lan8770ReadPhyReg(interface, i));
330  }
331 
332  //Terminate with a line feed
333  TRACE_DEBUG("\r\n");
334 }
335 
336 
337 /**
338  * @brief Write extended register
339  * @param[in] interface Underlying network interface
340  * @param[in] bank Register bank
341  * @param[in] addr Register address
342  * @param[in] data Extended register value
343  **/
344 
345 void lan8770WriteExtReg(NetInterface *interface, uint8_t bank,
346  uint8_t addr, uint16_t data)
347 {
348  uint16_t temp;
349 
350  //Set up a write operation
352  //Select register bank
353  temp |= (bank << 8) & LAN8770_EXT_REG_CTL_REGISTER_BANK;
354  //Select register address
356 
357  //Write the EXT_REG_WR_DATA register with the desired 16-bit data
359 
360  //Write the EXT_REG_CTL register
361  lan8770WritePhyReg(interface, LAN8770_EXT_REG_CTL, temp);
362 }
363 
364 
365 /**
366  * @brief Read extended register
367  * @param[in] interface Underlying network interface
368  * @param[in] bank Register bank
369  * @param[in] addr Register address
370  * @return Extended register value
371  **/
372 
373 uint16_t lan8770ReadExtReg(NetInterface *interface, uint8_t bank,
374  uint8_t addr)
375 {
376  uint16_t temp;
377 
378  //Set up a read operation
380  //Select register bank
381  temp |= (bank << 8) & LAN8770_EXT_REG_CTL_REGISTER_BANK;
382  //Select register address
384 
385  //Write the EXT_REG_CTL register
386  lan8770WritePhyReg(interface, LAN8770_EXT_REG_CTL, temp);
387 
388  //Read the EXT_REG_RD_DATA register
389  return lan8770ReadPhyReg(interface, LAN8770_EXT_REG_RD_DATA);
390 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
#define LAN8770_BASIC_STATUS
#define LAN8770_EXT_REG_CTL
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
void lan8770WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define LAN8770_WKP_COM_CTL1
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define LAN8770_AFE_PORT_CFG1_TX_AMP_DEFAULT
#define LAN8770_EXT_REG_RD_DATA
error_t lan8770Init(NetInterface *interface)
LAN8770 PHY transceiver initialization.
#define TRUE
Definition: os_port.h:50
void lan8770EventHandler(NetInterface *interface)
LAN8770 event handler.
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define LAN8770_INTERRUPT_SOURCE
const PhyDriver lan8770PhyDriver
LAN8770 Ethernet PHY driver.
LAN8770 100Base-T1 Ethernet PHY driver.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
uint16_t lan8770ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define LAN8770_BASIC_STATUS_LINK_STATUS
#define LAN8770_EXT_REG_WR_DATA
#define FALSE
Definition: os_port.h:46
#define LAN8770_BASIC_CONTROL_SW_RESET
uint16_t lan8770ReadExtReg(NetInterface *interface, uint8_t bank, uint8_t addr)
Read extended register.
error_t
Error codes.
Definition: error.h:43
void lan8770EnableIrq(NetInterface *interface)
Enable interrupts.
#define LAN8770_WKP_PRT_CTL
#define LAN8770_EXT_REG_CTL_READ_CONTROL
#define NetInterface
Definition: net.h:36
void lan8770DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define LAN8770_PHY_ADDR
#define LAN8770_WKP_COM_CTL0_RING_OSC_EN
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
#define LAN8770_POWER_DOWN_CONTROL_HARD_INIT_SEQ_EN
#define LAN8770_EXT_REG_CTL_REGISTER_ADDR
#define LAN8770_AFE_PORT_CFG1_TX_AMP
__weak_func void lan8770InitHook(NetInterface *interface)
LAN8770 custom configuration.
#define LAN8770_EXT_REG_CTL_WRITE_CONTROL
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define LAN8770_EXT_REG_CTL_REGISTER_BANK
Ipv6Addr address[]
Definition: ipv6.h:325
#define LAN8770_POWER_DOWN_CONTROL
uint8_t value[]
Definition: tcp.h:369
#define LAN8770_SLEEP_WAKE_DET_WUR_DETECT_LEN_DEFAULT
#define LAN8770_AFE_PORT_CFG1
#define LAN8770_BASIC_CONTROL
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define LAN8770_SLEEP_WAKE_DET_LPS_DETECT_LEN_DEFAULT
Ipv4Addr addr
Definition: nbns_common.h:123
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define LAN8770_WKP_COM_CTL0
#define LAN8770_INTERRUPT2_SOURCE
TCP/IP stack core.
#define LAN8770_SLEEP_WAKE_DET
void lan8770Tick(NetInterface *interface)
LAN8770 timer handler.
void lan8770DisableIrq(NetInterface *interface)
Disable interrupts.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void lan8770WriteExtReg(NetInterface *interface, uint8_t bank, uint8_t addr, uint16_t data)
Write extended register.