]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/arm/mach-omap2/gpmc.c
ARM: OMAP2+: gpmc: Print error message in set_gpmc_timing_reg()
[mirror_ubuntu-bionic-kernel.git] / arch / arm / mach-omap2 / gpmc.c
CommitLineData
4bbbc1ad
JY
1/*
2 * GPMC support functions
3 *
4 * Copyright (C) 2005-2006 Nokia Corporation
5 *
6 * Author: Juha Yrjola
7 *
44169075
SS
8 * Copyright (C) 2009 Texas Instruments
9 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
10 *
4bbbc1ad
JY
11 * 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 */
fd1dc87d
PW
15#undef DEBUG
16
db97eb7d 17#include <linux/irq.h>
4bbbc1ad
JY
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/err.h>
21#include <linux/clk.h>
f37e4580
ID
22#include <linux/ioport.h>
23#include <linux/spinlock.h>
fced80c7 24#include <linux/io.h>
fd1dc87d 25#include <linux/module.h>
db97eb7d 26#include <linux/interrupt.h>
da496873 27#include <linux/platform_device.h>
bc6b1e7b 28#include <linux/of.h>
cdd6928c 29#include <linux/of_address.h>
bc6b1e7b
DM
30#include <linux/of_mtd.h>
31#include <linux/of_device.h>
32#include <linux/mtd/nand.h>
b3f5525c 33#include <linux/pm_runtime.h>
4bbbc1ad 34
bc3668ea 35#include <linux/platform_data/mtd-nand-omap2.h>
4bbbc1ad 36
7f245162 37#include <asm/mach-types.h>
72d0f1c3 38
dbc04161 39#include "soc.h"
7d7e1eba 40#include "common.h"
25c7d49e 41#include "omap_device.h"
3ef5d007 42#include "gpmc.h"
bc6b1e7b 43#include "gpmc-nand.h"
75d3625e 44#include "gpmc-onenand.h"
7d7e1eba 45
4be48fd5
AM
46#define DEVICE_NAME "omap-gpmc"
47
fd1dc87d 48/* GPMC register offsets */
4bbbc1ad
JY
49#define GPMC_REVISION 0x00
50#define GPMC_SYSCONFIG 0x10
51#define GPMC_SYSSTATUS 0x14
52#define GPMC_IRQSTATUS 0x18
53#define GPMC_IRQENABLE 0x1c
54#define GPMC_TIMEOUT_CONTROL 0x40
55#define GPMC_ERR_ADDRESS 0x44
56#define GPMC_ERR_TYPE 0x48
57#define GPMC_CONFIG 0x50
58#define GPMC_STATUS 0x54
59#define GPMC_PREFETCH_CONFIG1 0x1e0
60#define GPMC_PREFETCH_CONFIG2 0x1e4
15e02a3b 61#define GPMC_PREFETCH_CONTROL 0x1ec
4bbbc1ad
JY
62#define GPMC_PREFETCH_STATUS 0x1f0
63#define GPMC_ECC_CONFIG 0x1f4
64#define GPMC_ECC_CONTROL 0x1f8
65#define GPMC_ECC_SIZE_CONFIG 0x1fc
948d38e7 66#define GPMC_ECC1_RESULT 0x200
8d602cf5 67#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
2fdf0c98
AM
68#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
69#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
70#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
27c9fd60 71#define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
72#define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
73#define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
4bbbc1ad 74
2c65e744
YY
75/* GPMC ECC control settings */
76#define GPMC_ECC_CTRL_ECCCLEAR 0x100
77#define GPMC_ECC_CTRL_ECCDISABLE 0x000
78#define GPMC_ECC_CTRL_ECCREG1 0x001
79#define GPMC_ECC_CTRL_ECCREG2 0x002
80#define GPMC_ECC_CTRL_ECCREG3 0x003
81#define GPMC_ECC_CTRL_ECCREG4 0x004
82#define GPMC_ECC_CTRL_ECCREG5 0x005
83#define GPMC_ECC_CTRL_ECCREG6 0x006
84#define GPMC_ECC_CTRL_ECCREG7 0x007
85#define GPMC_ECC_CTRL_ECCREG8 0x008
86#define GPMC_ECC_CTRL_ECCREG9 0x009
87
559d94b0
AM
88#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
89#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
90#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
91#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
92#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
93#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
94
948d38e7 95#define GPMC_CS0_OFFSET 0x60
4bbbc1ad 96#define GPMC_CS_SIZE 0x30
2fdf0c98 97#define GPMC_BCH_SIZE 0x10
4bbbc1ad 98
f37e4580 99#define GPMC_MEM_END 0x3FFFFFFF
f37e4580
ID
100
101#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
102#define GPMC_SECTION_SHIFT 28 /* 128 MB */
103
59e9c5ae 104#define CS_NUM_SHIFT 24
105#define ENABLE_PREFETCH (0x1 << 7)
106#define DMA_MPU_MODE 2
107
da496873
AM
108#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
109#define GPMC_REVISION_MINOR(l) (l & 0xf)
110
111#define GPMC_HAS_WR_ACCESS 0x1
112#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
aa8d4767 113#define GPMC_HAS_MUX_AAD 0x4
da496873 114
9f833156
JH
115#define GPMC_NR_WAITPINS 4
116
6b6c32fc
AM
117/* XXX: Only NAND irq has been considered,currently these are the only ones used
118 */
119#define GPMC_NR_IRQ 2
120
121struct gpmc_client_irq {
122 unsigned irq;
123 u32 bitmask;
124};
125
a2d3e7ba
RN
126/* Structure to save gpmc cs context */
127struct gpmc_cs_config {
128 u32 config1;
129 u32 config2;
130 u32 config3;
131 u32 config4;
132 u32 config5;
133 u32 config6;
134 u32 config7;
135 int is_valid;
136};
137
138/*
139 * Structure to save/restore gpmc context
140 * to support core off on OMAP3
141 */
142struct omap3_gpmc_regs {
143 u32 sysconfig;
144 u32 irqenable;
145 u32 timeout_ctrl;
146 u32 config;
147 u32 prefetch_config1;
148 u32 prefetch_config2;
149 u32 prefetch_control;
150 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
151};
152
6b6c32fc
AM
153static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
154static struct irq_chip gpmc_irq_chip;
af072196 155static int gpmc_irq_start;
6b6c32fc 156
f37e4580
ID
157static struct resource gpmc_mem_root;
158static struct resource gpmc_cs_mem[GPMC_CS_NUM];
87b247c4 159static DEFINE_SPINLOCK(gpmc_mem_lock);
6797b4fe
JH
160/* Define chip-selects as reserved by default until probe completes */
161static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
f34f3716 162static unsigned int gpmc_cs_num = GPMC_CS_NUM;
9f833156 163static unsigned int gpmc_nr_waitpins;
da496873
AM
164static struct device *gpmc_dev;
165static int gpmc_irq;
166static resource_size_t phys_base, mem_size;
167static unsigned gpmc_capability;
fd1dc87d 168static void __iomem *gpmc_base;
4bbbc1ad 169
fd1dc87d 170static struct clk *gpmc_l3_clk;
4bbbc1ad 171
db97eb7d
SG
172static irqreturn_t gpmc_handle_irq(int irq, void *dev);
173
4bbbc1ad
JY
174static void gpmc_write_reg(int idx, u32 val)
175{
edfaf05c 176 writel_relaxed(val, gpmc_base + idx);
4bbbc1ad
JY
177}
178
179static u32 gpmc_read_reg(int idx)
180{
edfaf05c 181 return readl_relaxed(gpmc_base + idx);
4bbbc1ad
JY
182}
183
184void gpmc_cs_write_reg(int cs, int idx, u32 val)
185{
186 void __iomem *reg_addr;
187
948d38e7 188 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
edfaf05c 189 writel_relaxed(val, reg_addr);
4bbbc1ad
JY
190}
191
3fc089e7 192static u32 gpmc_cs_read_reg(int cs, int idx)
4bbbc1ad 193{
fd1dc87d
PW
194 void __iomem *reg_addr;
195
948d38e7 196 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
edfaf05c 197 return readl_relaxed(reg_addr);
4bbbc1ad
JY
198}
199
fd1dc87d 200/* TODO: Add support for gpmc_fck to clock framework and use it */
3fc089e7 201static unsigned long gpmc_get_fclk_period(void)
4bbbc1ad 202{
fd1dc87d
PW
203 unsigned long rate = clk_get_rate(gpmc_l3_clk);
204
205 if (rate == 0) {
206 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
207 return 0;
208 }
209
210 rate /= 1000;
211 rate = 1000000000 / rate; /* In picoseconds */
212
213 return rate;
4bbbc1ad
JY
214}
215
3fc089e7 216static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
4bbbc1ad
JY
217{
218 unsigned long tick_ps;
219
220 /* Calculate in picosecs to yield more exact results */
221 tick_ps = gpmc_get_fclk_period();
222
223 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
224}
225
3fc089e7 226static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
a3551f5b
AH
227{
228 unsigned long tick_ps;
229
230 /* Calculate in picosecs to yield more exact results */
231 tick_ps = gpmc_get_fclk_period();
232
233 return (time_ps + tick_ps - 1) / tick_ps;
234}
235
fd1dc87d
PW
236unsigned int gpmc_ticks_to_ns(unsigned int ticks)
237{
238 return ticks * gpmc_get_fclk_period() / 1000;
239}
240
246da26d
AM
241static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
242{
243 return ticks * gpmc_get_fclk_period();
244}
245
246static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
247{
248 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
249
250 return ticks * gpmc_get_fclk_period();
251}
252
559d94b0
AM
253static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
254{
255 u32 l;
256
257 l = gpmc_cs_read_reg(cs, reg);
258 if (value)
259 l |= mask;
260 else
261 l &= ~mask;
262 gpmc_cs_write_reg(cs, reg, l);
263}
264
265static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
266{
267 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
268 GPMC_CONFIG1_TIME_PARA_GRAN,
269 p->time_para_granularity);
270 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
271 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
272 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
273 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
274 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
275 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
276 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
277 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
278 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
279 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
280 p->cycle2cyclesamecsen);
281 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
282 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
283 p->cycle2cyclediffcsen);
284}
285
4bbbc1ad 286static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
2aab6468 287 int time, const char *name)
4bbbc1ad
JY
288{
289 u32 l;
290 int ticks, mask, nr_bits;
291
292 if (time == 0)
293 ticks = 0;
294 else
295 ticks = gpmc_ns_to_ticks(time);
296 nr_bits = end_bit - st_bit + 1;
80323742
RQ
297 mask = (1 << nr_bits) - 1;
298
299 if (ticks > mask) {
300 pr_err("%s: GPMC error! CS%d: %s: %d ns, %d ticks > %d\n",
301 __func__, cs, name, time, ticks, mask);
302
4bbbc1ad 303 return -1;
1c22cc13 304 }
4bbbc1ad 305
4bbbc1ad
JY
306 l = gpmc_cs_read_reg(cs, reg);
307#ifdef DEBUG
1c22cc13
DB
308 printk(KERN_INFO
309 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
2aab6468 310 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
1c22cc13 311 (l >> st_bit) & mask, time);
4bbbc1ad
JY
312#endif
313 l &= ~(mask << st_bit);
314 l |= ticks << st_bit;
315 gpmc_cs_write_reg(cs, reg, l);
316
317 return 0;
318}
319
4bbbc1ad
JY
320#define GPMC_SET_ONE(reg, st, end, field) \
321 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
322 t->field, #field) < 0) \
323 return -1
4bbbc1ad 324
1b47ca1a 325int gpmc_calc_divider(unsigned int sync_clk)
4bbbc1ad
JY
326{
327 int div;
328 u32 l;
329
a3551f5b 330 l = sync_clk + (gpmc_get_fclk_period() - 1);
4bbbc1ad
JY
331 div = l / gpmc_get_fclk_period();
332 if (div > 4)
333 return -1;
1c22cc13 334 if (div <= 0)
4bbbc1ad
JY
335 div = 1;
336
337 return div;
338}
339
340int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
341{
342 int div;
343 u32 l;
344
1b47ca1a 345 div = gpmc_calc_divider(t->sync_clk);
4bbbc1ad 346 if (div < 0)
a032d33b 347 return div;
4bbbc1ad
JY
348
349 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
350 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
351 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
352
353 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
354 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
355 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
356
357 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
358 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
359 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
360 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
361
362 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
363 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
364 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
365
366 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
367
559d94b0
AM
368 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
369 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
370
371 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
372 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
373
da496873 374 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
cc26b3b0 375 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
da496873 376 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
cc26b3b0 377 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
cc26b3b0 378
1c22cc13
DB
379 /* caller is expected to have initialized CONFIG1 to cover
380 * at least sync vs async
381 */
382 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
383 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
4bbbc1ad 384#ifdef DEBUG
1c22cc13
DB
385 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
386 cs, (div * gpmc_get_fclk_period()) / 1000, div);
4bbbc1ad 387#endif
1c22cc13
DB
388 l &= ~0x03;
389 l |= (div - 1);
390 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
391 }
4bbbc1ad 392
559d94b0
AM
393 gpmc_cs_bool_timings(cs, &t->bool_timings);
394
4bbbc1ad
JY
395 return 0;
396}
397
c71f8e9b 398static int gpmc_cs_enable_mem(int cs, u32 base, u32 size)
f37e4580
ID
399{
400 u32 l;
401 u32 mask;
402
c71f8e9b
JH
403 /*
404 * Ensure that base address is aligned on a
405 * boundary equal to or greater than size.
406 */
407 if (base & (size - 1))
408 return -EINVAL;
409
f37e4580
ID
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;
a2d3e7ba 416 l |= GPMC_CONFIG7_CSVALID;
f37e4580 417 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
c71f8e9b
JH
418
419 return 0;
f37e4580
ID
420}
421
422static void gpmc_cs_disable_mem(int cs)
423{
424 u32 l;
425
426 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
a2d3e7ba 427 l &= ~GPMC_CONFIG7_CSVALID;
f37e4580
ID
428 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
429}
430
431static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
432{
433 u32 l;
434 u32 mask;
435
436 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
437 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
438 mask = (l >> 8) & 0x0f;
439 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
440}
441
442static int gpmc_cs_mem_enabled(int cs)
443{
444 u32 l;
445
446 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
a2d3e7ba 447 return l & GPMC_CONFIG7_CSVALID;
f37e4580
ID
448}
449
f5d8edaf 450static void gpmc_cs_set_reserved(int cs, int reserved)
4bbbc1ad 451{
f37e4580
ID
452 gpmc_cs_map &= ~(1 << cs);
453 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
454}
455
ae9d908a 456static bool gpmc_cs_reserved(int cs)
f37e4580
ID
457{
458 return gpmc_cs_map & (1 << cs);
459}
460
461static unsigned long gpmc_mem_align(unsigned long size)
462{
463 int order;
464
465 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
466 order = GPMC_CHUNK_SHIFT - 1;
467 do {
468 size >>= 1;
469 order++;
470 } while (size);
471 size = 1 << order;
472 return size;
473}
474
475static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
476{
477 struct resource *res = &gpmc_cs_mem[cs];
478 int r;
479
480 size = gpmc_mem_align(size);
481 spin_lock(&gpmc_mem_lock);
482 res->start = base;
483 res->end = base + size - 1;
484 r = request_resource(&gpmc_mem_root, res);
485 spin_unlock(&gpmc_mem_lock);
486
487 return r;
488}
489
da496873
AM
490static int gpmc_cs_delete_mem(int cs)
491{
492 struct resource *res = &gpmc_cs_mem[cs];
493 int r;
494
495 spin_lock(&gpmc_mem_lock);
efe80723 496 r = release_resource(res);
da496873
AM
497 res->start = 0;
498 res->end = 0;
499 spin_unlock(&gpmc_mem_lock);
500
501 return r;
502}
503
cdd6928c
JH
504/**
505 * gpmc_cs_remap - remaps a chip-select physical base address
506 * @cs: chip-select to remap
507 * @base: physical base address to re-map chip-select to
508 *
509 * Re-maps a chip-select to a new physical base address specified by
510 * "base". Returns 0 on success and appropriate negative error code
511 * on failure.
512 */
513static int gpmc_cs_remap(int cs, u32 base)
514{
515 int ret;
516 u32 old_base, size;
517
f34f3716
GP
518 if (cs > gpmc_cs_num) {
519 pr_err("%s: requested chip-select is disabled\n", __func__);
cdd6928c 520 return -ENODEV;
f34f3716 521 }
fb677ef7
TL
522
523 /*
524 * Make sure we ignore any device offsets from the GPMC partition
525 * allocated for the chip select and that the new base confirms
526 * to the GPMC 16MB minimum granularity.
527 */
528 base &= ~(SZ_16M - 1);
529
cdd6928c
JH
530 gpmc_cs_get_memconf(cs, &old_base, &size);
531 if (base == old_base)
532 return 0;
533 gpmc_cs_disable_mem(cs);
534 ret = gpmc_cs_delete_mem(cs);
535 if (ret < 0)
536 return ret;
537 ret = gpmc_cs_insert_mem(cs, base, size);
538 if (ret < 0)
539 return ret;
c71f8e9b
JH
540 ret = gpmc_cs_enable_mem(cs, base, size);
541 if (ret < 0)
542 return ret;
cdd6928c
JH
543
544 return 0;
545}
546
f37e4580
ID
547int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
548{
549 struct resource *res = &gpmc_cs_mem[cs];
550 int r = -1;
551
f34f3716
GP
552 if (cs > gpmc_cs_num) {
553 pr_err("%s: requested chip-select is disabled\n", __func__);
f37e4580 554 return -ENODEV;
f34f3716 555 }
f37e4580
ID
556 size = gpmc_mem_align(size);
557 if (size > (1 << GPMC_SECTION_SHIFT))
558 return -ENOMEM;
559
560 spin_lock(&gpmc_mem_lock);
561 if (gpmc_cs_reserved(cs)) {
562 r = -EBUSY;
563 goto out;
564 }
565 if (gpmc_cs_mem_enabled(cs))
566 r = adjust_resource(res, res->start & ~(size - 1), size);
567 if (r < 0)
568 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
569 size, NULL, NULL);
570 if (r < 0)
571 goto out;
572
c71f8e9b
JH
573 r = gpmc_cs_enable_mem(cs, res->start, resource_size(res));
574 if (r < 0) {
575 release_resource(res);
576 goto out;
577 }
578
f37e4580
ID
579 *base = res->start;
580 gpmc_cs_set_reserved(cs, 1);
581out:
582 spin_unlock(&gpmc_mem_lock);
583 return r;
584}
fd1dc87d 585EXPORT_SYMBOL(gpmc_cs_request);
f37e4580
ID
586
587void gpmc_cs_free(int cs)
588{
efe80723
TL
589 struct resource *res = &gpmc_cs_mem[cs];
590
f37e4580 591 spin_lock(&gpmc_mem_lock);
f34f3716 592 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
f37e4580
ID
593 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
594 BUG();
595 spin_unlock(&gpmc_mem_lock);
596 return;
597 }
598 gpmc_cs_disable_mem(cs);
efe80723
TL
599 if (res->flags)
600 release_resource(res);
f37e4580
ID
601 gpmc_cs_set_reserved(cs, 0);
602 spin_unlock(&gpmc_mem_lock);
603}
fd1dc87d 604EXPORT_SYMBOL(gpmc_cs_free);
f37e4580 605
948d38e7 606/**
3a544354 607 * gpmc_configure - write request to configure gpmc
948d38e7
SG
608 * @cmd: command type
609 * @wval: value to write
610 * @return status of the operation
611 */
3a544354 612int gpmc_configure(int cmd, int wval)
948d38e7 613{
3a544354 614 u32 regval;
948d38e7
SG
615
616 switch (cmd) {
db97eb7d
SG
617 case GPMC_ENABLE_IRQ:
618 gpmc_write_reg(GPMC_IRQENABLE, wval);
619 break;
620
948d38e7
SG
621 case GPMC_SET_IRQ_STATUS:
622 gpmc_write_reg(GPMC_IRQSTATUS, wval);
623 break;
624
625 case GPMC_CONFIG_WP:
626 regval = gpmc_read_reg(GPMC_CONFIG);
627 if (wval)
628 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
629 else
630 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
631 gpmc_write_reg(GPMC_CONFIG, regval);
632 break;
633
948d38e7 634 default:
3a544354
JH
635 pr_err("%s: command not supported\n", __func__);
636 return -EINVAL;
948d38e7
SG
637 }
638
3a544354 639 return 0;
948d38e7 640}
3a544354 641EXPORT_SYMBOL(gpmc_configure);
948d38e7 642
52bd138d
AM
643void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
644{
2fdf0c98
AM
645 int i;
646
52bd138d
AM
647 reg->gpmc_status = gpmc_base + GPMC_STATUS;
648 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
649 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
650 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
651 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
652 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
653 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
654 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
655 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
656 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
657 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
658 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
659 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
660 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
661 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
2fdf0c98
AM
662
663 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
664 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
665 GPMC_BCH_SIZE * i;
666 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
667 GPMC_BCH_SIZE * i;
668 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
669 GPMC_BCH_SIZE * i;
670 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
671 GPMC_BCH_SIZE * i;
27c9fd60 672 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
673 i * GPMC_BCH_SIZE;
674 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
675 i * GPMC_BCH_SIZE;
676 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
677 i * GPMC_BCH_SIZE;
2fdf0c98 678 }
52bd138d
AM
679}
680
6b6c32fc
AM
681int gpmc_get_client_irq(unsigned irq_config)
682{
683 int i;
684
685 if (hweight32(irq_config) > 1)
686 return 0;
687
688 for (i = 0; i < GPMC_NR_IRQ; i++)
689 if (gpmc_client_irq[i].bitmask & irq_config)
690 return gpmc_client_irq[i].irq;
691
692 return 0;
693}
694
695static int gpmc_irq_endis(unsigned irq, bool endis)
696{
697 int i;
698 u32 regval;
699
700 for (i = 0; i < GPMC_NR_IRQ; i++)
701 if (irq == gpmc_client_irq[i].irq) {
702 regval = gpmc_read_reg(GPMC_IRQENABLE);
703 if (endis)
704 regval |= gpmc_client_irq[i].bitmask;
705 else
706 regval &= ~gpmc_client_irq[i].bitmask;
707 gpmc_write_reg(GPMC_IRQENABLE, regval);
708 break;
709 }
710
711 return 0;
712}
713
714static void gpmc_irq_disable(struct irq_data *p)
715{
716 gpmc_irq_endis(p->irq, false);
717}
718
719static void gpmc_irq_enable(struct irq_data *p)
720{
721 gpmc_irq_endis(p->irq, true);
722}
723
724static void gpmc_irq_noop(struct irq_data *data) { }
725
726static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
727
da496873 728static int gpmc_setup_irq(void)
6b6c32fc
AM
729{
730 int i;
731 u32 regval;
732
733 if (!gpmc_irq)
734 return -EINVAL;
735
736 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
71856843 737 if (gpmc_irq_start < 0) {
6b6c32fc
AM
738 pr_err("irq_alloc_descs failed\n");
739 return gpmc_irq_start;
740 }
741
742 gpmc_irq_chip.name = "gpmc";
743 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
744 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
745 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
746 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
747 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
748 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
749 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
750
751 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
752 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
753
754 for (i = 0; i < GPMC_NR_IRQ; i++) {
755 gpmc_client_irq[i].irq = gpmc_irq_start + i;
756 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
757 &gpmc_irq_chip, handle_simple_irq);
758 set_irq_flags(gpmc_client_irq[i].irq,
759 IRQF_VALID | IRQF_NOAUTOEN);
760 }
761
762 /* Disable interrupts */
763 gpmc_write_reg(GPMC_IRQENABLE, 0);
764
765 /* clear interrupts */
766 regval = gpmc_read_reg(GPMC_IRQSTATUS);
767 gpmc_write_reg(GPMC_IRQSTATUS, regval);
768
769 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
770}
771
351a102d 772static int gpmc_free_irq(void)
da496873
AM
773{
774 int i;
775
776 if (gpmc_irq)
777 free_irq(gpmc_irq, NULL);
778
779 for (i = 0; i < GPMC_NR_IRQ; i++) {
780 irq_set_handler(gpmc_client_irq[i].irq, NULL);
781 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
782 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
783 }
784
785 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
786
787 return 0;
788}
789
351a102d 790static void gpmc_mem_exit(void)
da496873
AM
791{
792 int cs;
793
f34f3716 794 for (cs = 0; cs < gpmc_cs_num; cs++) {
da496873
AM
795 if (!gpmc_cs_mem_enabled(cs))
796 continue;
797 gpmc_cs_delete_mem(cs);
798 }
799
800}
801
84b00f0e 802static void gpmc_mem_init(void)
f37e4580 803{
84b00f0e 804 int cs;
f37e4580 805
bf234397
JH
806 /*
807 * The first 1MB of GPMC address space is typically mapped to
808 * the internal ROM. Never allocate the first page, to
809 * facilitate bug detection; even if we didn't boot from ROM.
7f245162 810 */
bf234397 811 gpmc_mem_root.start = SZ_1M;
f37e4580
ID
812 gpmc_mem_root.end = GPMC_MEM_END;
813
814 /* Reserve all regions that has been set up by bootloader */
f34f3716 815 for (cs = 0; cs < gpmc_cs_num; cs++) {
f37e4580
ID
816 u32 base, size;
817
818 if (!gpmc_cs_mem_enabled(cs))
819 continue;
820 gpmc_cs_get_memconf(cs, &base, &size);
84b00f0e
JH
821 if (gpmc_cs_insert_mem(cs, base, size)) {
822 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
823 __func__, cs, base, base + size);
824 gpmc_cs_disable_mem(cs);
8119024e 825 }
f37e4580 826 }
4bbbc1ad
JY
827}
828
246da26d
AM
829static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
830{
831 u32 temp;
832 int div;
833
834 div = gpmc_calc_divider(sync_clk);
835 temp = gpmc_ps_to_ticks(time_ps);
836 temp = (temp + div - 1) / div;
837 return gpmc_ticks_to_ps(temp * div);
838}
839
840/* XXX: can the cycles be avoided ? */
841static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
c3be5b45
JH
842 struct gpmc_device_timings *dev_t,
843 bool mux)
246da26d 844{
246da26d
AM
845 u32 temp;
846
847 /* adv_rd_off */
848 temp = dev_t->t_avdp_r;
849 /* XXX: mux check required ? */
850 if (mux) {
851 /* XXX: t_avdp not to be required for sync, only added for tusb
852 * this indirectly necessitates requirement of t_avdp_r and
853 * t_avdp_w instead of having a single t_avdp
854 */
855 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
856 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
857 }
858 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
859
860 /* oe_on */
861 temp = dev_t->t_oeasu; /* XXX: remove this ? */
862 if (mux) {
863 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
864 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
865 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
866 }
867 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
868
869 /* access */
870 /* XXX: any scope for improvement ?, by combining oe_on
871 * and clk_activation, need to check whether
872 * access = clk_activation + round to sync clk ?
873 */
874 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
875 temp += gpmc_t->clk_activation;
876 if (dev_t->cyc_oe)
877 temp = max_t(u32, temp, gpmc_t->oe_on +
878 gpmc_ticks_to_ps(dev_t->cyc_oe));
879 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
880
881 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
882 gpmc_t->cs_rd_off = gpmc_t->oe_off;
883
884 /* rd_cycle */
885 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
886 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
887 gpmc_t->access;
888 /* XXX: barter t_ce_rdyz with t_cez_r ? */
889 if (dev_t->t_ce_rdyz)
890 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
891 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
892
893 return 0;
894}
895
896static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
c3be5b45
JH
897 struct gpmc_device_timings *dev_t,
898 bool mux)
246da26d 899{
246da26d
AM
900 u32 temp;
901
902 /* adv_wr_off */
903 temp = dev_t->t_avdp_w;
904 if (mux) {
905 temp = max_t(u32, temp,
906 gpmc_t->clk_activation + dev_t->t_avdh);
907 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
908 }
909 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
910
911 /* wr_data_mux_bus */
912 temp = max_t(u32, dev_t->t_weasu,
913 gpmc_t->clk_activation + dev_t->t_rdyo);
914 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
915 * and in that case remember to handle we_on properly
916 */
917 if (mux) {
918 temp = max_t(u32, temp,
919 gpmc_t->adv_wr_off + dev_t->t_aavdh);
920 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
921 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
922 }
923 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
924
925 /* we_on */
926 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
927 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
928 else
929 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
930
931 /* wr_access */
932 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
933 gpmc_t->wr_access = gpmc_t->access;
934
935 /* we_off */
936 temp = gpmc_t->we_on + dev_t->t_wpl;
937 temp = max_t(u32, temp,
938 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
939 temp = max_t(u32, temp,
940 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
941 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
942
943 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
944 dev_t->t_wph);
945
946 /* wr_cycle */
947 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
948 temp += gpmc_t->wr_access;
949 /* XXX: barter t_ce_rdyz with t_cez_w ? */
950 if (dev_t->t_ce_rdyz)
951 temp = max_t(u32, temp,
952 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
953 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
954
955 return 0;
956}
957
958static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
c3be5b45
JH
959 struct gpmc_device_timings *dev_t,
960 bool mux)
246da26d 961{
246da26d
AM
962 u32 temp;
963
964 /* adv_rd_off */
965 temp = dev_t->t_avdp_r;
966 if (mux)
967 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
968 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
969
970 /* oe_on */
971 temp = dev_t->t_oeasu;
972 if (mux)
973 temp = max_t(u32, temp,
974 gpmc_t->adv_rd_off + dev_t->t_aavdh);
975 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
976
977 /* access */
978 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
979 gpmc_t->oe_on + dev_t->t_oe);
980 temp = max_t(u32, temp,
981 gpmc_t->cs_on + dev_t->t_ce);
982 temp = max_t(u32, temp,
983 gpmc_t->adv_on + dev_t->t_aa);
984 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
985
986 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
987 gpmc_t->cs_rd_off = gpmc_t->oe_off;
988
989 /* rd_cycle */
990 temp = max_t(u32, dev_t->t_rd_cycle,
991 gpmc_t->cs_rd_off + dev_t->t_cez_r);
992 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
993 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
994
995 return 0;
996}
997
998static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
c3be5b45
JH
999 struct gpmc_device_timings *dev_t,
1000 bool mux)
246da26d 1001{
246da26d
AM
1002 u32 temp;
1003
1004 /* adv_wr_off */
1005 temp = dev_t->t_avdp_w;
1006 if (mux)
1007 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1008 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1009
1010 /* wr_data_mux_bus */
1011 temp = dev_t->t_weasu;
1012 if (mux) {
1013 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1014 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1015 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1016 }
1017 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1018
1019 /* we_on */
1020 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1021 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1022 else
1023 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1024
1025 /* we_off */
1026 temp = gpmc_t->we_on + dev_t->t_wpl;
1027 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1028
1029 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1030 dev_t->t_wph);
1031
1032 /* wr_cycle */
1033 temp = max_t(u32, dev_t->t_wr_cycle,
1034 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1035 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1036
1037 return 0;
1038}
1039
1040static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1041 struct gpmc_device_timings *dev_t)
1042{
1043 u32 temp;
1044
1045 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1046 gpmc_get_fclk_period();
1047
1048 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1049 dev_t->t_bacc,
1050 gpmc_t->sync_clk);
1051
1052 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1053 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1054
1055 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1056 return 0;
1057
1058 if (dev_t->ce_xdelay)
1059 gpmc_t->bool_timings.cs_extra_delay = true;
1060 if (dev_t->avd_xdelay)
1061 gpmc_t->bool_timings.adv_extra_delay = true;
1062 if (dev_t->oe_xdelay)
1063 gpmc_t->bool_timings.oe_extra_delay = true;
1064 if (dev_t->we_xdelay)
1065 gpmc_t->bool_timings.we_extra_delay = true;
1066
1067 return 0;
1068}
1069
1070static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
c3be5b45
JH
1071 struct gpmc_device_timings *dev_t,
1072 bool sync)
246da26d
AM
1073{
1074 u32 temp;
1075
1076 /* cs_on */
1077 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1078
1079 /* adv_on */
1080 temp = dev_t->t_avdasu;
1081 if (dev_t->t_ce_avd)
1082 temp = max_t(u32, temp,
1083 gpmc_t->cs_on + dev_t->t_ce_avd);
1084 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1085
c3be5b45 1086 if (sync)
246da26d
AM
1087 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1088
1089 return 0;
1090}
1091
1092/* TODO: remove this function once all peripherals are confirmed to
1093 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1094 * has to be modified to handle timings in ps instead of ns
1095*/
1096static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1097{
1098 t->cs_on /= 1000;
1099 t->cs_rd_off /= 1000;
1100 t->cs_wr_off /= 1000;
1101 t->adv_on /= 1000;
1102 t->adv_rd_off /= 1000;
1103 t->adv_wr_off /= 1000;
1104 t->we_on /= 1000;
1105 t->we_off /= 1000;
1106 t->oe_on /= 1000;
1107 t->oe_off /= 1000;
1108 t->page_burst_access /= 1000;
1109 t->access /= 1000;
1110 t->rd_cycle /= 1000;
1111 t->wr_cycle /= 1000;
1112 t->bus_turnaround /= 1000;
1113 t->cycle2cycle_delay /= 1000;
1114 t->wait_monitoring /= 1000;
1115 t->clk_activation /= 1000;
1116 t->wr_access /= 1000;
1117 t->wr_data_mux_bus /= 1000;
1118}
1119
1120int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
c3be5b45
JH
1121 struct gpmc_settings *gpmc_s,
1122 struct gpmc_device_timings *dev_t)
246da26d 1123{
c3be5b45
JH
1124 bool mux = false, sync = false;
1125
1126 if (gpmc_s) {
1127 mux = gpmc_s->mux_add_data ? true : false;
1128 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1129 }
1130
246da26d
AM
1131 memset(gpmc_t, 0, sizeof(*gpmc_t));
1132
c3be5b45 1133 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
246da26d 1134
c3be5b45
JH
1135 if (gpmc_s && gpmc_s->sync_read)
1136 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
246da26d 1137 else
c3be5b45 1138 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
246da26d 1139
c3be5b45
JH
1140 if (gpmc_s && gpmc_s->sync_write)
1141 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
246da26d 1142 else
c3be5b45 1143 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
246da26d
AM
1144
1145 /* TODO: remove, see function definition */
1146 gpmc_convert_ps_to_ns(gpmc_t);
1147
1148 return 0;
1149}
1150
aa8d4767
JH
1151/**
1152 * gpmc_cs_program_settings - programs non-timing related settings
1153 * @cs: GPMC chip-select to program
1154 * @p: pointer to GPMC settings structure
1155 *
1156 * Programs non-timing related settings for a GPMC chip-select, such as
1157 * bus-width, burst configuration, etc. Function should be called once
1158 * for each chip-select that is being used and must be called before
1159 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1160 * register will be initialised to zero by this function. Returns 0 on
1161 * success and appropriate negative error code on failure.
1162 */
1163int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1164{
1165 u32 config1;
1166
1167 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1168 pr_err("%s: invalid width %d!", __func__, p->device_width);
1169 return -EINVAL;
1170 }
1171
1172 /* Address-data multiplexing not supported for NAND devices */
1173 if (p->device_nand && p->mux_add_data) {
1174 pr_err("%s: invalid configuration!\n", __func__);
1175 return -EINVAL;
1176 }
1177
1178 if ((p->mux_add_data > GPMC_MUX_AD) ||
1179 ((p->mux_add_data == GPMC_MUX_AAD) &&
1180 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1181 pr_err("%s: invalid multiplex configuration!\n", __func__);
1182 return -EINVAL;
1183 }
1184
1185 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1186 if (p->burst_read || p->burst_write) {
1187 switch (p->burst_len) {
1188 case GPMC_BURST_4:
1189 case GPMC_BURST_8:
1190 case GPMC_BURST_16:
1191 break;
1192 default:
1193 pr_err("%s: invalid page/burst-length (%d)\n",
1194 __func__, p->burst_len);
1195 return -EINVAL;
1196 }
1197 }
1198
2b54057c 1199 if (p->wait_pin > gpmc_nr_waitpins) {
aa8d4767
JH
1200 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1201 return -EINVAL;
1202 }
1203
1204 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1205
1206 if (p->sync_read)
1207 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1208 if (p->sync_write)
1209 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1210 if (p->wait_on_read)
1211 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1212 if (p->wait_on_write)
1213 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1214 if (p->wait_on_read || p->wait_on_write)
1215 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1216 if (p->device_nand)
1217 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1218 if (p->mux_add_data)
1219 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1220 if (p->burst_read)
1221 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1222 if (p->burst_write)
1223 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1224 if (p->burst_read || p->burst_write) {
1225 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1226 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1227 }
1228
1229 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1230
1231 return 0;
1232}
1233
bc6b1e7b 1234#ifdef CONFIG_OF
31957609 1235static const struct of_device_id gpmc_dt_ids[] = {
bc6b1e7b
DM
1236 { .compatible = "ti,omap2420-gpmc" },
1237 { .compatible = "ti,omap2430-gpmc" },
1238 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1239 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1240 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1241 { }
1242};
1243MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1244
8c8a7771
JH
1245/**
1246 * gpmc_read_settings_dt - read gpmc settings from device-tree
1247 * @np: pointer to device-tree node for a gpmc child device
1248 * @p: pointer to gpmc settings structure
1249 *
1250 * Reads the GPMC settings for a GPMC child device from device-tree and
1251 * stores them in the GPMC settings structure passed. The GPMC settings
1252 * structure is initialised to zero by this function and so any
1253 * previously stored settings will be cleared.
1254 */
1255void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1256{
1257 memset(p, 0, sizeof(struct gpmc_settings));
1258
1259 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1260 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
8c8a7771
JH
1261 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1262 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1263
1264 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1265 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1266 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1267 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1268 if (!p->burst_read && !p->burst_write)
1269 pr_warn("%s: page/burst-length set but not used!\n",
1270 __func__);
1271 }
1272
1273 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1274 p->wait_on_read = of_property_read_bool(np,
1275 "gpmc,wait-on-read");
1276 p->wait_on_write = of_property_read_bool(np,
1277 "gpmc,wait-on-write");
1278 if (!p->wait_on_read && !p->wait_on_write)
2b54057c
RQ
1279 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1280 __func__);
8c8a7771
JH
1281 }
1282}
1283
bc6b1e7b
DM
1284static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1285 struct gpmc_timings *gpmc_t)
1286{
d36b4cd4
JH
1287 struct gpmc_bool_timings *p;
1288
1289 if (!np || !gpmc_t)
1290 return;
bc6b1e7b
DM
1291
1292 memset(gpmc_t, 0, sizeof(*gpmc_t));
1293
1294 /* minimum clock period for syncronous mode */
d36b4cd4 1295 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
bc6b1e7b
DM
1296
1297 /* chip select timtings */
d36b4cd4
JH
1298 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1299 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1300 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
bc6b1e7b
DM
1301
1302 /* ADV signal timings */
d36b4cd4
JH
1303 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1304 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1305 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
bc6b1e7b
DM
1306
1307 /* WE signal timings */
d36b4cd4
JH
1308 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1309 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
bc6b1e7b
DM
1310
1311 /* OE signal timings */
d36b4cd4
JH
1312 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1313 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
bc6b1e7b
DM
1314
1315 /* access and cycle timings */
d36b4cd4
JH
1316 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1317 &gpmc_t->page_burst_access);
1318 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1319 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1320 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1321 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1322 &gpmc_t->bus_turnaround);
1323 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1324 &gpmc_t->cycle2cycle_delay);
1325 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1326 &gpmc_t->wait_monitoring);
1327 of_property_read_u32(np, "gpmc,clk-activation-ns",
1328 &gpmc_t->clk_activation);
1329
1330 /* only applicable to OMAP3+ */
1331 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1332 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1333 &gpmc_t->wr_data_mux_bus);
1334
1335 /* bool timing parameters */
1336 p = &gpmc_t->bool_timings;
1337
1338 p->cycle2cyclediffcsen =
1339 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1340 p->cycle2cyclesamecsen =
1341 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1342 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1343 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1344 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1345 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1346 p->time_para_granularity =
1347 of_property_read_bool(np, "gpmc,time-para-granularity");
bc6b1e7b
DM
1348}
1349
6b187b21 1350#if IS_ENABLED(CONFIG_MTD_NAND)
bc6b1e7b 1351
496c8a0b
MJ
1352static const char * const nand_xfer_types[] = {
1353 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1354 [NAND_OMAP_POLLED] = "polled",
1355 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1356 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1357};
1358
bc6b1e7b
DM
1359static int gpmc_probe_nand_child(struct platform_device *pdev,
1360 struct device_node *child)
1361{
1362 u32 val;
1363 const char *s;
1364 struct gpmc_timings gpmc_t;
1365 struct omap_nand_platform_data *gpmc_nand_data;
1366
1367 if (of_property_read_u32(child, "reg", &val) < 0) {
1368 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1369 child->full_name);
1370 return -ENODEV;
1371 }
1372
1373 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1374 GFP_KERNEL);
1375 if (!gpmc_nand_data)
1376 return -ENOMEM;
1377
1378 gpmc_nand_data->cs = val;
1379 gpmc_nand_data->of_node = child;
1380
ac65caf5
PG
1381 /* Detect availability of ELM module */
1382 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1383 if (gpmc_nand_data->elm_of_node == NULL)
1384 gpmc_nand_data->elm_of_node =
1385 of_parse_phandle(child, "elm_id", 0);
1386 if (gpmc_nand_data->elm_of_node == NULL)
1387 pr_warn("%s: ti,elm-id property not found\n", __func__);
1388
1389 /* select ecc-scheme for NAND */
1390 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1391 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1392 return -ENODEV;
1393 }
a3e83f05
RQ
1394
1395 if (!strcmp(s, "sw"))
1396 gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1397 else if (!strcmp(s, "ham1") ||
1398 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
ac65caf5
PG
1399 gpmc_nand_data->ecc_opt =
1400 OMAP_ECC_HAM1_CODE_HW;
1401 else if (!strcmp(s, "bch4"))
1402 if (gpmc_nand_data->elm_of_node)
1403 gpmc_nand_data->ecc_opt =
1404 OMAP_ECC_BCH4_CODE_HW;
1405 else
1406 gpmc_nand_data->ecc_opt =
1407 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1408 else if (!strcmp(s, "bch8"))
1409 if (gpmc_nand_data->elm_of_node)
1410 gpmc_nand_data->ecc_opt =
1411 OMAP_ECC_BCH8_CODE_HW;
1412 else
1413 gpmc_nand_data->ecc_opt =
1414 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
27c9fd60 1415 else if (!strcmp(s, "bch16"))
1416 if (gpmc_nand_data->elm_of_node)
1417 gpmc_nand_data->ecc_opt =
1418 OMAP_ECC_BCH16_CODE_HW;
1419 else
1420 pr_err("%s: BCH16 requires ELM support\n", __func__);
ac65caf5
PG
1421 else
1422 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
bc6b1e7b 1423
ac65caf5 1424 /* select data transfer mode for NAND controller */
496c8a0b
MJ
1425 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1426 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1427 if (!strcasecmp(s, nand_xfer_types[val])) {
1428 gpmc_nand_data->xfer_type = val;
1429 break;
1430 }
1431
fef775ca
EG
1432 gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
1433
bc6b1e7b
DM
1434 val = of_get_nand_bus_width(child);
1435 if (val == 16)
1436 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1437
1438 gpmc_read_timings_dt(child, &gpmc_t);
1439 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1440
1441 return 0;
1442}
1443#else
1444static int gpmc_probe_nand_child(struct platform_device *pdev,
1445 struct device_node *child)
1446{
1447 return 0;
1448}
1449#endif
1450
980386d2 1451#if IS_ENABLED(CONFIG_MTD_ONENAND)
75d3625e
EG
1452static int gpmc_probe_onenand_child(struct platform_device *pdev,
1453 struct device_node *child)
1454{
1455 u32 val;
1456 struct omap_onenand_platform_data *gpmc_onenand_data;
1457
1458 if (of_property_read_u32(child, "reg", &val) < 0) {
1459 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1460 child->full_name);
1461 return -ENODEV;
1462 }
1463
1464 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1465 GFP_KERNEL);
1466 if (!gpmc_onenand_data)
1467 return -ENOMEM;
1468
1469 gpmc_onenand_data->cs = val;
1470 gpmc_onenand_data->of_node = child;
1471 gpmc_onenand_data->dma_channel = -1;
1472
1473 if (!of_property_read_u32(child, "dma-channel", &val))
1474 gpmc_onenand_data->dma_channel = val;
1475
1476 gpmc_onenand_init(gpmc_onenand_data);
1477
1478 return 0;
1479}
1480#else
1481static int gpmc_probe_onenand_child(struct platform_device *pdev,
1482 struct device_node *child)
1483{
1484 return 0;
1485}
1486#endif
1487
cdd6928c 1488/**
3af91cf7 1489 * gpmc_probe_generic_child - configures the gpmc for a child device
cdd6928c 1490 * @pdev: pointer to gpmc platform device
3af91cf7 1491 * @child: pointer to device-tree node for child device
cdd6928c 1492 *
3af91cf7 1493 * Allocates and configures a GPMC chip-select for a child device.
cdd6928c
JH
1494 * Returns 0 on success and appropriate negative error code on failure.
1495 */
3af91cf7 1496static int gpmc_probe_generic_child(struct platform_device *pdev,
cdd6928c
JH
1497 struct device_node *child)
1498{
1499 struct gpmc_settings gpmc_s;
1500 struct gpmc_timings gpmc_t;
1501 struct resource res;
1502 unsigned long base;
1503 int ret, cs;
1504
1505 if (of_property_read_u32(child, "reg", &cs) < 0) {
1506 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1507 child->full_name);
1508 return -ENODEV;
1509 }
1510
1511 if (of_address_to_resource(child, 0, &res) < 0) {
1512 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1513 child->full_name);
1514 return -ENODEV;
1515 }
1516
1517 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1518 if (ret < 0) {
1519 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1520 return ret;
1521 }
1522
fd4446f2
TL
1523 /*
1524 * For some GPMC devices we still need to rely on the bootloader
1525 * timings because the devices can be connected via FPGA. So far
1526 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1527 * REVISIT: Add timing support from slls644g.pdf and from the
1528 * lan91c96 manual.
1529 */
1530 if (of_device_is_compatible(child, "ns16550a") ||
1531 of_device_is_compatible(child, "smsc,lan91c94") ||
1532 of_device_is_compatible(child, "smsc,lan91c111")) {
1533 dev_warn(&pdev->dev,
1534 "%s using bootloader timings on CS%d\n",
1535 child->name, cs);
1536 goto no_timings;
1537 }
1538
cdd6928c
JH
1539 /*
1540 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1541 * location in the gpmc address space. When booting with
1542 * device-tree we want the NOR flash to be mapped to the
1543 * location specified in the device-tree blob. So remap the
1544 * CS to this location. Once DT migration is complete should
1545 * just make gpmc_cs_request() map a specific address.
1546 */
1547 ret = gpmc_cs_remap(cs, res.start);
1548 if (ret < 0) {
f70bf2a3
FE
1549 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1550 cs, &res.start);
cdd6928c
JH
1551 goto err;
1552 }
1553
1554 gpmc_read_settings_dt(child, &gpmc_s);
1555
1556 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
1557 if (ret < 0)
1558 goto err;
1559
1560 ret = gpmc_cs_program_settings(cs, &gpmc_s);
1561 if (ret < 0)
1562 goto err;
1563
1564 gpmc_read_timings_dt(child, &gpmc_t);
1565 gpmc_cs_set_timings(cs, &gpmc_t);
1566
fd4446f2 1567no_timings:
cdd6928c
JH
1568 if (of_platform_device_create(child, NULL, &pdev->dev))
1569 return 0;
1570
1571 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
e8ffd6fd 1572 ret = -ENODEV;
cdd6928c
JH
1573
1574err:
1575 gpmc_cs_free(cs);
1576
1577 return ret;
1578}
1579
bc6b1e7b
DM
1580static int gpmc_probe_dt(struct platform_device *pdev)
1581{
1582 int ret;
1583 struct device_node *child;
1584 const struct of_device_id *of_id =
1585 of_match_device(gpmc_dt_ids, &pdev->dev);
1586
1587 if (!of_id)
1588 return 0;
1589
f34f3716
GP
1590 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
1591 &gpmc_cs_num);
1592 if (ret < 0) {
1593 pr_err("%s: number of chip-selects not defined\n", __func__);
1594 return ret;
1595 } else if (gpmc_cs_num < 1) {
1596 pr_err("%s: all chip-selects are disabled\n", __func__);
1597 return -EINVAL;
1598 } else if (gpmc_cs_num > GPMC_CS_NUM) {
1599 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1600 __func__, GPMC_CS_NUM);
1601 return -EINVAL;
1602 }
1603
9f833156
JH
1604 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
1605 &gpmc_nr_waitpins);
1606 if (ret < 0) {
1607 pr_err("%s: number of wait pins not found!\n", __func__);
1608 return ret;
1609 }
1610
68e2eb53 1611 for_each_available_child_of_node(pdev->dev.of_node, child) {
bc6b1e7b 1612
f2b09f67
JMC
1613 if (!child->name)
1614 continue;
cdd6928c 1615
f2b09f67
JMC
1616 if (of_node_cmp(child->name, "nand") == 0)
1617 ret = gpmc_probe_nand_child(pdev, child);
1618 else if (of_node_cmp(child->name, "onenand") == 0)
1619 ret = gpmc_probe_onenand_child(pdev, child);
1620 else if (of_node_cmp(child->name, "ethernet") == 0 ||
fd4446f2
TL
1621 of_node_cmp(child->name, "nor") == 0 ||
1622 of_node_cmp(child->name, "uart") == 0)
f2b09f67 1623 ret = gpmc_probe_generic_child(pdev, child);
cdd6928c 1624
b327b362
JMC
1625 if (WARN(ret < 0, "%s: probing gpmc child %s failed\n",
1626 __func__, child->full_name))
5330dc16 1627 of_node_put(child);
5330dc16
JMC
1628 }
1629
bc6b1e7b
DM
1630 return 0;
1631}
1632#else
1633static int gpmc_probe_dt(struct platform_device *pdev)
1634{
1635 return 0;
1636}
1637#endif
1638
351a102d 1639static int gpmc_probe(struct platform_device *pdev)
4bbbc1ad 1640{
8119024e 1641 int rc;
6b6c32fc 1642 u32 l;
da496873 1643 struct resource *res;
4bbbc1ad 1644
da496873
AM
1645 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1646 if (res == NULL)
1647 return -ENOENT;
8d08436d 1648
da496873
AM
1649 phys_base = res->start;
1650 mem_size = resource_size(res);
fd1dc87d 1651
5857bd98
TR
1652 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1653 if (IS_ERR(gpmc_base))
1654 return PTR_ERR(gpmc_base);
da496873
AM
1655
1656 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1657 if (res == NULL)
1658 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1659 else
1660 gpmc_irq = res->start;
1661
1662 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1663 if (IS_ERR(gpmc_l3_clk)) {
1664 dev_err(&pdev->dev, "error: clk_get\n");
1665 gpmc_irq = 0;
1666 return PTR_ERR(gpmc_l3_clk);
fd1dc87d
PW
1667 }
1668
b3f5525c 1669 pm_runtime_enable(&pdev->dev);
1670 pm_runtime_get_sync(&pdev->dev);
1daa8c1d 1671
da496873
AM
1672 gpmc_dev = &pdev->dev;
1673
4bbbc1ad 1674 l = gpmc_read_reg(GPMC_REVISION);
aa8d4767
JH
1675
1676 /*
1677 * FIXME: Once device-tree migration is complete the below flags
1678 * should be populated based upon the device-tree compatible
1679 * string. For now just use the IP revision. OMAP3+ devices have
1680 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1681 * devices support the addr-addr-data multiplex protocol.
1682 *
1683 * GPMC IP revisions:
1684 * - OMAP24xx = 2.0
1685 * - OMAP3xxx = 5.0
1686 * - OMAP44xx/54xx/AM335x = 6.0
1687 */
da496873
AM
1688 if (GPMC_REVISION_MAJOR(l) > 0x4)
1689 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
aa8d4767
JH
1690 if (GPMC_REVISION_MAJOR(l) > 0x5)
1691 gpmc_capability |= GPMC_HAS_MUX_AAD;
da496873
AM
1692 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1693 GPMC_REVISION_MINOR(l));
1694
84b00f0e 1695 gpmc_mem_init();
db97eb7d 1696
71856843 1697 if (gpmc_setup_irq() < 0)
da496873
AM
1698 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1699
31d9adca
JH
1700 /* Now the GPMC is initialised, unreserve the chip-selects */
1701 gpmc_cs_map = 0;
1702
f34f3716
GP
1703 if (!pdev->dev.of_node) {
1704 gpmc_cs_num = GPMC_CS_NUM;
9f833156 1705 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
f34f3716 1706 }
9f833156 1707
bc6b1e7b
DM
1708 rc = gpmc_probe_dt(pdev);
1709 if (rc < 0) {
b3f5525c 1710 pm_runtime_put_sync(&pdev->dev);
bc6b1e7b
DM
1711 clk_put(gpmc_l3_clk);
1712 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1713 return rc;
1714 }
1715
da496873
AM
1716 return 0;
1717}
1718
351a102d 1719static int gpmc_remove(struct platform_device *pdev)
da496873
AM
1720{
1721 gpmc_free_irq();
1722 gpmc_mem_exit();
b3f5525c 1723 pm_runtime_put_sync(&pdev->dev);
1724 pm_runtime_disable(&pdev->dev);
da496873
AM
1725 gpmc_dev = NULL;
1726 return 0;
1727}
1728
b536dd41 1729#ifdef CONFIG_PM_SLEEP
1730static int gpmc_suspend(struct device *dev)
1731{
1732 omap3_gpmc_save_context();
1733 pm_runtime_put_sync(dev);
1734 return 0;
1735}
1736
1737static int gpmc_resume(struct device *dev)
1738{
1739 pm_runtime_get_sync(dev);
1740 omap3_gpmc_restore_context();
1741 return 0;
1742}
1743#endif
1744
1745static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
1746
da496873
AM
1747static struct platform_driver gpmc_driver = {
1748 .probe = gpmc_probe,
351a102d 1749 .remove = gpmc_remove,
da496873
AM
1750 .driver = {
1751 .name = DEVICE_NAME,
1752 .owner = THIS_MODULE,
bc6b1e7b 1753 .of_match_table = of_match_ptr(gpmc_dt_ids),
b536dd41 1754 .pm = &gpmc_pm_ops,
da496873
AM
1755 },
1756};
1757
1758static __init int gpmc_init(void)
1759{
1760 return platform_driver_register(&gpmc_driver);
1761}
1762
1763static __exit void gpmc_exit(void)
1764{
1765 platform_driver_unregister(&gpmc_driver);
1766
db97eb7d 1767}
da496873 1768
b76c8b19 1769omap_postcore_initcall(gpmc_init);
da496873 1770module_exit(gpmc_exit);
db97eb7d 1771
4be48fd5
AM
1772static int __init omap_gpmc_init(void)
1773{
1774 struct omap_hwmod *oh;
1775 struct platform_device *pdev;
1776 char *oh_name = "gpmc";
1777
2f98ca89
DM
1778 /*
1779 * if the board boots up with a populated DT, do not
1780 * manually add the device from this initcall
1781 */
1782 if (of_have_populated_dt())
1783 return -ENODEV;
1784
4be48fd5
AM
1785 oh = omap_hwmod_lookup(oh_name);
1786 if (!oh) {
1787 pr_err("Could not look up %s\n", oh_name);
1788 return -ENODEV;
1789 }
1790
c1d1cd59 1791 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
4be48fd5
AM
1792 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1793
1261674a 1794 return PTR_RET(pdev);
4be48fd5 1795}
b76c8b19 1796omap_postcore_initcall(omap_gpmc_init);
4be48fd5 1797
db97eb7d
SG
1798static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1799{
6b6c32fc
AM
1800 int i;
1801 u32 regval;
1802
1803 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1804
1805 if (!regval)
1806 return IRQ_NONE;
1807
1808 for (i = 0; i < GPMC_NR_IRQ; i++)
1809 if (regval & gpmc_client_irq[i].bitmask)
1810 generic_handle_irq(gpmc_client_irq[i].irq);
db97eb7d 1811
6b6c32fc 1812 gpmc_write_reg(GPMC_IRQSTATUS, regval);
db97eb7d
SG
1813
1814 return IRQ_HANDLED;
4bbbc1ad 1815}
a2d3e7ba 1816
a2d3e7ba
RN
1817static struct omap3_gpmc_regs gpmc_context;
1818
b2fa3b7c 1819void omap3_gpmc_save_context(void)
a2d3e7ba
RN
1820{
1821 int i;
b2fa3b7c 1822
a2d3e7ba
RN
1823 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1824 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1825 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1826 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1827 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1828 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1829 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
f34f3716 1830 for (i = 0; i < gpmc_cs_num; i++) {
a2d3e7ba
RN
1831 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1832 if (gpmc_context.cs_context[i].is_valid) {
1833 gpmc_context.cs_context[i].config1 =
1834 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1835 gpmc_context.cs_context[i].config2 =
1836 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1837 gpmc_context.cs_context[i].config3 =
1838 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1839 gpmc_context.cs_context[i].config4 =
1840 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1841 gpmc_context.cs_context[i].config5 =
1842 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1843 gpmc_context.cs_context[i].config6 =
1844 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1845 gpmc_context.cs_context[i].config7 =
1846 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1847 }
1848 }
1849}
1850
b2fa3b7c 1851void omap3_gpmc_restore_context(void)
a2d3e7ba
RN
1852{
1853 int i;
b2fa3b7c 1854
a2d3e7ba
RN
1855 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1856 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1857 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1858 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1859 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1860 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1861 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
f34f3716 1862 for (i = 0; i < gpmc_cs_num; i++) {
a2d3e7ba
RN
1863 if (gpmc_context.cs_context[i].is_valid) {
1864 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1865 gpmc_context.cs_context[i].config1);
1866 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1867 gpmc_context.cs_context[i].config2);
1868 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1869 gpmc_context.cs_context[i].config3);
1870 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1871 gpmc_context.cs_context[i].config4);
1872 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1873 gpmc_context.cs_context[i].config5);
1874 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1875 gpmc_context.cs_context[i].config6);
1876 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1877 gpmc_context.cs_context[i].config7);
1878 }
1879 }
1880}