1 /*- 2 * Copyright 2016 Michal Meloun <[email protected]> 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 * $FreeBSD$ 27 */ 28 29 #ifndef _DEV_EXTRES_CLK_H_ 30 #define _DEV_EXTRES_CLK_H_ 31 #include "opt_platform.h" 32 33 #include <sys/kobj.h> 34 #ifdef FDT 35 #include <dev/ofw/ofw_bus.h> 36 #endif 37 #include "clknode_if.h" 38 39 #define CLKNODE_IDX_NONE -1 /* Not-selected index */ 40 41 /* clknode flags. */ 42 #define CLK_NODE_STATIC_STRINGS 0x00000001 /* Static name strings */ 43 #define CLK_NODE_GLITCH_FREE 0x00000002 /* Freq can change w/o stop */ 44 #define CLK_NODE_CANNOT_STOP 0x00000004 /* Cannot be disabled */ 45 #define CLK_NODE_LINKED 0x00000008 /* Is linked clock */ 46 #define CLK_NODE_REGISTERED 0x00000020 /* Is already registered */ 47 48 /* Flags passed to clk_set_freq() and clknode_set_freq(). */ 49 #define CLK_SET_ROUND(x) ((x) & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)) 50 #define CLK_SET_ROUND_EXACT 0 51 #define CLK_SET_ROUND_UP 0x00000001 52 #define CLK_SET_ROUND_DOWN 0x00000002 53 #define CLK_SET_ROUND_MULTIPLE 0x00000004 54 #define CLK_SET_ROUND_ANY (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN) 55 56 #define CLK_SET_USER_MASK 0x0000FFFF 57 #define CLK_SET_DRYRUN 0x00010000 58 59 typedef struct clk *clk_t; 60 61 /* Initialization parameters for clocknode creation. */ 62 struct clknode_init_def { 63 const char *name; 64 intptr_t id; 65 const char **parent_names; 66 int parent_cnt; 67 int flags; 68 }; 69 70 /* 71 * Shorthands for constructing method tables. 72 */ 73 #define CLKNODEMETHOD KOBJMETHOD 74 #define CLKNODEMETHOD_END KOBJMETHOD_END 75 #define clknode_method_t kobj_method_t 76 #define clknode_class_t kobj_class_t 77 DECLARE_CLASS(clknode_class); 78 79 /* 80 * Clock domain functions. 81 */ 82 struct clkdom *clkdom_create(device_t dev); 83 int clkdom_finit(struct clkdom *clkdom); 84 void clkdom_dump(struct clkdom * clkdom); 85 void clkdom_unlock(struct clkdom *clkdom); 86 void clkdom_xlock(struct clkdom *clkdom); 87 88 /* 89 * Clock providers interface. 90 */ 91 struct clkdom *clkdom_get_by_dev(const device_t dev); 92 93 struct clknode *clknode_create(struct clkdom *clkdom, 94 clknode_class_t clknode_class, const struct clknode_init_def *def); 95 struct clknode *clknode_register(struct clkdom *cldom, struct clknode *clk); 96 #ifdef FDT 97 typedef int clknode_ofw_mapper_func(struct clkdom *clkdom, uint32_t ncells, 98 phandle_t *cells, struct clknode **clk); 99 void clkdom_set_ofw_mapper(struct clkdom *clkdom, clknode_ofw_mapper_func *cmp); 100 #endif 101 102 void clknode_init_parent_idx(struct clknode *clknode, int idx); 103 int clknode_set_parent_by_idx(struct clknode *clk, int idx); 104 int clknode_set_parent_by_name(struct clknode *clk, const char *name); 105 const char *clknode_get_name(struct clknode *clk); 106 const char **clknode_get_parent_names(struct clknode *clk); 107 int clknode_get_parents_num(struct clknode *clk); 108 int clknode_get_parent_idx(struct clknode *clk); 109 struct clknode *clknode_get_parent(struct clknode *clk); 110 int clknode_get_flags(struct clknode *clk); 111 void *clknode_get_softc(struct clknode *clk); 112 device_t clknode_get_device(struct clknode *clk); 113 struct clknode *clknode_find_by_name(const char *name); 114 struct clknode *clknode_find_by_id(struct clkdom *clkdom, intptr_t id); 115 int clknode_get_freq(struct clknode *clknode, uint64_t *freq); 116 int clknode_set_freq(struct clknode *clknode, uint64_t freq, int flags, 117 int enablecnt); 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 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