|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Renesas Electronics Corporation |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +#include <zephyr/device.h> |
| 7 | +#include <zephyr/spinlock.h> |
| 8 | +#include <zephyr/drivers/timer/system_timer.h> |
| 9 | +#include <zephyr/irq.h> |
| 10 | +#include <zephyr/sys_clock.h> |
| 11 | +#include <zephyr/logging/log.h> |
| 12 | +#include <instances/rzg/r_gtm.h> |
| 13 | + |
| 14 | +LOG_MODULE_REGISTER(renesas_rz_gtm_timer); |
| 15 | + |
| 16 | +#define DT_DRV_COMPAT renesas_rz_gtm_os_timer |
| 17 | +#define TIMER_NODE DT_INST_PARENT(0) |
| 18 | + |
| 19 | +#define cycle_diff_t uint32_t |
| 20 | +#define CYCLE_DIFF_MAX (~(cycle_diff_t)0) |
| 21 | + |
| 22 | +/* |
| 23 | + * We have two constraints on the maximum number of cycles we can wait for. |
| 24 | + * |
| 25 | + * 1) sys_clock_announce() accepts at most INT32_MAX ticks. |
| 26 | + * |
| 27 | + * 2) The number of cycles between two reports must fit in a cycle_diff_t |
| 28 | + * variable before converting it to ticks. |
| 29 | + * |
| 30 | + * Then: |
| 31 | + * |
| 32 | + * 3) Pick the smallest between (1) and (2). |
| 33 | + * |
| 34 | + * 4) Take into account some room for the unavoidable IRQ servicing latency. |
| 35 | + * Let's use 3/4 of the max range. |
| 36 | + * |
| 37 | + * Finally let's add the LSB value to the result so to clear out a bunch of |
| 38 | + * consecutive set bits coming from the original max values to produce a |
| 39 | + * nicer literal for assembly generation. |
| 40 | + */ |
| 41 | +#define CYCLES_MAX_1 ((uint64_t)INT32_MAX * (uint64_t)CYC_PER_TICK) |
| 42 | +#define CYCLES_MAX_2 ((uint64_t)CYCLE_DIFF_MAX) |
| 43 | +#define CYCLES_MAX_3 MIN(CYCLES_MAX_1, CYCLES_MAX_2) |
| 44 | +#define CYCLES_MAX_4 (CYCLES_MAX_3 / 2 + CYCLES_MAX_3 / 4) |
| 45 | +#define CYCLES_MAX_5 (CYCLES_MAX_4 + LSB_GET(CYCLES_MAX_4)) |
| 46 | + |
| 47 | +/* precompute CYCLES_MAX and CYC_PER_TICK at driver init to avoid runtime double divisions */ |
| 48 | +static uint64_t cycles_max; |
| 49 | +static uint32_t cyc_per_tick; |
| 50 | +#define CYCLES_MAX cycles_max |
| 51 | +#define CYC_PER_TICK cyc_per_tick |
| 52 | + |
| 53 | +static void ostm_irq_handler(timer_callback_args_t *arg); |
| 54 | +void gtm_int_isr(void); |
| 55 | +const struct device *g_os_timer_dev = DEVICE_DT_INST_GET(0); |
| 56 | +extern unsigned int z_clock_hw_cycles_per_sec; |
| 57 | + |
| 58 | +struct rz_os_timer_config { |
| 59 | + timer_cfg_t *fsp_cfg; |
| 60 | + const timer_api_t *fsp_api; |
| 61 | +}; |
| 62 | + |
| 63 | +struct rz_os_timer_data { |
| 64 | + timer_ctrl_t *fsp_ctrl; |
| 65 | + struct k_spinlock lock; |
| 66 | + uint32_t last_cycle; |
| 67 | + uint32_t last_tick; |
| 68 | + uint32_t last_elapsed; |
| 69 | +}; |
| 70 | + |
| 71 | +static void ostm_irq_handler(timer_callback_args_t *arg) |
| 72 | +{ |
| 73 | + ARG_UNUSED(arg); |
| 74 | + |
| 75 | + struct rz_os_timer_data *data = (struct rz_os_timer_data *)g_os_timer_dev->data; |
| 76 | + |
| 77 | + uint32_t delta_cycles = sys_clock_cycle_get_32() - data->last_cycle; |
| 78 | + uint32_t delta_ticks = delta_cycles / CYC_PER_TICK; |
| 79 | + |
| 80 | + data->last_cycle += delta_ticks * CYC_PER_TICK; |
| 81 | + data->last_tick += delta_ticks; |
| 82 | + data->last_elapsed = 0; |
| 83 | + |
| 84 | + if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) { |
| 85 | + struct rz_os_timer_config *config = |
| 86 | + (struct rz_os_timer_config *)g_os_timer_dev->config; |
| 87 | + uint32_t next_cycle = data->last_cycle + CYC_PER_TICK; |
| 88 | + |
| 89 | + config->fsp_api->periodSet(data->fsp_ctrl, next_cycle); |
| 90 | + } else { |
| 91 | + irq_disable(DT_IRQN(TIMER_NODE)); |
| 92 | + } |
| 93 | + |
| 94 | + /* Announce to the kernel */ |
| 95 | + sys_clock_announce(delta_ticks); |
| 96 | +} |
| 97 | + |
| 98 | +void sys_clock_set_timeout(int32_t ticks, bool idle) |
| 99 | +{ |
| 100 | + if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + if (idle && ticks == K_TICKS_FOREVER) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + struct rz_os_timer_config *config = (struct rz_os_timer_config *)g_os_timer_dev->config; |
| 109 | + struct rz_os_timer_data *data = (struct rz_os_timer_data *)g_os_timer_dev->data; |
| 110 | + uint32_t next_cycle; |
| 111 | + |
| 112 | + k_spinlock_key_t key = k_spin_lock(&data->lock); |
| 113 | + |
| 114 | + if (ticks == K_TICKS_FOREVER) { |
| 115 | + next_cycle = data->last_cycle + CYCLES_MAX; |
| 116 | + } else { |
| 117 | + next_cycle = (data->last_tick + data->last_elapsed + ticks) * CYC_PER_TICK; |
| 118 | + if ((next_cycle - data->last_cycle) > CYCLES_MAX) { |
| 119 | + next_cycle = data->last_cycle + CYCLES_MAX; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + config->fsp_api->periodSet(data->fsp_ctrl, next_cycle); |
| 124 | + irq_enable(DT_IRQN(TIMER_NODE)); |
| 125 | + |
| 126 | + k_spin_unlock(&data->lock, key); |
| 127 | +} |
| 128 | + |
| 129 | +uint32_t sys_clock_elapsed(void) |
| 130 | +{ |
| 131 | + if (!IS_ENABLED(CONFIG_TICKLESS_KERNEL)) { |
| 132 | + return 0; |
| 133 | + } |
| 134 | + |
| 135 | + struct rz_os_timer_data *data = (struct rz_os_timer_data *)g_os_timer_dev->data; |
| 136 | + uint32_t delta_cycles = sys_clock_cycle_get_32() - data->last_cycle; |
| 137 | + uint32_t delta_ticks = delta_cycles / CYC_PER_TICK; |
| 138 | + |
| 139 | + data->last_elapsed = delta_ticks; |
| 140 | + |
| 141 | + return delta_ticks; |
| 142 | +} |
| 143 | + |
| 144 | +void sys_clock_disable(void) |
| 145 | +{ |
| 146 | + struct rz_os_timer_config *config = (struct rz_os_timer_config *)g_os_timer_dev->config; |
| 147 | + struct rz_os_timer_data *data = (struct rz_os_timer_data *)g_os_timer_dev->data; |
| 148 | + |
| 149 | + config->fsp_api->close(data->fsp_ctrl); |
| 150 | +} |
| 151 | + |
| 152 | +uint32_t sys_clock_cycle_get_32(void) |
| 153 | +{ |
| 154 | + struct rz_os_timer_config *config = (struct rz_os_timer_config *)g_os_timer_dev->config; |
| 155 | + struct rz_os_timer_data *data = (struct rz_os_timer_data *)g_os_timer_dev->data; |
| 156 | + timer_status_t timer_status; |
| 157 | + k_spinlock_key_t key = k_spin_lock(&data->lock); |
| 158 | + |
| 159 | + config->fsp_api->statusGet(data->fsp_ctrl, &timer_status); |
| 160 | + k_spin_unlock(&data->lock, key); |
| 161 | + |
| 162 | + return timer_status.counter; |
| 163 | +} |
| 164 | + |
| 165 | +static int sys_clock_driver_init(void) |
| 166 | +{ |
| 167 | + fsp_err_t ret; |
| 168 | + struct rz_os_timer_config *config = (struct rz_os_timer_config *)g_os_timer_dev->config; |
| 169 | + struct rz_os_timer_data *data = (struct rz_os_timer_data *)g_os_timer_dev->data; |
| 170 | + |
| 171 | + IRQ_CONNECT(DT_IRQN(TIMER_NODE), DT_IRQ(TIMER_NODE, priority), gtm_int_isr, |
| 172 | + DEVICE_DT_INST_GET(0), 0); |
| 173 | + |
| 174 | + data->last_tick = 0; |
| 175 | + data->last_cycle = 0; |
| 176 | + z_clock_hw_cycles_per_sec = R_FSP_SystemClockHzGet(FSP_PRIV_CLOCK_P0CLK); |
| 177 | + cyc_per_tick = sys_clock_hw_cycles_per_sec() / CONFIG_SYS_CLOCK_TICKS_PER_SEC; |
| 178 | + cycles_max = CYCLES_MAX_5; |
| 179 | + config->fsp_cfg->period_counts = CYC_PER_TICK; |
| 180 | + ret = config->fsp_api->open(data->fsp_ctrl, config->fsp_cfg); |
| 181 | + if (ret != FSP_SUCCESS) { |
| 182 | + LOG_ERR("timer initialize failed"); |
| 183 | + return -EIO; |
| 184 | + } |
| 185 | + |
| 186 | + ret = config->fsp_api->start(data->fsp_ctrl); |
| 187 | + if (ret != FSP_SUCCESS) { |
| 188 | + LOG_ERR("timer start failed"); |
| 189 | + return -EIO; |
| 190 | + } |
| 191 | + |
| 192 | + return 0; |
| 193 | +} |
| 194 | + |
| 195 | +#define OS_TIMER_RZG_GTM_INIT() \ |
| 196 | + const gtm_extended_cfg_t g_timer0_extend = { \ |
| 197 | + .generate_interrupt_when_starts = GTM_GIWS_TYPE_DISABLED, \ |
| 198 | + .gtm_mode = GTM_TIMER_MODE_FREERUN, \ |
| 199 | + }; \ |
| 200 | + \ |
| 201 | + static timer_cfg_t g_timer0_cfg = { \ |
| 202 | + .mode = TIMER_MODE_PERIODIC, \ |
| 203 | + .period_counts = 0, \ |
| 204 | + .channel = DT_PROP(TIMER_NODE, channel), \ |
| 205 | + .p_callback = ostm_irq_handler, \ |
| 206 | + .p_context = DEVICE_DT_INST_GET(0), \ |
| 207 | + .p_extend = &g_timer0_extend, \ |
| 208 | + .cycle_end_ipl = DT_IRQ(TIMER_NODE, priority), \ |
| 209 | + .cycle_end_irq = DT_IRQN(TIMER_NODE), \ |
| 210 | + }; \ |
| 211 | + \ |
| 212 | + static gtm_instance_ctrl_t g_timer0_ctrl; \ |
| 213 | + \ |
| 214 | + static struct rz_os_timer_data g_rz_os_timer_data = { \ |
| 215 | + .fsp_ctrl = (timer_ctrl_t *)&g_timer0_ctrl, \ |
| 216 | + }; \ |
| 217 | + \ |
| 218 | + struct rz_os_timer_config g_rz_os_timer_config = { \ |
| 219 | + .fsp_cfg = &g_timer0_cfg, \ |
| 220 | + .fsp_api = &g_timer_on_gtm, \ |
| 221 | + }; \ |
| 222 | + \ |
| 223 | + DEVICE_DT_INST_DEFINE(0, NULL, NULL, &g_rz_os_timer_data, &g_rz_os_timer_config, \ |
| 224 | + PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY, NULL); \ |
| 225 | + \ |
| 226 | + SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY); |
| 227 | + |
| 228 | +OS_TIMER_RZG_GTM_INIT(); |
0 commit comments