blob: 7c122bb5498aee1272efbb041bc8cc9bf2b73724 [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>
Daniel Mackbc6b1e72012-12-14 11:36:44 +010028#include <linux/of.h>
29#include <linux/of_mtd.h>
30#include <linux/of_device.h>
31#include <linux/mtd/nand.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070032
Afzal Mohammedbc3668e2012-09-29 12:26:13 +053033#include <linux/platform_data/mtd-nand-omap2.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070034
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070035#include <asm/mach-types.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070036
Tony Lindgrendbc04162012-08-31 10:59:07 -070037#include "soc.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070038#include "common.h"
Tony Lindgren25c7d492012-10-02 17:25:48 -070039#include "omap_device.h"
Afzal Mohammed3ef5d002012-10-05 10:37:27 +053040#include "gpmc.h"
Daniel Mackbc6b1e72012-12-14 11:36:44 +010041#include "gpmc-nand.h"
Ezequiel Garcia75d36252013-01-25 09:23:11 -030042#include "gpmc-onenand.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070043
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060044#define DEVICE_NAME "omap-gpmc"
45
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030046/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070047#define GPMC_REVISION 0x00
48#define GPMC_SYSCONFIG 0x10
49#define GPMC_SYSSTATUS 0x14
50#define GPMC_IRQSTATUS 0x18
51#define GPMC_IRQENABLE 0x1c
52#define GPMC_TIMEOUT_CONTROL 0x40
53#define GPMC_ERR_ADDRESS 0x44
54#define GPMC_ERR_TYPE 0x48
55#define GPMC_CONFIG 0x50
56#define GPMC_STATUS 0x54
57#define GPMC_PREFETCH_CONFIG1 0x1e0
58#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053059#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070060#define GPMC_PREFETCH_STATUS 0x1f0
61#define GPMC_ECC_CONFIG 0x1f4
62#define GPMC_ECC_CONTROL 0x1f8
63#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000064#define GPMC_ECC1_RESULT 0x200
Ivan Djelic8d602cf2012-04-26 14:17:49 +020065#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053066#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
67#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
68#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070069
Yegor Yefremov2c65e742012-05-09 08:32:49 -070070/* GPMC ECC control settings */
71#define GPMC_ECC_CTRL_ECCCLEAR 0x100
72#define GPMC_ECC_CTRL_ECCDISABLE 0x000
73#define GPMC_ECC_CTRL_ECCREG1 0x001
74#define GPMC_ECC_CTRL_ECCREG2 0x002
75#define GPMC_ECC_CTRL_ECCREG3 0x003
76#define GPMC_ECC_CTRL_ECCREG4 0x004
77#define GPMC_ECC_CTRL_ECCREG5 0x005
78#define GPMC_ECC_CTRL_ECCREG6 0x006
79#define GPMC_ECC_CTRL_ECCREG7 0x007
80#define GPMC_ECC_CTRL_ECCREG8 0x008
81#define GPMC_ECC_CTRL_ECCREG9 0x009
82
Afzal Mohammed559d94b2012-05-28 17:51:37 +053083#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
84#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
85#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
86#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
87#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
88#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
89
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000090#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070091#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053092#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070093
Imre Deakf37e4582006-09-25 12:41:33 +030094#define GPMC_MEM_START 0x00000000
95#define GPMC_MEM_END 0x3FFFFFFF
96#define BOOT_ROM_SPACE 0x100000 /* 1MB */
97
98#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
99#define GPMC_SECTION_SHIFT 28 /* 128 MB */
100
vimal singh59e9c5a2009-07-13 16:26:24 +0530101#define CS_NUM_SHIFT 24
102#define ENABLE_PREFETCH (0x1 << 7)
103#define DMA_MPU_MODE 2
104
Afzal Mohammedda496872012-09-23 17:28:25 -0600105#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
106#define GPMC_REVISION_MINOR(l) (l & 0xf)
107
108#define GPMC_HAS_WR_ACCESS 0x1
109#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
110
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700111/* XXX: Only NAND irq has been considered,currently these are the only ones used
112 */
113#define GPMC_NR_IRQ 2
114
115struct gpmc_client_irq {
116 unsigned irq;
117 u32 bitmask;
118};
119
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530120/* Structure to save gpmc cs context */
121struct gpmc_cs_config {
122 u32 config1;
123 u32 config2;
124 u32 config3;
125 u32 config4;
126 u32 config5;
127 u32 config6;
128 u32 config7;
129 int is_valid;
130};
131
132/*
133 * Structure to save/restore gpmc context
134 * to support core off on OMAP3
135 */
136struct omap3_gpmc_regs {
137 u32 sysconfig;
138 u32 irqenable;
139 u32 timeout_ctrl;
140 u32 config;
141 u32 prefetch_config1;
142 u32 prefetch_config2;
143 u32 prefetch_control;
144 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
145};
146
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700147static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
148static struct irq_chip gpmc_irq_chip;
149static unsigned gpmc_irq_start;
150
Imre Deakf37e4582006-09-25 12:41:33 +0300151static struct resource gpmc_mem_root;
152static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700153static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600154/* Define chip-selects as reserved by default until probe completes */
155static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
Afzal Mohammedda496872012-09-23 17:28:25 -0600156static struct device *gpmc_dev;
157static int gpmc_irq;
158static resource_size_t phys_base, mem_size;
159static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300160static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700161
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300162static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700163
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530164static irqreturn_t gpmc_handle_irq(int irq, void *dev);
165
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700166static void gpmc_write_reg(int idx, u32 val)
167{
168 __raw_writel(val, gpmc_base + idx);
169}
170
171static u32 gpmc_read_reg(int idx)
172{
173 return __raw_readl(gpmc_base + idx);
174}
175
176void gpmc_cs_write_reg(int cs, int idx, u32 val)
177{
178 void __iomem *reg_addr;
179
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000180 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700181 __raw_writel(val, reg_addr);
182}
183
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300184static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700185{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300186 void __iomem *reg_addr;
187
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000188 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300189 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700190}
191
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300192/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300193static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700194{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300195 unsigned long rate = clk_get_rate(gpmc_l3_clk);
196
197 if (rate == 0) {
198 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
199 return 0;
200 }
201
202 rate /= 1000;
203 rate = 1000000000 / rate; /* In picoseconds */
204
205 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700206}
207
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300208static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700209{
210 unsigned long tick_ps;
211
212 /* Calculate in picosecs to yield more exact results */
213 tick_ps = gpmc_get_fclk_period();
214
215 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
216}
217
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300218static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200219{
220 unsigned long tick_ps;
221
222 /* Calculate in picosecs to yield more exact results */
223 tick_ps = gpmc_get_fclk_period();
224
225 return (time_ps + tick_ps - 1) / tick_ps;
226}
227
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300228unsigned int gpmc_ticks_to_ns(unsigned int ticks)
229{
230 return ticks * gpmc_get_fclk_period() / 1000;
231}
232
Afzal Mohammed246da262012-08-02 20:02:10 +0530233static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
234{
235 return ticks * gpmc_get_fclk_period();
236}
237
238static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
239{
240 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
241
242 return ticks * gpmc_get_fclk_period();
243}
244
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530245static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
246{
247 u32 l;
248
249 l = gpmc_cs_read_reg(cs, reg);
250 if (value)
251 l |= mask;
252 else
253 l &= ~mask;
254 gpmc_cs_write_reg(cs, reg, l);
255}
256
257static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
258{
259 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
260 GPMC_CONFIG1_TIME_PARA_GRAN,
261 p->time_para_granularity);
262 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
263 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
264 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
265 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
266 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
267 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
268 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
269 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
270 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
271 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
272 p->cycle2cyclesamecsen);
273 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
274 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
275 p->cycle2cyclediffcsen);
276}
277
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700278#ifdef DEBUG
279static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700280 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700281#else
282static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
283 int time)
284#endif
285{
286 u32 l;
287 int ticks, mask, nr_bits;
288
289 if (time == 0)
290 ticks = 0;
291 else
292 ticks = gpmc_ns_to_ticks(time);
293 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800294 if (ticks >= 1 << nr_bits) {
295#ifdef DEBUG
296 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
297 cs, name, time, ticks, 1 << nr_bits);
298#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700299 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800300 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700301
302 mask = (1 << nr_bits) - 1;
303 l = gpmc_cs_read_reg(cs, reg);
304#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800305 printk(KERN_INFO
306 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700307 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800308 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700309#endif
310 l &= ~(mask << st_bit);
311 l |= ticks << st_bit;
312 gpmc_cs_write_reg(cs, reg, l);
313
314 return 0;
315}
316
317#ifdef DEBUG
318#define GPMC_SET_ONE(reg, st, end, field) \
319 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
320 t->field, #field) < 0) \
321 return -1
322#else
323#define GPMC_SET_ONE(reg, st, end, field) \
324 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
325 return -1
326#endif
327
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530328int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700329{
330 int div;
331 u32 l;
332
Adrian Huntera3551f52010-12-09 10:48:27 +0200333 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700334 div = l / gpmc_get_fclk_period();
335 if (div > 4)
336 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800337 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700338 div = 1;
339
340 return div;
341}
342
343int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
344{
345 int div;
346 u32 l;
347
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530348 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700349 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600350 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700351
352 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
353 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
354 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
355
356 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
357 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
358 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
359
360 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
361 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
362 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
363 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
364
365 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
366 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
367 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
368
369 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
370
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530371 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
372 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
373
374 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
375 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
376
Afzal Mohammedda496872012-09-23 17:28:25 -0600377 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300378 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600379 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300380 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300381
David Brownell1c22cc12006-12-06 17:13:55 -0800382 /* caller is expected to have initialized CONFIG1 to cover
383 * at least sync vs async
384 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700385 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800386 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
387#ifdef DEBUG
388 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
389 cs, (div * gpmc_get_fclk_period()) / 1000, div);
390#endif
391 l &= ~0x03;
392 l |= (div - 1);
393 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
394 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700395
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530396 gpmc_cs_bool_timings(cs, &t->bool_timings);
397
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700398 return 0;
399}
400
Imre Deakf37e4582006-09-25 12:41:33 +0300401static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700402{
Imre Deakf37e4582006-09-25 12:41:33 +0300403 u32 l;
404 u32 mask;
405
406 mask = (1 << GPMC_SECTION_SHIFT) - size;
407 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
408 l &= ~0x3f;
409 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
410 l &= ~(0x0f << 8);
411 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530412 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300413 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
414}
415
416static void gpmc_cs_disable_mem(int cs)
417{
418 u32 l;
419
420 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530421 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300422 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
423}
424
425static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
426{
427 u32 l;
428 u32 mask;
429
430 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
431 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
432 mask = (l >> 8) & 0x0f;
433 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
434}
435
436static int gpmc_cs_mem_enabled(int cs)
437{
438 u32 l;
439
440 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530441 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300442}
443
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300444static int gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300445{
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800446 if (cs > GPMC_CS_NUM)
447 return -ENODEV;
448
Imre Deakf37e4582006-09-25 12:41:33 +0300449 gpmc_cs_map &= ~(1 << cs);
450 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800451
452 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300453}
454
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300455static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300456{
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800457 if (cs > GPMC_CS_NUM)
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300458 return true;
Tony Lindgrenc40fae952006-12-07 13:58:10 -0800459
Imre Deakf37e4582006-09-25 12:41:33 +0300460 return gpmc_cs_map & (1 << cs);
461}
462
463static unsigned long gpmc_mem_align(unsigned long size)
464{
465 int order;
466
467 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
468 order = GPMC_CHUNK_SHIFT - 1;
469 do {
470 size >>= 1;
471 order++;
472 } while (size);
473 size = 1 << order;
474 return size;
475}
476
477static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
478{
479 struct resource *res = &gpmc_cs_mem[cs];
480 int r;
481
482 size = gpmc_mem_align(size);
483 spin_lock(&gpmc_mem_lock);
484 res->start = base;
485 res->end = base + size - 1;
486 r = request_resource(&gpmc_mem_root, res);
487 spin_unlock(&gpmc_mem_lock);
488
489 return r;
490}
491
Afzal Mohammedda496872012-09-23 17:28:25 -0600492static int gpmc_cs_delete_mem(int cs)
493{
494 struct resource *res = &gpmc_cs_mem[cs];
495 int r;
496
497 spin_lock(&gpmc_mem_lock);
498 r = release_resource(&gpmc_cs_mem[cs]);
499 res->start = 0;
500 res->end = 0;
501 spin_unlock(&gpmc_mem_lock);
502
503 return r;
504}
505
Imre Deakf37e4582006-09-25 12:41:33 +0300506int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
507{
508 struct resource *res = &gpmc_cs_mem[cs];
509 int r = -1;
510
511 if (cs > GPMC_CS_NUM)
512 return -ENODEV;
513
514 size = gpmc_mem_align(size);
515 if (size > (1 << GPMC_SECTION_SHIFT))
516 return -ENOMEM;
517
518 spin_lock(&gpmc_mem_lock);
519 if (gpmc_cs_reserved(cs)) {
520 r = -EBUSY;
521 goto out;
522 }
523 if (gpmc_cs_mem_enabled(cs))
524 r = adjust_resource(res, res->start & ~(size - 1), size);
525 if (r < 0)
526 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
527 size, NULL, NULL);
528 if (r < 0)
529 goto out;
530
Tobias Klauser6d135242009-11-10 18:55:19 -0800531 gpmc_cs_enable_mem(cs, res->start, resource_size(res));
Imre Deakf37e4582006-09-25 12:41:33 +0300532 *base = res->start;
533 gpmc_cs_set_reserved(cs, 1);
534out:
535 spin_unlock(&gpmc_mem_lock);
536 return r;
537}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300538EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300539
540void gpmc_cs_free(int cs)
541{
542 spin_lock(&gpmc_mem_lock);
Roel Kluine7fdc602009-11-17 14:39:06 -0800543 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300544 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
545 BUG();
546 spin_unlock(&gpmc_mem_lock);
547 return;
548 }
549 gpmc_cs_disable_mem(cs);
550 release_resource(&gpmc_cs_mem[cs]);
551 gpmc_cs_set_reserved(cs, 0);
552 spin_unlock(&gpmc_mem_lock);
553}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300554EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300555
vimal singh59e9c5a2009-07-13 16:26:24 +0530556/**
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000557 * gpmc_cs_configure - write request to configure gpmc
558 * @cs: chip select number
559 * @cmd: command type
560 * @wval: value to write
561 * @return status of the operation
562 */
563int gpmc_cs_configure(int cs, int cmd, int wval)
564{
565 int err = 0;
566 u32 regval = 0;
567
568 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530569 case GPMC_ENABLE_IRQ:
570 gpmc_write_reg(GPMC_IRQENABLE, wval);
571 break;
572
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000573 case GPMC_SET_IRQ_STATUS:
574 gpmc_write_reg(GPMC_IRQSTATUS, wval);
575 break;
576
577 case GPMC_CONFIG_WP:
578 regval = gpmc_read_reg(GPMC_CONFIG);
579 if (wval)
580 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
581 else
582 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
583 gpmc_write_reg(GPMC_CONFIG, regval);
584 break;
585
586 case GPMC_CONFIG_RDY_BSY:
587 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
588 if (wval)
589 regval |= WR_RD_PIN_MONITORING;
590 else
591 regval &= ~WR_RD_PIN_MONITORING;
592 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
593 break;
594
595 case GPMC_CONFIG_DEV_SIZE:
596 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
Yegor Yefremov8ef5d842012-01-23 08:32:23 +0100597
598 /* clear 2 target bits */
599 regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
600
601 /* set the proper value */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000602 regval |= GPMC_CONFIG1_DEVICESIZE(wval);
Yegor Yefremov8ef5d842012-01-23 08:32:23 +0100603
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000604 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
605 break;
606
607 case GPMC_CONFIG_DEV_TYPE:
608 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
Mark Jacksonc9fb8092013-03-05 10:13:40 +0000609 /* clear 4 target bits */
610 regval &= ~(GPMC_CONFIG1_DEVICETYPE(3) |
611 GPMC_CONFIG1_MUXTYPE(3));
612 /* set the proper value */
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000613 regval |= GPMC_CONFIG1_DEVICETYPE(wval);
614 if (wval == GPMC_DEVICETYPE_NOR)
615 regval |= GPMC_CONFIG1_MUXADDDATA;
616 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
617 break;
618
619 default:
620 printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
621 err = -EINVAL;
622 }
623
624 return err;
625}
626EXPORT_SYMBOL(gpmc_cs_configure);
627
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700628void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
629{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530630 int i;
631
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700632 reg->gpmc_status = gpmc_base + GPMC_STATUS;
633 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
634 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
635 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
636 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
637 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
638 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
639 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
640 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
641 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
642 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
643 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
644 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
645 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
646 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530647
648 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
649 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
650 GPMC_BCH_SIZE * i;
651 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
652 GPMC_BCH_SIZE * i;
653 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
654 GPMC_BCH_SIZE * i;
655 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
656 GPMC_BCH_SIZE * i;
657 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700658}
659
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700660int gpmc_get_client_irq(unsigned irq_config)
661{
662 int i;
663
664 if (hweight32(irq_config) > 1)
665 return 0;
666
667 for (i = 0; i < GPMC_NR_IRQ; i++)
668 if (gpmc_client_irq[i].bitmask & irq_config)
669 return gpmc_client_irq[i].irq;
670
671 return 0;
672}
673
674static int gpmc_irq_endis(unsigned irq, bool endis)
675{
676 int i;
677 u32 regval;
678
679 for (i = 0; i < GPMC_NR_IRQ; i++)
680 if (irq == gpmc_client_irq[i].irq) {
681 regval = gpmc_read_reg(GPMC_IRQENABLE);
682 if (endis)
683 regval |= gpmc_client_irq[i].bitmask;
684 else
685 regval &= ~gpmc_client_irq[i].bitmask;
686 gpmc_write_reg(GPMC_IRQENABLE, regval);
687 break;
688 }
689
690 return 0;
691}
692
693static void gpmc_irq_disable(struct irq_data *p)
694{
695 gpmc_irq_endis(p->irq, false);
696}
697
698static void gpmc_irq_enable(struct irq_data *p)
699{
700 gpmc_irq_endis(p->irq, true);
701}
702
703static void gpmc_irq_noop(struct irq_data *data) { }
704
705static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
706
Afzal Mohammedda496872012-09-23 17:28:25 -0600707static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700708{
709 int i;
710 u32 regval;
711
712 if (!gpmc_irq)
713 return -EINVAL;
714
715 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000716 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700717 pr_err("irq_alloc_descs failed\n");
718 return gpmc_irq_start;
719 }
720
721 gpmc_irq_chip.name = "gpmc";
722 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
723 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
724 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
725 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
726 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
727 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
728 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
729
730 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
731 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
732
733 for (i = 0; i < GPMC_NR_IRQ; i++) {
734 gpmc_client_irq[i].irq = gpmc_irq_start + i;
735 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
736 &gpmc_irq_chip, handle_simple_irq);
737 set_irq_flags(gpmc_client_irq[i].irq,
738 IRQF_VALID | IRQF_NOAUTOEN);
739 }
740
741 /* Disable interrupts */
742 gpmc_write_reg(GPMC_IRQENABLE, 0);
743
744 /* clear interrupts */
745 regval = gpmc_read_reg(GPMC_IRQSTATUS);
746 gpmc_write_reg(GPMC_IRQSTATUS, regval);
747
748 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
749}
750
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800751static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600752{
753 int i;
754
755 if (gpmc_irq)
756 free_irq(gpmc_irq, NULL);
757
758 for (i = 0; i < GPMC_NR_IRQ; i++) {
759 irq_set_handler(gpmc_client_irq[i].irq, NULL);
760 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
761 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
762 }
763
764 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
765
766 return 0;
767}
768
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800769static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600770{
771 int cs;
772
773 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
774 if (!gpmc_cs_mem_enabled(cs))
775 continue;
776 gpmc_cs_delete_mem(cs);
777 }
778
779}
780
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800781static int gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300782{
Jon Hunter81190242012-10-17 09:41:25 -0500783 int cs, rc;
Imre Deakf37e4582006-09-25 12:41:33 +0300784 unsigned long boot_rom_space = 0;
785
Kyungmin Park7f245162006-12-29 16:48:51 -0800786 /* never allocate the first page, to facilitate bug detection;
787 * even if we didn't boot from ROM.
788 */
789 boot_rom_space = BOOT_ROM_SPACE;
Imre Deakf37e4582006-09-25 12:41:33 +0300790 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
791 gpmc_mem_root.end = GPMC_MEM_END;
792
793 /* Reserve all regions that has been set up by bootloader */
794 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
795 u32 base, size;
796
797 if (!gpmc_cs_mem_enabled(cs))
798 continue;
799 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter81190242012-10-17 09:41:25 -0500800 rc = gpmc_cs_insert_mem(cs, base, size);
Russell King71856842013-03-13 20:44:21 +0000801 if (rc < 0) {
Jon Hunter81190242012-10-17 09:41:25 -0500802 while (--cs >= 0)
803 if (gpmc_cs_mem_enabled(cs))
804 gpmc_cs_delete_mem(cs);
805 return rc;
806 }
Imre Deakf37e4582006-09-25 12:41:33 +0300807 }
Jon Hunter81190242012-10-17 09:41:25 -0500808
809 return 0;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700810}
811
Afzal Mohammed246da262012-08-02 20:02:10 +0530812static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
813{
814 u32 temp;
815 int div;
816
817 div = gpmc_calc_divider(sync_clk);
818 temp = gpmc_ps_to_ticks(time_ps);
819 temp = (temp + div - 1) / div;
820 return gpmc_ticks_to_ps(temp * div);
821}
822
823/* XXX: can the cycles be avoided ? */
824static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
825 struct gpmc_device_timings *dev_t)
826{
827 bool mux = dev_t->mux;
828 u32 temp;
829
830 /* adv_rd_off */
831 temp = dev_t->t_avdp_r;
832 /* XXX: mux check required ? */
833 if (mux) {
834 /* XXX: t_avdp not to be required for sync, only added for tusb
835 * this indirectly necessitates requirement of t_avdp_r and
836 * t_avdp_w instead of having a single t_avdp
837 */
838 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
839 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
840 }
841 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
842
843 /* oe_on */
844 temp = dev_t->t_oeasu; /* XXX: remove this ? */
845 if (mux) {
846 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
847 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
848 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
849 }
850 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
851
852 /* access */
853 /* XXX: any scope for improvement ?, by combining oe_on
854 * and clk_activation, need to check whether
855 * access = clk_activation + round to sync clk ?
856 */
857 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
858 temp += gpmc_t->clk_activation;
859 if (dev_t->cyc_oe)
860 temp = max_t(u32, temp, gpmc_t->oe_on +
861 gpmc_ticks_to_ps(dev_t->cyc_oe));
862 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
863
864 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
865 gpmc_t->cs_rd_off = gpmc_t->oe_off;
866
867 /* rd_cycle */
868 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
869 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
870 gpmc_t->access;
871 /* XXX: barter t_ce_rdyz with t_cez_r ? */
872 if (dev_t->t_ce_rdyz)
873 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
874 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
875
876 return 0;
877}
878
879static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
880 struct gpmc_device_timings *dev_t)
881{
882 bool mux = dev_t->mux;
883 u32 temp;
884
885 /* adv_wr_off */
886 temp = dev_t->t_avdp_w;
887 if (mux) {
888 temp = max_t(u32, temp,
889 gpmc_t->clk_activation + dev_t->t_avdh);
890 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
891 }
892 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
893
894 /* wr_data_mux_bus */
895 temp = max_t(u32, dev_t->t_weasu,
896 gpmc_t->clk_activation + dev_t->t_rdyo);
897 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
898 * and in that case remember to handle we_on properly
899 */
900 if (mux) {
901 temp = max_t(u32, temp,
902 gpmc_t->adv_wr_off + dev_t->t_aavdh);
903 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
904 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
905 }
906 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
907
908 /* we_on */
909 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
910 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
911 else
912 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
913
914 /* wr_access */
915 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
916 gpmc_t->wr_access = gpmc_t->access;
917
918 /* we_off */
919 temp = gpmc_t->we_on + dev_t->t_wpl;
920 temp = max_t(u32, temp,
921 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
922 temp = max_t(u32, temp,
923 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
924 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
925
926 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
927 dev_t->t_wph);
928
929 /* wr_cycle */
930 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
931 temp += gpmc_t->wr_access;
932 /* XXX: barter t_ce_rdyz with t_cez_w ? */
933 if (dev_t->t_ce_rdyz)
934 temp = max_t(u32, temp,
935 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
936 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
937
938 return 0;
939}
940
941static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
942 struct gpmc_device_timings *dev_t)
943{
944 bool mux = dev_t->mux;
945 u32 temp;
946
947 /* adv_rd_off */
948 temp = dev_t->t_avdp_r;
949 if (mux)
950 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
951 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
952
953 /* oe_on */
954 temp = dev_t->t_oeasu;
955 if (mux)
956 temp = max_t(u32, temp,
957 gpmc_t->adv_rd_off + dev_t->t_aavdh);
958 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
959
960 /* access */
961 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
962 gpmc_t->oe_on + dev_t->t_oe);
963 temp = max_t(u32, temp,
964 gpmc_t->cs_on + dev_t->t_ce);
965 temp = max_t(u32, temp,
966 gpmc_t->adv_on + dev_t->t_aa);
967 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
968
969 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
970 gpmc_t->cs_rd_off = gpmc_t->oe_off;
971
972 /* rd_cycle */
973 temp = max_t(u32, dev_t->t_rd_cycle,
974 gpmc_t->cs_rd_off + dev_t->t_cez_r);
975 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
976 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
977
978 return 0;
979}
980
981static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
982 struct gpmc_device_timings *dev_t)
983{
984 bool mux = dev_t->mux;
985 u32 temp;
986
987 /* adv_wr_off */
988 temp = dev_t->t_avdp_w;
989 if (mux)
990 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
991 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
992
993 /* wr_data_mux_bus */
994 temp = dev_t->t_weasu;
995 if (mux) {
996 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
997 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
998 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
999 }
1000 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1001
1002 /* we_on */
1003 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1004 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1005 else
1006 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1007
1008 /* we_off */
1009 temp = gpmc_t->we_on + dev_t->t_wpl;
1010 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1011
1012 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1013 dev_t->t_wph);
1014
1015 /* wr_cycle */
1016 temp = max_t(u32, dev_t->t_wr_cycle,
1017 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1018 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1019
1020 return 0;
1021}
1022
1023static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1024 struct gpmc_device_timings *dev_t)
1025{
1026 u32 temp;
1027
1028 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1029 gpmc_get_fclk_period();
1030
1031 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1032 dev_t->t_bacc,
1033 gpmc_t->sync_clk);
1034
1035 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1036 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1037
1038 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1039 return 0;
1040
1041 if (dev_t->ce_xdelay)
1042 gpmc_t->bool_timings.cs_extra_delay = true;
1043 if (dev_t->avd_xdelay)
1044 gpmc_t->bool_timings.adv_extra_delay = true;
1045 if (dev_t->oe_xdelay)
1046 gpmc_t->bool_timings.oe_extra_delay = true;
1047 if (dev_t->we_xdelay)
1048 gpmc_t->bool_timings.we_extra_delay = true;
1049
1050 return 0;
1051}
1052
1053static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
1054 struct gpmc_device_timings *dev_t)
1055{
1056 u32 temp;
1057
1058 /* cs_on */
1059 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1060
1061 /* adv_on */
1062 temp = dev_t->t_avdasu;
1063 if (dev_t->t_ce_avd)
1064 temp = max_t(u32, temp,
1065 gpmc_t->cs_on + dev_t->t_ce_avd);
1066 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1067
1068 if (dev_t->sync_write || dev_t->sync_read)
1069 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1070
1071 return 0;
1072}
1073
1074/* TODO: remove this function once all peripherals are confirmed to
1075 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1076 * has to be modified to handle timings in ps instead of ns
1077*/
1078static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1079{
1080 t->cs_on /= 1000;
1081 t->cs_rd_off /= 1000;
1082 t->cs_wr_off /= 1000;
1083 t->adv_on /= 1000;
1084 t->adv_rd_off /= 1000;
1085 t->adv_wr_off /= 1000;
1086 t->we_on /= 1000;
1087 t->we_off /= 1000;
1088 t->oe_on /= 1000;
1089 t->oe_off /= 1000;
1090 t->page_burst_access /= 1000;
1091 t->access /= 1000;
1092 t->rd_cycle /= 1000;
1093 t->wr_cycle /= 1000;
1094 t->bus_turnaround /= 1000;
1095 t->cycle2cycle_delay /= 1000;
1096 t->wait_monitoring /= 1000;
1097 t->clk_activation /= 1000;
1098 t->wr_access /= 1000;
1099 t->wr_data_mux_bus /= 1000;
1100}
1101
1102int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
1103 struct gpmc_device_timings *dev_t)
1104{
1105 memset(gpmc_t, 0, sizeof(*gpmc_t));
1106
1107 gpmc_calc_common_timings(gpmc_t, dev_t);
1108
1109 if (dev_t->sync_read)
1110 gpmc_calc_sync_read_timings(gpmc_t, dev_t);
1111 else
1112 gpmc_calc_async_read_timings(gpmc_t, dev_t);
1113
1114 if (dev_t->sync_write)
1115 gpmc_calc_sync_write_timings(gpmc_t, dev_t);
1116 else
1117 gpmc_calc_async_write_timings(gpmc_t, dev_t);
1118
1119 /* TODO: remove, see function definition */
1120 gpmc_convert_ps_to_ns(gpmc_t);
1121
1122 return 0;
1123}
1124
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001125#ifdef CONFIG_OF
1126static struct of_device_id gpmc_dt_ids[] = {
1127 { .compatible = "ti,omap2420-gpmc" },
1128 { .compatible = "ti,omap2430-gpmc" },
1129 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1130 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1131 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1132 { }
1133};
1134MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1135
1136static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1137 struct gpmc_timings *gpmc_t)
1138{
1139 u32 val;
1140
1141 memset(gpmc_t, 0, sizeof(*gpmc_t));
1142
1143 /* minimum clock period for syncronous mode */
1144 if (!of_property_read_u32(np, "gpmc,sync-clk", &val))
1145 gpmc_t->sync_clk = val;
1146
1147 /* chip select timtings */
1148 if (!of_property_read_u32(np, "gpmc,cs-on", &val))
1149 gpmc_t->cs_on = val;
1150
1151 if (!of_property_read_u32(np, "gpmc,cs-rd-off", &val))
1152 gpmc_t->cs_rd_off = val;
1153
1154 if (!of_property_read_u32(np, "gpmc,cs-wr-off", &val))
1155 gpmc_t->cs_wr_off = val;
1156
1157 /* ADV signal timings */
1158 if (!of_property_read_u32(np, "gpmc,adv-on", &val))
1159 gpmc_t->adv_on = val;
1160
1161 if (!of_property_read_u32(np, "gpmc,adv-rd-off", &val))
1162 gpmc_t->adv_rd_off = val;
1163
1164 if (!of_property_read_u32(np, "gpmc,adv-wr-off", &val))
1165 gpmc_t->adv_wr_off = val;
1166
1167 /* WE signal timings */
1168 if (!of_property_read_u32(np, "gpmc,we-on", &val))
1169 gpmc_t->we_on = val;
1170
1171 if (!of_property_read_u32(np, "gpmc,we-off", &val))
1172 gpmc_t->we_off = val;
1173
1174 /* OE signal timings */
1175 if (!of_property_read_u32(np, "gpmc,oe-on", &val))
1176 gpmc_t->oe_on = val;
1177
1178 if (!of_property_read_u32(np, "gpmc,oe-off", &val))
1179 gpmc_t->oe_off = val;
1180
1181 /* access and cycle timings */
1182 if (!of_property_read_u32(np, "gpmc,page-burst-access", &val))
1183 gpmc_t->page_burst_access = val;
1184
1185 if (!of_property_read_u32(np, "gpmc,access", &val))
1186 gpmc_t->access = val;
1187
1188 if (!of_property_read_u32(np, "gpmc,rd-cycle", &val))
1189 gpmc_t->rd_cycle = val;
1190
1191 if (!of_property_read_u32(np, "gpmc,wr-cycle", &val))
1192 gpmc_t->wr_cycle = val;
1193
1194 /* only for OMAP3430 */
1195 if (!of_property_read_u32(np, "gpmc,wr-access", &val))
1196 gpmc_t->wr_access = val;
1197
1198 if (!of_property_read_u32(np, "gpmc,wr-data-mux-bus", &val))
1199 gpmc_t->wr_data_mux_bus = val;
1200}
1201
1202#ifdef CONFIG_MTD_NAND
1203
1204static const char * const nand_ecc_opts[] = {
1205 [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
1206 [OMAP_ECC_HAMMING_CODE_HW] = "hw",
1207 [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
1208 [OMAP_ECC_BCH4_CODE_HW] = "bch4",
1209 [OMAP_ECC_BCH8_CODE_HW] = "bch8",
1210};
1211
1212static int gpmc_probe_nand_child(struct platform_device *pdev,
1213 struct device_node *child)
1214{
1215 u32 val;
1216 const char *s;
1217 struct gpmc_timings gpmc_t;
1218 struct omap_nand_platform_data *gpmc_nand_data;
1219
1220 if (of_property_read_u32(child, "reg", &val) < 0) {
1221 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1222 child->full_name);
1223 return -ENODEV;
1224 }
1225
1226 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1227 GFP_KERNEL);
1228 if (!gpmc_nand_data)
1229 return -ENOMEM;
1230
1231 gpmc_nand_data->cs = val;
1232 gpmc_nand_data->of_node = child;
1233
1234 if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
1235 for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
1236 if (!strcasecmp(s, nand_ecc_opts[val])) {
1237 gpmc_nand_data->ecc_opt = val;
1238 break;
1239 }
1240
1241 val = of_get_nand_bus_width(child);
1242 if (val == 16)
1243 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1244
1245 gpmc_read_timings_dt(child, &gpmc_t);
1246 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1247
1248 return 0;
1249}
1250#else
1251static int gpmc_probe_nand_child(struct platform_device *pdev,
1252 struct device_node *child)
1253{
1254 return 0;
1255}
1256#endif
1257
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001258#ifdef CONFIG_MTD_ONENAND
1259static int gpmc_probe_onenand_child(struct platform_device *pdev,
1260 struct device_node *child)
1261{
1262 u32 val;
1263 struct omap_onenand_platform_data *gpmc_onenand_data;
1264
1265 if (of_property_read_u32(child, "reg", &val) < 0) {
1266 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1267 child->full_name);
1268 return -ENODEV;
1269 }
1270
1271 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1272 GFP_KERNEL);
1273 if (!gpmc_onenand_data)
1274 return -ENOMEM;
1275
1276 gpmc_onenand_data->cs = val;
1277 gpmc_onenand_data->of_node = child;
1278 gpmc_onenand_data->dma_channel = -1;
1279
1280 if (!of_property_read_u32(child, "dma-channel", &val))
1281 gpmc_onenand_data->dma_channel = val;
1282
1283 gpmc_onenand_init(gpmc_onenand_data);
1284
1285 return 0;
1286}
1287#else
1288static int gpmc_probe_onenand_child(struct platform_device *pdev,
1289 struct device_node *child)
1290{
1291 return 0;
1292}
1293#endif
1294
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001295static int gpmc_probe_dt(struct platform_device *pdev)
1296{
1297 int ret;
1298 struct device_node *child;
1299 const struct of_device_id *of_id =
1300 of_match_device(gpmc_dt_ids, &pdev->dev);
1301
1302 if (!of_id)
1303 return 0;
1304
1305 for_each_node_by_name(child, "nand") {
1306 ret = gpmc_probe_nand_child(pdev, child);
Ezequiel Garciaa1672372013-01-25 09:19:47 -03001307 if (ret < 0) {
1308 of_node_put(child);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001309 return ret;
Ezequiel Garciaa1672372013-01-25 09:19:47 -03001310 }
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001311 }
1312
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001313 for_each_node_by_name(child, "onenand") {
1314 ret = gpmc_probe_onenand_child(pdev, child);
1315 if (ret < 0) {
1316 of_node_put(child);
1317 return ret;
1318 }
1319 }
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001320 return 0;
1321}
1322#else
1323static int gpmc_probe_dt(struct platform_device *pdev)
1324{
1325 return 0;
1326}
1327#endif
1328
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001329static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001330{
Jon Hunter81190242012-10-17 09:41:25 -05001331 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001332 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001333 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001334
Afzal Mohammedda496872012-09-23 17:28:25 -06001335 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1336 if (res == NULL)
1337 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001338
Afzal Mohammedda496872012-09-23 17:28:25 -06001339 phys_base = res->start;
1340 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001341
Thierry Reding5857bd92013-01-21 11:08:55 +01001342 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1343 if (IS_ERR(gpmc_base))
1344 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001345
1346 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1347 if (res == NULL)
1348 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1349 else
1350 gpmc_irq = res->start;
1351
1352 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1353 if (IS_ERR(gpmc_l3_clk)) {
1354 dev_err(&pdev->dev, "error: clk_get\n");
1355 gpmc_irq = 0;
1356 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001357 }
1358
Rajendra Nayak4d7cb452012-09-22 02:24:16 -06001359 clk_prepare_enable(gpmc_l3_clk);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001360
Afzal Mohammedda496872012-09-23 17:28:25 -06001361 gpmc_dev = &pdev->dev;
1362
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001363 l = gpmc_read_reg(GPMC_REVISION);
Afzal Mohammedda496872012-09-23 17:28:25 -06001364 if (GPMC_REVISION_MAJOR(l) > 0x4)
1365 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
1366 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1367 GPMC_REVISION_MINOR(l));
1368
Jon Hunter81190242012-10-17 09:41:25 -05001369 rc = gpmc_mem_init();
Russell King71856842013-03-13 20:44:21 +00001370 if (rc < 0) {
Jon Hunter81190242012-10-17 09:41:25 -05001371 clk_disable_unprepare(gpmc_l3_clk);
1372 clk_put(gpmc_l3_clk);
1373 dev_err(gpmc_dev, "failed to reserve memory\n");
1374 return rc;
1375 }
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301376
Russell King71856842013-03-13 20:44:21 +00001377 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001378 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1379
Jon Hunter31d9adc2013-02-18 07:57:39 -06001380 /* Now the GPMC is initialised, unreserve the chip-selects */
1381 gpmc_cs_map = 0;
1382
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001383 rc = gpmc_probe_dt(pdev);
1384 if (rc < 0) {
1385 clk_disable_unprepare(gpmc_l3_clk);
1386 clk_put(gpmc_l3_clk);
1387 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1388 return rc;
1389 }
1390
Afzal Mohammedda496872012-09-23 17:28:25 -06001391 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301392}
Afzal Mohammedda496872012-09-23 17:28:25 -06001393
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001394static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001395{
1396 gpmc_free_irq();
1397 gpmc_mem_exit();
1398 gpmc_dev = NULL;
1399 return 0;
1400}
1401
1402static struct platform_driver gpmc_driver = {
1403 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001404 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001405 .driver = {
1406 .name = DEVICE_NAME,
1407 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001408 .of_match_table = of_match_ptr(gpmc_dt_ids),
Afzal Mohammedda496872012-09-23 17:28:25 -06001409 },
1410};
1411
1412static __init int gpmc_init(void)
1413{
1414 return platform_driver_register(&gpmc_driver);
1415}
1416
1417static __exit void gpmc_exit(void)
1418{
1419 platform_driver_unregister(&gpmc_driver);
1420
1421}
1422
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001423omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001424module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301425
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001426static int __init omap_gpmc_init(void)
1427{
1428 struct omap_hwmod *oh;
1429 struct platform_device *pdev;
1430 char *oh_name = "gpmc";
1431
Daniel Mack2f98ca82012-12-14 11:36:40 +01001432 /*
1433 * if the board boots up with a populated DT, do not
1434 * manually add the device from this initcall
1435 */
1436 if (of_have_populated_dt())
1437 return -ENODEV;
1438
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001439 oh = omap_hwmod_lookup(oh_name);
1440 if (!oh) {
1441 pr_err("Could not look up %s\n", oh_name);
1442 return -ENODEV;
1443 }
1444
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001445 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001446 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1447
1448 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
1449}
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001450omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001451
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301452static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1453{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001454 int i;
1455 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301456
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001457 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1458
1459 if (!regval)
1460 return IRQ_NONE;
1461
1462 for (i = 0; i < GPMC_NR_IRQ; i++)
1463 if (regval & gpmc_client_irq[i].bitmask)
1464 generic_handle_irq(gpmc_client_irq[i].irq);
1465
1466 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301467
1468 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001469}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301470
1471#ifdef CONFIG_ARCH_OMAP3
1472static struct omap3_gpmc_regs gpmc_context;
1473
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001474void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301475{
1476 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001477
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301478 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1479 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1480 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1481 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1482 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1483 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1484 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
1485 for (i = 0; i < GPMC_CS_NUM; i++) {
1486 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1487 if (gpmc_context.cs_context[i].is_valid) {
1488 gpmc_context.cs_context[i].config1 =
1489 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1490 gpmc_context.cs_context[i].config2 =
1491 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1492 gpmc_context.cs_context[i].config3 =
1493 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1494 gpmc_context.cs_context[i].config4 =
1495 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1496 gpmc_context.cs_context[i].config5 =
1497 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1498 gpmc_context.cs_context[i].config6 =
1499 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1500 gpmc_context.cs_context[i].config7 =
1501 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1502 }
1503 }
1504}
1505
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001506void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301507{
1508 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001509
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301510 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1511 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1512 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1513 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1514 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1515 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1516 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
1517 for (i = 0; i < GPMC_CS_NUM; i++) {
1518 if (gpmc_context.cs_context[i].is_valid) {
1519 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1520 gpmc_context.cs_context[i].config1);
1521 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1522 gpmc_context.cs_context[i].config2);
1523 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1524 gpmc_context.cs_context[i].config3);
1525 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1526 gpmc_context.cs_context[i].config4);
1527 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1528 gpmc_context.cs_context[i].config5);
1529 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1530 gpmc_context.cs_context[i].config6);
1531 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1532 gpmc_context.cs_context[i].config7);
1533 }
1534 }
1535}
1536#endif /* CONFIG_ARCH_OMAP3 */