pef7071_driver.c
Go to the documentation of this file.
1 /**
2  * @file pef7071_driver.c
3  * @brief XWAY PHY11G (PEF7071) Gigabit Ethernet PHY driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 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 2.4.0
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 PEF7071 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief PEF7071 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing PEF7071...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = PEF7071_PHY_ADDR;
70  }
71 
72  //Initialize serial management interface
73  if(interface->smiDriver != NULL)
74  {
75  interface->smiDriver->init();
76  }
77 
78  //Reset PHY transceiver
80 
81  //Wait for the reset to complete
83  {
84  }
85 
86  //Dump PHY registers for debugging purpose
87  pef7071DumpPhyReg(interface);
88 
89  //Select RMII mode
92 
93  //The link speed is forced to 10/100 Mbit/s only
94  pef7071WritePhyReg(interface, PEF7071_GCTRL, 0);
95 
96  //Restart auto-negotiation
99 
100  //Perform custom configuration
101  pef7071InitHook(interface);
102 
103  //Force the TCP/IP stack to poll the link state at startup
104  interface->phyEvent = TRUE;
105  //Notify the TCP/IP stack of the event
107 
108  //Successful initialization
109  return NO_ERROR;
110 }
111 
112 
113 /**
114  * @brief PEF7071 custom configuration
115  * @param[in] interface Underlying network interface
116  **/
117 
118 __weak_func void pef7071InitHook(NetInterface *interface)
119 {
120 }
121 
122 
123 /**
124  * @brief PEF7071 timer handler
125  * @param[in] interface Underlying network interface
126  **/
127 
128 void pef7071Tick(NetInterface *interface)
129 {
130  uint16_t value;
131  bool_t linkState;
132 
133  //Read status register
134  value = pef7071ReadPhyReg(interface, PEF7071_STAT);
135  //Retrieve current link state
136  linkState = (value & PEF7071_STAT_LS) ? TRUE : FALSE;
137 
138  //Link up event?
139  if(linkState && !interface->linkState)
140  {
141  //Set event flag
142  interface->phyEvent = TRUE;
143  //Notify the TCP/IP stack of the event
145  }
146  //Link down event?
147  else if(!linkState && interface->linkState)
148  {
149  //Set event flag
150  interface->phyEvent = TRUE;
151  //Notify the TCP/IP stack of the event
153  }
154 }
155 
156 
157 /**
158  * @brief Enable interrupts
159  * @param[in] interface Underlying network interface
160  **/
161 
163 {
164 }
165 
166 
167 /**
168  * @brief Disable interrupts
169  * @param[in] interface Underlying network interface
170  **/
171 
173 {
174 }
175 
176 
177 /**
178  * @brief PEF7071 event handler
179  * @param[in] interface Underlying network interface
180  **/
181 
183 {
184  uint16_t status;
185 
186  //Read status register
187  status = pef7071ReadPhyReg(interface, PEF7071_STAT);
188 
189  //Link is up?
190  if((status & PEF7071_STAT_LS) != 0)
191  {
192  //Read MII status register
193  status = pef7071ReadPhyReg(interface, PEF7071_MIISTAT);
194 
195  //Check current speed
196  switch(status & PEF7071_MIISTAT_SPEED)
197  {
198  //10BASE-T
200  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
201  break;
202  //100BASE-TX
204  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
205  break;
206  //1000BASE-T
208  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
209  break;
210  //Unknown speed
211  default:
212  //Debug message
213  TRACE_WARNING("Invalid speed\r\n");
214  break;
215  }
216 
217  //Check current duplex mode
218  if((status & PEF7071_MIISTAT_DPX) != 0)
219  {
220  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
221  }
222  else
223  {
224  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
225  }
226 
227  //Update link state
228  interface->linkState = TRUE;
229 
230  //Adjust MAC configuration parameters for proper operation
231  interface->nicDriver->updateMacConfig(interface);
232  }
233  else
234  {
235  //Update link state
236  interface->linkState = FALSE;
237  }
238 
239  //Process link state change event
240  nicNotifyLinkChange(interface);
241 }
242 
243 
244 /**
245  * @brief Write PHY register
246  * @param[in] interface Underlying network interface
247  * @param[in] address PHY register address
248  * @param[in] data Register value
249  **/
250 
251 void pef7071WritePhyReg(NetInterface *interface, uint8_t address,
252  uint16_t data)
253 {
254  //Write the specified PHY register
255  if(interface->smiDriver != NULL)
256  {
257  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
258  interface->phyAddr, address, data);
259  }
260  else
261  {
262  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
263  interface->phyAddr, address, data);
264  }
265 }
266 
267 
268 /**
269  * @brief Read PHY register
270  * @param[in] interface Underlying network interface
271  * @param[in] address PHY register address
272  * @return Register value
273  **/
274 
275 uint16_t pef7071ReadPhyReg(NetInterface *interface, uint8_t address)
276 {
277  uint16_t data;
278 
279  //Read the specified PHY register
280  if(interface->smiDriver != NULL)
281  {
282  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
283  interface->phyAddr, address);
284  }
285  else
286  {
287  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
288  interface->phyAddr, address);
289  }
290 
291  //Return the value of the PHY register
292  return data;
293 }
294 
295 
296 /**
297  * @brief Dump PHY registers for debugging purpose
298  * @param[in] interface Underlying network interface
299  **/
300 
302 {
303  uint8_t i;
304 
305  //Loop through PHY registers
306  for(i = 0; i < 32; i++)
307  {
308  //Display current PHY register
309  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
310  pef7071ReadPhyReg(interface, i));
311  }
312 
313  //Terminate with a line feed
314  TRACE_DEBUG("\r\n");
315 }
316 
317 
318 /**
319  * @brief Write MMD register
320  * @param[in] interface Underlying network interface
321  * @param[in] devAddr Device address
322  * @param[in] regAddr Register address
323  * @param[in] data MMD register value
324  **/
325 
326 void pef7071WriteMmdReg(NetInterface *interface, uint8_t devAddr,
327  uint16_t regAddr, uint16_t data)
328 {
329  //Select register operation
332 
333  //Write MMD register address
335 
336  //Select data operation
339 
340  //Write the content of the MMD register
342 }
343 
344 
345 /**
346  * @brief Read MMD register
347  * @param[in] interface Underlying network interface
348  * @param[in] devAddr Device address
349  * @param[in] regAddr Register address
350  * @return MMD register value
351  **/
352 
353 uint16_t pef7071ReadMmdReg(NetInterface *interface, uint8_t devAddr,
354  uint16_t regAddr)
355 {
356  //Select register operation
359 
360  //Write MMD register address
362 
363  //Select data operation
366 
367  //Read the content of the MMD register
368  return pef7071ReadPhyReg(interface, PEF7071_MMDDATA);
369 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_INFO(...)
Definition: debug.h:95
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
Ipv6Addr address[]
Definition: ipv6.h:316
uint16_t regAddr
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
error_t pef7071Init(NetInterface *interface)
PEF7071 PHY transceiver initialization.
void pef7071EnableIrq(NetInterface *interface)
Enable interrupts.
void pef7071Tick(NetInterface *interface)
PEF7071 timer handler.
const PhyDriver pef7071PhyDriver
PEF7071 Ethernet PHY driver.
__weak_func void pef7071InitHook(NetInterface *interface)
PEF7071 custom configuration.
uint16_t pef7071ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
uint16_t pef7071ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
void pef7071DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
void pef7071WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
void pef7071DisableIrq(NetInterface *interface)
Disable interrupts.
void pef7071EventHandler(NetInterface *interface)
PEF7071 event handler.
void pef7071WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
XWAY PHY11G (PEF7071) Gigabit Ethernet PHY driver.
#define PEF7071_MMDCTRL_ACTYPE_ADDR
#define PEF7071_MIISTAT
#define PEF7071_CTRL_RST
#define PEF7071_CTRL_ANEN
#define PEF7071_MIICTRL_MODE_RMII
#define PEF7071_MIISTAT_SPEED_GIGA
#define PEF7071_GCTRL
#define PEF7071_MIISTAT_SPEED_FAST
#define PEF7071_STAT_LS
#define PEF7071_CTRL
#define PEF7071_MIISTAT_DPX
#define PEF7071_PHY_ADDR
#define PEF7071_MMDCTRL
#define PEF7071_CTRL_ANRS
#define PEF7071_MMDCTRL_ACTYPE_DATA
#define PEF7071_STAT
#define PEF7071_MIICTRL_RXCOFF
#define PEF7071_MIISTAT_SPEED_TEN
#define PEF7071_MMDDATA
#define PEF7071_MIICTRL
#define PEF7071_MMDCTRL_DEVAD
#define PEF7071_MIISTAT_SPEED
Ethernet PHY driver.
Definition: nic.h:308
uint8_t value[]
Definition: tcp.h:369