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 : "?");
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 : "?");
118 EXPORT_SYMBOL(imx_gpio_request);
120 void imx_gpio_free(unsigned gpio)
122 if(gpio >= (GPIO_PORT_MAX + 1) * 32)
125 clear_bit(gpio, imx_gpio_alloc_map);
128 EXPORT_SYMBOL(imx_gpio_free);
130 int imx_gpio_direction_input(unsigned gpio)
132 imx_gpio_mode(gpio| GPIO_IN);
136 EXPORT_SYMBOL(imx_gpio_direction_input);
138 int imx_gpio_direction_output(unsigned gpio, int value)
140 imx_gpio_set_value(gpio, value);
141 imx_gpio_mode(gpio| GPIO_OUT);
145 EXPORT_SYMBOL(imx_gpio_direction_output);
147 int imx_gpio_setup_multiple_pins(const int *pin_list, unsigned count,
148 int alloc_mode, const char *label)
150 const int *p = pin_list;
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);
159 if (gpio >= (GPIO_PORT_MAX + 1) * 32)
162 if (alloc_mode & IMX_GPIO_ALLOC_MODE_RELEASE)
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))
169 if (!(alloc_mode & (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY |
170 IMX_GPIO_ALLOC_MODE_RELEASE)))
171 imx_gpio_mode(gpio | mode);
178 if(alloc_mode & (IMX_GPIO_ALLOC_MODE_NO_ALLOC |
179 IMX_GPIO_ALLOC_MODE_TRY_ALLOC))
182 while (p != pin_list) {
184 gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK);
191 EXPORT_SYMBOL(imx_gpio_setup_multiple_pins);
193 void __imx_gpio_set_value(unsigned gpio, int value)
195 imx_gpio_set_value_inline(gpio, value);
198 EXPORT_SYMBOL(__imx_gpio_set_value);
200 int imx_gpio_to_irq(unsigned gpio)
202 return IRQ_GPIOA(0) + gpio;
205 EXPORT_SYMBOL(imx_gpio_to_irq);
207 int imx_irq_to_gpio(unsigned irq)
209 if (irq < IRQ_GPIOA(0))
211 return irq - IRQ_GPIOA(0);
214 EXPORT_SYMBOL(imx_irq_to_gpio);
217 * get the system pll clock in Hz
219 * mfi + mfn / (mfd +1)
220 * f = 2 * f_ref * --------------------
223 static unsigned int imx_decode_pll(unsigned int pll, u32 f_ref)
225 unsigned long long ll;
228 u32 mfi = (pll >> 10) & 0xf;
229 u32 mfn = pll & 0x3ff;
230 u32 mfd = (pll >> 16) & 0x3ff;
231 u32 pd = (pll >> 26) & 0xf;
233 mfi = mfi <= 5 ? 5 : mfi;
235 ll = 2 * (unsigned long long)f_ref * ( (mfi<<16) + (mfn<<16) / (mfd+1) );
236 quot = (pd+1) * (1<<16);
239 return (unsigned int) ll;
242 unsigned int imx_get_system_clk(void)
244 u32 f_ref = (CSCR & CSCR_SYSTEM_SEL) ? 16000000 : (CLK32 * 512);
246 return imx_decode_pll(SPCTL0, f_ref);
248 EXPORT_SYMBOL(imx_get_system_clk);
250 unsigned int imx_get_mcu_clk(void)
252 return imx_decode_pll(MPCTL0, CLK32 * 512);
254 EXPORT_SYMBOL(imx_get_mcu_clk);
257 * get peripheral clock 1 ( UART[12], Timer[12], PWM )
259 unsigned int imx_get_perclk1(void)
261 return imx_get_system_clk() / (((PCDR) & 0xf)+1);
263 EXPORT_SYMBOL(imx_get_perclk1);
266 * get peripheral clock 2 ( LCD, SD, SPI[12] )
268 unsigned int imx_get_perclk2(void)
270 return imx_get_system_clk() / (((PCDR>>4) & 0xf)+1);
272 EXPORT_SYMBOL(imx_get_perclk2);
275 * get peripheral clock 3 ( SSI )
277 unsigned int imx_get_perclk3(void)
279 return imx_get_system_clk() / (((PCDR>>16) & 0x7f)+1);
281 EXPORT_SYMBOL(imx_get_perclk3);
284 * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
286 unsigned int imx_get_hclk(void)
288 return imx_get_system_clk() / (((CSCR>>10) & 0xf)+1);
290 EXPORT_SYMBOL(imx_get_hclk);
292 static struct resource imx_mmc_resources[] = {
296 .flags = IORESOURCE_MEM,
301 .flags = IORESOURCE_IRQ,
305 static u64 imxmmmc_dmamask = 0xffffffffUL;
307 static struct platform_device imx_mmc_device = {
311 .dma_mask = &imxmmmc_dmamask,
312 .coherent_dma_mask = 0xffffffff,
314 .num_resources = ARRAY_SIZE(imx_mmc_resources),
315 .resource = imx_mmc_resources,
318 void __init imx_set_mmc_info(struct imxmmc_platform_data *info)
320 imx_mmc_device.dev.platform_data = info;
323 static struct imxfb_mach_info imx_fb_info;
325 void __init set_imx_fb_info(struct imxfb_mach_info *hard_imx_fb_info)
327 memcpy(&imx_fb_info,hard_imx_fb_info,sizeof(struct imxfb_mach_info));
329 EXPORT_SYMBOL(set_imx_fb_info);
331 static struct resource imxfb_resources[] = {
335 .flags = IORESOURCE_MEM,
340 .flags = IORESOURCE_IRQ,
344 static u64 fb_dma_mask = ~(u64)0;
346 static struct platform_device imxfb_device = {
350 .platform_data = &imx_fb_info,
351 .dma_mask = &fb_dma_mask,
352 .coherent_dma_mask = 0xffffffff,
354 .num_resources = ARRAY_SIZE(imxfb_resources),
355 .resource = imxfb_resources,
358 static struct platform_device *devices[] __initdata = {
363 static struct map_desc imx_io_desc[] __initdata = {
365 .virtual = IMX_IO_BASE,
366 .pfn = __phys_to_pfn(IMX_IO_PHYS),
367 .length = IMX_IO_SIZE,
375 iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc));
378 static int __init imx_init(void)
380 return platform_add_devices(devices, ARRAY_SIZE(devices));
383 subsys_initcall(imx_init);