dp83848_driver.c
Go to the documentation of this file.
1 /**
2  * @file dp83848_driver.c
3  * @brief DP83848 Ethernet PHY driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2020 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 1.9.8
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 DP83848 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief DP83848 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  //Debug message
63  TRACE_INFO("Initializing DP83848...\r\n");
64 
65  //Undefined PHY address?
66  if(interface->phyAddr >= 32)
67  {
68  //Use the default address
69  interface->phyAddr = DP83848_PHY_ADDR;
70  }
71 
72  //Initialize serial management interface
73  if(interface->smiDriver != NULL)
74  {
75  interface->smiDriver->init();
76  }
77 
78  //Initialize external interrupt line driver
79  if(interface->extIntDriver != NULL)
80  {
81  interface->extIntDriver->init();
82  }
83 
84  //Reset PHY transceiver
86 
87  //Wait for the reset to complete
89  {
90  }
91 
92  //Dump PHY registers for debugging purpose
93  dp83848DumpPhyReg(interface);
94 
95  //Configure PWR_DOWN/INT pin as an interrupt output
98 
99  //The PHY will generate interrupts when link status changes are detected
101 
102  //Force the TCP/IP stack to poll the link state at startup
103  interface->phyEvent = TRUE;
104  //Notify the TCP/IP stack of the event
106 
107  //Successful initialization
108  return NO_ERROR;
109 }
110 
111 
112 /**
113  * @brief DP83848 timer handler
114  * @param[in] interface Underlying network interface
115  **/
116 
117 void dp83848Tick(NetInterface *interface)
118 {
119  uint16_t value;
120  bool_t linkState;
121 
122  //No external interrupt line driver?
123  if(interface->extIntDriver == NULL)
124  {
125  //Read basic status register
126  value = dp83848ReadPhyReg(interface, DP83848_BMSR);
127  //Retrieve current link state
128  linkState = (value & DP83848_BMSR_LINK_STATUS) ? TRUE : FALSE;
129 
130  //Link up event?
131  if(linkState && !interface->linkState)
132  {
133  //Set event flag
134  interface->phyEvent = TRUE;
135  //Notify the TCP/IP stack of the event
137  }
138  //Link down event?
139  else if(!linkState && interface->linkState)
140  {
141  //Set event flag
142  interface->phyEvent = TRUE;
143  //Notify the TCP/IP stack of the event
145  }
146  }
147 }
148 
149 
150 /**
151  * @brief Enable interrupts
152  * @param[in] interface Underlying network interface
153  **/
154 
156 {
157  //Enable PHY transceiver interrupts
158  if(interface->extIntDriver != NULL)
159  {
160  interface->extIntDriver->enableIrq();
161  }
162 }
163 
164 
165 /**
166  * @brief Disable interrupts
167  * @param[in] interface Underlying network interface
168  **/
169 
171 {
172  //Disable PHY transceiver interrupts
173  if(interface->extIntDriver != NULL)
174  {
175  interface->extIntDriver->disableIrq();
176  }
177 }
178 
179 
180 /**
181  * @brief DP83848 event handler
182  * @param[in] interface Underlying network interface
183  **/
184 
186 {
187  uint16_t status;
188 
189  //Read status register to acknowledge the interrupt
190  status = dp83848ReadPhyReg(interface, DP83848_MISR);
191 
192  //Link status change?
193  if((status & DP83848_MISR_LINK_INT) != 0)
194  {
195  //Read PHY status register
196  status = dp83848ReadPhyReg(interface, DP83848_PHYSTS);
197 
198  //Link is up?
199  if((status & DP83848_PHYSTS_LINK_STATUS) != 0)
200  {
201  //Check current speed
202  if((status & DP83848_PHYSTS_SPEED_STATUS) != 0)
203  {
204  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
205  }
206  else
207  {
208  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
209  }
210 
211  //Check duplex mode
212  if((status & DP83848_PHYSTS_DUPLEX_STATUS) != 0)
213  {
214  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
215  }
216  else
217  {
218  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
219  }
220 
221  //Update link state
222  interface->linkState = TRUE;
223 
224  //Adjust MAC configuration parameters for proper operation
225  interface->nicDriver->updateMacConfig(interface);
226  }
227  else
228  {
229  //Update link state
230  interface->linkState = FALSE;
231  }
232 
233  //Process link state change event
234  nicNotifyLinkChange(interface);
235  }
236 }
237 
238 
239 /**
240  * @brief Write PHY register
241  * @param[in] interface Underlying network interface
242  * @param[in] address PHY register address
243  * @param[in] data Register value
244  **/
245 
246 void dp83848WritePhyReg(NetInterface *interface, uint8_t address,
247  uint16_t data)
248 {
249  //Write the specified PHY register
250  if(interface->smiDriver != NULL)
251  {
252  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
253  interface->phyAddr, address, data);
254  }
255  else
256  {
257  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
258  interface->phyAddr, address, data);
259  }
260 }
261 
262 
263 /**
264  * @brief Read PHY register
265  * @param[in] interface Underlying network interface
266  * @param[in] address PHY register address
267  * @return Register value
268  **/
269 
270 uint16_t dp83848ReadPhyReg(NetInterface *interface, uint8_t address)
271 {
272  uint16_t data;
273 
274  //Read the specified PHY register
275  if(interface->smiDriver != NULL)
276  {
277  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
278  interface->phyAddr, address);
279  }
280  else
281  {
282  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
283  interface->phyAddr, address);
284  }
285 
286  //Return the value of the PHY register
287  return data;
288 }
289 
290 
291 /**
292  * @brief Dump PHY registers for debugging purpose
293  * @param[in] interface Underlying network interface
294  **/
295 
297 {
298  uint8_t i;
299 
300  //Loop through PHY registers
301  for(i = 0; i < 32; i++)
302  {
303  //Display current PHY register
304  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
305  dp83848ReadPhyReg(interface, i));
306  }
307 
308  //Terminate with a line feed
309  TRACE_DEBUG("\r\n");
310 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:532
#define DP83848_BMCR_RESET
int bool_t
Definition: compiler_port.h:49
#define netEvent
Definition: net_legacy.h:267
error_t dp83848Init(NetInterface *interface)
DP83848 PHY transceiver initialization.
void dp83848Tick(NetInterface *interface)
DP83848 timer handler.
uint8_t data[]
Definition: ethernet.h:209
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:292
#define DP83848_PHYSTS
#define DP83848_PHYSTS_LINK_STATUS
#define SMI_OPCODE_WRITE
Definition: nic.h:65
#define DP83848_MICR
#define DP83848_PHYSTS_DUPLEX_STATUS
#define FALSE
Definition: os_port.h:46
void dp83848DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define DP83848_BMSR
error_t
Error codes.
Definition: error.h:42
#define DP83848_MICR_INTEN
void dp83848EventHandler(NetInterface *interface)
DP83848 event handler.
uint8_t value[]
Definition: tcp.h:332
#define NetInterface
Definition: net.h:36
#define SMI_OPCODE_READ
Definition: nic.h:66
#define TRACE_INFO(...)
Definition: debug.h:95
#define DP83848_MISR_LINK_INT_EN
#define DP83848_MISR
DP83848 Ethernet PHY driver.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define DP83848_MICR_INT_OE
const PhyDriver dp83848PhyDriver
DP83848 Ethernet PHY driver.
#define DP83848_BMCR
#define DP83848_PHY_ADDR
void dp83848WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
uint16_t dp83848ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void dp83848EnableIrq(NetInterface *interface)
Enable interrupts.
#define DP83848_PHYSTS_SPEED_STATUS
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define DP83848_MISR_LINK_INT
TCP/IP stack core.
void dp83848DisableIrq(NetInterface *interface)
Disable interrupts.
#define DP83848_BMSR_LINK_STATUS
Success.
Definition: error.h:44
Debugging facilities.