mv88e1112_driver.c
Go to the documentation of this file.
1 /**
2  * @file mv88e1112_driver.c
3  * @brief 88E1112 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 88E1112 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief 88E1112 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing 88E1112...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = MV88E1112_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
81 
82  //Wait for the reset to complete
83  while(mv88e1112ReadPhyReg(interface, MV88E1112_COPPER_CTRL) &
85  {
86  }
87 
88  //Dump PHY registers for debugging purpose
89  mv88e1112DumpPhyReg(interface);
90 
91  //Perform custom configuration
92  mv88e1112InitHook(interface);
93 
94  //Force the TCP/IP stack to poll the link state at startup
95  interface->phyEvent = TRUE;
96  //Notify the TCP/IP stack of the event
98 
99  //Successful initialization
100  return NO_ERROR;
101 }
102 
103 
104 /**
105  * @brief 88E1112 custom configuration
106  * @param[in] interface Underlying network interface
107  **/
108 
109 __weak_func void mv88e1112InitHook(NetInterface *interface)
110 {
111 }
112 
113 
114 /**
115  * @brief 88E1112 timer handler
116  * @param[in] interface Underlying network interface
117  **/
118 
119 void mv88e1112Tick(NetInterface *interface)
120 {
121  uint16_t value;
122  bool_t linkState;
123 
124  //Read copper status register
126  //Retrieve current link state
128 
129  //Link up event?
130  if(linkState && !interface->linkState)
131  {
132  //Set event flag
133  interface->phyEvent = TRUE;
134  //Notify the TCP/IP stack of the event
136  }
137  //Link down event?
138  else if(!linkState && interface->linkState)
139  {
140  //Set event flag
141  interface->phyEvent = TRUE;
142  //Notify the TCP/IP stack of the event
144  }
145 }
146 
147 
148 /**
149  * @brief Enable interrupts
150  * @param[in] interface Underlying network interface
151  **/
152 
154 {
155 }
156 
157 
158 /**
159  * @brief Disable interrupts
160  * @param[in] interface Underlying network interface
161  **/
162 
164 {
165 }
166 
167 
168 /**
169  * @brief 88E1112 event handler
170  * @param[in] interface Underlying network interface
171  **/
172 
174 {
175  uint16_t status;
176 
177  //Read copper specific status 1 register
178  status = mv88e1112ReadPhyReg(interface, MV88E1112_COPPER_STAT1);
179 
180  //Link is up?
181  if((status & MV88E1112_COPPER_STAT1_LINK) != 0)
182  {
183  //Check current speed
184  switch(status & MV88E1112_COPPER_STAT1_SPEED)
185  {
186  //10BASE-T
188  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
189  break;
190  //100BASE-TX
192  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
193  break;
194  //1000BASE-T
196  interface->linkSpeed = NIC_LINK_SPEED_1GBPS;
197  break;
198  //Unknown speed
199  default:
200  //Debug message
201  TRACE_WARNING("Invalid speed\r\n");
202  break;
203  }
204 
205  //Check current duplex mode
206  if((status & MV88E1112_COPPER_STAT1_DUPLEX) != 0)
207  {
208  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
209  }
210  else
211  {
212  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
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 mv88e1112WritePhyReg(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 mv88e1112ReadPhyReg(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  mv88e1112ReadPhyReg(interface, i));
299  }
300 
301  //Terminate with a line feed
302  TRACE_DEBUG("\r\n");
303 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
@ NIC_LINK_SPEED_1GBPS
Definition: nic.h:113
const PhyDriver mv88e1112PhyDriver
88E1112 Ethernet PHY driver
#define MV88E1112_COPPER_CTRL_RESET
int bool_t
Definition: compiler_port.h:53
void mv88e1112WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define netEvent
Definition: net_legacy.h:196
__weak_func void mv88e1112InitHook(NetInterface *interface)
88E1112 custom configuration
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define MV88E1112_COPPER_STAT1_LINK
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
error_t mv88e1112Init(NetInterface *interface)
88E1112 PHY transceiver initialization
void mv88e1112Tick(NetInterface *interface)
88E1112 timer handler
uint16_t mv88e1112ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
88E1112 Gigabit Ethernet PHY driver
void mv88e1112DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define FALSE
Definition: os_port.h:46
#define MV88E1112_COPPER_STAT1_SPEED_100MBPS
error_t
Error codes.
Definition: error.h:43
#define MV88E1112_COPPER_STAT1_SPEED_10MBPS
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:111
#define MV88E1112_COPPER_STAT1_SPEED_1000MBPS
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
#define MV88E1112_COPPER_CTRL
#define MV88E1112_COPPER_STAT1_SPEED
#define MV88E1112_COPPER_STAT_LINK_STATUS
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
void mv88e1112EnableIrq(NetInterface *interface)
Enable interrupts.
#define MV88E1112_PHY_ADDR
void mv88e1112DisableIrq(NetInterface *interface)
Disable interrupts.
#define MV88E1112_COPPER_STAT1_DUPLEX
Ipv6Addr address[]
Definition: ipv6.h:325
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:124
uint8_t value[]
Definition: tcp.h:369
#define MV88E1112_COPPER_STAT
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 MV88E1112_COPPER_STAT1
void mv88e1112EventHandler(NetInterface *interface)
88E1112 event handler
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.