Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 1 | // 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 Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
| 18 | #include <malloc.h> |
| 19 | #include <clk-uclass.h> |
| 20 | #include <dm/device.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 21 | #include <dm/devres.h> |
Michael Trimarchi | 29c56cf | 2022-08-30 16:41:38 +0200 | [diff] [blame] | 22 | #include <linux/bug.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 23 | #include <linux/clk-provider.h> |
| 24 | #include <clk.h> |
| 25 | #include "clk.h" |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 26 | #include <linux/err.h> |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 27 | |
| 28 | #define UBOOT_DM_CLK_IMX_GATE2 "imx_clk_gate2" |
| 29 | |
| 30 | struct clk_gate2 { |
| 31 | struct clk clk; |
| 32 | void __iomem *reg; |
| 33 | u8 bit_idx; |
| 34 | u8 cgr_val; |
| 35 | u8 flags; |
Michael Trimarchi | 29c56cf | 2022-08-30 16:41:38 +0200 | [diff] [blame] | 36 | unsigned int *share_count; |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | #define to_clk_gate2(_clk) container_of(_clk, struct clk_gate2, clk) |
| 40 | |
| 41 | static int clk_gate2_enable(struct clk *clk) |
| 42 | { |
Sean Anderson | cfc2f02 | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 43 | struct clk_gate2 *gate = to_clk_gate2(clk); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 44 | u32 reg; |
| 45 | |
Michael Trimarchi | 29c56cf | 2022-08-30 16:41:38 +0200 | [diff] [blame] | 46 | if (gate->share_count && (*gate->share_count)++ > 0) |
| 47 | return 0; |
| 48 | |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 49 | 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 | |
| 57 | static int clk_gate2_disable(struct clk *clk) |
| 58 | { |
Sean Anderson | cfc2f02 | 2020-06-24 06:41:06 -0400 | [diff] [blame] | 59 | struct clk_gate2 *gate = to_clk_gate2(clk); |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 60 | u32 reg; |
| 61 | |
Michael Trimarchi | 29c56cf | 2022-08-30 16:41:38 +0200 | [diff] [blame] | 62 | 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 Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 69 | reg = readl(gate->reg); |
| 70 | reg &= ~(3 << gate->bit_idx); |
| 71 | writel(reg, gate->reg); |
| 72 | |
| 73 | return 0; |
| 74 | } |
| 75 | |
Peng Fan | eaffd47 | 2019-07-31 07:01:45 +0000 | [diff] [blame] | 76 | static 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 Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 86 | static const struct clk_ops clk_gate2_ops = { |
Peng Fan | eaffd47 | 2019-07-31 07:01:45 +0000 | [diff] [blame] | 87 | .set_rate = clk_gate2_set_rate, |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 88 | .enable = clk_gate2_enable, |
| 89 | .disable = clk_gate2_disable, |
| 90 | .get_rate = clk_generic_get_rate, |
| 91 | }; |
| 92 | |
| 93 | struct 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 Trimarchi | 29c56cf | 2022-08-30 16:41:38 +0200 | [diff] [blame] | 96 | u8 clk_gate2_flags, unsigned int *share_count) |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 97 | { |
| 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 Trimarchi | 29c56cf | 2022-08-30 16:41:38 +0200 | [diff] [blame] | 110 | gate->share_count = share_count; |
Lukasz Majewski | 4de44bb | 2019-06-24 15:50:45 +0200 | [diff] [blame] | 111 | |
| 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 | |
| 123 | U_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 | }; |