1 /*-
2 * Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #ifndef _DEV_EXTRES_CLK_H_
28 #define _DEV_EXTRES_CLK_H_
29 #include "opt_platform.h"
30
31 #include <sys/kobj.h>
32 #ifdef FDT
33 #include <dev/ofw/ofw_bus.h>
34 #endif
35 #include "clknode_if.h"
36
37 #define CLKNODE_IDX_NONE -1 /* Not-selected index */
38
39 /* clknode flags. */
40 #define CLK_NODE_STATIC_STRINGS 0x00000001 /* Static name strings */
41 #define CLK_NODE_GLITCH_FREE 0x00000002 /* Freq can change w/o stop */
42 #define CLK_NODE_CANNOT_STOP 0x00000004 /* Cannot be disabled */
43 #define CLK_NODE_LINKED 0x00000008 /* Is linked clock */
44 #define CLK_NODE_REGISTERED 0x00000020 /* Is already registered */
45
46 /* Flags passed to clk_set_freq() and clknode_set_freq(). */
47 #define CLK_SET_ROUND(x) ((x) & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN))
48 #define CLK_SET_ROUND_EXACT 0
49 #define CLK_SET_ROUND_UP 0x00000001
50 #define CLK_SET_ROUND_DOWN 0x00000002
51 #define CLK_SET_ROUND_MULTIPLE 0x00000004
52 #define CLK_SET_ROUND_ANY (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)
53
54 #define CLK_SET_USER_MASK 0x0000FFFF
55 #define CLK_SET_DRYRUN 0x00010000
56
57 typedef struct clk *clk_t;
58
59 /* Initialization parameters for clocknode creation. */
60 struct clknode_init_def {
61 const char *name;
62 intptr_t id;
63 const char **parent_names;
64 int parent_cnt;
65 int flags;
66 };
67
68 /*
69 * Shorthands for constructing method tables.
70 */
71 #define CLKNODEMETHOD KOBJMETHOD
72 #define CLKNODEMETHOD_END KOBJMETHOD_END
73 #define clknode_method_t kobj_method_t
74 #define clknode_class_t kobj_class_t
75 DECLARE_CLASS(clknode_class);
76
77 /*
78 * Clock domain functions.
79 */
80 struct clkdom *clkdom_create(device_t dev);
81 int clkdom_finit(struct clkdom *clkdom);
82 void clkdom_dump(struct clkdom * clkdom);
83 void clkdom_unlock(struct clkdom *clkdom);
84 void clkdom_xlock(struct clkdom *clkdom);
85
86 /*
87 * Clock providers interface.
88 */
89 struct clkdom *clkdom_get_by_dev(const device_t dev);
90
91 struct clknode *clknode_create(struct clkdom *clkdom,
92 clknode_class_t clknode_class, const struct clknode_init_def *def);
93 struct clknode *clknode_register(struct clkdom *cldom, struct clknode *clk);
94 #ifdef FDT
95 typedef int clknode_ofw_mapper_func(struct clkdom *clkdom, uint32_t ncells,
96 phandle_t *cells, struct clknode **clk);
97 void clkdom_set_ofw_mapper(struct clkdom *clkdom, clknode_ofw_mapper_func *cmp);
98 #endif
99
100 void clknode_init_parent_idx(struct clknode *clknode, int idx);
101 int clknode_set_parent_by_idx(struct clknode *clk, int idx);
102 int clknode_set_parent_by_name(struct clknode *clk, const char *name);
103 const char *clknode_get_name(struct clknode *clk);
104 const char **clknode_get_parent_names(struct clknode *clk);
105 int clknode_get_parents_num(struct clknode *clk);
106 int clknode_get_parent_idx(struct clknode *clk);
107 struct clknode *clknode_get_parent(struct clknode *clk);
108 int clknode_get_flags(struct clknode *clk);
109 void *clknode_get_softc(struct clknode *clk);
110 device_t clknode_get_device(struct clknode *clk);
111 struct clknode *clknode_find_by_name(const char *name);
112 struct clknode *clknode_find_by_id(struct clkdom *clkdom, intptr_t id);
113 int clknode_get_freq(struct clknode *clknode, uint64_t *freq);
114 int clknode_set_freq(struct clknode *clknode, uint64_t freq, int flags,
115 int enablecnt);
116 int clknode_test_freq(struct clknode *clknode, uint64_t freq, int flags,
117 int enablecnt, uint64_t *out_freq);
118 int clknode_enable(struct clknode *clknode);
119 int clknode_disable(struct clknode *clknode);
120 int clknode_stop(struct clknode *clknode, int depth);
121
122 /*
123 * Clock consumers interface.
124 */
125 int clk_get_by_name(device_t dev, const char *name, clk_t *clk);
126 int clk_get_by_id(device_t dev, struct clkdom *clkdom, intptr_t id, clk_t *clk);
127 int clk_release(clk_t clk);
128 int clk_get_freq(clk_t clk, uint64_t *freq);
129 int clk_set_freq(clk_t clk, uint64_t freq, int flags);
130 int clk_test_freq(clk_t clk, uint64_t freq, int flags);
131 int clk_enable(clk_t clk);
132 int clk_disable(clk_t clk);
133 int clk_stop(clk_t clk);
134 int clk_get_parent(clk_t clk, clk_t *parent);
135 int clk_set_parent_by_clk(clk_t clk, clk_t parent);
136 const char *clk_get_name(clk_t clk);
137
138 static inline uint64_t
clk_freq_diff(uint64_t x,uint64_t y)139 clk_freq_diff(uint64_t x, uint64_t y)
140 {
141 return (x >= y ? x - y : y - x);
142 }
143
144 #ifdef FDT
145 int clk_set_assigned(device_t dev, phandle_t node);
146 int clk_get_by_ofw_index(device_t dev, phandle_t node, int idx, clk_t *clk);
147 int clk_get_by_ofw_index_prop(device_t dev, phandle_t cnode, const char *prop, int idx, clk_t *clk);
148 int clk_get_by_ofw_name(device_t dev, phandle_t node, const char *name,
149 clk_t *clk);
150 int clk_parse_ofw_out_names(device_t dev, phandle_t node,
151 const char ***out_names, uint32_t **indices);
152 int clk_parse_ofw_clk_name(device_t dev, phandle_t node, const char **name);
153 #endif
154
155 #endif /* _DEV_EXTRES_CLK_H_ */
156