Skip to contentSkip to conversation

mpc5200: eliminate direct manipulation of shared registers from SPI driver

RFC Superseded Grant Likely 3 participants 5 messages Jan 7, 2008 – Jan 10, 2008

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover mpc5200: eliminate direct manipulation of shared registers from SPI driver

The mpc5200 PSC SPI driver driver directly manipulates the port_config and the CDM registers on the mpc5200 which it should not do. port_config should only be manipulated from within the board specific platform code and the CDM registers are shared between multiple devices.

This patch eliminates the port_config manipulations and adds a common routine for adjusting CDM settings. Boards using the SPI driver will need to add the required port_config changes to either the boot firmware or the platform code.

If there are no objections, I plan to ask Paulus to merge these in the next couple of days.

Cheers,
g.

--
Grant Likely, B.Sc. P.Eng.
Secret Lab Technologies Ltd.

#1 lore raw

RFC 1/2 mpc5200: Add common clock setting routine mpc52xx_set_psc_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 |   91 +++++++++++++++++++++-----
 arch/ppc/syslib/mpc52xx_setup.c              |   36 ++++++++++
 include/asm-powerpc/mpc52xx.h                |   13 ++--
 6 files changed, 130 insertions(+), 29 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 +75 -16

131 lines not on this page · raw message

arch/ppc/syslib/mpc52xx_setup.c +36 -0
index ecfa2c0..791fc8d 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 +8 -5
index 1c48c6d..c7a0710 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 __init mpc5200_setup_xlb_arbiter(void);
+extern void __init mpc52xx_declare_of_platform_devices(void);
+extern void __init 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
#2 lore raw

Re: [RFC 1/2] mpc5200: Add common clock setting routine mpc52xx_set_psc_clkdiv()

Quoted 4 lines · On Mon, 07 Jan 2008 12:03:59 -0700 Grant Likely <[email protected]>…

+++ b/arch/powerpc/platforms/52xx/mpc52xx_common.c

-static struct of_device_id __init mpc52xx_ids[] = {

Why not just leave this here and mark in __initdata?

arch/powerpc/platforms/52xx/mpc52xx_common.c mpc5200_setup_xlb_arbiter(void) · line 107

Quoted 9 lines
arch/powerpc/platforms/52xx/mpc52xx_common.c +6 -0
+/**
+ * mpc52xx_declare_of_platform_devices: register internal devices and children
+ *					of the localplus bus to the of_platform
+ *					bus.
+ */
 void __init
 mpc52xx_declare_of_platform_devices(void)
 {
+	const static struct of_device_id mpc52xx_bus_ids[] = {

Or just mark it __initdata instead of const?

arch/powerpc/platforms/52xx/mpc52xx_common.c mpc5200_setup_xlb_arbiter(void) · line 111

Quoted 4 lines
arch/powerpc/platforms/52xx/mpc52xx_common.c +4 -0
+		{ .compatible = "fsl,mpc5200-immr", },
+		{ .compatible = "fsl,lpb", },
+		{ .type = "builtin", .compatible = "mpc5200", }, /* efika */
+		{ .type = "soc", .compatible = "mpc5200", }, /* old */

And you lost the comment about the last two being deprecated mates.

arch/powerpc/platforms/52xx/mpc52xx_common.c mpc5200_setup_xlb_arbiter(void) · line 128

Quoted 5 lines
arch/powerpc/platforms/52xx/mpc52xx_common.c +2 -1
+mpc52xx_map_common_devices(void)
 {
 	struct device_node *np;
-	struct of_device_id gpt_ids[] = {
+	const static struct of_device_id gpt_ids[] = {

Again __initdata?

arch/powerpc/platforms/52xx/mpc52xx_common.c mpc5200_setup_xlb_arbiter(void) · line 133

Quoted 1 line
arch/powerpc/platforms/52xx/mpc52xx_common.c +1 -0
+	const static struct of_device_id cdm_ids[] = {

And again?

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

RFC 2/2 mpc52xx_psc_spi device driver must not touch port_config and cdm

In reply to Grant Likely

From: Grant Likely <[email protected]>

It is dangerous for an mpc52xx device driver to modify the port_config register. If the driver is probed incorrectly, it will change the pin IO configuration in ways which may not be compatible with the board. port_config should be set up by the bootloader, or failing that, in the platform setup code in arch/powerpc/platforms/52xx.

Also, modifying CDM registers directly can cause a race condition with other drivers. Instead call a common routine to modify CDM settings.

---

 drivers/spi/mpc52xx_psc_spi.c |   77 +----------------------------------------
 1 files changed, 2 insertions(+), 75 deletions(-)
drivers/spi/mpc52xx_psc_spi.c +2 -75
index a3ebc63..253ed56 100644
@@ -330,80 +330,13 @@ static void mpc52xx_psc_spi_cleanup(struct spi_device *spi)
 
 static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
 {
-	struct device_node *np;
-	struct mpc52xx_cdm __iomem *cdm;
-	struct mpc52xx_gpio __iomem *gpio;
 	struct mpc52xx_psc __iomem *psc = mps->psc;
-	u32 ul;
 	u32 mclken_div;
 	int ret = 0;
 
-#if defined(CONFIG_PPC_MERGE)
-	np = of_find_compatible_node(NULL, NULL, "mpc5200-cdm");
-	cdm = of_iomap(np, 0);
-	of_node_put(np);
-	np = of_find_compatible_node(NULL, NULL, "mpc5200-gpio");
-	gpio = of_iomap(np, 0);
-	of_node_put(np);
-#else
-	cdm = ioremap(MPC52xx_PA(MPC52xx_CDM_OFFSET), MPC52xx_CDM_SIZE);
-	gpio = ioremap(MPC52xx_PA(MPC52xx_GPIO_OFFSET), MPC52xx_GPIO_SIZE);
-#endif
-	if (!cdm || !gpio) {
-		printk(KERN_ERR "Error mapping CDM/GPIO\n");
-		ret = -EFAULT;
-		goto unmap_regs;
-	}
-
 	/* default sysclk is 512MHz */
-	mclken_div = 0x8000 |
-		(((mps->sysclk ? mps->sysclk : 512000000) / MCLK) & 0x1FF);
-
-	switch (psc_id) {
-	case 1:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFFFFFFF8;
-		ul |= 0x00000006;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc1, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000020;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	case 2:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFFFFFF8F;
-		ul |= 0x00000060;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc2, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000040;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	case 3:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFFFFF0FF;
-		ul |= 0x00000600;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc3, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000080;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	case 6:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFF8FFFFF;
-		ul |= 0x00700000;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc6, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000010;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	default:
-		ret = -EINVAL;
-		goto unmap_regs;
-	}
+	mclken_div = (mps->sysclk ? mps->sysclk : 512000000) / MCLK;
+	mpc52xx_set_psc_clkdiv(psc_id, mclken_div);
 
 	/* Reset the PSC into a known state */
 	out_8(&psc->command, MPC52xx_PSC_RST_RX);
@@ -427,12 +360,6 @@ static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
 
 	mps->bits_per_word = 8;
 
-unmap_regs:
-	if (cdm)
-		iounmap(cdm);
-	if (gpio)
-		iounmap(gpio);
-
 	return ret;
 }
 
Acked-by on v1 2/2
#4 lore raw

Re: [RFC 2/2] mpc52xx_psc_spi device driver must not touch port_config and cdm

Quoted 119 lines · Grant Likely

From: Grant Likely <[email protected]>

It is dangerous for an mpc52xx device driver to modify the port_config register. If the driver is probed incorrectly, it will change the pin IO configuration in ways which may not be compatible with the board. port_config should be set up by the bootloader, or failing that, in the platform setup code in arch/powerpc/platforms/52xx.

Also, modifying CDM registers directly can cause a race condition with other drivers. Instead call a common routine to modify CDM settings.

---

 drivers/spi/mpc52xx_psc_spi.c |   77 +----------------------------------------
 1 files changed, 2 insertions(+), 75 deletions(-)
drivers/spi/mpc52xx_psc_spi.c +2 -75
index a3ebc63..253ed56 100644
@@ -330,80 +330,13 @@ static void mpc52xx_psc_spi_cleanup(struct spi_device *spi)
 
 static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
 {
-	struct device_node *np;
-	struct mpc52xx_cdm __iomem *cdm;
-	struct mpc52xx_gpio __iomem *gpio;
 	struct mpc52xx_psc __iomem *psc = mps->psc;
-	u32 ul;
 	u32 mclken_div;
 	int ret = 0;
 
-#if defined(CONFIG_PPC_MERGE)
-	np = of_find_compatible_node(NULL, NULL, "mpc5200-cdm");
-	cdm = of_iomap(np, 0);
-	of_node_put(np);
-	np = of_find_compatible_node(NULL, NULL, "mpc5200-gpio");
-	gpio = of_iomap(np, 0);
-	of_node_put(np);
-#else
-	cdm = ioremap(MPC52xx_PA(MPC52xx_CDM_OFFSET), MPC52xx_CDM_SIZE);
-	gpio = ioremap(MPC52xx_PA(MPC52xx_GPIO_OFFSET), MPC52xx_GPIO_SIZE);
-#endif
-	if (!cdm || !gpio) {
-		printk(KERN_ERR "Error mapping CDM/GPIO\n");
-		ret = -EFAULT;
-		goto unmap_regs;
-	}
-
 	/* default sysclk is 512MHz */
-	mclken_div = 0x8000 |
-		(((mps->sysclk ? mps->sysclk : 512000000) / MCLK) & 0x1FF);
-
-	switch (psc_id) {
-	case 1:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFFFFFFF8;
-		ul |= 0x00000006;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc1, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000020;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	case 2:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFFFFFF8F;
-		ul |= 0x00000060;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc2, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000040;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	case 3:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFFFFF0FF;
-		ul |= 0x00000600;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc3, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000080;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	case 6:
-		ul = in_be32(&gpio->port_config);
-		ul &= 0xFF8FFFFF;
-		ul |= 0x00700000;
-		out_be32(&gpio->port_config, ul);
-		out_be16(&cdm->mclken_div_psc6, mclken_div);
-		ul = in_be32(&cdm->clk_enables);
-		ul |= 0x00000010;
-		out_be32(&cdm->clk_enables, ul);
-		break;
-	default:
-		ret = -EINVAL;
-		goto unmap_regs;
-	}
+	mclken_div = (mps->sysclk ? mps->sysclk : 512000000) / MCLK;
+	mpc52xx_set_psc_clkdiv(psc_id, mclken_div);
 
 	/* Reset the PSC into a known state */
 	out_8(&psc->command, MPC52xx_PSC_RST_RX);
@@ -427,12 +360,6 @@ static int mpc52xx_psc_spi_port_config(int psc_id, struct mpc52xx_psc_spi *mps)
 
 	mps->bits_per_word = 8;
 
-unmap_regs:
-	if (cdm)
-		iounmap(cdm);
-	if (gpio)
-		iounmap(gpio);
-
 	return ret;
 }