blob: 45f680f965ed9541b55d728557f96d7d1446b03d [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{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300176 writel_relaxed(val, gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700177}
178
179static u32 gpmc_read_reg(int idx)
180{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300181 return readl_relaxed(gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700182}
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;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300189 writel_relaxed(val, reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700190}
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;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300197 return readl_relaxed(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 -0700286static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700287 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700288{
289 u32 l;
290 int ticks, mask, nr_bits;
291
292 if (time == 0)
293 ticks = 0;
294 else
295 ticks = gpmc_ns_to_ticks(time);
296 nr_bits = end_bit - st_bit + 1;
Roger Quadros80323742014-08-29 19:11:50 +0300297 mask = (1 << nr_bits) - 1;
298
299 if (ticks > mask) {
300 pr_err("%s: GPMC error! CS%d: %s: %d ns, %d ticks > %d\n",
301 __func__, cs, name, time, ticks, mask);
302
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700303 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800304 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700305
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700306 l = gpmc_cs_read_reg(cs, reg);
307#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800308 printk(KERN_INFO
309 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700310 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800311 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700312#endif
313 l &= ~(mask << st_bit);
314 l |= ticks << st_bit;
315 gpmc_cs_write_reg(cs, reg, l);
316
317 return 0;
318}
319
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700320#define GPMC_SET_ONE(reg, st, end, field) \
321 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
322 t->field, #field) < 0) \
323 return -1
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700324
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530325int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700326{
327 int div;
328 u32 l;
329
Adrian Huntera3551f52010-12-09 10:48:27 +0200330 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700331 div = l / gpmc_get_fclk_period();
332 if (div > 4)
333 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800334 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700335 div = 1;
336
337 return div;
338}
339
340int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
341{
342 int div;
343 u32 l;
344
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530345 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700346 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600347 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700348
349 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
350 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
351 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
352
353 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
354 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
355 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
356
357 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
358 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
359 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
360 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
361
362 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
363 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
364 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
365
366 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
367
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530368 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
369 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
370
371 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
372 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
373
Afzal Mohammedda496872012-09-23 17:28:25 -0600374 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300375 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600376 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300377 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300378
David Brownell1c22cc12006-12-06 17:13:55 -0800379 /* caller is expected to have initialized CONFIG1 to cover
380 * at least sync vs async
381 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700382 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800383 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
384#ifdef DEBUG
385 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
386 cs, (div * gpmc_get_fclk_period()) / 1000, div);
387#endif
388 l &= ~0x03;
389 l |= (div - 1);
390 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
391 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700392
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530393 gpmc_cs_bool_timings(cs, &t->bool_timings);
394
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700395 return 0;
396}
397
Jon Hunterc71f8e92013-03-06 12:00:10 -0600398static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700399{
Imre Deakf37e4582006-09-25 12:41:33 +0300400 u32 l;
401 u32 mask;
402
Jon Hunterc71f8e92013-03-06 12:00:10 -0600403 /*
404 * Ensure that base address is aligned on a
405 * boundary equal to or greater than size.
406 */
407 if (base & (size - 1))
408 return -EINVAL;
409
Imre Deakf37e4582006-09-25 12:41:33 +0300410 mask = (1 << GPMC_SECTION_SHIFT) - size;
411 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
412 l &= ~0x3f;
413 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
414 l &= ~(0x0f << 8);
415 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530416 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300417 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600418
419 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300420}
421
422static void gpmc_cs_disable_mem(int cs)
423{
424 u32 l;
425
426 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
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);
429}
430
431static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
432{
433 u32 l;
434 u32 mask;
435
436 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
437 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
438 mask = (l >> 8) & 0x0f;
439 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
440}
441
442static int gpmc_cs_mem_enabled(int cs)
443{
444 u32 l;
445
446 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530447 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300448}
449
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300450static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300451{
452 gpmc_cs_map &= ~(1 << cs);
453 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
454}
455
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300456static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300457{
458 return gpmc_cs_map & (1 << cs);
459}
460
461static unsigned long gpmc_mem_align(unsigned long size)
462{
463 int order;
464
465 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
466 order = GPMC_CHUNK_SHIFT - 1;
467 do {
468 size >>= 1;
469 order++;
470 } while (size);
471 size = 1 << order;
472 return size;
473}
474
475static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
476{
477 struct resource *res = &gpmc_cs_mem[cs];
478 int r;
479
480 size = gpmc_mem_align(size);
481 spin_lock(&gpmc_mem_lock);
482 res->start = base;
483 res->end = base + size - 1;
484 r = request_resource(&gpmc_mem_root, res);
485 spin_unlock(&gpmc_mem_lock);
486
487 return r;
488}
489
Afzal Mohammedda496872012-09-23 17:28:25 -0600490static int gpmc_cs_delete_mem(int cs)
491{
492 struct resource *res = &gpmc_cs_mem[cs];
493 int r;
494
495 spin_lock(&gpmc_mem_lock);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700496 r = release_resource(res);
Afzal Mohammedda496872012-09-23 17:28:25 -0600497 res->start = 0;
498 res->end = 0;
499 spin_unlock(&gpmc_mem_lock);
500
501 return r;
502}
503
Jon Huntercdd69282013-02-08 16:46:13 -0600504/**
505 * gpmc_cs_remap - remaps a chip-select physical base address
506 * @cs: chip-select to remap
507 * @base: physical base address to re-map chip-select to
508 *
509 * Re-maps a chip-select to a new physical base address specified by
510 * "base". Returns 0 on success and appropriate negative error code
511 * on failure.
512 */
513static int gpmc_cs_remap(int cs, u32 base)
514{
515 int ret;
516 u32 old_base, size;
517
Gupta Pekonf34f3712013-05-31 17:31:30 +0530518 if (cs > gpmc_cs_num) {
519 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600520 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530521 }
Tony Lindgrenfb677ef2014-04-21 19:26:13 -0700522
523 /*
524 * Make sure we ignore any device offsets from the GPMC partition
525 * allocated for the chip select and that the new base confirms
526 * to the GPMC 16MB minimum granularity.
527 */
528 base &= ~(SZ_16M - 1);
529
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;
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
Roger Quadros2b540572014-09-02 16:57:06 +03001199 if (p->wait_pin > gpmc_nr_waitpins) {
Jon Hunteraa8d4762013-02-21 15:25:23 -06001200 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1201 return -EINVAL;
1202 }
1203
1204 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1205
1206 if (p->sync_read)
1207 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1208 if (p->sync_write)
1209 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1210 if (p->wait_on_read)
1211 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1212 if (p->wait_on_write)
1213 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1214 if (p->wait_on_read || p->wait_on_write)
1215 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1216 if (p->device_nand)
1217 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1218 if (p->mux_add_data)
1219 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1220 if (p->burst_read)
1221 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1222 if (p->burst_write)
1223 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1224 if (p->burst_read || p->burst_write) {
1225 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1226 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1227 }
1228
1229 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1230
1231 return 0;
1232}
1233
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001234#ifdef CONFIG_OF
Uwe Kleine-König31957602014-09-10 10:26:17 +02001235static const struct of_device_id gpmc_dt_ids[] = {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001236 { .compatible = "ti,omap2420-gpmc" },
1237 { .compatible = "ti,omap2430-gpmc" },
1238 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1239 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1240 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1241 { }
1242};
1243MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1244
Jon Hunter8c8a77712013-02-20 15:53:12 -06001245/**
1246 * gpmc_read_settings_dt - read gpmc settings from device-tree
1247 * @np: pointer to device-tree node for a gpmc child device
1248 * @p: pointer to gpmc settings structure
1249 *
1250 * Reads the GPMC settings for a GPMC child device from device-tree and
1251 * stores them in the GPMC settings structure passed. The GPMC settings
1252 * structure is initialised to zero by this function and so any
1253 * previously stored settings will be cleared.
1254 */
1255void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1256{
1257 memset(p, 0, sizeof(struct gpmc_settings));
1258
1259 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1260 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001261 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1262 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1263
1264 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1265 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1266 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1267 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1268 if (!p->burst_read && !p->burst_write)
1269 pr_warn("%s: page/burst-length set but not used!\n",
1270 __func__);
1271 }
1272
1273 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1274 p->wait_on_read = of_property_read_bool(np,
1275 "gpmc,wait-on-read");
1276 p->wait_on_write = of_property_read_bool(np,
1277 "gpmc,wait-on-write");
1278 if (!p->wait_on_read && !p->wait_on_write)
Roger Quadros2b540572014-09-02 16:57:06 +03001279 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1280 __func__);
Jon Hunter8c8a77712013-02-20 15:53:12 -06001281 }
1282}
1283
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001284static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1285 struct gpmc_timings *gpmc_t)
1286{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001287 struct gpmc_bool_timings *p;
1288
1289 if (!np || !gpmc_t)
1290 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001291
1292 memset(gpmc_t, 0, sizeof(*gpmc_t));
1293
1294 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001295 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001296
1297 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001298 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1299 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1300 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001301
1302 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001303 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1304 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1305 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001306
1307 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001308 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1309 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001310
1311 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001312 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1313 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001314
1315 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001316 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1317 &gpmc_t->page_burst_access);
1318 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1319 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1320 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1321 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1322 &gpmc_t->bus_turnaround);
1323 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1324 &gpmc_t->cycle2cycle_delay);
1325 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1326 &gpmc_t->wait_monitoring);
1327 of_property_read_u32(np, "gpmc,clk-activation-ns",
1328 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001329
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001330 /* only applicable to OMAP3+ */
1331 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1332 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1333 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001334
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001335 /* bool timing parameters */
1336 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001337
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001338 p->cycle2cyclediffcsen =
1339 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1340 p->cycle2cyclesamecsen =
1341 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1342 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1343 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1344 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1345 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1346 p->time_para_granularity =
1347 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001348}
1349
Pekon Gupta6b187b22014-01-28 11:42:40 +05301350#if IS_ENABLED(CONFIG_MTD_NAND)
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001351
Mark Jackson496c8a02013-04-19 21:08:28 +01001352static const char * const nand_xfer_types[] = {
1353 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1354 [NAND_OMAP_POLLED] = "polled",
1355 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1356 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1357};
1358
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001359static int gpmc_probe_nand_child(struct platform_device *pdev,
1360 struct device_node *child)
1361{
1362 u32 val;
1363 const char *s;
1364 struct gpmc_timings gpmc_t;
1365 struct omap_nand_platform_data *gpmc_nand_data;
1366
1367 if (of_property_read_u32(child, "reg", &val) < 0) {
1368 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1369 child->full_name);
1370 return -ENODEV;
1371 }
1372
1373 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1374 GFP_KERNEL);
1375 if (!gpmc_nand_data)
1376 return -ENOMEM;
1377
1378 gpmc_nand_data->cs = val;
1379 gpmc_nand_data->of_node = child;
1380
Pekon Guptaac65caf52013-10-24 18:20:17 +05301381 /* Detect availability of ELM module */
1382 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1383 if (gpmc_nand_data->elm_of_node == NULL)
1384 gpmc_nand_data->elm_of_node =
1385 of_parse_phandle(child, "elm_id", 0);
1386 if (gpmc_nand_data->elm_of_node == NULL)
1387 pr_warn("%s: ti,elm-id property not found\n", __func__);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001388
Pekon Guptaac65caf52013-10-24 18:20:17 +05301389 /* select ecc-scheme for NAND */
1390 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1391 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1392 return -ENODEV;
1393 }
Roger Quadrosa3e83f02014-08-25 16:15:33 -07001394
1395 if (!strcmp(s, "sw"))
1396 gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1397 else if (!strcmp(s, "ham1") ||
1398 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
Pekon Guptaac65caf52013-10-24 18:20:17 +05301399 gpmc_nand_data->ecc_opt =
1400 OMAP_ECC_HAM1_CODE_HW;
1401 else if (!strcmp(s, "bch4"))
1402 if (gpmc_nand_data->elm_of_node)
1403 gpmc_nand_data->ecc_opt =
1404 OMAP_ECC_BCH4_CODE_HW;
1405 else
1406 gpmc_nand_data->ecc_opt =
1407 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1408 else if (!strcmp(s, "bch8"))
1409 if (gpmc_nand_data->elm_of_node)
1410 gpmc_nand_data->ecc_opt =
1411 OMAP_ECC_BCH8_CODE_HW;
1412 else
1413 gpmc_nand_data->ecc_opt =
1414 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
pekon gupta27c9fd62014-05-19 13:24:39 +05301415 else if (!strcmp(s, "bch16"))
1416 if (gpmc_nand_data->elm_of_node)
1417 gpmc_nand_data->ecc_opt =
1418 OMAP_ECC_BCH16_CODE_HW;
1419 else
1420 pr_err("%s: BCH16 requires ELM support\n", __func__);
Pekon Guptaac65caf52013-10-24 18:20:17 +05301421 else
1422 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1423
1424 /* select data transfer mode for NAND controller */
Mark Jackson496c8a02013-04-19 21:08:28 +01001425 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1426 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1427 if (!strcasecmp(s, nand_xfer_types[val])) {
1428 gpmc_nand_data->xfer_type = val;
1429 break;
1430 }
1431
Ezequiel Garcíafef775c2014-09-11 12:02:08 -03001432 gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
1433
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001434 val = of_get_nand_bus_width(child);
1435 if (val == 16)
1436 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1437
1438 gpmc_read_timings_dt(child, &gpmc_t);
1439 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1440
1441 return 0;
1442}
1443#else
1444static int gpmc_probe_nand_child(struct platform_device *pdev,
1445 struct device_node *child)
1446{
1447 return 0;
1448}
1449#endif
1450
Pekon Gupta980386d2014-01-28 11:42:41 +05301451#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001452static int gpmc_probe_onenand_child(struct platform_device *pdev,
1453 struct device_node *child)
1454{
1455 u32 val;
1456 struct omap_onenand_platform_data *gpmc_onenand_data;
1457
1458 if (of_property_read_u32(child, "reg", &val) < 0) {
1459 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1460 child->full_name);
1461 return -ENODEV;
1462 }
1463
1464 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1465 GFP_KERNEL);
1466 if (!gpmc_onenand_data)
1467 return -ENOMEM;
1468
1469 gpmc_onenand_data->cs = val;
1470 gpmc_onenand_data->of_node = child;
1471 gpmc_onenand_data->dma_channel = -1;
1472
1473 if (!of_property_read_u32(child, "dma-channel", &val))
1474 gpmc_onenand_data->dma_channel = val;
1475
1476 gpmc_onenand_init(gpmc_onenand_data);
1477
1478 return 0;
1479}
1480#else
1481static int gpmc_probe_onenand_child(struct platform_device *pdev,
1482 struct device_node *child)
1483{
1484 return 0;
1485}
1486#endif
1487
Jon Huntercdd69282013-02-08 16:46:13 -06001488/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001489 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001490 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001491 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001492 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001493 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001494 * Returns 0 on success and appropriate negative error code on failure.
1495 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001496static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001497 struct device_node *child)
1498{
1499 struct gpmc_settings gpmc_s;
1500 struct gpmc_timings gpmc_t;
1501 struct resource res;
1502 unsigned long base;
1503 int ret, cs;
1504
1505 if (of_property_read_u32(child, "reg", &cs) < 0) {
1506 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1507 child->full_name);
1508 return -ENODEV;
1509 }
1510
1511 if (of_address_to_resource(child, 0, &res) < 0) {
1512 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1513 child->full_name);
1514 return -ENODEV;
1515 }
1516
1517 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1518 if (ret < 0) {
1519 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1520 return ret;
1521 }
1522
1523 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001524 * For some GPMC devices we still need to rely on the bootloader
1525 * timings because the devices can be connected via FPGA. So far
1526 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1527 * REVISIT: Add timing support from slls644g.pdf and from the
1528 * lan91c96 manual.
1529 */
1530 if (of_device_is_compatible(child, "ns16550a") ||
1531 of_device_is_compatible(child, "smsc,lan91c94") ||
1532 of_device_is_compatible(child, "smsc,lan91c111")) {
1533 dev_warn(&pdev->dev,
1534 "%s using bootloader timings on CS%d\n",
1535 child->name, cs);
1536 goto no_timings;
1537 }
1538
1539 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001540 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1541 * location in the gpmc address space. When booting with
1542 * device-tree we want the NOR flash to be mapped to the
1543 * location specified in the device-tree blob. So remap the
1544 * CS to this location. Once DT migration is complete should
1545 * just make gpmc_cs_request() map a specific address.
1546 */
1547 ret = gpmc_cs_remap(cs, res.start);
1548 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001549 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1550 cs, &res.start);
Jon Huntercdd69282013-02-08 16:46:13 -06001551 goto err;
1552 }
1553
1554 gpmc_read_settings_dt(child, &gpmc_s);
1555
1556 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1557 if (ret < 0)
1558 goto err;
1559
1560 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1561 if (ret < 0)
1562 goto err;
1563
1564 gpmc_read_timings_dt(child, &gpmc_t);
1565 gpmc_cs_set_timings(cs, &gpmc_t);
1566
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001567no_timings:
Jon Huntercdd69282013-02-08 16:46:13 -06001568 if (of_platform_device_create(child, NULL, &pdev->dev))
1569 return 0;
1570
1571 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01001572 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06001573
1574err:
1575 gpmc_cs_free(cs);
1576
1577 return ret;
1578}
1579
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001580static int gpmc_probe_dt(struct platform_device *pdev)
1581{
1582 int ret;
1583 struct device_node *child;
1584 const struct of_device_id *of_id =
1585 of_match_device(gpmc_dt_ids, &pdev->dev);
1586
1587 if (!of_id)
1588 return 0;
1589
Gupta Pekonf34f3712013-05-31 17:31:30 +05301590 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1591 &gpmc_cs_num);
1592 if (ret < 0) {
1593 pr_err("%s: number of chip-selects not defined\n", __func__);
1594 return ret;
1595 } else if (gpmc_cs_num < 1) {
1596 pr_err("%s: all chip-selects are disabled\n", __func__);
1597 return -EINVAL;
1598 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1599 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1600 __func__, GPMC_CS_NUM);
1601 return -EINVAL;
1602 }
1603
Jon Hunter9f833152013-02-20 15:53:38 -06001604 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1605 &gpmc_nr_waitpins);
1606 if (ret < 0) {
1607 pr_err("%s: number of wait pins not found!\n", __func__);
1608 return ret;
1609 }
1610
Guido Martínez68e2eb52014-07-02 10:35:18 -03001611 for_each_available_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001612
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001613 if (!child->name)
1614 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06001615
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001616 if (of_node_cmp(child->name, "nand") == 0)
1617 ret = gpmc_probe_nand_child(pdev, child);
1618 else if (of_node_cmp(child->name, "onenand") == 0)
1619 ret = gpmc_probe_onenand_child(pdev, child);
1620 else if (of_node_cmp(child->name, "ethernet") == 0 ||
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001621 of_node_cmp(child->name, "nor") == 0 ||
1622 of_node_cmp(child->name, "uart") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001623 ret = gpmc_probe_generic_child(pdev, child);
Jon Huntercdd69282013-02-08 16:46:13 -06001624
Javier Martinez Canillasb327b362013-04-17 22:34:12 +02001625 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1626 __func__, child->full_name))
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001627 of_node_put(child);
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001628 }
1629
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001630 return 0;
1631}
1632#else
1633static int gpmc_probe_dt(struct platform_device *pdev)
1634{
1635 return 0;
1636}
1637#endif
1638
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001639static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001640{
Jon Hunter81190242012-10-17 09:41:25 -05001641 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001642 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001643 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001644
Afzal Mohammedda496872012-09-23 17:28:25 -06001645 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1646 if (res == NULL)
1647 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001648
Afzal Mohammedda496872012-09-23 17:28:25 -06001649 phys_base = res->start;
1650 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001651
Thierry Reding5857bd92013-01-21 11:08:55 +01001652 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1653 if (IS_ERR(gpmc_base))
1654 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001655
1656 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1657 if (res == NULL)
1658 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1659 else
1660 gpmc_irq = res->start;
1661
1662 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1663 if (IS_ERR(gpmc_l3_clk)) {
1664 dev_err(&pdev->dev, "error: clk_get\n");
1665 gpmc_irq = 0;
1666 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001667 }
1668
avinash philipb3f55252013-06-12 16:30:56 +05301669 pm_runtime_enable(&pdev->dev);
1670 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001671
Afzal Mohammedda496872012-09-23 17:28:25 -06001672 gpmc_dev = &pdev->dev;
1673
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001674 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001675
1676 /*
1677 * FIXME: Once device-tree migration is complete the below flags
1678 * should be populated based upon the device-tree compatible
1679 * string. For now just use the IP revision. OMAP3+ devices have
1680 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1681 * devices support the addr-addr-data multiplex protocol.
1682 *
1683 * GPMC IP revisions:
1684 * - OMAP24xx = 2.0
1685 * - OMAP3xxx = 5.0
1686 * - OMAP44xx/54xx/AM335x = 6.0
1687 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001688 if (GPMC_REVISION_MAJOR(l) > 0x4)
1689 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001690 if (GPMC_REVISION_MAJOR(l) > 0x5)
1691 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001692 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1693 GPMC_REVISION_MINOR(l));
1694
Jon Hunter84b00f02013-03-06 14:36:47 -06001695 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301696
Russell King71856842013-03-13 20:44:21 +00001697 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001698 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1699
Jon Hunter31d9adc2013-02-18 07:57:39 -06001700 /* Now the GPMC is initialised, unreserve the chip-selects */
1701 gpmc_cs_map = 0;
1702
Gupta Pekonf34f3712013-05-31 17:31:30 +05301703 if (!pdev->dev.of_node) {
1704 gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -06001705 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
Gupta Pekonf34f3712013-05-31 17:31:30 +05301706 }
Jon Hunter9f833152013-02-20 15:53:38 -06001707
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001708 rc = gpmc_probe_dt(pdev);
1709 if (rc < 0) {
avinash philipb3f55252013-06-12 16:30:56 +05301710 pm_runtime_put_sync(&pdev->dev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001711 clk_put(gpmc_l3_clk);
1712 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1713 return rc;
1714 }
1715
Afzal Mohammedda496872012-09-23 17:28:25 -06001716 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301717}
Afzal Mohammedda496872012-09-23 17:28:25 -06001718
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001719static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001720{
1721 gpmc_free_irq();
1722 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05301723 pm_runtime_put_sync(&pdev->dev);
1724 pm_runtime_disable(&pdev->dev);
Afzal Mohammedda496872012-09-23 17:28:25 -06001725 gpmc_dev = NULL;
1726 return 0;
1727}
1728
avinash philipb536dd42013-06-18 00:16:38 +05301729#ifdef CONFIG_PM_SLEEP
1730static int gpmc_suspend(struct device *dev)
1731{
1732 omap3_gpmc_save_context();
1733 pm_runtime_put_sync(dev);
1734 return 0;
1735}
1736
1737static int gpmc_resume(struct device *dev)
1738{
1739 pm_runtime_get_sync(dev);
1740 omap3_gpmc_restore_context();
1741 return 0;
1742}
1743#endif
1744
1745static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1746
Afzal Mohammedda496872012-09-23 17:28:25 -06001747static struct platform_driver gpmc_driver = {
1748 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001749 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001750 .driver = {
1751 .name = DEVICE_NAME,
1752 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001753 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05301754 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06001755 },
1756};
1757
1758static __init int gpmc_init(void)
1759{
1760 return platform_driver_register(&gpmc_driver);
1761}
1762
1763static __exit void gpmc_exit(void)
1764{
1765 platform_driver_unregister(&gpmc_driver);
1766
1767}
1768
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001769omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001770module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301771
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001772static int __init omap_gpmc_init(void)
1773{
1774 struct omap_hwmod *oh;
1775 struct platform_device *pdev;
1776 char *oh_name = "gpmc";
1777
Daniel Mack2f98ca82012-12-14 11:36:40 +01001778 /*
1779 * if the board boots up with a populated DT, do not
1780 * manually add the device from this initcall
1781 */
1782 if (of_have_populated_dt())
1783 return -ENODEV;
1784
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001785 oh = omap_hwmod_lookup(oh_name);
1786 if (!oh) {
1787 pr_err("Could not look up %s\n", oh_name);
1788 return -ENODEV;
1789 }
1790
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001791 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001792 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1793
Thomas Meyer12616742013-06-01 11:44:44 +02001794 return PTR_RET(pdev);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001795}
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001796omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001797
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301798static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1799{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001800 int i;
1801 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301802
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001803 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1804
1805 if (!regval)
1806 return IRQ_NONE;
1807
1808 for (i = 0; i < GPMC_NR_IRQ; i++)
1809 if (regval & gpmc_client_irq[i].bitmask)
1810 generic_handle_irq(gpmc_client_irq[i].irq);
1811
1812 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301813
1814 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001815}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301816
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301817static struct omap3_gpmc_regs gpmc_context;
1818
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001819void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301820{
1821 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001822
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301823 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1824 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1825 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1826 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1827 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1828 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1829 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301830 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301831 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1832 if (gpmc_context.cs_context[i].is_valid) {
1833 gpmc_context.cs_context[i].config1 =
1834 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1835 gpmc_context.cs_context[i].config2 =
1836 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1837 gpmc_context.cs_context[i].config3 =
1838 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1839 gpmc_context.cs_context[i].config4 =
1840 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1841 gpmc_context.cs_context[i].config5 =
1842 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1843 gpmc_context.cs_context[i].config6 =
1844 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1845 gpmc_context.cs_context[i].config7 =
1846 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1847 }
1848 }
1849}
1850
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001851void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301852{
1853 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001854
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301855 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1856 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1857 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1858 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1859 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1860 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1861 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301862 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301863 if (gpmc_context.cs_context[i].is_valid) {
1864 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1865 gpmc_context.cs_context[i].config1);
1866 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1867 gpmc_context.cs_context[i].config2);
1868 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1869 gpmc_context.cs_context[i].config3);
1870 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1871 gpmc_context.cs_context[i].config4);
1872 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1873 gpmc_context.cs_context[i].config5);
1874 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1875 gpmc_context.cs_context[i].config6);
1876 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1877 gpmc_context.cs_context[i].config7);
1878 }
1879 }
1880}