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-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 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  //Wait for the MII interface to be ready
87  do
88  {
89  //Read PHY identification register 1
91 
92  //Check identifier value
94 
95  //Dump PHY registers for debugging purpose
96  tja1103DumpPhyReg(interface);
97 
98  //Enable configuration register access
101 
105 
106  //The PHY is configured for autonomous operation
110 
111  //Clear FUSA_PASS interrupt flag
114 
115  //Start operation
119 
120  //Perform custom configuration
121  tja1103InitHook(interface);
122 
123  //Force the TCP/IP stack to poll the link state at startup
124  interface->phyEvent = TRUE;
125  //Notify the TCP/IP stack of the event
127 
128  //Successful initialization
129  return NO_ERROR;
130 }
131 
132 
133 /**
134  * @brief TJA1103 custom configuration
135  * @param[in] interface Underlying network interface
136  **/
137 
138 __weak_func void tja1103InitHook(NetInterface *interface)
139 {
140 }
141 
142 
143 /**
144  * @brief TJA1103 timer handler
145  * @param[in] interface Underlying network interface
146  **/
147 
148 void tja1103Tick(NetInterface *interface)
149 {
150  uint16_t value;
151  bool_t linkState;
152 
153  //No external interrupt line driver?
154  if(interface->extIntDriver == NULL)
155  {
156  //Read status register
158  //Retrieve current link state
159  linkState = (value & TJA1103_PHY_STAT_LINK_STATUS) ? TRUE : FALSE;
160 
161  //Link up event?
162  if(linkState && !interface->linkState)
163  {
164  //Set event flag
165  interface->phyEvent = TRUE;
166  //Notify the TCP/IP stack of the event
168  }
169  //Link down event?
170  else if(!linkState && interface->linkState)
171  {
172  //Set event flag
173  interface->phyEvent = TRUE;
174  //Notify the TCP/IP stack of the event
176  }
177  }
178 }
179 
180 
181 /**
182  * @brief Enable interrupts
183  * @param[in] interface Underlying network interface
184  **/
185 
187 {
188  //Enable PHY transceiver interrupts
189  if(interface->extIntDriver != NULL)
190  {
191  interface->extIntDriver->enableIrq();
192  }
193 }
194 
195 
196 /**
197  * @brief Disable interrupts
198  * @param[in] interface Underlying network interface
199  **/
200 
202 {
203  //Disable PHY transceiver interrupts
204  if(interface->extIntDriver != NULL)
205  {
206  interface->extIntDriver->disableIrq();
207  }
208 }
209 
210 
211 /**
212  * @brief TJA1103 event handler
213  * @param[in] interface Underlying network interface
214  **/
215 
217 {
218  uint16_t value;
219 
220  //Read status register
222 
223  //Link is up?
225  {
226  //Adjust MAC configuration parameters for proper operation
227  interface->linkSpeed = NIC_LINK_SPEED_100MBPS;
228  interface->duplexMode = NIC_FULL_DUPLEX_MODE;
229  interface->nicDriver->updateMacConfig(interface);
230 
231  //Update link state
232  interface->linkState = TRUE;
233  }
234  else
235  {
236  //Update link state
237  interface->linkState = FALSE;
238  }
239 
240  //Process link state change event
241  nicNotifyLinkChange(interface);
242 }
243 
244 
245 /**
246  * @brief Write PHY register
247  * @param[in] interface Underlying network interface
248  * @param[in] address PHY register address
249  * @param[in] data Register value
250  **/
251 
252 void tja1103WritePhyReg(NetInterface *interface, uint8_t address,
253  uint16_t data)
254 {
255  //Write the specified PHY register
256  if(interface->smiDriver != NULL)
257  {
258  interface->smiDriver->writePhyReg(SMI_OPCODE_WRITE,
259  interface->phyAddr, address, data);
260  }
261  else
262  {
263  interface->nicDriver->writePhyReg(SMI_OPCODE_WRITE,
264  interface->phyAddr, address, data);
265  }
266 }
267 
268 
269 /**
270  * @brief Read PHY register
271  * @param[in] interface Underlying network interface
272  * @param[in] address PHY register address
273  * @return Register value
274  **/
275 
276 uint16_t tja1103ReadPhyReg(NetInterface *interface, uint8_t address)
277 {
278  uint16_t data;
279 
280  //Read the specified PHY register
281  if(interface->smiDriver != NULL)
282  {
283  data = interface->smiDriver->readPhyReg(SMI_OPCODE_READ,
284  interface->phyAddr, address);
285  }
286  else
287  {
288  data = interface->nicDriver->readPhyReg(SMI_OPCODE_READ,
289  interface->phyAddr, address);
290  }
291 
292  //Return the value of the PHY register
293  return data;
294 }
295 
296 
297 /**
298  * @brief Dump PHY registers for debugging purpose
299  * @param[in] interface Underlying network interface
300  **/
301 
303 {
304  uint8_t i;
305 
306  //Loop through PHY registers
307  for(i = 0; i < 32; i++)
308  {
309  //Display current PHY register
310  TRACE_DEBUG("%02" PRIu8 ": 0x%04" PRIX16 "\r\n", i,
311  tja1103ReadPhyReg(interface, i));
312  }
313 
314  //Terminate with a line feed
315  TRACE_DEBUG("\r\n");
316 }
317 
318 
319 /**
320  * @brief Write MMD register
321  * @param[in] interface Underlying network interface
322  * @param[in] devAddr Device address
323  * @param[in] regAddr Register address
324  * @param[in] data MMD register value
325  **/
326 
327 void tja1103WriteMmdReg(NetInterface *interface, uint8_t devAddr,
328  uint16_t regAddr, uint16_t data)
329 {
330  //Select register operation
333 
334  //Write MMD register address
336 
337  //Select data operation
340 
341  //Write the content of the MMD register
343 }
344 
345 
346 /**
347  * @brief Read MMD register
348  * @param[in] interface Underlying network interface
349  * @param[in] devAddr Device address
350  * @param[in] regAddr Register address
351  * @return MMD register value
352  **/
353 
354 uint16_t tja1103ReadMmdReg(NetInterface *interface, uint8_t devAddr,
355  uint16_t regAddr)
356 {
357  //Select register operation
360 
361  //Write MMD register address
363 
364  //Select data operation
367 
368  //Read the content of the MMD register
369  return tja1103ReadPhyReg(interface, TJA1103_MMDAD);
370 }
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
uint16_t regAddr
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
uint16_t tja1103ReadMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr)
Read MMD register.
void tja1103WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
void tja1103EventHandler(NetInterface *interface)
TJA1103 event handler.
void tja1103EnableIrq(NetInterface *interface)
Enable interrupts.
__weak_func void tja1103InitHook(NetInterface *interface)
TJA1103 custom configuration.
void tja1103Tick(NetInterface *interface)
TJA1103 timer handler.
void tja1103DisableIrq(NetInterface *interface)
Disable interrupts.
void tja1103WriteMmdReg(NetInterface *interface, uint8_t devAddr, uint16_t regAddr, uint16_t data)
Write MMD register.
uint16_t tja1103ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
void tja1103DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
const PhyDriver tja1103PhyDriver
TJA1103 Ethernet PHY driver.
error_t tja1103Init(NetInterface *interface)
TJA1103 PHY transceiver initialization.
TJA1103 100Base-T1 Ethernet PHY driver.
#define TJA1103_MMDCTRL
#define TJA1103_PHY_CTRL_CONFIG_EN
#define TJA1103_PHY_ADDR
#define TJA1103_PORT_CTRL
#define TJA1103_INFRA_CTRL_EN
#define TJA1103_MMDAD
#define TJA1103_INFRA_CTRL
#define TJA1103_DEVICE_CTRL
#define TJA1103_PHY_CONFIG_AUTO
#define TJA1103_DEVICE_CTRL_CONFIG_GLOBAL_EN
#define TJA1103_PHY_STAT_LINK_STATUS
#define TJA1103_ALWAYS_ACCESSIBLE
#define TJA1103_PORT_CTRL_EN
#define TJA1103_DEVICE_CTRL_CONFIG_ALL_EN
#define TJA1103_PHY_CONFIG
#define TJA1103_PHY_ID1
#define TJA1103_PHY_CTRL_START_OP
#define TJA1103_PHY_CTRL
#define TJA1103_MMDCTRL_DEVAD
#define TJA1103_MMDCTRL_FNCTN_DATA_NO_POST_INC
#define TJA1103_MMDCTRL_FNCTN_ADDR
#define TJA1103_ALWAYS_ACCESSIBLE_FUSA_PASS
#define TJA1103_PHY_STAT
#define TJA1103_PHY_ID1_OUI_MSB_DEFAULT