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-2021 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.0.2
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  //Force the TCP/IP stack to poll the link state at startup
90  interface->phyEvent = TRUE;
91  //Notify the TCP/IP stack of the event
93 
94  //Successful initialization
95  return NO_ERROR;
96 }
97 
98 
99 /**
100  * @brief IP101 timer handler
101  * @param[in] interface Underlying network interface
102  **/
103 
104 void ip101Tick(NetInterface *interface)
105 {
106  uint16_t value;
107  bool_t linkState;
108 
109  //Read basic status register
110  value = ip101ReadPhyReg(interface, IP101_PHYMCSSR);
111  //Retrieve current link state
112  linkState = (value & IP101_PHYMCSSR_LINK_UP) ? TRUE : FALSE;
113 
114  //Link up event?
115  if(linkState && !interface->linkState)
116  {
117  //Set event flag
118  interface->phyEvent = TRUE;
119  //Notify the TCP/IP stack of the event
121  }
122  //Link down event?
123  else if(!linkState && interface->linkState)
124  {
125  //Set event flag
126  interface->phyEvent = TRUE;
127  //Notify the TCP/IP stack of the event
129  }
130 }
131 
132 
133 /**
134  * @brief Enable interrupts
135  * @param[in] interface Underlying network interface
136  **/
137 
138 void ip101EnableIrq(NetInterface *interface)
139 {
140 }
141 
142 
143 /**
144  * @brief Disable interrupts
145  * @param[in] interface Underlying network interface
146  **/
147 
149 {
150 }
151 
152 
153 /**
154  * @brief IP101 event handler
155  * @param[in] interface Underlying network interface
156  **/
157 
159 {
160  uint16_t value;
161 
162  //Read PHY status register
163  value = ip101ReadPhyReg(interface, IP101_PHYMCSSR);
164 
165  //Link is up?
166  if((value & IP101_PHYMCSSR_LINK_UP) != 0)
167  {
168  //Check current operation mode
169  switch(value & IP101_PHYMCSSR_OP_MODE)
170  {
171  //10BASE-T half-duplex
173  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
174  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
175  break;
176  //10BASE-T full-duplex
178  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
179  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
180  break;
181  //100BASE-TX half-duplex
183  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
184  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
185  break;
186  //100BASE-TX full-duplex
188  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
189  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
190  break;
191  //Unknown operation mode
192  default:
193  //Debug message
194  TRACE_WARNING("Invalid operation mode!\r\n");
195  break;
196  }
197 
198  //Update link state
199  interface->linkState = TRUE;
200 
201  //Adjust MAC configuration parameters for proper operation
202  interface->nicDriver->updateMacConfig(interface);
203  }
204  else
205  {
206  //Update link state
207  interface->linkState = FALSE;
208  }
209 
210  //Process link state change event
211  nicNotifyLinkChange(interface);
212 }
213 
214 
215 /**
216  * @brief Write PHY register
217  * @param[in] interface Underlying network interface
218  * @param[in] address PHY register address
219  * @param[in] data Register value
220  **/
221 
222 void ip101WritePhyReg(NetInterface *interface, uint8_t address,
223  uint16_t data)
224 {
225  //Write the specified PHY register
226  if(interface->smiDriver != NULL)
227  {
228  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
229  interface->phyAddr, address, data);
230  }
231  else
232  {
233  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
234  interface->phyAddr, address, data);
235  }
236 }
237 
238 
239 /**
240  * @brief Read PHY register
241  * @param[in] interface Underlying network interface
242  * @param[in] address PHY register address
243  * @return Register value
244  **/
245 
246 uint16_t ip101ReadPhyReg(NetInterface *interface, uint8_t address)
247 {
248  uint16_t data;
249 
250  //Read the specified PHY register
251  if(interface->smiDriver != NULL)
252  {
253  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
254  interface->phyAddr, address);
255  }
256  else
257  {
258  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
259  interface->phyAddr, address);
260  }
261 
262  //Return the value of the PHY register
263  return data;
264 }
265 
266 
267 /**
268  * @brief Dump PHY registers for debugging purpose
269  * @param[in] interface Underlying network interface
270  **/
271 
273 {
274  uint8_t i;
275 
276  //Loop through PHY registers
277  for(i = 0; i < 32; i++)
278  {
279  //Display current PHY register
280  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
281  ip101ReadPhyReg(interface, i));
282  }
283 
284  //Terminate with a line feed
285  TRACE_DEBUG("\r\n");
286 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:538
int bool_t
Definition: compiler_port.h:49
#define netEvent
Definition: net_legacy.h:267
uint8_t data[]
Definition: ethernet.h:210
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:122
#define IP101_PHYMCSSR_OP_MODE
Definition: ip101_driver.h:191
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:293
#define IP101_PHYMCSSR_OP_MODE_10M_HD
Definition: ip101_driver.h:193
#define SMI_OPCODE_WRITE
Definition: nic.h:65
#define IP101_PHYMCSSR
Definition: ip101_driver.h:60
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
uint8_t value[]
Definition: tcp.h:339
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:108
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:272
#define SMI_OPCODE_READ
Definition: nic.h:66
#define TRACE_INFO(...)
Definition: debug.h:95
void ip101EventHandler(NetInterface *interface)
IP101 event handler.
Definition: ip101_driver.c:158
uint16_t ip101ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
Definition: ip101_driver.c:246
#define IP101_PHY_ADDR
Definition: ip101_driver.h:39
#define IP101_PHYMCSSR_LINK_UP
Definition: ip101_driver.h:189
#define IP101_PHYMCSSR_OP_MODE_10M_FD
Definition: ip101_driver.h:195
void ip101EnableIrq(NetInterface *interface)
Enable interrupts.
Definition: ip101_driver.c:138
void ip101Tick(NetInterface *interface)
IP101 timer handler.
Definition: ip101_driver.c:104
#define IP101_PHYMCSSR_OP_MODE_100M_FD
Definition: ip101_driver.h:196
#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:194
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:121
error_t ip101Init(NetInterface *interface)
IP101 PHY transceiver initialization.
Definition: ip101_driver.c:60
void ip101DisableIrq(NetInterface *interface)
Disable interrupts.
Definition: ip101_driver.c:148
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:222
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:109
TCP/IP stack core.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define IP101_BMCR_RESET
Definition: ip101_driver.h:63