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/interrupt.h>
25 #include <linux/irqdomain.h>
26 #include <linux/platform_device.h>
28 #include <linux/of_address.h>
29 #include <linux/of_mtd.h>
30 #include <linux/of_device.h>
31 #include <linux/of_platform.h>
32 #include <linux/omap-gpmc.h>
33 #include <linux/mtd/nand.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
98 #define GPMC_MEM_END 0x3FFFFFFF
100 #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
101 #define GPMC_SECTION_SHIFT 28 /* 128 MB */
103 #define CS_NUM_SHIFT 24
104 #define ENABLE_PREFETCH (0x1 << 7)
105 #define DMA_MPU_MODE 2
107 #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
108 #define GPMC_REVISION_MINOR(l) (l & 0xf)
110 #define GPMC_HAS_WR_ACCESS 0x1
111 #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
112 #define GPMC_HAS_MUX_AAD 0x4
114 #define GPMC_NR_WAITPINS 4
116 #define GPMC_CS_CONFIG1 0x00
117 #define GPMC_CS_CONFIG2 0x04
118 #define GPMC_CS_CONFIG3 0x08
119 #define GPMC_CS_CONFIG4 0x0c
120 #define GPMC_CS_CONFIG5 0x10
121 #define GPMC_CS_CONFIG6 0x14
122 #define GPMC_CS_CONFIG7 0x18
123 #define GPMC_CS_NAND_COMMAND 0x1c
124 #define GPMC_CS_NAND_ADDRESS 0x20
125 #define GPMC_CS_NAND_DATA 0x24
127 /* Control Commands */
128 #define GPMC_CONFIG_RDY_BSY 0x00000001
129 #define GPMC_CONFIG_DEV_SIZE 0x00000002
130 #define GPMC_CONFIG_DEV_TYPE 0x00000003
132 #define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31)
133 #define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30)
134 #define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29)
135 #define GPMC_CONFIG1_READTYPE_SYNC (1 << 29)
136 #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
137 #define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27)
138 #define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27)
139 #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
140 /** CLKACTIVATIONTIME Max Ticks */
141 #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
142 #define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23)
143 /** ATTACHEDDEVICEPAGELENGTH Max Value */
144 #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
145 #define GPMC_CONFIG1_WAIT_READ_MON (1 << 22)
146 #define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21)
147 #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
148 /** WAITMONITORINGTIME Max Ticks */
149 #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2
150 #define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16)
151 #define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12)
152 #define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1)
153 /** DEVICESIZE Max Value */
154 #define GPMC_CONFIG1_DEVICESIZE_MAX 1
155 #define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10)
156 #define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0)
157 #define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8)
158 #define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4)
159 #define GPMC_CONFIG1_FCLK_DIV(val) (val & 3)
160 #define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1))
161 #define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2))
162 #define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3))
163 #define GPMC_CONFIG7_CSVALID (1 << 6)
165 #define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f
166 #define GPMC_CONFIG7_CSVALID_MASK BIT(6)
167 #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
168 #define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
169 /* All CONFIG7 bits except reserved bits */
170 #define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \
171 GPMC_CONFIG7_CSVALID_MASK | \
172 GPMC_CONFIG7_MASKADDRESS_MASK)
174 #define GPMC_DEVICETYPE_NOR 0
175 #define GPMC_DEVICETYPE_NAND 2
176 #define GPMC_CONFIG_WRITEPROTECT 0x00000010
177 #define WR_RD_PIN_MONITORING 0x00600000
180 #define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */
181 #define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */
182 #define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */
184 /* XXX: Only NAND irq has been considered,currently these are the only ones used
186 #define GPMC_NR_IRQ 2
188 enum gpmc_clk_domain {
193 struct gpmc_cs_data {
196 #define GPMC_CS_RESERVED (1 << 0)
202 /* Structure to save gpmc cs context */
203 struct gpmc_cs_config {
215 * Structure to save/restore gpmc context
216 * to support core off on OMAP3
218 struct omap3_gpmc_regs {
223 u32 prefetch_config1;
224 u32 prefetch_config2;
225 u32 prefetch_control;
226 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
232 struct irq_chip irq_chip;
235 static struct irq_domain *gpmc_irq_domain;
237 static struct resource gpmc_mem_root;
238 static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
239 static DEFINE_SPINLOCK(gpmc_mem_lock);
240 /* Define chip-selects as reserved by default until probe completes */
241 static unsigned int gpmc_cs_num = GPMC_CS_NUM;
242 static unsigned int gpmc_nr_waitpins;
243 static resource_size_t phys_base, mem_size;
244 static unsigned gpmc_capability;
245 static void __iomem *gpmc_base;
247 static struct clk *gpmc_l3_clk;
249 static irqreturn_t gpmc_handle_irq(int irq, void *dev);
251 static void gpmc_write_reg(int idx, u32 val)
253 writel_relaxed(val, gpmc_base + idx);
256 static u32 gpmc_read_reg(int idx)
258 return readl_relaxed(gpmc_base + idx);
261 void gpmc_cs_write_reg(int cs, int idx, u32 val)
263 void __iomem *reg_addr;
265 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
266 writel_relaxed(val, reg_addr);
269 static u32 gpmc_cs_read_reg(int cs, int idx)
271 void __iomem *reg_addr;
273 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
274 return readl_relaxed(reg_addr);
277 /* TODO: Add support for gpmc_fck to clock framework and use it */
278 static unsigned long gpmc_get_fclk_period(void)
280 unsigned long rate = clk_get_rate(gpmc_l3_clk);
283 rate = 1000000000 / rate; /* In picoseconds */
289 * gpmc_get_clk_period - get period of selected clock domain in ps
290 * @cs Chip Select Region.
293 * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
294 * prior to calling this function with GPMC_CD_CLK.
296 static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
299 unsigned long tick_ps = gpmc_get_fclk_period();
305 /* get current clk divider */
306 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
307 div = (l & 0x03) + 1;
308 /* get GPMC_CLK period */
321 static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
322 enum gpmc_clk_domain cd)
324 unsigned long tick_ps;
326 /* Calculate in picosecs to yield more exact results */
327 tick_ps = gpmc_get_clk_period(cs, cd);
329 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
332 static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
334 return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
337 static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
339 unsigned long tick_ps;
341 /* Calculate in picosecs to yield more exact results */
342 tick_ps = gpmc_get_fclk_period();
344 return (time_ps + tick_ps - 1) / tick_ps;
347 unsigned int gpmc_clk_ticks_to_ns(unsigned ticks, int cs,
348 enum gpmc_clk_domain cd)
350 return ticks * gpmc_get_clk_period(cs, cd) / 1000;
353 unsigned int gpmc_ticks_to_ns(unsigned int ticks)
355 return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
358 static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
360 return ticks * gpmc_get_fclk_period();
363 static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
365 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
367 return ticks * gpmc_get_fclk_period();
370 static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
374 l = gpmc_cs_read_reg(cs, reg);
379 gpmc_cs_write_reg(cs, reg, l);
382 static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
384 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
385 GPMC_CONFIG1_TIME_PARA_GRAN,
386 p->time_para_granularity);
387 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
388 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
389 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
390 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
391 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
392 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
393 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
394 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
395 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
396 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
397 p->cycle2cyclesamecsen);
398 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
399 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
400 p->cycle2cyclediffcsen);
403 #ifdef CONFIG_OMAP_GPMC_DEBUG
405 * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
406 * @cs: Chip Select Region
407 * @reg: GPMC_CS_CONFIGn register offset.
409 * @end_bit: End Bit. Must be >= @st_bit.
410 * @ma:x Maximum parameter value (before optional @shift).
411 * If 0, maximum is as high as @st_bit and @end_bit allow.
412 * @name: DTS node name, w/o "gpmc,"
413 * @cd: Clock Domain of timing parameter.
414 * @shift: Parameter value left shifts @shift, which is then printed instead of value.
415 * @raw: Raw Format Option.
416 * raw format: gpmc,name = <value>
417 * tick format: gpmc,name = <value> /‍* x ns -- y ns; x ticks *‍/
418 * Where x ns -- y ns result in the same tick value.
419 * When @max is exceeded, "invalid" is printed inside comment.
420 * @noval: Parameter values equal to 0 are not printed.
421 * @return: Specified timing parameter (after optional @shift).
424 static int get_gpmc_timing_reg(
425 /* timing specifiers */
426 int cs, int reg, int st_bit, int end_bit, int max,
427 const char *name, const enum gpmc_clk_domain cd,
428 /* value transform */
430 /* format specifiers */
431 bool raw, bool noval)
438 l = gpmc_cs_read_reg(cs, reg);
439 nr_bits = end_bit - st_bit + 1;
440 mask = (1 << nr_bits) - 1;
441 l = (l >> st_bit) & mask;
447 if (noval && (l == 0))
450 /* DTS tick format for timings in ns */
451 unsigned int time_ns;
452 unsigned int time_ns_min = 0;
455 time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
456 time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
457 pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n",
458 name, time_ns, time_ns_min, time_ns, l,
459 invalid ? "; invalid " : " ");
462 pr_info("gpmc,%s = <%u>%s\n", name, l,
463 invalid ? " /* invalid */" : "");
469 #define GPMC_PRINT_CONFIG(cs, config) \
470 pr_info("cs%i %s: 0x%08x\n", cs, #config, \
471 gpmc_cs_read_reg(cs, config))
472 #define GPMC_GET_RAW(reg, st, end, field) \
473 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
474 #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
475 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
476 #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
477 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
478 #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
479 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
480 #define GPMC_GET_TICKS(reg, st, end, field) \
481 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
482 #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
483 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
484 #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
485 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
487 static void gpmc_show_regs(int cs, const char *desc)
489 pr_info("gpmc cs%i %s:\n", cs, desc);
490 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
491 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
492 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
493 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
494 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
495 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
499 * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
500 * see commit c9fb809.
502 static void gpmc_cs_show_timings(int cs, const char *desc)
504 gpmc_show_regs(cs, desc);
506 pr_info("gpmc cs%i access configuration:\n", cs);
507 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
508 GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
509 GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13,
510 GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
511 GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
512 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
513 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
514 GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
515 GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
517 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
518 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
519 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
520 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
521 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
523 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
525 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
527 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
528 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
530 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
531 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
533 pr_info("gpmc cs%i timings configuration:\n", cs);
534 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
535 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
536 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
538 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
539 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
540 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
541 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
542 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
543 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
544 "adv-aad-mux-rd-off-ns");
545 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
546 "adv-aad-mux-wr-off-ns");
549 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
550 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
551 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
552 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns");
553 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
555 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
556 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
558 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
559 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
560 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
562 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
564 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
565 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
567 GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
568 GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
569 "wait-monitoring-ns", GPMC_CD_CLK);
570 GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
571 GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
572 "clk-activation-ns", GPMC_CD_FCLK);
574 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
575 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
578 static inline void gpmc_cs_show_timings(int cs, const char *desc)
584 * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
585 * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
586 * prior to calling this function with @cd equal to GPMC_CD_CLK.
588 * @cs: Chip Select Region.
589 * @reg: GPMC_CS_CONFIGn register offset.
591 * @end_bit: End Bit. Must be >= @st_bit.
592 * @max: Maximum parameter value.
593 * If 0, maximum is as high as @st_bit and @end_bit allow.
594 * @time: Timing parameter in ns.
595 * @cd: Timing parameter clock domain.
596 * @name: Timing parameter name.
597 * @return: 0 on success, -1 on error.
599 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
600 int time, enum gpmc_clk_domain cd, const char *name)
603 int ticks, mask, nr_bits;
608 ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
609 nr_bits = end_bit - st_bit + 1;
610 mask = (1 << nr_bits) - 1;
616 pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
617 __func__, cs, name, time, ticks, max);
622 l = gpmc_cs_read_reg(cs, reg);
623 #ifdef CONFIG_OMAP_GPMC_DEBUG
625 "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
626 cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
627 (l >> st_bit) & mask, time);
629 l &= ~(mask << st_bit);
630 l |= ticks << st_bit;
631 gpmc_cs_write_reg(cs, reg, l);
636 #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \
637 if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
638 t->field, (cd), #field) < 0) \
641 #define GPMC_SET_ONE(reg, st, end, field) \
642 GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
645 * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
646 * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
647 * read --> don't sample bus too early
648 * write --> data is longer on bus
651 * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
652 * / waitmonitoring_ticks)
653 * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
656 * @wait_monitoring: WAITMONITORINGTIME in ns.
657 * @return: -1 on failure to scale, else proper divider > 0.
659 static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
662 int div = gpmc_ns_to_ticks(wait_monitoring);
664 div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
665 div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
677 * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
678 * @sync_clk: GPMC_CLK period in ps.
679 * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
682 int gpmc_calc_divider(unsigned int sync_clk)
684 int div = gpmc_ps_to_ticks(sync_clk);
695 * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
696 * @cs: Chip Select Region.
697 * @t: GPMC timing parameters.
698 * @s: GPMC timing settings.
699 * @return: 0 on success, -1 on error.
701 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
702 const struct gpmc_settings *s)
707 div = gpmc_calc_divider(t->sync_clk);
712 * See if we need to change the divider for waitmonitoringtime.
714 * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
715 * pure asynchronous accesses, i.e. both read and write asynchronous.
716 * However, only do so if WAITMONITORINGTIME is actually used, i.e.
717 * either WAITREADMONITORING or WAITWRITEMONITORING is set.
719 * This statement must not change div to scale async WAITMONITORINGTIME
720 * to protect mixed synchronous and asynchronous accesses.
722 * We raise an error later if WAITMONITORINGTIME does not fit.
724 if (!s->sync_read && !s->sync_write &&
725 (s->wait_on_read || s->wait_on_write)
728 div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
730 pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
738 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
739 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
740 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
742 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
743 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
744 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
745 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
746 GPMC_SET_ONE(GPMC_CS_CONFIG3, 4, 6, adv_aad_mux_on);
747 GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off);
748 GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off);
751 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
752 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
753 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
754 GPMC_SET_ONE(GPMC_CS_CONFIG4, 4, 6, oe_aad_mux_on);
755 GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off);
757 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
758 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
760 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
761 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
762 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
764 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
766 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
767 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
769 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
770 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
771 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
772 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
774 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
777 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
779 GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
780 GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
781 wait_monitoring, GPMC_CD_CLK);
782 GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
783 GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
784 clk_activation, GPMC_CD_FCLK);
786 #ifdef CONFIG_OMAP_GPMC_DEBUG
787 pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
788 cs, (div * gpmc_get_fclk_period()) / 1000, div);
791 gpmc_cs_bool_timings(cs, &t->bool_timings);
792 gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
797 static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
803 * Ensure that base address is aligned on a
804 * boundary equal to or greater than size.
806 if (base & (size - 1))
809 base >>= GPMC_CHUNK_SHIFT;
810 mask = (1 << GPMC_SECTION_SHIFT) - size;
811 mask >>= GPMC_CHUNK_SHIFT;
812 mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
814 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
815 l &= ~GPMC_CONFIG7_MASK;
816 l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
817 l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
818 l |= GPMC_CONFIG7_CSVALID;
819 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
824 static void gpmc_cs_enable_mem(int cs)
828 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
829 l |= GPMC_CONFIG7_CSVALID;
830 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
833 static void gpmc_cs_disable_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_get_memconf(int cs, u32 *base, u32 *size)
847 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
848 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
849 mask = (l >> 8) & 0x0f;
850 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
853 static int gpmc_cs_mem_enabled(int cs)
857 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
858 return l & GPMC_CONFIG7_CSVALID;
861 static void gpmc_cs_set_reserved(int cs, int reserved)
863 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
865 gpmc->flags |= GPMC_CS_RESERVED;
868 static bool gpmc_cs_reserved(int cs)
870 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
872 return gpmc->flags & GPMC_CS_RESERVED;
875 static void gpmc_cs_set_name(int cs, const char *name)
877 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
882 static const char *gpmc_cs_get_name(int cs)
884 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
889 static unsigned long gpmc_mem_align(unsigned long size)
893 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
894 order = GPMC_CHUNK_SHIFT - 1;
903 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
905 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
906 struct resource *res = &gpmc->mem;
909 size = gpmc_mem_align(size);
910 spin_lock(&gpmc_mem_lock);
912 res->end = base + size - 1;
913 r = request_resource(&gpmc_mem_root, res);
914 spin_unlock(&gpmc_mem_lock);
919 static int gpmc_cs_delete_mem(int cs)
921 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
922 struct resource *res = &gpmc->mem;
925 spin_lock(&gpmc_mem_lock);
926 r = release_resource(res);
929 spin_unlock(&gpmc_mem_lock);
935 * gpmc_cs_remap - remaps a chip-select physical base address
936 * @cs: chip-select to remap
937 * @base: physical base address to re-map chip-select to
939 * Re-maps a chip-select to a new physical base address specified by
940 * "base". Returns 0 on success and appropriate negative error code
943 static int gpmc_cs_remap(int cs, u32 base)
948 if (cs > gpmc_cs_num) {
949 pr_err("%s: requested chip-select is disabled\n", __func__);
954 * Make sure we ignore any device offsets from the GPMC partition
955 * allocated for the chip select and that the new base confirms
956 * to the GPMC 16MB minimum granularity.
958 base &= ~(SZ_16M - 1);
960 gpmc_cs_get_memconf(cs, &old_base, &size);
961 if (base == old_base)
964 ret = gpmc_cs_delete_mem(cs);
968 ret = gpmc_cs_insert_mem(cs, base, size);
972 ret = gpmc_cs_set_memconf(cs, base, size);
977 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
979 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
980 struct resource *res = &gpmc->mem;
983 if (cs > gpmc_cs_num) {
984 pr_err("%s: requested chip-select is disabled\n", __func__);
987 size = gpmc_mem_align(size);
988 if (size > (1 << GPMC_SECTION_SHIFT))
991 spin_lock(&gpmc_mem_lock);
992 if (gpmc_cs_reserved(cs)) {
996 if (gpmc_cs_mem_enabled(cs))
997 r = adjust_resource(res, res->start & ~(size - 1), size);
999 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
1004 /* Disable CS while changing base address and size mask */
1005 gpmc_cs_disable_mem(cs);
1007 r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
1009 release_resource(res);
1014 gpmc_cs_enable_mem(cs);
1016 gpmc_cs_set_reserved(cs, 1);
1018 spin_unlock(&gpmc_mem_lock);
1021 EXPORT_SYMBOL(gpmc_cs_request);
1023 void gpmc_cs_free(int cs)
1025 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
1026 struct resource *res = &gpmc->mem;
1028 spin_lock(&gpmc_mem_lock);
1029 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
1030 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
1032 spin_unlock(&gpmc_mem_lock);
1035 gpmc_cs_disable_mem(cs);
1037 release_resource(res);
1038 gpmc_cs_set_reserved(cs, 0);
1039 spin_unlock(&gpmc_mem_lock);
1041 EXPORT_SYMBOL(gpmc_cs_free);
1044 * gpmc_configure - write request to configure gpmc
1045 * @cmd: command type
1046 * @wval: value to write
1047 * @return status of the operation
1049 int gpmc_configure(int cmd, int wval)
1054 case GPMC_CONFIG_WP:
1055 regval = gpmc_read_reg(GPMC_CONFIG);
1057 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
1059 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
1060 gpmc_write_reg(GPMC_CONFIG, regval);
1064 pr_err("%s: command not supported\n", __func__);
1070 EXPORT_SYMBOL(gpmc_configure);
1072 void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
1076 reg->gpmc_status = gpmc_base + GPMC_STATUS;
1077 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
1078 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
1079 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
1080 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
1081 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
1082 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
1083 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
1084 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
1085 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
1086 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
1087 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
1088 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
1089 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
1090 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
1092 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
1093 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
1095 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
1097 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
1099 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
1101 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
1103 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
1105 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
1110 static bool gpmc_nand_writebuffer_empty(void)
1112 if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
1118 static struct gpmc_nand_ops nand_ops = {
1119 .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
1123 * gpmc_omap_get_nand_ops - Get the GPMC NAND interface
1124 * @regs: the GPMC NAND register map exclusive for NAND use.
1125 * @cs: GPMC chip select number on which the NAND sits. The
1126 * register map returned will be specific to this chip select.
1128 * Returns NULL on error e.g. invalid cs.
1130 struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
1132 if (cs >= gpmc_cs_num)
1135 gpmc_update_nand_reg(reg, cs);
1139 EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
1141 int gpmc_get_client_irq(unsigned irq_config)
1143 if (!gpmc_irq_domain) {
1144 pr_warn("%s called before GPMC IRQ domain available\n",
1149 if (irq_config >= GPMC_NR_IRQ)
1152 return irq_create_mapping(gpmc_irq_domain, irq_config);
1155 static int gpmc_irq_endis(unsigned long hwirq, bool endis)
1159 regval = gpmc_read_reg(GPMC_IRQENABLE);
1161 regval |= BIT(hwirq);
1163 regval &= ~BIT(hwirq);
1164 gpmc_write_reg(GPMC_IRQENABLE, regval);
1169 static void gpmc_irq_disable(struct irq_data *p)
1171 gpmc_irq_endis(p->hwirq, false);
1174 static void gpmc_irq_enable(struct irq_data *p)
1176 gpmc_irq_endis(p->hwirq, true);
1179 static void gpmc_irq_noop(struct irq_data *data) { }
1181 static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
1183 static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
1186 struct gpmc_device *gpmc = d->host_data;
1188 irq_set_chip_data(virq, gpmc);
1189 irq_set_chip_and_handler(virq, &gpmc->irq_chip, handle_simple_irq);
1190 irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
1195 static const struct irq_domain_ops gpmc_irq_domain_ops = {
1196 .map = gpmc_irq_map,
1197 .xlate = irq_domain_xlate_twocell,
1200 static irqreturn_t gpmc_handle_irq(int irq, void *data)
1204 struct gpmc_device *gpmc = data;
1206 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1211 for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++) {
1212 if (regval & BIT(hwirq)) {
1213 virq = irq_find_mapping(gpmc_irq_domain, hwirq);
1216 "spurious irq detected hwirq %d, virq %d\n",
1220 generic_handle_irq(virq);
1224 gpmc_write_reg(GPMC_IRQSTATUS, regval);
1229 static int gpmc_setup_irq(struct gpmc_device *gpmc)
1234 /* Disable interrupts */
1235 gpmc_write_reg(GPMC_IRQENABLE, 0);
1237 /* clear interrupts */
1238 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1239 gpmc_write_reg(GPMC_IRQSTATUS, regval);
1241 gpmc->irq_chip.name = "gpmc";
1242 gpmc->irq_chip.irq_startup = gpmc_irq_noop_ret;
1243 gpmc->irq_chip.irq_enable = gpmc_irq_enable;
1244 gpmc->irq_chip.irq_disable = gpmc_irq_disable;
1245 gpmc->irq_chip.irq_shutdown = gpmc_irq_noop;
1246 gpmc->irq_chip.irq_ack = gpmc_irq_noop;
1247 gpmc->irq_chip.irq_mask = gpmc_irq_noop;
1248 gpmc->irq_chip.irq_unmask = gpmc_irq_noop;
1250 gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
1252 &gpmc_irq_domain_ops,
1254 if (!gpmc_irq_domain) {
1255 dev_err(gpmc->dev, "IRQ domain add failed\n");
1259 rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
1261 dev_err(gpmc->dev, "failed to request irq %d: %d\n",
1263 irq_domain_remove(gpmc_irq_domain);
1264 gpmc_irq_domain = NULL;
1270 static int gpmc_free_irq(struct gpmc_device *gpmc)
1274 free_irq(gpmc->irq, gpmc);
1276 for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++)
1277 irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
1279 irq_domain_remove(gpmc_irq_domain);
1280 gpmc_irq_domain = NULL;
1285 static void gpmc_mem_exit(void)
1289 for (cs = 0; cs < gpmc_cs_num; cs++) {
1290 if (!gpmc_cs_mem_enabled(cs))
1292 gpmc_cs_delete_mem(cs);
1297 static void gpmc_mem_init(void)
1302 * The first 1MB of GPMC address space is typically mapped to
1303 * the internal ROM. Never allocate the first page, to
1304 * facilitate bug detection; even if we didn't boot from ROM.
1306 gpmc_mem_root.start = SZ_1M;
1307 gpmc_mem_root.end = GPMC_MEM_END;
1309 /* Reserve all regions that has been set up by bootloader */
1310 for (cs = 0; cs < gpmc_cs_num; cs++) {
1313 if (!gpmc_cs_mem_enabled(cs))
1315 gpmc_cs_get_memconf(cs, &base, &size);
1316 if (gpmc_cs_insert_mem(cs, base, size)) {
1317 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
1318 __func__, cs, base, base + size);
1319 gpmc_cs_disable_mem(cs);
1324 static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
1329 div = gpmc_calc_divider(sync_clk);
1330 temp = gpmc_ps_to_ticks(time_ps);
1331 temp = (temp + div - 1) / div;
1332 return gpmc_ticks_to_ps(temp * div);
1335 /* XXX: can the cycles be avoided ? */
1336 static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
1337 struct gpmc_device_timings *dev_t,
1343 temp = dev_t->t_avdp_r;
1344 /* XXX: mux check required ? */
1346 /* XXX: t_avdp not to be required for sync, only added for tusb
1347 * this indirectly necessitates requirement of t_avdp_r and
1348 * t_avdp_w instead of having a single t_avdp
1350 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
1351 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1353 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1356 temp = dev_t->t_oeasu; /* XXX: remove this ? */
1358 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
1359 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
1360 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
1362 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1365 /* XXX: any scope for improvement ?, by combining oe_on
1366 * and clk_activation, need to check whether
1367 * access = clk_activation + round to sync clk ?
1369 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
1370 temp += gpmc_t->clk_activation;
1372 temp = max_t(u32, temp, gpmc_t->oe_on +
1373 gpmc_ticks_to_ps(dev_t->cyc_oe));
1374 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1376 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1377 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1380 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
1381 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
1383 /* XXX: barter t_ce_rdyz with t_cez_r ? */
1384 if (dev_t->t_ce_rdyz)
1385 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
1386 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1391 static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
1392 struct gpmc_device_timings *dev_t,
1398 temp = dev_t->t_avdp_w;
1400 temp = max_t(u32, temp,
1401 gpmc_t->clk_activation + dev_t->t_avdh);
1402 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1404 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1406 /* wr_data_mux_bus */
1407 temp = max_t(u32, dev_t->t_weasu,
1408 gpmc_t->clk_activation + dev_t->t_rdyo);
1409 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
1410 * and in that case remember to handle we_on properly
1413 temp = max_t(u32, temp,
1414 gpmc_t->adv_wr_off + dev_t->t_aavdh);
1415 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1416 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1418 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1421 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1422 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1424 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1427 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
1428 gpmc_t->wr_access = gpmc_t->access;
1431 temp = gpmc_t->we_on + dev_t->t_wpl;
1432 temp = max_t(u32, temp,
1433 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
1434 temp = max_t(u32, temp,
1435 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
1436 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1438 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1442 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
1443 temp += gpmc_t->wr_access;
1444 /* XXX: barter t_ce_rdyz with t_cez_w ? */
1445 if (dev_t->t_ce_rdyz)
1446 temp = max_t(u32, temp,
1447 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
1448 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1453 static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
1454 struct gpmc_device_timings *dev_t,
1460 temp = dev_t->t_avdp_r;
1462 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1463 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1466 temp = dev_t->t_oeasu;
1468 temp = max_t(u32, temp,
1469 gpmc_t->adv_rd_off + dev_t->t_aavdh);
1470 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1473 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1474 gpmc_t->oe_on + dev_t->t_oe);
1475 temp = max_t(u32, temp,
1476 gpmc_t->cs_on + dev_t->t_ce);
1477 temp = max_t(u32, temp,
1478 gpmc_t->adv_on + dev_t->t_aa);
1479 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1481 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1482 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1485 temp = max_t(u32, dev_t->t_rd_cycle,
1486 gpmc_t->cs_rd_off + dev_t->t_cez_r);
1487 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1488 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1493 static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
1494 struct gpmc_device_timings *dev_t,
1500 temp = dev_t->t_avdp_w;
1502 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1503 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1505 /* wr_data_mux_bus */
1506 temp = dev_t->t_weasu;
1508 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1509 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1510 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1512 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1515 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1516 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1518 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1521 temp = gpmc_t->we_on + dev_t->t_wpl;
1522 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1524 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1528 temp = max_t(u32, dev_t->t_wr_cycle,
1529 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1530 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1535 static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1536 struct gpmc_device_timings *dev_t)
1540 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1541 gpmc_get_fclk_period();
1543 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1547 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1548 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1550 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1553 if (dev_t->ce_xdelay)
1554 gpmc_t->bool_timings.cs_extra_delay = true;
1555 if (dev_t->avd_xdelay)
1556 gpmc_t->bool_timings.adv_extra_delay = true;
1557 if (dev_t->oe_xdelay)
1558 gpmc_t->bool_timings.oe_extra_delay = true;
1559 if (dev_t->we_xdelay)
1560 gpmc_t->bool_timings.we_extra_delay = true;
1565 static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
1566 struct gpmc_device_timings *dev_t,
1572 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1575 temp = dev_t->t_avdasu;
1576 if (dev_t->t_ce_avd)
1577 temp = max_t(u32, temp,
1578 gpmc_t->cs_on + dev_t->t_ce_avd);
1579 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1582 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1587 /* TODO: remove this function once all peripherals are confirmed to
1588 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1589 * has to be modified to handle timings in ps instead of ns
1591 static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1594 t->cs_rd_off /= 1000;
1595 t->cs_wr_off /= 1000;
1597 t->adv_rd_off /= 1000;
1598 t->adv_wr_off /= 1000;
1603 t->page_burst_access /= 1000;
1605 t->rd_cycle /= 1000;
1606 t->wr_cycle /= 1000;
1607 t->bus_turnaround /= 1000;
1608 t->cycle2cycle_delay /= 1000;
1609 t->wait_monitoring /= 1000;
1610 t->clk_activation /= 1000;
1611 t->wr_access /= 1000;
1612 t->wr_data_mux_bus /= 1000;
1615 int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
1616 struct gpmc_settings *gpmc_s,
1617 struct gpmc_device_timings *dev_t)
1619 bool mux = false, sync = false;
1622 mux = gpmc_s->mux_add_data ? true : false;
1623 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1626 memset(gpmc_t, 0, sizeof(*gpmc_t));
1628 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
1630 if (gpmc_s && gpmc_s->sync_read)
1631 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
1633 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
1635 if (gpmc_s && gpmc_s->sync_write)
1636 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
1638 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
1640 /* TODO: remove, see function definition */
1641 gpmc_convert_ps_to_ns(gpmc_t);
1647 * gpmc_cs_program_settings - programs non-timing related settings
1648 * @cs: GPMC chip-select to program
1649 * @p: pointer to GPMC settings structure
1651 * Programs non-timing related settings for a GPMC chip-select, such as
1652 * bus-width, burst configuration, etc. Function should be called once
1653 * for each chip-select that is being used and must be called before
1654 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1655 * register will be initialised to zero by this function. Returns 0 on
1656 * success and appropriate negative error code on failure.
1658 int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1662 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1663 pr_err("%s: invalid width %d!", __func__, p->device_width);
1667 /* Address-data multiplexing not supported for NAND devices */
1668 if (p->device_nand && p->mux_add_data) {
1669 pr_err("%s: invalid configuration!\n", __func__);
1673 if ((p->mux_add_data > GPMC_MUX_AD) ||
1674 ((p->mux_add_data == GPMC_MUX_AAD) &&
1675 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1676 pr_err("%s: invalid multiplex configuration!\n", __func__);
1680 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1681 if (p->burst_read || p->burst_write) {
1682 switch (p->burst_len) {
1688 pr_err("%s: invalid page/burst-length (%d)\n",
1689 __func__, p->burst_len);
1694 if (p->wait_pin > gpmc_nr_waitpins) {
1695 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1699 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1702 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1704 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1705 if (p->wait_on_read)
1706 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1707 if (p->wait_on_write)
1708 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1709 if (p->wait_on_read || p->wait_on_write)
1710 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1712 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1713 if (p->mux_add_data)
1714 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1716 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1718 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1719 if (p->burst_read || p->burst_write) {
1720 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1721 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1724 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1730 static const struct of_device_id gpmc_dt_ids[] = {
1731 { .compatible = "ti,omap2420-gpmc" },
1732 { .compatible = "ti,omap2430-gpmc" },
1733 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1734 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1735 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1738 MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1741 * gpmc_read_settings_dt - read gpmc settings from device-tree
1742 * @np: pointer to device-tree node for a gpmc child device
1743 * @p: pointer to gpmc settings structure
1745 * Reads the GPMC settings for a GPMC child device from device-tree and
1746 * stores them in the GPMC settings structure passed. The GPMC settings
1747 * structure is initialised to zero by this function and so any
1748 * previously stored settings will be cleared.
1750 void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1752 memset(p, 0, sizeof(struct gpmc_settings));
1754 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1755 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
1756 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1757 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1759 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1760 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1761 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1762 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1763 if (!p->burst_read && !p->burst_write)
1764 pr_warn("%s: page/burst-length set but not used!\n",
1768 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1769 p->wait_on_read = of_property_read_bool(np,
1770 "gpmc,wait-on-read");
1771 p->wait_on_write = of_property_read_bool(np,
1772 "gpmc,wait-on-write");
1773 if (!p->wait_on_read && !p->wait_on_write)
1774 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1779 static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1780 struct gpmc_timings *gpmc_t)
1782 struct gpmc_bool_timings *p;
1787 memset(gpmc_t, 0, sizeof(*gpmc_t));
1789 /* minimum clock period for syncronous mode */
1790 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
1792 /* chip select timtings */
1793 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1794 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1795 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
1797 /* ADV signal timings */
1798 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1799 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1800 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
1801 of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
1802 &gpmc_t->adv_aad_mux_on);
1803 of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
1804 &gpmc_t->adv_aad_mux_rd_off);
1805 of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
1806 &gpmc_t->adv_aad_mux_wr_off);
1808 /* WE signal timings */
1809 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1810 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
1812 /* OE signal timings */
1813 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1814 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
1815 of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
1816 &gpmc_t->oe_aad_mux_on);
1817 of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
1818 &gpmc_t->oe_aad_mux_off);
1820 /* access and cycle timings */
1821 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1822 &gpmc_t->page_burst_access);
1823 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1824 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1825 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1826 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1827 &gpmc_t->bus_turnaround);
1828 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1829 &gpmc_t->cycle2cycle_delay);
1830 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1831 &gpmc_t->wait_monitoring);
1832 of_property_read_u32(np, "gpmc,clk-activation-ns",
1833 &gpmc_t->clk_activation);
1835 /* only applicable to OMAP3+ */
1836 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1837 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1838 &gpmc_t->wr_data_mux_bus);
1840 /* bool timing parameters */
1841 p = &gpmc_t->bool_timings;
1843 p->cycle2cyclediffcsen =
1844 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1845 p->cycle2cyclesamecsen =
1846 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1847 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1848 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1849 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1850 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1851 p->time_para_granularity =
1852 of_property_read_bool(np, "gpmc,time-para-granularity");
1855 #if IS_ENABLED(CONFIG_MTD_NAND)
1857 static const char * const nand_xfer_types[] = {
1858 [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled",
1859 [NAND_OMAP_POLLED] = "polled",
1860 [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma",
1861 [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq",
1864 static int gpmc_probe_nand_child(struct platform_device *pdev,
1865 struct device_node *child)
1869 struct gpmc_timings gpmc_t;
1870 struct omap_nand_platform_data *gpmc_nand_data;
1872 if (of_property_read_u32(child, "reg", &val) < 0) {
1873 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1878 gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
1880 if (!gpmc_nand_data)
1883 gpmc_nand_data->cs = val;
1884 gpmc_nand_data->of_node = child;
1886 /* Detect availability of ELM module */
1887 gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0);
1888 if (gpmc_nand_data->elm_of_node == NULL)
1889 gpmc_nand_data->elm_of_node =
1890 of_parse_phandle(child, "elm_id", 0);
1892 /* select ecc-scheme for NAND */
1893 if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) {
1894 pr_err("%s: ti,nand-ecc-opt not found\n", __func__);
1898 if (!strcmp(s, "sw"))
1899 gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW;
1900 else if (!strcmp(s, "ham1") ||
1901 !strcmp(s, "hw") || !strcmp(s, "hw-romcode"))
1902 gpmc_nand_data->ecc_opt =
1903 OMAP_ECC_HAM1_CODE_HW;
1904 else if (!strcmp(s, "bch4"))
1905 if (gpmc_nand_data->elm_of_node)
1906 gpmc_nand_data->ecc_opt =
1907 OMAP_ECC_BCH4_CODE_HW;
1909 gpmc_nand_data->ecc_opt =
1910 OMAP_ECC_BCH4_CODE_HW_DETECTION_SW;
1911 else if (!strcmp(s, "bch8"))
1912 if (gpmc_nand_data->elm_of_node)
1913 gpmc_nand_data->ecc_opt =
1914 OMAP_ECC_BCH8_CODE_HW;
1916 gpmc_nand_data->ecc_opt =
1917 OMAP_ECC_BCH8_CODE_HW_DETECTION_SW;
1918 else if (!strcmp(s, "bch16"))
1919 if (gpmc_nand_data->elm_of_node)
1920 gpmc_nand_data->ecc_opt =
1921 OMAP_ECC_BCH16_CODE_HW;
1923 pr_err("%s: BCH16 requires ELM support\n", __func__);
1925 pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__);
1927 /* select data transfer mode for NAND controller */
1928 if (!of_property_read_string(child, "ti,nand-xfer-type", &s))
1929 for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++)
1930 if (!strcasecmp(s, nand_xfer_types[val])) {
1931 gpmc_nand_data->xfer_type = val;
1935 gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child);
1937 val = of_get_nand_bus_width(child);
1939 gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
1941 gpmc_read_timings_dt(child, &gpmc_t);
1942 gpmc_nand_init(gpmc_nand_data, &gpmc_t);
1947 static int gpmc_probe_nand_child(struct platform_device *pdev,
1948 struct device_node *child)
1954 #if IS_ENABLED(CONFIG_MTD_ONENAND)
1955 static int gpmc_probe_onenand_child(struct platform_device *pdev,
1956 struct device_node *child)
1959 struct omap_onenand_platform_data *gpmc_onenand_data;
1961 if (of_property_read_u32(child, "reg", &val) < 0) {
1962 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1967 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1969 if (!gpmc_onenand_data)
1972 gpmc_onenand_data->cs = val;
1973 gpmc_onenand_data->of_node = child;
1974 gpmc_onenand_data->dma_channel = -1;
1976 if (!of_property_read_u32(child, "dma-channel", &val))
1977 gpmc_onenand_data->dma_channel = val;
1979 gpmc_onenand_init(gpmc_onenand_data);
1984 static int gpmc_probe_onenand_child(struct platform_device *pdev,
1985 struct device_node *child)
1992 * gpmc_probe_generic_child - configures the gpmc for a child device
1993 * @pdev: pointer to gpmc platform device
1994 * @child: pointer to device-tree node for child device
1996 * Allocates and configures a GPMC chip-select for a child device.
1997 * Returns 0 on success and appropriate negative error code on failure.
1999 static int gpmc_probe_generic_child(struct platform_device *pdev,
2000 struct device_node *child)
2002 struct gpmc_settings gpmc_s;
2003 struct gpmc_timings gpmc_t;
2004 struct resource res;
2010 if (of_property_read_u32(child, "reg", &cs) < 0) {
2011 dev_err(&pdev->dev, "%s has no 'reg' property\n",
2016 if (of_address_to_resource(child, 0, &res) < 0) {
2017 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
2023 * Check if we have multiple instances of the same device
2024 * on a single chip select. If so, use the already initialized
2027 name = gpmc_cs_get_name(cs);
2028 if (name && child->name && of_node_cmp(child->name, name) == 0)
2031 ret = gpmc_cs_request(cs, resource_size(&res), &base);
2033 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
2036 gpmc_cs_set_name(cs, child->name);
2038 gpmc_read_settings_dt(child, &gpmc_s);
2039 gpmc_read_timings_dt(child, &gpmc_t);
2042 * For some GPMC devices we still need to rely on the bootloader
2043 * timings because the devices can be connected via FPGA.
2044 * REVISIT: Add timing support from slls644g.pdf.
2046 if (!gpmc_t.cs_rd_off) {
2047 WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
2049 gpmc_cs_show_timings(cs,
2050 "please add GPMC bootloader timings to .dts");
2054 /* CS must be disabled while making changes to gpmc configuration */
2055 gpmc_cs_disable_mem(cs);
2058 * FIXME: gpmc_cs_request() will map the CS to an arbitary
2059 * location in the gpmc address space. When booting with
2060 * device-tree we want the NOR flash to be mapped to the
2061 * location specified in the device-tree blob. So remap the
2062 * CS to this location. Once DT migration is complete should
2063 * just make gpmc_cs_request() map a specific address.
2065 ret = gpmc_cs_remap(cs, res.start);
2067 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
2072 ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width);
2076 gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
2077 ret = gpmc_cs_program_settings(cs, &gpmc_s);
2081 ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
2083 dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
2088 /* Clear limited address i.e. enable A26-A11 */
2089 val = gpmc_read_reg(GPMC_CONFIG);
2090 val &= ~GPMC_CONFIG_LIMITEDADDRESS;
2091 gpmc_write_reg(GPMC_CONFIG, val);
2093 /* Enable CS region */
2094 gpmc_cs_enable_mem(cs);
2098 /* create platform device, NULL on error or when disabled */
2099 if (!of_platform_device_create(child, NULL, &pdev->dev))
2100 goto err_child_fail;
2102 /* is child a common bus? */
2103 if (of_match_node(of_default_bus_match_table, child))
2104 /* create children and other common bus children */
2105 if (of_platform_populate(child, of_default_bus_match_table,
2107 goto err_child_fail;
2113 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
2122 static int gpmc_probe_dt(struct platform_device *pdev)
2125 struct device_node *child;
2126 const struct of_device_id *of_id =
2127 of_match_device(gpmc_dt_ids, &pdev->dev);
2132 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
2135 pr_err("%s: number of chip-selects not defined\n", __func__);
2137 } else if (gpmc_cs_num < 1) {
2138 pr_err("%s: all chip-selects are disabled\n", __func__);
2140 } else if (gpmc_cs_num > GPMC_CS_NUM) {
2141 pr_err("%s: number of supported chip-selects cannot be > %d\n",
2142 __func__, GPMC_CS_NUM);
2146 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
2149 pr_err("%s: number of wait pins not found!\n", __func__);
2153 for_each_available_child_of_node(pdev->dev.of_node, child) {
2158 if (of_node_cmp(child->name, "nand") == 0)
2159 ret = gpmc_probe_nand_child(pdev, child);
2160 else if (of_node_cmp(child->name, "onenand") == 0)
2161 ret = gpmc_probe_onenand_child(pdev, child);
2163 ret = gpmc_probe_generic_child(pdev, child);
2169 static int gpmc_probe_dt(struct platform_device *pdev)
2175 static int gpmc_probe(struct platform_device *pdev)
2179 struct resource *res;
2180 struct gpmc_device *gpmc;
2182 gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
2186 gpmc->dev = &pdev->dev;
2187 platform_set_drvdata(pdev, gpmc);
2189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2193 phys_base = res->start;
2194 mem_size = resource_size(res);
2196 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
2197 if (IS_ERR(gpmc_base))
2198 return PTR_ERR(gpmc_base);
2200 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2202 dev_err(&pdev->dev, "Failed to get resource: irq\n");
2206 gpmc->irq = res->start;
2208 gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
2209 if (IS_ERR(gpmc_l3_clk)) {
2210 dev_err(&pdev->dev, "Failed to get GPMC fck\n");
2211 return PTR_ERR(gpmc_l3_clk);
2214 if (!clk_get_rate(gpmc_l3_clk)) {
2215 dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
2219 pm_runtime_enable(&pdev->dev);
2220 pm_runtime_get_sync(&pdev->dev);
2222 l = gpmc_read_reg(GPMC_REVISION);
2225 * FIXME: Once device-tree migration is complete the below flags
2226 * should be populated based upon the device-tree compatible
2227 * string. For now just use the IP revision. OMAP3+ devices have
2228 * the wr_access and wr_data_mux_bus register fields. OMAP4+
2229 * devices support the addr-addr-data multiplex protocol.
2231 * GPMC IP revisions:
2234 * - OMAP44xx/54xx/AM335x = 6.0
2236 if (GPMC_REVISION_MAJOR(l) > 0x4)
2237 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
2238 if (GPMC_REVISION_MAJOR(l) > 0x5)
2239 gpmc_capability |= GPMC_HAS_MUX_AAD;
2240 dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
2241 GPMC_REVISION_MINOR(l));
2245 rc = gpmc_setup_irq(gpmc);
2247 dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
2251 if (!pdev->dev.of_node) {
2252 gpmc_cs_num = GPMC_CS_NUM;
2253 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
2256 rc = gpmc_probe_dt(pdev);
2258 dev_err(gpmc->dev, "failed to probe DT parameters\n");
2259 gpmc_free_irq(gpmc);
2266 pm_runtime_put_sync(&pdev->dev);
2270 static int gpmc_remove(struct platform_device *pdev)
2272 struct gpmc_device *gpmc = platform_get_drvdata(pdev);
2274 gpmc_free_irq(gpmc);
2276 pm_runtime_put_sync(&pdev->dev);
2277 pm_runtime_disable(&pdev->dev);
2282 #ifdef CONFIG_PM_SLEEP
2283 static int gpmc_suspend(struct device *dev)
2285 omap3_gpmc_save_context();
2286 pm_runtime_put_sync(dev);
2290 static int gpmc_resume(struct device *dev)
2292 pm_runtime_get_sync(dev);
2293 omap3_gpmc_restore_context();
2298 static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
2300 static struct platform_driver gpmc_driver = {
2301 .probe = gpmc_probe,
2302 .remove = gpmc_remove,
2304 .name = DEVICE_NAME,
2305 .of_match_table = of_match_ptr(gpmc_dt_ids),
2310 static __init int gpmc_init(void)
2312 return platform_driver_register(&gpmc_driver);
2315 static __exit void gpmc_exit(void)
2317 platform_driver_unregister(&gpmc_driver);
2321 postcore_initcall(gpmc_init);
2322 module_exit(gpmc_exit);
2324 static struct omap3_gpmc_regs gpmc_context;
2326 void omap3_gpmc_save_context(void)
2333 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
2334 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
2335 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
2336 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
2337 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
2338 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
2339 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
2340 for (i = 0; i < gpmc_cs_num; i++) {
2341 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
2342 if (gpmc_context.cs_context[i].is_valid) {
2343 gpmc_context.cs_context[i].config1 =
2344 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
2345 gpmc_context.cs_context[i].config2 =
2346 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
2347 gpmc_context.cs_context[i].config3 =
2348 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
2349 gpmc_context.cs_context[i].config4 =
2350 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
2351 gpmc_context.cs_context[i].config5 =
2352 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
2353 gpmc_context.cs_context[i].config6 =
2354 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
2355 gpmc_context.cs_context[i].config7 =
2356 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
2361 void omap3_gpmc_restore_context(void)
2368 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
2369 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
2370 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
2371 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
2372 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
2373 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
2374 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
2375 for (i = 0; i < gpmc_cs_num; i++) {
2376 if (gpmc_context.cs_context[i].is_valid) {
2377 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
2378 gpmc_context.cs_context[i].config1);
2379 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
2380 gpmc_context.cs_context[i].config2);
2381 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
2382 gpmc_context.cs_context[i].config3);
2383 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
2384 gpmc_context.cs_context[i].config4);
2385 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
2386 gpmc_context.cs_context[i].config5);
2387 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
2388 gpmc_context.cs_context[i].config6);
2389 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
2390 gpmc_context.cs_context[i].config7);