blob: eee7065a5020078d31267674328407219ef6457a [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
Tony Lindgren35ac0512014-11-03 17:45:01 -0800295static int get_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
296 bool raw, bool noval, int shift,
297 const char *name)
298{
299 u32 l;
300 int nr_bits, max_value, mask;
301
302 l = gpmc_cs_read_reg(cs, reg);
303 nr_bits = end_bit - st_bit + 1;
304 max_value = (1 << nr_bits) - 1;
305 mask = max_value << st_bit;
306 l = (l & mask) >> st_bit;
307 if (shift)
308 l = (shift << l);
309 if (noval && (l == 0))
310 return 0;
311 if (!raw) {
312 unsigned int time_ns_min, time_ns, time_ns_max;
313
314 time_ns_min = gpmc_ticks_to_ns(l ? l - 1 : 0);
315 time_ns = gpmc_ticks_to_ns(l);
316 time_ns_max = gpmc_ticks_to_ns(l + 1 > max_value ?
317 max_value : l + 1);
318 pr_info("gpmc,%s = <%u> (%u - %u ns, %i ticks)\n",
319 name, time_ns, time_ns_min, time_ns_max, l);
320 } else {
321 pr_info("gpmc,%s = <%u>\n", name, l);
322 }
323
324 return l;
325}
326
327#define GPMC_PRINT_CONFIG(cs, config) \
328 pr_info("cs%i %s: 0x%08x\n", cs, #config, \
329 gpmc_cs_read_reg(cs, config))
330#define GPMC_GET_RAW(reg, st, end, field) \
331 get_gpmc_timing_reg(cs, (reg), (st), (end), 1, 0, 0, field)
332#define GPMC_GET_RAW_BOOL(reg, st, end, field) \
333 get_gpmc_timing_reg(cs, (reg), (st), (end), 1, 1, 0, field)
334#define GPMC_GET_RAW_SHIFT(reg, st, end, shift, field) \
335 get_gpmc_timing_reg(cs, (reg), (st), (end), 1, 1, (shift), field)
336#define GPMC_GET_TICKS(reg, st, end, field) \
337 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, 0, 0, field)
338
339static void gpmc_show_regs(int cs, const char *desc)
340{
341 pr_info("gpmc cs%i %s:\n", cs, desc);
342 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
343 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
344 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
345 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
346 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
347 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
348}
349
350/*
351 * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
352 * see commit c9fb809.
353 */
354static void gpmc_cs_show_timings(int cs, const char *desc)
355{
356 gpmc_show_regs(cs, desc);
357
358 pr_info("gpmc cs%i access configuration:\n", cs);
359 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
360 GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
361 GPMC_GET_RAW(GPMC_CS_CONFIG1, 12, 13, "device-width");
362 GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
363 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
364 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
365 GPMC_GET_RAW_SHIFT(GPMC_CS_CONFIG1, 23, 24, 4, "burst-length");
366 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
367 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
368 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
369 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
370 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
371
372 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
373
374 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
375
376 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
377 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
378
379 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
380 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
381
382 pr_info("gpmc cs%i timings configuration:\n", cs);
383 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
384 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
385 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
386
387 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
388 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
389 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
390
391 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
392 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
393 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
394 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
395
396 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
397 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
398 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
399
400 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
401
402 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
403 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
404
405 GPMC_GET_TICKS(GPMC_CS_CONFIG1, 18, 19, "wait-monitoring-ns");
406 GPMC_GET_TICKS(GPMC_CS_CONFIG1, 25, 26, "clk-activation-ns");
407
408 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
409 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
410}
411#else
412static inline void gpmc_cs_show_timings(int cs, const char *desc)
413{
414}
415#endif
416
417#ifdef DEBUG
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700418static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700419 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700420#else
421static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
422 int time)
423#endif
424{
425 u32 l;
426 int ticks, mask, nr_bits;
427
428 if (time == 0)
429 ticks = 0;
430 else
431 ticks = gpmc_ns_to_ticks(time);
432 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800433 if (ticks >= 1 << nr_bits) {
434#ifdef DEBUG
435 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
436 cs, name, time, ticks, 1 << nr_bits);
437#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700438 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800439 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700440
441 mask = (1 << nr_bits) - 1;
442 l = gpmc_cs_read_reg(cs, reg);
443#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800444 printk(KERN_INFO
445 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700446 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800447 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700448#endif
449 l &= ~(mask << st_bit);
450 l |= ticks << st_bit;
451 gpmc_cs_write_reg(cs, reg, l);
452
453 return 0;
454}
455
456#ifdef DEBUG
457#define GPMC_SET_ONE(reg, st, end, field) \
458 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
459 t->field, #field) < 0) \
460 return -1
461#else
462#define GPMC_SET_ONE(reg, st, end, field) \
463 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
464 return -1
465#endif
466
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530467int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700468{
469 int div;
470 u32 l;
471
Adrian Huntera3551f52010-12-09 10:48:27 +0200472 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700473 div = l / gpmc_get_fclk_period();
474 if (div > 4)
475 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800476 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700477 div = 1;
478
479 return div;
480}
481
482int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
483{
484 int div;
485 u32 l;
486
Tony Lindgren35ac0512014-11-03 17:45:01 -0800487 gpmc_cs_show_timings(cs, "before gpmc_cs_set_timings");
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530488 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700489 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600490 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700491
492 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
493 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
494 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
495
496 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
497 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
498 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
499
500 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
501 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
502 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
503 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
504
505 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
506 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
507 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
508
509 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
510
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530511 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
512 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
513
514 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
515 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
516
Afzal Mohammedda496872012-09-23 17:28:25 -0600517 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300518 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600519 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300520 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300521
David Brownell1c22cc12006-12-06 17:13:55 -0800522 /* caller is expected to have initialized CONFIG1 to cover
523 * at least sync vs async
524 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700525 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800526 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
527#ifdef DEBUG
528 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
529 cs, (div * gpmc_get_fclk_period()) / 1000, div);
530#endif
531 l &= ~0x03;
532 l |= (div - 1);
533 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
534 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700535
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530536 gpmc_cs_bool_timings(cs, &t->bool_timings);
Tony Lindgren35ac0512014-11-03 17:45:01 -0800537 gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530538
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700539 return 0;
540}
541
Jon Hunterc71f8e92013-03-06 12:00:10 -0600542static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700543{
Imre Deakf37e4582006-09-25 12:41:33 +0300544 u32 l;
545 u32 mask;
546
Jon Hunterc71f8e92013-03-06 12:00:10 -0600547 /*
548 * Ensure that base address is aligned on a
549 * boundary equal to or greater than size.
550 */
551 if (base & (size - 1))
552 return -EINVAL;
553
Imre Deakf37e4582006-09-25 12:41:33 +0300554 mask = (1 << GPMC_SECTION_SHIFT) - size;
555 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
556 l &= ~0x3f;
557 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
558 l &= ~(0x0f << 8);
559 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530560 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300561 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600562
563 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300564}
565
566static void gpmc_cs_disable_mem(int cs)
567{
568 u32 l;
569
570 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530571 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300572 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
573}
574
575static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
576{
577 u32 l;
578 u32 mask;
579
580 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
581 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
582 mask = (l >> 8) & 0x0f;
583 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
584}
585
586static int gpmc_cs_mem_enabled(int cs)
587{
588 u32 l;
589
590 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530591 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300592}
593
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300594static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300595{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800596 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
597
598 gpmc->flags |= GPMC_CS_RESERVED;
Imre Deakf37e4582006-09-25 12:41:33 +0300599}
600
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300601static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300602{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800603 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
604
605 return gpmc->flags & GPMC_CS_RESERVED;
606}
607
608static void gpmc_cs_set_name(int cs, const char *name)
609{
610 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
611
612 gpmc->name = name;
613}
614
615const char *gpmc_cs_get_name(int cs)
616{
617 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
618
619 return gpmc->name;
Imre Deakf37e4582006-09-25 12:41:33 +0300620}
621
622static unsigned long gpmc_mem_align(unsigned long size)
623{
624 int order;
625
626 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
627 order = GPMC_CHUNK_SHIFT - 1;
628 do {
629 size >>= 1;
630 order++;
631 } while (size);
632 size = 1 << order;
633 return size;
634}
635
636static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
637{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800638 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
639 struct resource *res = &gpmc->mem;
Imre Deakf37e4582006-09-25 12:41:33 +0300640 int r;
641
642 size = gpmc_mem_align(size);
643 spin_lock(&gpmc_mem_lock);
644 res->start = base;
645 res->end = base + size - 1;
646 r = request_resource(&gpmc_mem_root, res);
647 spin_unlock(&gpmc_mem_lock);
648
649 return r;
650}
651
Afzal Mohammedda496872012-09-23 17:28:25 -0600652static int gpmc_cs_delete_mem(int cs)
653{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800654 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
655 struct resource *res = &gpmc->mem;
Afzal Mohammedda496872012-09-23 17:28:25 -0600656 int r;
657
658 spin_lock(&gpmc_mem_lock);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700659 r = release_resource(res);
Afzal Mohammedda496872012-09-23 17:28:25 -0600660 res->start = 0;
661 res->end = 0;
662 spin_unlock(&gpmc_mem_lock);
663
664 return r;
665}
666
Jon Huntercdd69282013-02-08 16:46:13 -0600667/**
668 * gpmc_cs_remap - remaps a chip-select physical base address
669 * @cs: chip-select to remap
670 * @base: physical base address to re-map chip-select to
671 *
672 * Re-maps a chip-select to a new physical base address specified by
673 * "base". Returns 0 on success and appropriate negative error code
674 * on failure.
675 */
676static int gpmc_cs_remap(int cs, u32 base)
677{
678 int ret;
679 u32 old_base, size;
680
Gupta Pekonf34f3712013-05-31 17:31:30 +0530681 if (cs > gpmc_cs_num) {
682 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600683 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530684 }
Tony Lindgrenfb677ef2014-04-21 19:26:13 -0700685
686 /*
687 * Make sure we ignore any device offsets from the GPMC partition
688 * allocated for the chip select and that the new base confirms
689 * to the GPMC 16MB minimum granularity.
690 */
691 base &= ~(SZ_16M - 1);
692
Jon Huntercdd69282013-02-08 16:46:13 -0600693 gpmc_cs_get_memconf(cs, &old_base, &size);
694 if (base == old_base)
695 return 0;
696 gpmc_cs_disable_mem(cs);
697 ret = gpmc_cs_delete_mem(cs);
698 if (ret < 0)
699 return ret;
700 ret = gpmc_cs_insert_mem(cs, base, size);
701 if (ret < 0)
702 return ret;
Jon Hunterc71f8e92013-03-06 12:00:10 -0600703 ret = gpmc_cs_enable_mem(cs, base, size);
704 if (ret < 0)
705 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600706
707 return 0;
708}
709
Imre Deakf37e4582006-09-25 12:41:33 +0300710int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
711{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800712 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
713 struct resource *res = &gpmc->mem;
Imre Deakf37e4582006-09-25 12:41:33 +0300714 int r = -1;
715
Gupta Pekonf34f3712013-05-31 17:31:30 +0530716 if (cs > gpmc_cs_num) {
717 pr_err("%s: requested chip-select is disabled\n", __func__);
Imre Deakf37e4582006-09-25 12:41:33 +0300718 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530719 }
Imre Deakf37e4582006-09-25 12:41:33 +0300720 size = gpmc_mem_align(size);
721 if (size > (1 << GPMC_SECTION_SHIFT))
722 return -ENOMEM;
723
724 spin_lock(&gpmc_mem_lock);
725 if (gpmc_cs_reserved(cs)) {
726 r = -EBUSY;
727 goto out;
728 }
729 if (gpmc_cs_mem_enabled(cs))
730 r = adjust_resource(res, res->start & ~(size - 1), size);
731 if (r < 0)
732 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
733 size, NULL, NULL);
734 if (r < 0)
735 goto out;
736
Jon Hunterc71f8e92013-03-06 12:00:10 -0600737 r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
738 if (r < 0) {
739 release_resource(res);
740 goto out;
741 }
742
Imre Deakf37e4582006-09-25 12:41:33 +0300743 *base = res->start;
744 gpmc_cs_set_reserved(cs, 1);
745out:
746 spin_unlock(&gpmc_mem_lock);
747 return r;
748}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300749EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300750
751void gpmc_cs_free(int cs)
752{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800753 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
754 struct resource *res = &gpmc->mem;
Tony Lindgrenefe80722014-04-21 19:26:13 -0700755
Imre Deakf37e4582006-09-25 12:41:33 +0300756 spin_lock(&gpmc_mem_lock);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530757 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300758 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
759 BUG();
760 spin_unlock(&gpmc_mem_lock);
761 return;
762 }
763 gpmc_cs_disable_mem(cs);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700764 if (res->flags)
765 release_resource(res);
Imre Deakf37e4582006-09-25 12:41:33 +0300766 gpmc_cs_set_reserved(cs, 0);
767 spin_unlock(&gpmc_mem_lock);
768}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300769EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300770
vimal singh59e9c5a2009-07-13 16:26:24 +0530771/**
Jon Hunter3a544352013-02-21 13:00:21 -0600772 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000773 * @cmd: command type
774 * @wval: value to write
775 * @return status of the operation
776 */
Jon Hunter3a544352013-02-21 13:00:21 -0600777int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000778{
Jon Hunter3a544352013-02-21 13:00:21 -0600779 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000780
781 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530782 case GPMC_ENABLE_IRQ:
783 gpmc_write_reg(GPMC_IRQENABLE, wval);
784 break;
785
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000786 case GPMC_SET_IRQ_STATUS:
787 gpmc_write_reg(GPMC_IRQSTATUS, wval);
788 break;
789
790 case GPMC_CONFIG_WP:
791 regval = gpmc_read_reg(GPMC_CONFIG);
792 if (wval)
793 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
794 else
795 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
796 gpmc_write_reg(GPMC_CONFIG, regval);
797 break;
798
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000799 default:
Jon Hunter3a544352013-02-21 13:00:21 -0600800 pr_err("%s: command not supported\n", __func__);
801 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000802 }
803
Jon Hunter3a544352013-02-21 13:00:21 -0600804 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000805}
Jon Hunter3a544352013-02-21 13:00:21 -0600806EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000807
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700808void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
809{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530810 int i;
811
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700812 reg->gpmc_status = gpmc_base + GPMC_STATUS;
813 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
814 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
815 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
816 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
817 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
818 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
819 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
820 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
821 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
822 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
823 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
824 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
825 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
826 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530827
828 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
829 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
830 GPMC_BCH_SIZE * i;
831 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
832 GPMC_BCH_SIZE * i;
833 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
834 GPMC_BCH_SIZE * i;
835 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
836 GPMC_BCH_SIZE * i;
pekon gupta27c9fd62014-05-19 13:24:39 +0530837 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
838 i * GPMC_BCH_SIZE;
839 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
840 i * GPMC_BCH_SIZE;
841 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
842 i * GPMC_BCH_SIZE;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530843 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700844}
845
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700846int gpmc_get_client_irq(unsigned irq_config)
847{
848 int i;
849
850 if (hweight32(irq_config) > 1)
851 return 0;
852
853 for (i = 0; i < GPMC_NR_IRQ; i++)
854 if (gpmc_client_irq[i].bitmask & irq_config)
855 return gpmc_client_irq[i].irq;
856
857 return 0;
858}
859
860static int gpmc_irq_endis(unsigned irq, bool endis)
861{
862 int i;
863 u32 regval;
864
865 for (i = 0; i < GPMC_NR_IRQ; i++)
866 if (irq == gpmc_client_irq[i].irq) {
867 regval = gpmc_read_reg(GPMC_IRQENABLE);
868 if (endis)
869 regval |= gpmc_client_irq[i].bitmask;
870 else
871 regval &= ~gpmc_client_irq[i].bitmask;
872 gpmc_write_reg(GPMC_IRQENABLE, regval);
873 break;
874 }
875
876 return 0;
877}
878
879static void gpmc_irq_disable(struct irq_data *p)
880{
881 gpmc_irq_endis(p->irq, false);
882}
883
884static void gpmc_irq_enable(struct irq_data *p)
885{
886 gpmc_irq_endis(p->irq, true);
887}
888
889static void gpmc_irq_noop(struct irq_data *data) { }
890
891static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
892
Afzal Mohammedda496872012-09-23 17:28:25 -0600893static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700894{
895 int i;
896 u32 regval;
897
898 if (!gpmc_irq)
899 return -EINVAL;
900
901 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000902 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700903 pr_err("irq_alloc_descs failed\n");
904 return gpmc_irq_start;
905 }
906
907 gpmc_irq_chip.name = "gpmc";
908 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
909 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
910 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
911 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
912 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
913 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
914 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
915
916 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
917 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
918
919 for (i = 0; i < GPMC_NR_IRQ; i++) {
920 gpmc_client_irq[i].irq = gpmc_irq_start + i;
921 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
922 &gpmc_irq_chip, handle_simple_irq);
923 set_irq_flags(gpmc_client_irq[i].irq,
924 IRQF_VALID | IRQF_NOAUTOEN);
925 }
926
927 /* Disable interrupts */
928 gpmc_write_reg(GPMC_IRQENABLE, 0);
929
930 /* clear interrupts */
931 regval = gpmc_read_reg(GPMC_IRQSTATUS);
932 gpmc_write_reg(GPMC_IRQSTATUS, regval);
933
934 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
935}
936
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800937static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600938{
939 int i;
940
941 if (gpmc_irq)
942 free_irq(gpmc_irq, NULL);
943
944 for (i = 0; i < GPMC_NR_IRQ; i++) {
945 irq_set_handler(gpmc_client_irq[i].irq, NULL);
946 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
947 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
948 }
949
950 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
951
952 return 0;
953}
954
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800955static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600956{
957 int cs;
958
Gupta Pekonf34f3712013-05-31 17:31:30 +0530959 for (cs = 0; cs < gpmc_cs_num; cs++) {
Afzal Mohammedda496872012-09-23 17:28:25 -0600960 if (!gpmc_cs_mem_enabled(cs))
961 continue;
962 gpmc_cs_delete_mem(cs);
963 }
964
965}
966
Jon Hunter84b00f02013-03-06 14:36:47 -0600967static void gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300968{
Jon Hunter84b00f02013-03-06 14:36:47 -0600969 int cs;
Imre Deakf37e4582006-09-25 12:41:33 +0300970
Jon Hunterbf234392013-03-06 14:12:59 -0600971 /*
972 * The first 1MB of GPMC address space is typically mapped to
973 * the internal ROM. Never allocate the first page, to
974 * facilitate bug detection; even if we didn't boot from ROM.
Kyungmin Park7f245162006-12-29 16:48:51 -0800975 */
Jon Hunterbf234392013-03-06 14:12:59 -0600976 gpmc_mem_root.start = SZ_1M;
Imre Deakf37e4582006-09-25 12:41:33 +0300977 gpmc_mem_root.end = GPMC_MEM_END;
978
979 /* Reserve all regions that has been set up by bootloader */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530980 for (cs = 0; cs < gpmc_cs_num; cs++) {
Imre Deakf37e4582006-09-25 12:41:33 +0300981 u32 base, size;
982
983 if (!gpmc_cs_mem_enabled(cs))
984 continue;
985 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter84b00f02013-03-06 14:36:47 -0600986 if (gpmc_cs_insert_mem(cs, base, size)) {
987 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
988 __func__, cs, base, base + size);
989 gpmc_cs_disable_mem(cs);
Jon Hunter81190242012-10-17 09:41:25 -0500990 }
Imre Deakf37e4582006-09-25 12:41:33 +0300991 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700992}
993
Afzal Mohammed246da262012-08-02 20:02:10 +0530994static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
995{
996 u32 temp;
997 int div;
998
999 div = gpmc_calc_divider(sync_clk);
1000 temp = gpmc_ps_to_ticks(time_ps);
1001 temp = (temp + div - 1) / div;
1002 return gpmc_ticks_to_ps(temp * div);
1003}
1004
1005/* XXX: can the cycles be avoided ? */
1006static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001007 struct gpmc_device_timings *dev_t,
1008 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301009{
Afzal Mohammed246da262012-08-02 20:02:10 +05301010 u32 temp;
1011
1012 /* adv_rd_off */
1013 temp = dev_t->t_avdp_r;
1014 /* XXX: mux check required ? */
1015 if (mux) {
1016 /* XXX: t_avdp not to be required for sync, only added for tusb
1017 * this indirectly necessitates requirement of t_avdp_r and
1018 * t_avdp_w instead of having a single t_avdp
1019 */
1020 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
1021 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1022 }
1023 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1024
1025 /* oe_on */
1026 temp = dev_t->t_oeasu; /* XXX: remove this ? */
1027 if (mux) {
1028 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
1029 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
1030 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
1031 }
1032 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1033
1034 /* access */
1035 /* XXX: any scope for improvement ?, by combining oe_on
1036 * and clk_activation, need to check whether
1037 * access = clk_activation + round to sync clk ?
1038 */
1039 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
1040 temp += gpmc_t->clk_activation;
1041 if (dev_t->cyc_oe)
1042 temp = max_t(u32, temp, gpmc_t->oe_on +
1043 gpmc_ticks_to_ps(dev_t->cyc_oe));
1044 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1045
1046 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1047 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1048
1049 /* rd_cycle */
1050 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
1051 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
1052 gpmc_t->access;
1053 /* XXX: barter t_ce_rdyz with t_cez_r ? */
1054 if (dev_t->t_ce_rdyz)
1055 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
1056 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1057
1058 return 0;
1059}
1060
1061static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001062 struct gpmc_device_timings *dev_t,
1063 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301064{
Afzal Mohammed246da262012-08-02 20:02:10 +05301065 u32 temp;
1066
1067 /* adv_wr_off */
1068 temp = dev_t->t_avdp_w;
1069 if (mux) {
1070 temp = max_t(u32, temp,
1071 gpmc_t->clk_activation + dev_t->t_avdh);
1072 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1073 }
1074 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1075
1076 /* wr_data_mux_bus */
1077 temp = max_t(u32, dev_t->t_weasu,
1078 gpmc_t->clk_activation + dev_t->t_rdyo);
1079 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
1080 * and in that case remember to handle we_on properly
1081 */
1082 if (mux) {
1083 temp = max_t(u32, temp,
1084 gpmc_t->adv_wr_off + dev_t->t_aavdh);
1085 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1086 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1087 }
1088 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1089
1090 /* we_on */
1091 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1092 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1093 else
1094 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1095
1096 /* wr_access */
1097 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
1098 gpmc_t->wr_access = gpmc_t->access;
1099
1100 /* we_off */
1101 temp = gpmc_t->we_on + dev_t->t_wpl;
1102 temp = max_t(u32, temp,
1103 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
1104 temp = max_t(u32, temp,
1105 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
1106 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1107
1108 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1109 dev_t->t_wph);
1110
1111 /* wr_cycle */
1112 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
1113 temp += gpmc_t->wr_access;
1114 /* XXX: barter t_ce_rdyz with t_cez_w ? */
1115 if (dev_t->t_ce_rdyz)
1116 temp = max_t(u32, temp,
1117 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
1118 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1119
1120 return 0;
1121}
1122
1123static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001124 struct gpmc_device_timings *dev_t,
1125 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301126{
Afzal Mohammed246da262012-08-02 20:02:10 +05301127 u32 temp;
1128
1129 /* adv_rd_off */
1130 temp = dev_t->t_avdp_r;
1131 if (mux)
1132 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1133 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1134
1135 /* oe_on */
1136 temp = dev_t->t_oeasu;
1137 if (mux)
1138 temp = max_t(u32, temp,
1139 gpmc_t->adv_rd_off + dev_t->t_aavdh);
1140 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1141
1142 /* access */
1143 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1144 gpmc_t->oe_on + dev_t->t_oe);
1145 temp = max_t(u32, temp,
1146 gpmc_t->cs_on + dev_t->t_ce);
1147 temp = max_t(u32, temp,
1148 gpmc_t->adv_on + dev_t->t_aa);
1149 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1150
1151 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1152 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1153
1154 /* rd_cycle */
1155 temp = max_t(u32, dev_t->t_rd_cycle,
1156 gpmc_t->cs_rd_off + dev_t->t_cez_r);
1157 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1158 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1159
1160 return 0;
1161}
1162
1163static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001164 struct gpmc_device_timings *dev_t,
1165 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301166{
Afzal Mohammed246da262012-08-02 20:02:10 +05301167 u32 temp;
1168
1169 /* adv_wr_off */
1170 temp = dev_t->t_avdp_w;
1171 if (mux)
1172 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1173 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1174
1175 /* wr_data_mux_bus */
1176 temp = dev_t->t_weasu;
1177 if (mux) {
1178 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1179 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1180 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1181 }
1182 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1183
1184 /* we_on */
1185 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1186 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1187 else
1188 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1189
1190 /* we_off */
1191 temp = gpmc_t->we_on + dev_t->t_wpl;
1192 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1193
1194 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1195 dev_t->t_wph);
1196
1197 /* wr_cycle */
1198 temp = max_t(u32, dev_t->t_wr_cycle,
1199 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1200 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1201
1202 return 0;
1203}
1204
1205static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1206 struct gpmc_device_timings *dev_t)
1207{
1208 u32 temp;
1209
1210 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1211 gpmc_get_fclk_period();
1212
1213 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1214 dev_t->t_bacc,
1215 gpmc_t->sync_clk);
1216
1217 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1218 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1219
1220 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1221 return 0;
1222
1223 if (dev_t->ce_xdelay)
1224 gpmc_t->bool_timings.cs_extra_delay = true;
1225 if (dev_t->avd_xdelay)
1226 gpmc_t->bool_timings.adv_extra_delay = true;
1227 if (dev_t->oe_xdelay)
1228 gpmc_t->bool_timings.oe_extra_delay = true;
1229 if (dev_t->we_xdelay)
1230 gpmc_t->bool_timings.we_extra_delay = true;
1231
1232 return 0;
1233}
1234
1235static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001236 struct gpmc_device_timings *dev_t,
1237 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301238{
1239 u32 temp;
1240
1241 /* cs_on */
1242 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1243
1244 /* adv_on */
1245 temp = dev_t->t_avdasu;
1246 if (dev_t->t_ce_avd)
1247 temp = max_t(u32, temp,
1248 gpmc_t->cs_on + dev_t->t_ce_avd);
1249 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1250
Jon Hunterc3be5b42013-02-21 13:46:22 -06001251 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301252 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1253
1254 return 0;
1255}
1256
1257/* TODO: remove this function once all peripherals are confirmed to
1258 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1259 * has to be modified to handle timings in ps instead of ns
1260*/
1261static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1262{
1263 t->cs_on /= 1000;
1264 t->cs_rd_off /= 1000;
1265 t->cs_wr_off /= 1000;
1266 t->adv_on /= 1000;
1267 t->adv_rd_off /= 1000;
1268 t->adv_wr_off /= 1000;
1269 t->we_on /= 1000;
1270 t->we_off /= 1000;
1271 t->oe_on /= 1000;
1272 t->oe_off /= 1000;
1273 t->page_burst_access /= 1000;
1274 t->access /= 1000;
1275 t->rd_cycle /= 1000;
1276 t->wr_cycle /= 1000;
1277 t->bus_turnaround /= 1000;
1278 t->cycle2cycle_delay /= 1000;
1279 t->wait_monitoring /= 1000;
1280 t->clk_activation /= 1000;
1281 t->wr_access /= 1000;
1282 t->wr_data_mux_bus /= 1000;
1283}
1284
1285int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001286 struct gpmc_settings *gpmc_s,
1287 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301288{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001289 bool mux = false, sync = false;
1290
1291 if (gpmc_s) {
1292 mux = gpmc_s->mux_add_data ? true : false;
1293 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1294 }
1295
Afzal Mohammed246da262012-08-02 20:02:10 +05301296 memset(gpmc_t, 0, sizeof(*gpmc_t));
1297
Jon Hunterc3be5b42013-02-21 13:46:22 -06001298 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301299
Jon Hunterc3be5b42013-02-21 13:46:22 -06001300 if (gpmc_s && gpmc_s->sync_read)
1301 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301302 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001303 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301304
Jon Hunterc3be5b42013-02-21 13:46:22 -06001305 if (gpmc_s && gpmc_s->sync_write)
1306 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301307 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001308 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301309
1310 /* TODO: remove, see function definition */
1311 gpmc_convert_ps_to_ns(gpmc_t);
1312
1313 return 0;
1314}
1315
Jon Hunteraa8d4762013-02-21 15:25:23 -06001316/**
1317 * gpmc_cs_program_settings - programs non-timing related settings
1318 * @cs: GPMC chip-select to program
1319 * @p: pointer to GPMC settings structure
1320 *
1321 * Programs non-timing related settings for a GPMC chip-select, such as
1322 * bus-width, burst configuration, etc. Function should be called once
1323 * for each chip-select that is being used and must be called before
1324 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1325 * register will be initialised to zero by this function. Returns 0 on
1326 * success and appropriate negative error code on failure.
1327 */
1328int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1329{
1330 u32 config1;
1331
1332 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1333 pr_err("%s: invalid width %d!", __func__, p->device_width);
1334 return -EINVAL;
1335 }
1336
1337 /* Address-data multiplexing not supported for NAND devices */
1338 if (p->device_nand && p->mux_add_data) {
1339 pr_err("%s: invalid configuration!\n", __func__);
1340 return -EINVAL;
1341 }
1342
1343 if ((p->mux_add_data > GPMC_MUX_AD) ||
1344 ((p->mux_add_data == GPMC_MUX_AAD) &&
1345 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1346 pr_err("%s: invalid multiplex configuration!\n", __func__);
1347 return -EINVAL;
1348 }
1349
1350 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1351 if (p->burst_read || p->burst_write) {
1352 switch (p->burst_len) {
1353 case GPMC_BURST_4:
1354 case GPMC_BURST_8:
1355 case GPMC_BURST_16:
1356 break;
1357 default:
1358 pr_err("%s: invalid page/burst-length (%d)\n",
1359 __func__, p->burst_len);
1360 return -EINVAL;
1361 }
1362 }
1363
Roger Quadros2b540572014-09-02 16:57:06 +03001364 if (p->wait_pin > gpmc_nr_waitpins) {
Jon Hunteraa8d4762013-02-21 15:25:23 -06001365 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1366 return -EINVAL;
1367 }
1368
1369 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1370
1371 if (p->sync_read)
1372 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1373 if (p->sync_write)
1374 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1375 if (p->wait_on_read)
1376 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1377 if (p->wait_on_write)
1378 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1379 if (p->wait_on_read || p->wait_on_write)
1380 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1381 if (p->device_nand)
1382 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1383 if (p->mux_add_data)
1384 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1385 if (p->burst_read)
1386 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1387 if (p->burst_write)
1388 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1389 if (p->burst_read || p->burst_write) {
1390 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1391 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1392 }
1393
1394 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1395
1396 return 0;
1397}
1398
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001399#ifdef CONFIG_OF
Uwe Kleine-König31957602014-09-10 10:26:17 +02001400static const struct of_device_id gpmc_dt_ids[] = {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001401 { .compatible = "ti,omap2420-gpmc" },
1402 { .compatible = "ti,omap2430-gpmc" },
1403 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1404 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1405 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1406 { }
1407};
1408MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1409
Jon Hunter8c8a77712013-02-20 15:53:12 -06001410/**
1411 * gpmc_read_settings_dt - read gpmc settings from device-tree
1412 * @np: pointer to device-tree node for a gpmc child device
1413 * @p: pointer to gpmc settings structure
1414 *
1415 * Reads the GPMC settings for a GPMC child device from device-tree and
1416 * stores them in the GPMC settings structure passed. The GPMC settings
1417 * structure is initialised to zero by this function and so any
1418 * previously stored settings will be cleared.
1419 */
1420void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1421{
1422 memset(p, 0, sizeof(struct gpmc_settings));
1423
1424 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1425 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001426 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1427 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1428
1429 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1430 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1431 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1432 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1433 if (!p->burst_read && !p->burst_write)
1434 pr_warn("%s: page/burst-length set but not used!\n",
1435 __func__);
1436 }
1437
1438 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1439 p->wait_on_read = of_property_read_bool(np,
1440 "gpmc,wait-on-read");
1441 p->wait_on_write = of_property_read_bool(np,
1442 "gpmc,wait-on-write");
1443 if (!p->wait_on_read && !p->wait_on_write)
Roger Quadros2b540572014-09-02 16:57:06 +03001444 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1445 __func__);
Jon Hunter8c8a77712013-02-20 15:53:12 -06001446 }
1447}
1448
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001449static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1450 struct gpmc_timings *gpmc_t)
1451{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001452 struct gpmc_bool_timings *p;
1453
1454 if (!np || !gpmc_t)
1455 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001456
1457 memset(gpmc_t, 0, sizeof(*gpmc_t));
1458
1459 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001460 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001461
1462 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001463 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1464 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1465 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001466
1467 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001468 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1469 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1470 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001471
1472 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001473 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1474 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001475
1476 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001477 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1478 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001479
1480 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001481 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1482 &gpmc_t->page_burst_access);
1483 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1484 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1485 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1486 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1487 &gpmc_t->bus_turnaround);
1488 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1489 &gpmc_t->cycle2cycle_delay);
1490 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1491 &gpmc_t->wait_monitoring);
1492 of_property_read_u32(np, "gpmc,clk-activation-ns",
1493 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001494
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001495 /* only applicable to OMAP3+ */
1496 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1497 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1498 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001499
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001500 /* bool timing parameters */
1501 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001502
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001503 p->cycle2cyclediffcsen =
1504 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1505 p->cycle2cyclesamecsen =
1506 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1507 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1508 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1509 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1510 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1511 p->time_para_granularity =
1512 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001513}
1514
Pekon Gupta6b187b22014-01-28 11:42:40 +05301515#if IS_ENABLED(CONFIG_MTD_NAND)
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001516
Mark Jackson496c8a02013-04-19 21:08:28 +01001517static const char * const nand_xfer_types[] = {
1518 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1519 [NAND_OMAP_POLLED] = "polled",
1520 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1521 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1522};
1523
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001524static int gpmc_probe_nand_child(struct platform_device *pdev,
1525 struct device_node *child)
1526{
1527 u32 val;
1528 const char *s;
1529 struct gpmc_timings gpmc_t;
1530 struct omap_nand_platform_data *gpmc_nand_data;
1531
1532 if (of_property_read_u32(child, "reg", &val) < 0) {
1533 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1534 child->full_name);
1535 return -ENODEV;
1536 }
1537
1538 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1539 GFP_KERNEL);
1540 if (!gpmc_nand_data)
1541 return -ENOMEM;
1542
1543 gpmc_nand_data->cs = val;
1544 gpmc_nand_data->of_node = child;
1545
Pekon Guptaac65caf52013-10-24 18:20:17 +05301546 /* Detect availability of ELM module */
1547 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1548 if (gpmc_nand_data->elm_of_node == NULL)
1549 gpmc_nand_data->elm_of_node =
1550 of_parse_phandle(child, "elm_id", 0);
1551 if (gpmc_nand_data->elm_of_node == NULL)
1552 pr_warn("%s: ti,elm-id property not found\n", __func__);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001553
Pekon Guptaac65caf52013-10-24 18:20:17 +05301554 /* select ecc-scheme for NAND */
1555 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1556 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1557 return -ENODEV;
1558 }
Roger Quadrosa3e83f02014-08-25 16:15:33 -07001559
1560 if (!strcmp(s, "sw"))
1561 gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1562 else if (!strcmp(s, "ham1") ||
1563 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
Pekon Guptaac65caf52013-10-24 18:20:17 +05301564 gpmc_nand_data->ecc_opt =
1565 OMAP_ECC_HAM1_CODE_HW;
1566 else if (!strcmp(s, "bch4"))
1567 if (gpmc_nand_data->elm_of_node)
1568 gpmc_nand_data->ecc_opt =
1569 OMAP_ECC_BCH4_CODE_HW;
1570 else
1571 gpmc_nand_data->ecc_opt =
1572 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1573 else if (!strcmp(s, "bch8"))
1574 if (gpmc_nand_data->elm_of_node)
1575 gpmc_nand_data->ecc_opt =
1576 OMAP_ECC_BCH8_CODE_HW;
1577 else
1578 gpmc_nand_data->ecc_opt =
1579 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
pekon gupta27c9fd62014-05-19 13:24:39 +05301580 else if (!strcmp(s, "bch16"))
1581 if (gpmc_nand_data->elm_of_node)
1582 gpmc_nand_data->ecc_opt =
1583 OMAP_ECC_BCH16_CODE_HW;
1584 else
1585 pr_err("%s: BCH16 requires ELM support\n", __func__);
Pekon Guptaac65caf52013-10-24 18:20:17 +05301586 else
1587 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1588
1589 /* select data transfer mode for NAND controller */
Mark Jackson496c8a02013-04-19 21:08:28 +01001590 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1591 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1592 if (!strcasecmp(s, nand_xfer_types[val])) {
1593 gpmc_nand_data->xfer_type = val;
1594 break;
1595 }
1596
Ezequiel Garcíafef775c2014-09-11 12:02:08 -03001597 gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
1598
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001599 val = of_get_nand_bus_width(child);
1600 if (val == 16)
1601 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1602
1603 gpmc_read_timings_dt(child, &gpmc_t);
1604 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1605
1606 return 0;
1607}
1608#else
1609static int gpmc_probe_nand_child(struct platform_device *pdev,
1610 struct device_node *child)
1611{
1612 return 0;
1613}
1614#endif
1615
Pekon Gupta980386d2014-01-28 11:42:41 +05301616#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001617static int gpmc_probe_onenand_child(struct platform_device *pdev,
1618 struct device_node *child)
1619{
1620 u32 val;
1621 struct omap_onenand_platform_data *gpmc_onenand_data;
1622
1623 if (of_property_read_u32(child, "reg", &val) < 0) {
1624 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1625 child->full_name);
1626 return -ENODEV;
1627 }
1628
1629 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1630 GFP_KERNEL);
1631 if (!gpmc_onenand_data)
1632 return -ENOMEM;
1633
1634 gpmc_onenand_data->cs = val;
1635 gpmc_onenand_data->of_node = child;
1636 gpmc_onenand_data->dma_channel = -1;
1637
1638 if (!of_property_read_u32(child, "dma-channel", &val))
1639 gpmc_onenand_data->dma_channel = val;
1640
1641 gpmc_onenand_init(gpmc_onenand_data);
1642
1643 return 0;
1644}
1645#else
1646static int gpmc_probe_onenand_child(struct platform_device *pdev,
1647 struct device_node *child)
1648{
1649 return 0;
1650}
1651#endif
1652
Jon Huntercdd69282013-02-08 16:46:13 -06001653/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001654 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001655 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001656 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001657 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001658 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001659 * Returns 0 on success and appropriate negative error code on failure.
1660 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001661static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001662 struct device_node *child)
1663{
1664 struct gpmc_settings gpmc_s;
1665 struct gpmc_timings gpmc_t;
1666 struct resource res;
1667 unsigned long base;
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001668 const char *name;
Jon Huntercdd69282013-02-08 16:46:13 -06001669 int ret, cs;
1670
1671 if (of_property_read_u32(child, "reg", &cs) < 0) {
1672 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1673 child->full_name);
1674 return -ENODEV;
1675 }
1676
1677 if (of_address_to_resource(child, 0, &res) < 0) {
1678 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1679 child->full_name);
1680 return -ENODEV;
1681 }
1682
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001683 /*
1684 * Check if we have multiple instances of the same device
1685 * on a single chip select. If so, use the already initialized
1686 * timings.
1687 */
1688 name = gpmc_cs_get_name(cs);
1689 if (name && child->name && of_node_cmp(child->name, name) == 0)
1690 goto no_timings;
1691
Jon Huntercdd69282013-02-08 16:46:13 -06001692 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1693 if (ret < 0) {
1694 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1695 return ret;
1696 }
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001697 gpmc_cs_set_name(cs, child->name);
Jon Huntercdd69282013-02-08 16:46:13 -06001698
Tony Lindgren35ac0512014-11-03 17:45:01 -08001699 gpmc_read_settings_dt(child, &gpmc_s);
1700 gpmc_read_timings_dt(child, &gpmc_t);
1701
1702 /*
1703 * For some GPMC devices we still need to rely on the bootloader
1704 * timings because the devices can be connected via FPGA.
1705 * REVISIT: Add timing support from slls644g.pdf.
1706 */
1707 if (!gpmc_t.cs_rd_off) {
1708 WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
1709 cs);
1710 gpmc_cs_show_timings(cs,
1711 "please add GPMC bootloader timings to .dts");
1712 goto no_timings;
1713 }
1714
Jon Huntercdd69282013-02-08 16:46:13 -06001715 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001716 * For some GPMC devices we still need to rely on the bootloader
1717 * timings because the devices can be connected via FPGA. So far
1718 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1719 * REVISIT: Add timing support from slls644g.pdf and from the
1720 * lan91c96 manual.
1721 */
1722 if (of_device_is_compatible(child, "ns16550a") ||
1723 of_device_is_compatible(child, "smsc,lan91c94") ||
1724 of_device_is_compatible(child, "smsc,lan91c111")) {
1725 dev_warn(&pdev->dev,
1726 "%s using bootloader timings on CS%d\n",
1727 child->name, cs);
1728 goto no_timings;
1729 }
1730
1731 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001732 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1733 * location in the gpmc address space. When booting with
1734 * device-tree we want the NOR flash to be mapped to the
1735 * location specified in the device-tree blob. So remap the
1736 * CS to this location. Once DT migration is complete should
1737 * just make gpmc_cs_request() map a specific address.
1738 */
1739 ret = gpmc_cs_remap(cs, res.start);
1740 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001741 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1742 cs, &res.start);
Jon Huntercdd69282013-02-08 16:46:13 -06001743 goto err;
1744 }
1745
Jon Huntercdd69282013-02-08 16:46:13 -06001746 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1747 if (ret < 0)
1748 goto err;
1749
1750 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1751 if (ret < 0)
1752 goto err;
1753
Jon Huntercdd69282013-02-08 16:46:13 -06001754 gpmc_cs_set_timings(cs, &gpmc_t);
1755
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001756no_timings:
Jon Huntercdd69282013-02-08 16:46:13 -06001757 if (of_platform_device_create(child, NULL, &pdev->dev))
1758 return 0;
1759
1760 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01001761 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06001762
1763err:
1764 gpmc_cs_free(cs);
1765
1766 return ret;
1767}
1768
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001769static int gpmc_probe_dt(struct platform_device *pdev)
1770{
1771 int ret;
1772 struct device_node *child;
1773 const struct of_device_id *of_id =
1774 of_match_device(gpmc_dt_ids, &pdev->dev);
1775
1776 if (!of_id)
1777 return 0;
1778
Gupta Pekonf34f3712013-05-31 17:31:30 +05301779 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1780 &gpmc_cs_num);
1781 if (ret < 0) {
1782 pr_err("%s: number of chip-selects not defined\n", __func__);
1783 return ret;
1784 } else if (gpmc_cs_num < 1) {
1785 pr_err("%s: all chip-selects are disabled\n", __func__);
1786 return -EINVAL;
1787 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1788 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1789 __func__, GPMC_CS_NUM);
1790 return -EINVAL;
1791 }
1792
Jon Hunter9f833152013-02-20 15:53:38 -06001793 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1794 &gpmc_nr_waitpins);
1795 if (ret < 0) {
1796 pr_err("%s: number of wait pins not found!\n", __func__);
1797 return ret;
1798 }
1799
Guido Martínez68e2eb52014-07-02 10:35:18 -03001800 for_each_available_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001801
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001802 if (!child->name)
1803 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06001804
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001805 if (of_node_cmp(child->name, "nand") == 0)
1806 ret = gpmc_probe_nand_child(pdev, child);
1807 else if (of_node_cmp(child->name, "onenand") == 0)
1808 ret = gpmc_probe_onenand_child(pdev, child);
1809 else if (of_node_cmp(child->name, "ethernet") == 0 ||
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001810 of_node_cmp(child->name, "nor") == 0 ||
1811 of_node_cmp(child->name, "uart") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001812 ret = gpmc_probe_generic_child(pdev, child);
Jon Huntercdd69282013-02-08 16:46:13 -06001813
Javier Martinez Canillasb327b362013-04-17 22:34:12 +02001814 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1815 __func__, child->full_name))
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001816 of_node_put(child);
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001817 }
1818
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001819 return 0;
1820}
1821#else
1822static int gpmc_probe_dt(struct platform_device *pdev)
1823{
1824 return 0;
1825}
1826#endif
1827
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001828static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001829{
Jon Hunter81190242012-10-17 09:41:25 -05001830 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001831 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001832 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001833
Afzal Mohammedda496872012-09-23 17:28:25 -06001834 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1835 if (res == NULL)
1836 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001837
Afzal Mohammedda496872012-09-23 17:28:25 -06001838 phys_base = res->start;
1839 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001840
Thierry Reding5857bd92013-01-21 11:08:55 +01001841 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1842 if (IS_ERR(gpmc_base))
1843 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001844
1845 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1846 if (res == NULL)
1847 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1848 else
1849 gpmc_irq = res->start;
1850
1851 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1852 if (IS_ERR(gpmc_l3_clk)) {
1853 dev_err(&pdev->dev, "error: clk_get\n");
1854 gpmc_irq = 0;
1855 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001856 }
1857
avinash philipb3f55252013-06-12 16:30:56 +05301858 pm_runtime_enable(&pdev->dev);
1859 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001860
Afzal Mohammedda496872012-09-23 17:28:25 -06001861 gpmc_dev = &pdev->dev;
1862
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001863 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001864
1865 /*
1866 * FIXME: Once device-tree migration is complete the below flags
1867 * should be populated based upon the device-tree compatible
1868 * string. For now just use the IP revision. OMAP3+ devices have
1869 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1870 * devices support the addr-addr-data multiplex protocol.
1871 *
1872 * GPMC IP revisions:
1873 * - OMAP24xx = 2.0
1874 * - OMAP3xxx = 5.0
1875 * - OMAP44xx/54xx/AM335x = 6.0
1876 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001877 if (GPMC_REVISION_MAJOR(l) > 0x4)
1878 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001879 if (GPMC_REVISION_MAJOR(l) > 0x5)
1880 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001881 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1882 GPMC_REVISION_MINOR(l));
1883
Jon Hunter84b00f02013-03-06 14:36:47 -06001884 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301885
Russell King71856842013-03-13 20:44:21 +00001886 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001887 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1888
Gupta Pekonf34f3712013-05-31 17:31:30 +05301889 if (!pdev->dev.of_node) {
1890 gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -06001891 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
Gupta Pekonf34f3712013-05-31 17:31:30 +05301892 }
Jon Hunter9f833152013-02-20 15:53:38 -06001893
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001894 rc = gpmc_probe_dt(pdev);
1895 if (rc < 0) {
avinash philipb3f55252013-06-12 16:30:56 +05301896 pm_runtime_put_sync(&pdev->dev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001897 clk_put(gpmc_l3_clk);
1898 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1899 return rc;
1900 }
1901
Afzal Mohammedda496872012-09-23 17:28:25 -06001902 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301903}
Afzal Mohammedda496872012-09-23 17:28:25 -06001904
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001905static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001906{
1907 gpmc_free_irq();
1908 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05301909 pm_runtime_put_sync(&pdev->dev);
1910 pm_runtime_disable(&pdev->dev);
Afzal Mohammedda496872012-09-23 17:28:25 -06001911 gpmc_dev = NULL;
1912 return 0;
1913}
1914
avinash philipb536dd42013-06-18 00:16:38 +05301915#ifdef CONFIG_PM_SLEEP
1916static int gpmc_suspend(struct device *dev)
1917{
1918 omap3_gpmc_save_context();
1919 pm_runtime_put_sync(dev);
1920 return 0;
1921}
1922
1923static int gpmc_resume(struct device *dev)
1924{
1925 pm_runtime_get_sync(dev);
1926 omap3_gpmc_restore_context();
1927 return 0;
1928}
1929#endif
1930
1931static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1932
Afzal Mohammedda496872012-09-23 17:28:25 -06001933static struct platform_driver gpmc_driver = {
1934 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001935 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001936 .driver = {
1937 .name = DEVICE_NAME,
1938 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001939 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05301940 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06001941 },
1942};
1943
1944static __init int gpmc_init(void)
1945{
1946 return platform_driver_register(&gpmc_driver);
1947}
1948
1949static __exit void gpmc_exit(void)
1950{
1951 platform_driver_unregister(&gpmc_driver);
1952
1953}
1954
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001955omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001956module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301957
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001958static int __init omap_gpmc_init(void)
1959{
1960 struct omap_hwmod *oh;
1961 struct platform_device *pdev;
1962 char *oh_name = "gpmc";
1963
Daniel Mack2f98ca82012-12-14 11:36:40 +01001964 /*
1965 * if the board boots up with a populated DT, do not
1966 * manually add the device from this initcall
1967 */
1968 if (of_have_populated_dt())
1969 return -ENODEV;
1970
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001971 oh = omap_hwmod_lookup(oh_name);
1972 if (!oh) {
1973 pr_err("Could not look up %s\n", oh_name);
1974 return -ENODEV;
1975 }
1976
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001977 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001978 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1979
Thomas Meyer12616742013-06-01 11:44:44 +02001980 return PTR_RET(pdev);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001981}
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001982omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001983
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301984static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1985{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001986 int i;
1987 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301988
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001989 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1990
1991 if (!regval)
1992 return IRQ_NONE;
1993
1994 for (i = 0; i < GPMC_NR_IRQ; i++)
1995 if (regval & gpmc_client_irq[i].bitmask)
1996 generic_handle_irq(gpmc_client_irq[i].irq);
1997
1998 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301999
2000 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07002001}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302002
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302003static struct omap3_gpmc_regs gpmc_context;
2004
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002005void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302006{
2007 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002008
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302009 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
2010 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
2011 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
2012 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
2013 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
2014 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
2015 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05302016 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302017 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
2018 if (gpmc_context.cs_context[i].is_valid) {
2019 gpmc_context.cs_context[i].config1 =
2020 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
2021 gpmc_context.cs_context[i].config2 =
2022 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
2023 gpmc_context.cs_context[i].config3 =
2024 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
2025 gpmc_context.cs_context[i].config4 =
2026 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
2027 gpmc_context.cs_context[i].config5 =
2028 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
2029 gpmc_context.cs_context[i].config6 =
2030 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
2031 gpmc_context.cs_context[i].config7 =
2032 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
2033 }
2034 }
2035}
2036
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002037void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302038{
2039 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002040
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302041 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
2042 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
2043 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
2044 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
2045 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
2046 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
2047 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05302048 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302049 if (gpmc_context.cs_context[i].is_valid) {
2050 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
2051 gpmc_context.cs_context[i].config1);
2052 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
2053 gpmc_context.cs_context[i].config2);
2054 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
2055 gpmc_context.cs_context[i].config3);
2056 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
2057 gpmc_context.cs_context[i].config4);
2058 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
2059 gpmc_context.cs_context[i].config5);
2060 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
2061 gpmc_context.cs_context[i].config6);
2062 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
2063 gpmc_context.cs_context[i].config7);
2064 }
2065 }
2066}