2 * GPMC support functions
4 * Copyright (C) 2005-2006 Nokia Corporation
8 * Copyright (C) 2009 Texas Instruments
9 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
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.
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/err.h>
21 #include <linux/clk.h>
22 #include <linux/ioport.h>
23 #include <linux/spinlock.h>
25 #include <linux/module.h>
26 #include <linux/interrupt.h>
27 #include <linux/platform_device.h>
29 #include <linux/of_address.h>
30 #include <linux/of_mtd.h>
31 #include <linux/of_device.h>
32 #include <linux/mtd/nand.h>
33 #include <linux/pm_runtime.h>
35 #include <linux/platform_data/mtd-nand-omap2.h>
37 #include <asm/mach-types.h>
41 #include "omap_device.h"
43 #include "gpmc-nand.h"
44 #include "gpmc-onenand.h"
46 #define DEVICE_NAME "omap-gpmc"
48 /* GPMC register offsets */
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
61 #define GPMC_PREFETCH_CONTROL 0x1ec
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
66 #define GPMC_ECC1_RESULT 0x200
67 #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
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 */
71 #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
72 #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
73 #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
75 /* GPMC ECC control settings */
76 #define GPMC_ECC_CTRL_ECCCLEAR 0x100
77 #define GPMC_ECC_CTRL_ECCDISABLE 0x000
78 #define GPMC_ECC_CTRL_ECCREG1 0x001
79 #define GPMC_ECC_CTRL_ECCREG2 0x002
80 #define GPMC_ECC_CTRL_ECCREG3 0x003
81 #define GPMC_ECC_CTRL_ECCREG4 0x004
82 #define GPMC_ECC_CTRL_ECCREG5 0x005
83 #define GPMC_ECC_CTRL_ECCREG6 0x006
84 #define GPMC_ECC_CTRL_ECCREG7 0x007
85 #define GPMC_ECC_CTRL_ECCREG8 0x008
86 #define GPMC_ECC_CTRL_ECCREG9 0x009
88 #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
89 #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
90 #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
91 #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
92 #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
93 #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
95 #define GPMC_CS0_OFFSET 0x60
96 #define GPMC_CS_SIZE 0x30
97 #define GPMC_BCH_SIZE 0x10
99 #define GPMC_MEM_END 0x3FFFFFFF
101 #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
102 #define GPMC_SECTION_SHIFT 28 /* 128 MB */
104 #define CS_NUM_SHIFT 24
105 #define ENABLE_PREFETCH (0x1 << 7)
106 #define DMA_MPU_MODE 2
108 #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
109 #define GPMC_REVISION_MINOR(l) (l & 0xf)
111 #define GPMC_HAS_WR_ACCESS 0x1
112 #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
113 #define GPMC_HAS_MUX_AAD 0x4
115 #define GPMC_NR_WAITPINS 4
117 /* XXX: Only NAND irq has been considered,currently these are the only ones used
119 #define GPMC_NR_IRQ 2
121 struct gpmc_client_irq
{
126 /* Structure to save gpmc cs context */
127 struct gpmc_cs_config
{
139 * Structure to save/restore gpmc context
140 * to support core off on OMAP3
142 struct omap3_gpmc_regs
{
147 u32 prefetch_config1
;
148 u32 prefetch_config2
;
149 u32 prefetch_control
;
150 struct gpmc_cs_config cs_context
[GPMC_CS_NUM
];
153 static struct gpmc_client_irq gpmc_client_irq
[GPMC_NR_IRQ
];
154 static struct irq_chip gpmc_irq_chip
;
155 static int gpmc_irq_start
;
157 static struct resource gpmc_mem_root
;
158 static struct resource gpmc_cs_mem
[GPMC_CS_NUM
];
159 static DEFINE_SPINLOCK(gpmc_mem_lock
);
160 /* Define chip-selects as reserved by default until probe completes */
161 static unsigned int gpmc_cs_map
= ((1 << GPMC_CS_NUM
) - 1);
162 static unsigned int gpmc_cs_num
= GPMC_CS_NUM
;
163 static unsigned int gpmc_nr_waitpins
;
164 static struct device
*gpmc_dev
;
166 static resource_size_t phys_base
, mem_size
;
167 static unsigned gpmc_capability
;
168 static void __iomem
*gpmc_base
;
170 static struct clk
*gpmc_l3_clk
;
172 static irqreturn_t
gpmc_handle_irq(int irq
, void *dev
);
174 static void gpmc_write_reg(int idx
, u32 val
)
176 writel_relaxed(val
, gpmc_base
+ idx
);
179 static u32
gpmc_read_reg(int idx
)
181 return readl_relaxed(gpmc_base
+ idx
);
184 void gpmc_cs_write_reg(int cs
, int idx
, u32 val
)
186 void __iomem
*reg_addr
;
188 reg_addr
= gpmc_base
+ GPMC_CS0_OFFSET
+ (cs
* GPMC_CS_SIZE
) + idx
;
189 writel_relaxed(val
, reg_addr
);
192 static u32
gpmc_cs_read_reg(int cs
, int idx
)
194 void __iomem
*reg_addr
;
196 reg_addr
= gpmc_base
+ GPMC_CS0_OFFSET
+ (cs
* GPMC_CS_SIZE
) + idx
;
197 return readl_relaxed(reg_addr
);
200 /* TODO: Add support for gpmc_fck to clock framework and use it */
201 static unsigned long gpmc_get_fclk_period(void)
203 unsigned long rate
= clk_get_rate(gpmc_l3_clk
);
206 printk(KERN_WARNING
"gpmc_l3_clk not enabled\n");
211 rate
= 1000000000 / rate
; /* In picoseconds */
216 static unsigned int gpmc_ns_to_ticks(unsigned int time_ns
)
218 unsigned long tick_ps
;
220 /* Calculate in picosecs to yield more exact results */
221 tick_ps
= gpmc_get_fclk_period();
223 return (time_ns
* 1000 + tick_ps
- 1) / tick_ps
;
226 static unsigned int gpmc_ps_to_ticks(unsigned int time_ps
)
228 unsigned long tick_ps
;
230 /* Calculate in picosecs to yield more exact results */
231 tick_ps
= gpmc_get_fclk_period();
233 return (time_ps
+ tick_ps
- 1) / tick_ps
;
236 unsigned int gpmc_ticks_to_ns(unsigned int ticks
)
238 return ticks
* gpmc_get_fclk_period() / 1000;
241 static unsigned int gpmc_ticks_to_ps(unsigned int ticks
)
243 return ticks
* gpmc_get_fclk_period();
246 static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps
)
248 unsigned long ticks
= gpmc_ps_to_ticks(time_ps
);
250 return ticks
* gpmc_get_fclk_period();
253 static inline void gpmc_cs_modify_reg(int cs
, int reg
, u32 mask
, bool value
)
257 l
= gpmc_cs_read_reg(cs
, reg
);
262 gpmc_cs_write_reg(cs
, reg
, l
);
265 static void gpmc_cs_bool_timings(int cs
, const struct gpmc_bool_timings
*p
)
267 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG1
,
268 GPMC_CONFIG1_TIME_PARA_GRAN
,
269 p
->time_para_granularity
);
270 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG2
,
271 GPMC_CONFIG2_CSEXTRADELAY
, p
->cs_extra_delay
);
272 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG3
,
273 GPMC_CONFIG3_ADVEXTRADELAY
, p
->adv_extra_delay
);
274 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG4
,
275 GPMC_CONFIG4_OEEXTRADELAY
, p
->oe_extra_delay
);
276 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG4
,
277 GPMC_CONFIG4_OEEXTRADELAY
, p
->we_extra_delay
);
278 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG6
,
279 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN
,
280 p
->cycle2cyclesamecsen
);
281 gpmc_cs_modify_reg(cs
, GPMC_CS_CONFIG6
,
282 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN
,
283 p
->cycle2cyclediffcsen
);
287 static int set_gpmc_timing_reg(int cs
, int reg
, int st_bit
, int end_bit
,
288 int time
, const char *name
)
290 static int set_gpmc_timing_reg(int cs
, int reg
, int st_bit
, int end_bit
,
295 int ticks
, mask
, nr_bits
;
300 ticks
= gpmc_ns_to_ticks(time
);
301 nr_bits
= end_bit
- st_bit
+ 1;
302 if (ticks
>= 1 << nr_bits
) {
304 printk(KERN_INFO
"GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
305 cs
, name
, time
, ticks
, 1 << nr_bits
);
310 mask
= (1 << nr_bits
) - 1;
311 l
= gpmc_cs_read_reg(cs
, reg
);
314 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
315 cs
, name
, ticks
, gpmc_get_fclk_period() * ticks
/ 1000,
316 (l
>> st_bit
) & mask
, time
);
318 l
&= ~(mask
<< st_bit
);
319 l
|= ticks
<< st_bit
;
320 gpmc_cs_write_reg(cs
, reg
, l
);
326 #define GPMC_SET_ONE(reg, st, end, field) \
327 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
328 t->field, #field) < 0) \
331 #define GPMC_SET_ONE(reg, st, end, field) \
332 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
336 int gpmc_calc_divider(unsigned int sync_clk
)
341 l
= sync_clk
+ (gpmc_get_fclk_period() - 1);
342 div
= l
/ gpmc_get_fclk_period();
351 int gpmc_cs_set_timings(int cs
, const struct gpmc_timings
*t
)
356 div
= gpmc_calc_divider(t
->sync_clk
);
360 GPMC_SET_ONE(GPMC_CS_CONFIG2
, 0, 3, cs_on
);
361 GPMC_SET_ONE(GPMC_CS_CONFIG2
, 8, 12, cs_rd_off
);
362 GPMC_SET_ONE(GPMC_CS_CONFIG2
, 16, 20, cs_wr_off
);
364 GPMC_SET_ONE(GPMC_CS_CONFIG3
, 0, 3, adv_on
);
365 GPMC_SET_ONE(GPMC_CS_CONFIG3
, 8, 12, adv_rd_off
);
366 GPMC_SET_ONE(GPMC_CS_CONFIG3
, 16, 20, adv_wr_off
);
368 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 0, 3, oe_on
);
369 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 8, 12, oe_off
);
370 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 16, 19, we_on
);
371 GPMC_SET_ONE(GPMC_CS_CONFIG4
, 24, 28, we_off
);
373 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 0, 4, rd_cycle
);
374 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 8, 12, wr_cycle
);
375 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 16, 20, access
);
377 GPMC_SET_ONE(GPMC_CS_CONFIG5
, 24, 27, page_burst_access
);
379 GPMC_SET_ONE(GPMC_CS_CONFIG6
, 0, 3, bus_turnaround
);
380 GPMC_SET_ONE(GPMC_CS_CONFIG6
, 8, 11, cycle2cycle_delay
);
382 GPMC_SET_ONE(GPMC_CS_CONFIG1
, 18, 19, wait_monitoring
);
383 GPMC_SET_ONE(GPMC_CS_CONFIG1
, 25, 26, clk_activation
);
385 if (gpmc_capability
& GPMC_HAS_WR_DATA_MUX_BUS
)
386 GPMC_SET_ONE(GPMC_CS_CONFIG6
, 16, 19, wr_data_mux_bus
);
387 if (gpmc_capability
& GPMC_HAS_WR_ACCESS
)
388 GPMC_SET_ONE(GPMC_CS_CONFIG6
, 24, 28, wr_access
);
390 /* caller is expected to have initialized CONFIG1 to cover
391 * at least sync vs async
393 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG1
);
394 if (l
& (GPMC_CONFIG1_READTYPE_SYNC
| GPMC_CONFIG1_WRITETYPE_SYNC
)) {
396 printk(KERN_INFO
"GPMC CS%d CLK period is %lu ns (div %d)\n",
397 cs
, (div
* gpmc_get_fclk_period()) / 1000, div
);
401 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG1
, l
);
404 gpmc_cs_bool_timings(cs
, &t
->bool_timings
);
409 static int gpmc_cs_enable_mem(int cs
, u32 base
, u32 size
)
415 * Ensure that base address is aligned on a
416 * boundary equal to or greater than size.
418 if (base
& (size
- 1))
421 mask
= (1 << GPMC_SECTION_SHIFT
) - size
;
422 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
424 l
= (base
>> GPMC_CHUNK_SHIFT
) & 0x3f;
426 l
|= ((mask
>> GPMC_CHUNK_SHIFT
) & 0x0f) << 8;
427 l
|= GPMC_CONFIG7_CSVALID
;
428 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG7
, l
);
433 static void gpmc_cs_disable_mem(int cs
)
437 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
438 l
&= ~GPMC_CONFIG7_CSVALID
;
439 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG7
, l
);
442 static void gpmc_cs_get_memconf(int cs
, u32
*base
, u32
*size
)
447 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
448 *base
= (l
& 0x3f) << GPMC_CHUNK_SHIFT
;
449 mask
= (l
>> 8) & 0x0f;
450 *size
= (1 << GPMC_SECTION_SHIFT
) - (mask
<< GPMC_CHUNK_SHIFT
);
453 static int gpmc_cs_mem_enabled(int cs
)
457 l
= gpmc_cs_read_reg(cs
, GPMC_CS_CONFIG7
);
458 return l
& GPMC_CONFIG7_CSVALID
;
461 static void gpmc_cs_set_reserved(int cs
, int reserved
)
463 gpmc_cs_map
&= ~(1 << cs
);
464 gpmc_cs_map
|= (reserved
? 1 : 0) << cs
;
467 static bool gpmc_cs_reserved(int cs
)
469 return gpmc_cs_map
& (1 << cs
);
472 static unsigned long gpmc_mem_align(unsigned long size
)
476 size
= (size
- 1) >> (GPMC_CHUNK_SHIFT
- 1);
477 order
= GPMC_CHUNK_SHIFT
- 1;
486 static int gpmc_cs_insert_mem(int cs
, unsigned long base
, unsigned long size
)
488 struct resource
*res
= &gpmc_cs_mem
[cs
];
491 size
= gpmc_mem_align(size
);
492 spin_lock(&gpmc_mem_lock
);
494 res
->end
= base
+ size
- 1;
495 r
= request_resource(&gpmc_mem_root
, res
);
496 spin_unlock(&gpmc_mem_lock
);
501 static int gpmc_cs_delete_mem(int cs
)
503 struct resource
*res
= &gpmc_cs_mem
[cs
];
506 spin_lock(&gpmc_mem_lock
);
507 r
= release_resource(res
);
510 spin_unlock(&gpmc_mem_lock
);
516 * gpmc_cs_remap - remaps a chip-select physical base address
517 * @cs: chip-select to remap
518 * @base: physical base address to re-map chip-select to
520 * Re-maps a chip-select to a new physical base address specified by
521 * "base". Returns 0 on success and appropriate negative error code
524 static int gpmc_cs_remap(int cs
, u32 base
)
529 if (cs
> gpmc_cs_num
) {
530 pr_err("%s: requested chip-select is disabled\n", __func__
);
535 * Make sure we ignore any device offsets from the GPMC partition
536 * allocated for the chip select and that the new base confirms
537 * to the GPMC 16MB minimum granularity.
539 base
&= ~(SZ_16M
- 1);
541 gpmc_cs_get_memconf(cs
, &old_base
, &size
);
542 if (base
== old_base
)
544 gpmc_cs_disable_mem(cs
);
545 ret
= gpmc_cs_delete_mem(cs
);
548 ret
= gpmc_cs_insert_mem(cs
, base
, size
);
551 ret
= gpmc_cs_enable_mem(cs
, base
, size
);
558 int gpmc_cs_request(int cs
, unsigned long size
, unsigned long *base
)
560 struct resource
*res
= &gpmc_cs_mem
[cs
];
563 if (cs
> gpmc_cs_num
) {
564 pr_err("%s: requested chip-select is disabled\n", __func__
);
567 size
= gpmc_mem_align(size
);
568 if (size
> (1 << GPMC_SECTION_SHIFT
))
571 spin_lock(&gpmc_mem_lock
);
572 if (gpmc_cs_reserved(cs
)) {
576 if (gpmc_cs_mem_enabled(cs
))
577 r
= adjust_resource(res
, res
->start
& ~(size
- 1), size
);
579 r
= allocate_resource(&gpmc_mem_root
, res
, size
, 0, ~0,
584 r
= gpmc_cs_enable_mem(cs
, res
->start
, resource_size(res
));
586 release_resource(res
);
591 gpmc_cs_set_reserved(cs
, 1);
593 spin_unlock(&gpmc_mem_lock
);
596 EXPORT_SYMBOL(gpmc_cs_request
);
598 void gpmc_cs_free(int cs
)
600 struct resource
*res
= &gpmc_cs_mem
[cs
];
602 spin_lock(&gpmc_mem_lock
);
603 if (cs
>= gpmc_cs_num
|| cs
< 0 || !gpmc_cs_reserved(cs
)) {
604 printk(KERN_ERR
"Trying to free non-reserved GPMC CS%d\n", cs
);
606 spin_unlock(&gpmc_mem_lock
);
609 gpmc_cs_disable_mem(cs
);
611 release_resource(res
);
612 gpmc_cs_set_reserved(cs
, 0);
613 spin_unlock(&gpmc_mem_lock
);
615 EXPORT_SYMBOL(gpmc_cs_free
);
618 * gpmc_configure - write request to configure gpmc
620 * @wval: value to write
621 * @return status of the operation
623 int gpmc_configure(int cmd
, int wval
)
628 case GPMC_ENABLE_IRQ
:
629 gpmc_write_reg(GPMC_IRQENABLE
, wval
);
632 case GPMC_SET_IRQ_STATUS
:
633 gpmc_write_reg(GPMC_IRQSTATUS
, wval
);
637 regval
= gpmc_read_reg(GPMC_CONFIG
);
639 regval
&= ~GPMC_CONFIG_WRITEPROTECT
; /* WP is ON */
641 regval
|= GPMC_CONFIG_WRITEPROTECT
; /* WP is OFF */
642 gpmc_write_reg(GPMC_CONFIG
, regval
);
646 pr_err("%s: command not supported\n", __func__
);
652 EXPORT_SYMBOL(gpmc_configure
);
654 void gpmc_update_nand_reg(struct gpmc_nand_regs
*reg
, int cs
)
658 reg
->gpmc_status
= gpmc_base
+ GPMC_STATUS
;
659 reg
->gpmc_nand_command
= gpmc_base
+ GPMC_CS0_OFFSET
+
660 GPMC_CS_NAND_COMMAND
+ GPMC_CS_SIZE
* cs
;
661 reg
->gpmc_nand_address
= gpmc_base
+ GPMC_CS0_OFFSET
+
662 GPMC_CS_NAND_ADDRESS
+ GPMC_CS_SIZE
* cs
;
663 reg
->gpmc_nand_data
= gpmc_base
+ GPMC_CS0_OFFSET
+
664 GPMC_CS_NAND_DATA
+ GPMC_CS_SIZE
* cs
;
665 reg
->gpmc_prefetch_config1
= gpmc_base
+ GPMC_PREFETCH_CONFIG1
;
666 reg
->gpmc_prefetch_config2
= gpmc_base
+ GPMC_PREFETCH_CONFIG2
;
667 reg
->gpmc_prefetch_control
= gpmc_base
+ GPMC_PREFETCH_CONTROL
;
668 reg
->gpmc_prefetch_status
= gpmc_base
+ GPMC_PREFETCH_STATUS
;
669 reg
->gpmc_ecc_config
= gpmc_base
+ GPMC_ECC_CONFIG
;
670 reg
->gpmc_ecc_control
= gpmc_base
+ GPMC_ECC_CONTROL
;
671 reg
->gpmc_ecc_size_config
= gpmc_base
+ GPMC_ECC_SIZE_CONFIG
;
672 reg
->gpmc_ecc1_result
= gpmc_base
+ GPMC_ECC1_RESULT
;
674 for (i
= 0; i
< GPMC_BCH_NUM_REMAINDER
; i
++) {
675 reg
->gpmc_bch_result0
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_0
+
677 reg
->gpmc_bch_result1
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_1
+
679 reg
->gpmc_bch_result2
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_2
+
681 reg
->gpmc_bch_result3
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_3
+
683 reg
->gpmc_bch_result4
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_4
+
685 reg
->gpmc_bch_result5
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_5
+
687 reg
->gpmc_bch_result6
[i
] = gpmc_base
+ GPMC_ECC_BCH_RESULT_6
+
692 int gpmc_get_client_irq(unsigned irq_config
)
696 if (hweight32(irq_config
) > 1)
699 for (i
= 0; i
< GPMC_NR_IRQ
; i
++)
700 if (gpmc_client_irq
[i
].bitmask
& irq_config
)
701 return gpmc_client_irq
[i
].irq
;
706 static int gpmc_irq_endis(unsigned irq
, bool endis
)
711 for (i
= 0; i
< GPMC_NR_IRQ
; i
++)
712 if (irq
== gpmc_client_irq
[i
].irq
) {
713 regval
= gpmc_read_reg(GPMC_IRQENABLE
);
715 regval
|= gpmc_client_irq
[i
].bitmask
;
717 regval
&= ~gpmc_client_irq
[i
].bitmask
;
718 gpmc_write_reg(GPMC_IRQENABLE
, regval
);
725 static void gpmc_irq_disable(struct irq_data
*p
)
727 gpmc_irq_endis(p
->irq
, false);
730 static void gpmc_irq_enable(struct irq_data
*p
)
732 gpmc_irq_endis(p
->irq
, true);
735 static void gpmc_irq_noop(struct irq_data
*data
) { }
737 static unsigned int gpmc_irq_noop_ret(struct irq_data
*data
) { return 0; }
739 static int gpmc_setup_irq(void)
747 gpmc_irq_start
= irq_alloc_descs(-1, 0, GPMC_NR_IRQ
, 0);
748 if (gpmc_irq_start
< 0) {
749 pr_err("irq_alloc_descs failed\n");
750 return gpmc_irq_start
;
753 gpmc_irq_chip
.name
= "gpmc";
754 gpmc_irq_chip
.irq_startup
= gpmc_irq_noop_ret
;
755 gpmc_irq_chip
.irq_enable
= gpmc_irq_enable
;
756 gpmc_irq_chip
.irq_disable
= gpmc_irq_disable
;
757 gpmc_irq_chip
.irq_shutdown
= gpmc_irq_noop
;
758 gpmc_irq_chip
.irq_ack
= gpmc_irq_noop
;
759 gpmc_irq_chip
.irq_mask
= gpmc_irq_noop
;
760 gpmc_irq_chip
.irq_unmask
= gpmc_irq_noop
;
762 gpmc_client_irq
[0].bitmask
= GPMC_IRQ_FIFOEVENTENABLE
;
763 gpmc_client_irq
[1].bitmask
= GPMC_IRQ_COUNT_EVENT
;
765 for (i
= 0; i
< GPMC_NR_IRQ
; i
++) {
766 gpmc_client_irq
[i
].irq
= gpmc_irq_start
+ i
;
767 irq_set_chip_and_handler(gpmc_client_irq
[i
].irq
,
768 &gpmc_irq_chip
, handle_simple_irq
);
769 set_irq_flags(gpmc_client_irq
[i
].irq
,
770 IRQF_VALID
| IRQF_NOAUTOEN
);
773 /* Disable interrupts */
774 gpmc_write_reg(GPMC_IRQENABLE
, 0);
776 /* clear interrupts */
777 regval
= gpmc_read_reg(GPMC_IRQSTATUS
);
778 gpmc_write_reg(GPMC_IRQSTATUS
, regval
);
780 return request_irq(gpmc_irq
, gpmc_handle_irq
, 0, "gpmc", NULL
);
783 static int gpmc_free_irq(void)
788 free_irq(gpmc_irq
, NULL
);
790 for (i
= 0; i
< GPMC_NR_IRQ
; i
++) {
791 irq_set_handler(gpmc_client_irq
[i
].irq
, NULL
);
792 irq_set_chip(gpmc_client_irq
[i
].irq
, &no_irq_chip
);
793 irq_modify_status(gpmc_client_irq
[i
].irq
, 0, 0);
796 irq_free_descs(gpmc_irq_start
, GPMC_NR_IRQ
);
801 static void gpmc_mem_exit(void)
805 for (cs
= 0; cs
< gpmc_cs_num
; cs
++) {
806 if (!gpmc_cs_mem_enabled(cs
))
808 gpmc_cs_delete_mem(cs
);
813 static void gpmc_mem_init(void)
818 * The first 1MB of GPMC address space is typically mapped to
819 * the internal ROM. Never allocate the first page, to
820 * facilitate bug detection; even if we didn't boot from ROM.
822 gpmc_mem_root
.start
= SZ_1M
;
823 gpmc_mem_root
.end
= GPMC_MEM_END
;
825 /* Reserve all regions that has been set up by bootloader */
826 for (cs
= 0; cs
< gpmc_cs_num
; cs
++) {
829 if (!gpmc_cs_mem_enabled(cs
))
831 gpmc_cs_get_memconf(cs
, &base
, &size
);
832 if (gpmc_cs_insert_mem(cs
, base
, size
)) {
833 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
834 __func__
, cs
, base
, base
+ size
);
835 gpmc_cs_disable_mem(cs
);
840 static u32
gpmc_round_ps_to_sync_clk(u32 time_ps
, u32 sync_clk
)
845 div
= gpmc_calc_divider(sync_clk
);
846 temp
= gpmc_ps_to_ticks(time_ps
);
847 temp
= (temp
+ div
- 1) / div
;
848 return gpmc_ticks_to_ps(temp
* div
);
851 /* XXX: can the cycles be avoided ? */
852 static int gpmc_calc_sync_read_timings(struct gpmc_timings
*gpmc_t
,
853 struct gpmc_device_timings
*dev_t
,
859 temp
= dev_t
->t_avdp_r
;
860 /* XXX: mux check required ? */
862 /* XXX: t_avdp not to be required for sync, only added for tusb
863 * this indirectly necessitates requirement of t_avdp_r and
864 * t_avdp_w instead of having a single t_avdp
866 temp
= max_t(u32
, temp
, gpmc_t
->clk_activation
+ dev_t
->t_avdh
);
867 temp
= max_t(u32
, gpmc_t
->adv_on
+ gpmc_ticks_to_ps(1), temp
);
869 gpmc_t
->adv_rd_off
= gpmc_round_ps_to_ticks(temp
);
872 temp
= dev_t
->t_oeasu
; /* XXX: remove this ? */
874 temp
= max_t(u32
, temp
, gpmc_t
->clk_activation
+ dev_t
->t_ach
);
875 temp
= max_t(u32
, temp
, gpmc_t
->adv_rd_off
+
876 gpmc_ticks_to_ps(dev_t
->cyc_aavdh_oe
));
878 gpmc_t
->oe_on
= gpmc_round_ps_to_ticks(temp
);
881 /* XXX: any scope for improvement ?, by combining oe_on
882 * and clk_activation, need to check whether
883 * access = clk_activation + round to sync clk ?
885 temp
= max_t(u32
, dev_t
->t_iaa
, dev_t
->cyc_iaa
* gpmc_t
->sync_clk
);
886 temp
+= gpmc_t
->clk_activation
;
888 temp
= max_t(u32
, temp
, gpmc_t
->oe_on
+
889 gpmc_ticks_to_ps(dev_t
->cyc_oe
));
890 gpmc_t
->access
= gpmc_round_ps_to_ticks(temp
);
892 gpmc_t
->oe_off
= gpmc_t
->access
+ gpmc_ticks_to_ps(1);
893 gpmc_t
->cs_rd_off
= gpmc_t
->oe_off
;
896 temp
= max_t(u32
, dev_t
->t_cez_r
, dev_t
->t_oez
);
897 temp
= gpmc_round_ps_to_sync_clk(temp
, gpmc_t
->sync_clk
) +
899 /* XXX: barter t_ce_rdyz with t_cez_r ? */
900 if (dev_t
->t_ce_rdyz
)
901 temp
= max_t(u32
, temp
, gpmc_t
->cs_rd_off
+ dev_t
->t_ce_rdyz
);
902 gpmc_t
->rd_cycle
= gpmc_round_ps_to_ticks(temp
);
907 static int gpmc_calc_sync_write_timings(struct gpmc_timings
*gpmc_t
,
908 struct gpmc_device_timings
*dev_t
,
914 temp
= dev_t
->t_avdp_w
;
916 temp
= max_t(u32
, temp
,
917 gpmc_t
->clk_activation
+ dev_t
->t_avdh
);
918 temp
= max_t(u32
, gpmc_t
->adv_on
+ gpmc_ticks_to_ps(1), temp
);
920 gpmc_t
->adv_wr_off
= gpmc_round_ps_to_ticks(temp
);
922 /* wr_data_mux_bus */
923 temp
= max_t(u32
, dev_t
->t_weasu
,
924 gpmc_t
->clk_activation
+ dev_t
->t_rdyo
);
925 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
926 * and in that case remember to handle we_on properly
929 temp
= max_t(u32
, temp
,
930 gpmc_t
->adv_wr_off
+ dev_t
->t_aavdh
);
931 temp
= max_t(u32
, temp
, gpmc_t
->adv_wr_off
+
932 gpmc_ticks_to_ps(dev_t
->cyc_aavdh_we
));
934 gpmc_t
->wr_data_mux_bus
= gpmc_round_ps_to_ticks(temp
);
937 if (gpmc_capability
& GPMC_HAS_WR_DATA_MUX_BUS
)
938 gpmc_t
->we_on
= gpmc_round_ps_to_ticks(dev_t
->t_weasu
);
940 gpmc_t
->we_on
= gpmc_t
->wr_data_mux_bus
;
943 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
944 gpmc_t
->wr_access
= gpmc_t
->access
;
947 temp
= gpmc_t
->we_on
+ dev_t
->t_wpl
;
948 temp
= max_t(u32
, temp
,
949 gpmc_t
->wr_access
+ gpmc_ticks_to_ps(1));
950 temp
= max_t(u32
, temp
,
951 gpmc_t
->we_on
+ gpmc_ticks_to_ps(dev_t
->cyc_wpl
));
952 gpmc_t
->we_off
= gpmc_round_ps_to_ticks(temp
);
954 gpmc_t
->cs_wr_off
= gpmc_round_ps_to_ticks(gpmc_t
->we_off
+
958 temp
= gpmc_round_ps_to_sync_clk(dev_t
->t_cez_w
, gpmc_t
->sync_clk
);
959 temp
+= gpmc_t
->wr_access
;
960 /* XXX: barter t_ce_rdyz with t_cez_w ? */
961 if (dev_t
->t_ce_rdyz
)
962 temp
= max_t(u32
, temp
,
963 gpmc_t
->cs_wr_off
+ dev_t
->t_ce_rdyz
);
964 gpmc_t
->wr_cycle
= gpmc_round_ps_to_ticks(temp
);
969 static int gpmc_calc_async_read_timings(struct gpmc_timings
*gpmc_t
,
970 struct gpmc_device_timings
*dev_t
,
976 temp
= dev_t
->t_avdp_r
;
978 temp
= max_t(u32
, gpmc_t
->adv_on
+ gpmc_ticks_to_ps(1), temp
);
979 gpmc_t
->adv_rd_off
= gpmc_round_ps_to_ticks(temp
);
982 temp
= dev_t
->t_oeasu
;
984 temp
= max_t(u32
, temp
,
985 gpmc_t
->adv_rd_off
+ dev_t
->t_aavdh
);
986 gpmc_t
->oe_on
= gpmc_round_ps_to_ticks(temp
);
989 temp
= max_t(u32
, dev_t
->t_iaa
, /* XXX: remove t_iaa in async ? */
990 gpmc_t
->oe_on
+ dev_t
->t_oe
);
991 temp
= max_t(u32
, temp
,
992 gpmc_t
->cs_on
+ dev_t
->t_ce
);
993 temp
= max_t(u32
, temp
,
994 gpmc_t
->adv_on
+ dev_t
->t_aa
);
995 gpmc_t
->access
= gpmc_round_ps_to_ticks(temp
);
997 gpmc_t
->oe_off
= gpmc_t
->access
+ gpmc_ticks_to_ps(1);
998 gpmc_t
->cs_rd_off
= gpmc_t
->oe_off
;
1001 temp
= max_t(u32
, dev_t
->t_rd_cycle
,
1002 gpmc_t
->cs_rd_off
+ dev_t
->t_cez_r
);
1003 temp
= max_t(u32
, temp
, gpmc_t
->oe_off
+ dev_t
->t_oez
);
1004 gpmc_t
->rd_cycle
= gpmc_round_ps_to_ticks(temp
);
1009 static int gpmc_calc_async_write_timings(struct gpmc_timings
*gpmc_t
,
1010 struct gpmc_device_timings
*dev_t
,
1016 temp
= dev_t
->t_avdp_w
;
1018 temp
= max_t(u32
, gpmc_t
->adv_on
+ gpmc_ticks_to_ps(1), temp
);
1019 gpmc_t
->adv_wr_off
= gpmc_round_ps_to_ticks(temp
);
1021 /* wr_data_mux_bus */
1022 temp
= dev_t
->t_weasu
;
1024 temp
= max_t(u32
, temp
, gpmc_t
->adv_wr_off
+ dev_t
->t_aavdh
);
1025 temp
= max_t(u32
, temp
, gpmc_t
->adv_wr_off
+
1026 gpmc_ticks_to_ps(dev_t
->cyc_aavdh_we
));
1028 gpmc_t
->wr_data_mux_bus
= gpmc_round_ps_to_ticks(temp
);
1031 if (gpmc_capability
& GPMC_HAS_WR_DATA_MUX_BUS
)
1032 gpmc_t
->we_on
= gpmc_round_ps_to_ticks(dev_t
->t_weasu
);
1034 gpmc_t
->we_on
= gpmc_t
->wr_data_mux_bus
;
1037 temp
= gpmc_t
->we_on
+ dev_t
->t_wpl
;
1038 gpmc_t
->we_off
= gpmc_round_ps_to_ticks(temp
);
1040 gpmc_t
->cs_wr_off
= gpmc_round_ps_to_ticks(gpmc_t
->we_off
+
1044 temp
= max_t(u32
, dev_t
->t_wr_cycle
,
1045 gpmc_t
->cs_wr_off
+ dev_t
->t_cez_w
);
1046 gpmc_t
->wr_cycle
= gpmc_round_ps_to_ticks(temp
);
1051 static int gpmc_calc_sync_common_timings(struct gpmc_timings
*gpmc_t
,
1052 struct gpmc_device_timings
*dev_t
)
1056 gpmc_t
->sync_clk
= gpmc_calc_divider(dev_t
->clk
) *
1057 gpmc_get_fclk_period();
1059 gpmc_t
->page_burst_access
= gpmc_round_ps_to_sync_clk(
1063 temp
= max_t(u32
, dev_t
->t_ces
, dev_t
->t_avds
);
1064 gpmc_t
->clk_activation
= gpmc_round_ps_to_ticks(temp
);
1066 if (gpmc_calc_divider(gpmc_t
->sync_clk
) != 1)
1069 if (dev_t
->ce_xdelay
)
1070 gpmc_t
->bool_timings
.cs_extra_delay
= true;
1071 if (dev_t
->avd_xdelay
)
1072 gpmc_t
->bool_timings
.adv_extra_delay
= true;
1073 if (dev_t
->oe_xdelay
)
1074 gpmc_t
->bool_timings
.oe_extra_delay
= true;
1075 if (dev_t
->we_xdelay
)
1076 gpmc_t
->bool_timings
.we_extra_delay
= true;
1081 static int gpmc_calc_common_timings(struct gpmc_timings
*gpmc_t
,
1082 struct gpmc_device_timings
*dev_t
,
1088 gpmc_t
->cs_on
= gpmc_round_ps_to_ticks(dev_t
->t_ceasu
);
1091 temp
= dev_t
->t_avdasu
;
1092 if (dev_t
->t_ce_avd
)
1093 temp
= max_t(u32
, temp
,
1094 gpmc_t
->cs_on
+ dev_t
->t_ce_avd
);
1095 gpmc_t
->adv_on
= gpmc_round_ps_to_ticks(temp
);
1098 gpmc_calc_sync_common_timings(gpmc_t
, dev_t
);
1103 /* TODO: remove this function once all peripherals are confirmed to
1104 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1105 * has to be modified to handle timings in ps instead of ns
1107 static void gpmc_convert_ps_to_ns(struct gpmc_timings
*t
)
1110 t
->cs_rd_off
/= 1000;
1111 t
->cs_wr_off
/= 1000;
1113 t
->adv_rd_off
/= 1000;
1114 t
->adv_wr_off
/= 1000;
1119 t
->page_burst_access
/= 1000;
1121 t
->rd_cycle
/= 1000;
1122 t
->wr_cycle
/= 1000;
1123 t
->bus_turnaround
/= 1000;
1124 t
->cycle2cycle_delay
/= 1000;
1125 t
->wait_monitoring
/= 1000;
1126 t
->clk_activation
/= 1000;
1127 t
->wr_access
/= 1000;
1128 t
->wr_data_mux_bus
/= 1000;
1131 int gpmc_calc_timings(struct gpmc_timings
*gpmc_t
,
1132 struct gpmc_settings
*gpmc_s
,
1133 struct gpmc_device_timings
*dev_t
)
1135 bool mux
= false, sync
= false;
1138 mux
= gpmc_s
->mux_add_data
? true : false;
1139 sync
= (gpmc_s
->sync_read
|| gpmc_s
->sync_write
);
1142 memset(gpmc_t
, 0, sizeof(*gpmc_t
));
1144 gpmc_calc_common_timings(gpmc_t
, dev_t
, sync
);
1146 if (gpmc_s
&& gpmc_s
->sync_read
)
1147 gpmc_calc_sync_read_timings(gpmc_t
, dev_t
, mux
);
1149 gpmc_calc_async_read_timings(gpmc_t
, dev_t
, mux
);
1151 if (gpmc_s
&& gpmc_s
->sync_write
)
1152 gpmc_calc_sync_write_timings(gpmc_t
, dev_t
, mux
);
1154 gpmc_calc_async_write_timings(gpmc_t
, dev_t
, mux
);
1156 /* TODO: remove, see function definition */
1157 gpmc_convert_ps_to_ns(gpmc_t
);
1163 * gpmc_cs_program_settings - programs non-timing related settings
1164 * @cs: GPMC chip-select to program
1165 * @p: pointer to GPMC settings structure
1167 * Programs non-timing related settings for a GPMC chip-select, such as
1168 * bus-width, burst configuration, etc. Function should be called once
1169 * for each chip-select that is being used and must be called before
1170 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1171 * register will be initialised to zero by this function. Returns 0 on
1172 * success and appropriate negative error code on failure.
1174 int gpmc_cs_program_settings(int cs
, struct gpmc_settings
*p
)
1178 if ((!p
->device_width
) || (p
->device_width
> GPMC_DEVWIDTH_16BIT
)) {
1179 pr_err("%s: invalid width %d!", __func__
, p
->device_width
);
1183 /* Address-data multiplexing not supported for NAND devices */
1184 if (p
->device_nand
&& p
->mux_add_data
) {
1185 pr_err("%s: invalid configuration!\n", __func__
);
1189 if ((p
->mux_add_data
> GPMC_MUX_AD
) ||
1190 ((p
->mux_add_data
== GPMC_MUX_AAD
) &&
1191 !(gpmc_capability
& GPMC_HAS_MUX_AAD
))) {
1192 pr_err("%s: invalid multiplex configuration!\n", __func__
);
1196 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1197 if (p
->burst_read
|| p
->burst_write
) {
1198 switch (p
->burst_len
) {
1204 pr_err("%s: invalid page/burst-length (%d)\n",
1205 __func__
, p
->burst_len
);
1210 if ((p
->wait_on_read
|| p
->wait_on_write
) &&
1211 (p
->wait_pin
> gpmc_nr_waitpins
)) {
1212 pr_err("%s: invalid wait-pin (%d)\n", __func__
, p
->wait_pin
);
1216 config1
= GPMC_CONFIG1_DEVICESIZE((p
->device_width
- 1));
1219 config1
|= GPMC_CONFIG1_READTYPE_SYNC
;
1221 config1
|= GPMC_CONFIG1_WRITETYPE_SYNC
;
1222 if (p
->wait_on_read
)
1223 config1
|= GPMC_CONFIG1_WAIT_READ_MON
;
1224 if (p
->wait_on_write
)
1225 config1
|= GPMC_CONFIG1_WAIT_WRITE_MON
;
1226 if (p
->wait_on_read
|| p
->wait_on_write
)
1227 config1
|= GPMC_CONFIG1_WAIT_PIN_SEL(p
->wait_pin
);
1229 config1
|= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND
);
1230 if (p
->mux_add_data
)
1231 config1
|= GPMC_CONFIG1_MUXTYPE(p
->mux_add_data
);
1233 config1
|= GPMC_CONFIG1_READMULTIPLE_SUPP
;
1235 config1
|= GPMC_CONFIG1_WRITEMULTIPLE_SUPP
;
1236 if (p
->burst_read
|| p
->burst_write
) {
1237 config1
|= GPMC_CONFIG1_PAGE_LEN(p
->burst_len
>> 3);
1238 config1
|= p
->burst_wrap
? GPMC_CONFIG1_WRAPBURST_SUPP
: 0;
1241 gpmc_cs_write_reg(cs
, GPMC_CS_CONFIG1
, config1
);
1247 static struct of_device_id gpmc_dt_ids
[] = {
1248 { .compatible
= "ti,omap2420-gpmc" },
1249 { .compatible
= "ti,omap2430-gpmc" },
1250 { .compatible
= "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1251 { .compatible
= "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1252 { .compatible
= "ti,am3352-gpmc" }, /* am335x devices */
1255 MODULE_DEVICE_TABLE(of
, gpmc_dt_ids
);
1258 * gpmc_read_settings_dt - read gpmc settings from device-tree
1259 * @np: pointer to device-tree node for a gpmc child device
1260 * @p: pointer to gpmc settings structure
1262 * Reads the GPMC settings for a GPMC child device from device-tree and
1263 * stores them in the GPMC settings structure passed. The GPMC settings
1264 * structure is initialised to zero by this function and so any
1265 * previously stored settings will be cleared.
1267 void gpmc_read_settings_dt(struct device_node
*np
, struct gpmc_settings
*p
)
1269 memset(p
, 0, sizeof(struct gpmc_settings
));
1271 p
->sync_read
= of_property_read_bool(np
, "gpmc,sync-read");
1272 p
->sync_write
= of_property_read_bool(np
, "gpmc,sync-write");
1273 of_property_read_u32(np
, "gpmc,device-width", &p
->device_width
);
1274 of_property_read_u32(np
, "gpmc,mux-add-data", &p
->mux_add_data
);
1276 if (!of_property_read_u32(np
, "gpmc,burst-length", &p
->burst_len
)) {
1277 p
->burst_wrap
= of_property_read_bool(np
, "gpmc,burst-wrap");
1278 p
->burst_read
= of_property_read_bool(np
, "gpmc,burst-read");
1279 p
->burst_write
= of_property_read_bool(np
, "gpmc,burst-write");
1280 if (!p
->burst_read
&& !p
->burst_write
)
1281 pr_warn("%s: page/burst-length set but not used!\n",
1285 if (!of_property_read_u32(np
, "gpmc,wait-pin", &p
->wait_pin
)) {
1286 p
->wait_on_read
= of_property_read_bool(np
,
1287 "gpmc,wait-on-read");
1288 p
->wait_on_write
= of_property_read_bool(np
,
1289 "gpmc,wait-on-write");
1290 if (!p
->wait_on_read
&& !p
->wait_on_write
)
1291 pr_warn("%s: read/write wait monitoring not enabled!\n",
1296 static void __maybe_unused
gpmc_read_timings_dt(struct device_node
*np
,
1297 struct gpmc_timings
*gpmc_t
)
1299 struct gpmc_bool_timings
*p
;
1304 memset(gpmc_t
, 0, sizeof(*gpmc_t
));
1306 /* minimum clock period for syncronous mode */
1307 of_property_read_u32(np
, "gpmc,sync-clk-ps", &gpmc_t
->sync_clk
);
1309 /* chip select timtings */
1310 of_property_read_u32(np
, "gpmc,cs-on-ns", &gpmc_t
->cs_on
);
1311 of_property_read_u32(np
, "gpmc,cs-rd-off-ns", &gpmc_t
->cs_rd_off
);
1312 of_property_read_u32(np
, "gpmc,cs-wr-off-ns", &gpmc_t
->cs_wr_off
);
1314 /* ADV signal timings */
1315 of_property_read_u32(np
, "gpmc,adv-on-ns", &gpmc_t
->adv_on
);
1316 of_property_read_u32(np
, "gpmc,adv-rd-off-ns", &gpmc_t
->adv_rd_off
);
1317 of_property_read_u32(np
, "gpmc,adv-wr-off-ns", &gpmc_t
->adv_wr_off
);
1319 /* WE signal timings */
1320 of_property_read_u32(np
, "gpmc,we-on-ns", &gpmc_t
->we_on
);
1321 of_property_read_u32(np
, "gpmc,we-off-ns", &gpmc_t
->we_off
);
1323 /* OE signal timings */
1324 of_property_read_u32(np
, "gpmc,oe-on-ns", &gpmc_t
->oe_on
);
1325 of_property_read_u32(np
, "gpmc,oe-off-ns", &gpmc_t
->oe_off
);
1327 /* access and cycle timings */
1328 of_property_read_u32(np
, "gpmc,page-burst-access-ns",
1329 &gpmc_t
->page_burst_access
);
1330 of_property_read_u32(np
, "gpmc,access-ns", &gpmc_t
->access
);
1331 of_property_read_u32(np
, "gpmc,rd-cycle-ns", &gpmc_t
->rd_cycle
);
1332 of_property_read_u32(np
, "gpmc,wr-cycle-ns", &gpmc_t
->wr_cycle
);
1333 of_property_read_u32(np
, "gpmc,bus-turnaround-ns",
1334 &gpmc_t
->bus_turnaround
);
1335 of_property_read_u32(np
, "gpmc,cycle2cycle-delay-ns",
1336 &gpmc_t
->cycle2cycle_delay
);
1337 of_property_read_u32(np
, "gpmc,wait-monitoring-ns",
1338 &gpmc_t
->wait_monitoring
);
1339 of_property_read_u32(np
, "gpmc,clk-activation-ns",
1340 &gpmc_t
->clk_activation
);
1342 /* only applicable to OMAP3+ */
1343 of_property_read_u32(np
, "gpmc,wr-access-ns", &gpmc_t
->wr_access
);
1344 of_property_read_u32(np
, "gpmc,wr-data-mux-bus-ns",
1345 &gpmc_t
->wr_data_mux_bus
);
1347 /* bool timing parameters */
1348 p
= &gpmc_t
->bool_timings
;
1350 p
->cycle2cyclediffcsen
=
1351 of_property_read_bool(np
, "gpmc,cycle2cycle-diffcsen");
1352 p
->cycle2cyclesamecsen
=
1353 of_property_read_bool(np
, "gpmc,cycle2cycle-samecsen");
1354 p
->we_extra_delay
= of_property_read_bool(np
, "gpmc,we-extra-delay");
1355 p
->oe_extra_delay
= of_property_read_bool(np
, "gpmc,oe-extra-delay");
1356 p
->adv_extra_delay
= of_property_read_bool(np
, "gpmc,adv-extra-delay");
1357 p
->cs_extra_delay
= of_property_read_bool(np
, "gpmc,cs-extra-delay");
1358 p
->time_para_granularity
=
1359 of_property_read_bool(np
, "gpmc,time-para-granularity");
1362 #if IS_ENABLED(CONFIG_MTD_NAND)
1364 static const char * const nand_xfer_types
[] = {
1365 [NAND_OMAP_PREFETCH_POLLED
] = "prefetch-polled",
1366 [NAND_OMAP_POLLED
] = "polled",
1367 [NAND_OMAP_PREFETCH_DMA
] = "prefetch-dma",
1368 [NAND_OMAP_PREFETCH_IRQ
] = "prefetch-irq",
1371 static int gpmc_probe_nand_child(struct platform_device
*pdev
,
1372 struct device_node
*child
)
1376 struct gpmc_timings gpmc_t
;
1377 struct omap_nand_platform_data
*gpmc_nand_data
;
1379 if (of_property_read_u32(child
, "reg", &val
) < 0) {
1380 dev_err(&pdev
->dev
, "%s has no 'reg' property\n",
1385 gpmc_nand_data
= devm_kzalloc(&pdev
->dev
, sizeof(*gpmc_nand_data
),
1387 if (!gpmc_nand_data
)
1390 gpmc_nand_data
->cs
= val
;
1391 gpmc_nand_data
->of_node
= child
;
1393 /* Detect availability of ELM module */
1394 gpmc_nand_data
->elm_of_node
= of_parse_phandle(child
, "ti,elm-id", 0);
1395 if (gpmc_nand_data
->elm_of_node
== NULL
)
1396 gpmc_nand_data
->elm_of_node
=
1397 of_parse_phandle(child
, "elm_id", 0);
1398 if (gpmc_nand_data
->elm_of_node
== NULL
)
1399 pr_warn("%s: ti,elm-id property not found\n", __func__
);
1401 /* select ecc-scheme for NAND */
1402 if (of_property_read_string(child
, "ti,nand-ecc-opt", &s
)) {
1403 pr_err("%s: ti,nand-ecc-opt not found\n", __func__
);
1406 if (!strcmp(s
, "ham1") || !strcmp(s
, "sw") ||
1407 !strcmp(s
, "hw") || !strcmp(s
, "hw-romcode"))
1408 gpmc_nand_data
->ecc_opt
=
1409 OMAP_ECC_HAM1_CODE_HW
;
1410 else if (!strcmp(s
, "bch4"))
1411 if (gpmc_nand_data
->elm_of_node
)
1412 gpmc_nand_data
->ecc_opt
=
1413 OMAP_ECC_BCH4_CODE_HW
;
1415 gpmc_nand_data
->ecc_opt
=
1416 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW
;
1417 else if (!strcmp(s
, "bch8"))
1418 if (gpmc_nand_data
->elm_of_node
)
1419 gpmc_nand_data
->ecc_opt
=
1420 OMAP_ECC_BCH8_CODE_HW
;
1422 gpmc_nand_data
->ecc_opt
=
1423 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW
;
1424 else if (!strcmp(s
, "bch16"))
1425 if (gpmc_nand_data
->elm_of_node
)
1426 gpmc_nand_data
->ecc_opt
=
1427 OMAP_ECC_BCH16_CODE_HW
;
1429 pr_err("%s: BCH16 requires ELM support\n", __func__
);
1431 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__
);
1433 /* select data transfer mode for NAND controller */
1434 if (!of_property_read_string(child
, "ti,nand-xfer-type", &s
))
1435 for (val
= 0; val
< ARRAY_SIZE(nand_xfer_types
); val
++)
1436 if (!strcasecmp(s
, nand_xfer_types
[val
])) {
1437 gpmc_nand_data
->xfer_type
= val
;
1441 val
= of_get_nand_bus_width(child
);
1443 gpmc_nand_data
->devsize
= NAND_BUSWIDTH_16
;
1445 gpmc_read_timings_dt(child
, &gpmc_t
);
1446 gpmc_nand_init(gpmc_nand_data
, &gpmc_t
);
1451 static int gpmc_probe_nand_child(struct platform_device
*pdev
,
1452 struct device_node
*child
)
1458 #if IS_ENABLED(CONFIG_MTD_ONENAND)
1459 static int gpmc_probe_onenand_child(struct platform_device
*pdev
,
1460 struct device_node
*child
)
1463 struct omap_onenand_platform_data
*gpmc_onenand_data
;
1465 if (of_property_read_u32(child
, "reg", &val
) < 0) {
1466 dev_err(&pdev
->dev
, "%s has no 'reg' property\n",
1471 gpmc_onenand_data
= devm_kzalloc(&pdev
->dev
, sizeof(*gpmc_onenand_data
),
1473 if (!gpmc_onenand_data
)
1476 gpmc_onenand_data
->cs
= val
;
1477 gpmc_onenand_data
->of_node
= child
;
1478 gpmc_onenand_data
->dma_channel
= -1;
1480 if (!of_property_read_u32(child
, "dma-channel", &val
))
1481 gpmc_onenand_data
->dma_channel
= val
;
1483 gpmc_onenand_init(gpmc_onenand_data
);
1488 static int gpmc_probe_onenand_child(struct platform_device
*pdev
,
1489 struct device_node
*child
)
1496 * gpmc_probe_generic_child - configures the gpmc for a child device
1497 * @pdev: pointer to gpmc platform device
1498 * @child: pointer to device-tree node for child device
1500 * Allocates and configures a GPMC chip-select for a child device.
1501 * Returns 0 on success and appropriate negative error code on failure.
1503 static int gpmc_probe_generic_child(struct platform_device
*pdev
,
1504 struct device_node
*child
)
1506 struct gpmc_settings gpmc_s
;
1507 struct gpmc_timings gpmc_t
;
1508 struct resource res
;
1512 if (of_property_read_u32(child
, "reg", &cs
) < 0) {
1513 dev_err(&pdev
->dev
, "%s has no 'reg' property\n",
1518 if (of_address_to_resource(child
, 0, &res
) < 0) {
1519 dev_err(&pdev
->dev
, "%s has malformed 'reg' property\n",
1524 ret
= gpmc_cs_request(cs
, resource_size(&res
), &base
);
1526 dev_err(&pdev
->dev
, "cannot request GPMC CS %d\n", cs
);
1531 * For some GPMC devices we still need to rely on the bootloader
1532 * timings because the devices can be connected via FPGA. So far
1533 * the list is smc91x on the omap2 SDP boards, and 8250 on zooms.
1534 * REVISIT: Add timing support from slls644g.pdf and from the
1537 if (of_device_is_compatible(child
, "ns16550a") ||
1538 of_device_is_compatible(child
, "smsc,lan91c94") ||
1539 of_device_is_compatible(child
, "smsc,lan91c111")) {
1540 dev_warn(&pdev
->dev
,
1541 "%s using bootloader timings on CS%d\n",
1547 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1548 * location in the gpmc address space. When booting with
1549 * device-tree we want the NOR flash to be mapped to the
1550 * location specified in the device-tree blob. So remap the
1551 * CS to this location. Once DT migration is complete should
1552 * just make gpmc_cs_request() map a specific address.
1554 ret
= gpmc_cs_remap(cs
, res
.start
);
1556 dev_err(&pdev
->dev
, "cannot remap GPMC CS %d to %pa\n",
1561 gpmc_read_settings_dt(child
, &gpmc_s
);
1563 ret
= of_property_read_u32(child
, "bank-width", &gpmc_s
.device_width
);
1567 ret
= gpmc_cs_program_settings(cs
, &gpmc_s
);
1571 gpmc_read_timings_dt(child
, &gpmc_t
);
1572 gpmc_cs_set_timings(cs
, &gpmc_t
);
1575 if (of_platform_device_create(child
, NULL
, &pdev
->dev
))
1578 dev_err(&pdev
->dev
, "failed to create gpmc child %s\n", child
->name
);
1587 static int gpmc_probe_dt(struct platform_device
*pdev
)
1590 struct device_node
*child
;
1591 const struct of_device_id
*of_id
=
1592 of_match_device(gpmc_dt_ids
, &pdev
->dev
);
1597 ret
= of_property_read_u32(pdev
->dev
.of_node
, "gpmc,num-cs",
1600 pr_err("%s: number of chip-selects not defined\n", __func__
);
1602 } else if (gpmc_cs_num
< 1) {
1603 pr_err("%s: all chip-selects are disabled\n", __func__
);
1605 } else if (gpmc_cs_num
> GPMC_CS_NUM
) {
1606 pr_err("%s: number of supported chip-selects cannot be > %d\n",
1607 __func__
, GPMC_CS_NUM
);
1611 ret
= of_property_read_u32(pdev
->dev
.of_node
, "gpmc,num-waitpins",
1614 pr_err("%s: number of wait pins not found!\n", __func__
);
1618 for_each_child_of_node(pdev
->dev
.of_node
, child
) {
1623 if (of_node_cmp(child
->name
, "nand") == 0)
1624 ret
= gpmc_probe_nand_child(pdev
, child
);
1625 else if (of_node_cmp(child
->name
, "onenand") == 0)
1626 ret
= gpmc_probe_onenand_child(pdev
, child
);
1627 else if (of_node_cmp(child
->name
, "ethernet") == 0 ||
1628 of_node_cmp(child
->name
, "nor") == 0 ||
1629 of_node_cmp(child
->name
, "uart") == 0)
1630 ret
= gpmc_probe_generic_child(pdev
, child
);
1632 if (WARN(ret
< 0, "%s: probing gpmc child %s failed\n",
1633 __func__
, child
->full_name
))
1640 static int gpmc_probe_dt(struct platform_device
*pdev
)
1646 static int gpmc_probe(struct platform_device
*pdev
)
1650 struct resource
*res
;
1652 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1656 phys_base
= res
->start
;
1657 mem_size
= resource_size(res
);
1659 gpmc_base
= devm_ioremap_resource(&pdev
->dev
, res
);
1660 if (IS_ERR(gpmc_base
))
1661 return PTR_ERR(gpmc_base
);
1663 res
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1665 dev_warn(&pdev
->dev
, "Failed to get resource: irq\n");
1667 gpmc_irq
= res
->start
;
1669 gpmc_l3_clk
= clk_get(&pdev
->dev
, "fck");
1670 if (IS_ERR(gpmc_l3_clk
)) {
1671 dev_err(&pdev
->dev
, "error: clk_get\n");
1673 return PTR_ERR(gpmc_l3_clk
);
1676 pm_runtime_enable(&pdev
->dev
);
1677 pm_runtime_get_sync(&pdev
->dev
);
1679 gpmc_dev
= &pdev
->dev
;
1681 l
= gpmc_read_reg(GPMC_REVISION
);
1684 * FIXME: Once device-tree migration is complete the below flags
1685 * should be populated based upon the device-tree compatible
1686 * string. For now just use the IP revision. OMAP3+ devices have
1687 * the wr_access and wr_data_mux_bus register fields. OMAP4+
1688 * devices support the addr-addr-data multiplex protocol.
1690 * GPMC IP revisions:
1693 * - OMAP44xx/54xx/AM335x = 6.0
1695 if (GPMC_REVISION_MAJOR(l
) > 0x4)
1696 gpmc_capability
= GPMC_HAS_WR_ACCESS
| GPMC_HAS_WR_DATA_MUX_BUS
;
1697 if (GPMC_REVISION_MAJOR(l
) > 0x5)
1698 gpmc_capability
|= GPMC_HAS_MUX_AAD
;
1699 dev_info(gpmc_dev
, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l
),
1700 GPMC_REVISION_MINOR(l
));
1704 if (gpmc_setup_irq() < 0)
1705 dev_warn(gpmc_dev
, "gpmc_setup_irq failed\n");
1707 /* Now the GPMC is initialised, unreserve the chip-selects */
1710 if (!pdev
->dev
.of_node
) {
1711 gpmc_cs_num
= GPMC_CS_NUM
;
1712 gpmc_nr_waitpins
= GPMC_NR_WAITPINS
;
1715 rc
= gpmc_probe_dt(pdev
);
1717 pm_runtime_put_sync(&pdev
->dev
);
1718 clk_put(gpmc_l3_clk
);
1719 dev_err(gpmc_dev
, "failed to probe DT parameters\n");
1726 static int gpmc_remove(struct platform_device
*pdev
)
1730 pm_runtime_put_sync(&pdev
->dev
);
1731 pm_runtime_disable(&pdev
->dev
);
1736 #ifdef CONFIG_PM_SLEEP
1737 static int gpmc_suspend(struct device
*dev
)
1739 omap3_gpmc_save_context();
1740 pm_runtime_put_sync(dev
);
1744 static int gpmc_resume(struct device
*dev
)
1746 pm_runtime_get_sync(dev
);
1747 omap3_gpmc_restore_context();
1752 static SIMPLE_DEV_PM_OPS(gpmc_pm_ops
, gpmc_suspend
, gpmc_resume
);
1754 static struct platform_driver gpmc_driver
= {
1755 .probe
= gpmc_probe
,
1756 .remove
= gpmc_remove
,
1758 .name
= DEVICE_NAME
,
1759 .owner
= THIS_MODULE
,
1760 .of_match_table
= of_match_ptr(gpmc_dt_ids
),
1765 static __init
int gpmc_init(void)
1767 return platform_driver_register(&gpmc_driver
);
1770 static __exit
void gpmc_exit(void)
1772 platform_driver_unregister(&gpmc_driver
);
1776 omap_postcore_initcall(gpmc_init
);
1777 module_exit(gpmc_exit
);
1779 static int __init
omap_gpmc_init(void)
1781 struct omap_hwmod
*oh
;
1782 struct platform_device
*pdev
;
1783 char *oh_name
= "gpmc";
1786 * if the board boots up with a populated DT, do not
1787 * manually add the device from this initcall
1789 if (of_have_populated_dt())
1792 oh
= omap_hwmod_lookup(oh_name
);
1794 pr_err("Could not look up %s\n", oh_name
);
1798 pdev
= omap_device_build(DEVICE_NAME
, -1, oh
, NULL
, 0);
1799 WARN(IS_ERR(pdev
), "could not build omap_device for %s\n", oh_name
);
1801 return PTR_RET(pdev
);
1803 omap_postcore_initcall(omap_gpmc_init
);
1805 static irqreturn_t
gpmc_handle_irq(int irq
, void *dev
)
1810 regval
= gpmc_read_reg(GPMC_IRQSTATUS
);
1815 for (i
= 0; i
< GPMC_NR_IRQ
; i
++)
1816 if (regval
& gpmc_client_irq
[i
].bitmask
)
1817 generic_handle_irq(gpmc_client_irq
[i
].irq
);
1819 gpmc_write_reg(GPMC_IRQSTATUS
, regval
);
1824 static struct omap3_gpmc_regs gpmc_context
;
1826 void omap3_gpmc_save_context(void)
1830 gpmc_context
.sysconfig
= gpmc_read_reg(GPMC_SYSCONFIG
);
1831 gpmc_context
.irqenable
= gpmc_read_reg(GPMC_IRQENABLE
);
1832 gpmc_context
.timeout_ctrl
= gpmc_read_reg(GPMC_TIMEOUT_CONTROL
);
1833 gpmc_context
.config
= gpmc_read_reg(GPMC_CONFIG
);
1834 gpmc_context
.prefetch_config1
= gpmc_read_reg(GPMC_PREFETCH_CONFIG1
);
1835 gpmc_context
.prefetch_config2
= gpmc_read_reg(GPMC_PREFETCH_CONFIG2
);
1836 gpmc_context
.prefetch_control
= gpmc_read_reg(GPMC_PREFETCH_CONTROL
);
1837 for (i
= 0; i
< gpmc_cs_num
; i
++) {
1838 gpmc_context
.cs_context
[i
].is_valid
= gpmc_cs_mem_enabled(i
);
1839 if (gpmc_context
.cs_context
[i
].is_valid
) {
1840 gpmc_context
.cs_context
[i
].config1
=
1841 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG1
);
1842 gpmc_context
.cs_context
[i
].config2
=
1843 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG2
);
1844 gpmc_context
.cs_context
[i
].config3
=
1845 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG3
);
1846 gpmc_context
.cs_context
[i
].config4
=
1847 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG4
);
1848 gpmc_context
.cs_context
[i
].config5
=
1849 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG5
);
1850 gpmc_context
.cs_context
[i
].config6
=
1851 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG6
);
1852 gpmc_context
.cs_context
[i
].config7
=
1853 gpmc_cs_read_reg(i
, GPMC_CS_CONFIG7
);
1858 void omap3_gpmc_restore_context(void)
1862 gpmc_write_reg(GPMC_SYSCONFIG
, gpmc_context
.sysconfig
);
1863 gpmc_write_reg(GPMC_IRQENABLE
, gpmc_context
.irqenable
);
1864 gpmc_write_reg(GPMC_TIMEOUT_CONTROL
, gpmc_context
.timeout_ctrl
);
1865 gpmc_write_reg(GPMC_CONFIG
, gpmc_context
.config
);
1866 gpmc_write_reg(GPMC_PREFETCH_CONFIG1
, gpmc_context
.prefetch_config1
);
1867 gpmc_write_reg(GPMC_PREFETCH_CONFIG2
, gpmc_context
.prefetch_config2
);
1868 gpmc_write_reg(GPMC_PREFETCH_CONTROL
, gpmc_context
.prefetch_control
);
1869 for (i
= 0; i
< gpmc_cs_num
; i
++) {
1870 if (gpmc_context
.cs_context
[i
].is_valid
) {
1871 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG1
,
1872 gpmc_context
.cs_context
[i
].config1
);
1873 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG2
,
1874 gpmc_context
.cs_context
[i
].config2
);
1875 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG3
,
1876 gpmc_context
.cs_context
[i
].config3
);
1877 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG4
,
1878 gpmc_context
.cs_context
[i
].config4
);
1879 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG5
,
1880 gpmc_context
.cs_context
[i
].config5
);
1881 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG6
,
1882 gpmc_context
.cs_context
[i
].config6
);
1883 gpmc_cs_write_reg(i
, GPMC_CS_CONFIG7
,
1884 gpmc_context
.cs_context
[i
].config7
);