brcm2708: add support for 3.10 kernel
[openwrt.git] / target / linux / brcm2708 / patches-3.10 / 011-bcm2835-cpufreq-driver.patch
diff --git a/target/linux/brcm2708/patches-3.10/011-bcm2835-cpufreq-driver.patch b/target/linux/brcm2708/patches-3.10/011-bcm2835-cpufreq-driver.patch
new file mode 100644 (file)
index 0000000..b4b2eae
--- /dev/null
@@ -0,0 +1,269 @@
+diff -urN linux-3.10/drivers/cpufreq/bcm2835-cpufreq.c linux-rpi-3.10.y/drivers/cpufreq/bcm2835-cpufreq.c
+--- linux-3.10/drivers/cpufreq/bcm2835-cpufreq.c       1970-01-01 01:00:00.000000000 +0100
++++ linux-rpi-3.10.y/drivers/cpufreq/bcm2835-cpufreq.c 2013-07-06 15:25:50.000000000 +0100
+@@ -0,0 +1,239 @@
++/*****************************************************************************\r
++* Copyright 2011 Broadcom Corporation.  All rights reserved.\r
++*\r
++* Unless you and Broadcom execute a separate written software license\r
++* agreement governing use of this software, this software is licensed to you\r
++* under the terms of the GNU General Public License version 2, available at\r
++* http://www.broadcom.com/licenses/GPLv2.php (the "GPL").\r
++*     \r
++* Notwithstanding the above, under no circumstances may you combine this\r
++* software in any way with any other Broadcom software provided under a\r
++* license other than the GPL, without Broadcom's express prior written\r
++* consent.\r
++*****************************************************************************/\r
++\r
++/*****************************************************************************\r
++* FILENAME: bcm2835-cpufreq.h\r
++* DESCRIPTION: This driver dynamically manages the CPU Frequency of the ARM\r
++* processor. Messages are sent to Videocore either setting or requesting the\r
++* frequency of the ARM in order to match an appropiate frequency to the current\r
++* usage of the processor. The policy which selects the frequency to use is\r
++* defined in the kernel .config file, but can be changed during runtime.\r
++*****************************************************************************/\r
++\r
++/* ---------- INCLUDES ---------- */\r
++#include <linux/kernel.h>\r
++#include <linux/init.h>\r
++#include <linux/module.h>\r
++#include <linux/cpufreq.h>\r
++#include <mach/vcio.h>\r
++\r
++/* ---------- DEFINES ---------- */\r
++/*#define CPUFREQ_DEBUG_ENABLE*/              /* enable debugging */\r
++#define MODULE_NAME "bcm2835-cpufreq"\r
++\r
++#define VCMSG_ID_ARM_CLOCK 0x000000003                /* Clock/Voltage ID's */\r
++\r
++/* debug printk macros */\r
++#ifdef CPUFREQ_DEBUG_ENABLE\r
++#define print_debug(fmt,...) pr_debug("%s:%s:%d: "fmt, MODULE_NAME, __func__, __LINE__, ##__VA_ARGS__)\r
++#else\r
++#define print_debug(fmt,...)\r
++#endif\r
++#define print_err(fmt,...) pr_err("%s:%s:%d: "fmt, MODULE_NAME, __func__,__LINE__, ##__VA_ARGS__)\r
++#define print_info(fmt,...) pr_info("%s: "fmt, MODULE_NAME, ##__VA_ARGS__)\r
++\r
++/* tag part of the message */\r
++struct vc_msg_tag {\r
++      uint32_t tag_id;                /* the message id */\r
++      uint32_t buffer_size;           /* size of the buffer (which in this case is always 8 bytes) */\r
++      uint32_t data_size;             /* amount of data being sent or received */\r
++      uint32_t dev_id;                /* the ID of the clock/voltage to get or set */\r
++      uint32_t val;                   /* the value (e.g. rate (in Hz)) to set */\r
++};\r
++\r
++/* message structure to be sent to videocore */\r
++struct vc_msg {\r
++      uint32_t msg_size;              /* simply, sizeof(struct vc_msg) */\r
++      uint32_t request_code;          /* holds various information like the success and number of bytes returned (refer to mailboxes wiki) */\r
++      struct vc_msg_tag tag;          /* the tag structure above to make */\r
++      uint32_t end_tag;               /* an end identifier, should be set to NULL */\r
++};\r
++\r
++/* ---------- GLOBALS ---------- */\r
++static struct cpufreq_driver bcm2835_cpufreq_driver;  /* the cpufreq driver global */\r
++\r
++/*\r
++ ===============================================\r
++  clk_rate either gets or sets the clock rates.\r
++ ===============================================\r
++*/\r
++static uint32_t bcm2835_cpufreq_set_clock(int cur_rate, int arm_rate)\r
++{\r
++      int s, actual_rate=0;\r
++      struct vc_msg msg;\r
++      \r
++      /* wipe all previous message data */\r
++      memset(&msg, 0, sizeof msg);\r
++      \r
++      msg.msg_size = sizeof msg;\r
++                      \r
++      msg.tag.tag_id = VCMSG_SET_CLOCK_RATE;\r
++      msg.tag.buffer_size = 8;\r
++      msg.tag.data_size = 8;   /* we're sending the clock ID and the new rates which is a total of 2 words */\r
++      msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;\r
++      msg.tag.val = arm_rate * 1000;\r
++                      \r
++      /* send the message */\r
++      s = bcm_mailbox_property(&msg, sizeof msg);\r
++      \r
++      /* check if it was all ok and return the rate in KHz */\r
++      if (s == 0 && (msg.request_code & 0x80000000))\r
++              actual_rate = msg.tag.val/1000;\r
++\r
++      print_debug("Setting new frequency = %d -> %d (actual %d)\n", cur_rate, arm_rate, actual_rate); \r
++      return actual_rate;\r
++}\r
++\r
++static uint32_t bcm2835_cpufreq_get_clock(int tag)\r
++{\r
++      int s;\r
++      int arm_rate = 0;\r
++      struct vc_msg msg;\r
++      \r
++      /* wipe all previous message data */\r
++      memset(&msg, 0, sizeof msg);\r
++      \r
++      msg.msg_size = sizeof msg;\r
++      msg.tag.tag_id = tag;\r
++      msg.tag.buffer_size = 8;\r
++      msg.tag.data_size = 4; /* we're just sending the clock ID which is one word long */\r
++      msg.tag.dev_id = VCMSG_ID_ARM_CLOCK;\r
++      \r
++      /* send the message */\r
++      s = bcm_mailbox_property(&msg, sizeof msg);\r
++\r
++      /* check if it was all ok and return the rate in KHz */\r
++      if (s == 0 && (msg.request_code & 0x80000000))\r
++              arm_rate = msg.tag.val/1000;\r
++\r
++      print_debug("%s frequency = %d\n",\r
++              tag == VCMSG_GET_CLOCK_RATE ? "Current":\r
++              tag == VCMSG_GET_MIN_CLOCK ? "Min":\r
++              tag == VCMSG_GET_MAX_CLOCK ? "Max":\r
++              "Unexpected", arm_rate);\r
++      \r
++      return arm_rate;\r
++}\r
++\r
++/*\r
++ ====================================================\r
++  Module Initialisation registers the cpufreq driver\r
++ ====================================================\r
++*/\r
++static int __init bcm2835_cpufreq_module_init(void)\r
++{     \r
++      print_debug("IN\n");\r
++      return cpufreq_register_driver(&bcm2835_cpufreq_driver);\r
++}\r
++\r
++/*\r
++ =============\r
++  Module exit\r
++ =============\r
++*/\r
++static void __exit bcm2835_cpufreq_module_exit(void)\r
++{\r
++      print_debug("IN\n");\r
++      cpufreq_unregister_driver(&bcm2835_cpufreq_driver);\r
++      return;\r
++}\r
++\r
++/*\r
++ ==============================================================\r
++  Initialisation function sets up the CPU policy for first use\r
++ ==============================================================\r
++*/\r
++static int bcm2835_cpufreq_driver_init(struct cpufreq_policy *policy)\r
++{\r
++      /* measured value of how long it takes to change frequency */   \r
++      policy->cpuinfo.transition_latency = 355000; /* ns */\r
++\r
++      /* now find out what the maximum and minimum frequencies are */\r
++      policy->min = policy->cpuinfo.min_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MIN_CLOCK);\r
++      policy->max = policy->cpuinfo.max_freq = bcm2835_cpufreq_get_clock(VCMSG_GET_MAX_CLOCK);\r
++      policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);\r
++      \r
++      print_info("min=%d max=%d cur=%d\n", policy->min, policy->max, policy->cur);\r
++      return 0;\r
++}\r
++\r
++/*\r
++ =================================================================================\r
++  Target function chooses the most appropriate frequency from the table to enable\r
++ =================================================================================\r
++*/\r
++\r
++static int bcm2835_cpufreq_driver_target(struct cpufreq_policy *policy, unsigned int target_freq, unsigned int relation)\r
++{\r
++      unsigned int target = target_freq;\r
++      unsigned int cur = policy->cur;\r
++      print_debug("%s: min=%d max=%d cur=%d target=%d\n",policy->governor->name,policy->min,policy->max,policy->cur,target_freq);\r
++      \r
++      /* if we are above min and using ondemand, then just use max */\r
++      if (strcmp("ondemand", policy->governor->name)==0 && target > policy->min)\r
++              target = policy->max;\r
++      /* if the frequency is the same, just quit */\r
++      if (target == policy->cur)\r
++              return 0;\r
++\r
++      /* otherwise were good to set the clock frequency */\r
++      policy->cur = bcm2835_cpufreq_set_clock(policy->cur, target);\r
++      \r
++      if (!policy->cur)\r
++      {\r
++              print_err("Error occurred setting a new frequency (%d)!\n", target);\r
++              policy->cur = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);\r
++              return -EINVAL;\r
++      }\r
++      print_debug("Freq %d->%d (min=%d max=%d target=%d request=%d)\n", cur, policy->cur, policy->min, policy->max, target_freq, target);\r
++      return 0;\r
++}\r
++\r
++static unsigned int bcm2835_cpufreq_driver_get(unsigned int cpu)\r
++{\r
++      unsigned int actual_rate = bcm2835_cpufreq_get_clock(VCMSG_GET_CLOCK_RATE);\r
++      print_debug("cpu=%d\n", actual_rate);\r
++      return actual_rate;\r
++}\r
++\r
++/*\r
++ =================================================================================\r
++  Verify ensures that when a policy is changed, it is suitable for the CPU to use\r
++ =================================================================================\r
++*/\r
++\r
++static int bcm2835_cpufreq_driver_verify(struct cpufreq_policy *policy)\r
++{\r
++      print_info("switching to governor %s\n", policy->governor->name);\r
++      return 0;\r
++}\r
++\r
++\r
++/* the CPUFreq driver */\r
++static struct cpufreq_driver bcm2835_cpufreq_driver = {\r
++              .name   = "BCM2835 CPUFreq",\r
++              .owner  = THIS_MODULE,\r
++              .init   = bcm2835_cpufreq_driver_init,\r
++              .verify = bcm2835_cpufreq_driver_verify,\r
++              .target = bcm2835_cpufreq_driver_target,\r
++              .get    = bcm2835_cpufreq_driver_get\r
++};\r
++\r
++MODULE_AUTHOR("Dorian Peake and Dom Cobley");\r
++MODULE_DESCRIPTION("CPU frequency driver for BCM2835 chip");\r
++MODULE_LICENSE("GPL");\r
++\r
++module_init(bcm2835_cpufreq_module_init);\r
++module_exit(bcm2835_cpufreq_module_exit);\r
++\r
+diff -urN linux-3.10/drivers/cpufreq/Kconfig.arm linux-rpi-3.10.y/drivers/cpufreq/Kconfig.arm
+--- linux-3.10/drivers/cpufreq/Kconfig.arm     2013-06-30 23:13:29.000000000 +0100
++++ linux-rpi-3.10.y/drivers/cpufreq/Kconfig.arm       2013-07-06 15:25:50.000000000 +0100
+@@ -150,3 +150,11 @@
+       default y
+       help
+         This adds the CPUFreq driver support for SPEAr SOCs.
++
++config ARM_BCM2835_CPUFREQ
++      bool "BCM2835 Driver"
++      default y
++      help
++        This adds the CPUFreq driver for BCM2835
++
++        If in doubt, say N.
+diff -urN linux-3.10/drivers/cpufreq/Makefile linux-rpi-3.10.y/drivers/cpufreq/Makefile
+--- linux-3.10/drivers/cpufreq/Makefile        2013-06-30 23:13:29.000000000 +0100
++++ linux-rpi-3.10.y/drivers/cpufreq/Makefile  2013-07-06 15:25:50.000000000 +0100
+@@ -72,6 +72,7 @@
+ obj-$(CONFIG_ARM_SA1110_CPUFREQ)      += sa1110-cpufreq.o
+ obj-$(CONFIG_ARM_SPEAR_CPUFREQ)               += spear-cpufreq.o
+ obj-$(CONFIG_ARCH_TEGRA)              += tegra-cpufreq.o
++obj-$(CONFIG_ARM_BCM2835_CPUFREQ)     += bcm2835-cpufreq.o
+ ##################################################################################
+ # PowerPC platform drivers