blob: 85231b3a217e650ba4ee156ab887f1d395a0081e [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>
29#include <linux/of_mtd.h>
30#include <linux/of_device.h>
31#include <linux/mtd/nand.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070032
Afzal Mohammedbc3668e2012-09-29 12:26:13 +053033#include <linux/platform_data/mtd-nand-omap2.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070034
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070035#include <asm/mach-types.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070036
Tony Lindgrendbc04162012-08-31 10:59:07 -070037#include "soc.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070038#include "common.h"
Tony Lindgren25c7d492012-10-02 17:25:48 -070039#include "omap_device.h"
Afzal Mohammed3ef5d002012-10-05 10:37:27 +053040#include "gpmc.h"
Daniel Mackbc6b1e72012-12-14 11:36:44 +010041#include "gpmc-nand.h"
Ezequiel Garcia75d36252013-01-25 09:23:11 -030042#include "gpmc-onenand.h"
Tony Lindgren7d7e1eb2012-08-27 17:43:01 -070043
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060044#define DEVICE_NAME "omap-gpmc"
45
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030046/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070047#define GPMC_REVISION 0x00
48#define GPMC_SYSCONFIG 0x10
49#define GPMC_SYSSTATUS 0x14
50#define GPMC_IRQSTATUS 0x18
51#define GPMC_IRQENABLE 0x1c
52#define GPMC_TIMEOUT_CONTROL 0x40
53#define GPMC_ERR_ADDRESS 0x44
54#define GPMC_ERR_TYPE 0x48
55#define GPMC_CONFIG 0x50
56#define GPMC_STATUS 0x54
57#define GPMC_PREFETCH_CONFIG1 0x1e0
58#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053059#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070060#define GPMC_PREFETCH_STATUS 0x1f0
61#define GPMC_ECC_CONFIG 0x1f4
62#define GPMC_ECC_CONTROL 0x1f8
63#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000064#define GPMC_ECC1_RESULT 0x200
Ivan Djelic8d602cf2012-04-26 14:17:49 +020065#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053066#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
67#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
68#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070069
Yegor Yefremov2c65e742012-05-09 08:32:49 -070070/* GPMC ECC control settings */
71#define GPMC_ECC_CTRL_ECCCLEAR 0x100
72#define GPMC_ECC_CTRL_ECCDISABLE 0x000
73#define GPMC_ECC_CTRL_ECCREG1 0x001
74#define GPMC_ECC_CTRL_ECCREG2 0x002
75#define GPMC_ECC_CTRL_ECCREG3 0x003
76#define GPMC_ECC_CTRL_ECCREG4 0x004
77#define GPMC_ECC_CTRL_ECCREG5 0x005
78#define GPMC_ECC_CTRL_ECCREG6 0x006
79#define GPMC_ECC_CTRL_ECCREG7 0x007
80#define GPMC_ECC_CTRL_ECCREG8 0x008
81#define GPMC_ECC_CTRL_ECCREG9 0x009
82
Afzal Mohammed559d94b2012-05-28 17:51:37 +053083#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
84#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
85#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
86#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
87#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
88#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
89
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000090#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070091#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053092#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070093
Imre Deakf37e4582006-09-25 12:41:33 +030094#define GPMC_MEM_START 0x00000000
95#define GPMC_MEM_END 0x3FFFFFFF
96#define BOOT_ROM_SPACE 0x100000 /* 1MB */
97
98#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
99#define GPMC_SECTION_SHIFT 28 /* 128 MB */
100
vimal singh59e9c5a2009-07-13 16:26:24 +0530101#define CS_NUM_SHIFT 24
102#define ENABLE_PREFETCH (0x1 << 7)
103#define DMA_MPU_MODE 2
104
Afzal Mohammedda496872012-09-23 17:28:25 -0600105#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
106#define GPMC_REVISION_MINOR(l) (l & 0xf)
107
108#define GPMC_HAS_WR_ACCESS 0x1
109#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
Jon Hunteraa8d4762013-02-21 15:25:23 -0600110#define GPMC_HAS_MUX_AAD 0x4
Afzal Mohammedda496872012-09-23 17:28:25 -0600111
Jon Hunter9f833152013-02-20 15:53:38 -0600112#define GPMC_NR_WAITPINS 4
113
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700114/* XXX: Only NAND irq has been considered,currently these are the only ones used
115 */
116#define GPMC_NR_IRQ 2
117
118struct gpmc_client_irq {
119 unsigned irq;
120 u32 bitmask;
121};
122
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530123/* Structure to save gpmc cs context */
124struct gpmc_cs_config {
125 u32 config1;
126 u32 config2;
127 u32 config3;
128 u32 config4;
129 u32 config5;
130 u32 config6;
131 u32 config7;
132 int is_valid;
133};
134
135/*
136 * Structure to save/restore gpmc context
137 * to support core off on OMAP3
138 */
139struct omap3_gpmc_regs {
140 u32 sysconfig;
141 u32 irqenable;
142 u32 timeout_ctrl;
143 u32 config;
144 u32 prefetch_config1;
145 u32 prefetch_config2;
146 u32 prefetch_control;
147 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
148};
149
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700150static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
151static struct irq_chip gpmc_irq_chip;
152static unsigned gpmc_irq_start;
153
Imre Deakf37e4582006-09-25 12:41:33 +0300154static struct resource gpmc_mem_root;
155static struct resource gpmc_cs_mem[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700156static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600157/* Define chip-selects as reserved by default until probe completes */
158static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
Jon Hunter9f833152013-02-20 15:53:38 -0600159static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600160static struct device *gpmc_dev;
161static int gpmc_irq;
162static resource_size_t phys_base, mem_size;
163static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300164static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700165
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300166static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700167
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530168static irqreturn_t gpmc_handle_irq(int irq, void *dev);
169
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700170static void gpmc_write_reg(int idx, u32 val)
171{
172 __raw_writel(val, gpmc_base + idx);
173}
174
175static u32 gpmc_read_reg(int idx)
176{
177 return __raw_readl(gpmc_base + idx);
178}
179
180void gpmc_cs_write_reg(int cs, int idx, u32 val)
181{
182 void __iomem *reg_addr;
183
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000184 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700185 __raw_writel(val, reg_addr);
186}
187
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300188static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700189{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300190 void __iomem *reg_addr;
191
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000192 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300193 return __raw_readl(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700194}
195
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300196/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300197static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700198{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300199 unsigned long rate = clk_get_rate(gpmc_l3_clk);
200
201 if (rate == 0) {
202 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
203 return 0;
204 }
205
206 rate /= 1000;
207 rate = 1000000000 / rate; /* In picoseconds */
208
209 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700210}
211
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300212static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700213{
214 unsigned long tick_ps;
215
216 /* Calculate in picosecs to yield more exact results */
217 tick_ps = gpmc_get_fclk_period();
218
219 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
220}
221
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300222static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200223{
224 unsigned long tick_ps;
225
226 /* Calculate in picosecs to yield more exact results */
227 tick_ps = gpmc_get_fclk_period();
228
229 return (time_ps + tick_ps - 1) / tick_ps;
230}
231
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300232unsigned int gpmc_ticks_to_ns(unsigned int ticks)
233{
234 return ticks * gpmc_get_fclk_period() / 1000;
235}
236
Afzal Mohammed246da262012-08-02 20:02:10 +0530237static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
238{
239 return ticks * gpmc_get_fclk_period();
240}
241
242static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
243{
244 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
245
246 return ticks * gpmc_get_fclk_period();
247}
248
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530249static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
250{
251 u32 l;
252
253 l = gpmc_cs_read_reg(cs, reg);
254 if (value)
255 l |= mask;
256 else
257 l &= ~mask;
258 gpmc_cs_write_reg(cs, reg, l);
259}
260
261static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
262{
263 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
264 GPMC_CONFIG1_TIME_PARA_GRAN,
265 p->time_para_granularity);
266 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
267 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
268 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
269 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
270 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
271 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
272 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
273 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
274 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
275 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
276 p->cycle2cyclesamecsen);
277 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
278 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
279 p->cycle2cyclediffcsen);
280}
281
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700282#ifdef DEBUG
283static 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#else
286static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
287 int time)
288#endif
289{
290 u32 l;
291 int ticks, mask, nr_bits;
292
293 if (time == 0)
294 ticks = 0;
295 else
296 ticks = gpmc_ns_to_ticks(time);
297 nr_bits = end_bit - st_bit + 1;
David Brownell1c22cc12006-12-06 17:13:55 -0800298 if (ticks >= 1 << nr_bits) {
299#ifdef DEBUG
300 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
301 cs, name, time, ticks, 1 << nr_bits);
302#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700303 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800304 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700305
306 mask = (1 << nr_bits) - 1;
307 l = gpmc_cs_read_reg(cs, reg);
308#ifdef DEBUG
David Brownell1c22cc12006-12-06 17:13:55 -0800309 printk(KERN_INFO
310 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Juha Yrjola2aab6462006-06-26 16:16:21 -0700311 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800312 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700313#endif
314 l &= ~(mask << st_bit);
315 l |= ticks << st_bit;
316 gpmc_cs_write_reg(cs, reg, l);
317
318 return 0;
319}
320
321#ifdef DEBUG
322#define GPMC_SET_ONE(reg, st, end, field) \
323 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
324 t->field, #field) < 0) \
325 return -1
326#else
327#define GPMC_SET_ONE(reg, st, end, field) \
328 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
329 return -1
330#endif
331
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530332int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700333{
334 int div;
335 u32 l;
336
Adrian Huntera3551f52010-12-09 10:48:27 +0200337 l = sync_clk + (gpmc_get_fclk_period() - 1);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700338 div = l / gpmc_get_fclk_period();
339 if (div > 4)
340 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800341 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700342 div = 1;
343
344 return div;
345}
346
347int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
348{
349 int div;
350 u32 l;
351
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530352 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700353 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600354 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700355
356 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
357 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
358 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
359
360 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
361 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
362 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
363
364 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
365 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
366 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
367 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
368
369 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
370 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
371 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
372
373 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
374
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530375 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
376 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
377
378 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
379 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
380
Afzal Mohammedda496872012-09-23 17:28:25 -0600381 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300382 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600383 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300384 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300385
David Brownell1c22cc12006-12-06 17:13:55 -0800386 /* caller is expected to have initialized CONFIG1 to cover
387 * at least sync vs async
388 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700389 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
David Brownell1c22cc12006-12-06 17:13:55 -0800390 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
391#ifdef DEBUG
392 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
393 cs, (div * gpmc_get_fclk_period()) / 1000, div);
394#endif
395 l &= ~0x03;
396 l |= (div - 1);
397 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
398 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700399
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530400 gpmc_cs_bool_timings(cs, &t->bool_timings);
401
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700402 return 0;
403}
404
Imre Deakf37e4582006-09-25 12:41:33 +0300405static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700406{
Imre Deakf37e4582006-09-25 12:41:33 +0300407 u32 l;
408 u32 mask;
409
410 mask = (1 << GPMC_SECTION_SHIFT) - size;
411 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
412 l &= ~0x3f;
413 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
414 l &= ~(0x0f << 8);
415 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530416 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300417 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
418}
419
420static void gpmc_cs_disable_mem(int cs)
421{
422 u32 l;
423
424 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530425 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300426 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
427}
428
429static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
430{
431 u32 l;
432 u32 mask;
433
434 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
435 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
436 mask = (l >> 8) & 0x0f;
437 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
438}
439
440static int gpmc_cs_mem_enabled(int cs)
441{
442 u32 l;
443
444 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530445 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300446}
447
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300448static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300449{
450 gpmc_cs_map &= ~(1 << cs);
451 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
452}
453
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300454static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300455{
456 return gpmc_cs_map & (1 << cs);
457}
458
459static unsigned long gpmc_mem_align(unsigned long size)
460{
461 int order;
462
463 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
464 order = GPMC_CHUNK_SHIFT - 1;
465 do {
466 size >>= 1;
467 order++;
468 } while (size);
469 size = 1 << order;
470 return size;
471}
472
473static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
474{
475 struct resource *res = &gpmc_cs_mem[cs];
476 int r;
477
478 size = gpmc_mem_align(size);
479 spin_lock(&gpmc_mem_lock);
480 res->start = base;
481 res->end = base + size - 1;
482 r = request_resource(&gpmc_mem_root, res);
483 spin_unlock(&gpmc_mem_lock);
484
485 return r;
486}
487
Afzal Mohammedda496872012-09-23 17:28:25 -0600488static int gpmc_cs_delete_mem(int cs)
489{
490 struct resource *res = &gpmc_cs_mem[cs];
491 int r;
492
493 spin_lock(&gpmc_mem_lock);
494 r = release_resource(&gpmc_cs_mem[cs]);
495 res->start = 0;
496 res->end = 0;
497 spin_unlock(&gpmc_mem_lock);
498
499 return r;
500}
501
Imre Deakf37e4582006-09-25 12:41:33 +0300502int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
503{
504 struct resource *res = &gpmc_cs_mem[cs];
505 int r = -1;
506
507 if (cs > GPMC_CS_NUM)
508 return -ENODEV;
509
510 size = gpmc_mem_align(size);
511 if (size > (1 << GPMC_SECTION_SHIFT))
512 return -ENOMEM;
513
514 spin_lock(&gpmc_mem_lock);
515 if (gpmc_cs_reserved(cs)) {
516 r = -EBUSY;
517 goto out;
518 }
519 if (gpmc_cs_mem_enabled(cs))
520 r = adjust_resource(res, res->start & ~(size - 1), size);
521 if (r < 0)
522 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
523 size, NULL, NULL);
524 if (r < 0)
525 goto out;
526
Tobias Klauser6d135242009-11-10 18:55:19 -0800527 gpmc_cs_enable_mem(cs, res->start, resource_size(res));
Imre Deakf37e4582006-09-25 12:41:33 +0300528 *base = res->start;
529 gpmc_cs_set_reserved(cs, 1);
530out:
531 spin_unlock(&gpmc_mem_lock);
532 return r;
533}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300534EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +0300535
536void gpmc_cs_free(int cs)
537{
538 spin_lock(&gpmc_mem_lock);
Roel Kluine7fdc602009-11-17 14:39:06 -0800539 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +0300540 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
541 BUG();
542 spin_unlock(&gpmc_mem_lock);
543 return;
544 }
545 gpmc_cs_disable_mem(cs);
546 release_resource(&gpmc_cs_mem[cs]);
547 gpmc_cs_set_reserved(cs, 0);
548 spin_unlock(&gpmc_mem_lock);
549}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300550EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +0300551
vimal singh59e9c5a2009-07-13 16:26:24 +0530552/**
Jon Hunter3a544352013-02-21 13:00:21 -0600553 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000554 * @cmd: command type
555 * @wval: value to write
556 * @return status of the operation
557 */
Jon Hunter3a544352013-02-21 13:00:21 -0600558int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000559{
Jon Hunter3a544352013-02-21 13:00:21 -0600560 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000561
562 switch (cmd) {
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530563 case GPMC_ENABLE_IRQ:
564 gpmc_write_reg(GPMC_IRQENABLE, wval);
565 break;
566
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000567 case GPMC_SET_IRQ_STATUS:
568 gpmc_write_reg(GPMC_IRQSTATUS, wval);
569 break;
570
571 case GPMC_CONFIG_WP:
572 regval = gpmc_read_reg(GPMC_CONFIG);
573 if (wval)
574 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
575 else
576 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
577 gpmc_write_reg(GPMC_CONFIG, regval);
578 break;
579
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000580 default:
Jon Hunter3a544352013-02-21 13:00:21 -0600581 pr_err("%s: command not supported\n", __func__);
582 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000583 }
584
Jon Hunter3a544352013-02-21 13:00:21 -0600585 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000586}
Jon Hunter3a544352013-02-21 13:00:21 -0600587EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000588
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700589void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
590{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530591 int i;
592
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700593 reg->gpmc_status = gpmc_base + GPMC_STATUS;
594 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
595 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
596 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
597 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
598 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
599 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
600 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
601 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
602 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
603 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
604 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
605 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
606 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
607 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +0530608
609 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
610 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
611 GPMC_BCH_SIZE * i;
612 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
613 GPMC_BCH_SIZE * i;
614 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
615 GPMC_BCH_SIZE * i;
616 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
617 GPMC_BCH_SIZE * i;
618 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -0700619}
620
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700621int gpmc_get_client_irq(unsigned irq_config)
622{
623 int i;
624
625 if (hweight32(irq_config) > 1)
626 return 0;
627
628 for (i = 0; i < GPMC_NR_IRQ; i++)
629 if (gpmc_client_irq[i].bitmask & irq_config)
630 return gpmc_client_irq[i].irq;
631
632 return 0;
633}
634
635static int gpmc_irq_endis(unsigned irq, bool endis)
636{
637 int i;
638 u32 regval;
639
640 for (i = 0; i < GPMC_NR_IRQ; i++)
641 if (irq == gpmc_client_irq[i].irq) {
642 regval = gpmc_read_reg(GPMC_IRQENABLE);
643 if (endis)
644 regval |= gpmc_client_irq[i].bitmask;
645 else
646 regval &= ~gpmc_client_irq[i].bitmask;
647 gpmc_write_reg(GPMC_IRQENABLE, regval);
648 break;
649 }
650
651 return 0;
652}
653
654static void gpmc_irq_disable(struct irq_data *p)
655{
656 gpmc_irq_endis(p->irq, false);
657}
658
659static void gpmc_irq_enable(struct irq_data *p)
660{
661 gpmc_irq_endis(p->irq, true);
662}
663
664static void gpmc_irq_noop(struct irq_data *data) { }
665
666static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
667
Afzal Mohammedda496872012-09-23 17:28:25 -0600668static int gpmc_setup_irq(void)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700669{
670 int i;
671 u32 regval;
672
673 if (!gpmc_irq)
674 return -EINVAL;
675
676 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
Russell King71856842013-03-13 20:44:21 +0000677 if (gpmc_irq_start < 0) {
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700678 pr_err("irq_alloc_descs failed\n");
679 return gpmc_irq_start;
680 }
681
682 gpmc_irq_chip.name = "gpmc";
683 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
684 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
685 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
686 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
687 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
688 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
689 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
690
691 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
692 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
693
694 for (i = 0; i < GPMC_NR_IRQ; i++) {
695 gpmc_client_irq[i].irq = gpmc_irq_start + i;
696 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
697 &gpmc_irq_chip, handle_simple_irq);
698 set_irq_flags(gpmc_client_irq[i].irq,
699 IRQF_VALID | IRQF_NOAUTOEN);
700 }
701
702 /* Disable interrupts */
703 gpmc_write_reg(GPMC_IRQENABLE, 0);
704
705 /* clear interrupts */
706 regval = gpmc_read_reg(GPMC_IRQSTATUS);
707 gpmc_write_reg(GPMC_IRQSTATUS, regval);
708
709 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
710}
711
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800712static int gpmc_free_irq(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600713{
714 int i;
715
716 if (gpmc_irq)
717 free_irq(gpmc_irq, NULL);
718
719 for (i = 0; i < GPMC_NR_IRQ; i++) {
720 irq_set_handler(gpmc_client_irq[i].irq, NULL);
721 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
722 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
723 }
724
725 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
726
727 return 0;
728}
729
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800730static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -0600731{
732 int cs;
733
734 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
735 if (!gpmc_cs_mem_enabled(cs))
736 continue;
737 gpmc_cs_delete_mem(cs);
738 }
739
740}
741
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -0800742static int gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +0300743{
Jon Hunter81190242012-10-17 09:41:25 -0500744 int cs, rc;
Imre Deakf37e4582006-09-25 12:41:33 +0300745 unsigned long boot_rom_space = 0;
746
Kyungmin Park7f245162006-12-29 16:48:51 -0800747 /* never allocate the first page, to facilitate bug detection;
748 * even if we didn't boot from ROM.
749 */
750 boot_rom_space = BOOT_ROM_SPACE;
Imre Deakf37e4582006-09-25 12:41:33 +0300751 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
752 gpmc_mem_root.end = GPMC_MEM_END;
753
754 /* Reserve all regions that has been set up by bootloader */
755 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
756 u32 base, size;
757
758 if (!gpmc_cs_mem_enabled(cs))
759 continue;
760 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter81190242012-10-17 09:41:25 -0500761 rc = gpmc_cs_insert_mem(cs, base, size);
Russell King71856842013-03-13 20:44:21 +0000762 if (rc < 0) {
Jon Hunter81190242012-10-17 09:41:25 -0500763 while (--cs >= 0)
764 if (gpmc_cs_mem_enabled(cs))
765 gpmc_cs_delete_mem(cs);
766 return rc;
767 }
Imre Deakf37e4582006-09-25 12:41:33 +0300768 }
Jon Hunter81190242012-10-17 09:41:25 -0500769
770 return 0;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700771}
772
Afzal Mohammed246da262012-08-02 20:02:10 +0530773static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
774{
775 u32 temp;
776 int div;
777
778 div = gpmc_calc_divider(sync_clk);
779 temp = gpmc_ps_to_ticks(time_ps);
780 temp = (temp + div - 1) / div;
781 return gpmc_ticks_to_ps(temp * div);
782}
783
784/* XXX: can the cycles be avoided ? */
785static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600786 struct gpmc_device_timings *dev_t,
787 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530788{
Afzal Mohammed246da262012-08-02 20:02:10 +0530789 u32 temp;
790
791 /* adv_rd_off */
792 temp = dev_t->t_avdp_r;
793 /* XXX: mux check required ? */
794 if (mux) {
795 /* XXX: t_avdp not to be required for sync, only added for tusb
796 * this indirectly necessitates requirement of t_avdp_r and
797 * t_avdp_w instead of having a single t_avdp
798 */
799 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
800 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
801 }
802 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
803
804 /* oe_on */
805 temp = dev_t->t_oeasu; /* XXX: remove this ? */
806 if (mux) {
807 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
808 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
809 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
810 }
811 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
812
813 /* access */
814 /* XXX: any scope for improvement ?, by combining oe_on
815 * and clk_activation, need to check whether
816 * access = clk_activation + round to sync clk ?
817 */
818 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
819 temp += gpmc_t->clk_activation;
820 if (dev_t->cyc_oe)
821 temp = max_t(u32, temp, gpmc_t->oe_on +
822 gpmc_ticks_to_ps(dev_t->cyc_oe));
823 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
824
825 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
826 gpmc_t->cs_rd_off = gpmc_t->oe_off;
827
828 /* rd_cycle */
829 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
830 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
831 gpmc_t->access;
832 /* XXX: barter t_ce_rdyz with t_cez_r ? */
833 if (dev_t->t_ce_rdyz)
834 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
835 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
836
837 return 0;
838}
839
840static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600841 struct gpmc_device_timings *dev_t,
842 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530843{
Afzal Mohammed246da262012-08-02 20:02:10 +0530844 u32 temp;
845
846 /* adv_wr_off */
847 temp = dev_t->t_avdp_w;
848 if (mux) {
849 temp = max_t(u32, temp,
850 gpmc_t->clk_activation + dev_t->t_avdh);
851 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
852 }
853 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
854
855 /* wr_data_mux_bus */
856 temp = max_t(u32, dev_t->t_weasu,
857 gpmc_t->clk_activation + dev_t->t_rdyo);
858 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
859 * and in that case remember to handle we_on properly
860 */
861 if (mux) {
862 temp = max_t(u32, temp,
863 gpmc_t->adv_wr_off + dev_t->t_aavdh);
864 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
865 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
866 }
867 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
868
869 /* we_on */
870 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
871 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
872 else
873 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
874
875 /* wr_access */
876 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
877 gpmc_t->wr_access = gpmc_t->access;
878
879 /* we_off */
880 temp = gpmc_t->we_on + dev_t->t_wpl;
881 temp = max_t(u32, temp,
882 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
883 temp = max_t(u32, temp,
884 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
885 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
886
887 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
888 dev_t->t_wph);
889
890 /* wr_cycle */
891 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
892 temp += gpmc_t->wr_access;
893 /* XXX: barter t_ce_rdyz with t_cez_w ? */
894 if (dev_t->t_ce_rdyz)
895 temp = max_t(u32, temp,
896 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
897 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
898
899 return 0;
900}
901
902static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600903 struct gpmc_device_timings *dev_t,
904 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530905{
Afzal Mohammed246da262012-08-02 20:02:10 +0530906 u32 temp;
907
908 /* adv_rd_off */
909 temp = dev_t->t_avdp_r;
910 if (mux)
911 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
912 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
913
914 /* oe_on */
915 temp = dev_t->t_oeasu;
916 if (mux)
917 temp = max_t(u32, temp,
918 gpmc_t->adv_rd_off + dev_t->t_aavdh);
919 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
920
921 /* access */
922 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
923 gpmc_t->oe_on + dev_t->t_oe);
924 temp = max_t(u32, temp,
925 gpmc_t->cs_on + dev_t->t_ce);
926 temp = max_t(u32, temp,
927 gpmc_t->adv_on + dev_t->t_aa);
928 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
929
930 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
931 gpmc_t->cs_rd_off = gpmc_t->oe_off;
932
933 /* rd_cycle */
934 temp = max_t(u32, dev_t->t_rd_cycle,
935 gpmc_t->cs_rd_off + dev_t->t_cez_r);
936 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
937 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
938
939 return 0;
940}
941
942static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -0600943 struct gpmc_device_timings *dev_t,
944 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +0530945{
Afzal Mohammed246da262012-08-02 20:02:10 +0530946 u32 temp;
947
948 /* adv_wr_off */
949 temp = dev_t->t_avdp_w;
950 if (mux)
951 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
952 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
953
954 /* wr_data_mux_bus */
955 temp = dev_t->t_weasu;
956 if (mux) {
957 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
958 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
959 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
960 }
961 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
962
963 /* we_on */
964 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
965 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
966 else
967 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
968
969 /* we_off */
970 temp = gpmc_t->we_on + dev_t->t_wpl;
971 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
972
973 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
974 dev_t->t_wph);
975
976 /* wr_cycle */
977 temp = max_t(u32, dev_t->t_wr_cycle,
978 gpmc_t->cs_wr_off + dev_t->t_cez_w);
979 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
980
981 return 0;
982}
983
984static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
985 struct gpmc_device_timings *dev_t)
986{
987 u32 temp;
988
989 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
990 gpmc_get_fclk_period();
991
992 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
993 dev_t->t_bacc,
994 gpmc_t->sync_clk);
995
996 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
997 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
998
999 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1000 return 0;
1001
1002 if (dev_t->ce_xdelay)
1003 gpmc_t->bool_timings.cs_extra_delay = true;
1004 if (dev_t->avd_xdelay)
1005 gpmc_t->bool_timings.adv_extra_delay = true;
1006 if (dev_t->oe_xdelay)
1007 gpmc_t->bool_timings.oe_extra_delay = true;
1008 if (dev_t->we_xdelay)
1009 gpmc_t->bool_timings.we_extra_delay = true;
1010
1011 return 0;
1012}
1013
1014static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001015 struct gpmc_device_timings *dev_t,
1016 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301017{
1018 u32 temp;
1019
1020 /* cs_on */
1021 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1022
1023 /* adv_on */
1024 temp = dev_t->t_avdasu;
1025 if (dev_t->t_ce_avd)
1026 temp = max_t(u32, temp,
1027 gpmc_t->cs_on + dev_t->t_ce_avd);
1028 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1029
Jon Hunterc3be5b42013-02-21 13:46:22 -06001030 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301031 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1032
1033 return 0;
1034}
1035
1036/* TODO: remove this function once all peripherals are confirmed to
1037 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1038 * has to be modified to handle timings in ps instead of ns
1039*/
1040static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1041{
1042 t->cs_on /= 1000;
1043 t->cs_rd_off /= 1000;
1044 t->cs_wr_off /= 1000;
1045 t->adv_on /= 1000;
1046 t->adv_rd_off /= 1000;
1047 t->adv_wr_off /= 1000;
1048 t->we_on /= 1000;
1049 t->we_off /= 1000;
1050 t->oe_on /= 1000;
1051 t->oe_off /= 1000;
1052 t->page_burst_access /= 1000;
1053 t->access /= 1000;
1054 t->rd_cycle /= 1000;
1055 t->wr_cycle /= 1000;
1056 t->bus_turnaround /= 1000;
1057 t->cycle2cycle_delay /= 1000;
1058 t->wait_monitoring /= 1000;
1059 t->clk_activation /= 1000;
1060 t->wr_access /= 1000;
1061 t->wr_data_mux_bus /= 1000;
1062}
1063
1064int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001065 struct gpmc_settings *gpmc_s,
1066 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301067{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001068 bool mux = false, sync = false;
1069
1070 if (gpmc_s) {
1071 mux = gpmc_s->mux_add_data ? true : false;
1072 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1073 }
1074
Afzal Mohammed246da262012-08-02 20:02:10 +05301075 memset(gpmc_t, 0, sizeof(*gpmc_t));
1076
Jon Hunterc3be5b42013-02-21 13:46:22 -06001077 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301078
Jon Hunterc3be5b42013-02-21 13:46:22 -06001079 if (gpmc_s && gpmc_s->sync_read)
1080 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301081 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001082 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301083
Jon Hunterc3be5b42013-02-21 13:46:22 -06001084 if (gpmc_s && gpmc_s->sync_write)
1085 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301086 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001087 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301088
1089 /* TODO: remove, see function definition */
1090 gpmc_convert_ps_to_ns(gpmc_t);
1091
1092 return 0;
1093}
1094
Jon Hunteraa8d4762013-02-21 15:25:23 -06001095/**
1096 * gpmc_cs_program_settings - programs non-timing related settings
1097 * @cs: GPMC chip-select to program
1098 * @p: pointer to GPMC settings structure
1099 *
1100 * Programs non-timing related settings for a GPMC chip-select, such as
1101 * bus-width, burst configuration, etc. Function should be called once
1102 * for each chip-select that is being used and must be called before
1103 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1104 * register will be initialised to zero by this function. Returns 0 on
1105 * success and appropriate negative error code on failure.
1106 */
1107int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1108{
1109 u32 config1;
1110
1111 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1112 pr_err("%s: invalid width %d!", __func__, p->device_width);
1113 return -EINVAL;
1114 }
1115
1116 /* Address-data multiplexing not supported for NAND devices */
1117 if (p->device_nand && p->mux_add_data) {
1118 pr_err("%s: invalid configuration!\n", __func__);
1119 return -EINVAL;
1120 }
1121
1122 if ((p->mux_add_data > GPMC_MUX_AD) ||
1123 ((p->mux_add_data == GPMC_MUX_AAD) &&
1124 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1125 pr_err("%s: invalid multiplex configuration!\n", __func__);
1126 return -EINVAL;
1127 }
1128
1129 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1130 if (p->burst_read || p->burst_write) {
1131 switch (p->burst_len) {
1132 case GPMC_BURST_4:
1133 case GPMC_BURST_8:
1134 case GPMC_BURST_16:
1135 break;
1136 default:
1137 pr_err("%s: invalid page/burst-length (%d)\n",
1138 __func__, p->burst_len);
1139 return -EINVAL;
1140 }
1141 }
1142
1143 if ((p->wait_on_read || p->wait_on_write) &&
1144 (p->wait_pin > gpmc_nr_waitpins)) {
1145 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1146 return -EINVAL;
1147 }
1148
1149 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1150
1151 if (p->sync_read)
1152 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1153 if (p->sync_write)
1154 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1155 if (p->wait_on_read)
1156 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1157 if (p->wait_on_write)
1158 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1159 if (p->wait_on_read || p->wait_on_write)
1160 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1161 if (p->device_nand)
1162 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1163 if (p->mux_add_data)
1164 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1165 if (p->burst_read)
1166 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1167 if (p->burst_write)
1168 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1169 if (p->burst_read || p->burst_write) {
1170 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1171 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1172 }
1173
1174 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1175
1176 return 0;
1177}
1178
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001179#ifdef CONFIG_OF
1180static struct of_device_id gpmc_dt_ids[] = {
1181 { .compatible = "ti,omap2420-gpmc" },
1182 { .compatible = "ti,omap2430-gpmc" },
1183 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1184 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1185 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1186 { }
1187};
1188MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1189
Jon Hunter8c8a77712013-02-20 15:53:12 -06001190/**
1191 * gpmc_read_settings_dt - read gpmc settings from device-tree
1192 * @np: pointer to device-tree node for a gpmc child device
1193 * @p: pointer to gpmc settings structure
1194 *
1195 * Reads the GPMC settings for a GPMC child device from device-tree and
1196 * stores them in the GPMC settings structure passed. The GPMC settings
1197 * structure is initialised to zero by this function and so any
1198 * previously stored settings will be cleared.
1199 */
1200void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1201{
1202 memset(p, 0, sizeof(struct gpmc_settings));
1203
1204 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1205 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
1206 p->device_nand = of_property_read_bool(np, "gpmc,device-nand");
1207 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1208 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1209
1210 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1211 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1212 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1213 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1214 if (!p->burst_read && !p->burst_write)
1215 pr_warn("%s: page/burst-length set but not used!\n",
1216 __func__);
1217 }
1218
1219 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1220 p->wait_on_read = of_property_read_bool(np,
1221 "gpmc,wait-on-read");
1222 p->wait_on_write = of_property_read_bool(np,
1223 "gpmc,wait-on-write");
1224 if (!p->wait_on_read && !p->wait_on_write)
1225 pr_warn("%s: read/write wait monitoring not enabled!\n",
1226 __func__);
1227 }
1228}
1229
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001230static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1231 struct gpmc_timings *gpmc_t)
1232{
1233 u32 val;
1234
1235 memset(gpmc_t, 0, sizeof(*gpmc_t));
1236
1237 /* minimum clock period for syncronous mode */
1238 if (!of_property_read_u32(np, "gpmc,sync-clk", &val))
1239 gpmc_t->sync_clk = val;
1240
1241 /* chip select timtings */
1242 if (!of_property_read_u32(np, "gpmc,cs-on", &val))
1243 gpmc_t->cs_on = val;
1244
1245 if (!of_property_read_u32(np, "gpmc,cs-rd-off", &val))
1246 gpmc_t->cs_rd_off = val;
1247
1248 if (!of_property_read_u32(np, "gpmc,cs-wr-off", &val))
1249 gpmc_t->cs_wr_off = val;
1250
1251 /* ADV signal timings */
1252 if (!of_property_read_u32(np, "gpmc,adv-on", &val))
1253 gpmc_t->adv_on = val;
1254
1255 if (!of_property_read_u32(np, "gpmc,adv-rd-off", &val))
1256 gpmc_t->adv_rd_off = val;
1257
1258 if (!of_property_read_u32(np, "gpmc,adv-wr-off", &val))
1259 gpmc_t->adv_wr_off = val;
1260
1261 /* WE signal timings */
1262 if (!of_property_read_u32(np, "gpmc,we-on", &val))
1263 gpmc_t->we_on = val;
1264
1265 if (!of_property_read_u32(np, "gpmc,we-off", &val))
1266 gpmc_t->we_off = val;
1267
1268 /* OE signal timings */
1269 if (!of_property_read_u32(np, "gpmc,oe-on", &val))
1270 gpmc_t->oe_on = val;
1271
1272 if (!of_property_read_u32(np, "gpmc,oe-off", &val))
1273 gpmc_t->oe_off = val;
1274
1275 /* access and cycle timings */
1276 if (!of_property_read_u32(np, "gpmc,page-burst-access", &val))
1277 gpmc_t->page_burst_access = val;
1278
1279 if (!of_property_read_u32(np, "gpmc,access", &val))
1280 gpmc_t->access = val;
1281
1282 if (!of_property_read_u32(np, "gpmc,rd-cycle", &val))
1283 gpmc_t->rd_cycle = val;
1284
1285 if (!of_property_read_u32(np, "gpmc,wr-cycle", &val))
1286 gpmc_t->wr_cycle = val;
1287
1288 /* only for OMAP3430 */
1289 if (!of_property_read_u32(np, "gpmc,wr-access", &val))
1290 gpmc_t->wr_access = val;
1291
1292 if (!of_property_read_u32(np, "gpmc,wr-data-mux-bus", &val))
1293 gpmc_t->wr_data_mux_bus = val;
1294}
1295
1296#ifdef CONFIG_MTD_NAND
1297
1298static const char * const nand_ecc_opts[] = {
1299 [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
1300 [OMAP_ECC_HAMMING_CODE_HW] = "hw",
1301 [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
1302 [OMAP_ECC_BCH4_CODE_HW] = "bch4",
1303 [OMAP_ECC_BCH8_CODE_HW] = "bch8",
1304};
1305
1306static int gpmc_probe_nand_child(struct platform_device *pdev,
1307 struct device_node *child)
1308{
1309 u32 val;
1310 const char *s;
1311 struct gpmc_timings gpmc_t;
1312 struct omap_nand_platform_data *gpmc_nand_data;
1313
1314 if (of_property_read_u32(child, "reg", &val) < 0) {
1315 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1316 child->full_name);
1317 return -ENODEV;
1318 }
1319
1320 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1321 GFP_KERNEL);
1322 if (!gpmc_nand_data)
1323 return -ENOMEM;
1324
1325 gpmc_nand_data->cs = val;
1326 gpmc_nand_data->of_node = child;
1327
1328 if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
1329 for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
1330 if (!strcasecmp(s, nand_ecc_opts[val])) {
1331 gpmc_nand_data->ecc_opt = val;
1332 break;
1333 }
1334
1335 val = of_get_nand_bus_width(child);
1336 if (val == 16)
1337 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1338
1339 gpmc_read_timings_dt(child, &gpmc_t);
1340 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1341
1342 return 0;
1343}
1344#else
1345static int gpmc_probe_nand_child(struct platform_device *pdev,
1346 struct device_node *child)
1347{
1348 return 0;
1349}
1350#endif
1351
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001352#ifdef CONFIG_MTD_ONENAND
1353static int gpmc_probe_onenand_child(struct platform_device *pdev,
1354 struct device_node *child)
1355{
1356 u32 val;
1357 struct omap_onenand_platform_data *gpmc_onenand_data;
1358
1359 if (of_property_read_u32(child, "reg", &val) < 0) {
1360 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1361 child->full_name);
1362 return -ENODEV;
1363 }
1364
1365 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1366 GFP_KERNEL);
1367 if (!gpmc_onenand_data)
1368 return -ENOMEM;
1369
1370 gpmc_onenand_data->cs = val;
1371 gpmc_onenand_data->of_node = child;
1372 gpmc_onenand_data->dma_channel = -1;
1373
1374 if (!of_property_read_u32(child, "dma-channel", &val))
1375 gpmc_onenand_data->dma_channel = val;
1376
1377 gpmc_onenand_init(gpmc_onenand_data);
1378
1379 return 0;
1380}
1381#else
1382static int gpmc_probe_onenand_child(struct platform_device *pdev,
1383 struct device_node *child)
1384{
1385 return 0;
1386}
1387#endif
1388
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001389static int gpmc_probe_dt(struct platform_device *pdev)
1390{
1391 int ret;
1392 struct device_node *child;
1393 const struct of_device_id *of_id =
1394 of_match_device(gpmc_dt_ids, &pdev->dev);
1395
1396 if (!of_id)
1397 return 0;
1398
Jon Hunter9f833152013-02-20 15:53:38 -06001399 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1400 &gpmc_nr_waitpins);
1401 if (ret < 0) {
1402 pr_err("%s: number of wait pins not found!\n", __func__);
1403 return ret;
1404 }
1405
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001406 for_each_node_by_name(child, "nand") {
1407 ret = gpmc_probe_nand_child(pdev, child);
Ezequiel Garciaa1672372013-01-25 09:19:47 -03001408 if (ret < 0) {
1409 of_node_put(child);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001410 return ret;
Ezequiel Garciaa1672372013-01-25 09:19:47 -03001411 }
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001412 }
1413
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001414 for_each_node_by_name(child, "onenand") {
1415 ret = gpmc_probe_onenand_child(pdev, child);
1416 if (ret < 0) {
1417 of_node_put(child);
1418 return ret;
1419 }
1420 }
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001421 return 0;
1422}
1423#else
1424static int gpmc_probe_dt(struct platform_device *pdev)
1425{
1426 return 0;
1427}
1428#endif
1429
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001430static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001431{
Jon Hunter81190242012-10-17 09:41:25 -05001432 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001433 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06001434 struct resource *res;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001435
Afzal Mohammedda496872012-09-23 17:28:25 -06001436 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1437 if (res == NULL)
1438 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001439
Afzal Mohammedda496872012-09-23 17:28:25 -06001440 phys_base = res->start;
1441 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08001442
Thierry Reding5857bd92013-01-21 11:08:55 +01001443 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1444 if (IS_ERR(gpmc_base))
1445 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06001446
1447 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1448 if (res == NULL)
1449 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1450 else
1451 gpmc_irq = res->start;
1452
1453 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1454 if (IS_ERR(gpmc_l3_clk)) {
1455 dev_err(&pdev->dev, "error: clk_get\n");
1456 gpmc_irq = 0;
1457 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001458 }
1459
Rajendra Nayak4d7cb452012-09-22 02:24:16 -06001460 clk_prepare_enable(gpmc_l3_clk);
Olof Johansson1daa8c12010-01-20 22:39:29 +00001461
Afzal Mohammedda496872012-09-23 17:28:25 -06001462 gpmc_dev = &pdev->dev;
1463
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001464 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06001465
1466 /*
1467 * FIXME: Once device-tree migration is complete the below flags
1468 * should be populated based upon the device-tree compatible
1469 * string. For now just use the IP revision. OMAP3+ devices have
1470 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1471 * devices support the addr-addr-data multiplex protocol.
1472 *
1473 * GPMC IP revisions:
1474 * - OMAP24xx = 2.0
1475 * - OMAP3xxx = 5.0
1476 * - OMAP44xx/54xx/AM335x = 6.0
1477 */
Afzal Mohammedda496872012-09-23 17:28:25 -06001478 if (GPMC_REVISION_MAJOR(l) > 0x4)
1479 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06001480 if (GPMC_REVISION_MAJOR(l) > 0x5)
1481 gpmc_capability |= GPMC_HAS_MUX_AAD;
Afzal Mohammedda496872012-09-23 17:28:25 -06001482 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1483 GPMC_REVISION_MINOR(l));
1484
Jon Hunter81190242012-10-17 09:41:25 -05001485 rc = gpmc_mem_init();
Russell King71856842013-03-13 20:44:21 +00001486 if (rc < 0) {
Jon Hunter81190242012-10-17 09:41:25 -05001487 clk_disable_unprepare(gpmc_l3_clk);
1488 clk_put(gpmc_l3_clk);
1489 dev_err(gpmc_dev, "failed to reserve memory\n");
1490 return rc;
1491 }
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301492
Russell King71856842013-03-13 20:44:21 +00001493 if (gpmc_setup_irq() < 0)
Afzal Mohammedda496872012-09-23 17:28:25 -06001494 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1495
Jon Hunter31d9adc2013-02-18 07:57:39 -06001496 /* Now the GPMC is initialised, unreserve the chip-selects */
1497 gpmc_cs_map = 0;
1498
Jon Hunter9f833152013-02-20 15:53:38 -06001499 if (!pdev->dev.of_node)
1500 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
1501
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001502 rc = gpmc_probe_dt(pdev);
1503 if (rc < 0) {
1504 clk_disable_unprepare(gpmc_l3_clk);
1505 clk_put(gpmc_l3_clk);
1506 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1507 return rc;
1508 }
1509
Afzal Mohammedda496872012-09-23 17:28:25 -06001510 return 0;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301511}
Afzal Mohammedda496872012-09-23 17:28:25 -06001512
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001513static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06001514{
1515 gpmc_free_irq();
1516 gpmc_mem_exit();
1517 gpmc_dev = NULL;
1518 return 0;
1519}
1520
1521static struct platform_driver gpmc_driver = {
1522 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001523 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06001524 .driver = {
1525 .name = DEVICE_NAME,
1526 .owner = THIS_MODULE,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001527 .of_match_table = of_match_ptr(gpmc_dt_ids),
Afzal Mohammedda496872012-09-23 17:28:25 -06001528 },
1529};
1530
1531static __init int gpmc_init(void)
1532{
1533 return platform_driver_register(&gpmc_driver);
1534}
1535
1536static __exit void gpmc_exit(void)
1537{
1538 platform_driver_unregister(&gpmc_driver);
1539
1540}
1541
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001542omap_postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06001543module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301544
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001545static int __init omap_gpmc_init(void)
1546{
1547 struct omap_hwmod *oh;
1548 struct platform_device *pdev;
1549 char *oh_name = "gpmc";
1550
Daniel Mack2f98ca82012-12-14 11:36:40 +01001551 /*
1552 * if the board boots up with a populated DT, do not
1553 * manually add the device from this initcall
1554 */
1555 if (of_have_populated_dt())
1556 return -ENODEV;
1557
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001558 oh = omap_hwmod_lookup(oh_name);
1559 if (!oh) {
1560 pr_err("Could not look up %s\n", oh_name);
1561 return -ENODEV;
1562 }
1563
Paul Walmsleyc1d1cd52013-01-26 00:48:53 -07001564 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001565 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1566
1567 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
1568}
Tony Lindgrenb76c8b12013-01-11 11:24:18 -08001569omap_postcore_initcall(omap_gpmc_init);
Afzal Mohammed4be48fd2012-09-23 17:28:24 -06001570
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301571static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1572{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001573 int i;
1574 u32 regval;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301575
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001576 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1577
1578 if (!regval)
1579 return IRQ_NONE;
1580
1581 for (i = 0; i < GPMC_NR_IRQ; i++)
1582 if (regval & gpmc_client_irq[i].bitmask)
1583 generic_handle_irq(gpmc_client_irq[i].irq);
1584
1585 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05301586
1587 return IRQ_HANDLED;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001588}
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301589
1590#ifdef CONFIG_ARCH_OMAP3
1591static struct omap3_gpmc_regs gpmc_context;
1592
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001593void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301594{
1595 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001596
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301597 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1598 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1599 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1600 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1601 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1602 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1603 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
1604 for (i = 0; i < GPMC_CS_NUM; i++) {
1605 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1606 if (gpmc_context.cs_context[i].is_valid) {
1607 gpmc_context.cs_context[i].config1 =
1608 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1609 gpmc_context.cs_context[i].config2 =
1610 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1611 gpmc_context.cs_context[i].config3 =
1612 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1613 gpmc_context.cs_context[i].config4 =
1614 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1615 gpmc_context.cs_context[i].config5 =
1616 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1617 gpmc_context.cs_context[i].config6 =
1618 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1619 gpmc_context.cs_context[i].config7 =
1620 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1621 }
1622 }
1623}
1624
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001625void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301626{
1627 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08001628
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05301629 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1630 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1631 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1632 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1633 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1634 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1635 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
1636 for (i = 0; i < GPMC_CS_NUM; i++) {
1637 if (gpmc_context.cs_context[i].is_valid) {
1638 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1639 gpmc_context.cs_context[i].config1);
1640 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1641 gpmc_context.cs_context[i].config2);
1642 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1643 gpmc_context.cs_context[i].config3);
1644 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1645 gpmc_context.cs_context[i].config4);
1646 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1647 gpmc_context.cs_context[i].config5);
1648 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1649 gpmc_context.cs_context[i].config6);
1650 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1651 gpmc_context.cs_context[i].config7);
1652 }
1653 }
1654}
1655#endif /* CONFIG_ARCH_OMAP3 */