2019-03-21 22:48:45 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019, Linaro Limited.
|
2024-03-20 10:21:32 +08:00
|
|
|
* Copyright 2024 NXP
|
2019-03-21 22:48:45 +08:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 03:11:52 +08:00
|
|
|
#define DT_DRV_COMPAT nxp_imx_gpt
|
|
|
|
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/drivers/counter.h>
|
|
|
|
#include <zephyr/drivers/clock_control.h>
|
2022-10-04 21:33:53 +08:00
|
|
|
#include <zephyr/irq.h>
|
2019-03-21 22:48:45 +08:00
|
|
|
#include <fsl_gpt.h>
|
2022-05-06 16:25:46 +08:00
|
|
|
#include <zephyr/logging/log.h>
|
2023-05-02 17:24:50 +08:00
|
|
|
#include <zephyr/sys/barrier.h>
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
LOG_MODULE_REGISTER(mcux_gpt, CONFIG_COUNTER_LOG_LEVEL);
|
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
#define DEV_CFG(_dev) ((const struct mcux_gpt_config *)(_dev)->config)
|
|
|
|
#define DEV_DATA(_dev) ((struct mcux_gpt_data *)(_dev)->data)
|
|
|
|
|
2019-03-21 22:48:45 +08:00
|
|
|
struct mcux_gpt_config {
|
|
|
|
/* info must be first element */
|
|
|
|
struct counter_config_info info;
|
2024-03-20 10:21:32 +08:00
|
|
|
|
|
|
|
DEVICE_MMIO_NAMED_ROM(gpt_mmio);
|
|
|
|
|
2021-02-12 09:24:37 +08:00
|
|
|
const struct device *clock_dev;
|
2020-12-09 16:35:47 +08:00
|
|
|
clock_control_subsys_t clock_subsys;
|
2019-03-21 22:48:45 +08:00
|
|
|
clock_name_t clock_source;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mcux_gpt_data {
|
2024-03-20 10:21:32 +08:00
|
|
|
DEVICE_MMIO_NAMED_RAM(gpt_mmio);
|
2019-03-21 22:48:45 +08:00
|
|
|
counter_alarm_callback_t alarm_callback;
|
|
|
|
counter_top_callback_t top_callback;
|
|
|
|
void *alarm_user_data;
|
|
|
|
void *top_user_data;
|
|
|
|
};
|
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
static GPT_Type *get_base(const struct device *dev)
|
|
|
|
{
|
|
|
|
return (GPT_Type *)DEVICE_MMIO_NAMED_GET(dev, gpt_mmio);
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_start(const struct device *dev)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_StartTimer(base);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_stop(const struct device *dev)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_StopTimer(base);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_get_value(const struct device *dev, uint32_t *ticks)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
*ticks = GPT_GetCurrentTimerCount(base);
|
2020-01-18 22:24:32 +08:00
|
|
|
return 0;
|
2019-03-21 22:48:45 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_set_alarm(const struct device *dev, uint8_t chan_id,
|
2019-03-21 22:48:45 +08:00
|
|
|
const struct counter_alarm_cfg *alarm_cfg)
|
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2020-05-29 03:23:02 +08:00
|
|
|
struct mcux_gpt_data *data = dev->data;
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
uint32_t current = GPT_GetCurrentTimerCount(base);
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t ticks = alarm_cfg->ticks;
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
if (chan_id != 0) {
|
|
|
|
LOG_ERR("Invalid channel id");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:46:19 +08:00
|
|
|
if ((alarm_cfg->flags & COUNTER_ALARM_CFG_ABSOLUTE) == 0) {
|
2019-03-21 22:48:45 +08:00
|
|
|
ticks += current;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data->alarm_callback) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->alarm_callback = alarm_cfg->callback;
|
|
|
|
data->alarm_user_data = alarm_cfg->user_data;
|
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_SetOutputCompareValue(base, kGPT_OutputCompare_Channel1,
|
2019-03-21 22:48:45 +08:00
|
|
|
ticks);
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_EnableInterrupts(base, kGPT_OutputCompare1InterruptEnable);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_cancel_alarm(const struct device *dev, uint8_t chan_id)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2020-05-29 03:23:02 +08:00
|
|
|
struct mcux_gpt_data *data = dev->data;
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
if (chan_id != 0) {
|
|
|
|
LOG_ERR("Invalid channel id");
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_DisableInterrupts(base, kGPT_OutputCompare1InterruptEnable);
|
2019-03-21 22:48:45 +08:00
|
|
|
data->alarm_callback = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 20:58:56 +08:00
|
|
|
void mcux_gpt_isr(const struct device *dev)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2020-05-29 03:23:02 +08:00
|
|
|
struct mcux_gpt_data *data = dev->data;
|
2024-03-20 10:21:32 +08:00
|
|
|
uint32_t current = GPT_GetCurrentTimerCount(base);
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t status;
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
status = GPT_GetStatusFlags(base, kGPT_OutputCompare1Flag |
|
2019-03-21 22:48:45 +08:00
|
|
|
kGPT_RollOverFlag);
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_ClearStatusFlags(base, status);
|
2023-05-02 17:24:50 +08:00
|
|
|
barrier_dsync_fence_full();
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
if ((status & kGPT_OutputCompare1Flag) && data->alarm_callback) {
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_DisableInterrupts(base,
|
2019-03-21 22:48:45 +08:00
|
|
|
kGPT_OutputCompare1InterruptEnable);
|
|
|
|
counter_alarm_callback_t alarm_cb = data->alarm_callback;
|
|
|
|
data->alarm_callback = NULL;
|
|
|
|
alarm_cb(dev, 0, current, data->alarm_user_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((status & kGPT_RollOverFlag) && data->top_callback) {
|
|
|
|
data->top_callback(dev, data->top_user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static uint32_t mcux_gpt_get_pending_int(const struct device *dev)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
return GPT_GetStatusFlags(base, kGPT_OutputCompare1Flag);
|
2019-03-21 22:48:45 +08:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_set_top_value(const struct device *dev,
|
2019-03-21 22:48:45 +08:00
|
|
|
const struct counter_top_cfg *cfg)
|
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct mcux_gpt_config *config = dev->config;
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base = get_base(dev);
|
2020-05-29 03:23:02 +08:00
|
|
|
struct mcux_gpt_data *data = dev->data;
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
if (cfg->ticks != config->info.max_top_value) {
|
|
|
|
LOG_ERR("Wrap can only be set to 0x%x",
|
|
|
|
config->info.max_top_value);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
data->top_callback = cfg->callback;
|
|
|
|
data->top_user_data = cfg->user_data;
|
|
|
|
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_EnableInterrupts(base, kGPT_RollOverFlagInterruptEnable);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static uint32_t mcux_gpt_get_top_value(const struct device *dev)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct mcux_gpt_config *config = dev->config;
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
return config->info.max_top_value;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:33:38 +08:00
|
|
|
static int mcux_gpt_init(const struct device *dev)
|
2019-03-21 22:48:45 +08:00
|
|
|
{
|
2020-05-29 02:44:16 +08:00
|
|
|
const struct mcux_gpt_config *config = dev->config;
|
2019-03-21 22:48:45 +08:00
|
|
|
gpt_config_t gptConfig;
|
2020-05-28 00:26:57 +08:00
|
|
|
uint32_t clock_freq;
|
2024-03-20 10:21:32 +08:00
|
|
|
GPT_Type *base;
|
|
|
|
|
|
|
|
DEVICE_MMIO_NAMED_MAP(dev, gpt_mmio, K_MEM_CACHE_NONE | K_MEM_DIRECT_MAP);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
2022-08-08 19:32:12 +08:00
|
|
|
if (!device_is_ready(config->clock_dev)) {
|
|
|
|
LOG_ERR("clock control device not ready");
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
2021-02-12 09:24:37 +08:00
|
|
|
if (clock_control_get_rate(config->clock_dev, config->clock_subsys,
|
2020-12-09 16:35:47 +08:00
|
|
|
&clock_freq)) {
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
2019-03-21 22:48:45 +08:00
|
|
|
/* Adjust divider to match expected freq */
|
|
|
|
if (clock_freq % config->info.freq) {
|
|
|
|
LOG_ERR("Cannot Adjust GPT freq to %u\n", config->info.freq);
|
2020-12-09 16:35:47 +08:00
|
|
|
LOG_ERR("clock src is %u\n", clock_freq);
|
2019-03-21 22:48:45 +08:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
GPT_GetDefaultConfig(&gptConfig);
|
|
|
|
gptConfig.enableFreeRun = true; /* Do not reset on compare */
|
|
|
|
gptConfig.clockSource = kGPT_ClockSource_Periph;
|
|
|
|
gptConfig.divider = clock_freq / config->info.freq;
|
2024-03-20 10:21:32 +08:00
|
|
|
base = get_base(dev);
|
|
|
|
GPT_Init(base, &gptConfig);
|
2019-03-21 22:48:45 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const struct counter_driver_api mcux_gpt_driver_api = {
|
|
|
|
.start = mcux_gpt_start,
|
|
|
|
.stop = mcux_gpt_stop,
|
2020-01-18 22:24:32 +08:00
|
|
|
.get_value = mcux_gpt_get_value,
|
2019-03-21 22:48:45 +08:00
|
|
|
.set_alarm = mcux_gpt_set_alarm,
|
|
|
|
.cancel_alarm = mcux_gpt_cancel_alarm,
|
|
|
|
.set_top_value = mcux_gpt_set_top_value,
|
|
|
|
.get_pending_int = mcux_gpt_get_pending_int,
|
|
|
|
.get_top_value = mcux_gpt_get_top_value,
|
|
|
|
};
|
|
|
|
|
2020-05-06 07:06:32 +08:00
|
|
|
#define GPT_DEVICE_INIT_MCUX(n) \
|
|
|
|
static struct mcux_gpt_data mcux_gpt_data_ ## n; \
|
|
|
|
\
|
|
|
|
static const struct mcux_gpt_config mcux_gpt_config_ ## n = { \
|
2024-03-20 10:21:32 +08:00
|
|
|
DEVICE_MMIO_NAMED_ROM_INIT(gpt_mmio, DT_DRV_INST(n)), \
|
2021-02-12 09:24:37 +08:00
|
|
|
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(n)), \
|
2020-12-09 16:35:47 +08:00
|
|
|
.clock_subsys = \
|
|
|
|
(clock_control_subsys_t)DT_INST_CLOCKS_CELL(n, name),\
|
2020-05-06 07:06:32 +08:00
|
|
|
.info = { \
|
|
|
|
.max_top_value = UINT32_MAX, \
|
2020-12-09 16:35:47 +08:00
|
|
|
.freq = DT_INST_PROP(n, gptfreq), \
|
2020-05-06 07:06:32 +08:00
|
|
|
.channels = 1, \
|
|
|
|
.flags = COUNTER_CONFIG_INFO_COUNT_UP, \
|
|
|
|
}, \
|
|
|
|
}; \
|
|
|
|
\
|
2020-07-14 23:02:00 +08:00
|
|
|
static int mcux_gpt_## n ##_init(const struct device *dev); \
|
2020-12-16 02:58:44 +08:00
|
|
|
DEVICE_DT_INST_DEFINE(n, \
|
2020-05-06 07:06:32 +08:00
|
|
|
mcux_gpt_## n ##_init, \
|
2021-04-28 16:23:42 +08:00
|
|
|
NULL, \
|
2020-05-06 07:06:32 +08:00
|
|
|
&mcux_gpt_data_ ## n, \
|
|
|
|
&mcux_gpt_config_ ## n, \
|
|
|
|
POST_KERNEL, \
|
2021-10-21 04:15:14 +08:00
|
|
|
CONFIG_COUNTER_INIT_PRIORITY, \
|
2020-05-07 14:46:04 +08:00
|
|
|
&mcux_gpt_driver_api); \
|
|
|
|
\
|
2020-07-14 23:02:00 +08:00
|
|
|
static int mcux_gpt_## n ##_init(const struct device *dev) \
|
2020-05-07 14:46:04 +08:00
|
|
|
{ \
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(n), \
|
|
|
|
DT_INST_IRQ(n, priority), \
|
2020-12-16 02:58:44 +08:00
|
|
|
mcux_gpt_isr, DEVICE_DT_INST_GET(n), 0); \
|
2020-05-07 14:46:04 +08:00
|
|
|
irq_enable(DT_INST_IRQN(n)); \
|
|
|
|
return mcux_gpt_init(dev); \
|
|
|
|
} \
|
2020-05-06 07:06:32 +08:00
|
|
|
|
2020-05-07 02:23:07 +08:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(GPT_DEVICE_INIT_MCUX)
|