blob: 76970598f55071faefa72df5ac6970db61b8d2fe [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/arch/arm/mach-pxa/generic.c
3 *
4 * Author: Nicolas Pitre
5 * Created: Jun 15, 2001
6 * Copyright: MontaVista Software Inc.
7 *
8 * Code common to all PXA 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 version 2 as
12 * published by the Free Software Foundation.
13 *
14 * Since this file should be linked before any other machine specific file,
15 * the __initcall() here will be executed first. This serves as default
16 * initialization stuff for PXA machines which can be overridden later if
17 * need be.
18 */
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/init.h>
22#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/ioport.h>
24#include <linux/pm.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080025#include <linux/string.h>
eric miao16dfdbf2008-01-28 23:00:02 +000026#include <linux/sysdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28#include <asm/hardware.h>
29#include <asm/irq.h>
30#include <asm/system.h>
31#include <asm/pgtable.h>
32#include <asm/mach/map.h>
33
34#include <asm/arch/pxa-regs.h>
Philipp Zabel3deac042007-02-20 13:58:15 -080035#include <asm/arch/gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include "generic.h"
38
39/*
Russell King15a40332007-08-20 10:07:44 +010040 * Get the clock frequency as reflected by CCCR and the turbo flag.
41 * We assume these values have been applied via a fcs.
42 * If info is not 0 we also display the current settings.
43 */
44unsigned int get_clk_frequency_khz(int info)
45{
46 if (cpu_is_pxa21x() || cpu_is_pxa25x())
47 return pxa25x_get_clk_frequency_khz(info);
eric miao2c8086a2007-09-11 19:13:17 -070048 else if (cpu_is_pxa27x())
Russell King15a40332007-08-20 10:07:44 +010049 return pxa27x_get_clk_frequency_khz(info);
eric miao2c8086a2007-09-11 19:13:17 -070050 else
51 return pxa3xx_get_clk_frequency_khz(info);
Russell King15a40332007-08-20 10:07:44 +010052}
53EXPORT_SYMBOL(get_clk_frequency_khz);
54
55/*
56 * Return the current memory clock frequency in units of 10kHz
57 */
58unsigned int get_memclk_frequency_10khz(void)
59{
60 if (cpu_is_pxa21x() || cpu_is_pxa25x())
61 return pxa25x_get_memclk_frequency_10khz();
eric miao2c8086a2007-09-11 19:13:17 -070062 else if (cpu_is_pxa27x())
Russell King15a40332007-08-20 10:07:44 +010063 return pxa27x_get_memclk_frequency_10khz();
eric miao2c8086a2007-09-11 19:13:17 -070064 else
65 return pxa3xx_get_memclk_frequency_10khz();
Russell King15a40332007-08-20 10:07:44 +010066}
67EXPORT_SYMBOL(get_memclk_frequency_10khz);
68
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * Handy function to set GPIO alternate functions
71 */
eric miao30f0b402007-08-29 10:18:47 +010072int pxa_last_gpio;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Philipp Zabel3deac042007-02-20 13:58:15 -080074int pxa_gpio_mode(int gpio_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 unsigned long flags;
77 int gpio = gpio_mode & GPIO_MD_MASK_NR;
78 int fn = (gpio_mode & GPIO_MD_MASK_FN) >> 8;
79 int gafr;
80
eric miao30f0b402007-08-29 10:18:47 +010081 if (gpio > pxa_last_gpio)
Philipp Zabel3deac042007-02-20 13:58:15 -080082 return -EINVAL;
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 local_irq_save(flags);
85 if (gpio_mode & GPIO_DFLT_LOW)
86 GPCR(gpio) = GPIO_bit(gpio);
87 else if (gpio_mode & GPIO_DFLT_HIGH)
88 GPSR(gpio) = GPIO_bit(gpio);
89 if (gpio_mode & GPIO_MD_MASK_DIR)
90 GPDR(gpio) |= GPIO_bit(gpio);
91 else
92 GPDR(gpio) &= ~GPIO_bit(gpio);
93 gafr = GAFR(gpio) & ~(0x3 << (((gpio) & 0xf)*2));
94 GAFR(gpio) = gafr | (fn << (((gpio) & 0xf)*2));
95 local_irq_restore(flags);
Philipp Zabel3deac042007-02-20 13:58:15 -080096
97 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100EXPORT_SYMBOL(pxa_gpio_mode);
101
Russell King3e0cc7e2007-10-02 14:28:01 +0100102int gpio_direction_input(unsigned gpio)
103{
104 unsigned long flags;
105 u32 mask;
106
107 if (gpio > pxa_last_gpio)
108 return -EINVAL;
109
110 mask = GPIO_bit(gpio);
111 local_irq_save(flags);
112 GPDR(gpio) &= ~mask;
113 local_irq_restore(flags);
114
115 return 0;
116}
117EXPORT_SYMBOL(gpio_direction_input);
118
119int gpio_direction_output(unsigned gpio, int value)
120{
121 unsigned long flags;
122 u32 mask;
123
124 if (gpio > pxa_last_gpio)
125 return -EINVAL;
126
127 mask = GPIO_bit(gpio);
128 local_irq_save(flags);
129 if (value)
130 GPSR(gpio) = mask;
131 else
132 GPCR(gpio) = mask;
133 GPDR(gpio) |= mask;
134 local_irq_restore(flags);
135
136 return 0;
137}
138EXPORT_SYMBOL(gpio_direction_output);
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140/*
Philipp Zabel3deac042007-02-20 13:58:15 -0800141 * Return GPIO level
142 */
143int pxa_gpio_get_value(unsigned gpio)
144{
145 return __gpio_get_value(gpio);
146}
147
148EXPORT_SYMBOL(pxa_gpio_get_value);
149
150/*
151 * Set output GPIO level
152 */
153void pxa_gpio_set_value(unsigned gpio, int value)
154{
155 __gpio_set_value(gpio, value);
156}
157
158EXPORT_SYMBOL(pxa_gpio_set_value);
159
160/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 * Routine to safely enable or disable a clock in the CKEN
162 */
Russell Kinga7073b82007-09-19 09:33:55 +0100163void __pxa_set_cken(int clock, int enable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 unsigned long flags;
166 local_irq_save(flags);
167
168 if (enable)
Eric Miao7053acb2007-04-05 04:07:20 +0100169 CKEN |= (1 << clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 else
Eric Miao7053acb2007-04-05 04:07:20 +0100171 CKEN &= ~(1 << clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173 local_irq_restore(flags);
174}
175
Russell Kinga7073b82007-09-19 09:33:55 +0100176EXPORT_SYMBOL(__pxa_set_cken);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178/*
179 * Intel PXA2xx internal register mapping.
180 *
181 * Note 1: not all PXA2xx variants implement all those addresses.
182 *
183 * Note 2: virtual 0xfffe0000-0xffffffff is reserved for the vector table
184 * and cache flush area.
185 */
186static struct map_desc standard_io_desc[] __initdata = {
Deepak Saxena6f9182e2005-10-28 15:19:01 +0100187 { /* Devs */
188 .virtual = 0xf2000000,
189 .pfn = __phys_to_pfn(0x40000000),
190 .length = 0x02000000,
191 .type = MT_DEVICE
192 }, { /* LCD */
193 .virtual = 0xf4000000,
194 .pfn = __phys_to_pfn(0x44000000),
195 .length = 0x00100000,
196 .type = MT_DEVICE
197 }, { /* Mem Ctl */
198 .virtual = 0xf6000000,
199 .pfn = __phys_to_pfn(0x48000000),
Russell King7664c402008-01-04 22:41:09 +0000200 .length = 0x00200000,
Deepak Saxena6f9182e2005-10-28 15:19:01 +0100201 .type = MT_DEVICE
202 }, { /* USB host */
203 .virtual = 0xf8000000,
204 .pfn = __phys_to_pfn(0x4c000000),
205 .length = 0x00100000,
206 .type = MT_DEVICE
207 }, { /* Camera */
208 .virtual = 0xfa000000,
209 .pfn = __phys_to_pfn(0x50000000),
210 .length = 0x00100000,
211 .type = MT_DEVICE
212 }, { /* IMem ctl */
213 .virtual = 0xfe000000,
214 .pfn = __phys_to_pfn(0x58000000),
215 .length = 0x00100000,
216 .type = MT_DEVICE
217 }, { /* UNCACHED_PHYS_0 */
218 .virtual = 0xff000000,
219 .pfn = __phys_to_pfn(0x00000000),
220 .length = 0x00100000,
221 .type = MT_DEVICE
222 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223};
224
225void __init pxa_map_io(void)
226{
227 iotable_init(standard_io_desc, ARRAY_SIZE(standard_io_desc));
228 get_clk_frequency_khz(1);
229}
eric miao16dfdbf2008-01-28 23:00:02 +0000230
231#ifdef CONFIG_PM
232
233static unsigned long saved_gplr[4];
234static unsigned long saved_gpdr[4];
235static unsigned long saved_grer[4];
236static unsigned long saved_gfer[4];
237
238static int pxa_gpio_suspend(struct sys_device *dev, pm_message_t state)
239{
240 int i, gpio;
241
242 for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
243 saved_gplr[i] = GPLR(gpio);
244 saved_gpdr[i] = GPDR(gpio);
245 saved_grer[i] = GRER(gpio);
246 saved_gfer[i] = GFER(gpio);
247
248 /* Clear GPIO transition detect bits */
249 GEDR(gpio) = GEDR(gpio);
250 }
251 return 0;
252}
253
254static int pxa_gpio_resume(struct sys_device *dev)
255{
256 int i, gpio;
257
258 for (gpio = 0, i = 0; gpio < pxa_last_gpio; gpio += 32, i++) {
259 /* restore level with set/clear */
260 GPSR(gpio) = saved_gplr[i];
261 GPCR(gpio) = ~saved_gplr[i];
262
263 GRER(gpio) = saved_grer[i];
264 GFER(gpio) = saved_gfer[i];
265 GPDR(gpio) = saved_gpdr[i];
266 }
267 return 0;
268}
269#else
270#define pxa_gpio_suspend NULL
271#define pxa_gpio_resume NULL
272#endif
273
274struct sysdev_class pxa_gpio_sysclass = {
275 .name = "gpio",
276 .suspend = pxa_gpio_suspend,
277 .resume = pxa_gpio_resume,
278};
279
280static int __init pxa_gpio_init(void)
281{
282 return sysdev_class_register(&pxa_gpio_sysclass);
283}
284
285core_initcall(pxa_gpio_init);