blob: 34823b3092f36380b3b723474ae1f063cb456841 [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>
Afzal Mohammedda496872012-09-23 17:28:25 -060027#include <linux/platform_device.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070028
Afzal Mohammedbc3668e2012-09-29 12:26:13 +053029#include <linux/platform_data/mtd-nand-omap2.h>
30
Kyungmin Park7f245162006-12-29 16:48:51 -080031#include <asm/mach-types.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070032#include <plat/gpmc.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070033
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070034#include <plat/cpu.h>
Tony Lindgrendbc04162012-08-31 10:59:07 -070035#include <plat/gpmc.h>
Tony Lindgrence491cf2009-10-20 09:40:47 -070036#include <plat/sdrc.h>
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060037#include <plat/omap_device.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070038
Tony Lindgrendbc04162012-08-31 10:59:07 -070039#include "soc.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070040#include "common.h"
41
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060042#define DEVICE_NAME "omap-gpmc"
43
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030044/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070045#define GPMC_REVISION 0x00
46#define GPMC_SYSCONFIG 0x10
47#define GPMC_SYSSTATUS 0x14
48#define GPMC_IRQSTATUS 0x18
49#define GPMC_IRQENABLE 0x1c
50#define GPMC_TIMEOUT_CONTROL 0x40
51#define GPMC_ERR_ADDRESS 0x44
52#define GPMC_ERR_TYPE 0x48
53#define GPMC_CONFIG 0x50
54#define GPMC_STATUS 0x54
55#define GPMC_PREFETCH_CONFIG1 0x1e0
56#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053057#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070058#define GPMC_PREFETCH_STATUS 0x1f0
59#define GPMC_ECC_CONFIG 0x1f4
60#define GPMC_ECC_CONTROL 0x1f8
61#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000062#define GPMC_ECC1_RESULT 0x200
Ivan Djelic8d602cf2012-04-26 14:17:49 +020063#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070064
Yegor Yefremov2c65e742012-05-09 08:32:49 -070065/* GPMC ECC control settings */
66#define GPMC_ECC_CTRL_ECCCLEAR 0x100
67#define GPMC_ECC_CTRL_ECCDISABLE 0x000
68#define GPMC_ECC_CTRL_ECCREG1 0x001
69#define GPMC_ECC_CTRL_ECCREG2 0x002
70#define GPMC_ECC_CTRL_ECCREG3 0x003
71#define GPMC_ECC_CTRL_ECCREG4 0x004
72#define GPMC_ECC_CTRL_ECCREG5 0x005
73#define GPMC_ECC_CTRL_ECCREG6 0x006
74#define GPMC_ECC_CTRL_ECCREG7 0x007
75#define GPMC_ECC_CTRL_ECCREG8 0x008
76#define GPMC_ECC_CTRL_ECCREG9 0x009
77
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000078#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070079#define GPMC_CS_SIZE 0x30
80
Imre Deakf37e4582006-09-25 12:41:33 +030081#define GPMC_MEM_START 0x00000000
82#define GPMC_MEM_END 0x3FFFFFFF
83#define BOOT_ROM_SPACE 0x100000 /* 1MB */
84
85#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
86#define GPMC_SECTION_SHIFT 28 /* 128 MB */
87
vimal singh59e9c5a2009-07-13 16:26:24 +053088#define CS_NUM_SHIFT 24
89#define ENABLE_PREFETCH (0x1 << 7)
90#define DMA_MPU_MODE 2
91
Afzal Mohammedda496872012-09-23 17:28:25 -060092#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
93#define GPMC_REVISION_MINOR(l) (l & 0xf)
94
95#define GPMC_HAS_WR_ACCESS 0x1
96#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
97
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -070098/* XXX: Only NAND irq has been considered,currently these are the only ones used
99 */
100#define GPMC_NR_IRQ 2
101
102struct gpmc_client_irq {
103 unsigned irq;
104 u32 bitmask;
105};
106
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530107/* Structure to save gpmc cs context */
108struct gpmc_cs_config {
109 u32 config1;
110 u32 config2;
111 u32 config3;
112 u32 config4;
113 u32 config5;
114 u32 config6;
115 u32 config7;
116 int is_valid;
117};
118
119/*
120 * Structure to save/restore gpmc context
121 * to support core off on OMAP3
122 */
123struct omap3_gpmc_regs {
124 u32 sysconfig;
125 u32 irqenable;
126 u32 timeout_ctrl;
127 u32 config;
128 u32 prefetch_config1;
129 u32 prefetch_config2;
130 u32 prefetch_control;
131 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
132};
133
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700134static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
135static struct irq_chip gpmc_irq_chip;
136static unsigned gpmc_irq_start;
137
Imre Deakf37e4582006-09-25 12:41:33 +0300138static struct resource gpmc_mem_root;
139static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700140static DEFINE_SPINLOCK(gpmc_mem_lock);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000141static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
142static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
Afzal Mohammedda496872012-09-23 17:28:25 -0600143static struct device *gpmc_dev;
144static int gpmc_irq;
145static resource_size_t phys_base, mem_size;
146static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300147static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700148
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300149static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700150
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530151static irqreturn_t gpmc_handle_irq(int irq, void *dev);
152
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700153static void gpmc_write_reg(int idx, u32 val)
154{
155 __raw_writel(val, gpmc_base + idx);
156}
157
158static u32 gpmc_read_reg(int idx)
159{
160 return __raw_readl(gpmc_base + idx);
161}
162
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000163static void gpmc_cs_write_byte(int cs, int idx, u8 val)
164{
165 void __iomem *reg_addr;
166
167 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
168 __raw_writeb(val, reg_addr);
169}
170
171static u8 gpmc_cs_read_byte(int cs, int idx)
172{
173 void __iomem *reg_addr;
174
175 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
176 return __raw_readb(reg_addr);
177}
178
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700179void gpmc_cs_write_reg(int cs, int idx, u32 val)
180{
181 void __iomem *reg_addr;
182
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000183 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700184 __raw_writel(val, reg_addr);
185}
186
187u32 gpmc_cs_read_reg(int cs, int idx)
188{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300189 void __iomem *reg_addr;
190
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000191 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300192 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700193}
194
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300195/* TODO: Add support for gpmc_fck to clock framework and use it */
David Brownell1c22cc12006-12-06 17:13:55 -0800196unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700197{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300198 unsigned long rate = clk_get_rate(gpmc_l3_clk);
199
200 if (rate == 0) {
201 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
202 return 0;
203 }
204
205 rate /= 1000;
206 rate = 1000000000 / rate; /* In picoseconds */
207
208 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700209}
210
211unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
212{
213 unsigned long tick_ps;
214
215 /* Calculate in picosecs to yield more exact results */
216 tick_ps = gpmc_get_fclk_period();
217
218 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
219}
220
Adrian Huntera3551f52010-12-09 10:48:27 +0200221unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
222{
223 unsigned long tick_ps;
224
225 /* Calculate in picosecs to yield more exact results */
226 tick_ps = gpmc_get_fclk_period();
227
228 return (time_ps + tick_ps - 1) / tick_ps;
229}
230
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300231unsigned int gpmc_ticks_to_ns(unsigned int ticks)
232{
233 return ticks * gpmc_get_fclk_period() / 1000;
234}
235
Kai Svahn23300592007-01-26 12:29:40 -0800236unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
237{
238 unsigned long ticks = gpmc_ns_to_ticks(time_ns);
239
240 return ticks * gpmc_get_fclk_period() / 1000;
241}
242
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700243#ifdef DEBUG
244static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700245 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700246#else
247static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
248 int time)
249#endif
250{
251 u32 l;
252 int ticks, mask, nr_bits;
253
254 if (time == 0)
255 ticks = 0;
256 else
257 ticks = gpmc_ns_to_ticks(time);
258 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800259 if (ticks >= 1 << nr_bits) {
260#ifdef DEBUG
261 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
262 cs, name, time, ticks, 1 << nr_bits);
263#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700264 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800265 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700266
267 mask = (1 << nr_bits) - 1;
268 l = gpmc_cs_read_reg(cs, reg);
269#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800270 printk(KERN_INFO
271 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700272 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800273 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700274#endif
275 l &= ~(mask << st_bit);
276 l |= ticks << st_bit;
277 gpmc_cs_write_reg(cs, reg, l);
278
279 return 0;
280}
281
282#ifdef DEBUG
283#define GPMC_SET_ONE(reg, st, end, field) \
284 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
285 t->field, #field) < 0) \
286 return -1
287#else
288#define GPMC_SET_ONE(reg, st, end, field) \
289 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
290 return -1
291#endif
292
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530293int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700294{
295 int div;
296 u32 l;
297
Adrian Huntera3551f52010-12-09 10:48:27 +0200298 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700299 div = l / gpmc_get_fclk_period();
300 if (div > 4)
301 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800302 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700303 div = 1;
304
305 return div;
306}
307
308int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
309{
310 int div;
311 u32 l;
312
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530313 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700314 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600315 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700316
317 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
318 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
319 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
320
321 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
322 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
323 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
324
325 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
326 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
327 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
328 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
329
330 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
331 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
332 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
333
334 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
335
Afzal Mohammedda496872012-09-23 17:28:25 -0600336 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300337 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600338 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300339 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300340
David Brownell1c22cc12006-12-06 17:13:55 -0800341 /* caller is expected to have initialized CONFIG1 to cover
342 * at least sync vs async
343 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700344 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800345 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
346#ifdef DEBUG
347 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
348 cs, (div * gpmc_get_fclk_period()) / 1000, div);
349#endif
350 l &= ~0x03;
351 l |= (div - 1);
352 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
353 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700354
355 return 0;
356}
357
Imre Deakf37e4582006-09-25 12:41:33 +0300358static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700359{
Imre Deakf37e4582006-09-25 12:41:33 +0300360 u32 l;
361 u32 mask;
362
363 mask = (1 << GPMC_SECTION_SHIFT) - size;
364 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
365 l &= ~0x3f;
366 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
367 l &= ~(0x0f << 8);
368 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530369 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300370 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
371}
372
373static void gpmc_cs_disable_mem(int cs)
374{
375 u32 l;
376
377 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530378 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300379 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
380}
381
382static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
383{
384 u32 l;
385 u32 mask;
386
387 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
388 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
389 mask = (l >> 8) & 0x0f;
390 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
391}
392
393static int gpmc_cs_mem_enabled(int cs)
394{
395 u32 l;
396
397 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530398 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300399}
400
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800401int gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300402{
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800403 if (cs > GPMC_CS_NUM)
404 return -ENODEV;
405
Imre Deakf37e4582006-09-25 12:41:33 +0300406 gpmc_cs_map &= ~(1 << cs);
407 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800408
409 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300410}
411
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800412int gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300413{
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800414 if (cs > GPMC_CS_NUM)
415 return -ENODEV;
416
Imre Deakf37e4582006-09-25 12:41:33 +0300417 return gpmc_cs_map & (1 << cs);
418}
419
420static unsigned long gpmc_mem_align(unsigned long size)
421{
422 int order;
423
424 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
425 order = GPMC_CHUNK_SHIFT - 1;
426 do {
427 size >>= 1;
428 order++;
429 } while (size);
430 size = 1 << order;
431 return size;
432}
433
434static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
435{
436 struct resource *res = &gpmc_cs_mem[cs];
437 int r;
438
439 size = gpmc_mem_align(size);
440 spin_lock(&gpmc_mem_lock);
441 res->start = base;
442 res->end = base + size - 1;
443 r = request_resource(&gpmc_mem_root, res);
444 spin_unlock(&gpmc_mem_lock);
445
446 return r;
447}
448
Afzal Mohammedda496872012-09-23 17:28:25 -0600449static int gpmc_cs_delete_mem(int cs)
450{
451 struct resource *res = &gpmc_cs_mem[cs];
452 int r;
453
454 spin_lock(&gpmc_mem_lock);
455 r = release_resource(&gpmc_cs_mem[cs]);
456 res->start = 0;
457 res->end = 0;
458 spin_unlock(&gpmc_mem_lock);
459
460 return r;
461}
462
Imre Deakf37e4582006-09-25 12:41:33 +0300463int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
464{
465 struct resource *res = &gpmc_cs_mem[cs];
466 int r = -1;
467
468 if (cs > GPMC_CS_NUM)
469 return -ENODEV;
470
471 size = gpmc_mem_align(size);
472 if (size > (1 << GPMC_SECTION_SHIFT))
473 return -ENOMEM;
474
475 spin_lock(&gpmc_mem_lock);
476 if (gpmc_cs_reserved(cs)) {
477 r = -EBUSY;
478 goto out;
479 }
480 if (gpmc_cs_mem_enabled(cs))
481 r = adjust_resource(res, res->start & ~(size - 1), size);
482 if (r < 0)
483 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
484 size, NULL, NULL);
485 if (r < 0)
486 goto out;
487
Tobias Klauser6d135242009-11-10 18:55:19 -0800488 gpmc_cs_enable_mem(cs, res->start, resource_size(res));
Imre Deakf37e4582006-09-25 12:41:33 +0300489 *base = res->start;
490 gpmc_cs_set_reserved(cs, 1);
491out:
492 spin_unlock(&gpmc_mem_lock);
493 return r;
494}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300495EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300496
497void gpmc_cs_free(int cs)
498{
499 spin_lock(&gpmc_mem_lock);
Roel Kluine7fdc602009-11-17 14:39:06 -0800500 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300501 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
502 BUG();
503 spin_unlock(&gpmc_mem_lock);
504 return;
505 }
506 gpmc_cs_disable_mem(cs);
507 release_resource(&gpmc_cs_mem[cs]);
508 gpmc_cs_set_reserved(cs, 0);
509 spin_unlock(&gpmc_mem_lock);
510}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300511EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300512
vimal singh59e9c5a2009-07-13 16:26:24 +0530513/**
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000514 * gpmc_read_status - read access request to get the different gpmc status
515 * @cmd: command type
516 * @return status
517 */
518int gpmc_read_status(int cmd)
519{
520 int status = -EINVAL;
521 u32 regval = 0;
522
523 switch (cmd) {
524 case GPMC_GET_IRQ_STATUS:
525 status = gpmc_read_reg(GPMC_IRQSTATUS);
526 break;
527
528 case GPMC_PREFETCH_FIFO_CNT:
529 regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
530 status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
531 break;
532
533 case GPMC_PREFETCH_COUNT:
534 regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
535 status = GPMC_PREFETCH_STATUS_COUNT(regval);
536 break;
537
538 case GPMC_STATUS_BUFFER:
539 regval = gpmc_read_reg(GPMC_STATUS);
540 /* 1 : buffer is available to write */
541 status = regval & GPMC_STATUS_BUFF_EMPTY;
542 break;
543
544 default:
545 printk(KERN_ERR "gpmc_read_status: Not supported\n");
546 }
547 return status;
548}
549EXPORT_SYMBOL(gpmc_read_status);
550
551/**
552 * gpmc_cs_configure - write request to configure gpmc
553 * @cs: chip select number
554 * @cmd: command type
555 * @wval: value to write
556 * @return status of the operation
557 */
558int gpmc_cs_configure(int cs, int cmd, int wval)
559{
560 int err = 0;
561 u32 regval = 0;
562
563 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530564 case GPMC_ENABLE_IRQ:
565 gpmc_write_reg(GPMC_IRQENABLE, wval);
566 break;
567
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000568 case GPMC_SET_IRQ_STATUS:
569 gpmc_write_reg(GPMC_IRQSTATUS, wval);
570 break;
571
572 case GPMC_CONFIG_WP:
573 regval = gpmc_read_reg(GPMC_CONFIG);
574 if (wval)
575 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
576 else
577 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
578 gpmc_write_reg(GPMC_CONFIG, regval);
579 break;
580
581 case GPMC_CONFIG_RDY_BSY:
582 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
583 if (wval)
584 regval |= WR_RD_PIN_MONITORING;
585 else
586 regval &= ~WR_RD_PIN_MONITORING;
587 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
588 break;
589
590 case GPMC_CONFIG_DEV_SIZE:
591 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
Yegor Yefremov8ef5d842012-01-23 08:32:23 +0100592
593 /* clear 2 target bits */
594 regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
595
596 /* set the proper value */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000597 regval |= GPMC_CONFIG1_DEVICESIZE(wval);
Yegor Yefremov8ef5d842012-01-23 08:32:23 +0100598
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000599 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
600 break;
601
602 case GPMC_CONFIG_DEV_TYPE:
603 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
604 regval |= GPMC_CONFIG1_DEVICETYPE(wval);
605 if (wval == GPMC_DEVICETYPE_NOR)
606 regval |= GPMC_CONFIG1_MUXADDDATA;
607 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
608 break;
609
610 default:
611 printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
612 err = -EINVAL;
613 }
614
615 return err;
616}
617EXPORT_SYMBOL(gpmc_cs_configure);
618
619/**
620 * gpmc_nand_read - nand specific read access request
621 * @cs: chip select number
622 * @cmd: command type
623 */
624int gpmc_nand_read(int cs, int cmd)
625{
626 int rval = -EINVAL;
627
628 switch (cmd) {
629 case GPMC_NAND_DATA:
630 rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
631 break;
632
633 default:
634 printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
635 }
636 return rval;
637}
638EXPORT_SYMBOL(gpmc_nand_read);
639
640/**
641 * gpmc_nand_write - nand specific write request
642 * @cs: chip select number
643 * @cmd: command type
644 * @wval: value to write
645 */
646int gpmc_nand_write(int cs, int cmd, int wval)
647{
648 int err = 0;
649
650 switch (cmd) {
651 case GPMC_NAND_COMMAND:
652 gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
653 break;
654
655 case GPMC_NAND_ADDRESS:
656 gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
657 break;
658
659 case GPMC_NAND_DATA:
660 gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
661
662 default:
663 printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
664 err = -EINVAL;
665 }
666 return err;
667}
668EXPORT_SYMBOL(gpmc_nand_write);
669
670
671
672/**
vimal singh59e9c5a2009-07-13 16:26:24 +0530673 * gpmc_prefetch_enable - configures and starts prefetch transfer
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000674 * @cs: cs (chip select) number
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530675 * @fifo_th: fifo threshold to be used for read/ write
vimal singh59e9c5a2009-07-13 16:26:24 +0530676 * @dma_mode: dma mode enable (1) or disable (0)
677 * @u32_count: number of bytes to be transferred
678 * @is_write: prefetch read(0) or write post(1) mode
679 */
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530680int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
vimal singh59e9c5a2009-07-13 16:26:24 +0530681 unsigned int u32_count, int is_write)
682{
vimal singh59e9c5a2009-07-13 16:26:24 +0530683
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530684 if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
685 pr_err("gpmc: fifo threshold is not supported\n");
686 return -1;
687 } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
vimal singh59e9c5a2009-07-13 16:26:24 +0530688 /* Set the amount of bytes to be prefetched */
689 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
690
691 /* Set dma/mpu mode, the prefetch read / post write and
692 * enable the engine. Set which cs is has requested for.
693 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000694 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
Sukumar Ghorai317379a2011-01-28 15:42:07 +0530695 PREFETCH_FIFOTHRESHOLD(fifo_th) |
vimal singh59e9c5a2009-07-13 16:26:24 +0530696 ENABLE_PREFETCH |
697 (dma_mode << DMA_MPU_MODE) |
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000698 (0x1 & is_write)));
699
700 /* Start the prefetch engine */
701 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
vimal singh59e9c5a2009-07-13 16:26:24 +0530702 } else {
703 return -EBUSY;
704 }
vimal singh59e9c5a2009-07-13 16:26:24 +0530705
706 return 0;
707}
708EXPORT_SYMBOL(gpmc_prefetch_enable);
709
710/**
711 * gpmc_prefetch_reset - disables and stops the prefetch engine
712 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000713int gpmc_prefetch_reset(int cs)
vimal singh59e9c5a2009-07-13 16:26:24 +0530714{
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000715 u32 config1;
716
717 /* check if the same module/cs is trying to reset */
718 config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
719 if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
720 return -EINVAL;
721
vimal singh59e9c5a2009-07-13 16:26:24 +0530722 /* Stop the PFPW engine */
723 gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
724
725 /* Reset/disable the PFPW engine */
726 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000727
728 return 0;
vimal singh59e9c5a2009-07-13 16:26:24 +0530729}
730EXPORT_SYMBOL(gpmc_prefetch_reset);
731
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700732void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
733{
734 reg->gpmc_status = gpmc_base + GPMC_STATUS;
735 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
736 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
737 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
738 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
739 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
740 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
741 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
742 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
743 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
744 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
745 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
746 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
747 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
748 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
749 reg->gpmc_bch_result0 = gpmc_base + GPMC_ECC_BCH_RESULT_0;
750}
751
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700752int gpmc_get_client_irq(unsigned irq_config)
753{
754 int i;
755
756 if (hweight32(irq_config) > 1)
757 return 0;
758
759 for (i = 0; i < GPMC_NR_IRQ; i++)
760 if (gpmc_client_irq[i].bitmask & irq_config)
761 return gpmc_client_irq[i].irq;
762
763 return 0;
764}
765
766static int gpmc_irq_endis(unsigned irq, bool endis)
767{
768 int i;
769 u32 regval;
770
771 for (i = 0; i < GPMC_NR_IRQ; i++)
772 if (irq == gpmc_client_irq[i].irq) {
773 regval = gpmc_read_reg(GPMC_IRQENABLE);
774 if (endis)
775 regval |= gpmc_client_irq[i].bitmask;
776 else
777 regval &= ~gpmc_client_irq[i].bitmask;
778 gpmc_write_reg(GPMC_IRQENABLE, regval);
779 break;
780 }
781
782 return 0;
783}
784
785static void gpmc_irq_disable(struct irq_data *p)
786{
787 gpmc_irq_endis(p->irq, false);
788}
789
790static void gpmc_irq_enable(struct irq_data *p)
791{
792 gpmc_irq_endis(p->irq, true);
793}
794
795static void gpmc_irq_noop(struct irq_data *data) { }
796
797static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
798
Afzal Mohammedda496872012-09-23 17:28:25 -0600799static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700800{
801 int i;
802 u32 regval;
803
804 if (!gpmc_irq)
805 return -EINVAL;
806
807 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
808 if (IS_ERR_VALUE(gpmc_irq_start)) {
809 pr_err("irq_alloc_descs failed\n");
810 return gpmc_irq_start;
811 }
812
813 gpmc_irq_chip.name = "gpmc";
814 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
815 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
816 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
817 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
818 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
819 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
820 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
821
822 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
823 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
824
825 for (i = 0; i < GPMC_NR_IRQ; i++) {
826 gpmc_client_irq[i].irq = gpmc_irq_start + i;
827 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
828 &gpmc_irq_chip, handle_simple_irq);
829 set_irq_flags(gpmc_client_irq[i].irq,
830 IRQF_VALID | IRQF_NOAUTOEN);
831 }
832
833 /* Disable interrupts */
834 gpmc_write_reg(GPMC_IRQENABLE, 0);
835
836 /* clear interrupts */
837 regval = gpmc_read_reg(GPMC_IRQSTATUS);
838 gpmc_write_reg(GPMC_IRQSTATUS, regval);
839
840 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
841}
842
Afzal Mohammed61687c62012-10-04 14:01:57 +0530843static __devexit int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600844{
845 int i;
846
847 if (gpmc_irq)
848 free_irq(gpmc_irq, NULL);
849
850 for (i = 0; i < GPMC_NR_IRQ; i++) {
851 irq_set_handler(gpmc_client_irq[i].irq, NULL);
852 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
853 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
854 }
855
856 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
857
858 return 0;
859}
860
861static void __devexit gpmc_mem_exit(void)
862{
863 int cs;
864
865 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
866 if (!gpmc_cs_mem_enabled(cs))
867 continue;
868 gpmc_cs_delete_mem(cs);
869 }
870
871}
872
873static void __devinit gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300874{
875 int cs;
876 unsigned long boot_rom_space = 0;
877
Kyungmin Park7f245162006-12-29 16:48:51 -0800878 /* never allocate the first page, to facilitate bug detection;
879 * even if we didn't boot from ROM.
880 */
881 boot_rom_space = BOOT_ROM_SPACE;
882 /* In apollon the CS0 is mapped as 0x0000 0000 */
883 if (machine_is_omap_apollon())
884 boot_rom_space = 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300885 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
886 gpmc_mem_root.end = GPMC_MEM_END;
887
888 /* Reserve all regions that has been set up by bootloader */
889 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
890 u32 base, size;
891
892 if (!gpmc_cs_mem_enabled(cs))
893 continue;
894 gpmc_cs_get_memconf(cs, &base, &size);
895 if (gpmc_cs_insert_mem(cs, base, size) < 0)
896 BUG();
897 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700898}
899
Afzal Mohammedda496872012-09-23 17:28:25 -0600900static __devinit int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700901{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700902 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -0600903 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700904
Afzal Mohammedda496872012-09-23 17:28:25 -0600905 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
906 if (res == NULL)
907 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300908
Afzal Mohammedda496872012-09-23 17:28:25 -0600909 phys_base = res->start;
910 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -0800911
Afzal Mohammedda496872012-09-23 17:28:25 -0600912 gpmc_base = devm_request_and_ioremap(&pdev->dev, res);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300913 if (!gpmc_base) {
Afzal Mohammedda496872012-09-23 17:28:25 -0600914 dev_err(&pdev->dev, "error: request memory / ioremap\n");
915 return -EADDRNOTAVAIL;
916 }
917
918 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
919 if (res == NULL)
920 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
921 else
922 gpmc_irq = res->start;
923
924 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
925 if (IS_ERR(gpmc_l3_clk)) {
926 dev_err(&pdev->dev, "error: clk_get\n");
927 gpmc_irq = 0;
928 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300929 }
930
Rajendra Nayak4d7cb452012-09-22 02:24:16 -0600931 clk_prepare_enable(gpmc_l3_clk);
Olof Johansson1daa8c12010-01-20 22:39:29 +0000932
Afzal Mohammedda496872012-09-23 17:28:25 -0600933 gpmc_dev = &pdev->dev;
934
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700935 l = gpmc_read_reg(GPMC_REVISION);
Afzal Mohammedda496872012-09-23 17:28:25 -0600936 if (GPMC_REVISION_MAJOR(l) > 0x4)
937 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
938 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
939 GPMC_REVISION_MINOR(l));
940
Imre Deakf37e4582006-09-25 12:41:33 +0300941 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530942
Afzal Mohammedda496872012-09-23 17:28:25 -0600943 if (IS_ERR_VALUE(gpmc_setup_irq()))
944 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
945
946 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530947}
Afzal Mohammedda496872012-09-23 17:28:25 -0600948
Afzal Mohammed61687c62012-10-04 14:01:57 +0530949static __devexit int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -0600950{
951 gpmc_free_irq();
952 gpmc_mem_exit();
953 gpmc_dev = NULL;
954 return 0;
955}
956
957static struct platform_driver gpmc_driver = {
958 .probe = gpmc_probe,
959 .remove = __devexit_p(gpmc_remove),
960 .driver = {
961 .name = DEVICE_NAME,
962 .owner = THIS_MODULE,
963 },
964};
965
966static __init int gpmc_init(void)
967{
968 return platform_driver_register(&gpmc_driver);
969}
970
971static __exit void gpmc_exit(void)
972{
973 platform_driver_unregister(&gpmc_driver);
974
975}
976
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530977postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -0600978module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530979
Afzal Mohammed4be48fd2012-09-23 17:28:24 -0600980static int __init omap_gpmc_init(void)
981{
982 struct omap_hwmod *oh;
983 struct platform_device *pdev;
984 char *oh_name = "gpmc";
985
986 oh = omap_hwmod_lookup(oh_name);
987 if (!oh) {
988 pr_err("Could not look up %s\n", oh_name);
989 return -ENODEV;
990 }
991
992 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0, NULL, 0, 0);
993 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
994
995 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
996}
997postcore_initcall(omap_gpmc_init);
998
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530999static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1000{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001001 int i;
1002 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301003
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001004 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1005
1006 if (!regval)
1007 return IRQ_NONE;
1008
1009 for (i = 0; i < GPMC_NR_IRQ; i++)
1010 if (regval & gpmc_client_irq[i].bitmask)
1011 generic_handle_irq(gpmc_client_irq[i].irq);
1012
1013 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301014
1015 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001016}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301017
1018#ifdef CONFIG_ARCH_OMAP3
1019static struct omap3_gpmc_regs gpmc_context;
1020
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001021void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301022{
1023 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001024
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301025 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1026 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1027 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1028 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1029 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1030 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1031 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
1032 for (i = 0; i < GPMC_CS_NUM; i++) {
1033 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1034 if (gpmc_context.cs_context[i].is_valid) {
1035 gpmc_context.cs_context[i].config1 =
1036 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1037 gpmc_context.cs_context[i].config2 =
1038 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1039 gpmc_context.cs_context[i].config3 =
1040 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1041 gpmc_context.cs_context[i].config4 =
1042 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1043 gpmc_context.cs_context[i].config5 =
1044 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1045 gpmc_context.cs_context[i].config6 =
1046 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1047 gpmc_context.cs_context[i].config7 =
1048 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1049 }
1050 }
1051}
1052
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001053void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301054{
1055 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001056
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301057 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1058 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1059 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1060 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1061 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1062 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1063 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
1064 for (i = 0; i < GPMC_CS_NUM; i++) {
1065 if (gpmc_context.cs_context[i].is_valid) {
1066 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1067 gpmc_context.cs_context[i].config1);
1068 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1069 gpmc_context.cs_context[i].config2);
1070 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1071 gpmc_context.cs_context[i].config3);
1072 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1073 gpmc_context.cs_context[i].config4);
1074 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1075 gpmc_context.cs_context[i].config5);
1076 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1077 gpmc_context.cs_context[i].config6);
1078 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1079 gpmc_context.cs_context[i].config7);
1080 }
1081 }
1082}
1083#endif /* CONFIG_ARCH_OMAP3 */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001084
1085/**
1086 * gpmc_enable_hwecc - enable hardware ecc functionality
1087 * @cs: chip select number
1088 * @mode: read/write mode
1089 * @dev_width: device bus width(1 for x16, 0 for x8)
1090 * @ecc_size: bytes for which ECC will be generated
1091 */
1092int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
1093{
1094 unsigned int val;
1095
1096 /* check if ecc module is in used */
1097 if (gpmc_ecc_used != -EINVAL)
1098 return -EINVAL;
1099
1100 gpmc_ecc_used = cs;
1101
1102 /* clear ecc and enable bits */
Yegor Yefremov2c65e742012-05-09 08:32:49 -07001103 gpmc_write_reg(GPMC_ECC_CONTROL,
1104 GPMC_ECC_CTRL_ECCCLEAR |
1105 GPMC_ECC_CTRL_ECCREG1);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001106
1107 /* program ecc and result sizes */
1108 val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
1109 gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
1110
1111 switch (mode) {
1112 case GPMC_ECC_READ:
Yegor Yefremov2c65e742012-05-09 08:32:49 -07001113 case GPMC_ECC_WRITE:
1114 gpmc_write_reg(GPMC_ECC_CONTROL,
1115 GPMC_ECC_CTRL_ECCCLEAR |
1116 GPMC_ECC_CTRL_ECCREG1);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001117 break;
1118 case GPMC_ECC_READSYN:
Yegor Yefremov2c65e742012-05-09 08:32:49 -07001119 gpmc_write_reg(GPMC_ECC_CONTROL,
1120 GPMC_ECC_CTRL_ECCCLEAR |
1121 GPMC_ECC_CTRL_ECCDISABLE);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001122 break;
1123 default:
1124 printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
1125 break;
1126 }
1127
1128 /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
1129 val = (dev_width << 7) | (cs << 1) | (0x1);
1130 gpmc_write_reg(GPMC_ECC_CONFIG, val);
1131 return 0;
1132}
Bernhard Wallef611b022012-03-05 16:11:01 -08001133EXPORT_SYMBOL_GPL(gpmc_enable_hwecc);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001134
1135/**
1136 * gpmc_calculate_ecc - generate non-inverted ecc bytes
1137 * @cs: chip select number
1138 * @dat: data pointer over which ecc is computed
1139 * @ecc_code: ecc code buffer
1140 *
1141 * Using non-inverted ECC is considered ugly since writing a blank
1142 * page (padding) will clear the ECC bytes. This is not a problem as long
1143 * no one is trying to write data on the seemingly unused page. Reading
1144 * an erased page will produce an ECC mismatch between generated and read
1145 * ECC bytes that has to be dealt with separately.
1146 */
1147int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
1148{
1149 unsigned int val = 0x0;
1150
1151 if (gpmc_ecc_used != cs)
1152 return -EINVAL;
1153
1154 /* read ecc result */
1155 val = gpmc_read_reg(GPMC_ECC1_RESULT);
1156 *ecc_code++ = val; /* P128e, ..., P1e */
1157 *ecc_code++ = val >> 16; /* P128o, ..., P1o */
1158 /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
1159 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
1160
1161 gpmc_ecc_used = -EINVAL;
1162 return 0;
1163}
Bernhard Wallef611b022012-03-05 16:11:01 -08001164EXPORT_SYMBOL_GPL(gpmc_calculate_ecc);
Ivan Djelic8d602cf2012-04-26 14:17:49 +02001165
1166#ifdef CONFIG_ARCH_OMAP3
1167
1168/**
1169 * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality
1170 * @cs: chip select number
1171 * @nsectors: how many 512-byte sectors to process
1172 * @nerrors: how many errors to correct per sector (4 or 8)
1173 *
1174 * This function must be executed before any call to gpmc_enable_hwecc_bch.
1175 */
1176int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors)
1177{
1178 /* check if ecc module is in use */
1179 if (gpmc_ecc_used != -EINVAL)
1180 return -EINVAL;
1181
1182 /* support only OMAP3 class */
1183 if (!cpu_is_omap34xx()) {
1184 printk(KERN_ERR "BCH ecc is not supported on this CPU\n");
1185 return -EINVAL;
1186 }
1187
1188 /*
1189 * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
1190 * Other chips may be added if confirmed to work.
1191 */
1192 if ((nerrors == 4) &&
1193 (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
1194 printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
1195 return -EINVAL;
1196 }
1197
1198 /* sanity check */
1199 if (nsectors > 8) {
1200 printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n",
1201 nsectors);
1202 return -EINVAL;
1203 }
1204
1205 return 0;
1206}
1207EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch);
1208
1209/**
1210 * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality
1211 * @cs: chip select number
1212 * @mode: read/write mode
1213 * @dev_width: device bus width(1 for x16, 0 for x8)
1214 * @nsectors: how many 512-byte sectors to process
1215 * @nerrors: how many errors to correct per sector (4 or 8)
1216 */
1217int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors,
1218 int nerrors)
1219{
1220 unsigned int val;
1221
1222 /* check if ecc module is in use */
1223 if (gpmc_ecc_used != -EINVAL)
1224 return -EINVAL;
1225
1226 gpmc_ecc_used = cs;
1227
1228 /* clear ecc and enable bits */
1229 gpmc_write_reg(GPMC_ECC_CONTROL, 0x1);
1230
1231 /*
1232 * When using BCH, sector size is hardcoded to 512 bytes.
1233 * Here we are using wrapping mode 6 both for reading and writing, with:
1234 * size0 = 0 (no additional protected byte in spare area)
1235 * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
1236 */
1237 gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12));
1238
1239 /* BCH configuration */
1240 val = ((1 << 16) | /* enable BCH */
1241 (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
1242 (0x06 << 8) | /* wrap mode = 6 */
1243 (dev_width << 7) | /* bus width */
1244 (((nsectors-1) & 0x7) << 4) | /* number of sectors */
1245 (cs << 1) | /* ECC CS */
1246 (0x1)); /* enable ECC */
1247
1248 gpmc_write_reg(GPMC_ECC_CONFIG, val);
1249 gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
1250 return 0;
1251}
1252EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch);
1253
1254/**
1255 * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes
1256 * @cs: chip select number
1257 * @dat: The pointer to data on which ecc is computed
1258 * @ecc: The ecc output buffer
1259 */
1260int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc)
1261{
1262 int i;
1263 unsigned long nsectors, reg, val1, val2;
1264
1265 if (gpmc_ecc_used != cs)
1266 return -EINVAL;
1267
1268 nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
1269
1270 for (i = 0; i < nsectors; i++) {
1271
1272 reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
1273
1274 /* Read hw-computed remainder */
1275 val1 = gpmc_read_reg(reg + 0);
1276 val2 = gpmc_read_reg(reg + 4);
1277
1278 /*
1279 * Add constant polynomial to remainder, in order to get an ecc
1280 * sequence of 0xFFs for a buffer filled with 0xFFs; and
1281 * left-justify the resulting polynomial.
1282 */
1283 *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
1284 *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
1285 *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
1286 *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
1287 *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
1288 *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
1289 *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
1290 }
1291
1292 gpmc_ecc_used = -EINVAL;
1293 return 0;
1294}
1295EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4);
1296
1297/**
1298 * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes
1299 * @cs: chip select number
1300 * @dat: The pointer to data on which ecc is computed
1301 * @ecc: The ecc output buffer
1302 */
1303int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc)
1304{
1305 int i;
1306 unsigned long nsectors, reg, val1, val2, val3, val4;
1307
1308 if (gpmc_ecc_used != cs)
1309 return -EINVAL;
1310
1311 nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
1312
1313 for (i = 0; i < nsectors; i++) {
1314
1315 reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
1316
1317 /* Read hw-computed remainder */
1318 val1 = gpmc_read_reg(reg + 0);
1319 val2 = gpmc_read_reg(reg + 4);
1320 val3 = gpmc_read_reg(reg + 8);
1321 val4 = gpmc_read_reg(reg + 12);
1322
1323 /*
1324 * Add constant polynomial to remainder, in order to get an ecc
1325 * sequence of 0xFFs for a buffer filled with 0xFFs.
1326 */
1327 *ecc++ = 0xef ^ (val4 & 0xFF);
1328 *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
1329 *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
1330 *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
1331 *ecc++ = 0xed ^ (val3 & 0xFF);
1332 *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
1333 *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
1334 *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
1335 *ecc++ = 0x97 ^ (val2 & 0xFF);
1336 *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
1337 *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
1338 *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
1339 *ecc++ = 0xb5 ^ (val1 & 0xFF);
1340 }
1341
1342 gpmc_ecc_used = -EINVAL;
1343 return 0;
1344}
1345EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8);
1346
1347#endif /* CONFIG_ARCH_OMAP3 */