adin1100_driver.c
Go to the documentation of this file.
1 /**
2  * @file adin1100_driver.c
3  * @brief ADIN1100 10Base-T1L 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 ADIN1100 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief ADIN1100 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 ADIN1100...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = ADIN1100_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(adin1100ReadPhyReg(interface, ADIN1100_MI_CONTROL) &
93  {
94  }
95 
96  //Dump PHY registers for debugging purpose
97  adin1100DumpPhyReg(interface);
98 
99  //Enable LED1 output
104 
105  //Configure LED0 and LED1 function
111 
112  //Set LED0 and LED1 polarity
116 
117  //Clear the CRSM_SFT_PD bit to exit software power-down mode. At this point,
118  //the MAC-PHY starts autonegotiation and attempts to bring up a link after
119  //autonegotiation completes
123 
124  //Perform custom configuration
125  adin1100InitHook(interface);
126 
127  //Force the TCP/IP stack to poll the link state at startup
128  interface->phyEvent = TRUE;
129  //Notify the TCP/IP stack of the event
131 
132  //Successful initialization
133  return NO_ERROR;
134 }
135 
136 
137 /**
138  * @brief ADIN1100 custom configuration
139  * @param[in] interface Underlying network interface
140  **/
141 
142 __weak_func void adin1100InitHook(NetInterface *interface)
143 {
144 }
145 
146 
147 /**
148  * @brief ADIN1100 timer handler
149  * @param[in] interface Underlying network interface
150  **/
151 
152 void adin1100Tick(NetInterface *interface)
153 {
154  uint16_t value;
155  bool_t linkState;
156 
157  //No external interrupt line driver?
158  if(interface->extIntDriver == NULL)
159  {
160  //Read PHY status register
162  //Retrieve current link state
164 
165  //Link up event?
166  if(linkState && !interface->linkState)
167  {
168  //Set event flag
169  interface->phyEvent = TRUE;
170  //Notify the TCP/IP stack of the event
172  }
173  //Link down event?
174  else if(!linkState && interface->linkState)
175  {
176  //Set event flag
177  interface->phyEvent = TRUE;
178  //Notify the TCP/IP stack of the event
180  }
181  }
182 }
183 
184 
185 /**
186  * @brief Enable interrupts
187  * @param[in] interface Underlying network interface
188  **/
189 
191 {
192  //Enable PHY transceiver interrupts
193  if(interface->extIntDriver != NULL)
194  {
195  interface->extIntDriver->enableIrq();
196  }
197 }
198 
199 
200 /**
201  * @brief Disable interrupts
202  * @param[in] interface Underlying network interface
203  **/
204 
206 {
207  //Disable PHY transceiver interrupts
208  if(interface->extIntDriver != NULL)
209  {
210  interface->extIntDriver->disableIrq();
211  }
212 }
213 
214 
215 /**
216  * @brief ADIN1100 event handler
217  * @param[in] interface Underlying network interface
218  **/
219 
221 {
222  uint16_t value;
223 
224  //Read PHY status register
226 
227  //Link is up?
229  {
230  //The PHY is only able to operate in 10 Mbps mode
231  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
232  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
233 
234  //Adjust MAC configuration parameters for proper operation
235  interface->nicDriver->updateMacConfig(interface);
236 
237  //Update link state
238  interface->linkState = TRUE;
239  }
240  else
241  {
242  //Update link state
243  interface->linkState = FALSE;
244  }
245 
246  //Process link state change event
247  nicNotifyLinkChange(interface);
248 }
249 
250 
251 /**
252  * @brief Write PHY register
253  * @param[in] interface Underlying network interface
254  * @param[in] address PHY register address
255  * @param[in] data Register value
256  **/
257 
258 void adin1100WritePhyReg(NetInterface *interface, uint8_t address,
259  uint16_t data)
260 {
261  //Write the specified PHY register
262  if(interface->smiDriver != NULL)
263  {
264  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
265  interface->phyAddr, address, data);
266  }
267  else
268  {
269  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
270  interface->phyAddr, address, data);
271  }
272 }
273 
274 
275 /**
276  * @brief Read PHY register
277  * @param[in] interface Underlying network interface
278  * @param[in] address PHY register address
279  * @return Register value
280  **/
281 
282 uint16_t adin1100ReadPhyReg(NetInterface *interface, uint8_t address)
283 {
284  uint16_t data;
285 
286  //Read the specified PHY register
287  if(interface->smiDriver != NULL)
288  {
289  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
290  interface->phyAddr, address);
291  }
292  else
293  {
294  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
295  interface->phyAddr, address);
296  }
297 
298  //Return the value of the PHY register
299  return data;
300 }
301 
302 
303 /**
304  * @brief Dump PHY registers for debugging purpose
305  * @param[in] interface Underlying network interface
306  **/
307 
309 {
310  uint8_t i;
311 
312  //Loop through PHY registers
313  for(i = 0; i < 32; i++)
314  {
315  //Display current PHY register
316  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
317  adin1100ReadPhyReg(interface, i));
318  }
319 
320  //Terminate with a line feed
321  TRACE_DEBUG("\r\n");
322 }
323 
324 
325 /**
326  * @brief Write MMD register
327  * @param[in] interface Underlying network interface
328  * @param[in] devAddr Device address
329  * @param[in] regAddr Register address
330  * @param[in] data MMD register value
331  **/
332 
333 void adin1100WriteMmdReg(NetInterface *interface, uint8_t devAddr,
334  uint16_t regAddr, uint16_t data)
335 {
336  //Select register operation
340 
341  //Write MMD register address
343 
344  //Select data operation
348 
349  //Write the content of the MMD register
351 }
352 
353 
354 /**
355  * @brief Read MMD register
356  * @param[in] interface Underlying network interface
357  * @param[in] devAddr Device address
358  * @param[in] regAddr Register address
359  * @return MMD register value
360  **/
361 
362 uint16_t adin1100ReadMmdReg(NetInterface *interface, uint8_t devAddr,
363  uint16_t regAddr)
364 {
365  //Select register operation
369 
370  //Write MMD register address
372 
373  //Select data operation
377 
378  //Read the content of the MMD register
379  return adin1100ReadPhyReg(interface, ADIN1100_MMD_ACCESS);
380 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
#define ADIN1100_CRSM_SFT_PD_CNTRL_CRSM_SFT_PD
#define ADIN1100_DIGIO_PINMUX
int bool_t
Definition: compiler_port.h:53
#define ADIN1100_LED_CNTRL_LED0_EN
#define netEvent
Definition: net_legacy.h:196
#define ADIN1100_LED_CNTRL
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
ADIN1100 10Base-T1L Ethernet PHY driver.
void adin1100WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define ADIN1100_PHY_ADDR
void adin1100DisableIrq(NetInterface *interface)
Disable interrupts.
#define ADIN1100_MI_STATUS
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
void adin1100Tick(NetInterface *interface)
ADIN1100 timer handler.
#define ADIN1100_MMD_ACCESS
uint16_t adin1100ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define ADIN1100_LED_CNTRL_LED1_EN
#define SMI_OPCODE_WRITE
Definition: nic.h:66
const PhyDriver adin1100PhyDriver
ADIN1100 Ethernet PHY driver.
void adin1100EnableIrq(NetInterface *interface)
Enable interrupts.
#define ADIN1100_MI_CONTROL_MI_SFT_RST
#define ADIN1100_DIGIO_PINMUX_DIGIO_LED1_PINMUX_LED_1
#define FALSE
Definition: os_port.h:46
#define ADIN1100_LED_CNTRL_LED1_FUNCTION_MASTER
error_t
Error codes.
Definition: error.h:43
error_t adin1100Init(NetInterface *interface)
ADIN1100 PHY transceiver initialization.
#define ADIN1100_MI_STATUS_MI_LINK_STAT_LAT
#define NetInterface
Definition: net.h:36
#define ADIN1100_LED_POLARITY_LED0_POLARITY_AUTOSENSE
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
#define ADIN1100_CRSM_SFT_PD_CNTRL
uint16_t adin1100ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
#define TRACE_DEBUG(...)
Definition: debug.h:107
void adin1100EventHandler(NetInterface *interface)
ADIN1100 event handler.
void adin1100WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
#define ADIN1100_LED_POLARITY
uint16_t regAddr
#define ADIN1100_MI_CONTROL
void adin1100DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define ADIN1100_MMD_ACCESS_CNTRL_MMD_ACR_FUNCTION_ADDR
#define ADIN1100_MMD_ACCESS_CNTRL
Ipv6Addr address[]
Definition: ipv6.h:325
__weak_func void adin1100InitHook(NetInterface *interface)
ADIN1100 custom configuration.
#define ADIN1100_DIGIO_PINMUX_DIGIO_LED1_PINMUX
#define ADIN1100_LED_CNTRL_LED0_FUNCTION_LINKUP_TXRX_ACTIVITY
#define ADIN1100_MMD_ACCESS_CNTRL_MMD_ACR_DEVAD
uint8_t value[]
Definition: tcp.h:369
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
TCP/IP stack core.
#define ADIN1100_LED_POLARITY_LED1_POLARITY_AUTOSENSE
#define ADIN1100_MMD_ACCESS_CNTRL_MMD_ACR_FUNCTION_DATA_NO_POST_INC
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.