blob: 84e57e6fbc26332dcc2bbddefc60146661393ec4 [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 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070071
Yegor Yefremov2c65e742012-05-09 08:32:49 -070072/* GPMC ECC control settings */
73#define GPMC_ECC_CTRL_ECCCLEAR 0x100
74#define GPMC_ECC_CTRL_ECCDISABLE 0x000
75#define GPMC_ECC_CTRL_ECCREG1 0x001
76#define GPMC_ECC_CTRL_ECCREG2 0x002
77#define GPMC_ECC_CTRL_ECCREG3 0x003
78#define GPMC_ECC_CTRL_ECCREG4 0x004
79#define GPMC_ECC_CTRL_ECCREG5 0x005
80#define GPMC_ECC_CTRL_ECCREG6 0x006
81#define GPMC_ECC_CTRL_ECCREG7 0x007
82#define GPMC_ECC_CTRL_ECCREG8 0x008
83#define GPMC_ECC_CTRL_ECCREG9 0x009
84
Afzal Mohammed559d94b2012-05-28 17:51:37 +053085#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
86#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
87#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
88#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
89#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
90#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
91
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000092#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070093#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053094#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070095
Imre Deakf37e4582006-09-25 12:41:33 +030096#define GPMC_MEM_END 0x3FFFFFFF
Imre Deakf37e4582006-09-25 12:41:33 +030097
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
Jon Hunteraa8d4762013-02-21 15:25:23 -0600110#define GPMC_HAS_MUX_AAD 0x4
Afzal Mohammedda496872012-09-23 17:28:25 -0600111
Jon Hunter9f833152013-02-20 15:53:38 -0600112#define GPMC_NR_WAITPINS 4
113
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700114/* XXX: Only NAND irq has been considered,currently these are the only ones used
115 */
116#define GPMC_NR_IRQ 2
117
118struct gpmc_client_irq {
119 unsigned irq;
120 u32 bitmask;
121};
122
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530123/* Structure to save gpmc cs context */
124struct gpmc_cs_config {
125 u32 config1;
126 u32 config2;
127 u32 config3;
128 u32 config4;
129 u32 config5;
130 u32 config6;
131 u32 config7;
132 int is_valid;
133};
134
135/*
136 * Structure to save/restore gpmc context
137 * to support core off on OMAP3
138 */
139struct omap3_gpmc_regs {
140 u32 sysconfig;
141 u32 irqenable;
142 u32 timeout_ctrl;
143 u32 config;
144 u32 prefetch_config1;
145 u32 prefetch_config2;
146 u32 prefetch_control;
147 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
148};
149
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700150static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
151static struct irq_chip gpmc_irq_chip;
Chen Gangaf072192013-08-22 15:47:21 +0800152static int gpmc_irq_start;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700153
Imre Deakf37e4582006-09-25 12:41:33 +0300154static struct resource gpmc_mem_root;
155static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700156static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600157/* Define chip-selects as reserved by default until probe completes */
158static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530159static unsigned int gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -0600160static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600161static struct device *gpmc_dev;
162static int gpmc_irq;
163static resource_size_t phys_base, mem_size;
164static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300165static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700166
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300167static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700168
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530169static irqreturn_t gpmc_handle_irq(int irq, void *dev);
170
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700171static void gpmc_write_reg(int idx, u32 val)
172{
173 __raw_writel(val, gpmc_base + idx);
174}
175
176static u32 gpmc_read_reg(int idx)
177{
178 return __raw_readl(gpmc_base + idx);
179}
180
181void gpmc_cs_write_reg(int cs, int idx, u32 val)
182{
183 void __iomem *reg_addr;
184
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000185 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700186 __raw_writel(val, reg_addr);
187}
188
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300189static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700190{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300191 void __iomem *reg_addr;
192
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000193 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300194 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700195}
196
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300197/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300198static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700199{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300200 unsigned long rate = clk_get_rate(gpmc_l3_clk);
201
202 if (rate == 0) {
203 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
204 return 0;
205 }
206
207 rate /= 1000;
208 rate = 1000000000 / rate; /* In picoseconds */
209
210 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700211}
212
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300213static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700214{
215 unsigned long tick_ps;
216
217 /* Calculate in picosecs to yield more exact results */
218 tick_ps = gpmc_get_fclk_period();
219
220 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
221}
222
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300223static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200224{
225 unsigned long tick_ps;
226
227 /* Calculate in picosecs to yield more exact results */
228 tick_ps = gpmc_get_fclk_period();
229
230 return (time_ps + tick_ps - 1) / tick_ps;
231}
232
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300233unsigned int gpmc_ticks_to_ns(unsigned int ticks)
234{
235 return ticks * gpmc_get_fclk_period() / 1000;
236}
237
Afzal Mohammed246da262012-08-02 20:02:10 +0530238static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
239{
240 return ticks * gpmc_get_fclk_period();
241}
242
243static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
244{
245 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
246
247 return ticks * gpmc_get_fclk_period();
248}
249
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530250static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
251{
252 u32 l;
253
254 l = gpmc_cs_read_reg(cs, reg);
255 if (value)
256 l |= mask;
257 else
258 l &= ~mask;
259 gpmc_cs_write_reg(cs, reg, l);
260}
261
262static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
263{
264 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
265 GPMC_CONFIG1_TIME_PARA_GRAN,
266 p->time_para_granularity);
267 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
268 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
269 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
270 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
271 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
272 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
273 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
274 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
275 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
276 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
277 p->cycle2cyclesamecsen);
278 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
279 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
280 p->cycle2cyclediffcsen);
281}
282
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700283#ifdef DEBUG
284static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700285 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700286#else
287static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
288 int time)
289#endif
290{
291 u32 l;
292 int ticks, mask, nr_bits;
293
294 if (time == 0)
295 ticks = 0;
296 else
297 ticks = gpmc_ns_to_ticks(time);
298 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800299 if (ticks >= 1 << nr_bits) {
300#ifdef DEBUG
301 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
302 cs, name, time, ticks, 1 << nr_bits);
303#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700304 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800305 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700306
307 mask = (1 << nr_bits) - 1;
308 l = gpmc_cs_read_reg(cs, reg);
309#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800310 printk(KERN_INFO
311 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700312 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800313 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700314#endif
315 l &= ~(mask << st_bit);
316 l |= ticks << st_bit;
317 gpmc_cs_write_reg(cs, reg, l);
318
319 return 0;
320}
321
322#ifdef DEBUG
323#define GPMC_SET_ONE(reg, st, end, field) \
324 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
325 t->field, #field) < 0) \
326 return -1
327#else
328#define GPMC_SET_ONE(reg, st, end, field) \
329 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
330 return -1
331#endif
332
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530333int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700334{
335 int div;
336 u32 l;
337
Adrian Huntera3551f52010-12-09 10:48:27 +0200338 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700339 div = l / gpmc_get_fclk_period();
340 if (div > 4)
341 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800342 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700343 div = 1;
344
345 return div;
346}
347
348int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
349{
350 int div;
351 u32 l;
352
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530353 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700354 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600355 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700356
357 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
358 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
359 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
360
361 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
362 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
363 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
364
365 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
366 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
367 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
368 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
369
370 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
371 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
372 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
373
374 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
375
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530376 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
377 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
378
379 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
380 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
381
Afzal Mohammedda496872012-09-23 17:28:25 -0600382 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300383 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600384 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300385 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300386
David Brownell1c22cc12006-12-06 17:13:55 -0800387 /* caller is expected to have initialized CONFIG1 to cover
388 * at least sync vs async
389 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700390 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800391 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
392#ifdef DEBUG
393 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
394 cs, (div * gpmc_get_fclk_period()) / 1000, div);
395#endif
396 l &= ~0x03;
397 l |= (div - 1);
398 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
399 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700400
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530401 gpmc_cs_bool_timings(cs, &t->bool_timings);
402
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700403 return 0;
404}
405
Jon Hunterc71f8e92013-03-06 12:00:10 -0600406static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700407{
Imre Deakf37e4582006-09-25 12:41:33 +0300408 u32 l;
409 u32 mask;
410
Jon Hunterc71f8e92013-03-06 12:00:10 -0600411 /*
412 * Ensure that base address is aligned on a
413 * boundary equal to or greater than size.
414 */
415 if (base & (size - 1))
416 return -EINVAL;
417
Imre Deakf37e4582006-09-25 12:41:33 +0300418 mask = (1 << GPMC_SECTION_SHIFT) - size;
419 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
420 l &= ~0x3f;
421 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
422 l &= ~(0x0f << 8);
423 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530424 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300425 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600426
427 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300428}
429
430static void gpmc_cs_disable_mem(int cs)
431{
432 u32 l;
433
434 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530435 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300436 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
437}
438
439static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
440{
441 u32 l;
442 u32 mask;
443
444 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
445 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
446 mask = (l >> 8) & 0x0f;
447 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
448}
449
450static int gpmc_cs_mem_enabled(int cs)
451{
452 u32 l;
453
454 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530455 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300456}
457
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300458static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300459{
460 gpmc_cs_map &= ~(1 << cs);
461 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
462}
463
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300464static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300465{
466 return gpmc_cs_map & (1 << cs);
467}
468
469static unsigned long gpmc_mem_align(unsigned long size)
470{
471 int order;
472
473 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
474 order = GPMC_CHUNK_SHIFT - 1;
475 do {
476 size >>= 1;
477 order++;
478 } while (size);
479 size = 1 << order;
480 return size;
481}
482
483static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
484{
485 struct resource *res = &gpmc_cs_mem[cs];
486 int r;
487
488 size = gpmc_mem_align(size);
489 spin_lock(&gpmc_mem_lock);
490 res->start = base;
491 res->end = base + size - 1;
492 r = request_resource(&gpmc_mem_root, res);
493 spin_unlock(&gpmc_mem_lock);
494
495 return r;
496}
497
Afzal Mohammedda496872012-09-23 17:28:25 -0600498static int gpmc_cs_delete_mem(int cs)
499{
500 struct resource *res = &gpmc_cs_mem[cs];
501 int r;
502
503 spin_lock(&gpmc_mem_lock);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700504 r = release_resource(res);
Afzal Mohammedda496872012-09-23 17:28:25 -0600505 res->start = 0;
506 res->end = 0;
507 spin_unlock(&gpmc_mem_lock);
508
509 return r;
510}
511
Jon Huntercdd69282013-02-08 16:46:13 -0600512/**
513 * gpmc_cs_remap - remaps a chip-select physical base address
514 * @cs: chip-select to remap
515 * @base: physical base address to re-map chip-select to
516 *
517 * Re-maps a chip-select to a new physical base address specified by
518 * "base". Returns 0 on success and appropriate negative error code
519 * on failure.
520 */
521static int gpmc_cs_remap(int cs, u32 base)
522{
523 int ret;
524 u32 old_base, size;
525
Gupta Pekonf34f3712013-05-31 17:31:30 +0530526 if (cs > gpmc_cs_num) {
527 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600528 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530529 }
Jon Huntercdd69282013-02-08 16:46:13 -0600530 gpmc_cs_get_memconf(cs, &old_base, &size);
531 if (base == old_base)
532 return 0;
533 gpmc_cs_disable_mem(cs);
534 ret = gpmc_cs_delete_mem(cs);
535 if (ret < 0)
536 return ret;
537 ret = gpmc_cs_insert_mem(cs, base, size);
538 if (ret < 0)
539 return ret;
Jon Hunterc71f8e92013-03-06 12:00:10 -0600540 ret = gpmc_cs_enable_mem(cs, base, size);
541 if (ret < 0)
542 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600543
544 return 0;
545}
546
Imre Deakf37e4582006-09-25 12:41:33 +0300547int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
548{
549 struct resource *res = &gpmc_cs_mem[cs];
550 int r = -1;
551
Gupta Pekonf34f3712013-05-31 17:31:30 +0530552 if (cs > gpmc_cs_num) {
553 pr_err("%s: requested chip-select is disabled\n", __func__);
Imre Deakf37e4582006-09-25 12:41:33 +0300554 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530555 }
Imre Deakf37e4582006-09-25 12:41:33 +0300556 size = gpmc_mem_align(size);
557 if (size > (1 << GPMC_SECTION_SHIFT))
558 return -ENOMEM;
559
560 spin_lock(&gpmc_mem_lock);
561 if (gpmc_cs_reserved(cs)) {
562 r = -EBUSY;
563 goto out;
564 }
565 if (gpmc_cs_mem_enabled(cs))
566 r = adjust_resource(res, res->start & ~(size - 1), size);
567 if (r < 0)
568 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
569 size, NULL, NULL);
570 if (r < 0)
571 goto out;
572
Jon Hunterc71f8e92013-03-06 12:00:10 -0600573 r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
574 if (r < 0) {
575 release_resource(res);
576 goto out;
577 }
578
Imre Deakf37e4582006-09-25 12:41:33 +0300579 *base = res->start;
580 gpmc_cs_set_reserved(cs, 1);
581out:
582 spin_unlock(&gpmc_mem_lock);
583 return r;
584}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300585EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300586
587void gpmc_cs_free(int cs)
588{
Tony Lindgrenefe80722014-04-21 19:26:13 -0700589 struct resource *res = &gpmc_cs_mem[cs];
590
Imre Deakf37e4582006-09-25 12:41:33 +0300591 spin_lock(&gpmc_mem_lock);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530592 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300593 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
594 BUG();
595 spin_unlock(&gpmc_mem_lock);
596 return;
597 }
598 gpmc_cs_disable_mem(cs);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700599 if (res->flags)
600 release_resource(res);
Imre Deakf37e4582006-09-25 12:41:33 +0300601 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;
672 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700673}
674
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700675int gpmc_get_client_irq(unsigned irq_config)
676{
677 int i;
678
679 if (hweight32(irq_config) > 1)
680 return 0;
681
682 for (i = 0; i < GPMC_NR_IRQ; i++)
683 if (gpmc_client_irq[i].bitmask & irq_config)
684 return gpmc_client_irq[i].irq;
685
686 return 0;
687}
688
689static int gpmc_irq_endis(unsigned irq, bool endis)
690{
691 int i;
692 u32 regval;
693
694 for (i = 0; i < GPMC_NR_IRQ; i++)
695 if (irq == gpmc_client_irq[i].irq) {
696 regval = gpmc_read_reg(GPMC_IRQENABLE);
697 if (endis)
698 regval |= gpmc_client_irq[i].bitmask;
699 else
700 regval &= ~gpmc_client_irq[i].bitmask;
701 gpmc_write_reg(GPMC_IRQENABLE, regval);
702 break;
703 }
704
705 return 0;
706}
707
708static void gpmc_irq_disable(struct irq_data *p)
709{
710 gpmc_irq_endis(p->irq, false);
711}
712
713static void gpmc_irq_enable(struct irq_data *p)
714{
715 gpmc_irq_endis(p->irq, true);
716}
717
718static void gpmc_irq_noop(struct irq_data *data) { }
719
720static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
721
Afzal Mohammedda496872012-09-23 17:28:25 -0600722static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700723{
724 int i;
725 u32 regval;
726
727 if (!gpmc_irq)
728 return -EINVAL;
729
730 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000731 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700732 pr_err("irq_alloc_descs failed\n");
733 return gpmc_irq_start;
734 }
735
736 gpmc_irq_chip.name = "gpmc";
737 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
738 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
739 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
740 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
741 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
742 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
743 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
744
745 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
746 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
747
748 for (i = 0; i < GPMC_NR_IRQ; i++) {
749 gpmc_client_irq[i].irq = gpmc_irq_start + i;
750 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
751 &gpmc_irq_chip, handle_simple_irq);
752 set_irq_flags(gpmc_client_irq[i].irq,
753 IRQF_VALID | IRQF_NOAUTOEN);
754 }
755
756 /* Disable interrupts */
757 gpmc_write_reg(GPMC_IRQENABLE, 0);
758
759 /* clear interrupts */
760 regval = gpmc_read_reg(GPMC_IRQSTATUS);
761 gpmc_write_reg(GPMC_IRQSTATUS, regval);
762
763 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
764}
765
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800766static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600767{
768 int i;
769
770 if (gpmc_irq)
771 free_irq(gpmc_irq, NULL);
772
773 for (i = 0; i < GPMC_NR_IRQ; i++) {
774 irq_set_handler(gpmc_client_irq[i].irq, NULL);
775 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
776 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
777 }
778
779 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
780
781 return 0;
782}
783
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800784static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600785{
786 int cs;
787
Gupta Pekonf34f3712013-05-31 17:31:30 +0530788 for (cs = 0; cs < gpmc_cs_num; cs++) {
Afzal Mohammedda496872012-09-23 17:28:25 -0600789 if (!gpmc_cs_mem_enabled(cs))
790 continue;
791 gpmc_cs_delete_mem(cs);
792 }
793
794}
795
Jon Hunter84b00f02013-03-06 14:36:47 -0600796static void gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300797{
Jon Hunter84b00f02013-03-06 14:36:47 -0600798 int cs;
Imre Deakf37e4582006-09-25 12:41:33 +0300799
Jon Hunterbf234392013-03-06 14:12:59 -0600800 /*
801 * The first 1MB of GPMC address space is typically mapped to
802 * the internal ROM. Never allocate the first page, to
803 * facilitate bug detection; even if we didn't boot from ROM.
Kyungmin Park7f245162006-12-29 16:48:51 -0800804 */
Jon Hunterbf234392013-03-06 14:12:59 -0600805 gpmc_mem_root.start = SZ_1M;
Imre Deakf37e4582006-09-25 12:41:33 +0300806 gpmc_mem_root.end = GPMC_MEM_END;
807
808 /* Reserve all regions that has been set up by bootloader */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530809 for (cs = 0; cs < gpmc_cs_num; cs++) {
Imre Deakf37e4582006-09-25 12:41:33 +0300810 u32 base, size;
811
812 if (!gpmc_cs_mem_enabled(cs))
813 continue;
814 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter84b00f02013-03-06 14:36:47 -0600815 if (gpmc_cs_insert_mem(cs, base, size)) {
816 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
817 __func__, cs, base, base + size);
818 gpmc_cs_disable_mem(cs);
Jon Hunter81190242012-10-17 09:41:25 -0500819 }
Imre Deakf37e4582006-09-25 12:41:33 +0300820 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700821}
822
Afzal Mohammed246da262012-08-02 20:02:10 +0530823static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
824{
825 u32 temp;
826 int div;
827
828 div = gpmc_calc_divider(sync_clk);
829 temp = gpmc_ps_to_ticks(time_ps);
830 temp = (temp + div - 1) / div;
831 return gpmc_ticks_to_ps(temp * div);
832}
833
834/* XXX: can the cycles be avoided ? */
835static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600836 struct gpmc_device_timings *dev_t,
837 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530838{
Afzal Mohammed246da262012-08-02 20:02:10 +0530839 u32 temp;
840
841 /* adv_rd_off */
842 temp = dev_t->t_avdp_r;
843 /* XXX: mux check required ? */
844 if (mux) {
845 /* XXX: t_avdp not to be required for sync, only added for tusb
846 * this indirectly necessitates requirement of t_avdp_r and
847 * t_avdp_w instead of having a single t_avdp
848 */
849 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
850 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
851 }
852 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
853
854 /* oe_on */
855 temp = dev_t->t_oeasu; /* XXX: remove this ? */
856 if (mux) {
857 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
858 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
859 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
860 }
861 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
862
863 /* access */
864 /* XXX: any scope for improvement ?, by combining oe_on
865 * and clk_activation, need to check whether
866 * access = clk_activation + round to sync clk ?
867 */
868 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
869 temp += gpmc_t->clk_activation;
870 if (dev_t->cyc_oe)
871 temp = max_t(u32, temp, gpmc_t->oe_on +
872 gpmc_ticks_to_ps(dev_t->cyc_oe));
873 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
874
875 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
876 gpmc_t->cs_rd_off = gpmc_t->oe_off;
877
878 /* rd_cycle */
879 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
880 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
881 gpmc_t->access;
882 /* XXX: barter t_ce_rdyz with t_cez_r ? */
883 if (dev_t->t_ce_rdyz)
884 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
885 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
886
887 return 0;
888}
889
890static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600891 struct gpmc_device_timings *dev_t,
892 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530893{
Afzal Mohammed246da262012-08-02 20:02:10 +0530894 u32 temp;
895
896 /* adv_wr_off */
897 temp = dev_t->t_avdp_w;
898 if (mux) {
899 temp = max_t(u32, temp,
900 gpmc_t->clk_activation + dev_t->t_avdh);
901 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
902 }
903 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
904
905 /* wr_data_mux_bus */
906 temp = max_t(u32, dev_t->t_weasu,
907 gpmc_t->clk_activation + dev_t->t_rdyo);
908 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
909 * and in that case remember to handle we_on properly
910 */
911 if (mux) {
912 temp = max_t(u32, temp,
913 gpmc_t->adv_wr_off + dev_t->t_aavdh);
914 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
915 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
916 }
917 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
918
919 /* we_on */
920 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
921 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
922 else
923 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
924
925 /* wr_access */
926 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
927 gpmc_t->wr_access = gpmc_t->access;
928
929 /* we_off */
930 temp = gpmc_t->we_on + dev_t->t_wpl;
931 temp = max_t(u32, temp,
932 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
933 temp = max_t(u32, temp,
934 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
935 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
936
937 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
938 dev_t->t_wph);
939
940 /* wr_cycle */
941 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
942 temp += gpmc_t->wr_access;
943 /* XXX: barter t_ce_rdyz with t_cez_w ? */
944 if (dev_t->t_ce_rdyz)
945 temp = max_t(u32, temp,
946 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
947 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
948
949 return 0;
950}
951
952static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600953 struct gpmc_device_timings *dev_t,
954 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530955{
Afzal Mohammed246da262012-08-02 20:02:10 +0530956 u32 temp;
957
958 /* adv_rd_off */
959 temp = dev_t->t_avdp_r;
960 if (mux)
961 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
962 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
963
964 /* oe_on */
965 temp = dev_t->t_oeasu;
966 if (mux)
967 temp = max_t(u32, temp,
968 gpmc_t->adv_rd_off + dev_t->t_aavdh);
969 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
970
971 /* access */
972 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
973 gpmc_t->oe_on + dev_t->t_oe);
974 temp = max_t(u32, temp,
975 gpmc_t->cs_on + dev_t->t_ce);
976 temp = max_t(u32, temp,
977 gpmc_t->adv_on + dev_t->t_aa);
978 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
979
980 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
981 gpmc_t->cs_rd_off = gpmc_t->oe_off;
982
983 /* rd_cycle */
984 temp = max_t(u32, dev_t->t_rd_cycle,
985 gpmc_t->cs_rd_off + dev_t->t_cez_r);
986 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
987 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
988
989 return 0;
990}
991
992static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600993 struct gpmc_device_timings *dev_t,
994 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530995{
Afzal Mohammed246da262012-08-02 20:02:10 +0530996 u32 temp;
997
998 /* adv_wr_off */
999 temp = dev_t->t_avdp_w;
1000 if (mux)
1001 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1002 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1003
1004 /* wr_data_mux_bus */
1005 temp = dev_t->t_weasu;
1006 if (mux) {
1007 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1008 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1009 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1010 }
1011 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1012
1013 /* we_on */
1014 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1015 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1016 else
1017 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1018
1019 /* we_off */
1020 temp = gpmc_t->we_on + dev_t->t_wpl;
1021 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1022
1023 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1024 dev_t->t_wph);
1025
1026 /* wr_cycle */
1027 temp = max_t(u32, dev_t->t_wr_cycle,
1028 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1029 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1030
1031 return 0;
1032}
1033
1034static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1035 struct gpmc_device_timings *dev_t)
1036{
1037 u32 temp;
1038
1039 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1040 gpmc_get_fclk_period();
1041
1042 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1043 dev_t->t_bacc,
1044 gpmc_t->sync_clk);
1045
1046 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1047 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1048
1049 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1050 return 0;
1051
1052 if (dev_t->ce_xdelay)
1053 gpmc_t->bool_timings.cs_extra_delay = true;
1054 if (dev_t->avd_xdelay)
1055 gpmc_t->bool_timings.adv_extra_delay = true;
1056 if (dev_t->oe_xdelay)
1057 gpmc_t->bool_timings.oe_extra_delay = true;
1058 if (dev_t->we_xdelay)
1059 gpmc_t->bool_timings.we_extra_delay = true;
1060
1061 return 0;
1062}
1063
1064static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001065 struct gpmc_device_timings *dev_t,
1066 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301067{
1068 u32 temp;
1069
1070 /* cs_on */
1071 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1072
1073 /* adv_on */
1074 temp = dev_t->t_avdasu;
1075 if (dev_t->t_ce_avd)
1076 temp = max_t(u32, temp,
1077 gpmc_t->cs_on + dev_t->t_ce_avd);
1078 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1079
Jon Hunterc3be5b42013-02-21 13:46:22 -06001080 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301081 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1082
1083 return 0;
1084}
1085
1086/* TODO: remove this function once all peripherals are confirmed to
1087 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1088 * has to be modified to handle timings in ps instead of ns
1089*/
1090static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1091{
1092 t->cs_on /= 1000;
1093 t->cs_rd_off /= 1000;
1094 t->cs_wr_off /= 1000;
1095 t->adv_on /= 1000;
1096 t->adv_rd_off /= 1000;
1097 t->adv_wr_off /= 1000;
1098 t->we_on /= 1000;
1099 t->we_off /= 1000;
1100 t->oe_on /= 1000;
1101 t->oe_off /= 1000;
1102 t->page_burst_access /= 1000;
1103 t->access /= 1000;
1104 t->rd_cycle /= 1000;
1105 t->wr_cycle /= 1000;
1106 t->bus_turnaround /= 1000;
1107 t->cycle2cycle_delay /= 1000;
1108 t->wait_monitoring /= 1000;
1109 t->clk_activation /= 1000;
1110 t->wr_access /= 1000;
1111 t->wr_data_mux_bus /= 1000;
1112}
1113
1114int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001115 struct gpmc_settings *gpmc_s,
1116 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301117{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001118 bool mux = false, sync = false;
1119
1120 if (gpmc_s) {
1121 mux = gpmc_s->mux_add_data ? true : false;
1122 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1123 }
1124
Afzal Mohammed246da262012-08-02 20:02:10 +05301125 memset(gpmc_t, 0, sizeof(*gpmc_t));
1126
Jon Hunterc3be5b42013-02-21 13:46:22 -06001127 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301128
Jon Hunterc3be5b42013-02-21 13:46:22 -06001129 if (gpmc_s && gpmc_s->sync_read)
1130 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301131 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001132 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301133
Jon Hunterc3be5b42013-02-21 13:46:22 -06001134 if (gpmc_s && gpmc_s->sync_write)
1135 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301136 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001137 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301138
1139 /* TODO: remove, see function definition */
1140 gpmc_convert_ps_to_ns(gpmc_t);
1141
1142 return 0;
1143}
1144
Jon Hunteraa8d4762013-02-21 15:25:23 -06001145/**
1146 * gpmc_cs_program_settings - programs non-timing related settings
1147 * @cs: GPMC chip-select to program
1148 * @p: pointer to GPMC settings structure
1149 *
1150 * Programs non-timing related settings for a GPMC chip-select, such as
1151 * bus-width, burst configuration, etc. Function should be called once
1152 * for each chip-select that is being used and must be called before
1153 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1154 * register will be initialised to zero by this function. Returns 0 on
1155 * success and appropriate negative error code on failure.
1156 */
1157int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1158{
1159 u32 config1;
1160
1161 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1162 pr_err("%s: invalid width %d!", __func__, p->device_width);
1163 return -EINVAL;
1164 }
1165
1166 /* Address-data multiplexing not supported for NAND devices */
1167 if (p->device_nand && p->mux_add_data) {
1168 pr_err("%s: invalid configuration!\n", __func__);
1169 return -EINVAL;
1170 }
1171
1172 if ((p->mux_add_data > GPMC_MUX_AD) ||
1173 ((p->mux_add_data == GPMC_MUX_AAD) &&
1174 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1175 pr_err("%s: invalid multiplex configuration!\n", __func__);
1176 return -EINVAL;
1177 }
1178
1179 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1180 if (p->burst_read || p->burst_write) {
1181 switch (p->burst_len) {
1182 case GPMC_BURST_4:
1183 case GPMC_BURST_8:
1184 case GPMC_BURST_16:
1185 break;
1186 default:
1187 pr_err("%s: invalid page/burst-length (%d)\n",
1188 __func__, p->burst_len);
1189 return -EINVAL;
1190 }
1191 }
1192
1193 if ((p->wait_on_read || p->wait_on_write) &&
1194 (p->wait_pin > gpmc_nr_waitpins)) {
1195 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1196 return -EINVAL;
1197 }
1198
1199 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1200
1201 if (p->sync_read)
1202 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1203 if (p->sync_write)
1204 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1205 if (p->wait_on_read)
1206 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1207 if (p->wait_on_write)
1208 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1209 if (p->wait_on_read || p->wait_on_write)
1210 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1211 if (p->device_nand)
1212 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1213 if (p->mux_add_data)
1214 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1215 if (p->burst_read)
1216 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1217 if (p->burst_write)
1218 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1219 if (p->burst_read || p->burst_write) {
1220 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1221 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1222 }
1223
1224 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1225
1226 return 0;
1227}
1228
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001229#ifdef CONFIG_OF
1230static struct of_device_id gpmc_dt_ids[] = {
1231 { .compatible = "ti,omap2420-gpmc" },
1232 { .compatible = "ti,omap2430-gpmc" },
1233 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1234 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1235 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1236 { }
1237};
1238MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1239
Jon Hunter8c8a77712013-02-20 15:53:12 -06001240/**
1241 * gpmc_read_settings_dt - read gpmc settings from device-tree
1242 * @np: pointer to device-tree node for a gpmc child device
1243 * @p: pointer to gpmc settings structure
1244 *
1245 * Reads the GPMC settings for a GPMC child device from device-tree and
1246 * stores them in the GPMC settings structure passed. The GPMC settings
1247 * structure is initialised to zero by this function and so any
1248 * previously stored settings will be cleared.
1249 */
1250void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1251{
1252 memset(p, 0, sizeof(struct gpmc_settings));
1253
1254 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1255 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001256 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1257 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1258
1259 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1260 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1261 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1262 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1263 if (!p->burst_read && !p->burst_write)
1264 pr_warn("%s: page/burst-length set but not used!\n",
1265 __func__);
1266 }
1267
1268 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1269 p->wait_on_read = of_property_read_bool(np,
1270 "gpmc,wait-on-read");
1271 p->wait_on_write = of_property_read_bool(np,
1272 "gpmc,wait-on-write");
1273 if (!p->wait_on_read && !p->wait_on_write)
1274 pr_warn("%s: read/write wait monitoring not enabled!\n",
1275 __func__);
1276 }
1277}
1278
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001279static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1280 struct gpmc_timings *gpmc_t)
1281{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001282 struct gpmc_bool_timings *p;
1283
1284 if (!np || !gpmc_t)
1285 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001286
1287 memset(gpmc_t, 0, sizeof(*gpmc_t));
1288
1289 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001290 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001291
1292 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001293 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1294 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1295 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001296
1297 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001298 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1299 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1300 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001301
1302 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001303 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1304 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001305
1306 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001307 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1308 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001309
1310 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001311 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1312 &gpmc_t->page_burst_access);
1313 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1314 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1315 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1316 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1317 &gpmc_t->bus_turnaround);
1318 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1319 &gpmc_t->cycle2cycle_delay);
1320 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1321 &gpmc_t->wait_monitoring);
1322 of_property_read_u32(np, "gpmc,clk-activation-ns",
1323 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001324
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001325 /* only applicable to OMAP3+ */
1326 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1327 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1328 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001329
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001330 /* bool timing parameters */
1331 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001332
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001333 p->cycle2cyclediffcsen =
1334 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1335 p->cycle2cyclesamecsen =
1336 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1337 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1338 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1339 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1340 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1341 p->time_para_granularity =
1342 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001343}
1344
Pekon Gupta6b187b22014-01-28 11:42:40 +05301345#if IS_ENABLED(CONFIG_MTD_NAND)
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001346
Mark Jackson496c8a02013-04-19 21:08:28 +01001347static const char * const nand_xfer_types[] = {
1348 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1349 [NAND_OMAP_POLLED] = "polled",
1350 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1351 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1352};
1353
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001354static int gpmc_probe_nand_child(struct platform_device *pdev,
1355 struct device_node *child)
1356{
1357 u32 val;
1358 const char *s;
1359 struct gpmc_timings gpmc_t;
1360 struct omap_nand_platform_data *gpmc_nand_data;
1361
1362 if (of_property_read_u32(child, "reg", &val) < 0) {
1363 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1364 child->full_name);
1365 return -ENODEV;
1366 }
1367
1368 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1369 GFP_KERNEL);
1370 if (!gpmc_nand_data)
1371 return -ENOMEM;
1372
1373 gpmc_nand_data->cs = val;
1374 gpmc_nand_data->of_node = child;
1375
Pekon Guptaac65caf2013-10-24 18:20:17 +05301376 /* Detect availability of ELM module */
1377 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1378 if (gpmc_nand_data->elm_of_node == NULL)
1379 gpmc_nand_data->elm_of_node =
1380 of_parse_phandle(child, "elm_id", 0);
1381 if (gpmc_nand_data->elm_of_node == NULL)
1382 pr_warn("%s: ti,elm-id property not found\n", __func__);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001383
Pekon Guptaac65caf2013-10-24 18:20:17 +05301384 /* select ecc-scheme for NAND */
1385 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1386 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1387 return -ENODEV;
1388 }
1389 if (!strcmp(s, "ham1") || !strcmp(s, "sw") ||
1390 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
1391 gpmc_nand_data->ecc_opt =
1392 OMAP_ECC_HAM1_CODE_HW;
1393 else if (!strcmp(s, "bch4"))
1394 if (gpmc_nand_data->elm_of_node)
1395 gpmc_nand_data->ecc_opt =
1396 OMAP_ECC_BCH4_CODE_HW;
1397 else
1398 gpmc_nand_data->ecc_opt =
1399 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1400 else if (!strcmp(s, "bch8"))
1401 if (gpmc_nand_data->elm_of_node)
1402 gpmc_nand_data->ecc_opt =
1403 OMAP_ECC_BCH8_CODE_HW;
1404 else
1405 gpmc_nand_data->ecc_opt =
1406 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
1407 else
1408 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1409
1410 /* select data transfer mode for NAND controller */
Mark Jackson496c8a02013-04-19 21:08:28 +01001411 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1412 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1413 if (!strcasecmp(s, nand_xfer_types[val])) {
1414 gpmc_nand_data->xfer_type = val;
1415 break;
1416 }
1417
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001418 val = of_get_nand_bus_width(child);
1419 if (val == 16)
1420 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1421
1422 gpmc_read_timings_dt(child, &gpmc_t);
1423 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1424
1425 return 0;
1426}
1427#else
1428static int gpmc_probe_nand_child(struct platform_device *pdev,
1429 struct device_node *child)
1430{
1431 return 0;
1432}
1433#endif
1434
Pekon Gupta980386d2014-01-28 11:42:41 +05301435#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001436static int gpmc_probe_onenand_child(struct platform_device *pdev,
1437 struct device_node *child)
1438{
1439 u32 val;
1440 struct omap_onenand_platform_data *gpmc_onenand_data;
1441
1442 if (of_property_read_u32(child, "reg", &val) < 0) {
1443 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1444 child->full_name);
1445 return -ENODEV;
1446 }
1447
1448 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1449 GFP_KERNEL);
1450 if (!gpmc_onenand_data)
1451 return -ENOMEM;
1452
1453 gpmc_onenand_data->cs = val;
1454 gpmc_onenand_data->of_node = child;
1455 gpmc_onenand_data->dma_channel = -1;
1456
1457 if (!of_property_read_u32(child, "dma-channel", &val))
1458 gpmc_onenand_data->dma_channel = val;
1459
1460 gpmc_onenand_init(gpmc_onenand_data);
1461
1462 return 0;
1463}
1464#else
1465static int gpmc_probe_onenand_child(struct platform_device *pdev,
1466 struct device_node *child)
1467{
1468 return 0;
1469}
1470#endif
1471
Jon Huntercdd69282013-02-08 16:46:13 -06001472/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001473 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001474 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001475 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001476 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001477 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001478 * Returns 0 on success and appropriate negative error code on failure.
1479 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001480static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001481 struct device_node *child)
1482{
1483 struct gpmc_settings gpmc_s;
1484 struct gpmc_timings gpmc_t;
1485 struct resource res;
1486 unsigned long base;
1487 int ret, cs;
1488
1489 if (of_property_read_u32(child, "reg", &cs) < 0) {
1490 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1491 child->full_name);
1492 return -ENODEV;
1493 }
1494
1495 if (of_address_to_resource(child, 0, &res) < 0) {
1496 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1497 child->full_name);
1498 return -ENODEV;
1499 }
1500
1501 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1502 if (ret < 0) {
1503 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1504 return ret;
1505 }
1506
1507 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001508 * For some GPMC devices we still need to rely on the bootloader
1509 * timings because the devices can be connected via FPGA. So far
1510 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1511 * REVISIT: Add timing support from slls644g.pdf and from the
1512 * lan91c96 manual.
1513 */
1514 if (of_device_is_compatible(child, "ns16550a") ||
1515 of_device_is_compatible(child, "smsc,lan91c94") ||
1516 of_device_is_compatible(child, "smsc,lan91c111")) {
1517 dev_warn(&pdev->dev,
1518 "%s using bootloader timings on CS%d\n",
1519 child->name, cs);
1520 goto no_timings;
1521 }
1522
1523 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001524 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1525 * location in the gpmc address space. When booting with
1526 * device-tree we want the NOR flash to be mapped to the
1527 * location specified in the device-tree blob. So remap the
1528 * CS to this location. Once DT migration is complete should
1529 * just make gpmc_cs_request() map a specific address.
1530 */
1531 ret = gpmc_cs_remap(cs, res.start);
1532 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001533 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1534 cs, &res.start);
Jon Huntercdd69282013-02-08 16:46:13 -06001535 goto err;
1536 }
1537
1538 gpmc_read_settings_dt(child, &gpmc_s);
1539
1540 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1541 if (ret < 0)
1542 goto err;
1543
1544 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1545 if (ret < 0)
1546 goto err;
1547
1548 gpmc_read_timings_dt(child, &gpmc_t);
1549 gpmc_cs_set_timings(cs, &gpmc_t);
1550
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001551no_timings:
Jon Huntercdd69282013-02-08 16:46:13 -06001552 if (of_platform_device_create(child, NULL, &pdev->dev))
1553 return 0;
1554
1555 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01001556 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06001557
1558err:
1559 gpmc_cs_free(cs);
1560
1561 return ret;
1562}
1563
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001564static int gpmc_probe_dt(struct platform_device *pdev)
1565{
1566 int ret;
1567 struct device_node *child;
1568 const struct of_device_id *of_id =
1569 of_match_device(gpmc_dt_ids, &pdev->dev);
1570
1571 if (!of_id)
1572 return 0;
1573
Gupta Pekonf34f3712013-05-31 17:31:30 +05301574 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1575 &gpmc_cs_num);
1576 if (ret < 0) {
1577 pr_err("%s: number of chip-selects not defined\n", __func__);
1578 return ret;
1579 } else if (gpmc_cs_num < 1) {
1580 pr_err("%s: all chip-selects are disabled\n", __func__);
1581 return -EINVAL;
1582 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1583 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1584 __func__, GPMC_CS_NUM);
1585 return -EINVAL;
1586 }
1587
Jon Hunter9f833152013-02-20 15:53:38 -06001588 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1589 &gpmc_nr_waitpins);
1590 if (ret < 0) {
1591 pr_err("%s: number of wait pins not found!\n", __func__);
1592 return ret;
1593 }
1594
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001595 for_each_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001596
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001597 if (!child->name)
1598 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06001599
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001600 if (of_node_cmp(child->name, "nand") == 0)
1601 ret = gpmc_probe_nand_child(pdev, child);
1602 else if (of_node_cmp(child->name, "onenand") == 0)
1603 ret = gpmc_probe_onenand_child(pdev, child);
1604 else if (of_node_cmp(child->name, "ethernet") == 0 ||
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001605 of_node_cmp(child->name, "nor") == 0 ||
1606 of_node_cmp(child->name, "uart") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001607 ret = gpmc_probe_generic_child(pdev, child);
Jon Huntercdd69282013-02-08 16:46:13 -06001608
Javier Martinez Canillasb327b362013-04-17 22:34:12 +02001609 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1610 __func__, child->full_name))
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001611 of_node_put(child);
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001612 }
1613
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001614 return 0;
1615}
1616#else
1617static int gpmc_probe_dt(struct platform_device *pdev)
1618{
1619 return 0;
1620}
1621#endif
1622
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001623static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001624{
Jon Hunter81190242012-10-17 09:41:25 -05001625 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001626 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001627 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001628
Afzal Mohammedda496872012-09-23 17:28:25 -06001629 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1630 if (res == NULL)
1631 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001632
Afzal Mohammedda496872012-09-23 17:28:25 -06001633 phys_base = res->start;
1634 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001635
Thierry Reding5857bd92013-01-21 11:08:55 +01001636 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1637 if (IS_ERR(gpmc_base))
1638 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001639
1640 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1641 if (res == NULL)
1642 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1643 else
1644 gpmc_irq = res->start;
1645
1646 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1647 if (IS_ERR(gpmc_l3_clk)) {
1648 dev_err(&pdev->dev, "error: clk_get\n");
1649 gpmc_irq = 0;
1650 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001651 }
1652
avinash philipb3f55252013-06-12 16:30:56 +05301653 pm_runtime_enable(&pdev->dev);
1654 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001655
Afzal Mohammedda496872012-09-23 17:28:25 -06001656 gpmc_dev = &pdev->dev;
1657
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001658 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001659
1660 /*
1661 * FIXME: Once device-tree migration is complete the below flags
1662 * should be populated based upon the device-tree compatible
1663 * string. For now just use the IP revision. OMAP3+ devices have
1664 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1665 * devices support the addr-addr-data multiplex protocol.
1666 *
1667 * GPMC IP revisions:
1668 * - OMAP24xx = 2.0
1669 * - OMAP3xxx = 5.0
1670 * - OMAP44xx/54xx/AM335x = 6.0
1671 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001672 if (GPMC_REVISION_MAJOR(l) > 0x4)
1673 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001674 if (GPMC_REVISION_MAJOR(l) > 0x5)
1675 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001676 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1677 GPMC_REVISION_MINOR(l));
1678
Jon Hunter84b00f02013-03-06 14:36:47 -06001679 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301680
Russell King71856842013-03-13 20:44:21 +00001681 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001682 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1683
Jon Hunter31d9adc2013-02-18 07:57:39 -06001684 /* Now the GPMC is initialised, unreserve the chip-selects */
1685 gpmc_cs_map = 0;
1686
Gupta Pekonf34f3712013-05-31 17:31:30 +05301687 if (!pdev->dev.of_node) {
1688 gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -06001689 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
Gupta Pekonf34f3712013-05-31 17:31:30 +05301690 }
Jon Hunter9f833152013-02-20 15:53:38 -06001691
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001692 rc = gpmc_probe_dt(pdev);
1693 if (rc < 0) {
avinash philipb3f55252013-06-12 16:30:56 +05301694 pm_runtime_put_sync(&pdev->dev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001695 clk_put(gpmc_l3_clk);
1696 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1697 return rc;
1698 }
1699
Afzal Mohammedda496872012-09-23 17:28:25 -06001700 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301701}
Afzal Mohammedda496872012-09-23 17:28:25 -06001702
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001703static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001704{
1705 gpmc_free_irq();
1706 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05301707 pm_runtime_put_sync(&pdev->dev);
1708 pm_runtime_disable(&pdev->dev);
Afzal Mohammedda496872012-09-23 17:28:25 -06001709 gpmc_dev = NULL;
1710 return 0;
1711}
1712
avinash philipb536dd42013-06-18 00:16:38 +05301713#ifdef CONFIG_PM_SLEEP
1714static int gpmc_suspend(struct device *dev)
1715{
1716 omap3_gpmc_save_context();
1717 pm_runtime_put_sync(dev);
1718 return 0;
1719}
1720
1721static int gpmc_resume(struct device *dev)
1722{
1723 pm_runtime_get_sync(dev);
1724 omap3_gpmc_restore_context();
1725 return 0;
1726}
1727#endif
1728
1729static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1730
Afzal Mohammedda496872012-09-23 17:28:25 -06001731static struct platform_driver gpmc_driver = {
1732 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001733 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001734 .driver = {
1735 .name = DEVICE_NAME,
1736 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001737 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05301738 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06001739 },
1740};
1741
1742static __init int gpmc_init(void)
1743{
1744 return platform_driver_register(&gpmc_driver);
1745}
1746
1747static __exit void gpmc_exit(void)
1748{
1749 platform_driver_unregister(&gpmc_driver);
1750
1751}
1752
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001753omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001754module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301755
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001756static int __init omap_gpmc_init(void)
1757{
1758 struct omap_hwmod *oh;
1759 struct platform_device *pdev;
1760 char *oh_name = "gpmc";
1761
Daniel Mack2f98ca82012-12-14 11:36:40 +01001762 /*
1763 * if the board boots up with a populated DT, do not
1764 * manually add the device from this initcall
1765 */
1766 if (of_have_populated_dt())
1767 return -ENODEV;
1768
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001769 oh = omap_hwmod_lookup(oh_name);
1770 if (!oh) {
1771 pr_err("Could not look up %s\n", oh_name);
1772 return -ENODEV;
1773 }
1774
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001775 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001776 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1777
Thomas Meyer12616742013-06-01 11:44:44 +02001778 return PTR_RET(pdev);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001779}
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001780omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001781
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301782static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1783{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001784 int i;
1785 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301786
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001787 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1788
1789 if (!regval)
1790 return IRQ_NONE;
1791
1792 for (i = 0; i < GPMC_NR_IRQ; i++)
1793 if (regval & gpmc_client_irq[i].bitmask)
1794 generic_handle_irq(gpmc_client_irq[i].irq);
1795
1796 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301797
1798 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001799}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301800
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301801static struct omap3_gpmc_regs gpmc_context;
1802
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001803void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301804{
1805 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001806
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301807 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1808 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1809 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1810 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1811 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1812 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1813 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301814 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301815 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1816 if (gpmc_context.cs_context[i].is_valid) {
1817 gpmc_context.cs_context[i].config1 =
1818 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1819 gpmc_context.cs_context[i].config2 =
1820 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1821 gpmc_context.cs_context[i].config3 =
1822 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1823 gpmc_context.cs_context[i].config4 =
1824 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1825 gpmc_context.cs_context[i].config5 =
1826 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1827 gpmc_context.cs_context[i].config6 =
1828 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1829 gpmc_context.cs_context[i].config7 =
1830 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1831 }
1832 }
1833}
1834
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001835void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301836{
1837 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001838
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301839 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1840 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1841 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1842 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1843 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1844 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1845 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301846 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301847 if (gpmc_context.cs_context[i].is_valid) {
1848 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1849 gpmc_context.cs_context[i].config1);
1850 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1851 gpmc_context.cs_context[i].config2);
1852 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1853 gpmc_context.cs_context[i].config3);
1854 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1855 gpmc_context.cs_context[i].config4);
1856 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1857 gpmc_context.cs_context[i].config5);
1858 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1859 gpmc_context.cs_context[i].config6);
1860 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1861 gpmc_context.cs_context[i].config7);
1862 }
1863 }
1864}