http://downloads.netgear.com/files/GPL/GPL_Source_V361j_DM111PSP_series_consumer_rele...
[bcm963xx.git] / kernel / linux / 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         *IXP4XX_ICMR &= ~(1 << irq);
149 }
150
151 static void ixp4xx_irq_mask_ack(unsigned int irq)
152 {
153         ixp4xx_irq_mask(irq);
154 }
155
156 static void ixp4xx_irq_unmask(unsigned int irq)
157 {
158         static int irq2gpio[NR_IRQS] = {
159                 -1, -1, -1, -1, -1, -1,  0,  1,
160                 -1, -1, -1, -1, -1, -1, -1, -1,
161                 -1, -1, -1,  2,  3,  4,  5,  6,
162                  7,  8,  9, 10, 11, 12, -1, -1,
163         };
164         int line = irq2gpio[irq];
165
166         /*
167          * This only works for LEVEL gpio IRQs as per the IXP4xx developer's
168          * manual. If edge-triggered, need to move it to the mask_ack.
169          * Nobody seems to be using the edge-triggered mode on the GPIOs. 
170          */
171         if (line >= 0)
172                 gpio_line_isr_clear(line);
173
174         *IXP4XX_ICMR |= (1 << irq);
175 }
176
177 static struct irqchip ixp4xx_irq_chip = {
178         .ack    = ixp4xx_irq_mask_ack,
179         .mask   = ixp4xx_irq_mask,
180         .unmask = ixp4xx_irq_unmask,
181 };
182
183 void __init ixp4xx_init_irq(void)
184 {
185         int i = 0;
186
187         /* Route all sources to IRQ instead of FIQ */
188         *IXP4XX_ICLR = 0x0;
189
190         /* Disable all interrupt */
191         *IXP4XX_ICMR = 0x0; 
192
193         for(i = 0; i < NR_IRQS; i++)
194         {
195                 set_irq_chip(i, &ixp4xx_irq_chip);
196                 set_irq_handler(i, do_level_IRQ);
197                 set_irq_flags(i, IRQF_VALID);
198         }
199 }
200
201
202 /*************************************************************************
203  * IXP4xx timer tick
204  * We use OS timer1 on the CPU for the timer tick and the timestamp 
205  * counter as a source of real clock ticks to account for missed jiffies.
206  *************************************************************************/
207
208 static unsigned volatile last_jiffy_time;
209
210 #define CLOCK_TICKS_PER_USEC    (CLOCK_TICK_RATE / USEC_PER_SEC)
211
212 /* IRQs are disabled before entering here from do_gettimeofday() */
213 static unsigned long ixp4xx_gettimeoffset(void)
214 {
215         u32 elapsed;
216
217         elapsed = *IXP4XX_OSTS - last_jiffy_time;
218
219         return elapsed / CLOCK_TICKS_PER_USEC;
220 }
221
222 static irqreturn_t ixp4xx_timer_interrupt(int irq, void *dev_id, struct pt_regs *regs)
223 {
224         /* Clear Pending Interrupt by writing '1' to it */
225         *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
226
227         /*
228          * Catch up with the real idea of time
229          */
230         do {    
231                 timer_tick(regs);
232                 last_jiffy_time += LATCH;
233         } while((*IXP4XX_OSTS - last_jiffy_time) > LATCH);
234
235         return IRQ_HANDLED;
236 }
237
238 static struct irqaction ixp4xx_timer_irq = {
239         .name           = "IXP4xx Timer Tick",
240         .flags          = SA_INTERRUPT,
241         .handler        = ixp4xx_timer_interrupt
242 };
243
244 void __init ixp4xx_init_time(void)
245 {
246         gettimeoffset = ixp4xx_gettimeoffset;
247
248         /* Clear Pending Interrupt by writing '1' to it */
249         *IXP4XX_OSST = IXP4XX_OSST_TIMER_1_PEND;
250
251         /* Setup the Timer counter value */
252         *IXP4XX_OSRT1 = (LATCH & ~IXP4XX_OST_RELOAD_MASK) | IXP4XX_OST_ENABLE;
253
254         /* Reset time-stamp counter */
255         *IXP4XX_OSTS = 0;
256         last_jiffy_time = 0;
257
258         /* Connect the interrupt handler and enable the interrupt */
259         setup_irq(IRQ_IXP4XX_TIMER1, &ixp4xx_timer_irq);
260 }
261
262