ksz8721_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz8721_driver.c
3  * @brief KSZ8721 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 KSZ8721 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief KSZ8721 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing KSZ8721...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = KSZ8721_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  ksz8721DumpPhyReg(interface);
94 
95  //The PHY will generate interrupts when link status changes are detected
98 
99  //Perform custom configuration
100  ksz8721InitHook(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 KSZ8721 custom configuration
114  * @param[in] interface Underlying network interface
115  **/
116 
117 __weak_func void ksz8721InitHook(NetInterface *interface)
118 {
119 }
120 
121 
122 /**
123  * @brief KSZ8721 timer handler
124  * @param[in] interface Underlying network interface
125  **/
126 
127 void ksz8721Tick(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 = ksz8721ReadPhyReg(interface, KSZ8721_BMSR);
137  //Retrieve current link state
138  linkState = (value & KSZ8721_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 KSZ8721 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 = ksz8721ReadPhyReg(interface, KSZ8721_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 = ksz8721ReadPhyReg(interface, KSZ8721_BMSR);
208  value = ksz8721ReadPhyReg(interface, KSZ8721_BMSR);
209 
210  //Link is up?
211  if((value & KSZ8721_BMSR_LINK_STATUS) != 0)
212  {
213  //Read PHY control register
214  value = ksz8721ReadPhyReg(interface, KSZ8721_PHYCON);
215 
216  //Check current operation mode
217  switch(value & KSZ8721_PHYCON_OP_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 ksz8721WritePhyReg(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 ksz8721ReadPhyReg(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  ksz8721ReadPhyReg(interface, i));
335  }
336 
337  //Terminate with a line feed
338  TRACE_DEBUG("\r\n");
339 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
void ksz8721Tick(NetInterface *interface)
KSZ8721 timer handler.
int bool_t
Definition: compiler_port.h:53
#define KSZ8721_PHYCON_OP_MODE_10BT_HD
#define KSZ8721_BMCR_RESET
void ksz8721DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define netEvent
Definition: net_legacy.h:196
#define KSZ8721_ICSR_LINK_DOWN_IE
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
KSZ8721 Ethernet PHY driver.
#define KSZ8721_ICSR_LINK_UP_IF
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define FALSE
Definition: os_port.h:46
error_t ksz8721Init(NetInterface *interface)
KSZ8721 PHY transceiver initialization.
uint16_t ksz8721ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
error_t
Error codes.
Definition: error.h:43
#define KSZ8721_BMSR
#define KSZ8721_BMCR
#define KSZ8721_ICSR_LINK_UP_IE
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define KSZ8721_BMSR_LINK_STATUS
void ksz8721DisableIrq(NetInterface *interface)
Disable interrupts.
#define KSZ8721_PHYCON
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
void ksz8721WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
const PhyDriver ksz8721PhyDriver
KSZ8721 Ethernet PHY driver.
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define KSZ8721_PHYCON_OP_MODE_10BT_FD
void ksz8721EventHandler(NetInterface *interface)
KSZ8721 event handler.
void ksz8721EnableIrq(NetInterface *interface)
Enable interrupts.
Ipv6Addr address[]
Definition: ipv6.h:325
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
#define KSZ8721_ICSR
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define KSZ8721_PHYCON_OP_MODE
#define KSZ8721_PHY_ADDR
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define KSZ8721_PHYCON_OP_MODE_100BTX_FD
#define KSZ8721_ICSR_LINK_DOWN_IF
TCP/IP stack core.
__weak_func void ksz8721InitHook(NetInterface *interface)
KSZ8721 custom configuration.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define KSZ8721_PHYCON_OP_MODE_100BTX_HD