Skip to contentSkip to conversation

Add common clock setting routine mpc52xx_psc_set_clkdiv()

PATCH In review Grant Likely 2 participants 4 messages Jan 10, 2008

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 Add common clock setting routine mpc52xx_psc_set_clkdiv()

From: Grant Likely <[email protected]>

PSC drivers should not access the CDM registers directly. Instead provide a common routine for setting the PSC clock parameters with the required locking.

---

 arch/powerpc/platforms/52xx/efika.c          |    3 +
 arch/powerpc/platforms/52xx/lite5200.c       |   10 ++--
 arch/powerpc/platforms/52xx/mpc5200_simple.c |    6 +-
 arch/powerpc/platforms/52xx/mpc52xx_common.c |   65 ++++++++++++++++++++++++--
 arch/ppc/syslib/mpc52xx_setup.c              |   36 ++++++++++++++
 include/asm-powerpc/mpc52xx.h                |    9 ++--
 6 files changed, 114 insertions(+), 15 deletions(-)
arch/powerpc/platforms/52xx/efika.c +3 -0
index a0da70c..a2068fa 100644
@@ -180,6 +180,9 @@ static void __init efika_setup_arch(void)
 {
 	rtas_initialize();
 
+	/* Map important registers from the internal memory map */
+	mpc52xx_map_common_devices();
+
 	efika_pcisetup();
 
 #ifdef CONFIG_PM
arch/powerpc/platforms/52xx/lite5200.c +5 -5
index f8ba5f2..42e87b6 100644
@@ -150,15 +150,15 @@ static void __init lite5200_setup_arch(void)
 	if (ppc_md.progress)
 		ppc_md.progress("lite5200_setup_arch()", 0);
 
-	/* Fix things that firmware should have done. */
-	lite5200_fix_clock_config();
-	lite5200_fix_port_config();
+	/* Map important registers from the internal memory map */
+	mpc52xx_map_common_devices();
 
 	/* Some mpc5200 & mpc5200b related configuration */
 	mpc5200_setup_xlb_arbiter();
 
-	/* Map wdt for mpc52xx_restart() */
-	mpc52xx_map_wdt();
+	/* Fix things that firmware should have done. */
+	lite5200_fix_clock_config();
+	lite5200_fix_port_config();
 
 #ifdef CONFIG_PM
 	mpc52xx_suspend.board_suspend_prepare = lite5200_suspend_prepare;
arch/powerpc/platforms/52xx/mpc5200_simple.c +3 -3
index 754aa93..c48b82b 100644
@@ -39,12 +39,12 @@ static void __init mpc5200_simple_setup_arch(void)
 	if (ppc_md.progress)
 		ppc_md.progress("mpc5200_simple_setup_arch()", 0);
 
+	/* Map important registers from the internal memory map */
+	mpc52xx_map_common_devices();
+
 	/* Some mpc5200 & mpc5200b related configuration */
 	mpc5200_setup_xlb_arbiter();
 
-	/* Map wdt for mpc52xx_restart() */
-	mpc52xx_map_wdt();
-
 	mpc52xx_setup_pci();
 }
 
arch/powerpc/platforms/52xx/mpc52xx_common.c +61 -4

103 lines not on this page · raw message

arch/ppc/syslib/mpc52xx_setup.c +36 -0
index ecfa2c0..9f504fc 100644
@@ -16,6 +16,7 @@
  */
 
 
+#include <linux/spinlock.h>
 #include <asm/io.h>
 #include <asm/time.h>
 #include <asm/mpc52xx.h>
@@ -275,3 +276,38 @@ int mpc52xx_match_psc_function(int psc_idx, const char *func)
 
 	return 0;
 }
+
+int mpc52xx_set_psc_clkdiv(int psc_id, int clkdiv)
+{
+	static spinlock_t lock = SPIN_LOCK_UNLOCKED;
+	struct mpc52xx_cdm __iomem *cdm;
+	unsigned long flags;
+	u16 mclken_div;
+	u16 __iomem *reg;
+	u32 mask;
+
+	cdm = ioremap(MPC52xx_PA(MPC52xx_CDM_OFFSET), MPC52xx_CDM_SIZE);
+	if (!cdm) {
+		printk(KERN_ERR __FILE__ ": Error mapping CDM\n");
+		return -ENODEV;
+	}
+
+	mclken_div = 0x8000 | (clkdiv & 0x1FF);
+	switch (psc_id) {
+	case 1: reg = &cdm->mclken_div_psc1; mask = 0x20; break;
+	case 2: reg = &cdm->mclken_div_psc2; mask = 0x40; break;
+	case 3: reg = &cdm->mclken_div_psc3; mask = 0x80; break;
+	case 6: reg = &cdm->mclken_div_psc6; mask = 0x10; break;
+	default:
+		return -ENODEV;
+	}
+
+	/* Set the rate and enable the clock */
+	spin_lock_irqsave(&lock, flags);
+	out_be16(reg, mclken_div);
+	out_be32(&cdm->clk_enables, in_be32(&cdm->clk_enables) | mask);
+	spin_unlock_irqrestore(&lock, flags);
+
+	iounmap(cdm);
+	return 0;
+}
include/asm-powerpc/mpc52xx.h +6 -3
index 1c48c6d..81ef10b 100644
@@ -248,13 +248,19 @@ struct mpc52xx_cdm {
 
 #ifndef __ASSEMBLY__
 
+/* mpc52xx_common.c */
 extern unsigned int mpc52xx_find_ipb_freq(struct device_node *node);
 extern void mpc5200_setup_xlb_arbiter(void);
 extern void mpc52xx_declare_of_platform_devices(void);
+extern void mpc52xx_map_common_devices(void);
+extern int mpc52xx_set_psc_clkdiv(int psc_id, int clkdiv);
+extern void mpc52xx_restart(char *cmd);
 
+/* mpc52xx_pic.c */
 extern void mpc52xx_init_irq(void);
 extern unsigned int mpc52xx_get_irq(void);
 
+/* mpc52xx_pci.c */
 #ifdef CONFIG_PCI
 extern int __init mpc52xx_add_bridge(struct device_node *node);
 extern void __init mpc52xx_setup_pci(void);
@@ -262,9 +268,6 @@ extern void __init mpc52xx_setup_pci(void);
 static inline void mpc52xx_setup_pci(void) { }
 #endif
 
-extern void __init mpc52xx_map_wdt(void);
-extern void mpc52xx_restart(char *cmd);
-
 #endif /* __ASSEMBLY__ */
 
 #ifdef CONFIG_PM
#1 lore raw

Hi Grant,

Review

Quoted 6 lines · On Wed, 09 Jan 2008 22:26:30 -0700 Grant Likely <[email protected]>…
(quoted diff) +3 -0
+mpc52xx_map_common_devices(void)
 {
 
+	/* Clock Distribution Module, used by PSC clock setting function */
+	np = of_find_matching_node(NULL, mpc52xx_cdm_ids);

What happens if we find no node?

Quoted 3 lines
arch/powerpc/platforms/52xx/mpc52xx_common.c +2 -0
+	mpc52xx_cdm = of_iomap(np, 0);
+	of_node_put(np);
 }
Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/
#2 lore raw
Quoted 11 lines · Stephen Rothwell

Hi Grant,

Review

Quoted 6 lines · On Wed, 09 Jan 2008 22:26:30 -0700 Grant Likely <[email protected]>…
(quoted diff) +3 -0
+mpc52xx_map_common_devices(void)
 {
 
+     /* Clock Distribution Module, used by PSC clock setting function */
+     np = of_find_matching_node(NULL, mpc52xx_cdm_ids);

What happens if we find no node?

Both of_iomap and of_put fail gracefully if np is NULL. IIRC that was done on purpose so this exact thing could be done.

Users of mpc52xx_cdm (and the others done in this style) all check for mpc52xx_cdm being NULL before dereferencing.

Quoted 11 lines

arch/powerpc/platforms/52xx/mpc52xx_common.c mpc52xx_map_wdt(void) · line 165

Quoted 4 lines
arch/powerpc/platforms/52xx/mpc52xx_common.c +2 -0
+     mpc52xx_cdm = of_iomap(np, 0);
+     of_node_put(np);
 }

--
Cheers,
Stephen Rothwell [email protected]
http://www.canb.auug.org.au/~sfr/

Signature · 2 lines
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.
#3 lore raw
Quoted 12 lines · On Thu, 10 Jan 2008 08:01:09 -0700 "Grant Likely" <[email protected]>…
Quoted 10 lines · Stephen Rothwell

Hi Grant,

Quoted 6 lines · On Wed, 09 Jan 2008 22:26:30 -0700 Grant Likely <[email protected]>…
(quoted diff) +3 -0
+mpc52xx_map_common_devices(void)
 {
 
+     /* Clock Distribution Module, used by PSC clock setting function */
+     np = of_find_matching_node(NULL, mpc52xx_cdm_ids);
Quoted 7 lines
Quoted 1 line

What happens if we find no node?

Both of_iomap and of_put fail gracefully if np is NULL. IIRC that was done on purpose so this exact thing could be done.

Users of mpc52xx_cdm (and the others done in this style) all check for mpc52xx_cdm being NULL before dereferencing.

Excellent, thanks.

Signature · 3 lines
Cheers,
Stephen Rothwell                    [email protected]
http://www.canb.auug.org.au/~sfr/