blob: a3a1fdd3b2fe67157a20f6f9df03210029f6eab6 [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>
15#include <linux/clk-provider.h>
16#include <clk.h>
17#include "clk.h"
18
19#define UBOOT_DM_CLK_GATE "clk_gate"
20
21/**
22 * DOC: basic gatable clock which can gate and ungate it's output
23 *
24 * Traits of this clock:
25 * prepare - clk_(un)prepare only ensures parent is (un)prepared
26 * enable - clk_enable and clk_disable are functional & control gating
27 * rate - inherits rate from parent. No clk_set_rate support
28 * parent - fixed parent. No clk_set_parent support
29 */
30
31/*
32 * It works on following logic:
33 *
34 * For enabling clock, enable = 1
35 * set2dis = 1 -> clear bit -> set = 0
36 * set2dis = 0 -> set bit -> set = 1
37 *
38 * For disabling clock, enable = 0
39 * set2dis = 1 -> set bit -> set = 1
40 * set2dis = 0 -> clear bit -> set = 0
41 *
42 * So, result is always: enable xor set2dis.
43 */
44static void clk_gate_endisable(struct clk *clk, int enable)
45{
46 struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
47 dev_get_clk_ptr(clk->dev) : clk);
48 int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
49 u32 reg;
50
51 set ^= enable;
52
53 if (gate->flags & CLK_GATE_HIWORD_MASK) {
54 reg = BIT(gate->bit_idx + 16);
55 if (set)
56 reg |= BIT(gate->bit_idx);
57 } else {
58 reg = readl(gate->reg);
59
60 if (set)
61 reg |= BIT(gate->bit_idx);
62 else
63 reg &= ~BIT(gate->bit_idx);
64 }
65
66 writel(reg, gate->reg);
67}
68
69static int clk_gate_enable(struct clk *clk)
70{
71 clk_gate_endisable(clk, 1);
72
73 return 0;
74}
75
76static int clk_gate_disable(struct clk *clk)
77{
78 clk_gate_endisable(clk, 0);
79
80 return 0;
81}
82
83int clk_gate_is_enabled(struct clk *clk)
84{
85 struct clk_gate *gate = to_clk_gate(clk_dev_binded(clk) ?
86 dev_get_clk_ptr(clk->dev) : clk);
87 u32 reg;
88
89 reg = readl(gate->reg);
90
91 /* if a set bit disables this clk, flip it before masking */
92 if (gate->flags & CLK_GATE_SET_TO_DISABLE)
93 reg ^= BIT(gate->bit_idx);
94
95 reg &= BIT(gate->bit_idx);
96
97 return reg ? 1 : 0;
98}
99
100const struct clk_ops clk_gate_ops = {
101 .enable = clk_gate_enable,
102 .disable = clk_gate_disable,
103 .get_rate = clk_generic_get_rate,
104};
105
106struct clk *clk_register_gate(struct device *dev, const char *name,
107 const char *parent_name, unsigned long flags,
108 void __iomem *reg, u8 bit_idx,
109 u8 clk_gate_flags, spinlock_t *lock)
110{
111 struct clk_gate *gate;
112 struct clk *clk;
113 int ret;
114
115 if (clk_gate_flags & CLK_GATE_HIWORD_MASK) {
116 if (bit_idx > 15) {
117 pr_err("gate bit exceeds LOWORD field\n");
118 return ERR_PTR(-EINVAL);
119 }
120 }
121
122 /* allocate the gate */
123 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
124 if (!gate)
125 return ERR_PTR(-ENOMEM);
126
127 /* struct clk_gate assignments */
128 gate->reg = reg;
129 gate->bit_idx = bit_idx;
130 gate->flags = clk_gate_flags;
131
132 clk = &gate->clk;
133
134 ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name);
135 if (ret) {
136 kfree(gate);
137 return ERR_PTR(ret);
138 }
139
140 return clk;
141}
142
143U_BOOT_DRIVER(clk_gate) = {
144 .name = UBOOT_DM_CLK_GATE,
145 .id = UCLASS_CLK,
146 .ops = &clk_gate_ops,
147 .flags = DM_FLAG_PRE_RELOC,
148};