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 return intc_get_field_from_handle(__raw_readb(addr), h);
81 static unsigned long test_16(unsigned long addr, unsigned long h,
84 return intc_get_field_from_handle(__raw_readw(addr), h);
87 static unsigned long test_32(unsigned long addr, unsigned long h,
90 return intc_get_field_from_handle(__raw_readl(addr), h);
93 static unsigned long write_8(unsigned long addr, unsigned long h,
96 __raw_writeb(intc_set_field_from_handle(0, data, h), addr);
97 (void)__raw_readb(addr); /* Defeat write posting */
101 static unsigned long write_16(unsigned long addr, unsigned long h,
104 __raw_writew(intc_set_field_from_handle(0, data, h), addr);
105 (void)__raw_readw(addr); /* Defeat write posting */
109 static unsigned long write_32(unsigned long addr, unsigned long h,
112 __raw_writel(intc_set_field_from_handle(0, data, h), addr);
113 (void)__raw_readl(addr); /* Defeat write posting */
117 static unsigned long modify_8(unsigned long addr, unsigned long h,
122 local_irq_save(flags);
123 value = intc_set_field_from_handle(__raw_readb(addr), data, h);
124 __raw_writeb(value, addr);
125 (void)__raw_readb(addr); /* Defeat write posting */
126 local_irq_restore(flags);
130 static unsigned long modify_16(unsigned long addr, unsigned long h,
135 local_irq_save(flags);
136 value = intc_set_field_from_handle(__raw_readw(addr), data, h);
137 __raw_writew(value, addr);
138 (void)__raw_readw(addr); /* Defeat write posting */
139 local_irq_restore(flags);
143 static unsigned long modify_32(unsigned long addr, unsigned long h,
148 local_irq_save(flags);
149 value = intc_set_field_from_handle(__raw_readl(addr), data, h);
150 __raw_writel(value, addr);
151 (void)__raw_readl(addr); /* Defeat write posting */
152 local_irq_restore(flags);
156 static unsigned long intc_mode_field(unsigned long addr,
157 unsigned long handle,
158 unsigned long (*fn)(unsigned long,
163 return fn(addr, handle, ((1 << _INTC_WIDTH(handle)) - 1));
166 static unsigned long intc_mode_zero(unsigned long addr,
167 unsigned long handle,
168 unsigned long (*fn)(unsigned long,
173 return fn(addr, handle, 0);
176 static unsigned long intc_mode_prio(unsigned long addr,
177 unsigned long handle,
178 unsigned long (*fn)(unsigned long,
183 return fn(addr, handle, intc_get_prio_level(irq));
186 unsigned long (*intc_reg_fns[])(unsigned long addr,
188 unsigned long data) = {
189 [REG_FN_TEST_BASE + 0] = test_8,
190 [REG_FN_TEST_BASE + 1] = test_16,
191 [REG_FN_TEST_BASE + 3] = test_32,
192 [REG_FN_WRITE_BASE + 0] = write_8,
193 [REG_FN_WRITE_BASE + 1] = write_16,
194 [REG_FN_WRITE_BASE + 3] = write_32,
195 [REG_FN_MODIFY_BASE + 0] = modify_8,
196 [REG_FN_MODIFY_BASE + 1] = modify_16,
197 [REG_FN_MODIFY_BASE + 3] = modify_32,
200 unsigned long (*intc_enable_fns[])(unsigned long addr,
201 unsigned long handle,
202 unsigned long (*fn)(unsigned long,
205 unsigned int irq) = {
206 [MODE_ENABLE_REG] = intc_mode_field,
207 [MODE_MASK_REG] = intc_mode_zero,
208 [MODE_DUAL_REG] = intc_mode_field,
209 [MODE_PRIO_REG] = intc_mode_prio,
210 [MODE_PCLR_REG] = intc_mode_prio,
213 unsigned long (*intc_disable_fns[])(unsigned long addr,
214 unsigned long handle,
215 unsigned long (*fn)(unsigned long,
218 unsigned int irq) = {
219 [MODE_ENABLE_REG] = intc_mode_zero,
220 [MODE_MASK_REG] = intc_mode_field,
221 [MODE_DUAL_REG] = intc_mode_field,
222 [MODE_PRIO_REG] = intc_mode_zero,
223 [MODE_PCLR_REG] = intc_mode_field,
226 unsigned long (*intc_enable_noprio_fns[])(unsigned long addr,
227 unsigned long handle,
228 unsigned long (*fn)(unsigned long,
231 unsigned int irq) = {
232 [MODE_ENABLE_REG] = intc_mode_field,
233 [MODE_MASK_REG] = intc_mode_zero,
234 [MODE_DUAL_REG] = intc_mode_field,
235 [MODE_PRIO_REG] = intc_mode_field,
236 [MODE_PCLR_REG] = intc_mode_field,