-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathdrv_rtc.c
More file actions
486 lines (426 loc) · 13.1 KB
/
drv_rtc.c
File metadata and controls
486 lines (426 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/* Copyright (c) 2023, Canaan Bright Sight Co., Ltd
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 2006-2025 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <rtthread.h>
#include <rthw.h>
#include <rtdevice.h>
#include <riscv_io.h>
#include <ioremap.h>
#include <time.h>
#include "board.h"
#include "drv_rtc.h"
#undef DBG_TAG
#undef DBG_LVL
#define DBG_TAG "drv_rtc"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>
struct k230_rtc_dev
{
struct rt_device device;
const char *name;
rt_ubase_t base;
size_t size;
int vector;
void (*vector_callback)(void);
};
static void pmu_isolation_rtc(void)
{
/* map pwr base address */
volatile void *reg_pmu_pwr = rt_ioremap((void *)PWR_BASE_ADDR, PWR_IO_SIZE);
uint32_t *addr = (uint32_t *)(reg_pmu_pwr + 0x158); /* pmu power control register */
uint32_t data;
/* disable pmu isolation */
data = *addr;
data &= ~0x20;
*addr = data;
rt_iounmap(reg_pmu_pwr);
/* map pmu base address */
volatile void *reg_pmu = rt_ioremap((void*)PMU_BASE_ADDR, PMU_IO_SIZE);
addr = (uint32_t*)(reg_pmu + 0x48); /* pmu int0 to cpu register */
/* enable int6 int7 */
data = *addr;
data |= 0x06;
*addr = data;
addr = (uint32_t*)(reg_pmu + 0x4c); /* pmu int detect en register */
/* enable int6 rtc alarm detection and int7 rtc tick detection */
data = *addr;
data |= 0x06;
*addr = data;
rt_iounmap(reg_pmu);
}
static int rtc_year_is_leap(int year)
{
return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
static void rtc_timer_set_clock_count_value(struct k230_rtc_dev *dev, uint16_t count)
{
volatile volatile rtc_t *rtc = (rtc_t *)dev->base;
rtc->count.curr_count = count;
rtc->count.sum_count = 0x7FFF;
rtc->int_ctrl.timer_w_en = 1;
rt_thread_mdelay(1);
rtc->int_ctrl.timer_w_en = 0;
rtc->int_ctrl.timer_r_en = 1;
}
static void rtc_interrupt_ctrl_set(struct k230_rtc_dev *dev, rtc_interrupt_mode_t mode)
{
volatile rtc_t *rtc = (rtc_t *)dev->base;
if (mode < RTC_INT_TICK_YEAR)
{
rtc->int_ctrl.year_cmp = 0;
rtc->int_ctrl.month_cmp = 0;
rtc->int_ctrl.day_cmp = 0;
rtc->int_ctrl.week_cmp = 0;
rtc->int_ctrl.hour_cmp = 0;
rtc->int_ctrl.minute_cmp = 0;
rtc->int_ctrl.second_cmp = 0;
if (mode & RTC_INT_ALARM_YEAR)
{
rtc->int_ctrl.year_cmp = 1;
}
if (mode & RTC_INT_ALARM_MONTH)
{
rtc->int_ctrl.month_cmp = 1;
}
if (mode & RTC_INT_ALARM_DAY)
{
rtc->int_ctrl.day_cmp = 1;
}
if (mode & RTC_INT_ALARM_WEEK)
{
rtc->int_ctrl.week_cmp = 1;
}
if (mode & RTC_INT_ALARM_HOUR)
{
rtc->int_ctrl.hour_cmp = 1;
}
if (mode & RTC_INT_ALARM_MINUTE)
{
rtc->int_ctrl.minute_cmp = 1;
}
if (mode & RTC_INT_ALARM_SECOND)
{
rtc->int_ctrl.second_cmp = 1;
}
rtc->int_ctrl.alarm_en = 1;
}
else
{
switch(mode)
{
case RTC_INT_TICK_YEAR:
rtc->int_ctrl.tick_sel = 0x8;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_MONTH:
rtc->int_ctrl.tick_sel = 0x7;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_DAY:
rtc->int_ctrl.tick_sel = 0x6;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_WEEK:
rtc->int_ctrl.tick_sel = 0x5;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_HOUR:
rtc->int_ctrl.tick_sel = 0x4;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_MINUTE:
rtc->int_ctrl.tick_sel = 0x3;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_SECOND:
rtc->int_ctrl.tick_sel = 0x2;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_S8:
rtc->int_ctrl.tick_sel = 0x1;
rtc->int_ctrl.tick_en = 1;
break;
case RTC_INT_TICK_S64:
rtc->int_ctrl.tick_sel = 0x0;
rtc->int_ctrl.tick_en = 1;
break;
default :
break;
}
}
}
static void rtc_stop_interrupt(struct k230_rtc_dev *dev)
{
rt_hw_interrupt_mask(dev->vector);
}
static void rtc_alarm_stop(struct k230_rtc_dev *dev)
{
volatile rtc_t *rtc = (rtc_t *)dev->base;
rtc->int_ctrl.alarm_en = 0;
rtc_stop_interrupt(dev);
}
static void rtc_tick_stop(struct k230_rtc_dev *dev)
{
volatile rtc_t *rtc = (rtc_t *)dev->base;
rtc->int_ctrl.tick_en = 0;
rtc_stop_interrupt(dev);
}
static void rtc_alarm_clear_interrupt(struct k230_rtc_dev *dev)
{
volatile rtc_t *rtc = (rtc_t *)dev->base;
rtc->int_ctrl.alarm_clr = 1;
}
static void rtc_irq(int vector, void *param)
{
struct k230_rtc_dev *dev = (struct k230_rtc_dev *)param;
rtc_alarm_clear_interrupt(dev);
if (dev->vector_callback != RT_NULL)
{
dev->vector_callback();
}
}
static void rtc_date_time_set(struct k230_rtc_dev *dev, int year, int month, int day, \
int hour, int minute, int second, int week)
{
rtc_date_t date;
rtc_time_t time;
rtc_count_t count;
volatile rtc_t *rtc = (rtc_t *)dev->base;
int val = year % 100;
int year_l, year_h;
if(val == 0)
{
year_l = 100;
year_h = year / 100 - 1;
}
else
{
year_l = val;
year_h = (year - val) / 100;
}
rtc->int_ctrl.timer_w_en = 1;
date.year_h = year_h;
date.year_l = year_l;
date.month = month;
date.day = day;
date.leap_year = rtc_year_is_leap(year);
time.week = week;
time.hour = hour;
time.minute = minute;
time.second = second;
rtc->date = date;
rtc->time = time;
}
static void rtc_timer_get(struct k230_rtc_dev *dev, time_t *t)
{
volatile rtc_t *rtc = (rtc_t *)dev->base;
struct tm tm;
if (rtc->int_ctrl.timer_r_en == 0)
{
rtc->int_ctrl.timer_r_en = 1;
}
tm.tm_sec = rtc->time.second;
tm.tm_min = rtc->time.minute;
tm.tm_hour = rtc->time.hour;
tm.tm_mday = rtc->date.day;
tm.tm_mon = rtc->date.month - 1;
tm.tm_year = (rtc->date.year_h * 100 + rtc->date.year_l) - 1900;
tm.tm_wday = rtc->time.week;
*t = timegm(&tm);
}
static void rtc_timer_set(struct k230_rtc_dev *dev, time_t *t)
{
struct tm p_tm;
gmtime_r(t, &p_tm);
rtc_date_time_set(dev, (p_tm.tm_year + 1900), p_tm.tm_mon + 1, p_tm.tm_mday, \
p_tm.tm_hour, p_tm.tm_min, p_tm.tm_sec, p_tm.tm_wday);
rtc_timer_set_clock_count_value(dev, 0);
}
static void rtc_alarm_get(struct k230_rtc_dev *dev, void *args)
{
struct tm *tm = (struct tm*)args;
volatile rtc_t *rtc = (rtc_t *)dev->base;
rtc_alarm_date_t alarm_date = rtc->alarm_date;
rtc_alarm_time_t alarm_time = rtc->alarm_time;
tm->tm_year = (alarm_date.alarm_year_h * 100 + alarm_date.alarm_year_l) -1900;
tm->tm_mon = alarm_date.alarm_month - 1;
tm->tm_mday = alarm_date.alarm_day;
tm->tm_hour = alarm_time.alarm_hour;
tm->tm_min = alarm_time.alarm_minute;
tm->tm_sec = alarm_time.alarm_second;
}
static void rtc_alarm_set(struct k230_rtc_dev *dev, void *args)
{
rtc_alarm_setup_t *setup = (rtc_alarm_setup_t *)args;
struct tm tm = setup->tm;
time_t t;
struct tm p_tm;
volatile rtc_t *rtc = (rtc_t *)dev->base;
rtc_alarm_time_t alarm_time;
rtc_alarm_date_t alarm_date;
rtc_date_t date = rtc->date;
int year, year_l, year_h, val;
t = mktime(&tm);
gmtime_r(&t, &p_tm);
year = p_tm.tm_year + 1900;
val = year % 100;
if(val == 0)
{
year_l = 100;
year_h = year / 100 - 1;
}
else
{
year_l = val;
year_h = (year - val) / 100;
}
alarm_date.alarm_year_h = year_h;
alarm_date.alarm_year_l = year_l;
alarm_date.alarm_month = p_tm.tm_mon + 1;
alarm_date.alarm_day = p_tm.tm_mday;
alarm_time.alarm_hour = p_tm.tm_hour;
alarm_time.alarm_minute = p_tm.tm_min;
alarm_time.alarm_second = p_tm.tm_sec;
rtc->alarm_date = alarm_date;
rtc->alarm_time = alarm_time;
rtc_alarm_clear_interrupt(dev);
rt_hw_interrupt_install(dev->vector, rtc_irq, dev, "rtc");
rt_hw_interrupt_umask(dev->vector);
rtc_interrupt_ctrl_set(dev, setup->flag);
}
static rt_err_t rtc_device_init(rt_device_t dev)
{
struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
rtc_alarm_stop(rtc_dev);
rtc_tick_stop(rtc_dev);
return RT_EOK;
}
static rt_err_t rtc_device_open(rt_device_t dev, rt_uint16_t oflag)
{
return RT_EOK;
}
static rt_err_t rtc_device_close(rt_device_t dev)
{
return RT_EOK;
}
static rt_ssize_t rtc_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
time_t t;
struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
rtc_timer_get(rtc_dev, &t);
rt_memcpy(buffer, (void*)&t, sizeof(t));
return size;
}
static rt_ssize_t rtc_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
struct tm *tm = (struct tm*)buffer;
time_t t = mktime(tm);
struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
rtc_timer_set(rtc_dev, &t);
return size;
}
static rt_err_t rtc_device_control(rt_device_t dev, int cmd, void *args)
{
time_t time;
RT_ASSERT(dev != RT_NULL);
struct k230_rtc_dev *rtc_dev = rt_container_of(dev, struct k230_rtc_dev, device);
RT_ASSERT(rtc_dev != RT_NULL);
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
rtc_timer_get(rtc_dev, (time_t*)args);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
rtc_timer_set(rtc_dev, (time_t *)args);
break;
case RT_DEVICE_CTRL_RTC_GET_ALARM:
rtc_alarm_get(rtc_dev, args);
break;
case RT_DEVICE_CTRL_RTC_SET_ALARM:
rtc_alarm_set(rtc_dev, args);
break;
case RT_DEVICE_CTRL_RTC_STOP_ALARM:
rtc_alarm_stop(rtc_dev);
break;
case RT_DEVICE_CTRL_RTC_STOP_TICK:
rtc_tick_stop(rtc_dev);
break;
case RT_DEVICE_CTRL_RTC_SET_CALLBACK:
rtc_dev->vector_callback = args;
break;
default:
return -RT_EINVAL;
}
return RT_EOK;
}
const static struct rt_device_ops rtc_ops =
{
.init = rtc_device_init,
.open = rtc_device_open,
.close = rtc_device_close,
.read = rtc_device_read,
.write = rtc_device_write,
.control = rtc_device_control,
};
static struct k230_rtc_dev rtc_dev =
{
.name = "rtc",
.base = RTC_BASE_ADDR,
.size = RTC_IO_SIZE,
.vector = K230_IRQ_PMU,
.vector_callback = RT_NULL,
};
static int rt_hw_rtc_init(void)
{
rt_err_t ret;
pmu_isolation_rtc();
rtc_dev.device.type = RT_Device_Class_RTC;
rtc_dev.device.rx_indicate = RT_NULL;
rtc_dev.device.tx_complete = RT_NULL;
#ifdef RT_USING_DEVICE_OPS
rtc_dev.device.ops = &rtc_ops;
#else
rtc_dev.device.init = rtc_device_init;
rtc_dev.device.open = rtc_device_open;
rtc_dev.device.close = rtc_device_close;
rtc_dev.device.read = rtc_device_read;
rtc_dev.device.write = rtc_device_write;
rtc_dev.device.control = rtc_device_control;
#endif /* RT_USING_DEVICE_OPS */
rtc_dev.device.user_data = RT_NULL;
rtc_dev.base = (rt_ubase_t)rt_ioremap((void *)rtc_dev.base, rtc_dev.size);
RT_ASSERT(rtc_dev.base != RT_NULL);
ret = rt_device_register(&rtc_dev.device, "rtc", RT_DEVICE_FLAG_RDWR);
RT_ASSERT(ret == RT_EOK);
LOG_D("rtc driver register OK");
rtc_alarm_stop(&rtc_dev);
rtc_tick_stop(&rtc_dev);
return ret;
}
INIT_DEVICE_EXPORT(rt_hw_rtc_init);