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> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 27 | |
Kyungmin Park | 7f24516 | 2006-12-29 16:48:51 -0800 | [diff] [blame] | 28 | #include <asm/mach-types.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 29 | #include <plat/gpmc.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 30 | |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 31 | #include <plat/sdrc.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 32 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 33 | /* GPMC register offsets */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 34 | #define GPMC_REVISION 0x00 |
| 35 | #define GPMC_SYSCONFIG 0x10 |
| 36 | #define GPMC_SYSSTATUS 0x14 |
| 37 | #define GPMC_IRQSTATUS 0x18 |
| 38 | #define GPMC_IRQENABLE 0x1c |
| 39 | #define GPMC_TIMEOUT_CONTROL 0x40 |
| 40 | #define GPMC_ERR_ADDRESS 0x44 |
| 41 | #define GPMC_ERR_TYPE 0x48 |
| 42 | #define GPMC_CONFIG 0x50 |
| 43 | #define GPMC_STATUS 0x54 |
| 44 | #define GPMC_PREFETCH_CONFIG1 0x1e0 |
| 45 | #define GPMC_PREFETCH_CONFIG2 0x1e4 |
Thara Gopinath | 15e02a3 | 2008-04-28 16:55:01 +0530 | [diff] [blame] | 46 | #define GPMC_PREFETCH_CONTROL 0x1ec |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 47 | #define GPMC_PREFETCH_STATUS 0x1f0 |
| 48 | #define GPMC_ECC_CONFIG 0x1f4 |
| 49 | #define GPMC_ECC_CONTROL 0x1f8 |
| 50 | #define GPMC_ECC_SIZE_CONFIG 0x1fc |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 51 | #define GPMC_ECC1_RESULT 0x200 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 52 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 53 | #define GPMC_CS0_OFFSET 0x60 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 54 | #define GPMC_CS_SIZE 0x30 |
| 55 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 56 | #define GPMC_MEM_START 0x00000000 |
| 57 | #define GPMC_MEM_END 0x3FFFFFFF |
| 58 | #define BOOT_ROM_SPACE 0x100000 /* 1MB */ |
| 59 | |
| 60 | #define GPMC_CHUNK_SHIFT 24 /* 16 MB */ |
| 61 | #define GPMC_SECTION_SHIFT 28 /* 128 MB */ |
| 62 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 63 | #define CS_NUM_SHIFT 24 |
| 64 | #define ENABLE_PREFETCH (0x1 << 7) |
| 65 | #define DMA_MPU_MODE 2 |
| 66 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 67 | /* Structure to save gpmc cs context */ |
| 68 | struct gpmc_cs_config { |
| 69 | u32 config1; |
| 70 | u32 config2; |
| 71 | u32 config3; |
| 72 | u32 config4; |
| 73 | u32 config5; |
| 74 | u32 config6; |
| 75 | u32 config7; |
| 76 | int is_valid; |
| 77 | }; |
| 78 | |
| 79 | /* |
| 80 | * Structure to save/restore gpmc context |
| 81 | * to support core off on OMAP3 |
| 82 | */ |
| 83 | struct omap3_gpmc_regs { |
| 84 | u32 sysconfig; |
| 85 | u32 irqenable; |
| 86 | u32 timeout_ctrl; |
| 87 | u32 config; |
| 88 | u32 prefetch_config1; |
| 89 | u32 prefetch_config2; |
| 90 | u32 prefetch_control; |
| 91 | struct gpmc_cs_config cs_context[GPMC_CS_NUM]; |
| 92 | }; |
| 93 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 94 | static struct resource gpmc_mem_root; |
| 95 | static struct resource gpmc_cs_mem[GPMC_CS_NUM]; |
Thomas Gleixner | 87b247c | 2007-05-10 22:33:04 -0700 | [diff] [blame] | 96 | static DEFINE_SPINLOCK(gpmc_mem_lock); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 97 | static unsigned int gpmc_cs_map; /* flag for cs which are initialized */ |
| 98 | static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */ |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 99 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 100 | static void __iomem *gpmc_base; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 101 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 102 | static struct clk *gpmc_l3_clk; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 103 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 104 | static irqreturn_t gpmc_handle_irq(int irq, void *dev); |
| 105 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 106 | static void gpmc_write_reg(int idx, u32 val) |
| 107 | { |
| 108 | __raw_writel(val, gpmc_base + idx); |
| 109 | } |
| 110 | |
| 111 | static u32 gpmc_read_reg(int idx) |
| 112 | { |
| 113 | return __raw_readl(gpmc_base + idx); |
| 114 | } |
| 115 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 116 | static void gpmc_cs_write_byte(int cs, int idx, u8 val) |
| 117 | { |
| 118 | void __iomem *reg_addr; |
| 119 | |
| 120 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
| 121 | __raw_writeb(val, reg_addr); |
| 122 | } |
| 123 | |
| 124 | static u8 gpmc_cs_read_byte(int cs, int idx) |
| 125 | { |
| 126 | void __iomem *reg_addr; |
| 127 | |
| 128 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
| 129 | return __raw_readb(reg_addr); |
| 130 | } |
| 131 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 132 | void gpmc_cs_write_reg(int cs, int idx, u32 val) |
| 133 | { |
| 134 | void __iomem *reg_addr; |
| 135 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 136 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 137 | __raw_writel(val, reg_addr); |
| 138 | } |
| 139 | |
| 140 | u32 gpmc_cs_read_reg(int cs, int idx) |
| 141 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 142 | void __iomem *reg_addr; |
| 143 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 144 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 145 | return __raw_readl(reg_addr); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 148 | /* TODO: Add support for gpmc_fck to clock framework and use it */ |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 149 | unsigned long gpmc_get_fclk_period(void) |
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 | unsigned long rate = clk_get_rate(gpmc_l3_clk); |
| 152 | |
| 153 | if (rate == 0) { |
| 154 | printk(KERN_WARNING "gpmc_l3_clk not enabled\n"); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | rate /= 1000; |
| 159 | rate = 1000000000 / rate; /* In picoseconds */ |
| 160 | |
| 161 | return rate; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | unsigned int gpmc_ns_to_ticks(unsigned int time_ns) |
| 165 | { |
| 166 | unsigned long tick_ps; |
| 167 | |
| 168 | /* Calculate in picosecs to yield more exact results */ |
| 169 | tick_ps = gpmc_get_fclk_period(); |
| 170 | |
| 171 | return (time_ns * 1000 + tick_ps - 1) / tick_ps; |
| 172 | } |
| 173 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 174 | unsigned int gpmc_ps_to_ticks(unsigned int time_ps) |
| 175 | { |
| 176 | unsigned long tick_ps; |
| 177 | |
| 178 | /* Calculate in picosecs to yield more exact results */ |
| 179 | tick_ps = gpmc_get_fclk_period(); |
| 180 | |
| 181 | return (time_ps + tick_ps - 1) / tick_ps; |
| 182 | } |
| 183 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 184 | unsigned int gpmc_ticks_to_ns(unsigned int ticks) |
| 185 | { |
| 186 | return ticks * gpmc_get_fclk_period() / 1000; |
| 187 | } |
| 188 | |
Kai Svahn | 2330059 | 2007-01-26 12:29:40 -0800 | [diff] [blame] | 189 | unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns) |
| 190 | { |
| 191 | unsigned long ticks = gpmc_ns_to_ticks(time_ns); |
| 192 | |
| 193 | return ticks * gpmc_get_fclk_period() / 1000; |
| 194 | } |
| 195 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 196 | #ifdef DEBUG |
| 197 | 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] | 198 | int time, const char *name) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 199 | #else |
| 200 | static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, |
| 201 | int time) |
| 202 | #endif |
| 203 | { |
| 204 | u32 l; |
| 205 | int ticks, mask, nr_bits; |
| 206 | |
| 207 | if (time == 0) |
| 208 | ticks = 0; |
| 209 | else |
| 210 | ticks = gpmc_ns_to_ticks(time); |
| 211 | nr_bits = end_bit - st_bit + 1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 212 | if (ticks >= 1 << nr_bits) { |
| 213 | #ifdef DEBUG |
| 214 | printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n", |
| 215 | cs, name, time, ticks, 1 << nr_bits); |
| 216 | #endif |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 217 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 218 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 219 | |
| 220 | mask = (1 << nr_bits) - 1; |
| 221 | l = gpmc_cs_read_reg(cs, reg); |
| 222 | #ifdef DEBUG |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 223 | printk(KERN_INFO |
| 224 | "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] | 225 | cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000, |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 226 | (l >> st_bit) & mask, time); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 227 | #endif |
| 228 | l &= ~(mask << st_bit); |
| 229 | l |= ticks << st_bit; |
| 230 | gpmc_cs_write_reg(cs, reg, l); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | #ifdef DEBUG |
| 236 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 237 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), \ |
| 238 | t->field, #field) < 0) \ |
| 239 | return -1 |
| 240 | #else |
| 241 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 242 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \ |
| 243 | return -1 |
| 244 | #endif |
| 245 | |
| 246 | int gpmc_cs_calc_divider(int cs, unsigned int sync_clk) |
| 247 | { |
| 248 | int div; |
| 249 | u32 l; |
| 250 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 251 | l = sync_clk + (gpmc_get_fclk_period() - 1); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 252 | div = l / gpmc_get_fclk_period(); |
| 253 | if (div > 4) |
| 254 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 255 | if (div <= 0) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 256 | div = 1; |
| 257 | |
| 258 | return div; |
| 259 | } |
| 260 | |
| 261 | int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t) |
| 262 | { |
| 263 | int div; |
| 264 | u32 l; |
| 265 | |
| 266 | div = gpmc_cs_calc_divider(cs, t->sync_clk); |
| 267 | if (div < 0) |
| 268 | return -1; |
| 269 | |
| 270 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on); |
| 271 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off); |
| 272 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off); |
| 273 | |
| 274 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on); |
| 275 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off); |
| 276 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off); |
| 277 | |
| 278 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on); |
| 279 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off); |
| 280 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on); |
| 281 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off); |
| 282 | |
| 283 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle); |
| 284 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle); |
| 285 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access); |
| 286 | |
| 287 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access); |
| 288 | |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 289 | if (cpu_is_omap34xx()) { |
| 290 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus); |
| 291 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access); |
| 292 | } |
| 293 | |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 294 | /* caller is expected to have initialized CONFIG1 to cover |
| 295 | * at least sync vs async |
| 296 | */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 297 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 298 | if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) { |
| 299 | #ifdef DEBUG |
| 300 | printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n", |
| 301 | cs, (div * gpmc_get_fclk_period()) / 1000, div); |
| 302 | #endif |
| 303 | l &= ~0x03; |
| 304 | l |= (div - 1); |
| 305 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l); |
| 306 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 311 | static void gpmc_cs_enable_mem(int cs, u32 base, u32 size) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 312 | { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 313 | u32 l; |
| 314 | u32 mask; |
| 315 | |
| 316 | mask = (1 << GPMC_SECTION_SHIFT) - size; |
| 317 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 318 | l &= ~0x3f; |
| 319 | l = (base >> GPMC_CHUNK_SHIFT) & 0x3f; |
| 320 | l &= ~(0x0f << 8); |
| 321 | l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8; |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 322 | l |= GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 323 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 324 | } |
| 325 | |
| 326 | static void gpmc_cs_disable_mem(int cs) |
| 327 | { |
| 328 | u32 l; |
| 329 | |
| 330 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 331 | l &= ~GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 332 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 333 | } |
| 334 | |
| 335 | static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size) |
| 336 | { |
| 337 | u32 l; |
| 338 | u32 mask; |
| 339 | |
| 340 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 341 | *base = (l & 0x3f) << GPMC_CHUNK_SHIFT; |
| 342 | mask = (l >> 8) & 0x0f; |
| 343 | *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT); |
| 344 | } |
| 345 | |
| 346 | static int gpmc_cs_mem_enabled(int cs) |
| 347 | { |
| 348 | u32 l; |
| 349 | |
| 350 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 351 | return l & GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 352 | } |
| 353 | |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 354 | int gpmc_cs_set_reserved(int cs, int reserved) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 355 | { |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 356 | if (cs > GPMC_CS_NUM) |
| 357 | return -ENODEV; |
| 358 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 359 | gpmc_cs_map &= ~(1 << cs); |
| 360 | gpmc_cs_map |= (reserved ? 1 : 0) << cs; |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 361 | |
| 362 | return 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 363 | } |
| 364 | |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 365 | int gpmc_cs_reserved(int cs) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 366 | { |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 367 | if (cs > GPMC_CS_NUM) |
| 368 | return -ENODEV; |
| 369 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 370 | return gpmc_cs_map & (1 << cs); |
| 371 | } |
| 372 | |
| 373 | static unsigned long gpmc_mem_align(unsigned long size) |
| 374 | { |
| 375 | int order; |
| 376 | |
| 377 | size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1); |
| 378 | order = GPMC_CHUNK_SHIFT - 1; |
| 379 | do { |
| 380 | size >>= 1; |
| 381 | order++; |
| 382 | } while (size); |
| 383 | size = 1 << order; |
| 384 | return size; |
| 385 | } |
| 386 | |
| 387 | static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size) |
| 388 | { |
| 389 | struct resource *res = &gpmc_cs_mem[cs]; |
| 390 | int r; |
| 391 | |
| 392 | size = gpmc_mem_align(size); |
| 393 | spin_lock(&gpmc_mem_lock); |
| 394 | res->start = base; |
| 395 | res->end = base + size - 1; |
| 396 | r = request_resource(&gpmc_mem_root, res); |
| 397 | spin_unlock(&gpmc_mem_lock); |
| 398 | |
| 399 | return r; |
| 400 | } |
| 401 | |
| 402 | int gpmc_cs_request(int cs, unsigned long size, unsigned long *base) |
| 403 | { |
| 404 | struct resource *res = &gpmc_cs_mem[cs]; |
| 405 | int r = -1; |
| 406 | |
| 407 | if (cs > GPMC_CS_NUM) |
| 408 | return -ENODEV; |
| 409 | |
| 410 | size = gpmc_mem_align(size); |
| 411 | if (size > (1 << GPMC_SECTION_SHIFT)) |
| 412 | return -ENOMEM; |
| 413 | |
| 414 | spin_lock(&gpmc_mem_lock); |
| 415 | if (gpmc_cs_reserved(cs)) { |
| 416 | r = -EBUSY; |
| 417 | goto out; |
| 418 | } |
| 419 | if (gpmc_cs_mem_enabled(cs)) |
| 420 | r = adjust_resource(res, res->start & ~(size - 1), size); |
| 421 | if (r < 0) |
| 422 | r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0, |
| 423 | size, NULL, NULL); |
| 424 | if (r < 0) |
| 425 | goto out; |
| 426 | |
Tobias Klauser | 6d13524 | 2009-11-10 18:55:19 -0800 | [diff] [blame] | 427 | gpmc_cs_enable_mem(cs, res->start, resource_size(res)); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 428 | *base = res->start; |
| 429 | gpmc_cs_set_reserved(cs, 1); |
| 430 | out: |
| 431 | spin_unlock(&gpmc_mem_lock); |
| 432 | return r; |
| 433 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 434 | EXPORT_SYMBOL(gpmc_cs_request); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 435 | |
| 436 | void gpmc_cs_free(int cs) |
| 437 | { |
| 438 | spin_lock(&gpmc_mem_lock); |
Roel Kluin | e7fdc60 | 2009-11-17 14:39:06 -0800 | [diff] [blame] | 439 | if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 440 | printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs); |
| 441 | BUG(); |
| 442 | spin_unlock(&gpmc_mem_lock); |
| 443 | return; |
| 444 | } |
| 445 | gpmc_cs_disable_mem(cs); |
| 446 | release_resource(&gpmc_cs_mem[cs]); |
| 447 | gpmc_cs_set_reserved(cs, 0); |
| 448 | spin_unlock(&gpmc_mem_lock); |
| 449 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 450 | EXPORT_SYMBOL(gpmc_cs_free); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 451 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 452 | /** |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 453 | * gpmc_read_status - read access request to get the different gpmc status |
| 454 | * @cmd: command type |
| 455 | * @return status |
| 456 | */ |
| 457 | int gpmc_read_status(int cmd) |
| 458 | { |
| 459 | int status = -EINVAL; |
| 460 | u32 regval = 0; |
| 461 | |
| 462 | switch (cmd) { |
| 463 | case GPMC_GET_IRQ_STATUS: |
| 464 | status = gpmc_read_reg(GPMC_IRQSTATUS); |
| 465 | break; |
| 466 | |
| 467 | case GPMC_PREFETCH_FIFO_CNT: |
| 468 | regval = gpmc_read_reg(GPMC_PREFETCH_STATUS); |
| 469 | status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval); |
| 470 | break; |
| 471 | |
| 472 | case GPMC_PREFETCH_COUNT: |
| 473 | regval = gpmc_read_reg(GPMC_PREFETCH_STATUS); |
| 474 | status = GPMC_PREFETCH_STATUS_COUNT(regval); |
| 475 | break; |
| 476 | |
| 477 | case GPMC_STATUS_BUFFER: |
| 478 | regval = gpmc_read_reg(GPMC_STATUS); |
| 479 | /* 1 : buffer is available to write */ |
| 480 | status = regval & GPMC_STATUS_BUFF_EMPTY; |
| 481 | break; |
| 482 | |
| 483 | default: |
| 484 | printk(KERN_ERR "gpmc_read_status: Not supported\n"); |
| 485 | } |
| 486 | return status; |
| 487 | } |
| 488 | EXPORT_SYMBOL(gpmc_read_status); |
| 489 | |
| 490 | /** |
| 491 | * gpmc_cs_configure - write request to configure gpmc |
| 492 | * @cs: chip select number |
| 493 | * @cmd: command type |
| 494 | * @wval: value to write |
| 495 | * @return status of the operation |
| 496 | */ |
| 497 | int gpmc_cs_configure(int cs, int cmd, int wval) |
| 498 | { |
| 499 | int err = 0; |
| 500 | u32 regval = 0; |
| 501 | |
| 502 | switch (cmd) { |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 503 | case GPMC_ENABLE_IRQ: |
| 504 | gpmc_write_reg(GPMC_IRQENABLE, wval); |
| 505 | break; |
| 506 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 507 | case GPMC_SET_IRQ_STATUS: |
| 508 | gpmc_write_reg(GPMC_IRQSTATUS, wval); |
| 509 | break; |
| 510 | |
| 511 | case GPMC_CONFIG_WP: |
| 512 | regval = gpmc_read_reg(GPMC_CONFIG); |
| 513 | if (wval) |
| 514 | regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */ |
| 515 | else |
| 516 | regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */ |
| 517 | gpmc_write_reg(GPMC_CONFIG, regval); |
| 518 | break; |
| 519 | |
| 520 | case GPMC_CONFIG_RDY_BSY: |
| 521 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
| 522 | if (wval) |
| 523 | regval |= WR_RD_PIN_MONITORING; |
| 524 | else |
| 525 | regval &= ~WR_RD_PIN_MONITORING; |
| 526 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 527 | break; |
| 528 | |
| 529 | case GPMC_CONFIG_DEV_SIZE: |
| 530 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
Yegor Yefremov | 8ef5d84 | 2012-01-23 08:32:23 +0100 | [diff] [blame] | 531 | |
| 532 | /* clear 2 target bits */ |
| 533 | regval &= ~GPMC_CONFIG1_DEVICESIZE(3); |
| 534 | |
| 535 | /* set the proper value */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 536 | regval |= GPMC_CONFIG1_DEVICESIZE(wval); |
Yegor Yefremov | 8ef5d84 | 2012-01-23 08:32:23 +0100 | [diff] [blame] | 537 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 538 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 539 | break; |
| 540 | |
| 541 | case GPMC_CONFIG_DEV_TYPE: |
| 542 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
| 543 | regval |= GPMC_CONFIG1_DEVICETYPE(wval); |
| 544 | if (wval == GPMC_DEVICETYPE_NOR) |
| 545 | regval |= GPMC_CONFIG1_MUXADDDATA; |
| 546 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 547 | break; |
| 548 | |
| 549 | default: |
| 550 | printk(KERN_ERR "gpmc_configure_cs: Not supported\n"); |
| 551 | err = -EINVAL; |
| 552 | } |
| 553 | |
| 554 | return err; |
| 555 | } |
| 556 | EXPORT_SYMBOL(gpmc_cs_configure); |
| 557 | |
| 558 | /** |
| 559 | * gpmc_nand_read - nand specific read access request |
| 560 | * @cs: chip select number |
| 561 | * @cmd: command type |
| 562 | */ |
| 563 | int gpmc_nand_read(int cs, int cmd) |
| 564 | { |
| 565 | int rval = -EINVAL; |
| 566 | |
| 567 | switch (cmd) { |
| 568 | case GPMC_NAND_DATA: |
| 569 | rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA); |
| 570 | break; |
| 571 | |
| 572 | default: |
| 573 | printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n"); |
| 574 | } |
| 575 | return rval; |
| 576 | } |
| 577 | EXPORT_SYMBOL(gpmc_nand_read); |
| 578 | |
| 579 | /** |
| 580 | * gpmc_nand_write - nand specific write request |
| 581 | * @cs: chip select number |
| 582 | * @cmd: command type |
| 583 | * @wval: value to write |
| 584 | */ |
| 585 | int gpmc_nand_write(int cs, int cmd, int wval) |
| 586 | { |
| 587 | int err = 0; |
| 588 | |
| 589 | switch (cmd) { |
| 590 | case GPMC_NAND_COMMAND: |
| 591 | gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval); |
| 592 | break; |
| 593 | |
| 594 | case GPMC_NAND_ADDRESS: |
| 595 | gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval); |
| 596 | break; |
| 597 | |
| 598 | case GPMC_NAND_DATA: |
| 599 | gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval); |
| 600 | |
| 601 | default: |
| 602 | printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n"); |
| 603 | err = -EINVAL; |
| 604 | } |
| 605 | return err; |
| 606 | } |
| 607 | EXPORT_SYMBOL(gpmc_nand_write); |
| 608 | |
| 609 | |
| 610 | |
| 611 | /** |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 612 | * gpmc_prefetch_enable - configures and starts prefetch transfer |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 613 | * @cs: cs (chip select) number |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 614 | * @fifo_th: fifo threshold to be used for read/ write |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 615 | * @dma_mode: dma mode enable (1) or disable (0) |
| 616 | * @u32_count: number of bytes to be transferred |
| 617 | * @is_write: prefetch read(0) or write post(1) mode |
| 618 | */ |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 619 | int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode, |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 620 | unsigned int u32_count, int is_write) |
| 621 | { |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 622 | |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 623 | if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) { |
| 624 | pr_err("gpmc: fifo threshold is not supported\n"); |
| 625 | return -1; |
| 626 | } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) { |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 627 | /* Set the amount of bytes to be prefetched */ |
| 628 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count); |
| 629 | |
| 630 | /* Set dma/mpu mode, the prefetch read / post write and |
| 631 | * enable the engine. Set which cs is has requested for. |
| 632 | */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 633 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) | |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 634 | PREFETCH_FIFOTHRESHOLD(fifo_th) | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 635 | ENABLE_PREFETCH | |
| 636 | (dma_mode << DMA_MPU_MODE) | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 637 | (0x1 & is_write))); |
| 638 | |
| 639 | /* Start the prefetch engine */ |
| 640 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 641 | } else { |
| 642 | return -EBUSY; |
| 643 | } |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 644 | |
| 645 | return 0; |
| 646 | } |
| 647 | EXPORT_SYMBOL(gpmc_prefetch_enable); |
| 648 | |
| 649 | /** |
| 650 | * gpmc_prefetch_reset - disables and stops the prefetch engine |
| 651 | */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 652 | int gpmc_prefetch_reset(int cs) |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 653 | { |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 654 | u32 config1; |
| 655 | |
| 656 | /* check if the same module/cs is trying to reset */ |
| 657 | config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); |
| 658 | if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs) |
| 659 | return -EINVAL; |
| 660 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 661 | /* Stop the PFPW engine */ |
| 662 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0); |
| 663 | |
| 664 | /* Reset/disable the PFPW engine */ |
| 665 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 666 | |
| 667 | return 0; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 668 | } |
| 669 | EXPORT_SYMBOL(gpmc_prefetch_reset); |
| 670 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 671 | static void __init gpmc_mem_init(void) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 672 | { |
| 673 | int cs; |
| 674 | unsigned long boot_rom_space = 0; |
| 675 | |
Kyungmin Park | 7f24516 | 2006-12-29 16:48:51 -0800 | [diff] [blame] | 676 | /* never allocate the first page, to facilitate bug detection; |
| 677 | * even if we didn't boot from ROM. |
| 678 | */ |
| 679 | boot_rom_space = BOOT_ROM_SPACE; |
| 680 | /* In apollon the CS0 is mapped as 0x0000 0000 */ |
| 681 | if (machine_is_omap_apollon()) |
| 682 | boot_rom_space = 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 683 | gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space; |
| 684 | gpmc_mem_root.end = GPMC_MEM_END; |
| 685 | |
| 686 | /* Reserve all regions that has been set up by bootloader */ |
| 687 | for (cs = 0; cs < GPMC_CS_NUM; cs++) { |
| 688 | u32 base, size; |
| 689 | |
| 690 | if (!gpmc_cs_mem_enabled(cs)) |
| 691 | continue; |
| 692 | gpmc_cs_get_memconf(cs, &base, &size); |
| 693 | if (gpmc_cs_insert_mem(cs, base, size) < 0) |
| 694 | BUG(); |
| 695 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 696 | } |
| 697 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 698 | static int __init gpmc_init(void) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 699 | { |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 700 | u32 l, irq; |
| 701 | int cs, ret = -EINVAL; |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 702 | int gpmc_irq; |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 703 | char *ck = NULL; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 704 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 705 | if (cpu_is_omap24xx()) { |
| 706 | ck = "core_l3_ck"; |
| 707 | if (cpu_is_omap2420()) |
| 708 | l = OMAP2420_GPMC_BASE; |
| 709 | else |
| 710 | l = OMAP34XX_GPMC_BASE; |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 711 | gpmc_irq = INT_34XX_GPMC_IRQ; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 712 | } else if (cpu_is_omap34xx()) { |
| 713 | ck = "gpmc_fck"; |
| 714 | l = OMAP34XX_GPMC_BASE; |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 715 | gpmc_irq = INT_34XX_GPMC_IRQ; |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 716 | } else if (cpu_is_omap44xx()) { |
Rajendra Nayak | d79b126 | 2009-12-09 00:01:44 +0530 | [diff] [blame] | 717 | ck = "gpmc_ck"; |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 718 | l = OMAP44XX_GPMC_BASE; |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 719 | gpmc_irq = OMAP44XX_IRQ_GPMC; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 720 | } |
| 721 | |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 722 | if (WARN_ON(!ck)) |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 723 | return ret; |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 724 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 725 | gpmc_l3_clk = clk_get(NULL, ck); |
| 726 | if (IS_ERR(gpmc_l3_clk)) { |
| 727 | printk(KERN_ERR "Could not get GPMC clock %s\n", ck); |
Sanjeev Premi | 85d7a07 | 2008-11-04 13:35:06 -0800 | [diff] [blame] | 728 | BUG(); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | gpmc_base = ioremap(l, SZ_4K); |
| 732 | if (!gpmc_base) { |
| 733 | clk_put(gpmc_l3_clk); |
| 734 | printk(KERN_ERR "Could not get GPMC register memory\n"); |
Sanjeev Premi | 85d7a07 | 2008-11-04 13:35:06 -0800 | [diff] [blame] | 735 | BUG(); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 736 | } |
| 737 | |
Olof Johansson | 1daa8c1 | 2010-01-20 22:39:29 +0000 | [diff] [blame] | 738 | clk_enable(gpmc_l3_clk); |
| 739 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 740 | l = gpmc_read_reg(GPMC_REVISION); |
| 741 | printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f); |
| 742 | /* Set smart idle mode and automatic L3 clock gating */ |
| 743 | l = gpmc_read_reg(GPMC_SYSCONFIG); |
| 744 | l &= 0x03 << 3; |
| 745 | l |= (0x02 << 3) | (1 << 0); |
| 746 | gpmc_write_reg(GPMC_SYSCONFIG, l); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 747 | gpmc_mem_init(); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 748 | |
| 749 | /* initalize the irq_chained */ |
| 750 | irq = OMAP_GPMC_IRQ_BASE; |
| 751 | for (cs = 0; cs < GPMC_CS_NUM; cs++) { |
Thomas Gleixner | 6845664a | 2011-03-24 13:25:22 +0100 | [diff] [blame] | 752 | irq_set_chip_and_handler(irq, &dummy_irq_chip, |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 753 | handle_simple_irq); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 754 | set_irq_flags(irq, IRQF_VALID); |
| 755 | irq++; |
| 756 | } |
| 757 | |
Paul Walmsley | 355f8ee | 2012-04-13 06:34:28 -0600 | [diff] [blame^] | 758 | ret = request_irq(gpmc_irq, gpmc_handle_irq, IRQF_SHARED, "gpmc", NULL); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 759 | if (ret) |
| 760 | pr_err("gpmc: irq-%d could not claim: err %d\n", |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 761 | gpmc_irq, ret); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 762 | return ret; |
| 763 | } |
| 764 | postcore_initcall(gpmc_init); |
| 765 | |
| 766 | static irqreturn_t gpmc_handle_irq(int irq, void *dev) |
| 767 | { |
| 768 | u8 cs; |
| 769 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 770 | /* check cs to invoke the irq */ |
| 771 | cs = ((gpmc_read_reg(GPMC_PREFETCH_CONFIG1)) >> CS_NUM_SHIFT) & 0x7; |
| 772 | if (OMAP_GPMC_IRQ_BASE+cs <= OMAP_GPMC_IRQ_END) |
| 773 | generic_handle_irq(OMAP_GPMC_IRQ_BASE+cs); |
| 774 | |
| 775 | return IRQ_HANDLED; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 776 | } |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 777 | |
| 778 | #ifdef CONFIG_ARCH_OMAP3 |
| 779 | static struct omap3_gpmc_regs gpmc_context; |
| 780 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 781 | void omap3_gpmc_save_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 782 | { |
| 783 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 784 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 785 | gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG); |
| 786 | gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE); |
| 787 | gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL); |
| 788 | gpmc_context.config = gpmc_read_reg(GPMC_CONFIG); |
| 789 | gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); |
| 790 | gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2); |
| 791 | gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL); |
| 792 | for (i = 0; i < GPMC_CS_NUM; i++) { |
| 793 | gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i); |
| 794 | if (gpmc_context.cs_context[i].is_valid) { |
| 795 | gpmc_context.cs_context[i].config1 = |
| 796 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG1); |
| 797 | gpmc_context.cs_context[i].config2 = |
| 798 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG2); |
| 799 | gpmc_context.cs_context[i].config3 = |
| 800 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG3); |
| 801 | gpmc_context.cs_context[i].config4 = |
| 802 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG4); |
| 803 | gpmc_context.cs_context[i].config5 = |
| 804 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG5); |
| 805 | gpmc_context.cs_context[i].config6 = |
| 806 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG6); |
| 807 | gpmc_context.cs_context[i].config7 = |
| 808 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG7); |
| 809 | } |
| 810 | } |
| 811 | } |
| 812 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 813 | void omap3_gpmc_restore_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 814 | { |
| 815 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 816 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 817 | gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig); |
| 818 | gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable); |
| 819 | gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl); |
| 820 | gpmc_write_reg(GPMC_CONFIG, gpmc_context.config); |
| 821 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1); |
| 822 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2); |
| 823 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control); |
| 824 | for (i = 0; i < GPMC_CS_NUM; i++) { |
| 825 | if (gpmc_context.cs_context[i].is_valid) { |
| 826 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG1, |
| 827 | gpmc_context.cs_context[i].config1); |
| 828 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG2, |
| 829 | gpmc_context.cs_context[i].config2); |
| 830 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG3, |
| 831 | gpmc_context.cs_context[i].config3); |
| 832 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG4, |
| 833 | gpmc_context.cs_context[i].config4); |
| 834 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG5, |
| 835 | gpmc_context.cs_context[i].config5); |
| 836 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG6, |
| 837 | gpmc_context.cs_context[i].config6); |
| 838 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG7, |
| 839 | gpmc_context.cs_context[i].config7); |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | #endif /* CONFIG_ARCH_OMAP3 */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 844 | |
| 845 | /** |
| 846 | * gpmc_enable_hwecc - enable hardware ecc functionality |
| 847 | * @cs: chip select number |
| 848 | * @mode: read/write mode |
| 849 | * @dev_width: device bus width(1 for x16, 0 for x8) |
| 850 | * @ecc_size: bytes for which ECC will be generated |
| 851 | */ |
| 852 | int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size) |
| 853 | { |
| 854 | unsigned int val; |
| 855 | |
| 856 | /* check if ecc module is in used */ |
| 857 | if (gpmc_ecc_used != -EINVAL) |
| 858 | return -EINVAL; |
| 859 | |
| 860 | gpmc_ecc_used = cs; |
| 861 | |
| 862 | /* clear ecc and enable bits */ |
| 863 | val = ((0x00000001<<8) | 0x00000001); |
| 864 | gpmc_write_reg(GPMC_ECC_CONTROL, val); |
| 865 | |
| 866 | /* program ecc and result sizes */ |
| 867 | val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F)); |
| 868 | gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val); |
| 869 | |
| 870 | switch (mode) { |
| 871 | case GPMC_ECC_READ: |
| 872 | gpmc_write_reg(GPMC_ECC_CONTROL, 0x101); |
| 873 | break; |
| 874 | case GPMC_ECC_READSYN: |
| 875 | gpmc_write_reg(GPMC_ECC_CONTROL, 0x100); |
| 876 | break; |
| 877 | case GPMC_ECC_WRITE: |
| 878 | gpmc_write_reg(GPMC_ECC_CONTROL, 0x101); |
| 879 | break; |
| 880 | default: |
| 881 | printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode); |
| 882 | break; |
| 883 | } |
| 884 | |
| 885 | /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */ |
| 886 | val = (dev_width << 7) | (cs << 1) | (0x1); |
| 887 | gpmc_write_reg(GPMC_ECC_CONFIG, val); |
| 888 | return 0; |
| 889 | } |
Bernhard Walle | f611b02 | 2012-03-05 16:11:01 -0800 | [diff] [blame] | 890 | EXPORT_SYMBOL_GPL(gpmc_enable_hwecc); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 891 | |
| 892 | /** |
| 893 | * gpmc_calculate_ecc - generate non-inverted ecc bytes |
| 894 | * @cs: chip select number |
| 895 | * @dat: data pointer over which ecc is computed |
| 896 | * @ecc_code: ecc code buffer |
| 897 | * |
| 898 | * Using non-inverted ECC is considered ugly since writing a blank |
| 899 | * page (padding) will clear the ECC bytes. This is not a problem as long |
| 900 | * no one is trying to write data on the seemingly unused page. Reading |
| 901 | * an erased page will produce an ECC mismatch between generated and read |
| 902 | * ECC bytes that has to be dealt with separately. |
| 903 | */ |
| 904 | int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code) |
| 905 | { |
| 906 | unsigned int val = 0x0; |
| 907 | |
| 908 | if (gpmc_ecc_used != cs) |
| 909 | return -EINVAL; |
| 910 | |
| 911 | /* read ecc result */ |
| 912 | val = gpmc_read_reg(GPMC_ECC1_RESULT); |
| 913 | *ecc_code++ = val; /* P128e, ..., P1e */ |
| 914 | *ecc_code++ = val >> 16; /* P128o, ..., P1o */ |
| 915 | /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */ |
| 916 | *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0); |
| 917 | |
| 918 | gpmc_ecc_used = -EINVAL; |
| 919 | return 0; |
| 920 | } |
Bernhard Walle | f611b02 | 2012-03-05 16:11:01 -0800 | [diff] [blame] | 921 | EXPORT_SYMBOL_GPL(gpmc_calculate_ecc); |