blob: 35bad7f5f731fbd2acf4e065ba1124e3f21cb9be [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasute11008b2018-01-15 16:44:39 +01002/*
3 * Renesas RCar Gen3 CPG MSSR driver
4 *
5 * Copyright (C) 2018 Marek Vasut <marek.vasut@gmail.com>
6 *
7 * Based on the following driver from Linux kernel:
8 * r8a7796 Clock Pulse Generator / Module Standby and Software Reset
9 *
10 * Copyright (C) 2016 Glider bvba
Marek Vasute11008b2018-01-15 16:44:39 +010011 */
Marek Vasute11008b2018-01-15 16:44:39 +010012#include <clk-uclass.h>
13#include <dm.h>
14#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060015#include <log.h>
Marek Vasute11008b2018-01-15 16:44:39 +010016#include <wait_bit.h>
17#include <asm/io.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Marek Vasute11008b2018-01-15 16:44:39 +010019
20#include <dt-bindings/clock/renesas-cpg-mssr.h>
21
22#include "renesas-cpg-mssr.h"
23
Marek Vasute11008b2018-01-15 16:44:39 +010024bool renesas_clk_is_mod(struct clk *clk)
25{
26 return (clk->id >> 16) == CPG_MOD;
27}
28
29int renesas_clk_get_mod(struct clk *clk, struct cpg_mssr_info *info,
30 const struct mssr_mod_clk **mssr)
31{
32 const unsigned long clkid = clk->id & 0xffff;
33 int i;
34
35 for (i = 0; i < info->mod_clk_size; i++) {
36 if (info->mod_clk[i].id !=
37 (info->mod_clk_base + MOD_CLK_PACK(clkid)))
38 continue;
39
40 *mssr = &info->mod_clk[i];
41 return 0;
42 }
43
44 return -ENODEV;
45}
46
47int renesas_clk_get_core(struct clk *clk, struct cpg_mssr_info *info,
48 const struct cpg_core_clk **core)
49{
50 const unsigned long clkid = clk->id & 0xffff;
51 int i;
52
53 for (i = 0; i < info->core_clk_size; i++) {
54 if (info->core_clk[i].id != clkid)
55 continue;
56
57 *core = &info->core_clk[i];
58 return 0;
59 }
60
61 return -ENODEV;
62}
63
64int renesas_clk_get_parent(struct clk *clk, struct cpg_mssr_info *info,
65 struct clk *parent)
66{
67 const struct cpg_core_clk *core;
68 const struct mssr_mod_clk *mssr;
69 int ret;
70
71 if (renesas_clk_is_mod(clk)) {
72 ret = renesas_clk_get_mod(clk, info, &mssr);
73 if (ret)
74 return ret;
75
76 parent->id = mssr->parent;
77 } else {
78 ret = renesas_clk_get_core(clk, info, &core);
79 if (ret)
80 return ret;
81
82 if (core->type == CLK_TYPE_IN)
83 parent->id = ~0; /* Top-level clock */
84 else
85 parent->id = core->parent;
86 }
87
88 parent->dev = clk->dev;
89
90 return 0;
91}
92
Hai Pham5460ee02020-05-22 10:39:04 +070093int renesas_clk_endisable(struct clk *clk, void __iomem *base,
94 struct cpg_mssr_info *info, bool enable)
Marek Vasute11008b2018-01-15 16:44:39 +010095{
96 const unsigned long clkid = clk->id & 0xffff;
97 const unsigned int reg = clkid / 100;
98 const unsigned int bit = clkid % 100;
99 const u32 bitmask = BIT(bit);
100
101 if (!renesas_clk_is_mod(clk))
102 return -EINVAL;
103
104 debug("%s[%i] MSTP %lu=%02u/%02u %s\n", __func__, __LINE__,
105 clkid, reg, bit, enable ? "ON" : "OFF");
106
107 if (enable) {
Hai Pham94803462020-11-05 22:30:37 +0700108 clrbits_le32(base + info->control_regs[reg], bitmask);
109 return wait_for_bit_le32(base + info->status_regs[reg],
Marek Vasute11008b2018-01-15 16:44:39 +0100110 bitmask, 0, 100, 0);
111 } else {
Hai Pham94803462020-11-05 22:30:37 +0700112 setbits_le32(base + info->control_regs[reg], bitmask);
Marek Vasute11008b2018-01-15 16:44:39 +0100113 return 0;
114 }
115}
116
117int renesas_clk_remove(void __iomem *base, struct cpg_mssr_info *info)
118{
119 unsigned int i;
120
121 /* Stop TMU0 */
122 clrbits_le32(TMU_BASE + TSTR0, TSTR0_STR0);
123
124 /* Stop module clock */
125 for (i = 0; i < info->mstp_table_size; i++) {
Hai Pham94803462020-11-05 22:30:37 +0700126 clrsetbits_le32(base + info->control_regs[i],
Marek Vasute11008b2018-01-15 16:44:39 +0100127 info->mstp_table[i].sdis,
128 info->mstp_table[i].sen);
Hai Pham86d59f32020-08-11 10:46:34 +0700129
Marek Vasutba2c7d22023-02-28 22:34:38 +0100130 if (info->reg_layout == CLK_REG_LAYOUT_RCAR_GEN4)
Hai Pham86d59f32020-08-11 10:46:34 +0700131 continue;
132
Marek Vasute11008b2018-01-15 16:44:39 +0100133 clrsetbits_le32(base + RMSTPCR(i),
134 info->mstp_table[i].rdis,
135 info->mstp_table[i].ren);
136 }
137
138 return 0;
139}