aboutsummaryrefslogtreecommitdiff
path: root/drivers/clk/altera/clk-mem-n5x.c
blob: 9bbe2cd0ca70751cd2682e7d844b17406536fd84 (plain)
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
128
129
130
131
132
133
134
135
136
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
 * Copyright (C) 2020-2022 Intel Corporation <www.intel.com>
 */

#include <common.h>
#include <asm/arch/clock_manager.h>
#include <asm/global_data.h>
#include <asm/io.h>
#include "clk-mem-n5x.h"
#include <clk-uclass.h>
#include <dm.h>
#include <dm/lists.h>
#include <dm/util.h>
#include <dt-bindings/clock/n5x-clock.h>

DECLARE_GLOBAL_DATA_PTR;

struct socfpga_mem_clk_plat {
	void __iomem *regs;
};

void clk_mem_wait_for_lock(struct socfpga_mem_clk_plat *plat, u32 mask)
{
	u32 inter_val;
	u32 retry = 0;

	do {
		inter_val = CM_REG_READL(plat, MEMCLKMGR_STAT) & mask;

		/* Wait for stable lock */
		if (inter_val == mask)
			retry++;
		else
			retry = 0;

		if (retry >= 10)
			return;
	} while (1);
}

/*
 * function to write the bypass register which requires a poll of the
 * busy bit
 */
void clk_mem_write_bypass_mempll(struct socfpga_mem_clk_plat *plat, u32 val)
{
	CM_REG_WRITEL(plat, val, MEMCLKMGR_MEMPLL_BYPASS);
}

/*
 * Setup clocks while making no assumptions about previous state of the clocks.
 */
static void clk_mem_basic_init(struct udevice *dev,
			       const struct cm_config * const cfg)
{
	struct socfpga_mem_clk_plat *plat = dev_get_plat(dev);

	if (!cfg)
		return;

	/* Put PLLs in bypass */
	clk_mem_write_bypass_mempll(plat, MEMCLKMGR_BYPASS_MEMPLL_ALL);

	/* Put PLLs in Reset */
	CM_REG_SETBITS(plat, MEMCLKMGR_MEMPLL_PLLCTRL,
		       MEMCLKMGR_PLLCTRL_BYPASS_MASK);

	/* setup mem PLL */
	CM_REG_WRITEL(plat, cfg->mem_memdiv, MEMCLKMGR_MEMPLL_MEMDIV);
	CM_REG_WRITEL(plat, cfg->mem_pllglob, MEMCLKMGR_MEMPLL_PLLGLOB);
	CM_REG_WRITEL(plat, cfg->mem_plldiv, MEMCLKMGR_MEMPLL_PLLDIV);
	CM_REG_WRITEL(plat, cfg->mem_plloutdiv, MEMCLKMGR_MEMPLL_PLLOUTDIV);

	/* Take PLL out of reset and power up */
	CM_REG_CLRBITS(plat, MEMCLKMGR_MEMPLL_PLLCTRL,
		       MEMCLKMGR_PLLCTRL_BYPASS_MASK);
}

static int socfpga_mem_clk_enable(struct clk *clk)
{
	const struct cm_config *cm_default_cfg = cm_get_default_config();
	struct socfpga_mem_clk_plat *plat = dev_get_plat(clk->dev);

	clk_mem_basic_init(clk->dev, cm_default_cfg);

	clk_mem_wait_for_lock(plat, MEMCLKMGR_STAT_ALLPLL_LOCKED_MASK);

	CM_REG_WRITEL(plat, CM_REG_READL(plat, MEMCLKMGR_MEMPLL_PLLGLOB) |
		      MEMCLKMGR_PLLGLOB_CLR_LOSTLOCK_BYPASS_MASK,
		      MEMCLKMGR_MEMPLL_PLLGLOB);

	/* Take all PLLs out of bypass */
	clk_mem_write_bypass_mempll(plat, 0);

	/* Clear the loss of lock bits (write 1 to clear) */
	CM_REG_CLRBITS(plat, MEMCLKMGR_INTRCLR,
		       MEMCLKMGR_INTER_MEMPLLLOST_MASK);

	/* Take all ping pong counters out of reset */
	CM_REG_CLRBITS(plat, MEMCLKMGR_MEMPLL_EXTCNTRST,
		       MEMCLKMGR_EXTCNTRST_ALLCNTRST);

	return 0;
}

static int socfpga_mem_clk_of_to_plat(struct udevice *dev)
{
	struct socfpga_mem_clk_plat *plat = dev_get_plat(dev);
	fdt_addr_t addr;

	addr = devfdt_get_addr(dev);
	if (addr == FDT_ADDR_T_NONE)
		return -EINVAL;
	plat->regs = (void __iomem *)addr;

	return 0;
}

static struct clk_ops socfpga_mem_clk_ops = {
	.enable		= socfpga_mem_clk_enable
};

static const struct udevice_id socfpga_mem_clk_match[] = {
	{ .compatible = "intel,n5x-mem-clkmgr" },
	{}
};

U_BOOT_DRIVER(socfpga_n5x_mem_clk) = {
	.name		= "mem-clk-n5x",
	.id		= UCLASS_CLK,
	.of_match	= socfpga_mem_clk_match,
	.ops		= &socfpga_mem_clk_ops,
	.of_to_plat     = socfpga_mem_clk_of_to_plat,
	.plat_auto	= sizeof(struct socfpga_mem_clk_plat),
};