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