Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 2 | /* |
| 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 Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 11 | */ |
| 12 | #include <common.h> |
| 13 | #include <clk-uclass.h> |
| 14 | #include <dm.h> |
| 15 | #include <errno.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 17 | #include <wait_bit.h> |
| 18 | #include <asm/io.h> |
Simon Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 19 | #include <linux/bitops.h> |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 20 | |
| 21 | #include <dt-bindings/clock/renesas-cpg-mssr.h> |
| 22 | |
| 23 | #include "renesas-cpg-mssr.h" |
| 24 | |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 25 | bool renesas_clk_is_mod(struct clk *clk) |
| 26 | { |
| 27 | return (clk->id >> 16) == CPG_MOD; |
| 28 | } |
| 29 | |
| 30 | int renesas_clk_get_mod(struct clk *clk, struct cpg_mssr_info *info, |
| 31 | const struct mssr_mod_clk **mssr) |
| 32 | { |
| 33 | const unsigned long clkid = clk->id & 0xffff; |
| 34 | int i; |
| 35 | |
| 36 | for (i = 0; i < info->mod_clk_size; i++) { |
| 37 | if (info->mod_clk[i].id != |
| 38 | (info->mod_clk_base + MOD_CLK_PACK(clkid))) |
| 39 | continue; |
| 40 | |
| 41 | *mssr = &info->mod_clk[i]; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | return -ENODEV; |
| 46 | } |
| 47 | |
| 48 | int renesas_clk_get_core(struct clk *clk, struct cpg_mssr_info *info, |
| 49 | const struct cpg_core_clk **core) |
| 50 | { |
| 51 | const unsigned long clkid = clk->id & 0xffff; |
| 52 | int i; |
| 53 | |
| 54 | for (i = 0; i < info->core_clk_size; i++) { |
| 55 | if (info->core_clk[i].id != clkid) |
| 56 | continue; |
| 57 | |
| 58 | *core = &info->core_clk[i]; |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | return -ENODEV; |
| 63 | } |
| 64 | |
| 65 | int renesas_clk_get_parent(struct clk *clk, struct cpg_mssr_info *info, |
| 66 | struct clk *parent) |
| 67 | { |
| 68 | const struct cpg_core_clk *core; |
| 69 | const struct mssr_mod_clk *mssr; |
| 70 | int ret; |
| 71 | |
| 72 | if (renesas_clk_is_mod(clk)) { |
| 73 | ret = renesas_clk_get_mod(clk, info, &mssr); |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
| 77 | parent->id = mssr->parent; |
| 78 | } else { |
| 79 | ret = renesas_clk_get_core(clk, info, &core); |
| 80 | if (ret) |
| 81 | return ret; |
| 82 | |
| 83 | if (core->type == CLK_TYPE_IN) |
| 84 | parent->id = ~0; /* Top-level clock */ |
| 85 | else |
| 86 | parent->id = core->parent; |
| 87 | } |
| 88 | |
| 89 | parent->dev = clk->dev; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
Hai Pham | f7f8d47 | 2020-05-22 10:39:04 +0700 | [diff] [blame] | 94 | int renesas_clk_endisable(struct clk *clk, void __iomem *base, |
| 95 | struct cpg_mssr_info *info, bool enable) |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 96 | { |
| 97 | const unsigned long clkid = clk->id & 0xffff; |
| 98 | const unsigned int reg = clkid / 100; |
| 99 | const unsigned int bit = clkid % 100; |
| 100 | const u32 bitmask = BIT(bit); |
| 101 | |
| 102 | if (!renesas_clk_is_mod(clk)) |
| 103 | return -EINVAL; |
| 104 | |
| 105 | debug("%s[%i] MSTP %lu=%02u/%02u %s\n", __func__, __LINE__, |
| 106 | clkid, reg, bit, enable ? "ON" : "OFF"); |
| 107 | |
| 108 | if (enable) { |
Hai Pham | d413214 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 109 | clrbits_le32(base + info->control_regs[reg], bitmask); |
| 110 | return wait_for_bit_le32(base + info->status_regs[reg], |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 111 | bitmask, 0, 100, 0); |
| 112 | } else { |
Hai Pham | d413214 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 113 | setbits_le32(base + info->control_regs[reg], bitmask); |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | int renesas_clk_remove(void __iomem *base, struct cpg_mssr_info *info) |
| 119 | { |
| 120 | unsigned int i; |
| 121 | |
| 122 | /* Stop TMU0 */ |
| 123 | clrbits_le32(TMU_BASE + TSTR0, TSTR0_STR0); |
| 124 | |
| 125 | /* Stop module clock */ |
| 126 | for (i = 0; i < info->mstp_table_size; i++) { |
Hai Pham | d413214 | 2020-11-05 22:30:37 +0700 | [diff] [blame] | 127 | clrsetbits_le32(base + info->control_regs[i], |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 128 | info->mstp_table[i].sdis, |
| 129 | info->mstp_table[i].sen); |
Hai Pham | b092f96 | 2020-08-11 10:46:34 +0700 | [diff] [blame] | 130 | |
| 131 | if (info->reg_layout == CLK_REG_LAYOUT_RCAR_V3U) |
| 132 | continue; |
| 133 | |
Marek Vasut | d262867 | 2018-01-15 16:44:39 +0100 | [diff] [blame] | 134 | clrsetbits_le32(base + RMSTPCR(i), |
| 135 | info->mstp_table[i].rdis, |
| 136 | info->mstp_table[i].ren); |
| 137 | } |
| 138 | |
| 139 | return 0; |
| 140 | } |