blob: 104bc2c5098761228d93b7079c718c8ca36114bd [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
Roger Quadrose378d222014-08-29 19:11:52 +030088#define GPMC_CONFIG_LIMITEDADDRESS BIT(1)
89
Afzal Mohammed559d94b2012-05-28 17:51:37 +053090#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
91#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
92#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
93#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
94#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
95#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
96
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000097#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070098#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053099#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700100
Imre Deakf37e4582006-09-25 12:41:33 +0300101#define GPMC_MEM_END 0x3FFFFFFF
Imre Deakf37e4582006-09-25 12:41:33 +0300102
103#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
104#define GPMC_SECTION_SHIFT 28 /* 128 MB */
105
vimal singh59e9c5a2009-07-13 16:26:24 +0530106#define CS_NUM_SHIFT 24
107#define ENABLE_PREFETCH (0x1 << 7)
108#define DMA_MPU_MODE 2
109
Afzal Mohammedda496872012-09-23 17:28:25 -0600110#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
111#define GPMC_REVISION_MINOR(l) (l & 0xf)
112
113#define GPMC_HAS_WR_ACCESS 0x1
114#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
Jon Hunteraa8d4762013-02-21 15:25:23 -0600115#define GPMC_HAS_MUX_AAD 0x4
Afzal Mohammedda496872012-09-23 17:28:25 -0600116
Jon Hunter9f833152013-02-20 15:53:38 -0600117#define GPMC_NR_WAITPINS 4
118
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700119/* XXX: Only NAND irq has been considered,currently these are the only ones used
120 */
121#define GPMC_NR_IRQ 2
122
123struct gpmc_client_irq {
124 unsigned irq;
125 u32 bitmask;
126};
127
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530128/* Structure to save gpmc cs context */
129struct gpmc_cs_config {
130 u32 config1;
131 u32 config2;
132 u32 config3;
133 u32 config4;
134 u32 config5;
135 u32 config6;
136 u32 config7;
137 int is_valid;
138};
139
140/*
141 * Structure to save/restore gpmc context
142 * to support core off on OMAP3
143 */
144struct omap3_gpmc_regs {
145 u32 sysconfig;
146 u32 irqenable;
147 u32 timeout_ctrl;
148 u32 config;
149 u32 prefetch_config1;
150 u32 prefetch_config2;
151 u32 prefetch_control;
152 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
153};
154
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700155static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
156static struct irq_chip gpmc_irq_chip;
Chen Gangaf072192013-08-22 15:47:21 +0800157static int gpmc_irq_start;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700158
Imre Deakf37e4582006-09-25 12:41:33 +0300159static struct resource gpmc_mem_root;
160static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700161static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600162/* Define chip-selects as reserved by default until probe completes */
163static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530164static unsigned int gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -0600165static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600166static struct device *gpmc_dev;
167static int gpmc_irq;
168static resource_size_t phys_base, mem_size;
169static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300170static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700171
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300172static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700173
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530174static irqreturn_t gpmc_handle_irq(int irq, void *dev);
175
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700176static void gpmc_write_reg(int idx, u32 val)
177{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300178 writel_relaxed(val, gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700179}
180
181static u32 gpmc_read_reg(int idx)
182{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300183 return readl_relaxed(gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700184}
185
186void gpmc_cs_write_reg(int cs, int idx, u32 val)
187{
188 void __iomem *reg_addr;
189
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000190 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300191 writel_relaxed(val, reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700192}
193
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300194static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700195{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300196 void __iomem *reg_addr;
197
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000198 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300199 return readl_relaxed(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700200}
201
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300202/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300203static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700204{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300205 unsigned long rate = clk_get_rate(gpmc_l3_clk);
206
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300207 rate /= 1000;
208 rate = 1000000000 / rate; /* In picoseconds */
209
210 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700211}
212
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300213static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700214{
215 unsigned long tick_ps;
216
217 /* Calculate in picosecs to yield more exact results */
218 tick_ps = gpmc_get_fclk_period();
219
220 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
221}
222
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300223static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200224{
225 unsigned long tick_ps;
226
227 /* Calculate in picosecs to yield more exact results */
228 tick_ps = gpmc_get_fclk_period();
229
230 return (time_ps + tick_ps - 1) / tick_ps;
231}
232
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300233unsigned int gpmc_ticks_to_ns(unsigned int ticks)
234{
235 return ticks * gpmc_get_fclk_period() / 1000;
236}
237
Afzal Mohammed246da262012-08-02 20:02:10 +0530238static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
239{
240 return ticks * gpmc_get_fclk_period();
241}
242
243static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
244{
245 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
246
247 return ticks * gpmc_get_fclk_period();
248}
249
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530250static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
251{
252 u32 l;
253
254 l = gpmc_cs_read_reg(cs, reg);
255 if (value)
256 l |= mask;
257 else
258 l &= ~mask;
259 gpmc_cs_write_reg(cs, reg, l);
260}
261
262static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
263{
264 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
265 GPMC_CONFIG1_TIME_PARA_GRAN,
266 p->time_para_granularity);
267 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
268 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
269 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
270 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
271 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
272 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
273 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
274 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
275 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
276 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
277 p->cycle2cyclesamecsen);
278 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
279 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
280 p->cycle2cyclediffcsen);
281}
282
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700283static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700284 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700285{
286 u32 l;
287 int ticks, mask, nr_bits;
288
289 if (time == 0)
290 ticks = 0;
291 else
292 ticks = gpmc_ns_to_ticks(time);
293 nr_bits = end_bit - st_bit + 1;
Roger Quadros80323742014-08-29 19:11:50 +0300294 mask = (1 << nr_bits) - 1;
295
296 if (ticks > mask) {
297 pr_err("%s: GPMC error! CS%d: %s: %d ns, %d ticks > %d\n",
298 __func__, cs, name, time, ticks, mask);
299
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700300 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800301 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700302
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700303 l = gpmc_cs_read_reg(cs, reg);
304#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800305 printk(KERN_INFO
306 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700307 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800308 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700309#endif
310 l &= ~(mask << st_bit);
311 l |= ticks << st_bit;
312 gpmc_cs_write_reg(cs, reg, l);
313
314 return 0;
315}
316
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700317#define GPMC_SET_ONE(reg, st, end, field) \
318 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
319 t->field, #field) < 0) \
320 return -1
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700321
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530322int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700323{
324 int div;
325 u32 l;
326
Adrian Huntera3551f52010-12-09 10:48:27 +0200327 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700328 div = l / gpmc_get_fclk_period();
329 if (div > 4)
330 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800331 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700332 div = 1;
333
334 return div;
335}
336
337int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
338{
339 int div;
340 u32 l;
341
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530342 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700343 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600344 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700345
346 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
347 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
348 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
349
350 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
351 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
352 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
353
354 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
355 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
356 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
357 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
358
359 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
360 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
361 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
362
363 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
364
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530365 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
366 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
367
368 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
369 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
370
Afzal Mohammedda496872012-09-23 17:28:25 -0600371 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300372 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600373 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300374 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300375
David Brownell1c22cc12006-12-06 17:13:55 -0800376 /* caller is expected to have initialized CONFIG1 to cover
377 * at least sync vs async
378 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700379 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800380 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
381#ifdef DEBUG
382 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
383 cs, (div * gpmc_get_fclk_period()) / 1000, div);
384#endif
385 l &= ~0x03;
386 l |= (div - 1);
387 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
388 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700389
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530390 gpmc_cs_bool_timings(cs, &t->bool_timings);
391
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700392 return 0;
393}
394
Roger Quadros4cf27d22014-08-29 19:11:53 +0300395static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700396{
Imre Deakf37e4582006-09-25 12:41:33 +0300397 u32 l;
398 u32 mask;
399
Jon Hunterc71f8e92013-03-06 12:00:10 -0600400 /*
401 * Ensure that base address is aligned on a
402 * boundary equal to or greater than size.
403 */
404 if (base & (size - 1))
405 return -EINVAL;
406
Imre Deakf37e4582006-09-25 12:41:33 +0300407 mask = (1 << GPMC_SECTION_SHIFT) - size;
408 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
409 l &= ~0x3f;
410 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
411 l &= ~(0x0f << 8);
412 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530413 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300414 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600415
416 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300417}
418
Roger Quadros4cf27d22014-08-29 19:11:53 +0300419static void gpmc_cs_enable_mem(int cs)
420{
421 u32 l;
422
423 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
424 l |= GPMC_CONFIG7_CSVALID;
425 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
426}
427
Imre Deakf37e4582006-09-25 12:41:33 +0300428static void gpmc_cs_disable_mem(int cs)
429{
430 u32 l;
431
432 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530433 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300434 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
435}
436
437static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
438{
439 u32 l;
440 u32 mask;
441
442 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
443 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
444 mask = (l >> 8) & 0x0f;
445 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
446}
447
448static int gpmc_cs_mem_enabled(int cs)
449{
450 u32 l;
451
452 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530453 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300454}
455
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300456static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300457{
458 gpmc_cs_map &= ~(1 << cs);
459 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
460}
461
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300462static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300463{
464 return gpmc_cs_map & (1 << cs);
465}
466
467static unsigned long gpmc_mem_align(unsigned long size)
468{
469 int order;
470
471 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
472 order = GPMC_CHUNK_SHIFT - 1;
473 do {
474 size >>= 1;
475 order++;
476 } while (size);
477 size = 1 << order;
478 return size;
479}
480
481static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
482{
483 struct resource *res = &gpmc_cs_mem[cs];
484 int r;
485
486 size = gpmc_mem_align(size);
487 spin_lock(&gpmc_mem_lock);
488 res->start = base;
489 res->end = base + size - 1;
490 r = request_resource(&gpmc_mem_root, res);
491 spin_unlock(&gpmc_mem_lock);
492
493 return r;
494}
495
Afzal Mohammedda496872012-09-23 17:28:25 -0600496static int gpmc_cs_delete_mem(int cs)
497{
498 struct resource *res = &gpmc_cs_mem[cs];
499 int r;
500
501 spin_lock(&gpmc_mem_lock);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700502 r = release_resource(res);
Afzal Mohammedda496872012-09-23 17:28:25 -0600503 res->start = 0;
504 res->end = 0;
505 spin_unlock(&gpmc_mem_lock);
506
507 return r;
508}
509
Jon Huntercdd69282013-02-08 16:46:13 -0600510/**
511 * gpmc_cs_remap - remaps a chip-select physical base address
512 * @cs: chip-select to remap
513 * @base: physical base address to re-map chip-select to
514 *
515 * Re-maps a chip-select to a new physical base address specified by
516 * "base". Returns 0 on success and appropriate negative error code
517 * on failure.
518 */
519static int gpmc_cs_remap(int cs, u32 base)
520{
521 int ret;
522 u32 old_base, size;
523
Gupta Pekonf34f3712013-05-31 17:31:30 +0530524 if (cs > gpmc_cs_num) {
525 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600526 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530527 }
Tony Lindgrenfb677ef2014-04-21 19:26:13 -0700528
529 /*
530 * Make sure we ignore any device offsets from the GPMC partition
531 * allocated for the chip select and that the new base confirms
532 * to the GPMC 16MB minimum granularity.
533 */
534 base &= ~(SZ_16M - 1);
535
Jon Huntercdd69282013-02-08 16:46:13 -0600536 gpmc_cs_get_memconf(cs, &old_base, &size);
537 if (base == old_base)
538 return 0;
Roger Quadros4cf27d22014-08-29 19:11:53 +0300539
Jon Huntercdd69282013-02-08 16:46:13 -0600540 ret = gpmc_cs_delete_mem(cs);
541 if (ret < 0)
542 return ret;
Roger Quadros4cf27d22014-08-29 19:11:53 +0300543
Jon Huntercdd69282013-02-08 16:46:13 -0600544 ret = gpmc_cs_insert_mem(cs, base, size);
545 if (ret < 0)
546 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600547
Roger Quadros4cf27d22014-08-29 19:11:53 +0300548 ret = gpmc_cs_set_memconf(cs, base, size);
549
550 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600551}
552
Imre Deakf37e4582006-09-25 12:41:33 +0300553int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
554{
555 struct resource *res = &gpmc_cs_mem[cs];
556 int r = -1;
557
Gupta Pekonf34f3712013-05-31 17:31:30 +0530558 if (cs > gpmc_cs_num) {
559 pr_err("%s: requested chip-select is disabled\n", __func__);
Imre Deakf37e4582006-09-25 12:41:33 +0300560 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530561 }
Imre Deakf37e4582006-09-25 12:41:33 +0300562 size = gpmc_mem_align(size);
563 if (size > (1 << GPMC_SECTION_SHIFT))
564 return -ENOMEM;
565
566 spin_lock(&gpmc_mem_lock);
567 if (gpmc_cs_reserved(cs)) {
568 r = -EBUSY;
569 goto out;
570 }
571 if (gpmc_cs_mem_enabled(cs))
572 r = adjust_resource(res, res->start & ~(size - 1), size);
573 if (r < 0)
574 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
575 size, NULL, NULL);
576 if (r < 0)
577 goto out;
578
Roger Quadros4cf27d22014-08-29 19:11:53 +0300579 /* Disable CS while changing base address and size mask */
580 gpmc_cs_disable_mem(cs);
581
582 r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
Jon Hunterc71f8e92013-03-06 12:00:10 -0600583 if (r < 0) {
584 release_resource(res);
585 goto out;
586 }
587
Roger Quadros4cf27d22014-08-29 19:11:53 +0300588 /* Enable CS */
589 gpmc_cs_enable_mem(cs);
Imre Deakf37e4582006-09-25 12:41:33 +0300590 *base = res->start;
591 gpmc_cs_set_reserved(cs, 1);
592out:
593 spin_unlock(&gpmc_mem_lock);
594 return r;
595}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300596EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300597
598void gpmc_cs_free(int cs)
599{
Tony Lindgrenefe80722014-04-21 19:26:13 -0700600 struct resource *res = &gpmc_cs_mem[cs];
601
Imre Deakf37e4582006-09-25 12:41:33 +0300602 spin_lock(&gpmc_mem_lock);
Gupta Pekonf34f3712013-05-31 17:31:30 +0530603 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300604 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
605 BUG();
606 spin_unlock(&gpmc_mem_lock);
607 return;
608 }
609 gpmc_cs_disable_mem(cs);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700610 if (res->flags)
611 release_resource(res);
Imre Deakf37e4582006-09-25 12:41:33 +0300612 gpmc_cs_set_reserved(cs, 0);
613 spin_unlock(&gpmc_mem_lock);
614}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300615EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300616
vimal singh59e9c5a2009-07-13 16:26:24 +0530617/**
Jon Hunter3a544352013-02-21 13:00:21 -0600618 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000619 * @cmd: command type
620 * @wval: value to write
621 * @return status of the operation
622 */
Jon Hunter3a544352013-02-21 13:00:21 -0600623int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000624{
Jon Hunter3a544352013-02-21 13:00:21 -0600625 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000626
627 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530628 case GPMC_ENABLE_IRQ:
629 gpmc_write_reg(GPMC_IRQENABLE, wval);
630 break;
631
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000632 case GPMC_SET_IRQ_STATUS:
633 gpmc_write_reg(GPMC_IRQSTATUS, wval);
634 break;
635
636 case GPMC_CONFIG_WP:
637 regval = gpmc_read_reg(GPMC_CONFIG);
638 if (wval)
639 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
640 else
641 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
642 gpmc_write_reg(GPMC_CONFIG, regval);
643 break;
644
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000645 default:
Jon Hunter3a544352013-02-21 13:00:21 -0600646 pr_err("%s: command not supported\n", __func__);
647 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000648 }
649
Jon Hunter3a544352013-02-21 13:00:21 -0600650 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000651}
Jon Hunter3a544352013-02-21 13:00:21 -0600652EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000653
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700654void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
655{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530656 int i;
657
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700658 reg->gpmc_status = gpmc_base + GPMC_STATUS;
659 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
660 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
661 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
662 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
663 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
664 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
665 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
666 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
667 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
668 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
669 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
670 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
671 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
672 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530673
674 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
675 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
676 GPMC_BCH_SIZE * i;
677 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
678 GPMC_BCH_SIZE * i;
679 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
680 GPMC_BCH_SIZE * i;
681 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
682 GPMC_BCH_SIZE * i;
pekon gupta27c9fd62014-05-19 13:24:39 +0530683 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
684 i * GPMC_BCH_SIZE;
685 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
686 i * GPMC_BCH_SIZE;
687 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
688 i * GPMC_BCH_SIZE;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530689 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700690}
691
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700692int gpmc_get_client_irq(unsigned irq_config)
693{
694 int i;
695
696 if (hweight32(irq_config) > 1)
697 return 0;
698
699 for (i = 0; i < GPMC_NR_IRQ; i++)
700 if (gpmc_client_irq[i].bitmask & irq_config)
701 return gpmc_client_irq[i].irq;
702
703 return 0;
704}
705
706static int gpmc_irq_endis(unsigned irq, bool endis)
707{
708 int i;
709 u32 regval;
710
711 for (i = 0; i < GPMC_NR_IRQ; i++)
712 if (irq == gpmc_client_irq[i].irq) {
713 regval = gpmc_read_reg(GPMC_IRQENABLE);
714 if (endis)
715 regval |= gpmc_client_irq[i].bitmask;
716 else
717 regval &= ~gpmc_client_irq[i].bitmask;
718 gpmc_write_reg(GPMC_IRQENABLE, regval);
719 break;
720 }
721
722 return 0;
723}
724
725static void gpmc_irq_disable(struct irq_data *p)
726{
727 gpmc_irq_endis(p->irq, false);
728}
729
730static void gpmc_irq_enable(struct irq_data *p)
731{
732 gpmc_irq_endis(p->irq, true);
733}
734
735static void gpmc_irq_noop(struct irq_data *data) { }
736
737static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
738
Afzal Mohammedda496872012-09-23 17:28:25 -0600739static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700740{
741 int i;
742 u32 regval;
743
744 if (!gpmc_irq)
745 return -EINVAL;
746
747 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000748 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700749 pr_err("irq_alloc_descs failed\n");
750 return gpmc_irq_start;
751 }
752
753 gpmc_irq_chip.name = "gpmc";
754 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
755 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
756 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
757 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
758 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
759 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
760 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
761
762 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
763 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
764
765 for (i = 0; i < GPMC_NR_IRQ; i++) {
766 gpmc_client_irq[i].irq = gpmc_irq_start + i;
767 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
768 &gpmc_irq_chip, handle_simple_irq);
769 set_irq_flags(gpmc_client_irq[i].irq,
770 IRQF_VALID | IRQF_NOAUTOEN);
771 }
772
773 /* Disable interrupts */
774 gpmc_write_reg(GPMC_IRQENABLE, 0);
775
776 /* clear interrupts */
777 regval = gpmc_read_reg(GPMC_IRQSTATUS);
778 gpmc_write_reg(GPMC_IRQSTATUS, regval);
779
780 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
781}
782
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800783static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600784{
785 int i;
786
787 if (gpmc_irq)
788 free_irq(gpmc_irq, NULL);
789
790 for (i = 0; i < GPMC_NR_IRQ; i++) {
791 irq_set_handler(gpmc_client_irq[i].irq, NULL);
792 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
793 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
794 }
795
796 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
797
798 return 0;
799}
800
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800801static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600802{
803 int cs;
804
Gupta Pekonf34f3712013-05-31 17:31:30 +0530805 for (cs = 0; cs < gpmc_cs_num; cs++) {
Afzal Mohammedda496872012-09-23 17:28:25 -0600806 if (!gpmc_cs_mem_enabled(cs))
807 continue;
808 gpmc_cs_delete_mem(cs);
809 }
810
811}
812
Jon Hunter84b00f02013-03-06 14:36:47 -0600813static void gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300814{
Jon Hunter84b00f02013-03-06 14:36:47 -0600815 int cs;
Imre Deakf37e4582006-09-25 12:41:33 +0300816
Jon Hunterbf234392013-03-06 14:12:59 -0600817 /*
818 * The first 1MB of GPMC address space is typically mapped to
819 * the internal ROM. Never allocate the first page, to
820 * facilitate bug detection; even if we didn't boot from ROM.
Kyungmin Park7f245162006-12-29 16:48:51 -0800821 */
Jon Hunterbf234392013-03-06 14:12:59 -0600822 gpmc_mem_root.start = SZ_1M;
Imre Deakf37e4582006-09-25 12:41:33 +0300823 gpmc_mem_root.end = GPMC_MEM_END;
824
825 /* Reserve all regions that has been set up by bootloader */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530826 for (cs = 0; cs < gpmc_cs_num; cs++) {
Imre Deakf37e4582006-09-25 12:41:33 +0300827 u32 base, size;
828
829 if (!gpmc_cs_mem_enabled(cs))
830 continue;
831 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter84b00f02013-03-06 14:36:47 -0600832 if (gpmc_cs_insert_mem(cs, base, size)) {
833 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
834 __func__, cs, base, base + size);
835 gpmc_cs_disable_mem(cs);
Jon Hunter81190242012-10-17 09:41:25 -0500836 }
Imre Deakf37e4582006-09-25 12:41:33 +0300837 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700838}
839
Afzal Mohammed246da262012-08-02 20:02:10 +0530840static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
841{
842 u32 temp;
843 int div;
844
845 div = gpmc_calc_divider(sync_clk);
846 temp = gpmc_ps_to_ticks(time_ps);
847 temp = (temp + div - 1) / div;
848 return gpmc_ticks_to_ps(temp * div);
849}
850
851/* XXX: can the cycles be avoided ? */
852static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600853 struct gpmc_device_timings *dev_t,
854 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530855{
Afzal Mohammed246da262012-08-02 20:02:10 +0530856 u32 temp;
857
858 /* adv_rd_off */
859 temp = dev_t->t_avdp_r;
860 /* XXX: mux check required ? */
861 if (mux) {
862 /* XXX: t_avdp not to be required for sync, only added for tusb
863 * this indirectly necessitates requirement of t_avdp_r and
864 * t_avdp_w instead of having a single t_avdp
865 */
866 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
867 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
868 }
869 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
870
871 /* oe_on */
872 temp = dev_t->t_oeasu; /* XXX: remove this ? */
873 if (mux) {
874 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
875 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
876 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
877 }
878 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
879
880 /* access */
881 /* XXX: any scope for improvement ?, by combining oe_on
882 * and clk_activation, need to check whether
883 * access = clk_activation + round to sync clk ?
884 */
885 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
886 temp += gpmc_t->clk_activation;
887 if (dev_t->cyc_oe)
888 temp = max_t(u32, temp, gpmc_t->oe_on +
889 gpmc_ticks_to_ps(dev_t->cyc_oe));
890 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
891
892 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
893 gpmc_t->cs_rd_off = gpmc_t->oe_off;
894
895 /* rd_cycle */
896 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
897 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
898 gpmc_t->access;
899 /* XXX: barter t_ce_rdyz with t_cez_r ? */
900 if (dev_t->t_ce_rdyz)
901 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
902 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
903
904 return 0;
905}
906
907static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600908 struct gpmc_device_timings *dev_t,
909 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530910{
Afzal Mohammed246da262012-08-02 20:02:10 +0530911 u32 temp;
912
913 /* adv_wr_off */
914 temp = dev_t->t_avdp_w;
915 if (mux) {
916 temp = max_t(u32, temp,
917 gpmc_t->clk_activation + dev_t->t_avdh);
918 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
919 }
920 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
921
922 /* wr_data_mux_bus */
923 temp = max_t(u32, dev_t->t_weasu,
924 gpmc_t->clk_activation + dev_t->t_rdyo);
925 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
926 * and in that case remember to handle we_on properly
927 */
928 if (mux) {
929 temp = max_t(u32, temp,
930 gpmc_t->adv_wr_off + dev_t->t_aavdh);
931 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
932 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
933 }
934 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
935
936 /* we_on */
937 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
938 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
939 else
940 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
941
942 /* wr_access */
943 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
944 gpmc_t->wr_access = gpmc_t->access;
945
946 /* we_off */
947 temp = gpmc_t->we_on + dev_t->t_wpl;
948 temp = max_t(u32, temp,
949 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
950 temp = max_t(u32, temp,
951 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
952 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
953
954 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
955 dev_t->t_wph);
956
957 /* wr_cycle */
958 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
959 temp += gpmc_t->wr_access;
960 /* XXX: barter t_ce_rdyz with t_cez_w ? */
961 if (dev_t->t_ce_rdyz)
962 temp = max_t(u32, temp,
963 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
964 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
965
966 return 0;
967}
968
969static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600970 struct gpmc_device_timings *dev_t,
971 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530972{
Afzal Mohammed246da262012-08-02 20:02:10 +0530973 u32 temp;
974
975 /* adv_rd_off */
976 temp = dev_t->t_avdp_r;
977 if (mux)
978 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
979 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
980
981 /* oe_on */
982 temp = dev_t->t_oeasu;
983 if (mux)
984 temp = max_t(u32, temp,
985 gpmc_t->adv_rd_off + dev_t->t_aavdh);
986 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
987
988 /* access */
989 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
990 gpmc_t->oe_on + dev_t->t_oe);
991 temp = max_t(u32, temp,
992 gpmc_t->cs_on + dev_t->t_ce);
993 temp = max_t(u32, temp,
994 gpmc_t->adv_on + dev_t->t_aa);
995 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
996
997 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
998 gpmc_t->cs_rd_off = gpmc_t->oe_off;
999
1000 /* rd_cycle */
1001 temp = max_t(u32, dev_t->t_rd_cycle,
1002 gpmc_t->cs_rd_off + dev_t->t_cez_r);
1003 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1004 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1005
1006 return 0;
1007}
1008
1009static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001010 struct gpmc_device_timings *dev_t,
1011 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301012{
Afzal Mohammed246da262012-08-02 20:02:10 +05301013 u32 temp;
1014
1015 /* adv_wr_off */
1016 temp = dev_t->t_avdp_w;
1017 if (mux)
1018 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1019 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1020
1021 /* wr_data_mux_bus */
1022 temp = dev_t->t_weasu;
1023 if (mux) {
1024 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1025 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1026 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1027 }
1028 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1029
1030 /* we_on */
1031 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1032 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1033 else
1034 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1035
1036 /* we_off */
1037 temp = gpmc_t->we_on + dev_t->t_wpl;
1038 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1039
1040 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1041 dev_t->t_wph);
1042
1043 /* wr_cycle */
1044 temp = max_t(u32, dev_t->t_wr_cycle,
1045 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1046 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1047
1048 return 0;
1049}
1050
1051static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1052 struct gpmc_device_timings *dev_t)
1053{
1054 u32 temp;
1055
1056 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1057 gpmc_get_fclk_period();
1058
1059 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1060 dev_t->t_bacc,
1061 gpmc_t->sync_clk);
1062
1063 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1064 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1065
1066 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1067 return 0;
1068
1069 if (dev_t->ce_xdelay)
1070 gpmc_t->bool_timings.cs_extra_delay = true;
1071 if (dev_t->avd_xdelay)
1072 gpmc_t->bool_timings.adv_extra_delay = true;
1073 if (dev_t->oe_xdelay)
1074 gpmc_t->bool_timings.oe_extra_delay = true;
1075 if (dev_t->we_xdelay)
1076 gpmc_t->bool_timings.we_extra_delay = true;
1077
1078 return 0;
1079}
1080
1081static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001082 struct gpmc_device_timings *dev_t,
1083 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301084{
1085 u32 temp;
1086
1087 /* cs_on */
1088 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1089
1090 /* adv_on */
1091 temp = dev_t->t_avdasu;
1092 if (dev_t->t_ce_avd)
1093 temp = max_t(u32, temp,
1094 gpmc_t->cs_on + dev_t->t_ce_avd);
1095 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1096
Jon Hunterc3be5b42013-02-21 13:46:22 -06001097 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301098 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1099
1100 return 0;
1101}
1102
1103/* TODO: remove this function once all peripherals are confirmed to
1104 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1105 * has to be modified to handle timings in ps instead of ns
1106*/
1107static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1108{
1109 t->cs_on /= 1000;
1110 t->cs_rd_off /= 1000;
1111 t->cs_wr_off /= 1000;
1112 t->adv_on /= 1000;
1113 t->adv_rd_off /= 1000;
1114 t->adv_wr_off /= 1000;
1115 t->we_on /= 1000;
1116 t->we_off /= 1000;
1117 t->oe_on /= 1000;
1118 t->oe_off /= 1000;
1119 t->page_burst_access /= 1000;
1120 t->access /= 1000;
1121 t->rd_cycle /= 1000;
1122 t->wr_cycle /= 1000;
1123 t->bus_turnaround /= 1000;
1124 t->cycle2cycle_delay /= 1000;
1125 t->wait_monitoring /= 1000;
1126 t->clk_activation /= 1000;
1127 t->wr_access /= 1000;
1128 t->wr_data_mux_bus /= 1000;
1129}
1130
1131int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001132 struct gpmc_settings *gpmc_s,
1133 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301134{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001135 bool mux = false, sync = false;
1136
1137 if (gpmc_s) {
1138 mux = gpmc_s->mux_add_data ? true : false;
1139 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1140 }
1141
Afzal Mohammed246da262012-08-02 20:02:10 +05301142 memset(gpmc_t, 0, sizeof(*gpmc_t));
1143
Jon Hunterc3be5b42013-02-21 13:46:22 -06001144 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301145
Jon Hunterc3be5b42013-02-21 13:46:22 -06001146 if (gpmc_s && gpmc_s->sync_read)
1147 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301148 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001149 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301150
Jon Hunterc3be5b42013-02-21 13:46:22 -06001151 if (gpmc_s && gpmc_s->sync_write)
1152 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301153 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001154 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301155
1156 /* TODO: remove, see function definition */
1157 gpmc_convert_ps_to_ns(gpmc_t);
1158
1159 return 0;
1160}
1161
Jon Hunteraa8d4762013-02-21 15:25:23 -06001162/**
1163 * gpmc_cs_program_settings - programs non-timing related settings
1164 * @cs: GPMC chip-select to program
1165 * @p: pointer to GPMC settings structure
1166 *
1167 * Programs non-timing related settings for a GPMC chip-select, such as
1168 * bus-width, burst configuration, etc. Function should be called once
1169 * for each chip-select that is being used and must be called before
1170 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1171 * register will be initialised to zero by this function. Returns 0 on
1172 * success and appropriate negative error code on failure.
1173 */
1174int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1175{
1176 u32 config1;
1177
1178 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1179 pr_err("%s: invalid width %d!", __func__, p->device_width);
1180 return -EINVAL;
1181 }
1182
1183 /* Address-data multiplexing not supported for NAND devices */
1184 if (p->device_nand && p->mux_add_data) {
1185 pr_err("%s: invalid configuration!\n", __func__);
1186 return -EINVAL;
1187 }
1188
1189 if ((p->mux_add_data > GPMC_MUX_AD) ||
1190 ((p->mux_add_data == GPMC_MUX_AAD) &&
1191 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1192 pr_err("%s: invalid multiplex configuration!\n", __func__);
1193 return -EINVAL;
1194 }
1195
1196 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1197 if (p->burst_read || p->burst_write) {
1198 switch (p->burst_len) {
1199 case GPMC_BURST_4:
1200 case GPMC_BURST_8:
1201 case GPMC_BURST_16:
1202 break;
1203 default:
1204 pr_err("%s: invalid page/burst-length (%d)\n",
1205 __func__, p->burst_len);
1206 return -EINVAL;
1207 }
1208 }
1209
Roger Quadros2b540572014-09-02 16:57:06 +03001210 if (p->wait_pin > gpmc_nr_waitpins) {
Jon Hunteraa8d4762013-02-21 15:25:23 -06001211 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1212 return -EINVAL;
1213 }
1214
1215 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1216
1217 if (p->sync_read)
1218 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1219 if (p->sync_write)
1220 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1221 if (p->wait_on_read)
1222 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1223 if (p->wait_on_write)
1224 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1225 if (p->wait_on_read || p->wait_on_write)
1226 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1227 if (p->device_nand)
1228 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1229 if (p->mux_add_data)
1230 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1231 if (p->burst_read)
1232 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1233 if (p->burst_write)
1234 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1235 if (p->burst_read || p->burst_write) {
1236 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1237 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1238 }
1239
1240 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1241
1242 return 0;
1243}
1244
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001245#ifdef CONFIG_OF
Uwe Kleine-König31957602014-09-10 10:26:17 +02001246static const struct of_device_id gpmc_dt_ids[] = {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001247 { .compatible = "ti,omap2420-gpmc" },
1248 { .compatible = "ti,omap2430-gpmc" },
1249 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1250 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1251 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1252 { }
1253};
1254MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1255
Jon Hunter8c8a77712013-02-20 15:53:12 -06001256/**
1257 * gpmc_read_settings_dt - read gpmc settings from device-tree
1258 * @np: pointer to device-tree node for a gpmc child device
1259 * @p: pointer to gpmc settings structure
1260 *
1261 * Reads the GPMC settings for a GPMC child device from device-tree and
1262 * stores them in the GPMC settings structure passed. The GPMC settings
1263 * structure is initialised to zero by this function and so any
1264 * previously stored settings will be cleared.
1265 */
1266void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1267{
1268 memset(p, 0, sizeof(struct gpmc_settings));
1269
1270 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1271 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001272 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1273 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1274
1275 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1276 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1277 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1278 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1279 if (!p->burst_read && !p->burst_write)
1280 pr_warn("%s: page/burst-length set but not used!\n",
1281 __func__);
1282 }
1283
1284 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1285 p->wait_on_read = of_property_read_bool(np,
1286 "gpmc,wait-on-read");
1287 p->wait_on_write = of_property_read_bool(np,
1288 "gpmc,wait-on-write");
1289 if (!p->wait_on_read && !p->wait_on_write)
Roger Quadros2b540572014-09-02 16:57:06 +03001290 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1291 __func__);
Jon Hunter8c8a77712013-02-20 15:53:12 -06001292 }
1293}
1294
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001295static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1296 struct gpmc_timings *gpmc_t)
1297{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001298 struct gpmc_bool_timings *p;
1299
1300 if (!np || !gpmc_t)
1301 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001302
1303 memset(gpmc_t, 0, sizeof(*gpmc_t));
1304
1305 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001306 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001307
1308 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001309 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1310 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1311 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001312
1313 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001314 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1315 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1316 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001317
1318 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001319 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1320 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001321
1322 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001323 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1324 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001325
1326 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001327 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1328 &gpmc_t->page_burst_access);
1329 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1330 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1331 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1332 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1333 &gpmc_t->bus_turnaround);
1334 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1335 &gpmc_t->cycle2cycle_delay);
1336 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1337 &gpmc_t->wait_monitoring);
1338 of_property_read_u32(np, "gpmc,clk-activation-ns",
1339 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001340
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001341 /* only applicable to OMAP3+ */
1342 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1343 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1344 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001345
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001346 /* bool timing parameters */
1347 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001348
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001349 p->cycle2cyclediffcsen =
1350 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1351 p->cycle2cyclesamecsen =
1352 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1353 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1354 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1355 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1356 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1357 p->time_para_granularity =
1358 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001359}
1360
Pekon Gupta6b187b22014-01-28 11:42:40 +05301361#if IS_ENABLED(CONFIG_MTD_NAND)
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001362
Mark Jackson496c8a02013-04-19 21:08:28 +01001363static const char * const nand_xfer_types[] = {
1364 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1365 [NAND_OMAP_POLLED] = "polled",
1366 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1367 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1368};
1369
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001370static int gpmc_probe_nand_child(struct platform_device *pdev,
1371 struct device_node *child)
1372{
1373 u32 val;
1374 const char *s;
1375 struct gpmc_timings gpmc_t;
1376 struct omap_nand_platform_data *gpmc_nand_data;
1377
1378 if (of_property_read_u32(child, "reg", &val) < 0) {
1379 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1380 child->full_name);
1381 return -ENODEV;
1382 }
1383
1384 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1385 GFP_KERNEL);
1386 if (!gpmc_nand_data)
1387 return -ENOMEM;
1388
1389 gpmc_nand_data->cs = val;
1390 gpmc_nand_data->of_node = child;
1391
Pekon Guptaac65caf52013-10-24 18:20:17 +05301392 /* Detect availability of ELM module */
1393 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1394 if (gpmc_nand_data->elm_of_node == NULL)
1395 gpmc_nand_data->elm_of_node =
1396 of_parse_phandle(child, "elm_id", 0);
1397 if (gpmc_nand_data->elm_of_node == NULL)
1398 pr_warn("%s: ti,elm-id property not found\n", __func__);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001399
Pekon Guptaac65caf52013-10-24 18:20:17 +05301400 /* select ecc-scheme for NAND */
1401 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1402 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1403 return -ENODEV;
1404 }
Roger Quadrosa3e83f02014-08-25 16:15:33 -07001405
1406 if (!strcmp(s, "sw"))
1407 gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1408 else if (!strcmp(s, "ham1") ||
1409 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
Pekon Guptaac65caf52013-10-24 18:20:17 +05301410 gpmc_nand_data->ecc_opt =
1411 OMAP_ECC_HAM1_CODE_HW;
1412 else if (!strcmp(s, "bch4"))
1413 if (gpmc_nand_data->elm_of_node)
1414 gpmc_nand_data->ecc_opt =
1415 OMAP_ECC_BCH4_CODE_HW;
1416 else
1417 gpmc_nand_data->ecc_opt =
1418 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1419 else if (!strcmp(s, "bch8"))
1420 if (gpmc_nand_data->elm_of_node)
1421 gpmc_nand_data->ecc_opt =
1422 OMAP_ECC_BCH8_CODE_HW;
1423 else
1424 gpmc_nand_data->ecc_opt =
1425 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
pekon gupta27c9fd62014-05-19 13:24:39 +05301426 else if (!strcmp(s, "bch16"))
1427 if (gpmc_nand_data->elm_of_node)
1428 gpmc_nand_data->ecc_opt =
1429 OMAP_ECC_BCH16_CODE_HW;
1430 else
1431 pr_err("%s: BCH16 requires ELM support\n", __func__);
Pekon Guptaac65caf52013-10-24 18:20:17 +05301432 else
1433 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1434
1435 /* select data transfer mode for NAND controller */
Mark Jackson496c8a02013-04-19 21:08:28 +01001436 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1437 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1438 if (!strcasecmp(s, nand_xfer_types[val])) {
1439 gpmc_nand_data->xfer_type = val;
1440 break;
1441 }
1442
Ezequiel Garcíafef775c2014-09-11 12:02:08 -03001443 gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
1444
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001445 val = of_get_nand_bus_width(child);
1446 if (val == 16)
1447 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1448
1449 gpmc_read_timings_dt(child, &gpmc_t);
1450 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1451
1452 return 0;
1453}
1454#else
1455static int gpmc_probe_nand_child(struct platform_device *pdev,
1456 struct device_node *child)
1457{
1458 return 0;
1459}
1460#endif
1461
Pekon Gupta980386d2014-01-28 11:42:41 +05301462#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001463static int gpmc_probe_onenand_child(struct platform_device *pdev,
1464 struct device_node *child)
1465{
1466 u32 val;
1467 struct omap_onenand_platform_data *gpmc_onenand_data;
1468
1469 if (of_property_read_u32(child, "reg", &val) < 0) {
1470 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1471 child->full_name);
1472 return -ENODEV;
1473 }
1474
1475 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1476 GFP_KERNEL);
1477 if (!gpmc_onenand_data)
1478 return -ENOMEM;
1479
1480 gpmc_onenand_data->cs = val;
1481 gpmc_onenand_data->of_node = child;
1482 gpmc_onenand_data->dma_channel = -1;
1483
1484 if (!of_property_read_u32(child, "dma-channel", &val))
1485 gpmc_onenand_data->dma_channel = val;
1486
1487 gpmc_onenand_init(gpmc_onenand_data);
1488
1489 return 0;
1490}
1491#else
1492static int gpmc_probe_onenand_child(struct platform_device *pdev,
1493 struct device_node *child)
1494{
1495 return 0;
1496}
1497#endif
1498
Jon Huntercdd69282013-02-08 16:46:13 -06001499/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001500 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001501 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001502 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001503 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001504 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001505 * Returns 0 on success and appropriate negative error code on failure.
1506 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001507static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001508 struct device_node *child)
1509{
1510 struct gpmc_settings gpmc_s;
1511 struct gpmc_timings gpmc_t;
1512 struct resource res;
1513 unsigned long base;
1514 int ret, cs;
Roger Quadrose378d222014-08-29 19:11:52 +03001515 u32 val;
Jon Huntercdd69282013-02-08 16:46:13 -06001516
1517 if (of_property_read_u32(child, "reg", &cs) < 0) {
1518 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1519 child->full_name);
1520 return -ENODEV;
1521 }
1522
1523 if (of_address_to_resource(child, 0, &res) < 0) {
1524 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1525 child->full_name);
1526 return -ENODEV;
1527 }
1528
1529 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1530 if (ret < 0) {
1531 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1532 return ret;
1533 }
1534
1535 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001536 * For some GPMC devices we still need to rely on the bootloader
1537 * timings because the devices can be connected via FPGA. So far
1538 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1539 * REVISIT: Add timing support from slls644g.pdf and from the
1540 * lan91c96 manual.
1541 */
1542 if (of_device_is_compatible(child, "ns16550a") ||
1543 of_device_is_compatible(child, "smsc,lan91c94") ||
1544 of_device_is_compatible(child, "smsc,lan91c111")) {
1545 dev_warn(&pdev->dev,
1546 "%s using bootloader timings on CS%d\n",
1547 child->name, cs);
1548 goto no_timings;
1549 }
1550
Roger Quadros4cf27d22014-08-29 19:11:53 +03001551 /* CS must be disabled while making changes to gpmc configuration */
1552 gpmc_cs_disable_mem(cs);
1553
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001554 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001555 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1556 * location in the gpmc address space. When booting with
1557 * device-tree we want the NOR flash to be mapped to the
1558 * location specified in the device-tree blob. So remap the
1559 * CS to this location. Once DT migration is complete should
1560 * just make gpmc_cs_request() map a specific address.
1561 */
1562 ret = gpmc_cs_remap(cs, res.start);
1563 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001564 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1565 cs, &res.start);
Jon Huntercdd69282013-02-08 16:46:13 -06001566 goto err;
1567 }
1568
1569 gpmc_read_settings_dt(child, &gpmc_s);
1570
1571 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1572 if (ret < 0)
1573 goto err;
1574
1575 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1576 if (ret < 0)
1577 goto err;
1578
1579 gpmc_read_timings_dt(child, &gpmc_t);
Roger Quadros7604baf2014-08-29 19:11:51 +03001580 ret = gpmc_cs_set_timings(cs, &gpmc_t);
1581 if (ret) {
1582 dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
1583 child->name);
1584 goto err;
1585 }
Jon Huntercdd69282013-02-08 16:46:13 -06001586
Roger Quadrose378d222014-08-29 19:11:52 +03001587 /* Clear limited address i.e. enable A26-A11 */
1588 val = gpmc_read_reg(GPMC_CONFIG);
1589 val &= ~GPMC_CONFIG_LIMITEDADDRESS;
1590 gpmc_write_reg(GPMC_CONFIG, val);
1591
Roger Quadros4cf27d22014-08-29 19:11:53 +03001592 /* Enable CS region */
1593 gpmc_cs_enable_mem(cs);
1594
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001595no_timings:
Jon Huntercdd69282013-02-08 16:46:13 -06001596 if (of_platform_device_create(child, NULL, &pdev->dev))
1597 return 0;
1598
1599 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01001600 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06001601
1602err:
1603 gpmc_cs_free(cs);
1604
1605 return ret;
1606}
1607
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001608static int gpmc_probe_dt(struct platform_device *pdev)
1609{
1610 int ret;
1611 struct device_node *child;
1612 const struct of_device_id *of_id =
1613 of_match_device(gpmc_dt_ids, &pdev->dev);
1614
1615 if (!of_id)
1616 return 0;
1617
Gupta Pekonf34f3712013-05-31 17:31:30 +05301618 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1619 &gpmc_cs_num);
1620 if (ret < 0) {
1621 pr_err("%s: number of chip-selects not defined\n", __func__);
1622 return ret;
1623 } else if (gpmc_cs_num < 1) {
1624 pr_err("%s: all chip-selects are disabled\n", __func__);
1625 return -EINVAL;
1626 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1627 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1628 __func__, GPMC_CS_NUM);
1629 return -EINVAL;
1630 }
1631
Jon Hunter9f833152013-02-20 15:53:38 -06001632 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1633 &gpmc_nr_waitpins);
1634 if (ret < 0) {
1635 pr_err("%s: number of wait pins not found!\n", __func__);
1636 return ret;
1637 }
1638
Guido Martínez68e2eb52014-07-02 10:35:18 -03001639 for_each_available_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001640
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001641 if (!child->name)
1642 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06001643
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001644 if (of_node_cmp(child->name, "nand") == 0)
1645 ret = gpmc_probe_nand_child(pdev, child);
1646 else if (of_node_cmp(child->name, "onenand") == 0)
1647 ret = gpmc_probe_onenand_child(pdev, child);
1648 else if (of_node_cmp(child->name, "ethernet") == 0 ||
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001649 of_node_cmp(child->name, "nor") == 0 ||
1650 of_node_cmp(child->name, "uart") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02001651 ret = gpmc_probe_generic_child(pdev, child);
Jon Huntercdd69282013-02-08 16:46:13 -06001652
Javier Martinez Canillasb327b362013-04-17 22:34:12 +02001653 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1654 __func__, child->full_name))
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001655 of_node_put(child);
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01001656 }
1657
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001658 return 0;
1659}
1660#else
1661static int gpmc_probe_dt(struct platform_device *pdev)
1662{
1663 return 0;
1664}
1665#endif
1666
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001667static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001668{
Jon Hunter81190242012-10-17 09:41:25 -05001669 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001670 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001671 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001672
Afzal Mohammedda496872012-09-23 17:28:25 -06001673 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1674 if (res == NULL)
1675 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001676
Afzal Mohammedda496872012-09-23 17:28:25 -06001677 phys_base = res->start;
1678 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001679
Thierry Reding5857bd92013-01-21 11:08:55 +01001680 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1681 if (IS_ERR(gpmc_base))
1682 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001683
1684 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1685 if (res == NULL)
1686 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1687 else
1688 gpmc_irq = res->start;
1689
Roger Quadros8bf9be52014-09-01 15:18:56 +03001690 gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
Afzal Mohammedda496872012-09-23 17:28:25 -06001691 if (IS_ERR(gpmc_l3_clk)) {
Roger Quadros8bf9be52014-09-01 15:18:56 +03001692 dev_err(&pdev->dev, "Failed to get GPMC fck\n");
Afzal Mohammedda496872012-09-23 17:28:25 -06001693 gpmc_irq = 0;
1694 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001695 }
1696
Roger Quadros8bf9be52014-09-01 15:18:56 +03001697 if (!clk_get_rate(gpmc_l3_clk)) {
1698 dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
1699 return -EINVAL;
1700 }
1701
avinash philipb3f55252013-06-12 16:30:56 +05301702 pm_runtime_enable(&pdev->dev);
1703 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001704
Afzal Mohammedda496872012-09-23 17:28:25 -06001705 gpmc_dev = &pdev->dev;
1706
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001707 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001708
1709 /*
1710 * FIXME: Once device-tree migration is complete the below flags
1711 * should be populated based upon the device-tree compatible
1712 * string. For now just use the IP revision. OMAP3+ devices have
1713 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1714 * devices support the addr-addr-data multiplex protocol.
1715 *
1716 * GPMC IP revisions:
1717 * - OMAP24xx = 2.0
1718 * - OMAP3xxx = 5.0
1719 * - OMAP44xx/54xx/AM335x = 6.0
1720 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001721 if (GPMC_REVISION_MAJOR(l) > 0x4)
1722 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001723 if (GPMC_REVISION_MAJOR(l) > 0x5)
1724 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001725 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1726 GPMC_REVISION_MINOR(l));
1727
Jon Hunter84b00f02013-03-06 14:36:47 -06001728 gpmc_mem_init();
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301729
Russell King71856842013-03-13 20:44:21 +00001730 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001731 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1732
Jon Hunter31d9adc2013-02-18 07:57:39 -06001733 /* Now the GPMC is initialised, unreserve the chip-selects */
1734 gpmc_cs_map = 0;
1735
Gupta Pekonf34f3712013-05-31 17:31:30 +05301736 if (!pdev->dev.of_node) {
1737 gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -06001738 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
Gupta Pekonf34f3712013-05-31 17:31:30 +05301739 }
Jon Hunter9f833152013-02-20 15:53:38 -06001740
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001741 rc = gpmc_probe_dt(pdev);
1742 if (rc < 0) {
avinash philipb3f55252013-06-12 16:30:56 +05301743 pm_runtime_put_sync(&pdev->dev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001744 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1745 return rc;
1746 }
1747
Afzal Mohammedda496872012-09-23 17:28:25 -06001748 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301749}
Afzal Mohammedda496872012-09-23 17:28:25 -06001750
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001751static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001752{
1753 gpmc_free_irq();
1754 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05301755 pm_runtime_put_sync(&pdev->dev);
1756 pm_runtime_disable(&pdev->dev);
Afzal Mohammedda496872012-09-23 17:28:25 -06001757 gpmc_dev = NULL;
1758 return 0;
1759}
1760
avinash philipb536dd42013-06-18 00:16:38 +05301761#ifdef CONFIG_PM_SLEEP
1762static int gpmc_suspend(struct device *dev)
1763{
1764 omap3_gpmc_save_context();
1765 pm_runtime_put_sync(dev);
1766 return 0;
1767}
1768
1769static int gpmc_resume(struct device *dev)
1770{
1771 pm_runtime_get_sync(dev);
1772 omap3_gpmc_restore_context();
1773 return 0;
1774}
1775#endif
1776
1777static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1778
Afzal Mohammedda496872012-09-23 17:28:25 -06001779static struct platform_driver gpmc_driver = {
1780 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001781 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001782 .driver = {
1783 .name = DEVICE_NAME,
1784 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001785 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05301786 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06001787 },
1788};
1789
1790static __init int gpmc_init(void)
1791{
1792 return platform_driver_register(&gpmc_driver);
1793}
1794
1795static __exit void gpmc_exit(void)
1796{
1797 platform_driver_unregister(&gpmc_driver);
1798
1799}
1800
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001801omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001802module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301803
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001804static int __init omap_gpmc_init(void)
1805{
1806 struct omap_hwmod *oh;
1807 struct platform_device *pdev;
1808 char *oh_name = "gpmc";
1809
Daniel Mack2f98ca82012-12-14 11:36:40 +01001810 /*
1811 * if the board boots up with a populated DT, do not
1812 * manually add the device from this initcall
1813 */
1814 if (of_have_populated_dt())
1815 return -ENODEV;
1816
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001817 oh = omap_hwmod_lookup(oh_name);
1818 if (!oh) {
1819 pr_err("Could not look up %s\n", oh_name);
1820 return -ENODEV;
1821 }
1822
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001823 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001824 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1825
Thomas Meyer12616742013-06-01 11:44:44 +02001826 return PTR_RET(pdev);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001827}
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001828omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001829
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301830static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1831{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001832 int i;
1833 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301834
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001835 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1836
1837 if (!regval)
1838 return IRQ_NONE;
1839
1840 for (i = 0; i < GPMC_NR_IRQ; i++)
1841 if (regval & gpmc_client_irq[i].bitmask)
1842 generic_handle_irq(gpmc_client_irq[i].irq);
1843
1844 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301845
1846 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001847}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301848
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301849static struct omap3_gpmc_regs gpmc_context;
1850
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001851void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301852{
1853 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001854
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301855 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1856 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1857 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1858 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1859 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1860 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1861 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301862 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301863 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1864 if (gpmc_context.cs_context[i].is_valid) {
1865 gpmc_context.cs_context[i].config1 =
1866 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1867 gpmc_context.cs_context[i].config2 =
1868 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1869 gpmc_context.cs_context[i].config3 =
1870 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1871 gpmc_context.cs_context[i].config4 =
1872 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1873 gpmc_context.cs_context[i].config5 =
1874 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1875 gpmc_context.cs_context[i].config6 =
1876 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1877 gpmc_context.cs_context[i].config7 =
1878 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1879 }
1880 }
1881}
1882
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001883void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301884{
1885 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001886
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301887 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1888 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1889 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1890 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1891 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1892 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1893 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301894 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301895 if (gpmc_context.cs_context[i].is_valid) {
1896 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1897 gpmc_context.cs_context[i].config1);
1898 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1899 gpmc_context.cs_context[i].config2);
1900 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1901 gpmc_context.cs_context[i].config3);
1902 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1903 gpmc_context.cs_context[i].config4);
1904 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1905 gpmc_context.cs_context[i].config5);
1906 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1907 gpmc_context.cs_context[i].config6);
1908 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1909 gpmc_context.cs_context[i].config7);
1910 }
1911 }
1912}