blob: 1ca8d7fd625a44c4567e314e4fc594cfc7b98d11 [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
Ivan Djelic8d602cf2012-04-26 14:17:49 +020052#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070053
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000054#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070055#define GPMC_CS_SIZE 0x30
56
Imre Deakf37e4582006-09-25 12:41:33 +030057#define GPMC_MEM_START 0x00000000
58#define GPMC_MEM_END 0x3FFFFFFF
59#define BOOT_ROM_SPACE 0x100000 /* 1MB */
60
61#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
62#define GPMC_SECTION_SHIFT 28 /* 128 MB */
63
vimal singh59e9c5a2009-07-13 16:26:24 +053064#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 Lindgrenc40fae952006-12-07 13:58:10 -0800355int gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300356{
Tony Lindgrenc40fae952006-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 Lindgrenc40fae952006-12-07 13:58:10 -0800362
363 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300364}
365
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800366int gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300367{
Tony Lindgrenc40fae952006-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);
Yegor Yefremov8ef5d842012-01-23 08:32:23 +0100532
533 /* clear 2 target bits */
534 regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
535
536 /* set the proper value */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000537 regval |= GPMC_CONFIG1_DEVICESIZE(wval);
Yegor Yefremov8ef5d842012-01-23 08:32:23 +0100538
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000539 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
540 break;
541
542 case GPMC_CONFIG_DEV_TYPE:
543 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
544 regval |= GPMC_CONFIG1_DEVICETYPE(wval);
545 if (wval == GPMC_DEVICETYPE_NOR)
546 regval |= GPMC_CONFIG1_MUXADDDATA;
547 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
548 break;
549
550 default:
551 printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
552 err = -EINVAL;
553 }
554
555 return err;
556}
557EXPORT_SYMBOL(gpmc_cs_configure);
558
559/**
560 * gpmc_nand_read - nand specific read access request
561 * @cs: chip select number
562 * @cmd: command type
563 */
564int gpmc_nand_read(int cs, int cmd)
565{
566 int rval = -EINVAL;
567
568 switch (cmd) {
569 case GPMC_NAND_DATA:
570 rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
571 break;
572
573 default:
574 printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
575 }
576 return rval;
577}
578EXPORT_SYMBOL(gpmc_nand_read);
579
580/**
581 * gpmc_nand_write - nand specific write request
582 * @cs: chip select number
583 * @cmd: command type
584 * @wval: value to write
585 */
586int gpmc_nand_write(int cs, int cmd, int wval)
587{
588 int err = 0;
589
590 switch (cmd) {
591 case GPMC_NAND_COMMAND:
592 gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
593 break;
594
595 case GPMC_NAND_ADDRESS:
596 gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
597 break;
598
599 case GPMC_NAND_DATA:
600 gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
601
602 default:
603 printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
604 err = -EINVAL;
605 }
606 return err;
607}
608EXPORT_SYMBOL(gpmc_nand_write);
609
610
611
612/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530613 * gpmc_prefetch_enable - configures and starts prefetch transfer
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000614 * @cs: cs (chip select) number
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530615 * @fifo_th: fifo threshold to be used for read/ write
vimal singh59e9c5a2009-07-13 16:26:24 +0530616 * @dma_mode: dma mode enable (1) or disable (0)
617 * @u32_count: number of bytes to be transferred
618 * @is_write: prefetch read(0) or write post(1) mode
619 */
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530620int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
vimal singh59e9c5a2009-07-13 16:26:24 +0530621 unsigned int u32_count, int is_write)
622{
vimal singh59e9c5a2009-07-13 16:26:24 +0530623
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530624 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
625 pr_err("gpmc: fifo threshold is not supported\n");
626 return -1;
627 } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530628 /* Set the amount of bytes to be prefetched */
629 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
630
631 /* Set dma/mpu mode, the prefetch read / post write and
632 * enable the engine. Set which cs is has requested for.
633 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000634 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530635 PREFETCH_FIFOTHRESHOLD(fifo_th) |
vimal singh59e9c5a2009-07-13 16:26:24 +0530636 ENABLE_PREFETCH |
637 (dma_mode << DMA_MPU_MODE) |
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000638 (0x1 & is_write)));
639
640 /* Start the prefetch engine */
641 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
vimal singh59e9c5a2009-07-13 16:26:24 +0530642 } else {
643 return -EBUSY;
644 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530645
646 return 0;
647}
648EXPORT_SYMBOL(gpmc_prefetch_enable);
649
650/**
651 * gpmc_prefetch_reset - disables and stops the prefetch engine
652 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000653int gpmc_prefetch_reset(int cs)
vimal singh59e9c5a2009-07-13 16:26:24 +0530654{
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000655 u32 config1;
656
657 /* check if the same module/cs is trying to reset */
658 config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
659 if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
660 return -EINVAL;
661
vimal singh59e9c5a2009-07-13 16:26:24 +0530662 /* Stop the PFPW engine */
663 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
664
665 /* Reset/disable the PFPW engine */
666 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000667
668 return 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530669}
670EXPORT_SYMBOL(gpmc_prefetch_reset);
671
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300672static void __init gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300673{
674 int cs;
675 unsigned long boot_rom_space = 0;
676
Kyungmin Park7f245162006-12-29 16:48:51 -0800677 /* never allocate the first page, to facilitate bug detection;
678 * even if we didn't boot from ROM.
679 */
680 boot_rom_space = BOOT_ROM_SPACE;
681 /* In apollon the CS0 is mapped as 0x0000 0000 */
682 if (machine_is_omap_apollon())
683 boot_rom_space = 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300684 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
685 gpmc_mem_root.end = GPMC_MEM_END;
686
687 /* Reserve all regions that has been set up by bootloader */
688 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
689 u32 base, size;
690
691 if (!gpmc_cs_mem_enabled(cs))
692 continue;
693 gpmc_cs_get_memconf(cs, &base, &size);
694 if (gpmc_cs_insert_mem(cs, base, size) < 0)
695 BUG();
696 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700697}
698
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530699static int __init gpmc_init(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700700{
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530701 u32 l, irq;
702 int cs, ret = -EINVAL;
Balaji T K77aded22011-03-18 16:53:20 -0700703 int gpmc_irq;
Kevin Hilman8d084362010-01-29 14:20:06 -0800704 char *ck = NULL;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700705
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300706 if (cpu_is_omap24xx()) {
707 ck = "core_l3_ck";
708 if (cpu_is_omap2420())
709 l = OMAP2420_GPMC_BASE;
710 else
711 l = OMAP34XX_GPMC_BASE;
Balaji T K77aded22011-03-18 16:53:20 -0700712 gpmc_irq = INT_34XX_GPMC_IRQ;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300713 } else if (cpu_is_omap34xx()) {
714 ck = "gpmc_fck";
715 l = OMAP34XX_GPMC_BASE;
Balaji T K77aded22011-03-18 16:53:20 -0700716 gpmc_irq = INT_34XX_GPMC_IRQ;
Santosh Shilimkar44169072009-05-28 14:16:04 -0700717 } else if (cpu_is_omap44xx()) {
Rajendra Nayakd79b1262009-12-09 00:01:44 +0530718 ck = "gpmc_ck";
Santosh Shilimkar44169072009-05-28 14:16:04 -0700719 l = OMAP44XX_GPMC_BASE;
Balaji T K77aded22011-03-18 16:53:20 -0700720 gpmc_irq = OMAP44XX_IRQ_GPMC;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300721 }
722
Kevin Hilman8d084362010-01-29 14:20:06 -0800723 if (WARN_ON(!ck))
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530724 return ret;
Kevin Hilman8d084362010-01-29 14:20:06 -0800725
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300726 gpmc_l3_clk = clk_get(NULL, ck);
727 if (IS_ERR(gpmc_l3_clk)) {
728 printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
Sanjeev Premi85d7a072008-11-04 13:35:06 -0800729 BUG();
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300730 }
731
732 gpmc_base = ioremap(l, SZ_4K);
733 if (!gpmc_base) {
734 clk_put(gpmc_l3_clk);
735 printk(KERN_ERR "Could not get GPMC register memory\n");
Sanjeev Premi85d7a072008-11-04 13:35:06 -0800736 BUG();
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300737 }
738
Olof Johansson1daa8c12010-01-20 22:39:29 +0000739 clk_enable(gpmc_l3_clk);
740
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700741 l = gpmc_read_reg(GPMC_REVISION);
742 printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
743 /* Set smart idle mode and automatic L3 clock gating */
744 l = gpmc_read_reg(GPMC_SYSCONFIG);
745 l &= 0x03 << 3;
746 l |= (0x02 << 3) | (1 << 0);
747 gpmc_write_reg(GPMC_SYSCONFIG, l);
Imre Deakf37e4582006-09-25 12:41:33 +0300748 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530749
750 /* initalize the irq_chained */
751 irq = OMAP_GPMC_IRQ_BASE;
752 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
Thomas Gleixner6845664a2011-03-24 13:25:22 +0100753 irq_set_chip_and_handler(irq, &dummy_irq_chip,
Balaji T K77aded22011-03-18 16:53:20 -0700754 handle_simple_irq);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530755 set_irq_flags(irq, IRQF_VALID);
756 irq++;
757 }
758
Balaji T K77aded22011-03-18 16:53:20 -0700759 ret = request_irq(gpmc_irq,
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530760 gpmc_handle_irq, IRQF_SHARED, "gpmc", gpmc_base);
761 if (ret)
762 pr_err("gpmc: irq-%d could not claim: err %d\n",
Balaji T K77aded22011-03-18 16:53:20 -0700763 gpmc_irq, ret);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530764 return ret;
765}
766postcore_initcall(gpmc_init);
767
768static irqreturn_t gpmc_handle_irq(int irq, void *dev)
769{
770 u8 cs;
771
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530772 /* check cs to invoke the irq */
773 cs = ((gpmc_read_reg(GPMC_PREFETCH_CONFIG1)) >> CS_NUM_SHIFT) & 0x7;
774 if (OMAP_GPMC_IRQ_BASE+cs <= OMAP_GPMC_IRQ_END)
775 generic_handle_irq(OMAP_GPMC_IRQ_BASE+cs);
776
777 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700778}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530779
780#ifdef CONFIG_ARCH_OMAP3
781static struct omap3_gpmc_regs gpmc_context;
782
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800783void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530784{
785 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800786
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530787 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
788 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
789 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
790 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
791 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
792 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
793 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
794 for (i = 0; i < GPMC_CS_NUM; i++) {
795 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
796 if (gpmc_context.cs_context[i].is_valid) {
797 gpmc_context.cs_context[i].config1 =
798 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
799 gpmc_context.cs_context[i].config2 =
800 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
801 gpmc_context.cs_context[i].config3 =
802 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
803 gpmc_context.cs_context[i].config4 =
804 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
805 gpmc_context.cs_context[i].config5 =
806 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
807 gpmc_context.cs_context[i].config6 =
808 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
809 gpmc_context.cs_context[i].config7 =
810 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
811 }
812 }
813}
814
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800815void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530816{
817 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -0800818
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530819 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
820 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
821 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
822 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
823 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
824 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
825 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
826 for (i = 0; i < GPMC_CS_NUM; i++) {
827 if (gpmc_context.cs_context[i].is_valid) {
828 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
829 gpmc_context.cs_context[i].config1);
830 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
831 gpmc_context.cs_context[i].config2);
832 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
833 gpmc_context.cs_context[i].config3);
834 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
835 gpmc_context.cs_context[i].config4);
836 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
837 gpmc_context.cs_context[i].config5);
838 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
839 gpmc_context.cs_context[i].config6);
840 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
841 gpmc_context.cs_context[i].config7);
842 }
843 }
844}
845#endif /* CONFIG_ARCH_OMAP3 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000846
847/**
848 * gpmc_enable_hwecc - enable hardware ecc functionality
849 * @cs: chip select number
850 * @mode: read/write mode
851 * @dev_width: device bus width(1 for x16, 0 for x8)
852 * @ecc_size: bytes for which ECC will be generated
853 */
854int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
855{
856 unsigned int val;
857
858 /* check if ecc module is in used */
859 if (gpmc_ecc_used != -EINVAL)
860 return -EINVAL;
861
862 gpmc_ecc_used = cs;
863
864 /* clear ecc and enable bits */
865 val = ((0x00000001<<8) | 0x00000001);
866 gpmc_write_reg(GPMC_ECC_CONTROL, val);
867
868 /* program ecc and result sizes */
869 val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
870 gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
871
872 switch (mode) {
873 case GPMC_ECC_READ:
874 gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
875 break;
876 case GPMC_ECC_READSYN:
877 gpmc_write_reg(GPMC_ECC_CONTROL, 0x100);
878 break;
879 case GPMC_ECC_WRITE:
880 gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
881 break;
882 default:
883 printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
884 break;
885 }
886
887 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
888 val = (dev_width << 7) | (cs << 1) | (0x1);
889 gpmc_write_reg(GPMC_ECC_CONFIG, val);
890 return 0;
891}
Bernhard Wallef611b022012-03-05 16:11:01 -0800892EXPORT_SYMBOL_GPL(gpmc_enable_hwecc);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000893
894/**
895 * gpmc_calculate_ecc - generate non-inverted ecc bytes
896 * @cs: chip select number
897 * @dat: data pointer over which ecc is computed
898 * @ecc_code: ecc code buffer
899 *
900 * Using non-inverted ECC is considered ugly since writing a blank
901 * page (padding) will clear the ECC bytes. This is not a problem as long
902 * no one is trying to write data on the seemingly unused page. Reading
903 * an erased page will produce an ECC mismatch between generated and read
904 * ECC bytes that has to be dealt with separately.
905 */
906int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
907{
908 unsigned int val = 0x0;
909
910 if (gpmc_ecc_used != cs)
911 return -EINVAL;
912
913 /* read ecc result */
914 val = gpmc_read_reg(GPMC_ECC1_RESULT);
915 *ecc_code++ = val; /* P128e, ..., P1e */
916 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
917 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
918 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
919
920 gpmc_ecc_used = -EINVAL;
921 return 0;
922}
Bernhard Wallef611b022012-03-05 16:11:01 -0800923EXPORT_SYMBOL_GPL(gpmc_calculate_ecc);
Ivan Djelic8d602cf2012-04-26 14:17:49 +0200924
925#ifdef CONFIG_ARCH_OMAP3
926
927/**
928 * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality
929 * @cs: chip select number
930 * @nsectors: how many 512-byte sectors to process
931 * @nerrors: how many errors to correct per sector (4 or 8)
932 *
933 * This function must be executed before any call to gpmc_enable_hwecc_bch.
934 */
935int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors)
936{
937 /* check if ecc module is in use */
938 if (gpmc_ecc_used != -EINVAL)
939 return -EINVAL;
940
941 /* support only OMAP3 class */
942 if (!cpu_is_omap34xx()) {
943 printk(KERN_ERR "BCH ecc is not supported on this CPU\n");
944 return -EINVAL;
945 }
946
947 /*
948 * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
949 * Other chips may be added if confirmed to work.
950 */
951 if ((nerrors == 4) &&
952 (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
953 printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
954 return -EINVAL;
955 }
956
957 /* sanity check */
958 if (nsectors > 8) {
959 printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n",
960 nsectors);
961 return -EINVAL;
962 }
963
964 return 0;
965}
966EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch);
967
968/**
969 * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality
970 * @cs: chip select number
971 * @mode: read/write mode
972 * @dev_width: device bus width(1 for x16, 0 for x8)
973 * @nsectors: how many 512-byte sectors to process
974 * @nerrors: how many errors to correct per sector (4 or 8)
975 */
976int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors,
977 int nerrors)
978{
979 unsigned int val;
980
981 /* check if ecc module is in use */
982 if (gpmc_ecc_used != -EINVAL)
983 return -EINVAL;
984
985 gpmc_ecc_used = cs;
986
987 /* clear ecc and enable bits */
988 gpmc_write_reg(GPMC_ECC_CONTROL, 0x1);
989
990 /*
991 * When using BCH, sector size is hardcoded to 512 bytes.
992 * Here we are using wrapping mode 6 both for reading and writing, with:
993 * size0 = 0 (no additional protected byte in spare area)
994 * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
995 */
996 gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12));
997
998 /* BCH configuration */
999 val = ((1 << 16) | /* enable BCH */
1000 (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
1001 (0x06 << 8) | /* wrap mode = 6 */
1002 (dev_width << 7) | /* bus width */
1003 (((nsectors-1) & 0x7) << 4) | /* number of sectors */
1004 (cs << 1) | /* ECC CS */
1005 (0x1)); /* enable ECC */
1006
1007 gpmc_write_reg(GPMC_ECC_CONFIG, val);
1008 gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
1009 return 0;
1010}
1011EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch);
1012
1013/**
1014 * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes
1015 * @cs: chip select number
1016 * @dat: The pointer to data on which ecc is computed
1017 * @ecc: The ecc output buffer
1018 */
1019int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc)
1020{
1021 int i;
1022 unsigned long nsectors, reg, val1, val2;
1023
1024 if (gpmc_ecc_used != cs)
1025 return -EINVAL;
1026
1027 nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
1028
1029 for (i = 0; i < nsectors; i++) {
1030
1031 reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
1032
1033 /* Read hw-computed remainder */
1034 val1 = gpmc_read_reg(reg + 0);
1035 val2 = gpmc_read_reg(reg + 4);
1036
1037 /*
1038 * Add constant polynomial to remainder, in order to get an ecc
1039 * sequence of 0xFFs for a buffer filled with 0xFFs; and
1040 * left-justify the resulting polynomial.
1041 */
1042 *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
1043 *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
1044 *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
1045 *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
1046 *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
1047 *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
1048 *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
1049 }
1050
1051 gpmc_ecc_used = -EINVAL;
1052 return 0;
1053}
1054EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4);
1055
1056/**
1057 * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes
1058 * @cs: chip select number
1059 * @dat: The pointer to data on which ecc is computed
1060 * @ecc: The ecc output buffer
1061 */
1062int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc)
1063{
1064 int i;
1065 unsigned long nsectors, reg, val1, val2, val3, val4;
1066
1067 if (gpmc_ecc_used != cs)
1068 return -EINVAL;
1069
1070 nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
1071
1072 for (i = 0; i < nsectors; i++) {
1073
1074 reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
1075
1076 /* Read hw-computed remainder */
1077 val1 = gpmc_read_reg(reg + 0);
1078 val2 = gpmc_read_reg(reg + 4);
1079 val3 = gpmc_read_reg(reg + 8);
1080 val4 = gpmc_read_reg(reg + 12);
1081
1082 /*
1083 * Add constant polynomial to remainder, in order to get an ecc
1084 * sequence of 0xFFs for a buffer filled with 0xFFs.
1085 */
1086 *ecc++ = 0xef ^ (val4 & 0xFF);
1087 *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
1088 *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
1089 *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
1090 *ecc++ = 0xed ^ (val3 & 0xFF);
1091 *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
1092 *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
1093 *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
1094 *ecc++ = 0x97 ^ (val2 & 0xFF);
1095 *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
1096 *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
1097 *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
1098 *ecc++ = 0xb5 ^ (val1 & 0xFF);
1099 }
1100
1101 gpmc_ecc_used = -EINVAL;
1102 return 0;
1103}
1104EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8);
1105
1106#endif /* CONFIG_ARCH_OMAP3 */