ksz8863_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz8863_driver.c
3  * @brief KSZ8863 Ethernet switch
4  *
5  * @section License
6  *
7  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 //Switch to the appropriate trace level
30 #define TRACE_LEVEL NIC_TRACE_LEVEL
31 
32 //Dependencies
33 #include "core/net.h"
35 #include "debug.h"
36 
37 
38 /**
39  * @brief KSZ8863 Ethernet switch driver
40  **/
41 
43 {
49 };
50 
51 
52 /**
53  * @brief KSZ8863 Ethernet switch initialization
54  * @param[in] interface Underlying network interface
55  * @return Error code
56  **/
57 
59 {
60  uint_t port;
61 
62  //Debug message
63  TRACE_INFO("Initializing KSZ8863...\r\n");
64 
65  //Loop through ports
67  {
68  //Debug message
69  TRACE_INFO("Port %u:\r\n", port);
70  //Dump PHY registers for debugging purpose
71  ksz8863DumpPhyReg(interface, port);
72  }
73 
74  //Force the TCP/IP stack to poll the link state at startup
75  interface->phyEvent = TRUE;
76  //Notify the TCP/IP stack of the event
78 
79  //Successful initialization
80  return NO_ERROR;
81 }
82 
83 
84 /**
85  * @brief Get link state
86  * @param[in] interface Underlying network interface
87  * @param[in] port Port number
88  * @return Link state
89  **/
90 
92 {
93  uint16_t status;
94  bool_t linkState;
95 
96  //Check port number
98  {
99  //Get exclusive access
101  //Read status register
102  status = ksz8863ReadPhyReg(interface, port, KSZ8863_PHY_REG_BMSR);
103  //Release exclusive access
105 
106  //Retrieve current link state
107  linkState = (status & BMSR_LINK_STATUS) ? TRUE : FALSE;
108  }
109  else
110  {
111  //The specified port number is not valid
112  linkState = FALSE;
113  }
114 
115  //Return link status
116  return linkState;
117 }
118 
119 
120 /**
121  * @brief KSZ8863 timer handler
122  * @param[in] interface Underlying network interface
123  **/
124 
125 void ksz8863Tick(NetInterface *interface)
126 {
127  uint_t port;
128  uint16_t status;
129  bool_t linkState;
130 
131  //Initialize link state
132  linkState = FALSE;
133 
134  //Loop through ports
136  {
137  //Read status register
138  status = ksz8863ReadPhyReg(interface, port, KSZ8863_PHY_REG_BMSR);
139 
140  //Retrieve current link state
141  if(status & BMSR_LINK_STATUS)
142  linkState = TRUE;
143  }
144 
145  //Link up event?
146  if(linkState)
147  {
148  if(!interface->linkState)
149  {
150  //Set event flag
151  interface->phyEvent = TRUE;
152  //Notify the TCP/IP stack of the event
154  }
155  }
156  //Link down event?
157  else
158  {
159  if(interface->linkState)
160  {
161  //Set event flag
162  interface->phyEvent = TRUE;
163  //Notify the TCP/IP stack of the event
165  }
166  }
167 }
168 
169 
170 /**
171  * @brief Enable interrupts
172  * @param[in] interface Underlying network interface
173  **/
174 
176 {
177 }
178 
179 
180 /**
181  * @brief Disable interrupts
182  * @param[in] interface Underlying network interface
183  **/
184 
186 {
187 }
188 
189 
190 /**
191  * @brief KSZ8863 event handler
192  * @param[in] interface Underlying network interface
193  **/
194 
196 {
197  uint_t port;
198  uint16_t status;
199  bool_t linkState;
200 
201  //Initialize link state
202  linkState = FALSE;
203 
204  //Loop through ports
206  {
207  //Read status register
208  status = ksz8863ReadPhyReg(interface, port, KSZ8863_PHY_REG_BMSR);
209 
210  //Retrieve current link state
211  if(status & BMSR_LINK_STATUS)
212  linkState = TRUE;
213  }
214 
215  //Link up event?
216  if(linkState)
217  {
218  //Set current speed
219  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
220  //Set duplex mode
221  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
222 
223  //Update link state
224  interface->linkState = TRUE;
225 
226  //Adjust MAC configuration parameters for proper operation
227  interface->nicDriver->updateMacConfig(interface);
228  }
229  else
230  {
231  //Update link state
232  interface->linkState = FALSE;
233  }
234 
235  //Process link state change event
236  nicNotifyLinkChange(interface);
237 }
238 
239 
240 /**
241  * @brief Write PHY register
242  * @param[in] interface Underlying network interface
243  * @param[in] port Port number
244  * @param[in] address PHY register address
245  * @param[in] data Register value
246  **/
247 
249  uint8_t port, uint8_t address, uint16_t data)
250 {
251  //Write the specified PHY register
252  interface->nicDriver->writePhyReg(port, address, data);
253 }
254 
255 
256 /**
257  * @brief Read PHY register
258  * @param[in] interface Underlying network interface
259  * @param[in] port Port number
260  * @param[in] address PHY register address
261  * @return Register value
262  **/
263 
264 uint16_t ksz8863ReadPhyReg(NetInterface *interface,
265  uint8_t port, uint8_t address)
266 {
267  //Read the specified PHY register
268  return interface->nicDriver->readPhyReg(port, address);
269 }
270 
271 
272 /**
273  * @brief Dump PHY registers for debugging purpose
274  * @param[in] interface Underlying network interface
275  * @param[in] port Port number
276  **/
277 
278 void ksz8863DumpPhyReg(NetInterface *interface, uint8_t port)
279 {
280  uint8_t i;
281 
282  //Loop through PHY registers
283  for(i = 0; i < 32; i++)
284  {
285  //Display current PHY register
286  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, ksz8863ReadPhyReg(interface, port, i));
287  }
288 
289  //Terminate with a line feed
290  TRACE_DEBUG("\r\n");
291 }
error_t ksz8863Init(NetInterface *interface)
KSZ8863 Ethernet switch initialization.
void ksz8863DisableIrq(NetInterface *interface)
Disable interrupts.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
TCP/IP stack core.
const PhyDriver ksz8863PhyDriver
KSZ8863 Ethernet switch driver.
Debugging facilities.
#define KSZ8863_PHY_REG_BMSR
void ksz8863WritePhyReg(NetInterface *interface, uint8_t port, uint8_t address, uint16_t data)
Write PHY register.
#define BMSR_LINK_STATUS
Definition: ar8031_driver.h:95
void ksz8863EnableIrq(NetInterface *interface)
Enable interrupts.
#define TRUE
Definition: os_port.h:48
PHY driver.
Definition: nic.h:196
uint16_t ksz8863ReadPhyReg(NetInterface *interface, uint8_t port, uint8_t address)
Read PHY register.
void ksz8863DumpPhyReg(NetInterface *interface, uint8_t port)
Dump PHY registers for debugging purpose.
KSZ8863 Ethernet switch.
#define TRACE_INFO(...)
Definition: debug.h:86
Success.
Definition: error.h:42
Ipv6Addr address
OsEvent netEvent
Definition: net.c:72
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
void osReleaseMutex(OsMutex *mutex)
Release ownership of the specified mutex object.
uint8_t data[]
Definition: dtls_misc.h:167
#define NetInterface
Definition: net.h:34
uint16_t port
Definition: dns_common.h:221
OsMutex netMutex
Definition: net.c:70
#define KSZ8863_PORT1
#define FALSE
Definition: os_port.h:44
bool_t ksz8863GetLinkState(NetInterface *interface, uint8_t port)
Get link state.
void ksz8863EventHandler(NetInterface *interface)
KSZ8863 event handler.
int bool_t
Definition: compiler_port.h:47
void osAcquireMutex(OsMutex *mutex)
Acquire ownership of the specified mutex object.
void ksz8863Tick(NetInterface *interface)
KSZ8863 timer handler.
#define KSZ8863_PORT2
#define TRACE_DEBUG(...)
Definition: debug.h:98