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