GNU Linux-libre 4.14.302-gnu1
[releases.git] / drivers / memory / omap-gpmc.c
1 /*
2  * GPMC support functions
3  *
4  * Copyright (C) 2005-2006 Nokia Corporation
5  *
6  * Author: Juha Yrjola
7  *
8  * Copyright (C) 2009 Texas Instruments
9  * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License version 2 as
13  * published by the Free Software Foundation.
14  */
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>
22 #include <linux/io.h>
23 #include <linux/gpio/driver.h>
24 #include <linux/interrupt.h>
25 #include <linux/irqdomain.h>
26 #include <linux/platform_device.h>
27 #include <linux/of.h>
28 #include <linux/of_address.h>
29 #include <linux/of_device.h>
30 #include <linux/of_platform.h>
31 #include <linux/omap-gpmc.h>
32 #include <linux/pm_runtime.h>
33
34 #include <linux/platform_data/mtd-nand-omap2.h>
35 #include <linux/platform_data/mtd-onenand-omap2.h>
36
37 #include <asm/mach-types.h>
38
39 #define DEVICE_NAME             "omap-gpmc"
40
41 /* GPMC register offsets */
42 #define GPMC_REVISION           0x00
43 #define GPMC_SYSCONFIG          0x10
44 #define GPMC_SYSSTATUS          0x14
45 #define GPMC_IRQSTATUS          0x18
46 #define GPMC_IRQENABLE          0x1c
47 #define GPMC_TIMEOUT_CONTROL    0x40
48 #define GPMC_ERR_ADDRESS        0x44
49 #define GPMC_ERR_TYPE           0x48
50 #define GPMC_CONFIG             0x50
51 #define GPMC_STATUS             0x54
52 #define GPMC_PREFETCH_CONFIG1   0x1e0
53 #define GPMC_PREFETCH_CONFIG2   0x1e4
54 #define GPMC_PREFETCH_CONTROL   0x1ec
55 #define GPMC_PREFETCH_STATUS    0x1f0
56 #define GPMC_ECC_CONFIG         0x1f4
57 #define GPMC_ECC_CONTROL        0x1f8
58 #define GPMC_ECC_SIZE_CONFIG    0x1fc
59 #define GPMC_ECC1_RESULT        0x200
60 #define GPMC_ECC_BCH_RESULT_0   0x240   /* not available on OMAP2 */
61 #define GPMC_ECC_BCH_RESULT_1   0x244   /* not available on OMAP2 */
62 #define GPMC_ECC_BCH_RESULT_2   0x248   /* not available on OMAP2 */
63 #define GPMC_ECC_BCH_RESULT_3   0x24c   /* not available on OMAP2 */
64 #define GPMC_ECC_BCH_RESULT_4   0x300   /* not available on OMAP2 */
65 #define GPMC_ECC_BCH_RESULT_5   0x304   /* not available on OMAP2 */
66 #define GPMC_ECC_BCH_RESULT_6   0x308   /* not available on OMAP2 */
67
68 /* GPMC ECC control settings */
69 #define GPMC_ECC_CTRL_ECCCLEAR          0x100
70 #define GPMC_ECC_CTRL_ECCDISABLE        0x000
71 #define GPMC_ECC_CTRL_ECCREG1           0x001
72 #define GPMC_ECC_CTRL_ECCREG2           0x002
73 #define GPMC_ECC_CTRL_ECCREG3           0x003
74 #define GPMC_ECC_CTRL_ECCREG4           0x004
75 #define GPMC_ECC_CTRL_ECCREG5           0x005
76 #define GPMC_ECC_CTRL_ECCREG6           0x006
77 #define GPMC_ECC_CTRL_ECCREG7           0x007
78 #define GPMC_ECC_CTRL_ECCREG8           0x008
79 #define GPMC_ECC_CTRL_ECCREG9           0x009
80
81 #define GPMC_CONFIG_LIMITEDADDRESS              BIT(1)
82
83 #define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS      BIT(0)
84
85 #define GPMC_CONFIG2_CSEXTRADELAY               BIT(7)
86 #define GPMC_CONFIG3_ADVEXTRADELAY              BIT(7)
87 #define GPMC_CONFIG4_OEEXTRADELAY               BIT(7)
88 #define GPMC_CONFIG4_WEEXTRADELAY               BIT(23)
89 #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN        BIT(6)
90 #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN        BIT(7)
91
92 #define GPMC_CS0_OFFSET         0x60
93 #define GPMC_CS_SIZE            0x30
94 #define GPMC_BCH_SIZE           0x10
95
96 /*
97  * The first 1MB of GPMC address space is typically mapped to
98  * the internal ROM. Never allocate the first page, to
99  * facilitate bug detection; even if we didn't boot from ROM.
100  * As GPMC minimum partition size is 16MB we can only start from
101  * there.
102  */
103 #define GPMC_MEM_START          0x1000000
104 #define GPMC_MEM_END            0x3FFFFFFF
105
106 #define GPMC_CHUNK_SHIFT        24              /* 16 MB */
107 #define GPMC_SECTION_SHIFT      28              /* 128 MB */
108
109 #define CS_NUM_SHIFT            24
110 #define ENABLE_PREFETCH         (0x1 << 7)
111 #define DMA_MPU_MODE            2
112
113 #define GPMC_REVISION_MAJOR(l)          ((l >> 4) & 0xf)
114 #define GPMC_REVISION_MINOR(l)          (l & 0xf)
115
116 #define GPMC_HAS_WR_ACCESS              0x1
117 #define GPMC_HAS_WR_DATA_MUX_BUS        0x2
118 #define GPMC_HAS_MUX_AAD                0x4
119
120 #define GPMC_NR_WAITPINS                4
121
122 #define GPMC_CS_CONFIG1         0x00
123 #define GPMC_CS_CONFIG2         0x04
124 #define GPMC_CS_CONFIG3         0x08
125 #define GPMC_CS_CONFIG4         0x0c
126 #define GPMC_CS_CONFIG5         0x10
127 #define GPMC_CS_CONFIG6         0x14
128 #define GPMC_CS_CONFIG7         0x18
129 #define GPMC_CS_NAND_COMMAND    0x1c
130 #define GPMC_CS_NAND_ADDRESS    0x20
131 #define GPMC_CS_NAND_DATA       0x24
132
133 /* Control Commands */
134 #define GPMC_CONFIG_RDY_BSY     0x00000001
135 #define GPMC_CONFIG_DEV_SIZE    0x00000002
136 #define GPMC_CONFIG_DEV_TYPE    0x00000003
137
138 #define GPMC_CONFIG1_WRAPBURST_SUPP     (1 << 31)
139 #define GPMC_CONFIG1_READMULTIPLE_SUPP  (1 << 30)
140 #define GPMC_CONFIG1_READTYPE_ASYNC     (0 << 29)
141 #define GPMC_CONFIG1_READTYPE_SYNC      (1 << 29)
142 #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
143 #define GPMC_CONFIG1_WRITETYPE_ASYNC    (0 << 27)
144 #define GPMC_CONFIG1_WRITETYPE_SYNC     (1 << 27)
145 #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
146 /** CLKACTIVATIONTIME Max Ticks */
147 #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
148 #define GPMC_CONFIG1_PAGE_LEN(val)      ((val & 3) << 23)
149 /** ATTACHEDDEVICEPAGELENGTH Max Value */
150 #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
151 #define GPMC_CONFIG1_WAIT_READ_MON      (1 << 22)
152 #define GPMC_CONFIG1_WAIT_WRITE_MON     (1 << 21)
153 #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
154 /** WAITMONITORINGTIME Max Ticks */
155 #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX  2
156 #define GPMC_CONFIG1_WAIT_PIN_SEL(val)  ((val & 3) << 16)
157 #define GPMC_CONFIG1_DEVICESIZE(val)    ((val & 3) << 12)
158 #define GPMC_CONFIG1_DEVICESIZE_16      GPMC_CONFIG1_DEVICESIZE(1)
159 /** DEVICESIZE Max Value */
160 #define GPMC_CONFIG1_DEVICESIZE_MAX     1
161 #define GPMC_CONFIG1_DEVICETYPE(val)    ((val & 3) << 10)
162 #define GPMC_CONFIG1_DEVICETYPE_NOR     GPMC_CONFIG1_DEVICETYPE(0)
163 #define GPMC_CONFIG1_MUXTYPE(val)       ((val & 3) << 8)
164 #define GPMC_CONFIG1_TIME_PARA_GRAN     (1 << 4)
165 #define GPMC_CONFIG1_FCLK_DIV(val)      (val & 3)
166 #define GPMC_CONFIG1_FCLK_DIV2          (GPMC_CONFIG1_FCLK_DIV(1))
167 #define GPMC_CONFIG1_FCLK_DIV3          (GPMC_CONFIG1_FCLK_DIV(2))
168 #define GPMC_CONFIG1_FCLK_DIV4          (GPMC_CONFIG1_FCLK_DIV(3))
169 #define GPMC_CONFIG7_CSVALID            (1 << 6)
170
171 #define GPMC_CONFIG7_BASEADDRESS_MASK   0x3f
172 #define GPMC_CONFIG7_CSVALID_MASK       BIT(6)
173 #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
174 #define GPMC_CONFIG7_MASKADDRESS_MASK   (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
175 /* All CONFIG7 bits except reserved bits */
176 #define GPMC_CONFIG7_MASK               (GPMC_CONFIG7_BASEADDRESS_MASK | \
177                                          GPMC_CONFIG7_CSVALID_MASK |     \
178                                          GPMC_CONFIG7_MASKADDRESS_MASK)
179
180 #define GPMC_DEVICETYPE_NOR             0
181 #define GPMC_DEVICETYPE_NAND            2
182 #define GPMC_CONFIG_WRITEPROTECT        0x00000010
183 #define WR_RD_PIN_MONITORING            0x00600000
184
185 /* ECC commands */
186 #define GPMC_ECC_READ           0 /* Reset Hardware ECC for read */
187 #define GPMC_ECC_WRITE          1 /* Reset Hardware ECC for write */
188 #define GPMC_ECC_READSYN        2 /* Reset before syndrom is read back */
189
190 #define GPMC_NR_NAND_IRQS       2 /* number of NAND specific IRQs */
191
192 enum gpmc_clk_domain {
193         GPMC_CD_FCLK,
194         GPMC_CD_CLK
195 };
196
197 struct gpmc_cs_data {
198         const char *name;
199
200 #define GPMC_CS_RESERVED        (1 << 0)
201         u32 flags;
202
203         struct resource mem;
204 };
205
206 /* Structure to save gpmc cs context */
207 struct gpmc_cs_config {
208         u32 config1;
209         u32 config2;
210         u32 config3;
211         u32 config4;
212         u32 config5;
213         u32 config6;
214         u32 config7;
215         int is_valid;
216 };
217
218 /*
219  * Structure to save/restore gpmc context
220  * to support core off on OMAP3
221  */
222 struct omap3_gpmc_regs {
223         u32 sysconfig;
224         u32 irqenable;
225         u32 timeout_ctrl;
226         u32 config;
227         u32 prefetch_config1;
228         u32 prefetch_config2;
229         u32 prefetch_control;
230         struct gpmc_cs_config cs_context[GPMC_CS_NUM];
231 };
232
233 struct gpmc_device {
234         struct device *dev;
235         int irq;
236         struct irq_chip irq_chip;
237         struct gpio_chip gpio_chip;
238         int nirqs;
239 };
240
241 static struct irq_domain *gpmc_irq_domain;
242
243 static struct resource  gpmc_mem_root;
244 static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
245 static DEFINE_SPINLOCK(gpmc_mem_lock);
246 /* Define chip-selects as reserved by default until probe completes */
247 static unsigned int gpmc_cs_num = GPMC_CS_NUM;
248 static unsigned int gpmc_nr_waitpins;
249 static resource_size_t phys_base, mem_size;
250 static unsigned gpmc_capability;
251 static void __iomem *gpmc_base;
252
253 static struct clk *gpmc_l3_clk;
254
255 static irqreturn_t gpmc_handle_irq(int irq, void *dev);
256
257 static void gpmc_write_reg(int idx, u32 val)
258 {
259         writel_relaxed(val, gpmc_base + idx);
260 }
261
262 static u32 gpmc_read_reg(int idx)
263 {
264         return readl_relaxed(gpmc_base + idx);
265 }
266
267 void gpmc_cs_write_reg(int cs, int idx, u32 val)
268 {
269         void __iomem *reg_addr;
270
271         reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
272         writel_relaxed(val, reg_addr);
273 }
274
275 static u32 gpmc_cs_read_reg(int cs, int idx)
276 {
277         void __iomem *reg_addr;
278
279         reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
280         return readl_relaxed(reg_addr);
281 }
282
283 /* TODO: Add support for gpmc_fck to clock framework and use it */
284 static unsigned long gpmc_get_fclk_period(void)
285 {
286         unsigned long rate = clk_get_rate(gpmc_l3_clk);
287
288         rate /= 1000;
289         rate = 1000000000 / rate;       /* In picoseconds */
290
291         return rate;
292 }
293
294 /**
295  * gpmc_get_clk_period - get period of selected clock domain in ps
296  * @cs Chip Select Region.
297  * @cd Clock Domain.
298  *
299  * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
300  * prior to calling this function with GPMC_CD_CLK.
301  */
302 static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
303 {
304
305         unsigned long tick_ps = gpmc_get_fclk_period();
306         u32 l;
307         int div;
308
309         switch (cd) {
310         case GPMC_CD_CLK:
311                 /* get current clk divider */
312                 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
313                 div = (l & 0x03) + 1;
314                 /* get GPMC_CLK period */
315                 tick_ps *= div;
316                 break;
317         case GPMC_CD_FCLK:
318                 /* FALL-THROUGH */
319         default:
320                 break;
321         }
322
323         return tick_ps;
324
325 }
326
327 static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
328                                          enum gpmc_clk_domain cd)
329 {
330         unsigned long tick_ps;
331
332         /* Calculate in picosecs to yield more exact results */
333         tick_ps = gpmc_get_clk_period(cs, cd);
334
335         return (time_ns * 1000 + tick_ps - 1) / tick_ps;
336 }
337
338 static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
339 {
340         return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
341 }
342
343 static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
344 {
345         unsigned long tick_ps;
346
347         /* Calculate in picosecs to yield more exact results */
348         tick_ps = gpmc_get_fclk_period();
349
350         return (time_ps + tick_ps - 1) / tick_ps;
351 }
352
353 static unsigned int gpmc_clk_ticks_to_ns(unsigned int ticks, int cs,
354                                          enum gpmc_clk_domain cd)
355 {
356         return ticks * gpmc_get_clk_period(cs, cd) / 1000;
357 }
358
359 unsigned int gpmc_ticks_to_ns(unsigned int ticks)
360 {
361         return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
362 }
363
364 static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
365 {
366         return ticks * gpmc_get_fclk_period();
367 }
368
369 static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
370 {
371         unsigned long ticks = gpmc_ps_to_ticks(time_ps);
372
373         return ticks * gpmc_get_fclk_period();
374 }
375
376 static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
377 {
378         u32 l;
379
380         l = gpmc_cs_read_reg(cs, reg);
381         if (value)
382                 l |= mask;
383         else
384                 l &= ~mask;
385         gpmc_cs_write_reg(cs, reg, l);
386 }
387
388 static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
389 {
390         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
391                            GPMC_CONFIG1_TIME_PARA_GRAN,
392                            p->time_para_granularity);
393         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
394                            GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
395         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
396                            GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
397         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
398                            GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
399         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
400                            GPMC_CONFIG4_WEEXTRADELAY, p->we_extra_delay);
401         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
402                            GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
403                            p->cycle2cyclesamecsen);
404         gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
405                            GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
406                            p->cycle2cyclediffcsen);
407 }
408
409 #ifdef CONFIG_OMAP_GPMC_DEBUG
410 /**
411  * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
412  * @cs:      Chip Select Region
413  * @reg:     GPMC_CS_CONFIGn register offset.
414  * @st_bit:  Start Bit
415  * @end_bit: End Bit. Must be >= @st_bit.
416  * @ma:x     Maximum parameter value (before optional @shift).
417  *           If 0, maximum is as high as @st_bit and @end_bit allow.
418  * @name:    DTS node name, w/o "gpmc,"
419  * @cd:      Clock Domain of timing parameter.
420  * @shift:   Parameter value left shifts @shift, which is then printed instead of value.
421  * @raw:     Raw Format Option.
422  *           raw format:  gpmc,name = <value>
423  *           tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
424  *           Where x ns -- y ns result in the same tick value.
425  *           When @max is exceeded, "invalid" is printed inside comment.
426  * @noval:   Parameter values equal to 0 are not printed.
427  * @return:  Specified timing parameter (after optional @shift).
428  *
429  */
430 static int get_gpmc_timing_reg(
431         /* timing specifiers */
432         int cs, int reg, int st_bit, int end_bit, int max,
433         const char *name, const enum gpmc_clk_domain cd,
434         /* value transform */
435         int shift,
436         /* format specifiers */
437         bool raw, bool noval)
438 {
439         u32 l;
440         int nr_bits;
441         int mask;
442         bool invalid;
443
444         l = gpmc_cs_read_reg(cs, reg);
445         nr_bits = end_bit - st_bit + 1;
446         mask = (1 << nr_bits) - 1;
447         l = (l >> st_bit) & mask;
448         if (!max)
449                 max = mask;
450         invalid = l > max;
451         if (shift)
452                 l = (shift << l);
453         if (noval && (l == 0))
454                 return 0;
455         if (!raw) {
456                 /* DTS tick format for timings in ns */
457                 unsigned int time_ns;
458                 unsigned int time_ns_min = 0;
459
460                 if (l)
461                         time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
462                 time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
463                 pr_info("gpmc,%s = <%u>; /* %u ns - %u ns; %i ticks%s*/\n",
464                         name, time_ns, time_ns_min, time_ns, l,
465                         invalid ? "; invalid " : " ");
466         } else {
467                 /* raw format */
468                 pr_info("gpmc,%s = <%u>;%s\n", name, l,
469                         invalid ? " /* invalid */" : "");
470         }
471
472         return l;
473 }
474
475 #define GPMC_PRINT_CONFIG(cs, config) \
476         pr_info("cs%i %s: 0x%08x\n", cs, #config, \
477                 gpmc_cs_read_reg(cs, config))
478 #define GPMC_GET_RAW(reg, st, end, field) \
479         get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
480 #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
481         get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
482 #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
483         get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
484 #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
485         get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
486 #define GPMC_GET_TICKS(reg, st, end, field) \
487         get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
488 #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
489         get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
490 #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
491         get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
492
493 static void gpmc_show_regs(int cs, const char *desc)
494 {
495         pr_info("gpmc cs%i %s:\n", cs, desc);
496         GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
497         GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
498         GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
499         GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
500         GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
501         GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
502 }
503
504 /*
505  * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
506  * see commit c9fb809.
507  */
508 static void gpmc_cs_show_timings(int cs, const char *desc)
509 {
510         gpmc_show_regs(cs, desc);
511
512         pr_info("gpmc cs%i access configuration:\n", cs);
513         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1,  4,  4, "time-para-granularity");
514         GPMC_GET_RAW(GPMC_CS_CONFIG1,  8,  9, "mux-add-data");
515         GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 12, 13, 1,
516                          GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
517         GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
518         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
519         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
520         GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
521                                GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
522                                "burst-length");
523         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
524         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
525         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
526         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
527         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
528
529         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2,  7,  7, "cs-extra-delay");
530
531         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3,  7,  7, "adv-extra-delay");
532
533         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
534         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4,  7,  7, "oe-extra-delay");
535
536         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6,  7,  7, "cycle2cycle-samecsen");
537         GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6,  6,  6, "cycle2cycle-diffcsen");
538
539         pr_info("gpmc cs%i timings configuration:\n", cs);
540         GPMC_GET_TICKS(GPMC_CS_CONFIG2,  0,  3, "cs-on-ns");
541         GPMC_GET_TICKS(GPMC_CS_CONFIG2,  8, 12, "cs-rd-off-ns");
542         GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
543
544         GPMC_GET_TICKS(GPMC_CS_CONFIG3,  0,  3, "adv-on-ns");
545         GPMC_GET_TICKS(GPMC_CS_CONFIG3,  8, 12, "adv-rd-off-ns");
546         GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
547         if (gpmc_capability & GPMC_HAS_MUX_AAD) {
548                 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
549                 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
550                                 "adv-aad-mux-rd-off-ns");
551                 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
552                                 "adv-aad-mux-wr-off-ns");
553         }
554
555         GPMC_GET_TICKS(GPMC_CS_CONFIG4,  0,  3, "oe-on-ns");
556         GPMC_GET_TICKS(GPMC_CS_CONFIG4,  8, 12, "oe-off-ns");
557         if (gpmc_capability & GPMC_HAS_MUX_AAD) {
558                 GPMC_GET_TICKS(GPMC_CS_CONFIG4,  4,  6, "oe-aad-mux-on-ns");
559                 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
560         }
561         GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
562         GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
563
564         GPMC_GET_TICKS(GPMC_CS_CONFIG5,  0,  4, "rd-cycle-ns");
565         GPMC_GET_TICKS(GPMC_CS_CONFIG5,  8, 12, "wr-cycle-ns");
566         GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
567
568         GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
569
570         GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
571         GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
572
573         GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
574                               GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
575                               "wait-monitoring-ns", GPMC_CD_CLK);
576         GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
577                               GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
578                               "clk-activation-ns", GPMC_CD_FCLK);
579
580         GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
581         GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
582 }
583 #else
584 static inline void gpmc_cs_show_timings(int cs, const char *desc)
585 {
586 }
587 #endif
588
589 /**
590  * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
591  * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
592  * prior to calling this function with @cd equal to GPMC_CD_CLK.
593  *
594  * @cs:      Chip Select Region.
595  * @reg:     GPMC_CS_CONFIGn register offset.
596  * @st_bit:  Start Bit
597  * @end_bit: End Bit. Must be >= @st_bit.
598  * @max:     Maximum parameter value.
599  *           If 0, maximum is as high as @st_bit and @end_bit allow.
600  * @time:    Timing parameter in ns.
601  * @cd:      Timing parameter clock domain.
602  * @name:    Timing parameter name.
603  * @return:  0 on success, -1 on error.
604  */
605 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
606                                int time, enum gpmc_clk_domain cd, const char *name)
607 {
608         u32 l;
609         int ticks, mask, nr_bits;
610
611         if (time == 0)
612                 ticks = 0;
613         else
614                 ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
615         nr_bits = end_bit - st_bit + 1;
616         mask = (1 << nr_bits) - 1;
617
618         if (!max)
619                 max = mask;
620
621         if (ticks > max) {
622                 pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
623                        __func__, cs, name, time, ticks, max);
624
625                 return -1;
626         }
627
628         l = gpmc_cs_read_reg(cs, reg);
629 #ifdef CONFIG_OMAP_GPMC_DEBUG
630         pr_info(
631                 "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
632                cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
633                         (l >> st_bit) & mask, time);
634 #endif
635         l &= ~(mask << st_bit);
636         l |= ticks << st_bit;
637         gpmc_cs_write_reg(cs, reg, l);
638
639         return 0;
640 }
641
642 #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd)  \
643         if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
644             t->field, (cd), #field) < 0)                       \
645                 return -1
646
647 #define GPMC_SET_ONE(reg, st, end, field) \
648         GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
649
650 /**
651  * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
652  * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
653  * read  --> don't sample bus too early
654  * write --> data is longer on bus
655  *
656  * Formula:
657  * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
658  *                    / waitmonitoring_ticks)
659  * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
660  * div <= 0 check.
661  *
662  * @wait_monitoring: WAITMONITORINGTIME in ns.
663  * @return:          -1 on failure to scale, else proper divider > 0.
664  */
665 static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
666 {
667
668         int div = gpmc_ns_to_ticks(wait_monitoring);
669
670         div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
671         div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
672
673         if (div > 4)
674                 return -1;
675         if (div <= 0)
676                 div = 1;
677
678         return div;
679
680 }
681
682 /**
683  * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
684  * @sync_clk: GPMC_CLK period in ps.
685  * @return:   Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
686  *            Else, returns -1.
687  */
688 int gpmc_calc_divider(unsigned int sync_clk)
689 {
690         int div = gpmc_ps_to_ticks(sync_clk);
691
692         if (div > 4)
693                 return -1;
694         if (div <= 0)
695                 div = 1;
696
697         return div;
698 }
699
700 /**
701  * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
702  * @cs:     Chip Select Region.
703  * @t:      GPMC timing parameters.
704  * @s:      GPMC timing settings.
705  * @return: 0 on success, -1 on error.
706  */
707 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
708                         const struct gpmc_settings *s)
709 {
710         int div;
711         u32 l;
712
713         div = gpmc_calc_divider(t->sync_clk);
714         if (div < 0)
715                 return div;
716
717         /*
718          * See if we need to change the divider for waitmonitoringtime.
719          *
720          * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
721          * pure asynchronous accesses, i.e. both read and write asynchronous.
722          * However, only do so if WAITMONITORINGTIME is actually used, i.e.
723          * either WAITREADMONITORING or WAITWRITEMONITORING is set.
724          *
725          * This statement must not change div to scale async WAITMONITORINGTIME
726          * to protect mixed synchronous and asynchronous accesses.
727          *
728          * We raise an error later if WAITMONITORINGTIME does not fit.
729          */
730         if (!s->sync_read && !s->sync_write &&
731             (s->wait_on_read || s->wait_on_write)
732            ) {
733
734                 div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
735                 if (div < 0) {
736                         pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
737                                __func__,
738                                t->wait_monitoring
739                                );
740                         return -1;
741                 }
742         }
743
744         GPMC_SET_ONE(GPMC_CS_CONFIG2,  0,  3, cs_on);
745         GPMC_SET_ONE(GPMC_CS_CONFIG2,  8, 12, cs_rd_off);
746         GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
747
748         GPMC_SET_ONE(GPMC_CS_CONFIG3,  0,  3, adv_on);
749         GPMC_SET_ONE(GPMC_CS_CONFIG3,  8, 12, adv_rd_off);
750         GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
751         if (gpmc_capability & GPMC_HAS_MUX_AAD) {
752                 GPMC_SET_ONE(GPMC_CS_CONFIG3,  4,  6, adv_aad_mux_on);
753                 GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off);
754                 GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off);
755         }
756
757         GPMC_SET_ONE(GPMC_CS_CONFIG4,  0,  3, oe_on);
758         GPMC_SET_ONE(GPMC_CS_CONFIG4,  8, 12, oe_off);
759         if (gpmc_capability & GPMC_HAS_MUX_AAD) {
760                 GPMC_SET_ONE(GPMC_CS_CONFIG4,  4,  6, oe_aad_mux_on);
761                 GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off);
762         }
763         GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
764         GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
765
766         GPMC_SET_ONE(GPMC_CS_CONFIG5,  0,  4, rd_cycle);
767         GPMC_SET_ONE(GPMC_CS_CONFIG5,  8, 12, wr_cycle);
768         GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
769
770         GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
771
772         GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
773         GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
774
775         if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
776                 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
777         if (gpmc_capability & GPMC_HAS_WR_ACCESS)
778                 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
779
780         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
781         l &= ~0x03;
782         l |= (div - 1);
783         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
784
785         GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
786                             GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
787                             wait_monitoring, GPMC_CD_CLK);
788         GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
789                             GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
790                             clk_activation, GPMC_CD_FCLK);
791
792 #ifdef CONFIG_OMAP_GPMC_DEBUG
793         pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
794                         cs, (div * gpmc_get_fclk_period()) / 1000, div);
795 #endif
796
797         gpmc_cs_bool_timings(cs, &t->bool_timings);
798         gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
799
800         return 0;
801 }
802
803 static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
804 {
805         u32 l;
806         u32 mask;
807
808         /*
809          * Ensure that base address is aligned on a
810          * boundary equal to or greater than size.
811          */
812         if (base & (size - 1))
813                 return -EINVAL;
814
815         base >>= GPMC_CHUNK_SHIFT;
816         mask = (1 << GPMC_SECTION_SHIFT) - size;
817         mask >>= GPMC_CHUNK_SHIFT;
818         mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
819
820         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
821         l &= ~GPMC_CONFIG7_MASK;
822         l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
823         l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
824         l |= GPMC_CONFIG7_CSVALID;
825         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
826
827         return 0;
828 }
829
830 static void gpmc_cs_enable_mem(int cs)
831 {
832         u32 l;
833
834         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
835         l |= GPMC_CONFIG7_CSVALID;
836         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
837 }
838
839 static void gpmc_cs_disable_mem(int cs)
840 {
841         u32 l;
842
843         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
844         l &= ~GPMC_CONFIG7_CSVALID;
845         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
846 }
847
848 static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
849 {
850         u32 l;
851         u32 mask;
852
853         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
854         *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
855         mask = (l >> 8) & 0x0f;
856         *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
857 }
858
859 static int gpmc_cs_mem_enabled(int cs)
860 {
861         u32 l;
862
863         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
864         return l & GPMC_CONFIG7_CSVALID;
865 }
866
867 static void gpmc_cs_set_reserved(int cs, int reserved)
868 {
869         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
870
871         gpmc->flags |= GPMC_CS_RESERVED;
872 }
873
874 static bool gpmc_cs_reserved(int cs)
875 {
876         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
877
878         return gpmc->flags & GPMC_CS_RESERVED;
879 }
880
881 static void gpmc_cs_set_name(int cs, const char *name)
882 {
883         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
884
885         gpmc->name = name;
886 }
887
888 static const char *gpmc_cs_get_name(int cs)
889 {
890         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
891
892         return gpmc->name;
893 }
894
895 static unsigned long gpmc_mem_align(unsigned long size)
896 {
897         int order;
898
899         size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
900         order = GPMC_CHUNK_SHIFT - 1;
901         do {
902                 size >>= 1;
903                 order++;
904         } while (size);
905         size = 1 << order;
906         return size;
907 }
908
909 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
910 {
911         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
912         struct resource *res = &gpmc->mem;
913         int r;
914
915         size = gpmc_mem_align(size);
916         spin_lock(&gpmc_mem_lock);
917         res->start = base;
918         res->end = base + size - 1;
919         r = request_resource(&gpmc_mem_root, res);
920         spin_unlock(&gpmc_mem_lock);
921
922         return r;
923 }
924
925 static int gpmc_cs_delete_mem(int cs)
926 {
927         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
928         struct resource *res = &gpmc->mem;
929         int r;
930
931         spin_lock(&gpmc_mem_lock);
932         r = release_resource(res);
933         res->start = 0;
934         res->end = 0;
935         spin_unlock(&gpmc_mem_lock);
936
937         return r;
938 }
939
940 /**
941  * gpmc_cs_remap - remaps a chip-select physical base address
942  * @cs:         chip-select to remap
943  * @base:       physical base address to re-map chip-select to
944  *
945  * Re-maps a chip-select to a new physical base address specified by
946  * "base". Returns 0 on success and appropriate negative error code
947  * on failure.
948  */
949 static int gpmc_cs_remap(int cs, u32 base)
950 {
951         int ret;
952         u32 old_base, size;
953
954         if (cs >= gpmc_cs_num) {
955                 pr_err("%s: requested chip-select is disabled\n", __func__);
956                 return -ENODEV;
957         }
958
959         /*
960          * Make sure we ignore any device offsets from the GPMC partition
961          * allocated for the chip select and that the new base confirms
962          * to the GPMC 16MB minimum granularity.
963          */ 
964         base &= ~(SZ_16M - 1);
965
966         gpmc_cs_get_memconf(cs, &old_base, &size);
967         if (base == old_base)
968                 return 0;
969
970         ret = gpmc_cs_delete_mem(cs);
971         if (ret < 0)
972                 return ret;
973
974         ret = gpmc_cs_insert_mem(cs, base, size);
975         if (ret < 0)
976                 return ret;
977
978         ret = gpmc_cs_set_memconf(cs, base, size);
979
980         return ret;
981 }
982
983 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
984 {
985         struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
986         struct resource *res = &gpmc->mem;
987         int r = -1;
988
989         if (cs >= gpmc_cs_num) {
990                 pr_err("%s: requested chip-select is disabled\n", __func__);
991                 return -ENODEV;
992         }
993         size = gpmc_mem_align(size);
994         if (size > (1 << GPMC_SECTION_SHIFT))
995                 return -ENOMEM;
996
997         spin_lock(&gpmc_mem_lock);
998         if (gpmc_cs_reserved(cs)) {
999                 r = -EBUSY;
1000                 goto out;
1001         }
1002         if (gpmc_cs_mem_enabled(cs))
1003                 r = adjust_resource(res, res->start & ~(size - 1), size);
1004         if (r < 0)
1005                 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
1006                                       size, NULL, NULL);
1007         if (r < 0)
1008                 goto out;
1009
1010         /* Disable CS while changing base address and size mask */
1011         gpmc_cs_disable_mem(cs);
1012
1013         r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
1014         if (r < 0) {
1015                 release_resource(res);
1016                 goto out;
1017         }
1018
1019         /* Enable CS */
1020         gpmc_cs_enable_mem(cs);
1021         *base = res->start;
1022         gpmc_cs_set_reserved(cs, 1);
1023 out:
1024         spin_unlock(&gpmc_mem_lock);
1025         return r;
1026 }
1027 EXPORT_SYMBOL(gpmc_cs_request);
1028
1029 void gpmc_cs_free(int cs)
1030 {
1031         struct gpmc_cs_data *gpmc;
1032         struct resource *res;
1033
1034         spin_lock(&gpmc_mem_lock);
1035         if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
1036                 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
1037                 BUG();
1038                 spin_unlock(&gpmc_mem_lock);
1039                 return;
1040         }
1041         gpmc = &gpmc_cs[cs];
1042         res = &gpmc->mem;
1043
1044         gpmc_cs_disable_mem(cs);
1045         if (res->flags)
1046                 release_resource(res);
1047         gpmc_cs_set_reserved(cs, 0);
1048         spin_unlock(&gpmc_mem_lock);
1049 }
1050 EXPORT_SYMBOL(gpmc_cs_free);
1051
1052 /**
1053  * gpmc_configure - write request to configure gpmc
1054  * @cmd: command type
1055  * @wval: value to write
1056  * @return status of the operation
1057  */
1058 int gpmc_configure(int cmd, int wval)
1059 {
1060         u32 regval;
1061
1062         switch (cmd) {
1063         case GPMC_CONFIG_WP:
1064                 regval = gpmc_read_reg(GPMC_CONFIG);
1065                 if (wval)
1066                         regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
1067                 else
1068                         regval |= GPMC_CONFIG_WRITEPROTECT;  /* WP is OFF */
1069                 gpmc_write_reg(GPMC_CONFIG, regval);
1070                 break;
1071
1072         default:
1073                 pr_err("%s: command not supported\n", __func__);
1074                 return -EINVAL;
1075         }
1076
1077         return 0;
1078 }
1079 EXPORT_SYMBOL(gpmc_configure);
1080
1081 void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
1082 {
1083         int i;
1084
1085         reg->gpmc_status = NULL;        /* deprecated */
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;
1100
1101         for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
1102                 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
1103                                            GPMC_BCH_SIZE * i;
1104                 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
1105                                            GPMC_BCH_SIZE * i;
1106                 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
1107                                            GPMC_BCH_SIZE * i;
1108                 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
1109                                            GPMC_BCH_SIZE * i;
1110                 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
1111                                            i * GPMC_BCH_SIZE;
1112                 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
1113                                            i * GPMC_BCH_SIZE;
1114                 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
1115                                            i * GPMC_BCH_SIZE;
1116         }
1117 }
1118
1119 static bool gpmc_nand_writebuffer_empty(void)
1120 {
1121         if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
1122                 return true;
1123
1124         return false;
1125 }
1126
1127 static struct gpmc_nand_ops nand_ops = {
1128         .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
1129 };
1130
1131 /**
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.
1136  *
1137  * Returns NULL on error e.g. invalid cs.
1138  */
1139 struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
1140 {
1141         if (cs >= gpmc_cs_num)
1142                 return NULL;
1143
1144         gpmc_update_nand_reg(reg, cs);
1145
1146         return &nand_ops;
1147 }
1148 EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
1149
1150 int gpmc_get_client_irq(unsigned irq_config)
1151 {
1152         if (!gpmc_irq_domain) {
1153                 pr_warn("%s called before GPMC IRQ domain available\n",
1154                         __func__);
1155                 return 0;
1156         }
1157
1158         /* we restrict this to NAND IRQs only */
1159         if (irq_config >= GPMC_NR_NAND_IRQS)
1160                 return 0;
1161
1162         return irq_create_mapping(gpmc_irq_domain, irq_config);
1163 }
1164
1165 static int gpmc_irq_endis(unsigned long hwirq, bool endis)
1166 {
1167         u32 regval;
1168
1169         /* bits GPMC_NR_NAND_IRQS to 8 are reserved */
1170         if (hwirq >= GPMC_NR_NAND_IRQS)
1171                 hwirq += 8 - GPMC_NR_NAND_IRQS;
1172
1173         regval = gpmc_read_reg(GPMC_IRQENABLE);
1174         if (endis)
1175                 regval |= BIT(hwirq);
1176         else
1177                 regval &= ~BIT(hwirq);
1178         gpmc_write_reg(GPMC_IRQENABLE, regval);
1179
1180         return 0;
1181 }
1182
1183 static void gpmc_irq_disable(struct irq_data *p)
1184 {
1185         gpmc_irq_endis(p->hwirq, false);
1186 }
1187
1188 static void gpmc_irq_enable(struct irq_data *p)
1189 {
1190         gpmc_irq_endis(p->hwirq, true);
1191 }
1192
1193 static void gpmc_irq_mask(struct irq_data *d)
1194 {
1195         gpmc_irq_endis(d->hwirq, false);
1196 }
1197
1198 static void gpmc_irq_unmask(struct irq_data *d)
1199 {
1200         gpmc_irq_endis(d->hwirq, true);
1201 }
1202
1203 static void gpmc_irq_edge_config(unsigned long hwirq, bool rising_edge)
1204 {
1205         u32 regval;
1206
1207         /* NAND IRQs polarity is not configurable */
1208         if (hwirq < GPMC_NR_NAND_IRQS)
1209                 return;
1210
1211         /* WAITPIN starts at BIT 8 */
1212         hwirq += 8 - GPMC_NR_NAND_IRQS;
1213
1214         regval = gpmc_read_reg(GPMC_CONFIG);
1215         if (rising_edge)
1216                 regval &= ~BIT(hwirq);
1217         else
1218                 regval |= BIT(hwirq);
1219
1220         gpmc_write_reg(GPMC_CONFIG, regval);
1221 }
1222
1223 static void gpmc_irq_ack(struct irq_data *d)
1224 {
1225         unsigned int hwirq = d->hwirq;
1226
1227         /* skip reserved bits */
1228         if (hwirq >= GPMC_NR_NAND_IRQS)
1229                 hwirq += 8 - GPMC_NR_NAND_IRQS;
1230
1231         /* Setting bit to 1 clears (or Acks) the interrupt */
1232         gpmc_write_reg(GPMC_IRQSTATUS, BIT(hwirq));
1233 }
1234
1235 static int gpmc_irq_set_type(struct irq_data *d, unsigned int trigger)
1236 {
1237         /* can't set type for NAND IRQs */
1238         if (d->hwirq < GPMC_NR_NAND_IRQS)
1239                 return -EINVAL;
1240
1241         /* We can support either rising or falling edge at a time */
1242         if (trigger == IRQ_TYPE_EDGE_FALLING)
1243                 gpmc_irq_edge_config(d->hwirq, false);
1244         else if (trigger == IRQ_TYPE_EDGE_RISING)
1245                 gpmc_irq_edge_config(d->hwirq, true);
1246         else
1247                 return -EINVAL;
1248
1249         return 0;
1250 }
1251
1252 static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
1253                         irq_hw_number_t hw)
1254 {
1255         struct gpmc_device *gpmc = d->host_data;
1256
1257         irq_set_chip_data(virq, gpmc);
1258         if (hw < GPMC_NR_NAND_IRQS) {
1259                 irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
1260                 irq_set_chip_and_handler(virq, &gpmc->irq_chip,
1261                                          handle_simple_irq);
1262         } else {
1263                 irq_set_chip_and_handler(virq, &gpmc->irq_chip,
1264                                          handle_edge_irq);
1265         }
1266
1267         return 0;
1268 }
1269
1270 static const struct irq_domain_ops gpmc_irq_domain_ops = {
1271         .map    = gpmc_irq_map,
1272         .xlate  = irq_domain_xlate_twocell,
1273 };
1274
1275 static irqreturn_t gpmc_handle_irq(int irq, void *data)
1276 {
1277         int hwirq, virq;
1278         u32 regval, regvalx;
1279         struct gpmc_device *gpmc = data;
1280
1281         regval = gpmc_read_reg(GPMC_IRQSTATUS);
1282         regvalx = regval;
1283
1284         if (!regval)
1285                 return IRQ_NONE;
1286
1287         for (hwirq = 0; hwirq < gpmc->nirqs; hwirq++) {
1288                 /* skip reserved status bits */
1289                 if (hwirq == GPMC_NR_NAND_IRQS)
1290                         regvalx >>= 8 - GPMC_NR_NAND_IRQS;
1291
1292                 if (regvalx & BIT(hwirq)) {
1293                         virq = irq_find_mapping(gpmc_irq_domain, hwirq);
1294                         if (!virq) {
1295                                 dev_warn(gpmc->dev,
1296                                          "spurious irq detected hwirq %d, virq %d\n",
1297                                          hwirq, virq);
1298                         }
1299
1300                         generic_handle_irq(virq);
1301                 }
1302         }
1303
1304         gpmc_write_reg(GPMC_IRQSTATUS, regval);
1305
1306         return IRQ_HANDLED;
1307 }
1308
1309 static int gpmc_setup_irq(struct gpmc_device *gpmc)
1310 {
1311         u32 regval;
1312         int rc;
1313
1314         /* Disable interrupts */
1315         gpmc_write_reg(GPMC_IRQENABLE, 0);
1316
1317         /* clear interrupts */
1318         regval = gpmc_read_reg(GPMC_IRQSTATUS);
1319         gpmc_write_reg(GPMC_IRQSTATUS, regval);
1320
1321         gpmc->irq_chip.name = "gpmc";
1322         gpmc->irq_chip.irq_enable = gpmc_irq_enable;
1323         gpmc->irq_chip.irq_disable = gpmc_irq_disable;
1324         gpmc->irq_chip.irq_ack = gpmc_irq_ack;
1325         gpmc->irq_chip.irq_mask = gpmc_irq_mask;
1326         gpmc->irq_chip.irq_unmask = gpmc_irq_unmask;
1327         gpmc->irq_chip.irq_set_type = gpmc_irq_set_type;
1328
1329         gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
1330                                                 gpmc->nirqs,
1331                                                 &gpmc_irq_domain_ops,
1332                                                 gpmc);
1333         if (!gpmc_irq_domain) {
1334                 dev_err(gpmc->dev, "IRQ domain add failed\n");
1335                 return -ENODEV;
1336         }
1337
1338         rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
1339         if (rc) {
1340                 dev_err(gpmc->dev, "failed to request irq %d: %d\n",
1341                         gpmc->irq, rc);
1342                 irq_domain_remove(gpmc_irq_domain);
1343                 gpmc_irq_domain = NULL;
1344         }
1345
1346         return rc;
1347 }
1348
1349 static int gpmc_free_irq(struct gpmc_device *gpmc)
1350 {
1351         int hwirq;
1352
1353         free_irq(gpmc->irq, gpmc);
1354
1355         for (hwirq = 0; hwirq < gpmc->nirqs; hwirq++)
1356                 irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
1357
1358         irq_domain_remove(gpmc_irq_domain);
1359         gpmc_irq_domain = NULL;
1360
1361         return 0;
1362 }
1363
1364 static void gpmc_mem_exit(void)
1365 {
1366         int cs;
1367
1368         for (cs = 0; cs < gpmc_cs_num; cs++) {
1369                 if (!gpmc_cs_mem_enabled(cs))
1370                         continue;
1371                 gpmc_cs_delete_mem(cs);
1372         }
1373
1374 }
1375
1376 static void gpmc_mem_init(void)
1377 {
1378         int cs;
1379
1380         gpmc_mem_root.start = GPMC_MEM_START;
1381         gpmc_mem_root.end = GPMC_MEM_END;
1382
1383         /* Reserve all regions that has been set up by bootloader */
1384         for (cs = 0; cs < gpmc_cs_num; cs++) {
1385                 u32 base, size;
1386
1387                 if (!gpmc_cs_mem_enabled(cs))
1388                         continue;
1389                 gpmc_cs_get_memconf(cs, &base, &size);
1390                 if (gpmc_cs_insert_mem(cs, base, size)) {
1391                         pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
1392                                 __func__, cs, base, base + size);
1393                         gpmc_cs_disable_mem(cs);
1394                 }
1395         }
1396 }
1397
1398 static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
1399 {
1400         u32 temp;
1401         int div;
1402
1403         div = gpmc_calc_divider(sync_clk);
1404         temp = gpmc_ps_to_ticks(time_ps);
1405         temp = (temp + div - 1) / div;
1406         return gpmc_ticks_to_ps(temp * div);
1407 }
1408
1409 /* XXX: can the cycles be avoided ? */
1410 static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
1411                                        struct gpmc_device_timings *dev_t,
1412                                        bool mux)
1413 {
1414         u32 temp;
1415
1416         /* adv_rd_off */
1417         temp = dev_t->t_avdp_r;
1418         /* XXX: mux check required ? */
1419         if (mux) {
1420                 /* XXX: t_avdp not to be required for sync, only added for tusb
1421                  * this indirectly necessitates requirement of t_avdp_r and
1422                  * t_avdp_w instead of having a single t_avdp
1423                  */
1424                 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
1425                 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1426         }
1427         gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1428
1429         /* oe_on */
1430         temp = dev_t->t_oeasu; /* XXX: remove this ? */
1431         if (mux) {
1432                 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
1433                 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
1434                                 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
1435         }
1436         gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1437
1438         /* access */
1439         /* XXX: any scope for improvement ?, by combining oe_on
1440          * and clk_activation, need to check whether
1441          * access = clk_activation + round to sync clk ?
1442          */
1443         temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
1444         temp += gpmc_t->clk_activation;
1445         if (dev_t->cyc_oe)
1446                 temp = max_t(u32, temp, gpmc_t->oe_on +
1447                                 gpmc_ticks_to_ps(dev_t->cyc_oe));
1448         gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1449
1450         gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1451         gpmc_t->cs_rd_off = gpmc_t->oe_off;
1452
1453         /* rd_cycle */
1454         temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
1455         temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
1456                                                         gpmc_t->access;
1457         /* XXX: barter t_ce_rdyz with t_cez_r ? */
1458         if (dev_t->t_ce_rdyz)
1459                 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
1460         gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1461
1462         return 0;
1463 }
1464
1465 static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
1466                                         struct gpmc_device_timings *dev_t,
1467                                         bool mux)
1468 {
1469         u32 temp;
1470
1471         /* adv_wr_off */
1472         temp = dev_t->t_avdp_w;
1473         if (mux) {
1474                 temp = max_t(u32, temp,
1475                         gpmc_t->clk_activation + dev_t->t_avdh);
1476                 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1477         }
1478         gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1479
1480         /* wr_data_mux_bus */
1481         temp = max_t(u32, dev_t->t_weasu,
1482                         gpmc_t->clk_activation + dev_t->t_rdyo);
1483         /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
1484          * and in that case remember to handle we_on properly
1485          */
1486         if (mux) {
1487                 temp = max_t(u32, temp,
1488                         gpmc_t->adv_wr_off + dev_t->t_aavdh);
1489                 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1490                                 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1491         }
1492         gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1493
1494         /* we_on */
1495         if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1496                 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1497         else
1498                 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1499
1500         /* wr_access */
1501         /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
1502         gpmc_t->wr_access = gpmc_t->access;
1503
1504         /* we_off */
1505         temp = gpmc_t->we_on + dev_t->t_wpl;
1506         temp = max_t(u32, temp,
1507                         gpmc_t->wr_access + gpmc_ticks_to_ps(1));
1508         temp = max_t(u32, temp,
1509                 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
1510         gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1511
1512         gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1513                                                         dev_t->t_wph);
1514
1515         /* wr_cycle */
1516         temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
1517         temp += gpmc_t->wr_access;
1518         /* XXX: barter t_ce_rdyz with t_cez_w ? */
1519         if (dev_t->t_ce_rdyz)
1520                 temp = max_t(u32, temp,
1521                                  gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
1522         gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1523
1524         return 0;
1525 }
1526
1527 static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
1528                                         struct gpmc_device_timings *dev_t,
1529                                         bool mux)
1530 {
1531         u32 temp;
1532
1533         /* adv_rd_off */
1534         temp = dev_t->t_avdp_r;
1535         if (mux)
1536                 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1537         gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1538
1539         /* oe_on */
1540         temp = dev_t->t_oeasu;
1541         if (mux)
1542                 temp = max_t(u32, temp,
1543                         gpmc_t->adv_rd_off + dev_t->t_aavdh);
1544         gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1545
1546         /* access */
1547         temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1548                                 gpmc_t->oe_on + dev_t->t_oe);
1549         temp = max_t(u32, temp,
1550                                 gpmc_t->cs_on + dev_t->t_ce);
1551         temp = max_t(u32, temp,
1552                                 gpmc_t->adv_on + dev_t->t_aa);
1553         gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1554
1555         gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1556         gpmc_t->cs_rd_off = gpmc_t->oe_off;
1557
1558         /* rd_cycle */
1559         temp = max_t(u32, dev_t->t_rd_cycle,
1560                         gpmc_t->cs_rd_off + dev_t->t_cez_r);
1561         temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1562         gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1563
1564         return 0;
1565 }
1566
1567 static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
1568                                          struct gpmc_device_timings *dev_t,
1569                                          bool mux)
1570 {
1571         u32 temp;
1572
1573         /* adv_wr_off */
1574         temp = dev_t->t_avdp_w;
1575         if (mux)
1576                 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1577         gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1578
1579         /* wr_data_mux_bus */
1580         temp = dev_t->t_weasu;
1581         if (mux) {
1582                 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1583                 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1584                                 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1585         }
1586         gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1587
1588         /* we_on */
1589         if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1590                 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1591         else
1592                 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1593
1594         /* we_off */
1595         temp = gpmc_t->we_on + dev_t->t_wpl;
1596         gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1597
1598         gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1599                                                         dev_t->t_wph);
1600
1601         /* wr_cycle */
1602         temp = max_t(u32, dev_t->t_wr_cycle,
1603                                 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1604         gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1605
1606         return 0;
1607 }
1608
1609 static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1610                         struct gpmc_device_timings *dev_t)
1611 {
1612         u32 temp;
1613
1614         gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1615                                                 gpmc_get_fclk_period();
1616
1617         gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1618                                         dev_t->t_bacc,
1619                                         gpmc_t->sync_clk);
1620
1621         temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1622         gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1623
1624         if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1625                 return 0;
1626
1627         if (dev_t->ce_xdelay)
1628                 gpmc_t->bool_timings.cs_extra_delay = true;
1629         if (dev_t->avd_xdelay)
1630                 gpmc_t->bool_timings.adv_extra_delay = true;
1631         if (dev_t->oe_xdelay)
1632                 gpmc_t->bool_timings.oe_extra_delay = true;
1633         if (dev_t->we_xdelay)
1634                 gpmc_t->bool_timings.we_extra_delay = true;
1635
1636         return 0;
1637 }
1638
1639 static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
1640                                     struct gpmc_device_timings *dev_t,
1641                                     bool sync)
1642 {
1643         u32 temp;
1644
1645         /* cs_on */
1646         gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1647
1648         /* adv_on */
1649         temp = dev_t->t_avdasu;
1650         if (dev_t->t_ce_avd)
1651                 temp = max_t(u32, temp,
1652                                 gpmc_t->cs_on + dev_t->t_ce_avd);
1653         gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1654
1655         if (sync)
1656                 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1657
1658         return 0;
1659 }
1660
1661 /* TODO: remove this function once all peripherals are confirmed to
1662  * work with generic timing. Simultaneously gpmc_cs_set_timings()
1663  * has to be modified to handle timings in ps instead of ns
1664 */
1665 static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1666 {
1667         t->cs_on /= 1000;
1668         t->cs_rd_off /= 1000;
1669         t->cs_wr_off /= 1000;
1670         t->adv_on /= 1000;
1671         t->adv_rd_off /= 1000;
1672         t->adv_wr_off /= 1000;
1673         t->we_on /= 1000;
1674         t->we_off /= 1000;
1675         t->oe_on /= 1000;
1676         t->oe_off /= 1000;
1677         t->page_burst_access /= 1000;
1678         t->access /= 1000;
1679         t->rd_cycle /= 1000;
1680         t->wr_cycle /= 1000;
1681         t->bus_turnaround /= 1000;
1682         t->cycle2cycle_delay /= 1000;
1683         t->wait_monitoring /= 1000;
1684         t->clk_activation /= 1000;
1685         t->wr_access /= 1000;
1686         t->wr_data_mux_bus /= 1000;
1687 }
1688
1689 int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
1690                       struct gpmc_settings *gpmc_s,
1691                       struct gpmc_device_timings *dev_t)
1692 {
1693         bool mux = false, sync = false;
1694
1695         if (gpmc_s) {
1696                 mux = gpmc_s->mux_add_data ? true : false;
1697                 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1698         }
1699
1700         memset(gpmc_t, 0, sizeof(*gpmc_t));
1701
1702         gpmc_calc_common_timings(gpmc_t, dev_t, sync);
1703
1704         if (gpmc_s && gpmc_s->sync_read)
1705                 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
1706         else
1707                 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
1708
1709         if (gpmc_s && gpmc_s->sync_write)
1710                 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
1711         else
1712                 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
1713
1714         /* TODO: remove, see function definition */
1715         gpmc_convert_ps_to_ns(gpmc_t);
1716
1717         return 0;
1718 }
1719
1720 /**
1721  * gpmc_cs_program_settings - programs non-timing related settings
1722  * @cs:         GPMC chip-select to program
1723  * @p:          pointer to GPMC settings structure
1724  *
1725  * Programs non-timing related settings for a GPMC chip-select, such as
1726  * bus-width, burst configuration, etc. Function should be called once
1727  * for each chip-select that is being used and must be called before
1728  * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1729  * register will be initialised to zero by this function. Returns 0 on
1730  * success and appropriate negative error code on failure.
1731  */
1732 int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1733 {
1734         u32 config1;
1735
1736         if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1737                 pr_err("%s: invalid width %d!", __func__, p->device_width);
1738                 return -EINVAL;
1739         }
1740
1741         /* Address-data multiplexing not supported for NAND devices */
1742         if (p->device_nand && p->mux_add_data) {
1743                 pr_err("%s: invalid configuration!\n", __func__);
1744                 return -EINVAL;
1745         }
1746
1747         if ((p->mux_add_data > GPMC_MUX_AD) ||
1748             ((p->mux_add_data == GPMC_MUX_AAD) &&
1749              !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1750                 pr_err("%s: invalid multiplex configuration!\n", __func__);
1751                 return -EINVAL;
1752         }
1753
1754         /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1755         if (p->burst_read || p->burst_write) {
1756                 switch (p->burst_len) {
1757                 case GPMC_BURST_4:
1758                 case GPMC_BURST_8:
1759                 case GPMC_BURST_16:
1760                         break;
1761                 default:
1762                         pr_err("%s: invalid page/burst-length (%d)\n",
1763                                __func__, p->burst_len);
1764                         return -EINVAL;
1765                 }
1766         }
1767
1768         if (p->wait_pin > gpmc_nr_waitpins) {
1769                 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1770                 return -EINVAL;
1771         }
1772
1773         config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1774
1775         if (p->sync_read)
1776                 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1777         if (p->sync_write)
1778                 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1779         if (p->wait_on_read)
1780                 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1781         if (p->wait_on_write)
1782                 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1783         if (p->wait_on_read || p->wait_on_write)
1784                 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1785         if (p->device_nand)
1786                 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1787         if (p->mux_add_data)
1788                 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1789         if (p->burst_read)
1790                 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1791         if (p->burst_write)
1792                 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1793         if (p->burst_read || p->burst_write) {
1794                 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1795                 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1796         }
1797
1798         gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1799
1800         return 0;
1801 }
1802
1803 #ifdef CONFIG_OF
1804 static const struct of_device_id gpmc_dt_ids[] = {
1805         { .compatible = "ti,omap2420-gpmc" },
1806         { .compatible = "ti,omap2430-gpmc" },
1807         { .compatible = "ti,omap3430-gpmc" },   /* omap3430 & omap3630 */
1808         { .compatible = "ti,omap4430-gpmc" },   /* omap4430 & omap4460 & omap543x */
1809         { .compatible = "ti,am3352-gpmc" },     /* am335x devices */
1810         { }
1811 };
1812
1813 /**
1814  * gpmc_read_settings_dt - read gpmc settings from device-tree
1815  * @np:         pointer to device-tree node for a gpmc child device
1816  * @p:          pointer to gpmc settings structure
1817  *
1818  * Reads the GPMC settings for a GPMC child device from device-tree and
1819  * stores them in the GPMC settings structure passed. The GPMC settings
1820  * structure is initialised to zero by this function and so any
1821  * previously stored settings will be cleared.
1822  */
1823 void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1824 {
1825         memset(p, 0, sizeof(struct gpmc_settings));
1826
1827         p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1828         p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
1829         of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1830         of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1831
1832         if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1833                 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1834                 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1835                 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1836                 if (!p->burst_read && !p->burst_write)
1837                         pr_warn("%s: page/burst-length set but not used!\n",
1838                                 __func__);
1839         }
1840
1841         if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1842                 p->wait_on_read = of_property_read_bool(np,
1843                                                         "gpmc,wait-on-read");
1844                 p->wait_on_write = of_property_read_bool(np,
1845                                                          "gpmc,wait-on-write");
1846                 if (!p->wait_on_read && !p->wait_on_write)
1847                         pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1848                                  __func__);
1849         }
1850 }
1851
1852 static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1853                                                 struct gpmc_timings *gpmc_t)
1854 {
1855         struct gpmc_bool_timings *p;
1856
1857         if (!np || !gpmc_t)
1858                 return;
1859
1860         memset(gpmc_t, 0, sizeof(*gpmc_t));
1861
1862         /* minimum clock period for syncronous mode */
1863         of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
1864
1865         /* chip select timtings */
1866         of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1867         of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1868         of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
1869
1870         /* ADV signal timings */
1871         of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1872         of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1873         of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
1874         of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
1875                              &gpmc_t->adv_aad_mux_on);
1876         of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
1877                              &gpmc_t->adv_aad_mux_rd_off);
1878         of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
1879                              &gpmc_t->adv_aad_mux_wr_off);
1880
1881         /* WE signal timings */
1882         of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1883         of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
1884
1885         /* OE signal timings */
1886         of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1887         of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
1888         of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
1889                              &gpmc_t->oe_aad_mux_on);
1890         of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
1891                              &gpmc_t->oe_aad_mux_off);
1892
1893         /* access and cycle timings */
1894         of_property_read_u32(np, "gpmc,page-burst-access-ns",
1895                              &gpmc_t->page_burst_access);
1896         of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1897         of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1898         of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1899         of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1900                              &gpmc_t->bus_turnaround);
1901         of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1902                              &gpmc_t->cycle2cycle_delay);
1903         of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1904                              &gpmc_t->wait_monitoring);
1905         of_property_read_u32(np, "gpmc,clk-activation-ns",
1906                              &gpmc_t->clk_activation);
1907
1908         /* only applicable to OMAP3+ */
1909         of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1910         of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1911                              &gpmc_t->wr_data_mux_bus);
1912
1913         /* bool timing parameters */
1914         p = &gpmc_t->bool_timings;
1915
1916         p->cycle2cyclediffcsen =
1917                 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1918         p->cycle2cyclesamecsen =
1919                 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1920         p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1921         p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1922         p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1923         p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1924         p->time_para_granularity =
1925                 of_property_read_bool(np, "gpmc,time-para-granularity");
1926 }
1927
1928 #if IS_ENABLED(CONFIG_MTD_ONENAND)
1929 static int gpmc_probe_onenand_child(struct platform_device *pdev,
1930                                  struct device_node *child)
1931 {
1932         u32 val;
1933         struct omap_onenand_platform_data *gpmc_onenand_data;
1934
1935         if (of_property_read_u32(child, "reg", &val) < 0) {
1936                 dev_err(&pdev->dev, "%pOF has no 'reg' property\n",
1937                         child);
1938                 return -ENODEV;
1939         }
1940
1941         gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1942                                          GFP_KERNEL);
1943         if (!gpmc_onenand_data)
1944                 return -ENOMEM;
1945
1946         gpmc_onenand_data->cs = val;
1947         gpmc_onenand_data->of_node = child;
1948         gpmc_onenand_data->dma_channel = -1;
1949
1950         if (!of_property_read_u32(child, "dma-channel", &val))
1951                 gpmc_onenand_data->dma_channel = val;
1952
1953         return gpmc_onenand_init(gpmc_onenand_data);
1954 }
1955 #else
1956 static int gpmc_probe_onenand_child(struct platform_device *pdev,
1957                                     struct device_node *child)
1958 {
1959         return 0;
1960 }
1961 #endif
1962
1963 /**
1964  * gpmc_probe_generic_child - configures the gpmc for a child device
1965  * @pdev:       pointer to gpmc platform device
1966  * @child:      pointer to device-tree node for child device
1967  *
1968  * Allocates and configures a GPMC chip-select for a child device.
1969  * Returns 0 on success and appropriate negative error code on failure.
1970  */
1971 static int gpmc_probe_generic_child(struct platform_device *pdev,
1972                                 struct device_node *child)
1973 {
1974         struct gpmc_settings gpmc_s;
1975         struct gpmc_timings gpmc_t;
1976         struct resource res;
1977         unsigned long base;
1978         const char *name;
1979         int ret, cs;
1980         u32 val;
1981         struct gpio_desc *waitpin_desc = NULL;
1982         struct gpmc_device *gpmc = platform_get_drvdata(pdev);
1983
1984         if (of_property_read_u32(child, "reg", &cs) < 0) {
1985                 dev_err(&pdev->dev, "%pOF has no 'reg' property\n",
1986                         child);
1987                 return -ENODEV;
1988         }
1989
1990         if (of_address_to_resource(child, 0, &res) < 0) {
1991                 dev_err(&pdev->dev, "%pOF has malformed 'reg' property\n",
1992                         child);
1993                 return -ENODEV;
1994         }
1995
1996         /*
1997          * Check if we have multiple instances of the same device
1998          * on a single chip select. If so, use the already initialized
1999          * timings.
2000          */
2001         name = gpmc_cs_get_name(cs);
2002         if (name && child->name && of_node_cmp(child->name, name) == 0)
2003                         goto no_timings;
2004
2005         ret = gpmc_cs_request(cs, resource_size(&res), &base);
2006         if (ret < 0) {
2007                 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
2008                 return ret;
2009         }
2010         gpmc_cs_set_name(cs, child->name);
2011
2012         gpmc_read_settings_dt(child, &gpmc_s);
2013         gpmc_read_timings_dt(child, &gpmc_t);
2014
2015         /*
2016          * For some GPMC devices we still need to rely on the bootloader
2017          * timings because the devices can be connected via FPGA.
2018          * REVISIT: Add timing support from slls644g.pdf.
2019          */
2020         if (!gpmc_t.cs_rd_off) {
2021                 WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
2022                         cs);
2023                 gpmc_cs_show_timings(cs,
2024                                      "please add GPMC bootloader timings to .dts");
2025                 goto no_timings;
2026         }
2027
2028         /* CS must be disabled while making changes to gpmc configuration */
2029         gpmc_cs_disable_mem(cs);
2030
2031         /*
2032          * FIXME: gpmc_cs_request() will map the CS to an arbitary
2033          * location in the gpmc address space. When booting with
2034          * device-tree we want the NOR flash to be mapped to the
2035          * location specified in the device-tree blob. So remap the
2036          * CS to this location. Once DT migration is complete should
2037          * just make gpmc_cs_request() map a specific address.
2038          */
2039         ret = gpmc_cs_remap(cs, res.start);
2040         if (ret < 0) {
2041                 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
2042                         cs, &res.start);
2043                 if (res.start < GPMC_MEM_START) {
2044                         dev_info(&pdev->dev,
2045                                  "GPMC CS %d start cannot be lesser than 0x%x\n",
2046                                  cs, GPMC_MEM_START);
2047                 } else if (res.end > GPMC_MEM_END) {
2048                         dev_info(&pdev->dev,
2049                                  "GPMC CS %d end cannot be greater than 0x%x\n",
2050                                  cs, GPMC_MEM_END);
2051                 }
2052                 goto err;
2053         }
2054
2055         if (of_node_cmp(child->name, "nand") == 0) {
2056                 /* Warn about older DT blobs with no compatible property */
2057                 if (!of_property_read_bool(child, "compatible")) {
2058                         dev_warn(&pdev->dev,
2059                                  "Incompatible NAND node: missing compatible");
2060                         ret = -EINVAL;
2061                         goto err;
2062                 }
2063         }
2064
2065         if (of_device_is_compatible(child, "ti,omap2-nand")) {
2066                 /* NAND specific setup */
2067                 val = 8;
2068                 of_property_read_u32(child, "nand-bus-width", &val);
2069                 switch (val) {
2070                 case 8:
2071                         gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
2072                         break;
2073                 case 16:
2074                         gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
2075                         break;
2076                 default:
2077                         dev_err(&pdev->dev, "%s: invalid 'nand-bus-width'\n",
2078                                 child->name);
2079                         ret = -EINVAL;
2080                         goto err;
2081                 }
2082
2083                 /* disable write protect */
2084                 gpmc_configure(GPMC_CONFIG_WP, 0);
2085                 gpmc_s.device_nand = true;
2086         } else {
2087                 ret = of_property_read_u32(child, "bank-width",
2088                                            &gpmc_s.device_width);
2089                 if (ret < 0) {
2090                         dev_err(&pdev->dev, "%pOF has no 'bank-width' property\n",
2091                                 child);
2092                         goto err;
2093                 }
2094         }
2095
2096         /* Reserve wait pin if it is required and valid */
2097         if (gpmc_s.wait_on_read || gpmc_s.wait_on_write) {
2098                 unsigned int wait_pin = gpmc_s.wait_pin;
2099
2100                 waitpin_desc = gpiochip_request_own_desc(&gpmc->gpio_chip,
2101                                                          wait_pin, "WAITPIN");
2102                 if (IS_ERR(waitpin_desc)) {
2103                         dev_err(&pdev->dev, "invalid wait-pin: %d\n", wait_pin);
2104                         ret = PTR_ERR(waitpin_desc);
2105                         goto err;
2106                 }
2107         }
2108
2109         gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
2110
2111         ret = gpmc_cs_program_settings(cs, &gpmc_s);
2112         if (ret < 0)
2113                 goto err_cs;
2114
2115         ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
2116         if (ret) {
2117                 dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
2118                         child->name);
2119                 goto err_cs;
2120         }
2121
2122         /* Clear limited address i.e. enable A26-A11 */
2123         val = gpmc_read_reg(GPMC_CONFIG);
2124         val &= ~GPMC_CONFIG_LIMITEDADDRESS;
2125         gpmc_write_reg(GPMC_CONFIG, val);
2126
2127         /* Enable CS region */
2128         gpmc_cs_enable_mem(cs);
2129
2130 no_timings:
2131
2132         /* create platform device, NULL on error or when disabled */
2133         if (!of_platform_device_create(child, NULL, &pdev->dev))
2134                 goto err_child_fail;
2135
2136         /* is child a common bus? */
2137         if (of_match_node(of_default_bus_match_table, child))
2138                 /* create children and other common bus children */
2139                 if (of_platform_default_populate(child, NULL, &pdev->dev))
2140                         goto err_child_fail;
2141
2142         return 0;
2143
2144 err_child_fail:
2145
2146         dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
2147         ret = -ENODEV;
2148
2149 err_cs:
2150         gpiochip_free_own_desc(waitpin_desc);
2151 err:
2152         gpmc_cs_free(cs);
2153
2154         return ret;
2155 }
2156
2157 static int gpmc_probe_dt(struct platform_device *pdev)
2158 {
2159         int ret;
2160         const struct of_device_id *of_id =
2161                 of_match_device(gpmc_dt_ids, &pdev->dev);
2162
2163         if (!of_id)
2164                 return 0;
2165
2166         ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
2167                                    &gpmc_cs_num);
2168         if (ret < 0) {
2169                 pr_err("%s: number of chip-selects not defined\n", __func__);
2170                 return ret;
2171         } else if (gpmc_cs_num < 1) {
2172                 pr_err("%s: all chip-selects are disabled\n", __func__);
2173                 return -EINVAL;
2174         } else if (gpmc_cs_num > GPMC_CS_NUM) {
2175                 pr_err("%s: number of supported chip-selects cannot be > %d\n",
2176                                          __func__, GPMC_CS_NUM);
2177                 return -EINVAL;
2178         }
2179
2180         ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
2181                                    &gpmc_nr_waitpins);
2182         if (ret < 0) {
2183                 pr_err("%s: number of wait pins not found!\n", __func__);
2184                 return ret;
2185         }
2186
2187         return 0;
2188 }
2189
2190 static void gpmc_probe_dt_children(struct platform_device *pdev)
2191 {
2192         int ret;
2193         struct device_node *child;
2194
2195         for_each_available_child_of_node(pdev->dev.of_node, child) {
2196
2197                 if (!child->name)
2198                         continue;
2199
2200                 if (of_node_cmp(child->name, "onenand") == 0)
2201                         ret = gpmc_probe_onenand_child(pdev, child);
2202                 else
2203                         ret = gpmc_probe_generic_child(pdev, child);
2204
2205                 if (ret) {
2206                         dev_err(&pdev->dev, "failed to probe DT child '%s': %d\n",
2207                                 child->name, ret);
2208                 }
2209         }
2210 }
2211 #else
2212 static int gpmc_probe_dt(struct platform_device *pdev)
2213 {
2214         return 0;
2215 }
2216
2217 static void gpmc_probe_dt_children(struct platform_device *pdev)
2218 {
2219 }
2220 #endif /* CONFIG_OF */
2221
2222 static int gpmc_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
2223 {
2224         return 1;       /* we're input only */
2225 }
2226
2227 static int gpmc_gpio_direction_input(struct gpio_chip *chip,
2228                                      unsigned int offset)
2229 {
2230         return 0;       /* we're input only */
2231 }
2232
2233 static int gpmc_gpio_direction_output(struct gpio_chip *chip,
2234                                       unsigned int offset, int value)
2235 {
2236         return -EINVAL; /* we're input only */
2237 }
2238
2239 static void gpmc_gpio_set(struct gpio_chip *chip, unsigned int offset,
2240                           int value)
2241 {
2242 }
2243
2244 static int gpmc_gpio_get(struct gpio_chip *chip, unsigned int offset)
2245 {
2246         u32 reg;
2247
2248         offset += 8;
2249
2250         reg = gpmc_read_reg(GPMC_STATUS) & BIT(offset);
2251
2252         return !!reg;
2253 }
2254
2255 static int gpmc_gpio_init(struct gpmc_device *gpmc)
2256 {
2257         int ret;
2258
2259         gpmc->gpio_chip.parent = gpmc->dev;
2260         gpmc->gpio_chip.owner = THIS_MODULE;
2261         gpmc->gpio_chip.label = DEVICE_NAME;
2262         gpmc->gpio_chip.ngpio = gpmc_nr_waitpins;
2263         gpmc->gpio_chip.get_direction = gpmc_gpio_get_direction;
2264         gpmc->gpio_chip.direction_input = gpmc_gpio_direction_input;
2265         gpmc->gpio_chip.direction_output = gpmc_gpio_direction_output;
2266         gpmc->gpio_chip.set = gpmc_gpio_set;
2267         gpmc->gpio_chip.get = gpmc_gpio_get;
2268         gpmc->gpio_chip.base = -1;
2269
2270         ret = devm_gpiochip_add_data(gpmc->dev, &gpmc->gpio_chip, NULL);
2271         if (ret < 0) {
2272                 dev_err(gpmc->dev, "could not register gpio chip: %d\n", ret);
2273                 return ret;
2274         }
2275
2276         return 0;
2277 }
2278
2279 static int gpmc_probe(struct platform_device *pdev)
2280 {
2281         int rc;
2282         u32 l;
2283         struct resource *res;
2284         struct gpmc_device *gpmc;
2285
2286         gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
2287         if (!gpmc)
2288                 return -ENOMEM;
2289
2290         gpmc->dev = &pdev->dev;
2291         platform_set_drvdata(pdev, gpmc);
2292
2293         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2294         if (res == NULL)
2295                 return -ENOENT;
2296
2297         phys_base = res->start;
2298         mem_size = resource_size(res);
2299
2300         gpmc_base = devm_ioremap_resource(&pdev->dev, res);
2301         if (IS_ERR(gpmc_base))
2302                 return PTR_ERR(gpmc_base);
2303
2304         res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2305         if (!res) {
2306                 dev_err(&pdev->dev, "Failed to get resource: irq\n");
2307                 return -ENOENT;
2308         }
2309
2310         gpmc->irq = res->start;
2311
2312         gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
2313         if (IS_ERR(gpmc_l3_clk)) {
2314                 dev_err(&pdev->dev, "Failed to get GPMC fck\n");
2315                 return PTR_ERR(gpmc_l3_clk);
2316         }
2317
2318         if (!clk_get_rate(gpmc_l3_clk)) {
2319                 dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
2320                 return -EINVAL;
2321         }
2322
2323         if (pdev->dev.of_node) {
2324                 rc = gpmc_probe_dt(pdev);
2325                 if (rc)
2326                         return rc;
2327         } else {
2328                 gpmc_cs_num = GPMC_CS_NUM;
2329                 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
2330         }
2331
2332         pm_runtime_enable(&pdev->dev);
2333         pm_runtime_get_sync(&pdev->dev);
2334
2335         l = gpmc_read_reg(GPMC_REVISION);
2336
2337         /*
2338          * FIXME: Once device-tree migration is complete the below flags
2339          * should be populated based upon the device-tree compatible
2340          * string. For now just use the IP revision. OMAP3+ devices have
2341          * the wr_access and wr_data_mux_bus register fields. OMAP4+
2342          * devices support the addr-addr-data multiplex protocol.
2343          *
2344          * GPMC IP revisions:
2345          * - OMAP24xx                   = 2.0
2346          * - OMAP3xxx                   = 5.0
2347          * - OMAP44xx/54xx/AM335x       = 6.0
2348          */
2349         if (GPMC_REVISION_MAJOR(l) > 0x4)
2350                 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
2351         if (GPMC_REVISION_MAJOR(l) > 0x5)
2352                 gpmc_capability |= GPMC_HAS_MUX_AAD;
2353         dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
2354                  GPMC_REVISION_MINOR(l));
2355
2356         gpmc_mem_init();
2357         rc = gpmc_gpio_init(gpmc);
2358         if (rc)
2359                 goto gpio_init_failed;
2360
2361         gpmc->nirqs = GPMC_NR_NAND_IRQS + gpmc_nr_waitpins;
2362         rc = gpmc_setup_irq(gpmc);
2363         if (rc) {
2364                 dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
2365                 goto gpio_init_failed;
2366         }
2367
2368         gpmc_probe_dt_children(pdev);
2369
2370         return 0;
2371
2372 gpio_init_failed:
2373         gpmc_mem_exit();
2374         pm_runtime_put_sync(&pdev->dev);
2375         pm_runtime_disable(&pdev->dev);
2376
2377         return rc;
2378 }
2379
2380 static int gpmc_remove(struct platform_device *pdev)
2381 {
2382         struct gpmc_device *gpmc = platform_get_drvdata(pdev);
2383
2384         gpmc_free_irq(gpmc);
2385         gpmc_mem_exit();
2386         pm_runtime_put_sync(&pdev->dev);
2387         pm_runtime_disable(&pdev->dev);
2388
2389         return 0;
2390 }
2391
2392 #ifdef CONFIG_PM_SLEEP
2393 static int gpmc_suspend(struct device *dev)
2394 {
2395         omap3_gpmc_save_context();
2396         pm_runtime_put_sync(dev);
2397         return 0;
2398 }
2399
2400 static int gpmc_resume(struct device *dev)
2401 {
2402         pm_runtime_get_sync(dev);
2403         omap3_gpmc_restore_context();
2404         return 0;
2405 }
2406 #endif
2407
2408 static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
2409
2410 static struct platform_driver gpmc_driver = {
2411         .probe          = gpmc_probe,
2412         .remove         = gpmc_remove,
2413         .driver         = {
2414                 .name   = DEVICE_NAME,
2415                 .of_match_table = of_match_ptr(gpmc_dt_ids),
2416                 .pm     = &gpmc_pm_ops,
2417         },
2418 };
2419
2420 static __init int gpmc_init(void)
2421 {
2422         return platform_driver_register(&gpmc_driver);
2423 }
2424 postcore_initcall(gpmc_init);
2425
2426 static struct omap3_gpmc_regs gpmc_context;
2427
2428 void omap3_gpmc_save_context(void)
2429 {
2430         int i;
2431
2432         if (!gpmc_base)
2433                 return;
2434
2435         gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
2436         gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
2437         gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
2438         gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
2439         gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
2440         gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
2441         gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
2442         for (i = 0; i < gpmc_cs_num; i++) {
2443                 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
2444                 if (gpmc_context.cs_context[i].is_valid) {
2445                         gpmc_context.cs_context[i].config1 =
2446                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
2447                         gpmc_context.cs_context[i].config2 =
2448                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
2449                         gpmc_context.cs_context[i].config3 =
2450                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
2451                         gpmc_context.cs_context[i].config4 =
2452                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
2453                         gpmc_context.cs_context[i].config5 =
2454                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
2455                         gpmc_context.cs_context[i].config6 =
2456                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
2457                         gpmc_context.cs_context[i].config7 =
2458                                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
2459                 }
2460         }
2461 }
2462
2463 void omap3_gpmc_restore_context(void)
2464 {
2465         int i;
2466
2467         if (!gpmc_base)
2468                 return;
2469
2470         gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
2471         gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
2472         gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
2473         gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
2474         gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
2475         gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
2476         gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
2477         for (i = 0; i < gpmc_cs_num; i++) {
2478                 if (gpmc_context.cs_context[i].is_valid) {
2479                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
2480                                 gpmc_context.cs_context[i].config1);
2481                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
2482                                 gpmc_context.cs_context[i].config2);
2483                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
2484                                 gpmc_context.cs_context[i].config3);
2485                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
2486                                 gpmc_context.cs_context[i].config4);
2487                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
2488                                 gpmc_context.cs_context[i].config5);
2489                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
2490                                 gpmc_context.cs_context[i].config6);
2491                         gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
2492                                 gpmc_context.cs_context[i].config7);
2493                 }
2494         }
2495 }