defxx: Use __maybe_unused rather than a local hack
[wrt350n-kernel.git] / arch / arm / mach-omap2 / gpmc.c
blobe290b989aa948f4af54ff7f9024287952c77e7c6
1 /*
2 * GPMC support functions
4 * Copyright (C) 2005-2006 Nokia Corporation
6 * Author: Juha Yrjola
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/err.h>
15 #include <linux/clk.h>
16 #include <linux/ioport.h>
17 #include <linux/spinlock.h>
19 #include <asm/io.h>
20 #include <asm/mach-types.h>
21 #include <asm/arch/gpmc.h>
23 #undef DEBUG
25 #define GPMC_BASE 0x6800a000
26 #define GPMC_REVISION 0x00
27 #define GPMC_SYSCONFIG 0x10
28 #define GPMC_SYSSTATUS 0x14
29 #define GPMC_IRQSTATUS 0x18
30 #define GPMC_IRQENABLE 0x1c
31 #define GPMC_TIMEOUT_CONTROL 0x40
32 #define GPMC_ERR_ADDRESS 0x44
33 #define GPMC_ERR_TYPE 0x48
34 #define GPMC_CONFIG 0x50
35 #define GPMC_STATUS 0x54
36 #define GPMC_PREFETCH_CONFIG1 0x1e0
37 #define GPMC_PREFETCH_CONFIG2 0x1e4
38 #define GPMC_PREFETCH_CONTROL 0x1e8
39 #define GPMC_PREFETCH_STATUS 0x1f0
40 #define GPMC_ECC_CONFIG 0x1f4
41 #define GPMC_ECC_CONTROL 0x1f8
42 #define GPMC_ECC_SIZE_CONFIG 0x1fc
44 #define GPMC_CS0 0x60
45 #define GPMC_CS_SIZE 0x30
47 #define GPMC_CS_NUM 8
48 #define GPMC_MEM_START 0x00000000
49 #define GPMC_MEM_END 0x3FFFFFFF
50 #define BOOT_ROM_SPACE 0x100000 /* 1MB */
52 #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
53 #define GPMC_SECTION_SHIFT 28 /* 128 MB */
55 static struct resource gpmc_mem_root;
56 static struct resource gpmc_cs_mem[GPMC_CS_NUM];
57 static DEFINE_SPINLOCK(gpmc_mem_lock);
58 static unsigned gpmc_cs_map;
60 static void __iomem *gpmc_base =
61 (void __iomem *) IO_ADDRESS(GPMC_BASE);
62 static void __iomem *gpmc_cs_base =
63 (void __iomem *) IO_ADDRESS(GPMC_BASE) + GPMC_CS0;
65 static struct clk *gpmc_l3_clk;
67 static void gpmc_write_reg(int idx, u32 val)
69 __raw_writel(val, gpmc_base + idx);
72 static u32 gpmc_read_reg(int idx)
74 return __raw_readl(gpmc_base + idx);
77 void gpmc_cs_write_reg(int cs, int idx, u32 val)
79 void __iomem *reg_addr;
81 reg_addr = gpmc_cs_base + (cs * GPMC_CS_SIZE) + idx;
82 __raw_writel(val, reg_addr);
85 u32 gpmc_cs_read_reg(int cs, int idx)
87 return __raw_readl(gpmc_cs_base + (cs * GPMC_CS_SIZE) + idx);
90 /* TODO: Add support for gpmc_fck to clock framework and use it */
91 static unsigned long gpmc_get_fclk_period(void)
93 /* In picoseconds */
94 return 1000000000 / ((clk_get_rate(gpmc_l3_clk)) / 1000);
97 unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
99 unsigned long tick_ps;
101 /* Calculate in picosecs to yield more exact results */
102 tick_ps = gpmc_get_fclk_period();
104 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
107 #ifdef DEBUG
108 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
109 int time, const char *name)
110 #else
111 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
112 int time)
113 #endif
115 u32 l;
116 int ticks, mask, nr_bits;
118 if (time == 0)
119 ticks = 0;
120 else
121 ticks = gpmc_ns_to_ticks(time);
122 nr_bits = end_bit - st_bit + 1;
123 if (ticks >= 1 << nr_bits)
124 return -1;
126 mask = (1 << nr_bits) - 1;
127 l = gpmc_cs_read_reg(cs, reg);
128 #ifdef DEBUG
129 printk(KERN_INFO "GPMC CS%d: %-10s: %d ticks, %3lu ns (was %i ticks)\n",
130 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
131 (l >> st_bit) & mask);
132 #endif
133 l &= ~(mask << st_bit);
134 l |= ticks << st_bit;
135 gpmc_cs_write_reg(cs, reg, l);
137 return 0;
140 #ifdef DEBUG
141 #define GPMC_SET_ONE(reg, st, end, field) \
142 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
143 t->field, #field) < 0) \
144 return -1
145 #else
146 #define GPMC_SET_ONE(reg, st, end, field) \
147 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
148 return -1
149 #endif
151 int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
153 int div;
154 u32 l;
156 l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
157 div = l / gpmc_get_fclk_period();
158 if (div > 4)
159 return -1;
160 if (div < 0)
161 div = 1;
163 return div;
166 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
168 int div;
169 u32 l;
171 div = gpmc_cs_calc_divider(cs, t->sync_clk);
172 if (div < 0)
173 return -1;
175 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
176 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
177 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
179 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
180 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
181 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
183 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
184 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
185 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
186 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
188 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
189 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
190 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
192 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
194 #ifdef DEBUG
195 printk(KERN_INFO "GPMC CS%d CLK period is %lu (div %d)\n",
196 cs, gpmc_get_fclk_period(), div);
197 #endif
199 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
200 l &= ~0x03;
201 l |= (div - 1);
203 return 0;
206 static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
208 u32 l;
209 u32 mask;
211 mask = (1 << GPMC_SECTION_SHIFT) - size;
212 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
213 l &= ~0x3f;
214 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
215 l &= ~(0x0f << 8);
216 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
217 l |= 1 << 6; /* CSVALID */
218 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
221 static void gpmc_cs_disable_mem(int cs)
223 u32 l;
225 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
226 l &= ~(1 << 6); /* CSVALID */
227 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
230 static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
232 u32 l;
233 u32 mask;
235 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
236 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
237 mask = (l >> 8) & 0x0f;
238 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
241 static int gpmc_cs_mem_enabled(int cs)
243 u32 l;
245 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
246 return l & (1 << 6);
249 int gpmc_cs_set_reserved(int cs, int reserved)
251 if (cs > GPMC_CS_NUM)
252 return -ENODEV;
254 gpmc_cs_map &= ~(1 << cs);
255 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
257 return 0;
260 int gpmc_cs_reserved(int cs)
262 if (cs > GPMC_CS_NUM)
263 return -ENODEV;
265 return gpmc_cs_map & (1 << cs);
268 static unsigned long gpmc_mem_align(unsigned long size)
270 int order;
272 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
273 order = GPMC_CHUNK_SHIFT - 1;
274 do {
275 size >>= 1;
276 order++;
277 } while (size);
278 size = 1 << order;
279 return size;
282 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
284 struct resource *res = &gpmc_cs_mem[cs];
285 int r;
287 size = gpmc_mem_align(size);
288 spin_lock(&gpmc_mem_lock);
289 res->start = base;
290 res->end = base + size - 1;
291 r = request_resource(&gpmc_mem_root, res);
292 spin_unlock(&gpmc_mem_lock);
294 return r;
297 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
299 struct resource *res = &gpmc_cs_mem[cs];
300 int r = -1;
302 if (cs > GPMC_CS_NUM)
303 return -ENODEV;
305 size = gpmc_mem_align(size);
306 if (size > (1 << GPMC_SECTION_SHIFT))
307 return -ENOMEM;
309 spin_lock(&gpmc_mem_lock);
310 if (gpmc_cs_reserved(cs)) {
311 r = -EBUSY;
312 goto out;
314 if (gpmc_cs_mem_enabled(cs))
315 r = adjust_resource(res, res->start & ~(size - 1), size);
316 if (r < 0)
317 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
318 size, NULL, NULL);
319 if (r < 0)
320 goto out;
322 gpmc_cs_enable_mem(cs, res->start, res->end - res->start + 1);
323 *base = res->start;
324 gpmc_cs_set_reserved(cs, 1);
325 out:
326 spin_unlock(&gpmc_mem_lock);
327 return r;
330 void gpmc_cs_free(int cs)
332 spin_lock(&gpmc_mem_lock);
333 if (cs >= GPMC_CS_NUM || !gpmc_cs_reserved(cs)) {
334 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
335 BUG();
336 spin_unlock(&gpmc_mem_lock);
337 return;
339 gpmc_cs_disable_mem(cs);
340 release_resource(&gpmc_cs_mem[cs]);
341 gpmc_cs_set_reserved(cs, 0);
342 spin_unlock(&gpmc_mem_lock);
345 void __init gpmc_mem_init(void)
347 int cs;
348 unsigned long boot_rom_space = 0;
350 /* never allocate the first page, to facilitate bug detection;
351 * even if we didn't boot from ROM.
353 boot_rom_space = BOOT_ROM_SPACE;
354 /* In apollon the CS0 is mapped as 0x0000 0000 */
355 if (machine_is_omap_apollon())
356 boot_rom_space = 0;
357 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
358 gpmc_mem_root.end = GPMC_MEM_END;
360 /* Reserve all regions that has been set up by bootloader */
361 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
362 u32 base, size;
364 if (!gpmc_cs_mem_enabled(cs))
365 continue;
366 gpmc_cs_get_memconf(cs, &base, &size);
367 if (gpmc_cs_insert_mem(cs, base, size) < 0)
368 BUG();
372 void __init gpmc_init(void)
374 u32 l;
376 gpmc_l3_clk = clk_get(NULL, "core_l3_ck");
377 BUG_ON(IS_ERR(gpmc_l3_clk));
379 l = gpmc_read_reg(GPMC_REVISION);
380 printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
381 /* Set smart idle mode and automatic L3 clock gating */
382 l = gpmc_read_reg(GPMC_SYSCONFIG);
383 l &= 0x03 << 3;
384 l |= (0x02 << 3) | (1 << 0);
385 gpmc_write_reg(GPMC_SYSCONFIG, l);
387 gpmc_mem_init();