blob: d7411f8f2822290f102d257faca5c46a0419f80a [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) 2011 Sascha Hauer, Pengutronix <s.hauer@pengutronix.de>
7 * Copyright (C) 2011 Richard Zhao, Linaro <richard.zhao@linaro.org>
8 * Copyright (C) 2011-2012 Mike Turquette, Linaro Ltd <mturquette@linaro.org>
9 *
10 * Simple multiplexer clock implementation
11 */
12
13/*
14 * U-Boot CCF porting node:
15 *
16 * The Linux kernel - as of tag: 5.0-rc3 is using also the imx_clk_fixup_mux()
17 * version of CCF mux. It is used on e.g. imx6q to provide fixes (like
18 * imx_cscmr1_fixup) for broken HW.
19 *
20 * At least for IMX6Q (but NOT IMX6QP) it is important when we set the parent
21 * clock.
22 */
23
Patrick Delaunay8767e792021-11-19 15:12:07 +010024#define LOG_CATEGORY UCLASS_CLK
25
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020026#include <clk.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020027#include <clk-uclass.h>
Patrick Delaunay8767e792021-11-19 15:12:07 +010028#include <log.h>
Patrick Delaunay283dadf2021-11-19 15:12:06 +010029#include <malloc.h>
30#include <asm/io.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020031#include <dm/device.h>
Patrick Delaunay8767e792021-11-19 15:12:07 +010032#include <dm/device_compat.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070033#include <dm/devres.h>
Lukasz Majewski2dbf94b2020-08-24 11:12:18 +020034#include <dm/uclass.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060035#include <linux/bitops.h>
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020036#include <linux/clk-provider.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070037#include <linux/err.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060038#include <linux/printk.h>
Patrick Delaunay283dadf2021-11-19 15:12:06 +010039
Dario Binacchi3b32e6a2020-05-02 17:58:31 +020040#include "clk.h"
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020041
42#define UBOOT_DM_CLK_CCF_MUX "ccf_clk_mux"
43
44int clk_mux_val_to_index(struct clk *clk, u32 *table, unsigned int flags,
45 unsigned int val)
46{
Sean Andersoncfc2f022020-06-24 06:41:06 -040047 struct clk_mux *mux = to_clk_mux(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020048 int num_parents = mux->num_parents;
49
50 if (table) {
51 int i;
52
53 for (i = 0; i < num_parents; i++)
54 if (table[i] == val)
55 return i;
56 return -EINVAL;
57 }
58
59 if (val && (flags & CLK_MUX_INDEX_BIT))
60 val = ffs(val) - 1;
61
62 if (val && (flags & CLK_MUX_INDEX_ONE))
63 val--;
64
65 if (val >= num_parents)
66 return -EINVAL;
67
68 return val;
69}
70
Peng Fan6a8c2ad2019-07-31 07:01:28 +000071unsigned int clk_mux_index_to_val(u32 *table, unsigned int flags, u8 index)
72{
73 unsigned int val = index;
74
75 if (table) {
76 val = table[index];
77 } else {
78 if (flags & CLK_MUX_INDEX_BIT)
79 val = 1 << index;
80
81 if (flags & CLK_MUX_INDEX_ONE)
82 val++;
83 }
84
85 return val;
86}
87
88u8 clk_mux_get_parent(struct clk *clk)
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020089{
Sean Andersoncfc2f022020-06-24 06:41:06 -040090 struct clk_mux *mux = to_clk_mux(clk);
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020091 u32 val;
92
Simon Glass0a6a0c42023-02-05 15:40:43 -070093#if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
Lukasz Majewski669b7732019-06-24 15:50:49 +020094 val = mux->io_mux_val;
95#else
96 val = readl(mux->reg);
97#endif
98 val >>= mux->shift;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +020099 val &= mux->mask;
100
101 return clk_mux_val_to_index(clk, mux->table, mux->flags, val);
102}
103
Michael Trimarchi6cfde412024-07-05 09:19:51 +0200104int clk_mux_fetch_parent_index(struct clk *clk, struct clk *parent)
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000105{
Sean Andersoncfc2f022020-06-24 06:41:06 -0400106 struct clk_mux *mux = to_clk_mux(clk);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000107
108 int i;
109
110 if (!parent)
111 return -EINVAL;
112
113 for (i = 0; i < mux->num_parents; i++) {
114 if (!strcmp(parent->dev->name, mux->parent_names[i]))
115 return i;
Marek Vasut4394a262025-03-23 16:58:33 +0100116 if (!strcmp(parent->dev->name,
117 clk_resolve_parent_clk(clk->dev,
118 mux->parent_names[i])))
119 return i;
120
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000121 }
122
123 return -EINVAL;
124}
125
126static int clk_mux_set_parent(struct clk *clk, struct clk *parent)
127{
Sean Andersoncfc2f022020-06-24 06:41:06 -0400128 struct clk_mux *mux = to_clk_mux(clk);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000129 int index;
130 u32 val;
131 u32 reg;
132
Michael Trimarchi6cfde412024-07-05 09:19:51 +0200133 index = clk_mux_fetch_parent_index(clk, parent);
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000134 if (index < 0) {
Patrick Delaunay8767e792021-11-19 15:12:07 +0100135 log_err("Could not fetch index\n");
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000136 return index;
137 }
138
139 val = clk_mux_index_to_val(mux->table, mux->flags, index);
140
141 if (mux->flags & CLK_MUX_HIWORD_MASK) {
142 reg = mux->mask << (mux->shift + 16);
143 } else {
Simon Glass0a6a0c42023-02-05 15:40:43 -0700144#if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200145 reg = mux->io_mux_val;
146#else
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000147 reg = readl(mux->reg);
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200148#endif
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000149 reg &= ~(mux->mask << mux->shift);
150 }
151 val = val << mux->shift;
152 reg |= val;
Simon Glass0a6a0c42023-02-05 15:40:43 -0700153#if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200154 mux->io_mux_val = reg;
155#else
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000156 writel(reg, mux->reg);
Dario Binacchi88ea8df2020-05-02 17:58:33 +0200157#endif
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000158
159 return 0;
160}
161
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200162const struct clk_ops clk_mux_ops = {
Dario Binacchi863efd82020-10-14 23:42:17 +0200163 .get_rate = clk_generic_get_rate,
Peng Fan6a8c2ad2019-07-31 07:01:28 +0000164 .set_parent = clk_mux_set_parent,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200165};
166
Marek Vasute1be4fc2025-03-23 16:58:32 +0100167struct clk *clk_register_mux(struct udevice *dev, const char *name,
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200168 const char * const *parent_names, u8 num_parents,
169 unsigned long flags,
Marek Vasutecb8b412025-03-23 16:58:31 +0100170 void __iomem *reg, u8 shift, u8 width,
171 u8 clk_mux_flags)
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200172{
Marek Vasutecb8b412025-03-23 16:58:31 +0100173 u32 mask = BIT(width) - 1;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200174 struct clk_mux *mux;
175 struct clk *clk;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200176 int ret;
177
178 if (clk_mux_flags & CLK_MUX_HIWORD_MASK) {
179 width = fls(mask) - ffs(mask) + 1;
180 if (width + shift > 16) {
Patrick Delaunay8767e792021-11-19 15:12:07 +0100181 dev_err(dev, "mux value exceeds LOWORD field\n");
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200182 return ERR_PTR(-EINVAL);
183 }
184 }
185
186 /* allocate the mux */
187 mux = kzalloc(sizeof(*mux), GFP_KERNEL);
188 if (!mux)
189 return ERR_PTR(-ENOMEM);
190
Michal Simek50fa1182023-05-17 09:17:16 +0200191 /* U-Boot specific assignments */
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200192 mux->parent_names = parent_names;
193 mux->num_parents = num_parents;
194
195 /* struct clk_mux assignments */
196 mux->reg = reg;
197 mux->shift = shift;
198 mux->mask = mask;
199 mux->flags = clk_mux_flags;
Marek Vasutecb8b412025-03-23 16:58:31 +0100200 mux->table = NULL;
Simon Glass0a6a0c42023-02-05 15:40:43 -0700201#if IS_ENABLED(CONFIG_SANDBOX_CLK_CCF)
Lukasz Majewski669b7732019-06-24 15:50:49 +0200202 mux->io_mux_val = *(u32 *)reg;
203#endif
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200204
205 clk = &mux->clk;
Dario Binacchi1a62dc12020-04-13 14:36:27 +0200206 clk->flags = flags;
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200207
208 /*
209 * Read the current mux setup - so we assign correct parent.
210 *
211 * Changing parent would require changing internals of udevice struct
Dario Binacchi5217bb12020-05-02 17:58:32 +0200212 * for the corresponding clock (to do that define .set_parent() method).
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200213 */
214 ret = clk_register(clk, UBOOT_DM_CLK_CCF_MUX, name,
Marek Vasut4394a262025-03-23 16:58:33 +0100215 clk_resolve_parent_clk(dev,
216 parent_names[clk_mux_get_parent(clk)]));
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200217 if (ret) {
218 kfree(mux);
219 return ERR_PTR(ret);
220 }
221
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200222 return clk;
223}
224
Lukasz Majewski4de44bb2019-06-24 15:50:45 +0200225U_BOOT_DRIVER(ccf_clk_mux) = {
226 .name = UBOOT_DM_CLK_CCF_MUX,
227 .id = UCLASS_CLK,
228 .ops = &clk_mux_ops,
229 .flags = DM_FLAG_PRE_RELOC,
230};