1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Coldfire generic GPIO support.
5 * (C) Copyright 2009, Steven King <sfking@fdwdc.com>
11 int __mcfgpio_get_value(unsigned gpio);
12 void __mcfgpio_set_value(unsigned gpio, int value);
13 int __mcfgpio_direction_input(unsigned gpio);
14 int __mcfgpio_direction_output(unsigned gpio, int value);
15 int __mcfgpio_request(unsigned gpio);
16 void __mcfgpio_free(unsigned gpio);
19 #include <linux/gpio.h>
22 /* our alternate 'gpiolib' functions */
23 static inline int __gpio_get_value(unsigned gpio)
25 if (gpio < MCFGPIO_PIN_MAX)
26 return __mcfgpio_get_value(gpio);
31 static inline void __gpio_set_value(unsigned gpio, int value)
33 if (gpio < MCFGPIO_PIN_MAX)
34 __mcfgpio_set_value(gpio, value);
37 static inline int __gpio_to_irq(unsigned gpio)
42 static inline int gpio_direction_input(unsigned gpio)
44 if (gpio < MCFGPIO_PIN_MAX)
45 return __mcfgpio_direction_input(gpio);
50 static inline int gpio_direction_output(unsigned gpio, int value)
52 if (gpio < MCFGPIO_PIN_MAX)
53 return __mcfgpio_direction_output(gpio, value);
58 static inline int gpio_request(unsigned gpio, const char *label)
60 if (gpio < MCFGPIO_PIN_MAX)
61 return __mcfgpio_request(gpio);
66 static inline void gpio_free(unsigned gpio)
68 if (gpio < MCFGPIO_PIN_MAX)
72 #endif /* CONFIG_GPIOLIB */
76 * The Freescale Coldfire family is quite varied in how they implement GPIO.
77 * Some parts have 8 bit ports, some have 16bit and some have 32bit; some have
78 * only one port, others have multiple ports; some have a single data latch
79 * for both input and output, others have a separate pin data register to read
80 * input; some require a read-modify-write access to change an output, others
81 * have set and clear registers for some of the outputs; Some have all the
82 * GPIOs in a single control area, others have some GPIOs implemented in
85 * This implementation attempts accommodate the differences while presenting
86 * a generic interface that will optimize to as few instructions as possible.
88 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
89 defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
90 defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
91 defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
92 defined(CONFIG_M5441x)
94 /* These parts have GPIO organized by 8 bit ports */
96 #define MCFGPIO_PORTTYPE u8
97 #define MCFGPIO_PORTSIZE 8
98 #define mcfgpio_read(port) __raw_readb(port)
99 #define mcfgpio_write(data, port) __raw_writeb(data, port)
101 #elif defined(CONFIG_M5307) || defined(CONFIG_M5407) || defined(CONFIG_M5272)
103 /* These parts have GPIO organized by 16 bit ports */
105 #define MCFGPIO_PORTTYPE u16
106 #define MCFGPIO_PORTSIZE 16
107 #define mcfgpio_read(port) __raw_readw(port)
108 #define mcfgpio_write(data, port) __raw_writew(data, port)
110 #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
112 /* These parts have GPIO organized by 32 bit ports */
114 #define MCFGPIO_PORTTYPE u32
115 #define MCFGPIO_PORTSIZE 32
116 #define mcfgpio_read(port) __raw_readl(port)
117 #define mcfgpio_write(data, port) __raw_writel(data, port)
121 #define mcfgpio_bit(gpio) (1 << ((gpio) % MCFGPIO_PORTSIZE))
122 #define mcfgpio_port(gpio) ((gpio) / MCFGPIO_PORTSIZE)
124 #if defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
125 defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
126 defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
127 defined(CONFIG_M5441x)
129 * These parts have an 'Edge' Port module (external interrupt/GPIO) which uses
130 * read-modify-write to change an output and a GPIO module which has separate
131 * set/clr registers to directly change outputs with a single write access.
133 #if defined(CONFIG_M528x)
135 * The 528x also has GPIOs in other modules (GPT, QADC) which use
136 * read-modify-write as well as those controlled by the EPORT and GPIO modules.
138 #define MCFGPIO_SCR_START 40
139 #elif defined(CONFIGM5441x)
140 /* The m5441x EPORT doesn't have its own GPIO port, uses PORT C */
141 #define MCFGPIO_SCR_START 0
143 #define MCFGPIO_SCR_START 8
146 #define MCFGPIO_SETR_PORT(gpio) (MCFGPIO_SETR + \
147 mcfgpio_port(gpio - MCFGPIO_SCR_START))
149 #define MCFGPIO_CLRR_PORT(gpio) (MCFGPIO_CLRR + \
150 mcfgpio_port(gpio - MCFGPIO_SCR_START))
153 #define MCFGPIO_SCR_START MCFGPIO_PIN_MAX
154 /* with MCFGPIO_SCR == MCFGPIO_PIN_MAX, these will be optimized away */
155 #define MCFGPIO_SETR_PORT(gpio) 0
156 #define MCFGPIO_CLRR_PORT(gpio) 0
160 * Coldfire specific helper functions
163 /* return the port pin data register for a gpio */
164 static inline u32 __mcfgpio_ppdr(unsigned gpio)
166 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
167 defined(CONFIG_M5307) || defined(CONFIG_M5407)
169 #elif defined(CONFIG_M5272)
176 #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
178 return MCFSIM2_GPIOREAD;
180 return MCFSIM2_GPIO1READ;
181 #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
182 defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
183 defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
184 defined(CONFIG_M5441x)
185 #if !defined(CONFIG_M5441x)
187 return MCFEPORT_EPPDR;
188 #if defined(CONFIG_M528x)
190 return MCFGPTA_GPTPORT;
192 return MCFGPTB_GPTPORT;
194 return MCFQADC_PORTQA;
196 return MCFQADC_PORTQB;
197 #endif /* defined(CONFIG_M528x) */
199 #endif /* !defined(CONFIG_M5441x) */
200 return MCFGPIO_PPDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
206 /* return the port output data register for a gpio */
207 static inline u32 __mcfgpio_podr(unsigned gpio)
209 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
210 defined(CONFIG_M5307) || defined(CONFIG_M5407)
212 #elif defined(CONFIG_M5272)
219 #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
221 return MCFSIM2_GPIOWRITE;
223 return MCFSIM2_GPIO1WRITE;
224 #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
225 defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
226 defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
227 defined(CONFIG_M5441x)
228 #if !defined(CONFIG_M5441x)
230 return MCFEPORT_EPDR;
231 #if defined(CONFIG_M528x)
233 return MCFGPTA_GPTPORT;
235 return MCFGPTB_GPTPORT;
237 return MCFQADC_PORTQA;
239 return MCFQADC_PORTQB;
240 #endif /* defined(CONFIG_M528x) */
242 #endif /* !defined(CONFIG_M5441x) */
243 return MCFGPIO_PODR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
249 /* return the port direction data register for a gpio */
250 static inline u32 __mcfgpio_pddr(unsigned gpio)
252 #if defined(CONFIG_M5206) || defined(CONFIG_M5206e) || \
253 defined(CONFIG_M5307) || defined(CONFIG_M5407)
255 #elif defined(CONFIG_M5272)
262 #elif defined(CONFIG_M5249) || defined(CONFIG_M525x)
264 return MCFSIM2_GPIOENABLE;
266 return MCFSIM2_GPIO1ENABLE;
267 #elif defined(CONFIG_M520x) || defined(CONFIG_M523x) || \
268 defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
269 defined(CONFIG_M53xx) || defined(CONFIG_M54xx) || \
270 defined(CONFIG_M5441x)
271 #if !defined(CONFIG_M5441x)
273 return MCFEPORT_EPDDR;
274 #if defined(CONFIG_M528x)
276 return MCFGPTA_GPTDDR;
278 return MCFGPTB_GPTDDR;
280 return MCFQADC_DDRQA;
282 return MCFQADC_DDRQB;
283 #endif /* defined(CONFIG_M528x) */
285 #endif /* !defined(CONFIG_M5441x) */
286 return MCFGPIO_PDDR + mcfgpio_port(gpio - MCFGPIO_SCR_START);
292 #endif /* mcfgpio_h */