summaryrefslogtreecommitdiff
path: root/arch/arm/mach-mvf/mvf_sema4.c
blob: 311b56a69d738b1c3535d024db772ed06d6e2f11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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;
}