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