2 * Copyright (C) 2013 Realtek Semiconductor Corp.
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
11 * $Date: 2013-05-20 16:59:23 +0800 (星期一, 20 五月 2013) $
13 * Purpose : RTK switch high-level API for RTL8367/RTL8367C
14 * Feature : Here is a list of all functions and variables in time module.
18 #include <rtk_switch.h>
19 #include <rtk_error.h>
23 #include <rtl8367c_asicdrv.h>
24 #include <rtl8367c_asicdrv_eav.h>
29 * PTP function initialization.
36 * RT_ERR_FAILED - Failed
37 * RT_ERR_SMI - SMI access error
39 * This API is used to initialize PTP status.
41 rtk_api_ret_t
rtk_ptp_init(void)
43 /* Check initialization state */
53 * Configure PTP mac address.
55 * mac - mac address to parser PTP packets.
60 * RT_ERR_FAILED - Failed
61 * RT_ERR_SMI - SMI access error
62 * RT_ERR_INPUT - Invalid input parameter.
66 rtk_api_ret_t
rtk_ptp_mac_set(rtk_mac_t mac
)
71 memcpy(sw_mac
.octet
, mac
.octet
, ETHER_ADDR_LEN
);
73 if((retVal
=rtl8367c_setAsicEavMacAddress(sw_mac
))!=RT_ERR_OK
)
82 * Get PTP mac address.
86 * pMac - mac address to parser PTP packets.
89 * RT_ERR_FAILED - Failed
90 * RT_ERR_SMI - SMI access error
91 * RT_ERR_INPUT - Invalid input parameter.
95 rtk_api_ret_t
rtk_ptp_mac_get(rtk_mac_t
*pMac
)
100 if((retVal
=rtl8367c_getAsicEavMacAddress(&sw_mac
))!=RT_ERR_OK
)
103 memcpy(pMac
->octet
, sw_mac
.octet
, ETHER_ADDR_LEN
);
111 * Configure PTP accepted outer & inner tag TPID.
113 * outerId - Ether type of S-tag frame parsing in PTP ports.
114 * innerId - Ether type of C-tag frame parsing in PTP ports.
119 * RT_ERR_FAILED - Failed
120 * RT_ERR_SMI - SMI access error
121 * RT_ERR_INPUT - Invalid input parameter.
125 rtk_api_ret_t
rtk_ptp_tpid_set(rtk_ptp_tpid_t outerId
, rtk_ptp_tpid_t innerId
)
127 rtk_api_ret_t retVal
;
129 /* Check initialization state */
130 RTK_CHK_INIT_STATE();
132 if ((outerId
>RTK_MAX_NUM_OF_TPID
) ||(innerId
>RTK_MAX_NUM_OF_TPID
))
135 if ((retVal
= rtl8367c_setAsicEavTpid(outerId
, innerId
)) != RT_ERR_OK
)
144 * Get PTP accepted outer & inner tag TPID.
148 * pOuterId - Ether type of S-tag frame parsing in PTP ports.
149 * pInnerId - Ether type of C-tag frame parsing in PTP ports.
152 * RT_ERR_FAILED - Failed
153 * RT_ERR_SMI - SMI access error
157 rtk_api_ret_t
rtk_ptp_tpid_get(rtk_ptp_tpid_t
*pOuterId
, rtk_ptp_tpid_t
*pInnerId
)
159 rtk_api_ret_t retVal
;
161 /* Check initialization state */
162 RTK_CHK_INIT_STATE();
164 if ((retVal
= rtl8367c_getAsicEavTpid(pOuterId
, pInnerId
)) != RT_ERR_OK
)
171 * rtk_ptp_refTime_set
173 * Set the reference time of the specified device.
175 * timeStamp - reference timestamp value
181 * RT_ERR_INPUT - invalid input parameter
187 rtk_api_ret_t
rtk_ptp_refTime_set(rtk_ptp_timeStamp_t timeStamp
)
189 rtk_api_ret_t retVal
;
191 /* Check initialization state */
192 RTK_CHK_INIT_STATE();
194 if (timeStamp
.nsec
> RTK_MAX_NUM_OF_NANO_SECOND
)
197 if ((retVal
= rtl8367c_setAsicEavSysTime(timeStamp
.sec
, timeStamp
.nsec
))!=RT_ERR_OK
)
204 * rtk_ptp_refTime_get
206 * Get the reference time of the specified device.
209 * pTimeStamp - pointer buffer of the reference time
213 * RT_ERR_UNIT_ID - invalid unit id
214 * RT_ERR_NOT_INIT - The module is not initial
215 * RT_ERR_NULL_POINTER - input parameter may be null pointer
221 rtk_api_ret_t
rtk_ptp_refTime_get(rtk_ptp_timeStamp_t
*pTimeStamp
)
223 rtk_api_ret_t retVal
;
225 /* Check initialization state */
226 RTK_CHK_INIT_STATE();
228 if ((retVal
= rtl8367c_getAsicEavSysTime(&pTimeStamp
->sec
, &pTimeStamp
->nsec
))!=RT_ERR_OK
)
235 * rtk_ptp_refTimeAdjust_set
237 * Adjust the reference time.
241 * timeStamp - reference timestamp value
247 * RT_ERR_UNIT_ID - invalid unit id
248 * RT_ERR_NOT_INIT - The module is not initial
249 * RT_ERR_INPUT - invalid input parameter
251 * sign=0 for positive adjustment, sign=1 for negative adjustment.
253 rtk_api_ret_t
rtk_ptp_refTimeAdjust_set(rtk_ptp_sys_adjust_t sign
, rtk_ptp_timeStamp_t timeStamp
)
255 rtk_api_ret_t retVal
;
257 /* Check initialization state */
258 RTK_CHK_INIT_STATE();
260 if (timeStamp
.nsec
> RTK_MAX_NUM_OF_NANO_SECOND
)
263 if ((retVal
= rtl8367c_setAsicEavSysTimeAdjust(sign
, timeStamp
.sec
, timeStamp
.nsec
))!=RT_ERR_OK
)
271 * rtk_ptp_refTimeEnable_set
273 * Set the enable state of reference time of the specified device.
281 * RT_ERR_INPUT - invalid input parameter
285 rtk_api_ret_t
rtk_ptp_refTimeEnable_set(rtk_enable_t enable
)
287 rtk_api_ret_t retVal
;
289 /* Check initialization state */
290 RTK_CHK_INIT_STATE();
292 if (enable
>= RTK_ENABLE_END
)
293 return RT_ERR_ENABLE
;
295 if ((retVal
= rtl8367c_setAsicEavSysTimeCtrl(enable
))!=RT_ERR_OK
)
302 * rtk_ptp_refTimeEnable_get
304 * Get the enable state of reference time of the specified device.
311 * RT_ERR_UNIT_ID - invalid unit id
312 * RT_ERR_NOT_INIT - The module is not initial
313 * RT_ERR_NULL_POINTER - input parameter may be null pointer
319 rtk_api_ret_t
rtk_ptp_refTimeEnable_get(rtk_enable_t
*pEnable
)
321 rtk_api_ret_t retVal
;
323 /* Check initialization state */
324 RTK_CHK_INIT_STATE();
326 if ((retVal
= rtl8367c_getAsicEavSysTimeCtrl(pEnable
))!=RT_ERR_OK
)
333 * rtk_ptp_portEnable_set
335 * Set PTP status of the specified port.
344 * RT_ERR_PORT - invalid port id
345 * RT_ERR_INPUT - invalid input parameter
349 rtk_api_ret_t
rtk_ptp_portEnable_set(rtk_port_t port
, rtk_enable_t enable
)
351 rtk_api_ret_t retVal
;
353 /* Check initialization state */
354 RTK_CHK_INIT_STATE();
356 /* Check port is PTP port */
357 RTK_CHK_PORT_IS_PTP(port
);
359 if (enable
>=RTK_ENABLE_END
)
362 if ((retVal
= rtl8367c_setAsicEavPortEnable(rtk_switch_port_L2P_get(port
), enable
)) != RT_ERR_OK
)
369 * rtk_ptp_portEnable_get
371 * Get PTP status of the specified port.
379 * RT_ERR_PORT - invalid port id
380 * RT_ERR_NULL_POINTER - input parameter may be null pointer
384 rtk_api_ret_t
rtk_ptp_portEnable_get(rtk_port_t port
, rtk_enable_t
*pEnable
)
386 rtk_api_ret_t retVal
;
388 /* Check initialization state */
389 RTK_CHK_INIT_STATE();
391 /* Check port is PTP port */
392 RTK_CHK_PORT_IS_PTP(port
);
394 if ((retVal
= rtl8367c_getAsicEavPortEnable(rtk_switch_port_L2P_get(port
), pEnable
)) != RT_ERR_OK
)
402 * rtk_ptp_portTimestamp_get
404 * Get PTP timstamp according to the PTP identifier on the dedicated port from the specified device.
408 * type - PTP message type
410 * pInfo - pointer buffer of sequence ID and timestamp
414 * RT_ERR_PORT_ID - invalid port id
415 * RT_ERR_INPUT - invalid input parameter
416 * RT_ERR_NULL_POINTER - input parameter may be null pointer
422 rtk_api_ret_t
rtk_ptp_portTimestamp_get( rtk_port_t port
, rtk_ptp_msgType_t type
, rtk_ptp_info_t
*pInfo
)
424 rtk_api_ret_t retVal
;
425 rtl8367c_ptp_time_stamp_t time
;
427 /* Check initialization state */
428 RTK_CHK_INIT_STATE();
430 /* Check port is PTP port */
431 RTK_CHK_PORT_IS_PTP(port
);
433 if ((retVal
= rtl8367c_getAsicEavPortTimeStamp(rtk_switch_port_L2P_get(port
), type
, &time
)) != RT_ERR_OK
)
436 pInfo
->sequenceId
= time
.sequence_id
;
437 pInfo
->timeStamp
.sec
= time
.second
;
438 pInfo
->timeStamp
.nsec
= time
.nano_second
;
444 * rtk_ptp_intControl_set
446 * Set PTP interrupt trigger status configuration.
448 * type - Interrupt type.
449 * enable - Interrupt status.
454 * RT_ERR_FAILED - Failed
455 * RT_ERR_SMI - SMI access error
456 * RT_ERR_INPUT - Invalid input parameters.
457 * RT_ERR_ENABLE - Invalid enable input.
459 * The API can set PTP interrupt status configuration.
460 * The interrupt trigger status is shown in the following:
461 * PTP_INT_TYPE_TX_SYNC = 0,
462 * PTP_INT_TYPE_TX_DELAY_REQ,
463 * PTP_INT_TYPE_TX_PDELAY_REQ,
464 * PTP_INT_TYPE_TX_PDELAY_RESP,
465 * PTP_INT_TYPE_RX_SYNC,
466 * PTP_INT_TYPE_RX_DELAY_REQ,
467 * PTP_INT_TYPE_RX_PDELAY_REQ,
468 * PTP_INT_TYPE_RX_PDELAY_RESP,
471 rtk_api_ret_t
rtk_ptp_intControl_set(rtk_ptp_intType_t type
, rtk_enable_t enable
)
473 rtk_api_ret_t retVal
;
476 /* Check initialization state */
477 RTK_CHK_INIT_STATE();
479 if (type
>=PTP_INT_TYPE_END
)
482 if (PTP_INT_TYPE_ALL
!=type
)
484 if ((retVal
= rtl8367c_getAsicEavInterruptMask(&mask
)) != RT_ERR_OK
)
487 if (ENABLED
== enable
)
488 mask
= mask
| (1<<type
);
489 else if (DISABLED
== enable
)
490 mask
= mask
& ~(1<<type
);
494 if ((retVal
= rtl8367c_setAsicEavInterruptMask(mask
)) != RT_ERR_OK
)
499 if (ENABLED
== enable
)
500 mask
= RTK_PTP_INTR_MASK
;
501 else if (DISABLED
== enable
)
506 if ((retVal
= rtl8367c_setAsicEavInterruptMask(mask
)) != RT_ERR_OK
)
514 * rtk_ptp_intControl_get
516 * Get PTP interrupt trigger status configuration.
518 * type - Interrupt type.
520 * pEnable - Interrupt status.
523 * RT_ERR_FAILED - Failed
524 * RT_ERR_SMI - SMI access error
525 * RT_ERR_INPUT - Invalid input parameters.
527 * The API can get interrupt status configuration.
528 * The interrupt trigger status is shown in the following:
529 * PTP_INT_TYPE_TX_SYNC = 0,
530 * PTP_INT_TYPE_TX_DELAY_REQ,
531 * PTP_INT_TYPE_TX_PDELAY_REQ,
532 * PTP_INT_TYPE_TX_PDELAY_RESP,
533 * PTP_INT_TYPE_RX_SYNC,
534 * PTP_INT_TYPE_RX_DELAY_REQ,
535 * PTP_INT_TYPE_RX_PDELAY_REQ,
536 * PTP_INT_TYPE_RX_PDELAY_RESP,
538 rtk_api_ret_t
rtk_ptp_intControl_get(rtk_ptp_intType_t type
, rtk_enable_t
*pEnable
)
540 rtk_api_ret_t retVal
;
543 /* Check initialization state */
544 RTK_CHK_INIT_STATE();
546 if (type
>=PTP_INT_TYPE_ALL
)
549 if ((retVal
= rtl8367c_getAsicEavInterruptMask(&mask
)) != RT_ERR_OK
)
552 if (0 == (mask
&(1<<type
)))
561 * rtk_ptp_intStatus_get
563 * Get PTP port interrupt trigger status.
565 * port - physical port
567 * pStatusMask - Interrupt status bit mask.
570 * RT_ERR_FAILED - Failed
571 * RT_ERR_SMI - SMI access error
572 * RT_ERR_INPUT - Invalid input parameters.
574 * The API can get interrupt trigger status when interrupt happened.
575 * The interrupt trigger status is shown in the following:
576 * - PORT 0 INT (value[0] (Bit0))
577 * - PORT 1 INT (value[0] (Bit1))
578 * - PORT 2 INT (value[0] (Bit2))
579 * - PORT 3 INT (value[0] (Bit3))
580 * - PORT 4 INT (value[0] (Bit4))
584 rtk_api_ret_t
rtk_ptp_intStatus_get(rtk_ptp_intStatus_t
*pStatusMask
)
586 rtk_api_ret_t retVal
;
588 /* Check initialization state */
589 RTK_CHK_INIT_STATE();
591 if(NULL
== pStatusMask
)
592 return RT_ERR_NULL_POINTER
;
594 if ((retVal
= rtl8367c_getAsicEavInterruptStatus(pStatusMask
)) != RT_ERR_OK
)
603 * rtk_ptp_portIntStatus_set
605 * Set PTP port interrupt trigger status to clean.
607 * port - physical port
608 * statusMask - Interrupt status bit mask.
613 * RT_ERR_FAILED - Failed
614 * RT_ERR_SMI - SMI access error
615 * RT_ERR_INPUT - Invalid input parameters.
617 * The API can clean interrupt trigger status when interrupt happened.
618 * The interrupt trigger status is shown in the following:
619 * - PTP_INT_TYPE_TX_SYNC (value[0] (Bit0))
620 * - PTP_INT_TYPE_TX_DELAY_REQ (value[0] (Bit1))
621 * - PTP_INT_TYPE_TX_PDELAY_REQ (value[0] (Bit2))
622 * - PTP_INT_TYPE_TX_PDELAY_RESP (value[0] (Bit3))
623 * - PTP_INT_TYPE_RX_SYNC (value[0] (Bit4))
624 * - PTP_INT_TYPE_RX_DELAY_REQ (value[0] (Bit5))
625 * - PTP_INT_TYPE_RX_PDELAY_REQ (value[0] (Bit6))
626 * - PTP_INT_TYPE_RX_PDELAY_RESP (value[0] (Bit7))
627 * The status will be cleared after execute this API.
629 rtk_api_ret_t
rtk_ptp_portIntStatus_set(rtk_port_t port
, rtk_ptp_intStatus_t statusMask
)
631 rtk_api_ret_t retVal
;
633 /* Check initialization state */
634 RTK_CHK_INIT_STATE();
636 /* Check port is PTP port */
637 RTK_CHK_PORT_IS_PTP(port
);
639 if ((retVal
= rtl8367c_setAsicEavPortInterruptStatus(rtk_switch_port_L2P_get(port
), statusMask
))!=RT_ERR_OK
)
646 * rtk_ptp_portIntStatus_get
648 * Get PTP port interrupt trigger status.
650 * port - physical port
652 * pStatusMask - Interrupt status bit mask.
655 * RT_ERR_FAILED - Failed
656 * RT_ERR_SMI - SMI access error
657 * RT_ERR_INPUT - Invalid input parameters.
659 * The API can get interrupt trigger status when interrupt happened.
660 * The interrupt trigger status is shown in the following:
661 * - PTP_INT_TYPE_TX_SYNC (value[0] (Bit0))
662 * - PTP_INT_TYPE_TX_DELAY_REQ (value[0] (Bit1))
663 * - PTP_INT_TYPE_TX_PDELAY_REQ (value[0] (Bit2))
664 * - PTP_INT_TYPE_TX_PDELAY_RESP (value[0] (Bit3))
665 * - PTP_INT_TYPE_RX_SYNC (value[0] (Bit4))
666 * - PTP_INT_TYPE_RX_DELAY_REQ (value[0] (Bit5))
667 * - PTP_INT_TYPE_RX_PDELAY_REQ (value[0] (Bit6))
668 * - PTP_INT_TYPE_RX_PDELAY_RESP (value[0] (Bit7))
671 rtk_api_ret_t
rtk_ptp_portIntStatus_get(rtk_port_t port
, rtk_ptp_intStatus_t
*pStatusMask
)
673 rtk_api_ret_t retVal
;
675 /* Check initialization state */
676 RTK_CHK_INIT_STATE();
678 /* Check port is PTP port */
679 RTK_CHK_PORT_IS_PTP(port
);
681 if(NULL
== pStatusMask
)
682 return RT_ERR_NULL_POINTER
;
684 if ((retVal
= rtl8367c_getAsicEavPortInterruptStatus(rtk_switch_port_L2P_get(port
), pStatusMask
)) != RT_ERR_OK
)
691 * rtk_ptp_portPtpTrap_set
693 * Set PTP packet trap of the specified port.
702 * RT_ERR_PORT - invalid port id
703 * RT_ERR_INPUT - invalid input parameter
707 rtk_api_ret_t
rtk_ptp_portTrap_set(rtk_port_t port
, rtk_enable_t enable
)
709 rtk_api_ret_t retVal
;
711 /* Check initialization state */
712 RTK_CHK_INIT_STATE();
714 /* Check Port Valid */
715 RTK_CHK_PORT_VALID(port
);
717 if (enable
>=RTK_ENABLE_END
)
720 if ((retVal
= rtl8367c_setAsicEavTrap(rtk_switch_port_L2P_get(port
), enable
)) != RT_ERR_OK
)
727 * rtk_ptp_portPtpEnable_get
729 * Get PTP packet trap of the specified port.
737 * RT_ERR_PORT - invalid port id
738 * RT_ERR_NULL_POINTER - input parameter may be null pointer
742 rtk_api_ret_t
rtk_ptp_portTrap_get(rtk_port_t port
, rtk_enable_t
*pEnable
)
744 rtk_api_ret_t retVal
;
746 /* Check initialization state */
747 RTK_CHK_INIT_STATE();
749 /* Check Port Valid */
750 RTK_CHK_PORT_VALID(port
);
752 if ((retVal
= rtl8367c_getAsicEavTrap(rtk_switch_port_L2P_get(port
), pEnable
)) != RT_ERR_OK
)