2 * Common INTC2 register accessors
4 * Copyright (C) 2007, 2008 Magnus Damm
5 * Copyright (C) 2009, 2010 Paul Mundt
7 * This file is subject to the terms and conditions of the GNU General Public
8 * License. See the file "COPYING" in the main directory of this archive
12 #include "internals.h"
14 unsigned long intc_phys_to_virt(struct intc_desc_int
*d
, unsigned long address
)
16 struct intc_window
*window
;
19 /* scan through physical windows and convert address */
20 for (k
= 0; k
< d
->nr_windows
; k
++) {
21 window
= d
->window
+ k
;
23 if (address
< window
->phys
)
26 if (address
>= (window
->phys
+ window
->size
))
29 address
-= window
->phys
;
30 address
+= (unsigned long)window
->virt
;
35 /* no windows defined, register must be 1:1 mapped virt:phys */
39 unsigned int intc_get_reg(struct intc_desc_int
*d
, unsigned long address
)
43 address
= intc_phys_to_virt(d
, address
);
45 for (k
= 0; k
< d
->nr_reg
; k
++) {
46 if (d
->reg
[k
] == address
)
54 unsigned int intc_set_field_from_handle(unsigned int value
,
55 unsigned int field_value
,
58 unsigned int width
= _INTC_WIDTH(handle
);
59 unsigned int shift
= _INTC_SHIFT(handle
);
61 value
&= ~(((1 << width
) - 1) << shift
);
62 value
|= field_value
<< shift
;
66 unsigned long intc_get_field_from_handle(unsigned int value
, unsigned int handle
)
68 unsigned int width
= _INTC_WIDTH(handle
);
69 unsigned int shift
= _INTC_SHIFT(handle
);
70 unsigned int mask
= ((1 << width
) - 1) << shift
;
72 return (value
& mask
) >> shift
;
75 static unsigned long test_8(unsigned long addr
, unsigned long h
,
78 void __iomem
*ptr
= (void __iomem
*)addr
;
79 return intc_get_field_from_handle(__raw_readb(ptr
), h
);
82 static unsigned long test_16(unsigned long addr
, unsigned long h
,
85 void __iomem
*ptr
= (void __iomem
*)addr
;
86 return intc_get_field_from_handle(__raw_readw(ptr
), h
);
89 static unsigned long test_32(unsigned long addr
, unsigned long h
,
92 void __iomem
*ptr
= (void __iomem
*)addr
;
93 return intc_get_field_from_handle(__raw_readl(ptr
), h
);
96 static unsigned long write_8(unsigned long addr
, unsigned long h
,
99 void __iomem
*ptr
= (void __iomem
*)addr
;
100 __raw_writeb(intc_set_field_from_handle(0, data
, h
), ptr
);
101 (void)__raw_readb(ptr
); /* Defeat write posting */
105 static unsigned long write_16(unsigned long addr
, unsigned long h
,
108 void __iomem
*ptr
= (void __iomem
*)addr
;
109 __raw_writew(intc_set_field_from_handle(0, data
, h
), ptr
);
110 (void)__raw_readw(ptr
); /* Defeat write posting */
114 static unsigned long write_32(unsigned long addr
, unsigned long h
,
117 void __iomem
*ptr
= (void __iomem
*)addr
;
118 __raw_writel(intc_set_field_from_handle(0, data
, h
), ptr
);
119 (void)__raw_readl(ptr
); /* Defeat write posting */
123 static unsigned long modify_8(unsigned long addr
, unsigned long h
,
126 void __iomem
*ptr
= (void __iomem
*)addr
;
129 local_irq_save(flags
);
130 value
= intc_set_field_from_handle(__raw_readb(ptr
), data
, h
);
131 __raw_writeb(value
, ptr
);
132 (void)__raw_readb(ptr
); /* Defeat write posting */
133 local_irq_restore(flags
);
137 static unsigned long modify_16(unsigned long addr
, unsigned long h
,
140 void __iomem
*ptr
= (void __iomem
*)addr
;
143 local_irq_save(flags
);
144 value
= intc_set_field_from_handle(__raw_readw(ptr
), data
, h
);
145 __raw_writew(value
, ptr
);
146 (void)__raw_readw(ptr
); /* Defeat write posting */
147 local_irq_restore(flags
);
151 static unsigned long modify_32(unsigned long addr
, unsigned long h
,
154 void __iomem
*ptr
= (void __iomem
*)addr
;
157 local_irq_save(flags
);
158 value
= intc_set_field_from_handle(__raw_readl(ptr
), data
, h
);
159 __raw_writel(value
, ptr
);
160 (void)__raw_readl(ptr
); /* Defeat write posting */
161 local_irq_restore(flags
);
165 static unsigned long intc_mode_field(unsigned long addr
,
166 unsigned long handle
,
167 unsigned long (*fn
)(unsigned long,
172 return fn(addr
, handle
, ((1 << _INTC_WIDTH(handle
)) - 1));
175 static unsigned long intc_mode_zero(unsigned long addr
,
176 unsigned long handle
,
177 unsigned long (*fn
)(unsigned long,
182 return fn(addr
, handle
, 0);
185 static unsigned long intc_mode_prio(unsigned long addr
,
186 unsigned long handle
,
187 unsigned long (*fn
)(unsigned long,
192 return fn(addr
, handle
, intc_get_prio_level(irq
));
195 unsigned long (*intc_reg_fns
[])(unsigned long addr
,
197 unsigned long data
) = {
198 [REG_FN_TEST_BASE
+ 0] = test_8
,
199 [REG_FN_TEST_BASE
+ 1] = test_16
,
200 [REG_FN_TEST_BASE
+ 3] = test_32
,
201 [REG_FN_WRITE_BASE
+ 0] = write_8
,
202 [REG_FN_WRITE_BASE
+ 1] = write_16
,
203 [REG_FN_WRITE_BASE
+ 3] = write_32
,
204 [REG_FN_MODIFY_BASE
+ 0] = modify_8
,
205 [REG_FN_MODIFY_BASE
+ 1] = modify_16
,
206 [REG_FN_MODIFY_BASE
+ 3] = modify_32
,
209 unsigned long (*intc_enable_fns
[])(unsigned long addr
,
210 unsigned long handle
,
211 unsigned long (*fn
)(unsigned long,
214 unsigned int irq
) = {
215 [MODE_ENABLE_REG
] = intc_mode_field
,
216 [MODE_MASK_REG
] = intc_mode_zero
,
217 [MODE_DUAL_REG
] = intc_mode_field
,
218 [MODE_PRIO_REG
] = intc_mode_prio
,
219 [MODE_PCLR_REG
] = intc_mode_prio
,
222 unsigned long (*intc_disable_fns
[])(unsigned long addr
,
223 unsigned long handle
,
224 unsigned long (*fn
)(unsigned long,
227 unsigned int irq
) = {
228 [MODE_ENABLE_REG
] = intc_mode_zero
,
229 [MODE_MASK_REG
] = intc_mode_field
,
230 [MODE_DUAL_REG
] = intc_mode_field
,
231 [MODE_PRIO_REG
] = intc_mode_zero
,
232 [MODE_PCLR_REG
] = intc_mode_field
,
235 unsigned long (*intc_enable_noprio_fns
[])(unsigned long addr
,
236 unsigned long handle
,
237 unsigned long (*fn
)(unsigned long,
240 unsigned int irq
) = {
241 [MODE_ENABLE_REG
] = intc_mode_field
,
242 [MODE_MASK_REG
] = intc_mode_zero
,
243 [MODE_DUAL_REG
] = intc_mode_field
,
244 [MODE_PRIO_REG
] = intc_mode_field
,
245 [MODE_PCLR_REG
] = intc_mode_field
,