davinci: add arch_ioremap() which uses existing static mappings
[linux-2.6] / arch / arm / mach-davinci / include / mach / gpio.h
1 /*
2  * TI DaVinci GPIO Support
3  *
4  * Copyright (c) 2006 David Brownell
5  * Copyright (c) 2007, MontaVista Software, Inc. <source@mvista.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  */
12
13 #ifndef __DAVINCI_GPIO_H
14 #define __DAVINCI_GPIO_H
15
16 #include <linux/io.h>
17 #include <asm-generic/gpio.h>
18
19 #include <mach/irqs.h>
20
21 #define DAVINCI_GPIO_BASE 0x01C67000
22
23 /*
24  * basic gpio routines
25  *
26  * board-specific init should be done by arch/.../.../board-XXX.c (maybe
27  * initializing banks together) rather than boot loaders; kexec() won't
28  * go through boot loaders.
29  *
30  * the gpio clock will be turned on when gpios are used, and you may also
31  * need to pay attention to PINMUX0 and PINMUX1 to be sure those pins are
32  * used as gpios, not with other peripherals.
33  *
34  * On-chip GPIOs are numbered 0..(DAVINCI_N_GPIO-1).  For documentation,
35  * and maybe for later updates, code should write GPIO(N) or:
36  *  - GPIOV18(N) for 1.8V pins, N in 0..53; same as GPIO(0)..GPIO(53)
37  *  - GPIOV33(N) for 3.3V pins, N in 0..17; same as GPIO(54)..GPIO(70)
38  *
39  * For GPIO IRQs use gpio_to_irq(GPIO(N)) or gpio_to_irq(GPIOV33(N)) etc
40  * for now, that's != GPIO(N)
41  *
42  * GPIOs can also be on external chips, numbered after the ones built-in
43  * to the DaVinci chip.  For now, they won't be usable as IRQ sources.
44  */
45 #define GPIO(X)         (X)             /* 0 <= X <= 70 */
46 #define GPIOV18(X)      (X)             /* 1.8V i/o; 0 <= X <= 53 */
47 #define GPIOV33(X)      ((X)+54)        /* 3.3V i/o; 0 <= X <= 17 */
48
49 struct gpio_controller {
50         u32     dir;
51         u32     out_data;
52         u32     set_data;
53         u32     clr_data;
54         u32     in_data;
55         u32     set_rising;
56         u32     clr_rising;
57         u32     set_falling;
58         u32     clr_falling;
59         u32     intstat;
60 };
61
62 /* The __gpio_to_controller() and __gpio_mask() functions inline to constants
63  * with constant parameters; or in outlined code they execute at runtime.
64  *
65  * You'd access the controller directly when reading or writing more than
66  * one gpio value at a time, and to support wired logic where the value
67  * being driven by the cpu need not match the value read back.
68  *
69  * These are NOT part of the cross-platform GPIO interface
70  */
71 static inline struct gpio_controller *__iomem
72 __gpio_to_controller(unsigned gpio)
73 {
74         void *__iomem ptr;
75
76         if (gpio < 32)
77                 ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x10);
78         else if (gpio < 64)
79                 ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x38);
80         else if (gpio < DAVINCI_N_GPIO)
81                 ptr = IO_ADDRESS(DAVINCI_GPIO_BASE + 0x60);
82         else
83                 ptr = NULL;
84         return ptr;
85 }
86
87 static inline u32 __gpio_mask(unsigned gpio)
88 {
89         return 1 << (gpio % 32);
90 }
91
92 /* The get/set/clear functions will inline when called with constant
93  * parameters referencing built-in GPIOs, for low-overhead bitbanging.
94  *
95  * Otherwise, calls with variable parameters or referencing external
96  * GPIOs (e.g. on GPIO expander chips) use outlined functions.
97  */
98 static inline void gpio_set_value(unsigned gpio, int value)
99 {
100         if (__builtin_constant_p(value) && gpio < DAVINCI_N_GPIO) {
101                 struct gpio_controller  *__iomem g;
102                 u32                     mask;
103
104                 g = __gpio_to_controller(gpio);
105                 mask = __gpio_mask(gpio);
106                 if (value)
107                         __raw_writel(mask, &g->set_data);
108                 else
109                         __raw_writel(mask, &g->clr_data);
110                 return;
111         }
112
113         __gpio_set_value(gpio, value);
114 }
115
116 /* Returns zero or nonzero; works for gpios configured as inputs OR
117  * as outputs, at least for built-in GPIOs.
118  *
119  * NOTE: for built-in GPIOs, changes in reported values are synchronized
120  * to the GPIO clock.  This is easily seen after calling gpio_set_value()
121  * and then immediately gpio_get_value(), where the gpio_get_value() will
122  * return the old value until the GPIO clock ticks and the new value gets
123  * latched.
124  */
125 static inline int gpio_get_value(unsigned gpio)
126 {
127         struct gpio_controller  *__iomem g;
128
129         if (!__builtin_constant_p(gpio) || gpio >= DAVINCI_N_GPIO)
130                 return __gpio_get_value(gpio);
131
132         g = __gpio_to_controller(gpio);
133         return __gpio_mask(gpio) & __raw_readl(&g->in_data);
134 }
135
136 static inline int gpio_cansleep(unsigned gpio)
137 {
138         if (__builtin_constant_p(gpio) && gpio < DAVINCI_N_GPIO)
139                 return 0;
140         else
141                 return __gpio_cansleep(gpio);
142 }
143
144 static inline int gpio_to_irq(unsigned gpio)
145 {
146         if (gpio >= DAVINCI_N_GPIO)
147                 return -EINVAL;
148         return DAVINCI_N_AINTC_IRQ + gpio;
149 }
150
151 static inline int irq_to_gpio(unsigned irq)
152 {
153         /* caller guarantees gpio_to_irq() succeeded */
154         return irq - DAVINCI_N_AINTC_IRQ;
155 }
156
157 #endif                          /* __DAVINCI_GPIO_H */