ksz9031_driver.c
Go to the documentation of this file.
1 /**
2  * @file ksz9031_driver.c
3  * @brief KSZ9031 Gigabit Ethernet PHY transceiver
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 KSZ9031 Ethernet PHY driver
40  **/
41 
43 {
49 };
50 
51 
52 /**
53  * @brief KSZ9031 PHY transceiver initialization
54  * @param[in] interface Underlying network interface
55  * @return Error code
56  **/
57 
59 {
60  //Debug message
61  TRACE_INFO("Initializing KSZ9031...\r\n");
62 
63  //Initialize external interrupt line driver
64  if(interface->extIntDriver != NULL)
65  interface->extIntDriver->init();
66 
67  //Reset PHY transceiver
69  //Wait for the reset to complete
71 
72  //Dump PHY registers for debugging purpose
73  ksz9031DumpPhyReg(interface);
74 
75  //The PHY will generate interrupts when link status changes are detected
77 
78  //Force the TCP/IP stack to poll the link state at startup
79  interface->phyEvent = TRUE;
80  //Notify the TCP/IP stack of the event
82 
83  //Successful initialization
84  return NO_ERROR;
85 }
86 
87 
88 /**
89  * @brief KSZ9031 timer handler
90  * @param[in] interface Underlying network interface
91  **/
92 
93 void ksz9031Tick(NetInterface *interface)
94 {
95  uint16_t value;
96  bool_t linkState;
97 
98  //No external interrupt line driver?
99  if(interface->extIntDriver == NULL)
100  {
101  //Read basic status register
103  //Retrieve current link state
104  linkState = (value & BMSR_LINK_STATUS) ? TRUE : FALSE;
105 
106  //Link up event?
107  if(linkState && !interface->linkState)
108  {
109  //Set event flag
110  interface->phyEvent = TRUE;
111  //Notify the TCP/IP stack of the event
113  }
114  //Link down event?
115  else if(!linkState && interface->linkState)
116  {
117  //Set event flag
118  interface->phyEvent = TRUE;
119  //Notify the TCP/IP stack of the event
121  }
122  }
123 }
124 
125 
126 /**
127  * @brief Enable interrupts
128  * @param[in] interface Underlying network interface
129  **/
130 
132 {
133  //Enable PHY transceiver interrupts
134  if(interface->extIntDriver != NULL)
135  interface->extIntDriver->enableIrq();
136 }
137 
138 
139 /**
140  * @brief Disable interrupts
141  * @param[in] interface Underlying network interface
142  **/
143 
145 {
146  //Disable PHY transceiver interrupts
147  if(interface->extIntDriver != NULL)
148  interface->extIntDriver->disableIrq();
149 }
150 
151 
152 /**
153  * @brief KSZ9031 event handler
154  * @param[in] interface Underlying network interface
155  **/
156 
158 {
159  uint16_t value;
160 
161  //Read status register to acknowledge the interrupt
163 
164  //Link status change?
166  {
167  //Any link failure condition is latched in the BMSR register... Reading
168  //the register twice will always return the actual link status
171 
172  //Link is up?
173  if(value & BMSR_LINK_STATUS)
174  {
175  //Read PHY control register
177 
178  //Check current speed
180  {
181  //1000BASE-T
182  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
183  }
184  else if(value & PHYCON_SPEED_100BTX)
185  {
186  //100BASE-TX
187  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
188  }
189  else if(value & PHYCON_SPEED_10BT)
190  {
191  //10BASE-T
192  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
193  }
194  else
195  {
196  //Debug message
197  TRACE_WARNING("Invalid speed!\r\n");
198  }
199 
200  //Check current duplex mode
202  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
203  else
204  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
205 
206  //Update link state
207  interface->linkState = TRUE;
208 
209  //Adjust MAC configuration parameters for proper operation
210  interface->nicDriver->updateMacConfig(interface);
211  }
212  else
213  {
214  //Update link state
215  interface->linkState = FALSE;
216  }
217 
218  //Process link state change event
219  nicNotifyLinkChange(interface);
220  }
221 }
222 
223 
224 /**
225  * @brief Write PHY register
226  * @param[in] interface Underlying network interface
227  * @param[in] address PHY register address
228  * @param[in] data Register value
229  **/
230 
231 void ksz9031WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
232 {
233  uint8_t phyAddr;
234 
235  //Get the address of the PHY transceiver
236  if(interface->phyAddr < 32)
237  phyAddr = interface->phyAddr;
238  else
239  phyAddr = KSZ9031_PHY_ADDR;
240 
241  //Write the specified PHY register
242  interface->nicDriver->writePhyReg(phyAddr, address, data);
243 }
244 
245 
246 /**
247  * @brief Read PHY register
248  * @param[in] interface Underlying network interface
249  * @param[in] address PHY register address
250  * @return Register value
251  **/
252 
253 uint16_t ksz9031ReadPhyReg(NetInterface *interface, uint8_t address)
254 {
255  uint8_t phyAddr;
256 
257  //Get the address of the PHY transceiver
258  if(interface->phyAddr < 32)
259  phyAddr = interface->phyAddr;
260  else
261  phyAddr = KSZ9031_PHY_ADDR;
262 
263  //Read the specified PHY register
264  return interface->nicDriver->readPhyReg(phyAddr, address);
265 }
266 
267 
268 /**
269  * @brief Dump PHY registers for debugging purpose
270  * @param[in] interface Underlying network interface
271  **/
272 
274 {
275  uint8_t i;
276 
277  //Loop through PHY registers
278  for(i = 0; i < 32; i++)
279  {
280  //Display current PHY register
281  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i, ksz9031ReadPhyReg(interface, i));
282  }
283 
284  //Terminate with a line feed
285  TRACE_DEBUG("\r\n");
286 }
void ksz9031DisableIrq(NetInterface *interface)
Disable interrupts.
void nicNotifyLinkChange(NetInterface *interface)
Process link state change event.
Definition: nic.c:298
void ksz9031EnableIrq(NetInterface *interface)
Enable interrupts.
TCP/IP stack core.
Debugging facilities.
void ksz9031WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define BMSR_LINK_STATUS
Definition: ar8031_driver.h:95
#define PHYCON_SPEED_100BTX
#define KSZ9031_PHY_ADDR
#define ICSR_LINK_UP_IF
#define TRUE
Definition: os_port.h:48
#define KSZ9031_PHY_REG_PHYCON
#define PHYCON_SPEED_1000BT
PHY driver.
Definition: nic.h:196
void ksz9031Tick(NetInterface *interface)
KSZ9031 timer handler.
#define BMCR_RESET
Definition: ar8031_driver.h:71
const PhyDriver ksz9031PhyDriver
KSZ9031 Ethernet PHY driver.
#define KSZ9031_PHY_REG_BMSR
#define ICSR_LINK_UP_IE
uint16_t ksz9031ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define ICSR_LINK_DOWN_IF
#define TRACE_INFO(...)
Definition: debug.h:86
#define PHYCON_SPEED_10BT
Success.
Definition: error.h:42
Ipv6Addr address
#define ICSR_LINK_DOWN_IE
KSZ9031 Gigabit Ethernet PHY transceiver.
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
#define TRACE_WARNING(...)
Definition: debug.h:78
#define PHYCON_DUPLEX_STATUS
uint8_t data[]
Definition: dtls_misc.h:167
error_t ksz9031Init(NetInterface *interface)
KSZ9031 PHY transceiver initialization.
#define NetInterface
Definition: net.h:34
#define KSZ9031_PHY_REG_BMCR
uint8_t value[]
Definition: dtls_misc.h:141
#define KSZ9031_PHY_REG_ICSR
void ksz9031DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define FALSE
Definition: os_port.h:44
int bool_t
Definition: compiler_port.h:47
void ksz9031EventHandler(NetInterface *interface)
KSZ9031 event handler.
#define TRACE_DEBUG(...)
Definition: debug.h:98