blob: 708499d95aeab061010801802d7c139f91646b20 [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>
Patrick Delaunay283dadf2021-11-19 15:12:06 +010011#include <clk.h>
Peng Fan0f085152019-07-31 07:01:34 +000012#include <clk-uclass.h>
Patrick Delaunay283dadf2021-11-19 15:12:06 +010013#include <malloc.h>
14#include <asm/io.h>
Peng Fan0f085152019-07-31 07:01:34 +000015#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <dm/devres.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Peng Fan0f085152019-07-31 07:01:34 +000018#include <linux/clk-provider.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Peng Fan0f085152019-07-31 07:01:34 +000020
Patrick Delaunay283dadf2021-11-19 15:12:06 +010021#include "clk.h"
22
Peng Fan0f085152019-07-31 07:01:34 +000023#define UBOOT_DM_CLK_GATE "clk_gate"
24
25/**
26 * DOC: basic gatable clock which can gate and ungate it's output
27 *
28 * Traits of this clock:
29 * prepare - clk_(un)prepare only ensures parent is (un)prepared
30 * enable - clk_enable and clk_disable are functional & control gating
31 * rate - inherits rate from parent. No clk_set_rate support
32 * parent - fixed parent. No clk_set_parent support
33 */
34
35/*
36 * It works on following logic:
37 *
38 * For enabling clock, enable = 1
39 * set2dis = 1 -> clear bit -> set = 0
40 * set2dis = 0 -> set bit -> set = 1
41 *
42 * For disabling clock, enable = 0
43 * set2dis = 1 -> set bit -> set = 1
44 * set2dis = 0 -> clear bit -> set = 0
45 *
46 * So, result is always: enable xor set2dis.
47 */
48static void clk_gate_endisable(struct clk *clk, int enable)
49{
Sean Andersoncfc2f022020-06-24 06:41:06 -040050 struct clk_gate *gate = to_clk_gate(clk);
Peng Fan0f085152019-07-31 07:01:34 +000051 int set = gate->flags & CLK_GATE_SET_TO_DISABLE ? 1 : 0;
52 u32 reg;
53
54 set ^= enable;
55
56 if (gate->flags & CLK_GATE_HIWORD_MASK) {
57 reg = BIT(gate->bit_idx + 16);
58 if (set)
59 reg |= BIT(gate->bit_idx);
60 } else {
Peng Fan3b7f3ae2019-07-31 07:01:57 +000061#if CONFIG_IS_ENABLED(SANDBOX_CLK_CCF)
62 reg = gate->io_gate_val;
63#else
Peng Fan0f085152019-07-31 07:01:34 +000064 reg = readl(gate->reg);
Peng Fan3b7f3ae2019-07-31 07:01:57 +000065#endif
Peng Fan0f085152019-07-31 07:01:34 +000066
67 if (set)
68 reg |= BIT(gate->bit_idx);
69 else
70 reg &= ~BIT(gate->bit_idx);
71 }
72
73 writel(reg, gate->reg);
74}
75
76static int clk_gate_enable(struct clk *clk)
77{
78 clk_gate_endisable(clk, 1);
79
80 return 0;
81}
82
83static int clk_gate_disable(struct clk *clk)
84{
85 clk_gate_endisable(clk, 0);
86
87 return 0;
88}
89
90int clk_gate_is_enabled(struct clk *clk)
91{
Sean Andersoncfc2f022020-06-24 06:41:06 -040092 struct clk_gate *gate = to_clk_gate(clk);
Peng Fan0f085152019-07-31 07:01:34 +000093 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;
Dario Binacchi1a62dc12020-04-13 14:36:27 +0200146 clk->flags = flags;
Peng Fan0f085152019-07-31 07:01:34 +0000147
148 ret = clk_register(clk, UBOOT_DM_CLK_GATE, name, parent_name);
149 if (ret) {
150 kfree(gate);
151 return ERR_PTR(ret);
152 }
153
154 return clk;
155}
156
157U_BOOT_DRIVER(clk_gate) = {
158 .name = UBOOT_DM_CLK_GATE,
159 .id = UCLASS_CLK,
160 .ops = &clk_gate_ops,
161 .flags = DM_FLAG_PRE_RELOC,
162};