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