blob: 6415c2f1b91dfa661ca716a74e479e5dbbdfd95a [file] [log] [blame]
Peng Fan0f085152019-07-31 07:01:34 +00001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
4 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
5 * Copyright 2019 NXP
6 *
7 * Gated clock implementation
8 */
9
10#include <common.h>
11#include <asm/io.h>
12#include <malloc.h>
13#include <clk-uclass.h>
14#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070015#include <dm/devres.h>
Peng Fan0f085152019-07-31 07:01:34 +000016#include <linux/clk-provider.h>
17#include <clk.h>
18#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Peng Fan0f085152019-07-31 07:01:34 +000020
21#define UBOOT_DM_CLK_GATE "clk_gate"
22
23/**
24 * DOC: basic gatable clock which can gate and ungate it's output
25 *
26 * Traits of this clock:
27 * prepare - clk_(un)prepare only ensures parent is (un)prepared
28 * enable - clk_enable and clk_disable are functional & control gating
29 * rate - inherits rate from parent. No clk_set_rate support
30 * parent - fixed parent. No clk_set_parent support
31 */
32
33/*
34 * It works on following logic:
35 *
36 * For enabling clock, enable = 1
37 * set2dis = 1 -> clear bit -> set = 0
38 * set2dis = 0 -> set bit -> set = 1
39 *
40 * For disabling clock, enable = 0
41 * set2dis = 1 -> set bit -> set = 1
42 * set2dis = 0 -> clear bit -> set = 0
43 *
44 * So, result is always: enable xor set2dis.
45 */
46static void clk_gate_endisable(struct clk *clk, int enable)
47{
48 struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
49 dev_get_clk_ptr(clk->dev) : clk);
50 int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
51 u32 reg;
52
53 set ^= enable;
54
55 if (gate->flags & CLK_GATE_HIWORD_MASK) {
56 reg = BIT(gate->bit_idx + 16);
57 if (set)
58 reg |= BIT(gate->bit_idx);
59 } else {
Peng Fan3b7f3ae2019-07-31 07:01:57 +000060#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
61 reg = gate->io_gate_val;
62#else
Peng Fan0f085152019-07-31 07:01:34 +000063 reg = readl(gate->reg);
Peng Fan3b7f3ae2019-07-31 07:01:57 +000064#endif
Peng Fan0f085152019-07-31 07:01:34 +000065
66 if (set)
67 reg |= BIT(gate->bit_idx);
68 else
69 reg &= ~BIT(gate->bit_idx);
70 }
71
72 writel(reg, gate->reg);
73}
74
75static int clk_gate_enable(struct clk *clk)
76{
77 clk_gate_endisable(clk, 1);
78
79 return 0;
80}
81
82static int clk_gate_disable(struct clk *clk)
83{
84 clk_gate_endisable(clk, 0);
85
86 return 0;
87}
88
89int clk_gate_is_enabled(struct clk *clk)
90{
91 struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
92 dev_get_clk_ptr(clk->dev) : clk);
93 u32 reg;
94
Peng Fan3b7f3ae2019-07-31 07:01:57 +000095#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
96 reg = gate->io_gate_val;
97#else
Peng Fan0f085152019-07-31 07:01:34 +000098 reg = readl(gate->reg);
Peng Fan3b7f3ae2019-07-31 07:01:57 +000099#endif
Peng Fan0f085152019-07-31 07:01:34 +0000100
101 /* if a set bit disables this clk, flip it before masking */
102 if (gate->flags & CLK_GATE_SET_TO_DISABLE)
103 reg ^= BIT(gate->bit_idx);
104
105 reg &= BIT(gate->bit_idx);
106
107 return reg ? 1 : 0;
108}
109
110const struct clk_ops clk_gate_ops = {
111 .enable = clk_gate_enable,
112 .disable = clk_gate_disable,
113 .get_rate = clk_generic_get_rate,
114};
115
116struct clk *clk_register_gate(struct device *dev, const char *name,
117 const char *parent_name, unsigned long flags,
118 void __iomem *reg, u8 bit_idx,
119 u8 clk_gate_flags, spinlock_t *lock)
120{
121 struct clk_gate *gate;
122 struct clk *clk;
123 int ret;
124
125 if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
126 if (bit_idx > 15) {
127 pr_err("gate bit exceeds LOWORD field\n");
128 return ERR_PTR(-EINVAL);
129 }
130 }
131
132 /* allocate the gate */
133 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
134 if (!gate)
135 return ERR_PTR(-ENOMEM);
136
137 /* struct clk_gate assignments */
138 gate->reg = reg;
139 gate->bit_idx = bit_idx;
140 gate->flags = clk_gate_flags;
Peng Fan3b7f3ae2019-07-31 07:01:57 +0000141#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
142 gate->io_gate_val = *(u32 *)reg;
143#endif
Peng Fan0f085152019-07-31 07:01:34 +0000144
145 clk = &gate->clk;
146
147 ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name);
148 if (ret) {
149 kfree(gate);
150 return ERR_PTR(ret);
151 }
152
153 return clk;
154}
155
156U_BOOT_DRIVER(clk_gate) = {
157 .name = UBOOT_DM_CLK_GATE,
158 .id = UCLASS_CLK,
159 .ops = &clk_gate_ops,
160 .flags = DM_FLAG_PRE_RELOC,
161};