2 * arch/arm/mach-imx/generic.c
5 * Created: april 20th, 2004
6 * Copyright: Synertronixx GmbH
8 * Common code for i.MX machines
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.
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.
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
25 #include <linux/platform_device.h>
26 #include <linux/init.h>
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/string.h>
31 #include <asm/errno.h>
32 #include <asm/arch/imxfb.h>
33 #include <asm/hardware.h>
34 #include <asm/arch/imx-regs.h>
36 #include <asm/mach/map.h>
37 #include <asm/arch/mmc.h>
38 #include <asm/arch/gpio.h>
40 unsigned long imx_gpio_alloc_map
[(GPIO_PORT_MAX
+ 1) * 32 / BITS_PER_LONG
];
42 void imx_gpio_mode(int gpio_mode
)
44 unsigned int pin
= gpio_mode
& GPIO_PIN_MASK
;
45 unsigned int port
= (gpio_mode
& GPIO_PORT_MASK
) >> GPIO_PORT_SHIFT
;
46 unsigned int ocr
= (gpio_mode
& GPIO_OCR_MASK
) >> GPIO_OCR_SHIFT
;
50 if(gpio_mode
& GPIO_PUEN
)
51 PUEN(port
) |= (1<<pin
);
53 PUEN(port
) &= ~(1<<pin
);
56 if(gpio_mode
& GPIO_OUT
)
59 DDIR(port
) &= ~(1<<pin
);
61 /* Primary / alternate function */
62 if(gpio_mode
& GPIO_AF
)
63 GPR(port
) |= (1<<pin
);
65 GPR(port
) &= ~(1<<pin
);
68 if(gpio_mode
& GPIO_GIUS
)
69 GIUS(port
) |= (1<<pin
);
71 GIUS(port
) &= ~(1<<pin
);
73 /* Output / input configuration */
74 /* FIXME: I'm not very sure about OCR and ICONF, someone
75 * should have a look over it
79 tmp
&= ~( 3<<(pin
*2));
80 tmp
|= (ocr
<< (pin
*2));
83 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);
89 tmp
&= ~( 3<<((pin
-16)*2));
90 tmp
|= (ocr
<< ((pin
-16)*2));
93 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);
100 EXPORT_SYMBOL(imx_gpio_mode
);
102 int imx_gpio_request(unsigned gpio
, const char *label
)
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
: "?");
110 if(test_and_set_bit(gpio
, imx_gpio_alloc_map
)) {
111 printk(KERN_ERR
"imx_gpio: GPIO %d already used. Allocation for \"%s\" failed\n",
112 gpio
, label
? label
: "?");
119 EXPORT_SYMBOL(imx_gpio_request
);
121 void imx_gpio_free(unsigned gpio
)
123 if(gpio
>= (GPIO_PORT_MAX
+ 1) * 32)
126 clear_bit(gpio
, imx_gpio_alloc_map
);
129 EXPORT_SYMBOL(imx_gpio_free
);
131 int imx_gpio_direction_input(unsigned gpio
)
133 imx_gpio_mode(gpio
| GPIO_IN
| GPIO_GIUS
| GPIO_DR
);
137 EXPORT_SYMBOL(imx_gpio_direction_input
);
139 int imx_gpio_direction_output(unsigned gpio
, int value
)
141 imx_gpio_set_value(gpio
, value
);
142 imx_gpio_mode(gpio
| GPIO_OUT
| GPIO_GIUS
| GPIO_DR
);
146 EXPORT_SYMBOL(imx_gpio_direction_output
);
148 int imx_gpio_setup_multiple_pins(const int *pin_list
, unsigned count
,
149 int alloc_mode
, const char *label
)
151 const int *p
= pin_list
;
156 for (i
= 0; i
< count
; i
++) {
157 gpio
= *p
& (GPIO_PIN_MASK
| GPIO_PORT_MASK
);
158 mode
= *p
& ~(GPIO_PIN_MASK
| GPIO_PORT_MASK
);
160 if (gpio
>= (GPIO_PORT_MAX
+ 1) * 32)
163 if (alloc_mode
& IMX_GPIO_ALLOC_MODE_RELEASE
)
165 else if (!(alloc_mode
& IMX_GPIO_ALLOC_MODE_NO_ALLOC
))
166 if (imx_gpio_request(gpio
, label
))
167 if (!(alloc_mode
& IMX_GPIO_ALLOC_MODE_TRY_ALLOC
))
170 if (!(alloc_mode
& (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY
|
171 IMX_GPIO_ALLOC_MODE_RELEASE
)))
172 imx_gpio_mode(gpio
| mode
);
179 if(alloc_mode
& (IMX_GPIO_ALLOC_MODE_NO_ALLOC
|
180 IMX_GPIO_ALLOC_MODE_TRY_ALLOC
))
183 while (p
!= pin_list
) {
185 gpio
= *p
& (GPIO_PIN_MASK
| GPIO_PORT_MASK
);
192 EXPORT_SYMBOL(imx_gpio_setup_multiple_pins
);
194 void __imx_gpio_set_value(unsigned gpio
, int value
)
196 imx_gpio_set_value_inline(gpio
, value
);
199 EXPORT_SYMBOL(__imx_gpio_set_value
);
201 int imx_gpio_to_irq(unsigned gpio
)
203 return IRQ_GPIOA(0) + gpio
;
206 EXPORT_SYMBOL(imx_gpio_to_irq
);
208 int imx_irq_to_gpio(unsigned irq
)
210 if (irq
< IRQ_GPIOA(0))
212 return irq
- IRQ_GPIOA(0);
215 EXPORT_SYMBOL(imx_irq_to_gpio
);
218 * get the system pll clock in Hz
220 * mfi + mfn / (mfd +1)
221 * f = 2 * f_ref * --------------------
224 static unsigned int imx_decode_pll(unsigned int pll
, u32 f_ref
)
226 unsigned long long ll
;
229 u32 mfi
= (pll
>> 10) & 0xf;
230 u32 mfn
= pll
& 0x3ff;
231 u32 mfd
= (pll
>> 16) & 0x3ff;
232 u32 pd
= (pll
>> 26) & 0xf;
234 mfi
= mfi
<= 5 ? 5 : mfi
;
236 ll
= 2 * (unsigned long long)f_ref
* ( (mfi
<<16) + (mfn
<<16) / (mfd
+1) );
237 quot
= (pd
+1) * (1<<16);
240 return (unsigned int) ll
;
243 unsigned int imx_get_system_clk(void)
245 u32 f_ref
= (CSCR
& CSCR_SYSTEM_SEL
) ? 16000000 : (CLK32
* 512);
247 return imx_decode_pll(SPCTL0
, f_ref
);
249 EXPORT_SYMBOL(imx_get_system_clk
);
251 unsigned int imx_get_mcu_clk(void)
253 return imx_decode_pll(MPCTL0
, CLK32
* 512);
255 EXPORT_SYMBOL(imx_get_mcu_clk
);
258 * get peripheral clock 1 ( UART[12], Timer[12], PWM )
260 unsigned int imx_get_perclk1(void)
262 return imx_get_system_clk() / (((PCDR
) & 0xf)+1);
264 EXPORT_SYMBOL(imx_get_perclk1
);
267 * get peripheral clock 2 ( LCD, SD, SPI[12] )
269 unsigned int imx_get_perclk2(void)
271 return imx_get_system_clk() / (((PCDR
>>4) & 0xf)+1);
273 EXPORT_SYMBOL(imx_get_perclk2
);
276 * get peripheral clock 3 ( SSI )
278 unsigned int imx_get_perclk3(void)
280 return imx_get_system_clk() / (((PCDR
>>16) & 0x7f)+1);
282 EXPORT_SYMBOL(imx_get_perclk3
);
285 * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
287 unsigned int imx_get_hclk(void)
289 return imx_get_system_clk() / (((CSCR
>>10) & 0xf)+1);
291 EXPORT_SYMBOL(imx_get_hclk
);
293 static struct resource imx_mmc_resources
[] = {
297 .flags
= IORESOURCE_MEM
,
302 .flags
= IORESOURCE_IRQ
,
306 static u64 imxmmmc_dmamask
= 0xffffffffUL
;
308 static struct platform_device imx_mmc_device
= {
312 .dma_mask
= &imxmmmc_dmamask
,
313 .coherent_dma_mask
= 0xffffffff,
315 .num_resources
= ARRAY_SIZE(imx_mmc_resources
),
316 .resource
= imx_mmc_resources
,
319 void __init
imx_set_mmc_info(struct imxmmc_platform_data
*info
)
321 imx_mmc_device
.dev
.platform_data
= info
;
324 static struct imxfb_mach_info imx_fb_info
;
326 void __init
set_imx_fb_info(struct imxfb_mach_info
*hard_imx_fb_info
)
328 memcpy(&imx_fb_info
,hard_imx_fb_info
,sizeof(struct imxfb_mach_info
));
330 EXPORT_SYMBOL(set_imx_fb_info
);
332 static struct resource imxfb_resources
[] = {
336 .flags
= IORESOURCE_MEM
,
341 .flags
= IORESOURCE_IRQ
,
345 static u64 fb_dma_mask
= ~(u64
)0;
347 static struct platform_device imxfb_device
= {
351 .platform_data
= &imx_fb_info
,
352 .dma_mask
= &fb_dma_mask
,
353 .coherent_dma_mask
= 0xffffffff,
355 .num_resources
= ARRAY_SIZE(imxfb_resources
),
356 .resource
= imxfb_resources
,
359 static struct platform_device
*devices
[] __initdata
= {
364 static struct map_desc imx_io_desc
[] __initdata
= {
366 .virtual = IMX_IO_BASE
,
367 .pfn
= __phys_to_pfn(IMX_IO_PHYS
),
368 .length
= IMX_IO_SIZE
,
376 iotable_init(imx_io_desc
, ARRAY_SIZE(imx_io_desc
));
379 static int __init
imx_init(void)
381 return platform_add_devices(devices
, ARRAY_SIZE(devices
));
384 subsys_initcall(imx_init
);