ksz8081_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz8081_driver.c
3  * @brief KSZ8081 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 KSZ8081 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief KSZ8081 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing KSZ8081...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = KSZ8081_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  ksz8081DumpPhyReg(interface);
94 
95 #if (KSZ8081_50MHZ_CLOCK_MODE_SUPPORT == ENABLED)
96  //Select 50MHz clock mode
99 #endif
100 
101  //Restore default auto-negotiation advertisement parameters
105 
106  //Enable auto-negotiation
108 
109  //The PHY will generate interrupts when link status changes are detected
112 
113  //Perform custom configuration
114  ksz8081InitHook(interface);
115 
116  //Force the TCP/IP stack to poll the link state at startup
117  interface->phyEvent = TRUE;
118  //Notify the TCP/IP stack of the event
120 
121  //Successful initialization
122  return NO_ERROR;
123 }
124 
125 
126 /**
127  * @brief KSZ8081 custom configuration
128  * @param[in] interface Underlying network interface
129  **/
130 
131 __weak_func void ksz8081InitHook(NetInterface *interface)
132 {
133 }
134 
135 
136 /**
137  * @brief KSZ8081 timer handler
138  * @param[in] interface Underlying network interface
139  **/
140 
141 void ksz8081Tick(NetInterface *interface)
142 {
143  uint16_t value;
144  bool_t linkState;
145 
146  //No external interrupt line driver?
147  if(interface->extIntDriver == NULL)
148  {
149  //Read basic status register
150  value = ksz8081ReadPhyReg(interface, KSZ8081_BMSR);
151  //Retrieve current link state
152  linkState = (value & KSZ8081_BMSR_LINK_STATUS) ? TRUE : FALSE;
153 
154  //Link up event?
155  if(linkState && !interface->linkState)
156  {
157  //Set event flag
158  interface->phyEvent = TRUE;
159  //Notify the TCP/IP stack of the event
161  }
162  //Link down event?
163  else if(!linkState && interface->linkState)
164  {
165  //Set event flag
166  interface->phyEvent = TRUE;
167  //Notify the TCP/IP stack of the event
169  }
170  }
171 }
172 
173 
174 /**
175  * @brief Enable interrupts
176  * @param[in] interface Underlying network interface
177  **/
178 
180 {
181  //Enable PHY transceiver interrupts
182  if(interface->extIntDriver != NULL)
183  {
184  interface->extIntDriver->enableIrq();
185  }
186 }
187 
188 
189 /**
190  * @brief Disable interrupts
191  * @param[in] interface Underlying network interface
192  **/
193 
195 {
196  //Disable PHY transceiver interrupts
197  if(interface->extIntDriver != NULL)
198  {
199  interface->extIntDriver->disableIrq();
200  }
201 }
202 
203 
204 /**
205  * @brief KSZ8081 event handler
206  * @param[in] interface Underlying network interface
207  **/
208 
210 {
211  uint16_t value;
212 
213  //Read status register to acknowledge the interrupt
214  value = ksz8081ReadPhyReg(interface, KSZ8081_ICSR);
215 
216  //Link status change?
218  {
219  //Any link failure condition is latched in the BMSR register. Reading
220  //the register twice will always return the actual link status
221  value = ksz8081ReadPhyReg(interface, KSZ8081_BMSR);
222  value = ksz8081ReadPhyReg(interface, KSZ8081_BMSR);
223 
224  //Link is up?
225  if((value & KSZ8081_BMSR_LINK_STATUS) != 0)
226  {
227  //Read PHY control register
229 
230  //Check current operation mode
232  {
233  //10BASE-T half-duplex
235  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
236  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
237  break;
238 
239  //10BASE-T full-duplex
241  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
242  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
243  break;
244 
245  //100BASE-TX half-duplex
247  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
248  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
249  break;
250 
251  //100BASE-TX full-duplex
253  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
254  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
255  break;
256 
257  //Unknown operation mode
258  default:
259  //Debug message
260  TRACE_WARNING("Invalid operation mode!\r\n");
261  break;
262  }
263 
264  //Update link state
265  interface->linkState = TRUE;
266 
267  //Adjust MAC configuration parameters for proper operation
268  interface->nicDriver->updateMacConfig(interface);
269  }
270  else
271  {
272  //Update link state
273  interface->linkState = FALSE;
274  }
275 
276  //Process link state change event
277  nicNotifyLinkChange(interface);
278  }
279 }
280 
281 
282 /**
283  * @brief Write PHY register
284  * @param[in] interface Underlying network interface
285  * @param[in] address PHY register address
286  * @param[in] data Register value
287  **/
288 
289 void ksz8081WritePhyReg(NetInterface *interface, uint8_t address,
290  uint16_t data)
291 {
292  //Write the specified PHY register
293  if(interface->smiDriver != NULL)
294  {
295  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
296  interface->phyAddr, address, data);
297  }
298  else
299  {
300  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
301  interface->phyAddr, address, data);
302  }
303 }
304 
305 
306 /**
307  * @brief Read PHY register
308  * @param[in] interface Underlying network interface
309  * @param[in] address PHY register address
310  * @return Register value
311  **/
312 
313 uint16_t ksz8081ReadPhyReg(NetInterface *interface, uint8_t address)
314 {
315  uint16_t data;
316 
317  //Read the specified PHY register
318  if(interface->smiDriver != NULL)
319  {
320  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
321  interface->phyAddr, address);
322  }
323  else
324  {
325  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
326  interface->phyAddr, address);
327  }
328 
329  //Return the value of the PHY register
330  return data;
331 }
332 
333 
334 /**
335  * @brief Dump PHY registers for debugging purpose
336  * @param[in] interface Underlying network interface
337  **/
338 
340 {
341  uint8_t i;
342 
343  //Loop through PHY registers
344  for(i = 0; i < 32; i++)
345  {
346  //Display current PHY register
347  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
348  ksz8081ReadPhyReg(interface, i));
349  }
350 
351  //Terminate with a line feed
352  TRACE_DEBUG("\r\n");
353 }
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
const PhyDriver ksz8081PhyDriver
KSZ8081 Ethernet PHY driver.
void ksz8081DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void ksz8081WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
__weak_func void ksz8081InitHook(NetInterface *interface)
KSZ8081 custom configuration.
void ksz8081Tick(NetInterface *interface)
KSZ8081 timer handler.
void ksz8081EnableIrq(NetInterface *interface)
Enable interrupts.
void ksz8081EventHandler(NetInterface *interface)
KSZ8081 event handler.
void ksz8081DisableIrq(NetInterface *interface)
Disable interrupts.
error_t ksz8081Init(NetInterface *interface)
KSZ8081 PHY transceiver initialization.
uint16_t ksz8081ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
KSZ8081 Ethernet PHY driver.
#define KSZ8081_PHYCON1_OP_MODE_100BTX_HD
#define KSZ8081_PHYCON1_OP_MODE_100BTX_FD
#define KSZ8081_PHYCON2_RMII_REF_CLK_SEL
#define KSZ8081_ANAR_100BTX_HD
#define KSZ8081_ICSR_LINK_UP_IF
#define KSZ8081_ANAR_100BTX_FD
#define KSZ8081_BMCR
#define KSZ8081_PHYCON1_OP_MODE
#define KSZ8081_ANAR
#define KSZ8081_ICSR_LINK_DOWN_IE
#define KSZ8081_ANAR_10BT_FD
#define KSZ8081_PHYCON1_OP_MODE_10BT_HD
#define KSZ8081_ICSR_LINK_DOWN_IF
#define KSZ8081_PHY_ADDR
#define KSZ8081_ICSR_LINK_UP_IE
#define KSZ8081_BMSR
#define KSZ8081_PHYCON2
#define KSZ8081_ANAR_10BT_HD
#define KSZ8081_ANAR_SELECTOR_DEFAULT
#define KSZ8081_BMCR_RESET
#define KSZ8081_BMSR_LINK_STATUS
#define KSZ8081_PHYCON1
#define KSZ8081_PHYCON2_JABBER_EN
#define KSZ8081_PHYCON1_OP_MODE_10BT_FD
#define KSZ8081_ICSR
#define KSZ8081_BMCR_AN_EN
#define KSZ8081_PHYCON2_HP_MDIX
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
#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