blob: 52f9438b92f2a5d165b548b323bacb8680588bae [file] [log] [blame]
Rajendra Nayak97f67892011-02-25 15:49:01 -07001/*
2 * OMAP4-specific DPLL control functions
3 *
4 * Copyright (C) 2011 Texas Instruments, Inc.
5 * Rajendra Nayak
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/clk.h>
15#include <linux/io.h>
16#include <linux/bitops.h>
17
Tony Lindgrendbc04162012-08-31 10:59:07 -070018#include "soc.h"
Rajendra Nayak97f67892011-02-25 15:49:01 -070019#include "clock.h"
Mike Turquettea1900f22011-10-07 00:52:58 -060020#include "clock44xx.h"
Rajendra Nayak97f67892011-02-25 15:49:01 -070021#include "cm-regbits-44xx.h"
22
Jon Hunter3ff51ed2012-12-15 01:35:46 -070023/*
24 * Maximum DPLL input frequency (FINT) and output frequency (FOUT) that
25 * can supported when using the DPLL low-power mode. Frequencies are
26 * defined in OMAP4430/60 Public TRM section 3.6.3.3.2 "Enable Control,
27 * Status, and Low-Power Operation Mode".
28 */
29#define OMAP4_DPLL_LP_FINT_MAX 1000000
30#define OMAP4_DPLL_LP_FOUT_MAX 100000000
31
Rajendra Nayak97f67892011-02-25 15:49:01 -070032/* Supported only on OMAP4 */
Mike Turquette32cc0022012-11-10 16:58:41 -070033int omap4_dpllmx_gatectrl_read(struct clk_hw_omap *clk)
Rajendra Nayak97f67892011-02-25 15:49:01 -070034{
35 u32 v;
36 u32 mask;
37
38 if (!clk || !clk->clksel_reg || !cpu_is_omap44xx())
39 return -EINVAL;
40
41 mask = clk->flags & CLOCK_CLKOUTX2 ?
42 OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
43 OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
44
Tero Kristo519ab8b2013-10-22 11:49:58 +030045 v = omap2_clk_readl(clk, clk->clksel_reg);
Rajendra Nayak97f67892011-02-25 15:49:01 -070046 v &= mask;
47 v >>= __ffs(mask);
48
49 return v;
50}
51
Mike Turquette32cc0022012-11-10 16:58:41 -070052void omap4_dpllmx_allow_gatectrl(struct clk_hw_omap *clk)
Rajendra Nayak97f67892011-02-25 15:49:01 -070053{
54 u32 v;
55 u32 mask;
56
57 if (!clk || !clk->clksel_reg || !cpu_is_omap44xx())
58 return;
59
60 mask = clk->flags & CLOCK_CLKOUTX2 ?
61 OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
62 OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
63
Tero Kristo519ab8b2013-10-22 11:49:58 +030064 v = omap2_clk_readl(clk, clk->clksel_reg);
Rajendra Nayak97f67892011-02-25 15:49:01 -070065 /* Clear the bit to allow gatectrl */
66 v &= ~mask;
Tero Kristo519ab8b2013-10-22 11:49:58 +030067 omap2_clk_writel(v, clk, clk->clksel_reg);
Rajendra Nayak97f67892011-02-25 15:49:01 -070068}
69
Mike Turquette32cc0022012-11-10 16:58:41 -070070void omap4_dpllmx_deny_gatectrl(struct clk_hw_omap *clk)
Rajendra Nayak97f67892011-02-25 15:49:01 -070071{
72 u32 v;
73 u32 mask;
74
75 if (!clk || !clk->clksel_reg || !cpu_is_omap44xx())
76 return;
77
78 mask = clk->flags & CLOCK_CLKOUTX2 ?
79 OMAP4430_DPLL_CLKOUTX2_GATE_CTRL_MASK :
80 OMAP4430_DPLL_CLKOUT_GATE_CTRL_MASK;
81
Tero Kristo519ab8b2013-10-22 11:49:58 +030082 v = omap2_clk_readl(clk, clk->clksel_reg);
Rajendra Nayak97f67892011-02-25 15:49:01 -070083 /* Set the bit to deny gatectrl */
84 v |= mask;
Tero Kristo519ab8b2013-10-22 11:49:58 +030085 omap2_clk_writel(v, clk, clk->clksel_reg);
Rajendra Nayak97f67892011-02-25 15:49:01 -070086}
Rajendra Nayak70db8a62011-02-25 15:49:02 -070087
Mike Turquette32cc0022012-11-10 16:58:41 -070088const struct clk_hw_omap_ops clkhwops_omap4_dpllmx = {
89 .allow_idle = omap4_dpllmx_allow_gatectrl,
90 .deny_idle = omap4_dpllmx_deny_gatectrl,
91};
Rajendra Nayak70db8a62011-02-25 15:49:02 -070092
Mike Turquettea1900f22011-10-07 00:52:58 -060093/**
Jon Hunter3ff51ed2012-12-15 01:35:46 -070094 * omap4_dpll_lpmode_recalc - compute DPLL low-power setting
95 * @dd: pointer to the dpll data structure
96 *
97 * Calculates if low-power mode can be enabled based upon the last
98 * multiplier and divider values calculated. If low-power mode can be
99 * enabled, then the bit to enable low-power mode is stored in the
100 * last_rounded_lpmode variable. This implementation is based upon the
101 * criteria for enabling low-power mode as described in the OMAP4430/60
102 * Public TRM section 3.6.3.3.2 "Enable Control, Status, and Low-Power
103 * Operation Mode".
104 */
105static void omap4_dpll_lpmode_recalc(struct dpll_data *dd)
106{
107 long fint, fout;
108
109 fint = __clk_get_rate(dd->clk_ref) / (dd->last_rounded_n + 1);
110 fout = fint * dd->last_rounded_m;
111
112 if ((fint < OMAP4_DPLL_LP_FINT_MAX) && (fout < OMAP4_DPLL_LP_FOUT_MAX))
113 dd->last_rounded_lpmode = 1;
114 else
115 dd->last_rounded_lpmode = 0;
116}
117
118/**
Mike Turquettea1900f22011-10-07 00:52:58 -0600119 * omap4_dpll_regm4xen_recalc - compute DPLL rate, considering REGM4XEN bit
120 * @clk: struct clk * of the DPLL to compute the rate for
121 *
122 * Compute the output rate for the OMAP4 DPLL represented by @clk.
123 * Takes the REGM4XEN bit into consideration, which is needed for the
124 * OMAP4 ABE DPLL. Returns the DPLL's output rate (before M-dividers)
125 * upon success, or 0 upon error.
126 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700127unsigned long omap4_dpll_regm4xen_recalc(struct clk_hw *hw,
128 unsigned long parent_rate)
129{
130 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
Mike Turquettea1900f22011-10-07 00:52:58 -0600131 u32 v;
132 unsigned long rate;
133 struct dpll_data *dd;
134
135 if (!clk || !clk->dpll_data)
136 return 0;
137
138 dd = clk->dpll_data;
139
140 rate = omap2_get_dpll_rate(clk);
141
142 /* regm4xen adds a multiplier of 4 to DPLL calculations */
Tero Kristo519ab8b2013-10-22 11:49:58 +0300143 v = omap2_clk_readl(clk, dd->control_reg);
Mike Turquettea1900f22011-10-07 00:52:58 -0600144 if (v & OMAP4430_DPLL_REGM4XEN_MASK)
145 rate *= OMAP4430_REGM4XEN_MULT;
146
147 return rate;
148}
149
150/**
151 * omap4_dpll_regm4xen_round_rate - round DPLL rate, considering REGM4XEN bit
152 * @clk: struct clk * of the DPLL to round a rate for
153 * @target_rate: the desired rate of the DPLL
154 *
155 * Compute the rate that would be programmed into the DPLL hardware
156 * for @clk if set_rate() were to be provided with the rate
157 * @target_rate. Takes the REGM4XEN bit into consideration, which is
158 * needed for the OMAP4 ABE DPLL. Returns the rounded rate (before
159 * M-dividers) upon success, -EINVAL if @clk is null or not a DPLL, or
160 * ~0 if an error occurred in omap2_dpll_round_rate().
161 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700162long omap4_dpll_regm4xen_round_rate(struct clk_hw *hw,
163 unsigned long target_rate,
164 unsigned long *parent_rate)
165{
166 struct clk_hw_omap *clk = to_clk_hw_omap(hw);
Mike Turquettea1900f22011-10-07 00:52:58 -0600167 struct dpll_data *dd;
168 long r;
169
170 if (!clk || !clk->dpll_data)
171 return -EINVAL;
172
173 dd = clk->dpll_data;
174
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700175 dd->last_rounded_m4xen = 0;
Mike Turquettea1900f22011-10-07 00:52:58 -0600176
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700177 /*
178 * First try to compute the DPLL configuration for
179 * target rate without using the 4X multiplier.
180 */
Mike Turquette32cc0022012-11-10 16:58:41 -0700181 r = omap2_dpll_round_rate(hw, target_rate, NULL);
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700182 if (r != ~0)
183 goto out;
184
185 /*
186 * If we did not find a valid DPLL configuration, try again, but
187 * this time see if using the 4X multiplier can help. Enabling the
188 * 4X multiplier is equivalent to dividing the target rate by 4.
189 */
190 r = omap2_dpll_round_rate(hw, target_rate / OMAP4430_REGM4XEN_MULT,
191 NULL);
Mike Turquettea1900f22011-10-07 00:52:58 -0600192 if (r == ~0)
193 return r;
194
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700195 dd->last_rounded_rate *= OMAP4430_REGM4XEN_MULT;
196 dd->last_rounded_m4xen = 1;
Mike Turquettea1900f22011-10-07 00:52:58 -0600197
Jon Hunter3ff51ed2012-12-15 01:35:46 -0700198out:
199 omap4_dpll_lpmode_recalc(dd);
200
201 return dd->last_rounded_rate;
Mike Turquettea1900f22011-10-07 00:52:58 -0600202}