blob: 7a7fa51ec62c11882e8833c94368536e3c77be2d [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
Sascha Hauera4938202005-05-03 22:57:56 +010031#include <asm/arch/imxfb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <asm/hardware.h>
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010033#include <asm/arch/imx-regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <asm/mach/map.h>
Pavel Pisa56ca90402006-04-02 19:27:07 +010036#include <asm/arch/mmc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38void imx_gpio_mode(int gpio_mode)
39{
40 unsigned int pin = gpio_mode & GPIO_PIN_MASK;
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010041 unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT;
42 unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 unsigned int tmp;
44
45 /* Pullup enable */
46 if(gpio_mode & GPIO_PUEN)
47 PUEN(port) |= (1<<pin);
48 else
49 PUEN(port) &= ~(1<<pin);
50
51 /* Data direction */
52 if(gpio_mode & GPIO_OUT)
53 DDIR(port) |= 1<<pin;
54 else
55 DDIR(port) &= ~(1<<pin);
56
57 /* Primary / alternate function */
58 if(gpio_mode & GPIO_AF)
59 GPR(port) |= (1<<pin);
60 else
61 GPR(port) &= ~(1<<pin);
62
63 /* use as gpio? */
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010064 if(gpio_mode & GPIO_GIUS)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 GIUS(port) |= (1<<pin);
66 else
67 GIUS(port) &= ~(1<<pin);
68
69 /* Output / input configuration */
70 /* FIXME: I'm not very sure about OCR and ICONF, someone
71 * should have a look over it
72 */
73 if(pin<16) {
74 tmp = OCR1(port);
75 tmp &= ~( 3<<(pin*2));
76 tmp |= (ocr << (pin*2));
77 OCR1(port) = tmp;
78
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010079 ICONFA1(port) &= ~( 3<<(pin*2));
80 ICONFA1(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2);
81 ICONFB1(port) &= ~( 3<<(pin*2));
82 ICONFB1(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 } else {
84 tmp = OCR2(port);
85 tmp &= ~( 3<<((pin-16)*2));
86 tmp |= (ocr << ((pin-16)*2));
87 OCR2(port) = tmp;
88
Sascha Hauer0a5b0aa2005-10-04 23:17:52 +010089 ICONFA2(port) &= ~( 3<<((pin-16)*2));
90 ICONFA2(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << ((pin-16) * 2);
91 ICONFB2(port) &= ~( 3<<((pin-16)*2));
92 ICONFB2(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << ((pin-16) * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 }
94}
95
96EXPORT_SYMBOL(imx_gpio_mode);
97
98/*
99 * get the system pll clock in Hz
100 *
101 * mfi + mfn / (mfd +1)
102 * f = 2 * f_ref * --------------------
103 * pd + 1
104 */
Pavel Pisab3c6b762007-03-07 23:56:16 +0100105static unsigned int imx_decode_pll(unsigned int pll, u32 f_ref)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Pavel Pisa5c894cd2006-12-06 17:08:27 +0100107 unsigned long long ll;
108 unsigned long quot;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 u32 mfi = (pll >> 10) & 0xf;
111 u32 mfn = pll & 0x3ff;
112 u32 mfd = (pll >> 16) & 0x3ff;
113 u32 pd = (pll >> 26) & 0xf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115 mfi = mfi <= 5 ? 5 : mfi;
116
Pavel Pisa5c894cd2006-12-06 17:08:27 +0100117 ll = 2 * (unsigned long long)f_ref * ( (mfi<<16) + (mfn<<16) / (mfd+1) );
118 quot = (pd+1) * (1<<16);
119 ll += quot / 2;
120 do_div(ll, quot);
121 return (unsigned int) ll;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124unsigned int imx_get_system_clk(void)
125{
Pavel Pisab3c6b762007-03-07 23:56:16 +0100126 u32 f_ref = (CSCR & CSCR_SYSTEM_SEL) ? 16000000 : (CLK32 * 512);
127
128 return imx_decode_pll(SPCTL0, f_ref);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130EXPORT_SYMBOL(imx_get_system_clk);
131
132unsigned int imx_get_mcu_clk(void)
133{
Pavel Pisab3c6b762007-03-07 23:56:16 +0100134 return imx_decode_pll(MPCTL0, CLK32 * 512);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136EXPORT_SYMBOL(imx_get_mcu_clk);
137
138/*
139 * get peripheral clock 1 ( UART[12], Timer[12], PWM )
140 */
141unsigned int imx_get_perclk1(void)
142{
143 return imx_get_system_clk() / (((PCDR) & 0xf)+1);
144}
145EXPORT_SYMBOL(imx_get_perclk1);
146
147/*
148 * get peripheral clock 2 ( LCD, SD, SPI[12] )
149 */
150unsigned int imx_get_perclk2(void)
151{
152 return imx_get_system_clk() / (((PCDR>>4) & 0xf)+1);
153}
154EXPORT_SYMBOL(imx_get_perclk2);
155
156/*
157 * get peripheral clock 3 ( SSI )
158 */
159unsigned int imx_get_perclk3(void)
160{
161 return imx_get_system_clk() / (((PCDR>>16) & 0x7f)+1);
162}
163EXPORT_SYMBOL(imx_get_perclk3);
164
165/*
166 * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
167 */
168unsigned int imx_get_hclk(void)
169{
170 return imx_get_system_clk() / (((CSCR>>10) & 0xf)+1);
171}
172EXPORT_SYMBOL(imx_get_hclk);
173
174static struct resource imx_mmc_resources[] = {
175 [0] = {
176 .start = 0x00214000,
177 .end = 0x002140FF,
178 .flags = IORESOURCE_MEM,
179 },
180 [1] = {
181 .start = (SDHC_INT),
182 .end = (SDHC_INT),
183 .flags = IORESOURCE_IRQ,
184 },
185};
186
Pavel Pisa56ca90402006-04-02 19:27:07 +0100187static u64 imxmmmc_dmamask = 0xffffffffUL;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189static struct platform_device imx_mmc_device = {
190 .name = "imx-mmc",
191 .id = 0,
Pavel Pisa56ca90402006-04-02 19:27:07 +0100192 .dev = {
193 .dma_mask = &imxmmmc_dmamask,
194 .coherent_dma_mask = 0xffffffff,
195 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 .num_resources = ARRAY_SIZE(imx_mmc_resources),
197 .resource = imx_mmc_resources,
198};
199
Pavel Pisa56ca90402006-04-02 19:27:07 +0100200void __init imx_set_mmc_info(struct imxmmc_platform_data *info)
201{
202 imx_mmc_device.dev.platform_data = info;
203}
204EXPORT_SYMBOL(imx_set_mmc_info);
205
Sascha Hauera4938202005-05-03 22:57:56 +0100206static struct imxfb_mach_info imx_fb_info;
207
208void __init set_imx_fb_info(struct imxfb_mach_info *hard_imx_fb_info)
209{
210 memcpy(&imx_fb_info,hard_imx_fb_info,sizeof(struct imxfb_mach_info));
211}
212EXPORT_SYMBOL(set_imx_fb_info);
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214static struct resource imxfb_resources[] = {
215 [0] = {
216 .start = 0x00205000,
217 .end = 0x002050FF,
218 .flags = IORESOURCE_MEM,
219 },
220 [1] = {
221 .start = LCDC_INT,
222 .end = LCDC_INT,
223 .flags = IORESOURCE_IRQ,
224 },
225};
226
Sascha Hauera4938202005-05-03 22:57:56 +0100227static u64 fb_dma_mask = ~(u64)0;
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static struct platform_device imxfb_device = {
230 .name = "imx-fb",
231 .id = 0,
Sascha Hauera4938202005-05-03 22:57:56 +0100232 .dev = {
233 .platform_data = &imx_fb_info,
234 .dma_mask = &fb_dma_mask,
235 .coherent_dma_mask = 0xffffffff,
236 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 .num_resources = ARRAY_SIZE(imxfb_resources),
238 .resource = imxfb_resources,
239};
240
241static struct platform_device *devices[] __initdata = {
242 &imx_mmc_device,
243 &imxfb_device,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244};
245
246static struct map_desc imx_io_desc[] __initdata = {
Deepak Saxenaadecef72005-10-28 15:19:10 +0100247 {
248 .virtual = IMX_IO_BASE,
249 .pfn = __phys_to_pfn(IMX_IO_PHYS),
250 .length = IMX_IO_SIZE,
251 .type = MT_DEVICE
252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
255void __init
256imx_map_io(void)
257{
258 iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc));
259}
260
261static int __init imx_init(void)
262{
263 return platform_add_devices(devices, ARRAY_SIZE(devices));
264}
265
266subsys_initcall(imx_init);