2 * Coldfire generic GPIO support
4 * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 #ifndef coldfire_gpio_h
17 #define coldfire_gpio_h
20 #include <asm-generic/gpio.h>
21 #include <asm/coldfire.h>
22 #include <asm/mcfsim.h>
25 * The Freescale Coldfire family is quite varied in how they implement GPIO.
26 * Some parts have 8 bit ports, some have 16bit and some have 32bit; some have
27 * only one port, others have multiple ports; some have a single data latch
28 * for both input and output, others have a separate pin data register to read
29 * input; some require a read-modify-write access to change an output, others
30 * have set and clear registers for some of the outputs; Some have all the
31 * GPIOs in a single control area, others have some GPIOs implemented in
34 * This implementation attempts accomodate the differences while presenting
35 * a generic interface that will optimize to as few instructions as possible.
37 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
38 defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
39 defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
41 /* These parts have GPIO organized by 8 bit ports */
43 #define MCFGPIO_PORTTYPE u8
44 #define MCFGPIO_PORTSIZE 8
45 #define mcfgpio_read(port) __raw_readb(port)
46 #define mcfgpio_write(data, port) __raw_writeb(data, port)
48 #elif defined(CONFIG_M5307) || defined(CONFIG_M5407) || defined(CONFIG_M5272)
50 /* These parts have GPIO organized by 16 bit ports */
52 #define MCFGPIO_PORTTYPE u16
53 #define MCFGPIO_PORTSIZE 16
54 #define mcfgpio_read(port) __raw_readw(port)
55 #define mcfgpio_write(data, port) __raw_writew(data, port)
57 #elif defined(CONFIG_M5249)
59 /* These parts have GPIO organized by 32 bit ports */
61 #define MCFGPIO_PORTTYPE u32
62 #define MCFGPIO_PORTSIZE 32
63 #define mcfgpio_read(port) __raw_readl(port)
64 #define mcfgpio_write(data, port) __raw_writel(data, port)
68 #define mcfgpio_bit(gpio) (1 << ((gpio) % MCFGPIO_PORTSIZE))
69 #define mcfgpio_port(gpio) ((gpio) / MCFGPIO_PORTSIZE)
71 #if defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
72 defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
74 * These parts have an 'Edge' Port module (external interrupt/GPIO) which uses
75 * read-modify-write to change an output and a GPIO module which has separate
76 * set/clr registers to directly change outputs with a single write access.
78 #if defined(CONFIG_M528x)
80 * The 528x also has GPIOs in other modules (GPT, QADC) which use
81 * read-modify-write as well as those controlled by the EPORT and GPIO modules.
83 #define MCFGPIO_SCR_START 40
85 #define MCFGPIO_SCR_START 8
88 #define MCFGPIO_SETR_PORT(gpio) (MCFGPIO_SETR + \
89 mcfgpio_port(gpio - MCFGPIO_SCR_START))
91 #define MCFGPIO_CLRR_PORT(gpio) (MCFGPIO_CLRR + \
92 mcfgpio_port(gpio - MCFGPIO_SCR_START))
95 #define MCFGPIO_SCR_START MCFGPIO_PIN_MAX
96 /* with MCFGPIO_SCR == MCFGPIO_PIN_MAX, these will be optimized away */
97 #define MCFGPIO_SETR_PORT(gpio) 0
98 #define MCFGPIO_CLRR_PORT(gpio) 0
102 * Coldfire specific helper functions
105 /* return the port pin data register for a gpio */
106 static inline u32
__mcf_gpio_ppdr(unsigned gpio
)
108 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
109 defined(CONFIG_M5307) || defined(CONFIG_M5407)
111 #elif defined(CONFIG_M5272)
118 #elif defined(CONFIG_M5249)
120 return MCFSIM2_GPIOREAD
;
122 return MCFSIM2_GPIO1READ
;
123 #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
124 defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
126 return MCFEPORT_EPPDR
;
127 #if defined(CONFIG_M528x)
129 return MCFGPTA_GPTPORT
;
131 return MCFGPTB_GPTPORT
;
133 return MCFQADC_PORTQA
;
135 return MCFQADC_PORTQB
;
138 return MCFGPIO_PPDR
+ mcfgpio_port(gpio
- MCFGPIO_SCR_START
);
142 /* return the port output data register for a gpio */
143 static inline u32
__mcf_gpio_podr(unsigned gpio
)
145 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
146 defined(CONFIG_M5307) || defined(CONFIG_M5407)
148 #elif defined(CONFIG_M5272)
155 #elif defined(CONFIG_M5249)
157 return MCFSIM2_GPIOWRITE
;
159 return MCFSIM2_GPIO1WRITE
;
160 #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
161 defined(CONFIG_M527x) || defined(CONFIG_M528x) || defined(CONFIG_M532x)
163 return MCFEPORT_EPDR
;
164 #if defined(CONFIG_M528x)
166 return MCFGPTA_GPTPORT
;
168 return MCFGPTB_GPTPORT
;
170 return MCFQADC_PORTQA
;
172 return MCFQADC_PORTQB
;
175 return MCFGPIO_PODR
+ mcfgpio_port(gpio
- MCFGPIO_SCR_START
);
180 * The Generic GPIO functions
182 * If the gpio is a compile time constant and is one of the Coldfire gpios,
183 * use the inline version, otherwise dispatch thru gpiolib.
186 static inline int gpio_get_value(unsigned gpio
)
188 if (__builtin_constant_p(gpio
) && gpio
< MCFGPIO_PIN_MAX
)
189 return mcfgpio_read(__mcf_gpio_ppdr(gpio
)) & mcfgpio_bit(gpio
);
191 return __gpio_get_value(gpio
);
194 static inline void gpio_set_value(unsigned gpio
, int value
)
196 if (__builtin_constant_p(gpio
) && gpio
< MCFGPIO_PIN_MAX
) {
197 if (gpio
< MCFGPIO_SCR_START
) {
199 MCFGPIO_PORTTYPE data
;
201 local_irq_save(flags
);
202 data
= mcfgpio_read(__mcf_gpio_podr(gpio
));
204 data
|= mcfgpio_bit(gpio
);
206 data
&= ~mcfgpio_bit(gpio
);
207 mcfgpio_write(data
, __mcf_gpio_podr(gpio
));
208 local_irq_restore(flags
);
211 mcfgpio_write(mcfgpio_bit(gpio
),
212 MCFGPIO_SETR_PORT(gpio
));
214 mcfgpio_write(~mcfgpio_bit(gpio
),
215 MCFGPIO_CLRR_PORT(gpio
));
218 __gpio_set_value(gpio
, value
);
221 static inline int gpio_to_irq(unsigned gpio
)
223 return (gpio
< MCFGPIO_IRQ_MAX
) ? gpio
+ MCFGPIO_IRQ_VECBASE
: -EINVAL
;
226 static inline int irq_to_gpio(unsigned irq
)
228 return (irq
>= MCFGPIO_IRQ_VECBASE
&&
229 irq
< (MCFGPIO_IRQ_VECBASE
+ MCFGPIO_IRQ_MAX
)) ?
230 irq
- MCFGPIO_IRQ_VECBASE
: -ENXIO
;
233 static inline int gpio_cansleep(unsigned gpio
)
235 return gpio
< MCFGPIO_PIN_MAX
? 0 : __gpio_cansleep(gpio
);