blob: 382dea83e4f038a74f0677418f5212ab8e2b76aa [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
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +053017#include <linux/irq.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070018#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/err.h>
21#include <linux/clk.h>
Imre Deakf37e4582006-09-25 12:41:33 +030022#include <linux/ioport.h>
23#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010024#include <linux/io.h>
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030025#include <linux/module.h>
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +053026#include <linux/interrupt.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070027
Kyungmin Park7f245162006-12-29 16:48:51 -080028#include <asm/mach-types.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070029#include <plat/gpmc.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070030
Tony Lindgrence491cf2009-10-20 09:40:47 -070031#include <plat/sdrc.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070032
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030033/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070034#define GPMC_REVISION 0x00
35#define GPMC_SYSCONFIG 0x10
36#define GPMC_SYSSTATUS 0x14
37#define GPMC_IRQSTATUS 0x18
38#define GPMC_IRQENABLE 0x1c
39#define GPMC_TIMEOUT_CONTROL 0x40
40#define GPMC_ERR_ADDRESS 0x44
41#define GPMC_ERR_TYPE 0x48
42#define GPMC_CONFIG 0x50
43#define GPMC_STATUS 0x54
44#define GPMC_PREFETCH_CONFIG1 0x1e0
45#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053046#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070047#define GPMC_PREFETCH_STATUS 0x1f0
48#define GPMC_ECC_CONFIG 0x1f4
49#define GPMC_ECC_CONTROL 0x1f8
50#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000051#define GPMC_ECC1_RESULT 0x200
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070052
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000053#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070054#define GPMC_CS_SIZE 0x30
55
Imre Deakf37e4582006-09-25 12:41:33 +030056#define GPMC_MEM_START 0x00000000
57#define GPMC_MEM_END 0x3FFFFFFF
58#define BOOT_ROM_SPACE 0x100000 /* 1MB */
59
60#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
61#define GPMC_SECTION_SHIFT 28 /* 128 MB */
62
vimal singh59e9c5a2009-07-13 16:26:24 +053063#define PREFETCH_FIFOTHRESHOLD (0x40 << 8)
64#define CS_NUM_SHIFT 24
65#define ENABLE_PREFETCH (0x1 << 7)
66#define DMA_MPU_MODE 2
67
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +053068/* Structure to save gpmc cs context */
69struct gpmc_cs_config {
70 u32 config1;
71 u32 config2;
72 u32 config3;
73 u32 config4;
74 u32 config5;
75 u32 config6;
76 u32 config7;
77 int is_valid;
78};
79
80/*
81 * Structure to save/restore gpmc context
82 * to support core off on OMAP3
83 */
84struct omap3_gpmc_regs {
85 u32 sysconfig;
86 u32 irqenable;
87 u32 timeout_ctrl;
88 u32 config;
89 u32 prefetch_config1;
90 u32 prefetch_config2;
91 u32 prefetch_control;
92 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
93};
94
Imre Deakf37e4582006-09-25 12:41:33 +030095static struct resource gpmc_mem_root;
96static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -070097static DEFINE_SPINLOCK(gpmc_mem_lock);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000098static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
99static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
Imre Deakf37e4582006-09-25 12:41:33 +0300100
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300101static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700102
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300103static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700104
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530105static irqreturn_t gpmc_handle_irq(int irq, void *dev);
106
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700107static void gpmc_write_reg(int idx, u32 val)
108{
109 __raw_writel(val, gpmc_base + idx);
110}
111
112static u32 gpmc_read_reg(int idx)
113{
114 return __raw_readl(gpmc_base + idx);
115}
116
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000117static void gpmc_cs_write_byte(int cs, int idx, u8 val)
118{
119 void __iomem *reg_addr;
120
121 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
122 __raw_writeb(val, reg_addr);
123}
124
125static u8 gpmc_cs_read_byte(int cs, int idx)
126{
127 void __iomem *reg_addr;
128
129 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
130 return __raw_readb(reg_addr);
131}
132
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700133void gpmc_cs_write_reg(int cs, int idx, u32 val)
134{
135 void __iomem *reg_addr;
136
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000137 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700138 __raw_writel(val, reg_addr);
139}
140
141u32 gpmc_cs_read_reg(int cs, int idx)
142{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300143 void __iomem *reg_addr;
144
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000145 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300146 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700147}
148
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300149/* TODO: Add support for gpmc_fck to clock framework and use it */
David Brownell1c22cc12006-12-06 17:13:55 -0800150unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700151{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300152 unsigned long rate = clk_get_rate(gpmc_l3_clk);
153
154 if (rate == 0) {
155 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
156 return 0;
157 }
158
159 rate /= 1000;
160 rate = 1000000000 / rate; /* In picoseconds */
161
162 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700163}
164
165unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
166{
167 unsigned long tick_ps;
168
169 /* Calculate in picosecs to yield more exact results */
170 tick_ps = gpmc_get_fclk_period();
171
172 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
173}
174
Adrian Huntera3551f52010-12-09 10:48:27 +0200175unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
176{
177 unsigned long tick_ps;
178
179 /* Calculate in picosecs to yield more exact results */
180 tick_ps = gpmc_get_fclk_period();
181
182 return (time_ps + tick_ps - 1) / tick_ps;
183}
184
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300185unsigned int gpmc_ticks_to_ns(unsigned int ticks)
186{
187 return ticks * gpmc_get_fclk_period() / 1000;
188}
189
Kai Svahn23300592007-01-26 12:29:40 -0800190unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
191{
192 unsigned long ticks = gpmc_ns_to_ticks(time_ns);
193
194 return ticks * gpmc_get_fclk_period() / 1000;
195}
196
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700197#ifdef DEBUG
198static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700199 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700200#else
201static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
202 int time)
203#endif
204{
205 u32 l;
206 int ticks, mask, nr_bits;
207
208 if (time == 0)
209 ticks = 0;
210 else
211 ticks = gpmc_ns_to_ticks(time);
212 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800213 if (ticks >= 1 << nr_bits) {
214#ifdef DEBUG
215 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
216 cs, name, time, ticks, 1 << nr_bits);
217#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700218 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800219 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700220
221 mask = (1 << nr_bits) - 1;
222 l = gpmc_cs_read_reg(cs, reg);
223#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800224 printk(KERN_INFO
225 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700226 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800227 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700228#endif
229 l &= ~(mask << st_bit);
230 l |= ticks << st_bit;
231 gpmc_cs_write_reg(cs, reg, l);
232
233 return 0;
234}
235
236#ifdef DEBUG
237#define GPMC_SET_ONE(reg, st, end, field) \
238 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
239 t->field, #field) < 0) \
240 return -1
241#else
242#define GPMC_SET_ONE(reg, st, end, field) \
243 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
244 return -1
245#endif
246
247int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
248{
249 int div;
250 u32 l;
251
Adrian Huntera3551f52010-12-09 10:48:27 +0200252 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700253 div = l / gpmc_get_fclk_period();
254 if (div > 4)
255 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800256 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700257 div = 1;
258
259 return div;
260}
261
262int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
263{
264 int div;
265 u32 l;
266
267 div = gpmc_cs_calc_divider(cs, t->sync_clk);
268 if (div < 0)
269 return -1;
270
271 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
272 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
273 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
274
275 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
276 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
277 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
278
279 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
280 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
281 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
282 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
283
284 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
285 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
286 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
287
288 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
289
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300290 if (cpu_is_omap34xx()) {
291 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
292 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
293 }
294
David Brownell1c22cc12006-12-06 17:13:55 -0800295 /* caller is expected to have initialized CONFIG1 to cover
296 * at least sync vs async
297 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700298 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800299 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
300#ifdef DEBUG
301 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
302 cs, (div * gpmc_get_fclk_period()) / 1000, div);
303#endif
304 l &= ~0x03;
305 l |= (div - 1);
306 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
307 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700308
309 return 0;
310}
311
Imre Deakf37e4582006-09-25 12:41:33 +0300312static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700313{
Imre Deakf37e4582006-09-25 12:41:33 +0300314 u32 l;
315 u32 mask;
316
317 mask = (1 << GPMC_SECTION_SHIFT) - size;
318 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
319 l &= ~0x3f;
320 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
321 l &= ~(0x0f << 8);
322 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530323 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300324 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
325}
326
327static void gpmc_cs_disable_mem(int cs)
328{
329 u32 l;
330
331 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530332 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300333 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
334}
335
336static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
337{
338 u32 l;
339 u32 mask;
340
341 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
342 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
343 mask = (l >> 8) & 0x0f;
344 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
345}
346
347static int gpmc_cs_mem_enabled(int cs)
348{
349 u32 l;
350
351 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530352 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300353}
354
Tony Lindgrenc40fae92006-12-07 13:58:10 -0800355int gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300356{
Tony Lindgrenc40fae92006-12-07 13:58:10 -0800357 if (cs > GPMC_CS_NUM)
358 return -ENODEV;
359
Imre Deakf37e4582006-09-25 12:41:33 +0300360 gpmc_cs_map &= ~(1 << cs);
361 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
Tony Lindgrenc40fae92006-12-07 13:58:10 -0800362
363 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300364}
365
Tony Lindgrenc40fae92006-12-07 13:58:10 -0800366int gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300367{
Tony Lindgrenc40fae92006-12-07 13:58:10 -0800368 if (cs > GPMC_CS_NUM)
369 return -ENODEV;
370
Imre Deakf37e4582006-09-25 12:41:33 +0300371 return gpmc_cs_map & (1 << cs);
372}
373
374static unsigned long gpmc_mem_align(unsigned long size)
375{
376 int order;
377
378 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
379 order = GPMC_CHUNK_SHIFT - 1;
380 do {
381 size >>= 1;
382 order++;
383 } while (size);
384 size = 1 << order;
385 return size;
386}
387
388static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
389{
390 struct resource *res = &gpmc_cs_mem[cs];
391 int r;
392
393 size = gpmc_mem_align(size);
394 spin_lock(&gpmc_mem_lock);
395 res->start = base;
396 res->end = base + size - 1;
397 r = request_resource(&gpmc_mem_root, res);
398 spin_unlock(&gpmc_mem_lock);
399
400 return r;
401}
402
403int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
404{
405 struct resource *res = &gpmc_cs_mem[cs];
406 int r = -1;
407
408 if (cs > GPMC_CS_NUM)
409 return -ENODEV;
410
411 size = gpmc_mem_align(size);
412 if (size > (1 << GPMC_SECTION_SHIFT))
413 return -ENOMEM;
414
415 spin_lock(&gpmc_mem_lock);
416 if (gpmc_cs_reserved(cs)) {
417 r = -EBUSY;
418 goto out;
419 }
420 if (gpmc_cs_mem_enabled(cs))
421 r = adjust_resource(res, res->start & ~(size - 1), size);
422 if (r < 0)
423 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
424 size, NULL, NULL);
425 if (r < 0)
426 goto out;
427
Tobias Klauser6d135242009-11-10 18:55:19 -0800428 gpmc_cs_enable_mem(cs, res->start, resource_size(res));
Imre Deakf37e4582006-09-25 12:41:33 +0300429 *base = res->start;
430 gpmc_cs_set_reserved(cs, 1);
431out:
432 spin_unlock(&gpmc_mem_lock);
433 return r;
434}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300435EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300436
437void gpmc_cs_free(int cs)
438{
439 spin_lock(&gpmc_mem_lock);
Roel Kluine7fdc602009-11-17 14:39:06 -0800440 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300441 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
442 BUG();
443 spin_unlock(&gpmc_mem_lock);
444 return;
445 }
446 gpmc_cs_disable_mem(cs);
447 release_resource(&gpmc_cs_mem[cs]);
448 gpmc_cs_set_reserved(cs, 0);
449 spin_unlock(&gpmc_mem_lock);
450}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300451EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300452
vimal singh59e9c5a2009-07-13 16:26:24 +0530453/**
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000454 * gpmc_read_status - read access request to get the different gpmc status
455 * @cmd: command type
456 * @return status
457 */
458int gpmc_read_status(int cmd)
459{
460 int status = -EINVAL;
461 u32 regval = 0;
462
463 switch (cmd) {
464 case GPMC_GET_IRQ_STATUS:
465 status = gpmc_read_reg(GPMC_IRQSTATUS);
466 break;
467
468 case GPMC_PREFETCH_FIFO_CNT:
469 regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
470 status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
471 break;
472
473 case GPMC_PREFETCH_COUNT:
474 regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
475 status = GPMC_PREFETCH_STATUS_COUNT(regval);
476 break;
477
478 case GPMC_STATUS_BUFFER:
479 regval = gpmc_read_reg(GPMC_STATUS);
480 /* 1 : buffer is available to write */
481 status = regval & GPMC_STATUS_BUFF_EMPTY;
482 break;
483
484 default:
485 printk(KERN_ERR "gpmc_read_status: Not supported\n");
486 }
487 return status;
488}
489EXPORT_SYMBOL(gpmc_read_status);
490
491/**
492 * gpmc_cs_configure - write request to configure gpmc
493 * @cs: chip select number
494 * @cmd: command type
495 * @wval: value to write
496 * @return status of the operation
497 */
498int gpmc_cs_configure(int cs, int cmd, int wval)
499{
500 int err = 0;
501 u32 regval = 0;
502
503 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530504 case GPMC_ENABLE_IRQ:
505 gpmc_write_reg(GPMC_IRQENABLE, wval);
506 break;
507
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000508 case GPMC_SET_IRQ_STATUS:
509 gpmc_write_reg(GPMC_IRQSTATUS, wval);
510 break;
511
512 case GPMC_CONFIG_WP:
513 regval = gpmc_read_reg(GPMC_CONFIG);
514 if (wval)
515 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
516 else
517 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
518 gpmc_write_reg(GPMC_CONFIG, regval);
519 break;
520
521 case GPMC_CONFIG_RDY_BSY:
522 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
523 if (wval)
524 regval |= WR_RD_PIN_MONITORING;
525 else
526 regval &= ~WR_RD_PIN_MONITORING;
527 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
528 break;
529
530 case GPMC_CONFIG_DEV_SIZE:
531 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
532 regval |= GPMC_CONFIG1_DEVICESIZE(wval);
533 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
534 break;
535
536 case GPMC_CONFIG_DEV_TYPE:
537 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
538 regval |= GPMC_CONFIG1_DEVICETYPE(wval);
539 if (wval == GPMC_DEVICETYPE_NOR)
540 regval |= GPMC_CONFIG1_MUXADDDATA;
541 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
542 break;
543
544 default:
545 printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
546 err = -EINVAL;
547 }
548
549 return err;
550}
551EXPORT_SYMBOL(gpmc_cs_configure);
552
553/**
554 * gpmc_nand_read - nand specific read access request
555 * @cs: chip select number
556 * @cmd: command type
557 */
558int gpmc_nand_read(int cs, int cmd)
559{
560 int rval = -EINVAL;
561
562 switch (cmd) {
563 case GPMC_NAND_DATA:
564 rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
565 break;
566
567 default:
568 printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
569 }
570 return rval;
571}
572EXPORT_SYMBOL(gpmc_nand_read);
573
574/**
575 * gpmc_nand_write - nand specific write request
576 * @cs: chip select number
577 * @cmd: command type
578 * @wval: value to write
579 */
580int gpmc_nand_write(int cs, int cmd, int wval)
581{
582 int err = 0;
583
584 switch (cmd) {
585 case GPMC_NAND_COMMAND:
586 gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
587 break;
588
589 case GPMC_NAND_ADDRESS:
590 gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
591 break;
592
593 case GPMC_NAND_DATA:
594 gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
595
596 default:
597 printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
598 err = -EINVAL;
599 }
600 return err;
601}
602EXPORT_SYMBOL(gpmc_nand_write);
603
604
605
606/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530607 * gpmc_prefetch_enable - configures and starts prefetch transfer
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000608 * @cs: cs (chip select) number
vimal singh59e9c5a2009-07-13 16:26:24 +0530609 * @dma_mode: dma mode enable (1) or disable (0)
610 * @u32_count: number of bytes to be transferred
611 * @is_write: prefetch read(0) or write post(1) mode
612 */
613int gpmc_prefetch_enable(int cs, int dma_mode,
614 unsigned int u32_count, int is_write)
615{
vimal singh59e9c5a2009-07-13 16:26:24 +0530616
617 if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
618 /* Set the amount of bytes to be prefetched */
619 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
620
621 /* Set dma/mpu mode, the prefetch read / post write and
622 * enable the engine. Set which cs is has requested for.
623 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000624 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
vimal singh59e9c5a2009-07-13 16:26:24 +0530625 PREFETCH_FIFOTHRESHOLD |
626 ENABLE_PREFETCH |
627 (dma_mode << DMA_MPU_MODE) |
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000628 (0x1 & is_write)));
629
630 /* Start the prefetch engine */
631 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
vimal singh59e9c5a2009-07-13 16:26:24 +0530632 } else {
633 return -EBUSY;
634 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530635
636 return 0;
637}
638EXPORT_SYMBOL(gpmc_prefetch_enable);
639
640/**
641 * gpmc_prefetch_reset - disables and stops the prefetch engine
642 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000643int gpmc_prefetch_reset(int cs)
vimal singh59e9c5a2009-07-13 16:26:24 +0530644{
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000645 u32 config1;
646
647 /* check if the same module/cs is trying to reset */
648 config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
649 if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
650 return -EINVAL;
651
vimal singh59e9c5a2009-07-13 16:26:24 +0530652 /* Stop the PFPW engine */
653 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
654
655 /* Reset/disable the PFPW engine */
656 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000657
658 return 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530659}
660EXPORT_SYMBOL(gpmc_prefetch_reset);
661
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300662static void __init gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300663{
664 int cs;
665 unsigned long boot_rom_space = 0;
666
Kyungmin Park7f245162006-12-29 16:48:51 -0800667 /* never allocate the first page, to facilitate bug detection;
668 * even if we didn't boot from ROM.
669 */
670 boot_rom_space = BOOT_ROM_SPACE;
671 /* In apollon the CS0 is mapped as 0x0000 0000 */
672 if (machine_is_omap_apollon())
673 boot_rom_space = 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300674 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
675 gpmc_mem_root.end = GPMC_MEM_END;
676
677 /* Reserve all regions that has been set up by bootloader */
678 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
679 u32 base, size;
680
681 if (!gpmc_cs_mem_enabled(cs))
682 continue;
683 gpmc_cs_get_memconf(cs, &base, &size);
684 if (gpmc_cs_insert_mem(cs, base, size) < 0)
685 BUG();
686 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700687}
688
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530689static int __init gpmc_init(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700690{
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530691 u32 l, irq;
692 int cs, ret = -EINVAL;
Kevin Hilman8d084362010-01-29 14:20:06 -0800693 char *ck = NULL;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700694
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300695 if (cpu_is_omap24xx()) {
696 ck = "core_l3_ck";
697 if (cpu_is_omap2420())
698 l = OMAP2420_GPMC_BASE;
699 else
700 l = OMAP34XX_GPMC_BASE;
701 } else if (cpu_is_omap34xx()) {
702 ck = "gpmc_fck";
703 l = OMAP34XX_GPMC_BASE;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700704 } else if (cpu_is_omap44xx()) {
Rajendra Nayakd79b1262009-12-09 00:01:44 +0530705 ck = "gpmc_ck";
Santosh Shilimkar44169072009-05-28 14:16:04 -0700706 l = OMAP44XX_GPMC_BASE;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300707 }
708
Kevin Hilman8d084362010-01-29 14:20:06 -0800709 if (WARN_ON(!ck))
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530710 return ret;
Kevin Hilman8d084362010-01-29 14:20:06 -0800711
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300712 gpmc_l3_clk = clk_get(NULL, ck);
713 if (IS_ERR(gpmc_l3_clk)) {
714 printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
Sanjeev Premi85d7a072008-11-04 13:35:06 -0800715 BUG();
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300716 }
717
718 gpmc_base = ioremap(l, SZ_4K);
719 if (!gpmc_base) {
720 clk_put(gpmc_l3_clk);
721 printk(KERN_ERR "Could not get GPMC register memory\n");
Sanjeev Premi85d7a072008-11-04 13:35:06 -0800722 BUG();
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300723 }
724
Olof Johansson1daa8c12010-01-20 22:39:29 +0000725 clk_enable(gpmc_l3_clk);
726
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700727 l = gpmc_read_reg(GPMC_REVISION);
728 printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
729 /* Set smart idle mode and automatic L3 clock gating */
730 l = gpmc_read_reg(GPMC_SYSCONFIG);
731 l &= 0x03 << 3;
732 l |= (0x02 << 3) | (1 << 0);
733 gpmc_write_reg(GPMC_SYSCONFIG, l);
Imre Deakf37e4582006-09-25 12:41:33 +0300734 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530735
736 /* initalize the irq_chained */
737 irq = OMAP_GPMC_IRQ_BASE;
738 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
739 set_irq_handler(irq, handle_simple_irq);
740 set_irq_flags(irq, IRQF_VALID);
741 irq++;
742 }
743
744 ret = request_irq(INT_34XX_GPMC_IRQ,
745 gpmc_handle_irq, IRQF_SHARED, "gpmc", gpmc_base);
746 if (ret)
747 pr_err("gpmc: irq-%d could not claim: err %d\n",
748 INT_34XX_GPMC_IRQ, ret);
749 return ret;
750}
751postcore_initcall(gpmc_init);
752
753static irqreturn_t gpmc_handle_irq(int irq, void *dev)
754{
755 u8 cs;
756
757 if (irq != INT_34XX_GPMC_IRQ)
758 return IRQ_HANDLED;
759 /* check cs to invoke the irq */
760 cs = ((gpmc_read_reg(GPMC_PREFETCH_CONFIG1)) >> CS_NUM_SHIFT) & 0x7;
761 if (OMAP_GPMC_IRQ_BASE+cs <= OMAP_GPMC_IRQ_END)
762 generic_handle_irq(OMAP_GPMC_IRQ_BASE+cs);
763
764 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700765}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530766
767#ifdef CONFIG_ARCH_OMAP3
768static struct omap3_gpmc_regs gpmc_context;
769
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800770void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530771{
772 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800773
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530774 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
775 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
776 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
777 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
778 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
779 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
780 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
781 for (i = 0; i < GPMC_CS_NUM; i++) {
782 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
783 if (gpmc_context.cs_context[i].is_valid) {
784 gpmc_context.cs_context[i].config1 =
785 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
786 gpmc_context.cs_context[i].config2 =
787 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
788 gpmc_context.cs_context[i].config3 =
789 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
790 gpmc_context.cs_context[i].config4 =
791 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
792 gpmc_context.cs_context[i].config5 =
793 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
794 gpmc_context.cs_context[i].config6 =
795 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
796 gpmc_context.cs_context[i].config7 =
797 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
798 }
799 }
800}
801
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800802void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530803{
804 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800805
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530806 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
807 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
808 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
809 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
810 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
811 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
812 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
813 for (i = 0; i < GPMC_CS_NUM; i++) {
814 if (gpmc_context.cs_context[i].is_valid) {
815 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
816 gpmc_context.cs_context[i].config1);
817 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
818 gpmc_context.cs_context[i].config2);
819 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
820 gpmc_context.cs_context[i].config3);
821 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
822 gpmc_context.cs_context[i].config4);
823 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
824 gpmc_context.cs_context[i].config5);
825 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
826 gpmc_context.cs_context[i].config6);
827 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
828 gpmc_context.cs_context[i].config7);
829 }
830 }
831}
832#endif /* CONFIG_ARCH_OMAP3 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000833
834/**
835 * gpmc_enable_hwecc - enable hardware ecc functionality
836 * @cs: chip select number
837 * @mode: read/write mode
838 * @dev_width: device bus width(1 for x16, 0 for x8)
839 * @ecc_size: bytes for which ECC will be generated
840 */
841int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
842{
843 unsigned int val;
844
845 /* check if ecc module is in used */
846 if (gpmc_ecc_used != -EINVAL)
847 return -EINVAL;
848
849 gpmc_ecc_used = cs;
850
851 /* clear ecc and enable bits */
852 val = ((0x00000001<<8) | 0x00000001);
853 gpmc_write_reg(GPMC_ECC_CONTROL, val);
854
855 /* program ecc and result sizes */
856 val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
857 gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
858
859 switch (mode) {
860 case GPMC_ECC_READ:
861 gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
862 break;
863 case GPMC_ECC_READSYN:
864 gpmc_write_reg(GPMC_ECC_CONTROL, 0x100);
865 break;
866 case GPMC_ECC_WRITE:
867 gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
868 break;
869 default:
870 printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
871 break;
872 }
873
874 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
875 val = (dev_width << 7) | (cs << 1) | (0x1);
876 gpmc_write_reg(GPMC_ECC_CONFIG, val);
877 return 0;
878}
879
880/**
881 * gpmc_calculate_ecc - generate non-inverted ecc bytes
882 * @cs: chip select number
883 * @dat: data pointer over which ecc is computed
884 * @ecc_code: ecc code buffer
885 *
886 * Using non-inverted ECC is considered ugly since writing a blank
887 * page (padding) will clear the ECC bytes. This is not a problem as long
888 * no one is trying to write data on the seemingly unused page. Reading
889 * an erased page will produce an ECC mismatch between generated and read
890 * ECC bytes that has to be dealt with separately.
891 */
892int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
893{
894 unsigned int val = 0x0;
895
896 if (gpmc_ecc_used != cs)
897 return -EINVAL;
898
899 /* read ecc result */
900 val = gpmc_read_reg(GPMC_ECC1_RESULT);
901 *ecc_code++ = val; /* P128e, ..., P1e */
902 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
903 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
904 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
905
906 gpmc_ecc_used = -EINVAL;
907 return 0;
908}