dp83822_driver.c
Go to the documentation of this file.
1 /**
2  * @file dp83822_driver.c
3  * @brief DP83822 Ethernet PHY transceiver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2019 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.6
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 DP83822 Ethernet PHY driver
42  **/
43 
45 {
51  NULL,
52  NULL
53 };
54 
55 
56 /**
57  * @brief DP83822 PHY transceiver initialization
58  * @param[in] interface Underlying network interface
59  * @return Error code
60  **/
61 
63 {
64  //Debug message
65  TRACE_INFO("Initializing DP83822...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = DP83822_PHY_ADDR;
72  }
73 
74  //Initialize external interrupt line driver
75  if(interface->extIntDriver != NULL)
76  {
77  interface->extIntDriver->init();
78  }
79 
80  //Reset PHY transceiver
82 
83  //Wait for the reset to complete
85  {
86  }
87 
88  //Dump PHY registers for debugging purpose
89  dp83822DumpPhyReg(interface);
90 
91  //Configure PWR_DOWN/INT pin as an interrupt output
94 
95  //The PHY will generate interrupts when link status changes are detected
97 
98  //Force the TCP/IP stack to poll the link state at startup
99  interface->phyEvent = TRUE;
100  //Notify the TCP/IP stack of the event
102 
103  //Successful initialization
104  return NO_ERROR;
105 }
106 
107 
108 /**
109  * @brief DP83822 timer handler
110  * @param[in] interface Underlying network interface
111  **/
112 
113 void dp83822Tick(NetInterface *interface)
114 {
115  uint16_t value;
116  bool_t linkState;
117 
118  //No external interrupt line driver?
119  if(interface->extIntDriver == NULL)
120  {
121  //Read basic status register
122  value = dp83822ReadPhyReg(interface, DP83822_BMSR);
123  //Retrieve current link state
124  linkState = (value & DP83822_BMSR_LINK_STATUS) ? TRUE : FALSE;
125 
126  //Link up event?
127  if(linkState && !interface->linkState)
128  {
129  //Set event flag
130  interface->phyEvent = TRUE;
131  //Notify the TCP/IP stack of the event
133  }
134  //Link down event?
135  else if(!linkState && interface->linkState)
136  {
137  //Set event flag
138  interface->phyEvent = TRUE;
139  //Notify the TCP/IP stack of the event
141  }
142  }
143 }
144 
145 
146 /**
147  * @brief Enable interrupts
148  * @param[in] interface Underlying network interface
149  **/
150 
152 {
153  //Enable PHY transceiver interrupts
154  if(interface->extIntDriver != NULL)
155  {
156  interface->extIntDriver->enableIrq();
157  }
158 }
159 
160 
161 /**
162  * @brief Disable interrupts
163  * @param[in] interface Underlying network interface
164  **/
165 
167 {
168  //Disable PHY transceiver interrupts
169  if(interface->extIntDriver != NULL)
170  {
171  interface->extIntDriver->disableIrq();
172  }
173 }
174 
175 
176 /**
177  * @brief DP83822 event handler
178  * @param[in] interface Underlying network interface
179  **/
180 
182 {
183  uint16_t status;
184 
185  //Read status register to acknowledge the interrupt
186  status = dp83822ReadPhyReg(interface, DP83822_MISR1);
187 
188  //Link status change?
189  if(status & DP83822_MISR1_LINK_INT)
190  {
191  //Read PHY status register
192  status = dp83822ReadPhyReg(interface, DP83822_PHYSTS);
193 
194  //Link is up?
195  if(status & DP83822_PHYSTS_LINK_STATUS)
196  {
197  //Check current speed
198  if(status & DP83822_PHYSTS_SPEED_STATUS)
199  interface->linkSpeed = NIC_LINK_SPEED_10MBPS;
200  else
201  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
202 
203  //Check duplex mode
204  if(status & DP83822_PHYSTS_DUPLEX_STATUS)
205  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
206  else
207  interface->duplexMode = NIC_HALF_DUPLEX_MODE;
208 
209  //Update link state
210  interface->linkState = TRUE;
211 
212  //Adjust MAC configuration parameters for proper operation
213  interface->nicDriver->updateMacConfig(interface);
214  }
215  else
216  {
217  //Update link state
218  interface->linkState = FALSE;
219  }
220 
221  //Process link state change event
222  nicNotifyLinkChange(interface);
223  }
224 }
225 
226 
227 /**
228  * @brief Write PHY register
229  * @param[in] interface Underlying network interface
230  * @param[in] address PHY register address
231  * @param[in] data Register value
232  **/
233 
234 void dp83822WritePhyReg(NetInterface *interface, uint8_t address,
235  uint16_t data)
236 {
237  //Write the specified PHY register
238  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
239  interface->phyAddr, address, data);
240 }
241 
242 
243 /**
244  * @brief Read PHY register
245  * @param[in] interface Underlying network interface
246  * @param[in] address PHY register address
247  * @return Register value
248  **/
249 
250 uint16_t dp83822ReadPhyReg(NetInterface *interface, uint8_t address)
251 {
252  //Read the specified PHY register
253  return interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
254  interface->phyAddr, address);
255 }
256 
257 
258 /**
259  * @brief Dump PHY registers for debugging purpose
260  * @param[in] interface Underlying network interface
261  **/
262 
264 {
265  uint8_t i;
266 
267  //Loop through PHY registers
268  for(i = 0; i < 32; i++)
269  {
270  //Display current PHY register
271  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
272  dp83822ReadPhyReg(interface, i));
273  }
274 
275  //Terminate with a line feed
276  TRACE_DEBUG("\r\n");
277 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:525
#define DP83822_PHYSCR_INT_OE
int bool_t
Definition: compiler_port.h:49
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:119
const PhyDriver dp83822PhyDriver
DP83822 Ethernet PHY driver.
#define DP83822_BMSR
#define DP83822_PHYSCR_INT_EN
#define TRUE
Definition: os_port.h:50
PHY driver.
Definition: nic.h:214
#define DP83822_BMCR
#define SMI_OPCODE_WRITE
Definition: nic.h:62
void dp83822EnableIrq(NetInterface *interface)
Enable interrupts.
#define FALSE
Definition: os_port.h:46
#define DP83822_PHYSTS_SPEED_STATUS
uint16_t dp83822ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
error_t
Error codes.
Definition: error.h:42
void dp83822DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
error_t dp83822Init(NetInterface *interface)
DP83822 PHY transceiver initialization.
#define NetInterface
Definition: net.h:36
@ NIC_LINK_SPEED_10MBPS
Definition: nic.h:105
void dp83822Tick(NetInterface *interface)
DP83822 timer handler.
OsEvent netEvent
Definition: net.c:77
#define SMI_OPCODE_READ
Definition: nic.h:63
#define TRACE_INFO(...)
Definition: debug.h:94
#define DP83822_PHYSTS
#define DP83822_PHY_ADDR
#define DP83822_PHYSTS_LINK_STATUS
#define DP83822_MISR1_LINK_INT
#define TRACE_DEBUG(...)
Definition: debug.h:106
#define DP83822_PHYSTS_DUPLEX_STATUS
DP83822 Ethernet PHY transceiver.
#define DP83822_PHYSCR
@ NIC_HALF_DUPLEX_MODE
Definition: nic.h:118
#define DP83822_BMCR_RESET
Ipv6Addr address
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define DP83822_MISR1
void dp83822EventHandler(NetInterface *interface)
DP83822 event handler.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:106
void dp83822DisableIrq(NetInterface *interface)
Disable interrupts.
uint8_t value[]
Definition: dtls_misc.h:150
TCP/IP stack core.
uint8_t data[]
Definition: dtls_misc.h:176
#define DP83822_BMSR_LINK_STATUS
#define DP83822_MISR1_LINK_INT_EN
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void dp83822WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.