aboutsummaryrefslogtreecommitdiff
path: root/sys/arm/mv/clk/a37x0_tbg_pll.c
blob: da8ff83cc8980380521aa2fdaa0526e9b50427a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*-
 * SPDX-License-Identifier: BSD-2-Clause
 *
 * Copyright (c) 2021 Semihalf.
 *
 * 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/param.h>
#include <sys/bus.h>
#include <sys/rman.h>
#include <machine/bus.h>

#include <dev/clk/clk.h>

#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>

#include "clkdev_if.h"

#include "a37x0_tbg_pll.h"

#define RD4(_clk, offset, val)			\
	CLKDEV_READ_4(clknode_get_device(_clk), offset, val)

struct a37x0_tbg_pll_softc {
	struct a37x0_tbg_pll_reg_def		vcodiv;
	struct a37x0_tbg_pll_reg_def		refdiv;
	struct a37x0_tbg_pll_reg_def		fbdiv;
	struct a37x0_tbg_pll_reg_def		tbg_bypass;
};

static int
a37x0_tbg_pll_recalc_freq(struct clknode *clk, uint64_t *freq)
{
	struct a37x0_tbg_pll_softc *sc;
	uint32_t vcodiv, fbdiv, refdiv;
	unsigned int val;

	sc = clknode_get_softc(clk);

	RD4(clk, sc->tbg_bypass.offset, &val);
	if ((val >> sc->tbg_bypass.shift) & sc->tbg_bypass.mask)
		return 0;

	RD4(clk, sc->vcodiv.offset, &val);
	vcodiv = 1 << ((val >> sc->vcodiv.shift) & sc->vcodiv.mask);

	RD4(clk, sc->refdiv.offset, &val);
	refdiv = (val >> sc->refdiv.shift) & sc->refdiv.mask;

	RD4(clk, sc->fbdiv.offset, &val);
	fbdiv = (val >> sc->fbdiv.shift) & sc->fbdiv.mask;

	if (refdiv == 0)
		refdiv = 1;

	*freq = *freq * (fbdiv / refdiv) * 4;
	*freq /= vcodiv;

	return (0);
}

static int
a37x0_tbg_pll_init(struct clknode *clk, device_t dev)
{

	clknode_init_parent_idx(clk, 0);

	return (0);
}

static clknode_method_t a37x0_tbg_pll_clknode_methods[] = {
	CLKNODEMETHOD(clknode_recalc_freq,	a37x0_tbg_pll_recalc_freq),
	CLKNODEMETHOD(clknode_init,		a37x0_tbg_pll_init),

	CLKNODEMETHOD_END
};

DEFINE_CLASS_1(a37x0_tbg_pll__clknode, a37x0_tbg_pll_clknode_class,
    a37x0_tbg_pll_clknode_methods, sizeof(struct a37x0_tbg_pll_softc),
    clknode_class);

int
a37x0_tbg_pll_clk_register(struct clkdom *clkdom,
    const struct a37x0_tbg_pll_clk_def *clkdef)
{
	struct a37x0_tbg_pll_softc *sc;
	struct clknode *clk;

	clk = clknode_create(clkdom, &a37x0_tbg_pll_clknode_class,
	    &clkdef->clkdef);

	if (clk == NULL)
		return (1);

	sc = clknode_get_softc(clk);

	sc->vcodiv = clkdef->vcodiv;
	sc->refdiv = clkdef->refdiv;
	sc->fbdiv = clkdef->fbdiv;
	sc->tbg_bypass = clkdef->tbg_bypass;

	if (clknode_register(clkdom, clk) == NULL)
		return (1);

	return (0);
}