ARM: OMAP2+: Simplify code configuring ONENAND devices
[deliverable/linux.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
DM
28#include <linux/of.h>
29#include <linux/of_mtd.h>
30#include <linux/of_device.h>
31#include <linux/mtd/nand.h>
4bbbc1ad 32
bc3668ea 33#include <linux/platform_data/mtd-nand-omap2.h>
4bbbc1ad 34
7f245162 35#include <asm/mach-types.h>
72d0f1c3 36
dbc04161 37#include "soc.h"
7d7e1eba 38#include "common.h"
25c7d49e 39#include "omap_device.h"
3ef5d007 40#include "gpmc.h"
bc6b1e7b 41#include "gpmc-nand.h"
75d3625e 42#include "gpmc-onenand.h"
7d7e1eba 43
4be48fd5
AM
44#define DEVICE_NAME "omap-gpmc"
45
fd1dc87d 46/* GPMC register offsets */
4bbbc1ad
JY
47#define GPMC_REVISION 0x00
48#define GPMC_SYSCONFIG 0x10
49#define GPMC_SYSSTATUS 0x14
50#define GPMC_IRQSTATUS 0x18
51#define GPMC_IRQENABLE 0x1c
52#define GPMC_TIMEOUT_CONTROL 0x40
53#define GPMC_ERR_ADDRESS 0x44
54#define GPMC_ERR_TYPE 0x48
55#define GPMC_CONFIG 0x50
56#define GPMC_STATUS 0x54
57#define GPMC_PREFETCH_CONFIG1 0x1e0
58#define GPMC_PREFETCH_CONFIG2 0x1e4
15e02a3b 59#define GPMC_PREFETCH_CONTROL 0x1ec
4bbbc1ad
JY
60#define GPMC_PREFETCH_STATUS 0x1f0
61#define GPMC_ECC_CONFIG 0x1f4
62#define GPMC_ECC_CONTROL 0x1f8
63#define GPMC_ECC_SIZE_CONFIG 0x1fc
948d38e7 64#define GPMC_ECC1_RESULT 0x200
8d602cf5 65#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
2fdf0c98
AM
66#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
67#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
68#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
4bbbc1ad 69
2c65e744
YY
70/* GPMC ECC control settings */
71#define GPMC_ECC_CTRL_ECCCLEAR 0x100
72#define GPMC_ECC_CTRL_ECCDISABLE 0x000
73#define GPMC_ECC_CTRL_ECCREG1 0x001
74#define GPMC_ECC_CTRL_ECCREG2 0x002
75#define GPMC_ECC_CTRL_ECCREG3 0x003
76#define GPMC_ECC_CTRL_ECCREG4 0x004
77#define GPMC_ECC_CTRL_ECCREG5 0x005
78#define GPMC_ECC_CTRL_ECCREG6 0x006
79#define GPMC_ECC_CTRL_ECCREG7 0x007
80#define GPMC_ECC_CTRL_ECCREG8 0x008
81#define GPMC_ECC_CTRL_ECCREG9 0x009
82
559d94b0
AM
83#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
84#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
85#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
86#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
87#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
88#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
89
948d38e7 90#define GPMC_CS0_OFFSET 0x60
4bbbc1ad 91#define GPMC_CS_SIZE 0x30
2fdf0c98 92#define GPMC_BCH_SIZE 0x10
4bbbc1ad 93
f37e4580
ID
94#define GPMC_MEM_START 0x00000000
95#define GPMC_MEM_END 0x3FFFFFFF
96#define BOOT_ROM_SPACE 0x100000 /* 1MB */
97
98#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
99#define GPMC_SECTION_SHIFT 28 /* 128 MB */
100
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
110
6b6c32fc
AM
111/* XXX: Only NAND irq has been considered,currently these are the only ones used
112 */
113#define GPMC_NR_IRQ 2
114
115struct gpmc_client_irq {
116 unsigned irq;
117 u32 bitmask;
118};
119
a2d3e7ba
RN
120/* Structure to save gpmc cs context */
121struct gpmc_cs_config {
122 u32 config1;
123 u32 config2;
124 u32 config3;
125 u32 config4;
126 u32 config5;
127 u32 config6;
128 u32 config7;
129 int is_valid;
130};
131
132/*
133 * Structure to save/restore gpmc context
134 * to support core off on OMAP3
135 */
136struct omap3_gpmc_regs {
137 u32 sysconfig;
138 u32 irqenable;
139 u32 timeout_ctrl;
140 u32 config;
141 u32 prefetch_config1;
142 u32 prefetch_config2;
143 u32 prefetch_control;
144 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
145};
146
6b6c32fc
AM
147static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
148static struct irq_chip gpmc_irq_chip;
149static unsigned gpmc_irq_start;
150
f37e4580
ID
151static struct resource gpmc_mem_root;
152static struct resource gpmc_cs_mem[GPMC_CS_NUM];
87b247c4 153static DEFINE_SPINLOCK(gpmc_mem_lock);
6797b4fe
JH
154/* Define chip-selects as reserved by default until probe completes */
155static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
da496873
AM
156static struct device *gpmc_dev;
157static int gpmc_irq;
158static resource_size_t phys_base, mem_size;
159static unsigned gpmc_capability;
fd1dc87d 160static void __iomem *gpmc_base;
4bbbc1ad 161
fd1dc87d 162static struct clk *gpmc_l3_clk;
4bbbc1ad 163
db97eb7d
SG
164static irqreturn_t gpmc_handle_irq(int irq, void *dev);
165
4bbbc1ad
JY
166static void gpmc_write_reg(int idx, u32 val)
167{
168 __raw_writel(val, gpmc_base + idx);
169}
170
171static u32 gpmc_read_reg(int idx)
172{
173 return __raw_readl(gpmc_base + idx);
174}
175
176void gpmc_cs_write_reg(int cs, int idx, u32 val)
177{
178 void __iomem *reg_addr;
179
948d38e7 180 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
4bbbc1ad
JY
181 __raw_writel(val, reg_addr);
182}
183
3fc089e7 184static u32 gpmc_cs_read_reg(int cs, int idx)
4bbbc1ad 185{
fd1dc87d
PW
186 void __iomem *reg_addr;
187
948d38e7 188 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
fd1dc87d 189 return __raw_readl(reg_addr);
4bbbc1ad
JY
190}
191
fd1dc87d 192/* TODO: Add support for gpmc_fck to clock framework and use it */
3fc089e7 193static unsigned long gpmc_get_fclk_period(void)
4bbbc1ad 194{
fd1dc87d
PW
195 unsigned long rate = clk_get_rate(gpmc_l3_clk);
196
197 if (rate == 0) {
198 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
199 return 0;
200 }
201
202 rate /= 1000;
203 rate = 1000000000 / rate; /* In picoseconds */
204
205 return rate;
4bbbc1ad
JY
206}
207
3fc089e7 208static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
4bbbc1ad
JY
209{
210 unsigned long tick_ps;
211
212 /* Calculate in picosecs to yield more exact results */
213 tick_ps = gpmc_get_fclk_period();
214
215 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
216}
217
3fc089e7 218static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
a3551f5b
AH
219{
220 unsigned long tick_ps;
221
222 /* Calculate in picosecs to yield more exact results */
223 tick_ps = gpmc_get_fclk_period();
224
225 return (time_ps + tick_ps - 1) / tick_ps;
226}
227
fd1dc87d
PW
228unsigned int gpmc_ticks_to_ns(unsigned int ticks)
229{
230 return ticks * gpmc_get_fclk_period() / 1000;
231}
232
246da26d
AM
233static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
234{
235 return ticks * gpmc_get_fclk_period();
236}
237
238static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
239{
240 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
241
242 return ticks * gpmc_get_fclk_period();
243}
244
559d94b0
AM
245static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
246{
247 u32 l;
248
249 l = gpmc_cs_read_reg(cs, reg);
250 if (value)
251 l |= mask;
252 else
253 l &= ~mask;
254 gpmc_cs_write_reg(cs, reg, l);
255}
256
257static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
258{
259 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
260 GPMC_CONFIG1_TIME_PARA_GRAN,
261 p->time_para_granularity);
262 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
263 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
264 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
265 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
266 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
267 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
268 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
269 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
270 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
271 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
272 p->cycle2cyclesamecsen);
273 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
274 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
275 p->cycle2cyclediffcsen);
276}
277
4bbbc1ad
JY
278#ifdef DEBUG
279static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
2aab6468 280 int time, const char *name)
4bbbc1ad
JY
281#else
282static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
283 int time)
284#endif
285{
286 u32 l;
287 int ticks, mask, nr_bits;
288
289 if (time == 0)
290 ticks = 0;
291 else
292 ticks = gpmc_ns_to_ticks(time);
293 nr_bits = end_bit - st_bit + 1;
1c22cc13
DB
294 if (ticks >= 1 << nr_bits) {
295#ifdef DEBUG
296 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
297 cs, name, time, ticks, 1 << nr_bits);
298#endif
4bbbc1ad 299 return -1;
1c22cc13 300 }
4bbbc1ad
JY
301
302 mask = (1 << nr_bits) - 1;
303 l = gpmc_cs_read_reg(cs, reg);
304#ifdef DEBUG
1c22cc13
DB
305 printk(KERN_INFO
306 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
2aab6468 307 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
1c22cc13 308 (l >> st_bit) & mask, time);
4bbbc1ad
JY
309#endif
310 l &= ~(mask << st_bit);
311 l |= ticks << st_bit;
312 gpmc_cs_write_reg(cs, reg, l);
313
314 return 0;
315}
316
317#ifdef DEBUG
318#define GPMC_SET_ONE(reg, st, end, field) \
319 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
320 t->field, #field) < 0) \
321 return -1
322#else
323#define GPMC_SET_ONE(reg, st, end, field) \
324 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
325 return -1
326#endif
327
1b47ca1a 328int gpmc_calc_divider(unsigned int sync_clk)
4bbbc1ad
JY
329{
330 int div;
331 u32 l;
332
a3551f5b 333 l = sync_clk + (gpmc_get_fclk_period() - 1);
4bbbc1ad
JY
334 div = l / gpmc_get_fclk_period();
335 if (div > 4)
336 return -1;
1c22cc13 337 if (div <= 0)
4bbbc1ad
JY
338 div = 1;
339
340 return div;
341}
342
343int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
344{
345 int div;
346 u32 l;
347
1b47ca1a 348 div = gpmc_calc_divider(t->sync_clk);
4bbbc1ad 349 if (div < 0)
a032d33b 350 return div;
4bbbc1ad
JY
351
352 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
353 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
354 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
355
356 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
357 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
358 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
359
360 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
361 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
362 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
363 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
364
365 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
366 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
367 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
368
369 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
370
559d94b0
AM
371 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
372 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
373
374 GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
375 GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
376
da496873 377 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
cc26b3b0 378 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
da496873 379 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
cc26b3b0 380 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
cc26b3b0 381
1c22cc13
DB
382 /* caller is expected to have initialized CONFIG1 to cover
383 * at least sync vs async
384 */
385 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
386 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
4bbbc1ad 387#ifdef DEBUG
1c22cc13
DB
388 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
389 cs, (div * gpmc_get_fclk_period()) / 1000, div);
4bbbc1ad 390#endif
1c22cc13
DB
391 l &= ~0x03;
392 l |= (div - 1);
393 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
394 }
4bbbc1ad 395
559d94b0
AM
396 gpmc_cs_bool_timings(cs, &t->bool_timings);
397
4bbbc1ad
JY
398 return 0;
399}
400
f37e4580
ID
401static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
402{
403 u32 l;
404 u32 mask;
405
406 mask = (1 << GPMC_SECTION_SHIFT) - size;
407 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
408 l &= ~0x3f;
409 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
410 l &= ~(0x0f << 8);
411 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
a2d3e7ba 412 l |= GPMC_CONFIG7_CSVALID;
f37e4580
ID
413 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
414}
415
416static void gpmc_cs_disable_mem(int cs)
417{
418 u32 l;
419
420 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
a2d3e7ba 421 l &= ~GPMC_CONFIG7_CSVALID;
f37e4580
ID
422 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
423}
424
425static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
426{
427 u32 l;
428 u32 mask;
429
430 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
431 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
432 mask = (l >> 8) & 0x0f;
433 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
434}
435
436static int gpmc_cs_mem_enabled(int cs)
437{
438 u32 l;
439
440 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
a2d3e7ba 441 return l & GPMC_CONFIG7_CSVALID;
f37e4580
ID
442}
443
f5d8edaf 444static void gpmc_cs_set_reserved(int cs, int reserved)
4bbbc1ad 445{
f37e4580
ID
446 gpmc_cs_map &= ~(1 << cs);
447 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
448}
449
ae9d908a 450static bool gpmc_cs_reserved(int cs)
f37e4580
ID
451{
452 return gpmc_cs_map & (1 << cs);
453}
454
455static unsigned long gpmc_mem_align(unsigned long size)
456{
457 int order;
458
459 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
460 order = GPMC_CHUNK_SHIFT - 1;
461 do {
462 size >>= 1;
463 order++;
464 } while (size);
465 size = 1 << order;
466 return size;
467}
468
469static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
470{
471 struct resource *res = &gpmc_cs_mem[cs];
472 int r;
473
474 size = gpmc_mem_align(size);
475 spin_lock(&gpmc_mem_lock);
476 res->start = base;
477 res->end = base + size - 1;
478 r = request_resource(&gpmc_mem_root, res);
479 spin_unlock(&gpmc_mem_lock);
480
481 return r;
482}
483
da496873
AM
484static int gpmc_cs_delete_mem(int cs)
485{
486 struct resource *res = &gpmc_cs_mem[cs];
487 int r;
488
489 spin_lock(&gpmc_mem_lock);
490 r = release_resource(&gpmc_cs_mem[cs]);
491 res->start = 0;
492 res->end = 0;
493 spin_unlock(&gpmc_mem_lock);
494
495 return r;
496}
497
f37e4580
ID
498int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
499{
500 struct resource *res = &gpmc_cs_mem[cs];
501 int r = -1;
502
503 if (cs > GPMC_CS_NUM)
504 return -ENODEV;
505
506 size = gpmc_mem_align(size);
507 if (size > (1 << GPMC_SECTION_SHIFT))
508 return -ENOMEM;
509
510 spin_lock(&gpmc_mem_lock);
511 if (gpmc_cs_reserved(cs)) {
512 r = -EBUSY;
513 goto out;
514 }
515 if (gpmc_cs_mem_enabled(cs))
516 r = adjust_resource(res, res->start & ~(size - 1), size);
517 if (r < 0)
518 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
519 size, NULL, NULL);
520 if (r < 0)
521 goto out;
522
6d135242 523 gpmc_cs_enable_mem(cs, res->start, resource_size(res));
f37e4580
ID
524 *base = res->start;
525 gpmc_cs_set_reserved(cs, 1);
526out:
527 spin_unlock(&gpmc_mem_lock);
528 return r;
529}
fd1dc87d 530EXPORT_SYMBOL(gpmc_cs_request);
f37e4580
ID
531
532void gpmc_cs_free(int cs)
533{
534 spin_lock(&gpmc_mem_lock);
e7fdc605 535 if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
f37e4580
ID
536 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
537 BUG();
538 spin_unlock(&gpmc_mem_lock);
539 return;
540 }
541 gpmc_cs_disable_mem(cs);
542 release_resource(&gpmc_cs_mem[cs]);
543 gpmc_cs_set_reserved(cs, 0);
544 spin_unlock(&gpmc_mem_lock);
545}
fd1dc87d 546EXPORT_SYMBOL(gpmc_cs_free);
f37e4580 547
948d38e7
SG
548/**
549 * gpmc_cs_configure - write request to configure gpmc
550 * @cs: chip select number
551 * @cmd: command type
552 * @wval: value to write
553 * @return status of the operation
554 */
555int gpmc_cs_configure(int cs, int cmd, int wval)
556{
557 int err = 0;
558 u32 regval = 0;
559
560 switch (cmd) {
db97eb7d
SG
561 case GPMC_ENABLE_IRQ:
562 gpmc_write_reg(GPMC_IRQENABLE, wval);
563 break;
564
948d38e7
SG
565 case GPMC_SET_IRQ_STATUS:
566 gpmc_write_reg(GPMC_IRQSTATUS, wval);
567 break;
568
569 case GPMC_CONFIG_WP:
570 regval = gpmc_read_reg(GPMC_CONFIG);
571 if (wval)
572 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
573 else
574 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
575 gpmc_write_reg(GPMC_CONFIG, regval);
576 break;
577
578 case GPMC_CONFIG_RDY_BSY:
579 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
580 if (wval)
581 regval |= WR_RD_PIN_MONITORING;
582 else
583 regval &= ~WR_RD_PIN_MONITORING;
584 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
585 break;
586
587 case GPMC_CONFIG_DEV_SIZE:
588 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
8ef5d844
YY
589
590 /* clear 2 target bits */
591 regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
592
593 /* set the proper value */
948d38e7 594 regval |= GPMC_CONFIG1_DEVICESIZE(wval);
8ef5d844 595
948d38e7
SG
596 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
597 break;
598
599 case GPMC_CONFIG_DEV_TYPE:
600 regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
c9fb8094
MJ
601 /* clear 4 target bits */
602 regval &= ~(GPMC_CONFIG1_DEVICETYPE(3) |
603 GPMC_CONFIG1_MUXTYPE(3));
604 /* set the proper value */
948d38e7
SG
605 regval |= GPMC_CONFIG1_DEVICETYPE(wval);
606 if (wval == GPMC_DEVICETYPE_NOR)
607 regval |= GPMC_CONFIG1_MUXADDDATA;
608 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
609 break;
610
611 default:
612 printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
613 err = -EINVAL;
614 }
615
616 return err;
617}
618EXPORT_SYMBOL(gpmc_cs_configure);
619
52bd138d
AM
620void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
621{
2fdf0c98
AM
622 int i;
623
52bd138d
AM
624 reg->gpmc_status = gpmc_base + GPMC_STATUS;
625 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
626 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
627 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
628 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
629 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
630 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
631 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
632 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
633 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
634 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
635 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
636 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
637 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
638 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
2fdf0c98
AM
639
640 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
641 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
642 GPMC_BCH_SIZE * i;
643 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
644 GPMC_BCH_SIZE * i;
645 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
646 GPMC_BCH_SIZE * i;
647 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
648 GPMC_BCH_SIZE * i;
649 }
52bd138d
AM
650}
651
6b6c32fc
AM
652int gpmc_get_client_irq(unsigned irq_config)
653{
654 int i;
655
656 if (hweight32(irq_config) > 1)
657 return 0;
658
659 for (i = 0; i < GPMC_NR_IRQ; i++)
660 if (gpmc_client_irq[i].bitmask & irq_config)
661 return gpmc_client_irq[i].irq;
662
663 return 0;
664}
665
666static int gpmc_irq_endis(unsigned irq, bool endis)
667{
668 int i;
669 u32 regval;
670
671 for (i = 0; i < GPMC_NR_IRQ; i++)
672 if (irq == gpmc_client_irq[i].irq) {
673 regval = gpmc_read_reg(GPMC_IRQENABLE);
674 if (endis)
675 regval |= gpmc_client_irq[i].bitmask;
676 else
677 regval &= ~gpmc_client_irq[i].bitmask;
678 gpmc_write_reg(GPMC_IRQENABLE, regval);
679 break;
680 }
681
682 return 0;
683}
684
685static void gpmc_irq_disable(struct irq_data *p)
686{
687 gpmc_irq_endis(p->irq, false);
688}
689
690static void gpmc_irq_enable(struct irq_data *p)
691{
692 gpmc_irq_endis(p->irq, true);
693}
694
695static void gpmc_irq_noop(struct irq_data *data) { }
696
697static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
698
da496873 699static int gpmc_setup_irq(void)
6b6c32fc
AM
700{
701 int i;
702 u32 regval;
703
704 if (!gpmc_irq)
705 return -EINVAL;
706
707 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
71856843 708 if (gpmc_irq_start < 0) {
6b6c32fc
AM
709 pr_err("irq_alloc_descs failed\n");
710 return gpmc_irq_start;
711 }
712
713 gpmc_irq_chip.name = "gpmc";
714 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
715 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
716 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
717 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
718 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
719 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
720 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
721
722 gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
723 gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
724
725 for (i = 0; i < GPMC_NR_IRQ; i++) {
726 gpmc_client_irq[i].irq = gpmc_irq_start + i;
727 irq_set_chip_and_handler(gpmc_client_irq[i].irq,
728 &gpmc_irq_chip, handle_simple_irq);
729 set_irq_flags(gpmc_client_irq[i].irq,
730 IRQF_VALID | IRQF_NOAUTOEN);
731 }
732
733 /* Disable interrupts */
734 gpmc_write_reg(GPMC_IRQENABLE, 0);
735
736 /* clear interrupts */
737 regval = gpmc_read_reg(GPMC_IRQSTATUS);
738 gpmc_write_reg(GPMC_IRQSTATUS, regval);
739
740 return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
741}
742
351a102d 743static int gpmc_free_irq(void)
da496873
AM
744{
745 int i;
746
747 if (gpmc_irq)
748 free_irq(gpmc_irq, NULL);
749
750 for (i = 0; i < GPMC_NR_IRQ; i++) {
751 irq_set_handler(gpmc_client_irq[i].irq, NULL);
752 irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
753 irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
754 }
755
756 irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
757
758 return 0;
759}
760
351a102d 761static void gpmc_mem_exit(void)
da496873
AM
762{
763 int cs;
764
765 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
766 if (!gpmc_cs_mem_enabled(cs))
767 continue;
768 gpmc_cs_delete_mem(cs);
769 }
770
771}
772
351a102d 773static int gpmc_mem_init(void)
f37e4580 774{
8119024e 775 int cs, rc;
f37e4580
ID
776 unsigned long boot_rom_space = 0;
777
7f245162
KP
778 /* never allocate the first page, to facilitate bug detection;
779 * even if we didn't boot from ROM.
780 */
781 boot_rom_space = BOOT_ROM_SPACE;
f37e4580
ID
782 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
783 gpmc_mem_root.end = GPMC_MEM_END;
784
785 /* Reserve all regions that has been set up by bootloader */
786 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
787 u32 base, size;
788
789 if (!gpmc_cs_mem_enabled(cs))
790 continue;
791 gpmc_cs_get_memconf(cs, &base, &size);
8119024e 792 rc = gpmc_cs_insert_mem(cs, base, size);
71856843 793 if (rc < 0) {
8119024e
JH
794 while (--cs >= 0)
795 if (gpmc_cs_mem_enabled(cs))
796 gpmc_cs_delete_mem(cs);
797 return rc;
798 }
f37e4580 799 }
8119024e
JH
800
801 return 0;
4bbbc1ad
JY
802}
803
246da26d
AM
804static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
805{
806 u32 temp;
807 int div;
808
809 div = gpmc_calc_divider(sync_clk);
810 temp = gpmc_ps_to_ticks(time_ps);
811 temp = (temp + div - 1) / div;
812 return gpmc_ticks_to_ps(temp * div);
813}
814
815/* XXX: can the cycles be avoided ? */
816static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
817 struct gpmc_device_timings *dev_t)
818{
819 bool mux = dev_t->mux;
820 u32 temp;
821
822 /* adv_rd_off */
823 temp = dev_t->t_avdp_r;
824 /* XXX: mux check required ? */
825 if (mux) {
826 /* XXX: t_avdp not to be required for sync, only added for tusb
827 * this indirectly necessitates requirement of t_avdp_r and
828 * t_avdp_w instead of having a single t_avdp
829 */
830 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
831 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
832 }
833 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
834
835 /* oe_on */
836 temp = dev_t->t_oeasu; /* XXX: remove this ? */
837 if (mux) {
838 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
839 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
840 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
841 }
842 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
843
844 /* access */
845 /* XXX: any scope for improvement ?, by combining oe_on
846 * and clk_activation, need to check whether
847 * access = clk_activation + round to sync clk ?
848 */
849 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
850 temp += gpmc_t->clk_activation;
851 if (dev_t->cyc_oe)
852 temp = max_t(u32, temp, gpmc_t->oe_on +
853 gpmc_ticks_to_ps(dev_t->cyc_oe));
854 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
855
856 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
857 gpmc_t->cs_rd_off = gpmc_t->oe_off;
858
859 /* rd_cycle */
860 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
861 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
862 gpmc_t->access;
863 /* XXX: barter t_ce_rdyz with t_cez_r ? */
864 if (dev_t->t_ce_rdyz)
865 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
866 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
867
868 return 0;
869}
870
871static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
872 struct gpmc_device_timings *dev_t)
873{
874 bool mux = dev_t->mux;
875 u32 temp;
876
877 /* adv_wr_off */
878 temp = dev_t->t_avdp_w;
879 if (mux) {
880 temp = max_t(u32, temp,
881 gpmc_t->clk_activation + dev_t->t_avdh);
882 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
883 }
884 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
885
886 /* wr_data_mux_bus */
887 temp = max_t(u32, dev_t->t_weasu,
888 gpmc_t->clk_activation + dev_t->t_rdyo);
889 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
890 * and in that case remember to handle we_on properly
891 */
892 if (mux) {
893 temp = max_t(u32, temp,
894 gpmc_t->adv_wr_off + dev_t->t_aavdh);
895 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
896 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
897 }
898 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
899
900 /* we_on */
901 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
902 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
903 else
904 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
905
906 /* wr_access */
907 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
908 gpmc_t->wr_access = gpmc_t->access;
909
910 /* we_off */
911 temp = gpmc_t->we_on + dev_t->t_wpl;
912 temp = max_t(u32, temp,
913 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
914 temp = max_t(u32, temp,
915 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
916 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
917
918 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
919 dev_t->t_wph);
920
921 /* wr_cycle */
922 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
923 temp += gpmc_t->wr_access;
924 /* XXX: barter t_ce_rdyz with t_cez_w ? */
925 if (dev_t->t_ce_rdyz)
926 temp = max_t(u32, temp,
927 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
928 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
929
930 return 0;
931}
932
933static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
934 struct gpmc_device_timings *dev_t)
935{
936 bool mux = dev_t->mux;
937 u32 temp;
938
939 /* adv_rd_off */
940 temp = dev_t->t_avdp_r;
941 if (mux)
942 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
943 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
944
945 /* oe_on */
946 temp = dev_t->t_oeasu;
947 if (mux)
948 temp = max_t(u32, temp,
949 gpmc_t->adv_rd_off + dev_t->t_aavdh);
950 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
951
952 /* access */
953 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
954 gpmc_t->oe_on + dev_t->t_oe);
955 temp = max_t(u32, temp,
956 gpmc_t->cs_on + dev_t->t_ce);
957 temp = max_t(u32, temp,
958 gpmc_t->adv_on + dev_t->t_aa);
959 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
960
961 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
962 gpmc_t->cs_rd_off = gpmc_t->oe_off;
963
964 /* rd_cycle */
965 temp = max_t(u32, dev_t->t_rd_cycle,
966 gpmc_t->cs_rd_off + dev_t->t_cez_r);
967 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
968 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
969
970 return 0;
971}
972
973static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
974 struct gpmc_device_timings *dev_t)
975{
976 bool mux = dev_t->mux;
977 u32 temp;
978
979 /* adv_wr_off */
980 temp = dev_t->t_avdp_w;
981 if (mux)
982 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
983 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
984
985 /* wr_data_mux_bus */
986 temp = dev_t->t_weasu;
987 if (mux) {
988 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
989 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
990 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
991 }
992 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
993
994 /* we_on */
995 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
996 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
997 else
998 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
999
1000 /* we_off */
1001 temp = gpmc_t->we_on + dev_t->t_wpl;
1002 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1003
1004 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1005 dev_t->t_wph);
1006
1007 /* wr_cycle */
1008 temp = max_t(u32, dev_t->t_wr_cycle,
1009 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1010 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1011
1012 return 0;
1013}
1014
1015static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1016 struct gpmc_device_timings *dev_t)
1017{
1018 u32 temp;
1019
1020 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1021 gpmc_get_fclk_period();
1022
1023 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1024 dev_t->t_bacc,
1025 gpmc_t->sync_clk);
1026
1027 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1028 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1029
1030 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1031 return 0;
1032
1033 if (dev_t->ce_xdelay)
1034 gpmc_t->bool_timings.cs_extra_delay = true;
1035 if (dev_t->avd_xdelay)
1036 gpmc_t->bool_timings.adv_extra_delay = true;
1037 if (dev_t->oe_xdelay)
1038 gpmc_t->bool_timings.oe_extra_delay = true;
1039 if (dev_t->we_xdelay)
1040 gpmc_t->bool_timings.we_extra_delay = true;
1041
1042 return 0;
1043}
1044
1045static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
1046 struct gpmc_device_timings *dev_t)
1047{
1048 u32 temp;
1049
1050 /* cs_on */
1051 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1052
1053 /* adv_on */
1054 temp = dev_t->t_avdasu;
1055 if (dev_t->t_ce_avd)
1056 temp = max_t(u32, temp,
1057 gpmc_t->cs_on + dev_t->t_ce_avd);
1058 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1059
1060 if (dev_t->sync_write || dev_t->sync_read)
1061 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1062
1063 return 0;
1064}
1065
1066/* TODO: remove this function once all peripherals are confirmed to
1067 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1068 * has to be modified to handle timings in ps instead of ns
1069*/
1070static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1071{
1072 t->cs_on /= 1000;
1073 t->cs_rd_off /= 1000;
1074 t->cs_wr_off /= 1000;
1075 t->adv_on /= 1000;
1076 t->adv_rd_off /= 1000;
1077 t->adv_wr_off /= 1000;
1078 t->we_on /= 1000;
1079 t->we_off /= 1000;
1080 t->oe_on /= 1000;
1081 t->oe_off /= 1000;
1082 t->page_burst_access /= 1000;
1083 t->access /= 1000;
1084 t->rd_cycle /= 1000;
1085 t->wr_cycle /= 1000;
1086 t->bus_turnaround /= 1000;
1087 t->cycle2cycle_delay /= 1000;
1088 t->wait_monitoring /= 1000;
1089 t->clk_activation /= 1000;
1090 t->wr_access /= 1000;
1091 t->wr_data_mux_bus /= 1000;
1092}
1093
1094int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
1095 struct gpmc_device_timings *dev_t)
1096{
1097 memset(gpmc_t, 0, sizeof(*gpmc_t));
1098
1099 gpmc_calc_common_timings(gpmc_t, dev_t);
1100
1101 if (dev_t->sync_read)
1102 gpmc_calc_sync_read_timings(gpmc_t, dev_t);
1103 else
1104 gpmc_calc_async_read_timings(gpmc_t, dev_t);
1105
1106 if (dev_t->sync_write)
1107 gpmc_calc_sync_write_timings(gpmc_t, dev_t);
1108 else
1109 gpmc_calc_async_write_timings(gpmc_t, dev_t);
1110
1111 /* TODO: remove, see function definition */
1112 gpmc_convert_ps_to_ns(gpmc_t);
1113
1114 return 0;
1115}
1116
bc6b1e7b
DM
1117#ifdef CONFIG_OF
1118static struct of_device_id gpmc_dt_ids[] = {
1119 { .compatible = "ti,omap2420-gpmc" },
1120 { .compatible = "ti,omap2430-gpmc" },
1121 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1122 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1123 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1124 { }
1125};
1126MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1127
1128static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1129 struct gpmc_timings *gpmc_t)
1130{
1131 u32 val;
1132
1133 memset(gpmc_t, 0, sizeof(*gpmc_t));
1134
1135 /* minimum clock period for syncronous mode */
1136 if (!of_property_read_u32(np, "gpmc,sync-clk", &val))
1137 gpmc_t->sync_clk = val;
1138
1139 /* chip select timtings */
1140 if (!of_property_read_u32(np, "gpmc,cs-on", &val))
1141 gpmc_t->cs_on = val;
1142
1143 if (!of_property_read_u32(np, "gpmc,cs-rd-off", &val))
1144 gpmc_t->cs_rd_off = val;
1145
1146 if (!of_property_read_u32(np, "gpmc,cs-wr-off", &val))
1147 gpmc_t->cs_wr_off = val;
1148
1149 /* ADV signal timings */
1150 if (!of_property_read_u32(np, "gpmc,adv-on", &val))
1151 gpmc_t->adv_on = val;
1152
1153 if (!of_property_read_u32(np, "gpmc,adv-rd-off", &val))
1154 gpmc_t->adv_rd_off = val;
1155
1156 if (!of_property_read_u32(np, "gpmc,adv-wr-off", &val))
1157 gpmc_t->adv_wr_off = val;
1158
1159 /* WE signal timings */
1160 if (!of_property_read_u32(np, "gpmc,we-on", &val))
1161 gpmc_t->we_on = val;
1162
1163 if (!of_property_read_u32(np, "gpmc,we-off", &val))
1164 gpmc_t->we_off = val;
1165
1166 /* OE signal timings */
1167 if (!of_property_read_u32(np, "gpmc,oe-on", &val))
1168 gpmc_t->oe_on = val;
1169
1170 if (!of_property_read_u32(np, "gpmc,oe-off", &val))
1171 gpmc_t->oe_off = val;
1172
1173 /* access and cycle timings */
1174 if (!of_property_read_u32(np, "gpmc,page-burst-access", &val))
1175 gpmc_t->page_burst_access = val;
1176
1177 if (!of_property_read_u32(np, "gpmc,access", &val))
1178 gpmc_t->access = val;
1179
1180 if (!of_property_read_u32(np, "gpmc,rd-cycle", &val))
1181 gpmc_t->rd_cycle = val;
1182
1183 if (!of_property_read_u32(np, "gpmc,wr-cycle", &val))
1184 gpmc_t->wr_cycle = val;
1185
1186 /* only for OMAP3430 */
1187 if (!of_property_read_u32(np, "gpmc,wr-access", &val))
1188 gpmc_t->wr_access = val;
1189
1190 if (!of_property_read_u32(np, "gpmc,wr-data-mux-bus", &val))
1191 gpmc_t->wr_data_mux_bus = val;
1192}
1193
1194#ifdef CONFIG_MTD_NAND
1195
1196static const char * const nand_ecc_opts[] = {
1197 [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
1198 [OMAP_ECC_HAMMING_CODE_HW] = "hw",
1199 [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
1200 [OMAP_ECC_BCH4_CODE_HW] = "bch4",
1201 [OMAP_ECC_BCH8_CODE_HW] = "bch8",
1202};
1203
1204static int gpmc_probe_nand_child(struct platform_device *pdev,
1205 struct device_node *child)
1206{
1207 u32 val;
1208 const char *s;
1209 struct gpmc_timings gpmc_t;
1210 struct omap_nand_platform_data *gpmc_nand_data;
1211
1212 if (of_property_read_u32(child, "reg", &val) < 0) {
1213 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1214 child->full_name);
1215 return -ENODEV;
1216 }
1217
1218 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1219 GFP_KERNEL);
1220 if (!gpmc_nand_data)
1221 return -ENOMEM;
1222
1223 gpmc_nand_data->cs = val;
1224 gpmc_nand_data->of_node = child;
1225
1226 if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
1227 for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
1228 if (!strcasecmp(s, nand_ecc_opts[val])) {
1229 gpmc_nand_data->ecc_opt = val;
1230 break;
1231 }
1232
1233 val = of_get_nand_bus_width(child);
1234 if (val == 16)
1235 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1236
1237 gpmc_read_timings_dt(child, &gpmc_t);
1238 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1239
1240 return 0;
1241}
1242#else
1243static int gpmc_probe_nand_child(struct platform_device *pdev,
1244 struct device_node *child)
1245{
1246 return 0;
1247}
1248#endif
1249
75d3625e
EG
1250#ifdef CONFIG_MTD_ONENAND
1251static int gpmc_probe_onenand_child(struct platform_device *pdev,
1252 struct device_node *child)
1253{
1254 u32 val;
1255 struct omap_onenand_platform_data *gpmc_onenand_data;
1256
1257 if (of_property_read_u32(child, "reg", &val) < 0) {
1258 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1259 child->full_name);
1260 return -ENODEV;
1261 }
1262
1263 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1264 GFP_KERNEL);
1265 if (!gpmc_onenand_data)
1266 return -ENOMEM;
1267
1268 gpmc_onenand_data->cs = val;
1269 gpmc_onenand_data->of_node = child;
1270 gpmc_onenand_data->dma_channel = -1;
1271
1272 if (!of_property_read_u32(child, "dma-channel", &val))
1273 gpmc_onenand_data->dma_channel = val;
1274
1275 gpmc_onenand_init(gpmc_onenand_data);
1276
1277 return 0;
1278}
1279#else
1280static int gpmc_probe_onenand_child(struct platform_device *pdev,
1281 struct device_node *child)
1282{
1283 return 0;
1284}
1285#endif
1286
bc6b1e7b
DM
1287static int gpmc_probe_dt(struct platform_device *pdev)
1288{
1289 int ret;
1290 struct device_node *child;
1291 const struct of_device_id *of_id =
1292 of_match_device(gpmc_dt_ids, &pdev->dev);
1293
1294 if (!of_id)
1295 return 0;
1296
1297 for_each_node_by_name(child, "nand") {
1298 ret = gpmc_probe_nand_child(pdev, child);
a1672370
EG
1299 if (ret < 0) {
1300 of_node_put(child);
bc6b1e7b 1301 return ret;
a1672370 1302 }
bc6b1e7b
DM
1303 }
1304
75d3625e
EG
1305 for_each_node_by_name(child, "onenand") {
1306 ret = gpmc_probe_onenand_child(pdev, child);
1307 if (ret < 0) {
1308 of_node_put(child);
1309 return ret;
1310 }
1311 }
bc6b1e7b
DM
1312 return 0;
1313}
1314#else
1315static int gpmc_probe_dt(struct platform_device *pdev)
1316{
1317 return 0;
1318}
1319#endif
1320
351a102d 1321static int gpmc_probe(struct platform_device *pdev)
4bbbc1ad 1322{
8119024e 1323 int rc;
6b6c32fc 1324 u32 l;
da496873 1325 struct resource *res;
4bbbc1ad 1326
da496873
AM
1327 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1328 if (res == NULL)
1329 return -ENOENT;
8d08436d 1330
da496873
AM
1331 phys_base = res->start;
1332 mem_size = resource_size(res);
fd1dc87d 1333
5857bd98
TR
1334 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
1335 if (IS_ERR(gpmc_base))
1336 return PTR_ERR(gpmc_base);
da496873
AM
1337
1338 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1339 if (res == NULL)
1340 dev_warn(&pdev->dev, "Failed to get resource: irq\n");
1341 else
1342 gpmc_irq = res->start;
1343
1344 gpmc_l3_clk = clk_get(&pdev->dev, "fck");
1345 if (IS_ERR(gpmc_l3_clk)) {
1346 dev_err(&pdev->dev, "error: clk_get\n");
1347 gpmc_irq = 0;
1348 return PTR_ERR(gpmc_l3_clk);
fd1dc87d
PW
1349 }
1350
4d7cb45e 1351 clk_prepare_enable(gpmc_l3_clk);
1daa8c1d 1352
da496873
AM
1353 gpmc_dev = &pdev->dev;
1354
4bbbc1ad 1355 l = gpmc_read_reg(GPMC_REVISION);
da496873
AM
1356 if (GPMC_REVISION_MAJOR(l) > 0x4)
1357 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
1358 dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
1359 GPMC_REVISION_MINOR(l));
1360
8119024e 1361 rc = gpmc_mem_init();
71856843 1362 if (rc < 0) {
8119024e
JH
1363 clk_disable_unprepare(gpmc_l3_clk);
1364 clk_put(gpmc_l3_clk);
1365 dev_err(gpmc_dev, "failed to reserve memory\n");
1366 return rc;
1367 }
db97eb7d 1368
71856843 1369 if (gpmc_setup_irq() < 0)
da496873
AM
1370 dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
1371
31d9adca
JH
1372 /* Now the GPMC is initialised, unreserve the chip-selects */
1373 gpmc_cs_map = 0;
1374
bc6b1e7b
DM
1375 rc = gpmc_probe_dt(pdev);
1376 if (rc < 0) {
1377 clk_disable_unprepare(gpmc_l3_clk);
1378 clk_put(gpmc_l3_clk);
1379 dev_err(gpmc_dev, "failed to probe DT parameters\n");
1380 return rc;
1381 }
1382
da496873
AM
1383 return 0;
1384}
1385
351a102d 1386static int gpmc_remove(struct platform_device *pdev)
da496873
AM
1387{
1388 gpmc_free_irq();
1389 gpmc_mem_exit();
1390 gpmc_dev = NULL;
1391 return 0;
1392}
1393
1394static struct platform_driver gpmc_driver = {
1395 .probe = gpmc_probe,
351a102d 1396 .remove = gpmc_remove,
da496873
AM
1397 .driver = {
1398 .name = DEVICE_NAME,
1399 .owner = THIS_MODULE,
bc6b1e7b 1400 .of_match_table = of_match_ptr(gpmc_dt_ids),
da496873
AM
1401 },
1402};
1403
1404static __init int gpmc_init(void)
1405{
1406 return platform_driver_register(&gpmc_driver);
1407}
1408
1409static __exit void gpmc_exit(void)
1410{
1411 platform_driver_unregister(&gpmc_driver);
1412
db97eb7d 1413}
da496873 1414
b76c8b19 1415omap_postcore_initcall(gpmc_init);
da496873 1416module_exit(gpmc_exit);
db97eb7d 1417
4be48fd5
AM
1418static int __init omap_gpmc_init(void)
1419{
1420 struct omap_hwmod *oh;
1421 struct platform_device *pdev;
1422 char *oh_name = "gpmc";
1423
2f98ca89
DM
1424 /*
1425 * if the board boots up with a populated DT, do not
1426 * manually add the device from this initcall
1427 */
1428 if (of_have_populated_dt())
1429 return -ENODEV;
1430
4be48fd5
AM
1431 oh = omap_hwmod_lookup(oh_name);
1432 if (!oh) {
1433 pr_err("Could not look up %s\n", oh_name);
1434 return -ENODEV;
1435 }
1436
c1d1cd59 1437 pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
4be48fd5
AM
1438 WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
1439
1440 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
1441}
b76c8b19 1442omap_postcore_initcall(omap_gpmc_init);
4be48fd5 1443
db97eb7d
SG
1444static irqreturn_t gpmc_handle_irq(int irq, void *dev)
1445{
6b6c32fc
AM
1446 int i;
1447 u32 regval;
1448
1449 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1450
1451 if (!regval)
1452 return IRQ_NONE;
1453
1454 for (i = 0; i < GPMC_NR_IRQ; i++)
1455 if (regval & gpmc_client_irq[i].bitmask)
1456 generic_handle_irq(gpmc_client_irq[i].irq);
db97eb7d 1457
6b6c32fc 1458 gpmc_write_reg(GPMC_IRQSTATUS, regval);
db97eb7d
SG
1459
1460 return IRQ_HANDLED;
4bbbc1ad 1461}
a2d3e7ba
RN
1462
1463#ifdef CONFIG_ARCH_OMAP3
1464static struct omap3_gpmc_regs gpmc_context;
1465
b2fa3b7c 1466void omap3_gpmc_save_context(void)
a2d3e7ba
RN
1467{
1468 int i;
b2fa3b7c 1469
a2d3e7ba
RN
1470 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
1471 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
1472 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
1473 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
1474 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
1475 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
1476 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
1477 for (i = 0; i < GPMC_CS_NUM; i++) {
1478 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
1479 if (gpmc_context.cs_context[i].is_valid) {
1480 gpmc_context.cs_context[i].config1 =
1481 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
1482 gpmc_context.cs_context[i].config2 =
1483 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
1484 gpmc_context.cs_context[i].config3 =
1485 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
1486 gpmc_context.cs_context[i].config4 =
1487 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
1488 gpmc_context.cs_context[i].config5 =
1489 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
1490 gpmc_context.cs_context[i].config6 =
1491 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
1492 gpmc_context.cs_context[i].config7 =
1493 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
1494 }
1495 }
1496}
1497
b2fa3b7c 1498void omap3_gpmc_restore_context(void)
a2d3e7ba
RN
1499{
1500 int i;
b2fa3b7c 1501
a2d3e7ba
RN
1502 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
1503 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
1504 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
1505 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
1506 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
1507 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
1508 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
1509 for (i = 0; i < GPMC_CS_NUM; i++) {
1510 if (gpmc_context.cs_context[i].is_valid) {
1511 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
1512 gpmc_context.cs_context[i].config1);
1513 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
1514 gpmc_context.cs_context[i].config2);
1515 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
1516 gpmc_context.cs_context[i].config3);
1517 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
1518 gpmc_context.cs_context[i].config4);
1519 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
1520 gpmc_context.cs_context[i].config5);
1521 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
1522 gpmc_context.cs_context[i].config6);
1523 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
1524 gpmc_context.cs_context[i].config7);
1525 }
1526 }
1527}
1528#endif /* CONFIG_ARCH_OMAP3 */
This page took 0.502474 seconds and 5 git commands to generate.