tja1103_driver.c
Go to the documentation of this file.
1 /**
2  * @file tja1103_driver.c
3  * @brief TJA1103 100Base-T1 Ethernet PHY driver
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2026 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.6.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 TJA1103 Ethernet PHY driver
42  **/
43 
45 {
51 };
52 
53 
54 /**
55  * @brief TJA1103 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 TJA1103...\r\n");
66 
67  //Undefined PHY address?
68  if(interface->phyAddr >= 32)
69  {
70  //Use the default address
71  interface->phyAddr = TJA1103_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(tja1103ReadMmdReg(interface, TJA1103_DEVICE_CONTROL) &
93  {
94  }
95 
96  //Dump PHY registers for debugging purpose
97  tja1103DumpPhyReg(interface);
98 
99  //Enable configuration register access
103 
106 
109 
112 
113  //Perform custom configuration
114  tja1103InitHook(interface);
115 
116  //The PHY is configured for autonomous operation
120 
121  //Clear FUSA_PASS_IRQ flag
124 
125  //Start operation
129 
130  //Force the TCP/IP stack to poll the link state at startup
131  interface->phyEvent = TRUE;
132  //Notify the TCP/IP stack of the event
133  osSetEvent(&interface->netContext->event);
134 
135  //Successful initialization
136  return NO_ERROR;
137 }
138 
139 
140 /**
141  * @brief TJA1103 custom configuration
142  * @param[in] interface Underlying network interface
143  **/
144 
145 __weak_func void tja1103InitHook(NetInterface *interface)
146 {
147 }
148 
149 
150 /**
151  * @brief TJA1103 timer handler
152  * @param[in] interface Underlying network interface
153  **/
154 
155 void tja1103Tick(NetInterface *interface)
156 {
157  uint16_t value;
158  bool_t linkState;
159 
160  //No external interrupt line driver?
161  if(interface->extIntDriver == NULL)
162  {
163  //Read status register
165  //Retrieve current link state
166  linkState = (value & TJA1103_PHY_STATUS_LINK_STATUS) ? TRUE : FALSE;
167 
168  //Link up event?
169  if(linkState && !interface->linkState)
170  {
171  //Set event flag
172  interface->phyEvent = TRUE;
173  //Notify the TCP/IP stack of the event
174  osSetEvent(&interface->netContext->event);
175  }
176  //Link down event?
177  else if(!linkState && interface->linkState)
178  {
179  //Set event flag
180  interface->phyEvent = TRUE;
181  //Notify the TCP/IP stack of the event
182  osSetEvent(&interface->netContext->event);
183  }
184  }
185 }
186 
187 
188 /**
189  * @brief Enable interrupts
190  * @param[in] interface Underlying network interface
191  **/
192 
194 {
195  //Enable PHY transceiver interrupts
196  if(interface->extIntDriver != NULL)
197  {
198  interface->extIntDriver->enableIrq();
199  }
200 }
201 
202 
203 /**
204  * @brief Disable interrupts
205  * @param[in] interface Underlying network interface
206  **/
207 
209 {
210  //Disable PHY transceiver interrupts
211  if(interface->extIntDriver != NULL)
212  {
213  interface->extIntDriver->disableIrq();
214  }
215 }
216 
217 
218 /**
219  * @brief TJA1103 event handler
220  * @param[in] interface Underlying network interface
221  **/
222 
224 {
225  uint16_t value;
226 
227  //Read status register
229 
230  //Check link state
232  {
233  //Adjust MAC configuration parameters for proper operation
234  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
235  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
236  interface->nicDriver->updateMacConfig(interface);
237 
238  //Update link state
239  interface->linkState = TRUE;
240  }
241  else
242  {
243  //Update link state
244  interface->linkState = FALSE;
245  }
246 
247  //Process link state change event
248  nicNotifyLinkChange(interface);
249 }
250 
251 
252 /**
253  * @brief Write PHY register
254  * @param[in] interface Underlying network interface
255  * @param[in] address PHY register address
256  * @param[in] data Register value
257  **/
258 
259 void tja1103WritePhyReg(NetInterface *interface, uint8_t address,
260  uint16_t data)
261 {
262  //Write the specified PHY register
263  if(interface->smiDriver != NULL)
264  {
265  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
266  interface->phyAddr, address, data);
267  }
268  else
269  {
270  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
271  interface->phyAddr, address, data);
272  }
273 }
274 
275 
276 /**
277  * @brief Read PHY register
278  * @param[in] interface Underlying network interface
279  * @param[in] address PHY register address
280  * @return Register value
281  **/
282 
283 uint16_t tja1103ReadPhyReg(NetInterface *interface, uint8_t address)
284 {
285  uint16_t data;
286 
287  //Read the specified PHY register
288  if(interface->smiDriver != NULL)
289  {
290  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
291  interface->phyAddr, address);
292  }
293  else
294  {
295  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
296  interface->phyAddr, address);
297  }
298 
299  //Return the value of the PHY register
300  return data;
301 }
302 
303 
304 /**
305  * @brief Dump PHY registers for debugging purpose
306  * @param[in] interface Underlying network interface
307  **/
308 
310 {
311  uint8_t i;
312 
313  //Loop through PHY registers
314  for(i = 0; i < 32; i++)
315  {
316  //Display current PHY register
317  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
318  tja1103ReadPhyReg(interface, i));
319  }
320 
321  //Terminate with a line feed
322  TRACE_DEBUG("\r\n");
323 }
324 
325 
326 /**
327  * @brief Write MMD register
328  * @param[in] interface Underlying network interface
329  * @param[in] devAddr Device address
330  * @param[in] regAddr Register address
331  * @param[in] data MMD register value
332  **/
333 
334 void tja1103WriteMmdReg(NetInterface *interface, uint8_t devAddr,
335  uint16_t regAddr, uint16_t data)
336 {
337  //Select register operation
340  (devAddr & TJA1103_CL45_ACCESS_CONTROL_MMD));
341 
342  //Write MMD register address
344 
345  //Select data operation
348  (devAddr & TJA1103_CL45_ACCESS_CONTROL_MMD));
349 
350  //Write the content of the MMD register
352 }
353 
354 
355 /**
356  * @brief Read MMD register
357  * @param[in] interface Underlying network interface
358  * @param[in] devAddr Device address
359  * @param[in] regAddr Register address
360  * @return MMD register value
361  **/
362 
363 uint16_t tja1103ReadMmdReg(NetInterface *interface, uint8_t devAddr,
364  uint16_t regAddr)
365 {
366  //Select register operation
369  (devAddr & TJA1103_CL45_ACCESS_CONTROL_MMD));
370 
371  //Write MMD register address
373 
374  //Select data operation
377  (devAddr & TJA1103_CL45_ACCESS_CONTROL_MMD));
378 
379  //Read the content of the MMD register
380  return tja1103ReadPhyReg(interface, TJA1103_CL45_ADDRESS_DATA);
381 }
void nicNotifyLinkChange(NetInterface *interface)
Process link state change notification.
Definition: nic.c:601
int bool_t
Definition: compiler_port.h:63
void tja1103EventHandler(NetInterface *interface)
TJA1103 event handler.
#define TJA1103_PORT_CONTROL
void tja1103DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
@ NIC_FULL_DUPLEX_MODE
Definition: nic.h:125
#define TJA1103_DEVICE_CONTROL
uint16_t tja1103ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void tja1103WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
#define TRUE
Definition: os_port.h:50
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:224
#define TJA1103_PHY_STATUS
error_t tja1103Init(NetInterface *interface)
TJA1103 PHY transceiver initialization.
void tja1103Tick(NetInterface *interface)
TJA1103 timer handler.
TJA1103 100Base-T1 Ethernet PHY driver.
#define TJA1103_PORT_INFRA_CONTROL_CONFIG_ENABLE
#define TJA1103_CL45_ACCESS_CONTROL_OP_DATA_NO_POST_INC
#define TJA1103_CL45_ACCESS_CONTROL_MMD
#define TJA1103_CL45_ADDRESS_DATA
#define TJA1103_ALWAYS_ACCESSIBLE_FUSA_PASS_IRQ
#define TJA1103_DEVICE_CONTROL_SUPER_CONFIG_ENABLE
#define TJA1103_PHY_CONTROL
#define SMI_OPCODE_WRITE
Definition: nic.h:66
#define FALSE
Definition: os_port.h:46
error_t
Error codes.
Definition: error.h:43
#define TJA1103_DEVICE_CONTROL_DEVICE_RESET
#define TJA1103_PHY_CONTROL_START_OPERATION
__weak_func void tja1103InitHook(NetInterface *interface)
TJA1103 custom configuration.
#define TJA1103_PHY_STATUS_LINK_STATUS
#define TJA1103_PORT_CONTROL_CONFIG_ENABLE
#define TJA1103_PHY_CONFIG
#define NetInterface
Definition: net.h:40
#define TJA1103_CL45_ACCESS_CONTROL
#define TJA1103_PORT_INFRA_CONTROL
#define SMI_OPCODE_READ
Definition: nic.h:67
void tja1103WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
#define TRACE_INFO(...)
Definition: debug.h:105
#define TJA1103_DEVICE_CONTROL_GLOBAL_CONFIG_ENABLE
#define TJA1103_ALWAYS_ACCESSIBLE
#define TRACE_DEBUG(...)
Definition: debug.h:119
uint16_t regAddr
uint16_t tja1103ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
Ipv6Addr address[]
Definition: ipv6.h:345
const PhyDriver tja1103PhyDriver
TJA1103 Ethernet PHY driver.
uint8_t value[]
Definition: tcp.h:376
#define TJA1103_PHY_ADDR
void osSetEvent(OsEvent *event)
Set the specified event object to the signaled state.
#define TJA1103_CL45_ACCESS_CONTROL_OP_ADDR
@ NIC_LINK_SPEED_100MBPS
Definition: nic.h:112
void tja1103EnableIrq(NetInterface *interface)
Enable interrupts.
TCP/IP stack core.
#define TJA1103_PHY_CONTROL_CONFIG_ENABLE
#define TJA1103_PHY_CONFIG_AUTO_OPERATION
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
void tja1103DisableIrq(NetInterface *interface)
Disable interrupts.