]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/arm/mach-omap2/gpmc.c
ARM: OMAP2: Fix sparse, checkpatch warnings fro GPMC code, use ioremap
[linux-2.6-omap-h63xx.git] / arch / arm / mach-omap2 / gpmc.c
1 /*
2  * GPMC support functions
3  *
4  * Copyright (C) 2005-2006 Nokia Corporation
5  *
6  * Author: Juha Yrjola
7  *
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.
11  */
12 #undef DEBUG
13
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/err.h>
17 #include <linux/clk.h>
18 #include <linux/ioport.h>
19 #include <linux/spinlock.h>
20 #include <linux/module.h>
21
22 #include <asm/io.h>
23 #include <asm/mach-types.h>
24 #include <mach/gpmc.h>
25
26 #include "memory.h"
27
28 /* GPMC register offsets */
29 #define GPMC_REVISION           0x00
30 #define GPMC_SYSCONFIG          0x10
31 #define GPMC_SYSSTATUS          0x14
32 #define GPMC_IRQSTATUS          0x18
33 #define GPMC_IRQENABLE          0x1c
34 #define GPMC_TIMEOUT_CONTROL    0x40
35 #define GPMC_ERR_ADDRESS        0x44
36 #define GPMC_ERR_TYPE           0x48
37 #define GPMC_CONFIG             0x50
38 #define GPMC_STATUS             0x54
39 #define GPMC_PREFETCH_CONFIG1   0x1e0
40 #define GPMC_PREFETCH_CONFIG2   0x1e4
41 #define GPMC_PREFETCH_CONTROL   0x1ec
42 #define GPMC_PREFETCH_STATUS    0x1f0
43 #define GPMC_ECC_CONFIG         0x1f4
44 #define GPMC_ECC_CONTROL        0x1f8
45 #define GPMC_ECC_SIZE_CONFIG    0x1fc
46
47 #define GPMC_CS0                0x60
48 #define GPMC_CS_SIZE            0x30
49
50 #define GPMC_MEM_START          0x00000000
51 #define GPMC_MEM_END            0x3FFFFFFF
52 #define BOOT_ROM_SPACE          0x100000        /* 1MB */
53
54 #define GPMC_CHUNK_SHIFT        24              /* 16 MB */
55 #define GPMC_SECTION_SHIFT      28              /* 128 MB */
56
57 static struct resource  gpmc_mem_root;
58 static struct resource  gpmc_cs_mem[GPMC_CS_NUM];
59 static DEFINE_SPINLOCK(gpmc_mem_lock);
60 static unsigned         gpmc_cs_map;
61
62 static void __iomem *gpmc_base;
63
64 static struct clk *gpmc_l3_clk;
65
66 static void gpmc_write_reg(int idx, u32 val)
67 {
68         __raw_writel(val, gpmc_base + idx);
69 }
70
71 static u32 gpmc_read_reg(int idx)
72 {
73         return __raw_readl(gpmc_base + idx);
74 }
75
76 void gpmc_cs_write_reg(int cs, int idx, u32 val)
77 {
78         void __iomem *reg_addr;
79
80         reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
81         __raw_writel(val, reg_addr);
82 }
83
84 u32 gpmc_cs_read_reg(int cs, int idx)
85 {
86         void __iomem *reg_addr;
87
88         reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
89         return __raw_readl(reg_addr);
90 }
91
92 /* TODO: Add support for gpmc_fck to clock framework and use it */
93 unsigned long gpmc_get_fclk_period(void)
94 {
95         unsigned long rate = clk_get_rate(gpmc_l3_clk);
96
97         if (rate == 0) {
98                 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
99                 return 0;
100         }
101
102         rate /= 1000;
103         rate = 1000000000 / rate;       /* In picoseconds */
104
105         return rate;
106 }
107
108 unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
109 {
110         unsigned long tick_ps;
111
112         /* Calculate in picosecs to yield more exact results */
113         tick_ps = gpmc_get_fclk_period();
114
115         return (time_ns * 1000 + tick_ps - 1) / tick_ps;
116 }
117
118 unsigned int gpmc_ticks_to_ns(unsigned int ticks)
119 {
120         return ticks * gpmc_get_fclk_period() / 1000;
121 }
122
123 unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
124 {
125         unsigned long ticks = gpmc_ns_to_ticks(time_ns);
126
127         return ticks * gpmc_get_fclk_period() / 1000;
128 }
129
130 #ifdef DEBUG
131 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
132                                int time, const char *name)
133 #else
134 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
135                                int time)
136 #endif
137 {
138         u32 l;
139         int ticks, mask, nr_bits;
140
141         if (time == 0)
142                 ticks = 0;
143         else
144                 ticks = gpmc_ns_to_ticks(time);
145         nr_bits = end_bit - st_bit + 1;
146         if (ticks >= 1 << nr_bits) {
147 #ifdef DEBUG
148                 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
149                                 cs, name, time, ticks, 1 << nr_bits);
150 #endif
151                 return -1;
152         }
153
154         mask = (1 << nr_bits) - 1;
155         l = gpmc_cs_read_reg(cs, reg);
156 #ifdef DEBUG
157         printk(KERN_INFO
158                 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
159                cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
160                         (l >> st_bit) & mask, time);
161 #endif
162         l &= ~(mask << st_bit);
163         l |= ticks << st_bit;
164         gpmc_cs_write_reg(cs, reg, l);
165
166         return 0;
167 }
168
169 #ifdef DEBUG
170 #define GPMC_SET_ONE(reg, st, end, field) \
171         if (set_gpmc_timing_reg(cs, (reg), (st), (end),         \
172                         t->field, #field) < 0)                  \
173                 return -1
174 #else
175 #define GPMC_SET_ONE(reg, st, end, field) \
176         if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
177                 return -1
178 #endif
179
180 int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
181 {
182         int div;
183         u32 l;
184
185         l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
186         div = l / gpmc_get_fclk_period();
187         if (div > 4)
188                 return -1;
189         if (div <= 0)
190                 div = 1;
191
192         return div;
193 }
194
195 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
196 {
197         int div;
198         u32 l;
199
200         div = gpmc_cs_calc_divider(cs, t->sync_clk);
201         if (div < 0)
202                 return -1;
203
204         GPMC_SET_ONE(GPMC_CS_CONFIG2,  0,  3, cs_on);
205         GPMC_SET_ONE(GPMC_CS_CONFIG2,  8, 12, cs_rd_off);
206         GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
207
208         GPMC_SET_ONE(GPMC_CS_CONFIG3,  0,  3, adv_on);
209         GPMC_SET_ONE(GPMC_CS_CONFIG3,  8, 12, adv_rd_off);
210         GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
211
212         GPMC_SET_ONE(GPMC_CS_CONFIG4,  0,  3, oe_on);
213         GPMC_SET_ONE(GPMC_CS_CONFIG4,  8, 12, oe_off);
214         GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
215         GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
216
217         GPMC_SET_ONE(GPMC_CS_CONFIG5,  0,  4, rd_cycle);
218         GPMC_SET_ONE(GPMC_CS_CONFIG5,  8, 12, wr_cycle);
219         GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
220
221         GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
222
223         /* caller is expected to have initialized CONFIG1 to cover
224          * at least sync vs async
225          */
226         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
227         if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
228 #ifdef DEBUG
229                 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
230                                 cs, (div * gpmc_get_fclk_period()) / 1000, div);
231 #endif
232                 l &= ~0x03;
233                 l |= (div - 1);
234                 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
235         }
236
237         return 0;
238 }
239
240 static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
241 {
242         u32 l;
243         u32 mask;
244
245         mask = (1 << GPMC_SECTION_SHIFT) - size;
246         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
247         l &= ~0x3f;
248         l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
249         l &= ~(0x0f << 8);
250         l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
251         l |= 1 << 6;            /* CSVALID */
252         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
253 }
254
255 static void gpmc_cs_disable_mem(int cs)
256 {
257         u32 l;
258
259         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
260         l &= ~(1 << 6);         /* CSVALID */
261         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
262 }
263
264 static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
265 {
266         u32 l;
267         u32 mask;
268
269         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
270         *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
271         mask = (l >> 8) & 0x0f;
272         *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
273 }
274
275 static int gpmc_cs_mem_enabled(int cs)
276 {
277         u32 l;
278
279         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
280         return l & (1 << 6);
281 }
282
283 int gpmc_cs_set_reserved(int cs, int reserved)
284 {
285         if (cs > GPMC_CS_NUM)
286                 return -ENODEV;
287
288         gpmc_cs_map &= ~(1 << cs);
289         gpmc_cs_map |= (reserved ? 1 : 0) << cs;
290
291         return 0;
292 }
293
294 int gpmc_cs_reserved(int cs)
295 {
296         if (cs > GPMC_CS_NUM)
297                 return -ENODEV;
298
299         return gpmc_cs_map & (1 << cs);
300 }
301
302 static unsigned long gpmc_mem_align(unsigned long size)
303 {
304         int order;
305
306         size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
307         order = GPMC_CHUNK_SHIFT - 1;
308         do {
309                 size >>= 1;
310                 order++;
311         } while (size);
312         size = 1 << order;
313         return size;
314 }
315
316 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
317 {
318         struct resource *res = &gpmc_cs_mem[cs];
319         int r;
320
321         size = gpmc_mem_align(size);
322         spin_lock(&gpmc_mem_lock);
323         res->start = base;
324         res->end = base + size - 1;
325         r = request_resource(&gpmc_mem_root, res);
326         spin_unlock(&gpmc_mem_lock);
327
328         return r;
329 }
330
331 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
332 {
333         struct resource *res = &gpmc_cs_mem[cs];
334         int r = -1;
335
336         if (cs > GPMC_CS_NUM)
337                 return -ENODEV;
338
339         size = gpmc_mem_align(size);
340         if (size > (1 << GPMC_SECTION_SHIFT))
341                 return -ENOMEM;
342
343         spin_lock(&gpmc_mem_lock);
344         if (gpmc_cs_reserved(cs)) {
345                 r = -EBUSY;
346                 goto out;
347         }
348         if (gpmc_cs_mem_enabled(cs))
349                 r = adjust_resource(res, res->start & ~(size - 1), size);
350         if (r < 0)
351                 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
352                                       size, NULL, NULL);
353         if (r < 0)
354                 goto out;
355
356         gpmc_cs_enable_mem(cs, res->start, res->end - res->start + 1);
357         *base = res->start;
358         gpmc_cs_set_reserved(cs, 1);
359 out:
360         spin_unlock(&gpmc_mem_lock);
361         return r;
362 }
363 EXPORT_SYMBOL(gpmc_cs_request);
364
365 void gpmc_cs_free(int cs)
366 {
367         spin_lock(&gpmc_mem_lock);
368         if (cs >= GPMC_CS_NUM || !gpmc_cs_reserved(cs)) {
369                 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
370                 BUG();
371                 spin_unlock(&gpmc_mem_lock);
372                 return;
373         }
374         gpmc_cs_disable_mem(cs);
375         release_resource(&gpmc_cs_mem[cs]);
376         gpmc_cs_set_reserved(cs, 0);
377         spin_unlock(&gpmc_mem_lock);
378 }
379 EXPORT_SYMBOL(gpmc_cs_free);
380
381 static void __init gpmc_mem_init(void)
382 {
383         int cs;
384         unsigned long boot_rom_space = 0;
385
386         /* never allocate the first page, to facilitate bug detection;
387          * even if we didn't boot from ROM.
388          */
389         boot_rom_space = BOOT_ROM_SPACE;
390         /* In apollon the CS0 is mapped as 0x0000 0000 */
391         if (machine_is_omap_apollon())
392                 boot_rom_space = 0;
393         gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
394         gpmc_mem_root.end = GPMC_MEM_END;
395
396         /* Reserve all regions that has been set up by bootloader */
397         for (cs = 0; cs < GPMC_CS_NUM; cs++) {
398                 u32 base, size;
399
400                 if (!gpmc_cs_mem_enabled(cs))
401                         continue;
402                 gpmc_cs_get_memconf(cs, &base, &size);
403                 if (gpmc_cs_insert_mem(cs, base, size) < 0)
404                         BUG();
405         }
406 }
407
408 void __init gpmc_init(void)
409 {
410         u32 l;
411         char *ck;
412
413         if (cpu_is_omap24xx()) {
414                 ck = "core_l3_ck";
415                 if (cpu_is_omap2420())
416                         l = OMAP2420_GPMC_BASE;
417                 else
418                         l = OMAP34XX_GPMC_BASE;
419         } else if (cpu_is_omap34xx()) {
420                 ck = "gpmc_fck";
421                 l = OMAP34XX_GPMC_BASE;
422         }
423
424         gpmc_l3_clk = clk_get(NULL, ck);
425         if (IS_ERR(gpmc_l3_clk)) {
426                 printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
427                 return -ENODEV;
428         }
429
430         gpmc_base = ioremap(l, SZ_4K);
431         if (!gpmc_base) {
432                 clk_put(gpmc_l3_clk);
433                 printk(KERN_ERR "Could not get GPMC register memory\n");
434                 return -ENOMEM;
435         }
436
437         BUG_ON(IS_ERR(gpmc_l3_clk));
438
439         l = gpmc_read_reg(GPMC_REVISION);
440         printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
441         /* Set smart idle mode and automatic L3 clock gating */
442         l = gpmc_read_reg(GPMC_SYSCONFIG);
443         l &= 0x03 << 3;
444         l |= (0x02 << 3) | (1 << 0);
445         gpmc_write_reg(GPMC_SYSCONFIG, l);
446
447         gpmc_mem_init();
448 }