ksz8041_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz8041_driver.c
3  * @brief KSZ8041 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 KSZ8041 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief KSZ8041 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing KSZ8041...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = KSZ8041_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  ksz8041DumpPhyReg(interface);
94 
95  //The PHY will generate interrupts when link status changes are detected
98 
99  //Perform custom configuration
100  ksz8041InitHook(interface);
101 
102  //Force the TCP/IP stack to poll the link state at startup
103  interface->phyEvent = TRUE;
104  //Notify the TCP/IP stack of the event
106 
107  //Successful initialization
108  return NO_ERROR;
109 }
110 
111 
112 /**
113  * @brief KSZ8041 custom configuration
114  * @param[in] interface Underlying network interface
115  **/
116 
117 __weak_func void ksz8041InitHook(NetInterface *interface)
118 {
119 }
120 
121 
122 /**
123  * @brief KSZ8041 timer handler
124  * @param[in] interface Underlying network interface
125  **/
126 
127 void ksz8041Tick(NetInterface *interface)
128 {
129  uint16_t value;
130  bool_t linkState;
131 
132  //No external interrupt line driver?
133  if(interface->extIntDriver == NULL)
134  {
135  //Read basic status register
136  value = ksz8041ReadPhyReg(interface, KSZ8041_BMSR);
137  //Retrieve current link state
138  linkState = (value & KSZ8041_BMSR_LINK_STATUS) ? TRUE : FALSE;
139 
140  //Link up event?
141  if(linkState && !interface->linkState)
142  {
143  //Set event flag
144  interface->phyEvent = TRUE;
145  //Notify the TCP/IP stack of the event
147  }
148  //Link down event?
149  else if(!linkState && interface->linkState)
150  {
151  //Set event flag
152  interface->phyEvent = TRUE;
153  //Notify the TCP/IP stack of the event
155  }
156  }
157 }
158 
159 
160 /**
161  * @brief Enable interrupts
162  * @param[in] interface Underlying network interface
163  **/
164 
166 {
167  //Enable PHY transceiver interrupts
168  if(interface->extIntDriver != NULL)
169  {
170  interface->extIntDriver->enableIrq();
171  }
172 }
173 
174 
175 /**
176  * @brief Disable interrupts
177  * @param[in] interface Underlying network interface
178  **/
179 
181 {
182  //Disable PHY transceiver interrupts
183  if(interface->extIntDriver != NULL)
184  {
185  interface->extIntDriver->disableIrq();
186  }
187 }
188 
189 
190 /**
191  * @brief KSZ8041 event handler
192  * @param[in] interface Underlying network interface
193  **/
194 
196 {
197  uint16_t value;
198 
199  //Read status register to acknowledge the interrupt
200  value = ksz8041ReadPhyReg(interface, KSZ8041_ICSR);
201 
202  //Link status change?
204  {
205  //Any link failure condition is latched in the BMSR register. Reading
206  //the register twice will always return the actual link status
207  value = ksz8041ReadPhyReg(interface, KSZ8041_BMSR);
208  value = ksz8041ReadPhyReg(interface, KSZ8041_BMSR);
209 
210  //Link is up?
211  if((value & KSZ8041_BMSR_LINK_STATUS) != 0)
212  {
213  //Read PHY control register
215 
216  //Check current operation mode
218  {
219  //10BASE-T half-duplex
221  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
222  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
223  break;
224 
225  //10BASE-T full-duplex
227  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
228  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
229  break;
230 
231  //100BASE-TX half-duplex
233  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
234  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
235  break;
236 
237  //100BASE-TX full-duplex
239  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
240  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
241  break;
242 
243  //Unknown operation mode
244  default:
245  //Debug message
246  TRACE_WARNING("Invalid operation mode!\r\n");
247  break;
248  }
249 
250  //Update link state
251  interface->linkState = TRUE;
252 
253  //Adjust MAC configuration parameters for proper operation
254  interface->nicDriver->updateMacConfig(interface);
255  }
256  else
257  {
258  //Update link state
259  interface->linkState = FALSE;
260  }
261 
262  //Process link state change event
263  nicNotifyLinkChange(interface);
264  }
265 }
266 
267 
268 /**
269  * @brief Write PHY register
270  * @param[in] interface Underlying network interface
271  * @param[in] address PHY register address
272  * @param[in] data Register value
273  **/
274 
275 void ksz8041WritePhyReg(NetInterface *interface, uint8_t address,
276  uint16_t data)
277 {
278  //Write the specified PHY register
279  if(interface->smiDriver != NULL)
280  {
281  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
282  interface->phyAddr, address, data);
283  }
284  else
285  {
286  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
287  interface->phyAddr, address, data);
288  }
289 }
290 
291 
292 /**
293  * @brief Read PHY register
294  * @param[in] interface Underlying network interface
295  * @param[in] address PHY register address
296  * @return Register value
297  **/
298 
299 uint16_t ksz8041ReadPhyReg(NetInterface *interface, uint8_t address)
300 {
301  uint16_t data;
302 
303  //Read the specified PHY register
304  if(interface->smiDriver != NULL)
305  {
306  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
307  interface->phyAddr, address);
308  }
309  else
310  {
311  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
312  interface->phyAddr, address);
313  }
314 
315  //Return the value of the PHY register
316  return data;
317 }
318 
319 
320 /**
321  * @brief Dump PHY registers for debugging purpose
322  * @param[in] interface Underlying network interface
323  **/
324 
326 {
327  uint8_t i;
328 
329  //Loop through PHY registers
330  for(i = 0; i < 32; i++)
331  {
332  //Display current PHY register
333  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
334  ksz8041ReadPhyReg(interface, i));
335  }
336 
337  //Terminate with a line feed
338  TRACE_DEBUG("\r\n");
339 }
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 ksz8041PhyDriver
KSZ8041 Ethernet PHY driver.
void ksz8041EventHandler(NetInterface *interface)
KSZ8041 event handler.
error_t ksz8041Init(NetInterface *interface)
KSZ8041 PHY transceiver initialization.
void ksz8041Tick(NetInterface *interface)
KSZ8041 timer handler.
void ksz8041DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void ksz8041EnableIrq(NetInterface *interface)
Enable interrupts.
__weak_func void ksz8041InitHook(NetInterface *interface)
KSZ8041 custom configuration.
uint16_t ksz8041ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void ksz8041DisableIrq(NetInterface *interface)
Disable interrupts.
void ksz8041WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
KSZ8041 Ethernet PHY driver.
#define KSZ8041_PHYCON2_OP_MODE_10BT_HD
#define KSZ8041_BMSR
#define KSZ8041_ICSR_LINK_DOWN_IF
#define KSZ8041_PHYCON2
#define KSZ8041_PHY_ADDR
#define KSZ8041_PHYCON2_OP_MODE_100BTX_FD
#define KSZ8041_BMCR
#define KSZ8041_ICSR_LINK_DOWN_IE
#define KSZ8041_ICSR_LINK_UP_IE
#define KSZ8041_PHYCON2_OP_MODE
#define KSZ8041_ICSR
#define KSZ8041_ICSR_LINK_UP_IF
#define KSZ8041_BMSR_LINK_STATUS
#define KSZ8041_BMCR_RESET
#define KSZ8041_PHYCON2_OP_MODE_10BT_FD
#define KSZ8041_PHYCON2_OP_MODE_100BTX_HD
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