2 * GPMC support functions
4 * Copyright (C) 2005-2006 Nokia Corporation
8 * Copyright (C) 2009 Texas Instruments
9 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
21 #include <linux/ioport.h>
22 #include <linux/spinlock.h>
24 #include <linux/module.h>
26 #include <asm/mach-types.h>
27 #include <mach/gpmc.h>
29 #include <mach/sdrc.h>
31 /* GPMC register offsets */
32 #define GPMC_REVISION 0x00
33 #define GPMC_SYSCONFIG 0x10
34 #define GPMC_SYSSTATUS 0x14
35 #define GPMC_IRQSTATUS 0x18
36 #define GPMC_IRQENABLE 0x1c
37 #define GPMC_TIMEOUT_CONTROL 0x40
38 #define GPMC_ERR_ADDRESS 0x44
39 #define GPMC_ERR_TYPE 0x48
40 #define GPMC_CONFIG 0x50
41 #define GPMC_STATUS 0x54
42 #define GPMC_PREFETCH_CONFIG1 0x1e0
43 #define GPMC_PREFETCH_CONFIG2 0x1e4
44 #define GPMC_PREFETCH_CONTROL 0x1ec
45 #define GPMC_PREFETCH_STATUS 0x1f0
46 #define GPMC_ECC_CONFIG 0x1f4
47 #define GPMC_ECC_CONTROL 0x1f8
48 #define GPMC_ECC_SIZE_CONFIG 0x1fc
51 #define GPMC_CS_SIZE 0x30
53 #define GPMC_MEM_START 0x00000000
54 #define GPMC_MEM_END 0x3FFFFFFF
55 #define BOOT_ROM_SPACE 0x100000 /* 1MB */
57 #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
58 #define GPMC_SECTION_SHIFT 28 /* 128 MB */
60 static struct resource gpmc_mem_root
;
61 static struct resource gpmc_cs_mem
[GPMC_CS_NUM
];
62 static DEFINE_SPINLOCK(gpmc_mem_lock
);
63 static unsigned gpmc_cs_map
;
65 static void __iomem
*gpmc_base
;
67 static struct clk
*gpmc_l3_clk
;
69 static void gpmc_write_reg(int idx
, u32 val
)
71 __raw_writel(val
, gpmc_base
+ idx
);
74 static u32
gpmc_read_reg(int idx
)
76 return __raw_readl(gpmc_base
+ idx
);
79 void gpmc_cs_write_reg(int cs
, int idx
, u32 val
)
81 void __iomem
*reg_addr
;
83 reg_addr
= gpmc_base
+ GPMC_CS0
+ (cs
* GPMC_CS_SIZE
) + idx
;
84 __raw_writel(val
, reg_addr
);
87 u32
gpmc_cs_read_reg(int cs
, int idx
)
89 void __iomem
*reg_addr
;
91 reg_addr
= gpmc_base
+ GPMC_CS0
+ (cs
* GPMC_CS_SIZE
) + idx
;
92 return __raw_readl(reg_addr
);
95 /* TODO: Add support for gpmc_fck to clock framework and use it */
96 unsigned long gpmc_get_fclk_period(void)
98 unsigned long rate
= clk_get_rate(gpmc_l3_clk
);
101 printk(KERN_WARNING
"gpmc_l3_clk not enabled\n");
106 rate
= 1000000000 / rate
; /* In picoseconds */
111 unsigned int gpmc_ns_to_ticks(unsigned int time_ns
)
113 unsigned long tick_ps
;
115 /* Calculate in picosecs to yield more exact results */
116 tick_ps
= gpmc_get_fclk_period();
118 return (time_ns
* 1000 + tick_ps
- 1) / tick_ps
;
121 unsigned int gpmc_ticks_to_ns(unsigned int ticks
)
123 return ticks
* gpmc_get_fclk_period() / 1000;
126 unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns
)
128 unsigned long ticks
= gpmc_ns_to_ticks(time_ns
);
130 return ticks
* gpmc_get_fclk_period() / 1000;
134 static int set_gpmc_timing_reg(int cs
, int reg
, int st_bit
, int end_bit
,
135 int time
, const char *name
)
137 static int set_gpmc_timing_reg(int cs
, int reg
, int st_bit
, int end_bit
,
142 int ticks
, mask
, nr_bits
;
147 ticks
= gpmc_ns_to_ticks(time
);
148 nr_bits
= end_bit
- st_bit
+ 1;
149 if (ticks
>= 1 << nr_bits
) {
151 printk(KERN_INFO
"GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
152 cs
, name
, time
, ticks
, 1 << nr_bits
);
157 mask
= (1 << nr_bits
) - 1;
158 l
= gpmc_cs_read_reg(cs
, reg
);
161 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
162 cs
, name
, ticks
, gpmc_get_fclk_period() * ticks
/ 1000,
163 (l
>> st_bit
) & mask
, time
);
165 l
&= ~(mask
<< st_bit
);
166 l
|= ticks
<< st_bit
;
167 gpmc_cs_write_reg(cs
, reg
, l
);
173 #define GPMC_SET_ONE(reg, st, end, field) \
174 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
175 t->field, #field) < 0) \
178 #define GPMC_SET_ONE(reg, st, end, field) \
179 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
183 int gpmc_cs_calc_divider(int cs
, unsigned int sync_clk
)
188 l
= sync_clk
* 1000 + (gpmc_get_fclk_period() - 1);
189 div
= l
/ gpmc_get_fclk_period();
198 int gpmc_cs_set_timings(int cs
, const struct gpmc_timings
*t
)
203 div
= gpmc_cs_calc_divider(cs
, t
->sync_clk
);
207 GPMC_SET_ONE(GPMC_CS_CONFIG2
, 0, 3, cs_on
);
208 GPMC_SET_ONE(GPMC_CS_CONFIG2
, 8, 12, cs_rd_off
);
209 GPMC_SET_ONE(GPMC_CS_CONFIG2
, 16, 20, cs_wr_off
);
211 GPMC_SET_ONE(GPMC_CS_CONFIG3
, 0, 3, adv_on
);
212 GPMC_SET_ONE(GPMC_CS_CONFIG3
, 8, 12, adv_rd_off
);
213 GPMC_SET_ONE(GPMC_CS_CONFIG3
, 16, 20, adv_wr_off
);
215 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 0, 3, oe_on
);
216 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 8, 12, oe_off
);
217 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 16, 19, we_on
);
218 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 24, 28, we_off
);
220 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 0, 4, rd_cycle
);
221 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 8, 12, wr_cycle
);
222 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 16, 20, access
);
224 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 24, 27, page_burst_access
);
226 if (cpu_is_omap34xx()) {
227 GPMC_SET_ONE(GPMC_CS_CONFIG6
, 16, 19, wr_data_mux_bus
);
228 GPMC_SET_ONE(GPMC_CS_CONFIG6
, 24, 28, wr_access
);
231 /* caller is expected to have initialized CONFIG1 to cover
232 * at least sync vs async
234 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG1
);
235 if (l
& (GPMC_CONFIG1_READTYPE_SYNC
| GPMC_CONFIG1_WRITETYPE_SYNC
)) {
237 printk(KERN_INFO
"GPMC CS%d CLK period is %lu ns (div %d)\n",
238 cs
, (div
* gpmc_get_fclk_period()) / 1000, div
);
242 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG1
, l
);
248 static void gpmc_cs_enable_mem(int cs
, u32 base
, u32 size
)
253 mask
= (1 << GPMC_SECTION_SHIFT
) - size
;
254 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
256 l
= (base
>> GPMC_CHUNK_SHIFT
) & 0x3f;
258 l
|= ((mask
>> GPMC_CHUNK_SHIFT
) & 0x0f) << 8;
259 l
|= 1 << 6; /* CSVALID */
260 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG7
, l
);
263 static void gpmc_cs_disable_mem(int cs
)
267 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
268 l
&= ~(1 << 6); /* CSVALID */
269 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG7
, l
);
272 static void gpmc_cs_get_memconf(int cs
, u32
*base
, u32
*size
)
277 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
278 *base
= (l
& 0x3f) << GPMC_CHUNK_SHIFT
;
279 mask
= (l
>> 8) & 0x0f;
280 *size
= (1 << GPMC_SECTION_SHIFT
) - (mask
<< GPMC_CHUNK_SHIFT
);
283 static int gpmc_cs_mem_enabled(int cs
)
287 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
291 int gpmc_cs_set_reserved(int cs
, int reserved
)
293 if (cs
> GPMC_CS_NUM
)
296 gpmc_cs_map
&= ~(1 << cs
);
297 gpmc_cs_map
|= (reserved
? 1 : 0) << cs
;
302 int gpmc_cs_reserved(int cs
)
304 if (cs
> GPMC_CS_NUM
)
307 return gpmc_cs_map
& (1 << cs
);
310 static unsigned long gpmc_mem_align(unsigned long size
)
314 size
= (size
- 1) >> (GPMC_CHUNK_SHIFT
- 1);
315 order
= GPMC_CHUNK_SHIFT
- 1;
324 static int gpmc_cs_insert_mem(int cs
, unsigned long base
, unsigned long size
)
326 struct resource
*res
= &gpmc_cs_mem
[cs
];
329 size
= gpmc_mem_align(size
);
330 spin_lock(&gpmc_mem_lock
);
332 res
->end
= base
+ size
- 1;
333 r
= request_resource(&gpmc_mem_root
, res
);
334 spin_unlock(&gpmc_mem_lock
);
339 int gpmc_cs_request(int cs
, unsigned long size
, unsigned long *base
)
341 struct resource
*res
= &gpmc_cs_mem
[cs
];
344 if (cs
> GPMC_CS_NUM
)
347 size
= gpmc_mem_align(size
);
348 if (size
> (1 << GPMC_SECTION_SHIFT
))
351 spin_lock(&gpmc_mem_lock
);
352 if (gpmc_cs_reserved(cs
)) {
356 if (gpmc_cs_mem_enabled(cs
))
357 r
= adjust_resource(res
, res
->start
& ~(size
- 1), size
);
359 r
= allocate_resource(&gpmc_mem_root
, res
, size
, 0, ~0,
364 gpmc_cs_enable_mem(cs
, res
->start
, res
->end
- res
->start
+ 1);
366 gpmc_cs_set_reserved(cs
, 1);
368 spin_unlock(&gpmc_mem_lock
);
371 EXPORT_SYMBOL(gpmc_cs_request
);
373 void gpmc_cs_free(int cs
)
375 spin_lock(&gpmc_mem_lock
);
376 if (cs
>= GPMC_CS_NUM
|| !gpmc_cs_reserved(cs
)) {
377 printk(KERN_ERR
"Trying to free non-reserved GPMC CS%d\n", cs
);
379 spin_unlock(&gpmc_mem_lock
);
382 gpmc_cs_disable_mem(cs
);
383 release_resource(&gpmc_cs_mem
[cs
]);
384 gpmc_cs_set_reserved(cs
, 0);
385 spin_unlock(&gpmc_mem_lock
);
387 EXPORT_SYMBOL(gpmc_cs_free
);
389 static void __init
gpmc_mem_init(void)
392 unsigned long boot_rom_space
= 0;
394 /* never allocate the first page, to facilitate bug detection;
395 * even if we didn't boot from ROM.
397 boot_rom_space
= BOOT_ROM_SPACE
;
398 /* In apollon the CS0 is mapped as 0x0000 0000 */
399 if (machine_is_omap_apollon())
401 gpmc_mem_root
.start
= GPMC_MEM_START
+ boot_rom_space
;
402 gpmc_mem_root
.end
= GPMC_MEM_END
;
404 /* Reserve all regions that has been set up by bootloader */
405 for (cs
= 0; cs
< GPMC_CS_NUM
; cs
++) {
408 if (!gpmc_cs_mem_enabled(cs
))
410 gpmc_cs_get_memconf(cs
, &base
, &size
);
411 if (gpmc_cs_insert_mem(cs
, base
, size
) < 0)
416 void __init
gpmc_init(void)
421 if (cpu_is_omap24xx()) {
423 if (cpu_is_omap2420())
424 l
= OMAP2420_GPMC_BASE
;
426 l
= OMAP34XX_GPMC_BASE
;
427 } else if (cpu_is_omap34xx()) {
429 l
= OMAP34XX_GPMC_BASE
;
430 } else if (cpu_is_omap44xx()) {
432 l
= OMAP44XX_GPMC_BASE
;
435 gpmc_l3_clk
= clk_get(NULL
, ck
);
436 if (IS_ERR(gpmc_l3_clk
)) {
437 printk(KERN_ERR
"Could not get GPMC clock %s\n", ck
);
441 gpmc_base
= ioremap(l
, SZ_4K
);
443 clk_put(gpmc_l3_clk
);
444 printk(KERN_ERR
"Could not get GPMC register memory\n");
448 l
= gpmc_read_reg(GPMC_REVISION
);
449 printk(KERN_INFO
"GPMC revision %d.%d\n", (l
>> 4) & 0x0f, l
& 0x0f);
450 /* Set smart idle mode and automatic L3 clock gating */
451 l
= gpmc_read_reg(GPMC_SYSCONFIG
);
453 l
|= (0x02 << 3) | (1 << 0);
454 gpmc_write_reg(GPMC_SYSCONFIG
, l
);