blob: 5bc3ca03551c23656e7d46732b5ccfe495e1a72a [file] [log] [blame]
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001/*
2 * GPMC support functions
3 *
4 * Copyright (C) 2005-2006 Nokia Corporation
5 *
6 * Author: Juha Yrjola
7 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07008 * Copyright (C) 2009 Texas Instruments
9 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
10 *
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070011 * 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.
14 */
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030015#undef DEBUG
16
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070017#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/err.h>
20#include <linux/clk.h>
Imre Deakf37e4582006-09-25 12:41:33 +030021#include <linux/ioport.h>
22#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010023#include <linux/io.h>
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030024#include <linux/module.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070025
Kyungmin Park7f245162006-12-29 16:48:51 -080026#include <asm/mach-types.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070027#include <plat/gpmc.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070028
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/sdrc.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070030
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030031/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070032#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
Thara Gopinath15e02a32008-04-28 16:55:01 +053044#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070045#define GPMC_PREFETCH_STATUS 0x1f0
46#define GPMC_ECC_CONFIG 0x1f4
47#define GPMC_ECC_CONTROL 0x1f8
48#define GPMC_ECC_SIZE_CONFIG 0x1fc
49
50#define GPMC_CS0 0x60
51#define GPMC_CS_SIZE 0x30
52
Imre Deakf37e4582006-09-25 12:41:33 +030053#define GPMC_MEM_START 0x00000000
54#define GPMC_MEM_END 0x3FFFFFFF
55#define BOOT_ROM_SPACE 0x100000 /* 1MB */
56
57#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
58#define GPMC_SECTION_SHIFT 28 /* 128 MB */
59
vimal singh59e9c5a2009-07-13 16:26:24 +053060#define PREFETCH_FIFOTHRESHOLD (0x40 << 8)
61#define CS_NUM_SHIFT 24
62#define ENABLE_PREFETCH (0x1 << 7)
63#define DMA_MPU_MODE 2
64
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +053065/* Structure to save gpmc cs context */
66struct gpmc_cs_config {
67 u32 config1;
68 u32 config2;
69 u32 config3;
70 u32 config4;
71 u32 config5;
72 u32 config6;
73 u32 config7;
74 int is_valid;
75};
76
77/*
78 * Structure to save/restore gpmc context
79 * to support core off on OMAP3
80 */
81struct omap3_gpmc_regs {
82 u32 sysconfig;
83 u32 irqenable;
84 u32 timeout_ctrl;
85 u32 config;
86 u32 prefetch_config1;
87 u32 prefetch_config2;
88 u32 prefetch_control;
89 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
90};
91
Imre Deakf37e4582006-09-25 12:41:33 +030092static struct resource gpmc_mem_root;
93static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -070094static DEFINE_SPINLOCK(gpmc_mem_lock);
Imre Deakf37e4582006-09-25 12:41:33 +030095static unsigned gpmc_cs_map;
96
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030097static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070098
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030099static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700100
101static void gpmc_write_reg(int idx, u32 val)
102{
103 __raw_writel(val, gpmc_base + idx);
104}
105
106static u32 gpmc_read_reg(int idx)
107{
108 return __raw_readl(gpmc_base + idx);
109}
110
111void gpmc_cs_write_reg(int cs, int idx, u32 val)
112{
113 void __iomem *reg_addr;
114
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300115 reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700116 __raw_writel(val, reg_addr);
117}
118
119u32 gpmc_cs_read_reg(int cs, int idx)
120{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300121 void __iomem *reg_addr;
122
123 reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
124 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700125}
126
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300127/* TODO: Add support for gpmc_fck to clock framework and use it */
David Brownell1c22cc12006-12-06 17:13:55 -0800128unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700129{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300130 unsigned long rate = clk_get_rate(gpmc_l3_clk);
131
132 if (rate == 0) {
133 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
134 return 0;
135 }
136
137 rate /= 1000;
138 rate = 1000000000 / rate; /* In picoseconds */
139
140 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700141}
142
143unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
144{
145 unsigned long tick_ps;
146
147 /* Calculate in picosecs to yield more exact results */
148 tick_ps = gpmc_get_fclk_period();
149
150 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
151}
152
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300153unsigned int gpmc_ticks_to_ns(unsigned int ticks)
154{
155 return ticks * gpmc_get_fclk_period() / 1000;
156}
157
Kai Svahn23300592007-01-26 12:29:40 -0800158unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
159{
160 unsigned long ticks = gpmc_ns_to_ticks(time_ns);
161
162 return ticks * gpmc_get_fclk_period() / 1000;
163}
164
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700165#ifdef DEBUG
166static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700167 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700168#else
169static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
170 int time)
171#endif
172{
173 u32 l;
174 int ticks, mask, nr_bits;
175
176 if (time == 0)
177 ticks = 0;
178 else
179 ticks = gpmc_ns_to_ticks(time);
180 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800181 if (ticks >= 1 << nr_bits) {
182#ifdef DEBUG
183 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
184 cs, name, time, ticks, 1 << nr_bits);
185#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700186 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800187 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700188
189 mask = (1 << nr_bits) - 1;
190 l = gpmc_cs_read_reg(cs, reg);
191#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800192 printk(KERN_INFO
193 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700194 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800195 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700196#endif
197 l &= ~(mask << st_bit);
198 l |= ticks << st_bit;
199 gpmc_cs_write_reg(cs, reg, l);
200
201 return 0;
202}
203
204#ifdef DEBUG
205#define GPMC_SET_ONE(reg, st, end, field) \
206 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
207 t->field, #field) < 0) \
208 return -1
209#else
210#define GPMC_SET_ONE(reg, st, end, field) \
211 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
212 return -1
213#endif
214
215int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
216{
217 int div;
218 u32 l;
219
220 l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
221 div = l / gpmc_get_fclk_period();
222 if (div > 4)
223 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800224 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700225 div = 1;
226
227 return div;
228}
229
230int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
231{
232 int div;
233 u32 l;
234
235 div = gpmc_cs_calc_divider(cs, t->sync_clk);
236 if (div < 0)
237 return -1;
238
239 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
240 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
241 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
242
243 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
244 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
245 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
246
247 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
248 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
249 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
250 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
251
252 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
253 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
254 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
255
256 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
257
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300258 if (cpu_is_omap34xx()) {
259 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
260 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
261 }
262
David Brownell1c22cc12006-12-06 17:13:55 -0800263 /* caller is expected to have initialized CONFIG1 to cover
264 * at least sync vs async
265 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700266 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800267 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
268#ifdef DEBUG
269 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
270 cs, (div * gpmc_get_fclk_period()) / 1000, div);
271#endif
272 l &= ~0x03;
273 l |= (div - 1);
274 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
275 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700276
277 return 0;
278}
279
Imre Deakf37e4582006-09-25 12:41:33 +0300280static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700281{
Imre Deakf37e4582006-09-25 12:41:33 +0300282 u32 l;
283 u32 mask;
284
285 mask = (1 << GPMC_SECTION_SHIFT) - size;
286 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
287 l &= ~0x3f;
288 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
289 l &= ~(0x0f << 8);
290 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530291 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300292 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
293}
294
295static void gpmc_cs_disable_mem(int cs)
296{
297 u32 l;
298
299 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530300 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300301 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
302}
303
304static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
305{
306 u32 l;
307 u32 mask;
308
309 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
310 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
311 mask = (l >> 8) & 0x0f;
312 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
313}
314
315static int gpmc_cs_mem_enabled(int cs)
316{
317 u32 l;
318
319 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530320 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300321}
322
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800323int gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300324{
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800325 if (cs > GPMC_CS_NUM)
326 return -ENODEV;
327
Imre Deakf37e4582006-09-25 12:41:33 +0300328 gpmc_cs_map &= ~(1 << cs);
329 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800330
331 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300332}
333
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800334int gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300335{
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800336 if (cs > GPMC_CS_NUM)
337 return -ENODEV;
338
Imre Deakf37e4582006-09-25 12:41:33 +0300339 return gpmc_cs_map & (1 << cs);
340}
341
342static unsigned long gpmc_mem_align(unsigned long size)
343{
344 int order;
345
346 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
347 order = GPMC_CHUNK_SHIFT - 1;
348 do {
349 size >>= 1;
350 order++;
351 } while (size);
352 size = 1 << order;
353 return size;
354}
355
356static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
357{
358 struct resource *res = &gpmc_cs_mem[cs];
359 int r;
360
361 size = gpmc_mem_align(size);
362 spin_lock(&gpmc_mem_lock);
363 res->start = base;
364 res->end = base + size - 1;
365 r = request_resource(&gpmc_mem_root, res);
366 spin_unlock(&gpmc_mem_lock);
367
368 return r;
369}
370
371int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
372{
373 struct resource *res = &gpmc_cs_mem[cs];
374 int r = -1;
375
376 if (cs > GPMC_CS_NUM)
377 return -ENODEV;
378
379 size = gpmc_mem_align(size);
380 if (size > (1 << GPMC_SECTION_SHIFT))
381 return -ENOMEM;
382
383 spin_lock(&gpmc_mem_lock);
384 if (gpmc_cs_reserved(cs)) {
385 r = -EBUSY;
386 goto out;
387 }
388 if (gpmc_cs_mem_enabled(cs))
389 r = adjust_resource(res, res->start & ~(size - 1), size);
390 if (r < 0)
391 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
392 size, NULL, NULL);
393 if (r < 0)
394 goto out;
395
Tobias Klauser6d135242009-11-10 18:55:19 -0800396 gpmc_cs_enable_mem(cs, res->start, resource_size(res));
Imre Deakf37e4582006-09-25 12:41:33 +0300397 *base = res->start;
398 gpmc_cs_set_reserved(cs, 1);
399out:
400 spin_unlock(&gpmc_mem_lock);
401 return r;
402}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300403EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300404
405void gpmc_cs_free(int cs)
406{
407 spin_lock(&gpmc_mem_lock);
Roel Kluine7fdc602009-11-17 14:39:06 -0800408 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300409 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
410 BUG();
411 spin_unlock(&gpmc_mem_lock);
412 return;
413 }
414 gpmc_cs_disable_mem(cs);
415 release_resource(&gpmc_cs_mem[cs]);
416 gpmc_cs_set_reserved(cs, 0);
417 spin_unlock(&gpmc_mem_lock);
418}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300419EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300420
vimal singh59e9c5a2009-07-13 16:26:24 +0530421/**
422 * gpmc_prefetch_enable - configures and starts prefetch transfer
423 * @cs: nand cs (chip select) number
424 * @dma_mode: dma mode enable (1) or disable (0)
425 * @u32_count: number of bytes to be transferred
426 * @is_write: prefetch read(0) or write post(1) mode
427 */
428int gpmc_prefetch_enable(int cs, int dma_mode,
429 unsigned int u32_count, int is_write)
430{
431 uint32_t prefetch_config1;
432
433 if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
434 /* Set the amount of bytes to be prefetched */
435 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
436
437 /* Set dma/mpu mode, the prefetch read / post write and
438 * enable the engine. Set which cs is has requested for.
439 */
440 prefetch_config1 = ((cs << CS_NUM_SHIFT) |
441 PREFETCH_FIFOTHRESHOLD |
442 ENABLE_PREFETCH |
443 (dma_mode << DMA_MPU_MODE) |
444 (0x1 & is_write));
445 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, prefetch_config1);
446 } else {
447 return -EBUSY;
448 }
449 /* Start the prefetch engine */
450 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
451
452 return 0;
453}
454EXPORT_SYMBOL(gpmc_prefetch_enable);
455
456/**
457 * gpmc_prefetch_reset - disables and stops the prefetch engine
458 */
459void gpmc_prefetch_reset(void)
460{
461 /* Stop the PFPW engine */
462 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
463
464 /* Reset/disable the PFPW engine */
465 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
466}
467EXPORT_SYMBOL(gpmc_prefetch_reset);
468
469/**
470 * gpmc_prefetch_status - reads prefetch status of engine
471 */
472int gpmc_prefetch_status(void)
473{
474 return gpmc_read_reg(GPMC_PREFETCH_STATUS);
475}
476EXPORT_SYMBOL(gpmc_prefetch_status);
477
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300478static void __init gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300479{
480 int cs;
481 unsigned long boot_rom_space = 0;
482
Kyungmin Park7f245162006-12-29 16:48:51 -0800483 /* never allocate the first page, to facilitate bug detection;
484 * even if we didn't boot from ROM.
485 */
486 boot_rom_space = BOOT_ROM_SPACE;
487 /* In apollon the CS0 is mapped as 0x0000 0000 */
488 if (machine_is_omap_apollon())
489 boot_rom_space = 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300490 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
491 gpmc_mem_root.end = GPMC_MEM_END;
492
493 /* Reserve all regions that has been set up by bootloader */
494 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
495 u32 base, size;
496
497 if (!gpmc_cs_mem_enabled(cs))
498 continue;
499 gpmc_cs_get_memconf(cs, &base, &size);
500 if (gpmc_cs_insert_mem(cs, base, size) < 0)
501 BUG();
502 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700503}
504
505void __init gpmc_init(void)
506{
507 u32 l;
Kevin Hilman8d084362010-01-29 14:20:06 -0800508 char *ck = NULL;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700509
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300510 if (cpu_is_omap24xx()) {
511 ck = "core_l3_ck";
512 if (cpu_is_omap2420())
513 l = OMAP2420_GPMC_BASE;
514 else
515 l = OMAP34XX_GPMC_BASE;
516 } else if (cpu_is_omap34xx()) {
517 ck = "gpmc_fck";
518 l = OMAP34XX_GPMC_BASE;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700519 } else if (cpu_is_omap44xx()) {
Rajendra Nayakd79b1262009-12-09 00:01:44 +0530520 ck = "gpmc_ck";
Santosh Shilimkar44169072009-05-28 14:16:04 -0700521 l = OMAP44XX_GPMC_BASE;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300522 }
523
Kevin Hilman8d084362010-01-29 14:20:06 -0800524 if (WARN_ON(!ck))
525 return;
526
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300527 gpmc_l3_clk = clk_get(NULL, ck);
528 if (IS_ERR(gpmc_l3_clk)) {
529 printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
Sanjeev Premi85d7a072008-11-04 13:35:06 -0800530 BUG();
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300531 }
532
533 gpmc_base = ioremap(l, SZ_4K);
534 if (!gpmc_base) {
535 clk_put(gpmc_l3_clk);
536 printk(KERN_ERR "Could not get GPMC register memory\n");
Sanjeev Premi85d7a072008-11-04 13:35:06 -0800537 BUG();
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300538 }
539
Olof Johansson1daa8c12010-01-20 22:39:29 +0000540 clk_enable(gpmc_l3_clk);
541
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700542 l = gpmc_read_reg(GPMC_REVISION);
543 printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
544 /* Set smart idle mode and automatic L3 clock gating */
545 l = gpmc_read_reg(GPMC_SYSCONFIG);
546 l &= 0x03 << 3;
547 l |= (0x02 << 3) | (1 << 0);
548 gpmc_write_reg(GPMC_SYSCONFIG, l);
Imre Deakf37e4582006-09-25 12:41:33 +0300549 gpmc_mem_init();
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700550}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530551
552#ifdef CONFIG_ARCH_OMAP3
553static struct omap3_gpmc_regs gpmc_context;
554
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800555void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530556{
557 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800558
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530559 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
560 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
561 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
562 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
563 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
564 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
565 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
566 for (i = 0; i < GPMC_CS_NUM; i++) {
567 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
568 if (gpmc_context.cs_context[i].is_valid) {
569 gpmc_context.cs_context[i].config1 =
570 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
571 gpmc_context.cs_context[i].config2 =
572 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
573 gpmc_context.cs_context[i].config3 =
574 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
575 gpmc_context.cs_context[i].config4 =
576 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
577 gpmc_context.cs_context[i].config5 =
578 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
579 gpmc_context.cs_context[i].config6 =
580 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
581 gpmc_context.cs_context[i].config7 =
582 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
583 }
584 }
585}
586
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800587void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530588{
589 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800590
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530591 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
592 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
593 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
594 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
595 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
596 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
597 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
598 for (i = 0; i < GPMC_CS_NUM; i++) {
599 if (gpmc_context.cs_context[i].is_valid) {
600 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
601 gpmc_context.cs_context[i].config1);
602 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
603 gpmc_context.cs_context[i].config2);
604 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
605 gpmc_context.cs_context[i].config3);
606 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
607 gpmc_context.cs_context[i].config4);
608 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
609 gpmc_context.cs_context[i].config5);
610 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
611 gpmc_context.cs_context[i].config6);
612 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
613 gpmc_context.cs_context[i].config7);
614 }
615 }
616}
617#endif /* CONFIG_ARCH_OMAP3 */