Merge branch 'fixes' into cleanups
[platform/kernel/u-boot.git] / include / asm-avr32 / arch-common / portmux-gpio.h
1 /*
2  * Copyright (C) 2008 Atmel Corporation
3  *
4  * See file CREDITS for list of people who contributed to this
5  * project.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  */
22 #ifndef __AVR32_PORTMUX_GPIO_H__
23 #define __AVR32_PORTMUX_GPIO_H__
24
25 #include <asm/io.h>
26
27 /* Register offsets */
28 struct gpio_regs {
29         u32     GPER;
30         u32     GPERS;
31         u32     GPERC;
32         u32     GPERT;
33         u32     PMR0;
34         u32     PMR0S;
35         u32     PMR0C;
36         u32     PMR0T;
37         u32     PMR1;
38         u32     PMR1S;
39         u32     PMR1C;
40         u32     PMR1T;
41         u32     __reserved0[4];
42         u32     ODER;
43         u32     ODERS;
44         u32     ODERC;
45         u32     ODERT;
46         u32     OVR;
47         u32     OVRS;
48         u32     OVRC;
49         u32     OVRT;
50         u32     PVR;
51         u32     __reserved_PVRS;
52         u32     __reserved_PVRC;
53         u32     __reserved_PVRT;
54         u32     PUER;
55         u32     PUERS;
56         u32     PUERC;
57         u32     PUERT;
58         u32     PDER;
59         u32     PDERS;
60         u32     PDERC;
61         u32     PDERT;
62         u32     IER;
63         u32     IERS;
64         u32     IERC;
65         u32     IERT;
66         u32     IMR0;
67         u32     IMR0S;
68         u32     IMR0C;
69         u32     IMR0T;
70         u32     IMR1;
71         u32     IMR1S;
72         u32     IMR1C;
73         u32     IMR1T;
74         u32     GFER;
75         u32     GFERS;
76         u32     GFERC;
77         u32     GFERT;
78         u32     IFR;
79         u32     __reserved_IFRS;
80         u32     IFRC;
81         u32     __reserved_IFRT;
82         u32     ODMER;
83         u32     ODMERS;
84         u32     ODMERC;
85         u32     ODMERT;
86         u32     __reserved1[4];
87         u32     ODCR0;
88         u32     ODCR0S;
89         u32     ODCR0C;
90         u32     ODCR0T;
91         u32     ODCR1;
92         u32     ODCR1S;
93         u32     ODCR1C;
94         u32     ODCR1T;
95         u32     __reserved2[4];
96         u32     OSRR0;
97         u32     OSRR0S;
98         u32     OSRR0C;
99         u32     OSRR0T;
100         u32     __reserved3[8];
101         u32     STER;
102         u32     STERS;
103         u32     STERC;
104         u32     STERT;
105         u32     __reserved4[35];
106         u32     VERSION;
107 };
108
109 /* Register access macros */
110 #define gpio_readl(port, reg)                                           \
111         __raw_readl(&((struct gpio_regs *)port)->reg)
112 #define gpio_writel(gpio, reg, value)                                   \
113         __raw_writel(value, &((struct gpio_regs *)port)->reg)
114
115 /* Portmux API starts here. See doc/README.AVR32-port-muxing */
116
117 enum portmux_function {
118         PORTMUX_FUNC_A,
119         PORTMUX_FUNC_B,
120         PORTMUX_FUNC_C,
121         PORTMUX_FUNC_D,
122 };
123
124 #define PORTMUX_DIR_INPUT       (0 << 0)
125 #define PORTMUX_DIR_OUTPUT      (1 << 0)
126 #define PORTMUX_INIT_LOW        (0 << 1)
127 #define PORTMUX_INIT_HIGH       (1 << 1)
128 #define PORTMUX_PULL_UP         (1 << 2)
129 #define PORTMUX_PULL_DOWN       (2 << 2)
130 #define PORTMUX_BUSKEEPER       (3 << 2)
131 #define PORTMUX_DRIVE_MIN       (0 << 4)
132 #define PORTMUX_DRIVE_LOW       (1 << 4)
133 #define PORTMUX_DRIVE_HIGH      (2 << 4)
134 #define PORTMUX_DRIVE_MAX       (3 << 4)
135 #define PORTMUX_OPEN_DRAIN      (1 << 6)
136
137 void portmux_select_peripheral(void *port, unsigned long pin_mask,
138                 enum portmux_function func, unsigned long flags);
139 void portmux_select_gpio(void *port, unsigned long pin_mask,
140                 unsigned long flags);
141
142 /* Internal helper functions */
143
144 static inline void *gpio_pin_to_port(unsigned int pin)
145 {
146         return (void *)GPIO_BASE + (pin >> 5) * 0x200;
147 }
148
149 static inline void __gpio_set_output_value(void *port, unsigned int pin,
150                 int value)
151 {
152         if (value)
153                 gpio_writel(port, OVRS, 1 << pin);
154         else
155                 gpio_writel(port, OVRC, 1 << pin);
156 }
157
158 static inline int __gpio_get_input_value(void *port, unsigned int pin)
159 {
160         return (gpio_readl(port, PVR) >> pin) & 1;
161 }
162
163 void gpio_set_output_value(unsigned int pin, int value);
164 int gpio_get_input_value(unsigned int pin);
165
166 /* GPIO API starts here */
167
168 /*
169  * GCC doesn't realize that the constant case is extremely trivial,
170  * so we need to help it make the right decision by using
171  * always_inline.
172  */
173 __attribute__((always_inline))
174 static inline void gpio_set_value(unsigned int pin, int value)
175 {
176         if (__builtin_constant_p(pin))
177                 __gpio_set_output_value(gpio_pin_to_port(pin),
178                                 pin & 0x1f, value);
179         else
180                 gpio_set_output_value(pin, value);
181 }
182
183 __attribute__((always_inline))
184 static inline int gpio_get_value(unsigned int pin)
185 {
186         if (__builtin_constant_p(pin))
187                 return __gpio_get_input_value(gpio_pin_to_port(pin),
188                                 pin & 0x1f);
189         else
190                 return gpio_get_input_value(pin);
191 }
192
193 #endif /* __AVR32_PORTMUX_GPIO_H__ */