Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * GPMC support functions
0004  *
0005  * Copyright (C) 2005-2006 Nokia Corporation
0006  *
0007  * Author: Juha Yrjola
0008  *
0009  * Copyright (C) 2009 Texas Instruments
0010  * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
0011  */
0012 #include <linux/cpu_pm.h>
0013 #include <linux/irq.h>
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/init.h>
0017 #include <linux/err.h>
0018 #include <linux/clk.h>
0019 #include <linux/ioport.h>
0020 #include <linux/spinlock.h>
0021 #include <linux/io.h>
0022 #include <linux/gpio/driver.h>
0023 #include <linux/gpio/consumer.h> /* GPIO descriptor enum */
0024 #include <linux/gpio/machine.h>
0025 #include <linux/interrupt.h>
0026 #include <linux/irqdomain.h>
0027 #include <linux/platform_device.h>
0028 #include <linux/of.h>
0029 #include <linux/of_address.h>
0030 #include <linux/of_device.h>
0031 #include <linux/of_platform.h>
0032 #include <linux/omap-gpmc.h>
0033 #include <linux/pm_runtime.h>
0034 #include <linux/sizes.h>
0035 
0036 #include <linux/platform_data/mtd-nand-omap2.h>
0037 
0038 #define DEVICE_NAME     "omap-gpmc"
0039 
0040 /* GPMC register offsets */
0041 #define GPMC_REVISION       0x00
0042 #define GPMC_SYSCONFIG      0x10
0043 #define GPMC_SYSSTATUS      0x14
0044 #define GPMC_IRQSTATUS      0x18
0045 #define GPMC_IRQENABLE      0x1c
0046 #define GPMC_TIMEOUT_CONTROL    0x40
0047 #define GPMC_ERR_ADDRESS    0x44
0048 #define GPMC_ERR_TYPE       0x48
0049 #define GPMC_CONFIG     0x50
0050 #define GPMC_STATUS     0x54
0051 #define GPMC_PREFETCH_CONFIG1   0x1e0
0052 #define GPMC_PREFETCH_CONFIG2   0x1e4
0053 #define GPMC_PREFETCH_CONTROL   0x1ec
0054 #define GPMC_PREFETCH_STATUS    0x1f0
0055 #define GPMC_ECC_CONFIG     0x1f4
0056 #define GPMC_ECC_CONTROL    0x1f8
0057 #define GPMC_ECC_SIZE_CONFIG    0x1fc
0058 #define GPMC_ECC1_RESULT        0x200
0059 #define GPMC_ECC_BCH_RESULT_0   0x240   /* not available on OMAP2 */
0060 #define GPMC_ECC_BCH_RESULT_1   0x244   /* not available on OMAP2 */
0061 #define GPMC_ECC_BCH_RESULT_2   0x248   /* not available on OMAP2 */
0062 #define GPMC_ECC_BCH_RESULT_3   0x24c   /* not available on OMAP2 */
0063 #define GPMC_ECC_BCH_RESULT_4   0x300   /* not available on OMAP2 */
0064 #define GPMC_ECC_BCH_RESULT_5   0x304   /* not available on OMAP2 */
0065 #define GPMC_ECC_BCH_RESULT_6   0x308   /* not available on OMAP2 */
0066 
0067 /* GPMC ECC control settings */
0068 #define GPMC_ECC_CTRL_ECCCLEAR      0x100
0069 #define GPMC_ECC_CTRL_ECCDISABLE    0x000
0070 #define GPMC_ECC_CTRL_ECCREG1       0x001
0071 #define GPMC_ECC_CTRL_ECCREG2       0x002
0072 #define GPMC_ECC_CTRL_ECCREG3       0x003
0073 #define GPMC_ECC_CTRL_ECCREG4       0x004
0074 #define GPMC_ECC_CTRL_ECCREG5       0x005
0075 #define GPMC_ECC_CTRL_ECCREG6       0x006
0076 #define GPMC_ECC_CTRL_ECCREG7       0x007
0077 #define GPMC_ECC_CTRL_ECCREG8       0x008
0078 #define GPMC_ECC_CTRL_ECCREG9       0x009
0079 
0080 #define GPMC_CONFIG_LIMITEDADDRESS      BIT(1)
0081 
0082 #define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS  BIT(0)
0083 
0084 #define GPMC_CONFIG2_CSEXTRADELAY       BIT(7)
0085 #define GPMC_CONFIG3_ADVEXTRADELAY      BIT(7)
0086 #define GPMC_CONFIG4_OEEXTRADELAY       BIT(7)
0087 #define GPMC_CONFIG4_WEEXTRADELAY       BIT(23)
0088 #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN    BIT(6)
0089 #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN    BIT(7)
0090 
0091 #define GPMC_CS0_OFFSET     0x60
0092 #define GPMC_CS_SIZE        0x30
0093 #define GPMC_BCH_SIZE       0x10
0094 
0095 /*
0096  * The first 1MB of GPMC address space is typically mapped to
0097  * the internal ROM. Never allocate the first page, to
0098  * facilitate bug detection; even if we didn't boot from ROM.
0099  * As GPMC minimum partition size is 16MB we can only start from
0100  * there.
0101  */
0102 #define GPMC_MEM_START      0x1000000
0103 #define GPMC_MEM_END        0x3FFFFFFF
0104 
0105 #define GPMC_CHUNK_SHIFT    24      /* 16 MB */
0106 #define GPMC_SECTION_SHIFT  28      /* 128 MB */
0107 
0108 #define CS_NUM_SHIFT        24
0109 #define ENABLE_PREFETCH     (0x1 << 7)
0110 #define DMA_MPU_MODE        2
0111 
0112 #define GPMC_REVISION_MAJOR(l)      (((l) >> 4) & 0xf)
0113 #define GPMC_REVISION_MINOR(l)      ((l) & 0xf)
0114 
0115 #define GPMC_HAS_WR_ACCESS      0x1
0116 #define GPMC_HAS_WR_DATA_MUX_BUS    0x2
0117 #define GPMC_HAS_MUX_AAD        0x4
0118 
0119 #define GPMC_NR_WAITPINS        4
0120 
0121 #define GPMC_CS_CONFIG1     0x00
0122 #define GPMC_CS_CONFIG2     0x04
0123 #define GPMC_CS_CONFIG3     0x08
0124 #define GPMC_CS_CONFIG4     0x0c
0125 #define GPMC_CS_CONFIG5     0x10
0126 #define GPMC_CS_CONFIG6     0x14
0127 #define GPMC_CS_CONFIG7     0x18
0128 #define GPMC_CS_NAND_COMMAND    0x1c
0129 #define GPMC_CS_NAND_ADDRESS    0x20
0130 #define GPMC_CS_NAND_DATA   0x24
0131 
0132 /* Control Commands */
0133 #define GPMC_CONFIG_RDY_BSY 0x00000001
0134 #define GPMC_CONFIG_DEV_SIZE    0x00000002
0135 #define GPMC_CONFIG_DEV_TYPE    0x00000003
0136 
0137 #define GPMC_CONFIG1_WRAPBURST_SUPP     (1 << 31)
0138 #define GPMC_CONFIG1_READMULTIPLE_SUPP  (1 << 30)
0139 #define GPMC_CONFIG1_READTYPE_ASYNC     (0 << 29)
0140 #define GPMC_CONFIG1_READTYPE_SYNC      (1 << 29)
0141 #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
0142 #define GPMC_CONFIG1_WRITETYPE_ASYNC    (0 << 27)
0143 #define GPMC_CONFIG1_WRITETYPE_SYNC     (1 << 27)
0144 #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) (((val) & 3) << 25)
0145 /** CLKACTIVATIONTIME Max Ticks */
0146 #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
0147 #define GPMC_CONFIG1_PAGE_LEN(val)      (((val) & 3) << 23)
0148 /** ATTACHEDDEVICEPAGELENGTH Max Value */
0149 #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
0150 #define GPMC_CONFIG1_WAIT_READ_MON      (1 << 22)
0151 #define GPMC_CONFIG1_WAIT_WRITE_MON     (1 << 21)
0152 #define GPMC_CONFIG1_WAIT_MON_TIME(val) (((val) & 3) << 18)
0153 /** WAITMONITORINGTIME Max Ticks */
0154 #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX  2
0155 #define GPMC_CONFIG1_WAIT_PIN_SEL(val)  (((val) & 3) << 16)
0156 #define GPMC_CONFIG1_DEVICESIZE(val)    (((val) & 3) << 12)
0157 #define GPMC_CONFIG1_DEVICESIZE_16      GPMC_CONFIG1_DEVICESIZE(1)
0158 /** DEVICESIZE Max Value */
0159 #define GPMC_CONFIG1_DEVICESIZE_MAX     1
0160 #define GPMC_CONFIG1_DEVICETYPE(val)    (((val) & 3) << 10)
0161 #define GPMC_CONFIG1_DEVICETYPE_NOR     GPMC_CONFIG1_DEVICETYPE(0)
0162 #define GPMC_CONFIG1_MUXTYPE(val)       (((val) & 3) << 8)
0163 #define GPMC_CONFIG1_TIME_PARA_GRAN     (1 << 4)
0164 #define GPMC_CONFIG1_FCLK_DIV(val)      ((val) & 3)
0165 #define GPMC_CONFIG1_FCLK_DIV2          (GPMC_CONFIG1_FCLK_DIV(1))
0166 #define GPMC_CONFIG1_FCLK_DIV3          (GPMC_CONFIG1_FCLK_DIV(2))
0167 #define GPMC_CONFIG1_FCLK_DIV4          (GPMC_CONFIG1_FCLK_DIV(3))
0168 #define GPMC_CONFIG7_CSVALID        (1 << 6)
0169 
0170 #define GPMC_CONFIG7_BASEADDRESS_MASK   0x3f
0171 #define GPMC_CONFIG7_CSVALID_MASK   BIT(6)
0172 #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
0173 #define GPMC_CONFIG7_MASKADDRESS_MASK   (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
0174 /* All CONFIG7 bits except reserved bits */
0175 #define GPMC_CONFIG7_MASK       (GPMC_CONFIG7_BASEADDRESS_MASK | \
0176                      GPMC_CONFIG7_CSVALID_MASK |     \
0177                      GPMC_CONFIG7_MASKADDRESS_MASK)
0178 
0179 #define GPMC_DEVICETYPE_NOR     0
0180 #define GPMC_DEVICETYPE_NAND        2
0181 #define GPMC_CONFIG_WRITEPROTECT    0x00000010
0182 #define WR_RD_PIN_MONITORING        0x00600000
0183 
0184 /* ECC commands */
0185 #define GPMC_ECC_READ       0 /* Reset Hardware ECC for read */
0186 #define GPMC_ECC_WRITE      1 /* Reset Hardware ECC for write */
0187 #define GPMC_ECC_READSYN    2 /* Reset before syndrom is read back */
0188 
0189 #define GPMC_NR_NAND_IRQS   2 /* number of NAND specific IRQs */
0190 
0191 enum gpmc_clk_domain {
0192     GPMC_CD_FCLK,
0193     GPMC_CD_CLK
0194 };
0195 
0196 struct gpmc_cs_data {
0197     const char *name;
0198 
0199 #define GPMC_CS_RESERVED    (1 << 0)
0200     u32 flags;
0201 
0202     struct resource mem;
0203 };
0204 
0205 /* Structure to save gpmc cs context */
0206 struct gpmc_cs_config {
0207     u32 config1;
0208     u32 config2;
0209     u32 config3;
0210     u32 config4;
0211     u32 config5;
0212     u32 config6;
0213     u32 config7;
0214     int is_valid;
0215 };
0216 
0217 /*
0218  * Structure to save/restore gpmc context
0219  * to support core off on OMAP3
0220  */
0221 struct omap3_gpmc_regs {
0222     u32 sysconfig;
0223     u32 irqenable;
0224     u32 timeout_ctrl;
0225     u32 config;
0226     u32 prefetch_config1;
0227     u32 prefetch_config2;
0228     u32 prefetch_control;
0229     struct gpmc_cs_config cs_context[GPMC_CS_NUM];
0230 };
0231 
0232 struct gpmc_device {
0233     struct device *dev;
0234     int irq;
0235     struct irq_chip irq_chip;
0236     struct gpio_chip gpio_chip;
0237     struct notifier_block nb;
0238     struct omap3_gpmc_regs context;
0239     int nirqs;
0240     unsigned int is_suspended:1;
0241     struct resource *data;
0242 };
0243 
0244 static struct irq_domain *gpmc_irq_domain;
0245 
0246 static struct resource  gpmc_mem_root;
0247 static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
0248 static DEFINE_SPINLOCK(gpmc_mem_lock);
0249 /* Define chip-selects as reserved by default until probe completes */
0250 static unsigned int gpmc_cs_num = GPMC_CS_NUM;
0251 static unsigned int gpmc_nr_waitpins;
0252 static unsigned int gpmc_capability;
0253 static void __iomem *gpmc_base;
0254 
0255 static struct clk *gpmc_l3_clk;
0256 
0257 static irqreturn_t gpmc_handle_irq(int irq, void *dev);
0258 
0259 static void gpmc_write_reg(int idx, u32 val)
0260 {
0261     writel_relaxed(val, gpmc_base + idx);
0262 }
0263 
0264 static u32 gpmc_read_reg(int idx)
0265 {
0266     return readl_relaxed(gpmc_base + idx);
0267 }
0268 
0269 void gpmc_cs_write_reg(int cs, int idx, u32 val)
0270 {
0271     void __iomem *reg_addr;
0272 
0273     reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
0274     writel_relaxed(val, reg_addr);
0275 }
0276 
0277 static u32 gpmc_cs_read_reg(int cs, int idx)
0278 {
0279     void __iomem *reg_addr;
0280 
0281     reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
0282     return readl_relaxed(reg_addr);
0283 }
0284 
0285 /* TODO: Add support for gpmc_fck to clock framework and use it */
0286 static unsigned long gpmc_get_fclk_period(void)
0287 {
0288     unsigned long rate = clk_get_rate(gpmc_l3_clk);
0289 
0290     rate /= 1000;
0291     rate = 1000000000 / rate;   /* In picoseconds */
0292 
0293     return rate;
0294 }
0295 
0296 /**
0297  * gpmc_get_clk_period - get period of selected clock domain in ps
0298  * @cs: Chip Select Region.
0299  * @cd: Clock Domain.
0300  *
0301  * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
0302  * prior to calling this function with GPMC_CD_CLK.
0303  */
0304 static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
0305 {
0306     unsigned long tick_ps = gpmc_get_fclk_period();
0307     u32 l;
0308     int div;
0309 
0310     switch (cd) {
0311     case GPMC_CD_CLK:
0312         /* get current clk divider */
0313         l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
0314         div = (l & 0x03) + 1;
0315         /* get GPMC_CLK period */
0316         tick_ps *= div;
0317         break;
0318     case GPMC_CD_FCLK:
0319     default:
0320         break;
0321     }
0322 
0323     return tick_ps;
0324 }
0325 
0326 static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
0327                      enum gpmc_clk_domain cd)
0328 {
0329     unsigned long tick_ps;
0330 
0331     /* Calculate in picosecs to yield more exact results */
0332     tick_ps = gpmc_get_clk_period(cs, cd);
0333 
0334     return (time_ns * 1000 + tick_ps - 1) / tick_ps;
0335 }
0336 
0337 static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
0338 {
0339     return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
0340 }
0341 
0342 static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
0343 {
0344     unsigned long tick_ps;
0345 
0346     /* Calculate in picosecs to yield more exact results */
0347     tick_ps = gpmc_get_fclk_period();
0348 
0349     return (time_ps + tick_ps - 1) / tick_ps;
0350 }
0351 
0352 static unsigned int gpmc_clk_ticks_to_ns(unsigned int ticks, int cs,
0353                      enum gpmc_clk_domain cd)
0354 {
0355     return ticks * gpmc_get_clk_period(cs, cd) / 1000;
0356 }
0357 
0358 unsigned int gpmc_ticks_to_ns(unsigned int ticks)
0359 {
0360     return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
0361 }
0362 
0363 static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
0364 {
0365     return ticks * gpmc_get_fclk_period();
0366 }
0367 
0368 static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
0369 {
0370     unsigned long ticks = gpmc_ps_to_ticks(time_ps);
0371 
0372     return ticks * gpmc_get_fclk_period();
0373 }
0374 
0375 static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
0376 {
0377     u32 l;
0378 
0379     l = gpmc_cs_read_reg(cs, reg);
0380     if (value)
0381         l |= mask;
0382     else
0383         l &= ~mask;
0384     gpmc_cs_write_reg(cs, reg, l);
0385 }
0386 
0387 static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
0388 {
0389     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
0390                GPMC_CONFIG1_TIME_PARA_GRAN,
0391                p->time_para_granularity);
0392     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
0393                GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
0394     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
0395                GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
0396     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
0397                GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
0398     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
0399                GPMC_CONFIG4_WEEXTRADELAY, p->we_extra_delay);
0400     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
0401                GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
0402                p->cycle2cyclesamecsen);
0403     gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
0404                GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
0405                p->cycle2cyclediffcsen);
0406 }
0407 
0408 #ifdef CONFIG_OMAP_GPMC_DEBUG
0409 /**
0410  * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
0411  * @cs:      Chip Select Region
0412  * @reg:     GPMC_CS_CONFIGn register offset.
0413  * @st_bit:  Start Bit
0414  * @end_bit: End Bit. Must be >= @st_bit.
0415  * @max:     Maximum parameter value (before optional @shift).
0416  *           If 0, maximum is as high as @st_bit and @end_bit allow.
0417  * @name:    DTS node name, w/o "gpmc,"
0418  * @cd:      Clock Domain of timing parameter.
0419  * @shift:   Parameter value left shifts @shift, which is then printed instead of value.
0420  * @raw:     Raw Format Option.
0421  *           raw format:  gpmc,name = <value>
0422  *           tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
0423  *           Where x ns -- y ns result in the same tick value.
0424  *           When @max is exceeded, "invalid" is printed inside comment.
0425  * @noval:   Parameter values equal to 0 are not printed.
0426  * @return:  Specified timing parameter (after optional @shift).
0427  *
0428  */
0429 static int get_gpmc_timing_reg(
0430     /* timing specifiers */
0431     int cs, int reg, int st_bit, int end_bit, int max,
0432     const char *name, const enum gpmc_clk_domain cd,
0433     /* value transform */
0434     int shift,
0435     /* format specifiers */
0436     bool raw, bool noval)
0437 {
0438     u32 l;
0439     int nr_bits;
0440     int mask;
0441     bool invalid;
0442 
0443     l = gpmc_cs_read_reg(cs, reg);
0444     nr_bits = end_bit - st_bit + 1;
0445     mask = (1 << nr_bits) - 1;
0446     l = (l >> st_bit) & mask;
0447     if (!max)
0448         max = mask;
0449     invalid = l > max;
0450     if (shift)
0451         l = (shift << l);
0452     if (noval && (l == 0))
0453         return 0;
0454     if (!raw) {
0455         /* DTS tick format for timings in ns */
0456         unsigned int time_ns;
0457         unsigned int time_ns_min = 0;
0458 
0459         if (l)
0460             time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
0461         time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
0462         pr_info("gpmc,%s = <%u>; /* %u ns - %u ns; %i ticks%s*/\n",
0463             name, time_ns, time_ns_min, time_ns, l,
0464             invalid ? "; invalid " : " ");
0465     } else {
0466         /* raw format */
0467         pr_info("gpmc,%s = <%u>;%s\n", name, l,
0468             invalid ? " /* invalid */" : "");
0469     }
0470 
0471     return l;
0472 }
0473 
0474 #define GPMC_PRINT_CONFIG(cs, config) \
0475     pr_info("cs%i %s: 0x%08x\n", cs, #config, \
0476         gpmc_cs_read_reg(cs, config))
0477 #define GPMC_GET_RAW(reg, st, end, field) \
0478     get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
0479 #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
0480     get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
0481 #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
0482     get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
0483 #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
0484     get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
0485 #define GPMC_GET_TICKS(reg, st, end, field) \
0486     get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
0487 #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
0488     get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
0489 #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
0490     get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
0491 
0492 static void gpmc_show_regs(int cs, const char *desc)
0493 {
0494     pr_info("gpmc cs%i %s:\n", cs, desc);
0495     GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
0496     GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
0497     GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
0498     GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
0499     GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
0500     GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
0501 }
0502 
0503 /*
0504  * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
0505  * see commit c9fb809.
0506  */
0507 static void gpmc_cs_show_timings(int cs, const char *desc)
0508 {
0509     gpmc_show_regs(cs, desc);
0510 
0511     pr_info("gpmc cs%i access configuration:\n", cs);
0512     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1,  4,  4, "time-para-granularity");
0513     GPMC_GET_RAW(GPMC_CS_CONFIG1,  8,  9, "mux-add-data");
0514     GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 12, 13, 1,
0515                    GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
0516     GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
0517     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
0518     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
0519     GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
0520                    GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
0521                    "burst-length");
0522     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
0523     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
0524     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
0525     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
0526     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
0527 
0528     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2,  7,  7, "cs-extra-delay");
0529 
0530     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3,  7,  7, "adv-extra-delay");
0531 
0532     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
0533     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4,  7,  7, "oe-extra-delay");
0534 
0535     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6,  7,  7, "cycle2cycle-samecsen");
0536     GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6,  6,  6, "cycle2cycle-diffcsen");
0537 
0538     pr_info("gpmc cs%i timings configuration:\n", cs);
0539     GPMC_GET_TICKS(GPMC_CS_CONFIG2,  0,  3, "cs-on-ns");
0540     GPMC_GET_TICKS(GPMC_CS_CONFIG2,  8, 12, "cs-rd-off-ns");
0541     GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
0542 
0543     GPMC_GET_TICKS(GPMC_CS_CONFIG3,  0,  3, "adv-on-ns");
0544     GPMC_GET_TICKS(GPMC_CS_CONFIG3,  8, 12, "adv-rd-off-ns");
0545     GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
0546     if (gpmc_capability & GPMC_HAS_MUX_AAD) {
0547         GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
0548         GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
0549                 "adv-aad-mux-rd-off-ns");
0550         GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
0551                 "adv-aad-mux-wr-off-ns");
0552     }
0553 
0554     GPMC_GET_TICKS(GPMC_CS_CONFIG4,  0,  3, "oe-on-ns");
0555     GPMC_GET_TICKS(GPMC_CS_CONFIG4,  8, 12, "oe-off-ns");
0556     if (gpmc_capability & GPMC_HAS_MUX_AAD) {
0557         GPMC_GET_TICKS(GPMC_CS_CONFIG4,  4,  6, "oe-aad-mux-on-ns");
0558         GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
0559     }
0560     GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
0561     GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
0562 
0563     GPMC_GET_TICKS(GPMC_CS_CONFIG5,  0,  4, "rd-cycle-ns");
0564     GPMC_GET_TICKS(GPMC_CS_CONFIG5,  8, 12, "wr-cycle-ns");
0565     GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
0566 
0567     GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
0568 
0569     GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
0570     GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
0571 
0572     GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
0573                   GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
0574                   "wait-monitoring-ns", GPMC_CD_CLK);
0575     GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
0576                   GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
0577                   "clk-activation-ns", GPMC_CD_FCLK);
0578 
0579     GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
0580     GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
0581 }
0582 #else
0583 static inline void gpmc_cs_show_timings(int cs, const char *desc)
0584 {
0585 }
0586 #endif
0587 
0588 /**
0589  * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
0590  * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
0591  * prior to calling this function with @cd equal to GPMC_CD_CLK.
0592  *
0593  * @cs:      Chip Select Region.
0594  * @reg:     GPMC_CS_CONFIGn register offset.
0595  * @st_bit:  Start Bit
0596  * @end_bit: End Bit. Must be >= @st_bit.
0597  * @max:     Maximum parameter value.
0598  *           If 0, maximum is as high as @st_bit and @end_bit allow.
0599  * @time:    Timing parameter in ns.
0600  * @cd:      Timing parameter clock domain.
0601  * @name:    Timing parameter name.
0602  * @return:  0 on success, -1 on error.
0603  */
0604 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
0605                    int time, enum gpmc_clk_domain cd, const char *name)
0606 {
0607     u32 l;
0608     int ticks, mask, nr_bits;
0609 
0610     if (time == 0)
0611         ticks = 0;
0612     else
0613         ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
0614     nr_bits = end_bit - st_bit + 1;
0615     mask = (1 << nr_bits) - 1;
0616 
0617     if (!max)
0618         max = mask;
0619 
0620     if (ticks > max) {
0621         pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
0622                __func__, cs, name, time, ticks, max);
0623 
0624         return -1;
0625     }
0626 
0627     l = gpmc_cs_read_reg(cs, reg);
0628 #ifdef CONFIG_OMAP_GPMC_DEBUG
0629     pr_info("GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
0630         cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
0631             (l >> st_bit) & mask, time);
0632 #endif
0633     l &= ~(mask << st_bit);
0634     l |= ticks << st_bit;
0635     gpmc_cs_write_reg(cs, reg, l);
0636 
0637     return 0;
0638 }
0639 
0640 /**
0641  * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
0642  * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
0643  * read  --> don't sample bus too early
0644  * write --> data is longer on bus
0645  *
0646  * Formula:
0647  * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
0648  *                    / waitmonitoring_ticks)
0649  * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
0650  * div <= 0 check.
0651  *
0652  * @wait_monitoring: WAITMONITORINGTIME in ns.
0653  * @return:          -1 on failure to scale, else proper divider > 0.
0654  */
0655 static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
0656 {
0657     int div = gpmc_ns_to_ticks(wait_monitoring);
0658 
0659     div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
0660     div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
0661 
0662     if (div > 4)
0663         return -1;
0664     if (div <= 0)
0665         div = 1;
0666 
0667     return div;
0668 }
0669 
0670 /**
0671  * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
0672  * @sync_clk: GPMC_CLK period in ps.
0673  * @return:   Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
0674  *            Else, returns -1.
0675  */
0676 int gpmc_calc_divider(unsigned int sync_clk)
0677 {
0678     int div = gpmc_ps_to_ticks(sync_clk);
0679 
0680     if (div > 4)
0681         return -1;
0682     if (div <= 0)
0683         div = 1;
0684 
0685     return div;
0686 }
0687 
0688 /**
0689  * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
0690  * @cs:     Chip Select Region.
0691  * @t:      GPMC timing parameters.
0692  * @s:      GPMC timing settings.
0693  * @return: 0 on success, -1 on error.
0694  */
0695 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
0696             const struct gpmc_settings *s)
0697 {
0698     int div, ret;
0699     u32 l;
0700 
0701     div = gpmc_calc_divider(t->sync_clk);
0702     if (div < 0)
0703         return -EINVAL;
0704 
0705     /*
0706      * See if we need to change the divider for waitmonitoringtime.
0707      *
0708      * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
0709      * pure asynchronous accesses, i.e. both read and write asynchronous.
0710      * However, only do so if WAITMONITORINGTIME is actually used, i.e.
0711      * either WAITREADMONITORING or WAITWRITEMONITORING is set.
0712      *
0713      * This statement must not change div to scale async WAITMONITORINGTIME
0714      * to protect mixed synchronous and asynchronous accesses.
0715      *
0716      * We raise an error later if WAITMONITORINGTIME does not fit.
0717      */
0718     if (!s->sync_read && !s->sync_write &&
0719         (s->wait_on_read || s->wait_on_write)
0720        ) {
0721         div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
0722         if (div < 0) {
0723             pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
0724                    __func__,
0725                    t->wait_monitoring
0726                    );
0727             return -ENXIO;
0728         }
0729     }
0730 
0731     ret = 0;
0732     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG2, 0, 3, 0, t->cs_on,
0733                    GPMC_CD_FCLK, "cs_on");
0734     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG2, 8, 12, 0, t->cs_rd_off,
0735                    GPMC_CD_FCLK, "cs_rd_off");
0736     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG2, 16, 20, 0, t->cs_wr_off,
0737                    GPMC_CD_FCLK, "cs_wr_off");
0738     if (ret)
0739         return -ENXIO;
0740 
0741     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 0, 3, 0, t->adv_on,
0742                    GPMC_CD_FCLK, "adv_on");
0743     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 8, 12, 0, t->adv_rd_off,
0744                    GPMC_CD_FCLK, "adv_rd_off");
0745     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 16, 20, 0, t->adv_wr_off,
0746                    GPMC_CD_FCLK, "adv_wr_off");
0747     if (ret)
0748         return -ENXIO;
0749 
0750     if (gpmc_capability & GPMC_HAS_MUX_AAD) {
0751         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 4, 6, 0,
0752                        t->adv_aad_mux_on, GPMC_CD_FCLK,
0753                        "adv_aad_mux_on");
0754         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 24, 26, 0,
0755                        t->adv_aad_mux_rd_off, GPMC_CD_FCLK,
0756                        "adv_aad_mux_rd_off");
0757         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG3, 28, 30, 0,
0758                        t->adv_aad_mux_wr_off, GPMC_CD_FCLK,
0759                        "adv_aad_mux_wr_off");
0760         if (ret)
0761             return -ENXIO;
0762     }
0763 
0764     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 0, 3, 0, t->oe_on,
0765                    GPMC_CD_FCLK, "oe_on");
0766     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 8, 12, 0, t->oe_off,
0767                    GPMC_CD_FCLK, "oe_off");
0768     if (gpmc_capability & GPMC_HAS_MUX_AAD) {
0769         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 4, 6, 0,
0770                        t->oe_aad_mux_on, GPMC_CD_FCLK,
0771                        "oe_aad_mux_on");
0772         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 13, 15, 0,
0773                        t->oe_aad_mux_off, GPMC_CD_FCLK,
0774                        "oe_aad_mux_off");
0775     }
0776     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 16, 19, 0, t->we_on,
0777                    GPMC_CD_FCLK, "we_on");
0778     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG4, 24, 28, 0, t->we_off,
0779                    GPMC_CD_FCLK, "we_off");
0780     if (ret)
0781         return -ENXIO;
0782 
0783     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 0, 4, 0, t->rd_cycle,
0784                    GPMC_CD_FCLK, "rd_cycle");
0785     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 8, 12, 0, t->wr_cycle,
0786                    GPMC_CD_FCLK, "wr_cycle");
0787     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 16, 20, 0, t->access,
0788                    GPMC_CD_FCLK, "access");
0789     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG5, 24, 27, 0,
0790                    t->page_burst_access, GPMC_CD_FCLK,
0791                    "page_burst_access");
0792     if (ret)
0793         return -ENXIO;
0794 
0795     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 0, 3, 0,
0796                    t->bus_turnaround, GPMC_CD_FCLK,
0797                    "bus_turnaround");
0798     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 8, 11, 0,
0799                    t->cycle2cycle_delay, GPMC_CD_FCLK,
0800                    "cycle2cycle_delay");
0801     if (ret)
0802         return -ENXIO;
0803 
0804     if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) {
0805         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 16, 19, 0,
0806                        t->wr_data_mux_bus, GPMC_CD_FCLK,
0807                        "wr_data_mux_bus");
0808         if (ret)
0809             return -ENXIO;
0810     }
0811     if (gpmc_capability & GPMC_HAS_WR_ACCESS) {
0812         ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG6, 24, 28, 0,
0813                        t->wr_access, GPMC_CD_FCLK,
0814                        "wr_access");
0815         if (ret)
0816             return -ENXIO;
0817     }
0818 
0819     l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
0820     l &= ~0x03;
0821     l |= (div - 1);
0822     gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
0823 
0824     ret = 0;
0825     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG1, 18, 19,
0826                    GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
0827                    t->wait_monitoring, GPMC_CD_CLK,
0828                    "wait_monitoring");
0829     ret |= set_gpmc_timing_reg(cs, GPMC_CS_CONFIG1, 25, 26,
0830                    GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
0831                    t->clk_activation, GPMC_CD_FCLK,
0832                    "clk_activation");
0833     if (ret)
0834         return -ENXIO;
0835 
0836 #ifdef CONFIG_OMAP_GPMC_DEBUG
0837     pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
0838             cs, (div * gpmc_get_fclk_period()) / 1000, div);
0839 #endif
0840 
0841     gpmc_cs_bool_timings(cs, &t->bool_timings);
0842     gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
0843 
0844     return 0;
0845 }
0846 
0847 static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
0848 {
0849     u32 l;
0850     u32 mask;
0851 
0852     /*
0853      * Ensure that base address is aligned on a
0854      * boundary equal to or greater than size.
0855      */
0856     if (base & (size - 1))
0857         return -EINVAL;
0858 
0859     base >>= GPMC_CHUNK_SHIFT;
0860     mask = (1 << GPMC_SECTION_SHIFT) - size;
0861     mask >>= GPMC_CHUNK_SHIFT;
0862     mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
0863 
0864     l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
0865     l &= ~GPMC_CONFIG7_MASK;
0866     l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
0867     l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
0868     l |= GPMC_CONFIG7_CSVALID;
0869     gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
0870 
0871     return 0;
0872 }
0873 
0874 static void gpmc_cs_enable_mem(int cs)
0875 {
0876     u32 l;
0877 
0878     l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
0879     l |= GPMC_CONFIG7_CSVALID;
0880     gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
0881 }
0882 
0883 static void gpmc_cs_disable_mem(int cs)
0884 {
0885     u32 l;
0886 
0887     l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
0888     l &= ~GPMC_CONFIG7_CSVALID;
0889     gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
0890 }
0891 
0892 static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
0893 {
0894     u32 l;
0895     u32 mask;
0896 
0897     l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
0898     *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
0899     mask = (l >> 8) & 0x0f;
0900     *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
0901 }
0902 
0903 static int gpmc_cs_mem_enabled(int cs)
0904 {
0905     u32 l;
0906 
0907     l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
0908     return l & GPMC_CONFIG7_CSVALID;
0909 }
0910 
0911 static void gpmc_cs_set_reserved(int cs, int reserved)
0912 {
0913     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
0914 
0915     gpmc->flags |= GPMC_CS_RESERVED;
0916 }
0917 
0918 static bool gpmc_cs_reserved(int cs)
0919 {
0920     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
0921 
0922     return gpmc->flags & GPMC_CS_RESERVED;
0923 }
0924 
0925 static unsigned long gpmc_mem_align(unsigned long size)
0926 {
0927     int order;
0928 
0929     size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
0930     order = GPMC_CHUNK_SHIFT - 1;
0931     do {
0932         size >>= 1;
0933         order++;
0934     } while (size);
0935     size = 1 << order;
0936     return size;
0937 }
0938 
0939 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
0940 {
0941     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
0942     struct resource *res = &gpmc->mem;
0943     int r;
0944 
0945     size = gpmc_mem_align(size);
0946     spin_lock(&gpmc_mem_lock);
0947     res->start = base;
0948     res->end = base + size - 1;
0949     r = request_resource(&gpmc_mem_root, res);
0950     spin_unlock(&gpmc_mem_lock);
0951 
0952     return r;
0953 }
0954 
0955 static int gpmc_cs_delete_mem(int cs)
0956 {
0957     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
0958     struct resource *res = &gpmc->mem;
0959     int r;
0960 
0961     spin_lock(&gpmc_mem_lock);
0962     r = release_resource(res);
0963     res->start = 0;
0964     res->end = 0;
0965     spin_unlock(&gpmc_mem_lock);
0966 
0967     return r;
0968 }
0969 
0970 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
0971 {
0972     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
0973     struct resource *res = &gpmc->mem;
0974     int r = -1;
0975 
0976     if (cs >= gpmc_cs_num) {
0977         pr_err("%s: requested chip-select is disabled\n", __func__);
0978         return -ENODEV;
0979     }
0980     size = gpmc_mem_align(size);
0981     if (size > (1 << GPMC_SECTION_SHIFT))
0982         return -ENOMEM;
0983 
0984     spin_lock(&gpmc_mem_lock);
0985     if (gpmc_cs_reserved(cs)) {
0986         r = -EBUSY;
0987         goto out;
0988     }
0989     if (gpmc_cs_mem_enabled(cs))
0990         r = adjust_resource(res, res->start & ~(size - 1), size);
0991     if (r < 0)
0992         r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
0993                       size, NULL, NULL);
0994     if (r < 0)
0995         goto out;
0996 
0997     /* Disable CS while changing base address and size mask */
0998     gpmc_cs_disable_mem(cs);
0999 
1000     r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
1001     if (r < 0) {
1002         release_resource(res);
1003         goto out;
1004     }
1005 
1006     /* Enable CS */
1007     gpmc_cs_enable_mem(cs);
1008     *base = res->start;
1009     gpmc_cs_set_reserved(cs, 1);
1010 out:
1011     spin_unlock(&gpmc_mem_lock);
1012     return r;
1013 }
1014 EXPORT_SYMBOL(gpmc_cs_request);
1015 
1016 void gpmc_cs_free(int cs)
1017 {
1018     struct gpmc_cs_data *gpmc;
1019     struct resource *res;
1020 
1021     spin_lock(&gpmc_mem_lock);
1022     if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
1023         WARN(1, "Trying to free non-reserved GPMC CS%d\n", cs);
1024         spin_unlock(&gpmc_mem_lock);
1025         return;
1026     }
1027     gpmc = &gpmc_cs[cs];
1028     res = &gpmc->mem;
1029 
1030     gpmc_cs_disable_mem(cs);
1031     if (res->flags)
1032         release_resource(res);
1033     gpmc_cs_set_reserved(cs, 0);
1034     spin_unlock(&gpmc_mem_lock);
1035 }
1036 EXPORT_SYMBOL(gpmc_cs_free);
1037 
1038 /**
1039  * gpmc_configure - write request to configure gpmc
1040  * @cmd: command type
1041  * @wval: value to write
1042  * @return status of the operation
1043  */
1044 int gpmc_configure(int cmd, int wval)
1045 {
1046     u32 regval;
1047 
1048     switch (cmd) {
1049     case GPMC_CONFIG_WP:
1050         regval = gpmc_read_reg(GPMC_CONFIG);
1051         if (wval)
1052             regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
1053         else
1054             regval |= GPMC_CONFIG_WRITEPROTECT;  /* WP is OFF */
1055         gpmc_write_reg(GPMC_CONFIG, regval);
1056         break;
1057 
1058     default:
1059         pr_err("%s: command not supported\n", __func__);
1060         return -EINVAL;
1061     }
1062 
1063     return 0;
1064 }
1065 EXPORT_SYMBOL(gpmc_configure);
1066 
1067 static bool gpmc_nand_writebuffer_empty(void)
1068 {
1069     if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
1070         return true;
1071 
1072     return false;
1073 }
1074 
1075 static struct gpmc_nand_ops nand_ops = {
1076     .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
1077 };
1078 
1079 /**
1080  * gpmc_omap_get_nand_ops - Get the GPMC NAND interface
1081  * @reg: the GPMC NAND register map exclusive for NAND use.
1082  * @cs: GPMC chip select number on which the NAND sits. The
1083  *      register map returned will be specific to this chip select.
1084  *
1085  * Returns NULL on error e.g. invalid cs.
1086  */
1087 struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
1088 {
1089     int i;
1090 
1091     if (cs >= gpmc_cs_num)
1092         return NULL;
1093 
1094     reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
1095                 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
1096     reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
1097                 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
1098     reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
1099                 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
1100     reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
1101     reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
1102     reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
1103     reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
1104     reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
1105     reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
1106     reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
1107     reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
1108 
1109     for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
1110         reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
1111                        GPMC_BCH_SIZE * i;
1112         reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
1113                        GPMC_BCH_SIZE * i;
1114         reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
1115                        GPMC_BCH_SIZE * i;
1116         reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
1117                        GPMC_BCH_SIZE * i;
1118         reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
1119                        i * GPMC_BCH_SIZE;
1120         reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
1121                        i * GPMC_BCH_SIZE;
1122         reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
1123                        i * GPMC_BCH_SIZE;
1124     }
1125 
1126     return &nand_ops;
1127 }
1128 EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
1129 
1130 static void gpmc_omap_onenand_calc_sync_timings(struct gpmc_timings *t,
1131                         struct gpmc_settings *s,
1132                         int freq, int latency)
1133 {
1134     struct gpmc_device_timings dev_t;
1135     const int t_cer  = 15;
1136     const int t_avdp = 12;
1137     const int t_cez  = 20; /* max of t_cez, t_oez */
1138     const int t_wpl  = 40;
1139     const int t_wph  = 30;
1140     int min_gpmc_clk_period, t_ces, t_avds, t_avdh, t_ach, t_aavdh, t_rdyo;
1141 
1142     switch (freq) {
1143     case 104:
1144         min_gpmc_clk_period = 9600; /* 104 MHz */
1145         t_ces   = 3;
1146         t_avds  = 4;
1147         t_avdh  = 2;
1148         t_ach   = 3;
1149         t_aavdh = 6;
1150         t_rdyo  = 6;
1151         break;
1152     case 83:
1153         min_gpmc_clk_period = 12000; /* 83 MHz */
1154         t_ces   = 5;
1155         t_avds  = 4;
1156         t_avdh  = 2;
1157         t_ach   = 6;
1158         t_aavdh = 6;
1159         t_rdyo  = 9;
1160         break;
1161     case 66:
1162         min_gpmc_clk_period = 15000; /* 66 MHz */
1163         t_ces   = 6;
1164         t_avds  = 5;
1165         t_avdh  = 2;
1166         t_ach   = 6;
1167         t_aavdh = 6;
1168         t_rdyo  = 11;
1169         break;
1170     default:
1171         min_gpmc_clk_period = 18500; /* 54 MHz */
1172         t_ces   = 7;
1173         t_avds  = 7;
1174         t_avdh  = 7;
1175         t_ach   = 9;
1176         t_aavdh = 7;
1177         t_rdyo  = 15;
1178         break;
1179     }
1180 
1181     /* Set synchronous read timings */
1182     memset(&dev_t, 0, sizeof(dev_t));
1183 
1184     if (!s->sync_write) {
1185         dev_t.t_avdp_w = max(t_avdp, t_cer) * 1000;
1186         dev_t.t_wpl = t_wpl * 1000;
1187         dev_t.t_wph = t_wph * 1000;
1188         dev_t.t_aavdh = t_aavdh * 1000;
1189     }
1190     dev_t.ce_xdelay = true;
1191     dev_t.avd_xdelay = true;
1192     dev_t.oe_xdelay = true;
1193     dev_t.we_xdelay = true;
1194     dev_t.clk = min_gpmc_clk_period;
1195     dev_t.t_bacc = dev_t.clk;
1196     dev_t.t_ces = t_ces * 1000;
1197     dev_t.t_avds = t_avds * 1000;
1198     dev_t.t_avdh = t_avdh * 1000;
1199     dev_t.t_ach = t_ach * 1000;
1200     dev_t.cyc_iaa = (latency + 1);
1201     dev_t.t_cez_r = t_cez * 1000;
1202     dev_t.t_cez_w = dev_t.t_cez_r;
1203     dev_t.cyc_aavdh_oe = 1;
1204     dev_t.t_rdyo = t_rdyo * 1000 + min_gpmc_clk_period;
1205 
1206     gpmc_calc_timings(t, s, &dev_t);
1207 }
1208 
1209 int gpmc_omap_onenand_set_timings(struct device *dev, int cs, int freq,
1210                   int latency,
1211                   struct gpmc_onenand_info *info)
1212 {
1213     int ret;
1214     struct gpmc_timings gpmc_t;
1215     struct gpmc_settings gpmc_s;
1216 
1217     gpmc_read_settings_dt(dev->of_node, &gpmc_s);
1218 
1219     info->sync_read = gpmc_s.sync_read;
1220     info->sync_write = gpmc_s.sync_write;
1221     info->burst_len = gpmc_s.burst_len;
1222 
1223     if (!gpmc_s.sync_read && !gpmc_s.sync_write)
1224         return 0;
1225 
1226     gpmc_omap_onenand_calc_sync_timings(&gpmc_t, &gpmc_s, freq, latency);
1227 
1228     ret = gpmc_cs_program_settings(cs, &gpmc_s);
1229     if (ret < 0)
1230         return ret;
1231 
1232     return gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
1233 }
1234 EXPORT_SYMBOL_GPL(gpmc_omap_onenand_set_timings);
1235 
1236 int gpmc_get_client_irq(unsigned int irq_config)
1237 {
1238     if (!gpmc_irq_domain) {
1239         pr_warn("%s called before GPMC IRQ domain available\n",
1240             __func__);
1241         return 0;
1242     }
1243 
1244     /* we restrict this to NAND IRQs only */
1245     if (irq_config >= GPMC_NR_NAND_IRQS)
1246         return 0;
1247 
1248     return irq_create_mapping(gpmc_irq_domain, irq_config);
1249 }
1250 
1251 static int gpmc_irq_endis(unsigned long hwirq, bool endis)
1252 {
1253     u32 regval;
1254 
1255     /* bits GPMC_NR_NAND_IRQS to 8 are reserved */
1256     if (hwirq >= GPMC_NR_NAND_IRQS)
1257         hwirq += 8 - GPMC_NR_NAND_IRQS;
1258 
1259     regval = gpmc_read_reg(GPMC_IRQENABLE);
1260     if (endis)
1261         regval |= BIT(hwirq);
1262     else
1263         regval &= ~BIT(hwirq);
1264     gpmc_write_reg(GPMC_IRQENABLE, regval);
1265 
1266     return 0;
1267 }
1268 
1269 static void gpmc_irq_disable(struct irq_data *p)
1270 {
1271     gpmc_irq_endis(p->hwirq, false);
1272 }
1273 
1274 static void gpmc_irq_enable(struct irq_data *p)
1275 {
1276     gpmc_irq_endis(p->hwirq, true);
1277 }
1278 
1279 static void gpmc_irq_mask(struct irq_data *d)
1280 {
1281     gpmc_irq_endis(d->hwirq, false);
1282 }
1283 
1284 static void gpmc_irq_unmask(struct irq_data *d)
1285 {
1286     gpmc_irq_endis(d->hwirq, true);
1287 }
1288 
1289 static void gpmc_irq_edge_config(unsigned long hwirq, bool rising_edge)
1290 {
1291     u32 regval;
1292 
1293     /* NAND IRQs polarity is not configurable */
1294     if (hwirq < GPMC_NR_NAND_IRQS)
1295         return;
1296 
1297     /* WAITPIN starts at BIT 8 */
1298     hwirq += 8 - GPMC_NR_NAND_IRQS;
1299 
1300     regval = gpmc_read_reg(GPMC_CONFIG);
1301     if (rising_edge)
1302         regval &= ~BIT(hwirq);
1303     else
1304         regval |= BIT(hwirq);
1305 
1306     gpmc_write_reg(GPMC_CONFIG, regval);
1307 }
1308 
1309 static void gpmc_irq_ack(struct irq_data *d)
1310 {
1311     unsigned int hwirq = d->hwirq;
1312 
1313     /* skip reserved bits */
1314     if (hwirq >= GPMC_NR_NAND_IRQS)
1315         hwirq += 8 - GPMC_NR_NAND_IRQS;
1316 
1317     /* Setting bit to 1 clears (or Acks) the interrupt */
1318     gpmc_write_reg(GPMC_IRQSTATUS, BIT(hwirq));
1319 }
1320 
1321 static int gpmc_irq_set_type(struct irq_data *d, unsigned int trigger)
1322 {
1323     /* can't set type for NAND IRQs */
1324     if (d->hwirq < GPMC_NR_NAND_IRQS)
1325         return -EINVAL;
1326 
1327     /* We can support either rising or falling edge at a time */
1328     if (trigger == IRQ_TYPE_EDGE_FALLING)
1329         gpmc_irq_edge_config(d->hwirq, false);
1330     else if (trigger == IRQ_TYPE_EDGE_RISING)
1331         gpmc_irq_edge_config(d->hwirq, true);
1332     else
1333         return -EINVAL;
1334 
1335     return 0;
1336 }
1337 
1338 static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
1339             irq_hw_number_t hw)
1340 {
1341     struct gpmc_device *gpmc = d->host_data;
1342 
1343     irq_set_chip_data(virq, gpmc);
1344     if (hw < GPMC_NR_NAND_IRQS) {
1345         irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
1346         irq_set_chip_and_handler(virq, &gpmc->irq_chip,
1347                      handle_simple_irq);
1348     } else {
1349         irq_set_chip_and_handler(virq, &gpmc->irq_chip,
1350                      handle_edge_irq);
1351     }
1352 
1353     return 0;
1354 }
1355 
1356 static const struct irq_domain_ops gpmc_irq_domain_ops = {
1357     .map    = gpmc_irq_map,
1358     .xlate  = irq_domain_xlate_twocell,
1359 };
1360 
1361 static irqreturn_t gpmc_handle_irq(int irq, void *data)
1362 {
1363     int hwirq, virq;
1364     u32 regval, regvalx;
1365     struct gpmc_device *gpmc = data;
1366 
1367     regval = gpmc_read_reg(GPMC_IRQSTATUS);
1368     regvalx = regval;
1369 
1370     if (!regval)
1371         return IRQ_NONE;
1372 
1373     for (hwirq = 0; hwirq < gpmc->nirqs; hwirq++) {
1374         /* skip reserved status bits */
1375         if (hwirq == GPMC_NR_NAND_IRQS)
1376             regvalx >>= 8 - GPMC_NR_NAND_IRQS;
1377 
1378         if (regvalx & BIT(hwirq)) {
1379             virq = irq_find_mapping(gpmc_irq_domain, hwirq);
1380             if (!virq) {
1381                 dev_warn(gpmc->dev,
1382                      "spurious irq detected hwirq %d, virq %d\n",
1383                      hwirq, virq);
1384             }
1385 
1386             generic_handle_irq(virq);
1387         }
1388     }
1389 
1390     gpmc_write_reg(GPMC_IRQSTATUS, regval);
1391 
1392     return IRQ_HANDLED;
1393 }
1394 
1395 static int gpmc_setup_irq(struct gpmc_device *gpmc)
1396 {
1397     u32 regval;
1398     int rc;
1399 
1400     /* Disable interrupts */
1401     gpmc_write_reg(GPMC_IRQENABLE, 0);
1402 
1403     /* clear interrupts */
1404     regval = gpmc_read_reg(GPMC_IRQSTATUS);
1405     gpmc_write_reg(GPMC_IRQSTATUS, regval);
1406 
1407     gpmc->irq_chip.name = "gpmc";
1408     gpmc->irq_chip.irq_enable = gpmc_irq_enable;
1409     gpmc->irq_chip.irq_disable = gpmc_irq_disable;
1410     gpmc->irq_chip.irq_ack = gpmc_irq_ack;
1411     gpmc->irq_chip.irq_mask = gpmc_irq_mask;
1412     gpmc->irq_chip.irq_unmask = gpmc_irq_unmask;
1413     gpmc->irq_chip.irq_set_type = gpmc_irq_set_type;
1414 
1415     gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
1416                         gpmc->nirqs,
1417                         &gpmc_irq_domain_ops,
1418                         gpmc);
1419     if (!gpmc_irq_domain) {
1420         dev_err(gpmc->dev, "IRQ domain add failed\n");
1421         return -ENODEV;
1422     }
1423 
1424     rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
1425     if (rc) {
1426         dev_err(gpmc->dev, "failed to request irq %d: %d\n",
1427             gpmc->irq, rc);
1428         irq_domain_remove(gpmc_irq_domain);
1429         gpmc_irq_domain = NULL;
1430     }
1431 
1432     return rc;
1433 }
1434 
1435 static int gpmc_free_irq(struct gpmc_device *gpmc)
1436 {
1437     int hwirq;
1438 
1439     free_irq(gpmc->irq, gpmc);
1440 
1441     for (hwirq = 0; hwirq < gpmc->nirqs; hwirq++)
1442         irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
1443 
1444     irq_domain_remove(gpmc_irq_domain);
1445     gpmc_irq_domain = NULL;
1446 
1447     return 0;
1448 }
1449 
1450 static void gpmc_mem_exit(void)
1451 {
1452     int cs;
1453 
1454     for (cs = 0; cs < gpmc_cs_num; cs++) {
1455         if (!gpmc_cs_mem_enabled(cs))
1456             continue;
1457         gpmc_cs_delete_mem(cs);
1458     }
1459 }
1460 
1461 static void gpmc_mem_init(struct gpmc_device *gpmc)
1462 {
1463     int cs;
1464 
1465     if (!gpmc->data) {
1466         /* All legacy devices have same data IO window */
1467         gpmc_mem_root.start = GPMC_MEM_START;
1468         gpmc_mem_root.end = GPMC_MEM_END;
1469     } else {
1470         gpmc_mem_root.start = gpmc->data->start;
1471         gpmc_mem_root.end = gpmc->data->end;
1472     }
1473 
1474     /* Reserve all regions that has been set up by bootloader */
1475     for (cs = 0; cs < gpmc_cs_num; cs++) {
1476         u32 base, size;
1477 
1478         if (!gpmc_cs_mem_enabled(cs))
1479             continue;
1480         gpmc_cs_get_memconf(cs, &base, &size);
1481         if (gpmc_cs_insert_mem(cs, base, size)) {
1482             pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
1483                 __func__, cs, base, base + size);
1484             gpmc_cs_disable_mem(cs);
1485         }
1486     }
1487 }
1488 
1489 static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
1490 {
1491     u32 temp;
1492     int div;
1493 
1494     div = gpmc_calc_divider(sync_clk);
1495     temp = gpmc_ps_to_ticks(time_ps);
1496     temp = (temp + div - 1) / div;
1497     return gpmc_ticks_to_ps(temp * div);
1498 }
1499 
1500 /* XXX: can the cycles be avoided ? */
1501 static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
1502                        struct gpmc_device_timings *dev_t,
1503                        bool mux)
1504 {
1505     u32 temp;
1506 
1507     /* adv_rd_off */
1508     temp = dev_t->t_avdp_r;
1509     /* XXX: mux check required ? */
1510     if (mux) {
1511         /* XXX: t_avdp not to be required for sync, only added for tusb
1512          * this indirectly necessitates requirement of t_avdp_r and
1513          * t_avdp_w instead of having a single t_avdp
1514          */
1515         temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
1516         temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1517     }
1518     gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1519 
1520     /* oe_on */
1521     temp = dev_t->t_oeasu; /* XXX: remove this ? */
1522     if (mux) {
1523         temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
1524         temp = max_t(u32, temp, gpmc_t->adv_rd_off +
1525                 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
1526     }
1527     gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1528 
1529     /* access */
1530     /* XXX: any scope for improvement ?, by combining oe_on
1531      * and clk_activation, need to check whether
1532      * access = clk_activation + round to sync clk ?
1533      */
1534     temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
1535     temp += gpmc_t->clk_activation;
1536     if (dev_t->cyc_oe)
1537         temp = max_t(u32, temp, gpmc_t->oe_on +
1538                 gpmc_ticks_to_ps(dev_t->cyc_oe));
1539     gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1540 
1541     gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1542     gpmc_t->cs_rd_off = gpmc_t->oe_off;
1543 
1544     /* rd_cycle */
1545     temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
1546     temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
1547                             gpmc_t->access;
1548     /* XXX: barter t_ce_rdyz with t_cez_r ? */
1549     if (dev_t->t_ce_rdyz)
1550         temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
1551     gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1552 
1553     return 0;
1554 }
1555 
1556 static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
1557                     struct gpmc_device_timings *dev_t,
1558                     bool mux)
1559 {
1560     u32 temp;
1561 
1562     /* adv_wr_off */
1563     temp = dev_t->t_avdp_w;
1564     if (mux) {
1565         temp = max_t(u32, temp,
1566             gpmc_t->clk_activation + dev_t->t_avdh);
1567         temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1568     }
1569     gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1570 
1571     /* wr_data_mux_bus */
1572     temp = max_t(u32, dev_t->t_weasu,
1573             gpmc_t->clk_activation + dev_t->t_rdyo);
1574     /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
1575      * and in that case remember to handle we_on properly
1576      */
1577     if (mux) {
1578         temp = max_t(u32, temp,
1579             gpmc_t->adv_wr_off + dev_t->t_aavdh);
1580         temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1581                 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1582     }
1583     gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1584 
1585     /* we_on */
1586     if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1587         gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1588     else
1589         gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1590 
1591     /* wr_access */
1592     /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
1593     gpmc_t->wr_access = gpmc_t->access;
1594 
1595     /* we_off */
1596     temp = gpmc_t->we_on + dev_t->t_wpl;
1597     temp = max_t(u32, temp,
1598             gpmc_t->wr_access + gpmc_ticks_to_ps(1));
1599     temp = max_t(u32, temp,
1600         gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
1601     gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1602 
1603     gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1604                             dev_t->t_wph);
1605 
1606     /* wr_cycle */
1607     temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
1608     temp += gpmc_t->wr_access;
1609     /* XXX: barter t_ce_rdyz with t_cez_w ? */
1610     if (dev_t->t_ce_rdyz)
1611         temp = max_t(u32, temp,
1612                  gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
1613     gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1614 
1615     return 0;
1616 }
1617 
1618 static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
1619                     struct gpmc_device_timings *dev_t,
1620                     bool mux)
1621 {
1622     u32 temp;
1623 
1624     /* adv_rd_off */
1625     temp = dev_t->t_avdp_r;
1626     if (mux)
1627         temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1628     gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1629 
1630     /* oe_on */
1631     temp = dev_t->t_oeasu;
1632     if (mux)
1633         temp = max_t(u32, temp, gpmc_t->adv_rd_off + dev_t->t_aavdh);
1634     gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1635 
1636     /* access */
1637     temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1638              gpmc_t->oe_on + dev_t->t_oe);
1639     temp = max_t(u32, temp, gpmc_t->cs_on + dev_t->t_ce);
1640     temp = max_t(u32, temp, gpmc_t->adv_on + dev_t->t_aa);
1641     gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1642 
1643     gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1644     gpmc_t->cs_rd_off = gpmc_t->oe_off;
1645 
1646     /* rd_cycle */
1647     temp = max_t(u32, dev_t->t_rd_cycle,
1648             gpmc_t->cs_rd_off + dev_t->t_cez_r);
1649     temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1650     gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1651 
1652     return 0;
1653 }
1654 
1655 static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
1656                      struct gpmc_device_timings *dev_t,
1657                      bool mux)
1658 {
1659     u32 temp;
1660 
1661     /* adv_wr_off */
1662     temp = dev_t->t_avdp_w;
1663     if (mux)
1664         temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1665     gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1666 
1667     /* wr_data_mux_bus */
1668     temp = dev_t->t_weasu;
1669     if (mux) {
1670         temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1671         temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1672                 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1673     }
1674     gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1675 
1676     /* we_on */
1677     if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1678         gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1679     else
1680         gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1681 
1682     /* we_off */
1683     temp = gpmc_t->we_on + dev_t->t_wpl;
1684     gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1685 
1686     gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1687                             dev_t->t_wph);
1688 
1689     /* wr_cycle */
1690     temp = max_t(u32, dev_t->t_wr_cycle,
1691                 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1692     gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1693 
1694     return 0;
1695 }
1696 
1697 static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1698             struct gpmc_device_timings *dev_t)
1699 {
1700     u32 temp;
1701 
1702     gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1703                         gpmc_get_fclk_period();
1704 
1705     gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1706                     dev_t->t_bacc,
1707                     gpmc_t->sync_clk);
1708 
1709     temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1710     gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1711 
1712     if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1713         return 0;
1714 
1715     if (dev_t->ce_xdelay)
1716         gpmc_t->bool_timings.cs_extra_delay = true;
1717     if (dev_t->avd_xdelay)
1718         gpmc_t->bool_timings.adv_extra_delay = true;
1719     if (dev_t->oe_xdelay)
1720         gpmc_t->bool_timings.oe_extra_delay = true;
1721     if (dev_t->we_xdelay)
1722         gpmc_t->bool_timings.we_extra_delay = true;
1723 
1724     return 0;
1725 }
1726 
1727 static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
1728                     struct gpmc_device_timings *dev_t,
1729                     bool sync)
1730 {
1731     u32 temp;
1732 
1733     /* cs_on */
1734     gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1735 
1736     /* adv_on */
1737     temp = dev_t->t_avdasu;
1738     if (dev_t->t_ce_avd)
1739         temp = max_t(u32, temp,
1740                 gpmc_t->cs_on + dev_t->t_ce_avd);
1741     gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1742 
1743     if (sync)
1744         gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1745 
1746     return 0;
1747 }
1748 
1749 /*
1750  * TODO: remove this function once all peripherals are confirmed to
1751  * work with generic timing. Simultaneously gpmc_cs_set_timings()
1752  * has to be modified to handle timings in ps instead of ns
1753  */
1754 static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1755 {
1756     t->cs_on /= 1000;
1757     t->cs_rd_off /= 1000;
1758     t->cs_wr_off /= 1000;
1759     t->adv_on /= 1000;
1760     t->adv_rd_off /= 1000;
1761     t->adv_wr_off /= 1000;
1762     t->we_on /= 1000;
1763     t->we_off /= 1000;
1764     t->oe_on /= 1000;
1765     t->oe_off /= 1000;
1766     t->page_burst_access /= 1000;
1767     t->access /= 1000;
1768     t->rd_cycle /= 1000;
1769     t->wr_cycle /= 1000;
1770     t->bus_turnaround /= 1000;
1771     t->cycle2cycle_delay /= 1000;
1772     t->wait_monitoring /= 1000;
1773     t->clk_activation /= 1000;
1774     t->wr_access /= 1000;
1775     t->wr_data_mux_bus /= 1000;
1776 }
1777 
1778 int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
1779               struct gpmc_settings *gpmc_s,
1780               struct gpmc_device_timings *dev_t)
1781 {
1782     bool mux = false, sync = false;
1783 
1784     if (gpmc_s) {
1785         mux = gpmc_s->mux_add_data ? true : false;
1786         sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1787     }
1788 
1789     memset(gpmc_t, 0, sizeof(*gpmc_t));
1790 
1791     gpmc_calc_common_timings(gpmc_t, dev_t, sync);
1792 
1793     if (gpmc_s && gpmc_s->sync_read)
1794         gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
1795     else
1796         gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
1797 
1798     if (gpmc_s && gpmc_s->sync_write)
1799         gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
1800     else
1801         gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
1802 
1803     /* TODO: remove, see function definition */
1804     gpmc_convert_ps_to_ns(gpmc_t);
1805 
1806     return 0;
1807 }
1808 
1809 /**
1810  * gpmc_cs_program_settings - programs non-timing related settings
1811  * @cs:     GPMC chip-select to program
1812  * @p:      pointer to GPMC settings structure
1813  *
1814  * Programs non-timing related settings for a GPMC chip-select, such as
1815  * bus-width, burst configuration, etc. Function should be called once
1816  * for each chip-select that is being used and must be called before
1817  * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1818  * register will be initialised to zero by this function. Returns 0 on
1819  * success and appropriate negative error code on failure.
1820  */
1821 int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1822 {
1823     u32 config1;
1824 
1825     if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1826         pr_err("%s: invalid width %d!", __func__, p->device_width);
1827         return -EINVAL;
1828     }
1829 
1830     /* Address-data multiplexing not supported for NAND devices */
1831     if (p->device_nand && p->mux_add_data) {
1832         pr_err("%s: invalid configuration!\n", __func__);
1833         return -EINVAL;
1834     }
1835 
1836     if ((p->mux_add_data > GPMC_MUX_AD) ||
1837         ((p->mux_add_data == GPMC_MUX_AAD) &&
1838          !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1839         pr_err("%s: invalid multiplex configuration!\n", __func__);
1840         return -EINVAL;
1841     }
1842 
1843     /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1844     if (p->burst_read || p->burst_write) {
1845         switch (p->burst_len) {
1846         case GPMC_BURST_4:
1847         case GPMC_BURST_8:
1848         case GPMC_BURST_16:
1849             break;
1850         default:
1851             pr_err("%s: invalid page/burst-length (%d)\n",
1852                    __func__, p->burst_len);
1853             return -EINVAL;
1854         }
1855     }
1856 
1857     if (p->wait_pin > gpmc_nr_waitpins) {
1858         pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1859         return -EINVAL;
1860     }
1861 
1862     config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1863 
1864     if (p->sync_read)
1865         config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1866     if (p->sync_write)
1867         config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1868     if (p->wait_on_read)
1869         config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1870     if (p->wait_on_write)
1871         config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1872     if (p->wait_on_read || p->wait_on_write)
1873         config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1874     if (p->device_nand)
1875         config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1876     if (p->mux_add_data)
1877         config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1878     if (p->burst_read)
1879         config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1880     if (p->burst_write)
1881         config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1882     if (p->burst_read || p->burst_write) {
1883         config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1884         config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1885     }
1886 
1887     gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1888 
1889     return 0;
1890 }
1891 
1892 #ifdef CONFIG_OF
1893 static void gpmc_cs_set_name(int cs, const char *name)
1894 {
1895     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
1896 
1897     gpmc->name = name;
1898 }
1899 
1900 static const char *gpmc_cs_get_name(int cs)
1901 {
1902     struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
1903 
1904     return gpmc->name;
1905 }
1906 
1907 /**
1908  * gpmc_cs_remap - remaps a chip-select physical base address
1909  * @cs:     chip-select to remap
1910  * @base:   physical base address to re-map chip-select to
1911  *
1912  * Re-maps a chip-select to a new physical base address specified by
1913  * "base". Returns 0 on success and appropriate negative error code
1914  * on failure.
1915  */
1916 static int gpmc_cs_remap(int cs, u32 base)
1917 {
1918     int ret;
1919     u32 old_base, size;
1920 
1921     if (cs >= gpmc_cs_num) {
1922         pr_err("%s: requested chip-select is disabled\n", __func__);
1923         return -ENODEV;
1924     }
1925 
1926     /*
1927      * Make sure we ignore any device offsets from the GPMC partition
1928      * allocated for the chip select and that the new base confirms
1929      * to the GPMC 16MB minimum granularity.
1930      */
1931     base &= ~(SZ_16M - 1);
1932 
1933     gpmc_cs_get_memconf(cs, &old_base, &size);
1934     if (base == old_base)
1935         return 0;
1936 
1937     ret = gpmc_cs_delete_mem(cs);
1938     if (ret < 0)
1939         return ret;
1940 
1941     ret = gpmc_cs_insert_mem(cs, base, size);
1942     if (ret < 0)
1943         return ret;
1944 
1945     ret = gpmc_cs_set_memconf(cs, base, size);
1946 
1947     return ret;
1948 }
1949 
1950 /**
1951  * gpmc_read_settings_dt - read gpmc settings from device-tree
1952  * @np:     pointer to device-tree node for a gpmc child device
1953  * @p:      pointer to gpmc settings structure
1954  *
1955  * Reads the GPMC settings for a GPMC child device from device-tree and
1956  * stores them in the GPMC settings structure passed. The GPMC settings
1957  * structure is initialised to zero by this function and so any
1958  * previously stored settings will be cleared.
1959  */
1960 void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1961 {
1962     memset(p, 0, sizeof(struct gpmc_settings));
1963 
1964     p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1965     p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
1966     of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1967     of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1968 
1969     if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1970         p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1971         p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1972         p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1973         if (!p->burst_read && !p->burst_write)
1974             pr_warn("%s: page/burst-length set but not used!\n",
1975                 __func__);
1976     }
1977 
1978     if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1979         p->wait_on_read = of_property_read_bool(np,
1980                             "gpmc,wait-on-read");
1981         p->wait_on_write = of_property_read_bool(np,
1982                              "gpmc,wait-on-write");
1983         if (!p->wait_on_read && !p->wait_on_write)
1984             pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1985                  __func__);
1986     }
1987 }
1988 
1989 static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1990                         struct gpmc_timings *gpmc_t)
1991 {
1992     struct gpmc_bool_timings *p;
1993 
1994     if (!np || !gpmc_t)
1995         return;
1996 
1997     memset(gpmc_t, 0, sizeof(*gpmc_t));
1998 
1999     /* minimum clock period for syncronous mode */
2000     of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
2001 
2002     /* chip select timtings */
2003     of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
2004     of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
2005     of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
2006 
2007     /* ADV signal timings */
2008     of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
2009     of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
2010     of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
2011     of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
2012                  &gpmc_t->adv_aad_mux_on);
2013     of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
2014                  &gpmc_t->adv_aad_mux_rd_off);
2015     of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
2016                  &gpmc_t->adv_aad_mux_wr_off);
2017 
2018     /* WE signal timings */
2019     of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
2020     of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
2021 
2022     /* OE signal timings */
2023     of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
2024     of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
2025     of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
2026                  &gpmc_t->oe_aad_mux_on);
2027     of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
2028                  &gpmc_t->oe_aad_mux_off);
2029 
2030     /* access and cycle timings */
2031     of_property_read_u32(np, "gpmc,page-burst-access-ns",
2032                  &gpmc_t->page_burst_access);
2033     of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
2034     of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
2035     of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
2036     of_property_read_u32(np, "gpmc,bus-turnaround-ns",
2037                  &gpmc_t->bus_turnaround);
2038     of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
2039                  &gpmc_t->cycle2cycle_delay);
2040     of_property_read_u32(np, "gpmc,wait-monitoring-ns",
2041                  &gpmc_t->wait_monitoring);
2042     of_property_read_u32(np, "gpmc,clk-activation-ns",
2043                  &gpmc_t->clk_activation);
2044 
2045     /* only applicable to OMAP3+ */
2046     of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
2047     of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
2048                  &gpmc_t->wr_data_mux_bus);
2049 
2050     /* bool timing parameters */
2051     p = &gpmc_t->bool_timings;
2052 
2053     p->cycle2cyclediffcsen =
2054         of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
2055     p->cycle2cyclesamecsen =
2056         of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
2057     p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
2058     p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
2059     p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
2060     p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
2061     p->time_para_granularity =
2062         of_property_read_bool(np, "gpmc,time-para-granularity");
2063 }
2064 
2065 /**
2066  * gpmc_probe_generic_child - configures the gpmc for a child device
2067  * @pdev:   pointer to gpmc platform device
2068  * @child:  pointer to device-tree node for child device
2069  *
2070  * Allocates and configures a GPMC chip-select for a child device.
2071  * Returns 0 on success and appropriate negative error code on failure.
2072  */
2073 static int gpmc_probe_generic_child(struct platform_device *pdev,
2074                 struct device_node *child)
2075 {
2076     struct gpmc_settings gpmc_s;
2077     struct gpmc_timings gpmc_t;
2078     struct resource res;
2079     unsigned long base;
2080     const char *name;
2081     int ret, cs;
2082     u32 val;
2083     struct gpio_desc *waitpin_desc = NULL;
2084     struct gpmc_device *gpmc = platform_get_drvdata(pdev);
2085 
2086     if (of_property_read_u32(child, "reg", &cs) < 0) {
2087         dev_err(&pdev->dev, "%pOF has no 'reg' property\n",
2088             child);
2089         return -ENODEV;
2090     }
2091 
2092     if (of_address_to_resource(child, 0, &res) < 0) {
2093         dev_err(&pdev->dev, "%pOF has malformed 'reg' property\n",
2094             child);
2095         return -ENODEV;
2096     }
2097 
2098     /*
2099      * Check if we have multiple instances of the same device
2100      * on a single chip select. If so, use the already initialized
2101      * timings.
2102      */
2103     name = gpmc_cs_get_name(cs);
2104     if (name && of_node_name_eq(child, name))
2105         goto no_timings;
2106 
2107     ret = gpmc_cs_request(cs, resource_size(&res), &base);
2108     if (ret < 0) {
2109         dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
2110         return ret;
2111     }
2112     gpmc_cs_set_name(cs, child->full_name);
2113 
2114     gpmc_read_settings_dt(child, &gpmc_s);
2115     gpmc_read_timings_dt(child, &gpmc_t);
2116 
2117     /*
2118      * For some GPMC devices we still need to rely on the bootloader
2119      * timings because the devices can be connected via FPGA.
2120      * REVISIT: Add timing support from slls644g.pdf.
2121      */
2122     if (!gpmc_t.cs_rd_off) {
2123         WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
2124             cs);
2125         gpmc_cs_show_timings(cs,
2126                      "please add GPMC bootloader timings to .dts");
2127         goto no_timings;
2128     }
2129 
2130     /* CS must be disabled while making changes to gpmc configuration */
2131     gpmc_cs_disable_mem(cs);
2132 
2133     /*
2134      * FIXME: gpmc_cs_request() will map the CS to an arbitrary
2135      * location in the gpmc address space. When booting with
2136      * device-tree we want the NOR flash to be mapped to the
2137      * location specified in the device-tree blob. So remap the
2138      * CS to this location. Once DT migration is complete should
2139      * just make gpmc_cs_request() map a specific address.
2140      */
2141     ret = gpmc_cs_remap(cs, res.start);
2142     if (ret < 0) {
2143         dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
2144             cs, &res.start);
2145         if (res.start < GPMC_MEM_START) {
2146             dev_info(&pdev->dev,
2147                  "GPMC CS %d start cannot be lesser than 0x%x\n",
2148                  cs, GPMC_MEM_START);
2149         } else if (res.end > GPMC_MEM_END) {
2150             dev_info(&pdev->dev,
2151                  "GPMC CS %d end cannot be greater than 0x%x\n",
2152                  cs, GPMC_MEM_END);
2153         }
2154         goto err;
2155     }
2156 
2157     if (of_node_name_eq(child, "nand")) {
2158         /* Warn about older DT blobs with no compatible property */
2159         if (!of_property_read_bool(child, "compatible")) {
2160             dev_warn(&pdev->dev,
2161                  "Incompatible NAND node: missing compatible");
2162             ret = -EINVAL;
2163             goto err;
2164         }
2165     }
2166 
2167     if (of_node_name_eq(child, "onenand")) {
2168         /* Warn about older DT blobs with no compatible property */
2169         if (!of_property_read_bool(child, "compatible")) {
2170             dev_warn(&pdev->dev,
2171                  "Incompatible OneNAND node: missing compatible");
2172             ret = -EINVAL;
2173             goto err;
2174         }
2175     }
2176 
2177     if (of_match_node(omap_nand_ids, child)) {
2178         /* NAND specific setup */
2179         val = 8;
2180         of_property_read_u32(child, "nand-bus-width", &val);
2181         switch (val) {
2182         case 8:
2183             gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
2184             break;
2185         case 16:
2186             gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
2187             break;
2188         default:
2189             dev_err(&pdev->dev, "%pOFn: invalid 'nand-bus-width'\n",
2190                 child);
2191             ret = -EINVAL;
2192             goto err;
2193         }
2194 
2195         /* disable write protect */
2196         gpmc_configure(GPMC_CONFIG_WP, 0);
2197         gpmc_s.device_nand = true;
2198     } else {
2199         ret = of_property_read_u32(child, "bank-width",
2200                        &gpmc_s.device_width);
2201         if (ret < 0 && !gpmc_s.device_width) {
2202             dev_err(&pdev->dev,
2203                 "%pOF has no 'gpmc,device-width' property\n",
2204                 child);
2205             goto err;
2206         }
2207     }
2208 
2209     /* Reserve wait pin if it is required and valid */
2210     if (gpmc_s.wait_on_read || gpmc_s.wait_on_write) {
2211         unsigned int wait_pin = gpmc_s.wait_pin;
2212 
2213         waitpin_desc = gpiochip_request_own_desc(&gpmc->gpio_chip,
2214                              wait_pin, "WAITPIN",
2215                              GPIO_ACTIVE_HIGH,
2216                              GPIOD_IN);
2217         if (IS_ERR(waitpin_desc)) {
2218             dev_err(&pdev->dev, "invalid wait-pin: %d\n", wait_pin);
2219             ret = PTR_ERR(waitpin_desc);
2220             goto err;
2221         }
2222     }
2223 
2224     gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
2225 
2226     ret = gpmc_cs_program_settings(cs, &gpmc_s);
2227     if (ret < 0)
2228         goto err_cs;
2229 
2230     ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
2231     if (ret) {
2232         dev_err(&pdev->dev, "failed to set gpmc timings for: %pOFn\n",
2233             child);
2234         goto err_cs;
2235     }
2236 
2237     /* Clear limited address i.e. enable A26-A11 */
2238     val = gpmc_read_reg(GPMC_CONFIG);
2239     val &= ~GPMC_CONFIG_LIMITEDADDRESS;
2240     gpmc_write_reg(GPMC_CONFIG, val);
2241 
2242     /* Enable CS region */
2243     gpmc_cs_enable_mem(cs);
2244 
2245 no_timings:
2246 
2247     /* create platform device, NULL on error or when disabled */
2248     if (!of_platform_device_create(child, NULL, &pdev->dev))
2249         goto err_child_fail;
2250 
2251     /* create children and other common bus children */
2252     if (of_platform_default_populate(child, NULL, &pdev->dev))
2253         goto err_child_fail;
2254 
2255     return 0;
2256 
2257 err_child_fail:
2258 
2259     dev_err(&pdev->dev, "failed to create gpmc child %pOFn\n", child);
2260     ret = -ENODEV;
2261 
2262 err_cs:
2263     gpiochip_free_own_desc(waitpin_desc);
2264 err:
2265     gpmc_cs_free(cs);
2266 
2267     return ret;
2268 }
2269 
2270 static const struct of_device_id gpmc_dt_ids[];
2271 
2272 static int gpmc_probe_dt(struct platform_device *pdev)
2273 {
2274     int ret;
2275     const struct of_device_id *of_id =
2276         of_match_device(gpmc_dt_ids, &pdev->dev);
2277 
2278     if (!of_id)
2279         return 0;
2280 
2281     ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
2282                    &gpmc_cs_num);
2283     if (ret < 0) {
2284         pr_err("%s: number of chip-selects not defined\n", __func__);
2285         return ret;
2286     } else if (gpmc_cs_num < 1) {
2287         pr_err("%s: all chip-selects are disabled\n", __func__);
2288         return -EINVAL;
2289     } else if (gpmc_cs_num > GPMC_CS_NUM) {
2290         pr_err("%s: number of supported chip-selects cannot be > %d\n",
2291                      __func__, GPMC_CS_NUM);
2292         return -EINVAL;
2293     }
2294 
2295     ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
2296                    &gpmc_nr_waitpins);
2297     if (ret < 0) {
2298         pr_err("%s: number of wait pins not found!\n", __func__);
2299         return ret;
2300     }
2301 
2302     return 0;
2303 }
2304 
2305 static void gpmc_probe_dt_children(struct platform_device *pdev)
2306 {
2307     int ret;
2308     struct device_node *child;
2309 
2310     for_each_available_child_of_node(pdev->dev.of_node, child) {
2311         ret = gpmc_probe_generic_child(pdev, child);
2312         if (ret) {
2313             dev_err(&pdev->dev, "failed to probe DT child '%pOFn': %d\n",
2314                 child, ret);
2315         }
2316     }
2317 }
2318 #else
2319 void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
2320 {
2321     memset(p, 0, sizeof(*p));
2322 }
2323 static int gpmc_probe_dt(struct platform_device *pdev)
2324 {
2325     return 0;
2326 }
2327 
2328 static void gpmc_probe_dt_children(struct platform_device *pdev)
2329 {
2330 }
2331 #endif /* CONFIG_OF */
2332 
2333 static int gpmc_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
2334 {
2335     return 1;   /* we're input only */
2336 }
2337 
2338 static int gpmc_gpio_direction_input(struct gpio_chip *chip,
2339                      unsigned int offset)
2340 {
2341     return 0;   /* we're input only */
2342 }
2343 
2344 static int gpmc_gpio_direction_output(struct gpio_chip *chip,
2345                       unsigned int offset, int value)
2346 {
2347     return -EINVAL; /* we're input only */
2348 }
2349 
2350 static void gpmc_gpio_set(struct gpio_chip *chip, unsigned int offset,
2351               int value)
2352 {
2353 }
2354 
2355 static int gpmc_gpio_get(struct gpio_chip *chip, unsigned int offset)
2356 {
2357     u32 reg;
2358 
2359     offset += 8;
2360 
2361     reg = gpmc_read_reg(GPMC_STATUS) & BIT(offset);
2362 
2363     return !!reg;
2364 }
2365 
2366 static int gpmc_gpio_init(struct gpmc_device *gpmc)
2367 {
2368     int ret;
2369 
2370     gpmc->gpio_chip.parent = gpmc->dev;
2371     gpmc->gpio_chip.owner = THIS_MODULE;
2372     gpmc->gpio_chip.label = DEVICE_NAME;
2373     gpmc->gpio_chip.ngpio = gpmc_nr_waitpins;
2374     gpmc->gpio_chip.get_direction = gpmc_gpio_get_direction;
2375     gpmc->gpio_chip.direction_input = gpmc_gpio_direction_input;
2376     gpmc->gpio_chip.direction_output = gpmc_gpio_direction_output;
2377     gpmc->gpio_chip.set = gpmc_gpio_set;
2378     gpmc->gpio_chip.get = gpmc_gpio_get;
2379     gpmc->gpio_chip.base = -1;
2380 
2381     ret = devm_gpiochip_add_data(gpmc->dev, &gpmc->gpio_chip, NULL);
2382     if (ret < 0) {
2383         dev_err(gpmc->dev, "could not register gpio chip: %d\n", ret);
2384         return ret;
2385     }
2386 
2387     return 0;
2388 }
2389 
2390 static void omap3_gpmc_save_context(struct gpmc_device *gpmc)
2391 {
2392     struct omap3_gpmc_regs *gpmc_context;
2393     int i;
2394 
2395     if (!gpmc || !gpmc_base)
2396         return;
2397 
2398     gpmc_context = &gpmc->context;
2399 
2400     gpmc_context->sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
2401     gpmc_context->irqenable = gpmc_read_reg(GPMC_IRQENABLE);
2402     gpmc_context->timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
2403     gpmc_context->config = gpmc_read_reg(GPMC_CONFIG);
2404     gpmc_context->prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
2405     gpmc_context->prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
2406     gpmc_context->prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
2407     for (i = 0; i < gpmc_cs_num; i++) {
2408         gpmc_context->cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
2409         if (gpmc_context->cs_context[i].is_valid) {
2410             gpmc_context->cs_context[i].config1 =
2411                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
2412             gpmc_context->cs_context[i].config2 =
2413                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
2414             gpmc_context->cs_context[i].config3 =
2415                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
2416             gpmc_context->cs_context[i].config4 =
2417                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
2418             gpmc_context->cs_context[i].config5 =
2419                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
2420             gpmc_context->cs_context[i].config6 =
2421                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
2422             gpmc_context->cs_context[i].config7 =
2423                 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
2424         }
2425     }
2426 }
2427 
2428 static void omap3_gpmc_restore_context(struct gpmc_device *gpmc)
2429 {
2430     struct omap3_gpmc_regs *gpmc_context;
2431     int i;
2432 
2433     if (!gpmc || !gpmc_base)
2434         return;
2435 
2436     gpmc_context = &gpmc->context;
2437 
2438     gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context->sysconfig);
2439     gpmc_write_reg(GPMC_IRQENABLE, gpmc_context->irqenable);
2440     gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context->timeout_ctrl);
2441     gpmc_write_reg(GPMC_CONFIG, gpmc_context->config);
2442     gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context->prefetch_config1);
2443     gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context->prefetch_config2);
2444     gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context->prefetch_control);
2445     for (i = 0; i < gpmc_cs_num; i++) {
2446         if (gpmc_context->cs_context[i].is_valid) {
2447             gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
2448                       gpmc_context->cs_context[i].config1);
2449             gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
2450                       gpmc_context->cs_context[i].config2);
2451             gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
2452                       gpmc_context->cs_context[i].config3);
2453             gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
2454                       gpmc_context->cs_context[i].config4);
2455             gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
2456                       gpmc_context->cs_context[i].config5);
2457             gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
2458                       gpmc_context->cs_context[i].config6);
2459             gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
2460                       gpmc_context->cs_context[i].config7);
2461         } else {
2462             gpmc_cs_write_reg(i, GPMC_CS_CONFIG7, 0);
2463         }
2464     }
2465 }
2466 
2467 static int omap_gpmc_context_notifier(struct notifier_block *nb,
2468                       unsigned long cmd, void *v)
2469 {
2470     struct gpmc_device *gpmc;
2471 
2472     gpmc = container_of(nb, struct gpmc_device, nb);
2473     if (gpmc->is_suspended || pm_runtime_suspended(gpmc->dev))
2474         return NOTIFY_OK;
2475 
2476     switch (cmd) {
2477     case CPU_CLUSTER_PM_ENTER:
2478         omap3_gpmc_save_context(gpmc);
2479         break;
2480     case CPU_CLUSTER_PM_ENTER_FAILED:   /* No need to restore context */
2481         break;
2482     case CPU_CLUSTER_PM_EXIT:
2483         omap3_gpmc_restore_context(gpmc);
2484         break;
2485     }
2486 
2487     return NOTIFY_OK;
2488 }
2489 
2490 static int gpmc_probe(struct platform_device *pdev)
2491 {
2492     int rc;
2493     u32 l;
2494     struct resource *res;
2495     struct gpmc_device *gpmc;
2496 
2497     gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
2498     if (!gpmc)
2499         return -ENOMEM;
2500 
2501     gpmc->dev = &pdev->dev;
2502     platform_set_drvdata(pdev, gpmc);
2503 
2504     res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
2505     if (!res) {
2506         /* legacy DT */
2507         gpmc_base = devm_platform_ioremap_resource(pdev, 0);
2508         if (IS_ERR(gpmc_base))
2509             return PTR_ERR(gpmc_base);
2510     } else {
2511         gpmc_base = devm_ioremap_resource(&pdev->dev, res);
2512         if (IS_ERR(gpmc_base))
2513             return PTR_ERR(gpmc_base);
2514 
2515         res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "data");
2516         if (!res) {
2517             dev_err(&pdev->dev, "couldn't get data reg resource\n");
2518             return -ENOENT;
2519         }
2520 
2521         gpmc->data = res;
2522     }
2523 
2524     gpmc->irq = platform_get_irq(pdev, 0);
2525     if (gpmc->irq < 0)
2526         return gpmc->irq;
2527 
2528     gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
2529     if (IS_ERR(gpmc_l3_clk)) {
2530         dev_err(&pdev->dev, "Failed to get GPMC fck\n");
2531         return PTR_ERR(gpmc_l3_clk);
2532     }
2533 
2534     if (!clk_get_rate(gpmc_l3_clk)) {
2535         dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
2536         return -EINVAL;
2537     }
2538 
2539     if (pdev->dev.of_node) {
2540         rc = gpmc_probe_dt(pdev);
2541         if (rc)
2542             return rc;
2543     } else {
2544         gpmc_cs_num = GPMC_CS_NUM;
2545         gpmc_nr_waitpins = GPMC_NR_WAITPINS;
2546     }
2547 
2548     pm_runtime_enable(&pdev->dev);
2549     pm_runtime_get_sync(&pdev->dev);
2550 
2551     l = gpmc_read_reg(GPMC_REVISION);
2552 
2553     /*
2554      * FIXME: Once device-tree migration is complete the below flags
2555      * should be populated based upon the device-tree compatible
2556      * string. For now just use the IP revision. OMAP3+ devices have
2557      * the wr_access and wr_data_mux_bus register fields. OMAP4+
2558      * devices support the addr-addr-data multiplex protocol.
2559      *
2560      * GPMC IP revisions:
2561      * - OMAP24xx           = 2.0
2562      * - OMAP3xxx           = 5.0
2563      * - OMAP44xx/54xx/AM335x   = 6.0
2564      */
2565     if (GPMC_REVISION_MAJOR(l) > 0x4)
2566         gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
2567     if (GPMC_REVISION_MAJOR(l) > 0x5)
2568         gpmc_capability |= GPMC_HAS_MUX_AAD;
2569     dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
2570          GPMC_REVISION_MINOR(l));
2571 
2572     gpmc_mem_init(gpmc);
2573     rc = gpmc_gpio_init(gpmc);
2574     if (rc)
2575         goto gpio_init_failed;
2576 
2577     gpmc->nirqs = GPMC_NR_NAND_IRQS + gpmc_nr_waitpins;
2578     rc = gpmc_setup_irq(gpmc);
2579     if (rc) {
2580         dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
2581         goto gpio_init_failed;
2582     }
2583 
2584     gpmc_probe_dt_children(pdev);
2585 
2586     gpmc->nb.notifier_call = omap_gpmc_context_notifier;
2587     cpu_pm_register_notifier(&gpmc->nb);
2588 
2589     return 0;
2590 
2591 gpio_init_failed:
2592     gpmc_mem_exit();
2593     pm_runtime_put_sync(&pdev->dev);
2594     pm_runtime_disable(&pdev->dev);
2595 
2596     return rc;
2597 }
2598 
2599 static int gpmc_remove(struct platform_device *pdev)
2600 {
2601     struct gpmc_device *gpmc = platform_get_drvdata(pdev);
2602 
2603     cpu_pm_unregister_notifier(&gpmc->nb);
2604     gpmc_free_irq(gpmc);
2605     gpmc_mem_exit();
2606     pm_runtime_put_sync(&pdev->dev);
2607     pm_runtime_disable(&pdev->dev);
2608 
2609     return 0;
2610 }
2611 
2612 #ifdef CONFIG_PM_SLEEP
2613 static int gpmc_suspend(struct device *dev)
2614 {
2615     struct gpmc_device *gpmc = dev_get_drvdata(dev);
2616 
2617     omap3_gpmc_save_context(gpmc);
2618     pm_runtime_put_sync(dev);
2619     gpmc->is_suspended = 1;
2620 
2621     return 0;
2622 }
2623 
2624 static int gpmc_resume(struct device *dev)
2625 {
2626     struct gpmc_device *gpmc = dev_get_drvdata(dev);
2627 
2628     pm_runtime_get_sync(dev);
2629     omap3_gpmc_restore_context(gpmc);
2630     gpmc->is_suspended = 0;
2631 
2632     return 0;
2633 }
2634 #endif
2635 
2636 static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
2637 
2638 #ifdef CONFIG_OF
2639 static const struct of_device_id gpmc_dt_ids[] = {
2640     { .compatible = "ti,omap2420-gpmc" },
2641     { .compatible = "ti,omap2430-gpmc" },
2642     { .compatible = "ti,omap3430-gpmc" },   /* omap3430 & omap3630 */
2643     { .compatible = "ti,omap4430-gpmc" },   /* omap4430 & omap4460 & omap543x */
2644     { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
2645     { .compatible = "ti,am64-gpmc" },
2646     { }
2647 };
2648 MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
2649 #endif
2650 
2651 static struct platform_driver gpmc_driver = {
2652     .probe      = gpmc_probe,
2653     .remove     = gpmc_remove,
2654     .driver     = {
2655         .name   = DEVICE_NAME,
2656         .of_match_table = of_match_ptr(gpmc_dt_ids),
2657         .pm = &gpmc_pm_ops,
2658     },
2659 };
2660 
2661 module_platform_driver(gpmc_driver);
2662 
2663 MODULE_DESCRIPTION("Texas Instruments GPMC driver");
2664 MODULE_LICENSE("GPL");