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.0
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  //Select RMII mode (50MHz output on REF_CLK)
109 
110  //The PHY is configured for autonomous operation
114 
115  //Perform custom configuration
116  tja1101InitHook(interface);
117 
118  //Force the TCP/IP stack to poll the link state at startup
119  interface->phyEvent = TRUE;
120  //Notify the TCP/IP stack of the event
122 
123  //Successful initialization
124  return NO_ERROR;
125 }
126 
127 
128 /**
129  * @brief TJA1101 custom configuration
130  * @param[in] interface Underlying network interface
131  **/
132 
133 __weak_func void tja1101InitHook(NetInterface *interface)
134 {
135 }
136 
137 
138 /**
139  * @brief TJA1101 timer handler
140  * @param[in] interface Underlying network interface
141  **/
142 
143 void tja1101Tick(NetInterface *interface)
144 {
145  uint16_t value;
146  bool_t linkState;
147 
148  //No external interrupt line driver?
149  if(interface->extIntDriver == NULL)
150  {
151  //Read status register
153  //Retrieve current link state
154  linkState = (value & TJA1101_BASIC_STAT_LINK_STATUS) ? TRUE : FALSE;
155 
156  //Link up event?
157  if(linkState && !interface->linkState)
158  {
159  //Set event flag
160  interface->phyEvent = TRUE;
161  //Notify the TCP/IP stack of the event
163  }
164  //Link down event?
165  else if(!linkState && interface->linkState)
166  {
167  //Set event flag
168  interface->phyEvent = TRUE;
169  //Notify the TCP/IP stack of the event
171  }
172  }
173 }
174 
175 
176 /**
177  * @brief Enable interrupts
178  * @param[in] interface Underlying network interface
179  **/
180 
182 {
183  //Enable PHY transceiver interrupts
184  if(interface->extIntDriver != NULL)
185  {
186  interface->extIntDriver->enableIrq();
187  }
188 }
189 
190 
191 /**
192  * @brief Disable interrupts
193  * @param[in] interface Underlying network interface
194  **/
195 
197 {
198  //Disable PHY transceiver interrupts
199  if(interface->extIntDriver != NULL)
200  {
201  interface->extIntDriver->disableIrq();
202  }
203 }
204 
205 
206 /**
207  * @brief TJA1101 event handler
208  * @param[in] interface Underlying network interface
209  **/
210 
212 {
213  uint16_t value;
214 
215  //Read status register
217 
218  //Link is up?
220  {
221  //Adjust MAC configuration parameters for proper operation
222  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
223  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
224  interface->nicDriver->updateMacConfig(interface);
225 
226  //Update link state
227  interface->linkState = TRUE;
228  }
229  else
230  {
231  //Update link state
232  interface->linkState = FALSE;
233  }
234 
235  //Process link state change event
236  nicNotifyLinkChange(interface);
237 }
238 
239 
240 /**
241  * @brief Write PHY register
242  * @param[in] interface Underlying network interface
243  * @param[in] address PHY register address
244  * @param[in] data Register value
245  **/
246 
247 void tja1101WritePhyReg(NetInterface *interface, uint8_t address,
248  uint16_t data)
249 {
250  //Write the specified PHY register
251  if(interface->smiDriver != NULL)
252  {
253  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
254  interface->phyAddr, address, data);
255  }
256  else
257  {
258  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
259  interface->phyAddr, address, data);
260  }
261 }
262 
263 
264 /**
265  * @brief Read PHY register
266  * @param[in] interface Underlying network interface
267  * @param[in] address PHY register address
268  * @return Register value
269  **/
270 
271 uint16_t tja1101ReadPhyReg(NetInterface *interface, uint8_t address)
272 {
273  uint16_t data;
274 
275  //Read the specified PHY register
276  if(interface->smiDriver != NULL)
277  {
278  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
279  interface->phyAddr, address);
280  }
281  else
282  {
283  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
284  interface->phyAddr, address);
285  }
286 
287  //Return the value of the PHY register
288  return data;
289 }
290 
291 
292 /**
293  * @brief Dump PHY registers for debugging purpose
294  * @param[in] interface Underlying network interface
295  **/
296 
298 {
299  uint8_t i;
300 
301  //Loop through PHY registers
302  for(i = 0; i < 32; i++)
303  {
304  //Display current PHY register
305  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
306  tja1101ReadPhyReg(interface, i));
307  }
308 
309  //Terminate with a line feed
310  TRACE_DEBUG("\r\n");
311 }
int bool_t
Definition: compiler_port.h:53
Debugging facilities.
#define TRACE_DEBUG(...)
Definition: debug.h:107
#define TRACE_INFO(...)
Definition: debug.h:95
error_t
Error codes.
Definition: error.h:43
@ NO_ERROR
Success.
Definition: error.h:44
uint8_t data[]
Definition: ethernet.h:222
Ipv6Addr address[]
Definition: ipv6.h:316
TCP/IP stack core.
#define NetInterface
Definition: net.h:36
#define netEvent
Definition: net_legacy.h:196
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:548
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define SMI_OPCODE_READ
Definition: nic.h:67
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
#define TRUE
Definition: os_port.h:50
#define FALSE
Definition: os_port.h:46
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
Ethernet PHY driver.
Definition: nic.h:308
uint8_t value[]
Definition: tcp.h:369
void tja1101EventHandler(NetInterface *interface)
TJA1101 event handler.
error_t tja1101Init(NetInterface *interface)
TJA1101 PHY transceiver initialization.
void tja1101EnableIrq(NetInterface *interface)
Enable interrupts.
void tja1101Tick(NetInterface *interface)
TJA1101 timer handler.
void tja1101DisableIrq(NetInterface *interface)
Disable interrupts.
__weak_func void tja1101InitHook(NetInterface *interface)
TJA1101 custom configuration.
const PhyDriver tja1101PhyDriver
TJA1101 Ethernet PHY driver.
void tja1101WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
uint16_t tja1101ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void tja1101DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
TJA1101 100Base-T1 Ethernet PHY driver.
#define TJA1101_CONFIG1_MII_MODE
#define TJA1101_COMM_CTRL
#define TJA1101_BASIC_STAT_LINK_STATUS
#define TJA1101_CONFIG1
#define TJA1101_BASIC_CTRL
#define TJA1101_CONFIG1_MII_MODE_RMII_50MHZ_REF_CLK_OUT
#define TJA1101_COMM_CTRL_AUTO_OP
#define TJA1101_BASIC_STAT
#define TJA1101_EXTENDED_CTRL_CONFIG_EN
#define TJA1101_PHY_ADDR
#define TJA1101_EXTENDED_CTRL
#define TJA1101_BASIC_CTRL_RESET