SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 1 | /* |
Andrew Victor | 9d04126 | 2007-02-05 11:42:07 +0100 | [diff] [blame] | 2 | * linux/arch/arm/mach-at91/clock.c |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2005 David Brownell |
| 5 | * Copyright (C) 2005 Ivan Kokshaysky |
| 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/fs.h> |
| 17 | #include <linux/debugfs.h> |
| 18 | #include <linux/seq_file.h> |
| 19 | #include <linux/list.h> |
| 20 | #include <linux/errno.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/spinlock.h> |
| 23 | #include <linux/delay.h> |
| 24 | #include <linux/clk.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 25 | #include <linux/io.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 26 | |
Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 27 | #include <mach/hardware.h> |
| 28 | #include <mach/at91_pmc.h> |
| 29 | #include <mach/cpu.h> |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 30 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 31 | #include "clock.h" |
Andrew Victor | 5e38efa | 2009-12-15 21:57:27 +0100 | [diff] [blame] | 32 | #include "generic.h" |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 33 | |
Andrew Victor | 55c20c0 | 2006-06-20 19:31:39 +0100 | [diff] [blame] | 34 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 35 | /* |
| 36 | * There's a lot more which can be done with clocks, including cpufreq |
| 37 | * integration, slow clock mode support (for system suspend), letting |
| 38 | * PLLB be used at other rates (on boards that don't need USB), etc. |
| 39 | */ |
| 40 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 41 | #define clk_is_primary(x) ((x)->type & CLK_TYPE_PRIMARY) |
| 42 | #define clk_is_programmable(x) ((x)->type & CLK_TYPE_PROGRAMMABLE) |
| 43 | #define clk_is_peripheral(x) ((x)->type & CLK_TYPE_PERIPHERAL) |
Andrew Victor | d481f86 | 2006-12-01 11:27:31 +0100 | [diff] [blame] | 44 | #define clk_is_sys(x) ((x)->type & CLK_TYPE_SYSTEM) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 45 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 46 | |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 47 | /* |
| 48 | * Chips have some kind of clocks : group them by functionality |
| 49 | */ |
Jean-Christophe PLAGNIOL-VILLARD | 9918cea | 2012-01-26 14:07:09 +0100 | [diff] [blame] | 50 | #define cpu_has_utmi() ( cpu_is_at91sam9rl() \ |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 51 | || cpu_is_at91sam9g45() \ |
| 52 | || cpu_is_at91sam9x5()) |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 53 | |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 54 | #define cpu_has_800M_plla() ( cpu_is_at91sam9g20() \ |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 55 | || cpu_is_at91sam9g45() \ |
| 56 | || cpu_is_at91sam9x5()) |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 57 | |
Nicolas Ferre | eab4170 | 2009-06-26 15:37:00 +0100 | [diff] [blame] | 58 | #define cpu_has_300M_plla() (cpu_is_at91sam9g10()) |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 59 | |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 60 | #define cpu_has_pllb() (!(cpu_is_at91sam9rl() \ |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 61 | || cpu_is_at91sam9g45() \ |
| 62 | || cpu_is_at91sam9x5())) |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 63 | |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 64 | #define cpu_has_upll() (cpu_is_at91sam9g45() \ |
| 65 | || cpu_is_at91sam9x5()) |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 66 | |
| 67 | /* USB host HS & FS */ |
| 68 | #define cpu_has_uhp() (!cpu_is_at91sam9rl()) |
| 69 | |
| 70 | /* USB device FS only */ |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 71 | #define cpu_has_udpfs() (!(cpu_is_at91sam9rl() \ |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 72 | || cpu_is_at91sam9g45() \ |
| 73 | || cpu_is_at91sam9x5())) |
| 74 | |
| 75 | #define cpu_has_plladiv2() (cpu_is_at91sam9g45() \ |
| 76 | || cpu_is_at91sam9x5()) |
| 77 | |
| 78 | #define cpu_has_mdiv3() (cpu_is_at91sam9g45() \ |
| 79 | || cpu_is_at91sam9x5()) |
| 80 | |
| 81 | #define cpu_has_alt_prescaler() (cpu_is_at91sam9x5()) |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 82 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 83 | static LIST_HEAD(clocks); |
| 84 | static DEFINE_SPINLOCK(clk_lock); |
| 85 | |
| 86 | static u32 at91_pllb_usb_init; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 87 | |
| 88 | /* |
| 89 | * Four primary clock sources: two crystal oscillators (32K, main), and |
| 90 | * two PLLs. PLLA usually runs the master clock; and PLLB must run at |
| 91 | * 48 MHz (unless no USB function clocks are needed). The main clock and |
| 92 | * both PLLs are turned off to run in "slow clock mode" (system suspend). |
| 93 | */ |
| 94 | static struct clk clk32k = { |
| 95 | .name = "clk32k", |
| 96 | .rate_hz = AT91_SLOW_CLOCK, |
| 97 | .users = 1, /* always on */ |
| 98 | .id = 0, |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 99 | .type = CLK_TYPE_PRIMARY, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 100 | }; |
| 101 | static struct clk main_clk = { |
| 102 | .name = "main", |
Andrew Victor | 91f8ed8 | 2006-06-19 13:20:23 +0100 | [diff] [blame] | 103 | .pmc_mask = AT91_PMC_MOSCS, /* in PMC_SR */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 104 | .id = 1, |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 105 | .type = CLK_TYPE_PRIMARY, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 106 | }; |
| 107 | static struct clk plla = { |
| 108 | .name = "plla", |
| 109 | .parent = &main_clk, |
Andrew Victor | 91f8ed8 | 2006-06-19 13:20:23 +0100 | [diff] [blame] | 110 | .pmc_mask = AT91_PMC_LOCKA, /* in PMC_SR */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 111 | .id = 2, |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 112 | .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static void pllb_mode(struct clk *clk, int is_on) |
| 116 | { |
| 117 | u32 value; |
| 118 | |
| 119 | if (is_on) { |
| 120 | is_on = AT91_PMC_LOCKB; |
| 121 | value = at91_pllb_usb_init; |
| 122 | } else |
| 123 | value = 0; |
| 124 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 125 | // REVISIT: Add work-around for AT91RM9200 Errata #26 ? |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 126 | at91_sys_write(AT91_CKGR_PLLBR, value); |
| 127 | |
| 128 | do { |
| 129 | cpu_relax(); |
| 130 | } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKB) != is_on); |
| 131 | } |
| 132 | |
| 133 | static struct clk pllb = { |
| 134 | .name = "pllb", |
| 135 | .parent = &main_clk, |
Andrew Victor | 91f8ed8 | 2006-06-19 13:20:23 +0100 | [diff] [blame] | 136 | .pmc_mask = AT91_PMC_LOCKB, /* in PMC_SR */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 137 | .mode = pllb_mode, |
| 138 | .id = 3, |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 139 | .type = CLK_TYPE_PRIMARY | CLK_TYPE_PLL, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static void pmc_sys_mode(struct clk *clk, int is_on) |
| 143 | { |
| 144 | if (is_on) |
| 145 | at91_sys_write(AT91_PMC_SCER, clk->pmc_mask); |
| 146 | else |
| 147 | at91_sys_write(AT91_PMC_SCDR, clk->pmc_mask); |
| 148 | } |
| 149 | |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 150 | static void pmc_uckr_mode(struct clk *clk, int is_on) |
| 151 | { |
| 152 | unsigned int uckr = at91_sys_read(AT91_CKGR_UCKR); |
| 153 | |
| 154 | if (is_on) { |
| 155 | is_on = AT91_PMC_LOCKU; |
| 156 | at91_sys_write(AT91_CKGR_UCKR, uckr | clk->pmc_mask); |
| 157 | } else |
| 158 | at91_sys_write(AT91_CKGR_UCKR, uckr & ~(clk->pmc_mask)); |
| 159 | |
| 160 | do { |
| 161 | cpu_relax(); |
| 162 | } while ((at91_sys_read(AT91_PMC_SR) & AT91_PMC_LOCKU) != is_on); |
| 163 | } |
| 164 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 165 | /* USB function clocks (PLLB must be 48 MHz) */ |
| 166 | static struct clk udpck = { |
| 167 | .name = "udpck", |
| 168 | .parent = &pllb, |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 169 | .mode = pmc_sys_mode, |
| 170 | }; |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 171 | struct clk utmi_clk = { |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 172 | .name = "utmi_clk", |
| 173 | .parent = &main_clk, |
| 174 | .pmc_mask = AT91_PMC_UPLLEN, /* in CKGR_UCKR */ |
| 175 | .mode = pmc_uckr_mode, |
| 176 | .type = CLK_TYPE_PLL, |
| 177 | }; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 178 | static struct clk uhpck = { |
| 179 | .name = "uhpck", |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 180 | /*.parent = ... we choose parent at runtime */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 181 | .mode = pmc_sys_mode, |
| 182 | }; |
| 183 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 184 | |
| 185 | /* |
| 186 | * The master clock is divided from the CPU clock (by 1-4). It's used for |
| 187 | * memory, interfaces to on-chip peripherals, the AIC, and sometimes more |
| 188 | * (e.g baud rate generation). It's sourced from one of the primary clocks. |
| 189 | */ |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 190 | struct clk mck = { |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 191 | .name = "mck", |
Andrew Victor | 91f8ed8 | 2006-06-19 13:20:23 +0100 | [diff] [blame] | 192 | .pmc_mask = AT91_PMC_MCKRDY, /* in PMC_SR */ |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static void pmc_periph_mode(struct clk *clk, int is_on) |
| 196 | { |
| 197 | if (is_on) |
| 198 | at91_sys_write(AT91_PMC_PCER, clk->pmc_mask); |
| 199 | else |
| 200 | at91_sys_write(AT91_PMC_PCDR, clk->pmc_mask); |
| 201 | } |
| 202 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 203 | static struct clk __init *at91_css_to_clk(unsigned long css) |
| 204 | { |
| 205 | switch (css) { |
| 206 | case AT91_PMC_CSS_SLOW: |
| 207 | return &clk32k; |
| 208 | case AT91_PMC_CSS_MAIN: |
| 209 | return &main_clk; |
| 210 | case AT91_PMC_CSS_PLLA: |
| 211 | return &plla; |
| 212 | case AT91_PMC_CSS_PLLB: |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 213 | if (cpu_has_upll()) |
| 214 | /* CSS_PLLB == CSS_UPLL */ |
| 215 | return &utmi_clk; |
| 216 | else if (cpu_has_pllb()) |
| 217 | return &pllb; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 218 | break; |
| 219 | /* alternate PMC: can use master clock */ |
| 220 | case AT91_PMC_CSS_MASTER: |
| 221 | return &mck; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 222 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 223 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 224 | return NULL; |
| 225 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 226 | |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 227 | static int pmc_prescaler_divider(u32 reg) |
| 228 | { |
| 229 | if (cpu_has_alt_prescaler()) { |
| 230 | return 1 << ((reg & AT91_PMC_ALT_PRES) >> PMC_ALT_PRES_OFFSET); |
| 231 | } else { |
| 232 | return 1 << ((reg & AT91_PMC_PRES) >> PMC_PRES_OFFSET); |
| 233 | } |
| 234 | } |
| 235 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 236 | static void __clk_enable(struct clk *clk) |
| 237 | { |
| 238 | if (clk->parent) |
| 239 | __clk_enable(clk->parent); |
| 240 | if (clk->users++ == 0 && clk->mode) |
| 241 | clk->mode(clk, 1); |
| 242 | } |
| 243 | |
| 244 | int clk_enable(struct clk *clk) |
| 245 | { |
| 246 | unsigned long flags; |
| 247 | |
| 248 | spin_lock_irqsave(&clk_lock, flags); |
| 249 | __clk_enable(clk); |
| 250 | spin_unlock_irqrestore(&clk_lock, flags); |
| 251 | return 0; |
| 252 | } |
| 253 | EXPORT_SYMBOL(clk_enable); |
| 254 | |
| 255 | static void __clk_disable(struct clk *clk) |
| 256 | { |
| 257 | BUG_ON(clk->users == 0); |
| 258 | if (--clk->users == 0 && clk->mode) |
| 259 | clk->mode(clk, 0); |
| 260 | if (clk->parent) |
| 261 | __clk_disable(clk->parent); |
| 262 | } |
| 263 | |
| 264 | void clk_disable(struct clk *clk) |
| 265 | { |
| 266 | unsigned long flags; |
| 267 | |
| 268 | spin_lock_irqsave(&clk_lock, flags); |
| 269 | __clk_disable(clk); |
| 270 | spin_unlock_irqrestore(&clk_lock, flags); |
| 271 | } |
| 272 | EXPORT_SYMBOL(clk_disable); |
| 273 | |
| 274 | unsigned long clk_get_rate(struct clk *clk) |
| 275 | { |
| 276 | unsigned long flags; |
| 277 | unsigned long rate; |
| 278 | |
| 279 | spin_lock_irqsave(&clk_lock, flags); |
| 280 | for (;;) { |
| 281 | rate = clk->rate_hz; |
| 282 | if (rate || !clk->parent) |
| 283 | break; |
| 284 | clk = clk->parent; |
| 285 | } |
| 286 | spin_unlock_irqrestore(&clk_lock, flags); |
| 287 | return rate; |
| 288 | } |
| 289 | EXPORT_SYMBOL(clk_get_rate); |
| 290 | |
| 291 | /*------------------------------------------------------------------------*/ |
| 292 | |
| 293 | #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS |
| 294 | |
| 295 | /* |
| 296 | * For now, only the programmable clocks support reparenting (MCK could |
| 297 | * do this too, with care) or rate changing (the PLLs could do this too, |
| 298 | * ditto MCK but that's more for cpufreq). Drivers may reparent to get |
| 299 | * a better rate match; we don't. |
| 300 | */ |
| 301 | |
| 302 | long clk_round_rate(struct clk *clk, unsigned long rate) |
| 303 | { |
| 304 | unsigned long flags; |
| 305 | unsigned prescale; |
| 306 | unsigned long actual; |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 307 | unsigned long prev = ULONG_MAX; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 308 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 309 | if (!clk_is_programmable(clk)) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 310 | return -EINVAL; |
| 311 | spin_lock_irqsave(&clk_lock, flags); |
| 312 | |
| 313 | actual = clk->parent->rate_hz; |
| 314 | for (prescale = 0; prescale < 7; prescale++) { |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 315 | if (actual > rate) |
| 316 | prev = actual; |
| 317 | |
| 318 | if (actual && actual <= rate) { |
| 319 | if ((prev - rate) < (rate - actual)) { |
| 320 | actual = prev; |
| 321 | prescale--; |
| 322 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 323 | break; |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 324 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 325 | actual >>= 1; |
| 326 | } |
| 327 | |
| 328 | spin_unlock_irqrestore(&clk_lock, flags); |
| 329 | return (prescale < 7) ? actual : -ENOENT; |
| 330 | } |
| 331 | EXPORT_SYMBOL(clk_round_rate); |
| 332 | |
| 333 | int clk_set_rate(struct clk *clk, unsigned long rate) |
| 334 | { |
| 335 | unsigned long flags; |
| 336 | unsigned prescale; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 337 | unsigned long prescale_offset, css_mask; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 338 | unsigned long actual; |
| 339 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 340 | if (!clk_is_programmable(clk)) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 341 | return -EINVAL; |
| 342 | if (clk->users) |
| 343 | return -EBUSY; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 344 | |
| 345 | if (cpu_has_alt_prescaler()) { |
| 346 | prescale_offset = PMC_ALT_PRES_OFFSET; |
| 347 | css_mask = AT91_PMC_ALT_PCKR_CSS; |
| 348 | } else { |
| 349 | prescale_offset = PMC_PRES_OFFSET; |
| 350 | css_mask = AT91_PMC_CSS; |
| 351 | } |
| 352 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 353 | spin_lock_irqsave(&clk_lock, flags); |
| 354 | |
| 355 | actual = clk->parent->rate_hz; |
| 356 | for (prescale = 0; prescale < 7; prescale++) { |
| 357 | if (actual && actual <= rate) { |
| 358 | u32 pckr; |
| 359 | |
| 360 | pckr = at91_sys_read(AT91_PMC_PCKR(clk->id)); |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 361 | pckr &= css_mask; /* keep clock selection */ |
| 362 | pckr |= prescale << prescale_offset; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 363 | at91_sys_write(AT91_PMC_PCKR(clk->id), pckr); |
| 364 | clk->rate_hz = actual; |
| 365 | break; |
| 366 | } |
| 367 | actual >>= 1; |
| 368 | } |
| 369 | |
| 370 | spin_unlock_irqrestore(&clk_lock, flags); |
| 371 | return (prescale < 7) ? actual : -ENOENT; |
| 372 | } |
| 373 | EXPORT_SYMBOL(clk_set_rate); |
| 374 | |
| 375 | struct clk *clk_get_parent(struct clk *clk) |
| 376 | { |
| 377 | return clk->parent; |
| 378 | } |
| 379 | EXPORT_SYMBOL(clk_get_parent); |
| 380 | |
| 381 | int clk_set_parent(struct clk *clk, struct clk *parent) |
| 382 | { |
| 383 | unsigned long flags; |
| 384 | |
| 385 | if (clk->users) |
| 386 | return -EBUSY; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 387 | if (!clk_is_primary(parent) || !clk_is_programmable(clk)) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 388 | return -EINVAL; |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 389 | |
| 390 | if (cpu_is_at91sam9rl() && parent->id == AT91_PMC_CSS_PLLB) |
| 391 | return -EINVAL; |
| 392 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 393 | spin_lock_irqsave(&clk_lock, flags); |
| 394 | |
| 395 | clk->rate_hz = parent->rate_hz; |
| 396 | clk->parent = parent; |
| 397 | at91_sys_write(AT91_PMC_PCKR(clk->id), parent->id); |
| 398 | |
| 399 | spin_unlock_irqrestore(&clk_lock, flags); |
| 400 | return 0; |
| 401 | } |
| 402 | EXPORT_SYMBOL(clk_set_parent); |
| 403 | |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 404 | /* establish PCK0..PCKN parentage and rate */ |
David Brownell | 72e7ae8 | 2008-02-06 22:03:42 +0100 | [diff] [blame] | 405 | static void __init init_programmable_clock(struct clk *clk) |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 406 | { |
| 407 | struct clk *parent; |
| 408 | u32 pckr; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 409 | unsigned int css_mask; |
| 410 | |
| 411 | if (cpu_has_alt_prescaler()) |
| 412 | css_mask = AT91_PMC_ALT_PCKR_CSS; |
| 413 | else |
| 414 | css_mask = AT91_PMC_CSS; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 415 | |
| 416 | pckr = at91_sys_read(AT91_PMC_PCKR(clk->id)); |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 417 | parent = at91_css_to_clk(pckr & css_mask); |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 418 | clk->parent = parent; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 419 | clk->rate_hz = parent->rate_hz / pmc_prescaler_divider(pckr); |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 420 | } |
| 421 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 422 | #endif /* CONFIG_AT91_PROGRAMMABLE_CLOCKS */ |
| 423 | |
| 424 | /*------------------------------------------------------------------------*/ |
| 425 | |
| 426 | #ifdef CONFIG_DEBUG_FS |
| 427 | |
| 428 | static int at91_clk_show(struct seq_file *s, void *unused) |
| 429 | { |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 430 | u32 scsr, pcsr, uckr = 0, sr; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 431 | struct clk *clk; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 432 | |
| 433 | seq_printf(s, "SCSR = %8x\n", scsr = at91_sys_read(AT91_PMC_SCSR)); |
| 434 | seq_printf(s, "PCSR = %8x\n", pcsr = at91_sys_read(AT91_PMC_PCSR)); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 435 | seq_printf(s, "MOR = %8x\n", at91_sys_read(AT91_CKGR_MOR)); |
| 436 | seq_printf(s, "MCFR = %8x\n", at91_sys_read(AT91_CKGR_MCFR)); |
| 437 | seq_printf(s, "PLLA = %8x\n", at91_sys_read(AT91_CKGR_PLLAR)); |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 438 | if (cpu_has_pllb()) |
Nicolas Ferre | ba45ca4 | 2008-04-08 13:59:18 +0100 | [diff] [blame] | 439 | seq_printf(s, "PLLB = %8x\n", at91_sys_read(AT91_CKGR_PLLBR)); |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 440 | if (cpu_has_utmi()) |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 441 | seq_printf(s, "UCKR = %8x\n", uckr = at91_sys_read(AT91_CKGR_UCKR)); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 442 | seq_printf(s, "MCKR = %8x\n", at91_sys_read(AT91_PMC_MCKR)); |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 443 | if (cpu_has_upll()) |
| 444 | seq_printf(s, "USB = %8x\n", at91_sys_read(AT91_PMC_USB)); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 445 | seq_printf(s, "SR = %8x\n", sr = at91_sys_read(AT91_PMC_SR)); |
| 446 | |
| 447 | seq_printf(s, "\n"); |
| 448 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 449 | list_for_each_entry(clk, &clocks, node) { |
| 450 | char *state; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 451 | |
| 452 | if (clk->mode == pmc_sys_mode) |
| 453 | state = (scsr & clk->pmc_mask) ? "on" : "off"; |
| 454 | else if (clk->mode == pmc_periph_mode) |
| 455 | state = (pcsr & clk->pmc_mask) ? "on" : "off"; |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 456 | else if (clk->mode == pmc_uckr_mode) |
| 457 | state = (uckr & clk->pmc_mask) ? "on" : "off"; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 458 | else if (clk->pmc_mask) |
| 459 | state = (sr & clk->pmc_mask) ? "on" : "off"; |
| 460 | else if (clk == &clk32k || clk == &main_clk) |
| 461 | state = "on"; |
| 462 | else |
| 463 | state = ""; |
| 464 | |
Andrew Victor | 69b648a | 2006-03-22 20:14:14 +0000 | [diff] [blame] | 465 | seq_printf(s, "%-10s users=%2d %-3s %9ld Hz %s\n", |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 466 | clk->name, clk->users, state, clk_get_rate(clk), |
| 467 | clk->parent ? clk->parent->name : ""); |
| 468 | } |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static int at91_clk_open(struct inode *inode, struct file *file) |
| 473 | { |
| 474 | return single_open(file, at91_clk_show, NULL); |
| 475 | } |
| 476 | |
Arjan van de Ven | 5dfe4c9 | 2007-02-12 00:55:31 -0800 | [diff] [blame] | 477 | static const struct file_operations at91_clk_operations = { |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 478 | .open = at91_clk_open, |
| 479 | .read = seq_read, |
| 480 | .llseek = seq_lseek, |
| 481 | .release = single_release, |
| 482 | }; |
| 483 | |
| 484 | static int __init at91_clk_debugfs_init(void) |
| 485 | { |
| 486 | /* /sys/kernel/debug/at91_clk */ |
| 487 | (void) debugfs_create_file("at91_clk", S_IFREG | S_IRUGO, NULL, NULL, &at91_clk_operations); |
| 488 | |
| 489 | return 0; |
| 490 | } |
| 491 | postcore_initcall(at91_clk_debugfs_init); |
| 492 | |
| 493 | #endif |
| 494 | |
| 495 | /*------------------------------------------------------------------------*/ |
| 496 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 497 | /* Register a new clock */ |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 498 | static void __init at91_clk_add(struct clk *clk) |
| 499 | { |
| 500 | list_add_tail(&clk->node, &clocks); |
| 501 | |
| 502 | clk->cl.con_id = clk->name; |
| 503 | clk->cl.clk = clk; |
| 504 | clkdev_add(&clk->cl); |
| 505 | } |
| 506 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 507 | int __init clk_register(struct clk *clk) |
| 508 | { |
| 509 | if (clk_is_peripheral(clk)) { |
Nicolas Ferre | 5afddee | 2010-09-09 19:58:23 +0200 | [diff] [blame] | 510 | if (!clk->parent) |
| 511 | clk->parent = &mck; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 512 | clk->mode = pmc_periph_mode; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 513 | } |
Andrew Victor | d481f86 | 2006-12-01 11:27:31 +0100 | [diff] [blame] | 514 | else if (clk_is_sys(clk)) { |
| 515 | clk->parent = &mck; |
| 516 | clk->mode = pmc_sys_mode; |
Andrew Victor | d481f86 | 2006-12-01 11:27:31 +0100 | [diff] [blame] | 517 | } |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 518 | #ifdef CONFIG_AT91_PROGRAMMABLE_CLOCKS |
| 519 | else if (clk_is_programmable(clk)) { |
| 520 | clk->mode = pmc_sys_mode; |
| 521 | init_programmable_clock(clk); |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 522 | } |
| 523 | #endif |
| 524 | |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 525 | at91_clk_add(clk); |
| 526 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 530 | /*------------------------------------------------------------------------*/ |
| 531 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 532 | static u32 __init at91_pll_rate(struct clk *pll, u32 freq, u32 reg) |
| 533 | { |
| 534 | unsigned mul, div; |
| 535 | |
| 536 | div = reg & 0xff; |
| 537 | mul = (reg >> 16) & 0x7ff; |
| 538 | if (div && mul) { |
| 539 | freq /= div; |
| 540 | freq *= mul + 1; |
| 541 | } else |
| 542 | freq = 0; |
Andrew Victor | 69b648a | 2006-03-22 20:14:14 +0000 | [diff] [blame] | 543 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 544 | return freq; |
| 545 | } |
| 546 | |
Andrew Victor | 69b648a | 2006-03-22 20:14:14 +0000 | [diff] [blame] | 547 | static u32 __init at91_usb_rate(struct clk *pll, u32 freq, u32 reg) |
| 548 | { |
| 549 | if (pll == &pllb && (reg & AT91_PMC_USB96M)) |
| 550 | return freq / 2; |
| 551 | else |
| 552 | return freq; |
| 553 | } |
| 554 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 555 | static unsigned __init at91_pll_calc(unsigned main_freq, unsigned out_freq) |
| 556 | { |
| 557 | unsigned i, div = 0, mul = 0, diff = 1 << 30; |
| 558 | unsigned ret = (out_freq > 155000000) ? 0xbe00 : 0x3e00; |
| 559 | |
| 560 | /* PLL output max 240 MHz (or 180 MHz per errata) */ |
| 561 | if (out_freq > 240000000) |
| 562 | goto fail; |
| 563 | |
| 564 | for (i = 1; i < 256; i++) { |
| 565 | int diff1; |
| 566 | unsigned input, mul1; |
| 567 | |
| 568 | /* |
| 569 | * PLL input between 1MHz and 32MHz per spec, but lower |
| 570 | * frequences seem necessary in some cases so allow 100K. |
sedji gaouaou | 6135266 | 2008-07-10 10:15:35 +0100 | [diff] [blame] | 571 | * Warning: some newer products need 2MHz min. |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 572 | */ |
| 573 | input = main_freq / i; |
sedji gaouaou | 6135266 | 2008-07-10 10:15:35 +0100 | [diff] [blame] | 574 | if (cpu_is_at91sam9g20() && input < 2000000) |
| 575 | continue; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 576 | if (input < 100000) |
| 577 | continue; |
| 578 | if (input > 32000000) |
| 579 | continue; |
| 580 | |
| 581 | mul1 = out_freq / input; |
sedji gaouaou | 6135266 | 2008-07-10 10:15:35 +0100 | [diff] [blame] | 582 | if (cpu_is_at91sam9g20() && mul > 63) |
| 583 | continue; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 584 | if (mul1 > 2048) |
| 585 | continue; |
| 586 | if (mul1 < 2) |
| 587 | goto fail; |
| 588 | |
| 589 | diff1 = out_freq - input * mul1; |
| 590 | if (diff1 < 0) |
| 591 | diff1 = -diff1; |
| 592 | if (diff > diff1) { |
| 593 | diff = diff1; |
| 594 | div = i; |
| 595 | mul = mul1; |
| 596 | if (diff == 0) |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | if (i == 256 && diff > (out_freq >> 5)) |
| 601 | goto fail; |
| 602 | return ret | ((mul - 1) << 16) | div; |
| 603 | fail: |
| 604 | return 0; |
| 605 | } |
| 606 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 607 | static struct clk *const standard_pmc_clocks[] __initdata = { |
| 608 | /* four primary clocks */ |
| 609 | &clk32k, |
| 610 | &main_clk, |
| 611 | &plla, |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 612 | |
| 613 | /* MCK */ |
| 614 | &mck |
| 615 | }; |
| 616 | |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 617 | /* PLLB generated USB full speed clock init */ |
| 618 | static void __init at91_pllb_usbfs_clock_init(unsigned long main_clock) |
| 619 | { |
| 620 | /* |
| 621 | * USB clock init: choose 48 MHz PLLB value, |
| 622 | * disable 48MHz clock during usb peripheral suspend. |
| 623 | * |
| 624 | * REVISIT: assumes MCK doesn't derive from PLLB! |
| 625 | */ |
| 626 | uhpck.parent = &pllb; |
| 627 | |
| 628 | at91_pllb_usb_init = at91_pll_calc(main_clock, 48000000 * 2) | AT91_PMC_USB96M; |
| 629 | pllb.rate_hz = at91_pll_rate(&pllb, main_clock, at91_pllb_usb_init); |
| 630 | if (cpu_is_at91rm9200()) { |
| 631 | uhpck.pmc_mask = AT91RM9200_PMC_UHP; |
| 632 | udpck.pmc_mask = AT91RM9200_PMC_UDP; |
| 633 | at91_sys_write(AT91_PMC_SCER, AT91RM9200_PMC_MCKUDP); |
Nicolas Ferre | eab4170 | 2009-06-26 15:37:00 +0100 | [diff] [blame] | 634 | } else if (cpu_is_at91sam9260() || cpu_is_at91sam9261() || |
| 635 | cpu_is_at91sam9263() || cpu_is_at91sam9g20() || |
Jean-Christophe PLAGNIOL-VILLARD | 7a2207a | 2011-05-17 20:51:14 +0800 | [diff] [blame] | 636 | cpu_is_at91sam9g10()) { |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 637 | uhpck.pmc_mask = AT91SAM926x_PMC_UHP; |
| 638 | udpck.pmc_mask = AT91SAM926x_PMC_UDP; |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 639 | } |
| 640 | at91_sys_write(AT91_CKGR_PLLBR, 0); |
| 641 | |
| 642 | udpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init); |
| 643 | uhpck.rate_hz = at91_usb_rate(&pllb, pllb.rate_hz, at91_pllb_usb_init); |
| 644 | } |
| 645 | |
| 646 | /* UPLL generated USB full speed clock init */ |
| 647 | static void __init at91_upll_usbfs_clock_init(unsigned long main_clock) |
| 648 | { |
| 649 | /* |
| 650 | * USB clock init: choose 480 MHz from UPLL, |
| 651 | */ |
| 652 | unsigned int usbr = AT91_PMC_USBS_UPLL; |
| 653 | |
| 654 | /* Setup divider by 10 to reach 48 MHz */ |
| 655 | usbr |= ((10 - 1) << 8) & AT91_PMC_OHCIUSBDIV; |
| 656 | |
| 657 | at91_sys_write(AT91_PMC_USB, usbr); |
| 658 | |
| 659 | /* Now set uhpck values */ |
| 660 | uhpck.parent = &utmi_clk; |
| 661 | uhpck.pmc_mask = AT91SAM926x_PMC_UHP; |
Ryan Mallon | 8251544 | 2010-06-02 12:55:36 +1200 | [diff] [blame] | 662 | uhpck.rate_hz = utmi_clk.rate_hz; |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 663 | uhpck.rate_hz /= 1 + ((at91_sys_read(AT91_PMC_USB) & AT91_PMC_OHCIUSBDIV) >> 8); |
| 664 | } |
| 665 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 666 | int __init at91_clock_init(unsigned long main_clock) |
| 667 | { |
| 668 | unsigned tmp, freq, mckr; |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 669 | int i; |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 670 | int pll_overclock = false; |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 671 | |
| 672 | /* |
| 673 | * When the bootloader initialized the main oscillator correctly, |
| 674 | * there's no problem using the cycle counter. But if it didn't, |
| 675 | * or when using oscillator bypass mode, we must be told the speed |
| 676 | * of the main clock. |
| 677 | */ |
| 678 | if (!main_clock) { |
| 679 | do { |
| 680 | tmp = at91_sys_read(AT91_CKGR_MCFR); |
Andrew Victor | 69b648a | 2006-03-22 20:14:14 +0000 | [diff] [blame] | 681 | } while (!(tmp & AT91_PMC_MAINRDY)); |
| 682 | main_clock = (tmp & AT91_PMC_MAINF) * (AT91_SLOW_CLOCK / 16); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 683 | } |
| 684 | main_clk.rate_hz = main_clock; |
| 685 | |
| 686 | /* report if PLLA is more than mildly overclocked */ |
| 687 | plla.rate_hz = at91_pll_rate(&plla, main_clock, at91_sys_read(AT91_CKGR_PLLAR)); |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 688 | if (cpu_has_300M_plla()) { |
| 689 | if (plla.rate_hz > 300000000) |
| 690 | pll_overclock = true; |
| 691 | } else if (cpu_has_800M_plla()) { |
| 692 | if (plla.rate_hz > 800000000) |
| 693 | pll_overclock = true; |
| 694 | } else { |
| 695 | if (plla.rate_hz > 209000000) |
| 696 | pll_overclock = true; |
| 697 | } |
| 698 | if (pll_overclock) |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 699 | pr_info("Clocks: PLLA overclocked, %ld MHz\n", plla.rate_hz / 1000000); |
| 700 | |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 701 | if (cpu_has_plladiv2()) { |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 702 | mckr = at91_sys_read(AT91_PMC_MCKR); |
| 703 | plla.rate_hz /= (1 << ((mckr & AT91_PMC_PLLADIV2) >> 12)); /* plla divisor by 2 */ |
| 704 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 705 | |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 706 | if (!cpu_has_pllb() && cpu_has_upll()) { |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 707 | /* setup UTMI clock as the fourth primary clock |
| 708 | * (instead of pllb) */ |
| 709 | utmi_clk.type |= CLK_TYPE_PRIMARY; |
| 710 | utmi_clk.id = 3; |
| 711 | } |
| 712 | |
Andrew Victor | 69b648a | 2006-03-22 20:14:14 +0000 | [diff] [blame] | 713 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 714 | /* |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 715 | * USB HS clock init |
| 716 | */ |
Andrew Victor | 5e38efa | 2009-12-15 21:57:27 +0100 | [diff] [blame] | 717 | if (cpu_has_utmi()) { |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 718 | /* |
| 719 | * multiplier is hard-wired to 40 |
| 720 | * (obtain the USB High Speed 480 MHz when input is 12 MHz) |
| 721 | */ |
| 722 | utmi_clk.rate_hz = 40 * utmi_clk.parent->rate_hz; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 723 | |
| 724 | /* UTMI bias and PLL are managed at the same time */ |
| 725 | if (cpu_has_upll()) |
| 726 | utmi_clk.pmc_mask |= AT91_PMC_BIASEN; |
Andrew Victor | 5e38efa | 2009-12-15 21:57:27 +0100 | [diff] [blame] | 727 | } |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * USB FS clock init |
| 731 | */ |
| 732 | if (cpu_has_pllb()) |
| 733 | at91_pllb_usbfs_clock_init(main_clock); |
| 734 | if (cpu_has_upll()) |
| 735 | /* assumes that we choose UPLL for USB and not PLLA */ |
| 736 | at91_upll_usbfs_clock_init(main_clock); |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 737 | |
| 738 | /* |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 739 | * MCK and CPU derive from one of those primary clocks. |
| 740 | * For now, assume this parentage won't change. |
| 741 | */ |
| 742 | mckr = at91_sys_read(AT91_PMC_MCKR); |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 743 | mck.parent = at91_css_to_clk(mckr & AT91_PMC_CSS); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 744 | freq = mck.parent->rate_hz; |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 745 | freq /= pmc_prescaler_divider(mckr); /* prescale */ |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 746 | if (cpu_is_at91rm9200()) { |
Andrew Victor | a95c729 | 2007-11-19 11:52:09 +0100 | [diff] [blame] | 747 | mck.rate_hz = freq / (1 + ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */ |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 748 | } else if (cpu_is_at91sam9g20()) { |
sedji gaouaou | 6135266 | 2008-07-10 10:15:35 +0100 | [diff] [blame] | 749 | mck.rate_hz = (mckr & AT91_PMC_MDIV) ? |
| 750 | freq / ((mckr & AT91_PMC_MDIV) >> 7) : freq; /* mdiv ; (x >> 7) = ((x >> 8) * 2) */ |
| 751 | if (mckr & AT91_PMC_PDIV) |
| 752 | freq /= 2; /* processor clock division */ |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 753 | } else if (cpu_has_mdiv3()) { |
Nicolas Ferre | 2ef9df7 | 2009-06-26 15:36:57 +0100 | [diff] [blame] | 754 | mck.rate_hz = (mckr & AT91_PMC_MDIV) == AT91SAM9_PMC_MDIV_3 ? |
| 755 | freq / 3 : freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */ |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 756 | } else { |
Andrew Victor | 5e38efa | 2009-12-15 21:57:27 +0100 | [diff] [blame] | 757 | mck.rate_hz = freq / (1 << ((mckr & AT91_PMC_MDIV) >> 8)); /* mdiv */ |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 758 | } |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 759 | |
Nicolas Ferre | 1112872 | 2011-03-10 19:08:54 +0100 | [diff] [blame^] | 760 | if (cpu_has_alt_prescaler()) { |
| 761 | /* Programmable clocks can use MCK */ |
| 762 | mck.type |= CLK_TYPE_PRIMARY; |
| 763 | mck.id = 4; |
| 764 | } |
| 765 | |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 766 | /* Register the PMC's standard clocks */ |
| 767 | for (i = 0; i < ARRAY_SIZE(standard_pmc_clocks); i++) |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 768 | at91_clk_add(standard_pmc_clocks[i]); |
Andrew Victor | 2eeaaa2 | 2006-09-27 10:50:59 +0100 | [diff] [blame] | 769 | |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 770 | if (cpu_has_pllb()) |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 771 | at91_clk_add(&pllb); |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 772 | |
| 773 | if (cpu_has_uhp()) |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 774 | at91_clk_add(&uhpck); |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 775 | |
| 776 | if (cpu_has_udpfs()) |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 777 | at91_clk_add(&udpck); |
Nicolas Ferre | 6d0485a | 2009-03-31 17:13:15 +0100 | [diff] [blame] | 778 | |
| 779 | if (cpu_has_utmi()) |
Jean-Christophe PLAGNIOL-VILLARD | bd60299 | 2011-02-02 07:27:07 +0100 | [diff] [blame] | 780 | at91_clk_add(&utmi_clk); |
Stelian Pop | 53d7168 | 2008-04-05 21:14:03 +0100 | [diff] [blame] | 781 | |
Andrew Victor | 91f8ed8 | 2006-06-19 13:20:23 +0100 | [diff] [blame] | 782 | /* MCK and CPU clock are "always on" */ |
| 783 | clk_enable(&mck); |
| 784 | |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 785 | printk("Clocks: CPU %u MHz, master %u MHz, main %u.%03u MHz\n", |
| 786 | freq / 1000000, (unsigned) mck.rate_hz / 1000000, |
| 787 | (unsigned) main_clock / 1000000, |
| 788 | ((unsigned) main_clock % 1000000) / 1000); |
| 789 | |
Andrew Victor | c9b75d1 | 2007-02-08 17:36:34 +0100 | [diff] [blame] | 790 | return 0; |
| 791 | } |
Andrew Victor | 91f8ed8 | 2006-06-19 13:20:23 +0100 | [diff] [blame] | 792 | |
Andrew Victor | c9b75d1 | 2007-02-08 17:36:34 +0100 | [diff] [blame] | 793 | /* |
| 794 | * Several unused clocks may be active. Turn them off. |
| 795 | */ |
| 796 | static int __init at91_clock_reset(void) |
| 797 | { |
| 798 | unsigned long pcdr = 0; |
| 799 | unsigned long scdr = 0; |
| 800 | struct clk *clk; |
| 801 | |
| 802 | list_for_each_entry(clk, &clocks, node) { |
| 803 | if (clk->users > 0) |
| 804 | continue; |
| 805 | |
| 806 | if (clk->mode == pmc_periph_mode) |
| 807 | pcdr |= clk->pmc_mask; |
| 808 | |
| 809 | if (clk->mode == pmc_sys_mode) |
| 810 | scdr |= clk->pmc_mask; |
| 811 | |
| 812 | pr_debug("Clocks: disable unused %s\n", clk->name); |
| 813 | } |
| 814 | |
| 815 | at91_sys_write(AT91_PMC_PCDR, pcdr); |
| 816 | at91_sys_write(AT91_PMC_SCDR, scdr); |
SAN People | 73a59c1 | 2006-01-09 17:05:41 +0000 | [diff] [blame] | 817 | |
| 818 | return 0; |
| 819 | } |
Andrew Victor | c9b75d1 | 2007-02-08 17:36:34 +0100 | [diff] [blame] | 820 | late_initcall(at91_clock_reset); |