-
Notifications
You must be signed in to change notification settings - Fork 7.8k
drivers: ptp_clock: Add NXP NETC PTP Clock Driver #89987
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
fabiobaltieri
merged 4 commits into
zephyrproject-rtos:main
from
nxp-upstream:netc_ptp_clock
Jun 6, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
817d05d
drivers: ptp_clock: Add NXP NETC PTP Clock Driver
ZhaoQiang-b45475 024308b
dts: bindings: Add NXP NETC PTP binding
ZhaoQiang-b45475 6ff18d7
dts: arm: nxp_imx95_m7: add ptp_clock node
ZhaoQiang-b45475 5d74645
west.yml: update hal_nxp
ZhaoQiang-b45475 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,7 @@ | |
&enetc_psi0 { | ||
status = "okay"; | ||
}; | ||
|
||
&enetc_ptp_clock { | ||
status = "okay"; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# Copyright 2025 NXP | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
config PTP_CLOCK_NXP_NETC | ||
bool "NXP NETC PTP Clock driver" | ||
default y | ||
depends on DT_HAS_NXP_NETC_PTP_CLOCK_ENABLED | ||
depends on ETH_NXP_IMX_NETC | ||
help | ||
Enable NXP NETC PTP clock support. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* Copyright 2025 NXP | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#define DT_DRV_COMPAT nxp_netc_ptp_clock | ||
|
||
#include <zephyr/drivers/ptp_clock.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/device.h> | ||
#include <zephyr/drivers/clock_control.h> | ||
#include <zephyr/drivers/pinctrl.h> | ||
|
||
#include <fsl_netc_timer.h> | ||
|
||
struct ptp_clock_nxp_netc_config { | ||
const struct device *dev; | ||
const struct device *clock_dev; | ||
clock_control_subsys_t clock_subsys; | ||
}; | ||
|
||
struct ptp_clock_nxp_netc_data { | ||
NETC_ENETC_Type *base; | ||
netc_timer_handle_t handle; | ||
struct k_mutex ptp_mutex; | ||
netc_timer_config_t ptp_config; | ||
}; | ||
|
||
static int ptp_clock_nxp_netc_set(const struct device *dev, | ||
struct net_ptp_time *tm) | ||
{ | ||
struct ptp_clock_nxp_netc_data *data = dev->data; | ||
uint64_t nanosecond; | ||
int key; | ||
|
||
nanosecond = tm->second * NSEC_PER_SEC + tm->nanosecond; | ||
|
||
key = irq_lock(); | ||
data->handle.hw.base->TMR_CNT_L = (uint32_t)nanosecond; | ||
data->handle.hw.base->TMR_CNT_H = (uint32_t)(nanosecond >> 32U); | ||
data->handle.hw.base->TMROFF_L = 0U; | ||
data->handle.hw.base->TMROFF_H = 0U; | ||
irq_unlock(key); | ||
|
||
return 0; | ||
} | ||
|
||
static int ptp_clock_nxp_netc_get(const struct device *dev, | ||
struct net_ptp_time *tm) | ||
{ | ||
struct ptp_clock_nxp_netc_data *data = dev->data; | ||
uint64_t nanosecond; | ||
|
||
NETC_TimerGetCurrentTime(&data->handle, &nanosecond); | ||
|
||
tm->second = nanosecond / NSEC_PER_SEC; | ||
tm->nanosecond = nanosecond % NSEC_PER_SEC; | ||
|
||
return 0; | ||
} | ||
|
||
static int ptp_clock_nxp_netc_adjust(const struct device *dev, | ||
int increment) | ||
{ | ||
struct ptp_clock_nxp_netc_data *data = dev->data; | ||
int key; | ||
|
||
key = irq_lock(); | ||
NETC_TimerAddOffset(&data->handle, (int64_t)increment * NSEC_PER_SEC); | ||
irq_unlock(key); | ||
|
||
return 0; | ||
|
||
} | ||
|
||
static int ptp_clock_nxp_netc_rate_adjust(const struct device *dev, | ||
double ratio) | ||
{ | ||
struct ptp_clock_nxp_netc_data *data = dev->data; | ||
netc_timer_config_t *ptp_config = &data->ptp_config; | ||
|
||
ptp_config->defaultPpb = (ratio - 1.0) * 1000000000LL; | ||
|
||
k_mutex_lock(&data->ptp_mutex, K_FOREVER); | ||
|
||
NETC_TimerAdjustFreq(&data->handle, ptp_config->defaultPpb); | ||
|
||
k_mutex_unlock(&data->ptp_mutex); | ||
|
||
return 0; | ||
} | ||
|
||
static int ptp_clock_nxp_netc_init(const struct device *dev) | ||
{ | ||
const struct ptp_clock_nxp_netc_config *config = dev->config; | ||
struct ptp_clock_nxp_netc_data *data = dev->data; | ||
netc_timer_config_t *ptp_config = &data->ptp_config; | ||
uint32_t netc_ref_pll_rate; | ||
int ret = 0; | ||
|
||
ret = clock_control_get_rate(config->clock_dev, config->clock_subsys, &netc_ref_pll_rate); | ||
if (ret) { | ||
return ret; | ||
} | ||
ptp_config->refClkHz = netc_ref_pll_rate/2; | ||
ptp_config->entryNum = 0U; | ||
ptp_config->defaultPpb = 0U; | ||
ptp_config->clockSelect = kNETC_TimerSystemClk; | ||
|
||
k_mutex_init(&data->ptp_mutex); | ||
|
||
ret = NETC_TimerInit(&data->handle, ptp_config); | ||
if (ret != kStatus_Success) { | ||
return ret; | ||
} | ||
|
||
NETC_TimerEnable(&data->handle, true); | ||
|
||
return ret; | ||
} | ||
|
||
|
||
static DEVICE_API(ptp_clock, ptp_clock_nxp_netc_api) = { | ||
.set = ptp_clock_nxp_netc_set, | ||
.get = ptp_clock_nxp_netc_get, | ||
.adjust = ptp_clock_nxp_netc_adjust, | ||
.rate_adjust = ptp_clock_nxp_netc_rate_adjust, | ||
}; | ||
|
||
#define PTP_CLOCK_NXP_NETC_INIT(n) \ | ||
static const struct ptp_clock_nxp_netc_config \ | ||
ptp_clock_nxp_netc_##n##_config = { \ | ||
.dev = DEVICE_DT_INST_GET(n), \ | ||
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \ | ||
.clock_subsys = (clock_control_subsys_t) \ | ||
DT_INST_CLOCKS_CELL(n, name), \ | ||
}; \ | ||
\ | ||
static struct ptp_clock_nxp_netc_data ptp_clock_nxp_netc_##n##_data; \ | ||
\ | ||
DEVICE_DT_INST_DEFINE(n, &ptp_clock_nxp_netc_init, NULL, \ | ||
&ptp_clock_nxp_netc_##n##_data, \ | ||
&ptp_clock_nxp_netc_##n##_config, \ | ||
POST_KERNEL, CONFIG_PTP_CLOCK_INIT_PRIORITY, \ | ||
&ptp_clock_nxp_netc_api); | ||
|
||
/* Only one instance supported right now */ | ||
DT_INST_FOREACH_STATUS_OKAY(PTP_CLOCK_NXP_NETC_INIT) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Copyright 2025 NXP | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
description: NXP NETC PTP (Precision Time Protocol) Clock | ||
|
||
compatible: "nxp,netc-ptp-clock" | ||
|
||
include: ["base.yaml"] | ||
|
||
properties: | ||
reg: | ||
required: true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.