blob: 9b27773db0405f1fb2d331f8ef95942bf60e0f99 [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>
Jon Huntercdd69282013-02-08 16:46:13 -060029#include <linux/of_address.h>
Daniel Mackbc6b1e72012-12-14 11:36:44 +010030#include <linux/of_mtd.h>
31#include <linux/of_device.h>
32#include <linux/mtd/nand.h>
avinash philipb3f55252013-06-12 16:30:56 +053033#include <linux/pm_runtime.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070034
Afzal Mohammedbc3668e2012-09-29 12:26:13 +053035#include <linux/platform_data/mtd-nand-omap2.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070036
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070037#include <asm/mach-types.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"
Tony Lindgren25c7d492012-10-02 17:25:48 -070041#include "omap_device.h"
Afzal Mohammed3ef5d002012-10-05 10:37:27 +053042#include "gpmc.h"
Daniel Mackbc6b1e72012-12-14 11:36:44 +010043#include "gpmc-nand.h"
Ezequiel Garcia75d36252013-01-25 09:23:11 -030044#include "gpmc-onenand.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070045
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060046#define DEVICE_NAME "omap-gpmc"
47
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030048/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070049#define GPMC_REVISION 0x00
50#define GPMC_SYSCONFIG 0x10
51#define GPMC_SYSSTATUS 0x14
52#define GPMC_IRQSTATUS 0x18
53#define GPMC_IRQENABLE 0x1c
54#define GPMC_TIMEOUT_CONTROL 0x40
55#define GPMC_ERR_ADDRESS 0x44
56#define GPMC_ERR_TYPE 0x48
57#define GPMC_CONFIG 0x50
58#define GPMC_STATUS 0x54
59#define GPMC_PREFETCH_CONFIG1 0x1e0
60#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053061#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070062#define GPMC_PREFETCH_STATUS 0x1f0
63#define GPMC_ECC_CONFIG 0x1f4
64#define GPMC_ECC_CONTROL 0x1f8
65#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000066#define GPMC_ECC1_RESULT 0x200
Ivan Djelic8d602cf2012-04-26 14:17:49 +020067#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053068#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
69#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
70#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
pekon gupta27c9fd62014-05-19 13:24:39 +053071#define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
72#define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
73#define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070074
Yegor Yefremov2c65e742012-05-09 08:32:49 -070075/* GPMC ECC control settings */
76#define GPMC_ECC_CTRL_ECCCLEAR 0x100
77#define GPMC_ECC_CTRL_ECCDISABLE 0x000
78#define GPMC_ECC_CTRL_ECCREG1 0x001
79#define GPMC_ECC_CTRL_ECCREG2 0x002
80#define GPMC_ECC_CTRL_ECCREG3 0x003
81#define GPMC_ECC_CTRL_ECCREG4 0x004
82#define GPMC_ECC_CTRL_ECCREG5 0x005
83#define GPMC_ECC_CTRL_ECCREG6 0x006
84#define GPMC_ECC_CTRL_ECCREG7 0x007
85#define GPMC_ECC_CTRL_ECCREG8 0x008
86#define GPMC_ECC_CTRL_ECCREG9 0x009
87
Afzal Mohammed559d94b2012-05-28 17:51:37 +053088#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
89#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
90#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
91#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
92#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
93#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
94
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000095#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070096#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053097#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070098
Imre Deakf37e4582006-09-25 12:41:33 +030099#define GPMC_MEM_END 0x3FFFFFFF
Imre Deakf37e4582006-09-25 12:41:33 +0300100
101#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
102#define GPMC_SECTION_SHIFT 28 /* 128 MB */
103
vimal singh59e9c5a2009-07-13 16:26:24 +0530104#define CS_NUM_SHIFT 24
105#define ENABLE_PREFETCH (0x1 << 7)
106#define DMA_MPU_MODE 2
107
Afzal Mohammedda496872012-09-23 17:28:25 -0600108#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
109#define GPMC_REVISION_MINOR(l) (l & 0xf)
110
111#define GPMC_HAS_WR_ACCESS 0x1
112#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
Jon Hunteraa8d4762013-02-21 15:25:23 -0600113#define GPMC_HAS_MUX_AAD 0x4
Afzal Mohammedda496872012-09-23 17:28:25 -0600114
Jon Hunter9f833152013-02-20 15:53:38 -0600115#define GPMC_NR_WAITPINS 4
116
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700117/* XXX: Only NAND irq has been considered,currently these are the only ones used
118 */
119#define GPMC_NR_IRQ 2
120
121struct gpmc_client_irq {
122 unsigned irq;
123 u32 bitmask;
124};
125
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530126/* Structure to save gpmc cs context */
127struct gpmc_cs_config {
128 u32 config1;
129 u32 config2;
130 u32 config3;
131 u32 config4;
132 u32 config5;
133 u32 config6;
134 u32 config7;
135 int is_valid;
136};
137
138/*
139 * Structure to save/restore gpmc context
140 * to support core off on OMAP3
141 */
142struct omap3_gpmc_regs {
143 u32 sysconfig;
144 u32 irqenable;
145 u32 timeout_ctrl;
146 u32 config;
147 u32 prefetch_config1;
148 u32 prefetch_config2;
149 u32 prefetch_control;
150 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
151};
152
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700153static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
154static struct irq_chip gpmc_irq_chip;
Chen Gangaf072192013-08-22 15:47:21 +0800155static int gpmc_irq_start;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700156
Imre Deakf37e4582006-09-25 12:41:33 +0300157static struct resource gpmc_mem_root;
158static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700159static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600160/* Define chip-selects as reserved by default until probe completes */
161static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530162static unsigned int gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -0600163static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600164static struct device *gpmc_dev;
165static int gpmc_irq;
166static resource_size_t phys_base, mem_size;
167static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300168static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700169
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300170static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700171
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530172static irqreturn_t gpmc_handle_irq(int irq, void *dev);
173
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700174static void gpmc_write_reg(int idx, u32 val)
175{
176 __raw_writel(val, gpmc_base + idx);
177}
178
179static u32 gpmc_read_reg(int idx)
180{
181 return __raw_readl(gpmc_base + idx);
182}
183
184void gpmc_cs_write_reg(int cs, int idx, u32 val)
185{
186 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;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700189 __raw_writel(val, reg_addr);
190}
191
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300192static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700193{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300194 void __iomem *reg_addr;
195
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000196 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300197 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700198}
199
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300200/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300201static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700202{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300203 unsigned long rate = clk_get_rate(gpmc_l3_clk);
204
205 if (rate == 0) {
206 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
207 return 0;
208 }
209
210 rate /= 1000;
211 rate = 1000000000 / rate; /* In picoseconds */
212
213 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700214}
215
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300216static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700217{
218 unsigned long tick_ps;
219
220 /* Calculate in picosecs to yield more exact results */
221 tick_ps = gpmc_get_fclk_period();
222
223 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
224}
225
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300226static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200227{
228 unsigned long tick_ps;
229
230 /* Calculate in picosecs to yield more exact results */
231 tick_ps = gpmc_get_fclk_period();
232
233 return (time_ps + tick_ps - 1) / tick_ps;
234}
235
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300236unsigned int gpmc_ticks_to_ns(unsigned int ticks)
237{
238 return ticks * gpmc_get_fclk_period() / 1000;
239}
240
Afzal Mohammed246da262012-08-02 20:02:10 +0530241static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
242{
243 return ticks * gpmc_get_fclk_period();
244}
245
246static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
247{
248 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
249
250 return ticks * gpmc_get_fclk_period();
251}
252
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530253static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
254{
255 u32 l;
256
257 l = gpmc_cs_read_reg(cs, reg);
258 if (value)
259 l |= mask;
260 else
261 l &= ~mask;
262 gpmc_cs_write_reg(cs, reg, l);
263}
264
265static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
266{
267 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
268 GPMC_CONFIG1_TIME_PARA_GRAN,
269 p->time_para_granularity);
270 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
271 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
272 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
273 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
274 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
275 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
276 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
277 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
278 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
279 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
280 p->cycle2cyclesamecsen);
281 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
282 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
283 p->cycle2cyclediffcsen);
284}
285
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700286#ifdef DEBUG
287static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700288 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700289#else
290static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
291 int time)
292#endif
293{
294 u32 l;
295 int ticks, mask, nr_bits;
296
297 if (time == 0)
298 ticks = 0;
299 else
300 ticks = gpmc_ns_to_ticks(time);
301 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800302 if (ticks >= 1 << nr_bits) {
303#ifdef DEBUG
304 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
305 cs, name, time, ticks, 1 << nr_bits);
306#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700307 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800308 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700309
310 mask = (1 << nr_bits) - 1;
311 l = gpmc_cs_read_reg(cs, reg);
312#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800313 printk(KERN_INFO
314 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700315 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800316 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700317#endif
318 l &= ~(mask << st_bit);
319 l |= ticks << st_bit;
320 gpmc_cs_write_reg(cs, reg, l);
321
322 return 0;
323}
324
325#ifdef DEBUG
326#define GPMC_SET_ONE(reg, st, end, field) \
327 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
328 t->field, #field) < 0) \
329 return -1
330#else
331#define GPMC_SET_ONE(reg, st, end, field) \
332 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
333 return -1
334#endif
335
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530336int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700337{
338 int div;
339 u32 l;
340
Adrian Huntera3551f52010-12-09 10:48:27 +0200341 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700342 div = l / gpmc_get_fclk_period();
343 if (div > 4)
344 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800345 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700346 div = 1;
347
348 return div;
349}
350
351int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
352{
353 int div;
354 u32 l;
355
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530356 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700357 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600358 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700359
360 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
361 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
362 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
363
364 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
365 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
366 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
367
368 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
369 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
370 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
371 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
372
373 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
374 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
375 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
376
377 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
378
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530379 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
380 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
381
382 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
383 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
384
Afzal Mohammedda496872012-09-23 17:28:25 -0600385 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300386 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600387 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300388 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300389
David Brownell1c22cc12006-12-06 17:13:55 -0800390 /* caller is expected to have initialized CONFIG1 to cover
391 * at least sync vs async
392 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700393 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800394 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
395#ifdef DEBUG
396 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
397 cs, (div * gpmc_get_fclk_period()) / 1000, div);
398#endif
399 l &= ~0x03;
400 l |= (div - 1);
401 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
402 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700403
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530404 gpmc_cs_bool_timings(cs, &t->bool_timings);
405
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700406 return 0;
407}
408
Jon Hunterc71f8e92013-03-06 12:00:10 -0600409static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700410{
Imre Deakf37e4582006-09-25 12:41:33 +0300411 u32 l;
412 u32 mask;
413
Jon Hunterc71f8e92013-03-06 12:00:10 -0600414 /*
415 * Ensure that base address is aligned on a
416 * boundary equal to or greater than size.
417 */
418 if (base & (size - 1))
419 return -EINVAL;
420
Imre Deakf37e4582006-09-25 12:41:33 +0300421 mask = (1 << GPMC_SECTION_SHIFT) - size;
422 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
423 l &= ~0x3f;
424 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
425 l &= ~(0x0f << 8);
426 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530427 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300428 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600429
430 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300431}
432
433static void gpmc_cs_disable_mem(int cs)
434{
435 u32 l;
436
437 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530438 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300439 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
440}
441
442static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
443{
444 u32 l;
445 u32 mask;
446
447 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
448 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
449 mask = (l >> 8) & 0x0f;
450 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
451}
452
453static int gpmc_cs_mem_enabled(int cs)
454{
455 u32 l;
456
457 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530458 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300459}
460
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300461static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300462{
463 gpmc_cs_map &= ~(1 << cs);
464 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
465}
466
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300467static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300468{
469 return gpmc_cs_map & (1 << cs);
470}
471
472static unsigned long gpmc_mem_align(unsigned long size)
473{
474 int order;
475
476 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
477 order = GPMC_CHUNK_SHIFT - 1;
478 do {
479 size >>= 1;
480 order++;
481 } while (size);
482 size = 1 << order;
483 return size;
484}
485
486static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
487{
488 struct resource *res = &gpmc_cs_mem[cs];
489 int r;
490
491 size = gpmc_mem_align(size);
492 spin_lock(&gpmc_mem_lock);
493 res->start = base;
494 res->end = base + size - 1;
495 r = request_resource(&gpmc_mem_root, res);
496 spin_unlock(&gpmc_mem_lock);
497
498 return r;
499}
500
Afzal Mohammedda496872012-09-23 17:28:25 -0600501static int gpmc_cs_delete_mem(int cs)
502{
503 struct resource *res = &gpmc_cs_mem[cs];
504 int r;
505
506 spin_lock(&gpmc_mem_lock);
507 r = release_resource(&gpmc_cs_mem[cs]);
508 res->start = 0;
509 res->end = 0;
510 spin_unlock(&gpmc_mem_lock);
511
512 return r;
513}
514
Jon Huntercdd69282013-02-08 16:46:13 -0600515/**
516 * gpmc_cs_remap - remaps a chip-select physical base address
517 * @cs: chip-select to remap
518 * @base: physical base address to re-map chip-select to
519 *
520 * Re-maps a chip-select to a new physical base address specified by
521 * "base". Returns 0 on success and appropriate negative error code
522 * on failure.
523 */
524static int gpmc_cs_remap(int cs, u32 base)
525{
526 int ret;
527 u32 old_base, size;
528
Gupta Pekonf34f3712013-05-31 17:31:30 +0530529 if (cs > gpmc_cs_num) {
530 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600531 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530532 }
Jon Huntercdd69282013-02-08 16:46:13 -0600533 gpmc_cs_get_memconf(cs, &old_base, &size);
534 if (base == old_base)
535 return 0;
536 gpmc_cs_disable_mem(cs);
537 ret = gpmc_cs_delete_mem(cs);
538 if (ret < 0)
539 return ret;
540 ret = gpmc_cs_insert_mem(cs, base, size);
541 if (ret < 0)
542 return ret;
Jon Hunterc71f8e92013-03-06 12:00:10 -0600543 ret = gpmc_cs_enable_mem(cs, base, size);
544 if (ret < 0)
545 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600546
547 return 0;
548}
549
Imre Deakf37e4582006-09-25 12:41:33 +0300550int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
551{
552 struct resource *res = &gpmc_cs_mem[cs];
553 int r = -1;
554
Gupta Pekonf34f3712013-05-31 17:31:30 +0530555 if (cs > gpmc_cs_num) {
556 pr_err("%s: requested chip-select is disabled\n", __func__);
Imre Deakf37e4582006-09-25 12:41:33 +0300557 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530558 }
Imre Deakf37e4582006-09-25 12:41:33 +0300559 size = gpmc_mem_align(size);
560 if (size > (1 << GPMC_SECTION_SHIFT))
561 return -ENOMEM;
562
563 spin_lock(&gpmc_mem_lock);
564 if (gpmc_cs_reserved(cs)) {
565 r = -EBUSY;
566 goto out;
567 }
568 if (gpmc_cs_mem_enabled(cs))
569 r = adjust_resource(res, res->start & ~(size - 1), size);
570 if (r < 0)
571 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
572 size, NULL, NULL);
573 if (r < 0)
574 goto out;
575
Jon Hunterc71f8e92013-03-06 12:00:10 -0600576 r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
577 if (r < 0) {
578 release_resource(res);
579 goto out;
580 }
581
Imre Deakf37e4582006-09-25 12:41:33 +0300582 *base = res->start;
583 gpmc_cs_set_reserved(cs, 1);
584out:
585 spin_unlock(&gpmc_mem_lock);
586 return r;
587}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300588EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300589
590void gpmc_cs_free(int cs)
591{
592 spin_lock(&gpmc_mem_lock);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530593 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300594 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
595 BUG();
596 spin_unlock(&gpmc_mem_lock);
597 return;
598 }
599 gpmc_cs_disable_mem(cs);
600 release_resource(&gpmc_cs_mem[cs]);
601 gpmc_cs_set_reserved(cs, 0);
602 spin_unlock(&gpmc_mem_lock);
603}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300604EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300605
vimal singh59e9c5a2009-07-13 16:26:24 +0530606/**
Jon Hunter3a544352013-02-21 13:00:21 -0600607 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000608 * @cmd: command type
609 * @wval: value to write
610 * @return status of the operation
611 */
Jon Hunter3a544352013-02-21 13:00:21 -0600612int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000613{
Jon Hunter3a544352013-02-21 13:00:21 -0600614 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000615
616 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530617 case GPMC_ENABLE_IRQ:
618 gpmc_write_reg(GPMC_IRQENABLE, wval);
619 break;
620
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000621 case GPMC_SET_IRQ_STATUS:
622 gpmc_write_reg(GPMC_IRQSTATUS, wval);
623 break;
624
625 case GPMC_CONFIG_WP:
626 regval = gpmc_read_reg(GPMC_CONFIG);
627 if (wval)
628 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
629 else
630 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
631 gpmc_write_reg(GPMC_CONFIG, regval);
632 break;
633
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000634 default:
Jon Hunter3a544352013-02-21 13:00:21 -0600635 pr_err("%s: command not supported\n", __func__);
636 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000637 }
638
Jon Hunter3a544352013-02-21 13:00:21 -0600639 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000640}
Jon Hunter3a544352013-02-21 13:00:21 -0600641EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000642
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700643void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
644{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530645 int i;
646
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700647 reg->gpmc_status = gpmc_base + GPMC_STATUS;
648 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
649 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
650 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
651 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
652 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
653 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
654 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
655 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
656 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
657 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
658 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
659 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
660 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
661 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530662
663 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
664 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
665 GPMC_BCH_SIZE * i;
666 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
667 GPMC_BCH_SIZE * i;
668 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
669 GPMC_BCH_SIZE * i;
670 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
671 GPMC_BCH_SIZE * i;
pekon gupta27c9fd62014-05-19 13:24:39 +0530672 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
673 i * GPMC_BCH_SIZE;
674 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
675 i * GPMC_BCH_SIZE;
676 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
677 i * GPMC_BCH_SIZE;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530678 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700679}
680
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700681int gpmc_get_client_irq(unsigned irq_config)
682{
683 int i;
684
685 if (hweight32(irq_config) > 1)
686 return 0;
687
688 for (i = 0; i < GPMC_NR_IRQ; i++)
689 if (gpmc_client_irq[i].bitmask & irq_config)
690 return gpmc_client_irq[i].irq;
691
692 return 0;
693}
694
695static int gpmc_irq_endis(unsigned irq, bool endis)
696{
697 int i;
698 u32 regval;
699
700 for (i = 0; i < GPMC_NR_IRQ; i++)
701 if (irq == gpmc_client_irq[i].irq) {
702 regval = gpmc_read_reg(GPMC_IRQENABLE);
703 if (endis)
704 regval |= gpmc_client_irq[i].bitmask;
705 else
706 regval &= ~gpmc_client_irq[i].bitmask;
707 gpmc_write_reg(GPMC_IRQENABLE, regval);
708 break;
709 }
710
711 return 0;
712}
713
714static void gpmc_irq_disable(struct irq_data *p)
715{
716 gpmc_irq_endis(p->irq, false);
717}
718
719static void gpmc_irq_enable(struct irq_data *p)
720{
721 gpmc_irq_endis(p->irq, true);
722}
723
724static void gpmc_irq_noop(struct irq_data *data) { }
725
726static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
727
Afzal Mohammedda496872012-09-23 17:28:25 -0600728static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700729{
730 int i;
731 u32 regval;
732
733 if (!gpmc_irq)
734 return -EINVAL;
735
736 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000737 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700738 pr_err("irq_alloc_descs failed\n");
739 return gpmc_irq_start;
740 }
741
742 gpmc_irq_chip.name = "gpmc";
743 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
744 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
745 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
746 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
747 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
748 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
749 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
750
751 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
752 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
753
754 for (i = 0; i < GPMC_NR_IRQ; i++) {
755 gpmc_client_irq[i].irq = gpmc_irq_start + i;
756 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
757 &gpmc_irq_chip, handle_simple_irq);
758 set_irq_flags(gpmc_client_irq[i].irq,
759 IRQF_VALID | IRQF_NOAUTOEN);
760 }
761
762 /* Disable interrupts */
763 gpmc_write_reg(GPMC_IRQENABLE, 0);
764
765 /* clear interrupts */
766 regval = gpmc_read_reg(GPMC_IRQSTATUS);
767 gpmc_write_reg(GPMC_IRQSTATUS, regval);
768
769 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
770}
771
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800772static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600773{
774 int i;
775
776 if (gpmc_irq)
777 free_irq(gpmc_irq, NULL);
778
779 for (i = 0; i < GPMC_NR_IRQ; i++) {
780 irq_set_handler(gpmc_client_irq[i].irq, NULL);
781 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
782 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
783 }
784
785 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
786
787 return 0;
788}
789
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800790static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600791{
792 int cs;
793
Gupta Pekonf34f3712013-05-31 17:31:30 +0530794 for (cs = 0; cs < gpmc_cs_num; cs++) {
Afzal Mohammedda496872012-09-23 17:28:25 -0600795 if (!gpmc_cs_mem_enabled(cs))
796 continue;
797 gpmc_cs_delete_mem(cs);
798 }
799
800}
801
Jon Hunter84b00f02013-03-06 14:36:47 -0600802static void gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300803{
Jon Hunter84b00f02013-03-06 14:36:47 -0600804 int cs;
Imre Deakf37e4582006-09-25 12:41:33 +0300805
Jon Hunterbf234392013-03-06 14:12:59 -0600806 /*
807 * The first 1MB of GPMC address space is typically mapped to
808 * the internal ROM. Never allocate the first page, to
809 * facilitate bug detection; even if we didn't boot from ROM.
Kyungmin Park7f245162006-12-29 16:48:51 -0800810 */
Jon Hunterbf234392013-03-06 14:12:59 -0600811 gpmc_mem_root.start = SZ_1M;
Imre Deakf37e4582006-09-25 12:41:33 +0300812 gpmc_mem_root.end = GPMC_MEM_END;
813
814 /* Reserve all regions that has been set up by bootloader */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530815 for (cs = 0; cs < gpmc_cs_num; cs++) {
Imre Deakf37e4582006-09-25 12:41:33 +0300816 u32 base, size;
817
818 if (!gpmc_cs_mem_enabled(cs))
819 continue;
820 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter84b00f02013-03-06 14:36:47 -0600821 if (gpmc_cs_insert_mem(cs, base, size)) {
822 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
823 __func__, cs, base, base + size);
824 gpmc_cs_disable_mem(cs);
Jon Hunter81190242012-10-17 09:41:25 -0500825 }
Imre Deakf37e4582006-09-25 12:41:33 +0300826 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700827}
828
Afzal Mohammed246da262012-08-02 20:02:10 +0530829static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
830{
831 u32 temp;
832 int div;
833
834 div = gpmc_calc_divider(sync_clk);
835 temp = gpmc_ps_to_ticks(time_ps);
836 temp = (temp + div - 1) / div;
837 return gpmc_ticks_to_ps(temp * div);
838}
839
840/* XXX: can the cycles be avoided ? */
841static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600842 struct gpmc_device_timings *dev_t,
843 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530844{
Afzal Mohammed246da262012-08-02 20:02:10 +0530845 u32 temp;
846
847 /* adv_rd_off */
848 temp = dev_t->t_avdp_r;
849 /* XXX: mux check required ? */
850 if (mux) {
851 /* XXX: t_avdp not to be required for sync, only added for tusb
852 * this indirectly necessitates requirement of t_avdp_r and
853 * t_avdp_w instead of having a single t_avdp
854 */
855 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
856 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
857 }
858 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
859
860 /* oe_on */
861 temp = dev_t->t_oeasu; /* XXX: remove this ? */
862 if (mux) {
863 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
864 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
865 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
866 }
867 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
868
869 /* access */
870 /* XXX: any scope for improvement ?, by combining oe_on
871 * and clk_activation, need to check whether
872 * access = clk_activation + round to sync clk ?
873 */
874 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
875 temp += gpmc_t->clk_activation;
876 if (dev_t->cyc_oe)
877 temp = max_t(u32, temp, gpmc_t->oe_on +
878 gpmc_ticks_to_ps(dev_t->cyc_oe));
879 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
880
881 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
882 gpmc_t->cs_rd_off = gpmc_t->oe_off;
883
884 /* rd_cycle */
885 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
886 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
887 gpmc_t->access;
888 /* XXX: barter t_ce_rdyz with t_cez_r ? */
889 if (dev_t->t_ce_rdyz)
890 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
891 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
892
893 return 0;
894}
895
896static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600897 struct gpmc_device_timings *dev_t,
898 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530899{
Afzal Mohammed246da262012-08-02 20:02:10 +0530900 u32 temp;
901
902 /* adv_wr_off */
903 temp = dev_t->t_avdp_w;
904 if (mux) {
905 temp = max_t(u32, temp,
906 gpmc_t->clk_activation + dev_t->t_avdh);
907 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
908 }
909 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
910
911 /* wr_data_mux_bus */
912 temp = max_t(u32, dev_t->t_weasu,
913 gpmc_t->clk_activation + dev_t->t_rdyo);
914 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
915 * and in that case remember to handle we_on properly
916 */
917 if (mux) {
918 temp = max_t(u32, temp,
919 gpmc_t->adv_wr_off + dev_t->t_aavdh);
920 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
921 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
922 }
923 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
924
925 /* we_on */
926 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
927 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
928 else
929 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
930
931 /* wr_access */
932 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
933 gpmc_t->wr_access = gpmc_t->access;
934
935 /* we_off */
936 temp = gpmc_t->we_on + dev_t->t_wpl;
937 temp = max_t(u32, temp,
938 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
939 temp = max_t(u32, temp,
940 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
941 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
942
943 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
944 dev_t->t_wph);
945
946 /* wr_cycle */
947 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
948 temp += gpmc_t->wr_access;
949 /* XXX: barter t_ce_rdyz with t_cez_w ? */
950 if (dev_t->t_ce_rdyz)
951 temp = max_t(u32, temp,
952 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
953 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
954
955 return 0;
956}
957
958static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600959 struct gpmc_device_timings *dev_t,
960 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530961{
Afzal Mohammed246da262012-08-02 20:02:10 +0530962 u32 temp;
963
964 /* adv_rd_off */
965 temp = dev_t->t_avdp_r;
966 if (mux)
967 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
968 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
969
970 /* oe_on */
971 temp = dev_t->t_oeasu;
972 if (mux)
973 temp = max_t(u32, temp,
974 gpmc_t->adv_rd_off + dev_t->t_aavdh);
975 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
976
977 /* access */
978 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
979 gpmc_t->oe_on + dev_t->t_oe);
980 temp = max_t(u32, temp,
981 gpmc_t->cs_on + dev_t->t_ce);
982 temp = max_t(u32, temp,
983 gpmc_t->adv_on + dev_t->t_aa);
984 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
985
986 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
987 gpmc_t->cs_rd_off = gpmc_t->oe_off;
988
989 /* rd_cycle */
990 temp = max_t(u32, dev_t->t_rd_cycle,
991 gpmc_t->cs_rd_off + dev_t->t_cez_r);
992 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
993 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
994
995 return 0;
996}
997
998static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600999 struct gpmc_device_timings *dev_t,
1000 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301001{
Afzal Mohammed246da262012-08-02 20:02:10 +05301002 u32 temp;
1003
1004 /* adv_wr_off */
1005 temp = dev_t->t_avdp_w;
1006 if (mux)
1007 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1008 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1009
1010 /* wr_data_mux_bus */
1011 temp = dev_t->t_weasu;
1012 if (mux) {
1013 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1014 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1015 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1016 }
1017 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1018
1019 /* we_on */
1020 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1021 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1022 else
1023 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1024
1025 /* we_off */
1026 temp = gpmc_t->we_on + dev_t->t_wpl;
1027 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1028
1029 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1030 dev_t->t_wph);
1031
1032 /* wr_cycle */
1033 temp = max_t(u32, dev_t->t_wr_cycle,
1034 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1035 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1036
1037 return 0;
1038}
1039
1040static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1041 struct gpmc_device_timings *dev_t)
1042{
1043 u32 temp;
1044
1045 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1046 gpmc_get_fclk_period();
1047
1048 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1049 dev_t->t_bacc,
1050 gpmc_t->sync_clk);
1051
1052 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1053 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1054
1055 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1056 return 0;
1057
1058 if (dev_t->ce_xdelay)
1059 gpmc_t->bool_timings.cs_extra_delay = true;
1060 if (dev_t->avd_xdelay)
1061 gpmc_t->bool_timings.adv_extra_delay = true;
1062 if (dev_t->oe_xdelay)
1063 gpmc_t->bool_timings.oe_extra_delay = true;
1064 if (dev_t->we_xdelay)
1065 gpmc_t->bool_timings.we_extra_delay = true;
1066
1067 return 0;
1068}
1069
1070static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001071 struct gpmc_device_timings *dev_t,
1072 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301073{
1074 u32 temp;
1075
1076 /* cs_on */
1077 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1078
1079 /* adv_on */
1080 temp = dev_t->t_avdasu;
1081 if (dev_t->t_ce_avd)
1082 temp = max_t(u32, temp,
1083 gpmc_t->cs_on + dev_t->t_ce_avd);
1084 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1085
Jon Hunterc3be5b42013-02-21 13:46:22 -06001086 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301087 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1088
1089 return 0;
1090}
1091
1092/* TODO: remove this function once all peripherals are confirmed to
1093 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1094 * has to be modified to handle timings in ps instead of ns
1095*/
1096static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1097{
1098 t->cs_on /= 1000;
1099 t->cs_rd_off /= 1000;
1100 t->cs_wr_off /= 1000;
1101 t->adv_on /= 1000;
1102 t->adv_rd_off /= 1000;
1103 t->adv_wr_off /= 1000;
1104 t->we_on /= 1000;
1105 t->we_off /= 1000;
1106 t->oe_on /= 1000;
1107 t->oe_off /= 1000;
1108 t->page_burst_access /= 1000;
1109 t->access /= 1000;
1110 t->rd_cycle /= 1000;
1111 t->wr_cycle /= 1000;
1112 t->bus_turnaround /= 1000;
1113 t->cycle2cycle_delay /= 1000;
1114 t->wait_monitoring /= 1000;
1115 t->clk_activation /= 1000;
1116 t->wr_access /= 1000;
1117 t->wr_data_mux_bus /= 1000;
1118}
1119
1120int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001121 struct gpmc_settings *gpmc_s,
1122 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301123{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001124 bool mux = false, sync = false;
1125
1126 if (gpmc_s) {
1127 mux = gpmc_s->mux_add_data ? true : false;
1128 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1129 }
1130
Afzal Mohammed246da262012-08-02 20:02:10 +05301131 memset(gpmc_t, 0, sizeof(*gpmc_t));
1132
Jon Hunterc3be5b42013-02-21 13:46:22 -06001133 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301134
Jon Hunterc3be5b42013-02-21 13:46:22 -06001135 if (gpmc_s && gpmc_s->sync_read)
1136 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301137 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001138 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301139
Jon Hunterc3be5b42013-02-21 13:46:22 -06001140 if (gpmc_s && gpmc_s->sync_write)
1141 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301142 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001143 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301144
1145 /* TODO: remove, see function definition */
1146 gpmc_convert_ps_to_ns(gpmc_t);
1147
1148 return 0;
1149}
1150
Jon Hunteraa8d4762013-02-21 15:25:23 -06001151/**
1152 * gpmc_cs_program_settings - programs non-timing related settings
1153 * @cs: GPMC chip-select to program
1154 * @p: pointer to GPMC settings structure
1155 *
1156 * Programs non-timing related settings for a GPMC chip-select, such as
1157 * bus-width, burst configuration, etc. Function should be called once
1158 * for each chip-select that is being used and must be called before
1159 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1160 * register will be initialised to zero by this function. Returns 0 on
1161 * success and appropriate negative error code on failure.
1162 */
1163int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1164{
1165 u32 config1;
1166
1167 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1168 pr_err("%s: invalid width %d!", __func__, p->device_width);
1169 return -EINVAL;
1170 }
1171
1172 /* Address-data multiplexing not supported for NAND devices */
1173 if (p->device_nand && p->mux_add_data) {
1174 pr_err("%s: invalid configuration!\n", __func__);
1175 return -EINVAL;
1176 }
1177
1178 if ((p->mux_add_data > GPMC_MUX_AD) ||
1179 ((p->mux_add_data == GPMC_MUX_AAD) &&
1180 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1181 pr_err("%s: invalid multiplex configuration!\n", __func__);
1182 return -EINVAL;
1183 }
1184
1185 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1186 if (p->burst_read || p->burst_write) {
1187 switch (p->burst_len) {
1188 case GPMC_BURST_4:
1189 case GPMC_BURST_8:
1190 case GPMC_BURST_16:
1191 break;
1192 default:
1193 pr_err("%s: invalid page/burst-length (%d)\n",
1194 __func__, p->burst_len);
1195 return -EINVAL;
1196 }
1197 }
1198
1199 if ((p->wait_on_read || p->wait_on_write) &&
1200 (p->wait_pin > gpmc_nr_waitpins)) {
1201 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1202 return -EINVAL;
1203 }
1204
1205 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1206
1207 if (p->sync_read)
1208 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1209 if (p->sync_write)
1210 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1211 if (p->wait_on_read)
1212 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1213 if (p->wait_on_write)
1214 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1215 if (p->wait_on_read || p->wait_on_write)
1216 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1217 if (p->device_nand)
1218 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1219 if (p->mux_add_data)
1220 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1221 if (p->burst_read)
1222 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1223 if (p->burst_write)
1224 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1225 if (p->burst_read || p->burst_write) {
1226 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1227 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1228 }
1229
1230 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1231
1232 return 0;
1233}
1234
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001235#ifdef CONFIG_OF
1236static struct of_device_id gpmc_dt_ids[] = {
1237 { .compatible = "ti,omap2420-gpmc" },
1238 { .compatible = "ti,omap2430-gpmc" },
1239 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1240 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1241 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1242 { }
1243};
1244MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1245
Jon Hunter8c8a77712013-02-20 15:53:12 -06001246/**
1247 * gpmc_read_settings_dt - read gpmc settings from device-tree
1248 * @np: pointer to device-tree node for a gpmc child device
1249 * @p: pointer to gpmc settings structure
1250 *
1251 * Reads the GPMC settings for a GPMC child device from device-tree and
1252 * stores them in the GPMC settings structure passed. The GPMC settings
1253 * structure is initialised to zero by this function and so any
1254 * previously stored settings will be cleared.
1255 */
1256void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1257{
1258 memset(p, 0, sizeof(struct gpmc_settings));
1259
1260 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1261 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001262 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1263 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1264
1265 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1266 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1267 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1268 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1269 if (!p->burst_read && !p->burst_write)
1270 pr_warn("%s: page/burst-length set but not used!\n",
1271 __func__);
1272 }
1273
1274 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1275 p->wait_on_read = of_property_read_bool(np,
1276 "gpmc,wait-on-read");
1277 p->wait_on_write = of_property_read_bool(np,
1278 "gpmc,wait-on-write");
1279 if (!p->wait_on_read && !p->wait_on_write)
1280 pr_warn("%s: read/write wait monitoring not enabled!\n",
1281 __func__);
1282 }
1283}
1284
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001285static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1286 struct gpmc_timings *gpmc_t)
1287{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001288 struct gpmc_bool_timings *p;
1289
1290 if (!np || !gpmc_t)
1291 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001292
1293 memset(gpmc_t, 0, sizeof(*gpmc_t));
1294
1295 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001296 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001297
1298 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001299 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1300 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1301 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001302
1303 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001304 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1305 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1306 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001307
1308 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001309 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1310 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001311
1312 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001313 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1314 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001315
1316 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001317 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1318 &gpmc_t->page_burst_access);
1319 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1320 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1321 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1322 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1323 &gpmc_t->bus_turnaround);
1324 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1325 &gpmc_t->cycle2cycle_delay);
1326 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1327 &gpmc_t->wait_monitoring);
1328 of_property_read_u32(np, "gpmc,clk-activation-ns",
1329 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001330
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001331 /* only applicable to OMAP3+ */
1332 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1333 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1334 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001335
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001336 /* bool timing parameters */
1337 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001338
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001339 p->cycle2cyclediffcsen =
1340 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1341 p->cycle2cyclesamecsen =
1342 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1343 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1344 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1345 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1346 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1347 p->time_para_granularity =
1348 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001349}
1350
Pekon Gupta6b187b22014-01-28 11:42:40 +05301351#if IS_ENABLED(CONFIG_MTD_NAND)
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001352
Mark Jackson496c8a02013-04-19 21:08:28 +01001353static const char * const nand_xfer_types[] = {
1354 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1355 [NAND_OMAP_POLLED] = "polled",
1356 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1357 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1358};
1359
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001360static int gpmc_probe_nand_child(struct platform_device *pdev,
1361 struct device_node *child)
1362{
1363 u32 val;
1364 const char *s;
1365 struct gpmc_timings gpmc_t;
1366 struct omap_nand_platform_data *gpmc_nand_data;
1367
1368 if (of_property_read_u32(child, "reg", &val) < 0) {
1369 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1370 child->full_name);
1371 return -ENODEV;
1372 }
1373
1374 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1375 GFP_KERNEL);
1376 if (!gpmc_nand_data)
1377 return -ENOMEM;
1378
1379 gpmc_nand_data->cs = val;
1380 gpmc_nand_data->of_node = child;
1381
Pekon Guptaac65caf2013-10-24 18:20:17 +05301382 /* Detect availability of ELM module */
1383 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1384 if (gpmc_nand_data->elm_of_node == NULL)
1385 gpmc_nand_data->elm_of_node =
1386 of_parse_phandle(child, "elm_id", 0);
1387 if (gpmc_nand_data->elm_of_node == NULL)
1388 pr_warn("%s: ti,elm-id property not found\n", __func__);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001389
Pekon Guptaac65caf2013-10-24 18:20:17 +05301390 /* select ecc-scheme for NAND */
1391 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1392 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1393 return -ENODEV;
1394 }
1395 if (!strcmp(s, "ham1") || !strcmp(s, "sw") ||
1396 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
1397 gpmc_nand_data->ecc_opt =
1398 OMAP_ECC_HAM1_CODE_HW;
1399 else if (!strcmp(s, "bch4"))
1400 if (gpmc_nand_data->elm_of_node)
1401 gpmc_nand_data->ecc_opt =
1402 OMAP_ECC_BCH4_CODE_HW;
1403 else
1404 gpmc_nand_data->ecc_opt =
1405 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1406 else if (!strcmp(s, "bch8"))
1407 if (gpmc_nand_data->elm_of_node)
1408 gpmc_nand_data->ecc_opt =
1409 OMAP_ECC_BCH8_CODE_HW;
1410 else
1411 gpmc_nand_data->ecc_opt =
1412 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
pekon gupta27c9fd62014-05-19 13:24:39 +05301413 else if (!strcmp(s, "bch16"))
1414 if (gpmc_nand_data->elm_of_node)
1415 gpmc_nand_data->ecc_opt =
1416 OMAP_ECC_BCH16_CODE_HW;
1417 else
1418 pr_err("%s: BCH16 requires ELM support\n", __func__);
Pekon Guptaac65caf2013-10-24 18:20:17 +05301419 else
1420 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1421
1422 /* select data transfer mode for NAND controller */
Mark Jackson496c8a02013-04-19 21:08:28 +01001423 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1424 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1425 if (!strcasecmp(s, nand_xfer_types[val])) {
1426 gpmc_nand_data->xfer_type = val;
1427 break;
1428 }
1429
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001430 val = of_get_nand_bus_width(child);
1431 if (val == 16)
1432 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1433
1434 gpmc_read_timings_dt(child, &gpmc_t);
1435 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1436
1437 return 0;
1438}
1439#else
1440static int gpmc_probe_nand_child(struct platform_device *pdev,
1441 struct device_node *child)
1442{
1443 return 0;
1444}
1445#endif
1446
Pekon Gupta980386d2014-01-28 11:42:41 +05301447#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001448static int gpmc_probe_onenand_child(struct platform_device *pdev,
1449 struct device_node *child)
1450{
1451 u32 val;
1452 struct omap_onenand_platform_data *gpmc_onenand_data;
1453
1454 if (of_property_read_u32(child, "reg", &val) < 0) {
1455 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1456 child->full_name);
1457 return -ENODEV;
1458 }
1459
1460 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1461 GFP_KERNEL);
1462 if (!gpmc_onenand_data)
1463 return -ENOMEM;
1464
1465 gpmc_onenand_data->cs = val;
1466 gpmc_onenand_data->of_node = child;
1467 gpmc_onenand_data->dma_channel = -1;
1468
1469 if (!of_property_read_u32(child, "dma-channel", &val))
1470 gpmc_onenand_data->dma_channel = val;
1471
1472 gpmc_onenand_init(gpmc_onenand_data);
1473
1474 return 0;
1475}
1476#else
1477static int gpmc_probe_onenand_child(struct platform_device *pdev,
1478 struct device_node *child)
1479{
1480 return 0;
1481}
1482#endif
1483
Jon Huntercdd69282013-02-08 16:46:13 -06001484/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001485 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001486 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001487 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001488 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001489 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001490 * Returns 0 on success and appropriate negative error code on failure.
1491 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001492static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001493 struct device_node *child)
1494{
1495 struct gpmc_settings gpmc_s;
1496 struct gpmc_timings gpmc_t;
1497 struct resource res;
1498 unsigned long base;
1499 int ret, cs;
1500
1501 if (of_property_read_u32(child, "reg", &cs) < 0) {
1502 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1503 child->full_name);
1504 return -ENODEV;
1505 }
1506
1507 if (of_address_to_resource(child, 0, &res) < 0) {
1508 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1509 child->full_name);
1510 return -ENODEV;
1511 }
1512
1513 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1514 if (ret < 0) {
1515 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1516 return ret;
1517 }
1518
1519 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001520 * For some GPMC devices we still need to rely on the bootloader
1521 * timings because the devices can be connected via FPGA. So far
1522 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1523 * REVISIT: Add timing support from slls644g.pdf and from the
1524 * lan91c96 manual.
1525 */
1526 if (of_device_is_compatible(child, "ns16550a") ||
1527 of_device_is_compatible(child, "smsc,lan91c94") ||
1528 of_device_is_compatible(child, "smsc,lan91c111")) {
1529 dev_warn(&pdev->dev,
1530 "%s using bootloader timings on CS%d\n",
1531 child->name, cs);
1532 goto no_timings;
1533 }
1534
1535 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001536 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1537 * location in the gpmc address space. When booting with
1538 * device-tree we want the NOR flash to be mapped to the
1539 * location specified in the device-tree blob. So remap the
1540 * CS to this location. Once DT migration is complete should
1541 * just make gpmc_cs_request() map a specific address.
1542 */
1543 ret = gpmc_cs_remap(cs, res.start);
1544 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001545 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1546 cs, &res.start);
Jon Huntercdd69282013-02-08 16:46:13 -06001547 goto err;
1548 }
1549
1550 gpmc_read_settings_dt(child, &gpmc_s);
1551
1552 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1553 if (ret < 0)
1554 goto err;
1555
1556 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1557 if (ret < 0)
1558 goto err;
1559
1560 gpmc_read_timings_dt(child, &gpmc_t);
1561 gpmc_cs_set_timings(cs, &gpmc_t);
1562
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001563no_timings:
Jon Huntercdd69282013-02-08 16:46:13 -06001564 if (of_platform_device_create(child, NULL, &pdev->dev))
1565 return 0;
1566
1567 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01001568 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06001569
1570err:
1571 gpmc_cs_free(cs);
1572
1573 return ret;
1574}
1575
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001576static int gpmc_probe_dt(struct platform_device *pdev)
1577{
1578 int ret;
1579 struct device_node *child;
1580 const struct of_device_id *of_id =
1581 of_match_device(gpmc_dt_ids, &pdev->dev);
1582
1583 if (!of_id)
1584 return 0;
1585
Gupta Pekonf34f3712013-05-31 17:31:30 +05301586 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1587 &gpmc_cs_num);
1588 if (ret < 0) {
1589 pr_err("%s: number of chip-selects not defined\n", __func__);
1590 return ret;
1591 } else if (gpmc_cs_num < 1) {
1592 pr_err("%s: all chip-selects are disabled\n", __func__);
1593 return -EINVAL;
1594 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1595 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1596 __func__, GPMC_CS_NUM);
1597 return -EINVAL;
1598 }
1599
Jon Hunter9f833152013-02-20 15:53:38 -06001600 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1601 &gpmc_nr_waitpins);
1602 if (ret < 0) {
1603 pr_err("%s: number of wait pins not found!\n", __func__);
1604 return ret;
1605 }
1606
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001607 for_each_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001608
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001609 if (!child->name)
1610 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06001611
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001612 if (of_node_cmp(child->name, "nand") == 0)
1613 ret = gpmc_probe_nand_child(pdev, child);
1614 else if (of_node_cmp(child->name, "onenand") == 0)
1615 ret = gpmc_probe_onenand_child(pdev, child);
1616 else if (of_node_cmp(child->name, "ethernet") == 0 ||
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001617 of_node_cmp(child->name, "nor") == 0 ||
1618 of_node_cmp(child->name, "uart") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001619 ret = gpmc_probe_generic_child(pdev, child);
Jon Huntercdd69282013-02-08 16:46:13 -06001620
Javier Martinez Canillasb327b362013-04-17 22:34:12 +02001621 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1622 __func__, child->full_name))
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001623 of_node_put(child);
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001624 }
1625
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001626 return 0;
1627}
1628#else
1629static int gpmc_probe_dt(struct platform_device *pdev)
1630{
1631 return 0;
1632}
1633#endif
1634
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001635static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001636{
Jon Hunter81190242012-10-17 09:41:25 -05001637 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001638 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001639 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001640
Afzal Mohammedda496872012-09-23 17:28:25 -06001641 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1642 if (res == NULL)
1643 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001644
Afzal Mohammedda496872012-09-23 17:28:25 -06001645 phys_base = res->start;
1646 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001647
Thierry Reding5857bd92013-01-21 11:08:55 +01001648 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1649 if (IS_ERR(gpmc_base))
1650 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001651
1652 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1653 if (res == NULL)
1654 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1655 else
1656 gpmc_irq = res->start;
1657
1658 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1659 if (IS_ERR(gpmc_l3_clk)) {
1660 dev_err(&pdev->dev, "error: clk_get\n");
1661 gpmc_irq = 0;
1662 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001663 }
1664
avinash philipb3f55252013-06-12 16:30:56 +05301665 pm_runtime_enable(&pdev->dev);
1666 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001667
Afzal Mohammedda496872012-09-23 17:28:25 -06001668 gpmc_dev = &pdev->dev;
1669
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001670 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001671
1672 /*
1673 * FIXME: Once device-tree migration is complete the below flags
1674 * should be populated based upon the device-tree compatible
1675 * string. For now just use the IP revision. OMAP3+ devices have
1676 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1677 * devices support the addr-addr-data multiplex protocol.
1678 *
1679 * GPMC IP revisions:
1680 * - OMAP24xx = 2.0
1681 * - OMAP3xxx = 5.0
1682 * - OMAP44xx/54xx/AM335x = 6.0
1683 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001684 if (GPMC_REVISION_MAJOR(l) > 0x4)
1685 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001686 if (GPMC_REVISION_MAJOR(l) > 0x5)
1687 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001688 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1689 GPMC_REVISION_MINOR(l));
1690
Jon Hunter84b00f02013-03-06 14:36:47 -06001691 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301692
Russell King71856842013-03-13 20:44:21 +00001693 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001694 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1695
Jon Hunter31d9adc2013-02-18 07:57:39 -06001696 /* Now the GPMC is initialised, unreserve the chip-selects */
1697 gpmc_cs_map = 0;
1698
Gupta Pekonf34f3712013-05-31 17:31:30 +05301699 if (!pdev->dev.of_node) {
1700 gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -06001701 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
Gupta Pekonf34f3712013-05-31 17:31:30 +05301702 }
Jon Hunter9f833152013-02-20 15:53:38 -06001703
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001704 rc = gpmc_probe_dt(pdev);
1705 if (rc < 0) {
avinash philipb3f55252013-06-12 16:30:56 +05301706 pm_runtime_put_sync(&pdev->dev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001707 clk_put(gpmc_l3_clk);
1708 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1709 return rc;
1710 }
1711
Afzal Mohammedda496872012-09-23 17:28:25 -06001712 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301713}
Afzal Mohammedda496872012-09-23 17:28:25 -06001714
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001715static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001716{
1717 gpmc_free_irq();
1718 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05301719 pm_runtime_put_sync(&pdev->dev);
1720 pm_runtime_disable(&pdev->dev);
Afzal Mohammedda496872012-09-23 17:28:25 -06001721 gpmc_dev = NULL;
1722 return 0;
1723}
1724
avinash philipb536dd42013-06-18 00:16:38 +05301725#ifdef CONFIG_PM_SLEEP
1726static int gpmc_suspend(struct device *dev)
1727{
1728 omap3_gpmc_save_context();
1729 pm_runtime_put_sync(dev);
1730 return 0;
1731}
1732
1733static int gpmc_resume(struct device *dev)
1734{
1735 pm_runtime_get_sync(dev);
1736 omap3_gpmc_restore_context();
1737 return 0;
1738}
1739#endif
1740
1741static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1742
Afzal Mohammedda496872012-09-23 17:28:25 -06001743static struct platform_driver gpmc_driver = {
1744 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001745 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001746 .driver = {
1747 .name = DEVICE_NAME,
1748 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001749 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05301750 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06001751 },
1752};
1753
1754static __init int gpmc_init(void)
1755{
1756 return platform_driver_register(&gpmc_driver);
1757}
1758
1759static __exit void gpmc_exit(void)
1760{
1761 platform_driver_unregister(&gpmc_driver);
1762
1763}
1764
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001765omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001766module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301767
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001768static int __init omap_gpmc_init(void)
1769{
1770 struct omap_hwmod *oh;
1771 struct platform_device *pdev;
1772 char *oh_name = "gpmc";
1773
Daniel Mack2f98ca82012-12-14 11:36:40 +01001774 /*
1775 * if the board boots up with a populated DT, do not
1776 * manually add the device from this initcall
1777 */
1778 if (of_have_populated_dt())
1779 return -ENODEV;
1780
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001781 oh = omap_hwmod_lookup(oh_name);
1782 if (!oh) {
1783 pr_err("Could not look up %s\n", oh_name);
1784 return -ENODEV;
1785 }
1786
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001787 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001788 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1789
Thomas Meyer12616742013-06-01 11:44:44 +02001790 return PTR_RET(pdev);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001791}
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001792omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001793
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301794static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1795{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001796 int i;
1797 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301798
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001799 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1800
1801 if (!regval)
1802 return IRQ_NONE;
1803
1804 for (i = 0; i < GPMC_NR_IRQ; i++)
1805 if (regval & gpmc_client_irq[i].bitmask)
1806 generic_handle_irq(gpmc_client_irq[i].irq);
1807
1808 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301809
1810 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001811}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301812
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301813static struct omap3_gpmc_regs gpmc_context;
1814
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001815void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301816{
1817 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001818
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301819 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1820 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1821 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1822 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1823 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1824 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1825 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301826 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301827 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1828 if (gpmc_context.cs_context[i].is_valid) {
1829 gpmc_context.cs_context[i].config1 =
1830 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1831 gpmc_context.cs_context[i].config2 =
1832 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1833 gpmc_context.cs_context[i].config3 =
1834 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1835 gpmc_context.cs_context[i].config4 =
1836 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1837 gpmc_context.cs_context[i].config5 =
1838 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1839 gpmc_context.cs_context[i].config6 =
1840 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1841 gpmc_context.cs_context[i].config7 =
1842 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1843 }
1844 }
1845}
1846
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001847void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301848{
1849 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001850
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301851 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1852 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1853 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1854 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1855 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1856 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1857 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301858 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301859 if (gpmc_context.cs_context[i].is_valid) {
1860 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1861 gpmc_context.cs_context[i].config1);
1862 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1863 gpmc_context.cs_context[i].config2);
1864 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1865 gpmc_context.cs_context[i].config3);
1866 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1867 gpmc_context.cs_context[i].config4);
1868 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1869 gpmc_context.cs_context[i].config5);
1870 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1871 gpmc_context.cs_context[i].config6);
1872 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1873 gpmc_context.cs_context[i].config7);
1874 }
1875 }
1876}