blob: 2c5f3485e44c7c8cc8b1456efc0eb77877030bb2 [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
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800121struct gpmc_cs_data {
122 const char *name;
123
124#define GPMC_CS_RESERVED (1 << 0)
125 u32 flags;
126
127 struct resource mem;
128};
129
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700130struct gpmc_client_irq {
131 unsigned irq;
132 u32 bitmask;
133};
134
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530135/* Structure to save gpmc cs context */
136struct gpmc_cs_config {
137 u32 config1;
138 u32 config2;
139 u32 config3;
140 u32 config4;
141 u32 config5;
142 u32 config6;
143 u32 config7;
144 int is_valid;
145};
146
147/*
148 * Structure to save/restore gpmc context
149 * to support core off on OMAP3
150 */
151struct omap3_gpmc_regs {
152 u32 sysconfig;
153 u32 irqenable;
154 u32 timeout_ctrl;
155 u32 config;
156 u32 prefetch_config1;
157 u32 prefetch_config2;
158 u32 prefetch_control;
159 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
160};
161
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700162static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
163static struct irq_chip gpmc_irq_chip;
Chen Gangaf072192013-08-22 15:47:21 +0800164static int gpmc_irq_start;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700165
Imre Deakf37e4582006-09-25 12:41:33 +0300166static struct resource gpmc_mem_root;
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800167static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700168static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600169/* Define chip-selects as reserved by default until probe completes */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530170static unsigned int gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -0600171static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600172static struct device *gpmc_dev;
173static int gpmc_irq;
174static resource_size_t phys_base, mem_size;
175static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300176static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700177
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300178static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700179
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530180static irqreturn_t gpmc_handle_irq(int irq, void *dev);
181
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700182static void gpmc_write_reg(int idx, u32 val)
183{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300184 writel_relaxed(val, gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700185}
186
187static u32 gpmc_read_reg(int idx)
188{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300189 return readl_relaxed(gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700190}
191
192void gpmc_cs_write_reg(int cs, int idx, u32 val)
193{
194 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 writel_relaxed(val, reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700198}
199
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300200static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700201{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300202 void __iomem *reg_addr;
203
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000204 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300205 return readl_relaxed(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700206}
207
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300208/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300209static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700210{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300211 unsigned long rate = clk_get_rate(gpmc_l3_clk);
212
213 if (rate == 0) {
214 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
215 return 0;
216 }
217
218 rate /= 1000;
219 rate = 1000000000 / rate; /* In picoseconds */
220
221 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700222}
223
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300224static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700225{
226 unsigned long tick_ps;
227
228 /* Calculate in picosecs to yield more exact results */
229 tick_ps = gpmc_get_fclk_period();
230
231 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
232}
233
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300234static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200235{
236 unsigned long tick_ps;
237
238 /* Calculate in picosecs to yield more exact results */
239 tick_ps = gpmc_get_fclk_period();
240
241 return (time_ps + tick_ps - 1) / tick_ps;
242}
243
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300244unsigned int gpmc_ticks_to_ns(unsigned int ticks)
245{
246 return ticks * gpmc_get_fclk_period() / 1000;
247}
248
Afzal Mohammed246da262012-08-02 20:02:10 +0530249static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
250{
251 return ticks * gpmc_get_fclk_period();
252}
253
254static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
255{
256 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
257
258 return ticks * gpmc_get_fclk_period();
259}
260
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530261static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
262{
263 u32 l;
264
265 l = gpmc_cs_read_reg(cs, reg);
266 if (value)
267 l |= mask;
268 else
269 l &= ~mask;
270 gpmc_cs_write_reg(cs, reg, l);
271}
272
273static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
274{
275 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
276 GPMC_CONFIG1_TIME_PARA_GRAN,
277 p->time_para_granularity);
278 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
279 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
280 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
281 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
282 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
283 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
284 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
285 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
286 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
287 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
288 p->cycle2cyclesamecsen);
289 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
290 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
291 p->cycle2cyclediffcsen);
292}
293
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700294#ifdef DEBUG
295static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700296 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700297#else
298static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
299 int time)
300#endif
301{
302 u32 l;
303 int ticks, mask, nr_bits;
304
305 if (time == 0)
306 ticks = 0;
307 else
308 ticks = gpmc_ns_to_ticks(time);
309 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800310 if (ticks >= 1 << nr_bits) {
311#ifdef DEBUG
312 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
313 cs, name, time, ticks, 1 << nr_bits);
314#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700315 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800316 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700317
318 mask = (1 << nr_bits) - 1;
319 l = gpmc_cs_read_reg(cs, reg);
320#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800321 printk(KERN_INFO
322 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700323 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800324 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700325#endif
326 l &= ~(mask << st_bit);
327 l |= ticks << st_bit;
328 gpmc_cs_write_reg(cs, reg, l);
329
330 return 0;
331}
332
333#ifdef DEBUG
334#define GPMC_SET_ONE(reg, st, end, field) \
335 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
336 t->field, #field) < 0) \
337 return -1
338#else
339#define GPMC_SET_ONE(reg, st, end, field) \
340 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
341 return -1
342#endif
343
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530344int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700345{
346 int div;
347 u32 l;
348
Adrian Huntera3551f52010-12-09 10:48:27 +0200349 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700350 div = l / gpmc_get_fclk_period();
351 if (div > 4)
352 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800353 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700354 div = 1;
355
356 return div;
357}
358
359int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
360{
361 int div;
362 u32 l;
363
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530364 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700365 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600366 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700367
368 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
369 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
370 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
371
372 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
373 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
374 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
375
376 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
377 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
378 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
379 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
380
381 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
382 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
383 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
384
385 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
386
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530387 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
388 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
389
390 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
391 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
392
Afzal Mohammedda496872012-09-23 17:28:25 -0600393 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300394 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600395 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300396 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300397
David Brownell1c22cc12006-12-06 17:13:55 -0800398 /* caller is expected to have initialized CONFIG1 to cover
399 * at least sync vs async
400 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700401 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800402 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
403#ifdef DEBUG
404 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
405 cs, (div * gpmc_get_fclk_period()) / 1000, div);
406#endif
407 l &= ~0x03;
408 l |= (div - 1);
409 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
410 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700411
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530412 gpmc_cs_bool_timings(cs, &t->bool_timings);
413
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700414 return 0;
415}
416
Jon Hunterc71f8e92013-03-06 12:00:10 -0600417static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700418{
Imre Deakf37e4582006-09-25 12:41:33 +0300419 u32 l;
420 u32 mask;
421
Jon Hunterc71f8e92013-03-06 12:00:10 -0600422 /*
423 * Ensure that base address is aligned on a
424 * boundary equal to or greater than size.
425 */
426 if (base & (size - 1))
427 return -EINVAL;
428
Imre Deakf37e4582006-09-25 12:41:33 +0300429 mask = (1 << GPMC_SECTION_SHIFT) - size;
430 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
431 l &= ~0x3f;
432 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
433 l &= ~(0x0f << 8);
434 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
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);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600437
438 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300439}
440
441static void gpmc_cs_disable_mem(int cs)
442{
443 u32 l;
444
445 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530446 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300447 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
448}
449
450static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
451{
452 u32 l;
453 u32 mask;
454
455 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
456 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
457 mask = (l >> 8) & 0x0f;
458 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
459}
460
461static int gpmc_cs_mem_enabled(int cs)
462{
463 u32 l;
464
465 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530466 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300467}
468
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300469static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300470{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800471 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
472
473 gpmc->flags |= GPMC_CS_RESERVED;
Imre Deakf37e4582006-09-25 12:41:33 +0300474}
475
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300476static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300477{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800478 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
479
480 return gpmc->flags & GPMC_CS_RESERVED;
481}
482
483static void gpmc_cs_set_name(int cs, const char *name)
484{
485 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
486
487 gpmc->name = name;
488}
489
490const char *gpmc_cs_get_name(int cs)
491{
492 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
493
494 return gpmc->name;
Imre Deakf37e4582006-09-25 12:41:33 +0300495}
496
497static unsigned long gpmc_mem_align(unsigned long size)
498{
499 int order;
500
501 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
502 order = GPMC_CHUNK_SHIFT - 1;
503 do {
504 size >>= 1;
505 order++;
506 } while (size);
507 size = 1 << order;
508 return size;
509}
510
511static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
512{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800513 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
514 struct resource *res = &gpmc->mem;
Imre Deakf37e4582006-09-25 12:41:33 +0300515 int r;
516
517 size = gpmc_mem_align(size);
518 spin_lock(&gpmc_mem_lock);
519 res->start = base;
520 res->end = base + size - 1;
521 r = request_resource(&gpmc_mem_root, res);
522 spin_unlock(&gpmc_mem_lock);
523
524 return r;
525}
526
Afzal Mohammedda496872012-09-23 17:28:25 -0600527static int gpmc_cs_delete_mem(int cs)
528{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800529 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
530 struct resource *res = &gpmc->mem;
Afzal Mohammedda496872012-09-23 17:28:25 -0600531 int r;
532
533 spin_lock(&gpmc_mem_lock);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700534 r = release_resource(res);
Afzal Mohammedda496872012-09-23 17:28:25 -0600535 res->start = 0;
536 res->end = 0;
537 spin_unlock(&gpmc_mem_lock);
538
539 return r;
540}
541
Jon Huntercdd69282013-02-08 16:46:13 -0600542/**
543 * gpmc_cs_remap - remaps a chip-select physical base address
544 * @cs: chip-select to remap
545 * @base: physical base address to re-map chip-select to
546 *
547 * Re-maps a chip-select to a new physical base address specified by
548 * "base". Returns 0 on success and appropriate negative error code
549 * on failure.
550 */
551static int gpmc_cs_remap(int cs, u32 base)
552{
553 int ret;
554 u32 old_base, size;
555
Gupta Pekonf34f3712013-05-31 17:31:30 +0530556 if (cs > gpmc_cs_num) {
557 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600558 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530559 }
Tony Lindgrenfb677ef2014-04-21 19:26:13 -0700560
561 /*
562 * Make sure we ignore any device offsets from the GPMC partition
563 * allocated for the chip select and that the new base confirms
564 * to the GPMC 16MB minimum granularity.
565 */
566 base &= ~(SZ_16M - 1);
567
Jon Huntercdd69282013-02-08 16:46:13 -0600568 gpmc_cs_get_memconf(cs, &old_base, &size);
569 if (base == old_base)
570 return 0;
571 gpmc_cs_disable_mem(cs);
572 ret = gpmc_cs_delete_mem(cs);
573 if (ret < 0)
574 return ret;
575 ret = gpmc_cs_insert_mem(cs, base, size);
576 if (ret < 0)
577 return ret;
Jon Hunterc71f8e92013-03-06 12:00:10 -0600578 ret = gpmc_cs_enable_mem(cs, base, size);
579 if (ret < 0)
580 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600581
582 return 0;
583}
584
Imre Deakf37e4582006-09-25 12:41:33 +0300585int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
586{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800587 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
588 struct resource *res = &gpmc->mem;
Imre Deakf37e4582006-09-25 12:41:33 +0300589 int r = -1;
590
Gupta Pekonf34f3712013-05-31 17:31:30 +0530591 if (cs > gpmc_cs_num) {
592 pr_err("%s: requested chip-select is disabled\n", __func__);
Imre Deakf37e4582006-09-25 12:41:33 +0300593 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530594 }
Imre Deakf37e4582006-09-25 12:41:33 +0300595 size = gpmc_mem_align(size);
596 if (size > (1 << GPMC_SECTION_SHIFT))
597 return -ENOMEM;
598
599 spin_lock(&gpmc_mem_lock);
600 if (gpmc_cs_reserved(cs)) {
601 r = -EBUSY;
602 goto out;
603 }
604 if (gpmc_cs_mem_enabled(cs))
605 r = adjust_resource(res, res->start & ~(size - 1), size);
606 if (r < 0)
607 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
608 size, NULL, NULL);
609 if (r < 0)
610 goto out;
611
Jon Hunterc71f8e92013-03-06 12:00:10 -0600612 r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
613 if (r < 0) {
614 release_resource(res);
615 goto out;
616 }
617
Imre Deakf37e4582006-09-25 12:41:33 +0300618 *base = res->start;
619 gpmc_cs_set_reserved(cs, 1);
620out:
621 spin_unlock(&gpmc_mem_lock);
622 return r;
623}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300624EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300625
626void gpmc_cs_free(int cs)
627{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800628 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
629 struct resource *res = &gpmc->mem;
Tony Lindgrenefe80722014-04-21 19:26:13 -0700630
Imre Deakf37e4582006-09-25 12:41:33 +0300631 spin_lock(&gpmc_mem_lock);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530632 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300633 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
634 BUG();
635 spin_unlock(&gpmc_mem_lock);
636 return;
637 }
638 gpmc_cs_disable_mem(cs);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700639 if (res->flags)
640 release_resource(res);
Imre Deakf37e4582006-09-25 12:41:33 +0300641 gpmc_cs_set_reserved(cs, 0);
642 spin_unlock(&gpmc_mem_lock);
643}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300644EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300645
vimal singh59e9c5a2009-07-13 16:26:24 +0530646/**
Jon Hunter3a544352013-02-21 13:00:21 -0600647 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000648 * @cmd: command type
649 * @wval: value to write
650 * @return status of the operation
651 */
Jon Hunter3a544352013-02-21 13:00:21 -0600652int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000653{
Jon Hunter3a544352013-02-21 13:00:21 -0600654 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000655
656 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530657 case GPMC_ENABLE_IRQ:
658 gpmc_write_reg(GPMC_IRQENABLE, wval);
659 break;
660
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000661 case GPMC_SET_IRQ_STATUS:
662 gpmc_write_reg(GPMC_IRQSTATUS, wval);
663 break;
664
665 case GPMC_CONFIG_WP:
666 regval = gpmc_read_reg(GPMC_CONFIG);
667 if (wval)
668 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
669 else
670 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
671 gpmc_write_reg(GPMC_CONFIG, regval);
672 break;
673
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000674 default:
Jon Hunter3a544352013-02-21 13:00:21 -0600675 pr_err("%s: command not supported\n", __func__);
676 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000677 }
678
Jon Hunter3a544352013-02-21 13:00:21 -0600679 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000680}
Jon Hunter3a544352013-02-21 13:00:21 -0600681EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000682
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700683void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
684{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530685 int i;
686
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700687 reg->gpmc_status = gpmc_base + GPMC_STATUS;
688 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
689 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
690 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
691 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
692 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
693 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
694 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
695 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
696 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
697 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
698 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
699 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
700 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
701 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530702
703 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
704 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
705 GPMC_BCH_SIZE * i;
706 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
707 GPMC_BCH_SIZE * i;
708 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
709 GPMC_BCH_SIZE * i;
710 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
711 GPMC_BCH_SIZE * i;
pekon gupta27c9fd62014-05-19 13:24:39 +0530712 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
713 i * GPMC_BCH_SIZE;
714 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
715 i * GPMC_BCH_SIZE;
716 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
717 i * GPMC_BCH_SIZE;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530718 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700719}
720
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700721int gpmc_get_client_irq(unsigned irq_config)
722{
723 int i;
724
725 if (hweight32(irq_config) > 1)
726 return 0;
727
728 for (i = 0; i < GPMC_NR_IRQ; i++)
729 if (gpmc_client_irq[i].bitmask & irq_config)
730 return gpmc_client_irq[i].irq;
731
732 return 0;
733}
734
735static int gpmc_irq_endis(unsigned irq, bool endis)
736{
737 int i;
738 u32 regval;
739
740 for (i = 0; i < GPMC_NR_IRQ; i++)
741 if (irq == gpmc_client_irq[i].irq) {
742 regval = gpmc_read_reg(GPMC_IRQENABLE);
743 if (endis)
744 regval |= gpmc_client_irq[i].bitmask;
745 else
746 regval &= ~gpmc_client_irq[i].bitmask;
747 gpmc_write_reg(GPMC_IRQENABLE, regval);
748 break;
749 }
750
751 return 0;
752}
753
754static void gpmc_irq_disable(struct irq_data *p)
755{
756 gpmc_irq_endis(p->irq, false);
757}
758
759static void gpmc_irq_enable(struct irq_data *p)
760{
761 gpmc_irq_endis(p->irq, true);
762}
763
764static void gpmc_irq_noop(struct irq_data *data) { }
765
766static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
767
Afzal Mohammedda496872012-09-23 17:28:25 -0600768static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700769{
770 int i;
771 u32 regval;
772
773 if (!gpmc_irq)
774 return -EINVAL;
775
776 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000777 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700778 pr_err("irq_alloc_descs failed\n");
779 return gpmc_irq_start;
780 }
781
782 gpmc_irq_chip.name = "gpmc";
783 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
784 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
785 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
786 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
787 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
788 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
789 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
790
791 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
792 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
793
794 for (i = 0; i < GPMC_NR_IRQ; i++) {
795 gpmc_client_irq[i].irq = gpmc_irq_start + i;
796 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
797 &gpmc_irq_chip, handle_simple_irq);
798 set_irq_flags(gpmc_client_irq[i].irq,
799 IRQF_VALID | IRQF_NOAUTOEN);
800 }
801
802 /* Disable interrupts */
803 gpmc_write_reg(GPMC_IRQENABLE, 0);
804
805 /* clear interrupts */
806 regval = gpmc_read_reg(GPMC_IRQSTATUS);
807 gpmc_write_reg(GPMC_IRQSTATUS, regval);
808
809 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
810}
811
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800812static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600813{
814 int i;
815
816 if (gpmc_irq)
817 free_irq(gpmc_irq, NULL);
818
819 for (i = 0; i < GPMC_NR_IRQ; i++) {
820 irq_set_handler(gpmc_client_irq[i].irq, NULL);
821 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
822 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
823 }
824
825 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
826
827 return 0;
828}
829
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800830static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600831{
832 int cs;
833
Gupta Pekonf34f3712013-05-31 17:31:30 +0530834 for (cs = 0; cs < gpmc_cs_num; cs++) {
Afzal Mohammedda496872012-09-23 17:28:25 -0600835 if (!gpmc_cs_mem_enabled(cs))
836 continue;
837 gpmc_cs_delete_mem(cs);
838 }
839
840}
841
Jon Hunter84b00f02013-03-06 14:36:47 -0600842static void gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300843{
Jon Hunter84b00f02013-03-06 14:36:47 -0600844 int cs;
Imre Deakf37e4582006-09-25 12:41:33 +0300845
Jon Hunterbf234392013-03-06 14:12:59 -0600846 /*
847 * The first 1MB of GPMC address space is typically mapped to
848 * the internal ROM. Never allocate the first page, to
849 * facilitate bug detection; even if we didn't boot from ROM.
Kyungmin Park7f245162006-12-29 16:48:51 -0800850 */
Jon Hunterbf234392013-03-06 14:12:59 -0600851 gpmc_mem_root.start = SZ_1M;
Imre Deakf37e4582006-09-25 12:41:33 +0300852 gpmc_mem_root.end = GPMC_MEM_END;
853
854 /* Reserve all regions that has been set up by bootloader */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530855 for (cs = 0; cs < gpmc_cs_num; cs++) {
Imre Deakf37e4582006-09-25 12:41:33 +0300856 u32 base, size;
857
858 if (!gpmc_cs_mem_enabled(cs))
859 continue;
860 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter84b00f02013-03-06 14:36:47 -0600861 if (gpmc_cs_insert_mem(cs, base, size)) {
862 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
863 __func__, cs, base, base + size);
864 gpmc_cs_disable_mem(cs);
Jon Hunter81190242012-10-17 09:41:25 -0500865 }
Imre Deakf37e4582006-09-25 12:41:33 +0300866 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700867}
868
Afzal Mohammed246da262012-08-02 20:02:10 +0530869static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
870{
871 u32 temp;
872 int div;
873
874 div = gpmc_calc_divider(sync_clk);
875 temp = gpmc_ps_to_ticks(time_ps);
876 temp = (temp + div - 1) / div;
877 return gpmc_ticks_to_ps(temp * div);
878}
879
880/* XXX: can the cycles be avoided ? */
881static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600882 struct gpmc_device_timings *dev_t,
883 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530884{
Afzal Mohammed246da262012-08-02 20:02:10 +0530885 u32 temp;
886
887 /* adv_rd_off */
888 temp = dev_t->t_avdp_r;
889 /* XXX: mux check required ? */
890 if (mux) {
891 /* XXX: t_avdp not to be required for sync, only added for tusb
892 * this indirectly necessitates requirement of t_avdp_r and
893 * t_avdp_w instead of having a single t_avdp
894 */
895 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
896 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
897 }
898 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
899
900 /* oe_on */
901 temp = dev_t->t_oeasu; /* XXX: remove this ? */
902 if (mux) {
903 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
904 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
905 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
906 }
907 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
908
909 /* access */
910 /* XXX: any scope for improvement ?, by combining oe_on
911 * and clk_activation, need to check whether
912 * access = clk_activation + round to sync clk ?
913 */
914 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
915 temp += gpmc_t->clk_activation;
916 if (dev_t->cyc_oe)
917 temp = max_t(u32, temp, gpmc_t->oe_on +
918 gpmc_ticks_to_ps(dev_t->cyc_oe));
919 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
920
921 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
922 gpmc_t->cs_rd_off = gpmc_t->oe_off;
923
924 /* rd_cycle */
925 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
926 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
927 gpmc_t->access;
928 /* XXX: barter t_ce_rdyz with t_cez_r ? */
929 if (dev_t->t_ce_rdyz)
930 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
931 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
932
933 return 0;
934}
935
936static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600937 struct gpmc_device_timings *dev_t,
938 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530939{
Afzal Mohammed246da262012-08-02 20:02:10 +0530940 u32 temp;
941
942 /* adv_wr_off */
943 temp = dev_t->t_avdp_w;
944 if (mux) {
945 temp = max_t(u32, temp,
946 gpmc_t->clk_activation + dev_t->t_avdh);
947 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
948 }
949 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
950
951 /* wr_data_mux_bus */
952 temp = max_t(u32, dev_t->t_weasu,
953 gpmc_t->clk_activation + dev_t->t_rdyo);
954 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
955 * and in that case remember to handle we_on properly
956 */
957 if (mux) {
958 temp = max_t(u32, temp,
959 gpmc_t->adv_wr_off + dev_t->t_aavdh);
960 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
961 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
962 }
963 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
964
965 /* we_on */
966 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
967 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
968 else
969 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
970
971 /* wr_access */
972 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
973 gpmc_t->wr_access = gpmc_t->access;
974
975 /* we_off */
976 temp = gpmc_t->we_on + dev_t->t_wpl;
977 temp = max_t(u32, temp,
978 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
979 temp = max_t(u32, temp,
980 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
981 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
982
983 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
984 dev_t->t_wph);
985
986 /* wr_cycle */
987 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
988 temp += gpmc_t->wr_access;
989 /* XXX: barter t_ce_rdyz with t_cez_w ? */
990 if (dev_t->t_ce_rdyz)
991 temp = max_t(u32, temp,
992 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
993 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
994
995 return 0;
996}
997
998static int gpmc_calc_async_read_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_rd_off */
1005 temp = dev_t->t_avdp_r;
1006 if (mux)
1007 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1008 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1009
1010 /* oe_on */
1011 temp = dev_t->t_oeasu;
1012 if (mux)
1013 temp = max_t(u32, temp,
1014 gpmc_t->adv_rd_off + dev_t->t_aavdh);
1015 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1016
1017 /* access */
1018 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1019 gpmc_t->oe_on + dev_t->t_oe);
1020 temp = max_t(u32, temp,
1021 gpmc_t->cs_on + dev_t->t_ce);
1022 temp = max_t(u32, temp,
1023 gpmc_t->adv_on + dev_t->t_aa);
1024 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1025
1026 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1027 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1028
1029 /* rd_cycle */
1030 temp = max_t(u32, dev_t->t_rd_cycle,
1031 gpmc_t->cs_rd_off + dev_t->t_cez_r);
1032 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1033 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1034
1035 return 0;
1036}
1037
1038static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001039 struct gpmc_device_timings *dev_t,
1040 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301041{
Afzal Mohammed246da262012-08-02 20:02:10 +05301042 u32 temp;
1043
1044 /* adv_wr_off */
1045 temp = dev_t->t_avdp_w;
1046 if (mux)
1047 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1048 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1049
1050 /* wr_data_mux_bus */
1051 temp = dev_t->t_weasu;
1052 if (mux) {
1053 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1054 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1055 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1056 }
1057 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1058
1059 /* we_on */
1060 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1061 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1062 else
1063 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1064
1065 /* we_off */
1066 temp = gpmc_t->we_on + dev_t->t_wpl;
1067 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1068
1069 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1070 dev_t->t_wph);
1071
1072 /* wr_cycle */
1073 temp = max_t(u32, dev_t->t_wr_cycle,
1074 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1075 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1076
1077 return 0;
1078}
1079
1080static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1081 struct gpmc_device_timings *dev_t)
1082{
1083 u32 temp;
1084
1085 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1086 gpmc_get_fclk_period();
1087
1088 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1089 dev_t->t_bacc,
1090 gpmc_t->sync_clk);
1091
1092 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1093 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1094
1095 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1096 return 0;
1097
1098 if (dev_t->ce_xdelay)
1099 gpmc_t->bool_timings.cs_extra_delay = true;
1100 if (dev_t->avd_xdelay)
1101 gpmc_t->bool_timings.adv_extra_delay = true;
1102 if (dev_t->oe_xdelay)
1103 gpmc_t->bool_timings.oe_extra_delay = true;
1104 if (dev_t->we_xdelay)
1105 gpmc_t->bool_timings.we_extra_delay = true;
1106
1107 return 0;
1108}
1109
1110static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001111 struct gpmc_device_timings *dev_t,
1112 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301113{
1114 u32 temp;
1115
1116 /* cs_on */
1117 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1118
1119 /* adv_on */
1120 temp = dev_t->t_avdasu;
1121 if (dev_t->t_ce_avd)
1122 temp = max_t(u32, temp,
1123 gpmc_t->cs_on + dev_t->t_ce_avd);
1124 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1125
Jon Hunterc3be5b42013-02-21 13:46:22 -06001126 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301127 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1128
1129 return 0;
1130}
1131
1132/* TODO: remove this function once all peripherals are confirmed to
1133 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1134 * has to be modified to handle timings in ps instead of ns
1135*/
1136static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1137{
1138 t->cs_on /= 1000;
1139 t->cs_rd_off /= 1000;
1140 t->cs_wr_off /= 1000;
1141 t->adv_on /= 1000;
1142 t->adv_rd_off /= 1000;
1143 t->adv_wr_off /= 1000;
1144 t->we_on /= 1000;
1145 t->we_off /= 1000;
1146 t->oe_on /= 1000;
1147 t->oe_off /= 1000;
1148 t->page_burst_access /= 1000;
1149 t->access /= 1000;
1150 t->rd_cycle /= 1000;
1151 t->wr_cycle /= 1000;
1152 t->bus_turnaround /= 1000;
1153 t->cycle2cycle_delay /= 1000;
1154 t->wait_monitoring /= 1000;
1155 t->clk_activation /= 1000;
1156 t->wr_access /= 1000;
1157 t->wr_data_mux_bus /= 1000;
1158}
1159
1160int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001161 struct gpmc_settings *gpmc_s,
1162 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301163{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001164 bool mux = false, sync = false;
1165
1166 if (gpmc_s) {
1167 mux = gpmc_s->mux_add_data ? true : false;
1168 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1169 }
1170
Afzal Mohammed246da262012-08-02 20:02:10 +05301171 memset(gpmc_t, 0, sizeof(*gpmc_t));
1172
Jon Hunterc3be5b42013-02-21 13:46:22 -06001173 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301174
Jon Hunterc3be5b42013-02-21 13:46:22 -06001175 if (gpmc_s && gpmc_s->sync_read)
1176 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301177 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001178 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301179
Jon Hunterc3be5b42013-02-21 13:46:22 -06001180 if (gpmc_s && gpmc_s->sync_write)
1181 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301182 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001183 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301184
1185 /* TODO: remove, see function definition */
1186 gpmc_convert_ps_to_ns(gpmc_t);
1187
1188 return 0;
1189}
1190
Jon Hunteraa8d4762013-02-21 15:25:23 -06001191/**
1192 * gpmc_cs_program_settings - programs non-timing related settings
1193 * @cs: GPMC chip-select to program
1194 * @p: pointer to GPMC settings structure
1195 *
1196 * Programs non-timing related settings for a GPMC chip-select, such as
1197 * bus-width, burst configuration, etc. Function should be called once
1198 * for each chip-select that is being used and must be called before
1199 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1200 * register will be initialised to zero by this function. Returns 0 on
1201 * success and appropriate negative error code on failure.
1202 */
1203int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1204{
1205 u32 config1;
1206
1207 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1208 pr_err("%s: invalid width %d!", __func__, p->device_width);
1209 return -EINVAL;
1210 }
1211
1212 /* Address-data multiplexing not supported for NAND devices */
1213 if (p->device_nand && p->mux_add_data) {
1214 pr_err("%s: invalid configuration!\n", __func__);
1215 return -EINVAL;
1216 }
1217
1218 if ((p->mux_add_data > GPMC_MUX_AD) ||
1219 ((p->mux_add_data == GPMC_MUX_AAD) &&
1220 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1221 pr_err("%s: invalid multiplex configuration!\n", __func__);
1222 return -EINVAL;
1223 }
1224
1225 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1226 if (p->burst_read || p->burst_write) {
1227 switch (p->burst_len) {
1228 case GPMC_BURST_4:
1229 case GPMC_BURST_8:
1230 case GPMC_BURST_16:
1231 break;
1232 default:
1233 pr_err("%s: invalid page/burst-length (%d)\n",
1234 __func__, p->burst_len);
1235 return -EINVAL;
1236 }
1237 }
1238
Roger Quadros2b540572014-09-02 16:57:06 +03001239 if (p->wait_pin > gpmc_nr_waitpins) {
Jon Hunteraa8d4762013-02-21 15:25:23 -06001240 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1241 return -EINVAL;
1242 }
1243
1244 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1245
1246 if (p->sync_read)
1247 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1248 if (p->sync_write)
1249 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1250 if (p->wait_on_read)
1251 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1252 if (p->wait_on_write)
1253 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1254 if (p->wait_on_read || p->wait_on_write)
1255 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1256 if (p->device_nand)
1257 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1258 if (p->mux_add_data)
1259 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1260 if (p->burst_read)
1261 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1262 if (p->burst_write)
1263 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1264 if (p->burst_read || p->burst_write) {
1265 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1266 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1267 }
1268
1269 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1270
1271 return 0;
1272}
1273
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001274#ifdef CONFIG_OF
Uwe Kleine-König31957602014-09-10 10:26:17 +02001275static const struct of_device_id gpmc_dt_ids[] = {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001276 { .compatible = "ti,omap2420-gpmc" },
1277 { .compatible = "ti,omap2430-gpmc" },
1278 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1279 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1280 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1281 { }
1282};
1283MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1284
Jon Hunter8c8a77712013-02-20 15:53:12 -06001285/**
1286 * gpmc_read_settings_dt - read gpmc settings from device-tree
1287 * @np: pointer to device-tree node for a gpmc child device
1288 * @p: pointer to gpmc settings structure
1289 *
1290 * Reads the GPMC settings for a GPMC child device from device-tree and
1291 * stores them in the GPMC settings structure passed. The GPMC settings
1292 * structure is initialised to zero by this function and so any
1293 * previously stored settings will be cleared.
1294 */
1295void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1296{
1297 memset(p, 0, sizeof(struct gpmc_settings));
1298
1299 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1300 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001301 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1302 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1303
1304 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1305 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1306 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1307 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1308 if (!p->burst_read && !p->burst_write)
1309 pr_warn("%s: page/burst-length set but not used!\n",
1310 __func__);
1311 }
1312
1313 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1314 p->wait_on_read = of_property_read_bool(np,
1315 "gpmc,wait-on-read");
1316 p->wait_on_write = of_property_read_bool(np,
1317 "gpmc,wait-on-write");
1318 if (!p->wait_on_read && !p->wait_on_write)
Roger Quadros2b540572014-09-02 16:57:06 +03001319 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1320 __func__);
Jon Hunter8c8a77712013-02-20 15:53:12 -06001321 }
1322}
1323
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001324static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1325 struct gpmc_timings *gpmc_t)
1326{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001327 struct gpmc_bool_timings *p;
1328
1329 if (!np || !gpmc_t)
1330 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001331
1332 memset(gpmc_t, 0, sizeof(*gpmc_t));
1333
1334 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001335 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001336
1337 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001338 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1339 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1340 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001341
1342 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001343 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1344 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1345 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001346
1347 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001348 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1349 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001350
1351 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001352 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1353 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001354
1355 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001356 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1357 &gpmc_t->page_burst_access);
1358 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1359 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1360 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1361 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1362 &gpmc_t->bus_turnaround);
1363 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1364 &gpmc_t->cycle2cycle_delay);
1365 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1366 &gpmc_t->wait_monitoring);
1367 of_property_read_u32(np, "gpmc,clk-activation-ns",
1368 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001369
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001370 /* only applicable to OMAP3+ */
1371 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1372 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1373 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001374
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001375 /* bool timing parameters */
1376 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001377
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001378 p->cycle2cyclediffcsen =
1379 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1380 p->cycle2cyclesamecsen =
1381 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1382 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1383 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1384 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1385 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1386 p->time_para_granularity =
1387 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001388}
1389
Pekon Gupta6b187b22014-01-28 11:42:40 +05301390#if IS_ENABLED(CONFIG_MTD_NAND)
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001391
Mark Jackson496c8a02013-04-19 21:08:28 +01001392static const char * const nand_xfer_types[] = {
1393 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1394 [NAND_OMAP_POLLED] = "polled",
1395 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1396 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1397};
1398
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001399static int gpmc_probe_nand_child(struct platform_device *pdev,
1400 struct device_node *child)
1401{
1402 u32 val;
1403 const char *s;
1404 struct gpmc_timings gpmc_t;
1405 struct omap_nand_platform_data *gpmc_nand_data;
1406
1407 if (of_property_read_u32(child, "reg", &val) < 0) {
1408 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1409 child->full_name);
1410 return -ENODEV;
1411 }
1412
1413 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1414 GFP_KERNEL);
1415 if (!gpmc_nand_data)
1416 return -ENOMEM;
1417
1418 gpmc_nand_data->cs = val;
1419 gpmc_nand_data->of_node = child;
1420
Pekon Guptaac65caf2013-10-24 18:20:17 +05301421 /* Detect availability of ELM module */
1422 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1423 if (gpmc_nand_data->elm_of_node == NULL)
1424 gpmc_nand_data->elm_of_node =
1425 of_parse_phandle(child, "elm_id", 0);
1426 if (gpmc_nand_data->elm_of_node == NULL)
1427 pr_warn("%s: ti,elm-id property not found\n", __func__);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001428
Pekon Guptaac65caf2013-10-24 18:20:17 +05301429 /* select ecc-scheme for NAND */
1430 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1431 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1432 return -ENODEV;
1433 }
Roger Quadrosa3e83f02014-08-25 16:15:33 -07001434
1435 if (!strcmp(s, "sw"))
1436 gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1437 else if (!strcmp(s, "ham1") ||
1438 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
Pekon Guptaac65caf2013-10-24 18:20:17 +05301439 gpmc_nand_data->ecc_opt =
1440 OMAP_ECC_HAM1_CODE_HW;
1441 else if (!strcmp(s, "bch4"))
1442 if (gpmc_nand_data->elm_of_node)
1443 gpmc_nand_data->ecc_opt =
1444 OMAP_ECC_BCH4_CODE_HW;
1445 else
1446 gpmc_nand_data->ecc_opt =
1447 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1448 else if (!strcmp(s, "bch8"))
1449 if (gpmc_nand_data->elm_of_node)
1450 gpmc_nand_data->ecc_opt =
1451 OMAP_ECC_BCH8_CODE_HW;
1452 else
1453 gpmc_nand_data->ecc_opt =
1454 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
pekon gupta27c9fd62014-05-19 13:24:39 +05301455 else if (!strcmp(s, "bch16"))
1456 if (gpmc_nand_data->elm_of_node)
1457 gpmc_nand_data->ecc_opt =
1458 OMAP_ECC_BCH16_CODE_HW;
1459 else
1460 pr_err("%s: BCH16 requires ELM support\n", __func__);
Pekon Guptaac65caf2013-10-24 18:20:17 +05301461 else
1462 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1463
1464 /* select data transfer mode for NAND controller */
Mark Jackson496c8a02013-04-19 21:08:28 +01001465 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1466 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1467 if (!strcasecmp(s, nand_xfer_types[val])) {
1468 gpmc_nand_data->xfer_type = val;
1469 break;
1470 }
1471
Ezequiel Garcíafef775c2014-09-11 12:02:08 -03001472 gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
1473
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001474 val = of_get_nand_bus_width(child);
1475 if (val == 16)
1476 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1477
1478 gpmc_read_timings_dt(child, &gpmc_t);
1479 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1480
1481 return 0;
1482}
1483#else
1484static int gpmc_probe_nand_child(struct platform_device *pdev,
1485 struct device_node *child)
1486{
1487 return 0;
1488}
1489#endif
1490
Pekon Gupta980386d2014-01-28 11:42:41 +05301491#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001492static int gpmc_probe_onenand_child(struct platform_device *pdev,
1493 struct device_node *child)
1494{
1495 u32 val;
1496 struct omap_onenand_platform_data *gpmc_onenand_data;
1497
1498 if (of_property_read_u32(child, "reg", &val) < 0) {
1499 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1500 child->full_name);
1501 return -ENODEV;
1502 }
1503
1504 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1505 GFP_KERNEL);
1506 if (!gpmc_onenand_data)
1507 return -ENOMEM;
1508
1509 gpmc_onenand_data->cs = val;
1510 gpmc_onenand_data->of_node = child;
1511 gpmc_onenand_data->dma_channel = -1;
1512
1513 if (!of_property_read_u32(child, "dma-channel", &val))
1514 gpmc_onenand_data->dma_channel = val;
1515
1516 gpmc_onenand_init(gpmc_onenand_data);
1517
1518 return 0;
1519}
1520#else
1521static int gpmc_probe_onenand_child(struct platform_device *pdev,
1522 struct device_node *child)
1523{
1524 return 0;
1525}
1526#endif
1527
Jon Huntercdd69282013-02-08 16:46:13 -06001528/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001529 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001530 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001531 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001532 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001533 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001534 * Returns 0 on success and appropriate negative error code on failure.
1535 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001536static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001537 struct device_node *child)
1538{
1539 struct gpmc_settings gpmc_s;
1540 struct gpmc_timings gpmc_t;
1541 struct resource res;
1542 unsigned long base;
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001543 const char *name;
Jon Huntercdd69282013-02-08 16:46:13 -06001544 int ret, cs;
1545
1546 if (of_property_read_u32(child, "reg", &cs) < 0) {
1547 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1548 child->full_name);
1549 return -ENODEV;
1550 }
1551
1552 if (of_address_to_resource(child, 0, &res) < 0) {
1553 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1554 child->full_name);
1555 return -ENODEV;
1556 }
1557
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001558 /*
1559 * Check if we have multiple instances of the same device
1560 * on a single chip select. If so, use the already initialized
1561 * timings.
1562 */
1563 name = gpmc_cs_get_name(cs);
1564 if (name && child->name && of_node_cmp(child->name, name) == 0)
1565 goto no_timings;
1566
Jon Huntercdd69282013-02-08 16:46:13 -06001567 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1568 if (ret < 0) {
1569 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1570 return ret;
1571 }
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001572 gpmc_cs_set_name(cs, child->name);
Jon Huntercdd69282013-02-08 16:46:13 -06001573
1574 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001575 * For some GPMC devices we still need to rely on the bootloader
1576 * timings because the devices can be connected via FPGA. So far
1577 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1578 * REVISIT: Add timing support from slls644g.pdf and from the
1579 * lan91c96 manual.
1580 */
1581 if (of_device_is_compatible(child, "ns16550a") ||
1582 of_device_is_compatible(child, "smsc,lan91c94") ||
1583 of_device_is_compatible(child, "smsc,lan91c111")) {
1584 dev_warn(&pdev->dev,
1585 "%s using bootloader timings on CS%d\n",
1586 child->name, cs);
1587 goto no_timings;
1588 }
1589
1590 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001591 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1592 * location in the gpmc address space. When booting with
1593 * device-tree we want the NOR flash to be mapped to the
1594 * location specified in the device-tree blob. So remap the
1595 * CS to this location. Once DT migration is complete should
1596 * just make gpmc_cs_request() map a specific address.
1597 */
1598 ret = gpmc_cs_remap(cs, res.start);
1599 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001600 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1601 cs, &res.start);
Jon Huntercdd69282013-02-08 16:46:13 -06001602 goto err;
1603 }
1604
1605 gpmc_read_settings_dt(child, &gpmc_s);
1606
1607 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1608 if (ret < 0)
1609 goto err;
1610
1611 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1612 if (ret < 0)
1613 goto err;
1614
1615 gpmc_read_timings_dt(child, &gpmc_t);
1616 gpmc_cs_set_timings(cs, &gpmc_t);
1617
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001618no_timings:
Jon Huntercdd69282013-02-08 16:46:13 -06001619 if (of_platform_device_create(child, NULL, &pdev->dev))
1620 return 0;
1621
1622 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01001623 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06001624
1625err:
1626 gpmc_cs_free(cs);
1627
1628 return ret;
1629}
1630
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001631static int gpmc_probe_dt(struct platform_device *pdev)
1632{
1633 int ret;
1634 struct device_node *child;
1635 const struct of_device_id *of_id =
1636 of_match_device(gpmc_dt_ids, &pdev->dev);
1637
1638 if (!of_id)
1639 return 0;
1640
Gupta Pekonf34f3712013-05-31 17:31:30 +05301641 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1642 &gpmc_cs_num);
1643 if (ret < 0) {
1644 pr_err("%s: number of chip-selects not defined\n", __func__);
1645 return ret;
1646 } else if (gpmc_cs_num < 1) {
1647 pr_err("%s: all chip-selects are disabled\n", __func__);
1648 return -EINVAL;
1649 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1650 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1651 __func__, GPMC_CS_NUM);
1652 return -EINVAL;
1653 }
1654
Jon Hunter9f833152013-02-20 15:53:38 -06001655 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1656 &gpmc_nr_waitpins);
1657 if (ret < 0) {
1658 pr_err("%s: number of wait pins not found!\n", __func__);
1659 return ret;
1660 }
1661
Guido Martínez68e2eb52014-07-02 10:35:18 -03001662 for_each_available_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001663
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001664 if (!child->name)
1665 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06001666
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001667 if (of_node_cmp(child->name, "nand") == 0)
1668 ret = gpmc_probe_nand_child(pdev, child);
1669 else if (of_node_cmp(child->name, "onenand") == 0)
1670 ret = gpmc_probe_onenand_child(pdev, child);
1671 else if (of_node_cmp(child->name, "ethernet") == 0 ||
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001672 of_node_cmp(child->name, "nor") == 0 ||
1673 of_node_cmp(child->name, "uart") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001674 ret = gpmc_probe_generic_child(pdev, child);
Jon Huntercdd69282013-02-08 16:46:13 -06001675
Javier Martinez Canillasb327b362013-04-17 22:34:12 +02001676 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1677 __func__, child->full_name))
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001678 of_node_put(child);
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001679 }
1680
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001681 return 0;
1682}
1683#else
1684static int gpmc_probe_dt(struct platform_device *pdev)
1685{
1686 return 0;
1687}
1688#endif
1689
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001690static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001691{
Jon Hunter81190242012-10-17 09:41:25 -05001692 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001693 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001694 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001695
Afzal Mohammedda496872012-09-23 17:28:25 -06001696 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1697 if (res == NULL)
1698 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001699
Afzal Mohammedda496872012-09-23 17:28:25 -06001700 phys_base = res->start;
1701 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001702
Thierry Reding5857bd92013-01-21 11:08:55 +01001703 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1704 if (IS_ERR(gpmc_base))
1705 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001706
1707 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1708 if (res == NULL)
1709 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1710 else
1711 gpmc_irq = res->start;
1712
1713 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1714 if (IS_ERR(gpmc_l3_clk)) {
1715 dev_err(&pdev->dev, "error: clk_get\n");
1716 gpmc_irq = 0;
1717 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001718 }
1719
avinash philipb3f55252013-06-12 16:30:56 +05301720 pm_runtime_enable(&pdev->dev);
1721 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001722
Afzal Mohammedda496872012-09-23 17:28:25 -06001723 gpmc_dev = &pdev->dev;
1724
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001725 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001726
1727 /*
1728 * FIXME: Once device-tree migration is complete the below flags
1729 * should be populated based upon the device-tree compatible
1730 * string. For now just use the IP revision. OMAP3+ devices have
1731 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1732 * devices support the addr-addr-data multiplex protocol.
1733 *
1734 * GPMC IP revisions:
1735 * - OMAP24xx = 2.0
1736 * - OMAP3xxx = 5.0
1737 * - OMAP44xx/54xx/AM335x = 6.0
1738 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001739 if (GPMC_REVISION_MAJOR(l) > 0x4)
1740 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001741 if (GPMC_REVISION_MAJOR(l) > 0x5)
1742 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001743 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1744 GPMC_REVISION_MINOR(l));
1745
Jon Hunter84b00f02013-03-06 14:36:47 -06001746 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301747
Russell King71856842013-03-13 20:44:21 +00001748 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001749 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1750
Gupta Pekonf34f3712013-05-31 17:31:30 +05301751 if (!pdev->dev.of_node) {
1752 gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -06001753 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
Gupta Pekonf34f3712013-05-31 17:31:30 +05301754 }
Jon Hunter9f833152013-02-20 15:53:38 -06001755
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001756 rc = gpmc_probe_dt(pdev);
1757 if (rc < 0) {
avinash philipb3f55252013-06-12 16:30:56 +05301758 pm_runtime_put_sync(&pdev->dev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001759 clk_put(gpmc_l3_clk);
1760 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1761 return rc;
1762 }
1763
Afzal Mohammedda496872012-09-23 17:28:25 -06001764 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301765}
Afzal Mohammedda496872012-09-23 17:28:25 -06001766
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001767static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001768{
1769 gpmc_free_irq();
1770 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05301771 pm_runtime_put_sync(&pdev->dev);
1772 pm_runtime_disable(&pdev->dev);
Afzal Mohammedda496872012-09-23 17:28:25 -06001773 gpmc_dev = NULL;
1774 return 0;
1775}
1776
avinash philipb536dd42013-06-18 00:16:38 +05301777#ifdef CONFIG_PM_SLEEP
1778static int gpmc_suspend(struct device *dev)
1779{
1780 omap3_gpmc_save_context();
1781 pm_runtime_put_sync(dev);
1782 return 0;
1783}
1784
1785static int gpmc_resume(struct device *dev)
1786{
1787 pm_runtime_get_sync(dev);
1788 omap3_gpmc_restore_context();
1789 return 0;
1790}
1791#endif
1792
1793static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1794
Afzal Mohammedda496872012-09-23 17:28:25 -06001795static struct platform_driver gpmc_driver = {
1796 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001797 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001798 .driver = {
1799 .name = DEVICE_NAME,
1800 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001801 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05301802 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06001803 },
1804};
1805
1806static __init int gpmc_init(void)
1807{
1808 return platform_driver_register(&gpmc_driver);
1809}
1810
1811static __exit void gpmc_exit(void)
1812{
1813 platform_driver_unregister(&gpmc_driver);
1814
1815}
1816
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001817omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001818module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301819
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001820static int __init omap_gpmc_init(void)
1821{
1822 struct omap_hwmod *oh;
1823 struct platform_device *pdev;
1824 char *oh_name = "gpmc";
1825
Daniel Mack2f98ca82012-12-14 11:36:40 +01001826 /*
1827 * if the board boots up with a populated DT, do not
1828 * manually add the device from this initcall
1829 */
1830 if (of_have_populated_dt())
1831 return -ENODEV;
1832
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001833 oh = omap_hwmod_lookup(oh_name);
1834 if (!oh) {
1835 pr_err("Could not look up %s\n", oh_name);
1836 return -ENODEV;
1837 }
1838
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001839 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001840 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1841
Thomas Meyer12616742013-06-01 11:44:44 +02001842 return PTR_RET(pdev);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001843}
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001844omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001845
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301846static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1847{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001848 int i;
1849 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301850
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001851 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1852
1853 if (!regval)
1854 return IRQ_NONE;
1855
1856 for (i = 0; i < GPMC_NR_IRQ; i++)
1857 if (regval & gpmc_client_irq[i].bitmask)
1858 generic_handle_irq(gpmc_client_irq[i].irq);
1859
1860 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301861
1862 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001863}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301864
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301865static struct omap3_gpmc_regs gpmc_context;
1866
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001867void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301868{
1869 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001870
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301871 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1872 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1873 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1874 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1875 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1876 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1877 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301878 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301879 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1880 if (gpmc_context.cs_context[i].is_valid) {
1881 gpmc_context.cs_context[i].config1 =
1882 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1883 gpmc_context.cs_context[i].config2 =
1884 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1885 gpmc_context.cs_context[i].config3 =
1886 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1887 gpmc_context.cs_context[i].config4 =
1888 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1889 gpmc_context.cs_context[i].config5 =
1890 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1891 gpmc_context.cs_context[i].config6 =
1892 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1893 gpmc_context.cs_context[i].config7 =
1894 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1895 }
1896 }
1897}
1898
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001899void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301900{
1901 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001902
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301903 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1904 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1905 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1906 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1907 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1908 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1909 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301910 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301911 if (gpmc_context.cs_context[i].is_valid) {
1912 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1913 gpmc_context.cs_context[i].config1);
1914 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1915 gpmc_context.cs_context[i].config2);
1916 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1917 gpmc_context.cs_context[i].config3);
1918 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1919 gpmc_context.cs_context[i].config4);
1920 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1921 gpmc_context.cs_context[i].config5);
1922 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1923 gpmc_context.cs_context[i].config6);
1924 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1925 gpmc_context.cs_context[i].config7);
1926 }
1927 }
1928}