Commit | Line | Data |
---|---|---|
9e58e185 HCE |
1 | /* |
2 | * Copyright (C) 2004-2007 Atmel Corporation | |
3 | * | |
4 | * Based on MIPS implementation arch/mips/kernel/time.c | |
5 | * Copyright 2001 MontaVista Software Inc. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License version 2 as | |
9 | * published by the Free Software Foundation. | |
10 | */ | |
11 | ||
12 | /*#define DEBUG*/ | |
13 | ||
14 | #include <linux/kernel.h> | |
15 | #include <linux/types.h> | |
16 | #include <linux/init.h> | |
17 | #include <linux/cpufreq.h> | |
18 | #include <linux/io.h> | |
19 | #include <linux/clk.h> | |
20 | #include <linux/err.h> | |
09cf6a29 | 21 | #include <linux/export.h> |
848cb944 | 22 | #include <linux/slab.h> |
9e58e185 HCE |
23 | |
24 | static struct clk *cpuclk; | |
848cb944 | 25 | static struct cpufreq_frequency_table *freq_table; |
9e58e185 | 26 | |
9e58e185 HCE |
27 | static unsigned int at32_get_speed(unsigned int cpu) |
28 | { | |
29 | /* No SMP support */ | |
30 | if (cpu) | |
31 | return 0; | |
32 | return (unsigned int)((clk_get_rate(cpuclk) + 500) / 1000); | |
33 | } | |
34 | ||
e3f91ca4 HS |
35 | static unsigned int ref_freq; |
36 | static unsigned long loops_per_jiffy_ref; | |
37 | ||
9c0ebcf7 | 38 | static int at32_set_target(struct cpufreq_policy *policy, unsigned int index) |
9e58e185 | 39 | { |
d4019f0a | 40 | unsigned int old_freq, new_freq; |
9e58e185 | 41 | |
d4019f0a VK |
42 | old_freq = at32_get_speed(0); |
43 | new_freq = freq_table[index].frequency; | |
9e58e185 | 44 | |
e3f91ca4 | 45 | if (!ref_freq) { |
d4019f0a | 46 | ref_freq = old_freq; |
e3f91ca4 HS |
47 | loops_per_jiffy_ref = boot_cpu_data.loops_per_jiffy; |
48 | } | |
49 | ||
d4019f0a | 50 | if (old_freq < new_freq) |
e3f91ca4 | 51 | boot_cpu_data.loops_per_jiffy = cpufreq_scale( |
d4019f0a VK |
52 | loops_per_jiffy_ref, ref_freq, new_freq); |
53 | clk_set_rate(cpuclk, new_freq * 1000); | |
54 | if (new_freq < old_freq) | |
e3f91ca4 | 55 | boot_cpu_data.loops_per_jiffy = cpufreq_scale( |
d4019f0a | 56 | loops_per_jiffy_ref, ref_freq, new_freq); |
9e58e185 HCE |
57 | |
58 | return 0; | |
59 | } | |
60 | ||
61 | static int __init at32_cpufreq_driver_init(struct cpufreq_policy *policy) | |
62 | { | |
017189b5 | 63 | unsigned int frequency, rate, min_freq; |
848cb944 HCE |
64 | int retval, steps, i; |
65 | ||
9e58e185 HCE |
66 | if (policy->cpu != 0) |
67 | return -EINVAL; | |
68 | ||
69 | cpuclk = clk_get(NULL, "cpu"); | |
70 | if (IS_ERR(cpuclk)) { | |
71 | pr_debug("cpufreq: could not get CPU clk\n"); | |
848cb944 HCE |
72 | retval = PTR_ERR(cpuclk); |
73 | goto out_err; | |
9e58e185 HCE |
74 | } |
75 | ||
017189b5 VK |
76 | min_freq = (clk_round_rate(cpuclk, 1) + 500) / 1000; |
77 | frequency = (clk_round_rate(cpuclk, ~0UL) + 500) / 1000; | |
9e58e185 | 78 | policy->cpuinfo.transition_latency = 0; |
9e58e185 | 79 | |
848cb944 HCE |
80 | /* |
81 | * AVR32 CPU frequency rate scales in power of two between maximum and | |
82 | * minimum, also add space for the table end marker. | |
83 | * | |
84 | * Further validate that the frequency is usable, and append it to the | |
85 | * frequency table. | |
86 | */ | |
017189b5 | 87 | steps = fls(frequency / min_freq) + 1; |
848cb944 HCE |
88 | freq_table = kzalloc(steps * sizeof(struct cpufreq_frequency_table), |
89 | GFP_KERNEL); | |
90 | if (!freq_table) { | |
91 | retval = -ENOMEM; | |
92 | goto out_err_put_clk; | |
93 | } | |
9e58e185 | 94 | |
848cb944 HCE |
95 | for (i = 0; i < (steps - 1); i++) { |
96 | rate = clk_round_rate(cpuclk, frequency * 1000) / 1000; | |
97 | ||
98 | if (rate != frequency) | |
99 | freq_table[i].frequency = CPUFREQ_ENTRY_INVALID; | |
100 | else | |
101 | freq_table[i].frequency = frequency; | |
102 | ||
103 | frequency /= 2; | |
104 | } | |
105 | ||
106 | freq_table[steps - 1].frequency = CPUFREQ_TABLE_END; | |
107 | ||
108 | retval = cpufreq_table_validate_and_show(policy, freq_table); | |
109 | if (!retval) { | |
110 | printk("cpufreq: AT32AP CPU frequency driver\n"); | |
111 | return 0; | |
112 | } | |
113 | ||
114 | kfree(freq_table); | |
115 | out_err_put_clk: | |
116 | clk_put(cpuclk); | |
117 | out_err: | |
118 | return retval; | |
9e58e185 HCE |
119 | } |
120 | ||
121 | static struct cpufreq_driver at32_driver = { | |
122 | .name = "at32ap", | |
9e58e185 | 123 | .init = at32_cpufreq_driver_init, |
5ae68f47 | 124 | .verify = cpufreq_generic_frequency_table_verify, |
9c0ebcf7 | 125 | .target_index = at32_set_target, |
9e58e185 HCE |
126 | .get = at32_get_speed, |
127 | .flags = CPUFREQ_STICKY, | |
128 | }; | |
129 | ||
130 | static int __init at32_cpufreq_init(void) | |
131 | { | |
132 | return cpufreq_register_driver(&at32_driver); | |
133 | } | |
f04d264a | 134 | late_initcall(at32_cpufreq_init); |