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.
15 #include <linux/irq.h>
16 #include <linux/kernel.h>
17 #include <linux/init.h>
18 #include <linux/err.h>
19 #include <linux/clk.h>
20 #include <linux/ioport.h>
21 #include <linux/spinlock.h>
23 #include <linux/module.h>
24 #include <linux/gpio/driver.h>
25 #include <linux/interrupt.h>
26 #include <linux/irqdomain.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/of_platform.h>
33 #include <linux/omap-gpmc.h>
34 #include <linux/pm_runtime.h>
36 #include <linux/platform_data/mtd-nand-omap2.h>
37 #include <linux/platform_data/mtd-onenand-omap2.h>
39 #include <asm/mach-types.h>
41 #define DEVICE_NAME "omap-gpmc"
43 /* GPMC register offsets */
44 #define GPMC_REVISION 0x00
45 #define GPMC_SYSCONFIG 0x10
46 #define GPMC_SYSSTATUS 0x14
47 #define GPMC_IRQSTATUS 0x18
48 #define GPMC_IRQENABLE 0x1c
49 #define GPMC_TIMEOUT_CONTROL 0x40
50 #define GPMC_ERR_ADDRESS 0x44
51 #define GPMC_ERR_TYPE 0x48
52 #define GPMC_CONFIG 0x50
53 #define GPMC_STATUS 0x54
54 #define GPMC_PREFETCH_CONFIG1 0x1e0
55 #define GPMC_PREFETCH_CONFIG2 0x1e4
56 #define GPMC_PREFETCH_CONTROL 0x1ec
57 #define GPMC_PREFETCH_STATUS 0x1f0
58 #define GPMC_ECC_CONFIG 0x1f4
59 #define GPMC_ECC_CONTROL 0x1f8
60 #define GPMC_ECC_SIZE_CONFIG 0x1fc
61 #define GPMC_ECC1_RESULT 0x200
62 #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
63 #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
64 #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
65 #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
66 #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
67 #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
68 #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
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
83 #define GPMC_CONFIG_LIMITEDADDRESS BIT(1)
85 #define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS BIT(0)
87 #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
88 #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
89 #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
90 #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
91 #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
92 #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
94 #define GPMC_CS0_OFFSET 0x60
95 #define GPMC_CS_SIZE 0x30
96 #define GPMC_BCH_SIZE 0x10
99 * The first 1MB of GPMC address space is typically mapped to
100 * the internal ROM. Never allocate the first page, to
101 * facilitate bug detection; even if we didn't boot from ROM.
102 * As GPMC minimum partition size is 16MB we can only start from
105 #define GPMC_MEM_START 0x1000000
106 #define GPMC_MEM_END 0x3FFFFFFF
108 #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
109 #define GPMC_SECTION_SHIFT 28 /* 128 MB */
111 #define CS_NUM_SHIFT 24
112 #define ENABLE_PREFETCH (0x1 << 7)
113 #define DMA_MPU_MODE 2
115 #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
116 #define GPMC_REVISION_MINOR(l) (l & 0xf)
118 #define GPMC_HAS_WR_ACCESS 0x1
119 #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
120 #define GPMC_HAS_MUX_AAD 0x4
122 #define GPMC_NR_WAITPINS 4
124 #define GPMC_CS_CONFIG1 0x00
125 #define GPMC_CS_CONFIG2 0x04
126 #define GPMC_CS_CONFIG3 0x08
127 #define GPMC_CS_CONFIG4 0x0c
128 #define GPMC_CS_CONFIG5 0x10
129 #define GPMC_CS_CONFIG6 0x14
130 #define GPMC_CS_CONFIG7 0x18
131 #define GPMC_CS_NAND_COMMAND 0x1c
132 #define GPMC_CS_NAND_ADDRESS 0x20
133 #define GPMC_CS_NAND_DATA 0x24
135 /* Control Commands */
136 #define GPMC_CONFIG_RDY_BSY 0x00000001
137 #define GPMC_CONFIG_DEV_SIZE 0x00000002
138 #define GPMC_CONFIG_DEV_TYPE 0x00000003
140 #define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31)
141 #define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30)
142 #define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29)
143 #define GPMC_CONFIG1_READTYPE_SYNC (1 << 29)
144 #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
145 #define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27)
146 #define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27)
147 #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
148 /** CLKACTIVATIONTIME Max Ticks */
149 #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
150 #define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23)
151 /** ATTACHEDDEVICEPAGELENGTH Max Value */
152 #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
153 #define GPMC_CONFIG1_WAIT_READ_MON (1 << 22)
154 #define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21)
155 #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
156 /** WAITMONITORINGTIME Max Ticks */
157 #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2
158 #define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16)
159 #define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12)
160 #define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1)
161 /** DEVICESIZE Max Value */
162 #define GPMC_CONFIG1_DEVICESIZE_MAX 1
163 #define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10)
164 #define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0)
165 #define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8)
166 #define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4)
167 #define GPMC_CONFIG1_FCLK_DIV(val) (val & 3)
168 #define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1))
169 #define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2))
170 #define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3))
171 #define GPMC_CONFIG7_CSVALID (1 << 6)
173 #define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f
174 #define GPMC_CONFIG7_CSVALID_MASK BIT(6)
175 #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
176 #define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
177 /* All CONFIG7 bits except reserved bits */
178 #define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \
179 GPMC_CONFIG7_CSVALID_MASK | \
180 GPMC_CONFIG7_MASKADDRESS_MASK)
182 #define GPMC_DEVICETYPE_NOR 0
183 #define GPMC_DEVICETYPE_NAND 2
184 #define GPMC_CONFIG_WRITEPROTECT 0x00000010
185 #define WR_RD_PIN_MONITORING 0x00600000
188 #define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */
189 #define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */
190 #define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */
192 /* XXX: Only NAND irq has been considered,currently these are the only ones used
194 #define GPMC_NR_IRQ 2
196 enum gpmc_clk_domain {
201 struct gpmc_cs_data {
204 #define GPMC_CS_RESERVED (1 << 0)
210 /* Structure to save gpmc cs context */
211 struct gpmc_cs_config {
223 * Structure to save/restore gpmc context
224 * to support core off on OMAP3
226 struct omap3_gpmc_regs {
231 u32 prefetch_config1;
232 u32 prefetch_config2;
233 u32 prefetch_control;
234 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
240 struct irq_chip irq_chip;
241 struct gpio_chip gpio_chip;
244 static struct irq_domain *gpmc_irq_domain;
246 static struct resource gpmc_mem_root;
247 static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
248 static DEFINE_SPINLOCK(gpmc_mem_lock);
249 /* Define chip-selects as reserved by default until probe completes */
250 static unsigned int gpmc_cs_num = GPMC_CS_NUM;
251 static unsigned int gpmc_nr_waitpins;
252 static resource_size_t phys_base, mem_size;
253 static unsigned gpmc_capability;
254 static void __iomem *gpmc_base;
256 static struct clk *gpmc_l3_clk;
258 static irqreturn_t gpmc_handle_irq(int irq, void *dev);
260 static void gpmc_write_reg(int idx, u32 val)
262 writel_relaxed(val, gpmc_base + idx);
265 static u32 gpmc_read_reg(int idx)
267 return readl_relaxed(gpmc_base + idx);
270 void gpmc_cs_write_reg(int cs, int idx, u32 val)
272 void __iomem *reg_addr;
274 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
275 writel_relaxed(val, reg_addr);
278 static u32 gpmc_cs_read_reg(int cs, int idx)
280 void __iomem *reg_addr;
282 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
283 return readl_relaxed(reg_addr);
286 /* TODO: Add support for gpmc_fck to clock framework and use it */
287 static unsigned long gpmc_get_fclk_period(void)
289 unsigned long rate = clk_get_rate(gpmc_l3_clk);
292 rate = 1000000000 / rate; /* In picoseconds */
298 * gpmc_get_clk_period - get period of selected clock domain in ps
299 * @cs Chip Select Region.
302 * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
303 * prior to calling this function with GPMC_CD_CLK.
305 static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
308 unsigned long tick_ps = gpmc_get_fclk_period();
314 /* get current clk divider */
315 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
316 div = (l & 0x03) + 1;
317 /* get GPMC_CLK period */
330 static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
331 enum gpmc_clk_domain cd)
333 unsigned long tick_ps;
335 /* Calculate in picosecs to yield more exact results */
336 tick_ps = gpmc_get_clk_period(cs, cd);
338 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
341 static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
343 return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
346 static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
348 unsigned long tick_ps;
350 /* Calculate in picosecs to yield more exact results */
351 tick_ps = gpmc_get_fclk_period();
353 return (time_ps + tick_ps - 1) / tick_ps;
356 unsigned int gpmc_clk_ticks_to_ns(unsigned ticks, int cs,
357 enum gpmc_clk_domain cd)
359 return ticks * gpmc_get_clk_period(cs, cd) / 1000;
362 unsigned int gpmc_ticks_to_ns(unsigned int ticks)
364 return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
367 static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
369 return ticks * gpmc_get_fclk_period();
372 static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
374 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
376 return ticks * gpmc_get_fclk_period();
379 static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
383 l = gpmc_cs_read_reg(cs, reg);
388 gpmc_cs_write_reg(cs, reg, l);
391 static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
393 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
394 GPMC_CONFIG1_TIME_PARA_GRAN,
395 p->time_para_granularity);
396 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
397 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
398 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
399 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
400 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
401 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
402 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
403 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
404 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
405 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
406 p->cycle2cyclesamecsen);
407 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
408 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
409 p->cycle2cyclediffcsen);
412 #ifdef CONFIG_OMAP_GPMC_DEBUG
414 * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
415 * @cs: Chip Select Region
416 * @reg: GPMC_CS_CONFIGn register offset.
418 * @end_bit: End Bit. Must be >= @st_bit.
419 * @ma:x Maximum parameter value (before optional @shift).
420 * If 0, maximum is as high as @st_bit and @end_bit allow.
421 * @name: DTS node name, w/o "gpmc,"
422 * @cd: Clock Domain of timing parameter.
423 * @shift: Parameter value left shifts @shift, which is then printed instead of value.
424 * @raw: Raw Format Option.
425 * raw format: gpmc,name = <value>
426 * tick format: gpmc,name = <value> /‍* x ns -- y ns; x ticks *‍/
427 * Where x ns -- y ns result in the same tick value.
428 * When @max is exceeded, "invalid" is printed inside comment.
429 * @noval: Parameter values equal to 0 are not printed.
430 * @return: Specified timing parameter (after optional @shift).
433 static int get_gpmc_timing_reg(
434 /* timing specifiers */
435 int cs, int reg, int st_bit, int end_bit, int max,
436 const char *name, const enum gpmc_clk_domain cd,
437 /* value transform */
439 /* format specifiers */
440 bool raw, bool noval)
447 l = gpmc_cs_read_reg(cs, reg);
448 nr_bits = end_bit - st_bit + 1;
449 mask = (1 << nr_bits) - 1;
450 l = (l >> st_bit) & mask;
456 if (noval && (l == 0))
459 /* DTS tick format for timings in ns */
460 unsigned int time_ns;
461 unsigned int time_ns_min = 0;
464 time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
465 time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
466 pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n",
467 name, time_ns, time_ns_min, time_ns, l,
468 invalid ? "; invalid " : " ");
471 pr_info("gpmc,%s = <%u>%s\n", name, l,
472 invalid ? " /* invalid */" : "");
478 #define GPMC_PRINT_CONFIG(cs, config) \
479 pr_info("cs%i %s: 0x%08x\n", cs, #config, \
480 gpmc_cs_read_reg(cs, config))
481 #define GPMC_GET_RAW(reg, st, end, field) \
482 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
483 #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
484 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
485 #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
486 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
487 #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
488 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
489 #define GPMC_GET_TICKS(reg, st, end, field) \
490 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
491 #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
492 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
493 #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
494 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
496 static void gpmc_show_regs(int cs, const char *desc)
498 pr_info("gpmc cs%i %s:\n", cs, desc);
499 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
500 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
501 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
502 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
503 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
504 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
508 * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
509 * see commit c9fb809.
511 static void gpmc_cs_show_timings(int cs, const char *desc)
513 gpmc_show_regs(cs, desc);
515 pr_info("gpmc cs%i access configuration:\n", cs);
516 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
517 GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
518 GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13,
519 GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
520 GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
521 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
522 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
523 GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
524 GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
526 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
527 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
528 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
529 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
530 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
532 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
534 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
536 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
537 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
539 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
540 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
542 pr_info("gpmc cs%i timings configuration:\n", cs);
543 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
544 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
545 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
547 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
548 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
549 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
550 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
551 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
552 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
553 "adv-aad-mux-rd-off-ns");
554 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
555 "adv-aad-mux-wr-off-ns");
558 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
559 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
560 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
561 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns");
562 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
564 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
565 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
567 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
568 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
569 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
571 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
573 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
574 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
576 GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
577 GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
578 "wait-monitoring-ns", GPMC_CD_CLK);
579 GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
580 GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
581 "clk-activation-ns", GPMC_CD_FCLK);
583 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
584 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
587 static inline void gpmc_cs_show_timings(int cs, const char *desc)
593 * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
594 * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
595 * prior to calling this function with @cd equal to GPMC_CD_CLK.
597 * @cs: Chip Select Region.
598 * @reg: GPMC_CS_CONFIGn register offset.
600 * @end_bit: End Bit. Must be >= @st_bit.
601 * @max: Maximum parameter value.
602 * If 0, maximum is as high as @st_bit and @end_bit allow.
603 * @time: Timing parameter in ns.
604 * @cd: Timing parameter clock domain.
605 * @name: Timing parameter name.
606 * @return: 0 on success, -1 on error.
608 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
609 int time, enum gpmc_clk_domain cd, const char *name)
612 int ticks, mask, nr_bits;
617 ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
618 nr_bits = end_bit - st_bit + 1;
619 mask = (1 << nr_bits) - 1;
625 pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
626 __func__, cs, name, time, ticks, max);
631 l = gpmc_cs_read_reg(cs, reg);
632 #ifdef CONFIG_OMAP_GPMC_DEBUG
634 "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
635 cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
636 (l >> st_bit) & mask, time);
638 l &= ~(mask << st_bit);
639 l |= ticks << st_bit;
640 gpmc_cs_write_reg(cs, reg, l);
645 #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \
646 if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
647 t->field, (cd), #field) < 0) \
650 #define GPMC_SET_ONE(reg, st, end, field) \
651 GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
654 * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
655 * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
656 * read --> don't sample bus too early
657 * write --> data is longer on bus
660 * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
661 * / waitmonitoring_ticks)
662 * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
665 * @wait_monitoring: WAITMONITORINGTIME in ns.
666 * @return: -1 on failure to scale, else proper divider > 0.
668 static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
671 int div = gpmc_ns_to_ticks(wait_monitoring);
673 div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
674 div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
686 * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
687 * @sync_clk: GPMC_CLK period in ps.
688 * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
691 int gpmc_calc_divider(unsigned int sync_clk)
693 int div = gpmc_ps_to_ticks(sync_clk);
704 * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
705 * @cs: Chip Select Region.
706 * @t: GPMC timing parameters.
707 * @s: GPMC timing settings.
708 * @return: 0 on success, -1 on error.
710 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
711 const struct gpmc_settings *s)
716 div = gpmc_calc_divider(t->sync_clk);
721 * See if we need to change the divider for waitmonitoringtime.
723 * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
724 * pure asynchronous accesses, i.e. both read and write asynchronous.
725 * However, only do so if WAITMONITORINGTIME is actually used, i.e.
726 * either WAITREADMONITORING or WAITWRITEMONITORING is set.
728 * This statement must not change div to scale async WAITMONITORINGTIME
729 * to protect mixed synchronous and asynchronous accesses.
731 * We raise an error later if WAITMONITORINGTIME does not fit.
733 if (!s->sync_read && !s->sync_write &&
734 (s->wait_on_read || s->wait_on_write)
737 div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
739 pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
747 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
748 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
749 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
751 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
752 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
753 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
754 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
755 GPMC_SET_ONE(GPMC_CS_CONFIG3, 4, 6, adv_aad_mux_on);
756 GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off);
757 GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off);
760 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
761 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
762 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
763 GPMC_SET_ONE(GPMC_CS_CONFIG4, 4, 6, oe_aad_mux_on);
764 GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off);
766 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
767 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
769 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
770 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
771 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
773 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
775 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
776 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
778 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
779 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
780 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
781 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
783 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
786 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
788 GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
789 GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
790 wait_monitoring, GPMC_CD_CLK);
791 GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
792 GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
793 clk_activation, GPMC_CD_FCLK);
795 #ifdef CONFIG_OMAP_GPMC_DEBUG
796 pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
797 cs, (div * gpmc_get_fclk_period()) / 1000, div);
800 gpmc_cs_bool_timings(cs, &t->bool_timings);
801 gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
806 static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
812 * Ensure that base address is aligned on a
813 * boundary equal to or greater than size.
815 if (base & (size - 1))
818 base >>= GPMC_CHUNK_SHIFT;
819 mask = (1 << GPMC_SECTION_SHIFT) - size;
820 mask >>= GPMC_CHUNK_SHIFT;
821 mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
823 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
824 l &= ~GPMC_CONFIG7_MASK;
825 l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
826 l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
827 l |= GPMC_CONFIG7_CSVALID;
828 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
833 static void gpmc_cs_enable_mem(int cs)
837 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
838 l |= GPMC_CONFIG7_CSVALID;
839 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
842 static void gpmc_cs_disable_mem(int cs)
846 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
847 l &= ~GPMC_CONFIG7_CSVALID;
848 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
851 static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
856 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
857 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
858 mask = (l >> 8) & 0x0f;
859 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
862 static int gpmc_cs_mem_enabled(int cs)
866 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
867 return l & GPMC_CONFIG7_CSVALID;
870 static void gpmc_cs_set_reserved(int cs, int reserved)
872 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
874 gpmc->flags |= GPMC_CS_RESERVED;
877 static bool gpmc_cs_reserved(int cs)
879 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
881 return gpmc->flags & GPMC_CS_RESERVED;
884 static void gpmc_cs_set_name(int cs, const char *name)
886 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
891 static const char *gpmc_cs_get_name(int cs)
893 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
898 static unsigned long gpmc_mem_align(unsigned long size)
902 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
903 order = GPMC_CHUNK_SHIFT - 1;
912 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
914 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
915 struct resource *res = &gpmc->mem;
918 size = gpmc_mem_align(size);
919 spin_lock(&gpmc_mem_lock);
921 res->end = base + size - 1;
922 r = request_resource(&gpmc_mem_root, res);
923 spin_unlock(&gpmc_mem_lock);
928 static int gpmc_cs_delete_mem(int cs)
930 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
931 struct resource *res = &gpmc->mem;
934 spin_lock(&gpmc_mem_lock);
935 r = release_resource(res);
938 spin_unlock(&gpmc_mem_lock);
944 * gpmc_cs_remap - remaps a chip-select physical base address
945 * @cs: chip-select to remap
946 * @base: physical base address to re-map chip-select to
948 * Re-maps a chip-select to a new physical base address specified by
949 * "base". Returns 0 on success and appropriate negative error code
952 static int gpmc_cs_remap(int cs, u32 base)
957 if (cs > gpmc_cs_num) {
958 pr_err("%s: requested chip-select is disabled\n", __func__);
963 * Make sure we ignore any device offsets from the GPMC partition
964 * allocated for the chip select and that the new base confirms
965 * to the GPMC 16MB minimum granularity.
967 base &= ~(SZ_16M - 1);
969 gpmc_cs_get_memconf(cs, &old_base, &size);
970 if (base == old_base)
973 ret = gpmc_cs_delete_mem(cs);
977 ret = gpmc_cs_insert_mem(cs, base, size);
981 ret = gpmc_cs_set_memconf(cs, base, size);
986 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
988 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
989 struct resource *res = &gpmc->mem;
992 if (cs > gpmc_cs_num) {
993 pr_err("%s: requested chip-select is disabled\n", __func__);
996 size = gpmc_mem_align(size);
997 if (size > (1 << GPMC_SECTION_SHIFT))
1000 spin_lock(&gpmc_mem_lock);
1001 if (gpmc_cs_reserved(cs)) {
1005 if (gpmc_cs_mem_enabled(cs))
1006 r = adjust_resource(res, res->start & ~(size - 1), size);
1008 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
1013 /* Disable CS while changing base address and size mask */
1014 gpmc_cs_disable_mem(cs);
1016 r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
1018 release_resource(res);
1023 gpmc_cs_enable_mem(cs);
1025 gpmc_cs_set_reserved(cs, 1);
1027 spin_unlock(&gpmc_mem_lock);
1030 EXPORT_SYMBOL(gpmc_cs_request);
1032 void gpmc_cs_free(int cs)
1034 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
1035 struct resource *res = &gpmc->mem;
1037 spin_lock(&gpmc_mem_lock);
1038 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
1039 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
1041 spin_unlock(&gpmc_mem_lock);
1044 gpmc_cs_disable_mem(cs);
1046 release_resource(res);
1047 gpmc_cs_set_reserved(cs, 0);
1048 spin_unlock(&gpmc_mem_lock);
1050 EXPORT_SYMBOL(gpmc_cs_free);
1053 * gpmc_configure - write request to configure gpmc
1054 * @cmd: command type
1055 * @wval: value to write
1056 * @return status of the operation
1058 int gpmc_configure(int cmd, int wval)
1063 case GPMC_CONFIG_WP:
1064 regval = gpmc_read_reg(GPMC_CONFIG);
1066 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
1068 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
1069 gpmc_write_reg(GPMC_CONFIG, regval);
1073 pr_err("%s: command not supported\n", __func__);
1079 EXPORT_SYMBOL(gpmc_configure);
1081 void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
1085 reg->gpmc_status = gpmc_base + GPMC_STATUS;
1086 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
1087 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
1088 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
1089 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
1090 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
1091 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
1092 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
1093 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
1094 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
1095 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
1096 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
1097 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
1098 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
1099 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
1101 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
1102 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
1104 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
1106 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
1108 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
1110 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
1112 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
1114 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
1119 static bool gpmc_nand_writebuffer_empty(void)
1121 if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
1127 static struct gpmc_nand_ops nand_ops = {
1128 .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
1132 * gpmc_omap_get_nand_ops - Get the GPMC NAND interface
1133 * @regs: the GPMC NAND register map exclusive for NAND use.
1134 * @cs: GPMC chip select number on which the NAND sits. The
1135 * register map returned will be specific to this chip select.
1137 * Returns NULL on error e.g. invalid cs.
1139 struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
1141 if (cs >= gpmc_cs_num)
1144 gpmc_update_nand_reg(reg, cs);
1148 EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
1150 int gpmc_get_client_irq(unsigned irq_config)
1152 if (!gpmc_irq_domain) {
1153 pr_warn("%s called before GPMC IRQ domain available\n",
1158 if (irq_config >= GPMC_NR_IRQ)
1161 return irq_create_mapping(gpmc_irq_domain, irq_config);
1164 static int gpmc_irq_endis(unsigned long hwirq, bool endis)
1168 regval = gpmc_read_reg(GPMC_IRQENABLE);
1170 regval |= BIT(hwirq);
1172 regval &= ~BIT(hwirq);
1173 gpmc_write_reg(GPMC_IRQENABLE, regval);
1178 static void gpmc_irq_disable(struct irq_data *p)
1180 gpmc_irq_endis(p->hwirq, false);
1183 static void gpmc_irq_enable(struct irq_data *p)
1185 gpmc_irq_endis(p->hwirq, true);
1188 static void gpmc_irq_noop(struct irq_data *data) { }
1190 static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
1192 static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
1195 struct gpmc_device *gpmc = d->host_data;
1197 irq_set_chip_data(virq, gpmc);
1198 irq_set_chip_and_handler(virq, &gpmc->irq_chip, handle_simple_irq);
1199 irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
1204 static const struct irq_domain_ops gpmc_irq_domain_ops = {
1205 .map = gpmc_irq_map,
1206 .xlate = irq_domain_xlate_twocell,
1209 static irqreturn_t gpmc_handle_irq(int irq, void *data)
1213 struct gpmc_device *gpmc = data;
1215 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1220 for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++) {
1221 if (regval & BIT(hwirq)) {
1222 virq = irq_find_mapping(gpmc_irq_domain, hwirq);
1225 "spurious irq detected hwirq %d, virq %d\n",
1229 generic_handle_irq(virq);
1233 gpmc_write_reg(GPMC_IRQSTATUS, regval);
1238 static int gpmc_setup_irq(struct gpmc_device *gpmc)
1243 /* Disable interrupts */
1244 gpmc_write_reg(GPMC_IRQENABLE, 0);
1246 /* clear interrupts */
1247 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1248 gpmc_write_reg(GPMC_IRQSTATUS, regval);
1250 gpmc->irq_chip.name = "gpmc";
1251 gpmc->irq_chip.irq_startup = gpmc_irq_noop_ret;
1252 gpmc->irq_chip.irq_enable = gpmc_irq_enable;
1253 gpmc->irq_chip.irq_disable = gpmc_irq_disable;
1254 gpmc->irq_chip.irq_shutdown = gpmc_irq_noop;
1255 gpmc->irq_chip.irq_ack = gpmc_irq_noop;
1256 gpmc->irq_chip.irq_mask = gpmc_irq_noop;
1257 gpmc->irq_chip.irq_unmask = gpmc_irq_noop;
1259 gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
1261 &gpmc_irq_domain_ops,
1263 if (!gpmc_irq_domain) {
1264 dev_err(gpmc->dev, "IRQ domain add failed\n");
1268 rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
1270 dev_err(gpmc->dev, "failed to request irq %d: %d\n",
1272 irq_domain_remove(gpmc_irq_domain);
1273 gpmc_irq_domain = NULL;
1279 static int gpmc_free_irq(struct gpmc_device *gpmc)
1283 free_irq(gpmc->irq, gpmc);
1285 for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++)
1286 irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
1288 irq_domain_remove(gpmc_irq_domain);
1289 gpmc_irq_domain = NULL;
1294 static void gpmc_mem_exit(void)
1298 for (cs = 0; cs < gpmc_cs_num; cs++) {
1299 if (!gpmc_cs_mem_enabled(cs))
1301 gpmc_cs_delete_mem(cs);
1306 static void gpmc_mem_init(void)
1310 gpmc_mem_root.start = GPMC_MEM_START;
1311 gpmc_mem_root.end = GPMC_MEM_END;
1313 /* Reserve all regions that has been set up by bootloader */
1314 for (cs = 0; cs < gpmc_cs_num; cs++) {
1317 if (!gpmc_cs_mem_enabled(cs))
1319 gpmc_cs_get_memconf(cs, &base, &size);
1320 if (gpmc_cs_insert_mem(cs, base, size)) {
1321 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
1322 __func__, cs, base, base + size);
1323 gpmc_cs_disable_mem(cs);
1328 static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
1333 div = gpmc_calc_divider(sync_clk);
1334 temp = gpmc_ps_to_ticks(time_ps);
1335 temp = (temp + div - 1) / div;
1336 return gpmc_ticks_to_ps(temp * div);
1339 /* XXX: can the cycles be avoided ? */
1340 static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
1341 struct gpmc_device_timings *dev_t,
1347 temp = dev_t->t_avdp_r;
1348 /* XXX: mux check required ? */
1350 /* XXX: t_avdp not to be required for sync, only added for tusb
1351 * this indirectly necessitates requirement of t_avdp_r and
1352 * t_avdp_w instead of having a single t_avdp
1354 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
1355 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1357 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1360 temp = dev_t->t_oeasu; /* XXX: remove this ? */
1362 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
1363 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
1364 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
1366 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1369 /* XXX: any scope for improvement ?, by combining oe_on
1370 * and clk_activation, need to check whether
1371 * access = clk_activation + round to sync clk ?
1373 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
1374 temp += gpmc_t->clk_activation;
1376 temp = max_t(u32, temp, gpmc_t->oe_on +
1377 gpmc_ticks_to_ps(dev_t->cyc_oe));
1378 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1380 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1381 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1384 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
1385 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
1387 /* XXX: barter t_ce_rdyz with t_cez_r ? */
1388 if (dev_t->t_ce_rdyz)
1389 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
1390 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1395 static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
1396 struct gpmc_device_timings *dev_t,
1402 temp = dev_t->t_avdp_w;
1404 temp = max_t(u32, temp,
1405 gpmc_t->clk_activation + dev_t->t_avdh);
1406 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1408 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1410 /* wr_data_mux_bus */
1411 temp = max_t(u32, dev_t->t_weasu,
1412 gpmc_t->clk_activation + dev_t->t_rdyo);
1413 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
1414 * and in that case remember to handle we_on properly
1417 temp = max_t(u32, temp,
1418 gpmc_t->adv_wr_off + dev_t->t_aavdh);
1419 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1420 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1422 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1425 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1426 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1428 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1431 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
1432 gpmc_t->wr_access = gpmc_t->access;
1435 temp = gpmc_t->we_on + dev_t->t_wpl;
1436 temp = max_t(u32, temp,
1437 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
1438 temp = max_t(u32, temp,
1439 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
1440 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1442 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1446 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
1447 temp += gpmc_t->wr_access;
1448 /* XXX: barter t_ce_rdyz with t_cez_w ? */
1449 if (dev_t->t_ce_rdyz)
1450 temp = max_t(u32, temp,
1451 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
1452 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1457 static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
1458 struct gpmc_device_timings *dev_t,
1464 temp = dev_t->t_avdp_r;
1466 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1467 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1470 temp = dev_t->t_oeasu;
1472 temp = max_t(u32, temp,
1473 gpmc_t->adv_rd_off + dev_t->t_aavdh);
1474 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1477 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1478 gpmc_t->oe_on + dev_t->t_oe);
1479 temp = max_t(u32, temp,
1480 gpmc_t->cs_on + dev_t->t_ce);
1481 temp = max_t(u32, temp,
1482 gpmc_t->adv_on + dev_t->t_aa);
1483 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1485 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1486 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1489 temp = max_t(u32, dev_t->t_rd_cycle,
1490 gpmc_t->cs_rd_off + dev_t->t_cez_r);
1491 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1492 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1497 static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
1498 struct gpmc_device_timings *dev_t,
1504 temp = dev_t->t_avdp_w;
1506 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1507 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1509 /* wr_data_mux_bus */
1510 temp = dev_t->t_weasu;
1512 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1513 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1514 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1516 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1519 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1520 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1522 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1525 temp = gpmc_t->we_on + dev_t->t_wpl;
1526 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1528 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1532 temp = max_t(u32, dev_t->t_wr_cycle,
1533 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1534 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1539 static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1540 struct gpmc_device_timings *dev_t)
1544 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1545 gpmc_get_fclk_period();
1547 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1551 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1552 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1554 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1557 if (dev_t->ce_xdelay)
1558 gpmc_t->bool_timings.cs_extra_delay = true;
1559 if (dev_t->avd_xdelay)
1560 gpmc_t->bool_timings.adv_extra_delay = true;
1561 if (dev_t->oe_xdelay)
1562 gpmc_t->bool_timings.oe_extra_delay = true;
1563 if (dev_t->we_xdelay)
1564 gpmc_t->bool_timings.we_extra_delay = true;
1569 static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
1570 struct gpmc_device_timings *dev_t,
1576 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1579 temp = dev_t->t_avdasu;
1580 if (dev_t->t_ce_avd)
1581 temp = max_t(u32, temp,
1582 gpmc_t->cs_on + dev_t->t_ce_avd);
1583 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1586 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1591 /* TODO: remove this function once all peripherals are confirmed to
1592 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1593 * has to be modified to handle timings in ps instead of ns
1595 static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1598 t->cs_rd_off /= 1000;
1599 t->cs_wr_off /= 1000;
1601 t->adv_rd_off /= 1000;
1602 t->adv_wr_off /= 1000;
1607 t->page_burst_access /= 1000;
1609 t->rd_cycle /= 1000;
1610 t->wr_cycle /= 1000;
1611 t->bus_turnaround /= 1000;
1612 t->cycle2cycle_delay /= 1000;
1613 t->wait_monitoring /= 1000;
1614 t->clk_activation /= 1000;
1615 t->wr_access /= 1000;
1616 t->wr_data_mux_bus /= 1000;
1619 int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
1620 struct gpmc_settings *gpmc_s,
1621 struct gpmc_device_timings *dev_t)
1623 bool mux = false, sync = false;
1626 mux = gpmc_s->mux_add_data ? true : false;
1627 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1630 memset(gpmc_t, 0, sizeof(*gpmc_t));
1632 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
1634 if (gpmc_s && gpmc_s->sync_read)
1635 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
1637 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
1639 if (gpmc_s && gpmc_s->sync_write)
1640 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
1642 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
1644 /* TODO: remove, see function definition */
1645 gpmc_convert_ps_to_ns(gpmc_t);
1651 * gpmc_cs_program_settings - programs non-timing related settings
1652 * @cs: GPMC chip-select to program
1653 * @p: pointer to GPMC settings structure
1655 * Programs non-timing related settings for a GPMC chip-select, such as
1656 * bus-width, burst configuration, etc. Function should be called once
1657 * for each chip-select that is being used and must be called before
1658 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1659 * register will be initialised to zero by this function. Returns 0 on
1660 * success and appropriate negative error code on failure.
1662 int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1666 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1667 pr_err("%s: invalid width %d!", __func__, p->device_width);
1671 /* Address-data multiplexing not supported for NAND devices */
1672 if (p->device_nand && p->mux_add_data) {
1673 pr_err("%s: invalid configuration!\n", __func__);
1677 if ((p->mux_add_data > GPMC_MUX_AD) ||
1678 ((p->mux_add_data == GPMC_MUX_AAD) &&
1679 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1680 pr_err("%s: invalid multiplex configuration!\n", __func__);
1684 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1685 if (p->burst_read || p->burst_write) {
1686 switch (p->burst_len) {
1692 pr_err("%s: invalid page/burst-length (%d)\n",
1693 __func__, p->burst_len);
1698 if (p->wait_pin > gpmc_nr_waitpins) {
1699 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1703 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1706 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1708 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1709 if (p->wait_on_read)
1710 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1711 if (p->wait_on_write)
1712 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1713 if (p->wait_on_read || p->wait_on_write)
1714 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1716 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1717 if (p->mux_add_data)
1718 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1720 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1722 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1723 if (p->burst_read || p->burst_write) {
1724 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1725 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1728 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1734 static const struct of_device_id gpmc_dt_ids[] = {
1735 { .compatible = "ti,omap2420-gpmc" },
1736 { .compatible = "ti,omap2430-gpmc" },
1737 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1738 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1739 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1742 MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1745 * gpmc_read_settings_dt - read gpmc settings from device-tree
1746 * @np: pointer to device-tree node for a gpmc child device
1747 * @p: pointer to gpmc settings structure
1749 * Reads the GPMC settings for a GPMC child device from device-tree and
1750 * stores them in the GPMC settings structure passed. The GPMC settings
1751 * structure is initialised to zero by this function and so any
1752 * previously stored settings will be cleared.
1754 void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1756 memset(p, 0, sizeof(struct gpmc_settings));
1758 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1759 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
1760 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1761 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1763 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1764 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1765 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1766 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1767 if (!p->burst_read && !p->burst_write)
1768 pr_warn("%s: page/burst-length set but not used!\n",
1772 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1773 p->wait_on_read = of_property_read_bool(np,
1774 "gpmc,wait-on-read");
1775 p->wait_on_write = of_property_read_bool(np,
1776 "gpmc,wait-on-write");
1777 if (!p->wait_on_read && !p->wait_on_write)
1778 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1783 static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1784 struct gpmc_timings *gpmc_t)
1786 struct gpmc_bool_timings *p;
1791 memset(gpmc_t, 0, sizeof(*gpmc_t));
1793 /* minimum clock period for syncronous mode */
1794 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
1796 /* chip select timtings */
1797 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1798 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1799 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
1801 /* ADV signal timings */
1802 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1803 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1804 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
1805 of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
1806 &gpmc_t->adv_aad_mux_on);
1807 of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
1808 &gpmc_t->adv_aad_mux_rd_off);
1809 of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
1810 &gpmc_t->adv_aad_mux_wr_off);
1812 /* WE signal timings */
1813 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1814 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
1816 /* OE signal timings */
1817 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1818 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
1819 of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
1820 &gpmc_t->oe_aad_mux_on);
1821 of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
1822 &gpmc_t->oe_aad_mux_off);
1824 /* access and cycle timings */
1825 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1826 &gpmc_t->page_burst_access);
1827 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1828 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1829 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1830 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1831 &gpmc_t->bus_turnaround);
1832 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1833 &gpmc_t->cycle2cycle_delay);
1834 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1835 &gpmc_t->wait_monitoring);
1836 of_property_read_u32(np, "gpmc,clk-activation-ns",
1837 &gpmc_t->clk_activation);
1839 /* only applicable to OMAP3+ */
1840 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1841 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1842 &gpmc_t->wr_data_mux_bus);
1844 /* bool timing parameters */
1845 p = &gpmc_t->bool_timings;
1847 p->cycle2cyclediffcsen =
1848 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1849 p->cycle2cyclesamecsen =
1850 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1851 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1852 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1853 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1854 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1855 p->time_para_granularity =
1856 of_property_read_bool(np, "gpmc,time-para-granularity");
1859 #if IS_ENABLED(CONFIG_MTD_ONENAND)
1860 static int gpmc_probe_onenand_child(struct platform_device *pdev,
1861 struct device_node *child)
1864 struct omap_onenand_platform_data *gpmc_onenand_data;
1866 if (of_property_read_u32(child, "reg", &val) < 0) {
1867 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1872 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1874 if (!gpmc_onenand_data)
1877 gpmc_onenand_data->cs = val;
1878 gpmc_onenand_data->of_node = child;
1879 gpmc_onenand_data->dma_channel = -1;
1881 if (!of_property_read_u32(child, "dma-channel", &val))
1882 gpmc_onenand_data->dma_channel = val;
1884 gpmc_onenand_init(gpmc_onenand_data);
1889 static int gpmc_probe_onenand_child(struct platform_device *pdev,
1890 struct device_node *child)
1897 * gpmc_probe_generic_child - configures the gpmc for a child device
1898 * @pdev: pointer to gpmc platform device
1899 * @child: pointer to device-tree node for child device
1901 * Allocates and configures a GPMC chip-select for a child device.
1902 * Returns 0 on success and appropriate negative error code on failure.
1904 static int gpmc_probe_generic_child(struct platform_device *pdev,
1905 struct device_node *child)
1907 struct gpmc_settings gpmc_s;
1908 struct gpmc_timings gpmc_t;
1909 struct resource res;
1915 if (of_property_read_u32(child, "reg", &cs) < 0) {
1916 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1921 if (of_address_to_resource(child, 0, &res) < 0) {
1922 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1928 * Check if we have multiple instances of the same device
1929 * on a single chip select. If so, use the already initialized
1932 name = gpmc_cs_get_name(cs);
1933 if (name && child->name && of_node_cmp(child->name, name) == 0)
1936 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1938 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1941 gpmc_cs_set_name(cs, child->name);
1943 gpmc_read_settings_dt(child, &gpmc_s);
1944 gpmc_read_timings_dt(child, &gpmc_t);
1947 * For some GPMC devices we still need to rely on the bootloader
1948 * timings because the devices can be connected via FPGA.
1949 * REVISIT: Add timing support from slls644g.pdf.
1951 if (!gpmc_t.cs_rd_off) {
1952 WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
1954 gpmc_cs_show_timings(cs,
1955 "please add GPMC bootloader timings to .dts");
1959 /* CS must be disabled while making changes to gpmc configuration */
1960 gpmc_cs_disable_mem(cs);
1963 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1964 * location in the gpmc address space. When booting with
1965 * device-tree we want the NOR flash to be mapped to the
1966 * location specified in the device-tree blob. So remap the
1967 * CS to this location. Once DT migration is complete should
1968 * just make gpmc_cs_request() map a specific address.
1970 ret = gpmc_cs_remap(cs, res.start);
1972 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1974 if (res.start < GPMC_MEM_START) {
1975 dev_info(&pdev->dev,
1976 "GPMC CS %d start cannot be lesser than 0x%x\n",
1977 cs, GPMC_MEM_START);
1978 } else if (res.end > GPMC_MEM_END) {
1979 dev_info(&pdev->dev,
1980 "GPMC CS %d end cannot be greater than 0x%x\n",
1986 if (of_node_cmp(child->name, "nand") == 0) {
1987 /* Warn about older DT blobs with no compatible property */
1988 if (!of_property_read_bool(child, "compatible")) {
1989 dev_warn(&pdev->dev,
1990 "Incompatible NAND node: missing compatible");
1996 if (of_device_is_compatible(child, "ti,omap2-nand")) {
1997 /* NAND specific setup */
1998 val = of_get_nand_bus_width(child);
2001 gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
2004 gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
2007 dev_err(&pdev->dev, "%s: invalid 'nand-bus-width'\n",
2013 /* disable write protect */
2014 gpmc_configure(GPMC_CONFIG_WP, 0);
2015 gpmc_s.device_nand = true;
2017 ret = of_property_read_u32(child, "bank-width",
2018 &gpmc_s.device_width);
2023 gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
2024 ret = gpmc_cs_program_settings(cs, &gpmc_s);
2028 ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
2030 dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
2035 /* Clear limited address i.e. enable A26-A11 */
2036 val = gpmc_read_reg(GPMC_CONFIG);
2037 val &= ~GPMC_CONFIG_LIMITEDADDRESS;
2038 gpmc_write_reg(GPMC_CONFIG, val);
2040 /* Enable CS region */
2041 gpmc_cs_enable_mem(cs);
2045 /* create platform device, NULL on error or when disabled */
2046 if (!of_platform_device_create(child, NULL, &pdev->dev))
2047 goto err_child_fail;
2049 /* is child a common bus? */
2050 if (of_match_node(of_default_bus_match_table, child))
2051 /* create children and other common bus children */
2052 if (of_platform_populate(child, of_default_bus_match_table,
2054 goto err_child_fail;
2060 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
2069 static int gpmc_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
2071 return 1; /* we're input only */
2074 static int gpmc_gpio_direction_input(struct gpio_chip *chip,
2075 unsigned int offset)
2077 return 0; /* we're input only */
2080 static int gpmc_gpio_direction_output(struct gpio_chip *chip,
2081 unsigned int offset, int value)
2083 return -EINVAL; /* we're input only */
2086 static void gpmc_gpio_set(struct gpio_chip *chip, unsigned int offset,
2091 static int gpmc_gpio_get(struct gpio_chip *chip, unsigned int offset)
2097 reg = gpmc_read_reg(GPMC_STATUS) & BIT(offset);
2102 static int gpmc_gpio_init(struct gpmc_device *gpmc)
2106 gpmc->gpio_chip.parent = gpmc->dev;
2107 gpmc->gpio_chip.owner = THIS_MODULE;
2108 gpmc->gpio_chip.label = DEVICE_NAME;
2109 gpmc->gpio_chip.ngpio = gpmc_nr_waitpins;
2110 gpmc->gpio_chip.get_direction = gpmc_gpio_get_direction;
2111 gpmc->gpio_chip.direction_input = gpmc_gpio_direction_input;
2112 gpmc->gpio_chip.direction_output = gpmc_gpio_direction_output;
2113 gpmc->gpio_chip.set = gpmc_gpio_set;
2114 gpmc->gpio_chip.get = gpmc_gpio_get;
2115 gpmc->gpio_chip.base = -1;
2117 ret = gpiochip_add(&gpmc->gpio_chip);
2119 dev_err(gpmc->dev, "could not register gpio chip: %d\n", ret);
2126 static void gpmc_gpio_exit(struct gpmc_device *gpmc)
2128 gpiochip_remove(&gpmc->gpio_chip);
2131 static int gpmc_probe_dt(struct platform_device *pdev)
2134 const struct of_device_id *of_id =
2135 of_match_device(gpmc_dt_ids, &pdev->dev);
2140 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
2143 pr_err("%s: number of chip-selects not defined\n", __func__);
2145 } else if (gpmc_cs_num < 1) {
2146 pr_err("%s: all chip-selects are disabled\n", __func__);
2148 } else if (gpmc_cs_num > GPMC_CS_NUM) {
2149 pr_err("%s: number of supported chip-selects cannot be > %d\n",
2150 __func__, GPMC_CS_NUM);
2154 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
2157 pr_err("%s: number of wait pins not found!\n", __func__);
2164 static int gpmc_probe_dt_children(struct platform_device *pdev)
2167 struct device_node *child;
2169 for_each_available_child_of_node(pdev->dev.of_node, child) {
2174 if (of_node_cmp(child->name, "onenand") == 0)
2175 ret = gpmc_probe_onenand_child(pdev, child);
2177 ret = gpmc_probe_generic_child(pdev, child);
2186 static int gpmc_probe_dt(struct platform_device *pdev)
2191 static int gpmc_probe_dt_children(struct platform_device *pdev)
2197 static int gpmc_probe(struct platform_device *pdev)
2201 struct resource *res;
2202 struct gpmc_device *gpmc;
2204 gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
2208 gpmc->dev = &pdev->dev;
2209 platform_set_drvdata(pdev, gpmc);
2211 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2215 phys_base = res->start;
2216 mem_size = resource_size(res);
2218 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
2219 if (IS_ERR(gpmc_base))
2220 return PTR_ERR(gpmc_base);
2222 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2224 dev_err(&pdev->dev, "Failed to get resource: irq\n");
2228 gpmc->irq = res->start;
2230 gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
2231 if (IS_ERR(gpmc_l3_clk)) {
2232 dev_err(&pdev->dev, "Failed to get GPMC fck\n");
2233 return PTR_ERR(gpmc_l3_clk);
2236 if (!clk_get_rate(gpmc_l3_clk)) {
2237 dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
2241 if (pdev->dev.of_node) {
2242 rc = gpmc_probe_dt(pdev);
2246 gpmc_cs_num = GPMC_CS_NUM;
2247 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
2250 pm_runtime_enable(&pdev->dev);
2251 pm_runtime_get_sync(&pdev->dev);
2253 l = gpmc_read_reg(GPMC_REVISION);
2256 * FIXME: Once device-tree migration is complete the below flags
2257 * should be populated based upon the device-tree compatible
2258 * string. For now just use the IP revision. OMAP3+ devices have
2259 * the wr_access and wr_data_mux_bus register fields. OMAP4+
2260 * devices support the addr-addr-data multiplex protocol.
2262 * GPMC IP revisions:
2265 * - OMAP44xx/54xx/AM335x = 6.0
2267 if (GPMC_REVISION_MAJOR(l) > 0x4)
2268 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
2269 if (GPMC_REVISION_MAJOR(l) > 0x5)
2270 gpmc_capability |= GPMC_HAS_MUX_AAD;
2271 dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
2272 GPMC_REVISION_MINOR(l));
2275 rc = gpmc_gpio_init(gpmc);
2277 goto gpio_init_failed;
2279 rc = gpmc_setup_irq(gpmc);
2281 dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
2282 goto setup_irq_failed;
2285 rc = gpmc_probe_dt_children(pdev);
2287 dev_err(gpmc->dev, "failed to probe DT children\n");
2288 goto dt_children_failed;
2294 gpmc_free_irq(gpmc);
2296 gpmc_gpio_exit(gpmc);
2299 pm_runtime_put_sync(&pdev->dev);
2300 pm_runtime_disable(&pdev->dev);
2305 static int gpmc_remove(struct platform_device *pdev)
2307 struct gpmc_device *gpmc = platform_get_drvdata(pdev);
2309 gpmc_free_irq(gpmc);
2310 gpmc_gpio_exit(gpmc);
2312 pm_runtime_put_sync(&pdev->dev);
2313 pm_runtime_disable(&pdev->dev);
2318 #ifdef CONFIG_PM_SLEEP
2319 static int gpmc_suspend(struct device *dev)
2321 omap3_gpmc_save_context();
2322 pm_runtime_put_sync(dev);
2326 static int gpmc_resume(struct device *dev)
2328 pm_runtime_get_sync(dev);
2329 omap3_gpmc_restore_context();
2334 static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
2336 static struct platform_driver gpmc_driver = {
2337 .probe = gpmc_probe,
2338 .remove = gpmc_remove,
2340 .name = DEVICE_NAME,
2341 .of_match_table = of_match_ptr(gpmc_dt_ids),
2346 static __init int gpmc_init(void)
2348 return platform_driver_register(&gpmc_driver);
2351 static __exit void gpmc_exit(void)
2353 platform_driver_unregister(&gpmc_driver);
2357 postcore_initcall(gpmc_init);
2358 module_exit(gpmc_exit);
2360 static struct omap3_gpmc_regs gpmc_context;
2362 void omap3_gpmc_save_context(void)
2369 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
2370 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
2371 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
2372 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
2373 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
2374 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
2375 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
2376 for (i = 0; i < gpmc_cs_num; i++) {
2377 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
2378 if (gpmc_context.cs_context[i].is_valid) {
2379 gpmc_context.cs_context[i].config1 =
2380 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
2381 gpmc_context.cs_context[i].config2 =
2382 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
2383 gpmc_context.cs_context[i].config3 =
2384 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
2385 gpmc_context.cs_context[i].config4 =
2386 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
2387 gpmc_context.cs_context[i].config5 =
2388 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
2389 gpmc_context.cs_context[i].config6 =
2390 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
2391 gpmc_context.cs_context[i].config7 =
2392 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
2397 void omap3_gpmc_restore_context(void)
2404 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
2405 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
2406 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
2407 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
2408 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
2409 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
2410 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
2411 for (i = 0; i < gpmc_cs_num; i++) {
2412 if (gpmc_context.cs_context[i].is_valid) {
2413 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
2414 gpmc_context.cs_context[i].config1);
2415 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
2416 gpmc_context.cs_context[i].config2);
2417 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
2418 gpmc_context.cs_context[i].config3);
2419 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
2420 gpmc_context.cs_context[i].config4);
2421 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
2422 gpmc_context.cs_context[i].config5);
2423 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
2424 gpmc_context.cs_context[i].config6);
2425 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
2426 gpmc_context.cs_context[i].config7);