Skip to contentSkip to conversation

mx51 cpufreq driver

PATCH New yong.shen at linaro.org 2 participants 6 messages Oct 5, 2010 – Oct 7, 2010

Subsystems (3)
lore
Share & tools
lore
#1 lore raw

PATCH 1/1 cpufreq for freescale mx51

From: Yong Shen <[email protected]>

it is tested on babbage 3.0

---
 arch/arm/Kconfig                       |    6 +
 arch/arm/mach-mx5/Kconfig              |    1 +
 arch/arm/mach-mx5/Makefile             |    2 +-
 arch/arm/mach-mx5/board-mx51_babbage.c |    7 +-
 arch/arm/mach-mx5/clock-mx51.c         |   53 +++++++
 arch/arm/mach-mx5/cpu_wp-mx51.c        |   43 ++++++
 arch/arm/plat-mxc/Makefile             |    2 +
 arch/arm/plat-mxc/cpufreq.c            |  254 ++++++++++++++++++++++++++++++++
 arch/arm/plat-mxc/include/mach/mxc.h   |   22 +++-
 9 files changed, 387 insertions(+), 3 deletions(-)
 create mode 100644 arch/arm/mach-mx5/cpu_wp-mx51.c
 create mode 100644 arch/arm/plat-mxc/cpufreq.c
arch/arm/Kconfig +6 -0
index 4db064e..64ebbc0 100644
@@ -1517,6 +1517,12 @@ if ARCH_HAS_CPUFREQ
 
 source "drivers/cpufreq/Kconfig"
 
+config CPU_FREQ_IMX
+	tristate "CPUfreq driver for i.MX CPUs"
+	depends on ARCH_MXC && CPU_FREQ
+	help
+	This enables the CPUfreq driver for i.MX CPUs.
+
 config CPU_FREQ_SA1100
 	bool
 
arch/arm/mach-mx5/Kconfig +1 -0
index 1576d51..5956fee 100644
@@ -5,6 +5,7 @@ config ARCH_MX51
 	default y
 	select MXC_TZIC
 	select ARCH_MXC_IOMUX_V3
+	select ARCH_HAS_CPUFREQ
 
 comment "MX5 platforms:"
 
arch/arm/mach-mx5/Makefile +1 -1
index bf23f86..aaa13f8 100644
@@ -3,7 +3,7 @@
 #
 
 # Object file lists.
-obj-y   := cpu.o mm.o clock-mx51.o devices.o
+obj-y   := cpu.o mm.o clock-mx51.o devices.o cpu_wp-mx51.o
 
 obj-$(CONFIG_MACH_MX51_BABBAGE) += board-mx51_babbage.o
 
arch/arm/mach-mx5/board-mx51_babbage.c +6 -1
index ed885f9..038760c 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright 2009 Freescale Semiconductor, Inc. All Rights Reserved.
+ * Copyright 2009-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  * Copyright (C) 2009-2010 Amit Kucheria <[email protected]>
  *
  * The code contained herein is licensed under the GNU General Public
@@ -43,6 +43,10 @@
 #define	MX51_USB_PLL_DIV_19_2_MHZ	0x01
 #define	MX51_USB_PLL_DIV_24_MHZ	0x02
 
+
+extern struct cpu_wp *mx51_get_cpu_wp(int *wp);
+struct cpu_wp *(*get_cpu_wp)(int *wp);
+
 static struct platform_device *devices[] __initdata = {
 	&mxc_fec_device,
 };
@@ -246,6 +250,7 @@ static void __init mxc_board_init(void)
 
 static void __init mx51_babbage_timer_init(void)
 {
+	get_cpu_wp = mx51_get_cpu_wp;
 	mx51_clocks_init(32768, 24000000, 22579200, 0);
 }
 
arch/arm/mach-mx5/clock-mx51.c +53 -0

106 lines not on this page · raw message

arch/arm/mach-mx5/cpu_wp-mx51.c new file +43 -0

43 lines not on this page · raw message

arch/arm/plat-mxc/Makefile +2 -0
index 895bc3c..c1bb400 100644
@@ -17,6 +17,8 @@ obj-$(CONFIG_USB_EHCI_MXC) += ehci.o
 obj-$(CONFIG_MXC_ULPI) += ulpi.o
 obj-$(CONFIG_ARCH_MXC_AUDMUX_V1) += audmux-v1.o
 obj-$(CONFIG_ARCH_MXC_AUDMUX_V2) += audmux-v2.o
+# CPU FREQ support
+obj-$(CONFIG_CPU_FREQ_IMX)    += cpufreq.o
 ifdef CONFIG_SND_IMX_SOC
 obj-y += ssi-fiq.o
 obj-y += ssi-fiq-ksym.o
arch/arm/plat-mxc/cpufreq.c new file +254 -0

254 lines not on this page · raw message

arch/arm/plat-mxc/include/mach/mxc.h +21 -1
index a790bf2..868655b 100644
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
+ * Copyright 2004-2010 Freescale Semiconductor, Inc. All Rights Reserved.
  * Copyright (C) 2008 Juergen Beisert (kernel at pengutronix.de)
  *
  * This program is free software; you can redistribute it and/or
@@ -133,6 +133,26 @@ extern unsigned int __mxc_cpu_type;
 # define cpu_is_mxc91231()	(0)
 #endif
 
+#ifndef __ASSEMBLY__
+
+struct cpu_wp {
+	u32 pll_reg;
+	u32 pll_rate;
+	u32 cpu_rate;
+	u32 pdr0_reg;
+	u32 pdf;
+	u32 mfi;
+	u32 mfd;
+	u32 mfn;
+	u32 cpu_voltage;
+	u32 cpu_podf;
+};
+
+#ifndef CONFIG_ARCH_MX5
+struct cpu_wp *get_cpu_wp(int *wp);
+#endif
+#endif
+
 #if defined(CONFIG_ARCH_MX3) || defined(CONFIG_ARCH_MX2)
 /* These are deprecated, use mx[23][157]_setup_weimcs instead. */
 #define CSCR_U(n) (IO_ADDRESS(WEIM_BASE_ADDR + n * 0x10))
Signature · 1 line
1.6.3.3
#2 lore raw

PATCH 1/1 cpufreq for freescale mx51

Quoted 3 lines

From: Yong Shen <[email protected]>

it is tested on babbage 3.0

One big comment and a couple of smaller ones:

It would be better to make this code a proper device driver, probably a platform_driver unless you have a way to probe the presence of the registers on another bus.

Making it a driver that registers to a bus lets you separate the probing from the implementation, and gives you a structure to add your private variables to.

arch/arm/mach-mx5/board-mx51_babbage.c line 52

Quoted 11 lines
arch/arm/mach-mx5/board-mx51_babbage.c +4 -0
@@ -43,6 +43,10 @@
 #define	MX51_USB_PLL_DIV_19_2_MHZ	0x01
 #define	MX51_USB_PLL_DIV_24_MHZ	0x02
 
+
+extern struct cpu_wp *mx51_get_cpu_wp(int *wp);
+struct cpu_wp *(*get_cpu_wp)(int *wp);
+
 static struct platform_device *devices[] __initdata = {
 	&mxc_fec_device,
 };

Please put the extern declarations into a header file, not in the implementation.

arch/arm/mach-mx5/board-mx51_babbage.c static void __init mxc_board_init(void) · line 255

Quoted 7 lines
arch/arm/mach-mx5/board-mx51_babbage.c +1 -0
@@ -246,6 +250,7 @@ static void __init mxc_board_init(void)
 
 static void __init mx51_babbage_timer_init(void)
 {
+	get_cpu_wp = mx51_get_cpu_wp;
 	mx51_clocks_init(32768, 24000000, 22579200, 0);
 }

It feels like mx51_babbage_timer_init() is not the right place to initialize this. Why not the mxc_board_init function?

arch/arm/mach-mx5/clock-mx51.c static struct clk periph_apm_clk; · line 52

Quoted 4 lines
arch/arm/mach-mx5/clock-mx51.c +1 -0
+#define SPIN_DELAY	1000000 /* in nanoseconds */
 #define MAX_DPLL_WAIT_TRIES	1000 /* 1000 * udelay(1) = 1ms */
 
 static int _clk_ccgr_enable(struct clk *clk)

The SPIN_DELAY variable doesn't seem to be used anywhere.

arch/arm/mach-mx5/clock-mx51.c static int _clk_lp_apm_set_parent(struct clk *clk, struct clk *parent) · line 347

Quoted 11 lines
arch/arm/mach-mx5/clock-mx51.c +7 -0
@@ -330,6 +338,32 @@ static int _clk_lp_apm_set_parent(struct clk *clk, struct clk *parent)
 	return 0;
 }
 
+/*!
+ * Setup cpu clock based on working point.
+ * @param	wp	cpu freq working point
+ * @return		0 on success or error code on failure.
+ */
+int cpu_clk_set_wp(int wp)
+{

could be 'static'.

Quoted 8 lines
arch/arm/mach-mx5/clock-mx51.c +8 -0
+	struct cpu_wp *p;
+	u32 reg;
+
+	if (wp == cpu_curr_wp)
+		return 0;
+
+	p = &cpu_wp_tbl[wp];
+

arch/arm/mach-mx5/clock-mx51.c static int _clk_lp_apm_set_parent(struct clk *clk, struct clk *parent) · line 365

Quoted 10 lines
arch/arm/mach-mx5/clock-mx51.c +10 -0
+	/*use post divider to change freq
+	 */
+	reg = __raw_readl(MXC_CCM_CACRR);
+	reg &= ~MXC_CCM_CACRR_ARM_PODF_MASK;
+	reg |= cpu_wp_tbl[wp].cpu_podf << MXC_CCM_CACRR_ARM_PODF_OFFSET;
+	__raw_writel(reg, MXC_CCM_CACRR);
+	cpu_curr_wp = wp;
+
+	return 0;
+}

This might need a spinlock to protect concurrent register access.

Don't use __raw_readl but readl/ioread32, which have more well-defined behaviour.

arch/arm/plat-mxc/cpufreq.c line 40

Quoted 6 lines
arch/arm/plat-mxc/cpufreq.c new file +6 -0
+int cpu_freq_khz_min;
+int cpu_freq_khz_max;
+int arm_lpm_clk;
+int arm_normal_clk;
+int cpufreq_suspended;
+int cpufreq_trig_needed;

You should not have private variables in the global name space like this. At least mark everything static that is only used in one file. Any global variable must (local variables should) be prefixed with the name of the driver like:

static int mxc_cpufreq_khz_min;
static int mxc_cpufreq_khz_max;
static int mxc_lpm_clk;

This will become more important when we move to multiplatform kernels.

arch/arm/plat-mxc/cpufreq.c line 51

Quoted 7 lines
arch/arm/plat-mxc/cpufreq.c new file +7 -0
+extern int set_low_bus_freq(void);
+extern int set_high_bus_freq(int high_bus_speed);
+extern int low_freq_bus_used(void);
+
+extern struct cpu_wp *(*get_cpu_wp)(int *wp);
+static int cpu_wp_nr;
+static struct cpu_wp *cpu_wp_tbl;

The same rules apply for functions.

Again, anything that can not be static should be declared in a header file.

Quoted 1 line

+MODULE_AUTHOR("Freescale Semiconductor, Inc.");

Please put your name and email address in here.

arch/arm/plat-mxc/include/mach/mxc.h extern unsigned int __mxc_cpu_type; · line 153

Quoted 3 lines
arch/arm/plat-mxc/include/mach/mxc.h +3 -0
+#ifndef CONFIG_ARCH_MX5
+struct cpu_wp *get_cpu_wp(int *wp);
+#endif

Do not enclose declarations in #ifdef. Anybody should be able to just enable your driver anyway without getting conflicts.

Arnd

author
#3 lore raw

PATCH 1/1 cpufreq for freescale mx51

Hi Arnd,

Really appreciate your valuable comments. Most of them are accepted. I have different option about two comments.
1.

Quoted 8 lines

It would be better to make this code a proper device driver, probably a platform_driver unless you have a way to probe the presence of the registers on another bus.

Making it a driver that registers to a bus lets you separate the probing from the implementation, and gives you a structure to add your private variables to.

cpufreq is supposed to be registered using cpufreq_register_driver directly, so no other platform data is needed. You can also find out other cpufreq driver is designed this way, like omap cpufreq driver.
2.

Quoted 3 lines

Don't use __raw_readl but readl/ioread32, which have more well-defined behaviour.

I think __raw_readl is ok here, since in the platform related code, we know the register layout and length, this is more efficient. Also this is extensively used in arch/arm/.

Again, thanks for your time for review. I will send out updated patch.

Yong

Quoted 121 lines · Arnd Bergmann
Quoted 3 lines

From: Yong Shen <[email protected]>

it is tested on babbage 3.0

One big comment and a couple of smaller ones:

It would be better to make this code a proper device driver, probably a platform_driver unless you have a way to probe the presence of the registers on another bus.

Making it a driver that registers to a bus lets you separate the probing from the implementation, and gives you a structure to add your private variables to.

arch/arm/mach-mx5/board-mx51_babbage.c line 52

Quoted 11 lines
arch/arm/mach-mx5/board-mx51_babbage.c +4 -0
@@ -43,6 +43,10 @@
 #define      MX51_USB_PLL_DIV_19_2_MHZ       0x01
 #define      MX51_USB_PLL_DIV_24_MHZ 0x02
 
+
+extern struct cpu_wp *mx51_get_cpu_wp(int *wp);
+struct cpu_wp *(*get_cpu_wp)(int *wp);
+
 static struct platform_device *devices[] __initdata = {
      &mxc_fec_device,
 };

Please put the extern declarations into a header file, not in the implementation.

arch/arm/mach-mx5/board-mx51_babbage.c static void __init mxc_board_init(void) · line 255

Quoted 7 lines
arch/arm/mach-mx5/board-mx51_babbage.c +1 -0
@@ -246,6 +250,7 @@ static void __init mxc_board_init(void)
 
 static void __init mx51_babbage_timer_init(void)
 {
+     get_cpu_wp = mx51_get_cpu_wp;
      mx51_clocks_init(32768, 24000000, 22579200, 0);
 }

It feels like mx51_babbage_timer_init() is not the right place to initialize this. Why not the mxc_board_init function?

arch/arm/mach-mx5/clock-mx51.c static struct clk periph_apm_clk; · line 52

Quoted 4 lines
arch/arm/mach-mx5/clock-mx51.c +1 -0
+#define SPIN_DELAY   1000000 /* in nanoseconds */
 #define MAX_DPLL_WAIT_TRIES  1000 /* 1000 * udelay(1) = 1ms */
 
 static int _clk_ccgr_enable(struct clk *clk)

The SPIN_DELAY variable doesn't seem to be used anywhere.

Review static int _clk_lp_apm_set_parent(struct clk *clk,

Quoted 1 line
(quoted diff) +0 -0
@@ -330,6 +338,32 @@ static int _clk_lp_apm_set_parent(struct clk *clk,

struct clk *parent)

arch/arm/mach-mx5/clock-mx51.c static int _clk_lp_apm_set_parent(struct clk *clk, struct clk *parent) · line 347

Quoted 10 lines
arch/arm/mach-mx5/clock-mx51.c +7 -0
      return 0;
 }
 
+/*!
+ * Setup cpu clock based on working point.
+ * @param    wp      cpu freq working point
+ * @return           0 on success or error code on failure.
+ */
+int cpu_clk_set_wp(int wp)
+{

could be 'static'.

Quoted 8 lines
arch/arm/mach-mx5/clock-mx51.c +8 -0
+     struct cpu_wp *p;
+     u32 reg;
+
+     if (wp == cpu_curr_wp)
+             return 0;
+
+     p = &cpu_wp_tbl[wp];
+

arch/arm/mach-mx5/clock-mx51.c static int _clk_lp_apm_set_parent(struct clk *clk, struct clk *parent) · line 365

Quoted 10 lines
arch/arm/mach-mx5/clock-mx51.c +10 -0
+     /*use post divider to change freq
+      */
+     reg = __raw_readl(MXC_CCM_CACRR);
+     reg &= ~MXC_CCM_CACRR_ARM_PODF_MASK;
+     reg |= cpu_wp_tbl[wp].cpu_podf << MXC_CCM_CACRR_ARM_PODF_OFFSET;
+     __raw_writel(reg, MXC_CCM_CACRR);
+     cpu_curr_wp = wp;
+
+     return 0;
+}

This might need a spinlock to protect concurrent register access.

Don't use __raw_readl but readl/ioread32, which have more well-defined behaviour.

arch/arm/plat-mxc/cpufreq.c line 40

Quoted 6 lines
arch/arm/plat-mxc/cpufreq.c new file +6 -0
+int cpu_freq_khz_min;
+int cpu_freq_khz_max;
+int arm_lpm_clk;
+int arm_normal_clk;
+int cpufreq_suspended;
+int cpufreq_trig_needed;

You should not have private variables in the global name space like this. At least mark everything static that is only used in one file. Any global variable must (local variables should) be prefixed with the name of the driver like:

static int mxc_cpufreq_khz_min;
static int mxc_cpufreq_khz_max;
static int mxc_lpm_clk;

This will become more important when we move to multiplatform kernels.

arch/arm/plat-mxc/cpufreq.c line 51

Quoted 7 lines
arch/arm/plat-mxc/cpufreq.c new file +7 -0
+extern int set_low_bus_freq(void);
+extern int set_high_bus_freq(int high_bus_speed);
+extern int low_freq_bus_used(void);
+
+extern struct cpu_wp *(*get_cpu_wp)(int *wp);
+static int cpu_wp_nr;
+static struct cpu_wp *cpu_wp_tbl;

The same rules apply for functions.

Again, anything that can not be static should be declared in a header file.

Quoted 1 line

+MODULE_AUTHOR("Freescale Semiconductor, Inc.");

Please put your name and email address in here.

Quoted 9 lines

arch/arm/plat-mxc/include/mach/mxc.h extern unsigned int __mxc_cpu_type; · line 153

Quoted 3 lines
arch/arm/plat-mxc/include/mach/mxc.h +3 -0
+#ifndef CONFIG_ARCH_MX5
+struct cpu_wp *get_cpu_wp(int *wp);
+#endif

Do not enclose declarations in #ifdef. Anybody should be able to just enable your driver anyway without getting conflicts.

Arnd

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20101006/0b5618c5/attachment-0001.html>

#4 lore raw

PATCH 1/1 cpufreq for freescale mx51

Quoted 10 lines · Yong Shen

Hi Arnd,

Really appreciate your valuable comments. Most of them are accepted. I have different option about two comments.
1.

Quoted 4 lines

It would be better to make this code a proper device driver, probably a platform_driver unless you have a way to probe the presence of the registers on another bus.

Quoted 7 lines
Quoted 4 lines

Making it a driver that registers to a bus lets you separate the probing from the implementation, and gives you a structure to add your private variables to.

cpufreq is supposed to be registered using cpufreq_register_driver directly, so no other platform data is needed. You can also find out other cpufreq driver is designed this way, like omap cpufreq driver.

Ok, it is indeed different from what I thought.

My thought was that you have resources that need to be attached to a device which then can get matched to a device_driver.

However, this is not how it works with the generic clock framework. The device that you are controlling is not a random platform device but the actual CPU, which has a node in the device tree already (/sys/devices/system/cpu/*) and that gets controlled by the generic cpufreq layer, while the resources are tied to the struct clk that you are controlling.

So if anything, the clk is what needs to be a platform device, not an artificial cpufreq device. You are only adding another clock to the clock-mx51.c file and you are consistent with the existing clocks in there, so I'm not asking you to change anything here.

I wonder however if we can create a common cpufreq driver that would work for all platforms that just need to call clk_set_rate in the end and can operate from platform specific tables otherwise. About half the cpufreq drivers in arm seem to be a variation of this already, so we might be able to make the clock framework good enough for this.

Quoted 8 lines

2.

Quoted 3 lines

Don't use __raw_readl but readl/ioread32, which have more well-defined behaviour.

I think __raw_readl is ok here, since in the platform related code, we know the register layout and length, this is more efficient. Also this is extensively used in arch/arm/.

I still disagree, but it's not important. IMHO most of the uses of __raw_readl should be converted to readl or readl_relaxed if you are worried about efficiency.

The main difference between __raw_readl and readl_relaxed is that the endianess is well-defined on readl_relaxed.

Arnd

author
#5 lore raw

PATCH 1/1 cpufreq for freescale mx51

Quoted 9 lines

I still disagree, but it's not important. IMHO most of the uses of __raw_readl should be converted to readl or readl_relaxed if you are worried about efficiency.

The main difference between __raw_readl and readl_relaxed is that the endianess is well-defined on readl_relaxed.

Arnd

I agree with you about that. I will keep that in mind and also notice my
colleages this in our future development.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20101007/133e8673/attachment-0001.html>