Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Cpufreq driver for the loongson-2 processors
0003  *
0004  * The 2E revision of loongson processor not support this feature.
0005  *
0006  * Copyright (C) 2006 - 2008 Lemote Inc. & Institute of Computing Technology
0007  * Author: Yanhua, yanh@lemote.com
0008  *
0009  * This file is subject to the terms and conditions of the GNU General Public
0010  * License.  See the file "COPYING" in the main directory of this archive
0011  * for more details.
0012  */
0013 
0014 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0015 
0016 #include <linux/cpufreq.h>
0017 #include <linux/module.h>
0018 #include <linux/err.h>
0019 #include <linux/delay.h>
0020 #include <linux/platform_device.h>
0021 
0022 #include <asm/idle.h>
0023 
0024 #include <asm/mach-loongson2ef/loongson.h>
0025 
0026 static uint nowait;
0027 
0028 static void (*saved_cpu_wait) (void);
0029 
0030 static int loongson2_cpu_freq_notifier(struct notifier_block *nb,
0031                     unsigned long val, void *data);
0032 
0033 static struct notifier_block loongson2_cpufreq_notifier_block = {
0034     .notifier_call = loongson2_cpu_freq_notifier
0035 };
0036 
0037 static int loongson2_cpu_freq_notifier(struct notifier_block *nb,
0038                     unsigned long val, void *data)
0039 {
0040     if (val == CPUFREQ_POSTCHANGE)
0041         current_cpu_data.udelay_val = loops_per_jiffy;
0042 
0043     return 0;
0044 }
0045 
0046 /*
0047  * Here we notify other drivers of the proposed change and the final change.
0048  */
0049 static int loongson2_cpufreq_target(struct cpufreq_policy *policy,
0050                      unsigned int index)
0051 {
0052     unsigned int freq;
0053 
0054     freq =
0055         ((cpu_clock_freq / 1000) *
0056          loongson2_clockmod_table[index].driver_data) / 8;
0057 
0058     /* setting the cpu frequency */
0059     loongson2_cpu_set_rate(freq);
0060 
0061     return 0;
0062 }
0063 
0064 static int loongson2_cpufreq_cpu_init(struct cpufreq_policy *policy)
0065 {
0066     int i;
0067     unsigned long rate;
0068     int ret;
0069 
0070     rate = cpu_clock_freq / 1000;
0071     if (!rate)
0072         return -EINVAL;
0073 
0074     /* clock table init */
0075     for (i = 2;
0076          (loongson2_clockmod_table[i].frequency != CPUFREQ_TABLE_END);
0077          i++)
0078         loongson2_clockmod_table[i].frequency = (rate * i) / 8;
0079 
0080     ret = loongson2_cpu_set_rate(rate);
0081     if (ret)
0082         return ret;
0083 
0084     cpufreq_generic_init(policy, &loongson2_clockmod_table[0], 0);
0085     return 0;
0086 }
0087 
0088 static int loongson2_cpufreq_exit(struct cpufreq_policy *policy)
0089 {
0090     return 0;
0091 }
0092 
0093 static struct cpufreq_driver loongson2_cpufreq_driver = {
0094     .name = "loongson2",
0095     .init = loongson2_cpufreq_cpu_init,
0096     .verify = cpufreq_generic_frequency_table_verify,
0097     .target_index = loongson2_cpufreq_target,
0098     .get = cpufreq_generic_get,
0099     .exit = loongson2_cpufreq_exit,
0100     .attr = cpufreq_generic_attr,
0101 };
0102 
0103 static const struct platform_device_id platform_device_ids[] = {
0104     {
0105         .name = "loongson2_cpufreq",
0106     },
0107     {}
0108 };
0109 
0110 MODULE_DEVICE_TABLE(platform, platform_device_ids);
0111 
0112 static struct platform_driver platform_driver = {
0113     .driver = {
0114         .name = "loongson2_cpufreq",
0115     },
0116     .id_table = platform_device_ids,
0117 };
0118 
0119 /*
0120  * This is the simple version of Loongson-2 wait, Maybe we need do this in
0121  * interrupt disabled context.
0122  */
0123 
0124 static DEFINE_SPINLOCK(loongson2_wait_lock);
0125 
0126 static void loongson2_cpu_wait(void)
0127 {
0128     unsigned long flags;
0129     u32 cpu_freq;
0130 
0131     spin_lock_irqsave(&loongson2_wait_lock, flags);
0132     cpu_freq = readl(LOONGSON_CHIPCFG);
0133     /* Put CPU into wait mode */
0134     writel(readl(LOONGSON_CHIPCFG) & ~0x7, LOONGSON_CHIPCFG);
0135     /* Restore CPU state */
0136     writel(cpu_freq, LOONGSON_CHIPCFG);
0137     spin_unlock_irqrestore(&loongson2_wait_lock, flags);
0138     local_irq_enable();
0139 }
0140 
0141 static int __init cpufreq_init(void)
0142 {
0143     int ret;
0144 
0145     /* Register platform stuff */
0146     ret = platform_driver_register(&platform_driver);
0147     if (ret)
0148         return ret;
0149 
0150     pr_info("Loongson-2F CPU frequency driver\n");
0151 
0152     cpufreq_register_notifier(&loongson2_cpufreq_notifier_block,
0153                   CPUFREQ_TRANSITION_NOTIFIER);
0154 
0155     ret = cpufreq_register_driver(&loongson2_cpufreq_driver);
0156 
0157     if (!ret && !nowait) {
0158         saved_cpu_wait = cpu_wait;
0159         cpu_wait = loongson2_cpu_wait;
0160     }
0161 
0162     return ret;
0163 }
0164 
0165 static void __exit cpufreq_exit(void)
0166 {
0167     if (!nowait && saved_cpu_wait)
0168         cpu_wait = saved_cpu_wait;
0169     cpufreq_unregister_driver(&loongson2_cpufreq_driver);
0170     cpufreq_unregister_notifier(&loongson2_cpufreq_notifier_block,
0171                     CPUFREQ_TRANSITION_NOTIFIER);
0172 
0173     platform_driver_unregister(&platform_driver);
0174 }
0175 
0176 module_init(cpufreq_init);
0177 module_exit(cpufreq_exit);
0178 
0179 module_param(nowait, uint, 0644);
0180 MODULE_PARM_DESC(nowait, "Disable Loongson-2F specific wait");
0181 
0182 MODULE_AUTHOR("Yanhua <yanh@lemote.com>");
0183 MODULE_DESCRIPTION("cpufreq driver for Loongson2F");
0184 MODULE_LICENSE("GPL");