Commit 45c7f75f authored by Russell King's avatar Russell King

MFD: mcp-sa11x0: convert mcp-sa11x0 to use platform resources

Patch taken from af9081ae (ARM: sa1100: Refactor mcp-sa11x0 to use
platform resources.) by Jochen Friedrich <jochen@scram.de>, and fixes
applied.

We can safely do this now that we have sanitized host removal; the
original patch had use-after-free bugs in the removal code.  Not only
that, but there was no checking of the ioremap() return.

The final change over Jochen's patch is that we wrap the base pointer
selection inside the various register indexes, which reduces the
possibility of the wrong register index being used.
Acked-by: default avatarJochen Friedrich <jochen@scram.de>
Signed-off-by: default avatarRussell King <rmk+kernel@arm.linux.org.uk>
parent c4592ce4
...@@ -221,6 +221,11 @@ static struct resource sa11x0mcp_resources[] = { ...@@ -221,6 +221,11 @@ static struct resource sa11x0mcp_resources[] = {
.flags = IORESOURCE_MEM, .flags = IORESOURCE_MEM,
}, },
[1] = { [1] = {
.start = __PREG(Ser4MCCR1),
.end = __PREG(Ser4MCCR1) + 4 - 1,
.flags = IORESOURCE_MEM,
},
[2] = {
.start = IRQ_Ser4MCP, .start = IRQ_Ser4MCP,
.end = IRQ_Ser4MCP, .end = IRQ_Ser4MCP,
.flags = IORESOURCE_IRQ, .flags = IORESOURCE_IRQ,
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
*/ */
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/io.h>
#include <linux/errno.h> #include <linux/errno.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/delay.h> #include <linux/delay.h>
...@@ -30,34 +31,44 @@ ...@@ -30,34 +31,44 @@
#define DRIVER_NAME "sa11x0-mcp" #define DRIVER_NAME "sa11x0-mcp"
struct mcp_sa11x0 { struct mcp_sa11x0 {
u32 mccr0; void __iomem *base0;
u32 mccr1; void __iomem *base1;
u32 mccr0;
u32 mccr1;
}; };
/* Register offsets */
#define MCCR0(m) ((m)->base0 + 0x00)
#define MCDR0(m) ((m)->base0 + 0x08)
#define MCDR1(m) ((m)->base0 + 0x0c)
#define MCDR2(m) ((m)->base0 + 0x10)
#define MCSR(m) ((m)->base0 + 0x18)
#define MCCR1(m) ((m)->base1 + 0x00)
#define priv(mcp) ((struct mcp_sa11x0 *)mcp_priv(mcp)) #define priv(mcp) ((struct mcp_sa11x0 *)mcp_priv(mcp))
static void static void
mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor) mcp_sa11x0_set_telecom_divisor(struct mcp *mcp, unsigned int divisor)
{ {
unsigned int mccr0; struct mcp_sa11x0 *m = priv(mcp);
divisor /= 32; divisor /= 32;
mccr0 = Ser4MCCR0 & ~0x00007f00; m->mccr0 &= ~0x00007f00;
mccr0 |= divisor << 8; m->mccr0 |= divisor << 8;
Ser4MCCR0 = mccr0; writel_relaxed(m->mccr0, MCCR0(m));
} }
static void static void
mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor) mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
{ {
unsigned int mccr0; struct mcp_sa11x0 *m = priv(mcp);
divisor /= 32; divisor /= 32;
mccr0 = Ser4MCCR0 & ~0x0000007f; m->mccr0 &= ~0x0000007f;
mccr0 |= divisor; m->mccr0 |= divisor;
Ser4MCCR0 = mccr0; writel_relaxed(m->mccr0, MCCR0(m));
} }
/* /*
...@@ -69,14 +80,15 @@ mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor) ...@@ -69,14 +80,15 @@ mcp_sa11x0_set_audio_divisor(struct mcp *mcp, unsigned int divisor)
static void static void
mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val) mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
{ {
struct mcp_sa11x0 *m = priv(mcp);
int ret = -ETIME; int ret = -ETIME;
int i; int i;
Ser4MCDR2 = reg << 17 | MCDR2_Wr | (val & 0xffff); writel_relaxed(reg << 17 | MCDR2_Wr | (val & 0xffff), MCDR2(m));
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
udelay(mcp->rw_timeout); udelay(mcp->rw_timeout);
if (Ser4MCSR & MCSR_CWC) { if (readl_relaxed(MCSR(m)) & MCSR_CWC) {
ret = 0; ret = 0;
break; break;
} }
...@@ -95,15 +107,16 @@ mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val) ...@@ -95,15 +107,16 @@ mcp_sa11x0_write(struct mcp *mcp, unsigned int reg, unsigned int val)
static unsigned int static unsigned int
mcp_sa11x0_read(struct mcp *mcp, unsigned int reg) mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
{ {
struct mcp_sa11x0 *m = priv(mcp);
int ret = -ETIME; int ret = -ETIME;
int i; int i;
Ser4MCDR2 = reg << 17 | MCDR2_Rd; writel_relaxed(reg << 17 | MCDR2_Rd, MCDR2(m));
for (i = 0; i < 2; i++) { for (i = 0; i < 2; i++) {
udelay(mcp->rw_timeout); udelay(mcp->rw_timeout);
if (Ser4MCSR & MCSR_CRC) { if (readl_relaxed(MCSR(m)) & MCSR_CRC) {
ret = Ser4MCDR2 & 0xffff; ret = readl_relaxed(MCDR2(m)) & 0xffff;
break; break;
} }
} }
...@@ -116,13 +129,19 @@ mcp_sa11x0_read(struct mcp *mcp, unsigned int reg) ...@@ -116,13 +129,19 @@ mcp_sa11x0_read(struct mcp *mcp, unsigned int reg)
static void mcp_sa11x0_enable(struct mcp *mcp) static void mcp_sa11x0_enable(struct mcp *mcp)
{ {
Ser4MCSR = -1; struct mcp_sa11x0 *m = priv(mcp);
Ser4MCCR0 |= MCCR0_MCE;
writel(-1, MCSR(m));
m->mccr0 |= MCCR0_MCE;
writel_relaxed(m->mccr0, MCCR0(m));
} }
static void mcp_sa11x0_disable(struct mcp *mcp) static void mcp_sa11x0_disable(struct mcp *mcp)
{ {
Ser4MCCR0 &= ~MCCR0_MCE; struct mcp_sa11x0 *m = priv(mcp);
m->mccr0 &= ~MCCR0_MCE;
writel_relaxed(m->mccr0, MCCR0(m));
} }
/* /*
...@@ -137,22 +156,38 @@ static struct mcp_ops mcp_sa11x0 = { ...@@ -137,22 +156,38 @@ static struct mcp_ops mcp_sa11x0 = {
.disable = mcp_sa11x0_disable, .disable = mcp_sa11x0_disable,
}; };
static int mcp_sa11x0_probe(struct platform_device *pdev) static int mcp_sa11x0_probe(struct platform_device *dev)
{ {
struct mcp_plat_data *data = pdev->dev.platform_data; struct mcp_plat_data *data = dev->dev.platform_data;
struct resource *mem0, *mem1;
struct mcp_sa11x0 *m;
struct mcp *mcp; struct mcp *mcp;
int ret; int ret;
if (!data) if (!data)
return -ENODEV; return -ENODEV;
if (!request_mem_region(0x80060000, 0x60, "sa11x0-mcp")) mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
return -EBUSY; mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
if (!mem0 || !mem1)
return -ENXIO;
if (!request_mem_region(mem0->start, resource_size(mem0),
DRIVER_NAME)) {
ret = -EBUSY;
goto err_mem0;
}
mcp = mcp_host_alloc(&pdev->dev, sizeof(struct mcp_sa11x0)); if (!request_mem_region(mem1->start, resource_size(mem1),
DRIVER_NAME)) {
ret = -EBUSY;
goto err_mem1;
}
mcp = mcp_host_alloc(&dev->dev, sizeof(struct mcp_sa11x0));
if (!mcp) { if (!mcp) {
ret = -ENOMEM; ret = -ENOMEM;
goto release; goto err_alloc;
} }
mcp->owner = THIS_MODULE; mcp->owner = THIS_MODULE;
...@@ -160,7 +195,18 @@ static int mcp_sa11x0_probe(struct platform_device *pdev) ...@@ -160,7 +195,18 @@ static int mcp_sa11x0_probe(struct platform_device *pdev)
mcp->sclk_rate = data->sclk_rate; mcp->sclk_rate = data->sclk_rate;
mcp->gpio_base = data->gpio_base; mcp->gpio_base = data->gpio_base;
platform_set_drvdata(pdev, mcp); m = priv(mcp);
m->mccr0 = data->mccr0 | 0x7f7f;
m->mccr1 = data->mccr1;
m->base0 = ioremap(mem0->start, resource_size(mem0));
m->base1 = ioremap(mem1->start, resource_size(mem1));
if (!m->base0 || !m->base1) {
ret = -ENOMEM;
goto err_ioremap;
}
platform_set_drvdata(dev, mcp);
if (machine_is_assabet()) { if (machine_is_assabet()) {
ASSABET_BCR_set(ASSABET_BCR_CODEC_RST); ASSABET_BCR_set(ASSABET_BCR_CODEC_RST);
...@@ -170,9 +216,9 @@ static int mcp_sa11x0_probe(struct platform_device *pdev) ...@@ -170,9 +216,9 @@ static int mcp_sa11x0_probe(struct platform_device *pdev)
* Initialise device. Note that we initially * Initialise device. Note that we initially
* set the sampling rate to minimum. * set the sampling rate to minimum.
*/ */
Ser4MCSR = -1; writel_relaxed(-1, MCSR(m));
Ser4MCCR1 = data->mccr1; writel_relaxed(m->mccr1, MCCR1(m));
Ser4MCCR0 = data->mccr0 | 0x7f7f; writel_relaxed(m->mccr0, MCCR0(m));
/* /*
* Calculate the read/write timeout (us) from the bit clock * Calculate the read/write timeout (us) from the bit clock
...@@ -184,46 +230,57 @@ static int mcp_sa11x0_probe(struct platform_device *pdev) ...@@ -184,46 +230,57 @@ static int mcp_sa11x0_probe(struct platform_device *pdev)
ret = mcp_host_add(mcp); ret = mcp_host_add(mcp);
if (ret == 0) if (ret == 0)
goto out; return 0;
mcp_host_free(mcp); platform_set_drvdata(dev, NULL);
release:
release_mem_region(0x80060000, 0x60);
platform_set_drvdata(pdev, NULL);
out: err_ioremap:
iounmap(m->base1);
iounmap(m->base0);
mcp_host_free(mcp);
err_alloc:
release_mem_region(mem1->start, resource_size(mem1));
err_mem1:
release_mem_region(mem0->start, resource_size(mem0));
err_mem0:
return ret; return ret;
} }
static int mcp_sa11x0_remove(struct platform_device *dev) static int mcp_sa11x0_remove(struct platform_device *dev)
{ {
struct mcp *mcp = platform_get_drvdata(dev); struct mcp *mcp = platform_get_drvdata(dev);
struct mcp_sa11x0 *m = priv(mcp);
struct resource *mem0, *mem1;
mem0 = platform_get_resource(dev, IORESOURCE_MEM, 0);
mem1 = platform_get_resource(dev, IORESOURCE_MEM, 1);
platform_set_drvdata(dev, NULL); platform_set_drvdata(dev, NULL);
mcp_host_del(mcp); mcp_host_del(mcp);
iounmap(m->base1);
iounmap(m->base0);
mcp_host_free(mcp); mcp_host_free(mcp);
release_mem_region(0x80060000, 0x60); release_mem_region(mem1->start, resource_size(mem1));
release_mem_region(mem0->start, resource_size(mem0));
return 0; return 0;
} }
static int mcp_sa11x0_suspend(struct platform_device *dev, pm_message_t state) static int mcp_sa11x0_suspend(struct platform_device *dev, pm_message_t state)
{ {
struct mcp *mcp = platform_get_drvdata(dev); struct mcp_sa11x0 *m = priv(platform_get_drvdata(dev));
priv(mcp)->mccr0 = Ser4MCCR0; writel(m->mccr0 & ~MCCR0_MCE, MCCR0(m));
priv(mcp)->mccr1 = Ser4MCCR1;
Ser4MCCR0 &= ~MCCR0_MCE;
return 0; return 0;
} }
static int mcp_sa11x0_resume(struct platform_device *dev) static int mcp_sa11x0_resume(struct platform_device *dev)
{ {
struct mcp *mcp = platform_get_drvdata(dev); struct mcp_sa11x0 *m = priv(platform_get_drvdata(dev));
Ser4MCCR1 = priv(mcp)->mccr1; writel_relaxed(m->mccr1, MCCR1(m));
Ser4MCCR0 = priv(mcp)->mccr0; writel_relaxed(m->mccr0, MCCR0(m));
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment