1d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2734f69a7SPaul Walmsley /*
3734f69a7SPaul Walmsley * OMAP2xxx DVFS virtual clock functions
4734f69a7SPaul Walmsley *
5baa689b8SPaul Walmsley * Copyright (C) 2005-2008, 2012 Texas Instruments, Inc.
6734f69a7SPaul Walmsley * Copyright (C) 2004-2010 Nokia Corporation
7734f69a7SPaul Walmsley *
8734f69a7SPaul Walmsley * Contacts:
9734f69a7SPaul Walmsley * Richard Woodruff <r-woodruff2@ti.com>
10734f69a7SPaul Walmsley * Paul Walmsley
11734f69a7SPaul Walmsley *
12734f69a7SPaul Walmsley * Based on earlier work by Tuukka Tikkanen, Tony Lindgren,
13734f69a7SPaul Walmsley * Gordon McNutt and RidgeRun, Inc.
14734f69a7SPaul Walmsley *
15734f69a7SPaul Walmsley * XXX Some of this code should be replaceable by the upcoming OPP layer
16734f69a7SPaul Walmsley * code. However, some notion of "rate set" is probably still necessary
17734f69a7SPaul Walmsley * for OMAP2xxx at least. Rate sets should be generalized so they can be
18734f69a7SPaul Walmsley * used for any OMAP chip, not just OMAP2xxx. In particular, Richard Woodruff
19734f69a7SPaul Walmsley * has in the past expressed a preference to use rate sets for OPP changes,
20734f69a7SPaul Walmsley * rather than dynamically recalculating the clock tree, so if someone wants
21734f69a7SPaul Walmsley * this badly enough to write the code to handle it, we should support it
22734f69a7SPaul Walmsley * as an option.
23734f69a7SPaul Walmsley */
24734f69a7SPaul Walmsley #undef DEBUG
25734f69a7SPaul Walmsley
26734f69a7SPaul Walmsley #include <linux/kernel.h>
27734f69a7SPaul Walmsley #include <linux/errno.h>
28734f69a7SPaul Walmsley #include <linux/clk.h>
29734f69a7SPaul Walmsley #include <linux/io.h>
30734f69a7SPaul Walmsley #include <linux/cpufreq.h>
315a0e3ad6STejun Heo #include <linux/slab.h>
32734f69a7SPaul Walmsley
33dbc04161STony Lindgren #include "soc.h"
34734f69a7SPaul Walmsley #include "clock.h"
35734f69a7SPaul Walmsley #include "clock2xxx.h"
36734f69a7SPaul Walmsley #include "opp2xxx.h"
37d9a16f9aSPaul Walmsley #include "cm2xxx.h"
38734f69a7SPaul Walmsley #include "cm-regbits-24xx.h"
393e6ece13SPaul Walmsley #include "sdrc.h"
40bf027ca1STony Lindgren #include "sram.h"
41734f69a7SPaul Walmsley
42cbcf7833SArnd Bergmann static u16 cpu_mask;
43cbcf7833SArnd Bergmann
44734f69a7SPaul Walmsley const struct prcm_config *curr_prcm_set;
45734f69a7SPaul Walmsley const struct prcm_config *rate_table;
46734f69a7SPaul Walmsley
47baa689b8SPaul Walmsley /*
48baa689b8SPaul Walmsley * sys_ck_rate: the rate of the external high-frequency clock
49baa689b8SPaul Walmsley * oscillator on the board. Set by the SoC-specific clock init code.
50baa689b8SPaul Walmsley * Once set during a boot, will not change.
51baa689b8SPaul Walmsley */
52baa689b8SPaul Walmsley static unsigned long sys_ck_rate;
53baa689b8SPaul Walmsley
54734f69a7SPaul Walmsley /**
55734f69a7SPaul Walmsley * omap2_table_mpu_recalc - just return the MPU speed
56734f69a7SPaul Walmsley * @clk: virt_prcm_set struct clk
57734f69a7SPaul Walmsley *
58734f69a7SPaul Walmsley * Set virt_prcm_set's rate to the mpu_speed field of the current PRCM set.
59734f69a7SPaul Walmsley */
omap2_table_mpu_recalc(struct clk_hw * clk,unsigned long parent_rate)60cbcf7833SArnd Bergmann static unsigned long omap2_table_mpu_recalc(struct clk_hw *clk,
61ed1ebc49SRajendra Nayak unsigned long parent_rate)
62734f69a7SPaul Walmsley {
63734f69a7SPaul Walmsley return curr_prcm_set->mpu_speed;
64734f69a7SPaul Walmsley }
65734f69a7SPaul Walmsley
66734f69a7SPaul Walmsley /*
67734f69a7SPaul Walmsley * Look for a rate equal or less than the target rate given a configuration set.
68734f69a7SPaul Walmsley *
69734f69a7SPaul Walmsley * What's not entirely clear is "which" field represents the key field.
70734f69a7SPaul Walmsley * Some might argue L3-DDR, others ARM, others IVA. This code is simple and
71734f69a7SPaul Walmsley * just uses the ARM rates.
72734f69a7SPaul Walmsley */
omap2_round_to_table_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)73cbcf7833SArnd Bergmann static long omap2_round_to_table_rate(struct clk_hw *hw, unsigned long rate,
74ed1ebc49SRajendra Nayak unsigned long *parent_rate)
75734f69a7SPaul Walmsley {
76734f69a7SPaul Walmsley const struct prcm_config *ptr;
77baa689b8SPaul Walmsley long highest_rate;
78734f69a7SPaul Walmsley
79734f69a7SPaul Walmsley highest_rate = -EINVAL;
80734f69a7SPaul Walmsley
81734f69a7SPaul Walmsley for (ptr = rate_table; ptr->mpu_speed; ptr++) {
82734f69a7SPaul Walmsley if (!(ptr->flags & cpu_mask))
83734f69a7SPaul Walmsley continue;
84baa689b8SPaul Walmsley if (ptr->xtal_speed != sys_ck_rate)
85734f69a7SPaul Walmsley continue;
86734f69a7SPaul Walmsley
87734f69a7SPaul Walmsley highest_rate = ptr->mpu_speed;
88734f69a7SPaul Walmsley
89734f69a7SPaul Walmsley /* Can check only after xtal frequency check */
90734f69a7SPaul Walmsley if (ptr->mpu_speed <= rate)
91734f69a7SPaul Walmsley break;
92734f69a7SPaul Walmsley }
93734f69a7SPaul Walmsley return highest_rate;
94734f69a7SPaul Walmsley }
95734f69a7SPaul Walmsley
96734f69a7SPaul Walmsley /* Sets basic clocks based on the specified rate */
omap2_select_table_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)97cbcf7833SArnd Bergmann static int omap2_select_table_rate(struct clk_hw *hw, unsigned long rate,
98ed1ebc49SRajendra Nayak unsigned long parent_rate)
99734f69a7SPaul Walmsley {
100cd6e9db2STero Kristo u32 cur_rate, done_rate, bypass = 0;
101734f69a7SPaul Walmsley const struct prcm_config *prcm;
102734f69a7SPaul Walmsley unsigned long found_speed = 0;
103734f69a7SPaul Walmsley unsigned long flags;
104734f69a7SPaul Walmsley
105734f69a7SPaul Walmsley for (prcm = rate_table; prcm->mpu_speed; prcm++) {
106734f69a7SPaul Walmsley if (!(prcm->flags & cpu_mask))
107734f69a7SPaul Walmsley continue;
108734f69a7SPaul Walmsley
109baa689b8SPaul Walmsley if (prcm->xtal_speed != sys_ck_rate)
110734f69a7SPaul Walmsley continue;
111734f69a7SPaul Walmsley
112734f69a7SPaul Walmsley if (prcm->mpu_speed <= rate) {
113734f69a7SPaul Walmsley found_speed = prcm->mpu_speed;
114734f69a7SPaul Walmsley break;
115734f69a7SPaul Walmsley }
116734f69a7SPaul Walmsley }
117734f69a7SPaul Walmsley
118734f69a7SPaul Walmsley if (!found_speed) {
119734f69a7SPaul Walmsley printk(KERN_INFO "Could not set MPU rate to %luMHz\n",
120734f69a7SPaul Walmsley rate / 1000000);
121734f69a7SPaul Walmsley return -EINVAL;
122734f69a7SPaul Walmsley }
123734f69a7SPaul Walmsley
124734f69a7SPaul Walmsley curr_prcm_set = prcm;
1255f039377SPaul Walmsley cur_rate = omap2xxx_clk_get_core_rate();
126734f69a7SPaul Walmsley
127734f69a7SPaul Walmsley if (prcm->dpll_speed == cur_rate / 2) {
128734f69a7SPaul Walmsley omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL, 1);
129734f69a7SPaul Walmsley } else if (prcm->dpll_speed == cur_rate * 2) {
130734f69a7SPaul Walmsley omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
131734f69a7SPaul Walmsley } else if (prcm->dpll_speed != cur_rate) {
132734f69a7SPaul Walmsley local_irq_save(flags);
133734f69a7SPaul Walmsley
134734f69a7SPaul Walmsley if (prcm->dpll_speed == prcm->xtal_speed)
135734f69a7SPaul Walmsley bypass = 1;
136734f69a7SPaul Walmsley
137734f69a7SPaul Walmsley if ((prcm->cm_clksel2_pll & OMAP24XX_CORE_CLK_SRC_MASK) ==
138734f69a7SPaul Walmsley CORE_CLK_SRC_DPLL_X2)
139734f69a7SPaul Walmsley done_rate = CORE_CLK_SRC_DPLL_X2;
140734f69a7SPaul Walmsley else
141734f69a7SPaul Walmsley done_rate = CORE_CLK_SRC_DPLL;
142734f69a7SPaul Walmsley
143cd6e9db2STero Kristo omap2xxx_cm_set_mod_dividers(prcm->cm_clksel_mpu,
144cd6e9db2STero Kristo prcm->cm_clksel_dsp,
145cd6e9db2STero Kristo prcm->cm_clksel_gfx,
146cd6e9db2STero Kristo prcm->cm_clksel1_core,
147cd6e9db2STero Kristo prcm->cm_clksel_mdm);
148734f69a7SPaul Walmsley
149734f69a7SPaul Walmsley /* x2 to enter omap2xxx_sdrc_init_params() */
150734f69a7SPaul Walmsley omap2xxx_sdrc_reprogram(CORE_CLK_SRC_DPLL_X2, 1);
151734f69a7SPaul Walmsley
152734f69a7SPaul Walmsley omap2_set_prcm(prcm->cm_clksel1_pll, prcm->base_sdrc_rfr,
153734f69a7SPaul Walmsley bypass);
154734f69a7SPaul Walmsley
155734f69a7SPaul Walmsley omap2xxx_sdrc_init_params(omap2xxx_sdrc_dll_is_unlocked());
156734f69a7SPaul Walmsley omap2xxx_sdrc_reprogram(done_rate, 0);
157734f69a7SPaul Walmsley
158734f69a7SPaul Walmsley local_irq_restore(flags);
159734f69a7SPaul Walmsley }
160734f69a7SPaul Walmsley
161734f69a7SPaul Walmsley return 0;
162734f69a7SPaul Walmsley }
163baa689b8SPaul Walmsley
164baa689b8SPaul Walmsley /**
165*e0174a22SRandy Dunlap * omap2xxx_clkt_vps_check_bootloader_rates - determine which of the rate
166baa689b8SPaul Walmsley * table sets matches the current CORE DPLL hardware rate
167baa689b8SPaul Walmsley *
168baa689b8SPaul Walmsley * Check the MPU rate set by bootloader. Sets the 'curr_prcm_set'
169baa689b8SPaul Walmsley * global to point to the active rate set when found; otherwise, sets
170baa689b8SPaul Walmsley * it to NULL. No return value;
171baa689b8SPaul Walmsley */
omap2xxx_clkt_vps_check_bootloader_rates(void)172cbcf7833SArnd Bergmann static void omap2xxx_clkt_vps_check_bootloader_rates(void)
173baa689b8SPaul Walmsley {
174baa689b8SPaul Walmsley const struct prcm_config *prcm = NULL;
175baa689b8SPaul Walmsley unsigned long rate;
176baa689b8SPaul Walmsley
177baa689b8SPaul Walmsley rate = omap2xxx_clk_get_core_rate();
178baa689b8SPaul Walmsley for (prcm = rate_table; prcm->mpu_speed; prcm++) {
179baa689b8SPaul Walmsley if (!(prcm->flags & cpu_mask))
180baa689b8SPaul Walmsley continue;
181baa689b8SPaul Walmsley if (prcm->xtal_speed != sys_ck_rate)
182baa689b8SPaul Walmsley continue;
183baa689b8SPaul Walmsley if (prcm->dpll_speed <= rate)
184baa689b8SPaul Walmsley break;
185baa689b8SPaul Walmsley }
186baa689b8SPaul Walmsley curr_prcm_set = prcm;
187baa689b8SPaul Walmsley }
188baa689b8SPaul Walmsley
189baa689b8SPaul Walmsley /**
190baa689b8SPaul Walmsley * omap2xxx_clkt_vps_late_init - store a copy of the sys_ck rate
191baa689b8SPaul Walmsley *
192baa689b8SPaul Walmsley * Store a copy of the sys_ck rate for later use by the OMAP2xxx DVFS
193baa689b8SPaul Walmsley * code. (The sys_ck rate does not -- or rather, must not -- change
194baa689b8SPaul Walmsley * during kernel runtime.) Must be called after we have a valid
195baa689b8SPaul Walmsley * sys_ck rate, but before the virt_prcm_set clock rate is
196baa689b8SPaul Walmsley * recalculated. No return value.
197baa689b8SPaul Walmsley */
omap2xxx_clkt_vps_late_init(void)198cbcf7833SArnd Bergmann static void omap2xxx_clkt_vps_late_init(void)
199baa689b8SPaul Walmsley {
200baa689b8SPaul Walmsley struct clk *c;
201baa689b8SPaul Walmsley
202baa689b8SPaul Walmsley c = clk_get(NULL, "sys_ck");
203baa689b8SPaul Walmsley if (IS_ERR(c)) {
204baa689b8SPaul Walmsley WARN(1, "could not locate sys_ck\n");
205baa689b8SPaul Walmsley } else {
206baa689b8SPaul Walmsley sys_ck_rate = clk_get_rate(c);
207baa689b8SPaul Walmsley clk_put(c);
208baa689b8SPaul Walmsley }
209baa689b8SPaul Walmsley }
21061f25ca7STero Kristo
21161f25ca7STero Kristo #ifdef CONFIG_OF
21261f25ca7STero Kristo #include <linux/clk-provider.h>
21361f25ca7STero Kristo #include <linux/clkdev.h>
21461f25ca7STero Kristo
21561f25ca7STero Kristo static const struct clk_ops virt_prcm_set_ops = {
21661f25ca7STero Kristo .recalc_rate = &omap2_table_mpu_recalc,
21761f25ca7STero Kristo .set_rate = &omap2_select_table_rate,
21861f25ca7STero Kristo .round_rate = &omap2_round_to_table_rate,
21961f25ca7STero Kristo };
22061f25ca7STero Kristo
22161f25ca7STero Kristo /**
22261f25ca7STero Kristo * omap2xxx_clkt_vps_init - initialize virt_prcm_set clock
22361f25ca7STero Kristo *
22461f25ca7STero Kristo * Does a manual init for the virtual prcm DVFS clock for OMAP2. This
22561f25ca7STero Kristo * function is called only from omap2 DT clock init, as the virtual
22661f25ca7STero Kristo * node is not modelled in the DT clock data.
22761f25ca7STero Kristo */
omap2xxx_clkt_vps_init(void)22861f25ca7STero Kristo void omap2xxx_clkt_vps_init(void)
22961f25ca7STero Kristo {
23061f25ca7STero Kristo struct clk_init_data init = { NULL };
23161f25ca7STero Kristo struct clk_hw_omap *hw = NULL;
23261f25ca7STero Kristo struct clk *clk;
23361f25ca7STero Kristo const char *parent_name = "mpu_ck";
23461f25ca7STero Kristo
23561f25ca7STero Kristo omap2xxx_clkt_vps_late_init();
23661f25ca7STero Kristo omap2xxx_clkt_vps_check_bootloader_rates();
23761f25ca7STero Kristo
23861f25ca7STero Kristo hw = kzalloc(sizeof(*hw), GFP_KERNEL);
239b2e01129SRussell King if (!hw)
240c3f244d5SYouling Tang return;
24161f25ca7STero Kristo init.name = "virt_prcm_set";
24261f25ca7STero Kristo init.ops = &virt_prcm_set_ops;
24361f25ca7STero Kristo init.parent_names = &parent_name;
24461f25ca7STero Kristo init.num_parents = 1;
24561f25ca7STero Kristo
24661f25ca7STero Kristo hw->hw.init = &init;
24761f25ca7STero Kristo
24861f25ca7STero Kristo clk = clk_register(NULL, &hw->hw);
2493c5902d2SYouling Tang if (IS_ERR(clk)) {
2503c5902d2SYouling Tang printk(KERN_ERR "Failed to register clock\n");
2513c5902d2SYouling Tang kfree(hw);
2523c5902d2SYouling Tang return;
2533c5902d2SYouling Tang }
2543c5902d2SYouling Tang
255b2e01129SRussell King clkdev_create(clk, "cpufreq_ck", NULL);
25661f25ca7STero Kristo }
25761f25ca7STero Kristo #endif
258