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