adin1300_driver.c
Go to the documentation of this file.
1 /**
2  * @file adin1300_driver.c
3  * @brief ADIN1300 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 ADIN1300 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief ADIN1300 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  uint16_t value;
63 
64  //Debug message
65  TRACE_INFO("Initializing ADIN1300...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = ADIN1300_PHY_ADDR;
72  }
73 
74  //Initialize serial management interface
75  if(interface->smiDriver != NULL)
76  {
77  interface->smiDriver->init();
78  }
79 
80  //Initialize external interrupt line driver
81  if(interface->extIntDriver != NULL)
82  {
83  interface->extIntDriver->init();
84  }
85 
86  //Reset PHY transceiver
89 
90  //Wait for the reset to complete
91  while(adin1300ReadPhyReg(interface, ADIN1300_MII_CONTROL) &
93  {
94  }
95 
96  //Dump PHY registers for debugging purpose
97  adin1300DumpPhyReg(interface);
98 
99  //The PHY will generate interrupts when link status changes are detected
102 
103  //Set the function of the INT_N pin
106 
107  //Exit software power-down
111 
112  //Perform custom configuration
113  adin1300InitHook(interface);
114 
115  //Force the TCP/IP stack to poll the link state at startup
116  interface->phyEvent = TRUE;
117  //Notify the TCP/IP stack of the event
119 
120  //Successful initialization
121  return NO_ERROR;
122 }
123 
124 
125 /**
126  * @brief ADIN1300 custom configuration
127  * @param[in] interface Underlying network interface
128  **/
129 
130 __weak_func void adin1300InitHook(NetInterface *interface)
131 {
132 }
133 
134 
135 /**
136  * @brief ADIN1300 timer handler
137  * @param[in] interface Underlying network interface
138  **/
139 
140 void adin1300Tick(NetInterface *interface)
141 {
142  uint16_t value;
143  bool_t linkState;
144 
145  //No external interrupt line driver?
146  if(interface->extIntDriver == NULL)
147  {
148  //Read basic status register
150  //Retrieve current link state
152 
153  //Link up event?
154  if(linkState && !interface->linkState)
155  {
156  //Set event flag
157  interface->phyEvent = TRUE;
158  //Notify the TCP/IP stack of the event
160  }
161  //Link down event?
162  else if(!linkState && interface->linkState)
163  {
164  //Set event flag
165  interface->phyEvent = TRUE;
166  //Notify the TCP/IP stack of the event
168  }
169  }
170 }
171 
172 
173 /**
174  * @brief Enable interrupts
175  * @param[in] interface Underlying network interface
176  **/
177 
179 {
180  //Enable PHY transceiver interrupts
181  if(interface->extIntDriver != NULL)
182  {
183  interface->extIntDriver->enableIrq();
184  }
185 }
186 
187 
188 /**
189  * @brief Disable interrupts
190  * @param[in] interface Underlying network interface
191  **/
192 
194 {
195  //Disable PHY transceiver interrupts
196  if(interface->extIntDriver != NULL)
197  {
198  interface->extIntDriver->disableIrq();
199  }
200 }
201 
202 
203 /**
204  * @brief ADIN1300 event handler
205  * @param[in] interface Underlying network interface
206  **/
207 
209 {
210  uint16_t value;
211 
212  //Read IRQ status register to acknowledge the interrupt
214 
215  //Link status change?
217  {
218  //Read PHY status register
220 
221  //Link is up?
223  {
224  //The HCD_TECH field indicates the resolved technology after the link
225  //is established
227  {
228  //10BASE-T half-duplex
230  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
231  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
232  break;
233 
234  //10BASE-T full-duplex
236  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
237  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
238  break;
239 
240  //100BASE-TX half-duplex
242  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
243  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
244  break;
245 
246  //100BASE-TX full-duplex
248  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
249  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
250  break;
251 
252  //1000BASE-T half-duplex
254  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
255  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
256  break;
257 
258  //1000BASE-T full-duplex
260  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
261  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
262  break;
263 
264  //Unknown operation mode
265  default:
266  //Debug message
267  TRACE_WARNING("Invalid operation mode!\r\n");
268  break;
269  }
270 
271  //Update link state
272  interface->linkState = TRUE;
273 
274  //Adjust MAC configuration parameters for proper operation
275  interface->nicDriver->updateMacConfig(interface);
276  }
277  else
278  {
279  //Update link state
280  interface->linkState = FALSE;
281  }
282 
283  //Process link state change event
284  nicNotifyLinkChange(interface);
285  }
286 }
287 
288 
289 /**
290  * @brief Write PHY register
291  * @param[in] interface Underlying network interface
292  * @param[in] address PHY register address
293  * @param[in] data Register value
294  **/
295 
296 void adin1300WritePhyReg(NetInterface *interface, uint8_t address,
297  uint16_t data)
298 {
299  //Write the specified PHY register
300  if(interface->smiDriver != NULL)
301  {
302  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
303  interface->phyAddr, address, data);
304  }
305  else
306  {
307  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
308  interface->phyAddr, address, data);
309  }
310 }
311 
312 
313 /**
314  * @brief Read PHY register
315  * @param[in] interface Underlying network interface
316  * @param[in] address PHY register address
317  * @return Register value
318  **/
319 
320 uint16_t adin1300ReadPhyReg(NetInterface *interface, uint8_t address)
321 {
322  uint16_t data;
323 
324  //Read the specified PHY register
325  if(interface->smiDriver != NULL)
326  {
327  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
328  interface->phyAddr, address);
329  }
330  else
331  {
332  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
333  interface->phyAddr, address);
334  }
335 
336  //Return the value of the PHY register
337  return data;
338 }
339 
340 
341 /**
342  * @brief Dump PHY registers for debugging purpose
343  * @param[in] interface Underlying network interface
344  **/
345 
347 {
348  uint8_t i;
349 
350  //Loop through PHY registers
351  for(i = 0; i < 32; i++)
352  {
353  //Display current PHY register
354  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
355  adin1300ReadPhyReg(interface, i));
356  }
357 
358  //Terminate with a line feed
359  TRACE_DEBUG("\r\n");
360 }
361 
362 
363 /**
364  * @brief Write extended register
365  * @param[in] interface Underlying network interface
366  * @param[in] address Extended register address
367  * @param[in] data Register value
368  **/
369 
370 void adin1300WriteExtReg(NetInterface *interface, uint16_t address,
371  uint16_t data)
372 {
373  //The EXT_REG_PTR and EXT_REG_DATA registers provide a mechanism to access
374  //the indirect access address map via directly accessible registers
376 
377  //Write the content of the extended register
379 }
380 
381 
382 /**
383  * @brief Read extended register
384  * @param[in] interface Underlying network interface
385  * @param[in] address Extended register address
386  * @return Register value
387  **/
388 
389 uint16_t adin1300ReadExtReg(NetInterface *interface, uint16_t address)
390 {
391  //The EXT_REG_PTR and EXT_REG_DATA registers provide a mechanism to access
392  //the indirect access address map via directly accessible registers
394 
395  //Read the content of the extended register
396  return adin1300ReadPhyReg(interface, ADIN1300_EXT_REG_DATA);
397 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
#define ADIN1300_IRQ_STATUS_LNK_STAT_CHNG_IRQ_STAT
#define ADIN1300_MII_CONTROL
uint16_t adin1300ReadExtReg(NetInterface *interface, uint16_t address)
Read extended register.
#define ADIN1300_IRQ_STATUS
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
void adin1300EnableIrq(NetInterface *interface)
Enable interrupts.
void adin1300WriteExtReg(NetInterface *interface, uint16_t address, uint16_t data)
Write extended register.
void adin1300WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define ADIN1300_MII_STATUS
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define ADIN1300_PHY_STATUS_1
#define ADIN1300_IRQ_MASK_LNK_STAT_CHNG_IRQ_EN
#define ADIN1300_IRQ_MASK_HW_IRQ_EN
ADIN1300 Gigabit Ethernet PHY driver.
void adin1300DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define ADIN1300_EXT_REG_DATA
#define ADIN1300_PHY_STATUS_1_HCD_TECH
#define ADIN1300_PHY_STATUS_1_LINK_STAT
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define ADIN1300_PHY_STATUS_1_HCD_TECH_1000BT_HD
__weak_func void adin1300InitHook(NetInterface *interface)
ADIN1300 custom configuration.
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
#define ADIN1300_PHY_STATUS_1_HCD_TECH_1000BT_FD
uint16_t adin1300ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define ADIN1300_PHY_STATUS_1_HCD_TECH_10BT_HD
#define ADIN1300_MII_CONTROL_SFT_RST
#define ADIN1300_IRQ_MASK
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
#define ADIN1300_PHY_STATUS_1_HCD_TECH_10BT_FD
void adin1300EventHandler(NetInterface *interface)
ADIN1300 event handler.
#define ADIN1300_MII_STATUS_LINK_STAT_LAT
#define ADIN1300_GE_IO_INT_N_OR_CNTRL
#define ADIN1300_PHY_STATUS_1_HCD_TECH_100BTX_HD
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
Ipv6Addr address[]
Definition: ipv6.h:325
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
#define ADIN1300_GE_IO_INT_N_OR_CNTRL_GE_IO_INT_N_OR_CNTRL_DEFAULT
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define ADIN1300_MII_CONTROL_SFT_PD
const PhyDriver adin1300PhyDriver
ADIN1300 Ethernet PHY driver.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
TCP/IP stack core.
void adin1300Tick(NetInterface *interface)
ADIN1300 timer handler.
#define ADIN1300_PHY_ADDR
void adin1300DisableIrq(NetInterface *interface)
Disable interrupts.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t adin1300Init(NetInterface *interface)
ADIN1300 PHY transceiver initialization.
#define ADIN1300_EXT_REG_PTR
#define ADIN1300_PHY_STATUS_1_HCD_TECH_100BTX_FD