refactor: make pci device driver loading passive, pci bus scanner will not load them...
[lunaix-os.git] / lunaix-os / kernel / time / clock.c
1 #include <lunaix/clock.h>
2 #include <lunaix/device.h>
3 #include <lunaix/fs/twifs.h>
4 #include <lunaix/spike.h>
5
6 #include <klibc/string.h>
7
8 void
9 __clock_read_systime(struct twimap* map)
10 {
11     ticks_t sys_time = clock_systime();
12     twimap_printf(map, "%u", sys_time);
13 }
14
15 void
16 __clock_read_datetime(struct twimap* map)
17 {
18     datetime_t dt;
19     clock_walltime(&dt);
20     twimap_printf(map,
21                   "%.4d-%.2d-%.2d %.2d:%.2d:%.2d",
22                   dt.year,
23                   dt.month,
24                   dt.day,
25                   dt.hour,
26                   dt.minute,
27                   dt.second);
28 }
29
30 void
31 __clock_read_unix(struct twimap* map)
32 {
33     twimap_printf(map, "%u", clock_unixtime());
34 }
35
36 void
37 clock_build_mapping()
38 {
39     struct twifs_node* root = twifs_dir_node(NULL, "clock");
40     struct twimap* map;
41
42     map = twifs_mapping(root, NULL, "systime");
43     map->read = __clock_read_systime;
44
45     map = twifs_mapping(root, NULL, "unix");
46     map->read = __clock_read_unix;
47
48     map = twifs_mapping(root, NULL, "datetime");
49     map->read = __clock_read_datetime;
50 }
51 EXPORT_TWIFS_PLUGIN(sys_clock, clock_build_mapping);
52
53 time_t
54 clock_unixtime()
55 {
56     datetime_t dt;
57     hwrtc_walltime(&dt);
58     return datetime_tounix(&dt);
59 }
60
61 time_t
62 clock_systime()
63 {
64     if (!systimer) {
65         return 0;
66     }
67
68     ticks_t t = hwtimer_current_systicks();
69     ticks_t tu = systimer->running_freq / 1000;
70     return t / (tu + 1);
71 }
72
73 void
74 clock_walltime(datetime_t* datetime)
75 {
76     sysrtc->get_walltime(sysrtc, datetime);
77 }
78
79 void
80 clock_init()
81 {
82     int idx = 0;
83     struct device_def* pos;
84     foreach_exported_device_of(load_timedev, idx, pos)
85     {
86         if (pos->class.device != DEV_RTC) {
87             continue;
88         }
89
90         pos->init(pos);
91     }
92 }