Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 1 | /* |
| 2 | * GPMC support functions |
| 3 | * |
| 4 | * Copyright (C) 2005-2006 Nokia Corporation |
| 5 | * |
| 6 | * Author: Juha Yrjola |
| 7 | * |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 8 | * Copyright (C) 2009 Texas Instruments |
| 9 | * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com> |
| 10 | * |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License version 2 as |
| 13 | * published by the Free Software Foundation. |
| 14 | */ |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 15 | #undef DEBUG |
| 16 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 17 | #include <linux/irq.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/init.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/clk.h> |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 22 | #include <linux/ioport.h> |
| 23 | #include <linux/spinlock.h> |
Russell King | fced80c | 2008-09-06 12:10:45 +0100 | [diff] [blame] | 24 | #include <linux/io.h> |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 25 | #include <linux/module.h> |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 26 | #include <linux/interrupt.h> |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 27 | #include <linux/platform_device.h> |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 28 | #include <linux/of.h> |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 29 | #include <linux/of_address.h> |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 30 | #include <linux/of_mtd.h> |
| 31 | #include <linux/of_device.h> |
| 32 | #include <linux/mtd/nand.h> |
avinash philip | b3f5525 | 2013-06-12 16:30:56 +0530 | [diff] [blame] | 33 | #include <linux/pm_runtime.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 34 | |
Afzal Mohammed | bc3668e | 2012-09-29 12:26:13 +0530 | [diff] [blame] | 35 | #include <linux/platform_data/mtd-nand-omap2.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 36 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 37 | #include <asm/mach-types.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 38 | |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 39 | #include "soc.h" |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 40 | #include "common.h" |
Tony Lindgren | 25c7d49 | 2012-10-02 17:25:48 -0700 | [diff] [blame] | 41 | #include "omap_device.h" |
Afzal Mohammed | 3ef5d00 | 2012-10-05 10:37:27 +0530 | [diff] [blame] | 42 | #include "gpmc.h" |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 43 | #include "gpmc-nand.h" |
Ezequiel Garcia | 75d3625 | 2013-01-25 09:23:11 -0300 | [diff] [blame] | 44 | #include "gpmc-onenand.h" |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 45 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 46 | #define DEVICE_NAME "omap-gpmc" |
| 47 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 48 | /* GPMC register offsets */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 49 | #define GPMC_REVISION 0x00 |
| 50 | #define GPMC_SYSCONFIG 0x10 |
| 51 | #define GPMC_SYSSTATUS 0x14 |
| 52 | #define GPMC_IRQSTATUS 0x18 |
| 53 | #define GPMC_IRQENABLE 0x1c |
| 54 | #define GPMC_TIMEOUT_CONTROL 0x40 |
| 55 | #define GPMC_ERR_ADDRESS 0x44 |
| 56 | #define GPMC_ERR_TYPE 0x48 |
| 57 | #define GPMC_CONFIG 0x50 |
| 58 | #define GPMC_STATUS 0x54 |
| 59 | #define GPMC_PREFETCH_CONFIG1 0x1e0 |
| 60 | #define GPMC_PREFETCH_CONFIG2 0x1e4 |
Thara Gopinath | 15e02a3 | 2008-04-28 16:55:01 +0530 | [diff] [blame] | 61 | #define GPMC_PREFETCH_CONTROL 0x1ec |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 62 | #define GPMC_PREFETCH_STATUS 0x1f0 |
| 63 | #define GPMC_ECC_CONFIG 0x1f4 |
| 64 | #define GPMC_ECC_CONTROL 0x1f8 |
| 65 | #define GPMC_ECC_SIZE_CONFIG 0x1fc |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 66 | #define GPMC_ECC1_RESULT 0x200 |
Ivan Djelic | 8d602cf | 2012-04-26 14:17:49 +0200 | [diff] [blame] | 67 | #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */ |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 68 | #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */ |
| 69 | #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */ |
| 70 | #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */ |
pekon gupta | 27c9fd6 | 2014-05-19 13:24:39 +0530 | [diff] [blame] | 71 | #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */ |
| 72 | #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */ |
| 73 | #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 74 | |
Yegor Yefremov | 2c65e74 | 2012-05-09 08:32:49 -0700 | [diff] [blame] | 75 | /* GPMC ECC control settings */ |
| 76 | #define GPMC_ECC_CTRL_ECCCLEAR 0x100 |
| 77 | #define GPMC_ECC_CTRL_ECCDISABLE 0x000 |
| 78 | #define GPMC_ECC_CTRL_ECCREG1 0x001 |
| 79 | #define GPMC_ECC_CTRL_ECCREG2 0x002 |
| 80 | #define GPMC_ECC_CTRL_ECCREG3 0x003 |
| 81 | #define GPMC_ECC_CTRL_ECCREG4 0x004 |
| 82 | #define GPMC_ECC_CTRL_ECCREG5 0x005 |
| 83 | #define GPMC_ECC_CTRL_ECCREG6 0x006 |
| 84 | #define GPMC_ECC_CTRL_ECCREG7 0x007 |
| 85 | #define GPMC_ECC_CTRL_ECCREG8 0x008 |
| 86 | #define GPMC_ECC_CTRL_ECCREG9 0x009 |
| 87 | |
Afzal Mohammed | 559d94b | 2012-05-28 17:51:37 +0530 | [diff] [blame] | 88 | #define GPMC_CONFIG2_CSEXTRADELAY BIT(7) |
| 89 | #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7) |
| 90 | #define GPMC_CONFIG4_OEEXTRADELAY BIT(7) |
| 91 | #define GPMC_CONFIG4_WEEXTRADELAY BIT(23) |
| 92 | #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6) |
| 93 | #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7) |
| 94 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 95 | #define GPMC_CS0_OFFSET 0x60 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 96 | #define GPMC_CS_SIZE 0x30 |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 97 | #define GPMC_BCH_SIZE 0x10 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 98 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 99 | #define GPMC_MEM_END 0x3FFFFFFF |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 100 | |
| 101 | #define GPMC_CHUNK_SHIFT 24 /* 16 MB */ |
| 102 | #define GPMC_SECTION_SHIFT 28 /* 128 MB */ |
| 103 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 104 | #define CS_NUM_SHIFT 24 |
| 105 | #define ENABLE_PREFETCH (0x1 << 7) |
| 106 | #define DMA_MPU_MODE 2 |
| 107 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 108 | #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf) |
| 109 | #define GPMC_REVISION_MINOR(l) (l & 0xf) |
| 110 | |
| 111 | #define GPMC_HAS_WR_ACCESS 0x1 |
| 112 | #define GPMC_HAS_WR_DATA_MUX_BUS 0x2 |
Jon Hunter | aa8d476 | 2013-02-21 15:25:23 -0600 | [diff] [blame] | 113 | #define GPMC_HAS_MUX_AAD 0x4 |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 114 | |
Jon Hunter | 9f83315 | 2013-02-20 15:53:38 -0600 | [diff] [blame] | 115 | #define GPMC_NR_WAITPINS 4 |
| 116 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 117 | /* XXX: Only NAND irq has been considered,currently these are the only ones used |
| 118 | */ |
| 119 | #define GPMC_NR_IRQ 2 |
| 120 | |
| 121 | struct gpmc_client_irq { |
| 122 | unsigned irq; |
| 123 | u32 bitmask; |
| 124 | }; |
| 125 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 126 | /* Structure to save gpmc cs context */ |
| 127 | struct gpmc_cs_config { |
| 128 | u32 config1; |
| 129 | u32 config2; |
| 130 | u32 config3; |
| 131 | u32 config4; |
| 132 | u32 config5; |
| 133 | u32 config6; |
| 134 | u32 config7; |
| 135 | int is_valid; |
| 136 | }; |
| 137 | |
| 138 | /* |
| 139 | * Structure to save/restore gpmc context |
| 140 | * to support core off on OMAP3 |
| 141 | */ |
| 142 | struct omap3_gpmc_regs { |
| 143 | u32 sysconfig; |
| 144 | u32 irqenable; |
| 145 | u32 timeout_ctrl; |
| 146 | u32 config; |
| 147 | u32 prefetch_config1; |
| 148 | u32 prefetch_config2; |
| 149 | u32 prefetch_control; |
| 150 | struct gpmc_cs_config cs_context[GPMC_CS_NUM]; |
| 151 | }; |
| 152 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 153 | static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ]; |
| 154 | static struct irq_chip gpmc_irq_chip; |
Chen Gang | af07219 | 2013-08-22 15:47:21 +0800 | [diff] [blame] | 155 | static int gpmc_irq_start; |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 156 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 157 | static struct resource gpmc_mem_root; |
| 158 | static struct resource gpmc_cs_mem[GPMC_CS_NUM]; |
Thomas Gleixner | 87b247c | 2007-05-10 22:33:04 -0700 | [diff] [blame] | 159 | static DEFINE_SPINLOCK(gpmc_mem_lock); |
Jon Hunter | 6797b4f | 2013-02-01 10:38:45 -0600 | [diff] [blame] | 160 | /* Define chip-selects as reserved by default until probe completes */ |
| 161 | static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1); |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 162 | static unsigned int gpmc_cs_num = GPMC_CS_NUM; |
Jon Hunter | 9f83315 | 2013-02-20 15:53:38 -0600 | [diff] [blame] | 163 | static unsigned int gpmc_nr_waitpins; |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 164 | static struct device *gpmc_dev; |
| 165 | static int gpmc_irq; |
| 166 | static resource_size_t phys_base, mem_size; |
| 167 | static unsigned gpmc_capability; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 168 | static void __iomem *gpmc_base; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 169 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 170 | static struct clk *gpmc_l3_clk; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 171 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 172 | static irqreturn_t gpmc_handle_irq(int irq, void *dev); |
| 173 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 174 | static void gpmc_write_reg(int idx, u32 val) |
| 175 | { |
Victor Kamensky | edfaf05 | 2014-04-15 20:37:46 +0300 | [diff] [blame] | 176 | writel_relaxed(val, gpmc_base + idx); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | static u32 gpmc_read_reg(int idx) |
| 180 | { |
Victor Kamensky | edfaf05 | 2014-04-15 20:37:46 +0300 | [diff] [blame] | 181 | return readl_relaxed(gpmc_base + idx); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | void gpmc_cs_write_reg(int cs, int idx, u32 val) |
| 185 | { |
| 186 | void __iomem *reg_addr; |
| 187 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 188 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Victor Kamensky | edfaf05 | 2014-04-15 20:37:46 +0300 | [diff] [blame] | 189 | writel_relaxed(val, reg_addr); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 190 | } |
| 191 | |
Ezequiel Garcia | 3fc089e | 2013-02-12 16:22:17 -0300 | [diff] [blame] | 192 | static u32 gpmc_cs_read_reg(int cs, int idx) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 193 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 194 | void __iomem *reg_addr; |
| 195 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 196 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Victor Kamensky | edfaf05 | 2014-04-15 20:37:46 +0300 | [diff] [blame] | 197 | return readl_relaxed(reg_addr); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 200 | /* TODO: Add support for gpmc_fck to clock framework and use it */ |
Ezequiel Garcia | 3fc089e | 2013-02-12 16:22:17 -0300 | [diff] [blame] | 201 | static unsigned long gpmc_get_fclk_period(void) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 202 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 203 | unsigned long rate = clk_get_rate(gpmc_l3_clk); |
| 204 | |
| 205 | if (rate == 0) { |
| 206 | printk(KERN_WARNING "gpmc_l3_clk not enabled\n"); |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | rate /= 1000; |
| 211 | rate = 1000000000 / rate; /* In picoseconds */ |
| 212 | |
| 213 | return rate; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 214 | } |
| 215 | |
Ezequiel Garcia | 3fc089e | 2013-02-12 16:22:17 -0300 | [diff] [blame] | 216 | static unsigned int gpmc_ns_to_ticks(unsigned int time_ns) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 217 | { |
| 218 | unsigned long tick_ps; |
| 219 | |
| 220 | /* Calculate in picosecs to yield more exact results */ |
| 221 | tick_ps = gpmc_get_fclk_period(); |
| 222 | |
| 223 | return (time_ns * 1000 + tick_ps - 1) / tick_ps; |
| 224 | } |
| 225 | |
Ezequiel Garcia | 3fc089e | 2013-02-12 16:22:17 -0300 | [diff] [blame] | 226 | static unsigned int gpmc_ps_to_ticks(unsigned int time_ps) |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 227 | { |
| 228 | unsigned long tick_ps; |
| 229 | |
| 230 | /* Calculate in picosecs to yield more exact results */ |
| 231 | tick_ps = gpmc_get_fclk_period(); |
| 232 | |
| 233 | return (time_ps + tick_ps - 1) / tick_ps; |
| 234 | } |
| 235 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 236 | unsigned int gpmc_ticks_to_ns(unsigned int ticks) |
| 237 | { |
| 238 | return ticks * gpmc_get_fclk_period() / 1000; |
| 239 | } |
| 240 | |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 241 | static unsigned int gpmc_ticks_to_ps(unsigned int ticks) |
| 242 | { |
| 243 | return ticks * gpmc_get_fclk_period(); |
| 244 | } |
| 245 | |
| 246 | static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps) |
| 247 | { |
| 248 | unsigned long ticks = gpmc_ps_to_ticks(time_ps); |
| 249 | |
| 250 | return ticks * gpmc_get_fclk_period(); |
| 251 | } |
| 252 | |
Afzal Mohammed | 559d94b | 2012-05-28 17:51:37 +0530 | [diff] [blame] | 253 | static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value) |
| 254 | { |
| 255 | u32 l; |
| 256 | |
| 257 | l = gpmc_cs_read_reg(cs, reg); |
| 258 | if (value) |
| 259 | l |= mask; |
| 260 | else |
| 261 | l &= ~mask; |
| 262 | gpmc_cs_write_reg(cs, reg, l); |
| 263 | } |
| 264 | |
| 265 | static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p) |
| 266 | { |
| 267 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1, |
| 268 | GPMC_CONFIG1_TIME_PARA_GRAN, |
| 269 | p->time_para_granularity); |
| 270 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2, |
| 271 | GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay); |
| 272 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3, |
| 273 | GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay); |
| 274 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4, |
| 275 | GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay); |
| 276 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4, |
| 277 | GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay); |
| 278 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6, |
| 279 | GPMC_CONFIG6_CYCLE2CYCLESAMECSEN, |
| 280 | p->cycle2cyclesamecsen); |
| 281 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6, |
| 282 | GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN, |
| 283 | p->cycle2cyclediffcsen); |
| 284 | } |
| 285 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 286 | static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, |
Juha Yrjola | 2aab646 | 2006-06-26 16:16:21 -0700 | [diff] [blame] | 287 | int time, const char *name) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 288 | { |
| 289 | u32 l; |
| 290 | int ticks, mask, nr_bits; |
| 291 | |
| 292 | if (time == 0) |
| 293 | ticks = 0; |
| 294 | else |
| 295 | ticks = gpmc_ns_to_ticks(time); |
| 296 | nr_bits = end_bit - st_bit + 1; |
Roger Quadros | 8032374 | 2014-08-29 19:11:50 +0300 | [diff] [blame^] | 297 | mask = (1 << nr_bits) - 1; |
| 298 | |
| 299 | if (ticks > mask) { |
| 300 | pr_err("%s: GPMC error! CS%d: %s: %d ns, %d ticks > %d\n", |
| 301 | __func__, cs, name, time, ticks, mask); |
| 302 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 303 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 304 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 305 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 306 | l = gpmc_cs_read_reg(cs, reg); |
| 307 | #ifdef DEBUG |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 308 | printk(KERN_INFO |
| 309 | "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n", |
Juha Yrjola | 2aab646 | 2006-06-26 16:16:21 -0700 | [diff] [blame] | 310 | cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000, |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 311 | (l >> st_bit) & mask, time); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 312 | #endif |
| 313 | l &= ~(mask << st_bit); |
| 314 | l |= ticks << st_bit; |
| 315 | gpmc_cs_write_reg(cs, reg, l); |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 320 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 321 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), \ |
| 322 | t->field, #field) < 0) \ |
| 323 | return -1 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 324 | |
Afzal Mohammed | 1b47ca1 | 2012-08-19 18:29:45 +0530 | [diff] [blame] | 325 | int gpmc_calc_divider(unsigned int sync_clk) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 326 | { |
| 327 | int div; |
| 328 | u32 l; |
| 329 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 330 | l = sync_clk + (gpmc_get_fclk_period() - 1); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 331 | div = l / gpmc_get_fclk_period(); |
| 332 | if (div > 4) |
| 333 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 334 | if (div <= 0) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 335 | div = 1; |
| 336 | |
| 337 | return div; |
| 338 | } |
| 339 | |
| 340 | int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t) |
| 341 | { |
| 342 | int div; |
| 343 | u32 l; |
| 344 | |
Afzal Mohammed | 1b47ca1 | 2012-08-19 18:29:45 +0530 | [diff] [blame] | 345 | div = gpmc_calc_divider(t->sync_clk); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 346 | if (div < 0) |
Paul Walmsley | a032d33 | 2012-08-03 09:21:10 -0600 | [diff] [blame] | 347 | return div; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 348 | |
| 349 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on); |
| 350 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off); |
| 351 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off); |
| 352 | |
| 353 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on); |
| 354 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off); |
| 355 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off); |
| 356 | |
| 357 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on); |
| 358 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off); |
| 359 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on); |
| 360 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off); |
| 361 | |
| 362 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle); |
| 363 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle); |
| 364 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access); |
| 365 | |
| 366 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access); |
| 367 | |
Afzal Mohammed | 559d94b | 2012-05-28 17:51:37 +0530 | [diff] [blame] | 368 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround); |
| 369 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay); |
| 370 | |
| 371 | GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring); |
| 372 | GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation); |
| 373 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 374 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 375 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 376 | if (gpmc_capability & GPMC_HAS_WR_ACCESS) |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 377 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access); |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 378 | |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 379 | /* caller is expected to have initialized CONFIG1 to cover |
| 380 | * at least sync vs async |
| 381 | */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 382 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 383 | if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) { |
| 384 | #ifdef DEBUG |
| 385 | printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n", |
| 386 | cs, (div * gpmc_get_fclk_period()) / 1000, div); |
| 387 | #endif |
| 388 | l &= ~0x03; |
| 389 | l |= (div - 1); |
| 390 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l); |
| 391 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 392 | |
Afzal Mohammed | 559d94b | 2012-05-28 17:51:37 +0530 | [diff] [blame] | 393 | gpmc_cs_bool_timings(cs, &t->bool_timings); |
| 394 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 395 | return 0; |
| 396 | } |
| 397 | |
Jon Hunter | c71f8e9 | 2013-03-06 12:00:10 -0600 | [diff] [blame] | 398 | static int gpmc_cs_enable_mem(int cs, u32 base, u32 size) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 399 | { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 400 | u32 l; |
| 401 | u32 mask; |
| 402 | |
Jon Hunter | c71f8e9 | 2013-03-06 12:00:10 -0600 | [diff] [blame] | 403 | /* |
| 404 | * Ensure that base address is aligned on a |
| 405 | * boundary equal to or greater than size. |
| 406 | */ |
| 407 | if (base & (size - 1)) |
| 408 | return -EINVAL; |
| 409 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 410 | mask = (1 << GPMC_SECTION_SHIFT) - size; |
| 411 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 412 | l &= ~0x3f; |
| 413 | l = (base >> GPMC_CHUNK_SHIFT) & 0x3f; |
| 414 | l &= ~(0x0f << 8); |
| 415 | l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8; |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 416 | l |= GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 417 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
Jon Hunter | c71f8e9 | 2013-03-06 12:00:10 -0600 | [diff] [blame] | 418 | |
| 419 | return 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | static void gpmc_cs_disable_mem(int cs) |
| 423 | { |
| 424 | u32 l; |
| 425 | |
| 426 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 427 | l &= ~GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 428 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 429 | } |
| 430 | |
| 431 | static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size) |
| 432 | { |
| 433 | u32 l; |
| 434 | u32 mask; |
| 435 | |
| 436 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 437 | *base = (l & 0x3f) << GPMC_CHUNK_SHIFT; |
| 438 | mask = (l >> 8) & 0x0f; |
| 439 | *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT); |
| 440 | } |
| 441 | |
| 442 | static int gpmc_cs_mem_enabled(int cs) |
| 443 | { |
| 444 | u32 l; |
| 445 | |
| 446 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 447 | return l & GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 448 | } |
| 449 | |
Ezequiel Garcia | f5d8eda | 2013-02-12 16:22:24 -0300 | [diff] [blame] | 450 | static void gpmc_cs_set_reserved(int cs, int reserved) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 451 | { |
| 452 | gpmc_cs_map &= ~(1 << cs); |
| 453 | gpmc_cs_map |= (reserved ? 1 : 0) << cs; |
| 454 | } |
| 455 | |
Ezequiel Garcia | ae9d908 | 2013-02-12 16:22:19 -0300 | [diff] [blame] | 456 | static bool gpmc_cs_reserved(int cs) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 457 | { |
| 458 | return gpmc_cs_map & (1 << cs); |
| 459 | } |
| 460 | |
| 461 | static unsigned long gpmc_mem_align(unsigned long size) |
| 462 | { |
| 463 | int order; |
| 464 | |
| 465 | size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1); |
| 466 | order = GPMC_CHUNK_SHIFT - 1; |
| 467 | do { |
| 468 | size >>= 1; |
| 469 | order++; |
| 470 | } while (size); |
| 471 | size = 1 << order; |
| 472 | return size; |
| 473 | } |
| 474 | |
| 475 | static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size) |
| 476 | { |
| 477 | struct resource *res = &gpmc_cs_mem[cs]; |
| 478 | int r; |
| 479 | |
| 480 | size = gpmc_mem_align(size); |
| 481 | spin_lock(&gpmc_mem_lock); |
| 482 | res->start = base; |
| 483 | res->end = base + size - 1; |
| 484 | r = request_resource(&gpmc_mem_root, res); |
| 485 | spin_unlock(&gpmc_mem_lock); |
| 486 | |
| 487 | return r; |
| 488 | } |
| 489 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 490 | static int gpmc_cs_delete_mem(int cs) |
| 491 | { |
| 492 | struct resource *res = &gpmc_cs_mem[cs]; |
| 493 | int r; |
| 494 | |
| 495 | spin_lock(&gpmc_mem_lock); |
Tony Lindgren | efe8072 | 2014-04-21 19:26:13 -0700 | [diff] [blame] | 496 | r = release_resource(res); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 497 | res->start = 0; |
| 498 | res->end = 0; |
| 499 | spin_unlock(&gpmc_mem_lock); |
| 500 | |
| 501 | return r; |
| 502 | } |
| 503 | |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 504 | /** |
| 505 | * gpmc_cs_remap - remaps a chip-select physical base address |
| 506 | * @cs: chip-select to remap |
| 507 | * @base: physical base address to re-map chip-select to |
| 508 | * |
| 509 | * Re-maps a chip-select to a new physical base address specified by |
| 510 | * "base". Returns 0 on success and appropriate negative error code |
| 511 | * on failure. |
| 512 | */ |
| 513 | static int gpmc_cs_remap(int cs, u32 base) |
| 514 | { |
| 515 | int ret; |
| 516 | u32 old_base, size; |
| 517 | |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 518 | if (cs > gpmc_cs_num) { |
| 519 | pr_err("%s: requested chip-select is disabled\n", __func__); |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 520 | return -ENODEV; |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 521 | } |
Tony Lindgren | fb677ef | 2014-04-21 19:26:13 -0700 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * Make sure we ignore any device offsets from the GPMC partition |
| 525 | * allocated for the chip select and that the new base confirms |
| 526 | * to the GPMC 16MB minimum granularity. |
| 527 | */ |
| 528 | base &= ~(SZ_16M - 1); |
| 529 | |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 530 | gpmc_cs_get_memconf(cs, &old_base, &size); |
| 531 | if (base == old_base) |
| 532 | return 0; |
| 533 | gpmc_cs_disable_mem(cs); |
| 534 | ret = gpmc_cs_delete_mem(cs); |
| 535 | if (ret < 0) |
| 536 | return ret; |
| 537 | ret = gpmc_cs_insert_mem(cs, base, size); |
| 538 | if (ret < 0) |
| 539 | return ret; |
Jon Hunter | c71f8e9 | 2013-03-06 12:00:10 -0600 | [diff] [blame] | 540 | ret = gpmc_cs_enable_mem(cs, base, size); |
| 541 | if (ret < 0) |
| 542 | return ret; |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 547 | int gpmc_cs_request(int cs, unsigned long size, unsigned long *base) |
| 548 | { |
| 549 | struct resource *res = &gpmc_cs_mem[cs]; |
| 550 | int r = -1; |
| 551 | |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 552 | if (cs > gpmc_cs_num) { |
| 553 | pr_err("%s: requested chip-select is disabled\n", __func__); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 554 | return -ENODEV; |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 555 | } |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 556 | size = gpmc_mem_align(size); |
| 557 | if (size > (1 << GPMC_SECTION_SHIFT)) |
| 558 | return -ENOMEM; |
| 559 | |
| 560 | spin_lock(&gpmc_mem_lock); |
| 561 | if (gpmc_cs_reserved(cs)) { |
| 562 | r = -EBUSY; |
| 563 | goto out; |
| 564 | } |
| 565 | if (gpmc_cs_mem_enabled(cs)) |
| 566 | r = adjust_resource(res, res->start & ~(size - 1), size); |
| 567 | if (r < 0) |
| 568 | r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0, |
| 569 | size, NULL, NULL); |
| 570 | if (r < 0) |
| 571 | goto out; |
| 572 | |
Jon Hunter | c71f8e9 | 2013-03-06 12:00:10 -0600 | [diff] [blame] | 573 | r = gpmc_cs_enable_mem(cs, res->start, resource_size(res)); |
| 574 | if (r < 0) { |
| 575 | release_resource(res); |
| 576 | goto out; |
| 577 | } |
| 578 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 579 | *base = res->start; |
| 580 | gpmc_cs_set_reserved(cs, 1); |
| 581 | out: |
| 582 | spin_unlock(&gpmc_mem_lock); |
| 583 | return r; |
| 584 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 585 | EXPORT_SYMBOL(gpmc_cs_request); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 586 | |
| 587 | void gpmc_cs_free(int cs) |
| 588 | { |
Tony Lindgren | efe8072 | 2014-04-21 19:26:13 -0700 | [diff] [blame] | 589 | struct resource *res = &gpmc_cs_mem[cs]; |
| 590 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 591 | spin_lock(&gpmc_mem_lock); |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 592 | if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 593 | printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs); |
| 594 | BUG(); |
| 595 | spin_unlock(&gpmc_mem_lock); |
| 596 | return; |
| 597 | } |
| 598 | gpmc_cs_disable_mem(cs); |
Tony Lindgren | efe8072 | 2014-04-21 19:26:13 -0700 | [diff] [blame] | 599 | if (res->flags) |
| 600 | release_resource(res); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 601 | gpmc_cs_set_reserved(cs, 0); |
| 602 | spin_unlock(&gpmc_mem_lock); |
| 603 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 604 | EXPORT_SYMBOL(gpmc_cs_free); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 605 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 606 | /** |
Jon Hunter | 3a54435 | 2013-02-21 13:00:21 -0600 | [diff] [blame] | 607 | * gpmc_configure - write request to configure gpmc |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 608 | * @cmd: command type |
| 609 | * @wval: value to write |
| 610 | * @return status of the operation |
| 611 | */ |
Jon Hunter | 3a54435 | 2013-02-21 13:00:21 -0600 | [diff] [blame] | 612 | int gpmc_configure(int cmd, int wval) |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 613 | { |
Jon Hunter | 3a54435 | 2013-02-21 13:00:21 -0600 | [diff] [blame] | 614 | u32 regval; |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 615 | |
| 616 | switch (cmd) { |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 617 | case GPMC_ENABLE_IRQ: |
| 618 | gpmc_write_reg(GPMC_IRQENABLE, wval); |
| 619 | break; |
| 620 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 621 | case GPMC_SET_IRQ_STATUS: |
| 622 | gpmc_write_reg(GPMC_IRQSTATUS, wval); |
| 623 | break; |
| 624 | |
| 625 | case GPMC_CONFIG_WP: |
| 626 | regval = gpmc_read_reg(GPMC_CONFIG); |
| 627 | if (wval) |
| 628 | regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */ |
| 629 | else |
| 630 | regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */ |
| 631 | gpmc_write_reg(GPMC_CONFIG, regval); |
| 632 | break; |
| 633 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 634 | default: |
Jon Hunter | 3a54435 | 2013-02-21 13:00:21 -0600 | [diff] [blame] | 635 | pr_err("%s: command not supported\n", __func__); |
| 636 | return -EINVAL; |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 637 | } |
| 638 | |
Jon Hunter | 3a54435 | 2013-02-21 13:00:21 -0600 | [diff] [blame] | 639 | return 0; |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 640 | } |
Jon Hunter | 3a54435 | 2013-02-21 13:00:21 -0600 | [diff] [blame] | 641 | EXPORT_SYMBOL(gpmc_configure); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 642 | |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 643 | void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs) |
| 644 | { |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 645 | int i; |
| 646 | |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 647 | reg->gpmc_status = gpmc_base + GPMC_STATUS; |
| 648 | reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET + |
| 649 | GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs; |
| 650 | reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET + |
| 651 | GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs; |
| 652 | reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET + |
| 653 | GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs; |
| 654 | reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1; |
| 655 | reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2; |
| 656 | reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL; |
| 657 | reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS; |
| 658 | reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG; |
| 659 | reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL; |
| 660 | reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG; |
| 661 | reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT; |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 662 | |
| 663 | for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) { |
| 664 | reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 + |
| 665 | GPMC_BCH_SIZE * i; |
| 666 | reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 + |
| 667 | GPMC_BCH_SIZE * i; |
| 668 | reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 + |
| 669 | GPMC_BCH_SIZE * i; |
| 670 | reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 + |
| 671 | GPMC_BCH_SIZE * i; |
pekon gupta | 27c9fd6 | 2014-05-19 13:24:39 +0530 | [diff] [blame] | 672 | reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 + |
| 673 | i * GPMC_BCH_SIZE; |
| 674 | reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 + |
| 675 | i * GPMC_BCH_SIZE; |
| 676 | reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 + |
| 677 | i * GPMC_BCH_SIZE; |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 678 | } |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 679 | } |
| 680 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 681 | int gpmc_get_client_irq(unsigned irq_config) |
| 682 | { |
| 683 | int i; |
| 684 | |
| 685 | if (hweight32(irq_config) > 1) |
| 686 | return 0; |
| 687 | |
| 688 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 689 | if (gpmc_client_irq[i].bitmask & irq_config) |
| 690 | return gpmc_client_irq[i].irq; |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | static int gpmc_irq_endis(unsigned irq, bool endis) |
| 696 | { |
| 697 | int i; |
| 698 | u32 regval; |
| 699 | |
| 700 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 701 | if (irq == gpmc_client_irq[i].irq) { |
| 702 | regval = gpmc_read_reg(GPMC_IRQENABLE); |
| 703 | if (endis) |
| 704 | regval |= gpmc_client_irq[i].bitmask; |
| 705 | else |
| 706 | regval &= ~gpmc_client_irq[i].bitmask; |
| 707 | gpmc_write_reg(GPMC_IRQENABLE, regval); |
| 708 | break; |
| 709 | } |
| 710 | |
| 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static void gpmc_irq_disable(struct irq_data *p) |
| 715 | { |
| 716 | gpmc_irq_endis(p->irq, false); |
| 717 | } |
| 718 | |
| 719 | static void gpmc_irq_enable(struct irq_data *p) |
| 720 | { |
| 721 | gpmc_irq_endis(p->irq, true); |
| 722 | } |
| 723 | |
| 724 | static void gpmc_irq_noop(struct irq_data *data) { } |
| 725 | |
| 726 | static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; } |
| 727 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 728 | static int gpmc_setup_irq(void) |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 729 | { |
| 730 | int i; |
| 731 | u32 regval; |
| 732 | |
| 733 | if (!gpmc_irq) |
| 734 | return -EINVAL; |
| 735 | |
| 736 | gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0); |
Russell King | 7185684 | 2013-03-13 20:44:21 +0000 | [diff] [blame] | 737 | if (gpmc_irq_start < 0) { |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 738 | pr_err("irq_alloc_descs failed\n"); |
| 739 | return gpmc_irq_start; |
| 740 | } |
| 741 | |
| 742 | gpmc_irq_chip.name = "gpmc"; |
| 743 | gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret; |
| 744 | gpmc_irq_chip.irq_enable = gpmc_irq_enable; |
| 745 | gpmc_irq_chip.irq_disable = gpmc_irq_disable; |
| 746 | gpmc_irq_chip.irq_shutdown = gpmc_irq_noop; |
| 747 | gpmc_irq_chip.irq_ack = gpmc_irq_noop; |
| 748 | gpmc_irq_chip.irq_mask = gpmc_irq_noop; |
| 749 | gpmc_irq_chip.irq_unmask = gpmc_irq_noop; |
| 750 | |
| 751 | gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE; |
| 752 | gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT; |
| 753 | |
| 754 | for (i = 0; i < GPMC_NR_IRQ; i++) { |
| 755 | gpmc_client_irq[i].irq = gpmc_irq_start + i; |
| 756 | irq_set_chip_and_handler(gpmc_client_irq[i].irq, |
| 757 | &gpmc_irq_chip, handle_simple_irq); |
| 758 | set_irq_flags(gpmc_client_irq[i].irq, |
| 759 | IRQF_VALID | IRQF_NOAUTOEN); |
| 760 | } |
| 761 | |
| 762 | /* Disable interrupts */ |
| 763 | gpmc_write_reg(GPMC_IRQENABLE, 0); |
| 764 | |
| 765 | /* clear interrupts */ |
| 766 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
| 767 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
| 768 | |
| 769 | return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL); |
| 770 | } |
| 771 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 772 | static int gpmc_free_irq(void) |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 773 | { |
| 774 | int i; |
| 775 | |
| 776 | if (gpmc_irq) |
| 777 | free_irq(gpmc_irq, NULL); |
| 778 | |
| 779 | for (i = 0; i < GPMC_NR_IRQ; i++) { |
| 780 | irq_set_handler(gpmc_client_irq[i].irq, NULL); |
| 781 | irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip); |
| 782 | irq_modify_status(gpmc_client_irq[i].irq, 0, 0); |
| 783 | } |
| 784 | |
| 785 | irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ); |
| 786 | |
| 787 | return 0; |
| 788 | } |
| 789 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 790 | static void gpmc_mem_exit(void) |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 791 | { |
| 792 | int cs; |
| 793 | |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 794 | for (cs = 0; cs < gpmc_cs_num; cs++) { |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 795 | if (!gpmc_cs_mem_enabled(cs)) |
| 796 | continue; |
| 797 | gpmc_cs_delete_mem(cs); |
| 798 | } |
| 799 | |
| 800 | } |
| 801 | |
Jon Hunter | 84b00f0 | 2013-03-06 14:36:47 -0600 | [diff] [blame] | 802 | static void gpmc_mem_init(void) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 803 | { |
Jon Hunter | 84b00f0 | 2013-03-06 14:36:47 -0600 | [diff] [blame] | 804 | int cs; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 805 | |
Jon Hunter | bf23439 | 2013-03-06 14:12:59 -0600 | [diff] [blame] | 806 | /* |
| 807 | * The first 1MB of GPMC address space is typically mapped to |
| 808 | * the internal ROM. Never allocate the first page, to |
| 809 | * facilitate bug detection; even if we didn't boot from ROM. |
Kyungmin Park | 7f24516 | 2006-12-29 16:48:51 -0800 | [diff] [blame] | 810 | */ |
Jon Hunter | bf23439 | 2013-03-06 14:12:59 -0600 | [diff] [blame] | 811 | gpmc_mem_root.start = SZ_1M; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 812 | gpmc_mem_root.end = GPMC_MEM_END; |
| 813 | |
| 814 | /* Reserve all regions that has been set up by bootloader */ |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 815 | for (cs = 0; cs < gpmc_cs_num; cs++) { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 816 | u32 base, size; |
| 817 | |
| 818 | if (!gpmc_cs_mem_enabled(cs)) |
| 819 | continue; |
| 820 | gpmc_cs_get_memconf(cs, &base, &size); |
Jon Hunter | 84b00f0 | 2013-03-06 14:36:47 -0600 | [diff] [blame] | 821 | if (gpmc_cs_insert_mem(cs, base, size)) { |
| 822 | pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n", |
| 823 | __func__, cs, base, base + size); |
| 824 | gpmc_cs_disable_mem(cs); |
Jon Hunter | 8119024 | 2012-10-17 09:41:25 -0500 | [diff] [blame] | 825 | } |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 826 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 829 | static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk) |
| 830 | { |
| 831 | u32 temp; |
| 832 | int div; |
| 833 | |
| 834 | div = gpmc_calc_divider(sync_clk); |
| 835 | temp = gpmc_ps_to_ticks(time_ps); |
| 836 | temp = (temp + div - 1) / div; |
| 837 | return gpmc_ticks_to_ps(temp * div); |
| 838 | } |
| 839 | |
| 840 | /* XXX: can the cycles be avoided ? */ |
| 841 | static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t, |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 842 | struct gpmc_device_timings *dev_t, |
| 843 | bool mux) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 844 | { |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 845 | u32 temp; |
| 846 | |
| 847 | /* adv_rd_off */ |
| 848 | temp = dev_t->t_avdp_r; |
| 849 | /* XXX: mux check required ? */ |
| 850 | if (mux) { |
| 851 | /* XXX: t_avdp not to be required for sync, only added for tusb |
| 852 | * this indirectly necessitates requirement of t_avdp_r and |
| 853 | * t_avdp_w instead of having a single t_avdp |
| 854 | */ |
| 855 | temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh); |
| 856 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); |
| 857 | } |
| 858 | gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp); |
| 859 | |
| 860 | /* oe_on */ |
| 861 | temp = dev_t->t_oeasu; /* XXX: remove this ? */ |
| 862 | if (mux) { |
| 863 | temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach); |
| 864 | temp = max_t(u32, temp, gpmc_t->adv_rd_off + |
| 865 | gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe)); |
| 866 | } |
| 867 | gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp); |
| 868 | |
| 869 | /* access */ |
| 870 | /* XXX: any scope for improvement ?, by combining oe_on |
| 871 | * and clk_activation, need to check whether |
| 872 | * access = clk_activation + round to sync clk ? |
| 873 | */ |
| 874 | temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk); |
| 875 | temp += gpmc_t->clk_activation; |
| 876 | if (dev_t->cyc_oe) |
| 877 | temp = max_t(u32, temp, gpmc_t->oe_on + |
| 878 | gpmc_ticks_to_ps(dev_t->cyc_oe)); |
| 879 | gpmc_t->access = gpmc_round_ps_to_ticks(temp); |
| 880 | |
| 881 | gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1); |
| 882 | gpmc_t->cs_rd_off = gpmc_t->oe_off; |
| 883 | |
| 884 | /* rd_cycle */ |
| 885 | temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez); |
| 886 | temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) + |
| 887 | gpmc_t->access; |
| 888 | /* XXX: barter t_ce_rdyz with t_cez_r ? */ |
| 889 | if (dev_t->t_ce_rdyz) |
| 890 | temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz); |
| 891 | gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp); |
| 892 | |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t, |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 897 | struct gpmc_device_timings *dev_t, |
| 898 | bool mux) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 899 | { |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 900 | u32 temp; |
| 901 | |
| 902 | /* adv_wr_off */ |
| 903 | temp = dev_t->t_avdp_w; |
| 904 | if (mux) { |
| 905 | temp = max_t(u32, temp, |
| 906 | gpmc_t->clk_activation + dev_t->t_avdh); |
| 907 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); |
| 908 | } |
| 909 | gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp); |
| 910 | |
| 911 | /* wr_data_mux_bus */ |
| 912 | temp = max_t(u32, dev_t->t_weasu, |
| 913 | gpmc_t->clk_activation + dev_t->t_rdyo); |
| 914 | /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?, |
| 915 | * and in that case remember to handle we_on properly |
| 916 | */ |
| 917 | if (mux) { |
| 918 | temp = max_t(u32, temp, |
| 919 | gpmc_t->adv_wr_off + dev_t->t_aavdh); |
| 920 | temp = max_t(u32, temp, gpmc_t->adv_wr_off + |
| 921 | gpmc_ticks_to_ps(dev_t->cyc_aavdh_we)); |
| 922 | } |
| 923 | gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp); |
| 924 | |
| 925 | /* we_on */ |
| 926 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) |
| 927 | gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu); |
| 928 | else |
| 929 | gpmc_t->we_on = gpmc_t->wr_data_mux_bus; |
| 930 | |
| 931 | /* wr_access */ |
| 932 | /* XXX: gpmc_capability check reqd ? , even if not, will not harm */ |
| 933 | gpmc_t->wr_access = gpmc_t->access; |
| 934 | |
| 935 | /* we_off */ |
| 936 | temp = gpmc_t->we_on + dev_t->t_wpl; |
| 937 | temp = max_t(u32, temp, |
| 938 | gpmc_t->wr_access + gpmc_ticks_to_ps(1)); |
| 939 | temp = max_t(u32, temp, |
| 940 | gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl)); |
| 941 | gpmc_t->we_off = gpmc_round_ps_to_ticks(temp); |
| 942 | |
| 943 | gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off + |
| 944 | dev_t->t_wph); |
| 945 | |
| 946 | /* wr_cycle */ |
| 947 | temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk); |
| 948 | temp += gpmc_t->wr_access; |
| 949 | /* XXX: barter t_ce_rdyz with t_cez_w ? */ |
| 950 | if (dev_t->t_ce_rdyz) |
| 951 | temp = max_t(u32, temp, |
| 952 | gpmc_t->cs_wr_off + dev_t->t_ce_rdyz); |
| 953 | gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp); |
| 954 | |
| 955 | return 0; |
| 956 | } |
| 957 | |
| 958 | static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t, |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 959 | struct gpmc_device_timings *dev_t, |
| 960 | bool mux) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 961 | { |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 962 | u32 temp; |
| 963 | |
| 964 | /* adv_rd_off */ |
| 965 | temp = dev_t->t_avdp_r; |
| 966 | if (mux) |
| 967 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); |
| 968 | gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp); |
| 969 | |
| 970 | /* oe_on */ |
| 971 | temp = dev_t->t_oeasu; |
| 972 | if (mux) |
| 973 | temp = max_t(u32, temp, |
| 974 | gpmc_t->adv_rd_off + dev_t->t_aavdh); |
| 975 | gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp); |
| 976 | |
| 977 | /* access */ |
| 978 | temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */ |
| 979 | gpmc_t->oe_on + dev_t->t_oe); |
| 980 | temp = max_t(u32, temp, |
| 981 | gpmc_t->cs_on + dev_t->t_ce); |
| 982 | temp = max_t(u32, temp, |
| 983 | gpmc_t->adv_on + dev_t->t_aa); |
| 984 | gpmc_t->access = gpmc_round_ps_to_ticks(temp); |
| 985 | |
| 986 | gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1); |
| 987 | gpmc_t->cs_rd_off = gpmc_t->oe_off; |
| 988 | |
| 989 | /* rd_cycle */ |
| 990 | temp = max_t(u32, dev_t->t_rd_cycle, |
| 991 | gpmc_t->cs_rd_off + dev_t->t_cez_r); |
| 992 | temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez); |
| 993 | gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp); |
| 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
| 998 | static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t, |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 999 | struct gpmc_device_timings *dev_t, |
| 1000 | bool mux) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1001 | { |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1002 | u32 temp; |
| 1003 | |
| 1004 | /* adv_wr_off */ |
| 1005 | temp = dev_t->t_avdp_w; |
| 1006 | if (mux) |
| 1007 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); |
| 1008 | gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp); |
| 1009 | |
| 1010 | /* wr_data_mux_bus */ |
| 1011 | temp = dev_t->t_weasu; |
| 1012 | if (mux) { |
| 1013 | temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh); |
| 1014 | temp = max_t(u32, temp, gpmc_t->adv_wr_off + |
| 1015 | gpmc_ticks_to_ps(dev_t->cyc_aavdh_we)); |
| 1016 | } |
| 1017 | gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp); |
| 1018 | |
| 1019 | /* we_on */ |
| 1020 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) |
| 1021 | gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu); |
| 1022 | else |
| 1023 | gpmc_t->we_on = gpmc_t->wr_data_mux_bus; |
| 1024 | |
| 1025 | /* we_off */ |
| 1026 | temp = gpmc_t->we_on + dev_t->t_wpl; |
| 1027 | gpmc_t->we_off = gpmc_round_ps_to_ticks(temp); |
| 1028 | |
| 1029 | gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off + |
| 1030 | dev_t->t_wph); |
| 1031 | |
| 1032 | /* wr_cycle */ |
| 1033 | temp = max_t(u32, dev_t->t_wr_cycle, |
| 1034 | gpmc_t->cs_wr_off + dev_t->t_cez_w); |
| 1035 | gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp); |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| 1040 | static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t, |
| 1041 | struct gpmc_device_timings *dev_t) |
| 1042 | { |
| 1043 | u32 temp; |
| 1044 | |
| 1045 | gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) * |
| 1046 | gpmc_get_fclk_period(); |
| 1047 | |
| 1048 | gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk( |
| 1049 | dev_t->t_bacc, |
| 1050 | gpmc_t->sync_clk); |
| 1051 | |
| 1052 | temp = max_t(u32, dev_t->t_ces, dev_t->t_avds); |
| 1053 | gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp); |
| 1054 | |
| 1055 | if (gpmc_calc_divider(gpmc_t->sync_clk) != 1) |
| 1056 | return 0; |
| 1057 | |
| 1058 | if (dev_t->ce_xdelay) |
| 1059 | gpmc_t->bool_timings.cs_extra_delay = true; |
| 1060 | if (dev_t->avd_xdelay) |
| 1061 | gpmc_t->bool_timings.adv_extra_delay = true; |
| 1062 | if (dev_t->oe_xdelay) |
| 1063 | gpmc_t->bool_timings.oe_extra_delay = true; |
| 1064 | if (dev_t->we_xdelay) |
| 1065 | gpmc_t->bool_timings.we_extra_delay = true; |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t, |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1071 | struct gpmc_device_timings *dev_t, |
| 1072 | bool sync) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1073 | { |
| 1074 | u32 temp; |
| 1075 | |
| 1076 | /* cs_on */ |
| 1077 | gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu); |
| 1078 | |
| 1079 | /* adv_on */ |
| 1080 | temp = dev_t->t_avdasu; |
| 1081 | if (dev_t->t_ce_avd) |
| 1082 | temp = max_t(u32, temp, |
| 1083 | gpmc_t->cs_on + dev_t->t_ce_avd); |
| 1084 | gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp); |
| 1085 | |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1086 | if (sync) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1087 | gpmc_calc_sync_common_timings(gpmc_t, dev_t); |
| 1088 | |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | /* TODO: remove this function once all peripherals are confirmed to |
| 1093 | * work with generic timing. Simultaneously gpmc_cs_set_timings() |
| 1094 | * has to be modified to handle timings in ps instead of ns |
| 1095 | */ |
| 1096 | static void gpmc_convert_ps_to_ns(struct gpmc_timings *t) |
| 1097 | { |
| 1098 | t->cs_on /= 1000; |
| 1099 | t->cs_rd_off /= 1000; |
| 1100 | t->cs_wr_off /= 1000; |
| 1101 | t->adv_on /= 1000; |
| 1102 | t->adv_rd_off /= 1000; |
| 1103 | t->adv_wr_off /= 1000; |
| 1104 | t->we_on /= 1000; |
| 1105 | t->we_off /= 1000; |
| 1106 | t->oe_on /= 1000; |
| 1107 | t->oe_off /= 1000; |
| 1108 | t->page_burst_access /= 1000; |
| 1109 | t->access /= 1000; |
| 1110 | t->rd_cycle /= 1000; |
| 1111 | t->wr_cycle /= 1000; |
| 1112 | t->bus_turnaround /= 1000; |
| 1113 | t->cycle2cycle_delay /= 1000; |
| 1114 | t->wait_monitoring /= 1000; |
| 1115 | t->clk_activation /= 1000; |
| 1116 | t->wr_access /= 1000; |
| 1117 | t->wr_data_mux_bus /= 1000; |
| 1118 | } |
| 1119 | |
| 1120 | int gpmc_calc_timings(struct gpmc_timings *gpmc_t, |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1121 | struct gpmc_settings *gpmc_s, |
| 1122 | struct gpmc_device_timings *dev_t) |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1123 | { |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1124 | bool mux = false, sync = false; |
| 1125 | |
| 1126 | if (gpmc_s) { |
| 1127 | mux = gpmc_s->mux_add_data ? true : false; |
| 1128 | sync = (gpmc_s->sync_read || gpmc_s->sync_write); |
| 1129 | } |
| 1130 | |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1131 | memset(gpmc_t, 0, sizeof(*gpmc_t)); |
| 1132 | |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1133 | gpmc_calc_common_timings(gpmc_t, dev_t, sync); |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1134 | |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1135 | if (gpmc_s && gpmc_s->sync_read) |
| 1136 | gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux); |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1137 | else |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1138 | gpmc_calc_async_read_timings(gpmc_t, dev_t, mux); |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1139 | |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1140 | if (gpmc_s && gpmc_s->sync_write) |
| 1141 | gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux); |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1142 | else |
Jon Hunter | c3be5b4 | 2013-02-21 13:46:22 -0600 | [diff] [blame] | 1143 | gpmc_calc_async_write_timings(gpmc_t, dev_t, mux); |
Afzal Mohammed | 246da26 | 2012-08-02 20:02:10 +0530 | [diff] [blame] | 1144 | |
| 1145 | /* TODO: remove, see function definition */ |
| 1146 | gpmc_convert_ps_to_ns(gpmc_t); |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Jon Hunter | aa8d476 | 2013-02-21 15:25:23 -0600 | [diff] [blame] | 1151 | /** |
| 1152 | * gpmc_cs_program_settings - programs non-timing related settings |
| 1153 | * @cs: GPMC chip-select to program |
| 1154 | * @p: pointer to GPMC settings structure |
| 1155 | * |
| 1156 | * Programs non-timing related settings for a GPMC chip-select, such as |
| 1157 | * bus-width, burst configuration, etc. Function should be called once |
| 1158 | * for each chip-select that is being used and must be called before |
| 1159 | * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1 |
| 1160 | * register will be initialised to zero by this function. Returns 0 on |
| 1161 | * success and appropriate negative error code on failure. |
| 1162 | */ |
| 1163 | int gpmc_cs_program_settings(int cs, struct gpmc_settings *p) |
| 1164 | { |
| 1165 | u32 config1; |
| 1166 | |
| 1167 | if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) { |
| 1168 | pr_err("%s: invalid width %d!", __func__, p->device_width); |
| 1169 | return -EINVAL; |
| 1170 | } |
| 1171 | |
| 1172 | /* Address-data multiplexing not supported for NAND devices */ |
| 1173 | if (p->device_nand && p->mux_add_data) { |
| 1174 | pr_err("%s: invalid configuration!\n", __func__); |
| 1175 | return -EINVAL; |
| 1176 | } |
| 1177 | |
| 1178 | if ((p->mux_add_data > GPMC_MUX_AD) || |
| 1179 | ((p->mux_add_data == GPMC_MUX_AAD) && |
| 1180 | !(gpmc_capability & GPMC_HAS_MUX_AAD))) { |
| 1181 | pr_err("%s: invalid multiplex configuration!\n", __func__); |
| 1182 | return -EINVAL; |
| 1183 | } |
| 1184 | |
| 1185 | /* Page/burst mode supports lengths of 4, 8 and 16 bytes */ |
| 1186 | if (p->burst_read || p->burst_write) { |
| 1187 | switch (p->burst_len) { |
| 1188 | case GPMC_BURST_4: |
| 1189 | case GPMC_BURST_8: |
| 1190 | case GPMC_BURST_16: |
| 1191 | break; |
| 1192 | default: |
| 1193 | pr_err("%s: invalid page/burst-length (%d)\n", |
| 1194 | __func__, p->burst_len); |
| 1195 | return -EINVAL; |
| 1196 | } |
| 1197 | } |
| 1198 | |
Roger Quadros | 2b54057 | 2014-09-02 16:57:06 +0300 | [diff] [blame] | 1199 | if (p->wait_pin > gpmc_nr_waitpins) { |
Jon Hunter | aa8d476 | 2013-02-21 15:25:23 -0600 | [diff] [blame] | 1200 | pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin); |
| 1201 | return -EINVAL; |
| 1202 | } |
| 1203 | |
| 1204 | config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1)); |
| 1205 | |
| 1206 | if (p->sync_read) |
| 1207 | config1 |= GPMC_CONFIG1_READTYPE_SYNC; |
| 1208 | if (p->sync_write) |
| 1209 | config1 |= GPMC_CONFIG1_WRITETYPE_SYNC; |
| 1210 | if (p->wait_on_read) |
| 1211 | config1 |= GPMC_CONFIG1_WAIT_READ_MON; |
| 1212 | if (p->wait_on_write) |
| 1213 | config1 |= GPMC_CONFIG1_WAIT_WRITE_MON; |
| 1214 | if (p->wait_on_read || p->wait_on_write) |
| 1215 | config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin); |
| 1216 | if (p->device_nand) |
| 1217 | config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND); |
| 1218 | if (p->mux_add_data) |
| 1219 | config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data); |
| 1220 | if (p->burst_read) |
| 1221 | config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP; |
| 1222 | if (p->burst_write) |
| 1223 | config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP; |
| 1224 | if (p->burst_read || p->burst_write) { |
| 1225 | config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3); |
| 1226 | config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0; |
| 1227 | } |
| 1228 | |
| 1229 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1); |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1234 | #ifdef CONFIG_OF |
Uwe Kleine-König | 3195760 | 2014-09-10 10:26:17 +0200 | [diff] [blame] | 1235 | static const struct of_device_id gpmc_dt_ids[] = { |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1236 | { .compatible = "ti,omap2420-gpmc" }, |
| 1237 | { .compatible = "ti,omap2430-gpmc" }, |
| 1238 | { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */ |
| 1239 | { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */ |
| 1240 | { .compatible = "ti,am3352-gpmc" }, /* am335x devices */ |
| 1241 | { } |
| 1242 | }; |
| 1243 | MODULE_DEVICE_TABLE(of, gpmc_dt_ids); |
| 1244 | |
Jon Hunter | 8c8a7771 | 2013-02-20 15:53:12 -0600 | [diff] [blame] | 1245 | /** |
| 1246 | * gpmc_read_settings_dt - read gpmc settings from device-tree |
| 1247 | * @np: pointer to device-tree node for a gpmc child device |
| 1248 | * @p: pointer to gpmc settings structure |
| 1249 | * |
| 1250 | * Reads the GPMC settings for a GPMC child device from device-tree and |
| 1251 | * stores them in the GPMC settings structure passed. The GPMC settings |
| 1252 | * structure is initialised to zero by this function and so any |
| 1253 | * previously stored settings will be cleared. |
| 1254 | */ |
| 1255 | void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p) |
| 1256 | { |
| 1257 | memset(p, 0, sizeof(struct gpmc_settings)); |
| 1258 | |
| 1259 | p->sync_read = of_property_read_bool(np, "gpmc,sync-read"); |
| 1260 | p->sync_write = of_property_read_bool(np, "gpmc,sync-write"); |
Jon Hunter | 8c8a7771 | 2013-02-20 15:53:12 -0600 | [diff] [blame] | 1261 | of_property_read_u32(np, "gpmc,device-width", &p->device_width); |
| 1262 | of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data); |
| 1263 | |
| 1264 | if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) { |
| 1265 | p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap"); |
| 1266 | p->burst_read = of_property_read_bool(np, "gpmc,burst-read"); |
| 1267 | p->burst_write = of_property_read_bool(np, "gpmc,burst-write"); |
| 1268 | if (!p->burst_read && !p->burst_write) |
| 1269 | pr_warn("%s: page/burst-length set but not used!\n", |
| 1270 | __func__); |
| 1271 | } |
| 1272 | |
| 1273 | if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) { |
| 1274 | p->wait_on_read = of_property_read_bool(np, |
| 1275 | "gpmc,wait-on-read"); |
| 1276 | p->wait_on_write = of_property_read_bool(np, |
| 1277 | "gpmc,wait-on-write"); |
| 1278 | if (!p->wait_on_read && !p->wait_on_write) |
Roger Quadros | 2b54057 | 2014-09-02 16:57:06 +0300 | [diff] [blame] | 1279 | pr_debug("%s: rd/wr wait monitoring not enabled!\n", |
| 1280 | __func__); |
Jon Hunter | 8c8a7771 | 2013-02-20 15:53:12 -0600 | [diff] [blame] | 1281 | } |
| 1282 | } |
| 1283 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1284 | static void __maybe_unused gpmc_read_timings_dt(struct device_node *np, |
| 1285 | struct gpmc_timings *gpmc_t) |
| 1286 | { |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1287 | struct gpmc_bool_timings *p; |
| 1288 | |
| 1289 | if (!np || !gpmc_t) |
| 1290 | return; |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1291 | |
| 1292 | memset(gpmc_t, 0, sizeof(*gpmc_t)); |
| 1293 | |
| 1294 | /* minimum clock period for syncronous mode */ |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1295 | of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1296 | |
| 1297 | /* chip select timtings */ |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1298 | of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on); |
| 1299 | of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off); |
| 1300 | of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1301 | |
| 1302 | /* ADV signal timings */ |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1303 | of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on); |
| 1304 | of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off); |
| 1305 | of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1306 | |
| 1307 | /* WE signal timings */ |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1308 | of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on); |
| 1309 | of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1310 | |
| 1311 | /* OE signal timings */ |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1312 | of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on); |
| 1313 | of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1314 | |
| 1315 | /* access and cycle timings */ |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1316 | of_property_read_u32(np, "gpmc,page-burst-access-ns", |
| 1317 | &gpmc_t->page_burst_access); |
| 1318 | of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access); |
| 1319 | of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle); |
| 1320 | of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle); |
| 1321 | of_property_read_u32(np, "gpmc,bus-turnaround-ns", |
| 1322 | &gpmc_t->bus_turnaround); |
| 1323 | of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns", |
| 1324 | &gpmc_t->cycle2cycle_delay); |
| 1325 | of_property_read_u32(np, "gpmc,wait-monitoring-ns", |
| 1326 | &gpmc_t->wait_monitoring); |
| 1327 | of_property_read_u32(np, "gpmc,clk-activation-ns", |
| 1328 | &gpmc_t->clk_activation); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1329 | |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1330 | /* only applicable to OMAP3+ */ |
| 1331 | of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access); |
| 1332 | of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns", |
| 1333 | &gpmc_t->wr_data_mux_bus); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1334 | |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1335 | /* bool timing parameters */ |
| 1336 | p = &gpmc_t->bool_timings; |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1337 | |
Jon Hunter | d36b4cd | 2013-02-21 18:51:27 -0600 | [diff] [blame] | 1338 | p->cycle2cyclediffcsen = |
| 1339 | of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen"); |
| 1340 | p->cycle2cyclesamecsen = |
| 1341 | of_property_read_bool(np, "gpmc,cycle2cycle-samecsen"); |
| 1342 | p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay"); |
| 1343 | p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay"); |
| 1344 | p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay"); |
| 1345 | p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay"); |
| 1346 | p->time_para_granularity = |
| 1347 | of_property_read_bool(np, "gpmc,time-para-granularity"); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
Pekon Gupta | 6b187b2 | 2014-01-28 11:42:40 +0530 | [diff] [blame] | 1350 | #if IS_ENABLED(CONFIG_MTD_NAND) |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1351 | |
Mark Jackson | 496c8a0 | 2013-04-19 21:08:28 +0100 | [diff] [blame] | 1352 | static const char * const nand_xfer_types[] = { |
| 1353 | [NAND_OMAP_PREFETCH_POLLED] = "prefetch-polled", |
| 1354 | [NAND_OMAP_POLLED] = "polled", |
| 1355 | [NAND_OMAP_PREFETCH_DMA] = "prefetch-dma", |
| 1356 | [NAND_OMAP_PREFETCH_IRQ] = "prefetch-irq", |
| 1357 | }; |
| 1358 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1359 | static int gpmc_probe_nand_child(struct platform_device *pdev, |
| 1360 | struct device_node *child) |
| 1361 | { |
| 1362 | u32 val; |
| 1363 | const char *s; |
| 1364 | struct gpmc_timings gpmc_t; |
| 1365 | struct omap_nand_platform_data *gpmc_nand_data; |
| 1366 | |
| 1367 | if (of_property_read_u32(child, "reg", &val) < 0) { |
| 1368 | dev_err(&pdev->dev, "%s has no 'reg' property\n", |
| 1369 | child->full_name); |
| 1370 | return -ENODEV; |
| 1371 | } |
| 1372 | |
| 1373 | gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data), |
| 1374 | GFP_KERNEL); |
| 1375 | if (!gpmc_nand_data) |
| 1376 | return -ENOMEM; |
| 1377 | |
| 1378 | gpmc_nand_data->cs = val; |
| 1379 | gpmc_nand_data->of_node = child; |
| 1380 | |
Pekon Gupta | ac65caf5 | 2013-10-24 18:20:17 +0530 | [diff] [blame] | 1381 | /* Detect availability of ELM module */ |
| 1382 | gpmc_nand_data->elm_of_node = of_parse_phandle(child, "ti,elm-id", 0); |
| 1383 | if (gpmc_nand_data->elm_of_node == NULL) |
| 1384 | gpmc_nand_data->elm_of_node = |
| 1385 | of_parse_phandle(child, "elm_id", 0); |
| 1386 | if (gpmc_nand_data->elm_of_node == NULL) |
| 1387 | pr_warn("%s: ti,elm-id property not found\n", __func__); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1388 | |
Pekon Gupta | ac65caf5 | 2013-10-24 18:20:17 +0530 | [diff] [blame] | 1389 | /* select ecc-scheme for NAND */ |
| 1390 | if (of_property_read_string(child, "ti,nand-ecc-opt", &s)) { |
| 1391 | pr_err("%s: ti,nand-ecc-opt not found\n", __func__); |
| 1392 | return -ENODEV; |
| 1393 | } |
Roger Quadros | a3e83f0 | 2014-08-25 16:15:33 -0700 | [diff] [blame] | 1394 | |
| 1395 | if (!strcmp(s, "sw")) |
| 1396 | gpmc_nand_data->ecc_opt = OMAP_ECC_HAM1_CODE_SW; |
| 1397 | else if (!strcmp(s, "ham1") || |
| 1398 | !strcmp(s, "hw") || !strcmp(s, "hw-romcode")) |
Pekon Gupta | ac65caf5 | 2013-10-24 18:20:17 +0530 | [diff] [blame] | 1399 | gpmc_nand_data->ecc_opt = |
| 1400 | OMAP_ECC_HAM1_CODE_HW; |
| 1401 | else if (!strcmp(s, "bch4")) |
| 1402 | if (gpmc_nand_data->elm_of_node) |
| 1403 | gpmc_nand_data->ecc_opt = |
| 1404 | OMAP_ECC_BCH4_CODE_HW; |
| 1405 | else |
| 1406 | gpmc_nand_data->ecc_opt = |
| 1407 | OMAP_ECC_BCH4_CODE_HW_DETECTION_SW; |
| 1408 | else if (!strcmp(s, "bch8")) |
| 1409 | if (gpmc_nand_data->elm_of_node) |
| 1410 | gpmc_nand_data->ecc_opt = |
| 1411 | OMAP_ECC_BCH8_CODE_HW; |
| 1412 | else |
| 1413 | gpmc_nand_data->ecc_opt = |
| 1414 | OMAP_ECC_BCH8_CODE_HW_DETECTION_SW; |
pekon gupta | 27c9fd6 | 2014-05-19 13:24:39 +0530 | [diff] [blame] | 1415 | else if (!strcmp(s, "bch16")) |
| 1416 | if (gpmc_nand_data->elm_of_node) |
| 1417 | gpmc_nand_data->ecc_opt = |
| 1418 | OMAP_ECC_BCH16_CODE_HW; |
| 1419 | else |
| 1420 | pr_err("%s: BCH16 requires ELM support\n", __func__); |
Pekon Gupta | ac65caf5 | 2013-10-24 18:20:17 +0530 | [diff] [blame] | 1421 | else |
| 1422 | pr_err("%s: ti,nand-ecc-opt invalid value\n", __func__); |
| 1423 | |
| 1424 | /* select data transfer mode for NAND controller */ |
Mark Jackson | 496c8a0 | 2013-04-19 21:08:28 +0100 | [diff] [blame] | 1425 | if (!of_property_read_string(child, "ti,nand-xfer-type", &s)) |
| 1426 | for (val = 0; val < ARRAY_SIZE(nand_xfer_types); val++) |
| 1427 | if (!strcasecmp(s, nand_xfer_types[val])) { |
| 1428 | gpmc_nand_data->xfer_type = val; |
| 1429 | break; |
| 1430 | } |
| 1431 | |
Ezequiel GarcÃa | fef775c | 2014-09-11 12:02:08 -0300 | [diff] [blame] | 1432 | gpmc_nand_data->flash_bbt = of_get_nand_on_flash_bbt(child); |
| 1433 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1434 | val = of_get_nand_bus_width(child); |
| 1435 | if (val == 16) |
| 1436 | gpmc_nand_data->devsize = NAND_BUSWIDTH_16; |
| 1437 | |
| 1438 | gpmc_read_timings_dt(child, &gpmc_t); |
| 1439 | gpmc_nand_init(gpmc_nand_data, &gpmc_t); |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
| 1443 | #else |
| 1444 | static int gpmc_probe_nand_child(struct platform_device *pdev, |
| 1445 | struct device_node *child) |
| 1446 | { |
| 1447 | return 0; |
| 1448 | } |
| 1449 | #endif |
| 1450 | |
Pekon Gupta | 980386d | 2014-01-28 11:42:41 +0530 | [diff] [blame] | 1451 | #if IS_ENABLED(CONFIG_MTD_ONENAND) |
Ezequiel Garcia | 75d3625 | 2013-01-25 09:23:11 -0300 | [diff] [blame] | 1452 | static int gpmc_probe_onenand_child(struct platform_device *pdev, |
| 1453 | struct device_node *child) |
| 1454 | { |
| 1455 | u32 val; |
| 1456 | struct omap_onenand_platform_data *gpmc_onenand_data; |
| 1457 | |
| 1458 | if (of_property_read_u32(child, "reg", &val) < 0) { |
| 1459 | dev_err(&pdev->dev, "%s has no 'reg' property\n", |
| 1460 | child->full_name); |
| 1461 | return -ENODEV; |
| 1462 | } |
| 1463 | |
| 1464 | gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data), |
| 1465 | GFP_KERNEL); |
| 1466 | if (!gpmc_onenand_data) |
| 1467 | return -ENOMEM; |
| 1468 | |
| 1469 | gpmc_onenand_data->cs = val; |
| 1470 | gpmc_onenand_data->of_node = child; |
| 1471 | gpmc_onenand_data->dma_channel = -1; |
| 1472 | |
| 1473 | if (!of_property_read_u32(child, "dma-channel", &val)) |
| 1474 | gpmc_onenand_data->dma_channel = val; |
| 1475 | |
| 1476 | gpmc_onenand_init(gpmc_onenand_data); |
| 1477 | |
| 1478 | return 0; |
| 1479 | } |
| 1480 | #else |
| 1481 | static int gpmc_probe_onenand_child(struct platform_device *pdev, |
| 1482 | struct device_node *child) |
| 1483 | { |
| 1484 | return 0; |
| 1485 | } |
| 1486 | #endif |
| 1487 | |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1488 | /** |
Javier Martinez Canillas | 3af91cf | 2013-03-14 16:09:21 +0100 | [diff] [blame] | 1489 | * gpmc_probe_generic_child - configures the gpmc for a child device |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1490 | * @pdev: pointer to gpmc platform device |
Javier Martinez Canillas | 3af91cf | 2013-03-14 16:09:21 +0100 | [diff] [blame] | 1491 | * @child: pointer to device-tree node for child device |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1492 | * |
Javier Martinez Canillas | 3af91cf | 2013-03-14 16:09:21 +0100 | [diff] [blame] | 1493 | * Allocates and configures a GPMC chip-select for a child device. |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1494 | * Returns 0 on success and appropriate negative error code on failure. |
| 1495 | */ |
Javier Martinez Canillas | 3af91cf | 2013-03-14 16:09:21 +0100 | [diff] [blame] | 1496 | static int gpmc_probe_generic_child(struct platform_device *pdev, |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1497 | struct device_node *child) |
| 1498 | { |
| 1499 | struct gpmc_settings gpmc_s; |
| 1500 | struct gpmc_timings gpmc_t; |
| 1501 | struct resource res; |
| 1502 | unsigned long base; |
| 1503 | int ret, cs; |
| 1504 | |
| 1505 | if (of_property_read_u32(child, "reg", &cs) < 0) { |
| 1506 | dev_err(&pdev->dev, "%s has no 'reg' property\n", |
| 1507 | child->full_name); |
| 1508 | return -ENODEV; |
| 1509 | } |
| 1510 | |
| 1511 | if (of_address_to_resource(child, 0, &res) < 0) { |
| 1512 | dev_err(&pdev->dev, "%s has malformed 'reg' property\n", |
| 1513 | child->full_name); |
| 1514 | return -ENODEV; |
| 1515 | } |
| 1516 | |
| 1517 | ret = gpmc_cs_request(cs, resource_size(&res), &base); |
| 1518 | if (ret < 0) { |
| 1519 | dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs); |
| 1520 | return ret; |
| 1521 | } |
| 1522 | |
| 1523 | /* |
Tony Lindgren | fd4446f | 2013-11-14 15:25:09 -0800 | [diff] [blame] | 1524 | * For some GPMC devices we still need to rely on the bootloader |
| 1525 | * timings because the devices can be connected via FPGA. So far |
| 1526 | * the list is smc91x on the omap2 SDP boards, and 8250 on zooms. |
| 1527 | * REVISIT: Add timing support from slls644g.pdf and from the |
| 1528 | * lan91c96 manual. |
| 1529 | */ |
| 1530 | if (of_device_is_compatible(child, "ns16550a") || |
| 1531 | of_device_is_compatible(child, "smsc,lan91c94") || |
| 1532 | of_device_is_compatible(child, "smsc,lan91c111")) { |
| 1533 | dev_warn(&pdev->dev, |
| 1534 | "%s using bootloader timings on CS%d\n", |
| 1535 | child->name, cs); |
| 1536 | goto no_timings; |
| 1537 | } |
| 1538 | |
| 1539 | /* |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1540 | * FIXME: gpmc_cs_request() will map the CS to an arbitary |
| 1541 | * location in the gpmc address space. When booting with |
| 1542 | * device-tree we want the NOR flash to be mapped to the |
| 1543 | * location specified in the device-tree blob. So remap the |
| 1544 | * CS to this location. Once DT migration is complete should |
| 1545 | * just make gpmc_cs_request() map a specific address. |
| 1546 | */ |
| 1547 | ret = gpmc_cs_remap(cs, res.start); |
| 1548 | if (ret < 0) { |
Fabio Estevam | f70bf2a | 2013-09-18 12:01:59 -0700 | [diff] [blame] | 1549 | dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n", |
| 1550 | cs, &res.start); |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1551 | goto err; |
| 1552 | } |
| 1553 | |
| 1554 | gpmc_read_settings_dt(child, &gpmc_s); |
| 1555 | |
| 1556 | ret = of_property_read_u32(child, "bank-width", &gpmc_s.device_width); |
| 1557 | if (ret < 0) |
| 1558 | goto err; |
| 1559 | |
| 1560 | ret = gpmc_cs_program_settings(cs, &gpmc_s); |
| 1561 | if (ret < 0) |
| 1562 | goto err; |
| 1563 | |
| 1564 | gpmc_read_timings_dt(child, &gpmc_t); |
| 1565 | gpmc_cs_set_timings(cs, &gpmc_t); |
| 1566 | |
Tony Lindgren | fd4446f | 2013-11-14 15:25:09 -0800 | [diff] [blame] | 1567 | no_timings: |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1568 | if (of_platform_device_create(child, NULL, &pdev->dev)) |
| 1569 | return 0; |
| 1570 | |
| 1571 | dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name); |
Javier Martinez Canillas | e8ffd6f | 2013-03-14 16:09:20 +0100 | [diff] [blame] | 1572 | ret = -ENODEV; |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1573 | |
| 1574 | err: |
| 1575 | gpmc_cs_free(cs); |
| 1576 | |
| 1577 | return ret; |
| 1578 | } |
| 1579 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1580 | static int gpmc_probe_dt(struct platform_device *pdev) |
| 1581 | { |
| 1582 | int ret; |
| 1583 | struct device_node *child; |
| 1584 | const struct of_device_id *of_id = |
| 1585 | of_match_device(gpmc_dt_ids, &pdev->dev); |
| 1586 | |
| 1587 | if (!of_id) |
| 1588 | return 0; |
| 1589 | |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 1590 | ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs", |
| 1591 | &gpmc_cs_num); |
| 1592 | if (ret < 0) { |
| 1593 | pr_err("%s: number of chip-selects not defined\n", __func__); |
| 1594 | return ret; |
| 1595 | } else if (gpmc_cs_num < 1) { |
| 1596 | pr_err("%s: all chip-selects are disabled\n", __func__); |
| 1597 | return -EINVAL; |
| 1598 | } else if (gpmc_cs_num > GPMC_CS_NUM) { |
| 1599 | pr_err("%s: number of supported chip-selects cannot be > %d\n", |
| 1600 | __func__, GPMC_CS_NUM); |
| 1601 | return -EINVAL; |
| 1602 | } |
| 1603 | |
Jon Hunter | 9f83315 | 2013-02-20 15:53:38 -0600 | [diff] [blame] | 1604 | ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins", |
| 1605 | &gpmc_nr_waitpins); |
| 1606 | if (ret < 0) { |
| 1607 | pr_err("%s: number of wait pins not found!\n", __func__); |
| 1608 | return ret; |
| 1609 | } |
| 1610 | |
Guido MartÃnez | 68e2eb5 | 2014-07-02 10:35:18 -0300 | [diff] [blame] | 1611 | for_each_available_child_of_node(pdev->dev.of_node, child) { |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1612 | |
Javier Martinez Canillas | f2b09f6 | 2013-04-17 22:34:11 +0200 | [diff] [blame] | 1613 | if (!child->name) |
| 1614 | continue; |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1615 | |
Javier Martinez Canillas | f2b09f6 | 2013-04-17 22:34:11 +0200 | [diff] [blame] | 1616 | if (of_node_cmp(child->name, "nand") == 0) |
| 1617 | ret = gpmc_probe_nand_child(pdev, child); |
| 1618 | else if (of_node_cmp(child->name, "onenand") == 0) |
| 1619 | ret = gpmc_probe_onenand_child(pdev, child); |
| 1620 | else if (of_node_cmp(child->name, "ethernet") == 0 || |
Tony Lindgren | fd4446f | 2013-11-14 15:25:09 -0800 | [diff] [blame] | 1621 | of_node_cmp(child->name, "nor") == 0 || |
| 1622 | of_node_cmp(child->name, "uart") == 0) |
Javier Martinez Canillas | f2b09f6 | 2013-04-17 22:34:11 +0200 | [diff] [blame] | 1623 | ret = gpmc_probe_generic_child(pdev, child); |
Jon Hunter | cdd6928 | 2013-02-08 16:46:13 -0600 | [diff] [blame] | 1624 | |
Javier Martinez Canillas | b327b36 | 2013-04-17 22:34:12 +0200 | [diff] [blame] | 1625 | if (WARN(ret < 0, "%s: probing gpmc child %s failed\n", |
| 1626 | __func__, child->full_name)) |
Javier Martinez Canillas | 5330dc1 | 2013-03-14 22:54:11 +0100 | [diff] [blame] | 1627 | of_node_put(child); |
Javier Martinez Canillas | 5330dc1 | 2013-03-14 22:54:11 +0100 | [diff] [blame] | 1628 | } |
| 1629 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1630 | return 0; |
| 1631 | } |
| 1632 | #else |
| 1633 | static int gpmc_probe_dt(struct platform_device *pdev) |
| 1634 | { |
| 1635 | return 0; |
| 1636 | } |
| 1637 | #endif |
| 1638 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 1639 | static int gpmc_probe(struct platform_device *pdev) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 1640 | { |
Jon Hunter | 8119024 | 2012-10-17 09:41:25 -0500 | [diff] [blame] | 1641 | int rc; |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 1642 | u32 l; |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1643 | struct resource *res; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 1644 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1645 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 1646 | if (res == NULL) |
| 1647 | return -ENOENT; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 1648 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1649 | phys_base = res->start; |
| 1650 | mem_size = resource_size(res); |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 1651 | |
Thierry Reding | 5857bd9 | 2013-01-21 11:08:55 +0100 | [diff] [blame] | 1652 | gpmc_base = devm_ioremap_resource(&pdev->dev, res); |
| 1653 | if (IS_ERR(gpmc_base)) |
| 1654 | return PTR_ERR(gpmc_base); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1655 | |
| 1656 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 1657 | if (res == NULL) |
| 1658 | dev_warn(&pdev->dev, "Failed to get resource: irq\n"); |
| 1659 | else |
| 1660 | gpmc_irq = res->start; |
| 1661 | |
| 1662 | gpmc_l3_clk = clk_get(&pdev->dev, "fck"); |
| 1663 | if (IS_ERR(gpmc_l3_clk)) { |
| 1664 | dev_err(&pdev->dev, "error: clk_get\n"); |
| 1665 | gpmc_irq = 0; |
| 1666 | return PTR_ERR(gpmc_l3_clk); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 1667 | } |
| 1668 | |
avinash philip | b3f5525 | 2013-06-12 16:30:56 +0530 | [diff] [blame] | 1669 | pm_runtime_enable(&pdev->dev); |
| 1670 | pm_runtime_get_sync(&pdev->dev); |
Olof Johansson | 1daa8c1 | 2010-01-20 22:39:29 +0000 | [diff] [blame] | 1671 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1672 | gpmc_dev = &pdev->dev; |
| 1673 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 1674 | l = gpmc_read_reg(GPMC_REVISION); |
Jon Hunter | aa8d476 | 2013-02-21 15:25:23 -0600 | [diff] [blame] | 1675 | |
| 1676 | /* |
| 1677 | * FIXME: Once device-tree migration is complete the below flags |
| 1678 | * should be populated based upon the device-tree compatible |
| 1679 | * string. For now just use the IP revision. OMAP3+ devices have |
| 1680 | * the wr_access and wr_data_mux_bus register fields. OMAP4+ |
| 1681 | * devices support the addr-addr-data multiplex protocol. |
| 1682 | * |
| 1683 | * GPMC IP revisions: |
| 1684 | * - OMAP24xx = 2.0 |
| 1685 | * - OMAP3xxx = 5.0 |
| 1686 | * - OMAP44xx/54xx/AM335x = 6.0 |
| 1687 | */ |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1688 | if (GPMC_REVISION_MAJOR(l) > 0x4) |
| 1689 | gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS; |
Jon Hunter | aa8d476 | 2013-02-21 15:25:23 -0600 | [diff] [blame] | 1690 | if (GPMC_REVISION_MAJOR(l) > 0x5) |
| 1691 | gpmc_capability |= GPMC_HAS_MUX_AAD; |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1692 | dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l), |
| 1693 | GPMC_REVISION_MINOR(l)); |
| 1694 | |
Jon Hunter | 84b00f0 | 2013-03-06 14:36:47 -0600 | [diff] [blame] | 1695 | gpmc_mem_init(); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 1696 | |
Russell King | 7185684 | 2013-03-13 20:44:21 +0000 | [diff] [blame] | 1697 | if (gpmc_setup_irq() < 0) |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1698 | dev_warn(gpmc_dev, "gpmc_setup_irq failed\n"); |
| 1699 | |
Jon Hunter | 31d9adc | 2013-02-18 07:57:39 -0600 | [diff] [blame] | 1700 | /* Now the GPMC is initialised, unreserve the chip-selects */ |
| 1701 | gpmc_cs_map = 0; |
| 1702 | |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 1703 | if (!pdev->dev.of_node) { |
| 1704 | gpmc_cs_num = GPMC_CS_NUM; |
Jon Hunter | 9f83315 | 2013-02-20 15:53:38 -0600 | [diff] [blame] | 1705 | gpmc_nr_waitpins = GPMC_NR_WAITPINS; |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 1706 | } |
Jon Hunter | 9f83315 | 2013-02-20 15:53:38 -0600 | [diff] [blame] | 1707 | |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1708 | rc = gpmc_probe_dt(pdev); |
| 1709 | if (rc < 0) { |
avinash philip | b3f5525 | 2013-06-12 16:30:56 +0530 | [diff] [blame] | 1710 | pm_runtime_put_sync(&pdev->dev); |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1711 | clk_put(gpmc_l3_clk); |
| 1712 | dev_err(gpmc_dev, "failed to probe DT parameters\n"); |
| 1713 | return rc; |
| 1714 | } |
| 1715 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1716 | return 0; |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 1717 | } |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1718 | |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 1719 | static int gpmc_remove(struct platform_device *pdev) |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1720 | { |
| 1721 | gpmc_free_irq(); |
| 1722 | gpmc_mem_exit(); |
avinash philip | b3f5525 | 2013-06-12 16:30:56 +0530 | [diff] [blame] | 1723 | pm_runtime_put_sync(&pdev->dev); |
| 1724 | pm_runtime_disable(&pdev->dev); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1725 | gpmc_dev = NULL; |
| 1726 | return 0; |
| 1727 | } |
| 1728 | |
avinash philip | b536dd4 | 2013-06-18 00:16:38 +0530 | [diff] [blame] | 1729 | #ifdef CONFIG_PM_SLEEP |
| 1730 | static int gpmc_suspend(struct device *dev) |
| 1731 | { |
| 1732 | omap3_gpmc_save_context(); |
| 1733 | pm_runtime_put_sync(dev); |
| 1734 | return 0; |
| 1735 | } |
| 1736 | |
| 1737 | static int gpmc_resume(struct device *dev) |
| 1738 | { |
| 1739 | pm_runtime_get_sync(dev); |
| 1740 | omap3_gpmc_restore_context(); |
| 1741 | return 0; |
| 1742 | } |
| 1743 | #endif |
| 1744 | |
| 1745 | static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume); |
| 1746 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1747 | static struct platform_driver gpmc_driver = { |
| 1748 | .probe = gpmc_probe, |
Greg Kroah-Hartman | 351a102 | 2012-12-21 14:02:24 -0800 | [diff] [blame] | 1749 | .remove = gpmc_remove, |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1750 | .driver = { |
| 1751 | .name = DEVICE_NAME, |
| 1752 | .owner = THIS_MODULE, |
Daniel Mack | bc6b1e7 | 2012-12-14 11:36:44 +0100 | [diff] [blame] | 1753 | .of_match_table = of_match_ptr(gpmc_dt_ids), |
avinash philip | b536dd4 | 2013-06-18 00:16:38 +0530 | [diff] [blame] | 1754 | .pm = &gpmc_pm_ops, |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1755 | }, |
| 1756 | }; |
| 1757 | |
| 1758 | static __init int gpmc_init(void) |
| 1759 | { |
| 1760 | return platform_driver_register(&gpmc_driver); |
| 1761 | } |
| 1762 | |
| 1763 | static __exit void gpmc_exit(void) |
| 1764 | { |
| 1765 | platform_driver_unregister(&gpmc_driver); |
| 1766 | |
| 1767 | } |
| 1768 | |
Tony Lindgren | b76c8b19 | 2013-01-11 11:24:18 -0800 | [diff] [blame] | 1769 | omap_postcore_initcall(gpmc_init); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 1770 | module_exit(gpmc_exit); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 1771 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 1772 | static int __init omap_gpmc_init(void) |
| 1773 | { |
| 1774 | struct omap_hwmod *oh; |
| 1775 | struct platform_device *pdev; |
| 1776 | char *oh_name = "gpmc"; |
| 1777 | |
Daniel Mack | 2f98ca8 | 2012-12-14 11:36:40 +0100 | [diff] [blame] | 1778 | /* |
| 1779 | * if the board boots up with a populated DT, do not |
| 1780 | * manually add the device from this initcall |
| 1781 | */ |
| 1782 | if (of_have_populated_dt()) |
| 1783 | return -ENODEV; |
| 1784 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 1785 | oh = omap_hwmod_lookup(oh_name); |
| 1786 | if (!oh) { |
| 1787 | pr_err("Could not look up %s\n", oh_name); |
| 1788 | return -ENODEV; |
| 1789 | } |
| 1790 | |
Paul Walmsley | c1d1cd5 | 2013-01-26 00:48:53 -0700 | [diff] [blame] | 1791 | pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0); |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 1792 | WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name); |
| 1793 | |
Thomas Meyer | 1261674 | 2013-06-01 11:44:44 +0200 | [diff] [blame] | 1794 | return PTR_RET(pdev); |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 1795 | } |
Tony Lindgren | b76c8b19 | 2013-01-11 11:24:18 -0800 | [diff] [blame] | 1796 | omap_postcore_initcall(omap_gpmc_init); |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 1797 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 1798 | static irqreturn_t gpmc_handle_irq(int irq, void *dev) |
| 1799 | { |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 1800 | int i; |
| 1801 | u32 regval; |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 1802 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 1803 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
| 1804 | |
| 1805 | if (!regval) |
| 1806 | return IRQ_NONE; |
| 1807 | |
| 1808 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 1809 | if (regval & gpmc_client_irq[i].bitmask) |
| 1810 | generic_handle_irq(gpmc_client_irq[i].irq); |
| 1811 | |
| 1812 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 1813 | |
| 1814 | return IRQ_HANDLED; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 1815 | } |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1816 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1817 | static struct omap3_gpmc_regs gpmc_context; |
| 1818 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1819 | void omap3_gpmc_save_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1820 | { |
| 1821 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1822 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1823 | gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG); |
| 1824 | gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE); |
| 1825 | gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL); |
| 1826 | gpmc_context.config = gpmc_read_reg(GPMC_CONFIG); |
| 1827 | gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); |
| 1828 | gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2); |
| 1829 | gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL); |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 1830 | for (i = 0; i < gpmc_cs_num; i++) { |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1831 | gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i); |
| 1832 | if (gpmc_context.cs_context[i].is_valid) { |
| 1833 | gpmc_context.cs_context[i].config1 = |
| 1834 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG1); |
| 1835 | gpmc_context.cs_context[i].config2 = |
| 1836 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG2); |
| 1837 | gpmc_context.cs_context[i].config3 = |
| 1838 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG3); |
| 1839 | gpmc_context.cs_context[i].config4 = |
| 1840 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG4); |
| 1841 | gpmc_context.cs_context[i].config5 = |
| 1842 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG5); |
| 1843 | gpmc_context.cs_context[i].config6 = |
| 1844 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG6); |
| 1845 | gpmc_context.cs_context[i].config7 = |
| 1846 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG7); |
| 1847 | } |
| 1848 | } |
| 1849 | } |
| 1850 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1851 | void omap3_gpmc_restore_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1852 | { |
| 1853 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 1854 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1855 | gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig); |
| 1856 | gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable); |
| 1857 | gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl); |
| 1858 | gpmc_write_reg(GPMC_CONFIG, gpmc_context.config); |
| 1859 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1); |
| 1860 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2); |
| 1861 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control); |
Gupta Pekon | f34f371 | 2013-05-31 17:31:30 +0530 | [diff] [blame] | 1862 | for (i = 0; i < gpmc_cs_num; i++) { |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 1863 | if (gpmc_context.cs_context[i].is_valid) { |
| 1864 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG1, |
| 1865 | gpmc_context.cs_context[i].config1); |
| 1866 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG2, |
| 1867 | gpmc_context.cs_context[i].config2); |
| 1868 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG3, |
| 1869 | gpmc_context.cs_context[i].config3); |
| 1870 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG4, |
| 1871 | gpmc_context.cs_context[i].config4); |
| 1872 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG5, |
| 1873 | gpmc_context.cs_context[i].config5); |
| 1874 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG6, |
| 1875 | gpmc_context.cs_context[i].config6); |
| 1876 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG7, |
| 1877 | gpmc_context.cs_context[i].config7); |
| 1878 | } |
| 1879 | } |
| 1880 | } |