Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 1 | /* |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 2 | * Clock and PLL control for DaVinci devices |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 3 | * |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 4 | * Copyright (C) 2006-2007 Texas Instruments. |
| 5 | * Copyright (C) 2008-2009 Deep Root Systems, LLC |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 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 as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/errno.h> |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 17 | #include <linux/clk.h> |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 18 | #include <linux/err.h> |
| 19 | #include <linux/mutex.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 20 | #include <linux/io.h> |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 21 | #include <linux/delay.h> |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 22 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 23 | #include <mach/hardware.h> |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 24 | |
Kevin Hilman | 28552c2 | 2010-02-25 15:36:38 -0800 | [diff] [blame] | 25 | #include <mach/clock.h> |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 26 | #include <mach/psc.h> |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 27 | #include <mach/cputype.h> |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 28 | #include "clock.h" |
| 29 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 30 | static LIST_HEAD(clocks); |
| 31 | static DEFINE_MUTEX(clocks_mutex); |
| 32 | static DEFINE_SPINLOCK(clockfw_lock); |
| 33 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 34 | static unsigned psc_domain(struct clk *clk) |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 35 | { |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 36 | return (clk->flags & PSC_DSP) |
| 37 | ? DAVINCI_GPSC_DSPDOMAIN |
| 38 | : DAVINCI_GPSC_ARMDOMAIN; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 39 | } |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 40 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 41 | static void __clk_enable(struct clk *clk) |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 42 | { |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 43 | if (clk->parent) |
| 44 | __clk_enable(clk->parent); |
| 45 | if (clk->usecount++ == 0 && (clk->flags & CLK_PSC)) |
Cyril Chemparathy | 52958be | 2010-03-25 17:43:47 -0400 | [diff] [blame] | 46 | davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc, |
Sekhar Nori | a51ca38 | 2011-07-06 06:01:21 +0000 | [diff] [blame^] | 47 | true, clk->flags); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void __clk_disable(struct clk *clk) |
| 51 | { |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 52 | if (WARN_ON(clk->usecount == 0)) |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 53 | return; |
Chaithrika U S | 679f921 | 2009-12-15 18:02:58 +0530 | [diff] [blame] | 54 | if (--clk->usecount == 0 && !(clk->flags & CLK_PLL) && |
| 55 | (clk->flags & CLK_PSC)) |
Cyril Chemparathy | 52958be | 2010-03-25 17:43:47 -0400 | [diff] [blame] | 56 | davinci_psc_config(psc_domain(clk), clk->gpsc, clk->lpsc, |
Sekhar Nori | a51ca38 | 2011-07-06 06:01:21 +0000 | [diff] [blame^] | 57 | false, clk->flags); |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 58 | if (clk->parent) |
| 59 | __clk_disable(clk->parent); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | int clk_enable(struct clk *clk) |
| 63 | { |
| 64 | unsigned long flags; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 65 | |
| 66 | if (clk == NULL || IS_ERR(clk)) |
| 67 | return -EINVAL; |
| 68 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 69 | spin_lock_irqsave(&clockfw_lock, flags); |
| 70 | __clk_enable(clk); |
| 71 | spin_unlock_irqrestore(&clockfw_lock, flags); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 72 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 73 | return 0; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 74 | } |
| 75 | EXPORT_SYMBOL(clk_enable); |
| 76 | |
| 77 | void clk_disable(struct clk *clk) |
| 78 | { |
| 79 | unsigned long flags; |
| 80 | |
| 81 | if (clk == NULL || IS_ERR(clk)) |
| 82 | return; |
| 83 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 84 | spin_lock_irqsave(&clockfw_lock, flags); |
| 85 | __clk_disable(clk); |
| 86 | spin_unlock_irqrestore(&clockfw_lock, flags); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 87 | } |
| 88 | EXPORT_SYMBOL(clk_disable); |
| 89 | |
| 90 | unsigned long clk_get_rate(struct clk *clk) |
| 91 | { |
| 92 | if (clk == NULL || IS_ERR(clk)) |
| 93 | return -EINVAL; |
| 94 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 95 | return clk->rate; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 96 | } |
| 97 | EXPORT_SYMBOL(clk_get_rate); |
| 98 | |
| 99 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 100 | { |
| 101 | if (clk == NULL || IS_ERR(clk)) |
| 102 | return -EINVAL; |
| 103 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 104 | if (clk->round_rate) |
| 105 | return clk->round_rate(clk, rate); |
| 106 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 107 | return clk->rate; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 108 | } |
| 109 | EXPORT_SYMBOL(clk_round_rate); |
| 110 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 111 | /* Propagate rate to children */ |
| 112 | static void propagate_rate(struct clk *root) |
| 113 | { |
| 114 | struct clk *clk; |
| 115 | |
| 116 | list_for_each_entry(clk, &root->children, childnode) { |
| 117 | if (clk->recalc) |
| 118 | clk->rate = clk->recalc(clk); |
| 119 | propagate_rate(clk); |
| 120 | } |
| 121 | } |
| 122 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 123 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 124 | { |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 125 | unsigned long flags; |
| 126 | int ret = -EINVAL; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 127 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 128 | if (clk == NULL || IS_ERR(clk)) |
| 129 | return ret; |
| 130 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 131 | if (clk->set_rate) |
| 132 | ret = clk->set_rate(clk, rate); |
Sekhar Nori | 3b43cd6 | 2010-01-12 18:55:35 +0530 | [diff] [blame] | 133 | |
| 134 | spin_lock_irqsave(&clockfw_lock, flags); |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 135 | if (ret == 0) { |
| 136 | if (clk->recalc) |
| 137 | clk->rate = clk->recalc(clk); |
| 138 | propagate_rate(clk); |
| 139 | } |
| 140 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 141 | |
| 142 | return ret; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 143 | } |
| 144 | EXPORT_SYMBOL(clk_set_rate); |
| 145 | |
Sekhar Nori | b82a51e | 2009-08-31 15:48:04 +0530 | [diff] [blame] | 146 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 147 | { |
| 148 | unsigned long flags; |
| 149 | |
| 150 | if (clk == NULL || IS_ERR(clk)) |
| 151 | return -EINVAL; |
| 152 | |
| 153 | /* Cannot change parent on enabled clock */ |
| 154 | if (WARN_ON(clk->usecount)) |
| 155 | return -EINVAL; |
| 156 | |
| 157 | mutex_lock(&clocks_mutex); |
| 158 | clk->parent = parent; |
| 159 | list_del_init(&clk->childnode); |
| 160 | list_add(&clk->childnode, &clk->parent->children); |
| 161 | mutex_unlock(&clocks_mutex); |
| 162 | |
| 163 | spin_lock_irqsave(&clockfw_lock, flags); |
| 164 | if (clk->recalc) |
| 165 | clk->rate = clk->recalc(clk); |
| 166 | propagate_rate(clk); |
| 167 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | EXPORT_SYMBOL(clk_set_parent); |
| 172 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 173 | int clk_register(struct clk *clk) |
| 174 | { |
| 175 | if (clk == NULL || IS_ERR(clk)) |
| 176 | return -EINVAL; |
| 177 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 178 | if (WARN(clk->parent && !clk->parent->rate, |
| 179 | "CLK: %s parent %s has no rate!\n", |
| 180 | clk->name, clk->parent->name)) |
| 181 | return -EINVAL; |
| 182 | |
Sekhar Nori | f02bf3b | 2009-08-31 15:48:01 +0530 | [diff] [blame] | 183 | INIT_LIST_HEAD(&clk->children); |
| 184 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 185 | mutex_lock(&clocks_mutex); |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 186 | list_add_tail(&clk->node, &clocks); |
Sekhar Nori | f02bf3b | 2009-08-31 15:48:01 +0530 | [diff] [blame] | 187 | if (clk->parent) |
| 188 | list_add_tail(&clk->childnode, &clk->parent->children); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 189 | mutex_unlock(&clocks_mutex); |
| 190 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 191 | /* If rate is already set, use it */ |
| 192 | if (clk->rate) |
| 193 | return 0; |
| 194 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 195 | /* Else, see if there is a way to calculate it */ |
| 196 | if (clk->recalc) |
| 197 | clk->rate = clk->recalc(clk); |
| 198 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 199 | /* Otherwise, default to parent rate */ |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 200 | else if (clk->parent) |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 201 | clk->rate = clk->parent->rate; |
| 202 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | EXPORT_SYMBOL(clk_register); |
| 206 | |
| 207 | void clk_unregister(struct clk *clk) |
| 208 | { |
| 209 | if (clk == NULL || IS_ERR(clk)) |
| 210 | return; |
| 211 | |
| 212 | mutex_lock(&clocks_mutex); |
| 213 | list_del(&clk->node); |
Sekhar Nori | f02bf3b | 2009-08-31 15:48:01 +0530 | [diff] [blame] | 214 | list_del(&clk->childnode); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 215 | mutex_unlock(&clocks_mutex); |
| 216 | } |
| 217 | EXPORT_SYMBOL(clk_unregister); |
| 218 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 219 | #ifdef CONFIG_DAVINCI_RESET_CLOCKS |
| 220 | /* |
| 221 | * Disable any unused clocks left on by the bootloader |
| 222 | */ |
| 223 | static int __init clk_disable_unused(void) |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 224 | { |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 225 | struct clk *ck; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 226 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 227 | spin_lock_irq(&clockfw_lock); |
| 228 | list_for_each_entry(ck, &clocks, node) { |
| 229 | if (ck->usecount > 0) |
| 230 | continue; |
| 231 | if (!(ck->flags & CLK_PSC)) |
| 232 | continue; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 233 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 234 | /* ignore if in Disabled or SwRstDisable states */ |
Sergei Shtylyov | 789a785 | 2009-09-30 19:48:03 +0400 | [diff] [blame] | 235 | if (!davinci_psc_is_clk_active(ck->gpsc, ck->lpsc)) |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 236 | continue; |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 237 | |
Kevin Hilman | c89f168 | 2010-08-05 10:55:16 -0700 | [diff] [blame] | 238 | pr_debug("Clocks: disable unused %s\n", ck->name); |
Cyril Chemparathy | 52958be | 2010-03-25 17:43:47 -0400 | [diff] [blame] | 239 | |
| 240 | davinci_psc_config(psc_domain(ck), ck->gpsc, ck->lpsc, |
Sekhar Nori | a51ca38 | 2011-07-06 06:01:21 +0000 | [diff] [blame^] | 241 | false, ck->flags); |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 242 | } |
| 243 | spin_unlock_irq(&clockfw_lock); |
| 244 | |
| 245 | return 0; |
| 246 | } |
| 247 | late_initcall(clk_disable_unused); |
| 248 | #endif |
| 249 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 250 | static unsigned long clk_sysclk_recalc(struct clk *clk) |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 251 | { |
| 252 | u32 v, plldiv; |
| 253 | struct pll_data *pll; |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 254 | unsigned long rate = clk->rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 255 | |
| 256 | /* If this is the PLL base clock, no more calculations needed */ |
| 257 | if (clk->pll_data) |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 258 | return rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 259 | |
| 260 | if (WARN_ON(!clk->parent)) |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 261 | return rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 262 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 263 | rate = clk->parent->rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 264 | |
| 265 | /* Otherwise, the parent must be a PLL */ |
| 266 | if (WARN_ON(!clk->parent->pll_data)) |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 267 | return rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 268 | |
| 269 | pll = clk->parent->pll_data; |
| 270 | |
| 271 | /* If pre-PLL, source clock is before the multiplier and divider(s) */ |
| 272 | if (clk->flags & PRE_PLL) |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 273 | rate = pll->input_rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 274 | |
| 275 | if (!clk->div_reg) |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 276 | return rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 277 | |
| 278 | v = __raw_readl(pll->base + clk->div_reg); |
| 279 | if (v & PLLDIV_EN) { |
Cyril Chemparathy | d6961e6 | 2010-04-14 14:44:49 -0400 | [diff] [blame] | 280 | plldiv = (v & pll->div_ratio_mask) + 1; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 281 | if (plldiv) |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 282 | rate /= plldiv; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 283 | } |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 284 | |
| 285 | return rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 286 | } |
| 287 | |
Sekhar Nori | b39639b | 2010-07-20 16:46:49 +0530 | [diff] [blame] | 288 | int davinci_set_sysclk_rate(struct clk *clk, unsigned long rate) |
| 289 | { |
| 290 | unsigned v; |
| 291 | struct pll_data *pll; |
| 292 | unsigned long input; |
| 293 | unsigned ratio = 0; |
| 294 | |
| 295 | /* If this is the PLL base clock, wrong function to call */ |
| 296 | if (clk->pll_data) |
| 297 | return -EINVAL; |
| 298 | |
| 299 | /* There must be a parent... */ |
| 300 | if (WARN_ON(!clk->parent)) |
| 301 | return -EINVAL; |
| 302 | |
| 303 | /* ... the parent must be a PLL... */ |
| 304 | if (WARN_ON(!clk->parent->pll_data)) |
| 305 | return -EINVAL; |
| 306 | |
| 307 | /* ... and this clock must have a divider. */ |
| 308 | if (WARN_ON(!clk->div_reg)) |
| 309 | return -EINVAL; |
| 310 | |
| 311 | pll = clk->parent->pll_data; |
| 312 | |
| 313 | input = clk->parent->rate; |
| 314 | |
| 315 | /* If pre-PLL, source clock is before the multiplier and divider(s) */ |
| 316 | if (clk->flags & PRE_PLL) |
| 317 | input = pll->input_rate; |
| 318 | |
| 319 | if (input > rate) { |
| 320 | /* |
| 321 | * Can afford to provide an output little higher than requested |
| 322 | * only if maximum rate supported by hardware on this sysclk |
| 323 | * is known. |
| 324 | */ |
| 325 | if (clk->maxrate) { |
| 326 | ratio = DIV_ROUND_CLOSEST(input, rate); |
| 327 | if (input / ratio > clk->maxrate) |
| 328 | ratio = 0; |
| 329 | } |
| 330 | |
| 331 | if (ratio == 0) |
| 332 | ratio = DIV_ROUND_UP(input, rate); |
| 333 | |
| 334 | ratio--; |
| 335 | } |
| 336 | |
Cyril Chemparathy | b1d05be | 2010-10-20 17:49:56 -0400 | [diff] [blame] | 337 | if (ratio > pll->div_ratio_mask) |
Sekhar Nori | b39639b | 2010-07-20 16:46:49 +0530 | [diff] [blame] | 338 | return -EINVAL; |
| 339 | |
| 340 | do { |
| 341 | v = __raw_readl(pll->base + PLLSTAT); |
| 342 | } while (v & PLLSTAT_GOSTAT); |
| 343 | |
| 344 | v = __raw_readl(pll->base + clk->div_reg); |
Cyril Chemparathy | b1d05be | 2010-10-20 17:49:56 -0400 | [diff] [blame] | 345 | v &= ~pll->div_ratio_mask; |
Sekhar Nori | b39639b | 2010-07-20 16:46:49 +0530 | [diff] [blame] | 346 | v |= ratio | PLLDIV_EN; |
| 347 | __raw_writel(v, pll->base + clk->div_reg); |
| 348 | |
| 349 | v = __raw_readl(pll->base + PLLCMD); |
| 350 | v |= PLLCMD_GOSET; |
| 351 | __raw_writel(v, pll->base + PLLCMD); |
| 352 | |
| 353 | do { |
| 354 | v = __raw_readl(pll->base + PLLSTAT); |
| 355 | } while (v & PLLSTAT_GOSTAT); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | EXPORT_SYMBOL(davinci_set_sysclk_rate); |
| 360 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 361 | static unsigned long clk_leafclk_recalc(struct clk *clk) |
| 362 | { |
| 363 | if (WARN_ON(!clk->parent)) |
| 364 | return clk->rate; |
| 365 | |
| 366 | return clk->parent->rate; |
| 367 | } |
| 368 | |
| 369 | static unsigned long clk_pllclk_recalc(struct clk *clk) |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 370 | { |
| 371 | u32 ctrl, mult = 1, prediv = 1, postdiv = 1; |
| 372 | u8 bypass; |
| 373 | struct pll_data *pll = clk->pll_data; |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 374 | unsigned long rate = clk->rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 375 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 376 | ctrl = __raw_readl(pll->base + PLLCTL); |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 377 | rate = pll->input_rate = clk->parent->rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 378 | |
| 379 | if (ctrl & PLLCTL_PLLEN) { |
| 380 | bypass = 0; |
| 381 | mult = __raw_readl(pll->base + PLLM); |
Sandeep Paulraj | fb8fcb8 | 2009-06-11 09:41:05 -0400 | [diff] [blame] | 382 | if (cpu_is_davinci_dm365()) |
| 383 | mult = 2 * (mult & PLLM_PLLM_MASK); |
| 384 | else |
| 385 | mult = (mult & PLLM_PLLM_MASK) + 1; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 386 | } else |
| 387 | bypass = 1; |
| 388 | |
| 389 | if (pll->flags & PLL_HAS_PREDIV) { |
| 390 | prediv = __raw_readl(pll->base + PREDIV); |
| 391 | if (prediv & PLLDIV_EN) |
Cyril Chemparathy | d6961e6 | 2010-04-14 14:44:49 -0400 | [diff] [blame] | 392 | prediv = (prediv & pll->div_ratio_mask) + 1; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 393 | else |
| 394 | prediv = 1; |
| 395 | } |
| 396 | |
| 397 | /* pre-divider is fixed, but (some?) chips won't report that */ |
| 398 | if (cpu_is_davinci_dm355() && pll->num == 1) |
| 399 | prediv = 8; |
| 400 | |
| 401 | if (pll->flags & PLL_HAS_POSTDIV) { |
| 402 | postdiv = __raw_readl(pll->base + POSTDIV); |
| 403 | if (postdiv & PLLDIV_EN) |
Cyril Chemparathy | d6961e6 | 2010-04-14 14:44:49 -0400 | [diff] [blame] | 404 | postdiv = (postdiv & pll->div_ratio_mask) + 1; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 405 | else |
| 406 | postdiv = 1; |
| 407 | } |
| 408 | |
| 409 | if (!bypass) { |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 410 | rate /= prediv; |
| 411 | rate *= mult; |
| 412 | rate /= postdiv; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | pr_debug("PLL%d: input = %lu MHz [ ", |
| 416 | pll->num, clk->parent->rate / 1000000); |
| 417 | if (bypass) |
| 418 | pr_debug("bypass "); |
| 419 | if (prediv > 1) |
| 420 | pr_debug("/ %d ", prediv); |
| 421 | if (mult > 1) |
| 422 | pr_debug("* %d ", mult); |
| 423 | if (postdiv > 1) |
| 424 | pr_debug("/ %d ", postdiv); |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 425 | pr_debug("] --> %lu MHz output.\n", rate / 1000000); |
| 426 | |
| 427 | return rate; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 428 | } |
| 429 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 430 | /** |
| 431 | * davinci_set_pllrate - set the output rate of a given PLL. |
| 432 | * |
| 433 | * Note: Currently tested to work with OMAP-L138 only. |
| 434 | * |
| 435 | * @pll: pll whose rate needs to be changed. |
| 436 | * @prediv: The pre divider value. Passing 0 disables the pre-divider. |
| 437 | * @pllm: The multiplier value. Passing 0 leads to multiply-by-one. |
| 438 | * @postdiv: The post divider value. Passing 0 disables the post-divider. |
| 439 | */ |
| 440 | int davinci_set_pllrate(struct pll_data *pll, unsigned int prediv, |
| 441 | unsigned int mult, unsigned int postdiv) |
| 442 | { |
| 443 | u32 ctrl; |
| 444 | unsigned int locktime; |
Sekhar Nori | 3b43cd6 | 2010-01-12 18:55:35 +0530 | [diff] [blame] | 445 | unsigned long flags; |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 446 | |
| 447 | if (pll->base == NULL) |
| 448 | return -EINVAL; |
| 449 | |
| 450 | /* |
| 451 | * PLL lock time required per OMAP-L138 datasheet is |
| 452 | * (2000 * prediv)/sqrt(pllm) OSCIN cycles. We approximate sqrt(pllm) |
| 453 | * as 4 and OSCIN cycle as 25 MHz. |
| 454 | */ |
| 455 | if (prediv) { |
| 456 | locktime = ((2000 * prediv) / 100); |
| 457 | prediv = (prediv - 1) | PLLDIV_EN; |
| 458 | } else { |
Sekhar Nori | 9a219a9 | 2009-11-16 17:21:33 +0530 | [diff] [blame] | 459 | locktime = PLL_LOCK_TIME; |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 460 | } |
| 461 | if (postdiv) |
| 462 | postdiv = (postdiv - 1) | PLLDIV_EN; |
| 463 | if (mult) |
| 464 | mult = mult - 1; |
| 465 | |
Sekhar Nori | 3b43cd6 | 2010-01-12 18:55:35 +0530 | [diff] [blame] | 466 | /* Protect against simultaneous calls to PLL setting seqeunce */ |
| 467 | spin_lock_irqsave(&clockfw_lock, flags); |
| 468 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 469 | ctrl = __raw_readl(pll->base + PLLCTL); |
| 470 | |
| 471 | /* Switch the PLL to bypass mode */ |
| 472 | ctrl &= ~(PLLCTL_PLLENSRC | PLLCTL_PLLEN); |
| 473 | __raw_writel(ctrl, pll->base + PLLCTL); |
| 474 | |
Sekhar Nori | 9a219a9 | 2009-11-16 17:21:33 +0530 | [diff] [blame] | 475 | udelay(PLL_BYPASS_TIME); |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 476 | |
| 477 | /* Reset and enable PLL */ |
| 478 | ctrl &= ~(PLLCTL_PLLRST | PLLCTL_PLLDIS); |
| 479 | __raw_writel(ctrl, pll->base + PLLCTL); |
| 480 | |
| 481 | if (pll->flags & PLL_HAS_PREDIV) |
| 482 | __raw_writel(prediv, pll->base + PREDIV); |
| 483 | |
| 484 | __raw_writel(mult, pll->base + PLLM); |
| 485 | |
| 486 | if (pll->flags & PLL_HAS_POSTDIV) |
| 487 | __raw_writel(postdiv, pll->base + POSTDIV); |
| 488 | |
Sekhar Nori | 9a219a9 | 2009-11-16 17:21:33 +0530 | [diff] [blame] | 489 | udelay(PLL_RESET_TIME); |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 490 | |
| 491 | /* Bring PLL out of reset */ |
| 492 | ctrl |= PLLCTL_PLLRST; |
| 493 | __raw_writel(ctrl, pll->base + PLLCTL); |
| 494 | |
| 495 | udelay(locktime); |
| 496 | |
| 497 | /* Remove PLL from bypass mode */ |
| 498 | ctrl |= PLLCTL_PLLEN; |
| 499 | __raw_writel(ctrl, pll->base + PLLCTL); |
| 500 | |
Sekhar Nori | 3b43cd6 | 2010-01-12 18:55:35 +0530 | [diff] [blame] | 501 | spin_unlock_irqrestore(&clockfw_lock, flags); |
| 502 | |
Sekhar Nori | d6a6156 | 2009-08-31 15:48:03 +0530 | [diff] [blame] | 503 | return 0; |
| 504 | } |
| 505 | EXPORT_SYMBOL(davinci_set_pllrate); |
| 506 | |
Kevin Hilman | 08aca08 | 2010-01-11 08:22:23 -0800 | [diff] [blame] | 507 | int __init davinci_clk_init(struct clk_lookup *clocks) |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 508 | { |
Kevin Hilman | 08aca08 | 2010-01-11 08:22:23 -0800 | [diff] [blame] | 509 | struct clk_lookup *c; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 510 | struct clk *clk; |
Kevin Hilman | 08aca08 | 2010-01-11 08:22:23 -0800 | [diff] [blame] | 511 | size_t num_clocks = 0; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 512 | |
Kevin Hilman | 08aca08 | 2010-01-11 08:22:23 -0800 | [diff] [blame] | 513 | for (c = clocks; c->clk; c++) { |
| 514 | clk = c->clk; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 515 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 516 | if (!clk->recalc) { |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 517 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 518 | /* Check if clock is a PLL */ |
| 519 | if (clk->pll_data) |
| 520 | clk->recalc = clk_pllclk_recalc; |
| 521 | |
| 522 | /* Else, if it is a PLL-derived clock */ |
| 523 | else if (clk->flags & CLK_PLL) |
| 524 | clk->recalc = clk_sysclk_recalc; |
| 525 | |
| 526 | /* Otherwise, it is a leaf clock (PSC clock) */ |
| 527 | else if (clk->parent) |
| 528 | clk->recalc = clk_leafclk_recalc; |
| 529 | } |
| 530 | |
Cyril Chemparathy | e4c822c | 2010-05-07 17:06:36 -0400 | [diff] [blame] | 531 | if (clk->pll_data) { |
| 532 | struct pll_data *pll = clk->pll_data; |
| 533 | |
| 534 | if (!pll->div_ratio_mask) |
| 535 | pll->div_ratio_mask = PLLDIV_RATIO_MASK; |
| 536 | |
| 537 | if (pll->phys_base && !pll->base) { |
| 538 | pll->base = ioremap(pll->phys_base, SZ_4K); |
| 539 | WARN_ON(!pll->base); |
| 540 | } |
| 541 | } |
Cyril Chemparathy | d6961e6 | 2010-04-14 14:44:49 -0400 | [diff] [blame] | 542 | |
Sekhar Nori | de381a9 | 2009-08-31 15:48:02 +0530 | [diff] [blame] | 543 | if (clk->recalc) |
| 544 | clk->rate = clk->recalc(clk); |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 545 | |
| 546 | if (clk->lpsc) |
| 547 | clk->flags |= CLK_PSC; |
| 548 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 549 | clk_register(clk); |
Kevin Hilman | 08aca08 | 2010-01-11 08:22:23 -0800 | [diff] [blame] | 550 | num_clocks++; |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 551 | |
| 552 | /* Turn on clocks that Linux doesn't otherwise manage */ |
| 553 | if (clk->flags & ALWAYS_ENABLED) |
| 554 | clk_enable(clk); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 555 | } |
| 556 | |
Kevin Hilman | 08aca08 | 2010-01-11 08:22:23 -0800 | [diff] [blame] | 557 | clkdev_add_table(clocks, num_clocks); |
| 558 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 562 | #ifdef CONFIG_DEBUG_FS |
| 563 | |
| 564 | #include <linux/debugfs.h> |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 565 | #include <linux/seq_file.h> |
| 566 | |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 567 | #define CLKNAME_MAX 10 /* longest clock name */ |
| 568 | #define NEST_DELTA 2 |
| 569 | #define NEST_MAX 4 |
| 570 | |
| 571 | static void |
| 572 | dump_clock(struct seq_file *s, unsigned nest, struct clk *parent) |
| 573 | { |
| 574 | char *state; |
| 575 | char buf[CLKNAME_MAX + NEST_DELTA * NEST_MAX]; |
| 576 | struct clk *clk; |
| 577 | unsigned i; |
| 578 | |
| 579 | if (parent->flags & CLK_PLL) |
| 580 | state = "pll"; |
| 581 | else if (parent->flags & CLK_PSC) |
| 582 | state = "psc"; |
| 583 | else |
| 584 | state = ""; |
| 585 | |
| 586 | /* <nest spaces> name <pad to end> */ |
| 587 | memset(buf, ' ', sizeof(buf) - 1); |
| 588 | buf[sizeof(buf) - 1] = 0; |
| 589 | i = strlen(parent->name); |
| 590 | memcpy(buf + nest, parent->name, |
| 591 | min(i, (unsigned)(sizeof(buf) - 1 - nest))); |
| 592 | |
| 593 | seq_printf(s, "%s users=%2d %-3s %9ld Hz\n", |
| 594 | buf, parent->usecount, state, clk_get_rate(parent)); |
| 595 | /* REVISIT show device associations too */ |
| 596 | |
| 597 | /* cost is now small, but not linear... */ |
Sekhar Nori | f02bf3b | 2009-08-31 15:48:01 +0530 | [diff] [blame] | 598 | list_for_each_entry(clk, &parent->children, childnode) { |
| 599 | dump_clock(s, nest + NEST_DELTA, clk); |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 603 | static int davinci_ck_show(struct seq_file *m, void *v) |
| 604 | { |
Sekhar Nori | f979aa6 | 2009-12-03 15:36:51 +0530 | [diff] [blame] | 605 | struct clk *clk; |
| 606 | |
| 607 | /* |
| 608 | * Show clock tree; We trust nonzero usecounts equate to PSC enables... |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 609 | */ |
| 610 | mutex_lock(&clocks_mutex); |
Sekhar Nori | f979aa6 | 2009-12-03 15:36:51 +0530 | [diff] [blame] | 611 | list_for_each_entry(clk, &clocks, node) |
| 612 | if (!clk->parent) |
| 613 | dump_clock(m, 0, clk); |
Kevin Hilman | c5b736d | 2009-03-20 17:29:01 -0700 | [diff] [blame] | 614 | mutex_unlock(&clocks_mutex); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 615 | |
| 616 | return 0; |
| 617 | } |
| 618 | |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 619 | static int davinci_ck_open(struct inode *inode, struct file *file) |
| 620 | { |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 621 | return single_open(file, davinci_ck_show, NULL); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 624 | static const struct file_operations davinci_ck_operations = { |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 625 | .open = davinci_ck_open, |
| 626 | .read = seq_read, |
| 627 | .llseek = seq_lseek, |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 628 | .release = single_release, |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 629 | }; |
| 630 | |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 631 | static int __init davinci_clk_debugfs_init(void) |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 632 | { |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 633 | debugfs_create_file("davinci_clocks", S_IFREG | S_IRUGO, NULL, NULL, |
| 634 | &davinci_ck_operations); |
Vladimir Barinov | 3e062b0 | 2007-06-05 16:36:55 +0100 | [diff] [blame] | 635 | return 0; |
| 636 | |
| 637 | } |
Sekhar Nori | 2f72e8d | 2009-12-03 15:36:52 +0530 | [diff] [blame] | 638 | device_initcall(davinci_clk_debugfs_init); |
| 639 | #endif /* CONFIG_DEBUG_FS */ |