vserver 1.9.5.x5
[linux-2.6.git] / arch / arm / mach-ixp4xx / common.c
1 /*
2  * arch/arm/mach-ixp4xx/common.c
3  *
4  * Generic code shared across all IXP4XX platforms
5  *
6  * Maintainer: Deepak Saxena <dsaxena@plexity.net>
7  *
8  * Copyright 2002 (c) Intel Corporation
9  * Copyright 2003-2004 (c) MontaVista, Software, Inc. 
10  * 
11  * This file is licensed under  the terms of the GNU General Public 
12  * License version 2. This program is licensed "as is" without any 
13  * warranty of any kind, whether express or implied.
14  */
15
16 #include <linux/config.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/init.h>
20 #include <linux/serial.h>
21 #include <linux/sched.h>
22 #include <linux/tty.h>
23 #include <linux/serial_core.h>
24 #include <linux/bootmem.h>
25 #include <linux/interrupt.h>
26 #include <linux/bitops.h>
27 #include <linux/time.h>
28 #include <linux/timex.h>
29
30 #include <asm/hardware.h>
31 #include <asm/uaccess.h>
32 #include <asm/io.h>
33 #include <asm/pgtable.h>
34 #include <asm/page.h>
35 #include <asm/irq.h>
36
37 #include <asm/mach/map.h>
38 #include <asm/mach/irq.h>
39 #include <asm/mach/time.h>
40
41
42 /*************************************************************************
43  * GPIO acces functions
44  *************************************************************************/
45
46 /*
47  * Configure GPIO line for input, interrupt, or output operation
48  *
49  * TODO: Enable/disable the irq_desc based on interrupt or output mode.
50  * TODO: Should these be named ixp4xx_gpio_?
51  */
52 void gpio_line_config(u8 line, u32 style)
53 {
54         u32 enable;
55         volatile u32 *int_reg;
56         u32 int_style;
57
58         enable = *IXP4XX_GPIO_GPOER;
59
60         if (style & IXP4XX_GPIO_OUT) {
61                 enable &= ~((1) << line);
62         } else if (style & IXP4XX_GPIO_IN) {
63                 enable |= ((1) << line);
64
65                 switch (style & IXP4XX_GPIO_INTSTYLE_MASK)
66                 {
67                 case (IXP4XX_GPIO_ACTIVE_HIGH):
68                         int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
69                         break;
70                 case (IXP4XX_GPIO_ACTIVE_LOW):
71                         int_style = IXP4XX_GPIO_STYLE_ACTIVE_LOW;
72                         break;
73                 case (IXP4XX_GPIO_RISING_EDGE):
74                         int_style = IXP4XX_GPIO_STYLE_RISING_EDGE;
75                         break;
76                 case (IXP4XX_GPIO_FALLING_EDGE):
77                         int_style = IXP4XX_GPIO_STYLE_FALLING_EDGE;
78                         break;
79                 case (IXP4XX_GPIO_TRANSITIONAL):
80                         int_style = IXP4XX_GPIO_STYLE_TRANSITIONAL;
81                         break;
82                 default:
83                         int_style = IXP4XX_GPIO_STYLE_ACTIVE_HIGH;
84                         break;
85                 }
86
87                 if (line >= 8) {        /* pins 8-15 */ 
88                         line -= 8;
89                         int_reg = IXP4XX_GPIO_GPIT2R;
90                 }
91                 else {                  /* pins 0-7 */
92                         int_reg = IXP4XX_GPIO_GPIT1R;
93                 }
94
95                 /* Clear the style for the appropriate pin */
96                 *int_reg &= ~(IXP4XX_GPIO_STYLE_CLEAR << 
97                                 (line * IXP4XX_GPIO_STYLE_SIZE));
98
99                 /* Set the new style */
100                 *int_reg |= (int_style << (line * IXP4XX_GPIO_STYLE_SIZE));
101         }
102
103         *IXP4XX_GPIO_GPOER = enable;
104 }
105
106 EXPORT_SYMBOL(gpio_line_config);
107
108 /*************************************************************************
109  * IXP4xx chipset I/O mapping
110  *************************************************************************/
111 static struct map_desc ixp4xx_io_desc[] __initdata = {
112         {       /* UART, Interrupt ctrl, GPIO, timers, NPEs, MACs, USB .... */
113                 .virtual        = IXP4XX_PERIPHERAL_BASE_VIRT,
114                 .physical       = IXP4XX_PERIPHERAL_BASE_PHYS,
115                 .length         = IXP4XX_PERIPHERAL_REGION_SIZE,
116                 .type           = MT_DEVICE
117         }, {    /* Expansion Bus Config Registers */
118                 .virtual        = IXP4XX_EXP_CFG_BASE_VIRT,
119                 .physical       = IXP4XX_EXP_CFG_BASE_PHYS,
120                 .length         = IXP4XX_EXP_CFG_REGION_SIZE,
121                 .type           = MT_DEVICE
122         }, {    /* PCI Registers */
123                 .virtual        = IXP4XX_PCI_CFG_BASE_VIRT,
124                 .physical       = IXP4XX_PCI_CFG_BASE_PHYS,
125                 .length         = IXP4XX_PCI_CFG_REGION_SIZE,
126                 .type           = MT_DEVICE
127         }
128 };
129
130 void __init ixp4xx_map_io(void)
131 {
132         iotable_init(ixp4xx_io_desc, ARRAY_SIZE(ixp4xx_io_desc));
133 }
134
135
136 /*************************************************************************
137  * IXP4xx chipset IRQ handling
138  *
139  * TODO: GPIO IRQs should be marked invalid until the user of the IRQ
140  *       (be it PCI or something else) configures that GPIO line
141  *       as an IRQ. Also, we should use a different chip structure for 
142  *       level-based GPIO vs edge-based GPIO. Currently nobody needs this as 
143  *       all HW that's publically available uses level IRQs, so we'll
144  *       worry about it if/when we have HW to test.
145  **************************************************************************/
146 static void ixp4xx_irq_mask(unsigned int irq)
147 {
148         if (cpu_is_ixp46x() && irq >= 32)
149                 *IXP4XX_ICMR2 &= ~(1 << (irq - 32));
150         else
151                 *IXP4XX_ICMR &= ~(1 << irq);
152 }
153
154 static void ixp4xx_irq_mask_ack(unsigned int irq)
155 {
156         ixp4xx_irq_mask(irq);
157 }
158
159 static void ixp4xx_irq_unmask(unsigned int irq)
160 {
161         static int irq2gpio[32] = {
162                 -1, -1, -1, -1, -1, -1,  0,  1,
163                 -1, -1, -1, -1, -1, -1, -1, -1,
164                 -1, -1, -1,  2,  3,  4,  5,  6,
165                  7,  8,  9, 10, 11, 12, -1, -1,
166         };
167         int line = (irq < 32) ? irq2gpio[irq] : -1;
168
169         /*
170          * This only works for LEVEL gpio IRQs as per the IXP4xx developer's
171          * manual. If edge-triggered, need to move it to the mask_ack.
172          * Nobody seems to be using the edge-triggered mode on the GPIOs. 
173          */
174         if (line >= 0)
175                 gpio_line_isr_clear(line);
176
177         if (cpu_is_ixp46x() && irq >= 32)
178                 *IXP4XX_ICMR2 |= (1 << (irq - 32));
179         else
180                 *IXP4XX_ICMR |= (1 << irq);
181 }
182
183 static struct irqchip ixp4xx_irq_chip = {
184         .ack    = ixp4xx_irq_mask_ack,
185         .mask   = ixp4xx_irq_mask,
186         .unmask = ixp4xx_irq_unmask,
187 };
188
189 void __init ixp4xx_init_irq(void)
190 {
191         int i = 0;
192
193         /* Route all sources to IRQ instead of FIQ */
194         *IXP4XX_ICLR = 0x0;
195
196         /* Disable all interrupt */
197         *IXP4XX_ICMR = 0x0; 
198
199         if (cpu_is_ixp46x()) {
200                 /* Route upper 32 sources to IRQ instead of FIQ */
201                 *IXP4XX_ICLR2 = 0x00;
202
203                 /* Disable upper 32 interrupts */
204                 *IXP4XX_ICMR2 = 0x00;
205         }
206
207         for(i = 0; i < NR_IRQS; i++)
208         {
209                 set_irq_chip(i, &ixp4xx_irq_chip);
210                 set_irq_handler(i, do_level_IRQ);
211                 set_irq_flags(i, IRQF_VALID);
212         }
213 }
214
215
216 /*************************************************************************
217  * IXP4xx timer tick
218  * We use OS timer1 on the CPU for the timer tick and the timestamp 
219  * counter as a source of real clock ticks to account for missed jiffies.
220  *************************************************************************/
221
222 static unsigned volatile last_jiffy_time;
223
224 #define CLOCK_TICKS_PER_USEC    ((CLOCK_TICK_RATE + USEC_PER_SEC/2) / USEC_PER_SEC)
225
226 /* IRQs are disabled before entering here from do_gettimeofday() */
227 static unsigned long ixp4xx_gettimeoffset(void)
228 {
229         u32 elapsed;
230
231         elapsed = *IXP4XX_OSTS - last_jiffy_time;
232
233         return elapsed / CLOCK_TICKS_PER_USEC;
234 }
235
236 static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
237 {
238         write_seqlock(&xtime_lock);
239
240         /* Clear Pending Interrupt by writing '1' to it */
241         *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
242
243         /*
244          * Catch up with the real idea of time
245          */
246         while ((*IXP4XX_OSTS - last_jiffy_time) > LATCH) {
247                 timer_tick(regs);
248                 last_jiffy_time += LATCH;
249         }
250
251         write_sequnlock(&xtime_lock);
252
253         return IRQ_HANDLED;
254 }
255
256 static struct irqaction ixp4xx_timer_irq = {
257         .name           = "IXP4xx Timer Tick",
258         .flags          = SA_INTERRUPT,
259         .handler        = ixp4xx_timer_interrupt
260 };
261
262 static void __init ixp4xx_timer_init(void)
263 {
264         /* Clear Pending Interrupt by writing '1' to it */
265         *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
266
267         /* Setup the Timer counter value */
268         *IXP4XX_OSRT1 = (LATCH & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
269
270         /* Reset time-stamp counter */
271         *IXP4XX_OSTS = 0;
272         last_jiffy_time = 0;
273
274         /* Connect the interrupt handler and enable the interrupt */
275         setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
276 }
277
278 struct sys_timer ixp4xx_timer = {
279         .init           = ixp4xx_timer_init,
280         .offset         = ixp4xx_gettimeoffset,
281 };
282
283 static struct resource ixp46x_i2c_resources[] = {
284         [0] = {
285                 .start  = 0xc8011000,
286                 .end    = 0xc801101c,
287                 .flags  = IORESOURCE_MEM,
288         },
289         [1] = {
290                 .start  = IRQ_IXP4XX_I2C,
291                 .end    = IRQ_IXP4XX_I2C,
292                 .flags  = IORESOURCE_IRQ
293         }
294 };
295
296 /*
297  * I2C controller. The IXP46x uses the same block as the IOP3xx, so
298  * we just use the same device name.
299  */
300 static struct platform_device ixp46x_i2c_controller = {
301         .name           = "IOP3xx-I2C",
302         .id             = 0,
303         .num_resources  = 2,
304         .resource       = ixp46x_i2c_resources
305 };
306
307 static struct platform_device *ixp46x_devices[] __initdata = {
308         &ixp46x_i2c_controller
309 };
310
311 void __init ixp4xx_sys_init(void)
312 {
313         if (cpu_is_ixp46x()) {
314                 platform_add_devices(ixp46x_devices,
315                                 ARRAY_SIZE(ixp46x_devices));
316         }
317 }
318