blob: 5c22b5adaeb50fd4f2a4b97e605ada07543bffc6 [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>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070033
Afzal Mohammedbc3668e2012-09-29 12:26:13 +053034#include <linux/platform_data/mtd-nand-omap2.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070035
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070036#include <asm/mach-types.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070037
Tony Lindgrendbc04162012-08-31 10:59:07 -070038#include "soc.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070039#include "common.h"
Tony Lindgren25c7d492012-10-02 17:25:48 -070040#include "omap_device.h"
Afzal Mohammed3ef5d002012-10-05 10:37:27 +053041#include "gpmc.h"
Daniel Mackbc6b1e72012-12-14 11:36:44 +010042#include "gpmc-nand.h"
Ezequiel Garcia75d36252013-01-25 09:23:11 -030043#include "gpmc-onenand.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070044
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060045#define DEVICE_NAME "omap-gpmc"
46
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030047/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070048#define GPMC_REVISION 0x00
49#define GPMC_SYSCONFIG 0x10
50#define GPMC_SYSSTATUS 0x14
51#define GPMC_IRQSTATUS 0x18
52#define GPMC_IRQENABLE 0x1c
53#define GPMC_TIMEOUT_CONTROL 0x40
54#define GPMC_ERR_ADDRESS 0x44
55#define GPMC_ERR_TYPE 0x48
56#define GPMC_CONFIG 0x50
57#define GPMC_STATUS 0x54
58#define GPMC_PREFETCH_CONFIG1 0x1e0
59#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053060#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070061#define GPMC_PREFETCH_STATUS 0x1f0
62#define GPMC_ECC_CONFIG 0x1f4
63#define GPMC_ECC_CONTROL 0x1f8
64#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000065#define GPMC_ECC1_RESULT 0x200
Ivan Djelic8d602cf2012-04-26 14:17:49 +020066#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053067#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
68#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
69#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070070
Yegor Yefremov2c65e742012-05-09 08:32:49 -070071/* GPMC ECC control settings */
72#define GPMC_ECC_CTRL_ECCCLEAR 0x100
73#define GPMC_ECC_CTRL_ECCDISABLE 0x000
74#define GPMC_ECC_CTRL_ECCREG1 0x001
75#define GPMC_ECC_CTRL_ECCREG2 0x002
76#define GPMC_ECC_CTRL_ECCREG3 0x003
77#define GPMC_ECC_CTRL_ECCREG4 0x004
78#define GPMC_ECC_CTRL_ECCREG5 0x005
79#define GPMC_ECC_CTRL_ECCREG6 0x006
80#define GPMC_ECC_CTRL_ECCREG7 0x007
81#define GPMC_ECC_CTRL_ECCREG8 0x008
82#define GPMC_ECC_CTRL_ECCREG9 0x009
83
Afzal Mohammed559d94b2012-05-28 17:51:37 +053084#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
85#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
86#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
87#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
88#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
89#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
90
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000091#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070092#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053093#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070094
Imre Deakf37e4582006-09-25 12:41:33 +030095#define GPMC_MEM_START 0x00000000
96#define GPMC_MEM_END 0x3FFFFFFF
97#define BOOT_ROM_SPACE 0x100000 /* 1MB */
98
99#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
100#define GPMC_SECTION_SHIFT 28 /* 128 MB */
101
vimal singh59e9c5a2009-07-13 16:26:24 +0530102#define CS_NUM_SHIFT 24
103#define ENABLE_PREFETCH (0x1 << 7)
104#define DMA_MPU_MODE 2
105
Afzal Mohammedda496872012-09-23 17:28:25 -0600106#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
107#define GPMC_REVISION_MINOR(l) (l & 0xf)
108
109#define GPMC_HAS_WR_ACCESS 0x1
110#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
Jon Hunteraa8d4762013-02-21 15:25:23 -0600111#define GPMC_HAS_MUX_AAD 0x4
Afzal Mohammedda496872012-09-23 17:28:25 -0600112
Jon Hunter9f833152013-02-20 15:53:38 -0600113#define GPMC_NR_WAITPINS 4
114
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700115/* XXX: Only NAND irq has been considered,currently these are the only ones used
116 */
117#define GPMC_NR_IRQ 2
118
119struct gpmc_client_irq {
120 unsigned irq;
121 u32 bitmask;
122};
123
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530124/* Structure to save gpmc cs context */
125struct gpmc_cs_config {
126 u32 config1;
127 u32 config2;
128 u32 config3;
129 u32 config4;
130 u32 config5;
131 u32 config6;
132 u32 config7;
133 int is_valid;
134};
135
136/*
137 * Structure to save/restore gpmc context
138 * to support core off on OMAP3
139 */
140struct omap3_gpmc_regs {
141 u32 sysconfig;
142 u32 irqenable;
143 u32 timeout_ctrl;
144 u32 config;
145 u32 prefetch_config1;
146 u32 prefetch_config2;
147 u32 prefetch_control;
148 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
149};
150
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700151static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
152static struct irq_chip gpmc_irq_chip;
153static unsigned gpmc_irq_start;
154
Imre Deakf37e4582006-09-25 12:41:33 +0300155static struct resource gpmc_mem_root;
156static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700157static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600158/* Define chip-selects as reserved by default until probe completes */
159static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
Jon Hunter9f833152013-02-20 15:53:38 -0600160static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600161static struct device *gpmc_dev;
162static int gpmc_irq;
163static resource_size_t phys_base, mem_size;
164static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300165static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700166
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300167static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700168
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530169static irqreturn_t gpmc_handle_irq(int irq, void *dev);
170
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700171static void gpmc_write_reg(int idx, u32 val)
172{
173 __raw_writel(val, gpmc_base + idx);
174}
175
176static u32 gpmc_read_reg(int idx)
177{
178 return __raw_readl(gpmc_base + idx);
179}
180
181void gpmc_cs_write_reg(int cs, int idx, u32 val)
182{
183 void __iomem *reg_addr;
184
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000185 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700186 __raw_writel(val, reg_addr);
187}
188
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300189static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700190{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300191 void __iomem *reg_addr;
192
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000193 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300194 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700195}
196
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300197/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300198static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700199{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300200 unsigned long rate = clk_get_rate(gpmc_l3_clk);
201
202 if (rate == 0) {
203 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
204 return 0;
205 }
206
207 rate /= 1000;
208 rate = 1000000000 / rate; /* In picoseconds */
209
210 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700211}
212
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300213static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700214{
215 unsigned long tick_ps;
216
217 /* Calculate in picosecs to yield more exact results */
218 tick_ps = gpmc_get_fclk_period();
219
220 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
221}
222
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300223static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200224{
225 unsigned long tick_ps;
226
227 /* Calculate in picosecs to yield more exact results */
228 tick_ps = gpmc_get_fclk_period();
229
230 return (time_ps + tick_ps - 1) / tick_ps;
231}
232
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300233unsigned int gpmc_ticks_to_ns(unsigned int ticks)
234{
235 return ticks * gpmc_get_fclk_period() / 1000;
236}
237
Afzal Mohammed246da262012-08-02 20:02:10 +0530238static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
239{
240 return ticks * gpmc_get_fclk_period();
241}
242
243static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
244{
245 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
246
247 return ticks * gpmc_get_fclk_period();
248}
249
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530250static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
251{
252 u32 l;
253
254 l = gpmc_cs_read_reg(cs, reg);
255 if (value)
256 l |= mask;
257 else
258 l &= ~mask;
259 gpmc_cs_write_reg(cs, reg, l);
260}
261
262static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
263{
264 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
265 GPMC_CONFIG1_TIME_PARA_GRAN,
266 p->time_para_granularity);
267 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
268 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
269 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
270 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
271 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
272 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
273 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
274 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
275 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
276 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
277 p->cycle2cyclesamecsen);
278 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
279 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
280 p->cycle2cyclediffcsen);
281}
282
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700283#ifdef DEBUG
284static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
Juha Yrjola2aab6462006-06-26 16:16:21 -0700285 int time, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700286#else
287static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
288 int time)
289#endif
290{
291 u32 l;
292 int ticks, mask, nr_bits;
293
294 if (time == 0)
295 ticks = 0;
296 else
297 ticks = gpmc_ns_to_ticks(time);
298 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800299 if (ticks >= 1 << nr_bits) {
300#ifdef DEBUG
301 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
302 cs, name, time, ticks, 1 << nr_bits);
303#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700304 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800305 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700306
307 mask = (1 << nr_bits) - 1;
308 l = gpmc_cs_read_reg(cs, reg);
309#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800310 printk(KERN_INFO
311 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700312 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800313 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700314#endif
315 l &= ~(mask << st_bit);
316 l |= ticks << st_bit;
317 gpmc_cs_write_reg(cs, reg, l);
318
319 return 0;
320}
321
322#ifdef DEBUG
323#define GPMC_SET_ONE(reg, st, end, field) \
324 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
325 t->field, #field) < 0) \
326 return -1
327#else
328#define GPMC_SET_ONE(reg, st, end, field) \
329 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
330 return -1
331#endif
332
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530333int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700334{
335 int div;
336 u32 l;
337
Adrian Huntera3551f52010-12-09 10:48:27 +0200338 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700339 div = l / gpmc_get_fclk_period();
340 if (div > 4)
341 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800342 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700343 div = 1;
344
345 return div;
346}
347
348int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
349{
350 int div;
351 u32 l;
352
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530353 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700354 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600355 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700356
357 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
358 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
359 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
360
361 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
362 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
363 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
364
365 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
366 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
367 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
368 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
369
370 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
371 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
372 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
373
374 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
375
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530376 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
377 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
378
379 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
380 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
381
Afzal Mohammedda496872012-09-23 17:28:25 -0600382 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300383 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600384 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300385 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300386
David Brownell1c22cc12006-12-06 17:13:55 -0800387 /* caller is expected to have initialized CONFIG1 to cover
388 * at least sync vs async
389 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700390 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800391 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
392#ifdef DEBUG
393 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
394 cs, (div * gpmc_get_fclk_period()) / 1000, div);
395#endif
396 l &= ~0x03;
397 l |= (div - 1);
398 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
399 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700400
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530401 gpmc_cs_bool_timings(cs, &t->bool_timings);
402
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700403 return 0;
404}
405
Jon Hunterc71f8e92013-03-06 12:00:10 -0600406static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700407{
Imre Deakf37e4582006-09-25 12:41:33 +0300408 u32 l;
409 u32 mask;
410
Jon Hunterc71f8e92013-03-06 12:00:10 -0600411 /*
412 * Ensure that base address is aligned on a
413 * boundary equal to or greater than size.
414 */
415 if (base & (size - 1))
416 return -EINVAL;
417
Imre Deakf37e4582006-09-25 12:41:33 +0300418 mask = (1 << GPMC_SECTION_SHIFT) - size;
419 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
420 l &= ~0x3f;
421 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
422 l &= ~(0x0f << 8);
423 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530424 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300425 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600426
427 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300428}
429
430static void gpmc_cs_disable_mem(int cs)
431{
432 u32 l;
433
434 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530435 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300436 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
437}
438
439static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
440{
441 u32 l;
442 u32 mask;
443
444 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
445 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
446 mask = (l >> 8) & 0x0f;
447 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
448}
449
450static int gpmc_cs_mem_enabled(int cs)
451{
452 u32 l;
453
454 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530455 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300456}
457
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300458static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300459{
460 gpmc_cs_map &= ~(1 << cs);
461 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
462}
463
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300464static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300465{
466 return gpmc_cs_map & (1 << cs);
467}
468
469static unsigned long gpmc_mem_align(unsigned long size)
470{
471 int order;
472
473 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
474 order = GPMC_CHUNK_SHIFT - 1;
475 do {
476 size >>= 1;
477 order++;
478 } while (size);
479 size = 1 << order;
480 return size;
481}
482
483static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
484{
485 struct resource *res = &gpmc_cs_mem[cs];
486 int r;
487
488 size = gpmc_mem_align(size);
489 spin_lock(&gpmc_mem_lock);
490 res->start = base;
491 res->end = base + size - 1;
492 r = request_resource(&gpmc_mem_root, res);
493 spin_unlock(&gpmc_mem_lock);
494
495 return r;
496}
497
Afzal Mohammedda496872012-09-23 17:28:25 -0600498static int gpmc_cs_delete_mem(int cs)
499{
500 struct resource *res = &gpmc_cs_mem[cs];
501 int r;
502
503 spin_lock(&gpmc_mem_lock);
504 r = release_resource(&gpmc_cs_mem[cs]);
505 res->start = 0;
506 res->end = 0;
507 spin_unlock(&gpmc_mem_lock);
508
509 return r;
510}
511
Jon Huntercdd69282013-02-08 16:46:13 -0600512/**
513 * gpmc_cs_remap - remaps a chip-select physical base address
514 * @cs: chip-select to remap
515 * @base: physical base address to re-map chip-select to
516 *
517 * Re-maps a chip-select to a new physical base address specified by
518 * "base". Returns 0 on success and appropriate negative error code
519 * on failure.
520 */
521static int gpmc_cs_remap(int cs, u32 base)
522{
523 int ret;
524 u32 old_base, size;
525
526 if (cs > GPMC_CS_NUM)
527 return -ENODEV;
528 gpmc_cs_get_memconf(cs, &old_base, &size);
529 if (base == old_base)
530 return 0;
531 gpmc_cs_disable_mem(cs);
532 ret = gpmc_cs_delete_mem(cs);
533 if (ret < 0)
534 return ret;
535 ret = gpmc_cs_insert_mem(cs, base, size);
536 if (ret < 0)
537 return ret;
Jon Hunterc71f8e92013-03-06 12:00:10 -0600538 ret = gpmc_cs_enable_mem(cs, base, size);
539 if (ret < 0)
540 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600541
542 return 0;
543}
544
Imre Deakf37e4582006-09-25 12:41:33 +0300545int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
546{
547 struct resource *res = &gpmc_cs_mem[cs];
548 int r = -1;
549
550 if (cs > GPMC_CS_NUM)
551 return -ENODEV;
552
553 size = gpmc_mem_align(size);
554 if (size > (1 << GPMC_SECTION_SHIFT))
555 return -ENOMEM;
556
557 spin_lock(&gpmc_mem_lock);
558 if (gpmc_cs_reserved(cs)) {
559 r = -EBUSY;
560 goto out;
561 }
562 if (gpmc_cs_mem_enabled(cs))
563 r = adjust_resource(res, res->start & ~(size - 1), size);
564 if (r < 0)
565 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
566 size, NULL, NULL);
567 if (r < 0)
568 goto out;
569
Jon Hunterc71f8e92013-03-06 12:00:10 -0600570 r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
571 if (r < 0) {
572 release_resource(res);
573 goto out;
574 }
575
Imre Deakf37e4582006-09-25 12:41:33 +0300576 *base = res->start;
577 gpmc_cs_set_reserved(cs, 1);
578out:
579 spin_unlock(&gpmc_mem_lock);
580 return r;
581}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300582EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300583
584void gpmc_cs_free(int cs)
585{
586 spin_lock(&gpmc_mem_lock);
Roel Kluine7fdc602009-11-17 14:39:06 -0800587 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300588 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
589 BUG();
590 spin_unlock(&gpmc_mem_lock);
591 return;
592 }
593 gpmc_cs_disable_mem(cs);
594 release_resource(&gpmc_cs_mem[cs]);
595 gpmc_cs_set_reserved(cs, 0);
596 spin_unlock(&gpmc_mem_lock);
597}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300598EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300599
vimal singh59e9c5a2009-07-13 16:26:24 +0530600/**
Jon Hunter3a544352013-02-21 13:00:21 -0600601 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000602 * @cmd: command type
603 * @wval: value to write
604 * @return status of the operation
605 */
Jon Hunter3a544352013-02-21 13:00:21 -0600606int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000607{
Jon Hunter3a544352013-02-21 13:00:21 -0600608 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000609
610 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530611 case GPMC_ENABLE_IRQ:
612 gpmc_write_reg(GPMC_IRQENABLE, wval);
613 break;
614
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000615 case GPMC_SET_IRQ_STATUS:
616 gpmc_write_reg(GPMC_IRQSTATUS, wval);
617 break;
618
619 case GPMC_CONFIG_WP:
620 regval = gpmc_read_reg(GPMC_CONFIG);
621 if (wval)
622 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
623 else
624 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
625 gpmc_write_reg(GPMC_CONFIG, regval);
626 break;
627
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000628 default:
Jon Hunter3a544352013-02-21 13:00:21 -0600629 pr_err("%s: command not supported\n", __func__);
630 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000631 }
632
Jon Hunter3a544352013-02-21 13:00:21 -0600633 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000634}
Jon Hunter3a544352013-02-21 13:00:21 -0600635EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000636
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700637void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
638{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530639 int i;
640
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700641 reg->gpmc_status = gpmc_base + GPMC_STATUS;
642 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
643 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
644 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
645 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
646 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
647 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
648 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
649 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
650 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
651 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
652 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
653 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
654 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
655 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530656
657 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
658 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
659 GPMC_BCH_SIZE * i;
660 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
661 GPMC_BCH_SIZE * i;
662 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
663 GPMC_BCH_SIZE * i;
664 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
665 GPMC_BCH_SIZE * i;
666 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700667}
668
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700669int gpmc_get_client_irq(unsigned irq_config)
670{
671 int i;
672
673 if (hweight32(irq_config) > 1)
674 return 0;
675
676 for (i = 0; i < GPMC_NR_IRQ; i++)
677 if (gpmc_client_irq[i].bitmask & irq_config)
678 return gpmc_client_irq[i].irq;
679
680 return 0;
681}
682
683static int gpmc_irq_endis(unsigned irq, bool endis)
684{
685 int i;
686 u32 regval;
687
688 for (i = 0; i < GPMC_NR_IRQ; i++)
689 if (irq == gpmc_client_irq[i].irq) {
690 regval = gpmc_read_reg(GPMC_IRQENABLE);
691 if (endis)
692 regval |= gpmc_client_irq[i].bitmask;
693 else
694 regval &= ~gpmc_client_irq[i].bitmask;
695 gpmc_write_reg(GPMC_IRQENABLE, regval);
696 break;
697 }
698
699 return 0;
700}
701
702static void gpmc_irq_disable(struct irq_data *p)
703{
704 gpmc_irq_endis(p->irq, false);
705}
706
707static void gpmc_irq_enable(struct irq_data *p)
708{
709 gpmc_irq_endis(p->irq, true);
710}
711
712static void gpmc_irq_noop(struct irq_data *data) { }
713
714static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
715
Afzal Mohammedda496872012-09-23 17:28:25 -0600716static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700717{
718 int i;
719 u32 regval;
720
721 if (!gpmc_irq)
722 return -EINVAL;
723
724 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000725 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700726 pr_err("irq_alloc_descs failed\n");
727 return gpmc_irq_start;
728 }
729
730 gpmc_irq_chip.name = "gpmc";
731 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
732 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
733 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
734 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
735 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
736 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
737 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
738
739 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
740 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
741
742 for (i = 0; i < GPMC_NR_IRQ; i++) {
743 gpmc_client_irq[i].irq = gpmc_irq_start + i;
744 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
745 &gpmc_irq_chip, handle_simple_irq);
746 set_irq_flags(gpmc_client_irq[i].irq,
747 IRQF_VALID | IRQF_NOAUTOEN);
748 }
749
750 /* Disable interrupts */
751 gpmc_write_reg(GPMC_IRQENABLE, 0);
752
753 /* clear interrupts */
754 regval = gpmc_read_reg(GPMC_IRQSTATUS);
755 gpmc_write_reg(GPMC_IRQSTATUS, regval);
756
757 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
758}
759
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800760static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600761{
762 int i;
763
764 if (gpmc_irq)
765 free_irq(gpmc_irq, NULL);
766
767 for (i = 0; i < GPMC_NR_IRQ; i++) {
768 irq_set_handler(gpmc_client_irq[i].irq, NULL);
769 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
770 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
771 }
772
773 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
774
775 return 0;
776}
777
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800778static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600779{
780 int cs;
781
782 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
783 if (!gpmc_cs_mem_enabled(cs))
784 continue;
785 gpmc_cs_delete_mem(cs);
786 }
787
788}
789
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800790static int gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300791{
Jon Hunter81190242012-10-17 09:41:25 -0500792 int cs, rc;
Imre Deakf37e4582006-09-25 12:41:33 +0300793 unsigned long boot_rom_space = 0;
794
Kyungmin Park7f245162006-12-29 16:48:51 -0800795 /* never allocate the first page, to facilitate bug detection;
796 * even if we didn't boot from ROM.
797 */
798 boot_rom_space = BOOT_ROM_SPACE;
Imre Deakf37e4582006-09-25 12:41:33 +0300799 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
800 gpmc_mem_root.end = GPMC_MEM_END;
801
802 /* Reserve all regions that has been set up by bootloader */
803 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
804 u32 base, size;
805
806 if (!gpmc_cs_mem_enabled(cs))
807 continue;
808 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter81190242012-10-17 09:41:25 -0500809 rc = gpmc_cs_insert_mem(cs, base, size);
Russell King71856842013-03-13 20:44:21 +0000810 if (rc < 0) {
Jon Hunter81190242012-10-17 09:41:25 -0500811 while (--cs >= 0)
812 if (gpmc_cs_mem_enabled(cs))
813 gpmc_cs_delete_mem(cs);
814 return rc;
815 }
Imre Deakf37e4582006-09-25 12:41:33 +0300816 }
Jon Hunter81190242012-10-17 09:41:25 -0500817
818 return 0;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700819}
820
Afzal Mohammed246da262012-08-02 20:02:10 +0530821static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
822{
823 u32 temp;
824 int div;
825
826 div = gpmc_calc_divider(sync_clk);
827 temp = gpmc_ps_to_ticks(time_ps);
828 temp = (temp + div - 1) / div;
829 return gpmc_ticks_to_ps(temp * div);
830}
831
832/* XXX: can the cycles be avoided ? */
833static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600834 struct gpmc_device_timings *dev_t,
835 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530836{
Afzal Mohammed246da262012-08-02 20:02:10 +0530837 u32 temp;
838
839 /* adv_rd_off */
840 temp = dev_t->t_avdp_r;
841 /* XXX: mux check required ? */
842 if (mux) {
843 /* XXX: t_avdp not to be required for sync, only added for tusb
844 * this indirectly necessitates requirement of t_avdp_r and
845 * t_avdp_w instead of having a single t_avdp
846 */
847 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
848 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
849 }
850 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
851
852 /* oe_on */
853 temp = dev_t->t_oeasu; /* XXX: remove this ? */
854 if (mux) {
855 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
856 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
857 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
858 }
859 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
860
861 /* access */
862 /* XXX: any scope for improvement ?, by combining oe_on
863 * and clk_activation, need to check whether
864 * access = clk_activation + round to sync clk ?
865 */
866 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
867 temp += gpmc_t->clk_activation;
868 if (dev_t->cyc_oe)
869 temp = max_t(u32, temp, gpmc_t->oe_on +
870 gpmc_ticks_to_ps(dev_t->cyc_oe));
871 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
872
873 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
874 gpmc_t->cs_rd_off = gpmc_t->oe_off;
875
876 /* rd_cycle */
877 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
878 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
879 gpmc_t->access;
880 /* XXX: barter t_ce_rdyz with t_cez_r ? */
881 if (dev_t->t_ce_rdyz)
882 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
883 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
884
885 return 0;
886}
887
888static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600889 struct gpmc_device_timings *dev_t,
890 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530891{
Afzal Mohammed246da262012-08-02 20:02:10 +0530892 u32 temp;
893
894 /* adv_wr_off */
895 temp = dev_t->t_avdp_w;
896 if (mux) {
897 temp = max_t(u32, temp,
898 gpmc_t->clk_activation + dev_t->t_avdh);
899 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
900 }
901 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
902
903 /* wr_data_mux_bus */
904 temp = max_t(u32, dev_t->t_weasu,
905 gpmc_t->clk_activation + dev_t->t_rdyo);
906 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
907 * and in that case remember to handle we_on properly
908 */
909 if (mux) {
910 temp = max_t(u32, temp,
911 gpmc_t->adv_wr_off + dev_t->t_aavdh);
912 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
913 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
914 }
915 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
916
917 /* we_on */
918 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
919 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
920 else
921 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
922
923 /* wr_access */
924 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
925 gpmc_t->wr_access = gpmc_t->access;
926
927 /* we_off */
928 temp = gpmc_t->we_on + dev_t->t_wpl;
929 temp = max_t(u32, temp,
930 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
931 temp = max_t(u32, temp,
932 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
933 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
934
935 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
936 dev_t->t_wph);
937
938 /* wr_cycle */
939 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
940 temp += gpmc_t->wr_access;
941 /* XXX: barter t_ce_rdyz with t_cez_w ? */
942 if (dev_t->t_ce_rdyz)
943 temp = max_t(u32, temp,
944 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
945 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
946
947 return 0;
948}
949
950static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600951 struct gpmc_device_timings *dev_t,
952 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530953{
Afzal Mohammed246da262012-08-02 20:02:10 +0530954 u32 temp;
955
956 /* adv_rd_off */
957 temp = dev_t->t_avdp_r;
958 if (mux)
959 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
960 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
961
962 /* oe_on */
963 temp = dev_t->t_oeasu;
964 if (mux)
965 temp = max_t(u32, temp,
966 gpmc_t->adv_rd_off + dev_t->t_aavdh);
967 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
968
969 /* access */
970 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
971 gpmc_t->oe_on + dev_t->t_oe);
972 temp = max_t(u32, temp,
973 gpmc_t->cs_on + dev_t->t_ce);
974 temp = max_t(u32, temp,
975 gpmc_t->adv_on + dev_t->t_aa);
976 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
977
978 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
979 gpmc_t->cs_rd_off = gpmc_t->oe_off;
980
981 /* rd_cycle */
982 temp = max_t(u32, dev_t->t_rd_cycle,
983 gpmc_t->cs_rd_off + dev_t->t_cez_r);
984 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
985 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
986
987 return 0;
988}
989
990static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600991 struct gpmc_device_timings *dev_t,
992 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530993{
Afzal Mohammed246da262012-08-02 20:02:10 +0530994 u32 temp;
995
996 /* adv_wr_off */
997 temp = dev_t->t_avdp_w;
998 if (mux)
999 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1000 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1001
1002 /* wr_data_mux_bus */
1003 temp = dev_t->t_weasu;
1004 if (mux) {
1005 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1006 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1007 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1008 }
1009 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1010
1011 /* we_on */
1012 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1013 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1014 else
1015 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1016
1017 /* we_off */
1018 temp = gpmc_t->we_on + dev_t->t_wpl;
1019 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1020
1021 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1022 dev_t->t_wph);
1023
1024 /* wr_cycle */
1025 temp = max_t(u32, dev_t->t_wr_cycle,
1026 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1027 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1028
1029 return 0;
1030}
1031
1032static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1033 struct gpmc_device_timings *dev_t)
1034{
1035 u32 temp;
1036
1037 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1038 gpmc_get_fclk_period();
1039
1040 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1041 dev_t->t_bacc,
1042 gpmc_t->sync_clk);
1043
1044 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1045 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1046
1047 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1048 return 0;
1049
1050 if (dev_t->ce_xdelay)
1051 gpmc_t->bool_timings.cs_extra_delay = true;
1052 if (dev_t->avd_xdelay)
1053 gpmc_t->bool_timings.adv_extra_delay = true;
1054 if (dev_t->oe_xdelay)
1055 gpmc_t->bool_timings.oe_extra_delay = true;
1056 if (dev_t->we_xdelay)
1057 gpmc_t->bool_timings.we_extra_delay = true;
1058
1059 return 0;
1060}
1061
1062static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001063 struct gpmc_device_timings *dev_t,
1064 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301065{
1066 u32 temp;
1067
1068 /* cs_on */
1069 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1070
1071 /* adv_on */
1072 temp = dev_t->t_avdasu;
1073 if (dev_t->t_ce_avd)
1074 temp = max_t(u32, temp,
1075 gpmc_t->cs_on + dev_t->t_ce_avd);
1076 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1077
Jon Hunterc3be5b42013-02-21 13:46:22 -06001078 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301079 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1080
1081 return 0;
1082}
1083
1084/* TODO: remove this function once all peripherals are confirmed to
1085 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1086 * has to be modified to handle timings in ps instead of ns
1087*/
1088static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1089{
1090 t->cs_on /= 1000;
1091 t->cs_rd_off /= 1000;
1092 t->cs_wr_off /= 1000;
1093 t->adv_on /= 1000;
1094 t->adv_rd_off /= 1000;
1095 t->adv_wr_off /= 1000;
1096 t->we_on /= 1000;
1097 t->we_off /= 1000;
1098 t->oe_on /= 1000;
1099 t->oe_off /= 1000;
1100 t->page_burst_access /= 1000;
1101 t->access /= 1000;
1102 t->rd_cycle /= 1000;
1103 t->wr_cycle /= 1000;
1104 t->bus_turnaround /= 1000;
1105 t->cycle2cycle_delay /= 1000;
1106 t->wait_monitoring /= 1000;
1107 t->clk_activation /= 1000;
1108 t->wr_access /= 1000;
1109 t->wr_data_mux_bus /= 1000;
1110}
1111
1112int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001113 struct gpmc_settings *gpmc_s,
1114 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301115{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001116 bool mux = false, sync = false;
1117
1118 if (gpmc_s) {
1119 mux = gpmc_s->mux_add_data ? true : false;
1120 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1121 }
1122
Afzal Mohammed246da262012-08-02 20:02:10 +05301123 memset(gpmc_t, 0, sizeof(*gpmc_t));
1124
Jon Hunterc3be5b42013-02-21 13:46:22 -06001125 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301126
Jon Hunterc3be5b42013-02-21 13:46:22 -06001127 if (gpmc_s && gpmc_s->sync_read)
1128 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301129 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001130 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301131
Jon Hunterc3be5b42013-02-21 13:46:22 -06001132 if (gpmc_s && gpmc_s->sync_write)
1133 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301134 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001135 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301136
1137 /* TODO: remove, see function definition */
1138 gpmc_convert_ps_to_ns(gpmc_t);
1139
1140 return 0;
1141}
1142
Jon Hunteraa8d4762013-02-21 15:25:23 -06001143/**
1144 * gpmc_cs_program_settings - programs non-timing related settings
1145 * @cs: GPMC chip-select to program
1146 * @p: pointer to GPMC settings structure
1147 *
1148 * Programs non-timing related settings for a GPMC chip-select, such as
1149 * bus-width, burst configuration, etc. Function should be called once
1150 * for each chip-select that is being used and must be called before
1151 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1152 * register will be initialised to zero by this function. Returns 0 on
1153 * success and appropriate negative error code on failure.
1154 */
1155int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1156{
1157 u32 config1;
1158
1159 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1160 pr_err("%s: invalid width %d!", __func__, p->device_width);
1161 return -EINVAL;
1162 }
1163
1164 /* Address-data multiplexing not supported for NAND devices */
1165 if (p->device_nand && p->mux_add_data) {
1166 pr_err("%s: invalid configuration!\n", __func__);
1167 return -EINVAL;
1168 }
1169
1170 if ((p->mux_add_data > GPMC_MUX_AD) ||
1171 ((p->mux_add_data == GPMC_MUX_AAD) &&
1172 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1173 pr_err("%s: invalid multiplex configuration!\n", __func__);
1174 return -EINVAL;
1175 }
1176
1177 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1178 if (p->burst_read || p->burst_write) {
1179 switch (p->burst_len) {
1180 case GPMC_BURST_4:
1181 case GPMC_BURST_8:
1182 case GPMC_BURST_16:
1183 break;
1184 default:
1185 pr_err("%s: invalid page/burst-length (%d)\n",
1186 __func__, p->burst_len);
1187 return -EINVAL;
1188 }
1189 }
1190
1191 if ((p->wait_on_read || p->wait_on_write) &&
1192 (p->wait_pin > gpmc_nr_waitpins)) {
1193 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1194 return -EINVAL;
1195 }
1196
1197 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1198
1199 if (p->sync_read)
1200 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1201 if (p->sync_write)
1202 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1203 if (p->wait_on_read)
1204 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1205 if (p->wait_on_write)
1206 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1207 if (p->wait_on_read || p->wait_on_write)
1208 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1209 if (p->device_nand)
1210 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1211 if (p->mux_add_data)
1212 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1213 if (p->burst_read)
1214 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1215 if (p->burst_write)
1216 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1217 if (p->burst_read || p->burst_write) {
1218 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1219 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1220 }
1221
1222 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1223
1224 return 0;
1225}
1226
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001227#ifdef CONFIG_OF
1228static struct of_device_id gpmc_dt_ids[] = {
1229 { .compatible = "ti,omap2420-gpmc" },
1230 { .compatible = "ti,omap2430-gpmc" },
1231 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1232 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1233 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1234 { }
1235};
1236MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1237
Jon Hunter8c8a77712013-02-20 15:53:12 -06001238/**
1239 * gpmc_read_settings_dt - read gpmc settings from device-tree
1240 * @np: pointer to device-tree node for a gpmc child device
1241 * @p: pointer to gpmc settings structure
1242 *
1243 * Reads the GPMC settings for a GPMC child device from device-tree and
1244 * stores them in the GPMC settings structure passed. The GPMC settings
1245 * structure is initialised to zero by this function and so any
1246 * previously stored settings will be cleared.
1247 */
1248void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1249{
1250 memset(p, 0, sizeof(struct gpmc_settings));
1251
1252 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1253 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
1254 p->device_nand = of_property_read_bool(np, "gpmc,device-nand");
1255 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1256 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1257
1258 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1259 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1260 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1261 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1262 if (!p->burst_read && !p->burst_write)
1263 pr_warn("%s: page/burst-length set but not used!\n",
1264 __func__);
1265 }
1266
1267 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1268 p->wait_on_read = of_property_read_bool(np,
1269 "gpmc,wait-on-read");
1270 p->wait_on_write = of_property_read_bool(np,
1271 "gpmc,wait-on-write");
1272 if (!p->wait_on_read && !p->wait_on_write)
1273 pr_warn("%s: read/write wait monitoring not enabled!\n",
1274 __func__);
1275 }
1276}
1277
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001278static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1279 struct gpmc_timings *gpmc_t)
1280{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001281 struct gpmc_bool_timings *p;
1282
1283 if (!np || !gpmc_t)
1284 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001285
1286 memset(gpmc_t, 0, sizeof(*gpmc_t));
1287
1288 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001289 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001290
1291 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001292 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1293 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1294 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001295
1296 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001297 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1298 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1299 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001300
1301 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001302 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1303 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001304
1305 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001306 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1307 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001308
1309 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001310 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1311 &gpmc_t->page_burst_access);
1312 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1313 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1314 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1315 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1316 &gpmc_t->bus_turnaround);
1317 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1318 &gpmc_t->cycle2cycle_delay);
1319 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1320 &gpmc_t->wait_monitoring);
1321 of_property_read_u32(np, "gpmc,clk-activation-ns",
1322 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001323
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001324 /* only applicable to OMAP3+ */
1325 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1326 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1327 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001328
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001329 /* bool timing parameters */
1330 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001331
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001332 p->cycle2cyclediffcsen =
1333 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1334 p->cycle2cyclesamecsen =
1335 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1336 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1337 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1338 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1339 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1340 p->time_para_granularity =
1341 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001342}
1343
1344#ifdef CONFIG_MTD_NAND
1345
1346static const char * const nand_ecc_opts[] = {
1347 [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
1348 [OMAP_ECC_HAMMING_CODE_HW] = "hw",
1349 [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
1350 [OMAP_ECC_BCH4_CODE_HW] = "bch4",
1351 [OMAP_ECC_BCH8_CODE_HW] = "bch8",
1352};
1353
1354static int gpmc_probe_nand_child(struct platform_device *pdev,
1355 struct device_node *child)
1356{
1357 u32 val;
1358 const char *s;
1359 struct gpmc_timings gpmc_t;
1360 struct omap_nand_platform_data *gpmc_nand_data;
1361
1362 if (of_property_read_u32(child, "reg", &val) < 0) {
1363 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1364 child->full_name);
1365 return -ENODEV;
1366 }
1367
1368 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1369 GFP_KERNEL);
1370 if (!gpmc_nand_data)
1371 return -ENOMEM;
1372
1373 gpmc_nand_data->cs = val;
1374 gpmc_nand_data->of_node = child;
1375
1376 if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
1377 for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
1378 if (!strcasecmp(s, nand_ecc_opts[val])) {
1379 gpmc_nand_data->ecc_opt = val;
1380 break;
1381 }
1382
1383 val = of_get_nand_bus_width(child);
1384 if (val == 16)
1385 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1386
1387 gpmc_read_timings_dt(child, &gpmc_t);
1388 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1389
1390 return 0;
1391}
1392#else
1393static int gpmc_probe_nand_child(struct platform_device *pdev,
1394 struct device_node *child)
1395{
1396 return 0;
1397}
1398#endif
1399
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001400#ifdef CONFIG_MTD_ONENAND
1401static int gpmc_probe_onenand_child(struct platform_device *pdev,
1402 struct device_node *child)
1403{
1404 u32 val;
1405 struct omap_onenand_platform_data *gpmc_onenand_data;
1406
1407 if (of_property_read_u32(child, "reg", &val) < 0) {
1408 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1409 child->full_name);
1410 return -ENODEV;
1411 }
1412
1413 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1414 GFP_KERNEL);
1415 if (!gpmc_onenand_data)
1416 return -ENOMEM;
1417
1418 gpmc_onenand_data->cs = val;
1419 gpmc_onenand_data->of_node = child;
1420 gpmc_onenand_data->dma_channel = -1;
1421
1422 if (!of_property_read_u32(child, "dma-channel", &val))
1423 gpmc_onenand_data->dma_channel = val;
1424
1425 gpmc_onenand_init(gpmc_onenand_data);
1426
1427 return 0;
1428}
1429#else
1430static int gpmc_probe_onenand_child(struct platform_device *pdev,
1431 struct device_node *child)
1432{
1433 return 0;
1434}
1435#endif
1436
Jon Huntercdd69282013-02-08 16:46:13 -06001437/**
1438 * gpmc_probe_nor_child - configures the gpmc for a nor device
1439 * @pdev: pointer to gpmc platform device
1440 * @child: pointer to device-tree node for nor device
1441 *
1442 * Allocates and configures a GPMC chip-select for a NOR flash device.
1443 * Returns 0 on success and appropriate negative error code on failure.
1444 */
1445static int gpmc_probe_nor_child(struct platform_device *pdev,
1446 struct device_node *child)
1447{
1448 struct gpmc_settings gpmc_s;
1449 struct gpmc_timings gpmc_t;
1450 struct resource res;
1451 unsigned long base;
1452 int ret, cs;
1453
1454 if (of_property_read_u32(child, "reg", &cs) < 0) {
1455 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1456 child->full_name);
1457 return -ENODEV;
1458 }
1459
1460 if (of_address_to_resource(child, 0, &res) < 0) {
1461 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1462 child->full_name);
1463 return -ENODEV;
1464 }
1465
1466 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1467 if (ret < 0) {
1468 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1469 return ret;
1470 }
1471
1472 /*
1473 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1474 * location in the gpmc address space. When booting with
1475 * device-tree we want the NOR flash to be mapped to the
1476 * location specified in the device-tree blob. So remap the
1477 * CS to this location. Once DT migration is complete should
1478 * just make gpmc_cs_request() map a specific address.
1479 */
1480 ret = gpmc_cs_remap(cs, res.start);
1481 if (ret < 0) {
1482 dev_err(&pdev->dev, "cannot remap GPMC CS %d to 0x%x\n",
1483 cs, res.start);
1484 goto err;
1485 }
1486
1487 gpmc_read_settings_dt(child, &gpmc_s);
1488
1489 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1490 if (ret < 0)
1491 goto err;
1492
1493 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1494 if (ret < 0)
1495 goto err;
1496
1497 gpmc_read_timings_dt(child, &gpmc_t);
1498 gpmc_cs_set_timings(cs, &gpmc_t);
1499
1500 if (of_platform_device_create(child, NULL, &pdev->dev))
1501 return 0;
1502
1503 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
1504
1505err:
1506 gpmc_cs_free(cs);
1507
1508 return ret;
1509}
1510
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001511static int gpmc_probe_dt(struct platform_device *pdev)
1512{
1513 int ret;
1514 struct device_node *child;
1515 const struct of_device_id *of_id =
1516 of_match_device(gpmc_dt_ids, &pdev->dev);
1517
1518 if (!of_id)
1519 return 0;
1520
Jon Hunter9f833152013-02-20 15:53:38 -06001521 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1522 &gpmc_nr_waitpins);
1523 if (ret < 0) {
1524 pr_err("%s: number of wait pins not found!\n", __func__);
1525 return ret;
1526 }
1527
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001528 for_each_node_by_name(child, "nand") {
1529 ret = gpmc_probe_nand_child(pdev, child);
Ezequiel Garciaa1672372013-01-25 09:19:47 -03001530 if (ret < 0) {
1531 of_node_put(child);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001532 return ret;
Ezequiel Garciaa1672372013-01-25 09:19:47 -03001533 }
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001534 }
1535
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001536 for_each_node_by_name(child, "onenand") {
1537 ret = gpmc_probe_onenand_child(pdev, child);
1538 if (ret < 0) {
1539 of_node_put(child);
1540 return ret;
1541 }
1542 }
Jon Huntercdd69282013-02-08 16:46:13 -06001543
1544 for_each_node_by_name(child, "nor") {
1545 ret = gpmc_probe_nor_child(pdev, child);
1546 if (ret < 0) {
1547 of_node_put(child);
1548 return ret;
1549 }
1550 }
1551
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001552 return 0;
1553}
1554#else
1555static int gpmc_probe_dt(struct platform_device *pdev)
1556{
1557 return 0;
1558}
1559#endif
1560
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001561static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001562{
Jon Hunter81190242012-10-17 09:41:25 -05001563 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001564 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001565 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001566
Afzal Mohammedda496872012-09-23 17:28:25 -06001567 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1568 if (res == NULL)
1569 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001570
Afzal Mohammedda496872012-09-23 17:28:25 -06001571 phys_base = res->start;
1572 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001573
Thierry Reding5857bd92013-01-21 11:08:55 +01001574 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1575 if (IS_ERR(gpmc_base))
1576 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001577
1578 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1579 if (res == NULL)
1580 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1581 else
1582 gpmc_irq = res->start;
1583
1584 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1585 if (IS_ERR(gpmc_l3_clk)) {
1586 dev_err(&pdev->dev, "error: clk_get\n");
1587 gpmc_irq = 0;
1588 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001589 }
1590
Rajendra Nayak4d7cb452012-09-22 02:24:16 -06001591 clk_prepare_enable(gpmc_l3_clk);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001592
Afzal Mohammedda496872012-09-23 17:28:25 -06001593 gpmc_dev = &pdev->dev;
1594
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001595 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001596
1597 /*
1598 * FIXME: Once device-tree migration is complete the below flags
1599 * should be populated based upon the device-tree compatible
1600 * string. For now just use the IP revision. OMAP3+ devices have
1601 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1602 * devices support the addr-addr-data multiplex protocol.
1603 *
1604 * GPMC IP revisions:
1605 * - OMAP24xx = 2.0
1606 * - OMAP3xxx = 5.0
1607 * - OMAP44xx/54xx/AM335x = 6.0
1608 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001609 if (GPMC_REVISION_MAJOR(l) > 0x4)
1610 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001611 if (GPMC_REVISION_MAJOR(l) > 0x5)
1612 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001613 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1614 GPMC_REVISION_MINOR(l));
1615
Jon Hunter81190242012-10-17 09:41:25 -05001616 rc = gpmc_mem_init();
Russell King71856842013-03-13 20:44:21 +00001617 if (rc < 0) {
Jon Hunter81190242012-10-17 09:41:25 -05001618 clk_disable_unprepare(gpmc_l3_clk);
1619 clk_put(gpmc_l3_clk);
1620 dev_err(gpmc_dev, "failed to reserve memory\n");
1621 return rc;
1622 }
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301623
Russell King71856842013-03-13 20:44:21 +00001624 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001625 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1626
Jon Hunter31d9adc2013-02-18 07:57:39 -06001627 /* Now the GPMC is initialised, unreserve the chip-selects */
1628 gpmc_cs_map = 0;
1629
Jon Hunter9f833152013-02-20 15:53:38 -06001630 if (!pdev->dev.of_node)
1631 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
1632
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001633 rc = gpmc_probe_dt(pdev);
1634 if (rc < 0) {
1635 clk_disable_unprepare(gpmc_l3_clk);
1636 clk_put(gpmc_l3_clk);
1637 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1638 return rc;
1639 }
1640
Afzal Mohammedda496872012-09-23 17:28:25 -06001641 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301642}
Afzal Mohammedda496872012-09-23 17:28:25 -06001643
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001644static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001645{
1646 gpmc_free_irq();
1647 gpmc_mem_exit();
1648 gpmc_dev = NULL;
1649 return 0;
1650}
1651
1652static struct platform_driver gpmc_driver = {
1653 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001654 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001655 .driver = {
1656 .name = DEVICE_NAME,
1657 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001658 .of_match_table = of_match_ptr(gpmc_dt_ids),
Afzal Mohammedda496872012-09-23 17:28:25 -06001659 },
1660};
1661
1662static __init int gpmc_init(void)
1663{
1664 return platform_driver_register(&gpmc_driver);
1665}
1666
1667static __exit void gpmc_exit(void)
1668{
1669 platform_driver_unregister(&gpmc_driver);
1670
1671}
1672
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001673omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001674module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301675
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001676static int __init omap_gpmc_init(void)
1677{
1678 struct omap_hwmod *oh;
1679 struct platform_device *pdev;
1680 char *oh_name = "gpmc";
1681
Daniel Mack2f98ca82012-12-14 11:36:40 +01001682 /*
1683 * if the board boots up with a populated DT, do not
1684 * manually add the device from this initcall
1685 */
1686 if (of_have_populated_dt())
1687 return -ENODEV;
1688
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001689 oh = omap_hwmod_lookup(oh_name);
1690 if (!oh) {
1691 pr_err("Could not look up %s\n", oh_name);
1692 return -ENODEV;
1693 }
1694
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001695 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001696 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1697
1698 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
1699}
Tony Lindgrenb76c8b192013-01-11 11:24:18 -08001700omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001701
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301702static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1703{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001704 int i;
1705 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301706
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001707 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1708
1709 if (!regval)
1710 return IRQ_NONE;
1711
1712 for (i = 0; i < GPMC_NR_IRQ; i++)
1713 if (regval & gpmc_client_irq[i].bitmask)
1714 generic_handle_irq(gpmc_client_irq[i].irq);
1715
1716 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301717
1718 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001719}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301720
1721#ifdef CONFIG_ARCH_OMAP3
1722static struct omap3_gpmc_regs gpmc_context;
1723
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001724void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301725{
1726 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001727
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301728 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1729 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1730 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1731 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1732 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1733 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1734 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
1735 for (i = 0; i < GPMC_CS_NUM; i++) {
1736 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1737 if (gpmc_context.cs_context[i].is_valid) {
1738 gpmc_context.cs_context[i].config1 =
1739 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1740 gpmc_context.cs_context[i].config2 =
1741 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1742 gpmc_context.cs_context[i].config3 =
1743 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1744 gpmc_context.cs_context[i].config4 =
1745 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1746 gpmc_context.cs_context[i].config5 =
1747 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1748 gpmc_context.cs_context[i].config6 =
1749 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1750 gpmc_context.cs_context[i].config7 =
1751 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1752 }
1753 }
1754}
1755
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001756void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301757{
1758 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001759
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301760 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1761 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1762 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1763 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1764 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1765 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1766 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
1767 for (i = 0; i < GPMC_CS_NUM; i++) {
1768 if (gpmc_context.cs_context[i].is_valid) {
1769 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1770 gpmc_context.cs_context[i].config1);
1771 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1772 gpmc_context.cs_context[i].config2);
1773 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1774 gpmc_context.cs_context[i].config3);
1775 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1776 gpmc_context.cs_context[i].config4);
1777 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1778 gpmc_context.cs_context[i].config5);
1779 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1780 gpmc_context.cs_context[i].config6);
1781 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1782 gpmc_context.cs_context[i].config7);
1783 }
1784 }
1785}
1786#endif /* CONFIG_ARCH_OMAP3 */