mediatek: mt7622: add Linux 5.10 support
[openwrt/staging/rmilecki.git] / target / linux / mediatek / files-5.10 / drivers / net / phy / rtk / rtl8367c / rldp.c
1 /*
2 * Copyright (C) 2013 Realtek Semiconductor Corp.
3 * All Rights Reserved.
4 *
5 * Unless you and Realtek execute a separate written software license
6 * agreement governing use of this software, this software is licensed
7 * to you under the terms of the GNU General Public License version 2,
8 * available at https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
9 *
10 * $Revision: 76306 $
11 * $Date: 2017-03-08 15:13:58 +0800 (¶g¤T, 08 ¤T¤ë 2017) $
12 *
13 * Purpose : Declaration of RLDP and RLPP API
14 *
15 * Feature : The file have include the following module and sub-modules
16 * 1) RLDP and RLPP configuration and status
17 *
18 */
19
20
21 /*
22 * Include Files
23 */
24 #include <rtk_switch.h>
25 #include <rtk_error.h>
26 //#include <rtk_types.h>
27 #include <rldp.h>
28
29 #include <rtl8367c_asicdrv.h>
30 #include <rtl8367c_asicdrv_rldp.h>
31
32 /*
33 * Symbol Definition
34 */
35
36
37 /*
38 * Data Declaration
39 */
40
41
42 /*
43 * Macro Declaration
44 */
45
46
47 /*
48 * Function Declaration
49 */
50
51 /* Module Name : RLDP */
52
53 /* Function Name:
54 * rtk_rldp_config_set
55 * Description:
56 * Set RLDP module configuration
57 * Input:
58 * pConfig - configuration structure of RLDP
59 * Output:
60 * None
61 * Return:
62 * RT_ERR_OK
63 * RT_ERR_FAILED
64 * RT_ERR_INPUT
65 * RT_ERR_NULL_POINTER
66 * Note:
67 * None
68 */
69 rtk_api_ret_t rtk_rldp_config_set(rtk_rldp_config_t *pConfig)
70 {
71 rtk_api_ret_t retVal;
72 ether_addr_t magic;
73 rtk_uint32 pmsk;
74
75 /* Check initialization state */
76 RTK_CHK_INIT_STATE();
77
78 if (pConfig->rldp_enable >= RTK_ENABLE_END)
79 return RT_ERR_INPUT;
80
81 if (pConfig->trigger_mode >= RTK_RLDP_TRIGGER_END)
82 return RT_ERR_INPUT;
83
84 if (pConfig->compare_type >= RTK_RLDP_CMPTYPE_END)
85 return RT_ERR_INPUT;
86
87 if (pConfig->num_check >= RTK_RLDP_NUM_MAX)
88 return RT_ERR_INPUT;
89
90 if (pConfig->interval_check >= RTK_RLDP_INTERVAL_MAX)
91 return RT_ERR_INPUT;
92
93 if (pConfig->num_loop >= RTK_RLDP_NUM_MAX)
94 return RT_ERR_INPUT;
95
96 if (pConfig->interval_loop >= RTK_RLDP_INTERVAL_MAX)
97 return RT_ERR_INPUT;
98
99 if ((retVal = rtl8367c_getAsicRldpTxPortmask(&pmsk))!=RT_ERR_OK)
100 return retVal;
101
102 if ((retVal = rtl8367c_setAsicRldpTxPortmask(0x00))!=RT_ERR_OK)
103 return retVal;
104
105 if ((retVal = rtl8367c_setAsicRldpTxPortmask(pmsk))!=RT_ERR_OK)
106 return retVal;
107
108 if ((retVal = rtl8367c_setAsicRldp(pConfig->rldp_enable))!=RT_ERR_OK)
109 return retVal;
110
111 if ((retVal = rtl8367c_setAsicRldpTriggerMode(pConfig->trigger_mode))!=RT_ERR_OK)
112 return retVal;
113
114 memcpy(&magic, &pConfig->magic, sizeof(ether_addr_t));
115 if ((retVal = rtl8367c_setAsicRldpMagicNum(magic))!=RT_ERR_OK)
116 return retVal;
117
118 if ((retVal = rtl8367c_setAsicRldpCompareRandomNumber(pConfig->compare_type))!=RT_ERR_OK)
119 return retVal;
120
121 if ((retVal = rtl8367c_setAsicRldpCompareRandomNumber(pConfig->compare_type))!=RT_ERR_OK)
122 return retVal;
123
124 if ((retVal = rtl8367c_setAsicRldpCheckingStatePara(pConfig->num_check, pConfig->interval_check))!=RT_ERR_OK)
125 return retVal;
126
127 if ((retVal = rtl8367c_setAsicRldpLoopStatePara(pConfig->num_loop, pConfig->interval_loop))!=RT_ERR_OK)
128 return retVal;
129
130 return RT_ERR_OK;
131 }
132
133
134 /* Function Name:
135 * rtk_rldp_config_get
136 * Description:
137 * Get RLDP module configuration
138 * Input:
139 * None
140 * Output:
141 * pConfig - configuration structure of RLDP
142 * Return:
143 * RT_ERR_OK
144 * RT_ERR_FAILED
145 * RT_ERR_INPUT
146 * RT_ERR_NULL_POINTER
147 * Note:
148 * None
149 */
150 rtk_api_ret_t rtk_rldp_config_get(rtk_rldp_config_t *pConfig)
151 {
152 rtk_api_ret_t retVal;
153 ether_addr_t magic;
154
155 /* Check initialization state */
156 RTK_CHK_INIT_STATE();
157
158 if ((retVal = rtl8367c_getAsicRldp(&pConfig->rldp_enable))!=RT_ERR_OK)
159 return retVal;
160
161 if ((retVal = rtl8367c_getAsicRldpTriggerMode(&pConfig->trigger_mode))!=RT_ERR_OK)
162 return retVal;
163
164 if ((retVal = rtl8367c_getAsicRldpMagicNum(&magic))!=RT_ERR_OK)
165 return retVal;
166 memcpy(&pConfig->magic, &magic, sizeof(ether_addr_t));
167
168 if ((retVal = rtl8367c_getAsicRldpCompareRandomNumber(&pConfig->compare_type))!=RT_ERR_OK)
169 return retVal;
170
171 if ((retVal = rtl8367c_getAsicRldpCompareRandomNumber(&pConfig->compare_type))!=RT_ERR_OK)
172 return retVal;
173
174 if ((retVal = rtl8367c_getAsicRldpCheckingStatePara(&pConfig->num_check, &pConfig->interval_check))!=RT_ERR_OK)
175 return retVal;
176
177 if ((retVal = rtl8367c_getAsicRldpLoopStatePara(&pConfig->num_loop, &pConfig->interval_loop))!=RT_ERR_OK)
178 return retVal;
179
180 return RT_ERR_OK;
181 }
182
183
184 /* Function Name:
185 * rtk_rldp_portConfig_set
186 * Description:
187 * Set per port RLDP module configuration
188 * Input:
189 * port - port number to be configured
190 * pPortConfig - per port configuration structure of RLDP
191 * Output:
192 * None
193 * Return:
194 * RT_ERR_OK
195 * RT_ERR_FAILED
196 * RT_ERR_INPUT
197 * RT_ERR_NULL_POINTER
198 * Note:
199 * None
200 */
201 rtk_api_ret_t rtk_rldp_portConfig_set(rtk_port_t port, rtk_rldp_portConfig_t *pPortConfig)
202 {
203 rtk_api_ret_t retVal;
204 rtk_uint32 pmsk;
205 rtk_uint32 phy_port;
206
207 /* Check initialization state */
208 RTK_CHK_INIT_STATE();
209
210 /* Check Port Valid */
211 RTK_CHK_PORT_VALID(port);
212
213 if (pPortConfig->tx_enable>= RTK_ENABLE_END)
214 return RT_ERR_INPUT;
215
216 phy_port = rtk_switch_port_L2P_get(port);
217
218 if ((retVal = rtl8367c_getAsicRldpTxPortmask(&pmsk))!=RT_ERR_OK)
219 return retVal;
220
221 if (pPortConfig->tx_enable)
222 {
223 pmsk |=(1<<phy_port);
224 }
225 else
226 {
227 pmsk &= ~(1<<phy_port);
228 }
229
230 if ((retVal = rtl8367c_setAsicRldpTxPortmask(pmsk))!=RT_ERR_OK)
231 return retVal;
232
233 return RT_ERR_OK;
234
235 } /* end of rtk_rldp_portConfig_set */
236
237
238 /* Function Name:
239 * rtk_rldp_portConfig_get
240 * Description:
241 * Get per port RLDP module configuration
242 * Input:
243 * port - port number to be get
244 * Output:
245 * pPortConfig - per port configuration structure of RLDP
246 * Return:
247 * RT_ERR_OK
248 * RT_ERR_FAILED
249 * RT_ERR_INPUT
250 * RT_ERR_NULL_POINTER
251 * Note:
252 * None
253 */
254 rtk_api_ret_t rtk_rldp_portConfig_get(rtk_port_t port, rtk_rldp_portConfig_t *pPortConfig)
255 {
256 rtk_api_ret_t retVal;
257 rtk_uint32 pmsk;
258 rtk_portmask_t logicalPmask;
259
260 /* Check initialization state */
261 RTK_CHK_INIT_STATE();
262
263 /* Check Port Valid */
264 RTK_CHK_PORT_VALID(port);
265
266 if ((retVal = rtl8367c_getAsicRldpTxPortmask(&pmsk))!=RT_ERR_OK)
267 return retVal;
268
269 if ((retVal = rtk_switch_portmask_P2L_get(pmsk, &logicalPmask)) != RT_ERR_OK)
270 return retVal;
271
272
273 if (logicalPmask.bits[0] & (1<<port))
274 {
275 pPortConfig->tx_enable = ENABLED;
276 }
277 else
278 {
279 pPortConfig->tx_enable = DISABLED;
280 }
281
282 return RT_ERR_OK;
283 } /* end of rtk_rldp_portConfig_get */
284
285
286 /* Function Name:
287 * rtk_rldp_status_get
288 * Description:
289 * Get RLDP module status
290 * Input:
291 * None
292 * Output:
293 * pStatus - status structure of RLDP
294 * Return:
295 * RT_ERR_OK
296 * RT_ERR_FAILED
297 * RT_ERR_NULL_POINTER
298 * Note:
299 * None
300 */
301 rtk_api_ret_t rtk_rldp_status_get(rtk_rldp_status_t *pStatus)
302 {
303 rtk_api_ret_t retVal;
304 ether_addr_t seed;
305
306 /* Check initialization state */
307 RTK_CHK_INIT_STATE();
308
309 if ((retVal = rtl8367c_getAsicRldpRandomNumber(&seed))!=RT_ERR_OK)
310 return retVal;
311 memcpy(&pStatus->id, &seed, sizeof(ether_addr_t));
312
313 return RT_ERR_OK;
314 } /* end of rtk_rldp_status_get */
315
316
317 /* Function Name:
318 * rtk_rldp_portStatus_get
319 * Description:
320 * Get RLDP module status
321 * Input:
322 * port - port number to be get
323 * Output:
324 * pPortStatus - per port status structure of RLDP
325 * Return:
326 * RT_ERR_OK
327 * RT_ERR_FAILED
328 * RT_ERR_INPUT
329 * RT_ERR_NULL_POINTER
330 * Note:
331 * None
332 */
333 rtk_api_ret_t rtk_rldp_portStatus_get(rtk_port_t port, rtk_rldp_portStatus_t *pPortStatus)
334 {
335 rtk_api_ret_t retVal;
336 rtk_uint32 pmsk;
337 rtk_portmask_t logicalPmask;
338
339 /* Check initialization state */
340 RTK_CHK_INIT_STATE();
341
342 /* Check Port Valid */
343 RTK_CHK_PORT_VALID(port);
344
345 if ((retVal = rtl8367c_getAsicRldpLoopedPortmask(&pmsk))!=RT_ERR_OK)
346 return retVal;
347 if ((retVal = rtk_switch_portmask_P2L_get(pmsk, &logicalPmask)) != RT_ERR_OK)
348 return retVal;
349
350 if (logicalPmask.bits[0] & (1<<port))
351 {
352 pPortStatus->loop_status = RTK_RLDP_LOOPSTS_LOOPING;
353 }
354 else
355 {
356 pPortStatus->loop_status = RTK_RLDP_LOOPSTS_NONE;
357 }
358
359 if ((retVal = rtl8367c_getAsicRldpEnterLoopedPortmask(&pmsk))!=RT_ERR_OK)
360 return retVal;
361 if ((retVal = rtk_switch_portmask_P2L_get(pmsk, &logicalPmask)) != RT_ERR_OK)
362 return retVal;
363
364 if (logicalPmask.bits[0] & (1<<port))
365 {
366 pPortStatus->loop_enter = RTK_RLDP_LOOPSTS_LOOPING;
367 }
368 else
369 {
370 pPortStatus->loop_enter = RTK_RLDP_LOOPSTS_NONE;
371 }
372
373 if ((retVal = rtl8367c_getAsicRldpLeaveLoopedPortmask(&pmsk))!=RT_ERR_OK)
374 return retVal;
375 if ((retVal = rtk_switch_portmask_P2L_get(pmsk, &logicalPmask)) != RT_ERR_OK)
376 return retVal;
377
378 if (logicalPmask.bits[0] & (1<<port))
379 {
380 pPortStatus->loop_leave = RTK_RLDP_LOOPSTS_LOOPING;
381 }
382 else
383 {
384 pPortStatus->loop_leave = RTK_RLDP_LOOPSTS_NONE;
385 }
386
387 return RT_ERR_OK;
388 }
389
390
391 /* Function Name:
392 * rtk_rldp_portStatus_clear
393 * Description:
394 * Clear RLDP module status
395 * Input:
396 * port - port number to be clear
397 * pPortStatus - per port status structure of RLDP
398 * Output:
399 * None
400 * Return:
401 * RT_ERR_OK
402 * RT_ERR_FAILED
403 * RT_ERR_INPUT
404 * RT_ERR_NULL_POINTER
405 * Note:
406 * Clear operation effect loop_enter and loop_leave only, other field in
407 * the structure are don't care. Loop status cab't be clean.
408 */
409 rtk_api_ret_t rtk_rldp_portStatus_set(rtk_port_t port, rtk_rldp_portStatus_t *pPortStatus)
410 {
411 rtk_api_ret_t retVal;
412 rtk_uint32 pmsk;
413
414 /* Check initialization state */
415 RTK_CHK_INIT_STATE();
416
417 /* Check Port Valid */
418 RTK_CHK_PORT_VALID(port);
419
420 pmsk = (pPortStatus->loop_enter)<<rtk_switch_port_L2P_get(port);
421 if ((retVal = rtl8367c_setAsicRldpEnterLoopedPortmask(pmsk))!=RT_ERR_OK)
422 return retVal;
423
424 pmsk = (pPortStatus->loop_leave)<<rtk_switch_port_L2P_get(port);
425 if ((retVal = rtl8367c_setAsicRldpLeaveLoopedPortmask(pmsk))!=RT_ERR_OK)
426 return retVal;
427
428 return RT_ERR_OK;
429 }
430
431 /* Function Name:
432 * rtk_rldp_portLoopPair_get
433 * Description:
434 * Get RLDP port loop pairs
435 * Input:
436 * port - port number to be get
437 * Output:
438 * pPortmask - per port related loop ports
439 * Return:
440 * RT_ERR_OK
441 * RT_ERR_FAILED
442 * RT_ERR_INPUT
443 * RT_ERR_NULL_POINTER
444 * Note:
445 * None
446 */
447 rtk_api_ret_t rtk_rldp_portLoopPair_get(rtk_port_t port, rtk_portmask_t *pPortmask)
448 {
449 rtk_api_ret_t retVal;
450 rtk_uint32 pmsk;
451
452 /* Check initialization state */
453 RTK_CHK_INIT_STATE();
454
455 /* Check Port Valid */
456 RTK_CHK_PORT_VALID(port);
457
458 if ((retVal = rtl8367c_getAsicRldpLoopedPortPair(rtk_switch_port_L2P_get(port), &pmsk))!=RT_ERR_OK)
459 return retVal;
460
461 if ((retVal = rtk_switch_portmask_P2L_get(pmsk, pPortmask)) != RT_ERR_OK)
462 return retVal;
463
464 return RT_ERR_OK;
465 }
466
467
468