blob: 65fa6b5b139f31c8c2f098cc306c25892f4c81d0 [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
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020017#include <asm/io.h>
18#include <malloc.h>
19#include <clk-uclass.h>
20#include <dm/device.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020022#include <linux/bug.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020023#include <linux/clk-provider.h>
24#include <clk.h>
25#include "clk.h"
Simon Glassd66c5f72020-02-03 07:36:15 -070026#include <linux/err.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020027
28#define UBOOT_DM_CLK_IMX_GATE2 "imx_clk_gate2"
29
30struct clk_gate2 {
31 struct clk clk;
32 void __iomem *reg;
33 u8 bit_idx;
34 u8 cgr_val;
35 u8 flags;
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020036 unsigned int *share_count;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020037};
38
39#define to_clk_gate2(_clk) container_of(_clk, struct clk_gate2, clk)
40
41static int clk_gate2_enable(struct clk *clk)
42{
Sean Andersoncfc2f022020-06-24 06:41:06 -040043 struct clk_gate2 *gate = to_clk_gate2(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020044 u32 reg;
45
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020046 if (gate->share_count && (*gate->share_count)++ > 0)
47 return 0;
48
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020049 reg = readl(gate->reg);
50 reg &= ~(3 << gate->bit_idx);
51 reg |= gate->cgr_val << gate->bit_idx;
52 writel(reg, gate->reg);
53
54 return 0;
55}
56
57static int clk_gate2_disable(struct clk *clk)
58{
Sean Andersoncfc2f022020-06-24 06:41:06 -040059 struct clk_gate2 *gate = to_clk_gate2(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020060 u32 reg;
61
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020062 if (gate->share_count) {
63 if (WARN_ON(*gate->share_count == 0))
64 return 0;
65 else if (--(*gate->share_count) > 0)
66 return 0;
67 }
68
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020069 reg = readl(gate->reg);
70 reg &= ~(3 << gate->bit_idx);
71 writel(reg, gate->reg);
72
73 return 0;
74}
75
Peng Faneaffd472019-07-31 07:01:45 +000076static ulong clk_gate2_set_rate(struct clk *clk, ulong rate)
77{
78 struct clk *parent = clk_get_parent(clk);
79
80 if (parent)
81 return clk_set_rate(parent, rate);
82
83 return -ENODEV;
84}
85
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020086static const struct clk_ops clk_gate2_ops = {
Peng Faneaffd472019-07-31 07:01:45 +000087 .set_rate = clk_gate2_set_rate,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020088 .enable = clk_gate2_enable,
89 .disable = clk_gate2_disable,
90 .get_rate = clk_generic_get_rate,
91};
92
93struct clk *clk_register_gate2(struct device *dev, const char *name,
94 const char *parent_name, unsigned long flags,
95 void __iomem *reg, u8 bit_idx, u8 cgr_val,
Michael Trimarchi29c56cf2022-08-30 16:41:38 +020096 u8 clk_gate2_flags, unsigned int *share_count)
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020097{
98 struct clk_gate2 *gate;
99 struct clk *clk;
100 int ret;
101
102 gate = kzalloc(sizeof(*gate), GFP_KERNEL);
103 if (!gate)
104 return ERR_PTR(-ENOMEM);
105
106 gate->reg = reg;
107 gate->bit_idx = bit_idx;
108 gate->cgr_val = cgr_val;
109 gate->flags = clk_gate2_flags;
Michael Trimarchi29c56cf2022-08-30 16:41:38 +0200110 gate->share_count = share_count;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200111
112 clk = &gate->clk;
113
114 ret = clk_register(clk, UBOOT_DM_CLK_IMX_GATE2, name, parent_name);
115 if (ret) {
116 kfree(gate);
117 return ERR_PTR(ret);
118 }
119
120 return clk;
121}
122
123U_BOOT_DRIVER(clk_gate2) = {
124 .name = UBOOT_DM_CLK_IMX_GATE2,
125 .id = UCLASS_CLK,
126 .ops = &clk_gate2_ops,
127 .flags = DM_FLAG_PRE_RELOC,
128};