blob: a58b678006dff1ce50ef7ccaa86ffc1cc0b9fe1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * arch/arm/mach-imx/generic.c
3 *
4 * author: Sascha Hauer
5 * Created: april 20th, 2004
6 * Copyright: Synertronixx GmbH
7 *
8 * Common code for i.MX machines
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 *
24 */
Russell Kingd052d1b2005-10-29 19:07:23 +010025#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/kernel.h>
28#include <linux/module.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080029#include <linux/string.h>
30
Pavel Pisab3e6a502007-05-12 14:31:17 +010031#include <asm/errno.h>
Sascha Hauera4938202005-05-03 22:57:56 +010032#include <asm/arch/imxfb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/hardware.h>
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010034#include <asm/arch/imx-regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <asm/mach/map.h>
Pavel Pisa56ca90402006-04-02 19:27:07 +010037#include <asm/arch/mmc.h>
Pavel Pisab3e6a502007-05-12 14:31:17 +010038#include <asm/arch/gpio.h>
39
40unsigned long imx_gpio_alloc_map[(GPIO_PORT_MAX + 1) * 32 / BITS_PER_LONG];
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42void imx_gpio_mode(int gpio_mode)
43{
44 unsigned int pin = gpio_mode & GPIO_PIN_MASK;
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010045 unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
46 unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 unsigned int tmp;
48
49 /* Pullup enable */
50 if(gpio_mode & GPIO_PUEN)
51 PUEN(port) |= (1<<pin);
52 else
53 PUEN(port) &= ~(1<<pin);
54
55 /* Data direction */
56 if(gpio_mode & GPIO_OUT)
57 DDIR(port) |= 1<<pin;
58 else
59 DDIR(port) &= ~(1<<pin);
60
61 /* Primary / alternate function */
62 if(gpio_mode & GPIO_AF)
63 GPR(port) |= (1<<pin);
64 else
65 GPR(port) &= ~(1<<pin);
66
67 /* use as gpio? */
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010068 if(gpio_mode & GPIO_GIUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 GIUS(port) |= (1<<pin);
70 else
71 GIUS(port) &= ~(1<<pin);
72
73 /* Output / input configuration */
74 /* FIXME: I'm not very sure about OCR and ICONF, someone
75 * should have a look over it
76 */
77 if(pin<16) {
78 tmp = OCR1(port);
79 tmp &= ~( 3<<(pin*2));
80 tmp |= (ocr << (pin*2));
81 OCR1(port) = tmp;
82
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010083 ICONFA1(port) &= ~( 3<<(pin*2));
84 ICONFA1(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2);
85 ICONFB1(port) &= ~( 3<<(pin*2));
86 ICONFB1(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 } else {
88 tmp = OCR2(port);
89 tmp &= ~( 3<<((pin-16)*2));
90 tmp |= (ocr << ((pin-16)*2));
91 OCR2(port) = tmp;
92
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010093 ICONFA2(port) &= ~( 3<<((pin-16)*2));
94 ICONFA2(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << ((pin-16) * 2);
95 ICONFB2(port) &= ~( 3<<((pin-16)*2));
96 ICONFB2(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << ((pin-16) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98}
99
100EXPORT_SYMBOL(imx_gpio_mode);
101
Pavel Pisab3e6a502007-05-12 14:31:17 +0100102int imx_gpio_request(unsigned gpio, const char *label)
103{
104 if(gpio >= (GPIO_PORT_MAX + 1) * 32)
105 printk(KERN_ERR "imx_gpio: Attempt to request nonexistent GPIO %d for \"%s\"\n",
106 gpio, label ? label : "?");
107 return -EINVAL;
108
109 if(test_and_set_bit(gpio, imx_gpio_alloc_map)) {
110 printk(KERN_ERR "imx_gpio: GPIO %d already used. Allocation for \"%s\" failed\n",
111 gpio, label ? label : "?");
112 return -EBUSY;
113 }
114
115 return 0;
116}
117
118EXPORT_SYMBOL(imx_gpio_request);
119
120void imx_gpio_free(unsigned gpio)
121{
122 if(gpio >= (GPIO_PORT_MAX + 1) * 32)
123 return;
124
125 clear_bit(gpio, imx_gpio_alloc_map);
126}
127
128EXPORT_SYMBOL(imx_gpio_free);
129
130int imx_gpio_direction_input(unsigned gpio)
131{
132 imx_gpio_mode(gpio| GPIO_IN);
133 return 0;
134}
135
136EXPORT_SYMBOL(imx_gpio_direction_input);
137
138int imx_gpio_direction_output(unsigned gpio, int value)
139{
140 imx_gpio_set_value(gpio, value);
141 imx_gpio_mode(gpio| GPIO_OUT);
142 return 0;
143}
144
145EXPORT_SYMBOL(imx_gpio_direction_output);
146
147int imx_gpio_setup_multiple_pins(const int *pin_list, unsigned count,
148 int alloc_mode, const char *label)
149{
150 const int *p = pin_list;
151 int i;
152 unsigned gpio;
153 unsigned mode;
154
155 for (i = 0; i < count; i++) {
156 gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
157 mode = *p & ~(GPIO_PIN_MASK | GPIO_PORT_MASK);
158
159 if (gpio >= (GPIO_PORT_MAX + 1) * 32)
160 goto setup_error;
161
162 if (alloc_mode & IMX_GPIO_ALLOC_MODE_RELEASE)
163 imx_gpio_free(gpio);
164 else if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_NO_ALLOC))
165 if (imx_gpio_request(gpio, label))
166 if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
167 goto setup_error;
168
169 if (!(alloc_mode & (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY |
170 IMX_GPIO_ALLOC_MODE_RELEASE)))
171 imx_gpio_mode(gpio | mode);
172
173 p++;
174 }
175 return 0;
176
177setup_error:
178 if(alloc_mode & (IMX_GPIO_ALLOC_MODE_NO_ALLOC |
179 IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
180 return -EINVAL;
181
182 while (p != pin_list) {
183 p--;
184 gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
185 imx_gpio_free(gpio);
186 }
187
188 return -EINVAL;
189}
190
191EXPORT_SYMBOL(imx_gpio_setup_multiple_pins);
192
193void __imx_gpio_set_value(unsigned gpio, int value)
194{
195 imx_gpio_set_value_inline(gpio, value);
196}
197
198EXPORT_SYMBOL(__imx_gpio_set_value);
199
200int imx_gpio_to_irq(unsigned gpio)
201{
202 return IRQ_GPIOA(0) + gpio;
203}
204
205EXPORT_SYMBOL(imx_gpio_to_irq);
206
207int imx_irq_to_gpio(unsigned irq)
208{
209 if (irq < IRQ_GPIOA(0))
210 return -EINVAL;
211 return irq - IRQ_GPIOA(0);
212}
213
214EXPORT_SYMBOL(imx_irq_to_gpio);
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * get the system pll clock in Hz
218 *
219 * mfi + mfn / (mfd +1)
220 * f = 2 * f_ref * --------------------
221 * pd + 1
222 */
Pavel Pisab3c6b762007-03-07 23:56:16 +0100223static unsigned int imx_decode_pll(unsigned int pll, u32 f_ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Pavel Pisa5c894cd2006-12-06 17:08:27 +0100225 unsigned long long ll;
226 unsigned long quot;
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 u32 mfi = (pll >> 10) & 0xf;
229 u32 mfn = pll & 0x3ff;
230 u32 mfd = (pll >> 16) & 0x3ff;
231 u32 pd = (pll >> 26) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 mfi = mfi <= 5 ? 5 : mfi;
234
Pavel Pisa5c894cd2006-12-06 17:08:27 +0100235 ll = 2 * (unsigned long long)f_ref * ( (mfi<<16) + (mfn<<16) / (mfd+1) );
236 quot = (pd+1) * (1<<16);
237 ll += quot / 2;
238 do_div(ll, quot);
239 return (unsigned int) ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
242unsigned int imx_get_system_clk(void)
243{
Pavel Pisab3c6b762007-03-07 23:56:16 +0100244 u32 f_ref = (CSCR & CSCR_SYSTEM_SEL) ? 16000000 : (CLK32 * 512);
245
246 return imx_decode_pll(SPCTL0, f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248EXPORT_SYMBOL(imx_get_system_clk);
249
250unsigned int imx_get_mcu_clk(void)
251{
Pavel Pisab3c6b762007-03-07 23:56:16 +0100252 return imx_decode_pll(MPCTL0, CLK32 * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254EXPORT_SYMBOL(imx_get_mcu_clk);
255
256/*
257 * get peripheral clock 1 ( UART[12], Timer[12], PWM )
258 */
259unsigned int imx_get_perclk1(void)
260{
261 return imx_get_system_clk() / (((PCDR) & 0xf)+1);
262}
263EXPORT_SYMBOL(imx_get_perclk1);
264
265/*
266 * get peripheral clock 2 ( LCD, SD, SPI[12] )
267 */
268unsigned int imx_get_perclk2(void)
269{
270 return imx_get_system_clk() / (((PCDR>>4) & 0xf)+1);
271}
272EXPORT_SYMBOL(imx_get_perclk2);
273
274/*
275 * get peripheral clock 3 ( SSI )
276 */
277unsigned int imx_get_perclk3(void)
278{
279 return imx_get_system_clk() / (((PCDR>>16) & 0x7f)+1);
280}
281EXPORT_SYMBOL(imx_get_perclk3);
282
283/*
284 * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
285 */
286unsigned int imx_get_hclk(void)
287{
288 return imx_get_system_clk() / (((CSCR>>10) & 0xf)+1);
289}
290EXPORT_SYMBOL(imx_get_hclk);
291
292static struct resource imx_mmc_resources[] = {
293 [0] = {
294 .start = 0x00214000,
295 .end = 0x002140FF,
296 .flags = IORESOURCE_MEM,
297 },
298 [1] = {
299 .start = (SDHC_INT),
300 .end = (SDHC_INT),
301 .flags = IORESOURCE_IRQ,
302 },
303};
304
Pavel Pisa56ca90402006-04-02 19:27:07 +0100305static u64 imxmmmc_dmamask = 0xffffffffUL;
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static struct platform_device imx_mmc_device = {
308 .name = "imx-mmc",
309 .id = 0,
Pavel Pisa56ca90402006-04-02 19:27:07 +0100310 .dev = {
311 .dma_mask = &imxmmmc_dmamask,
312 .coherent_dma_mask = 0xffffffff,
313 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 .num_resources = ARRAY_SIZE(imx_mmc_resources),
315 .resource = imx_mmc_resources,
316};
317
Pavel Pisa56ca90402006-04-02 19:27:07 +0100318void __init imx_set_mmc_info(struct imxmmc_platform_data *info)
319{
320 imx_mmc_device.dev.platform_data = info;
321}
Pavel Pisa56ca90402006-04-02 19:27:07 +0100322
Sascha Hauera4938202005-05-03 22:57:56 +0100323static struct imxfb_mach_info imx_fb_info;
324
325void __init set_imx_fb_info(struct imxfb_mach_info *hard_imx_fb_info)
326{
327 memcpy(&imx_fb_info,hard_imx_fb_info,sizeof(struct imxfb_mach_info));
328}
329EXPORT_SYMBOL(set_imx_fb_info);
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static struct resource imxfb_resources[] = {
332 [0] = {
333 .start = 0x00205000,
334 .end = 0x002050FF,
335 .flags = IORESOURCE_MEM,
336 },
337 [1] = {
338 .start = LCDC_INT,
339 .end = LCDC_INT,
340 .flags = IORESOURCE_IRQ,
341 },
342};
343
Sascha Hauera4938202005-05-03 22:57:56 +0100344static u64 fb_dma_mask = ~(u64)0;
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static struct platform_device imxfb_device = {
347 .name = "imx-fb",
348 .id = 0,
Sascha Hauera4938202005-05-03 22:57:56 +0100349 .dev = {
350 .platform_data = &imx_fb_info,
351 .dma_mask = &fb_dma_mask,
352 .coherent_dma_mask = 0xffffffff,
353 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .num_resources = ARRAY_SIZE(imxfb_resources),
355 .resource = imxfb_resources,
356};
357
358static struct platform_device *devices[] __initdata = {
359 &imx_mmc_device,
360 &imxfb_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
363static struct map_desc imx_io_desc[] __initdata = {
Deepak Saxenaadecef72005-10-28 15:19:10 +0100364 {
365 .virtual = IMX_IO_BASE,
366 .pfn = __phys_to_pfn(IMX_IO_PHYS),
367 .length = IMX_IO_SIZE,
368 .type = MT_DEVICE
369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370};
371
372void __init
373imx_map_io(void)
374{
375 iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc));
376}
377
378static int __init imx_init(void)
379{
380 return platform_add_devices(devices, ARRAY_SIZE(devices));
381}
382
383subsys_initcall(imx_init);