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> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 28 | |
Afzal Mohammed | bc3668e | 2012-09-29 12:26:13 +0530 | [diff] [blame] | 29 | #include <linux/platform_data/mtd-nand-omap2.h> |
| 30 | |
Kyungmin Park | 7f24516 | 2006-12-29 16:48:51 -0800 | [diff] [blame] | 31 | #include <asm/mach-types.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 32 | |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 33 | #include <plat/cpu.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 34 | #include <plat/sdrc.h> |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 35 | #include <plat/omap_device.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 36 | |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 37 | #include "soc.h" |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 38 | #include "common.h" |
Afzal Mohammed | 3ef5d00 | 2012-10-05 10:37:27 +0530 | [diff] [blame^] | 39 | #include "gpmc.h" |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 40 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 41 | #define DEVICE_NAME "omap-gpmc" |
| 42 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 43 | /* GPMC register offsets */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 44 | #define GPMC_REVISION 0x00 |
| 45 | #define GPMC_SYSCONFIG 0x10 |
| 46 | #define GPMC_SYSSTATUS 0x14 |
| 47 | #define GPMC_IRQSTATUS 0x18 |
| 48 | #define GPMC_IRQENABLE 0x1c |
| 49 | #define GPMC_TIMEOUT_CONTROL 0x40 |
| 50 | #define GPMC_ERR_ADDRESS 0x44 |
| 51 | #define GPMC_ERR_TYPE 0x48 |
| 52 | #define GPMC_CONFIG 0x50 |
| 53 | #define GPMC_STATUS 0x54 |
| 54 | #define GPMC_PREFETCH_CONFIG1 0x1e0 |
| 55 | #define GPMC_PREFETCH_CONFIG2 0x1e4 |
Thara Gopinath | 15e02a3 | 2008-04-28 16:55:01 +0530 | [diff] [blame] | 56 | #define GPMC_PREFETCH_CONTROL 0x1ec |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 57 | #define GPMC_PREFETCH_STATUS 0x1f0 |
| 58 | #define GPMC_ECC_CONFIG 0x1f4 |
| 59 | #define GPMC_ECC_CONTROL 0x1f8 |
| 60 | #define GPMC_ECC_SIZE_CONFIG 0x1fc |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 61 | #define GPMC_ECC1_RESULT 0x200 |
Ivan Djelic | 8d602cf | 2012-04-26 14:17:49 +0200 | [diff] [blame] | 62 | #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */ |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 63 | #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */ |
| 64 | #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */ |
| 65 | #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 66 | |
Yegor Yefremov | 2c65e74 | 2012-05-09 08:32:49 -0700 | [diff] [blame] | 67 | /* GPMC ECC control settings */ |
| 68 | #define GPMC_ECC_CTRL_ECCCLEAR 0x100 |
| 69 | #define GPMC_ECC_CTRL_ECCDISABLE 0x000 |
| 70 | #define GPMC_ECC_CTRL_ECCREG1 0x001 |
| 71 | #define GPMC_ECC_CTRL_ECCREG2 0x002 |
| 72 | #define GPMC_ECC_CTRL_ECCREG3 0x003 |
| 73 | #define GPMC_ECC_CTRL_ECCREG4 0x004 |
| 74 | #define GPMC_ECC_CTRL_ECCREG5 0x005 |
| 75 | #define GPMC_ECC_CTRL_ECCREG6 0x006 |
| 76 | #define GPMC_ECC_CTRL_ECCREG7 0x007 |
| 77 | #define GPMC_ECC_CTRL_ECCREG8 0x008 |
| 78 | #define GPMC_ECC_CTRL_ECCREG9 0x009 |
| 79 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 80 | #define GPMC_CS0_OFFSET 0x60 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 81 | #define GPMC_CS_SIZE 0x30 |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 82 | #define GPMC_BCH_SIZE 0x10 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 83 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 84 | #define GPMC_MEM_START 0x00000000 |
| 85 | #define GPMC_MEM_END 0x3FFFFFFF |
| 86 | #define BOOT_ROM_SPACE 0x100000 /* 1MB */ |
| 87 | |
| 88 | #define GPMC_CHUNK_SHIFT 24 /* 16 MB */ |
| 89 | #define GPMC_SECTION_SHIFT 28 /* 128 MB */ |
| 90 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 91 | #define CS_NUM_SHIFT 24 |
| 92 | #define ENABLE_PREFETCH (0x1 << 7) |
| 93 | #define DMA_MPU_MODE 2 |
| 94 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 95 | #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf) |
| 96 | #define GPMC_REVISION_MINOR(l) (l & 0xf) |
| 97 | |
| 98 | #define GPMC_HAS_WR_ACCESS 0x1 |
| 99 | #define GPMC_HAS_WR_DATA_MUX_BUS 0x2 |
| 100 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 101 | /* XXX: Only NAND irq has been considered,currently these are the only ones used |
| 102 | */ |
| 103 | #define GPMC_NR_IRQ 2 |
| 104 | |
| 105 | struct gpmc_client_irq { |
| 106 | unsigned irq; |
| 107 | u32 bitmask; |
| 108 | }; |
| 109 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 110 | /* Structure to save gpmc cs context */ |
| 111 | struct gpmc_cs_config { |
| 112 | u32 config1; |
| 113 | u32 config2; |
| 114 | u32 config3; |
| 115 | u32 config4; |
| 116 | u32 config5; |
| 117 | u32 config6; |
| 118 | u32 config7; |
| 119 | int is_valid; |
| 120 | }; |
| 121 | |
| 122 | /* |
| 123 | * Structure to save/restore gpmc context |
| 124 | * to support core off on OMAP3 |
| 125 | */ |
| 126 | struct omap3_gpmc_regs { |
| 127 | u32 sysconfig; |
| 128 | u32 irqenable; |
| 129 | u32 timeout_ctrl; |
| 130 | u32 config; |
| 131 | u32 prefetch_config1; |
| 132 | u32 prefetch_config2; |
| 133 | u32 prefetch_control; |
| 134 | struct gpmc_cs_config cs_context[GPMC_CS_NUM]; |
| 135 | }; |
| 136 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 137 | static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ]; |
| 138 | static struct irq_chip gpmc_irq_chip; |
| 139 | static unsigned gpmc_irq_start; |
| 140 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 141 | static struct resource gpmc_mem_root; |
| 142 | static struct resource gpmc_cs_mem[GPMC_CS_NUM]; |
Thomas Gleixner | 87b247c | 2007-05-10 22:33:04 -0700 | [diff] [blame] | 143 | static DEFINE_SPINLOCK(gpmc_mem_lock); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 144 | static unsigned int gpmc_cs_map; /* flag for cs which are initialized */ |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 145 | static struct device *gpmc_dev; |
| 146 | static int gpmc_irq; |
| 147 | static resource_size_t phys_base, mem_size; |
| 148 | static unsigned gpmc_capability; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 149 | static void __iomem *gpmc_base; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 150 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 151 | static struct clk *gpmc_l3_clk; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 152 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 153 | static irqreturn_t gpmc_handle_irq(int irq, void *dev); |
| 154 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 155 | static void gpmc_write_reg(int idx, u32 val) |
| 156 | { |
| 157 | __raw_writel(val, gpmc_base + idx); |
| 158 | } |
| 159 | |
| 160 | static u32 gpmc_read_reg(int idx) |
| 161 | { |
| 162 | return __raw_readl(gpmc_base + idx); |
| 163 | } |
| 164 | |
| 165 | void gpmc_cs_write_reg(int cs, int idx, u32 val) |
| 166 | { |
| 167 | void __iomem *reg_addr; |
| 168 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 169 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 170 | __raw_writel(val, reg_addr); |
| 171 | } |
| 172 | |
| 173 | u32 gpmc_cs_read_reg(int cs, int idx) |
| 174 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 175 | void __iomem *reg_addr; |
| 176 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 177 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 178 | return __raw_readl(reg_addr); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 181 | /* TODO: Add support for gpmc_fck to clock framework and use it */ |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 182 | unsigned long gpmc_get_fclk_period(void) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 183 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 184 | unsigned long rate = clk_get_rate(gpmc_l3_clk); |
| 185 | |
| 186 | if (rate == 0) { |
| 187 | printk(KERN_WARNING "gpmc_l3_clk not enabled\n"); |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | rate /= 1000; |
| 192 | rate = 1000000000 / rate; /* In picoseconds */ |
| 193 | |
| 194 | return rate; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | unsigned int gpmc_ns_to_ticks(unsigned int time_ns) |
| 198 | { |
| 199 | unsigned long tick_ps; |
| 200 | |
| 201 | /* Calculate in picosecs to yield more exact results */ |
| 202 | tick_ps = gpmc_get_fclk_period(); |
| 203 | |
| 204 | return (time_ns * 1000 + tick_ps - 1) / tick_ps; |
| 205 | } |
| 206 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 207 | unsigned int gpmc_ps_to_ticks(unsigned int time_ps) |
| 208 | { |
| 209 | unsigned long tick_ps; |
| 210 | |
| 211 | /* Calculate in picosecs to yield more exact results */ |
| 212 | tick_ps = gpmc_get_fclk_period(); |
| 213 | |
| 214 | return (time_ps + tick_ps - 1) / tick_ps; |
| 215 | } |
| 216 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 217 | unsigned int gpmc_ticks_to_ns(unsigned int ticks) |
| 218 | { |
| 219 | return ticks * gpmc_get_fclk_period() / 1000; |
| 220 | } |
| 221 | |
Kai Svahn | 2330059 | 2007-01-26 12:29:40 -0800 | [diff] [blame] | 222 | unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns) |
| 223 | { |
| 224 | unsigned long ticks = gpmc_ns_to_ticks(time_ns); |
| 225 | |
| 226 | return ticks * gpmc_get_fclk_period() / 1000; |
| 227 | } |
| 228 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 229 | #ifdef DEBUG |
| 230 | 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] | 231 | int time, const char *name) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 232 | #else |
| 233 | static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, |
| 234 | int time) |
| 235 | #endif |
| 236 | { |
| 237 | u32 l; |
| 238 | int ticks, mask, nr_bits; |
| 239 | |
| 240 | if (time == 0) |
| 241 | ticks = 0; |
| 242 | else |
| 243 | ticks = gpmc_ns_to_ticks(time); |
| 244 | nr_bits = end_bit - st_bit + 1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 245 | if (ticks >= 1 << nr_bits) { |
| 246 | #ifdef DEBUG |
| 247 | printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n", |
| 248 | cs, name, time, ticks, 1 << nr_bits); |
| 249 | #endif |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 250 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 251 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 252 | |
| 253 | mask = (1 << nr_bits) - 1; |
| 254 | l = gpmc_cs_read_reg(cs, reg); |
| 255 | #ifdef DEBUG |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 256 | printk(KERN_INFO |
| 257 | "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] | 258 | cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000, |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 259 | (l >> st_bit) & mask, time); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 260 | #endif |
| 261 | l &= ~(mask << st_bit); |
| 262 | l |= ticks << st_bit; |
| 263 | gpmc_cs_write_reg(cs, reg, l); |
| 264 | |
| 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | #ifdef DEBUG |
| 269 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 270 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), \ |
| 271 | t->field, #field) < 0) \ |
| 272 | return -1 |
| 273 | #else |
| 274 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 275 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \ |
| 276 | return -1 |
| 277 | #endif |
| 278 | |
Afzal Mohammed | 1b47ca1 | 2012-08-19 18:29:45 +0530 | [diff] [blame] | 279 | int gpmc_calc_divider(unsigned int sync_clk) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 280 | { |
| 281 | int div; |
| 282 | u32 l; |
| 283 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 284 | l = sync_clk + (gpmc_get_fclk_period() - 1); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 285 | div = l / gpmc_get_fclk_period(); |
| 286 | if (div > 4) |
| 287 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 288 | if (div <= 0) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 289 | div = 1; |
| 290 | |
| 291 | return div; |
| 292 | } |
| 293 | |
| 294 | int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t) |
| 295 | { |
| 296 | int div; |
| 297 | u32 l; |
| 298 | |
Afzal Mohammed | 1b47ca1 | 2012-08-19 18:29:45 +0530 | [diff] [blame] | 299 | div = gpmc_calc_divider(t->sync_clk); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 300 | if (div < 0) |
Paul Walmsley | a032d33 | 2012-08-03 09:21:10 -0600 | [diff] [blame] | 301 | return div; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 302 | |
| 303 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on); |
| 304 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off); |
| 305 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off); |
| 306 | |
| 307 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on); |
| 308 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off); |
| 309 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off); |
| 310 | |
| 311 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on); |
| 312 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off); |
| 313 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on); |
| 314 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off); |
| 315 | |
| 316 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle); |
| 317 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle); |
| 318 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access); |
| 319 | |
| 320 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access); |
| 321 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 322 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 323 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 324 | if (gpmc_capability & GPMC_HAS_WR_ACCESS) |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 325 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access); |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 326 | |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 327 | /* caller is expected to have initialized CONFIG1 to cover |
| 328 | * at least sync vs async |
| 329 | */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 330 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 331 | if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) { |
| 332 | #ifdef DEBUG |
| 333 | printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n", |
| 334 | cs, (div * gpmc_get_fclk_period()) / 1000, div); |
| 335 | #endif |
| 336 | l &= ~0x03; |
| 337 | l |= (div - 1); |
| 338 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l); |
| 339 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 340 | |
| 341 | return 0; |
| 342 | } |
| 343 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 344 | static void gpmc_cs_enable_mem(int cs, u32 base, u32 size) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 345 | { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 346 | u32 l; |
| 347 | u32 mask; |
| 348 | |
| 349 | mask = (1 << GPMC_SECTION_SHIFT) - size; |
| 350 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 351 | l &= ~0x3f; |
| 352 | l = (base >> GPMC_CHUNK_SHIFT) & 0x3f; |
| 353 | l &= ~(0x0f << 8); |
| 354 | l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8; |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 355 | l |= GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 356 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 357 | } |
| 358 | |
| 359 | static void gpmc_cs_disable_mem(int cs) |
| 360 | { |
| 361 | u32 l; |
| 362 | |
| 363 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 364 | l &= ~GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 365 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 366 | } |
| 367 | |
| 368 | static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size) |
| 369 | { |
| 370 | u32 l; |
| 371 | u32 mask; |
| 372 | |
| 373 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 374 | *base = (l & 0x3f) << GPMC_CHUNK_SHIFT; |
| 375 | mask = (l >> 8) & 0x0f; |
| 376 | *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT); |
| 377 | } |
| 378 | |
| 379 | static int gpmc_cs_mem_enabled(int cs) |
| 380 | { |
| 381 | u32 l; |
| 382 | |
| 383 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 384 | return l & GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 385 | } |
| 386 | |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 387 | int gpmc_cs_set_reserved(int cs, int reserved) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 388 | { |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 389 | if (cs > GPMC_CS_NUM) |
| 390 | return -ENODEV; |
| 391 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 392 | gpmc_cs_map &= ~(1 << cs); |
| 393 | gpmc_cs_map |= (reserved ? 1 : 0) << cs; |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 394 | |
| 395 | return 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 396 | } |
| 397 | |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 398 | int gpmc_cs_reserved(int cs) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 399 | { |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 400 | if (cs > GPMC_CS_NUM) |
| 401 | return -ENODEV; |
| 402 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 403 | return gpmc_cs_map & (1 << cs); |
| 404 | } |
| 405 | |
| 406 | static unsigned long gpmc_mem_align(unsigned long size) |
| 407 | { |
| 408 | int order; |
| 409 | |
| 410 | size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1); |
| 411 | order = GPMC_CHUNK_SHIFT - 1; |
| 412 | do { |
| 413 | size >>= 1; |
| 414 | order++; |
| 415 | } while (size); |
| 416 | size = 1 << order; |
| 417 | return size; |
| 418 | } |
| 419 | |
| 420 | static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size) |
| 421 | { |
| 422 | struct resource *res = &gpmc_cs_mem[cs]; |
| 423 | int r; |
| 424 | |
| 425 | size = gpmc_mem_align(size); |
| 426 | spin_lock(&gpmc_mem_lock); |
| 427 | res->start = base; |
| 428 | res->end = base + size - 1; |
| 429 | r = request_resource(&gpmc_mem_root, res); |
| 430 | spin_unlock(&gpmc_mem_lock); |
| 431 | |
| 432 | return r; |
| 433 | } |
| 434 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 435 | static int gpmc_cs_delete_mem(int cs) |
| 436 | { |
| 437 | struct resource *res = &gpmc_cs_mem[cs]; |
| 438 | int r; |
| 439 | |
| 440 | spin_lock(&gpmc_mem_lock); |
| 441 | r = release_resource(&gpmc_cs_mem[cs]); |
| 442 | res->start = 0; |
| 443 | res->end = 0; |
| 444 | spin_unlock(&gpmc_mem_lock); |
| 445 | |
| 446 | return r; |
| 447 | } |
| 448 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 449 | int gpmc_cs_request(int cs, unsigned long size, unsigned long *base) |
| 450 | { |
| 451 | struct resource *res = &gpmc_cs_mem[cs]; |
| 452 | int r = -1; |
| 453 | |
| 454 | if (cs > GPMC_CS_NUM) |
| 455 | return -ENODEV; |
| 456 | |
| 457 | size = gpmc_mem_align(size); |
| 458 | if (size > (1 << GPMC_SECTION_SHIFT)) |
| 459 | return -ENOMEM; |
| 460 | |
| 461 | spin_lock(&gpmc_mem_lock); |
| 462 | if (gpmc_cs_reserved(cs)) { |
| 463 | r = -EBUSY; |
| 464 | goto out; |
| 465 | } |
| 466 | if (gpmc_cs_mem_enabled(cs)) |
| 467 | r = adjust_resource(res, res->start & ~(size - 1), size); |
| 468 | if (r < 0) |
| 469 | r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0, |
| 470 | size, NULL, NULL); |
| 471 | if (r < 0) |
| 472 | goto out; |
| 473 | |
Tobias Klauser | 6d13524 | 2009-11-10 18:55:19 -0800 | [diff] [blame] | 474 | gpmc_cs_enable_mem(cs, res->start, resource_size(res)); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 475 | *base = res->start; |
| 476 | gpmc_cs_set_reserved(cs, 1); |
| 477 | out: |
| 478 | spin_unlock(&gpmc_mem_lock); |
| 479 | return r; |
| 480 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 481 | EXPORT_SYMBOL(gpmc_cs_request); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 482 | |
| 483 | void gpmc_cs_free(int cs) |
| 484 | { |
| 485 | spin_lock(&gpmc_mem_lock); |
Roel Kluin | e7fdc60 | 2009-11-17 14:39:06 -0800 | [diff] [blame] | 486 | if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 487 | printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs); |
| 488 | BUG(); |
| 489 | spin_unlock(&gpmc_mem_lock); |
| 490 | return; |
| 491 | } |
| 492 | gpmc_cs_disable_mem(cs); |
| 493 | release_resource(&gpmc_cs_mem[cs]); |
| 494 | gpmc_cs_set_reserved(cs, 0); |
| 495 | spin_unlock(&gpmc_mem_lock); |
| 496 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 497 | EXPORT_SYMBOL(gpmc_cs_free); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 498 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 499 | /** |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 500 | * gpmc_cs_configure - write request to configure gpmc |
| 501 | * @cs: chip select number |
| 502 | * @cmd: command type |
| 503 | * @wval: value to write |
| 504 | * @return status of the operation |
| 505 | */ |
| 506 | int gpmc_cs_configure(int cs, int cmd, int wval) |
| 507 | { |
| 508 | int err = 0; |
| 509 | u32 regval = 0; |
| 510 | |
| 511 | switch (cmd) { |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 512 | case GPMC_ENABLE_IRQ: |
| 513 | gpmc_write_reg(GPMC_IRQENABLE, wval); |
| 514 | break; |
| 515 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 516 | case GPMC_SET_IRQ_STATUS: |
| 517 | gpmc_write_reg(GPMC_IRQSTATUS, wval); |
| 518 | break; |
| 519 | |
| 520 | case GPMC_CONFIG_WP: |
| 521 | regval = gpmc_read_reg(GPMC_CONFIG); |
| 522 | if (wval) |
| 523 | regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */ |
| 524 | else |
| 525 | regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */ |
| 526 | gpmc_write_reg(GPMC_CONFIG, regval); |
| 527 | break; |
| 528 | |
| 529 | case GPMC_CONFIG_RDY_BSY: |
| 530 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
| 531 | if (wval) |
| 532 | regval |= WR_RD_PIN_MONITORING; |
| 533 | else |
| 534 | regval &= ~WR_RD_PIN_MONITORING; |
| 535 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 536 | break; |
| 537 | |
| 538 | case GPMC_CONFIG_DEV_SIZE: |
| 539 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
Yegor Yefremov | 8ef5d84 | 2012-01-23 08:32:23 +0100 | [diff] [blame] | 540 | |
| 541 | /* clear 2 target bits */ |
| 542 | regval &= ~GPMC_CONFIG1_DEVICESIZE(3); |
| 543 | |
| 544 | /* set the proper value */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 545 | regval |= GPMC_CONFIG1_DEVICESIZE(wval); |
Yegor Yefremov | 8ef5d84 | 2012-01-23 08:32:23 +0100 | [diff] [blame] | 546 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 547 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 548 | break; |
| 549 | |
| 550 | case GPMC_CONFIG_DEV_TYPE: |
| 551 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
| 552 | regval |= GPMC_CONFIG1_DEVICETYPE(wval); |
| 553 | if (wval == GPMC_DEVICETYPE_NOR) |
| 554 | regval |= GPMC_CONFIG1_MUXADDDATA; |
| 555 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 556 | break; |
| 557 | |
| 558 | default: |
| 559 | printk(KERN_ERR "gpmc_configure_cs: Not supported\n"); |
| 560 | err = -EINVAL; |
| 561 | } |
| 562 | |
| 563 | return err; |
| 564 | } |
| 565 | EXPORT_SYMBOL(gpmc_cs_configure); |
| 566 | |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 567 | void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs) |
| 568 | { |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 569 | int i; |
| 570 | |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 571 | reg->gpmc_status = gpmc_base + GPMC_STATUS; |
| 572 | reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET + |
| 573 | GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs; |
| 574 | reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET + |
| 575 | GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs; |
| 576 | reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET + |
| 577 | GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs; |
| 578 | reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1; |
| 579 | reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2; |
| 580 | reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL; |
| 581 | reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS; |
| 582 | reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG; |
| 583 | reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL; |
| 584 | reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG; |
| 585 | reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT; |
Afzal Mohammed | 2fdf0c9 | 2012-10-04 15:49:04 +0530 | [diff] [blame] | 586 | |
| 587 | for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) { |
| 588 | reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 + |
| 589 | GPMC_BCH_SIZE * i; |
| 590 | reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 + |
| 591 | GPMC_BCH_SIZE * i; |
| 592 | reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 + |
| 593 | GPMC_BCH_SIZE * i; |
| 594 | reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 + |
| 595 | GPMC_BCH_SIZE * i; |
| 596 | } |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 597 | } |
| 598 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 599 | int gpmc_get_client_irq(unsigned irq_config) |
| 600 | { |
| 601 | int i; |
| 602 | |
| 603 | if (hweight32(irq_config) > 1) |
| 604 | return 0; |
| 605 | |
| 606 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 607 | if (gpmc_client_irq[i].bitmask & irq_config) |
| 608 | return gpmc_client_irq[i].irq; |
| 609 | |
| 610 | return 0; |
| 611 | } |
| 612 | |
| 613 | static int gpmc_irq_endis(unsigned irq, bool endis) |
| 614 | { |
| 615 | int i; |
| 616 | u32 regval; |
| 617 | |
| 618 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 619 | if (irq == gpmc_client_irq[i].irq) { |
| 620 | regval = gpmc_read_reg(GPMC_IRQENABLE); |
| 621 | if (endis) |
| 622 | regval |= gpmc_client_irq[i].bitmask; |
| 623 | else |
| 624 | regval &= ~gpmc_client_irq[i].bitmask; |
| 625 | gpmc_write_reg(GPMC_IRQENABLE, regval); |
| 626 | break; |
| 627 | } |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static void gpmc_irq_disable(struct irq_data *p) |
| 633 | { |
| 634 | gpmc_irq_endis(p->irq, false); |
| 635 | } |
| 636 | |
| 637 | static void gpmc_irq_enable(struct irq_data *p) |
| 638 | { |
| 639 | gpmc_irq_endis(p->irq, true); |
| 640 | } |
| 641 | |
| 642 | static void gpmc_irq_noop(struct irq_data *data) { } |
| 643 | |
| 644 | static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; } |
| 645 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 646 | static int gpmc_setup_irq(void) |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 647 | { |
| 648 | int i; |
| 649 | u32 regval; |
| 650 | |
| 651 | if (!gpmc_irq) |
| 652 | return -EINVAL; |
| 653 | |
| 654 | gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0); |
| 655 | if (IS_ERR_VALUE(gpmc_irq_start)) { |
| 656 | pr_err("irq_alloc_descs failed\n"); |
| 657 | return gpmc_irq_start; |
| 658 | } |
| 659 | |
| 660 | gpmc_irq_chip.name = "gpmc"; |
| 661 | gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret; |
| 662 | gpmc_irq_chip.irq_enable = gpmc_irq_enable; |
| 663 | gpmc_irq_chip.irq_disable = gpmc_irq_disable; |
| 664 | gpmc_irq_chip.irq_shutdown = gpmc_irq_noop; |
| 665 | gpmc_irq_chip.irq_ack = gpmc_irq_noop; |
| 666 | gpmc_irq_chip.irq_mask = gpmc_irq_noop; |
| 667 | gpmc_irq_chip.irq_unmask = gpmc_irq_noop; |
| 668 | |
| 669 | gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE; |
| 670 | gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT; |
| 671 | |
| 672 | for (i = 0; i < GPMC_NR_IRQ; i++) { |
| 673 | gpmc_client_irq[i].irq = gpmc_irq_start + i; |
| 674 | irq_set_chip_and_handler(gpmc_client_irq[i].irq, |
| 675 | &gpmc_irq_chip, handle_simple_irq); |
| 676 | set_irq_flags(gpmc_client_irq[i].irq, |
| 677 | IRQF_VALID | IRQF_NOAUTOEN); |
| 678 | } |
| 679 | |
| 680 | /* Disable interrupts */ |
| 681 | gpmc_write_reg(GPMC_IRQENABLE, 0); |
| 682 | |
| 683 | /* clear interrupts */ |
| 684 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
| 685 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
| 686 | |
| 687 | return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL); |
| 688 | } |
| 689 | |
Afzal Mohammed | 61687c6 | 2012-10-04 14:01:57 +0530 | [diff] [blame] | 690 | static __devexit int gpmc_free_irq(void) |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 691 | { |
| 692 | int i; |
| 693 | |
| 694 | if (gpmc_irq) |
| 695 | free_irq(gpmc_irq, NULL); |
| 696 | |
| 697 | for (i = 0; i < GPMC_NR_IRQ; i++) { |
| 698 | irq_set_handler(gpmc_client_irq[i].irq, NULL); |
| 699 | irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip); |
| 700 | irq_modify_status(gpmc_client_irq[i].irq, 0, 0); |
| 701 | } |
| 702 | |
| 703 | irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ); |
| 704 | |
| 705 | return 0; |
| 706 | } |
| 707 | |
| 708 | static void __devexit gpmc_mem_exit(void) |
| 709 | { |
| 710 | int cs; |
| 711 | |
| 712 | for (cs = 0; cs < GPMC_CS_NUM; cs++) { |
| 713 | if (!gpmc_cs_mem_enabled(cs)) |
| 714 | continue; |
| 715 | gpmc_cs_delete_mem(cs); |
| 716 | } |
| 717 | |
| 718 | } |
| 719 | |
| 720 | static void __devinit gpmc_mem_init(void) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 721 | { |
| 722 | int cs; |
| 723 | unsigned long boot_rom_space = 0; |
| 724 | |
Kyungmin Park | 7f24516 | 2006-12-29 16:48:51 -0800 | [diff] [blame] | 725 | /* never allocate the first page, to facilitate bug detection; |
| 726 | * even if we didn't boot from ROM. |
| 727 | */ |
| 728 | boot_rom_space = BOOT_ROM_SPACE; |
| 729 | /* In apollon the CS0 is mapped as 0x0000 0000 */ |
| 730 | if (machine_is_omap_apollon()) |
| 731 | boot_rom_space = 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 732 | gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space; |
| 733 | gpmc_mem_root.end = GPMC_MEM_END; |
| 734 | |
| 735 | /* Reserve all regions that has been set up by bootloader */ |
| 736 | for (cs = 0; cs < GPMC_CS_NUM; cs++) { |
| 737 | u32 base, size; |
| 738 | |
| 739 | if (!gpmc_cs_mem_enabled(cs)) |
| 740 | continue; |
| 741 | gpmc_cs_get_memconf(cs, &base, &size); |
| 742 | if (gpmc_cs_insert_mem(cs, base, size) < 0) |
| 743 | BUG(); |
| 744 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 745 | } |
| 746 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 747 | static __devinit int gpmc_probe(struct platform_device *pdev) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 748 | { |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 749 | u32 l; |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 750 | struct resource *res; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 751 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 752 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 753 | if (res == NULL) |
| 754 | return -ENOENT; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 755 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 756 | phys_base = res->start; |
| 757 | mem_size = resource_size(res); |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 758 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 759 | gpmc_base = devm_request_and_ioremap(&pdev->dev, res); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 760 | if (!gpmc_base) { |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 761 | dev_err(&pdev->dev, "error: request memory / ioremap\n"); |
| 762 | return -EADDRNOTAVAIL; |
| 763 | } |
| 764 | |
| 765 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 766 | if (res == NULL) |
| 767 | dev_warn(&pdev->dev, "Failed to get resource: irq\n"); |
| 768 | else |
| 769 | gpmc_irq = res->start; |
| 770 | |
| 771 | gpmc_l3_clk = clk_get(&pdev->dev, "fck"); |
| 772 | if (IS_ERR(gpmc_l3_clk)) { |
| 773 | dev_err(&pdev->dev, "error: clk_get\n"); |
| 774 | gpmc_irq = 0; |
| 775 | return PTR_ERR(gpmc_l3_clk); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 776 | } |
| 777 | |
Rajendra Nayak | 4d7cb45 | 2012-09-22 02:24:16 -0600 | [diff] [blame] | 778 | clk_prepare_enable(gpmc_l3_clk); |
Olof Johansson | 1daa8c1 | 2010-01-20 22:39:29 +0000 | [diff] [blame] | 779 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 780 | gpmc_dev = &pdev->dev; |
| 781 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 782 | l = gpmc_read_reg(GPMC_REVISION); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 783 | if (GPMC_REVISION_MAJOR(l) > 0x4) |
| 784 | gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS; |
| 785 | dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l), |
| 786 | GPMC_REVISION_MINOR(l)); |
| 787 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 788 | gpmc_mem_init(); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 789 | |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 790 | if (IS_ERR_VALUE(gpmc_setup_irq())) |
| 791 | dev_warn(gpmc_dev, "gpmc_setup_irq failed\n"); |
| 792 | |
| 793 | return 0; |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 794 | } |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 795 | |
Afzal Mohammed | 61687c6 | 2012-10-04 14:01:57 +0530 | [diff] [blame] | 796 | static __devexit int gpmc_remove(struct platform_device *pdev) |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 797 | { |
| 798 | gpmc_free_irq(); |
| 799 | gpmc_mem_exit(); |
| 800 | gpmc_dev = NULL; |
| 801 | return 0; |
| 802 | } |
| 803 | |
| 804 | static struct platform_driver gpmc_driver = { |
| 805 | .probe = gpmc_probe, |
| 806 | .remove = __devexit_p(gpmc_remove), |
| 807 | .driver = { |
| 808 | .name = DEVICE_NAME, |
| 809 | .owner = THIS_MODULE, |
| 810 | }, |
| 811 | }; |
| 812 | |
| 813 | static __init int gpmc_init(void) |
| 814 | { |
| 815 | return platform_driver_register(&gpmc_driver); |
| 816 | } |
| 817 | |
| 818 | static __exit void gpmc_exit(void) |
| 819 | { |
| 820 | platform_driver_unregister(&gpmc_driver); |
| 821 | |
| 822 | } |
| 823 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 824 | postcore_initcall(gpmc_init); |
Afzal Mohammed | da49687 | 2012-09-23 17:28:25 -0600 | [diff] [blame] | 825 | module_exit(gpmc_exit); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 826 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame] | 827 | static int __init omap_gpmc_init(void) |
| 828 | { |
| 829 | struct omap_hwmod *oh; |
| 830 | struct platform_device *pdev; |
| 831 | char *oh_name = "gpmc"; |
| 832 | |
| 833 | oh = omap_hwmod_lookup(oh_name); |
| 834 | if (!oh) { |
| 835 | pr_err("Could not look up %s\n", oh_name); |
| 836 | return -ENODEV; |
| 837 | } |
| 838 | |
| 839 | pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0, NULL, 0, 0); |
| 840 | WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name); |
| 841 | |
| 842 | return IS_ERR(pdev) ? PTR_ERR(pdev) : 0; |
| 843 | } |
| 844 | postcore_initcall(omap_gpmc_init); |
| 845 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 846 | static irqreturn_t gpmc_handle_irq(int irq, void *dev) |
| 847 | { |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 848 | int i; |
| 849 | u32 regval; |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 850 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 851 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
| 852 | |
| 853 | if (!regval) |
| 854 | return IRQ_NONE; |
| 855 | |
| 856 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 857 | if (regval & gpmc_client_irq[i].bitmask) |
| 858 | generic_handle_irq(gpmc_client_irq[i].irq); |
| 859 | |
| 860 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 861 | |
| 862 | return IRQ_HANDLED; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 863 | } |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 864 | |
| 865 | #ifdef CONFIG_ARCH_OMAP3 |
| 866 | static struct omap3_gpmc_regs gpmc_context; |
| 867 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 868 | void omap3_gpmc_save_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 869 | { |
| 870 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 871 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 872 | gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG); |
| 873 | gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE); |
| 874 | gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL); |
| 875 | gpmc_context.config = gpmc_read_reg(GPMC_CONFIG); |
| 876 | gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); |
| 877 | gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2); |
| 878 | gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL); |
| 879 | for (i = 0; i < GPMC_CS_NUM; i++) { |
| 880 | gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i); |
| 881 | if (gpmc_context.cs_context[i].is_valid) { |
| 882 | gpmc_context.cs_context[i].config1 = |
| 883 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG1); |
| 884 | gpmc_context.cs_context[i].config2 = |
| 885 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG2); |
| 886 | gpmc_context.cs_context[i].config3 = |
| 887 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG3); |
| 888 | gpmc_context.cs_context[i].config4 = |
| 889 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG4); |
| 890 | gpmc_context.cs_context[i].config5 = |
| 891 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG5); |
| 892 | gpmc_context.cs_context[i].config6 = |
| 893 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG6); |
| 894 | gpmc_context.cs_context[i].config7 = |
| 895 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG7); |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 900 | void omap3_gpmc_restore_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 901 | { |
| 902 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 903 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 904 | gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig); |
| 905 | gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable); |
| 906 | gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl); |
| 907 | gpmc_write_reg(GPMC_CONFIG, gpmc_context.config); |
| 908 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1); |
| 909 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2); |
| 910 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control); |
| 911 | for (i = 0; i < GPMC_CS_NUM; i++) { |
| 912 | if (gpmc_context.cs_context[i].is_valid) { |
| 913 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG1, |
| 914 | gpmc_context.cs_context[i].config1); |
| 915 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG2, |
| 916 | gpmc_context.cs_context[i].config2); |
| 917 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG3, |
| 918 | gpmc_context.cs_context[i].config3); |
| 919 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG4, |
| 920 | gpmc_context.cs_context[i].config4); |
| 921 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG5, |
| 922 | gpmc_context.cs_context[i].config5); |
| 923 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG6, |
| 924 | gpmc_context.cs_context[i].config6); |
| 925 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG7, |
| 926 | gpmc_context.cs_context[i].config7); |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | #endif /* CONFIG_ARCH_OMAP3 */ |