aboutsummaryrefslogtreecommitdiff
path: root/drivers/sh/intc/access.c
blob: f892ae1d212a10a6671b88c60d8ec8a3ad9b822c (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
235
236
237
/*
 * Common INTC2 register accessors
 *
 * Copyright (C) 2007, 2008 Magnus Damm
 * Copyright (C) 2009, 2010 Paul Mundt
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */
#include <linux/io.h>
#include "internals.h"

unsigned long intc_phys_to_virt(struct intc_desc_int *d, unsigned long address)
{
	struct intc_window *window;
	int k;

	/* scan through physical windows and convert address */
	for (k = 0; k < d->nr_windows; k++) {
		window = d->window + k;

		if (address < window->phys)
			continue;

		if (address >= (window->phys + window->size))
			continue;

		address -= window->phys;
		address += (unsigned long)window->virt;

		return address;
	}

	/* no windows defined, register must be 1:1 mapped virt:phys */
	return address;
}

unsigned int intc_get_reg(struct intc_desc_int *d, unsigned long address)
{
	unsigned int k;

	address = intc_phys_to_virt(d, address);

	for (k = 0; k < d->nr_reg; k++) {
		if (d->reg[k] == address)
			return k;
	}

	BUG();
	return 0;
}

unsigned int intc_set_field_from_handle(unsigned int value,
					unsigned int field_value,
					unsigned int handle)
{
	unsigned int width = _INTC_WIDTH(handle);
	unsigned int shift = _INTC_SHIFT(handle);

	value &= ~(((1 << width) - 1) << shift);
	value |= field_value << shift;
	return value;
}

unsigned long intc_get_field_from_handle(unsigned int value, unsigned int handle)
{
	unsigned int width = _INTC_WIDTH(handle);
	unsigned int shift = _INTC_SHIFT(handle);
	unsigned int mask = ((1 << width) - 1) << shift;

	return (value & mask) >> shift;
}

static unsigned long test_8(unsigned long addr, unsigned long h,
			    unsigned long ignore)
{
	return intc_get_field_from_handle(__raw_readb(addr), h);
}

static unsigned long test_16(unsigned long addr, unsigned long h,
			     unsigned long ignore)
{
	return intc_get_field_from_handle(__raw_readw(addr), h);
}

static unsigned long test_32(unsigned long addr, unsigned long h,
			     unsigned long ignore)
{
	return intc_get_field_from_handle(__raw_readl(addr), h);
}

static unsigned long write_8(unsigned long addr, unsigned long h,
			     unsigned long data)
{
	__raw_writeb(intc_set_field_from_handle(0, data, h), addr);
	(void)__raw_readb(addr);	/* Defeat write posting */
	return 0;
}

static unsigned long write_16(unsigned long addr, unsigned long h,
			      unsigned long data)
{
	__raw_writew(intc_set_field_from_handle(0, data, h), addr);
	(void)__raw_readw(addr);	/* Defeat write posting */
	return 0;
}

static unsigned long write_32(unsigned long addr, unsigned long h,
			      unsigned long data)
{
	__raw_writel(intc_set_field_from_handle(0, data, h), addr);
	(void)__raw_readl(addr);	/* Defeat write posting */
	return 0;
}

static unsigned long modify_8(unsigned long addr, unsigned long h,
			      unsigned long data)
{
	unsigned long flags;
	unsigned int value;
	local_irq_save(flags);
	value = intc_set_field_from_handle(__raw_readb(addr), data, h);
	__raw_writeb(value, addr);
	(void)__raw_readb(addr);	/* Defeat write posting */
	local_irq_restore(flags);
	return 0;
}

static unsigned long modify_16(unsigned long addr, unsigned long h,
			       unsigned long data)
{
	unsigned long flags;
	unsigned int value;
	local_irq_save(flags);
	value = intc_set_field_from_handle(__raw_readw(addr), data, h);
	__raw_writew(value, addr);
	(void)__raw_readw(addr);	/* Defeat write posting */
	local_irq_restore(flags);
	return 0;
}

static unsigned long modify_32(unsigned long addr, unsigned long h,
			       unsigned long data)
{
	unsigned long flags;
	unsigned int value;
	local_irq_save(flags);
	value = intc_set_field_from_handle(__raw_readl(addr), data, h);
	__raw_writel(value, addr);
	(void)__raw_readl(addr);	/* Defeat write posting */
	local_irq_restore(flags);
	return 0;
}

static unsigned long intc_mode_field(unsigned long addr,
				     unsigned long handle,
				     unsigned long (*fn)(unsigned long,
						unsigned long,
						unsigned long),
				     unsigned int irq)
{
	return fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
}

static unsigned long intc_mode_zero(unsigned long addr,
				    unsigned long handle,
				    unsigned long (*fn)(unsigned long,
					       unsigned long,
					       unsigned long),
				    unsigned int irq)
{
	return fn(addr, handle, 0);
}

static unsigned long intc_mode_prio(unsigned long addr,
				    unsigned long handle,
				    unsigned long (*fn)(unsigned long,
					       unsigned long,
					       unsigned long),
				    unsigned int irq)
{
	return fn(addr, handle, intc_get_prio_level(irq));
}

unsigned long (*intc_reg_fns[])(unsigned long addr,
				unsigned long h,
				unsigned long data) = {
	[REG_FN_TEST_BASE + 0] = test_8,
	[REG_FN_TEST_BASE + 1] = test_16,
	[REG_FN_TEST_BASE + 3] = test_32,
	[REG_FN_WRITE_BASE + 0] = write_8,
	[REG_FN_WRITE_BASE + 1] = write_16,
	[REG_FN_WRITE_BASE + 3] = write_32,
	[REG_FN_MODIFY_BASE + 0] = modify_8,
	[REG_FN_MODIFY_BASE + 1] = modify_16,
	[REG_FN_MODIFY_BASE + 3] = modify_32,
};

unsigned long (*intc_enable_fns[])(unsigned long addr,
				   unsigned long handle,
				   unsigned long (*fn)(unsigned long,
					    unsigned long,
					    unsigned long),
				   unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_field,
	[MODE_MASK_REG] = intc_mode_zero,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_prio,
	[MODE_PCLR_REG] = intc_mode_prio,
};

unsigned long (*intc_disable_fns[])(unsigned long addr,
				    unsigned long handle,
				    unsigned long (*fn)(unsigned long,
					     unsigned long,
					     unsigned long),
				    unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_zero,
	[MODE_MASK_REG] = intc_mode_field,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_zero,
	[MODE_PCLR_REG] = intc_mode_field,
};

unsigned long (*intc_enable_noprio_fns[])(unsigned long addr,
					  unsigned long handle,
					  unsigned long (*fn)(unsigned long,
						unsigned long,
						unsigned long),
					  unsigned int irq) = {
	[MODE_ENABLE_REG] = intc_mode_field,
	[MODE_MASK_REG] = intc_mode_zero,
	[MODE_DUAL_REG] = intc_mode_field,
	[MODE_PRIO_REG] = intc_mode_field,
	[MODE_PCLR_REG] = intc_mode_field,
};