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-2020 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 1.9.8
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  //Debug message
63  TRACE_INFO("Initializing KSZ9031...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = KSZ9031_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  ksz9031DumpPhyReg(interface);
94 
95  //The PHY will generate interrupts when link status changes are detected
98 
99  //Force the TCP/IP stack to poll the link state at startup
100  interface->phyEvent = TRUE;
101  //Notify the TCP/IP stack of the event
103 
104  //Successful initialization
105  return NO_ERROR;
106 }
107 
108 
109 /**
110  * @brief KSZ9031 timer handler
111  * @param[in] interface Underlying network interface
112  **/
113 
114 void ksz9031Tick(NetInterface *interface)
115 {
116  uint16_t value;
117  bool_t linkState;
118 
119  //No external interrupt line driver?
120  if(interface->extIntDriver == NULL)
121  {
122  //Read basic status register
123  value = ksz9031ReadPhyReg(interface, KSZ9031_BMSR);
124  //Retrieve current link state
125  linkState = (value & KSZ9031_BMSR_LINK_STATUS) ? TRUE : FALSE;
126 
127  //Link up event?
128  if(linkState && !interface->linkState)
129  {
130  //Set event flag
131  interface->phyEvent = TRUE;
132  //Notify the TCP/IP stack of the event
134  }
135  //Link down event?
136  else if(!linkState && interface->linkState)
137  {
138  //Set event flag
139  interface->phyEvent = TRUE;
140  //Notify the TCP/IP stack of the event
142  }
143  }
144 }
145 
146 
147 /**
148  * @brief Enable interrupts
149  * @param[in] interface Underlying network interface
150  **/
151 
153 {
154  //Enable PHY transceiver interrupts
155  if(interface->extIntDriver != NULL)
156  {
157  interface->extIntDriver->enableIrq();
158  }
159 }
160 
161 
162 /**
163  * @brief Disable interrupts
164  * @param[in] interface Underlying network interface
165  **/
166 
168 {
169  //Disable PHY transceiver interrupts
170  if(interface->extIntDriver != NULL)
171  {
172  interface->extIntDriver->disableIrq();
173  }
174 }
175 
176 
177 /**
178  * @brief KSZ9031 event handler
179  * @param[in] interface Underlying network interface
180  **/
181 
183 {
184  uint16_t value;
185 
186  //Read status register to acknowledge the interrupt
187  value = ksz9031ReadPhyReg(interface, KSZ9031_ICSR);
188 
189  //Link status change?
191  {
192  //Any link failure condition is latched in the BMSR register. Reading
193  //the register twice will always return the actual link status
194  value = ksz9031ReadPhyReg(interface, KSZ9031_BMSR);
195  value = ksz9031ReadPhyReg(interface, KSZ9031_BMSR);
196 
197  //Link is up?
198  if((value & KSZ9031_BMSR_LINK_STATUS) != 0)
199  {
200  //Read PHY control register
201  value = ksz9031ReadPhyReg(interface, KSZ9031_PHYCON);
202 
203  //Check current speed
205  {
206  //1000BASE-T
207  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
208  }
209  else if((value & KSZ9031_PHYCON_SPEED_100BTX) != 0)
210  {
211  //100BASE-TX
212  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
213  }
214  else if((value & KSZ9031_PHYCON_SPEED_10BT) != 0)
215  {
216  //10BASE-T
217  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
218  }
219  else
220  {
221  //Debug message
222  TRACE_WARNING("Invalid speed!\r\n");
223  }
224 
225  //Check current duplex mode
227  {
228  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
229  }
230  else
231  {
232  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
233  }
234 
235  //Update link state
236  interface->linkState = TRUE;
237 
238  //Adjust MAC configuration parameters for proper operation
239  interface->nicDriver->updateMacConfig(interface);
240  }
241  else
242  {
243  //Update link state
244  interface->linkState = FALSE;
245  }
246 
247  //Process link state change event
248  nicNotifyLinkChange(interface);
249  }
250 }
251 
252 
253 /**
254  * @brief Write PHY register
255  * @param[in] interface Underlying network interface
256  * @param[in] address PHY register address
257  * @param[in] data Register value
258  **/
259 
260 void ksz9031WritePhyReg(NetInterface *interface, uint8_t address,
261  uint16_t data)
262 {
263  //Write the specified PHY register
264  if(interface->smiDriver != NULL)
265  {
266  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
267  interface->phyAddr, address, data);
268  }
269  else
270  {
271  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
272  interface->phyAddr, address, data);
273  }
274 }
275 
276 
277 /**
278  * @brief Read PHY register
279  * @param[in] interface Underlying network interface
280  * @param[in] address PHY register address
281  * @return Register value
282  **/
283 
284 uint16_t ksz9031ReadPhyReg(NetInterface *interface, uint8_t address)
285 {
286  uint16_t data;
287 
288  //Read the specified PHY register
289  if(interface->smiDriver != NULL)
290  {
291  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
292  interface->phyAddr, address);
293  }
294  else
295  {
296  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
297  interface->phyAddr, address);
298  }
299 
300  //Return the value of the PHY register
301  return data;
302 }
303 
304 
305 /**
306  * @brief Dump PHY registers for debugging purpose
307  * @param[in] interface Underlying network interface
308  **/
309 
311 {
312  uint8_t i;
313 
314  //Loop through PHY registers
315  for(i = 0; i < 32; i++)
316  {
317  //Display current PHY register
318  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
319  ksz9031ReadPhyReg(interface, i));
320  }
321 
322  //Terminate with a line feed
323  TRACE_DEBUG("\r\n");
324 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
#define KSZ9031_ICSR
int bool_t
Definition: compiler_port.h:49
void ksz9031Tick(NetInterface *interface)
KSZ9031 timer handler.
#define netEvent
Definition: net_legacy.h:267
#define KSZ9031_PHY_ADDR
uint8_t data[]
Definition: ethernet.h:209
#define TRUE
Definition: os_port.h:50
#define KSZ9031_PHYCON_SPEED_10BT
Ethernet PHY driver.
Definition: nic.h:292
#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:65
#define KSZ9031_ICSR_LINK_DOWN_IE
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
uint8_t value[]
Definition: tcp.h:332
void ksz9031EventHandler(NetInterface *interface)
KSZ9031 event handler.
#define NetInterface
Definition: net.h:36
KSZ9031 Gigabit Ethernet PHY driver.
#define SMI_OPCODE_READ
Definition: nic.h:66
error_t ksz9031Init(NetInterface *interface)
KSZ9031 PHY transceiver initialization.
#define TRACE_INFO(...)
Definition: debug.h:95
#define KSZ9031_PHYCON_SPEED_1000BT
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define KSZ9031_BMCR_RESET
void ksz9031DisableIrq(NetInterface *interface)
Disable interrupts.
#define KSZ9031_BMSR
#define KSZ9031_PHYCON_SPEED_100BTX
#define KSZ9031_BMSR_LINK_STATUS
#define KSZ9031_PHYCON_DUPLEX_STATUS
#define KSZ9031_ICSR_LINK_DOWN_IF
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void ksz9031DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
const PhyDriver ksz9031PhyDriver
KSZ9031 Ethernet PHY driver.
TCP/IP stack core.
#define KSZ9031_PHYCON
void ksz9031WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define KSZ9031_ICSR_LINK_UP_IE
Success.
Definition: error.h:44
Debugging facilities.
#define KSZ9031_BMCR