summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Robinson Jr <rrobinson@phytec.com>2013-02-21 15:14:59 -0800
committerRussell Robinson Jr <rrobinson@phytec.com>2013-03-14 17:37:37 -0700
commit58b87cfa999fdcc9ea6ce966f0a1cd96071e4b3a (patch)
treef1926676699c6eb1e7c5dce1e8145af83d4bd567
parent89df1897f9e305288503bccac1b7de0c196dca48 (diff)
Initial changes to support mcc and i2c semaphore for ping pong demoPD13.0.2
Signed-off-by: Russell Robinson Jr <rrobinson@phytec.com>
-rw-r--r--.gitignore1
-rw-r--r--arch/arm/mach-mvf/Makefile2
-rw-r--r--arch/arm/mach-mvf/mvf_sema4.c234
-rw-r--r--drivers/i2c/busses/i2c-imx.c34
-rw-r--r--include/linux/mvf_sema4.h15
5 files changed, 281 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 9dacde0a4b2d..13f15ddf6dbf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,6 +10,7 @@
# Normal rules
#
.*
+!.config
*.o
*.o.*
*.a
diff --git a/arch/arm/mach-mvf/Makefile b/arch/arm/mach-mvf/Makefile
index fad70a4a9dc7..3128d4288c3d 100644
--- a/arch/arm/mach-mvf/Makefile
+++ b/arch/arm/mach-mvf/Makefile
@@ -3,7 +3,7 @@
#
# Object file lists.
-obj-y := cpu.o mm.o system.o devices.o dummy_gpio.o irq.o bus_freq.o mvf_fec.o usb_dr.o usb_dr2.o pm.o
+obj-y := cpu.o mm.o system.o devices.o dummy_gpio.o irq.o bus_freq.o mvf_fec.o usb_dr.o usb_dr2.o pm.o mvf_sema4.o
obj-y += l2switch.o
obj-$(CONFIG_ARCH_MVFA5) += clock.o mvf_suspend.o
diff --git a/arch/arm/mach-mvf/mvf_sema4.c b/arch/arm/mach-mvf/mvf_sema4.c
new file mode 100644
index 000000000000..311b56a69d73
--- /dev/null
+++ b/arch/arm/mach-mvf/mvf_sema4.c
@@ -0,0 +1,234 @@
+/*
+* header goes here
+*/
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/device.h>
+#include <linux/sched.h>
+
+#include <mach/mvf.h>
+
+#define NUM_GATES 16
+#define MASK_FROM_GATE(gate_num) ((u32)(1 << (NUM_GATES*2 - 1 - idx[gate_num])))
+
+#define THIS_CORE (0)
+#define LOCK_VALUE (THIS_CORE + 1)
+
+#define SEMA4_CP0INE (MVF_SEMA4_BASE_ADDR + 0x40)
+#define SEMA4_CP0NTF (MVF_SEMA4_BASE_ADDR + 0x80)
+
+//#if 0
+#include <linux/mm.h>
+#include <linux/version.h>
+#include <linux/kdev_t.h>
+#include <linux/fs.h>
+#include <linux/cdev.h>
+#include <mach/hardware.h>
+#include <linux/string.h>
+#include <linux/uaccess.h>
+#include <linux/delay.h>
+#include <linux/time.h>
+//#endif
+
+#include <linux/mvf_sema4.h>
+
+// ************************************ Local Data *************************************************
+
+static MVF_SEMA4* gates[NUM_GATES];
+
+static bool initialized = false;
+
+// account for the way the bits are set / returned in CP0INE and CP0NTF
+static const int idx[16] = {3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12};
+
+
+// ************************************ Interrupt handler *************************************************
+
+static irqreturn_t sema4_irq_handler(int irq, void *dev_id)
+{
+ int gate_num;
+
+ u32 cp0ntf = readl(MVF_IO_ADDRESS(SEMA4_CP0NTF));
+
+ for(gate_num=0; gate_num<NUM_GATES; gate_num++)
+ {
+ // interrupt from this gate?
+ if(cp0ntf & MASK_FROM_GATE(gate_num))
+ {
+ // grab the gate to stop the interrupts
+ writeb(LOCK_VALUE, MVF_IO_ADDRESS(MVF_SEMA4_BASE_ADDR) + gate_num);
+
+ // make sure there's a gate assigned
+ if(gates[gate_num])
+ {
+ if(gates[gate_num]->use_interrupts)
+ // wake up whoever was aiting
+ wake_up_interruptible(&(gates[gate_num]->wait_queue));
+ }
+ }
+ }
+
+ return IRQ_HANDLED;
+}
+
+// ************************************ Utility functions *************************************************
+
+static int find_sema4(MVF_SEMA4 *sema4)
+{
+ int i;
+
+ for(i=0; i<NUM_GATES; i++)
+ {
+ if(gates[i] == sema4)
+ return i;
+ }
+
+ return -EINVAL;
+}
+
+static int initialize(void)
+{
+ int i;
+
+ // clear the gates table
+ for(i=0; i<NUM_GATES; i++)
+ gates[i] = NULL;
+
+ // clear out all notification requests
+ writel(0, MVF_IO_ADDRESS(SEMA4_CP0INE));
+
+ //Register the interrupt handler
+ if (request_irq(MVF_INT_SEMA4, sema4_irq_handler, 0, "mvf_sema4_handler", NULL) != 0)
+ {
+ printk(KERN_ERR "Failed to register MVF_INT_SEMA4 interrupt.\n");
+ return -EIO;
+ }
+
+ initialized = true;
+ return 0;
+}
+
+int mvf_sema4_assign(int gate_num, bool use_interrupts, MVF_SEMA4** sema4_p)
+{
+ int retval;
+ u32 cp0ine;
+ unsigned long irq_flags;
+
+ // take the opportunity to initialize the whole sub-system
+ if(!initialized)
+ {
+ retval = initialize();
+ if(retval)
+ return retval;
+ }
+
+ if((gate_num < 0) || (gate_num >= NUM_GATES))
+ return -EINVAL;
+
+ if(gates[gate_num])
+ return -EBUSY;
+
+ *sema4_p = (MVF_SEMA4 *)kmalloc(sizeof(MVF_SEMA4), GFP_KERNEL);
+ if(*sema4_p == NULL)
+ return -ENOMEM;
+
+ gates[gate_num] = *sema4_p;
+ (*sema4_p)->gate_num = gate_num;
+ (*sema4_p)->use_interrupts = use_interrupts;
+
+ if(use_interrupts)
+ {
+ init_waitqueue_head(&((*sema4_p)->wait_queue));
+ local_irq_save(irq_flags);
+ cp0ine = readl(MVF_IO_ADDRESS(SEMA4_CP0INE));
+ cp0ine |= MASK_FROM_GATE(gate_num);
+ writel(cp0ine, MVF_IO_ADDRESS(SEMA4_CP0INE));
+ local_irq_restore(irq_flags);
+ }
+
+ return 0;
+}
+
+int mvf_sema4_deassign(MVF_SEMA4 *sema4)
+{
+ u32 cp0ine;
+ unsigned long irq_flags;
+
+ int gate_num = find_sema4(sema4);
+ if(gate_num < 0)
+ return gate_num;
+
+ if(sema4->use_interrupts)
+ {
+ local_irq_save(irq_flags);
+ cp0ine = readl(MVF_IO_ADDRESS(SEMA4_CP0INE));
+ cp0ine &= ~MASK_FROM_GATE(gate_num);
+ writel(cp0ine, MVF_IO_ADDRESS(SEMA4_CP0INE));
+ local_irq_restore(irq_flags);
+ }
+
+ kfree(sema4);
+ gates[gate_num] = NULL;
+
+ return 0;
+}
+
+int mvf_sema4_lock(MVF_SEMA4 *sema4, unsigned int timeout_us)
+{
+ int retval;
+ int gate_num = find_sema4(sema4);
+ if(gate_num < 0)
+ return gate_num;
+
+ // cant use timeouts if not using interruppts
+ // TODO use spin lock if not using interrupts
+ if((!sema4->use_interrupts) && timeout_us)
+ return -EINVAL;
+
+ // try to grab it
+ writeb(LOCK_VALUE, MVF_IO_ADDRESS(MVF_SEMA4_BASE_ADDR) + gate_num);
+ if(readb(MVF_IO_ADDRESS(MVF_SEMA4_BASE_ADDR) + gate_num) == LOCK_VALUE)
+ return 0;
+
+ // no timeout, fail
+ if(!timeout_us)
+ return -EBUSY;
+
+ // wait forever?
+ if(timeout_us == 0xffffffff)
+ {
+ if(wait_event_interruptible(sema4->wait_queue, (readb(MVF_IO_ADDRESS(MVF_SEMA4_BASE_ADDR) + gate_num) == LOCK_VALUE)))
+ return -ERESTARTSYS;
+ }
+ else
+ {
+ // return: 0 = timeout, >0 = woke up with that many jiffies left, <0 = error
+ retval = wait_event_interruptible_timeout(sema4->wait_queue,
+ (readb(MVF_IO_ADDRESS(MVF_SEMA4_BASE_ADDR) + gate_num) == LOCK_VALUE),
+ usecs_to_jiffies(timeout_us));
+ if(retval == 0)
+ return -ETIME;
+ else if(retval < 0)
+ return retval;
+ }
+
+ return 0;
+}
+
+int mvf_sema4_unlock(MVF_SEMA4 *sema4)
+{
+ int gate_num = find_sema4(sema4);
+ if(gate_num < 0)
+ return gate_num;
+
+ // unlock it
+ writeb(0, MVF_IO_ADDRESS(MVF_SEMA4_BASE_ADDR) + gate_num);
+
+ return 0;
+}
+
diff --git a/drivers/i2c/busses/i2c-imx.c b/drivers/i2c/busses/i2c-imx.c
index 71b8cd4f8a3e..853e0688998e 100644
--- a/drivers/i2c/busses/i2c-imx.c
+++ b/drivers/i2c/busses/i2c-imx.c
@@ -52,6 +52,7 @@
#include <mach/irqs.h>
#include <mach/hardware.h>
#include <mach/i2c.h>
+#include <linux/mvf_sema4.h>
/** Defines ********************************************************************
*******************************************************************************/
@@ -122,6 +123,8 @@ static u16 __initdata i2c_clk_div[60][2] = {
{ 2304, 0x3C }, { 2560, 0x3D }, { 3072, 0x3E }, { 3584, 0x7A },
{ 3840, 0x3F }, { 4096, 0x7B }, { 5120, 0x7D }, { 6144, 0x7E },
};
+
+static MVF_SEMA4* sema4 = NULL;
#else
static u16 __initdata i2c_clk_div[50][2] = {
{ 22, 0x20 }, { 24, 0x21 }, { 26, 0x22 }, { 28, 0x23 },
@@ -215,7 +218,7 @@ static int i2c_imx_start(struct imx_i2c_struct *i2c_imx)
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
- clk_enable(i2c_imx->clk);
+ //clk_enable(i2c_imx->clk);
writeb(i2c_imx->ifdr, i2c_imx->base + IMX_I2C_IFDR);
/* Enable I2C controller */
#ifdef CONFIG_ARCH_MVF
@@ -269,8 +272,8 @@ static void i2c_imx_stop(struct imx_i2c_struct *i2c_imx)
}
/* Disable I2C controller */
- writeb(0, i2c_imx->base + IMX_I2C_I2CR);
- clk_disable(i2c_imx->clk);
+ writeb(0x80, i2c_imx->base + IMX_I2C_I2CR);
+ //clk_disable(i2c_imx->clk);
}
static void __init i2c_imx_set_clk(struct imx_i2c_struct *i2c_imx,
@@ -434,6 +437,23 @@ static int i2c_imx_xfer(struct i2c_adapter *adapter,
dev_dbg(&i2c_imx->adapter.dev, "<%s>\n", __func__);
+#ifdef CONFIG_ARCH_MVF
+ // since this can get called before probe, assign happens here
+ if(!sema4)
+ {
+ result = mvf_sema4_assign(3, true, &sema4);
+ if(result) {
+ printk(KERN_ERR "can't assign sema4 %s %s exiting.\n",__FILE__,__func__);
+ return result;
+ }
+ }
+
+ // lock out MQX
+ result = mvf_sema4_lock(sema4, 10000000); // 10 seconds
+ if(result)
+ return result;
+#endif
+
/* Start I2C transfer */
result = i2c_imx_start(i2c_imx);
if (result)
@@ -482,6 +502,10 @@ fail0:
/* Stop I2C transfer */
i2c_imx_stop(i2c_imx);
+#ifdef CONFIG_ARCH_MVF
+ mvf_sema4_unlock(sema4);
+#endif
+
dev_dbg(&i2c_imx->adapter.dev, "<%s> exit with: %s: %d\n", __func__,
(result < 0) ? "error" : "success msg",
(result < 0) ? result : num);
@@ -571,6 +595,7 @@ static int __init i2c_imx_probe(struct platform_device *pdev)
dev_err(&pdev->dev, "can't get I2C clock\n");
goto fail3;
}
+clk_enable(i2c_imx->clk);
/* Request IRQ */
ret = request_irq(i2c_imx->irq, i2c_imx_isr, 0, pdev->name, i2c_imx);
@@ -592,8 +617,9 @@ static int __init i2c_imx_probe(struct platform_device *pdev)
i2c_imx_set_clk(i2c_imx, IMX_I2C_BIT_RATE);
/* Set up chip registers to defaults */
- writeb(0, i2c_imx->base + IMX_I2C_I2CR);
+ writeb(0x80, i2c_imx->base + IMX_I2C_I2CR);
writeb(0, i2c_imx->base + IMX_I2C_I2SR);
+//clk_disable(i2c_imx->clk);
/* Add I2C adapter */
ret = i2c_add_numbered_adapter(&i2c_imx->adapter);
diff --git a/include/linux/mvf_sema4.h b/include/linux/mvf_sema4.h
new file mode 100644
index 000000000000..898065afb23f
--- /dev/null
+++ b/include/linux/mvf_sema4.h
@@ -0,0 +1,15 @@
+#ifndef __MVF_SEMA4__
+#define __MVF_SEMA4__
+
+typedef struct mvf_sema4_handle_struct {
+ int gate_num;
+ int use_interrupts;
+ wait_queue_head_t wait_queue;
+} MVF_SEMA4;
+
+int mvf_sema4_assign(int gate_num, bool use_interrupts, MVF_SEMA4** sema4_p);
+int mvf_sema4_deassign(MVF_SEMA4 *sema4);
+int mvf_sema4_lock(MVF_SEMA4 *sema4, unsigned int timeout_us);
+int mvf_sema4_unlock(MVF_SEMA4 *sema4);
+
+#endif