ksz8041_driver.h
Go to the documentation of this file.
1 /**
2  * @file ksz8041_driver.h
3  * @brief KSZ8041 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 #ifndef _KSZ8041_DRIVER_H
32 #define _KSZ8041_DRIVER_H
33 
34 //Dependencies
35 #include "core/nic.h"
36 
37 //PHY address
38 #ifndef KSZ8041_PHY_ADDR
39  #define KSZ8041_PHY_ADDR 1
40 #elif (KSZ8041_PHY_ADDR < 0 || KSZ8041_PHY_ADDR > 31)
41  #error KSZ8041_PHY_ADDR parameter is not valid
42 #endif
43 
44 //KSZ8041 PHY registers
45 #define KSZ8041_BMCR 0x00
46 #define KSZ8041_BMSR 0x01
47 #define KSZ8041_PHYID1 0x02
48 #define KSZ8041_PHYID2 0x03
49 #define KSZ8041_ANAR 0x04
50 #define KSZ8041_ANLPAR 0x05
51 #define KSZ8041_ANER 0x06
52 #define KSZ8041_ANNPR 0x07
53 #define KSZ8041_ANLPNPR 0x08
54 #define KSZ8041_MIICON 0x14
55 #define KSZ8041_RXERCTR 0x15
56 #define KSZ8041_ICSR 0x1B
57 #define KSZ8041_PHYCON1 0x1E
58 #define KSZ8041_PHYCON2 0x1F
59 
60 //Basic Control register
61 #define KSZ8041_BMCR_RESET 0x8000
62 #define KSZ8041_BMCR_LOOPBACK 0x4000
63 #define KSZ8041_BMCR_SPEED_SEL 0x2000
64 #define KSZ8041_BMCR_AN_EN 0x1000
65 #define KSZ8041_BMCR_POWER_DOWN 0x0800
66 #define KSZ8041_BMCR_ISOLATE 0x0400
67 #define KSZ8041_BMCR_RESTART_AN 0x0200
68 #define KSZ8041_BMCR_DUPLEX_MODE 0x0100
69 #define KSZ8041_BMCR_COL_TEST 0x0080
70 #define KSZ8041_BMCR_TX_DIS 0x0001
71 
72 //Basic Status register
73 #define KSZ8041_BMSR_100BT4 0x8000
74 #define KSZ8041_BMSR_100BTX_FD 0x4000
75 #define KSZ8041_BMSR_100BTX_HD 0x2000
76 #define KSZ8041_BMSR_10BT_FD 0x1000
77 #define KSZ8041_BMSR_10BT_HD 0x0800
78 #define KSZ8041_BMSR_NO_PREAMBLE 0x0040
79 #define KSZ8041_BMSR_AN_COMPLETE 0x0020
80 #define KSZ8041_BMSR_REMOTE_FAULT 0x0010
81 #define KSZ8041_BMSR_AN_CAPABLE 0x0008
82 #define KSZ8041_BMSR_LINK_STATUS 0x0004
83 #define KSZ8041_BMSR_JABBER_DETECT 0x0002
84 #define KSZ8041_BMSR_EXTENDED_CAPABLE 0x0001
85 
86 //PHY Identifier 1 register
87 #define KSZ8041_PHYID1_PHY_ID_MSB 0xFFFF
88 #define KSZ8041_PHYID1_PHY_ID_MSB_DEFAULT 0x0022
89 
90 //PHY Identifier 2 register
91 #define KSZ8041_PHYID2_PHY_ID_LSB 0xFC00
92 #define KSZ8041_PHYID2_PHY_ID_LSB_DEFAULT 0x1400
93 #define KSZ8041_PHYID2_MODEL_NUM 0x03F0
94 #define KSZ8041_PHYID2_MODEL_NUM_DEFAULT 0x0110
95 #define KSZ8041_PHYID2_REVISION_NUM 0x000F
96 
97 //Auto-Negotiation Advertisement register
98 #define KSZ8041_ANAR_NEXT_PAGE 0x8000
99 #define KSZ8041_ANAR_REMOTE_FAULT 0x2000
100 #define KSZ8041_ANAR_PAUSE 0x0C00
101 #define KSZ8041_ANAR_100BT4 0x0200
102 #define KSZ8041_ANAR_100BTX_FD 0x0100
103 #define KSZ8041_ANAR_100BTX_HD 0x0080
104 #define KSZ8041_ANAR_10BT_FD 0x0040
105 #define KSZ8041_ANAR_10BT_HD 0x0020
106 #define KSZ8041_ANAR_SELECTOR 0x001F
107 #define KSZ8041_ANAR_SELECTOR_DEFAULT 0x0001
108 
109 //Auto-Negotiation Link Partner Ability register
110 #define KSZ8041_ANLPAR_NEXT_PAGE 0x8000
111 #define KSZ8041_ANLPAR_ACK 0x4000
112 #define KSZ8041_ANLPAR_REMOTE_FAULT 0x2000
113 #define KSZ8041_ANLPAR_PAUSE 0x0C00
114 #define KSZ8041_ANLPAR_100BT4 0x0200
115 #define KSZ8041_ANLPAR_100BTX_FD 0x0100
116 #define KSZ8041_ANLPAR_100BTX_HD 0x0080
117 #define KSZ8041_ANLPAR_10BT_FD 0x0040
118 #define KSZ8041_ANLPAR_10BT_HD 0x0020
119 #define KSZ8041_ANLPAR_SELECTOR 0x001F
120 #define KSZ8041_ANLPAR_SELECTOR_DEFAULT 0x0001
121 
122 //Auto-Negotiation Expansion register
123 #define KSZ8041_ANER_PAR_DETECT_FAULT 0x0010
124 #define KSZ8041_ANER_LP_NEXT_PAGE_ABLE 0x0008
125 #define KSZ8041_ANER_NEXT_PAGE_ABLE 0x0004
126 #define KSZ8041_ANER_PAGE_RECEIVED 0x0002
127 #define KSZ8041_ANER_LP_AN_ABLE 0x0001
128 
129 //Auto-Negotiation Next Page register
130 #define KSZ8041_ANNPR_NEXT_PAGE 0x8000
131 #define KSZ8041_ANNPR_MSG_PAGE 0x2000
132 #define KSZ8041_ANNPR_ACK2 0x1000
133 #define KSZ8041_ANNPR_TOGGLE 0x0800
134 #define KSZ8041_ANNPR_MESSAGE 0x07FF
135 
136 //Link Partner Next Page Ability register
137 #define KSZ8041_ANLPNPR_NEXT_PAGE 0x8000
138 #define KSZ8041_ANLPNPR_ACK 0x4000
139 #define KSZ8041_ANLPNPR_MSG_PAGE 0x2000
140 #define KSZ8041_ANLPNPR_ACK2 0x1000
141 #define KSZ8041_ANLPNPR_TOGGLE 0x0800
142 #define KSZ8041_ANLPNPR_MESSAGE 0x07FF
143 
144 //MII Control register
145 #define KSZ8041_MIICON_100BTX_PREAMBLE_RESTORE 0x0080
146 #define KSZ8041_MIICON_10BT_PREAMBLE_RESTORE 0x0040
147 
148 //Interrupt Control/Status register
149 #define KSZ8041_ICSR_JABBER_IE 0x8000
150 #define KSZ8041_ICSR_RECEIVE_ERROR_IE 0x4000
151 #define KSZ8041_ICSR_PAGE_RECEIVED_IE 0x2000
152 #define KSZ8041_ICSR_PAR_DETECT_FAULT_IE 0x1000
153 #define KSZ8041_ICSR_LP_ACK_IE 0x0800
154 #define KSZ8041_ICSR_LINK_DOWN_IE 0x0400
155 #define KSZ8041_ICSR_REMOTE_FAULT_IE 0x0200
156 #define KSZ8041_ICSR_LINK_UP_IE 0x0100
157 #define KSZ8041_ICSR_JABBER_IF 0x0080
158 #define KSZ8041_ICSR_RECEIVE_ERROR_IF 0x0040
159 #define KSZ8041_ICSR_PAGE_RECEIVED_IF 0x0020
160 #define KSZ8041_ICSR_PAR_DETECT_FAULT_IF 0x0010
161 #define KSZ8041_ICSR_LP_ACK_IF 0x0008
162 #define KSZ8041_ICSR_LINK_DOWN_IF 0x0004
163 #define KSZ8041_ICSR_REMOTE_FAULT_IF 0x0002
164 #define KSZ8041_ICSR_LINK_UP_IF 0x0001
165 
166 //PHY Control 1 register
167 #define KSZ8041_PHYCON1_LED_MODE 0xC000
168 #define KSZ8041_PHYCON1_POLARITY 0x2000
169 #define KSZ8041_PHYCON1_MDIX_STATE 0x0800
170 #define KSZ8041_PHYCON1_REMOTE_LOOPBACK 0x0080
171 
172 //PHY Control 2 register
173 #define KSZ8041_PHYCON2_HP_MDIX 0x8000
174 #define KSZ8041_PHYCON2_MDIX_SEL 0x4000
175 #define KSZ8041_PHYCON2_PAIR_SWAP_DIS 0x2000
176 #define KSZ8041_PHYCON2_ENERGY_DETECT 0x1000
177 #define KSZ8041_PHYCON2_FORCE_LINK 0x0800
178 #define KSZ8041_PHYCON2_POWER_SAVING 0x0400
179 #define KSZ8041_PHYCON2_INT_LEVEL 0x0200
180 #define KSZ8041_PHYCON2_JABBER_EN 0x0100
181 #define KSZ8041_PHYCON2_AN_COMPLETE 0x0080
182 #define KSZ8041_PHYCON2_PAUSE_EN 0x0040
183 #define KSZ8041_PHYCON2_PHY_ISOLATE 0x0020
184 #define KSZ8041_PHYCON2_OP_MODE 0x001C
185 #define KSZ8041_PHYCON2_OP_MODE_AN 0x0000
186 #define KSZ8041_PHYCON2_OP_MODE_10BT_HD 0x0004
187 #define KSZ8041_PHYCON2_OP_MODE_100BTX_HD 0x0008
188 #define KSZ8041_PHYCON2_OP_MODE_10BT_FD 0x0014
189 #define KSZ8041_PHYCON2_OP_MODE_100BTX_FD 0x0018
190 #define KSZ8041_PHYCON2_SQE_TEST_EN 0x0002
191 #define KSZ8041_PHYCON2_DATA_SCRAMBLING_DIS 0x0001
192 
193 //C++ guard
194 #ifdef __cplusplus
195 extern "C" {
196 #endif
197 
198 //KSZ8041 Ethernet PHY driver
199 extern const PhyDriver ksz8041PhyDriver;
200 
201 //KSZ8041 related functions
202 error_t ksz8041Init(NetInterface *interface);
203 void ksz8041InitHook(NetInterface *interface);
204 
205 void ksz8041Tick(NetInterface *interface);
206 
207 void ksz8041EnableIrq(NetInterface *interface);
208 void ksz8041DisableIrq(NetInterface *interface);
209 
210 void ksz8041EventHandler(NetInterface *interface);
211 
212 void ksz8041WritePhyReg(NetInterface *interface, uint8_t address,
213  uint16_t data);
214 
215 uint16_t ksz8041ReadPhyReg(NetInterface *interface, uint8_t address);
216 
217 void ksz8041DumpPhyReg(NetInterface *interface);
218 
219 //C++ guard
220 #ifdef __cplusplus
221 }
222 #endif
223 
224 #endif
void ksz8041InitHook(NetInterface *interface)
KSZ8041 custom configuration.
void ksz8041EnableIrq(NetInterface *interface)
Enable interrupts.
Ethernet PHY driver.
Definition: nic.h:311
uint8_t data[]
Definition: ethernet.h:222
void ksz8041WritePhyReg(NetInterface *interface, uint8_t address, uint16_t data)
Write PHY register.
uint16_t ksz8041ReadPhyReg(NetInterface *interface, uint8_t address)
Read PHY register.
error_t
Error codes.
Definition: error.h:43
#define NetInterface
Definition: net.h:36
void ksz8041Tick(NetInterface *interface)
KSZ8041 timer handler.
void ksz8041DumpPhyReg(NetInterface *interface)
Dump PHY registers for debugging purpose.
Ipv6Addr address[]
Definition: ipv6.h:325
Network interface controller abstraction layer.
void ksz8041DisableIrq(NetInterface *interface)
Disable interrupts.
error_t ksz8041Init(NetInterface *interface)
KSZ8041 PHY transceiver initialization.
const PhyDriver ksz8041PhyDriver
KSZ8041 Ethernet PHY driver.
void ksz8041EventHandler(NetInterface *interface)
KSZ8041 event handler.