ksz9031_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz9031_driver.c
3  * @brief KSZ9031 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.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 KSZ9031 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief KSZ9031 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  uint16_t value;
63 
64  //Debug message
65  TRACE_INFO("Initializing KSZ9031...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = KSZ9031_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  //Reset PHY transceiver
88 
89  //Wait for the reset to complete
91  {
92  }
93 
94  //Dump PHY registers for debugging purpose
95  ksz9031DumpPhyReg(interface);
96 
97  //Select single-LED mode
101 
102  //Change the FLP interval to 16ms (silicon errata workaround 5)
105 
106  //Restart auto-negotiation for the 16ms FLP interval setting to take effect
109 
110  //The PHY will generate interrupts when link status changes are detected
113 
114  //Perform custom configuration
115  ksz9031InitHook(interface);
116 
117  //Force the TCP/IP stack to poll the link state at startup
118  interface->phyEvent = TRUE;
119  //Notify the TCP/IP stack of the event
121 
122  //Successful initialization
123  return NO_ERROR;
124 }
125 
126 
127 /**
128  * @brief KSZ9031 custom configuration
129  * @param[in] interface Underlying network interface
130  **/
131 
132 __weak_func void ksz9031InitHook(NetInterface *interface)
133 {
134 }
135 
136 
137 /**
138  * @brief KSZ9031 timer handler
139  * @param[in] interface Underlying network interface
140  **/
141 
142 void ksz9031Tick(NetInterface *interface)
143 {
144  uint16_t value;
145  bool_t linkState;
146 
147  //No external interrupt line driver?
148  if(interface->extIntDriver == NULL)
149  {
150  //Read basic status register
151  value = ksz9031ReadPhyReg(interface, KSZ9031_BMSR);
152  //Retrieve current link state
153  linkState = (value & KSZ9031_BMSR_LINK_STATUS) ? TRUE : FALSE;
154 
155  //Link up event?
156  if(linkState && !interface->linkState)
157  {
158  //Set event flag
159  interface->phyEvent = TRUE;
160  //Notify the TCP/IP stack of the event
162  }
163  //Link down event?
164  else if(!linkState && interface->linkState)
165  {
166  //Set event flag
167  interface->phyEvent = TRUE;
168  //Notify the TCP/IP stack of the event
170  }
171  }
172 }
173 
174 
175 /**
176  * @brief Enable interrupts
177  * @param[in] interface Underlying network interface
178  **/
179 
181 {
182  //Enable PHY transceiver interrupts
183  if(interface->extIntDriver != NULL)
184  {
185  interface->extIntDriver->enableIrq();
186  }
187 }
188 
189 
190 /**
191  * @brief Disable interrupts
192  * @param[in] interface Underlying network interface
193  **/
194 
196 {
197  //Disable PHY transceiver interrupts
198  if(interface->extIntDriver != NULL)
199  {
200  interface->extIntDriver->disableIrq();
201  }
202 }
203 
204 
205 /**
206  * @brief KSZ9031 event handler
207  * @param[in] interface Underlying network interface
208  **/
209 
211 {
212  uint16_t value;
213 
214  //Read status register to acknowledge the interrupt
215  value = ksz9031ReadPhyReg(interface, KSZ9031_ICSR);
216 
217  //Link status change?
219  {
220  //Any link failure condition is latched in the BMSR register. Reading
221  //the register twice will always return the actual link status
222  value = ksz9031ReadPhyReg(interface, KSZ9031_BMSR);
223  value = ksz9031ReadPhyReg(interface, KSZ9031_BMSR);
224 
225  //Link is up?
226  if((value & KSZ9031_BMSR_LINK_STATUS) != 0)
227  {
228  //Read PHY control register
229  value = ksz9031ReadPhyReg(interface, KSZ9031_PHYCON);
230 
231  //Check current speed
233  {
234  //1000BASE-T
235  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
236  }
237  else if((value & KSZ9031_PHYCON_SPEED_100BTX) != 0)
238  {
239  //100BASE-TX
240  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
241  }
242  else if((value & KSZ9031_PHYCON_SPEED_10BT) != 0)
243  {
244  //10BASE-T
245  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
246  }
247  else
248  {
249  //Debug message
250  TRACE_WARNING("Invalid speed!\r\n");
251  }
252 
253  //Check current duplex mode
255  {
256  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
257  }
258  else
259  {
260  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
261  }
262 
263  //Update link state
264  interface->linkState = TRUE;
265 
266  //Adjust MAC configuration parameters for proper operation
267  interface->nicDriver->updateMacConfig(interface);
268  }
269  else
270  {
271  //Update link state
272  interface->linkState = FALSE;
273  }
274 
275  //Process link state change event
276  nicNotifyLinkChange(interface);
277  }
278 }
279 
280 
281 /**
282  * @brief Write PHY register
283  * @param[in] interface Underlying network interface
284  * @param[in] address PHY register address
285  * @param[in] data Register value
286  **/
287 
288 void ksz9031WritePhyReg(NetInterface *interface, uint8_t address,
289  uint16_t data)
290 {
291  //Write the specified PHY register
292  if(interface->smiDriver != NULL)
293  {
294  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
295  interface->phyAddr, address, data);
296  }
297  else
298  {
299  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
300  interface->phyAddr, address, data);
301  }
302 }
303 
304 
305 /**
306  * @brief Read PHY register
307  * @param[in] interface Underlying network interface
308  * @param[in] address PHY register address
309  * @return Register value
310  **/
311 
312 uint16_t ksz9031ReadPhyReg(NetInterface *interface, uint8_t address)
313 {
314  uint16_t data;
315 
316  //Read the specified PHY register
317  if(interface->smiDriver != NULL)
318  {
319  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
320  interface->phyAddr, address);
321  }
322  else
323  {
324  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
325  interface->phyAddr, address);
326  }
327 
328  //Return the value of the PHY register
329  return data;
330 }
331 
332 
333 /**
334  * @brief Dump PHY registers for debugging purpose
335  * @param[in] interface Underlying network interface
336  **/
337 
339 {
340  uint8_t i;
341 
342  //Loop through PHY registers
343  for(i = 0; i < 32; i++)
344  {
345  //Display current PHY register
346  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
347  ksz9031ReadPhyReg(interface, i));
348  }
349 
350  //Terminate with a line feed
351  TRACE_DEBUG("\r\n");
352 }
353 
354 
355 /**
356  * @brief Write MMD register
357  * @param[in] interface Underlying network interface
358  * @param[in] devAddr Device address
359  * @param[in] regAddr Register address
360  * @param[in] data MMD register value
361  **/
362 
363 void ksz9031WriteMmdReg(NetInterface *interface, uint8_t devAddr,
364  uint16_t regAddr, uint16_t data)
365 {
366  //Select register operation
369 
370  //Write MMD register address
372 
373  //Select data operation
376 
377  //Write the content of the MMD register
379 }
380 
381 
382 /**
383  * @brief Read MMD register
384  * @param[in] interface Underlying network interface
385  * @param[in] devAddr Device address
386  * @param[in] regAddr Register address
387  * @return MMD register value
388  **/
389 
390 uint16_t ksz9031ReadMmdReg(NetInterface *interface, uint8_t devAddr,
391  uint16_t regAddr)
392 {
393  //Select register operation
396 
397  //Write MMD register address
399 
400  //Select data operation
403 
404  //Read the content of the MMD register
405  return ksz9031ReadPhyReg(interface, KSZ9031_MMDAADR);
406 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define KSZ9031_ICSR
int bool_t
Definition: compiler_port.h:53
void ksz9031Tick(NetInterface *interface)
KSZ9031 timer handler.
#define netEvent
Definition: net_legacy.h:196
#define KSZ9031_PHY_ADDR
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define KSZ9031_MMDACR_FUNC_ADDR
#define KSZ9031_MMDAADR
#define TRUE
Definition: os_port.h:50
#define KSZ9031_PHYCON_SPEED_10BT
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define KSZ9031_MMDACR_DEVAD
void ksz9031WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
#define KSZ9031_ICSR_LINK_UP_IF
void ksz9031EnableIrq(NetInterface *interface)
Enable interrupts.
uint16_t ksz9031ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define KSZ9031_ICSR_LINK_DOWN_IE
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
#define KSZ9031_BMCR_RESTART_AN
void ksz9031EventHandler(NetInterface *interface)
KSZ9031 event handler.
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define KSZ9031_COMMON_CTRL_LED_MODE_OVERRIDE
KSZ9031 Gigabit Ethernet PHY driver.
__weak_func void ksz9031InitHook(NetInterface *interface)
KSZ9031 custom configuration.
#define SMI_OPCODE_READ
Definition: nic.h:67
error_t ksz9031Init(NetInterface *interface)
KSZ9031 PHY transceiver initialization.
#define TRACE_INFO(...)
Definition: debug.h:95
#define KSZ9031_PHYCON_SPEED_1000BT
#define KSZ9031_BMCR_AN_EN
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define KSZ9031_BMCR_RESET
void ksz9031DisableIrq(NetInterface *interface)
Disable interrupts.
uint16_t regAddr
#define KSZ9031_AN_FLP_BURST_TRANSMIT_LO
#define KSZ9031_BMSR
#define KSZ9031_AN_FLP_BURST_TRANSMIT_HI
Ipv6Addr address[]
Definition: ipv6.h:325
#define KSZ9031_PHYCON_SPEED_100BTX
#define KSZ9031_BMSR_LINK_STATUS
#define KSZ9031_PHYCON_DUPLEX_STATUS
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
#define KSZ9031_MMDACR
#define KSZ9031_COMMON_CTRL
#define KSZ9031_ICSR_LINK_DOWN_IF
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void ksz9031DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
const PhyDriver ksz9031PhyDriver
KSZ9031 Ethernet PHY driver.
TCP/IP stack core.
#define KSZ9031_PHYCON
uint16_t ksz9031ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
void ksz9031WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define KSZ9031_ICSR_LINK_UP_IE
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define KSZ9031_MMDACR_FUNC_DATA_NO_POST_INC
#define KSZ9031_BMCR