ip101_driver.c
Go to the documentation of this file.
1 /**
2  * @file ip101_driver.c
3  * @brief IC+ IP101 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 IP101 Ethernet PHY driver
42  **/
43 
45 {
46  ip101Init,
47  ip101Tick,
51 };
52 
53 
54 /**
55  * @brief IP101 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing IP101...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = IP101_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
82  while(ip101ReadPhyReg(interface, IP101_BMCR) & IP101_BMCR_RESET)
83  {
84  }
85 
86  //Dump PHY registers for debugging purpose
87  ip101DumpPhyReg(interface);
88 
89  //Perform custom configuration
90  ip101InitHook(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 IP101 custom configuration
104  * @param[in] interface Underlying network interface
105  **/
106 
107 __weak_func void ip101InitHook(NetInterface *interface)
108 {
109 }
110 
111 
112 /**
113  * @brief IP101 timer handler
114  * @param[in] interface Underlying network interface
115  **/
116 
117 void ip101Tick(NetInterface *interface)
118 {
119  uint16_t value;
120  bool_t linkState;
121 
122  //Read basic status register
123  value = ip101ReadPhyReg(interface, IP101_PHYMCSSR);
124  //Retrieve current link state
125  linkState = (value & IP101_PHYMCSSR_LINK_UP) ? TRUE : FALSE;
126 
127  //Link up event?
128  if(linkState && !interface->linkState)
129  {
130  //Set event flag
131  interface->phyEvent = TRUE;
132  //Notify the TCP/IP stack of the event
134  }
135  //Link down event?
136  else if(!linkState && interface->linkState)
137  {
138  //Set event flag
139  interface->phyEvent = TRUE;
140  //Notify the TCP/IP stack of the event
142  }
143 }
144 
145 
146 /**
147  * @brief Enable interrupts
148  * @param[in] interface Underlying network interface
149  **/
150 
151 void ip101EnableIrq(NetInterface *interface)
152 {
153 }
154 
155 
156 /**
157  * @brief Disable interrupts
158  * @param[in] interface Underlying network interface
159  **/
160 
162 {
163 }
164 
165 
166 /**
167  * @brief IP101 event handler
168  * @param[in] interface Underlying network interface
169  **/
170 
172 {
173  uint16_t value;
174 
175  //Read PHY status register
176  value = ip101ReadPhyReg(interface, IP101_PHYMCSSR);
177 
178  //Link is up?
179  if((value & IP101_PHYMCSSR_LINK_UP) != 0)
180  {
181  //Check current operation mode
182  switch(value & IP101_PHYMCSSR_OP_MODE)
183  {
184  //10BASE-T half-duplex
186  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
187  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
188  break;
189 
190  //10BASE-T full-duplex
192  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
193  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
194  break;
195 
196  //100BASE-TX half-duplex
198  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
199  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
200  break;
201 
202  //100BASE-TX full-duplex
204  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
205  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
206  break;
207 
208  //Unknown operation mode
209  default:
210  //Debug message
211  TRACE_WARNING("Invalid operation mode!\r\n");
212  break;
213  }
214 
215  //Update link state
216  interface->linkState = TRUE;
217 
218  //Adjust MAC configuration parameters for proper operation
219  interface->nicDriver->updateMacConfig(interface);
220  }
221  else
222  {
223  //Update link state
224  interface->linkState = FALSE;
225  }
226 
227  //Process link state change event
228  nicNotifyLinkChange(interface);
229 }
230 
231 
232 /**
233  * @brief Write PHY register
234  * @param[in] interface Underlying network interface
235  * @param[in] address PHY register address
236  * @param[in] data Register value
237  **/
238 
239 void ip101WritePhyReg(NetInterface *interface, uint8_t address,
240  uint16_t data)
241 {
242  //Write the specified PHY register
243  if(interface->smiDriver != NULL)
244  {
245  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
246  interface->phyAddr, address, data);
247  }
248  else
249  {
250  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
251  interface->phyAddr, address, data);
252  }
253 }
254 
255 
256 /**
257  * @brief Read PHY register
258  * @param[in] interface Underlying network interface
259  * @param[in] address PHY register address
260  * @return Register value
261  **/
262 
263 uint16_t ip101ReadPhyReg(NetInterface *interface, uint8_t address)
264 {
265  uint16_t data;
266 
267  //Read the specified PHY register
268  if(interface->smiDriver != NULL)
269  {
270  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
271  interface->phyAddr, address);
272  }
273  else
274  {
275  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
276  interface->phyAddr, address);
277  }
278 
279  //Return the value of the PHY register
280  return data;
281 }
282 
283 
284 /**
285  * @brief Dump PHY registers for debugging purpose
286  * @param[in] interface Underlying network interface
287  **/
288 
290 {
291  uint8_t i;
292 
293  //Loop through PHY registers
294  for(i = 0; i < 32; i++)
295  {
296  //Display current PHY register
297  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
298  ip101ReadPhyReg(interface, i));
299  }
300 
301  //Terminate with a line feed
302  TRACE_DEBUG("\r\n");
303 }
304 
305 
306 /**
307  * @brief Write MMD register
308  * @param[in] interface Underlying network interface
309  * @param[in] devAddr Device address
310  * @param[in] regAddr Register address
311  * @param[in] data MMD register value
312  **/
313 
314 void ip101WriteMmdReg(NetInterface *interface, uint8_t devAddr,
315  uint16_t regAddr, uint16_t data)
316 {
317  //Select register operation
318  ip101WritePhyReg(interface, IP101_MMDACR,
320 
321  //Write MMD register address
323 
324  //Select data operation
325  ip101WritePhyReg(interface, IP101_MMDACR,
327 
328  //Write the content of the MMD register
329  ip101WritePhyReg(interface, IP101_MMDAADR, data);
330 }
331 
332 
333 /**
334  * @brief Read MMD register
335  * @param[in] interface Underlying network interface
336  * @param[in] devAddr Device address
337  * @param[in] regAddr Register address
338  * @return MMD register value
339  **/
340 
341 uint16_t ip101ReadMmdReg(NetInterface *interface, uint8_t devAddr,
342  uint16_t regAddr)
343 {
344  //Select register operation
345  ip101WritePhyReg(interface, IP101_MMDACR,
347 
348  //Write MMD register address
350 
351  //Select data operation
352  ip101WritePhyReg(interface, IP101_MMDACR,
354 
355  //Read the content of the MMD register
356  return ip101ReadPhyReg(interface, IP101_MMDAADR);
357 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define IP101_PHYMCSSR_OP_MODE
Definition: ip101_driver.h:199
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define IP101_PHYMCSSR_OP_MODE_10M_HD
Definition: ip101_driver.h:201
#define IP101_MMDACR_FUNC_ADDR
Definition: ip101_driver.h:153
#define IP101_MMDACR
Definition: ip101_driver.h:54
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define IP101_PHYMCSSR
Definition: ip101_driver.h:60
#define FALSE
Definition: os_port.h:46
#define IP101_MMDACR_DEVAD
Definition: ip101_driver.h:157
error_t
Error codes.
Definition: error.h:43
#define IP101_MMDAADR
Definition: ip101_driver.h:55
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
const PhyDriver ip101PhyDriver
IP101 Ethernet PHY driver.
Definition: ip101_driver.c:44
void ip101DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
Definition: ip101_driver.c:289
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
void ip101EventHandler(NetInterface *interface)
IP101 event handler.
Definition: ip101_driver.c:171
uint16_t ip101ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
Definition: ip101_driver.c:263
#define IP101_PHY_ADDR
Definition: ip101_driver.h:39
#define IP101_PHYMCSSR_LINK_UP
Definition: ip101_driver.h:197
#define IP101_PHYMCSSR_OP_MODE_10M_FD
Definition: ip101_driver.h:203
void ip101EnableIrq(NetInterface *interface)
Enable interrupts.
Definition: ip101_driver.c:151
__weak_func void ip101InitHook(NetInterface *interface)
IP101 custom configuration.
Definition: ip101_driver.c:107
void ip101Tick(NetInterface *interface)
IP101 timer handler.
Definition: ip101_driver.c:117
#define IP101_PHYMCSSR_OP_MODE_100M_FD
Definition: ip101_driver.h:204
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define IP101_PHYMCSSR_OP_MODE_100M_HD
Definition: ip101_driver.h:202
uint16_t regAddr
uint16_t ip101ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
Definition: ip101_driver.c:341
Ipv6Addr address[]
Definition: ipv6.h:325
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
error_t ip101Init(NetInterface *interface)
IP101 PHY transceiver initialization.
Definition: ip101_driver.c:60
void ip101DisableIrq(NetInterface *interface)
Disable interrupts.
Definition: ip101_driver.c:161
uint8_t value[]
Definition: tcp.h:369
IC+ IP101 Ethernet PHY driver.
#define IP101_BMCR
Definition: ip101_driver.h:45
void ip101WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
Definition: ip101_driver.c:239
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
void ip101WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
Definition: ip101_driver.c:314
TCP/IP stack core.
#define IP101_MMDACR_FUNC_DATA_NO_POST_INC
Definition: ip101_driver.h:154
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IP101_BMCR_RESET
Definition: ip101_driver.h:71