ksz8061_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz8061_driver.c
3  * @brief KSZ8061 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 KSZ8061 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief KSZ8061 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing KSZ8061...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = KSZ8061_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  ksz8061DumpPhyReg(interface);
94 
95  //Silicon errata workaround #1
96  ksz8061WritePhyReg(interface, KSZ8061_MMDACR, 0x0001);
97  ksz8061WritePhyReg(interface, KSZ8061_MMDAADR, 0x0002);
98  ksz8061WritePhyReg(interface, KSZ8061_MMDACR, 0x4001);
99  ksz8061WritePhyReg(interface, KSZ8061_MMDAADR, 0xB61A);
100 
101  //Silicon errata workaround #2
102  ksz8061WritePhyReg(interface, KSZ8061_MMDACR, 0x0001);
103  ksz8061WritePhyReg(interface, KSZ8061_MMDAADR, 0x001D);
104  ksz8061WritePhyReg(interface, KSZ8061_MMDACR, 0x4001);
105  ksz8061WritePhyReg(interface, KSZ8061_MMDAADR, 0x0110);
106 
107  //The PHY will generate interrupts when link status changes are detected
110 
111  //Force the TCP/IP stack to poll the link state at startup
112  interface->phyEvent = TRUE;
113  //Notify the TCP/IP stack of the event
115 
116  //Successful initialization
117  return NO_ERROR;
118 }
119 
120 
121 /**
122  * @brief KSZ8061 timer handler
123  * @param[in] interface Underlying network interface
124  **/
125 
126 void ksz8061Tick(NetInterface *interface)
127 {
128  uint16_t value;
129  bool_t linkState;
130 
131  //No external interrupt line driver?
132  if(interface->extIntDriver == NULL)
133  {
134  //Read basic status register
135  value = ksz8061ReadPhyReg(interface, KSZ8061_BMSR);
136  //Retrieve current link state
137  linkState = (value & KSZ8061_BMSR_LINK_STATUS) ? TRUE : FALSE;
138 
139  //Link up event?
140  if(linkState && !interface->linkState)
141  {
142  //Set event flag
143  interface->phyEvent = TRUE;
144  //Notify the TCP/IP stack of the event
146  }
147  //Link down event?
148  else if(!linkState && interface->linkState)
149  {
150  //Set event flag
151  interface->phyEvent = TRUE;
152  //Notify the TCP/IP stack of the event
154  }
155  }
156 }
157 
158 
159 /**
160  * @brief Enable interrupts
161  * @param[in] interface Underlying network interface
162  **/
163 
165 {
166  //Enable PHY transceiver interrupts
167  if(interface->extIntDriver != NULL)
168  {
169  interface->extIntDriver->enableIrq();
170  }
171 }
172 
173 
174 /**
175  * @brief Disable interrupts
176  * @param[in] interface Underlying network interface
177  **/
178 
180 {
181  //Disable PHY transceiver interrupts
182  if(interface->extIntDriver != NULL)
183  {
184  interface->extIntDriver->disableIrq();
185  }
186 }
187 
188 
189 /**
190  * @brief KSZ8061 event handler
191  * @param[in] interface Underlying network interface
192  **/
193 
195 {
196  uint16_t value;
197 
198  //Read status register to acknowledge the interrupt
199  value = ksz8061ReadPhyReg(interface, KSZ8061_ICSR);
200 
201  //Link status change?
203  {
204  //Any link failure condition is latched in the BMSR register. Reading
205  //the register twice will always return the actual link status
206  value = ksz8061ReadPhyReg(interface, KSZ8061_BMSR);
207  value = ksz8061ReadPhyReg(interface, KSZ8061_BMSR);
208 
209  //Link is up?
210  if((value & KSZ8061_BMSR_LINK_STATUS) != 0)
211  {
212  //Read PHY control register
214 
215  //Check current operation mode
217  {
218  //10BASE-T half-duplex
220  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
221  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
222  break;
223  //10BASE-T full-duplex
225  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
226  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
227  break;
228  //100BASE-TX half-duplex
230  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
231  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
232  break;
233  //100BASE-TX full-duplex
235  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
236  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
237  break;
238  //Unknown operation mode
239  default:
240  //Debug message
241  TRACE_WARNING("Invalid operation mode!\r\n");
242  break;
243  }
244 
245  //Update link state
246  interface->linkState = TRUE;
247 
248  //Adjust MAC configuration parameters for proper operation
249  interface->nicDriver->updateMacConfig(interface);
250  }
251  else
252  {
253  //Update link state
254  interface->linkState = FALSE;
255  }
256 
257  //Process link state change event
258  nicNotifyLinkChange(interface);
259  }
260 }
261 
262 
263 /**
264  * @brief Write PHY register
265  * @param[in] interface Underlying network interface
266  * @param[in] address PHY register address
267  * @param[in] data Register value
268  **/
269 
270 void ksz8061WritePhyReg(NetInterface *interface, uint8_t address,
271  uint16_t data)
272 {
273  //Write the specified PHY register
274  if(interface->smiDriver != NULL)
275  {
276  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
277  interface->phyAddr, address, data);
278  }
279  else
280  {
281  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
282  interface->phyAddr, address, data);
283  }
284 }
285 
286 
287 /**
288  * @brief Read PHY register
289  * @param[in] interface Underlying network interface
290  * @param[in] address PHY register address
291  * @return Register value
292  **/
293 
294 uint16_t ksz8061ReadPhyReg(NetInterface *interface, uint8_t address)
295 {
296  uint16_t data;
297 
298  //Read the specified PHY register
299  if(interface->smiDriver != NULL)
300  {
301  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
302  interface->phyAddr, address);
303  }
304  else
305  {
306  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
307  interface->phyAddr, address);
308  }
309 
310  //Return the value of the PHY register
311  return data;
312 }
313 
314 
315 /**
316  * @brief Dump PHY registers for debugging purpose
317  * @param[in] interface Underlying network interface
318  **/
319 
321 {
322  uint8_t i;
323 
324  //Loop through PHY registers
325  for(i = 0; i < 32; i++)
326  {
327  //Display current PHY register
328  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
329  ksz8061ReadPhyReg(interface, i));
330  }
331 
332  //Terminate with a line feed
333  TRACE_DEBUG("\r\n");
334 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
int bool_t
Definition: compiler_port.h:49
#define netEvent
Definition: net_legacy.h:267
#define KSZ8061_BMCR
uint8_t data[]
Definition: ethernet.h:209
#define KSZ8061_BMSR_LINK_STATUS
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:292
#define KSZ8061_PHYCON1_OP_MODE_100BTX_HD
#define KSZ8061_PHYCON1_OP_MODE_100BTX_FD
uint16_t ksz8061ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void ksz8061DisableIrq(NetInterface *interface)
Disable interrupts.
#define SMI_OPCODE_WRITE
Definition: nic.h:65
#define KSZ8061_PHYCON1_OP_MODE_10BT_FD
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:42
error_t ksz8061Init(NetInterface *interface)
KSZ8061 PHY transceiver initialization.
#define KSZ8061_PHYCON1
uint8_t value[]
Definition: tcp.h:332
#define KSZ8061_PHYCON1_OP_MODE_10BT_HD
#define NetInterface
Definition: net.h:36
#define KSZ8061_ICSR_LINK_DOWN_IF
#define SMI_OPCODE_READ
Definition: nic.h:66
#define TRACE_INFO(...)
Definition: debug.h:95
void ksz8061EnableIrq(NetInterface *interface)
Enable interrupts.
KSZ8061 Ethernet PHY driver.
const PhyDriver ksz8061PhyDriver
KSZ8061 Ethernet PHY driver.
#define KSZ8061_PHY_ADDR
#define KSZ8061_BMCR_RESET
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define KSZ8061_MMDACR
#define KSZ8061_PHYCON1_OP_MODE
void ksz8061Tick(NetInterface *interface)
KSZ8061 timer handler.
void ksz8061DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define KSZ8061_ICSR_LINK_UP_IF
#define KSZ8061_ICSR_LINK_DOWN_IE
#define KSZ8061_MMDAADR
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
TCP/IP stack core.
void ksz8061EventHandler(NetInterface *interface)
KSZ8061 event handler.
#define KSZ8061_ICSR
void ksz8061WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define KSZ8061_ICSR_LINK_UP_IE
Success.
Definition: error.h:44
#define KSZ8061_BMSR
Debugging facilities.