|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 BayLibre, SAS |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#define DT_DRV_COMPAT ti_cc23x0_rtc |
| 8 | + |
| 9 | +#include <zephyr/device.h> |
| 10 | +#include <zephyr/drivers/counter.h> |
| 11 | +#include <zephyr/spinlock.h> |
| 12 | +#include <zephyr/kernel.h> |
| 13 | +#include <zephyr/drivers/clock_control.h> |
| 14 | +#include <zephyr/logging/log.h> |
| 15 | + |
| 16 | +#include <inc/hw_rtc.h> |
| 17 | +#include <inc/hw_types.h> |
| 18 | +#include <inc/hw_evtsvt.h> |
| 19 | +#include <inc/hw_memmap.h> |
| 20 | + |
| 21 | +LOG_MODULE_REGISTER(cc23x0_counter_rtc, CONFIG_COUNTER_LOG_LEVEL); |
| 22 | + |
| 23 | +static void counter_cc23x0_isr(const struct device *dev); |
| 24 | + |
| 25 | +struct counter_cc23x0_config { |
| 26 | + struct counter_config_info counter_info; |
| 27 | + uint32_t base; |
| 28 | +}; |
| 29 | + |
| 30 | +struct counter_cc23x0_data { |
| 31 | + struct counter_alarm_cfg alarm_cfg0; |
| 32 | +}; |
| 33 | + |
| 34 | +static int counter_cc23x0_get_value(const struct device *dev, uint32_t *ticks) |
| 35 | +{ |
| 36 | + /* Resolution is 8us and max timeout ~9.5h */ |
| 37 | + const struct counter_cc23x0_config *config = dev->config; |
| 38 | + |
| 39 | + *ticks = HWREG(config->base + RTC_O_TIME8U); |
| 40 | + |
| 41 | + return 0; |
| 42 | +} |
| 43 | + |
| 44 | +static int counter_cc23x0_get_value_64(const struct device *dev, uint64_t *ticks) |
| 45 | +{ |
| 46 | + const struct counter_cc23x0_config *config = dev->config; |
| 47 | + |
| 48 | + /* |
| 49 | + * RTC counter register is 67 bits, only part of the bits are accessible. |
| 50 | + * They are split in two partially overlapping registers: |
| 51 | + * TIME524M [50:19] |
| 52 | + * TIME8U [34:3] |
| 53 | + */ |
| 54 | + |
| 55 | + uint64_t rtc_time_now = |
| 56 | + ((HWREG(config->base + RTC_O_TIME524M) << 16) & 0xFFFFFFF800000000) | |
| 57 | + HWREG(config->base + RTC_O_TIME8U); |
| 58 | + |
| 59 | + *ticks = rtc_time_now; |
| 60 | + |
| 61 | + return 0; |
| 62 | +} |
| 63 | + |
| 64 | +static void counter_cc23x0_isr(const struct device *dev) |
| 65 | +{ |
| 66 | + const struct counter_cc23x0_config *config = dev->config; |
| 67 | + struct counter_cc23x0_data *data = dev->data; |
| 68 | + |
| 69 | + /* Clear RTC interrupt regs */ |
| 70 | + HWREG(config->base + RTC_O_ICLR) = 0x3; |
| 71 | + HWREG(config->base + RTC_O_IMCLR) = 0x3; |
| 72 | + |
| 73 | + uint32_t now = HWREG(config->base + RTC_O_TIME8U); |
| 74 | + |
| 75 | + if (data->alarm_cfg0.callback) { |
| 76 | + data->alarm_cfg0.callback(dev, 0, now, data->alarm_cfg0.user_data); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +static int counter_cc23x0_set_alarm(const struct device *dev, uint8_t chan_id, |
| 81 | + const struct counter_alarm_cfg *alarm_cfg) |
| 82 | +{ |
| 83 | + const struct counter_cc23x0_config *config = dev->config; |
| 84 | + struct counter_cc23x0_data *data = dev->data; |
| 85 | + |
| 86 | + /* RTC have resolutiuon of 8us */ |
| 87 | + if (counter_ticks_to_us(dev, alarm_cfg->ticks) <= 8) { |
| 88 | + return -ENOTSUP; |
| 89 | + } |
| 90 | + |
| 91 | + uint32_t now = HWREG(config->base + RTC_O_TIME8U); |
| 92 | + |
| 93 | + /* Calculate next alarm relative to current time in us */ |
| 94 | + uint32_t next_alarm = now + (counter_ticks_to_us(dev, alarm_cfg->ticks) / 8); |
| 95 | + |
| 96 | + HWREG(config->base + RTC_O_CH0CC8U) = next_alarm; |
| 97 | + HWREG(config->base + RTC_O_IMASK) = 0x1; |
| 98 | + HWREG(config->base + RTC_O_ARMSET) = 0x1; |
| 99 | + |
| 100 | + HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ3SEL) = EVTSVT_CPUIRQ16SEL_PUBID_AON_RTC_COMB; |
| 101 | + |
| 102 | + IRQ_CONNECT(DT_INST_IRQN(0), DT_INST_IRQ(0, priority), counter_cc23x0_isr, |
| 103 | + DEVICE_DT_INST_GET(0), 0); |
| 104 | + |
| 105 | + irq_enable(DT_INST_IRQN(0)); |
| 106 | + |
| 107 | + data->alarm_cfg0.flags = 0; |
| 108 | + data->alarm_cfg0.ticks = alarm_cfg->ticks; |
| 109 | + data->alarm_cfg0.callback = alarm_cfg->callback; |
| 110 | + data->alarm_cfg0.user_data = alarm_cfg->user_data; |
| 111 | + |
| 112 | + return 0; |
| 113 | +} |
| 114 | + |
| 115 | +static int counter_cc23x0_cancel_alarm(const struct device *dev, uint8_t chan_id) |
| 116 | +{ |
| 117 | + const struct counter_cc23x0_config *config = dev->config; |
| 118 | + |
| 119 | + /* Unset interrupt source */ |
| 120 | + HWREG(EVTSVT_BASE + EVTSVT_O_CPUIRQ3SEL) = 0x0; |
| 121 | + |
| 122 | + /* Unarm both channels */ |
| 123 | + HWREG(config->base + RTC_O_ARMCLR) = 0x3; |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +static int counter_cc23x0_set_top_value(const struct device *dev, const struct counter_top_cfg *cfg) |
| 129 | +{ |
| 130 | + return -ENOTSUP; |
| 131 | +} |
| 132 | + |
| 133 | +static uint32_t counter_cc23x0_get_pending_int(const struct device *dev) |
| 134 | +{ |
| 135 | + const struct counter_cc23x0_config *config = dev->config; |
| 136 | + struct counter_cc23x0_data *data = dev->data; |
| 137 | + |
| 138 | + /* Check interrupt and mask */ |
| 139 | + if (HWREG(config->base + RTC_O_RIS) & HWREG(config->base + RTC_O_MIS)) { |
| 140 | + /* Clear RTC interrupt regs */ |
| 141 | + HWREG(config->base + RTC_O_ICLR) = 0x3; |
| 142 | + HWREG(config->base + RTC_O_IMCLR) = 0x3; |
| 143 | + |
| 144 | + uint32_t now = HWREG(config->base + RTC_O_TIME8U); |
| 145 | + |
| 146 | + if (data->alarm_cfg0.callback) { |
| 147 | + data->alarm_cfg0.callback(dev, 0, now, data->alarm_cfg0.user_data); |
| 148 | + } |
| 149 | + |
| 150 | + return 0; |
| 151 | + } |
| 152 | + |
| 153 | + return -ESRCH; |
| 154 | +} |
| 155 | + |
| 156 | +static uint32_t counter_cc23x0_get_top_value(const struct device *dev) |
| 157 | +{ |
| 158 | + ARG_UNUSED(dev); |
| 159 | + |
| 160 | + return -ENOTSUP; |
| 161 | +} |
| 162 | + |
| 163 | +static uint32_t counter_cc23x0_get_freq(const struct device *dev) |
| 164 | +{ |
| 165 | + ARG_UNUSED(dev); |
| 166 | + |
| 167 | + /* |
| 168 | + * From TRM clock for RTC is 24Mhz handled internally |
| 169 | + * which is 1/2 from main 48Mhz clock = 24Mhz |
| 170 | + * Accessible for user resolution is 8us per bit |
| 171 | + * TIME8U [34:3] ~ 9.5h |
| 172 | + */ |
| 173 | + |
| 174 | + return (DT_PROP(DT_PATH(cpus, cpu_0), clock_frequency) / 2); |
| 175 | +} |
| 176 | + |
| 177 | +static int counter_cc23x0_start(const struct device *dev) |
| 178 | +{ |
| 179 | + ARG_UNUSED(dev); |
| 180 | + |
| 181 | + /* RTC timer runs after power-on reset */ |
| 182 | + |
| 183 | + return -ENOTSUP; |
| 184 | +} |
| 185 | + |
| 186 | +static int counter_cc23x0_stop(const struct device *dev) |
| 187 | +{ |
| 188 | + ARG_UNUSED(dev); |
| 189 | + |
| 190 | + /* Any reset/sleep mode, except for POR, will not stop or reset the RTC timer */ |
| 191 | + |
| 192 | + return -ENOTSUP; |
| 193 | +} |
| 194 | + |
| 195 | +static int counter_cc23x0_init(const struct device *dev) |
| 196 | +{ |
| 197 | + const struct counter_cc23x0_config *config = dev->config; |
| 198 | + |
| 199 | + /* Clear interrupt Mask */ |
| 200 | + HWREG(config->base + RTC_O_IMCLR) = 0x3; |
| 201 | + |
| 202 | + /* Clear Interrupt */ |
| 203 | + HWREG(config->base + RTC_O_ICLR) = 0x3; |
| 204 | + |
| 205 | + /* Clear Armed */ |
| 206 | + HWREG(config->base + RTC_O_ARMCLR) = 0x3; |
| 207 | + |
| 208 | + return 0; |
| 209 | +} |
| 210 | + |
| 211 | +static const struct counter_driver_api rtc_cc23x0_api = { |
| 212 | + .start = counter_cc23x0_start, |
| 213 | + .stop = counter_cc23x0_stop, |
| 214 | + .get_value = counter_cc23x0_get_value, |
| 215 | + .get_value_64 = counter_cc23x0_get_value_64, |
| 216 | + .set_alarm = counter_cc23x0_set_alarm, |
| 217 | + .cancel_alarm = counter_cc23x0_cancel_alarm, |
| 218 | + .get_top_value = counter_cc23x0_get_top_value, |
| 219 | + .set_top_value = counter_cc23x0_set_top_value, |
| 220 | + .get_pending_int = counter_cc23x0_get_pending_int, |
| 221 | + .get_freq = counter_cc23x0_get_freq, |
| 222 | +}; |
| 223 | + |
| 224 | +#define CC23X0_INIT(inst) \ |
| 225 | + static const struct counter_cc23x0_config cc23x0_config_##inst = { \ |
| 226 | + .counter_info = \ |
| 227 | + { \ |
| 228 | + .max_top_value = UINT32_MAX, \ |
| 229 | + .flags = COUNTER_CONFIG_INFO_COUNT_UP, \ |
| 230 | + .channels = 1, \ |
| 231 | + }, \ |
| 232 | + .base = DT_INST_REG_ADDR(inst), \ |
| 233 | + }; \ |
| 234 | + \ |
| 235 | + static struct counter_cc23x0_data cc23x0_data_##inst; \ |
| 236 | + \ |
| 237 | + DEVICE_DT_INST_DEFINE(0, &counter_cc23x0_init, NULL, &cc23x0_data_##inst, \ |
| 238 | + &cc23x0_config_##inst, POST_KERNEL, CONFIG_COUNTER_INIT_PRIORITY, \ |
| 239 | + &rtc_cc23x0_api); |
| 240 | + |
| 241 | +DT_INST_FOREACH_STATUS_OKAY(CC23X0_INIT) |
0 commit comments