clk_fixed.c revision 294660
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#include <sys/cdefs.h> 28__FBSDID("$FreeBSD: head/sys/dev/extres/clk/clk_fixed.c 294660 2016-01-24 11:00:38Z mmel $"); 29 30 31#include <sys/param.h> 32#include <sys/conf.h> 33#include <sys/bus.h> 34#include <sys/kernel.h> 35#include <sys/kobj.h> 36#include <sys/malloc.h> 37#include <sys/mutex.h> 38#include <sys/rman.h> 39#include <sys/systm.h> 40 41#include <machine/bus.h> 42 43#include <dev/extres/clk/clk_fixed.h> 44 45#define DEVICE_LOCK(_sc) mtx_lock((_sc)->mtx) 46#define DEVICE_UNLOCK(_sc) mtx_unlock((_sc)->mtx) 47 48static int clknode_fixed_init(struct clknode *clk, device_t dev); 49static int clknode_fixed_recalc(struct clknode *clk, uint64_t *freq); 50struct clknode_fixed_sc { 51 struct mtx *mtx; 52 int fixed_flags; 53 uint64_t freq; 54 uint32_t mult; 55 uint32_t div; 56}; 57 58static clknode_method_t clknode_fixed_methods[] = { 59 /* Device interface */ 60 CLKNODEMETHOD(clknode_init, clknode_fixed_init), 61 CLKNODEMETHOD(clknode_recalc_freq, clknode_fixed_recalc), 62 CLKNODEMETHOD_END 63}; 64DEFINE_CLASS_1(clknode_fixed, clknode_fixed_class, clknode_fixed_methods, 65 sizeof(struct clknode_fixed_sc), clknode_class); 66 67static int 68clknode_fixed_init(struct clknode *clk, device_t dev) 69{ 70 struct clknode_fixed_sc *sc; 71 72 sc = clknode_get_softc(clk); 73 if (sc->freq == 0) 74 clknode_init_parent_idx(clk, 0); 75 return(0); 76} 77static int 78clknode_fixed_recalc(struct clknode *clk, uint64_t *freq) 79{ 80 struct clknode_fixed_sc *sc; 81 82 sc = clknode_get_softc(clk); 83 if (sc->freq != 0) 84 *freq = sc->freq; 85 else if ((sc->mult != 0) && (sc->div != 0)) 86 *freq = (*freq / sc->div) * sc->mult; 87 else 88 *freq = 0; 89 return (0); 90} 91 92int 93clknode_fixed_register(struct clkdom *clkdom, struct clk_fixed_def *clkdef, 94 struct mtx *dev_mtx) 95{ 96 struct clknode *clk; 97 struct clknode_fixed_sc *sc; 98 99 if ((clkdef->freq == 0) && (clkdef->clkdef.parent_cnt == 0)) 100 panic("fixed clk: Frequency is not defined for clock source"); 101 clk = clknode_create(clkdom, &clknode_fixed_class, &clkdef->clkdef); 102 if (clk == NULL) 103 return (1); 104 105 sc = clknode_get_softc(clk); 106 sc->mtx = dev_mtx; 107 sc->fixed_flags = clkdef->fixed_flags; 108 sc->freq = clkdef->freq; 109 sc->mult = clkdef->mult; 110 sc->div = clkdef->div; 111 112 clknode_register(clkdom, clk); 113 return (0); 114} 115