Add clock framework, a first part of new 'extended resources' family of

support frameworks(i.e. reset/regulators/phy/tsensors/fuses...).

The clock framework significantly simplifies handling of complex clock
structures found in modern SoCs. It provides the unified consumers
interface, holds and manages actual clock topology, frequency and gating.

It's tested on three different ARM boards (Nvidia Tegra TK1, Inforce 6410 and
Odroid XU2) and on one MIPS board (Creator Ci20) by kan@.

The framework is still far from perfect and probably doesn't have stable
interface yet, but we want to start testing it on more real boards and
different architectures.

Reviewed by: ian, kan (earlier version)
This commit is contained in:
Michal Meloun 2016-01-24 11:00:38 +00:00
parent 386b134364
commit 12a05f9a86
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=294660
14 changed files with 2304 additions and 0 deletions

View File

@ -1410,6 +1410,13 @@ dev/ex/if_ex.c optional ex
dev/ex/if_ex_isa.c optional ex isa
dev/ex/if_ex_pccard.c optional ex pccard
dev/exca/exca.c optional cbb
dev/extres/clk/clk.c optional ext_resources clk
dev/extres/clk/clkdev_if.m optional ext_resources clk
dev/extres/clk/clknode_if.m optional ext_resources clk
dev/extres/clk/clk_div.c optional ext_resources clk
dev/extres/clk/clk_fixed.c optional ext_resources clk
dev/extres/clk/clk_gate.c optional ext_resources clk
dev/extres/clk/clk_mux.c optional ext_resources clk
dev/fatm/if_fatm.c optional fatm pci
dev/fb/fbd.c optional fbd | vt
dev/fb/fb_if.m standard

View File

@ -90,6 +90,7 @@ COMPAT_LINUXKPI opt_compat.h
COMPILING_LINT opt_global.h
CY_PCI_FASTINTR
DEADLKRES opt_watchdog.h
EXT_RESOURCES opt_global.h
DIRECTIO
FILEMON opt_dontuse.h
FFCLOCK

1261
sys/dev/extres/clk/clk.c Normal file

File diff suppressed because it is too large Load Diff

136
sys/dev/extres/clk/clk.h Normal file
View File

@ -0,0 +1,136 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRES_CLK_H_
#define _DEV_EXTRES_CLK_H_
#include "opt_platform.h"
#include <sys/kobj.h>
#ifdef FDT
#include <dev/ofw/ofw_bus.h>
#endif
#include "clknode_if.h"
#define CLKNODE_IDX_NONE -1 /* Not-selected index */
/* clknode flags. */
#define CLK_NODE_STATIC_STRINGS 0x00000001 /* Static name strings */
#define CLK_NODE_GLITCH_FREE 0x00000002 /* Freq can change w/o stop */
#define CLK_NODE_CANNOT_STOP 0x00000004 /* Clock cannot be disabled */
/* Flags passed to clk_set_freq() and clknode_set_freq(). */
#define CLK_SET_ROUND_UP 0x00000001
#define CLK_SET_ROUND_DOWN 0x00000002
#define CLK_SET_USER_MASK 0x0000FFFF
#define CLK_SET_DRYRUN 0x00010000
typedef struct clk *clk_t;
/* Initialization parameters for clocknode creation. */
struct clknode_init_def {
const char *name;
intptr_t id;
const char **parent_names;
int parent_cnt;
int flags;
};
/*
* Shorthands for constructing method tables.
*/
#define CLKNODEMETHOD KOBJMETHOD
#define CLKNODEMETHOD_END KOBJMETHOD_END
#define clknode_method_t kobj_method_t
#define clknode_class_t kobj_class_t
DECLARE_CLASS(clknode_class);
/*
* Clock domain functions.
*/
struct clkdom *clkdom_create(device_t dev);
int clkdom_finit(struct clkdom *clkdom);
void clkdom_dump(struct clkdom * clkdom);
void clkdom_unlock(struct clkdom *clkdom);
void clkdom_xlock(struct clkdom *clkdom);
/*
* Clock providers interface.
*/
struct clkdom *clkdom_get_by_dev(const device_t dev);
struct clknode *clknode_create(struct clkdom *clkdom,
clknode_class_t clknode_class, const struct clknode_init_def *def);
struct clknode *clknode_register(struct clkdom *cldom, struct clknode *clk);
#ifdef FDT
typedef int clknode_ofw_mapper_func(struct clkdom *clkdom, uint32_t ncells,
phandle_t *cells, struct clknode **clk);
void clkdom_set_ofw_mapper(struct clkdom *clkdom, clknode_ofw_mapper_func *cmp);
#endif
void clknode_init_parent_idx(struct clknode *clknode, int idx);
int clknode_set_parent_by_idx(struct clknode *clk, int idx);
int clknode_set_parent_by_name(struct clknode *clk, const char *name);
const char *clknode_get_name(struct clknode *clk);
const char **clknode_get_parent_names(struct clknode *clk);
int clknode_get_parents_num(struct clknode *clk);
int clknode_get_parent_idx(struct clknode *clk);
struct clknode *clknode_get_parent(struct clknode *clk);
int clknode_get_flags(struct clknode *clk);
void *clknode_get_softc(struct clknode *clk);
device_t clknode_get_device(struct clknode *clk);
struct clknode *clknode_find_by_name(const char *name);
struct clknode *clknode_find_by_id(struct clkdom *clkdom, intptr_t id);
int clknode_get_freq(struct clknode *clknode, uint64_t *freq);
int clknode_set_freq(struct clknode *clknode, uint64_t freq, int flags,
int enablecnt);
int clknode_enable(struct clknode *clknode);
int clknode_disable(struct clknode *clknode);
int clknode_stop(struct clknode *clknode, int depth);
/*
* Clock consumers interface.
*/
int clk_get_by_name(device_t dev, const char *name, clk_t *clk);
int clk_get_by_id(device_t dev, struct clkdom *clkdom, intptr_t id, clk_t *clk);
int clk_release(clk_t clk);
int clk_get_freq(clk_t clk, uint64_t *freq);
int clk_set_freq(clk_t clk, uint64_t freq, int flags);
int clk_test_freq(clk_t clk, uint64_t freq, int flags);
int clk_enable(clk_t clk);
int clk_disable(clk_t clk);
int clk_stop(clk_t clk);
int clk_get_parent(clk_t clk, clk_t *parent);
int clk_set_parent_by_clk(clk_t clk, clk_t parent);
const char *clk_get_name(clk_t clk);
#ifdef FDT
int clk_get_by_ofw_index(device_t dev, int idx, clk_t *clk);
int clk_get_by_ofw_name(device_t dev, const char *name, clk_t *clk);
#endif
#endif /* _DEV_EXTRES_CLK_H_ */

View File

@ -0,0 +1,209 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <dev/extres/clk/clk_div.h>
#include "clkdev_if.h"
#define WR4(_clk, off, val) \
CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
#define RD4(_clk, off, val) \
CLKDEV_READ_4(clknode_get_device(_clk), off, val)
#define MD4(_clk, off, clr, set ) \
CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
static int clknode_div_init(struct clknode *clk, device_t dev);
static int clknode_div_recalc(struct clknode *clk, uint64_t *req);
static int clknode_div_set_freq(struct clknode *clknode, uint64_t fin,
uint64_t *fout, int flag, int *stop);
struct clknode_div_sc {
struct mtx *mtx;
struct resource *mem_res;
uint32_t offset;
uint32_t i_shift;
uint32_t i_mask;
uint32_t i_width;
uint32_t f_shift;
uint32_t f_mask;
uint32_t f_width;
int div_flags;
uint32_t divider; /* in natural form */
};
static clknode_method_t clknode_div_methods[] = {
/* Device interface */
CLKNODEMETHOD(clknode_init, clknode_div_init),
CLKNODEMETHOD(clknode_recalc_freq, clknode_div_recalc),
CLKNODEMETHOD(clknode_set_freq, clknode_div_set_freq),
CLKNODEMETHOD_END
};
DEFINE_CLASS_1(clknode_div, clknode_div_class, clknode_div_methods,
sizeof(struct clknode_div_sc), clknode_class);
static int
clknode_div_init(struct clknode *clk, device_t dev)
{
uint32_t reg;
struct clknode_div_sc *sc;
uint32_t i_div, f_div;
int rv;
sc = clknode_get_softc(clk);
rv = RD4(clk, sc->offset, &reg);
if (rv != 0)
return (rv);
i_div = (reg >> sc->i_shift) & sc->i_mask;
if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
i_div++;
f_div = (reg >> sc->f_shift) & sc->f_mask;
sc->divider = i_div << sc->f_width | f_div;
clknode_init_parent_idx(clk, 0);
return(0);
}
static int
clknode_div_recalc(struct clknode *clk, uint64_t *freq)
{
struct clknode_div_sc *sc;
sc = clknode_get_softc(clk);
if (sc->divider == 0) {
printf("%s: %s divider is zero!\n", clknode_get_name(clk),
__func__);
*freq = 0;
return(EINVAL);
}
*freq = (*freq << sc->f_width) / sc->divider;
return (0);
}
static int
clknode_div_set_freq(struct clknode *clk, uint64_t fin, uint64_t *fout,
int flags, int *stop)
{
struct clknode_div_sc *sc;
uint64_t divider, _fin, _fout;
uint32_t reg, i_div, f_div, hw_i_div;
int rv;
sc = clknode_get_softc(clk);
/* For fractional divider. */
_fin = fin << sc->f_width;
divider = (_fin + *fout / 2) / *fout;
_fout = _fin / divider;
/* Rounding. */
if ((flags & CLK_SET_ROUND_UP) && (*fout < _fout))
divider--;
else if ((flags & CLK_SET_ROUND_DOWN) && (*fout > _fout))
divider++;
/* Break divider into integer and fractional parts. */
i_div = divider >> sc->f_width;
f_div = divider & sc->f_mask;
if (i_div == 0) {
printf("%s: %s integer divider is zero!\n",
clknode_get_name(clk), __func__);
return(EINVAL);
}
hw_i_div = i_div;
if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
hw_i_div--;
*stop = 1;
if (hw_i_div > sc->i_mask) {
/* XXX Or only return error? */
printf("%s: %s integer divider is too big: %u\n",
clknode_get_name(clk), __func__, hw_i_div);
hw_i_div = sc->i_mask;
*stop = 0;
}
i_div = hw_i_div;
if (!(sc->div_flags & CLK_DIV_ZERO_BASED))
i_div++;
divider = i_div << sc->f_width | f_div;
if ((flags & CLK_SET_DRYRUN) == 0) {
if ((*stop != 0) &&
((flags & (CLK_SET_ROUND_UP | CLK_SET_ROUND_DOWN)) == 0) &&
(*fout != (_fin / divider)))
return (ERANGE);
rv = MD4(clk, sc->offset,
(sc->i_mask << sc->i_shift) | (sc->f_mask << sc->f_shift),
(i_div << sc->i_shift) | (f_div << sc->f_shift));
if (rv != 0)
return (rv);
RD4(clk, sc->offset, &reg);
sc->divider = divider;
}
*fout = _fin / divider;
return (0);
}
int
clknode_div_register(struct clkdom *clkdom, struct clk_div_def *clkdef)
{
struct clknode *clk;
struct clknode_div_sc *sc;
clk = clknode_create(clkdom, &clknode_div_class, &clkdef->clkdef);
if (clk == NULL)
return (1);
sc = clknode_get_softc(clk);
sc->offset = clkdef->offset;
sc->i_shift = clkdef->i_shift;
sc->i_width = clkdef->i_width;
sc->i_mask = (1 << clkdef->i_width) - 1;
sc->f_shift = clkdef->f_shift;
sc->f_width = clkdef->f_width;
sc->f_mask = (1 << clkdef->f_width) - 1;
sc->div_flags = clkdef->div_flags;
clknode_register(clkdom, clk);
return (0);
}

View File

@ -0,0 +1,48 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRES_CLK_DIV_H_
#define _DEV_EXTRES_CLK_DIV_H_
#include <dev/extres/clk/clk.h>
#define CLK_DIV_ZERO_BASED 0x0001 /* Zero based divider. */
struct clk_div_def {
struct clknode_init_def clkdef;
uint32_t offset; /* Divider register offset */
uint32_t i_shift; /* Pos of div bits in reg */
uint32_t i_width; /* Width of div bit field */
uint32_t f_shift; /* Fractional divide bits, */
uint32_t f_width; /* set to 0 for int divider */
int div_flags; /* Divider-specific flags */
};
int clknode_div_register(struct clkdom *clkdom, struct clk_div_def *clkdef);
#endif /*_DEV_EXTRES_CLK_DIV_H_*/

View File

@ -0,0 +1,114 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/kobj.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/rman.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <dev/extres/clk/clk_fixed.h>
#define DEVICE_LOCK(_sc) mtx_lock((_sc)->mtx)
#define DEVICE_UNLOCK(_sc) mtx_unlock((_sc)->mtx)
static int clknode_fixed_init(struct clknode *clk, device_t dev);
static int clknode_fixed_recalc(struct clknode *clk, uint64_t *freq);
struct clknode_fixed_sc {
struct mtx *mtx;
int fixed_flags;
uint64_t freq;
uint32_t mult;
uint32_t div;
};
static clknode_method_t clknode_fixed_methods[] = {
/* Device interface */
CLKNODEMETHOD(clknode_init, clknode_fixed_init),
CLKNODEMETHOD(clknode_recalc_freq, clknode_fixed_recalc),
CLKNODEMETHOD_END
};
DEFINE_CLASS_1(clknode_fixed, clknode_fixed_class, clknode_fixed_methods,
sizeof(struct clknode_fixed_sc), clknode_class);
static int
clknode_fixed_init(struct clknode *clk, device_t dev)
{
struct clknode_fixed_sc *sc;
sc = clknode_get_softc(clk);
if (sc->freq == 0)
clknode_init_parent_idx(clk, 0);
return(0);
}
static int
clknode_fixed_recalc(struct clknode *clk, uint64_t *freq)
{
struct clknode_fixed_sc *sc;
sc = clknode_get_softc(clk);
if (sc->freq != 0)
*freq = sc->freq;
else if ((sc->mult != 0) && (sc->div != 0))
*freq = (*freq / sc->div) * sc->mult;
else
*freq = 0;
return (0);
}
int
clknode_fixed_register(struct clkdom *clkdom, struct clk_fixed_def *clkdef,
struct mtx *dev_mtx)
{
struct clknode *clk;
struct clknode_fixed_sc *sc;
if ((clkdef->freq == 0) && (clkdef->clkdef.parent_cnt == 0))
panic("fixed clk: Frequency is not defined for clock source");
clk = clknode_create(clkdom, &clknode_fixed_class, &clkdef->clkdef);
if (clk == NULL)
return (1);
sc = clknode_get_softc(clk);
sc->mtx = dev_mtx;
sc->fixed_flags = clkdef->fixed_flags;
sc->freq = clkdef->freq;
sc->mult = clkdef->mult;
sc->div = clkdef->div;
clknode_register(clkdom, clk);
return (0);
}

View File

@ -0,0 +1,53 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRES_CLK_FIXED_H_
#define _DEV_EXTRES_CLK_FIXED_H_
#include <dev/extres/clk/clk.h>
/*
* A fixed clock can represent several different real-world objects, including
* an oscillator with a fixed output frequency, a fixed divider (multiplier and
* divisor must both be > 0), or a phase-fractional divider within a PLL
* (however the code currently divides first, then multiplies, potentially
* leading to different roundoff errors than the hardware PLL).
*/
struct clk_fixed_def {
struct clknode_init_def clkdef;
uint64_t freq;
uint32_t mult;
uint32_t div;
int fixed_flags;
};
int clknode_fixed_register(struct clkdom *clkdom, struct clk_fixed_def *clkdef,
struct mtx *dev_mtx);
#endif /*_DEV_EXTRES_CLK_FIXED_H_*/

View File

@ -0,0 +1,126 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <dev/extres/clk/clk_gate.h>
#include "clkdev_if.h"
#define WR4(_clk, off, val) \
CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
#define RD4(_clk, off, val) \
CLKDEV_READ_4(clknode_get_device(_clk), off, val)
#define MD4(_clk, off, clr, set ) \
CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
static int clknode_gate_init(struct clknode *clk, device_t dev);
static int clknode_gate_set_gate(struct clknode *clk, bool enable);
struct clknode_gate_sc {
uint32_t offset;
uint32_t shift;
uint32_t mask;
uint32_t on_value;
uint32_t off_value;
int gate_flags;
bool ungated;
};
static clknode_method_t clknode_gate_methods[] = {
/* Device interface */
CLKNODEMETHOD(clknode_init, clknode_gate_init),
CLKNODEMETHOD(clknode_set_gate, clknode_gate_set_gate),
CLKNODEMETHOD_END
};
DEFINE_CLASS_1(clknode_gate, clknode_gate_class, clknode_gate_methods,
sizeof(struct clknode_gate_sc), clknode_class);
static int
clknode_gate_init(struct clknode *clk, device_t dev)
{
uint32_t reg;
struct clknode_gate_sc *sc;
int rv;
sc = clknode_get_softc(clk);
rv = RD4(clk, sc->offset, &reg);
if (rv != 0)
return (rv);
reg = (reg >> sc->shift) & sc->mask;
sc->ungated = reg == sc->on_value ? 1 : 0;
clknode_init_parent_idx(clk, 0);
return(0);
}
static int
clknode_gate_set_gate(struct clknode *clk, bool enable)
{
uint32_t reg;
struct clknode_gate_sc *sc;
int rv;
sc = clknode_get_softc(clk);
sc->ungated = enable;
rv = MD4(clk, sc->offset, sc->mask << sc->shift,
(sc->ungated ? sc->on_value : sc->off_value) << sc->shift);
if (rv != 0)
return (rv);
RD4(clk, sc->offset, &reg);
return(0);
}
int
clknode_gate_register(struct clkdom *clkdom, struct clk_gate_def *clkdef)
{
struct clknode *clk;
struct clknode_gate_sc *sc;
clk = clknode_create(clkdom, &clknode_gate_class, &clkdef->clkdef);
if (clk == NULL)
return (1);
sc = clknode_get_softc(clk);
sc->offset = clkdef->offset;
sc->shift = clkdef->shift;
sc->mask = clkdef->mask;
sc->on_value = clkdef->on_value;
sc->off_value = clkdef->off_value;
sc->gate_flags = clkdef->gate_flags;
clknode_register(clkdom, clk);
return (0);
}

View File

@ -0,0 +1,46 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRES_CLK_GATE_H_
#define _DEV_EXTRES_CLK_GATE_H_
#include <dev/extres/clk/clk.h>
struct clk_gate_def {
struct clknode_init_def clkdef;
uint32_t offset;
uint32_t shift;
uint32_t mask;
uint32_t on_value;
uint32_t off_value;
int gate_flags;
};
int clknode_gate_register(struct clkdom *clkdom, struct clk_gate_def *clkdef);
#endif /* _DEV_EXTRES_CLK_GATE_H_ */

View File

@ -0,0 +1,122 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/conf.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <dev/extres/clk/clk_mux.h>
#include "clkdev_if.h"
#define WR4(_clk, off, val) \
CLKDEV_WRITE_4(clknode_get_device(_clk), off, val)
#define RD4(_clk, off, val) \
CLKDEV_READ_4(clknode_get_device(_clk), off, val)
#define MD4(_clk, off, clr, set ) \
CLKDEV_MODIFY_4(clknode_get_device(_clk), off, clr, set)
static int clknode_mux_init(struct clknode *clk, device_t dev);
static int clknode_mux_set_mux(struct clknode *clk, int idx);
struct clknode_mux_sc {
uint32_t offset;
uint32_t shift;
uint32_t mask;
int mux_flags;
};
static clknode_method_t clknode_mux_methods[] = {
/* Device interface */
CLKNODEMETHOD(clknode_init, clknode_mux_init),
CLKNODEMETHOD(clknode_set_mux, clknode_mux_set_mux),
CLKNODEMETHOD_END
};
DEFINE_CLASS_1(clknode_mux, clknode_mux_class, clknode_mux_methods,
sizeof(struct clknode_mux_sc), clknode_class);
static int
clknode_mux_init(struct clknode *clk, device_t dev)
{
uint32_t reg;
struct clknode_mux_sc *sc;
int rv;
sc = clknode_get_softc(clk);
rv = RD4(clk, sc->offset, &reg);
if (rv != 0)
return (rv);
reg = (reg >> sc->shift) & sc->mask;
clknode_init_parent_idx(clk, reg);
return(0);
}
static int
clknode_mux_set_mux(struct clknode *clk, int idx)
{
uint32_t reg;
struct clknode_mux_sc *sc;
int rv;
sc = clknode_get_softc(clk);
rv = MD4(clk, sc->offset, sc->mask << sc->shift,
(idx & sc->mask) << sc->shift);
if (rv != 0)
return (rv);
RD4(clk, sc->offset, &reg);
return(0);
}
int
clknode_mux_register(struct clkdom *clkdom, struct clk_mux_def *clkdef)
{
struct clknode *clk;
struct clknode_mux_sc *sc;
clk = clknode_create(clkdom, &clknode_mux_class, &clkdef->clkdef);
if (clk == NULL)
return (1);
sc = clknode_get_softc(clk);
sc->offset = clkdef->offset;
sc->shift = clkdef->shift;
sc->mask = (1 << clkdef->width) - 1;
sc->mux_flags = clkdef->mux_flags;
clknode_register(clkdom, clk);
return (0);
}

View File

@ -0,0 +1,43 @@
/*-
* Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $FreeBSD$
*/
#ifndef _DEV_EXTRESF_CLK_MUX_H_
#define _DEV_EXTRESF_CLK_MUX_H_
#include <dev/extres/clk/clk.h>
struct clk_mux_def {
struct clknode_init_def clkdef;
uint32_t offset;
uint32_t shift;
uint32_t width;
int mux_flags;
};
int clknode_mux_register(struct clkdom *clkdom, struct clk_mux_def *clkdef);
#endif /* _DEV_EXTRESF_CLK_MUX_H_ */

View File

@ -0,0 +1,59 @@
#-
# Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
#include <machine/bus.h>
INTERFACE clkdev;
#
# Write single register
#
METHOD int write_4 {
device_t dev;
bus_addr_t addr;
uint32_t val;
};
#
# Read single register
#
METHOD int read_4 {
device_t dev;
bus_addr_t addr;
uint32_t *val;
};
#
# Modify single register
#
METHOD int modify_4 {
device_t dev;
bus_addr_t addr;
uint32_t clear_mask;
uint32_t set_mask;
};

View File

@ -0,0 +1,79 @@
#-
# Copyright 2016 Michal Meloun <mmel@FreeBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $FreeBSD$
#
INTERFACE clknode;
HEADER {
struct clknode;
}
#
# Initialize clock node, get shanpshot of cached values
#
METHOD int init {
struct clknode *clk;
device_t dev;
};
#
# Recalculate frequency
# req - in/out recalulated frequency
#
METHOD int recalc_freq {
struct clknode *clk;
uint64_t *freq;
};
#
# Set frequency
# fin - parent (input)frequency.
# fout - requested output freqency. If clock cannot change frequency,
# then must return new requested frequency for his parent
METHOD int set_freq {
struct clknode *clk;
uint64_t fin;
uint64_t *fout;
int flags;
int *done;
};
#
# Enable/disable clock
#
METHOD int set_gate {
struct clknode *clk;
bool enable;
};
#
# Set multiplexer
#
METHOD int set_mux {
struct clknode *clk;
int idx;
};