blob: da2723023778eff9ce1eac9ed8ec7f7fcdb436b5 [file] [log] [blame]
Lukasz Majewski4de44bb2019-06-24 15:50:45 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 *
6 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
7 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * Gated clock implementation
14 *
15 */
16
17#include <common.h>
18#include <asm/io.h>
19#include <malloc.h>
20#include <clk-uclass.h>
21#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <dm/devres.h>
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020023#include <linux/bug.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020024#include <linux/clk-provider.h>
25#include <clk.h>
26#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070027#include <linux/err.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020028
29#define UBOOT_DM_CLK_IMX_GATE2 "imx_clk_gate2"
30
31struct clk_gate2 {
32 struct clk clk;
33 void __iomem *reg;
34 u8 bit_idx;
35 u8 cgr_val;
36 u8 flags;
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020037 unsigned int *share_count;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020038};
39
40#define to_clk_gate2(_clk) container_of(_clk, struct clk_gate2, clk)
41
42static int clk_gate2_enable(struct clk *clk)
43{
Sean Andersoncfc2f022020-06-24 06:41:06 -040044 struct clk_gate2 *gate = to_clk_gate2(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020045 u32 reg;
46
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020047 if (gate->share_count && (*gate->share_count)++ > 0)
48 return 0;
49
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020050 reg = readl(gate->reg);
51 reg &= ~(3 << gate->bit_idx);
52 reg |= gate->cgr_val << gate->bit_idx;
53 writel(reg, gate->reg);
54
55 return 0;
56}
57
58static int clk_gate2_disable(struct clk *clk)
59{
Sean Andersoncfc2f022020-06-24 06:41:06 -040060 struct clk_gate2 *gate = to_clk_gate2(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020061 u32 reg;
62
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020063 if (gate->share_count) {
64 if (WARN_ON(*gate->share_count == 0))
65 return 0;
66 else if (--(*gate->share_count) > 0)
67 return 0;
68 }
69
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020070 reg = readl(gate->reg);
71 reg &= ~(3 << gate->bit_idx);
72 writel(reg, gate->reg);
73
74 return 0;
75}
76
Peng Faneaffd472019-07-31 07:01:45 +000077static ulong clk_gate2_set_rate(struct clk *clk, ulong rate)
78{
79 struct clk *parent = clk_get_parent(clk);
80
81 if (parent)
82 return clk_set_rate(parent, rate);
83
84 return -ENODEV;
85}
86
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020087static const struct clk_ops clk_gate2_ops = {
Peng Faneaffd472019-07-31 07:01:45 +000088 .set_rate = clk_gate2_set_rate,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020089 .enable = clk_gate2_enable,
90 .disable = clk_gate2_disable,
91 .get_rate = clk_generic_get_rate,
92};
93
94struct clk *clk_register_gate2(struct device *dev, const char *name,
95 const char *parent_name, unsigned long flags,
96 void __iomem *reg, u8 bit_idx, u8 cgr_val,
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020097 u8 clk_gate2_flags, unsigned int *share_count)
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020098{
99 struct clk_gate2 *gate;
100 struct clk *clk;
101 int ret;
102
103 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
104 if (!gate)
105 return ERR_PTR(-ENOMEM);
106
107 gate->reg = reg;
108 gate->bit_idx = bit_idx;
109 gate->cgr_val = cgr_val;
110 gate->flags = clk_gate2_flags;
Michael Trimarchi29c56cf2022-08-30 16:41:38 +0200111 gate->share_count = share_count;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200112
113 clk = &gate->clk;
114
115 ret = clk_register(clk, UBOOT_DM_CLK_IMX_GATE2, name, parent_name);
116 if (ret) {
117 kfree(gate);
118 return ERR_PTR(ret);
119 }
120
121 return clk;
122}
123
124U_BOOT_DRIVER(clk_gate2) = {
125 .name = UBOOT_DM_CLK_IMX_GATE2,
126 .id = UCLASS_CLK,
127 .ops = &clk_gate2_ops,
128 .flags = DM_FLAG_PRE_RELOC,
129};