tja1101_driver.c
Go to the documentation of this file.
1 /**
2  * @file tja1101_driver.c
3  * @brief TJA1101 100Base-T1 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 TJA1101 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief TJA1101 PHY transceiver initialization
56  * @param[in] interface Underlying network interface
57  * @return Error code
58  **/
59 
61 {
62  uint16_t value;
63 
64  //Debug message
65  TRACE_INFO("Initializing TJA1101...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = TJA1101_PHY_ADDR;
72  }
73 
74  //Initialize serial management interface
75  if(interface->smiDriver != NULL)
76  {
77  interface->smiDriver->init();
78  }
79 
80  //Initialize external interrupt line driver
81  if(interface->extIntDriver != NULL)
82  {
83  interface->extIntDriver->init();
84  }
85 
86  //Reset PHY transceiver
89 
90  //Wait for the reset to complete
91  while(tja1101ReadPhyReg(interface, TJA1101_BASIC_CTRL) &
93  {
94  }
95 
96  //Dump PHY registers for debugging purpose
97  tja1101DumpPhyReg(interface);
98 
99  //Enable configuration register access
103 
104  //Perform custom configuration
105  tja1101InitHook(interface);
106 
107  //The PHY is configured for autonomous operation
111 
112  //Force the TCP/IP stack to poll the link state at startup
113  interface->phyEvent = TRUE;
114  //Notify the TCP/IP stack of the event
116 
117  //Successful initialization
118  return NO_ERROR;
119 }
120 
121 
122 /**
123  * @brief TJA1101 custom configuration
124  * @param[in] interface Underlying network interface
125  **/
126 
127 __weak_func void tja1101InitHook(NetInterface *interface)
128 {
129 }
130 
131 
132 /**
133  * @brief TJA1101 timer handler
134  * @param[in] interface Underlying network interface
135  **/
136 
137 void tja1101Tick(NetInterface *interface)
138 {
139  uint16_t value;
140  bool_t linkState;
141 
142  //No external interrupt line driver?
143  if(interface->extIntDriver == NULL)
144  {
145  //Read status register
147  //Retrieve current link state
148  linkState = (value & TJA1101_BASIC_STAT_LINK_STATUS) ? TRUE : FALSE;
149 
150  //Link up event?
151  if(linkState && !interface->linkState)
152  {
153  //Set event flag
154  interface->phyEvent = TRUE;
155  //Notify the TCP/IP stack of the event
157  }
158  //Link down event?
159  else if(!linkState && interface->linkState)
160  {
161  //Set event flag
162  interface->phyEvent = TRUE;
163  //Notify the TCP/IP stack of the event
165  }
166  }
167 }
168 
169 
170 /**
171  * @brief Enable interrupts
172  * @param[in] interface Underlying network interface
173  **/
174 
176 {
177  //Enable PHY transceiver interrupts
178  if(interface->extIntDriver != NULL)
179  {
180  interface->extIntDriver->enableIrq();
181  }
182 }
183 
184 
185 /**
186  * @brief Disable interrupts
187  * @param[in] interface Underlying network interface
188  **/
189 
191 {
192  //Disable PHY transceiver interrupts
193  if(interface->extIntDriver != NULL)
194  {
195  interface->extIntDriver->disableIrq();
196  }
197 }
198 
199 
200 /**
201  * @brief TJA1101 event handler
202  * @param[in] interface Underlying network interface
203  **/
204 
206 {
207  uint16_t value;
208 
209  //Read status register
211 
212  //Link is up?
214  {
215  //Adjust MAC configuration parameters for proper operation
216  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
217  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
218  interface->nicDriver->updateMacConfig(interface);
219 
220  //Update link state
221  interface->linkState = TRUE;
222  }
223  else
224  {
225  //Update link state
226  interface->linkState = FALSE;
227  }
228 
229  //Process link state change event
230  nicNotifyLinkChange(interface);
231 }
232 
233 
234 /**
235  * @brief Write PHY register
236  * @param[in] interface Underlying network interface
237  * @param[in] address PHY register address
238  * @param[in] data Register value
239  **/
240 
241 void tja1101WritePhyReg(NetInterface *interface, uint8_t address,
242  uint16_t data)
243 {
244  //Write the specified PHY register
245  if(interface->smiDriver != NULL)
246  {
247  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
248  interface->phyAddr, address, data);
249  }
250  else
251  {
252  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
253  interface->phyAddr, address, data);
254  }
255 }
256 
257 
258 /**
259  * @brief Read PHY register
260  * @param[in] interface Underlying network interface
261  * @param[in] address PHY register address
262  * @return Register value
263  **/
264 
265 uint16_t tja1101ReadPhyReg(NetInterface *interface, uint8_t address)
266 {
267  uint16_t data;
268 
269  //Read the specified PHY register
270  if(interface->smiDriver != NULL)
271  {
272  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
273  interface->phyAddr, address);
274  }
275  else
276  {
277  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
278  interface->phyAddr, address);
279  }
280 
281  //Return the value of the PHY register
282  return data;
283 }
284 
285 
286 /**
287  * @brief Dump PHY registers for debugging purpose
288  * @param[in] interface Underlying network interface
289  **/
290 
292 {
293  uint8_t i;
294 
295  //Loop through PHY registers
296  for(i = 0; i < 32; i++)
297  {
298  //Display current PHY register
299  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
300  tja1101ReadPhyReg(interface, i));
301  }
302 
303  //Terminate with a line feed
304  TRACE_DEBUG("\r\n");
305 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:559
__weak_func void tja1101InitHook(NetInterface *interface)
TJA1101 custom configuration.
#define TJA1101_BASIC_STAT
int bool_t
Definition: compiler_port.h:53
#define netEvent
Definition: net_legacy.h:196
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define TJA1101_PHY_ADDR
const PhyDriver tja1101PhyDriver
TJA1101 Ethernet PHY driver.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
#define TJA1101_COMM_CTRL
void tja1101DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define TJA1101_BASIC_STAT_LINK_STATUS
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
#define TJA1101_BASIC_CTRL
#define NetInterface
Definition: net.h:36
void tja1101Tick(NetInterface *interface)
TJA1101 timer handler.
#define TJA1101_BASIC_CTRL_RESET
void tja1101DisableIrq(NetInterface *interface)
Disable interrupts.
#define SMI_OPCODE_READ
Definition: nic.h:67
#define TRACE_INFO(...)
Definition: debug.h:95
error_t tja1101Init(NetInterface *interface)
TJA1101 PHY transceiver initialization.
uint16_t tja1101ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
#define TJA1101_EXTENDED_CTRL_CONFIG_EN
#define TJA1101_EXTENDED_CTRL
#define TRACE_DEBUG(...)
Definition: debug.h:107
Ipv6Addr address[]
Definition: ipv6.h:325
void tja1101WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define TJA1101_COMM_CTRL_AUTO_OP
uint8_t value[]
Definition: tcp.h:369
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
void tja1101EventHandler(NetInterface *interface)
TJA1101 event handler.
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
TCP/IP stack core.
TJA1101 100Base-T1 Ethernet PHY driver.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void tja1101EnableIrq(NetInterface *interface)
Enable interrupts.