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