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-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 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  //Force the TCP/IP stack to poll the link state at startup
114  interface->phyEvent = TRUE;
115  //Notify the TCP/IP stack of the event
117 
118  //Successful initialization
119  return NO_ERROR;
120 }
121 
122 
123 /**
124  * @brief KSZ8081 timer handler
125  * @param[in] interface Underlying network interface
126  **/
127 
128 void ksz8081Tick(NetInterface *interface)
129 {
130  uint16_t value;
131  bool_t linkState;
132 
133  //No external interrupt line driver?
134  if(interface->extIntDriver == NULL)
135  {
136  //Read basic status register
137  value = ksz8081ReadPhyReg(interface, KSZ8081_BMSR);
138  //Retrieve current link state
139  linkState = (value & KSZ8081_BMSR_LINK_STATUS) ? TRUE : FALSE;
140 
141  //Link up event?
142  if(linkState && !interface->linkState)
143  {
144  //Set event flag
145  interface->phyEvent = TRUE;
146  //Notify the TCP/IP stack of the event
148  }
149  //Link down event?
150  else if(!linkState && interface->linkState)
151  {
152  //Set event flag
153  interface->phyEvent = TRUE;
154  //Notify the TCP/IP stack of the event
156  }
157  }
158 }
159 
160 
161 /**
162  * @brief Enable interrupts
163  * @param[in] interface Underlying network interface
164  **/
165 
167 {
168  //Enable PHY transceiver interrupts
169  if(interface->extIntDriver != NULL)
170  {
171  interface->extIntDriver->enableIrq();
172  }
173 }
174 
175 
176 /**
177  * @brief Disable interrupts
178  * @param[in] interface Underlying network interface
179  **/
180 
182 {
183  //Disable PHY transceiver interrupts
184  if(interface->extIntDriver != NULL)
185  {
186  interface->extIntDriver->disableIrq();
187  }
188 }
189 
190 
191 /**
192  * @brief KSZ8081 event handler
193  * @param[in] interface Underlying network interface
194  **/
195 
197 {
198  uint16_t value;
199 
200  //Read status register to acknowledge the interrupt
201  value = ksz8081ReadPhyReg(interface, KSZ8081_ICSR);
202 
203  //Link status change?
205  {
206  //Any link failure condition is latched in the BMSR register. Reading
207  //the register twice will always return the actual link status
208  value = ksz8081ReadPhyReg(interface, KSZ8081_BMSR);
209  value = ksz8081ReadPhyReg(interface, KSZ8081_BMSR);
210 
211  //Link is up?
212  if((value & KSZ8081_BMSR_LINK_STATUS) != 0)
213  {
214  //Read PHY control register
216 
217  //Check current operation mode
219  {
220  //10BASE-T half-duplex
222  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
223  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
224  break;
225  //10BASE-T full-duplex
227  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
228  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
229  break;
230  //100BASE-TX half-duplex
232  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
233  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
234  break;
235  //100BASE-TX full-duplex
237  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
238  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
239  break;
240  //Unknown operation mode
241  default:
242  //Debug message
243  TRACE_WARNING("Invalid operation mode!\r\n");
244  break;
245  }
246 
247  //Update link state
248  interface->linkState = TRUE;
249 
250  //Adjust MAC configuration parameters for proper operation
251  interface->nicDriver->updateMacConfig(interface);
252  }
253  else
254  {
255  //Update link state
256  interface->linkState = FALSE;
257  }
258 
259  //Process link state change event
260  nicNotifyLinkChange(interface);
261  }
262 }
263 
264 
265 /**
266  * @brief Write PHY register
267  * @param[in] interface Underlying network interface
268  * @param[in] address PHY register address
269  * @param[in] data Register value
270  **/
271 
272 void ksz8081WritePhyReg(NetInterface *interface, uint8_t address,
273  uint16_t data)
274 {
275  //Write the specified PHY register
276  if(interface->smiDriver != NULL)
277  {
278  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
279  interface->phyAddr, address, data);
280  }
281  else
282  {
283  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
284  interface->phyAddr, address, data);
285  }
286 }
287 
288 
289 /**
290  * @brief Read PHY register
291  * @param[in] interface Underlying network interface
292  * @param[in] address PHY register address
293  * @return Register value
294  **/
295 
296 uint16_t ksz8081ReadPhyReg(NetInterface *interface, uint8_t address)
297 {
298  uint16_t data;
299 
300  //Read the specified PHY register
301  if(interface->smiDriver != NULL)
302  {
303  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
304  interface->phyAddr, address);
305  }
306  else
307  {
308  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
309  interface->phyAddr, address);
310  }
311 
312  //Return the value of the PHY register
313  return data;
314 }
315 
316 
317 /**
318  * @brief Dump PHY registers for debugging purpose
319  * @param[in] interface Underlying network interface
320  **/
321 
323 {
324  uint8_t i;
325 
326  //Loop through PHY registers
327  for(i = 0; i < 32; i++)
328  {
329  //Display current PHY register
330  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
331  ksz8081ReadPhyReg(interface, i));
332  }
333 
334  //Terminate with a line feed
335  TRACE_DEBUG("\r\n");
336 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
#define KSZ8081_PHYCON1
int bool_t
Definition: compiler_port.h:49
#define netEvent
Definition: net_legacy.h:267
#define KSZ8081_ICSR
uint8_t data[]
Definition: ethernet.h:209
#define KSZ8081_BMCR_AN_EN
#define KSZ8081_ICSR_LINK_DOWN_IE
void ksz8081DisableIrq(NetInterface *interface)
Disable interrupts.
KSZ8081 Ethernet PHY driver.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:292
#define KSZ8081_ANAR_10BT_FD
#define KSZ8081_PHYCON2_JABBER_EN
#define KSZ8081_PHYCON2
#define SMI_OPCODE_WRITE
Definition: nic.h:65
#define KSZ8081_ICSR_LINK_DOWN_IF
#define KSZ8081_BMSR_LINK_STATUS
#define FALSE
Definition: os_port.h:46
#define KSZ8081_ICSR_LINK_UP_IE
#define KSZ8081_BMSR
error_t
Error codes.
Definition: error.h:42
#define KSZ8081_ICSR_LINK_UP_IF
#define KSZ8081_PHYCON1_OP_MODE_10BT_HD
uint8_t value[]
Definition: tcp.h:332
#define KSZ8081_PHYCON1_OP_MODE
#define NetInterface
Definition: net.h:36
void ksz8081Tick(NetInterface *interface)
KSZ8081 timer handler.
void ksz8081WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define KSZ8081_BMCR
#define SMI_OPCODE_READ
Definition: nic.h:66
#define TRACE_INFO(...)
Definition: debug.h:95
#define KSZ8081_ANAR_100BTX_HD
#define KSZ8081_PHYCON1_OP_MODE_10BT_FD
#define KSZ8081_PHY_ADDR
#define KSZ8081_ANAR
error_t ksz8081Init(NetInterface *interface)
KSZ8081 PHY transceiver initialization.
uint16_t ksz8081ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define KSZ8081_PHYCON2_HP_MDIX
#define KSZ8081_PHYCON1_OP_MODE_100BTX_FD
#define KSZ8081_PHYCON2_RMII_REF_CLK_SEL
#define KSZ8081_ANAR_SELECTOR_DEFAULT
#define KSZ8081_ANAR_10BT_HD
void ksz8081EventHandler(NetInterface *interface)
KSZ8081 event handler.
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
const PhyDriver ksz8081PhyDriver
KSZ8081 Ethernet PHY driver.
#define KSZ8081_ANAR_100BTX_FD
void ksz8081DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void ksz8081EnableIrq(NetInterface *interface)
Enable interrupts.
#define KSZ8081_PHYCON1_OP_MODE_100BTX_HD
TCP/IP stack core.
#define KSZ8081_BMCR_RESET
Success.
Definition: error.h:44
Debugging facilities.