2016-12-13 15:40:53 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016 Linaro Limited.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-25 08:41:26 -05:00
|
|
|
#define DT_DRV_COMPAT arm_cmsdk_dtimer
|
|
|
|
|
2019-06-25 15:53:48 -04:00
|
|
|
#include <drivers/counter.h>
|
2016-12-13 15:40:53 +00:00
|
|
|
#include <device.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <init.h>
|
|
|
|
#include <soc.h>
|
2020-01-25 05:34:53 -06:00
|
|
|
#include <drivers/clock_control/arm_clock_control.h>
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
#include "dualtimer_cmsdk_apb.h"
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
typedef void (*dtimer_config_func_t)(const struct device *dev);
|
2016-12-13 15:40:53 +00:00
|
|
|
|
2019-02-01 10:59:03 -06:00
|
|
|
struct dtmr_cmsdk_apb_cfg {
|
|
|
|
struct counter_config_info info;
|
2016-12-13 15:40:53 +00:00
|
|
|
volatile struct dualtimer_cmsdk_apb *dtimer;
|
|
|
|
dtimer_config_func_t dtimer_config_func;
|
|
|
|
/* Dualtimer Clock control in Active State */
|
|
|
|
const struct arm_clock_control_t dtimer_cc_as;
|
|
|
|
/* Dualtimer Clock control in Sleep State */
|
|
|
|
const struct arm_clock_control_t dtimer_cc_ss;
|
|
|
|
/* Dualtimer Clock control in Deep Sleep State */
|
|
|
|
const struct arm_clock_control_t dtimer_cc_dss;
|
|
|
|
};
|
|
|
|
|
2019-02-01 10:59:03 -06:00
|
|
|
struct dtmr_cmsdk_apb_dev_data {
|
|
|
|
counter_top_callback_t top_callback;
|
|
|
|
void *top_user_data;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t load;
|
2016-12-13 15:40:53 +00:00
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int dtmr_cmsdk_apb_start(const struct device *dev)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct dtmr_cmsdk_apb_dev_data *data = dev->data;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
2019-02-01 10:59:03 -06:00
|
|
|
/* Set the timer reload to count */
|
|
|
|
cfg->dtimer->timer1load = data->load;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
/* Enable the dualtimer in 32 bit mode */
|
|
|
|
cfg->dtimer->timer1ctrl = (DUALTIMER_CTRL_EN | DUALTIMER_CTRL_SIZE_32);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int dtmr_cmsdk_apb_stop(const struct device *dev)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
/* Disable the dualtimer */
|
|
|
|
cfg->dtimer->timer1ctrl = 0x0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int dtmr_cmsdk_apb_get_value(const struct device *dev, uint32_t *ticks)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct dtmr_cmsdk_apb_dev_data *data = dev->data;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
2020-01-18 15:24:32 +01:00
|
|
|
*ticks = data->load - cfg->dtimer->timer1value;
|
|
|
|
return 0;
|
2016-12-13 15:40:53 +00:00
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int dtmr_cmsdk_apb_set_top_value(const struct device *dev,
|
2019-03-21 16:31:16 +01:00
|
|
|
const struct counter_top_cfg *top_cfg)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2020-05-28 21:23:02 +02:00
|
|
|
struct dtmr_cmsdk_apb_dev_data *data = dev->data;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
2019-03-21 16:31:16 +01:00
|
|
|
data->top_callback = top_cfg->callback;
|
|
|
|
data->top_user_data = top_cfg->user_data;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
/* Store the reload value */
|
2019-03-21 16:31:16 +01:00
|
|
|
data->load = top_cfg->ticks;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
/* Set the timer to count */
|
2019-03-21 16:31:16 +01:00
|
|
|
if (top_cfg->flags & COUNTER_TOP_CFG_DONT_RESET) {
|
|
|
|
/*
|
|
|
|
* Write to background load register will not affect
|
|
|
|
* the current value of the counter.
|
|
|
|
*/
|
|
|
|
cfg->dtimer->timer1bgload = top_cfg->ticks;
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Write to load register will also set
|
|
|
|
* the current value of the counter.
|
|
|
|
*/
|
|
|
|
cfg->dtimer->timer1load = top_cfg->ticks;
|
|
|
|
}
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
/* Enable IRQ */
|
|
|
|
cfg->dtimer->timer1ctrl |= (DUALTIMER_CTRL_INTEN
|
|
|
|
| DUALTIMER_CTRL_MODE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t dtmr_cmsdk_apb_get_top_value(const struct device *dev)
|
2019-02-01 10:59:03 -06:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct dtmr_cmsdk_apb_dev_data *data = dev->data;
|
2019-02-01 10:59:03 -06:00
|
|
|
|
2020-05-27 11:26:57 -05:00
|
|
|
uint32_t ticks = data->load;
|
2019-02-01 10:59:03 -06:00
|
|
|
|
|
|
|
return ticks;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static uint32_t dtmr_cmsdk_apb_get_pending_int(const struct device *dev)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
return cfg->dtimer->timer1ris;
|
|
|
|
}
|
|
|
|
|
2019-02-01 10:59:03 -06:00
|
|
|
static const struct counter_driver_api dtmr_cmsdk_apb_api = {
|
|
|
|
.start = dtmr_cmsdk_apb_start,
|
|
|
|
.stop = dtmr_cmsdk_apb_stop,
|
2020-01-18 15:24:32 +01:00
|
|
|
.get_value = dtmr_cmsdk_apb_get_value,
|
2019-02-01 10:59:03 -06:00
|
|
|
.set_top_value = dtmr_cmsdk_apb_set_top_value,
|
|
|
|
.get_pending_int = dtmr_cmsdk_apb_get_pending_int,
|
|
|
|
.get_top_value = dtmr_cmsdk_apb_get_top_value,
|
2016-12-13 15:40:53 +00:00
|
|
|
};
|
|
|
|
|
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 14:58:56 +02:00
|
|
|
static void dtmr_cmsdk_apb_isr(const struct device *dev)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 21:23:02 +02:00
|
|
|
struct dtmr_cmsdk_apb_dev_data *data = dev->data;
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
cfg->dtimer->timer1intclr = DUALTIMER_INTCLR;
|
2019-02-01 10:59:03 -06:00
|
|
|
if (data->top_callback) {
|
|
|
|
data->top_callback(dev, data->top_user_data);
|
2016-12-13 15:40:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int dtmr_cmsdk_apb_init(const struct device *dev)
|
2016-12-13 15:40:53 +00:00
|
|
|
{
|
2020-05-28 20:44:16 +02:00
|
|
|
const struct dtmr_cmsdk_apb_cfg * const cfg = dev->config;
|
2016-12-13 15:40:53 +00:00
|
|
|
|
|
|
|
#ifdef CONFIG_CLOCK_CONTROL
|
|
|
|
/* Enable clock for subsystem */
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *clk =
|
2016-12-13 15:40:53 +00:00
|
|
|
device_get_binding(CONFIG_ARM_CLOCK_CONTROL_DEV_NAME);
|
|
|
|
|
|
|
|
#ifdef CONFIG_SOC_SERIES_BEETLE
|
|
|
|
clock_control_on(clk, (clock_control_subsys_t *) &cfg->dtimer_cc_as);
|
|
|
|
clock_control_on(clk, (clock_control_subsys_t *) &cfg->dtimer_cc_ss);
|
|
|
|
clock_control_on(clk, (clock_control_subsys_t *) &cfg->dtimer_cc_dss);
|
|
|
|
#endif /* CONFIG_SOC_SERIES_BEETLE */
|
|
|
|
#endif /* CONFIG_CLOCK_CONTROL */
|
|
|
|
|
|
|
|
cfg->dtimer_config_func(dev);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-05-05 16:06:32 -07:00
|
|
|
#define DTIMER_CMSDK_REG(inst) \
|
|
|
|
((volatile struct dualtimer_cmsdk_apb *)DT_INST_REG_ADDR(inst))
|
|
|
|
|
|
|
|
#define DTIMER_CMSDK_INIT(inst) \
|
2020-07-14 17:02:00 +02:00
|
|
|
static void dtimer_cmsdk_apb_config_##inst(const struct device *dev); \
|
2020-05-05 16:06:32 -07:00
|
|
|
\
|
|
|
|
static const struct dtmr_cmsdk_apb_cfg \
|
|
|
|
dtmr_cmsdk_apb_cfg_##inst = { \
|
|
|
|
.info = { \
|
|
|
|
.max_top_value = UINT32_MAX, \
|
|
|
|
.freq = 24000000U, \
|
|
|
|
.flags = 0, \
|
|
|
|
.channels = 0U, \
|
|
|
|
}, \
|
|
|
|
.dtimer = DTIMER_CMSDK_REG(inst), \
|
|
|
|
.dtimer_config_func = dtimer_cmsdk_apb_config_##inst, \
|
|
|
|
.dtimer_cc_as = {.bus = CMSDK_APB, \
|
|
|
|
.state = SOC_ACTIVE, \
|
|
|
|
.device = DT_INST_REG_ADDR(inst),}, \
|
|
|
|
.dtimer_cc_ss = {.bus = CMSDK_APB, \
|
|
|
|
.state = SOC_SLEEP, \
|
|
|
|
.device = DT_INST_REG_ADDR(inst),}, \
|
|
|
|
.dtimer_cc_dss = {.bus = CMSDK_APB, \
|
|
|
|
.state = SOC_DEEPSLEEP, \
|
|
|
|
.device = DT_INST_REG_ADDR(inst),}, \
|
|
|
|
}; \
|
|
|
|
\
|
|
|
|
static struct dtmr_cmsdk_apb_dev_data \
|
|
|
|
dtmr_cmsdk_apb_dev_data_##inst = { \
|
|
|
|
.load = UINT_MAX, \
|
|
|
|
}; \
|
|
|
|
\
|
2020-12-15 12:58:44 -06:00
|
|
|
DEVICE_DT_INST_DEFINE(inst, \
|
2020-05-05 16:06:32 -07:00
|
|
|
dtmr_cmsdk_apb_init, \
|
2021-04-28 10:23:42 +02:00
|
|
|
NULL, \
|
2020-05-05 16:06:32 -07:00
|
|
|
&dtmr_cmsdk_apb_dev_data_##inst, \
|
|
|
|
&dtmr_cmsdk_apb_cfg_##inst, POST_KERNEL, \
|
2021-10-20 15:15:14 -05:00
|
|
|
CONFIG_COUNTER_INIT_PRIORITY, \
|
2020-05-05 16:06:32 -07:00
|
|
|
&dtmr_cmsdk_apb_api); \
|
|
|
|
\
|
2020-07-14 17:02:00 +02:00
|
|
|
static void dtimer_cmsdk_apb_config_##inst(const struct device *dev) \
|
2020-05-05 16:06:32 -07:00
|
|
|
{ \
|
|
|
|
IRQ_CONNECT(DT_INST_IRQN(inst), \
|
|
|
|
DT_INST_IRQ(inst, priority), \
|
|
|
|
dtmr_cmsdk_apb_isr, \
|
2020-12-15 12:58:44 -06:00
|
|
|
DEVICE_DT_INST_GET(inst), \
|
2020-05-05 16:06:32 -07:00
|
|
|
0); \
|
|
|
|
irq_enable(DT_INST_IRQN(inst)); \
|
|
|
|
}
|
2016-12-13 15:40:53 +00:00
|
|
|
|
2020-05-06 11:23:07 -07:00
|
|
|
DT_INST_FOREACH_STATUS_OKAY(DTIMER_CMSDK_INIT)
|