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