hw/elf_ops: Fix a typo
[qemu/ar7.git] / hw / core / clock.c
blob76b5f468b6ec1f7d3a4ef959f2c3e70bd0572c42
1 /*
2  * Hardware Clocks
3  *
4  * Copyright GreenSocs 2016-2020
5  *
6  * Authors:
7  *  Frederic Konrad
8  *  Damien Hedde
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or later.
11  * See the COPYING file in the top-level directory.
12  */
14 #include "qemu/osdep.h"
15 #include "qemu/cutils.h"
16 #include "hw/clock.h"
17 #include "trace.h"
19 #define CLOCK_PATH(_clk) (_clk->canonical_path)
21 void clock_setup_canonical_path(Clock *clk)
23     g_free(clk->canonical_path);
24     clk->canonical_path = object_get_canonical_path(OBJECT(clk));
27 Clock *clock_new(Object *parent, const char *name)
29     Object *obj;
30     Clock *clk;
32     obj = object_new(TYPE_CLOCK);
33     object_property_add_child(parent, name, obj);
34     object_unref(obj);
36     clk = CLOCK(obj);
37     clock_setup_canonical_path(clk);
39     return clk;
42 void clock_set_callback(Clock *clk, ClockCallback *cb, void *opaque)
44     clk->callback = cb;
45     clk->callback_opaque = opaque;
48 void clock_clear_callback(Clock *clk)
50     clock_set_callback(clk, NULL, NULL);
53 bool clock_set(Clock *clk, uint64_t period)
55     if (clk->period == period) {
56         return false;
57     }
58     trace_clock_set(CLOCK_PATH(clk), CLOCK_PERIOD_TO_HZ(clk->period),
59                     CLOCK_PERIOD_TO_HZ(period));
60     clk->period = period;
62     return true;
65 static void clock_propagate_period(Clock *clk, bool call_callbacks)
67     Clock *child;
69     QLIST_FOREACH(child, &clk->children, sibling) {
70         if (child->period != clk->period) {
71             child->period = clk->period;
72             trace_clock_update(CLOCK_PATH(child), CLOCK_PATH(clk),
73                                CLOCK_PERIOD_TO_HZ(clk->period),
74                                call_callbacks);
75             if (call_callbacks && child->callback) {
76                 child->callback(child->callback_opaque);
77             }
78             clock_propagate_period(child, call_callbacks);
79         }
80     }
83 void clock_propagate(Clock *clk)
85     assert(clk->source == NULL);
86     trace_clock_propagate(CLOCK_PATH(clk));
87     clock_propagate_period(clk, true);
90 void clock_set_source(Clock *clk, Clock *src)
92     /* changing clock source is not supported */
93     assert(!clk->source);
95     trace_clock_set_source(CLOCK_PATH(clk), CLOCK_PATH(src));
97     clk->period = src->period;
98     QLIST_INSERT_HEAD(&src->children, clk, sibling);
99     clk->source = src;
100     clock_propagate_period(clk, false);
103 static void clock_disconnect(Clock *clk)
105     if (clk->source == NULL) {
106         return;
107     }
109     trace_clock_disconnect(CLOCK_PATH(clk));
111     clk->source = NULL;
112     QLIST_REMOVE(clk, sibling);
115 char *clock_display_freq(Clock *clk)
117     return freq_to_str(clock_get_hz(clk));
120 static void clock_initfn(Object *obj)
122     Clock *clk = CLOCK(obj);
124     QLIST_INIT(&clk->children);
127 static void clock_finalizefn(Object *obj)
129     Clock *clk = CLOCK(obj);
130     Clock *child, *next;
132     /* clear our list of children */
133     QLIST_FOREACH_SAFE(child, &clk->children, sibling, next) {
134         clock_disconnect(child);
135     }
137     /* remove us from source's children list */
138     clock_disconnect(clk);
140     g_free(clk->canonical_path);
143 static const TypeInfo clock_info = {
144     .name              = TYPE_CLOCK,
145     .parent            = TYPE_OBJECT,
146     .instance_size     = sizeof(Clock),
147     .instance_init     = clock_initfn,
148     .instance_finalize = clock_finalizefn,
151 static void clock_register_types(void)
153     type_register_static(&clock_info);
156 type_init(clock_register_types)