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 | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 31 | #include <plat/cpu.h> |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 32 | #include <plat/gpmc.h> |
Tony Lindgren | ce491cf | 2009-10-20 09:40:47 -0700 | [diff] [blame] | 33 | #include <plat/sdrc.h> |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame^] | 34 | #include <plat/omap_device.h> |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 35 | |
Tony Lindgren | dbc0416 | 2012-08-31 10:59:07 -0700 | [diff] [blame] | 36 | #include "soc.h" |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 37 | #include "common.h" |
| 38 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame^] | 39 | #define DEVICE_NAME "omap-gpmc" |
| 40 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 41 | /* GPMC register offsets */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 42 | #define GPMC_REVISION 0x00 |
| 43 | #define GPMC_SYSCONFIG 0x10 |
| 44 | #define GPMC_SYSSTATUS 0x14 |
| 45 | #define GPMC_IRQSTATUS 0x18 |
| 46 | #define GPMC_IRQENABLE 0x1c |
| 47 | #define GPMC_TIMEOUT_CONTROL 0x40 |
| 48 | #define GPMC_ERR_ADDRESS 0x44 |
| 49 | #define GPMC_ERR_TYPE 0x48 |
| 50 | #define GPMC_CONFIG 0x50 |
| 51 | #define GPMC_STATUS 0x54 |
| 52 | #define GPMC_PREFETCH_CONFIG1 0x1e0 |
| 53 | #define GPMC_PREFETCH_CONFIG2 0x1e4 |
Thara Gopinath | 15e02a3 | 2008-04-28 16:55:01 +0530 | [diff] [blame] | 54 | #define GPMC_PREFETCH_CONTROL 0x1ec |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 55 | #define GPMC_PREFETCH_STATUS 0x1f0 |
| 56 | #define GPMC_ECC_CONFIG 0x1f4 |
| 57 | #define GPMC_ECC_CONTROL 0x1f8 |
| 58 | #define GPMC_ECC_SIZE_CONFIG 0x1fc |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 59 | #define GPMC_ECC1_RESULT 0x200 |
Ivan Djelic | 8d602cf | 2012-04-26 14:17:49 +0200 | [diff] [blame] | 60 | #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 61 | |
Yegor Yefremov | 2c65e74 | 2012-05-09 08:32:49 -0700 | [diff] [blame] | 62 | /* GPMC ECC control settings */ |
| 63 | #define GPMC_ECC_CTRL_ECCCLEAR 0x100 |
| 64 | #define GPMC_ECC_CTRL_ECCDISABLE 0x000 |
| 65 | #define GPMC_ECC_CTRL_ECCREG1 0x001 |
| 66 | #define GPMC_ECC_CTRL_ECCREG2 0x002 |
| 67 | #define GPMC_ECC_CTRL_ECCREG3 0x003 |
| 68 | #define GPMC_ECC_CTRL_ECCREG4 0x004 |
| 69 | #define GPMC_ECC_CTRL_ECCREG5 0x005 |
| 70 | #define GPMC_ECC_CTRL_ECCREG6 0x006 |
| 71 | #define GPMC_ECC_CTRL_ECCREG7 0x007 |
| 72 | #define GPMC_ECC_CTRL_ECCREG8 0x008 |
| 73 | #define GPMC_ECC_CTRL_ECCREG9 0x009 |
| 74 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 75 | #define GPMC_CS0_OFFSET 0x60 |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 76 | #define GPMC_CS_SIZE 0x30 |
| 77 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 78 | #define GPMC_MEM_START 0x00000000 |
| 79 | #define GPMC_MEM_END 0x3FFFFFFF |
| 80 | #define BOOT_ROM_SPACE 0x100000 /* 1MB */ |
| 81 | |
| 82 | #define GPMC_CHUNK_SHIFT 24 /* 16 MB */ |
| 83 | #define GPMC_SECTION_SHIFT 28 /* 128 MB */ |
| 84 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 85 | #define CS_NUM_SHIFT 24 |
| 86 | #define ENABLE_PREFETCH (0x1 << 7) |
| 87 | #define DMA_MPU_MODE 2 |
| 88 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 89 | /* XXX: Only NAND irq has been considered,currently these are the only ones used |
| 90 | */ |
| 91 | #define GPMC_NR_IRQ 2 |
| 92 | |
| 93 | struct gpmc_client_irq { |
| 94 | unsigned irq; |
| 95 | u32 bitmask; |
| 96 | }; |
| 97 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 98 | /* Structure to save gpmc cs context */ |
| 99 | struct gpmc_cs_config { |
| 100 | u32 config1; |
| 101 | u32 config2; |
| 102 | u32 config3; |
| 103 | u32 config4; |
| 104 | u32 config5; |
| 105 | u32 config6; |
| 106 | u32 config7; |
| 107 | int is_valid; |
| 108 | }; |
| 109 | |
| 110 | /* |
| 111 | * Structure to save/restore gpmc context |
| 112 | * to support core off on OMAP3 |
| 113 | */ |
| 114 | struct omap3_gpmc_regs { |
| 115 | u32 sysconfig; |
| 116 | u32 irqenable; |
| 117 | u32 timeout_ctrl; |
| 118 | u32 config; |
| 119 | u32 prefetch_config1; |
| 120 | u32 prefetch_config2; |
| 121 | u32 prefetch_control; |
| 122 | struct gpmc_cs_config cs_context[GPMC_CS_NUM]; |
| 123 | }; |
| 124 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 125 | static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ]; |
| 126 | static struct irq_chip gpmc_irq_chip; |
| 127 | static unsigned gpmc_irq_start; |
| 128 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 129 | static struct resource gpmc_mem_root; |
| 130 | static struct resource gpmc_cs_mem[GPMC_CS_NUM]; |
Thomas Gleixner | 87b247c | 2007-05-10 22:33:04 -0700 | [diff] [blame] | 131 | static DEFINE_SPINLOCK(gpmc_mem_lock); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 132 | static unsigned int gpmc_cs_map; /* flag for cs which are initialized */ |
| 133 | static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */ |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 134 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 135 | static void __iomem *gpmc_base; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 136 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 137 | static struct clk *gpmc_l3_clk; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 138 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 139 | static irqreturn_t gpmc_handle_irq(int irq, void *dev); |
| 140 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 141 | static void gpmc_write_reg(int idx, u32 val) |
| 142 | { |
| 143 | __raw_writel(val, gpmc_base + idx); |
| 144 | } |
| 145 | |
| 146 | static u32 gpmc_read_reg(int idx) |
| 147 | { |
| 148 | return __raw_readl(gpmc_base + idx); |
| 149 | } |
| 150 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 151 | static void gpmc_cs_write_byte(int cs, int idx, u8 val) |
| 152 | { |
| 153 | void __iomem *reg_addr; |
| 154 | |
| 155 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
| 156 | __raw_writeb(val, reg_addr); |
| 157 | } |
| 158 | |
| 159 | static u8 gpmc_cs_read_byte(int cs, int idx) |
| 160 | { |
| 161 | void __iomem *reg_addr; |
| 162 | |
| 163 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
| 164 | return __raw_readb(reg_addr); |
| 165 | } |
| 166 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 167 | void gpmc_cs_write_reg(int cs, int idx, u32 val) |
| 168 | { |
| 169 | void __iomem *reg_addr; |
| 170 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 171 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 172 | __raw_writel(val, reg_addr); |
| 173 | } |
| 174 | |
| 175 | u32 gpmc_cs_read_reg(int cs, int idx) |
| 176 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 177 | void __iomem *reg_addr; |
| 178 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 179 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 180 | return __raw_readl(reg_addr); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 183 | /* TODO: Add support for gpmc_fck to clock framework and use it */ |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 184 | unsigned long gpmc_get_fclk_period(void) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 185 | { |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 186 | unsigned long rate = clk_get_rate(gpmc_l3_clk); |
| 187 | |
| 188 | if (rate == 0) { |
| 189 | printk(KERN_WARNING "gpmc_l3_clk not enabled\n"); |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | rate /= 1000; |
| 194 | rate = 1000000000 / rate; /* In picoseconds */ |
| 195 | |
| 196 | return rate; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | unsigned int gpmc_ns_to_ticks(unsigned int time_ns) |
| 200 | { |
| 201 | unsigned long tick_ps; |
| 202 | |
| 203 | /* Calculate in picosecs to yield more exact results */ |
| 204 | tick_ps = gpmc_get_fclk_period(); |
| 205 | |
| 206 | return (time_ns * 1000 + tick_ps - 1) / tick_ps; |
| 207 | } |
| 208 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 209 | unsigned int gpmc_ps_to_ticks(unsigned int time_ps) |
| 210 | { |
| 211 | unsigned long tick_ps; |
| 212 | |
| 213 | /* Calculate in picosecs to yield more exact results */ |
| 214 | tick_ps = gpmc_get_fclk_period(); |
| 215 | |
| 216 | return (time_ps + tick_ps - 1) / tick_ps; |
| 217 | } |
| 218 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 219 | unsigned int gpmc_ticks_to_ns(unsigned int ticks) |
| 220 | { |
| 221 | return ticks * gpmc_get_fclk_period() / 1000; |
| 222 | } |
| 223 | |
Kai Svahn | 2330059 | 2007-01-26 12:29:40 -0800 | [diff] [blame] | 224 | unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns) |
| 225 | { |
| 226 | unsigned long ticks = gpmc_ns_to_ticks(time_ns); |
| 227 | |
| 228 | return ticks * gpmc_get_fclk_period() / 1000; |
| 229 | } |
| 230 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 231 | #ifdef DEBUG |
| 232 | 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] | 233 | int time, const char *name) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 234 | #else |
| 235 | static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, |
| 236 | int time) |
| 237 | #endif |
| 238 | { |
| 239 | u32 l; |
| 240 | int ticks, mask, nr_bits; |
| 241 | |
| 242 | if (time == 0) |
| 243 | ticks = 0; |
| 244 | else |
| 245 | ticks = gpmc_ns_to_ticks(time); |
| 246 | nr_bits = end_bit - st_bit + 1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 247 | if (ticks >= 1 << nr_bits) { |
| 248 | #ifdef DEBUG |
| 249 | printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n", |
| 250 | cs, name, time, ticks, 1 << nr_bits); |
| 251 | #endif |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 252 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 253 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 254 | |
| 255 | mask = (1 << nr_bits) - 1; |
| 256 | l = gpmc_cs_read_reg(cs, reg); |
| 257 | #ifdef DEBUG |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 258 | printk(KERN_INFO |
| 259 | "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] | 260 | cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000, |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 261 | (l >> st_bit) & mask, time); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 262 | #endif |
| 263 | l &= ~(mask << st_bit); |
| 264 | l |= ticks << st_bit; |
| 265 | gpmc_cs_write_reg(cs, reg, l); |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | #ifdef DEBUG |
| 271 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 272 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), \ |
| 273 | t->field, #field) < 0) \ |
| 274 | return -1 |
| 275 | #else |
| 276 | #define GPMC_SET_ONE(reg, st, end, field) \ |
| 277 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \ |
| 278 | return -1 |
| 279 | #endif |
| 280 | |
| 281 | int gpmc_cs_calc_divider(int cs, unsigned int sync_clk) |
| 282 | { |
| 283 | int div; |
| 284 | u32 l; |
| 285 | |
Adrian Hunter | a3551f5 | 2010-12-09 10:48:27 +0200 | [diff] [blame] | 286 | l = sync_clk + (gpmc_get_fclk_period() - 1); |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 287 | div = l / gpmc_get_fclk_period(); |
| 288 | if (div > 4) |
| 289 | return -1; |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 290 | if (div <= 0) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 291 | div = 1; |
| 292 | |
| 293 | return div; |
| 294 | } |
| 295 | |
| 296 | int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t) |
| 297 | { |
| 298 | int div; |
| 299 | u32 l; |
| 300 | |
| 301 | div = gpmc_cs_calc_divider(cs, t->sync_clk); |
| 302 | if (div < 0) |
Paul Walmsley | a032d33 | 2012-08-03 09:21:10 -0600 | [diff] [blame] | 303 | return div; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 304 | |
| 305 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on); |
| 306 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off); |
| 307 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off); |
| 308 | |
| 309 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on); |
| 310 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off); |
| 311 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off); |
| 312 | |
| 313 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on); |
| 314 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off); |
| 315 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on); |
| 316 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off); |
| 317 | |
| 318 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle); |
| 319 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle); |
| 320 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access); |
| 321 | |
| 322 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access); |
| 323 | |
Syed Mohammed, Khasim | cc26b3b | 2008-10-09 17:51:41 +0300 | [diff] [blame] | 324 | if (cpu_is_omap34xx()) { |
| 325 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus); |
| 326 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access); |
| 327 | } |
| 328 | |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 329 | /* caller is expected to have initialized CONFIG1 to cover |
| 330 | * at least sync vs async |
| 331 | */ |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 332 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
David Brownell | 1c22cc1 | 2006-12-06 17:13:55 -0800 | [diff] [blame] | 333 | if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) { |
| 334 | #ifdef DEBUG |
| 335 | printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n", |
| 336 | cs, (div * gpmc_get_fclk_period()) / 1000, div); |
| 337 | #endif |
| 338 | l &= ~0x03; |
| 339 | l |= (div - 1); |
| 340 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l); |
| 341 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 346 | static void gpmc_cs_enable_mem(int cs, u32 base, u32 size) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 347 | { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 348 | u32 l; |
| 349 | u32 mask; |
| 350 | |
| 351 | mask = (1 << GPMC_SECTION_SHIFT) - size; |
| 352 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 353 | l &= ~0x3f; |
| 354 | l = (base >> GPMC_CHUNK_SHIFT) & 0x3f; |
| 355 | l &= ~(0x0f << 8); |
| 356 | l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8; |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 357 | l |= GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 358 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 359 | } |
| 360 | |
| 361 | static void gpmc_cs_disable_mem(int cs) |
| 362 | { |
| 363 | u32 l; |
| 364 | |
| 365 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 366 | l &= ~GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 367 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
| 368 | } |
| 369 | |
| 370 | static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size) |
| 371 | { |
| 372 | u32 l; |
| 373 | u32 mask; |
| 374 | |
| 375 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
| 376 | *base = (l & 0x3f) << GPMC_CHUNK_SHIFT; |
| 377 | mask = (l >> 8) & 0x0f; |
| 378 | *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT); |
| 379 | } |
| 380 | |
| 381 | static int gpmc_cs_mem_enabled(int cs) |
| 382 | { |
| 383 | u32 l; |
| 384 | |
| 385 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 386 | return l & GPMC_CONFIG7_CSVALID; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 387 | } |
| 388 | |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 389 | int gpmc_cs_set_reserved(int cs, int reserved) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 390 | { |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 391 | if (cs > GPMC_CS_NUM) |
| 392 | return -ENODEV; |
| 393 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 394 | gpmc_cs_map &= ~(1 << cs); |
| 395 | gpmc_cs_map |= (reserved ? 1 : 0) << cs; |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 396 | |
| 397 | return 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 398 | } |
| 399 | |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 400 | int gpmc_cs_reserved(int cs) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 401 | { |
Tony Lindgren | c40fae95 | 2006-12-07 13:58:10 -0800 | [diff] [blame] | 402 | if (cs > GPMC_CS_NUM) |
| 403 | return -ENODEV; |
| 404 | |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 405 | return gpmc_cs_map & (1 << cs); |
| 406 | } |
| 407 | |
| 408 | static unsigned long gpmc_mem_align(unsigned long size) |
| 409 | { |
| 410 | int order; |
| 411 | |
| 412 | size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1); |
| 413 | order = GPMC_CHUNK_SHIFT - 1; |
| 414 | do { |
| 415 | size >>= 1; |
| 416 | order++; |
| 417 | } while (size); |
| 418 | size = 1 << order; |
| 419 | return size; |
| 420 | } |
| 421 | |
| 422 | static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size) |
| 423 | { |
| 424 | struct resource *res = &gpmc_cs_mem[cs]; |
| 425 | int r; |
| 426 | |
| 427 | size = gpmc_mem_align(size); |
| 428 | spin_lock(&gpmc_mem_lock); |
| 429 | res->start = base; |
| 430 | res->end = base + size - 1; |
| 431 | r = request_resource(&gpmc_mem_root, res); |
| 432 | spin_unlock(&gpmc_mem_lock); |
| 433 | |
| 434 | return r; |
| 435 | } |
| 436 | |
| 437 | int gpmc_cs_request(int cs, unsigned long size, unsigned long *base) |
| 438 | { |
| 439 | struct resource *res = &gpmc_cs_mem[cs]; |
| 440 | int r = -1; |
| 441 | |
| 442 | if (cs > GPMC_CS_NUM) |
| 443 | return -ENODEV; |
| 444 | |
| 445 | size = gpmc_mem_align(size); |
| 446 | if (size > (1 << GPMC_SECTION_SHIFT)) |
| 447 | return -ENOMEM; |
| 448 | |
| 449 | spin_lock(&gpmc_mem_lock); |
| 450 | if (gpmc_cs_reserved(cs)) { |
| 451 | r = -EBUSY; |
| 452 | goto out; |
| 453 | } |
| 454 | if (gpmc_cs_mem_enabled(cs)) |
| 455 | r = adjust_resource(res, res->start & ~(size - 1), size); |
| 456 | if (r < 0) |
| 457 | r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0, |
| 458 | size, NULL, NULL); |
| 459 | if (r < 0) |
| 460 | goto out; |
| 461 | |
Tobias Klauser | 6d13524 | 2009-11-10 18:55:19 -0800 | [diff] [blame] | 462 | gpmc_cs_enable_mem(cs, res->start, resource_size(res)); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 463 | *base = res->start; |
| 464 | gpmc_cs_set_reserved(cs, 1); |
| 465 | out: |
| 466 | spin_unlock(&gpmc_mem_lock); |
| 467 | return r; |
| 468 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 469 | EXPORT_SYMBOL(gpmc_cs_request); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 470 | |
| 471 | void gpmc_cs_free(int cs) |
| 472 | { |
| 473 | spin_lock(&gpmc_mem_lock); |
Roel Kluin | e7fdc60 | 2009-11-17 14:39:06 -0800 | [diff] [blame] | 474 | if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) { |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 475 | printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs); |
| 476 | BUG(); |
| 477 | spin_unlock(&gpmc_mem_lock); |
| 478 | return; |
| 479 | } |
| 480 | gpmc_cs_disable_mem(cs); |
| 481 | release_resource(&gpmc_cs_mem[cs]); |
| 482 | gpmc_cs_set_reserved(cs, 0); |
| 483 | spin_unlock(&gpmc_mem_lock); |
| 484 | } |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 485 | EXPORT_SYMBOL(gpmc_cs_free); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 486 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 487 | /** |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 488 | * gpmc_read_status - read access request to get the different gpmc status |
| 489 | * @cmd: command type |
| 490 | * @return status |
| 491 | */ |
| 492 | int gpmc_read_status(int cmd) |
| 493 | { |
| 494 | int status = -EINVAL; |
| 495 | u32 regval = 0; |
| 496 | |
| 497 | switch (cmd) { |
| 498 | case GPMC_GET_IRQ_STATUS: |
| 499 | status = gpmc_read_reg(GPMC_IRQSTATUS); |
| 500 | break; |
| 501 | |
| 502 | case GPMC_PREFETCH_FIFO_CNT: |
| 503 | regval = gpmc_read_reg(GPMC_PREFETCH_STATUS); |
| 504 | status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval); |
| 505 | break; |
| 506 | |
| 507 | case GPMC_PREFETCH_COUNT: |
| 508 | regval = gpmc_read_reg(GPMC_PREFETCH_STATUS); |
| 509 | status = GPMC_PREFETCH_STATUS_COUNT(regval); |
| 510 | break; |
| 511 | |
| 512 | case GPMC_STATUS_BUFFER: |
| 513 | regval = gpmc_read_reg(GPMC_STATUS); |
| 514 | /* 1 : buffer is available to write */ |
| 515 | status = regval & GPMC_STATUS_BUFF_EMPTY; |
| 516 | break; |
| 517 | |
| 518 | default: |
| 519 | printk(KERN_ERR "gpmc_read_status: Not supported\n"); |
| 520 | } |
| 521 | return status; |
| 522 | } |
| 523 | EXPORT_SYMBOL(gpmc_read_status); |
| 524 | |
| 525 | /** |
| 526 | * gpmc_cs_configure - write request to configure gpmc |
| 527 | * @cs: chip select number |
| 528 | * @cmd: command type |
| 529 | * @wval: value to write |
| 530 | * @return status of the operation |
| 531 | */ |
| 532 | int gpmc_cs_configure(int cs, int cmd, int wval) |
| 533 | { |
| 534 | int err = 0; |
| 535 | u32 regval = 0; |
| 536 | |
| 537 | switch (cmd) { |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 538 | case GPMC_ENABLE_IRQ: |
| 539 | gpmc_write_reg(GPMC_IRQENABLE, wval); |
| 540 | break; |
| 541 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 542 | case GPMC_SET_IRQ_STATUS: |
| 543 | gpmc_write_reg(GPMC_IRQSTATUS, wval); |
| 544 | break; |
| 545 | |
| 546 | case GPMC_CONFIG_WP: |
| 547 | regval = gpmc_read_reg(GPMC_CONFIG); |
| 548 | if (wval) |
| 549 | regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */ |
| 550 | else |
| 551 | regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */ |
| 552 | gpmc_write_reg(GPMC_CONFIG, regval); |
| 553 | break; |
| 554 | |
| 555 | case GPMC_CONFIG_RDY_BSY: |
| 556 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
| 557 | if (wval) |
| 558 | regval |= WR_RD_PIN_MONITORING; |
| 559 | else |
| 560 | regval &= ~WR_RD_PIN_MONITORING; |
| 561 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 562 | break; |
| 563 | |
| 564 | case GPMC_CONFIG_DEV_SIZE: |
| 565 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
Yegor Yefremov | 8ef5d84 | 2012-01-23 08:32:23 +0100 | [diff] [blame] | 566 | |
| 567 | /* clear 2 target bits */ |
| 568 | regval &= ~GPMC_CONFIG1_DEVICESIZE(3); |
| 569 | |
| 570 | /* set the proper value */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 571 | regval |= GPMC_CONFIG1_DEVICESIZE(wval); |
Yegor Yefremov | 8ef5d84 | 2012-01-23 08:32:23 +0100 | [diff] [blame] | 572 | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 573 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 574 | break; |
| 575 | |
| 576 | case GPMC_CONFIG_DEV_TYPE: |
| 577 | regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
| 578 | regval |= GPMC_CONFIG1_DEVICETYPE(wval); |
| 579 | if (wval == GPMC_DEVICETYPE_NOR) |
| 580 | regval |= GPMC_CONFIG1_MUXADDDATA; |
| 581 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval); |
| 582 | break; |
| 583 | |
| 584 | default: |
| 585 | printk(KERN_ERR "gpmc_configure_cs: Not supported\n"); |
| 586 | err = -EINVAL; |
| 587 | } |
| 588 | |
| 589 | return err; |
| 590 | } |
| 591 | EXPORT_SYMBOL(gpmc_cs_configure); |
| 592 | |
| 593 | /** |
| 594 | * gpmc_nand_read - nand specific read access request |
| 595 | * @cs: chip select number |
| 596 | * @cmd: command type |
| 597 | */ |
| 598 | int gpmc_nand_read(int cs, int cmd) |
| 599 | { |
| 600 | int rval = -EINVAL; |
| 601 | |
| 602 | switch (cmd) { |
| 603 | case GPMC_NAND_DATA: |
| 604 | rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA); |
| 605 | break; |
| 606 | |
| 607 | default: |
| 608 | printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n"); |
| 609 | } |
| 610 | return rval; |
| 611 | } |
| 612 | EXPORT_SYMBOL(gpmc_nand_read); |
| 613 | |
| 614 | /** |
| 615 | * gpmc_nand_write - nand specific write request |
| 616 | * @cs: chip select number |
| 617 | * @cmd: command type |
| 618 | * @wval: value to write |
| 619 | */ |
| 620 | int gpmc_nand_write(int cs, int cmd, int wval) |
| 621 | { |
| 622 | int err = 0; |
| 623 | |
| 624 | switch (cmd) { |
| 625 | case GPMC_NAND_COMMAND: |
| 626 | gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval); |
| 627 | break; |
| 628 | |
| 629 | case GPMC_NAND_ADDRESS: |
| 630 | gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval); |
| 631 | break; |
| 632 | |
| 633 | case GPMC_NAND_DATA: |
| 634 | gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval); |
| 635 | |
| 636 | default: |
| 637 | printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n"); |
| 638 | err = -EINVAL; |
| 639 | } |
| 640 | return err; |
| 641 | } |
| 642 | EXPORT_SYMBOL(gpmc_nand_write); |
| 643 | |
| 644 | |
| 645 | |
| 646 | /** |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 647 | * gpmc_prefetch_enable - configures and starts prefetch transfer |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 648 | * @cs: cs (chip select) number |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 649 | * @fifo_th: fifo threshold to be used for read/ write |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 650 | * @dma_mode: dma mode enable (1) or disable (0) |
| 651 | * @u32_count: number of bytes to be transferred |
| 652 | * @is_write: prefetch read(0) or write post(1) mode |
| 653 | */ |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 654 | int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode, |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 655 | unsigned int u32_count, int is_write) |
| 656 | { |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 657 | |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 658 | if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) { |
| 659 | pr_err("gpmc: fifo threshold is not supported\n"); |
| 660 | return -1; |
| 661 | } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) { |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 662 | /* Set the amount of bytes to be prefetched */ |
| 663 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count); |
| 664 | |
| 665 | /* Set dma/mpu mode, the prefetch read / post write and |
| 666 | * enable the engine. Set which cs is has requested for. |
| 667 | */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 668 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) | |
Sukumar Ghorai | 317379a | 2011-01-28 15:42:07 +0530 | [diff] [blame] | 669 | PREFETCH_FIFOTHRESHOLD(fifo_th) | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 670 | ENABLE_PREFETCH | |
| 671 | (dma_mode << DMA_MPU_MODE) | |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 672 | (0x1 & is_write))); |
| 673 | |
| 674 | /* Start the prefetch engine */ |
| 675 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1); |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 676 | } else { |
| 677 | return -EBUSY; |
| 678 | } |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | EXPORT_SYMBOL(gpmc_prefetch_enable); |
| 683 | |
| 684 | /** |
| 685 | * gpmc_prefetch_reset - disables and stops the prefetch engine |
| 686 | */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 687 | int gpmc_prefetch_reset(int cs) |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 688 | { |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 689 | u32 config1; |
| 690 | |
| 691 | /* check if the same module/cs is trying to reset */ |
| 692 | config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); |
| 693 | if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs) |
| 694 | return -EINVAL; |
| 695 | |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 696 | /* Stop the PFPW engine */ |
| 697 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0); |
| 698 | |
| 699 | /* Reset/disable the PFPW engine */ |
| 700 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 701 | |
| 702 | return 0; |
vimal singh | 59e9c5a | 2009-07-13 16:26:24 +0530 | [diff] [blame] | 703 | } |
| 704 | EXPORT_SYMBOL(gpmc_prefetch_reset); |
| 705 | |
Afzal Mohammed | 52bd138 | 2012-08-30 12:53:22 -0700 | [diff] [blame] | 706 | void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs) |
| 707 | { |
| 708 | reg->gpmc_status = gpmc_base + GPMC_STATUS; |
| 709 | reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET + |
| 710 | GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs; |
| 711 | reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET + |
| 712 | GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs; |
| 713 | reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET + |
| 714 | GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs; |
| 715 | reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1; |
| 716 | reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2; |
| 717 | reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL; |
| 718 | reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS; |
| 719 | reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG; |
| 720 | reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL; |
| 721 | reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG; |
| 722 | reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT; |
| 723 | reg->gpmc_bch_result0 = gpmc_base + GPMC_ECC_BCH_RESULT_0; |
| 724 | } |
| 725 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 726 | int gpmc_get_client_irq(unsigned irq_config) |
| 727 | { |
| 728 | int i; |
| 729 | |
| 730 | if (hweight32(irq_config) > 1) |
| 731 | return 0; |
| 732 | |
| 733 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 734 | if (gpmc_client_irq[i].bitmask & irq_config) |
| 735 | return gpmc_client_irq[i].irq; |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | static int gpmc_irq_endis(unsigned irq, bool endis) |
| 741 | { |
| 742 | int i; |
| 743 | u32 regval; |
| 744 | |
| 745 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 746 | if (irq == gpmc_client_irq[i].irq) { |
| 747 | regval = gpmc_read_reg(GPMC_IRQENABLE); |
| 748 | if (endis) |
| 749 | regval |= gpmc_client_irq[i].bitmask; |
| 750 | else |
| 751 | regval &= ~gpmc_client_irq[i].bitmask; |
| 752 | gpmc_write_reg(GPMC_IRQENABLE, regval); |
| 753 | break; |
| 754 | } |
| 755 | |
| 756 | return 0; |
| 757 | } |
| 758 | |
| 759 | static void gpmc_irq_disable(struct irq_data *p) |
| 760 | { |
| 761 | gpmc_irq_endis(p->irq, false); |
| 762 | } |
| 763 | |
| 764 | static void gpmc_irq_enable(struct irq_data *p) |
| 765 | { |
| 766 | gpmc_irq_endis(p->irq, true); |
| 767 | } |
| 768 | |
| 769 | static void gpmc_irq_noop(struct irq_data *data) { } |
| 770 | |
| 771 | static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; } |
| 772 | |
| 773 | static int gpmc_setup_irq(int gpmc_irq) |
| 774 | { |
| 775 | int i; |
| 776 | u32 regval; |
| 777 | |
| 778 | if (!gpmc_irq) |
| 779 | return -EINVAL; |
| 780 | |
| 781 | gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0); |
| 782 | if (IS_ERR_VALUE(gpmc_irq_start)) { |
| 783 | pr_err("irq_alloc_descs failed\n"); |
| 784 | return gpmc_irq_start; |
| 785 | } |
| 786 | |
| 787 | gpmc_irq_chip.name = "gpmc"; |
| 788 | gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret; |
| 789 | gpmc_irq_chip.irq_enable = gpmc_irq_enable; |
| 790 | gpmc_irq_chip.irq_disable = gpmc_irq_disable; |
| 791 | gpmc_irq_chip.irq_shutdown = gpmc_irq_noop; |
| 792 | gpmc_irq_chip.irq_ack = gpmc_irq_noop; |
| 793 | gpmc_irq_chip.irq_mask = gpmc_irq_noop; |
| 794 | gpmc_irq_chip.irq_unmask = gpmc_irq_noop; |
| 795 | |
| 796 | gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE; |
| 797 | gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT; |
| 798 | |
| 799 | for (i = 0; i < GPMC_NR_IRQ; i++) { |
| 800 | gpmc_client_irq[i].irq = gpmc_irq_start + i; |
| 801 | irq_set_chip_and_handler(gpmc_client_irq[i].irq, |
| 802 | &gpmc_irq_chip, handle_simple_irq); |
| 803 | set_irq_flags(gpmc_client_irq[i].irq, |
| 804 | IRQF_VALID | IRQF_NOAUTOEN); |
| 805 | } |
| 806 | |
| 807 | /* Disable interrupts */ |
| 808 | gpmc_write_reg(GPMC_IRQENABLE, 0); |
| 809 | |
| 810 | /* clear interrupts */ |
| 811 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
| 812 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
| 813 | |
| 814 | return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL); |
| 815 | } |
| 816 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 817 | static void __init gpmc_mem_init(void) |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 818 | { |
| 819 | int cs; |
| 820 | unsigned long boot_rom_space = 0; |
| 821 | |
Kyungmin Park | 7f24516 | 2006-12-29 16:48:51 -0800 | [diff] [blame] | 822 | /* never allocate the first page, to facilitate bug detection; |
| 823 | * even if we didn't boot from ROM. |
| 824 | */ |
| 825 | boot_rom_space = BOOT_ROM_SPACE; |
| 826 | /* In apollon the CS0 is mapped as 0x0000 0000 */ |
| 827 | if (machine_is_omap_apollon()) |
| 828 | boot_rom_space = 0; |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 829 | gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space; |
| 830 | gpmc_mem_root.end = GPMC_MEM_END; |
| 831 | |
| 832 | /* Reserve all regions that has been set up by bootloader */ |
| 833 | for (cs = 0; cs < GPMC_CS_NUM; cs++) { |
| 834 | u32 base, size; |
| 835 | |
| 836 | if (!gpmc_cs_mem_enabled(cs)) |
| 837 | continue; |
| 838 | gpmc_cs_get_memconf(cs, &base, &size); |
| 839 | if (gpmc_cs_insert_mem(cs, base, size) < 0) |
| 840 | BUG(); |
| 841 | } |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 844 | static int __init gpmc_init(void) |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 845 | { |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 846 | u32 l; |
| 847 | int ret = -EINVAL; |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 848 | int gpmc_irq; |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 849 | char *ck = NULL; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 850 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 851 | if (cpu_is_omap24xx()) { |
| 852 | ck = "core_l3_ck"; |
| 853 | if (cpu_is_omap2420()) |
| 854 | l = OMAP2420_GPMC_BASE; |
| 855 | else |
| 856 | l = OMAP34XX_GPMC_BASE; |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 857 | gpmc_irq = 20 + OMAP_INTC_START; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 858 | } else if (cpu_is_omap34xx()) { |
| 859 | ck = "gpmc_fck"; |
| 860 | l = OMAP34XX_GPMC_BASE; |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 861 | gpmc_irq = 20 + OMAP_INTC_START; |
R Sricharan | 1a5da21 | 2012-04-19 18:03:02 +0530 | [diff] [blame] | 862 | } else if (cpu_is_omap44xx() || soc_is_omap54xx()) { |
| 863 | /* Base address and irq number are same for OMAP4/5 */ |
Rajendra Nayak | d79b126 | 2009-12-09 00:01:44 +0530 | [diff] [blame] | 864 | ck = "gpmc_ck"; |
Santosh Shilimkar | 4416907 | 2009-05-28 14:16:04 -0700 | [diff] [blame] | 865 | l = OMAP44XX_GPMC_BASE; |
Tony Lindgren | 7d7e1eb | 2012-08-27 17:43:01 -0700 | [diff] [blame] | 866 | gpmc_irq = 20 + OMAP44XX_IRQ_GIC_START; |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 867 | } |
| 868 | |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 869 | if (WARN_ON(!ck)) |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 870 | return ret; |
Kevin Hilman | 8d08436 | 2010-01-29 14:20:06 -0800 | [diff] [blame] | 871 | |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 872 | gpmc_l3_clk = clk_get(NULL, ck); |
| 873 | if (IS_ERR(gpmc_l3_clk)) { |
| 874 | printk(KERN_ERR "Could not get GPMC clock %s\n", ck); |
Sanjeev Premi | 85d7a07 | 2008-11-04 13:35:06 -0800 | [diff] [blame] | 875 | BUG(); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 876 | } |
| 877 | |
| 878 | gpmc_base = ioremap(l, SZ_4K); |
| 879 | if (!gpmc_base) { |
| 880 | clk_put(gpmc_l3_clk); |
| 881 | printk(KERN_ERR "Could not get GPMC register memory\n"); |
Sanjeev Premi | 85d7a07 | 2008-11-04 13:35:06 -0800 | [diff] [blame] | 882 | BUG(); |
Paul Walmsley | fd1dc87 | 2008-10-06 15:49:17 +0300 | [diff] [blame] | 883 | } |
| 884 | |
Rajendra Nayak | 4d7cb45 | 2012-09-22 02:24:16 -0600 | [diff] [blame] | 885 | clk_prepare_enable(gpmc_l3_clk); |
Olof Johansson | 1daa8c1 | 2010-01-20 22:39:29 +0000 | [diff] [blame] | 886 | |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 887 | l = gpmc_read_reg(GPMC_REVISION); |
| 888 | printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f); |
| 889 | /* Set smart idle mode and automatic L3 clock gating */ |
| 890 | l = gpmc_read_reg(GPMC_SYSCONFIG); |
| 891 | l &= 0x03 << 3; |
| 892 | l |= (0x02 << 3) | (1 << 0); |
| 893 | gpmc_write_reg(GPMC_SYSCONFIG, l); |
Imre Deak | f37e458 | 2006-09-25 12:41:33 +0300 | [diff] [blame] | 894 | gpmc_mem_init(); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 895 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 896 | ret = gpmc_setup_irq(gpmc_irq); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 897 | if (ret) |
| 898 | pr_err("gpmc: irq-%d could not claim: err %d\n", |
Balaji T K | 77aded2 | 2011-03-18 16:53:20 -0700 | [diff] [blame] | 899 | gpmc_irq, ret); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 900 | return ret; |
| 901 | } |
| 902 | postcore_initcall(gpmc_init); |
| 903 | |
Afzal Mohammed | 4be48fd | 2012-09-23 17:28:24 -0600 | [diff] [blame^] | 904 | static int __init omap_gpmc_init(void) |
| 905 | { |
| 906 | struct omap_hwmod *oh; |
| 907 | struct platform_device *pdev; |
| 908 | char *oh_name = "gpmc"; |
| 909 | |
| 910 | oh = omap_hwmod_lookup(oh_name); |
| 911 | if (!oh) { |
| 912 | pr_err("Could not look up %s\n", oh_name); |
| 913 | return -ENODEV; |
| 914 | } |
| 915 | |
| 916 | pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0, NULL, 0, 0); |
| 917 | WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name); |
| 918 | |
| 919 | return IS_ERR(pdev) ? PTR_ERR(pdev) : 0; |
| 920 | } |
| 921 | postcore_initcall(omap_gpmc_init); |
| 922 | |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 923 | static irqreturn_t gpmc_handle_irq(int irq, void *dev) |
| 924 | { |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 925 | int i; |
| 926 | u32 regval; |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 927 | |
Afzal Mohammed | 6b6c32f | 2012-08-30 12:53:23 -0700 | [diff] [blame] | 928 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
| 929 | |
| 930 | if (!regval) |
| 931 | return IRQ_NONE; |
| 932 | |
| 933 | for (i = 0; i < GPMC_NR_IRQ; i++) |
| 934 | if (regval & gpmc_client_irq[i].bitmask) |
| 935 | generic_handle_irq(gpmc_client_irq[i].irq); |
| 936 | |
| 937 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
Sukumar Ghorai | db97eb7d | 2011-01-28 15:42:05 +0530 | [diff] [blame] | 938 | |
| 939 | return IRQ_HANDLED; |
Juha Yrjola | 4bbbc1a | 2006-06-26 16:16:16 -0700 | [diff] [blame] | 940 | } |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 941 | |
| 942 | #ifdef CONFIG_ARCH_OMAP3 |
| 943 | static struct omap3_gpmc_regs gpmc_context; |
| 944 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 945 | void omap3_gpmc_save_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 946 | { |
| 947 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 948 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 949 | gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG); |
| 950 | gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE); |
| 951 | gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL); |
| 952 | gpmc_context.config = gpmc_read_reg(GPMC_CONFIG); |
| 953 | gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); |
| 954 | gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2); |
| 955 | gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL); |
| 956 | for (i = 0; i < GPMC_CS_NUM; i++) { |
| 957 | gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i); |
| 958 | if (gpmc_context.cs_context[i].is_valid) { |
| 959 | gpmc_context.cs_context[i].config1 = |
| 960 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG1); |
| 961 | gpmc_context.cs_context[i].config2 = |
| 962 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG2); |
| 963 | gpmc_context.cs_context[i].config3 = |
| 964 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG3); |
| 965 | gpmc_context.cs_context[i].config4 = |
| 966 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG4); |
| 967 | gpmc_context.cs_context[i].config5 = |
| 968 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG5); |
| 969 | gpmc_context.cs_context[i].config6 = |
| 970 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG6); |
| 971 | gpmc_context.cs_context[i].config7 = |
| 972 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG7); |
| 973 | } |
| 974 | } |
| 975 | } |
| 976 | |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 977 | void omap3_gpmc_restore_context(void) |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 978 | { |
| 979 | int i; |
Felipe Balbi | b2fa3b7 | 2010-02-15 10:03:33 -0800 | [diff] [blame] | 980 | |
Rajendra Nayak | a2d3e7b | 2008-09-26 17:47:33 +0530 | [diff] [blame] | 981 | gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig); |
| 982 | gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable); |
| 983 | gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl); |
| 984 | gpmc_write_reg(GPMC_CONFIG, gpmc_context.config); |
| 985 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1); |
| 986 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2); |
| 987 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control); |
| 988 | for (i = 0; i < GPMC_CS_NUM; i++) { |
| 989 | if (gpmc_context.cs_context[i].is_valid) { |
| 990 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG1, |
| 991 | gpmc_context.cs_context[i].config1); |
| 992 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG2, |
| 993 | gpmc_context.cs_context[i].config2); |
| 994 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG3, |
| 995 | gpmc_context.cs_context[i].config3); |
| 996 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG4, |
| 997 | gpmc_context.cs_context[i].config4); |
| 998 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG5, |
| 999 | gpmc_context.cs_context[i].config5); |
| 1000 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG6, |
| 1001 | gpmc_context.cs_context[i].config6); |
| 1002 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG7, |
| 1003 | gpmc_context.cs_context[i].config7); |
| 1004 | } |
| 1005 | } |
| 1006 | } |
| 1007 | #endif /* CONFIG_ARCH_OMAP3 */ |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 1008 | |
| 1009 | /** |
| 1010 | * gpmc_enable_hwecc - enable hardware ecc functionality |
| 1011 | * @cs: chip select number |
| 1012 | * @mode: read/write mode |
| 1013 | * @dev_width: device bus width(1 for x16, 0 for x8) |
| 1014 | * @ecc_size: bytes for which ECC will be generated |
| 1015 | */ |
| 1016 | int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size) |
| 1017 | { |
| 1018 | unsigned int val; |
| 1019 | |
| 1020 | /* check if ecc module is in used */ |
| 1021 | if (gpmc_ecc_used != -EINVAL) |
| 1022 | return -EINVAL; |
| 1023 | |
| 1024 | gpmc_ecc_used = cs; |
| 1025 | |
| 1026 | /* clear ecc and enable bits */ |
Yegor Yefremov | 2c65e74 | 2012-05-09 08:32:49 -0700 | [diff] [blame] | 1027 | gpmc_write_reg(GPMC_ECC_CONTROL, |
| 1028 | GPMC_ECC_CTRL_ECCCLEAR | |
| 1029 | GPMC_ECC_CTRL_ECCREG1); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 1030 | |
| 1031 | /* program ecc and result sizes */ |
| 1032 | val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F)); |
| 1033 | gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val); |
| 1034 | |
| 1035 | switch (mode) { |
| 1036 | case GPMC_ECC_READ: |
Yegor Yefremov | 2c65e74 | 2012-05-09 08:32:49 -0700 | [diff] [blame] | 1037 | case GPMC_ECC_WRITE: |
| 1038 | gpmc_write_reg(GPMC_ECC_CONTROL, |
| 1039 | GPMC_ECC_CTRL_ECCCLEAR | |
| 1040 | GPMC_ECC_CTRL_ECCREG1); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 1041 | break; |
| 1042 | case GPMC_ECC_READSYN: |
Yegor Yefremov | 2c65e74 | 2012-05-09 08:32:49 -0700 | [diff] [blame] | 1043 | gpmc_write_reg(GPMC_ECC_CONTROL, |
| 1044 | GPMC_ECC_CTRL_ECCCLEAR | |
| 1045 | GPMC_ECC_CTRL_ECCDISABLE); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 1046 | break; |
| 1047 | default: |
| 1048 | printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode); |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */ |
| 1053 | val = (dev_width << 7) | (cs << 1) | (0x1); |
| 1054 | gpmc_write_reg(GPMC_ECC_CONFIG, val); |
| 1055 | return 0; |
| 1056 | } |
Bernhard Walle | f611b02 | 2012-03-05 16:11:01 -0800 | [diff] [blame] | 1057 | EXPORT_SYMBOL_GPL(gpmc_enable_hwecc); |
Sukumar Ghorai | 948d38e | 2010-07-09 09:14:44 +0000 | [diff] [blame] | 1058 | |
| 1059 | /** |
| 1060 | * gpmc_calculate_ecc - generate non-inverted ecc bytes |
| 1061 | * @cs: chip select number |
| 1062 | * @dat: data pointer over which ecc is computed |
| 1063 | * @ecc_code: ecc code buffer |
| 1064 | * |
| 1065 | * Using non-inverted ECC is considered ugly since writing a blank |
| 1066 | * page (padding) will clear the ECC bytes. This is not a problem as long |
| 1067 | * no one is trying to write data on the seemingly unused page. Reading |
| 1068 | * an erased page will produce an ECC mismatch between generated and read |
| 1069 | * ECC bytes that has to be dealt with separately. |
| 1070 | */ |
| 1071 | int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code) |
| 1072 | { |
| 1073 | unsigned int val = 0x0; |
| 1074 | |
| 1075 | if (gpmc_ecc_used != cs) |
| 1076 | return -EINVAL; |
| 1077 | |
| 1078 | /* read ecc result */ |
| 1079 | val = gpmc_read_reg(GPMC_ECC1_RESULT); |
| 1080 | *ecc_code++ = val; /* P128e, ..., P1e */ |
| 1081 | *ecc_code++ = val >> 16; /* P128o, ..., P1o */ |
| 1082 | /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */ |
| 1083 | *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0); |
| 1084 | |
| 1085 | gpmc_ecc_used = -EINVAL; |
| 1086 | return 0; |
| 1087 | } |
Bernhard Walle | f611b02 | 2012-03-05 16:11:01 -0800 | [diff] [blame] | 1088 | EXPORT_SYMBOL_GPL(gpmc_calculate_ecc); |
Ivan Djelic | 8d602cf | 2012-04-26 14:17:49 +0200 | [diff] [blame] | 1089 | |
| 1090 | #ifdef CONFIG_ARCH_OMAP3 |
| 1091 | |
| 1092 | /** |
| 1093 | * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality |
| 1094 | * @cs: chip select number |
| 1095 | * @nsectors: how many 512-byte sectors to process |
| 1096 | * @nerrors: how many errors to correct per sector (4 or 8) |
| 1097 | * |
| 1098 | * This function must be executed before any call to gpmc_enable_hwecc_bch. |
| 1099 | */ |
| 1100 | int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors) |
| 1101 | { |
| 1102 | /* check if ecc module is in use */ |
| 1103 | if (gpmc_ecc_used != -EINVAL) |
| 1104 | return -EINVAL; |
| 1105 | |
| 1106 | /* support only OMAP3 class */ |
| 1107 | if (!cpu_is_omap34xx()) { |
| 1108 | printk(KERN_ERR "BCH ecc is not supported on this CPU\n"); |
| 1109 | return -EINVAL; |
| 1110 | } |
| 1111 | |
| 1112 | /* |
| 1113 | * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1. |
| 1114 | * Other chips may be added if confirmed to work. |
| 1115 | */ |
| 1116 | if ((nerrors == 4) && |
| 1117 | (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) { |
| 1118 | printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n"); |
| 1119 | return -EINVAL; |
| 1120 | } |
| 1121 | |
| 1122 | /* sanity check */ |
| 1123 | if (nsectors > 8) { |
| 1124 | printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n", |
| 1125 | nsectors); |
| 1126 | return -EINVAL; |
| 1127 | } |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch); |
| 1132 | |
| 1133 | /** |
| 1134 | * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality |
| 1135 | * @cs: chip select number |
| 1136 | * @mode: read/write mode |
| 1137 | * @dev_width: device bus width(1 for x16, 0 for x8) |
| 1138 | * @nsectors: how many 512-byte sectors to process |
| 1139 | * @nerrors: how many errors to correct per sector (4 or 8) |
| 1140 | */ |
| 1141 | int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors, |
| 1142 | int nerrors) |
| 1143 | { |
| 1144 | unsigned int val; |
| 1145 | |
| 1146 | /* check if ecc module is in use */ |
| 1147 | if (gpmc_ecc_used != -EINVAL) |
| 1148 | return -EINVAL; |
| 1149 | |
| 1150 | gpmc_ecc_used = cs; |
| 1151 | |
| 1152 | /* clear ecc and enable bits */ |
| 1153 | gpmc_write_reg(GPMC_ECC_CONTROL, 0x1); |
| 1154 | |
| 1155 | /* |
| 1156 | * When using BCH, sector size is hardcoded to 512 bytes. |
| 1157 | * Here we are using wrapping mode 6 both for reading and writing, with: |
| 1158 | * size0 = 0 (no additional protected byte in spare area) |
| 1159 | * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area) |
| 1160 | */ |
| 1161 | gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12)); |
| 1162 | |
| 1163 | /* BCH configuration */ |
| 1164 | val = ((1 << 16) | /* enable BCH */ |
| 1165 | (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */ |
| 1166 | (0x06 << 8) | /* wrap mode = 6 */ |
| 1167 | (dev_width << 7) | /* bus width */ |
| 1168 | (((nsectors-1) & 0x7) << 4) | /* number of sectors */ |
| 1169 | (cs << 1) | /* ECC CS */ |
| 1170 | (0x1)); /* enable ECC */ |
| 1171 | |
| 1172 | gpmc_write_reg(GPMC_ECC_CONFIG, val); |
| 1173 | gpmc_write_reg(GPMC_ECC_CONTROL, 0x101); |
| 1174 | return 0; |
| 1175 | } |
| 1176 | EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch); |
| 1177 | |
| 1178 | /** |
| 1179 | * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes |
| 1180 | * @cs: chip select number |
| 1181 | * @dat: The pointer to data on which ecc is computed |
| 1182 | * @ecc: The ecc output buffer |
| 1183 | */ |
| 1184 | int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc) |
| 1185 | { |
| 1186 | int i; |
| 1187 | unsigned long nsectors, reg, val1, val2; |
| 1188 | |
| 1189 | if (gpmc_ecc_used != cs) |
| 1190 | return -EINVAL; |
| 1191 | |
| 1192 | nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1; |
| 1193 | |
| 1194 | for (i = 0; i < nsectors; i++) { |
| 1195 | |
| 1196 | reg = GPMC_ECC_BCH_RESULT_0 + 16*i; |
| 1197 | |
| 1198 | /* Read hw-computed remainder */ |
| 1199 | val1 = gpmc_read_reg(reg + 0); |
| 1200 | val2 = gpmc_read_reg(reg + 4); |
| 1201 | |
| 1202 | /* |
| 1203 | * Add constant polynomial to remainder, in order to get an ecc |
| 1204 | * sequence of 0xFFs for a buffer filled with 0xFFs; and |
| 1205 | * left-justify the resulting polynomial. |
| 1206 | */ |
| 1207 | *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF); |
| 1208 | *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF); |
| 1209 | *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF)); |
| 1210 | *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF); |
| 1211 | *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF); |
| 1212 | *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF); |
| 1213 | *ecc++ = 0x7f ^ ((val1 & 0xF) << 4); |
| 1214 | } |
| 1215 | |
| 1216 | gpmc_ecc_used = -EINVAL; |
| 1217 | return 0; |
| 1218 | } |
| 1219 | EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4); |
| 1220 | |
| 1221 | /** |
| 1222 | * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes |
| 1223 | * @cs: chip select number |
| 1224 | * @dat: The pointer to data on which ecc is computed |
| 1225 | * @ecc: The ecc output buffer |
| 1226 | */ |
| 1227 | int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc) |
| 1228 | { |
| 1229 | int i; |
| 1230 | unsigned long nsectors, reg, val1, val2, val3, val4; |
| 1231 | |
| 1232 | if (gpmc_ecc_used != cs) |
| 1233 | return -EINVAL; |
| 1234 | |
| 1235 | nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1; |
| 1236 | |
| 1237 | for (i = 0; i < nsectors; i++) { |
| 1238 | |
| 1239 | reg = GPMC_ECC_BCH_RESULT_0 + 16*i; |
| 1240 | |
| 1241 | /* Read hw-computed remainder */ |
| 1242 | val1 = gpmc_read_reg(reg + 0); |
| 1243 | val2 = gpmc_read_reg(reg + 4); |
| 1244 | val3 = gpmc_read_reg(reg + 8); |
| 1245 | val4 = gpmc_read_reg(reg + 12); |
| 1246 | |
| 1247 | /* |
| 1248 | * Add constant polynomial to remainder, in order to get an ecc |
| 1249 | * sequence of 0xFFs for a buffer filled with 0xFFs. |
| 1250 | */ |
| 1251 | *ecc++ = 0xef ^ (val4 & 0xFF); |
| 1252 | *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF); |
| 1253 | *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF); |
| 1254 | *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF); |
| 1255 | *ecc++ = 0xed ^ (val3 & 0xFF); |
| 1256 | *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF); |
| 1257 | *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF); |
| 1258 | *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF); |
| 1259 | *ecc++ = 0x97 ^ (val2 & 0xFF); |
| 1260 | *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF); |
| 1261 | *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF); |
| 1262 | *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF); |
| 1263 | *ecc++ = 0xb5 ^ (val1 & 0xFF); |
| 1264 | } |
| 1265 | |
| 1266 | gpmc_ecc_used = -EINVAL; |
| 1267 | return 0; |
| 1268 | } |
| 1269 | EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8); |
| 1270 | |
| 1271 | #endif /* CONFIG_ARCH_OMAP3 */ |