blob: 4dd1c65ee70c63a39e273871ef3d431ea18f1bec [file] [log] [blame]
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001/*
2 * GPMC support functions
3 *
4 * Copyright (C) 2005-2006 Nokia Corporation
5 *
6 * Author: Juha Yrjola
7 *
Santosh Shilimkar44169072009-05-28 14:16:04 -07008 * Copyright (C) 2009 Texas Instruments
9 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
10 *
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070011 * 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 */
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +053015#include <linux/irq.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070016#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/err.h>
19#include <linux/clk.h>
Imre Deakf37e4582006-09-25 12:41:33 +030020#include <linux/ioport.h>
21#include <linux/spinlock.h>
Russell Kingfced80c2008-09-06 12:10:45 +010022#include <linux/io.h>
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030023#include <linux/module.h>
Roger Quadrosd2d00862016-03-07 12:18:43 +020024#include <linux/gpio/driver.h>
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +053025#include <linux/interrupt.h>
Roger Quadros384258f2015-07-30 14:49:23 +030026#include <linux/irqdomain.h>
Afzal Mohammedda496872012-09-23 17:28:25 -060027#include <linux/platform_device.h>
Daniel Mackbc6b1e72012-12-14 11:36:44 +010028#include <linux/of.h>
Jon Huntercdd69282013-02-08 16:46:13 -060029#include <linux/of_address.h>
Daniel Mackbc6b1e72012-12-14 11:36:44 +010030#include <linux/of_mtd.h>
31#include <linux/of_device.h>
Robert ABELb1dc1ca2015-02-27 16:56:49 +010032#include <linux/of_platform.h>
Tony Lindgrene639cd52014-11-20 12:11:25 -080033#include <linux/omap-gpmc.h>
avinash philipb3f55252013-06-12 16:30:56 +053034#include <linux/pm_runtime.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070035
Afzal Mohammedbc3668e2012-09-29 12:26:13 +053036#include <linux/platform_data/mtd-nand-omap2.h>
Tony Lindgrene639cd52014-11-20 12:11:25 -080037#include <linux/platform_data/mtd-onenand-omap2.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070038
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070039#include <asm/mach-types.h>
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070040
Afzal Mohammed4be48fd2012-09-23 17:28:24 -060041#define DEVICE_NAME "omap-gpmc"
42
Paul Walmsleyfd1dc872008-10-06 15:49:17 +030043/* GPMC register offsets */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070044#define GPMC_REVISION 0x00
45#define GPMC_SYSCONFIG 0x10
46#define GPMC_SYSSTATUS 0x14
47#define GPMC_IRQSTATUS 0x18
48#define GPMC_IRQENABLE 0x1c
49#define GPMC_TIMEOUT_CONTROL 0x40
50#define GPMC_ERR_ADDRESS 0x44
51#define GPMC_ERR_TYPE 0x48
52#define GPMC_CONFIG 0x50
53#define GPMC_STATUS 0x54
54#define GPMC_PREFETCH_CONFIG1 0x1e0
55#define GPMC_PREFETCH_CONFIG2 0x1e4
Thara Gopinath15e02a32008-04-28 16:55:01 +053056#define GPMC_PREFETCH_CONTROL 0x1ec
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070057#define GPMC_PREFETCH_STATUS 0x1f0
58#define GPMC_ECC_CONFIG 0x1f4
59#define GPMC_ECC_CONTROL 0x1f8
60#define GPMC_ECC_SIZE_CONFIG 0x1fc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000061#define GPMC_ECC1_RESULT 0x200
Ivan Djelic8d602cf2012-04-26 14:17:49 +020062#define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053063#define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
64#define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
65#define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
pekon gupta27c9fd62014-05-19 13:24:39 +053066#define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
67#define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
68#define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070069
Yegor Yefremov2c65e742012-05-09 08:32:49 -070070/* 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
Roger Quadrose378d222014-08-29 19:11:52 +030083#define GPMC_CONFIG_LIMITEDADDRESS BIT(1)
84
Roger Quadros512d73d2015-08-05 13:34:50 +030085#define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS BIT(0)
86
Afzal Mohammed559d94b2012-05-28 17:51:37 +053087#define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
88#define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
89#define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
90#define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
91#define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
92#define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
93
Sukumar Ghorai948d38e2010-07-09 09:14:44 +000094#define GPMC_CS0_OFFSET 0x60
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070095#define GPMC_CS_SIZE 0x30
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +053096#define GPMC_BCH_SIZE 0x10
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -070097
Roger Quadrosbdd7e032015-07-09 17:31:45 +030098/*
99 * The first 1MB of GPMC address space is typically mapped to
100 * the internal ROM. Never allocate the first page, to
101 * facilitate bug detection; even if we didn't boot from ROM.
102 * As GPMC minimum partition size is 16MB we can only start from
103 * there.
104 */
105#define GPMC_MEM_START 0x1000000
Imre Deakf37e4582006-09-25 12:41:33 +0300106#define GPMC_MEM_END 0x3FFFFFFF
Imre Deakf37e4582006-09-25 12:41:33 +0300107
108#define GPMC_CHUNK_SHIFT 24 /* 16 MB */
109#define GPMC_SECTION_SHIFT 28 /* 128 MB */
110
vimal singh59e9c5a2009-07-13 16:26:24 +0530111#define CS_NUM_SHIFT 24
112#define ENABLE_PREFETCH (0x1 << 7)
113#define DMA_MPU_MODE 2
114
Afzal Mohammedda496872012-09-23 17:28:25 -0600115#define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
116#define GPMC_REVISION_MINOR(l) (l & 0xf)
117
118#define GPMC_HAS_WR_ACCESS 0x1
119#define GPMC_HAS_WR_DATA_MUX_BUS 0x2
Jon Hunteraa8d4762013-02-21 15:25:23 -0600120#define GPMC_HAS_MUX_AAD 0x4
Afzal Mohammedda496872012-09-23 17:28:25 -0600121
Jon Hunter9f833152013-02-20 15:53:38 -0600122#define GPMC_NR_WAITPINS 4
123
Tony Lindgrene639cd52014-11-20 12:11:25 -0800124#define GPMC_CS_CONFIG1 0x00
125#define GPMC_CS_CONFIG2 0x04
126#define GPMC_CS_CONFIG3 0x08
127#define GPMC_CS_CONFIG4 0x0c
128#define GPMC_CS_CONFIG5 0x10
129#define GPMC_CS_CONFIG6 0x14
130#define GPMC_CS_CONFIG7 0x18
131#define GPMC_CS_NAND_COMMAND 0x1c
132#define GPMC_CS_NAND_ADDRESS 0x20
133#define GPMC_CS_NAND_DATA 0x24
134
135/* Control Commands */
136#define GPMC_CONFIG_RDY_BSY 0x00000001
137#define GPMC_CONFIG_DEV_SIZE 0x00000002
138#define GPMC_CONFIG_DEV_TYPE 0x00000003
Tony Lindgrene639cd52014-11-20 12:11:25 -0800139
140#define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31)
141#define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30)
142#define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29)
143#define GPMC_CONFIG1_READTYPE_SYNC (1 << 29)
144#define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
145#define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27)
146#define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27)
147#define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
Robert ABEL4b613e92015-02-27 16:56:55 +0100148/** CLKACTIVATIONTIME Max Ticks */
149#define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
Tony Lindgrene639cd52014-11-20 12:11:25 -0800150#define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23)
Robert ABEL4b613e92015-02-27 16:56:55 +0100151/** ATTACHEDDEVICEPAGELENGTH Max Value */
152#define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
Tony Lindgrene639cd52014-11-20 12:11:25 -0800153#define GPMC_CONFIG1_WAIT_READ_MON (1 << 22)
154#define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21)
Robert ABEL2e676902015-02-27 16:56:53 +0100155#define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
156/** WAITMONITORINGTIME Max Ticks */
157#define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2
Tony Lindgrene639cd52014-11-20 12:11:25 -0800158#define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16)
159#define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12)
160#define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1)
Robert ABEL4b613e92015-02-27 16:56:55 +0100161/** DEVICESIZE Max Value */
162#define GPMC_CONFIG1_DEVICESIZE_MAX 1
Tony Lindgrene639cd52014-11-20 12:11:25 -0800163#define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10)
164#define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0)
165#define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8)
166#define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4)
167#define GPMC_CONFIG1_FCLK_DIV(val) (val & 3)
168#define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1))
169#define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2))
170#define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3))
171#define GPMC_CONFIG7_CSVALID (1 << 6)
172
Semen Protsenko9c4f7572015-01-24 22:28:38 +0200173#define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f
174#define GPMC_CONFIG7_CSVALID_MASK BIT(6)
175#define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
176#define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
177/* All CONFIG7 bits except reserved bits */
178#define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \
179 GPMC_CONFIG7_CSVALID_MASK | \
180 GPMC_CONFIG7_MASKADDRESS_MASK)
181
Tony Lindgrene639cd52014-11-20 12:11:25 -0800182#define GPMC_DEVICETYPE_NOR 0
183#define GPMC_DEVICETYPE_NAND 2
184#define GPMC_CONFIG_WRITEPROTECT 0x00000010
185#define WR_RD_PIN_MONITORING 0x00600000
186
Tony Lindgrene639cd52014-11-20 12:11:25 -0800187/* ECC commands */
188#define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */
189#define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */
190#define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */
191
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700192/* XXX: Only NAND irq has been considered,currently these are the only ones used
193 */
194#define GPMC_NR_IRQ 2
195
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100196enum gpmc_clk_domain {
197 GPMC_CD_FCLK,
198 GPMC_CD_CLK
199};
200
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800201struct gpmc_cs_data {
202 const char *name;
203
204#define GPMC_CS_RESERVED (1 << 0)
205 u32 flags;
206
207 struct resource mem;
208};
209
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530210/* Structure to save gpmc cs context */
211struct gpmc_cs_config {
212 u32 config1;
213 u32 config2;
214 u32 config3;
215 u32 config4;
216 u32 config5;
217 u32 config6;
218 u32 config7;
219 int is_valid;
220};
221
222/*
223 * Structure to save/restore gpmc context
224 * to support core off on OMAP3
225 */
226struct omap3_gpmc_regs {
227 u32 sysconfig;
228 u32 irqenable;
229 u32 timeout_ctrl;
230 u32 config;
231 u32 prefetch_config1;
232 u32 prefetch_config2;
233 u32 prefetch_control;
234 struct gpmc_cs_config cs_context[GPMC_CS_NUM];
235};
236
Roger Quadros384258f2015-07-30 14:49:23 +0300237struct gpmc_device {
238 struct device *dev;
239 int irq;
240 struct irq_chip irq_chip;
Roger Quadrosd2d00862016-03-07 12:18:43 +0200241 struct gpio_chip gpio_chip;
Roger Quadros384258f2015-07-30 14:49:23 +0300242};
243
244static struct irq_domain *gpmc_irq_domain;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -0700245
Imre Deakf37e4582006-09-25 12:41:33 +0300246static struct resource gpmc_mem_root;
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800247static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
Thomas Gleixner87b247c2007-05-10 22:33:04 -0700248static DEFINE_SPINLOCK(gpmc_mem_lock);
Jon Hunter6797b4f2013-02-01 10:38:45 -0600249/* Define chip-selects as reserved by default until probe completes */
Gupta Pekonf34f3712013-05-31 17:31:30 +0530250static unsigned int gpmc_cs_num = GPMC_CS_NUM;
Jon Hunter9f833152013-02-20 15:53:38 -0600251static unsigned int gpmc_nr_waitpins;
Afzal Mohammedda496872012-09-23 17:28:25 -0600252static resource_size_t phys_base, mem_size;
253static unsigned gpmc_capability;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300254static void __iomem *gpmc_base;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700255
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300256static struct clk *gpmc_l3_clk;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700257
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +0530258static irqreturn_t gpmc_handle_irq(int irq, void *dev);
259
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700260static void gpmc_write_reg(int idx, u32 val)
261{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300262 writel_relaxed(val, gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700263}
264
265static u32 gpmc_read_reg(int idx)
266{
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300267 return readl_relaxed(gpmc_base + idx);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700268}
269
270void gpmc_cs_write_reg(int cs, int idx, u32 val)
271{
272 void __iomem *reg_addr;
273
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000274 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300275 writel_relaxed(val, reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700276}
277
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300278static u32 gpmc_cs_read_reg(int cs, int idx)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700279{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300280 void __iomem *reg_addr;
281
Sukumar Ghorai948d38e2010-07-09 09:14:44 +0000282 reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
Victor Kamenskyedfaf052014-04-15 20:37:46 +0300283 return readl_relaxed(reg_addr);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700284}
285
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300286/* TODO: Add support for gpmc_fck to clock framework and use it */
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300287static unsigned long gpmc_get_fclk_period(void)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700288{
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300289 unsigned long rate = clk_get_rate(gpmc_l3_clk);
290
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300291 rate /= 1000;
292 rate = 1000000000 / rate; /* In picoseconds */
293
294 return rate;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700295}
296
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100297/**
298 * gpmc_get_clk_period - get period of selected clock domain in ps
299 * @cs Chip Select Region.
300 * @cd Clock Domain.
301 *
302 * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
303 * prior to calling this function with GPMC_CD_CLK.
304 */
305static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
306{
307
308 unsigned long tick_ps = gpmc_get_fclk_period();
309 u32 l;
310 int div;
311
312 switch (cd) {
313 case GPMC_CD_CLK:
314 /* get current clk divider */
315 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
316 div = (l & 0x03) + 1;
317 /* get GPMC_CLK period */
318 tick_ps *= div;
319 break;
320 case GPMC_CD_FCLK:
321 /* FALL-THROUGH */
322 default:
323 break;
324 }
325
326 return tick_ps;
327
328}
329
330static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
331 enum gpmc_clk_domain cd)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700332{
333 unsigned long tick_ps;
334
335 /* Calculate in picosecs to yield more exact results */
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100336 tick_ps = gpmc_get_clk_period(cs, cd);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700337
338 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
339}
340
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100341static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
342{
343 return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
344}
345
Ezequiel Garcia3fc089e2013-02-12 16:22:17 -0300346static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
Adrian Huntera3551f52010-12-09 10:48:27 +0200347{
348 unsigned long tick_ps;
349
350 /* Calculate in picosecs to yield more exact results */
351 tick_ps = gpmc_get_fclk_period();
352
353 return (time_ps + tick_ps - 1) / tick_ps;
354}
355
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100356unsigned int gpmc_clk_ticks_to_ns(unsigned ticks, int cs,
357 enum gpmc_clk_domain cd)
358{
359 return ticks * gpmc_get_clk_period(cs, cd) / 1000;
360}
361
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300362unsigned int gpmc_ticks_to_ns(unsigned int ticks)
363{
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100364 return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +0300365}
366
Afzal Mohammed246da262012-08-02 20:02:10 +0530367static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
368{
369 return ticks * gpmc_get_fclk_period();
370}
371
372static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
373{
374 unsigned long ticks = gpmc_ps_to_ticks(time_ps);
375
376 return ticks * gpmc_get_fclk_period();
377}
378
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530379static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
380{
381 u32 l;
382
383 l = gpmc_cs_read_reg(cs, reg);
384 if (value)
385 l |= mask;
386 else
387 l &= ~mask;
388 gpmc_cs_write_reg(cs, reg, l);
389}
390
391static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
392{
393 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
394 GPMC_CONFIG1_TIME_PARA_GRAN,
395 p->time_para_granularity);
396 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
397 GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
398 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
399 GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
400 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
401 GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
402 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
403 GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
404 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
405 GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
406 p->cycle2cyclesamecsen);
407 gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
408 GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
409 p->cycle2cyclediffcsen);
410}
411
Tony Lindgren63aa9452015-06-01 19:22:10 -0600412#ifdef CONFIG_OMAP_GPMC_DEBUG
Robert ABEL563dbb22015-02-27 16:56:51 +0100413/**
414 * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
415 * @cs: Chip Select Region
416 * @reg: GPMC_CS_CONFIGn register offset.
417 * @st_bit: Start Bit
418 * @end_bit: End Bit. Must be >= @st_bit.
Robert ABEL4b613e92015-02-27 16:56:55 +0100419 * @ma:x Maximum parameter value (before optional @shift).
420 * If 0, maximum is as high as @st_bit and @end_bit allow.
Robert ABEL563dbb22015-02-27 16:56:51 +0100421 * @name: DTS node name, w/o "gpmc,"
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100422 * @cd: Clock Domain of timing parameter.
423 * @shift: Parameter value left shifts @shift, which is then printed instead of value.
Robert ABEL563dbb22015-02-27 16:56:51 +0100424 * @raw: Raw Format Option.
425 * raw format: gpmc,name = <value>
426 * tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
427 * Where x ns -- y ns result in the same tick value.
Robert ABEL4b613e92015-02-27 16:56:55 +0100428 * When @max is exceeded, "invalid" is printed inside comment.
Robert ABEL563dbb22015-02-27 16:56:51 +0100429 * @noval: Parameter values equal to 0 are not printed.
Robert ABEL563dbb22015-02-27 16:56:51 +0100430 * @return: Specified timing parameter (after optional @shift).
431 *
432 */
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100433static int get_gpmc_timing_reg(
434 /* timing specifiers */
Robert ABEL4b613e92015-02-27 16:56:55 +0100435 int cs, int reg, int st_bit, int end_bit, int max,
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100436 const char *name, const enum gpmc_clk_domain cd,
437 /* value transform */
438 int shift,
439 /* format specifiers */
440 bool raw, bool noval)
Tony Lindgren35ac0512014-11-03 17:45:01 -0800441{
442 u32 l;
Robert ABEL563dbb22015-02-27 16:56:51 +0100443 int nr_bits;
444 int mask;
Robert ABEL4b613e92015-02-27 16:56:55 +0100445 bool invalid;
Tony Lindgren35ac0512014-11-03 17:45:01 -0800446
447 l = gpmc_cs_read_reg(cs, reg);
448 nr_bits = end_bit - st_bit + 1;
Robert ABEL563dbb22015-02-27 16:56:51 +0100449 mask = (1 << nr_bits) - 1;
450 l = (l >> st_bit) & mask;
Robert ABEL4b613e92015-02-27 16:56:55 +0100451 if (!max)
452 max = mask;
453 invalid = l > max;
Tony Lindgren35ac0512014-11-03 17:45:01 -0800454 if (shift)
455 l = (shift << l);
456 if (noval && (l == 0))
457 return 0;
458 if (!raw) {
Robert ABEL563dbb22015-02-27 16:56:51 +0100459 /* DTS tick format for timings in ns */
460 unsigned int time_ns;
461 unsigned int time_ns_min = 0;
Tony Lindgren35ac0512014-11-03 17:45:01 -0800462
Robert ABEL563dbb22015-02-27 16:56:51 +0100463 if (l)
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100464 time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
465 time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
Robert ABEL4b613e92015-02-27 16:56:55 +0100466 pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n",
467 name, time_ns, time_ns_min, time_ns, l,
468 invalid ? "; invalid " : " ");
Tony Lindgren35ac0512014-11-03 17:45:01 -0800469 } else {
Robert ABEL563dbb22015-02-27 16:56:51 +0100470 /* raw format */
Robert ABEL4b613e92015-02-27 16:56:55 +0100471 pr_info("gpmc,%s = <%u>%s\n", name, l,
472 invalid ? " /* invalid */" : "");
Tony Lindgren35ac0512014-11-03 17:45:01 -0800473 }
474
475 return l;
476}
477
478#define GPMC_PRINT_CONFIG(cs, config) \
479 pr_info("cs%i %s: 0x%08x\n", cs, #config, \
480 gpmc_cs_read_reg(cs, config))
481#define GPMC_GET_RAW(reg, st, end, field) \
Robert ABEL4b613e92015-02-27 16:56:55 +0100482 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
483#define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
484 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
Tony Lindgren35ac0512014-11-03 17:45:01 -0800485#define GPMC_GET_RAW_BOOL(reg, st, end, field) \
Robert ABEL4b613e92015-02-27 16:56:55 +0100486 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
487#define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
488 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
Tony Lindgren35ac0512014-11-03 17:45:01 -0800489#define GPMC_GET_TICKS(reg, st, end, field) \
Robert ABEL4b613e92015-02-27 16:56:55 +0100490 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100491#define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
Robert ABEL4b613e92015-02-27 16:56:55 +0100492 get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
493#define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
494 get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
Tony Lindgren35ac0512014-11-03 17:45:01 -0800495
496static void gpmc_show_regs(int cs, const char *desc)
497{
498 pr_info("gpmc cs%i %s:\n", cs, desc);
499 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
500 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
501 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
502 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
503 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
504 GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
505}
506
507/*
508 * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
509 * see commit c9fb809.
510 */
511static void gpmc_cs_show_timings(int cs, const char *desc)
512{
513 gpmc_show_regs(cs, desc);
514
515 pr_info("gpmc cs%i access configuration:\n", cs);
516 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
517 GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
Robert ABEL4b613e92015-02-27 16:56:55 +0100518 GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13,
519 GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
Tony Lindgren35ac0512014-11-03 17:45:01 -0800520 GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
521 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
522 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
Robert ABEL4b613e92015-02-27 16:56:55 +0100523 GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
524 GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
525 "burst-length");
Tony Lindgren35ac0512014-11-03 17:45:01 -0800526 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
527 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
528 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
529 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
530 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
531
532 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
533
534 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
535
536 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
537 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
538
539 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
540 GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
541
542 pr_info("gpmc cs%i timings configuration:\n", cs);
543 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
544 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
545 GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
546
547 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
548 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
549 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
Neil Armstrong2c92c042015-12-28 14:39:20 +0100550 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
551 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
552 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
553 "adv-aad-mux-rd-off-ns");
554 GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
555 "adv-aad-mux-wr-off-ns");
556 }
Tony Lindgren35ac0512014-11-03 17:45:01 -0800557
558 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
559 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
Neil Armstrong2c92c042015-12-28 14:39:20 +0100560 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
561 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns");
562 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
563 }
Tony Lindgren35ac0512014-11-03 17:45:01 -0800564 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
565 GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
566
567 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
568 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
569 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
570
571 GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
572
573 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
574 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
575
Robert ABEL4b613e92015-02-27 16:56:55 +0100576 GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
577 GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
578 "wait-monitoring-ns", GPMC_CD_CLK);
579 GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
580 GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
581 "clk-activation-ns", GPMC_CD_FCLK);
Tony Lindgren35ac0512014-11-03 17:45:01 -0800582
583 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
584 GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
585}
586#else
587static inline void gpmc_cs_show_timings(int cs, const char *desc)
588{
589}
590#endif
591
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100592/**
593 * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
594 * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
595 * prior to calling this function with @cd equal to GPMC_CD_CLK.
596 *
597 * @cs: Chip Select Region.
598 * @reg: GPMC_CS_CONFIGn register offset.
599 * @st_bit: Start Bit
600 * @end_bit: End Bit. Must be >= @st_bit.
Robert ABEL4b613e92015-02-27 16:56:55 +0100601 * @max: Maximum parameter value.
602 * If 0, maximum is as high as @st_bit and @end_bit allow.
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100603 * @time: Timing parameter in ns.
604 * @cd: Timing parameter clock domain.
605 * @name: Timing parameter name.
606 * @return: 0 on success, -1 on error.
607 */
Robert ABEL4b613e92015-02-27 16:56:55 +0100608static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100609 int time, enum gpmc_clk_domain cd, const char *name)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700610{
611 u32 l;
612 int ticks, mask, nr_bits;
613
614 if (time == 0)
615 ticks = 0;
616 else
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100617 ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700618 nr_bits = end_bit - st_bit + 1;
Roger Quadros80323742014-08-29 19:11:50 +0300619 mask = (1 << nr_bits) - 1;
620
Robert ABEL4b613e92015-02-27 16:56:55 +0100621 if (!max)
622 max = mask;
623
624 if (ticks > max) {
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100625 pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
Robert ABEL4b613e92015-02-27 16:56:55 +0100626 __func__, cs, name, time, ticks, max);
Roger Quadros80323742014-08-29 19:11:50 +0300627
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700628 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800629 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700630
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700631 l = gpmc_cs_read_reg(cs, reg);
Tony Lindgren63aa9452015-06-01 19:22:10 -0600632#ifdef CONFIG_OMAP_GPMC_DEBUG
Robert ABELf5850702015-02-27 16:56:52 +0100633 pr_info(
Robert ABEL2affc812015-02-27 16:56:50 +0100634 "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100635 cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
David Brownell1c22cc12006-12-06 17:13:55 -0800636 (l >> st_bit) & mask, time);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700637#endif
638 l &= ~(mask << st_bit);
639 l |= ticks << st_bit;
640 gpmc_cs_write_reg(cs, reg, l);
641
642 return 0;
643}
644
Robert ABEL4b613e92015-02-27 16:56:55 +0100645#define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \
646 if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
647 t->field, (cd), #field) < 0) \
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700648 return -1
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700649
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100650#define GPMC_SET_ONE(reg, st, end, field) \
Robert ABEL4b613e92015-02-27 16:56:55 +0100651 GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100652
Robert ABEL2e676902015-02-27 16:56:53 +0100653/**
654 * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
655 * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
656 * read --> don't sample bus too early
657 * write --> data is longer on bus
658 *
659 * Formula:
660 * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
661 * / waitmonitoring_ticks)
662 * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
663 * div <= 0 check.
664 *
665 * @wait_monitoring: WAITMONITORINGTIME in ns.
666 * @return: -1 on failure to scale, else proper divider > 0.
667 */
668static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
669{
670
671 int div = gpmc_ns_to_ticks(wait_monitoring);
672
673 div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
674 div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
675
676 if (div > 4)
677 return -1;
678 if (div <= 0)
679 div = 1;
680
681 return div;
682
683}
684
685/**
686 * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
687 * @sync_clk: GPMC_CLK period in ps.
688 * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
689 * Else, returns -1.
690 */
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530691int gpmc_calc_divider(unsigned int sync_clk)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700692{
Robert ABEL2e676902015-02-27 16:56:53 +0100693 int div = gpmc_ps_to_ticks(sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700694
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700695 if (div > 4)
696 return -1;
David Brownell1c22cc12006-12-06 17:13:55 -0800697 if (div <= 0)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700698 div = 1;
699
700 return div;
701}
702
Robert ABEL2e676902015-02-27 16:56:53 +0100703/**
704 * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
705 * @cs: Chip Select Region.
706 * @t: GPMC timing parameters.
707 * @s: GPMC timing settings.
708 * @return: 0 on success, -1 on error.
709 */
710int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
711 const struct gpmc_settings *s)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700712{
713 int div;
714 u32 l;
715
Afzal Mohammed1b47ca12012-08-19 18:29:45 +0530716 div = gpmc_calc_divider(t->sync_clk);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700717 if (div < 0)
Paul Walmsleya032d332012-08-03 09:21:10 -0600718 return div;
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700719
Robert ABEL2e676902015-02-27 16:56:53 +0100720 /*
721 * See if we need to change the divider for waitmonitoringtime.
722 *
723 * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
724 * pure asynchronous accesses, i.e. both read and write asynchronous.
725 * However, only do so if WAITMONITORINGTIME is actually used, i.e.
726 * either WAITREADMONITORING or WAITWRITEMONITORING is set.
727 *
728 * This statement must not change div to scale async WAITMONITORINGTIME
729 * to protect mixed synchronous and asynchronous accesses.
730 *
731 * We raise an error later if WAITMONITORINGTIME does not fit.
732 */
733 if (!s->sync_read && !s->sync_write &&
734 (s->wait_on_read || s->wait_on_write)
735 ) {
736
737 div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
738 if (div < 0) {
739 pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
740 __func__,
741 t->wait_monitoring
742 );
743 return -1;
744 }
745 }
746
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700747 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
748 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
749 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
750
751 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
752 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
753 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
Neil Armstrong2c92c042015-12-28 14:39:20 +0100754 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
755 GPMC_SET_ONE(GPMC_CS_CONFIG3, 4, 6, adv_aad_mux_on);
756 GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off);
757 GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off);
758 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700759
760 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
761 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
Neil Armstrong2c92c042015-12-28 14:39:20 +0100762 if (gpmc_capability & GPMC_HAS_MUX_AAD) {
763 GPMC_SET_ONE(GPMC_CS_CONFIG4, 4, 6, oe_aad_mux_on);
764 GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off);
765 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700766 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
767 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
768
769 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
770 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
771 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
772
773 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
774
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530775 GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
776 GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
777
Afzal Mohammedda496872012-09-23 17:28:25 -0600778 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300779 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
Afzal Mohammedda496872012-09-23 17:28:25 -0600780 if (gpmc_capability & GPMC_HAS_WR_ACCESS)
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300781 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
Syed Mohammed, Khasimcc26b3b2008-10-09 17:51:41 +0300782
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700783 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100784 l &= ~0x03;
785 l |= (div - 1);
786 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
787
Robert ABEL4b613e92015-02-27 16:56:55 +0100788 GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
789 GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
790 wait_monitoring, GPMC_CD_CLK);
791 GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
792 GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
793 clk_activation, GPMC_CD_FCLK);
Robert ABEL7f2e8c52015-02-27 16:56:54 +0100794
Tony Lindgren63aa9452015-06-01 19:22:10 -0600795#ifdef CONFIG_OMAP_GPMC_DEBUG
Robert ABELf5850702015-02-27 16:56:52 +0100796 pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
797 cs, (div * gpmc_get_fclk_period()) / 1000, div);
David Brownell1c22cc12006-12-06 17:13:55 -0800798#endif
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700799
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530800 gpmc_cs_bool_timings(cs, &t->bool_timings);
Tony Lindgren35ac0512014-11-03 17:45:01 -0800801 gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
Afzal Mohammed559d94b2012-05-28 17:51:37 +0530802
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700803 return 0;
804}
805
Roger Quadros4cf27d22014-08-29 19:11:53 +0300806static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -0700807{
Imre Deakf37e4582006-09-25 12:41:33 +0300808 u32 l;
809 u32 mask;
810
Jon Hunterc71f8e92013-03-06 12:00:10 -0600811 /*
812 * Ensure that base address is aligned on a
813 * boundary equal to or greater than size.
814 */
815 if (base & (size - 1))
816 return -EINVAL;
817
Semen Protsenko9c4f7572015-01-24 22:28:38 +0200818 base >>= GPMC_CHUNK_SHIFT;
Imre Deakf37e4582006-09-25 12:41:33 +0300819 mask = (1 << GPMC_SECTION_SHIFT) - size;
Semen Protsenko9c4f7572015-01-24 22:28:38 +0200820 mask >>= GPMC_CHUNK_SHIFT;
821 mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
822
Imre Deakf37e4582006-09-25 12:41:33 +0300823 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Semen Protsenko9c4f7572015-01-24 22:28:38 +0200824 l &= ~GPMC_CONFIG7_MASK;
825 l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
826 l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530827 l |= GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300828 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
Jon Hunterc71f8e92013-03-06 12:00:10 -0600829
830 return 0;
Imre Deakf37e4582006-09-25 12:41:33 +0300831}
832
Roger Quadros4cf27d22014-08-29 19:11:53 +0300833static void gpmc_cs_enable_mem(int cs)
834{
835 u32 l;
836
837 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
838 l |= GPMC_CONFIG7_CSVALID;
839 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
840}
841
Imre Deakf37e4582006-09-25 12:41:33 +0300842static void gpmc_cs_disable_mem(int cs)
843{
844 u32 l;
845
846 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530847 l &= ~GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300848 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
849}
850
851static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
852{
853 u32 l;
854 u32 mask;
855
856 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
857 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
858 mask = (l >> 8) & 0x0f;
859 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
860}
861
862static int gpmc_cs_mem_enabled(int cs)
863{
864 u32 l;
865
866 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +0530867 return l & GPMC_CONFIG7_CSVALID;
Imre Deakf37e4582006-09-25 12:41:33 +0300868}
869
Ezequiel Garciaf5d8eda2013-02-12 16:22:24 -0300870static void gpmc_cs_set_reserved(int cs, int reserved)
Imre Deakf37e4582006-09-25 12:41:33 +0300871{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800872 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
873
874 gpmc->flags |= GPMC_CS_RESERVED;
Imre Deakf37e4582006-09-25 12:41:33 +0300875}
876
Ezequiel Garciaae9d9082013-02-12 16:22:19 -0300877static bool gpmc_cs_reserved(int cs)
Imre Deakf37e4582006-09-25 12:41:33 +0300878{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800879 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
880
881 return gpmc->flags & GPMC_CS_RESERVED;
882}
883
884static void gpmc_cs_set_name(int cs, const char *name)
885{
886 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
887
888 gpmc->name = name;
889}
890
Semen Protsenko2e25b0e2015-01-24 22:28:39 +0200891static const char *gpmc_cs_get_name(int cs)
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800892{
893 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
894
895 return gpmc->name;
Imre Deakf37e4582006-09-25 12:41:33 +0300896}
897
898static unsigned long gpmc_mem_align(unsigned long size)
899{
900 int order;
901
902 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
903 order = GPMC_CHUNK_SHIFT - 1;
904 do {
905 size >>= 1;
906 order++;
907 } while (size);
908 size = 1 << order;
909 return size;
910}
911
912static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
913{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800914 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
915 struct resource *res = &gpmc->mem;
Imre Deakf37e4582006-09-25 12:41:33 +0300916 int r;
917
918 size = gpmc_mem_align(size);
919 spin_lock(&gpmc_mem_lock);
920 res->start = base;
921 res->end = base + size - 1;
922 r = request_resource(&gpmc_mem_root, res);
923 spin_unlock(&gpmc_mem_lock);
924
925 return r;
926}
927
Afzal Mohammedda496872012-09-23 17:28:25 -0600928static int gpmc_cs_delete_mem(int cs)
929{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800930 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
931 struct resource *res = &gpmc->mem;
Afzal Mohammedda496872012-09-23 17:28:25 -0600932 int r;
933
934 spin_lock(&gpmc_mem_lock);
Tony Lindgrenefe80722014-04-21 19:26:13 -0700935 r = release_resource(res);
Afzal Mohammedda496872012-09-23 17:28:25 -0600936 res->start = 0;
937 res->end = 0;
938 spin_unlock(&gpmc_mem_lock);
939
940 return r;
941}
942
Jon Huntercdd69282013-02-08 16:46:13 -0600943/**
944 * gpmc_cs_remap - remaps a chip-select physical base address
945 * @cs: chip-select to remap
946 * @base: physical base address to re-map chip-select to
947 *
948 * Re-maps a chip-select to a new physical base address specified by
949 * "base". Returns 0 on success and appropriate negative error code
950 * on failure.
951 */
952static int gpmc_cs_remap(int cs, u32 base)
953{
954 int ret;
955 u32 old_base, size;
956
Gupta Pekonf34f3712013-05-31 17:31:30 +0530957 if (cs > gpmc_cs_num) {
958 pr_err("%s: requested chip-select is disabled\n", __func__);
Jon Huntercdd69282013-02-08 16:46:13 -0600959 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530960 }
Tony Lindgrenfb677ef2014-04-21 19:26:13 -0700961
962 /*
963 * Make sure we ignore any device offsets from the GPMC partition
964 * allocated for the chip select and that the new base confirms
965 * to the GPMC 16MB minimum granularity.
966 */
967 base &= ~(SZ_16M - 1);
968
Jon Huntercdd69282013-02-08 16:46:13 -0600969 gpmc_cs_get_memconf(cs, &old_base, &size);
970 if (base == old_base)
971 return 0;
Roger Quadros4cf27d22014-08-29 19:11:53 +0300972
Jon Huntercdd69282013-02-08 16:46:13 -0600973 ret = gpmc_cs_delete_mem(cs);
974 if (ret < 0)
975 return ret;
Roger Quadros4cf27d22014-08-29 19:11:53 +0300976
Jon Huntercdd69282013-02-08 16:46:13 -0600977 ret = gpmc_cs_insert_mem(cs, base, size);
978 if (ret < 0)
979 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600980
Roger Quadros4cf27d22014-08-29 19:11:53 +0300981 ret = gpmc_cs_set_memconf(cs, base, size);
982
983 return ret;
Jon Huntercdd69282013-02-08 16:46:13 -0600984}
985
Imre Deakf37e4582006-09-25 12:41:33 +0300986int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
987{
Tony Lindgren9ed7a772014-11-03 17:45:01 -0800988 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
989 struct resource *res = &gpmc->mem;
Imre Deakf37e4582006-09-25 12:41:33 +0300990 int r = -1;
991
Gupta Pekonf34f3712013-05-31 17:31:30 +0530992 if (cs > gpmc_cs_num) {
993 pr_err("%s: requested chip-select is disabled\n", __func__);
Imre Deakf37e4582006-09-25 12:41:33 +0300994 return -ENODEV;
Gupta Pekonf34f3712013-05-31 17:31:30 +0530995 }
Imre Deakf37e4582006-09-25 12:41:33 +0300996 size = gpmc_mem_align(size);
997 if (size > (1 << GPMC_SECTION_SHIFT))
998 return -ENOMEM;
999
1000 spin_lock(&gpmc_mem_lock);
1001 if (gpmc_cs_reserved(cs)) {
1002 r = -EBUSY;
1003 goto out;
1004 }
1005 if (gpmc_cs_mem_enabled(cs))
1006 r = adjust_resource(res, res->start & ~(size - 1), size);
1007 if (r < 0)
1008 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
1009 size, NULL, NULL);
1010 if (r < 0)
1011 goto out;
1012
Roger Quadros4cf27d22014-08-29 19:11:53 +03001013 /* Disable CS while changing base address and size mask */
1014 gpmc_cs_disable_mem(cs);
1015
1016 r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
Jon Hunterc71f8e92013-03-06 12:00:10 -06001017 if (r < 0) {
1018 release_resource(res);
1019 goto out;
1020 }
1021
Roger Quadros4cf27d22014-08-29 19:11:53 +03001022 /* Enable CS */
1023 gpmc_cs_enable_mem(cs);
Imre Deakf37e4582006-09-25 12:41:33 +03001024 *base = res->start;
1025 gpmc_cs_set_reserved(cs, 1);
1026out:
1027 spin_unlock(&gpmc_mem_lock);
1028 return r;
1029}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001030EXPORT_SYMBOL(gpmc_cs_request);
Imre Deakf37e4582006-09-25 12:41:33 +03001031
1032void gpmc_cs_free(int cs)
1033{
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001034 struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
1035 struct resource *res = &gpmc->mem;
Tony Lindgrenefe80722014-04-21 19:26:13 -07001036
Imre Deakf37e4582006-09-25 12:41:33 +03001037 spin_lock(&gpmc_mem_lock);
Gupta Pekonf34f3712013-05-31 17:31:30 +05301038 if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
Imre Deakf37e4582006-09-25 12:41:33 +03001039 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
1040 BUG();
1041 spin_unlock(&gpmc_mem_lock);
1042 return;
1043 }
1044 gpmc_cs_disable_mem(cs);
Tony Lindgrenefe80722014-04-21 19:26:13 -07001045 if (res->flags)
1046 release_resource(res);
Imre Deakf37e4582006-09-25 12:41:33 +03001047 gpmc_cs_set_reserved(cs, 0);
1048 spin_unlock(&gpmc_mem_lock);
1049}
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03001050EXPORT_SYMBOL(gpmc_cs_free);
Imre Deakf37e4582006-09-25 12:41:33 +03001051
vimal singh59e9c5a2009-07-13 16:26:24 +05301052/**
Jon Hunter3a544352013-02-21 13:00:21 -06001053 * gpmc_configure - write request to configure gpmc
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001054 * @cmd: command type
1055 * @wval: value to write
1056 * @return status of the operation
1057 */
Jon Hunter3a544352013-02-21 13:00:21 -06001058int gpmc_configure(int cmd, int wval)
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001059{
Jon Hunter3a544352013-02-21 13:00:21 -06001060 u32 regval;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001061
1062 switch (cmd) {
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001063 case GPMC_CONFIG_WP:
1064 regval = gpmc_read_reg(GPMC_CONFIG);
1065 if (wval)
1066 regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
1067 else
1068 regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
1069 gpmc_write_reg(GPMC_CONFIG, regval);
1070 break;
1071
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001072 default:
Jon Hunter3a544352013-02-21 13:00:21 -06001073 pr_err("%s: command not supported\n", __func__);
1074 return -EINVAL;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001075 }
1076
Jon Hunter3a544352013-02-21 13:00:21 -06001077 return 0;
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001078}
Jon Hunter3a544352013-02-21 13:00:21 -06001079EXPORT_SYMBOL(gpmc_configure);
Sukumar Ghorai948d38e2010-07-09 09:14:44 +00001080
Afzal Mohammed52bd1382012-08-30 12:53:22 -07001081void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
1082{
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +05301083 int i;
1084
Afzal Mohammed52bd1382012-08-30 12:53:22 -07001085 reg->gpmc_status = gpmc_base + GPMC_STATUS;
1086 reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
1087 GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
1088 reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
1089 GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
1090 reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
1091 GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
1092 reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
1093 reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
1094 reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
1095 reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
1096 reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
1097 reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
1098 reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
1099 reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +05301100
1101 for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
1102 reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
1103 GPMC_BCH_SIZE * i;
1104 reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
1105 GPMC_BCH_SIZE * i;
1106 reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
1107 GPMC_BCH_SIZE * i;
1108 reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
1109 GPMC_BCH_SIZE * i;
pekon gupta27c9fd62014-05-19 13:24:39 +05301110 reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
1111 i * GPMC_BCH_SIZE;
1112 reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
1113 i * GPMC_BCH_SIZE;
1114 reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
1115 i * GPMC_BCH_SIZE;
Afzal Mohammed2fdf0c92012-10-04 15:49:04 +05301116 }
Afzal Mohammed52bd1382012-08-30 12:53:22 -07001117}
1118
Roger Quadros512d73d2015-08-05 13:34:50 +03001119static bool gpmc_nand_writebuffer_empty(void)
1120{
1121 if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
1122 return true;
1123
1124 return false;
1125}
1126
1127static struct gpmc_nand_ops nand_ops = {
1128 .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
1129};
Roger Quadrosf47fcad2015-08-05 13:58:01 +03001130
1131/**
1132 * gpmc_omap_get_nand_ops - Get the GPMC NAND interface
1133 * @regs: the GPMC NAND register map exclusive for NAND use.
1134 * @cs: GPMC chip select number on which the NAND sits. The
1135 * register map returned will be specific to this chip select.
1136 *
1137 * Returns NULL on error e.g. invalid cs.
1138 */
1139struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
1140{
1141 if (cs >= gpmc_cs_num)
1142 return NULL;
1143
1144 gpmc_update_nand_reg(reg, cs);
1145
1146 return &nand_ops;
1147}
1148EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
1149
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001150int gpmc_get_client_irq(unsigned irq_config)
1151{
Roger Quadros384258f2015-07-30 14:49:23 +03001152 if (!gpmc_irq_domain) {
1153 pr_warn("%s called before GPMC IRQ domain available\n",
1154 __func__);
1155 return 0;
1156 }
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001157
Roger Quadros384258f2015-07-30 14:49:23 +03001158 if (irq_config >= GPMC_NR_IRQ)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001159 return 0;
1160
Roger Quadros384258f2015-07-30 14:49:23 +03001161 return irq_create_mapping(gpmc_irq_domain, irq_config);
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001162}
1163
Roger Quadros384258f2015-07-30 14:49:23 +03001164static int gpmc_irq_endis(unsigned long hwirq, bool endis)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001165{
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001166 u32 regval;
1167
Roger Quadros384258f2015-07-30 14:49:23 +03001168 regval = gpmc_read_reg(GPMC_IRQENABLE);
1169 if (endis)
1170 regval |= BIT(hwirq);
1171 else
1172 regval &= ~BIT(hwirq);
1173 gpmc_write_reg(GPMC_IRQENABLE, regval);
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001174
1175 return 0;
1176}
1177
1178static void gpmc_irq_disable(struct irq_data *p)
1179{
Roger Quadros384258f2015-07-30 14:49:23 +03001180 gpmc_irq_endis(p->hwirq, false);
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001181}
1182
1183static void gpmc_irq_enable(struct irq_data *p)
1184{
Roger Quadros384258f2015-07-30 14:49:23 +03001185 gpmc_irq_endis(p->hwirq, true);
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001186}
1187
1188static void gpmc_irq_noop(struct irq_data *data) { }
1189
1190static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
1191
Roger Quadros384258f2015-07-30 14:49:23 +03001192static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
1193 irq_hw_number_t hw)
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001194{
Roger Quadros384258f2015-07-30 14:49:23 +03001195 struct gpmc_device *gpmc = d->host_data;
1196
1197 irq_set_chip_data(virq, gpmc);
1198 irq_set_chip_and_handler(virq, &gpmc->irq_chip, handle_simple_irq);
1199 irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
1200
1201 return 0;
1202}
1203
1204static const struct irq_domain_ops gpmc_irq_domain_ops = {
1205 .map = gpmc_irq_map,
1206 .xlate = irq_domain_xlate_twocell,
1207};
1208
1209static irqreturn_t gpmc_handle_irq(int irq, void *data)
1210{
1211 int hwirq, virq;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001212 u32 regval;
Roger Quadros384258f2015-07-30 14:49:23 +03001213 struct gpmc_device *gpmc = data;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001214
Roger Quadros384258f2015-07-30 14:49:23 +03001215 regval = gpmc_read_reg(GPMC_IRQSTATUS);
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001216
Roger Quadros384258f2015-07-30 14:49:23 +03001217 if (!regval)
1218 return IRQ_NONE;
1219
1220 for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++) {
1221 if (regval & BIT(hwirq)) {
1222 virq = irq_find_mapping(gpmc_irq_domain, hwirq);
1223 if (!virq) {
1224 dev_warn(gpmc->dev,
1225 "spurious irq detected hwirq %d, virq %d\n",
1226 hwirq, virq);
1227 }
1228
1229 generic_handle_irq(virq);
1230 }
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001231 }
1232
Roger Quadros384258f2015-07-30 14:49:23 +03001233 gpmc_write_reg(GPMC_IRQSTATUS, regval);
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001234
Roger Quadros384258f2015-07-30 14:49:23 +03001235 return IRQ_HANDLED;
1236}
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001237
Roger Quadros384258f2015-07-30 14:49:23 +03001238static int gpmc_setup_irq(struct gpmc_device *gpmc)
1239{
1240 u32 regval;
1241 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001242
1243 /* Disable interrupts */
1244 gpmc_write_reg(GPMC_IRQENABLE, 0);
1245
1246 /* clear interrupts */
1247 regval = gpmc_read_reg(GPMC_IRQSTATUS);
1248 gpmc_write_reg(GPMC_IRQSTATUS, regval);
1249
Roger Quadros384258f2015-07-30 14:49:23 +03001250 gpmc->irq_chip.name = "gpmc";
1251 gpmc->irq_chip.irq_startup = gpmc_irq_noop_ret;
1252 gpmc->irq_chip.irq_enable = gpmc_irq_enable;
1253 gpmc->irq_chip.irq_disable = gpmc_irq_disable;
1254 gpmc->irq_chip.irq_shutdown = gpmc_irq_noop;
1255 gpmc->irq_chip.irq_ack = gpmc_irq_noop;
1256 gpmc->irq_chip.irq_mask = gpmc_irq_noop;
1257 gpmc->irq_chip.irq_unmask = gpmc_irq_noop;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07001258
Roger Quadros384258f2015-07-30 14:49:23 +03001259 gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
1260 GPMC_NR_IRQ,
1261 &gpmc_irq_domain_ops,
1262 gpmc);
1263 if (!gpmc_irq_domain) {
1264 dev_err(gpmc->dev, "IRQ domain add failed\n");
1265 return -ENODEV;
Afzal Mohammedda496872012-09-23 17:28:25 -06001266 }
1267
Roger Quadros384258f2015-07-30 14:49:23 +03001268 rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
1269 if (rc) {
1270 dev_err(gpmc->dev, "failed to request irq %d: %d\n",
1271 gpmc->irq, rc);
1272 irq_domain_remove(gpmc_irq_domain);
1273 gpmc_irq_domain = NULL;
1274 }
1275
1276 return rc;
1277}
1278
1279static int gpmc_free_irq(struct gpmc_device *gpmc)
1280{
1281 int hwirq;
1282
1283 free_irq(gpmc->irq, gpmc);
1284
1285 for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++)
1286 irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
1287
1288 irq_domain_remove(gpmc_irq_domain);
1289 gpmc_irq_domain = NULL;
Afzal Mohammedda496872012-09-23 17:28:25 -06001290
1291 return 0;
1292}
1293
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08001294static void gpmc_mem_exit(void)
Afzal Mohammedda496872012-09-23 17:28:25 -06001295{
1296 int cs;
1297
Gupta Pekonf34f3712013-05-31 17:31:30 +05301298 for (cs = 0; cs < gpmc_cs_num; cs++) {
Afzal Mohammedda496872012-09-23 17:28:25 -06001299 if (!gpmc_cs_mem_enabled(cs))
1300 continue;
1301 gpmc_cs_delete_mem(cs);
1302 }
1303
1304}
1305
Jon Hunter84b00f02013-03-06 14:36:47 -06001306static void gpmc_mem_init(void)
Imre Deakf37e4582006-09-25 12:41:33 +03001307{
Jon Hunter84b00f02013-03-06 14:36:47 -06001308 int cs;
Imre Deakf37e4582006-09-25 12:41:33 +03001309
Roger Quadrosbdd7e032015-07-09 17:31:45 +03001310 gpmc_mem_root.start = GPMC_MEM_START;
Imre Deakf37e4582006-09-25 12:41:33 +03001311 gpmc_mem_root.end = GPMC_MEM_END;
1312
1313 /* Reserve all regions that has been set up by bootloader */
Gupta Pekonf34f3712013-05-31 17:31:30 +05301314 for (cs = 0; cs < gpmc_cs_num; cs++) {
Imre Deakf37e4582006-09-25 12:41:33 +03001315 u32 base, size;
1316
1317 if (!gpmc_cs_mem_enabled(cs))
1318 continue;
1319 gpmc_cs_get_memconf(cs, &base, &size);
Jon Hunter84b00f02013-03-06 14:36:47 -06001320 if (gpmc_cs_insert_mem(cs, base, size)) {
1321 pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
1322 __func__, cs, base, base + size);
1323 gpmc_cs_disable_mem(cs);
Jon Hunter81190242012-10-17 09:41:25 -05001324 }
Imre Deakf37e4582006-09-25 12:41:33 +03001325 }
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07001326}
1327
Afzal Mohammed246da262012-08-02 20:02:10 +05301328static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
1329{
1330 u32 temp;
1331 int div;
1332
1333 div = gpmc_calc_divider(sync_clk);
1334 temp = gpmc_ps_to_ticks(time_ps);
1335 temp = (temp + div - 1) / div;
1336 return gpmc_ticks_to_ps(temp * div);
1337}
1338
1339/* XXX: can the cycles be avoided ? */
1340static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001341 struct gpmc_device_timings *dev_t,
1342 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301343{
Afzal Mohammed246da262012-08-02 20:02:10 +05301344 u32 temp;
1345
1346 /* adv_rd_off */
1347 temp = dev_t->t_avdp_r;
1348 /* XXX: mux check required ? */
1349 if (mux) {
1350 /* XXX: t_avdp not to be required for sync, only added for tusb
1351 * this indirectly necessitates requirement of t_avdp_r and
1352 * t_avdp_w instead of having a single t_avdp
1353 */
1354 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
1355 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1356 }
1357 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1358
1359 /* oe_on */
1360 temp = dev_t->t_oeasu; /* XXX: remove this ? */
1361 if (mux) {
1362 temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
1363 temp = max_t(u32, temp, gpmc_t->adv_rd_off +
1364 gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
1365 }
1366 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1367
1368 /* access */
1369 /* XXX: any scope for improvement ?, by combining oe_on
1370 * and clk_activation, need to check whether
1371 * access = clk_activation + round to sync clk ?
1372 */
1373 temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
1374 temp += gpmc_t->clk_activation;
1375 if (dev_t->cyc_oe)
1376 temp = max_t(u32, temp, gpmc_t->oe_on +
1377 gpmc_ticks_to_ps(dev_t->cyc_oe));
1378 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1379
1380 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1381 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1382
1383 /* rd_cycle */
1384 temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
1385 temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
1386 gpmc_t->access;
1387 /* XXX: barter t_ce_rdyz with t_cez_r ? */
1388 if (dev_t->t_ce_rdyz)
1389 temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
1390 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1391
1392 return 0;
1393}
1394
1395static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001396 struct gpmc_device_timings *dev_t,
1397 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301398{
Afzal Mohammed246da262012-08-02 20:02:10 +05301399 u32 temp;
1400
1401 /* adv_wr_off */
1402 temp = dev_t->t_avdp_w;
1403 if (mux) {
1404 temp = max_t(u32, temp,
1405 gpmc_t->clk_activation + dev_t->t_avdh);
1406 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1407 }
1408 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1409
1410 /* wr_data_mux_bus */
1411 temp = max_t(u32, dev_t->t_weasu,
1412 gpmc_t->clk_activation + dev_t->t_rdyo);
1413 /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
1414 * and in that case remember to handle we_on properly
1415 */
1416 if (mux) {
1417 temp = max_t(u32, temp,
1418 gpmc_t->adv_wr_off + dev_t->t_aavdh);
1419 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1420 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1421 }
1422 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1423
1424 /* we_on */
1425 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1426 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1427 else
1428 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1429
1430 /* wr_access */
1431 /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
1432 gpmc_t->wr_access = gpmc_t->access;
1433
1434 /* we_off */
1435 temp = gpmc_t->we_on + dev_t->t_wpl;
1436 temp = max_t(u32, temp,
1437 gpmc_t->wr_access + gpmc_ticks_to_ps(1));
1438 temp = max_t(u32, temp,
1439 gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
1440 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1441
1442 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1443 dev_t->t_wph);
1444
1445 /* wr_cycle */
1446 temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
1447 temp += gpmc_t->wr_access;
1448 /* XXX: barter t_ce_rdyz with t_cez_w ? */
1449 if (dev_t->t_ce_rdyz)
1450 temp = max_t(u32, temp,
1451 gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
1452 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1453
1454 return 0;
1455}
1456
1457static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001458 struct gpmc_device_timings *dev_t,
1459 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301460{
Afzal Mohammed246da262012-08-02 20:02:10 +05301461 u32 temp;
1462
1463 /* adv_rd_off */
1464 temp = dev_t->t_avdp_r;
1465 if (mux)
1466 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1467 gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
1468
1469 /* oe_on */
1470 temp = dev_t->t_oeasu;
1471 if (mux)
1472 temp = max_t(u32, temp,
1473 gpmc_t->adv_rd_off + dev_t->t_aavdh);
1474 gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
1475
1476 /* access */
1477 temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
1478 gpmc_t->oe_on + dev_t->t_oe);
1479 temp = max_t(u32, temp,
1480 gpmc_t->cs_on + dev_t->t_ce);
1481 temp = max_t(u32, temp,
1482 gpmc_t->adv_on + dev_t->t_aa);
1483 gpmc_t->access = gpmc_round_ps_to_ticks(temp);
1484
1485 gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
1486 gpmc_t->cs_rd_off = gpmc_t->oe_off;
1487
1488 /* rd_cycle */
1489 temp = max_t(u32, dev_t->t_rd_cycle,
1490 gpmc_t->cs_rd_off + dev_t->t_cez_r);
1491 temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
1492 gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
1493
1494 return 0;
1495}
1496
1497static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001498 struct gpmc_device_timings *dev_t,
1499 bool mux)
Afzal Mohammed246da262012-08-02 20:02:10 +05301500{
Afzal Mohammed246da262012-08-02 20:02:10 +05301501 u32 temp;
1502
1503 /* adv_wr_off */
1504 temp = dev_t->t_avdp_w;
1505 if (mux)
1506 temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
1507 gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
1508
1509 /* wr_data_mux_bus */
1510 temp = dev_t->t_weasu;
1511 if (mux) {
1512 temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
1513 temp = max_t(u32, temp, gpmc_t->adv_wr_off +
1514 gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
1515 }
1516 gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
1517
1518 /* we_on */
1519 if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
1520 gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
1521 else
1522 gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
1523
1524 /* we_off */
1525 temp = gpmc_t->we_on + dev_t->t_wpl;
1526 gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
1527
1528 gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
1529 dev_t->t_wph);
1530
1531 /* wr_cycle */
1532 temp = max_t(u32, dev_t->t_wr_cycle,
1533 gpmc_t->cs_wr_off + dev_t->t_cez_w);
1534 gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
1535
1536 return 0;
1537}
1538
1539static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
1540 struct gpmc_device_timings *dev_t)
1541{
1542 u32 temp;
1543
1544 gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
1545 gpmc_get_fclk_period();
1546
1547 gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
1548 dev_t->t_bacc,
1549 gpmc_t->sync_clk);
1550
1551 temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
1552 gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
1553
1554 if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
1555 return 0;
1556
1557 if (dev_t->ce_xdelay)
1558 gpmc_t->bool_timings.cs_extra_delay = true;
1559 if (dev_t->avd_xdelay)
1560 gpmc_t->bool_timings.adv_extra_delay = true;
1561 if (dev_t->oe_xdelay)
1562 gpmc_t->bool_timings.oe_extra_delay = true;
1563 if (dev_t->we_xdelay)
1564 gpmc_t->bool_timings.we_extra_delay = true;
1565
1566 return 0;
1567}
1568
1569static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001570 struct gpmc_device_timings *dev_t,
1571 bool sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301572{
1573 u32 temp;
1574
1575 /* cs_on */
1576 gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
1577
1578 /* adv_on */
1579 temp = dev_t->t_avdasu;
1580 if (dev_t->t_ce_avd)
1581 temp = max_t(u32, temp,
1582 gpmc_t->cs_on + dev_t->t_ce_avd);
1583 gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
1584
Jon Hunterc3be5b42013-02-21 13:46:22 -06001585 if (sync)
Afzal Mohammed246da262012-08-02 20:02:10 +05301586 gpmc_calc_sync_common_timings(gpmc_t, dev_t);
1587
1588 return 0;
1589}
1590
1591/* TODO: remove this function once all peripherals are confirmed to
1592 * work with generic timing. Simultaneously gpmc_cs_set_timings()
1593 * has to be modified to handle timings in ps instead of ns
1594*/
1595static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
1596{
1597 t->cs_on /= 1000;
1598 t->cs_rd_off /= 1000;
1599 t->cs_wr_off /= 1000;
1600 t->adv_on /= 1000;
1601 t->adv_rd_off /= 1000;
1602 t->adv_wr_off /= 1000;
1603 t->we_on /= 1000;
1604 t->we_off /= 1000;
1605 t->oe_on /= 1000;
1606 t->oe_off /= 1000;
1607 t->page_burst_access /= 1000;
1608 t->access /= 1000;
1609 t->rd_cycle /= 1000;
1610 t->wr_cycle /= 1000;
1611 t->bus_turnaround /= 1000;
1612 t->cycle2cycle_delay /= 1000;
1613 t->wait_monitoring /= 1000;
1614 t->clk_activation /= 1000;
1615 t->wr_access /= 1000;
1616 t->wr_data_mux_bus /= 1000;
1617}
1618
1619int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
Jon Hunterc3be5b42013-02-21 13:46:22 -06001620 struct gpmc_settings *gpmc_s,
1621 struct gpmc_device_timings *dev_t)
Afzal Mohammed246da262012-08-02 20:02:10 +05301622{
Jon Hunterc3be5b42013-02-21 13:46:22 -06001623 bool mux = false, sync = false;
1624
1625 if (gpmc_s) {
1626 mux = gpmc_s->mux_add_data ? true : false;
1627 sync = (gpmc_s->sync_read || gpmc_s->sync_write);
1628 }
1629
Afzal Mohammed246da262012-08-02 20:02:10 +05301630 memset(gpmc_t, 0, sizeof(*gpmc_t));
1631
Jon Hunterc3be5b42013-02-21 13:46:22 -06001632 gpmc_calc_common_timings(gpmc_t, dev_t, sync);
Afzal Mohammed246da262012-08-02 20:02:10 +05301633
Jon Hunterc3be5b42013-02-21 13:46:22 -06001634 if (gpmc_s && gpmc_s->sync_read)
1635 gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301636 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001637 gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301638
Jon Hunterc3be5b42013-02-21 13:46:22 -06001639 if (gpmc_s && gpmc_s->sync_write)
1640 gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301641 else
Jon Hunterc3be5b42013-02-21 13:46:22 -06001642 gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
Afzal Mohammed246da262012-08-02 20:02:10 +05301643
1644 /* TODO: remove, see function definition */
1645 gpmc_convert_ps_to_ns(gpmc_t);
1646
1647 return 0;
1648}
1649
Jon Hunteraa8d4762013-02-21 15:25:23 -06001650/**
1651 * gpmc_cs_program_settings - programs non-timing related settings
1652 * @cs: GPMC chip-select to program
1653 * @p: pointer to GPMC settings structure
1654 *
1655 * Programs non-timing related settings for a GPMC chip-select, such as
1656 * bus-width, burst configuration, etc. Function should be called once
1657 * for each chip-select that is being used and must be called before
1658 * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
1659 * register will be initialised to zero by this function. Returns 0 on
1660 * success and appropriate negative error code on failure.
1661 */
1662int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
1663{
1664 u32 config1;
1665
1666 if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
1667 pr_err("%s: invalid width %d!", __func__, p->device_width);
1668 return -EINVAL;
1669 }
1670
1671 /* Address-data multiplexing not supported for NAND devices */
1672 if (p->device_nand && p->mux_add_data) {
1673 pr_err("%s: invalid configuration!\n", __func__);
1674 return -EINVAL;
1675 }
1676
1677 if ((p->mux_add_data > GPMC_MUX_AD) ||
1678 ((p->mux_add_data == GPMC_MUX_AAD) &&
1679 !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
1680 pr_err("%s: invalid multiplex configuration!\n", __func__);
1681 return -EINVAL;
1682 }
1683
1684 /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
1685 if (p->burst_read || p->burst_write) {
1686 switch (p->burst_len) {
1687 case GPMC_BURST_4:
1688 case GPMC_BURST_8:
1689 case GPMC_BURST_16:
1690 break;
1691 default:
1692 pr_err("%s: invalid page/burst-length (%d)\n",
1693 __func__, p->burst_len);
1694 return -EINVAL;
1695 }
1696 }
1697
Roger Quadros2b540572014-09-02 16:57:06 +03001698 if (p->wait_pin > gpmc_nr_waitpins) {
Jon Hunteraa8d4762013-02-21 15:25:23 -06001699 pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
1700 return -EINVAL;
1701 }
1702
1703 config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
1704
1705 if (p->sync_read)
1706 config1 |= GPMC_CONFIG1_READTYPE_SYNC;
1707 if (p->sync_write)
1708 config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
1709 if (p->wait_on_read)
1710 config1 |= GPMC_CONFIG1_WAIT_READ_MON;
1711 if (p->wait_on_write)
1712 config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
1713 if (p->wait_on_read || p->wait_on_write)
1714 config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
1715 if (p->device_nand)
1716 config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
1717 if (p->mux_add_data)
1718 config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
1719 if (p->burst_read)
1720 config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
1721 if (p->burst_write)
1722 config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
1723 if (p->burst_read || p->burst_write) {
1724 config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
1725 config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
1726 }
1727
1728 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
1729
1730 return 0;
1731}
1732
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001733#ifdef CONFIG_OF
Uwe Kleine-König31957602014-09-10 10:26:17 +02001734static const struct of_device_id gpmc_dt_ids[] = {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001735 { .compatible = "ti,omap2420-gpmc" },
1736 { .compatible = "ti,omap2430-gpmc" },
1737 { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
1738 { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
1739 { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
1740 { }
1741};
1742MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
1743
Jon Hunter8c8a77712013-02-20 15:53:12 -06001744/**
1745 * gpmc_read_settings_dt - read gpmc settings from device-tree
1746 * @np: pointer to device-tree node for a gpmc child device
1747 * @p: pointer to gpmc settings structure
1748 *
1749 * Reads the GPMC settings for a GPMC child device from device-tree and
1750 * stores them in the GPMC settings structure passed. The GPMC settings
1751 * structure is initialised to zero by this function and so any
1752 * previously stored settings will be cleared.
1753 */
1754void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
1755{
1756 memset(p, 0, sizeof(struct gpmc_settings));
1757
1758 p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
1759 p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
Jon Hunter8c8a77712013-02-20 15:53:12 -06001760 of_property_read_u32(np, "gpmc,device-width", &p->device_width);
1761 of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
1762
1763 if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
1764 p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
1765 p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
1766 p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
1767 if (!p->burst_read && !p->burst_write)
1768 pr_warn("%s: page/burst-length set but not used!\n",
1769 __func__);
1770 }
1771
1772 if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
1773 p->wait_on_read = of_property_read_bool(np,
1774 "gpmc,wait-on-read");
1775 p->wait_on_write = of_property_read_bool(np,
1776 "gpmc,wait-on-write");
1777 if (!p->wait_on_read && !p->wait_on_write)
Roger Quadros2b540572014-09-02 16:57:06 +03001778 pr_debug("%s: rd/wr wait monitoring not enabled!\n",
1779 __func__);
Jon Hunter8c8a77712013-02-20 15:53:12 -06001780 }
1781}
1782
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001783static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
1784 struct gpmc_timings *gpmc_t)
1785{
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001786 struct gpmc_bool_timings *p;
1787
1788 if (!np || !gpmc_t)
1789 return;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001790
1791 memset(gpmc_t, 0, sizeof(*gpmc_t));
1792
1793 /* minimum clock period for syncronous mode */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001794 of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001795
1796 /* chip select timtings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001797 of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
1798 of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
1799 of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001800
1801 /* ADV signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001802 of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
1803 of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
1804 of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
Neil Armstrong2c92c042015-12-28 14:39:20 +01001805 of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
1806 &gpmc_t->adv_aad_mux_on);
1807 of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
1808 &gpmc_t->adv_aad_mux_rd_off);
1809 of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
1810 &gpmc_t->adv_aad_mux_wr_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001811
1812 /* WE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001813 of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
1814 of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001815
1816 /* OE signal timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001817 of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
1818 of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
Neil Armstrong2c92c042015-12-28 14:39:20 +01001819 of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
1820 &gpmc_t->oe_aad_mux_on);
1821 of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
1822 &gpmc_t->oe_aad_mux_off);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001823
1824 /* access and cycle timings */
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001825 of_property_read_u32(np, "gpmc,page-burst-access-ns",
1826 &gpmc_t->page_burst_access);
1827 of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
1828 of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
1829 of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
1830 of_property_read_u32(np, "gpmc,bus-turnaround-ns",
1831 &gpmc_t->bus_turnaround);
1832 of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
1833 &gpmc_t->cycle2cycle_delay);
1834 of_property_read_u32(np, "gpmc,wait-monitoring-ns",
1835 &gpmc_t->wait_monitoring);
1836 of_property_read_u32(np, "gpmc,clk-activation-ns",
1837 &gpmc_t->clk_activation);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001838
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001839 /* only applicable to OMAP3+ */
1840 of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
1841 of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
1842 &gpmc_t->wr_data_mux_bus);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001843
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001844 /* bool timing parameters */
1845 p = &gpmc_t->bool_timings;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001846
Jon Hunterd36b4cd2013-02-21 18:51:27 -06001847 p->cycle2cyclediffcsen =
1848 of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
1849 p->cycle2cyclesamecsen =
1850 of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
1851 p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
1852 p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
1853 p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
1854 p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
1855 p->time_para_granularity =
1856 of_property_read_bool(np, "gpmc,time-para-granularity");
Daniel Mackbc6b1e72012-12-14 11:36:44 +01001857}
1858
Pekon Gupta980386d2014-01-28 11:42:41 +05301859#if IS_ENABLED(CONFIG_MTD_ONENAND)
Ezequiel Garcia75d36252013-01-25 09:23:11 -03001860static int gpmc_probe_onenand_child(struct platform_device *pdev,
1861 struct device_node *child)
1862{
1863 u32 val;
1864 struct omap_onenand_platform_data *gpmc_onenand_data;
1865
1866 if (of_property_read_u32(child, "reg", &val) < 0) {
1867 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1868 child->full_name);
1869 return -ENODEV;
1870 }
1871
1872 gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
1873 GFP_KERNEL);
1874 if (!gpmc_onenand_data)
1875 return -ENOMEM;
1876
1877 gpmc_onenand_data->cs = val;
1878 gpmc_onenand_data->of_node = child;
1879 gpmc_onenand_data->dma_channel = -1;
1880
1881 if (!of_property_read_u32(child, "dma-channel", &val))
1882 gpmc_onenand_data->dma_channel = val;
1883
1884 gpmc_onenand_init(gpmc_onenand_data);
1885
1886 return 0;
1887}
1888#else
1889static int gpmc_probe_onenand_child(struct platform_device *pdev,
1890 struct device_node *child)
1891{
1892 return 0;
1893}
1894#endif
1895
Jon Huntercdd69282013-02-08 16:46:13 -06001896/**
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001897 * gpmc_probe_generic_child - configures the gpmc for a child device
Jon Huntercdd69282013-02-08 16:46:13 -06001898 * @pdev: pointer to gpmc platform device
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001899 * @child: pointer to device-tree node for child device
Jon Huntercdd69282013-02-08 16:46:13 -06001900 *
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001901 * Allocates and configures a GPMC chip-select for a child device.
Jon Huntercdd69282013-02-08 16:46:13 -06001902 * Returns 0 on success and appropriate negative error code on failure.
1903 */
Javier Martinez Canillas3af91cf2013-03-14 16:09:21 +01001904static int gpmc_probe_generic_child(struct platform_device *pdev,
Jon Huntercdd69282013-02-08 16:46:13 -06001905 struct device_node *child)
1906{
1907 struct gpmc_settings gpmc_s;
1908 struct gpmc_timings gpmc_t;
1909 struct resource res;
1910 unsigned long base;
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001911 const char *name;
Jon Huntercdd69282013-02-08 16:46:13 -06001912 int ret, cs;
Roger Quadrose378d222014-08-29 19:11:52 +03001913 u32 val;
Jon Huntercdd69282013-02-08 16:46:13 -06001914
1915 if (of_property_read_u32(child, "reg", &cs) < 0) {
1916 dev_err(&pdev->dev, "%s has no 'reg' property\n",
1917 child->full_name);
1918 return -ENODEV;
1919 }
1920
1921 if (of_address_to_resource(child, 0, &res) < 0) {
1922 dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
1923 child->full_name);
1924 return -ENODEV;
1925 }
1926
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001927 /*
1928 * Check if we have multiple instances of the same device
1929 * on a single chip select. If so, use the already initialized
1930 * timings.
1931 */
1932 name = gpmc_cs_get_name(cs);
1933 if (name && child->name && of_node_cmp(child->name, name) == 0)
1934 goto no_timings;
1935
Jon Huntercdd69282013-02-08 16:46:13 -06001936 ret = gpmc_cs_request(cs, resource_size(&res), &base);
1937 if (ret < 0) {
1938 dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
1939 return ret;
1940 }
Tony Lindgren9ed7a772014-11-03 17:45:01 -08001941 gpmc_cs_set_name(cs, child->name);
Jon Huntercdd69282013-02-08 16:46:13 -06001942
Tony Lindgren35ac0512014-11-03 17:45:01 -08001943 gpmc_read_settings_dt(child, &gpmc_s);
1944 gpmc_read_timings_dt(child, &gpmc_t);
Jon Huntercdd69282013-02-08 16:46:13 -06001945
1946 /*
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001947 * For some GPMC devices we still need to rely on the bootloader
Tony Lindgren35ac0512014-11-03 17:45:01 -08001948 * timings because the devices can be connected via FPGA.
1949 * REVISIT: Add timing support from slls644g.pdf.
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001950 */
Tony Lindgren35ac0512014-11-03 17:45:01 -08001951 if (!gpmc_t.cs_rd_off) {
1952 WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
1953 cs);
1954 gpmc_cs_show_timings(cs,
1955 "please add GPMC bootloader timings to .dts");
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001956 goto no_timings;
1957 }
1958
Roger Quadros4cf27d22014-08-29 19:11:53 +03001959 /* CS must be disabled while making changes to gpmc configuration */
1960 gpmc_cs_disable_mem(cs);
1961
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08001962 /*
Jon Huntercdd69282013-02-08 16:46:13 -06001963 * FIXME: gpmc_cs_request() will map the CS to an arbitary
1964 * location in the gpmc address space. When booting with
1965 * device-tree we want the NOR flash to be mapped to the
1966 * location specified in the device-tree blob. So remap the
1967 * CS to this location. Once DT migration is complete should
1968 * just make gpmc_cs_request() map a specific address.
1969 */
1970 ret = gpmc_cs_remap(cs, res.start);
1971 if (ret < 0) {
Fabio Estevamf70bf2a2013-09-18 12:01:59 -07001972 dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
1973 cs, &res.start);
Roger Quadrosbdd7e032015-07-09 17:31:45 +03001974 if (res.start < GPMC_MEM_START) {
1975 dev_info(&pdev->dev,
1976 "GPMC CS %d start cannot be lesser than 0x%x\n",
1977 cs, GPMC_MEM_START);
1978 } else if (res.end > GPMC_MEM_END) {
1979 dev_info(&pdev->dev,
1980 "GPMC CS %d end cannot be greater than 0x%x\n",
1981 cs, GPMC_MEM_END);
1982 }
Jon Huntercdd69282013-02-08 16:46:13 -06001983 goto err;
1984 }
1985
Roger Quadrosc9711ec2014-05-21 07:29:03 +03001986 if (of_node_cmp(child->name, "nand") == 0) {
1987 /* Warn about older DT blobs with no compatible property */
1988 if (!of_property_read_bool(child, "compatible")) {
1989 dev_warn(&pdev->dev,
1990 "Incompatible NAND node: missing compatible");
1991 ret = -EINVAL;
1992 goto err;
1993 }
1994 }
1995
1996 if (of_device_is_compatible(child, "ti,omap2-nand")) {
1997 /* NAND specific setup */
1998 val = of_get_nand_bus_width(child);
1999 switch (val) {
2000 case 8:
2001 gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
2002 break;
2003 case 16:
2004 gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
2005 break;
2006 default:
2007 dev_err(&pdev->dev, "%s: invalid 'nand-bus-width'\n",
2008 child->name);
2009 ret = -EINVAL;
2010 goto err;
2011 }
2012
2013 /* disable write protect */
2014 gpmc_configure(GPMC_CONFIG_WP, 0);
2015 gpmc_s.device_nand = true;
2016 } else {
2017 ret = of_property_read_u32(child, "bank-width",
2018 &gpmc_s.device_width);
2019 if (ret < 0)
2020 goto err;
2021 }
Jon Huntercdd69282013-02-08 16:46:13 -06002022
Uwe Kleine-Königfd820a12015-10-06 22:07:49 +02002023 gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
Jon Huntercdd69282013-02-08 16:46:13 -06002024 ret = gpmc_cs_program_settings(cs, &gpmc_s);
2025 if (ret < 0)
2026 goto err;
2027
Robert ABEL2e676902015-02-27 16:56:53 +01002028 ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
Roger Quadros7604baf2014-08-29 19:11:51 +03002029 if (ret) {
2030 dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
2031 child->name);
2032 goto err;
2033 }
Jon Huntercdd69282013-02-08 16:46:13 -06002034
Roger Quadrose378d222014-08-29 19:11:52 +03002035 /* Clear limited address i.e. enable A26-A11 */
2036 val = gpmc_read_reg(GPMC_CONFIG);
2037 val &= ~GPMC_CONFIG_LIMITEDADDRESS;
2038 gpmc_write_reg(GPMC_CONFIG, val);
2039
Roger Quadros4cf27d22014-08-29 19:11:53 +03002040 /* Enable CS region */
2041 gpmc_cs_enable_mem(cs);
Jon Huntercdd69282013-02-08 16:46:13 -06002042
Tony Lindgrenfd4446f2013-11-14 15:25:09 -08002043no_timings:
Robert ABELb1dc1ca2015-02-27 16:56:49 +01002044
2045 /* create platform device, NULL on error or when disabled */
2046 if (!of_platform_device_create(child, NULL, &pdev->dev))
2047 goto err_child_fail;
2048
2049 /* is child a common bus? */
2050 if (of_match_node(of_default_bus_match_table, child))
2051 /* create children and other common bus children */
2052 if (of_platform_populate(child, of_default_bus_match_table,
2053 NULL, &pdev->dev))
2054 goto err_child_fail;
2055
2056 return 0;
2057
2058err_child_fail:
Jon Huntercdd69282013-02-08 16:46:13 -06002059
2060 dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
Javier Martinez Canillase8ffd6f2013-03-14 16:09:20 +01002061 ret = -ENODEV;
Jon Huntercdd69282013-02-08 16:46:13 -06002062
2063err:
2064 gpmc_cs_free(cs);
2065
2066 return ret;
2067}
2068
Roger Quadrosd2d00862016-03-07 12:18:43 +02002069static int gpmc_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
2070{
2071 return 1; /* we're input only */
2072}
2073
2074static int gpmc_gpio_direction_input(struct gpio_chip *chip,
2075 unsigned int offset)
2076{
2077 return 0; /* we're input only */
2078}
2079
2080static int gpmc_gpio_direction_output(struct gpio_chip *chip,
2081 unsigned int offset, int value)
2082{
2083 return -EINVAL; /* we're input only */
2084}
2085
2086static void gpmc_gpio_set(struct gpio_chip *chip, unsigned int offset,
2087 int value)
2088{
2089}
2090
2091static int gpmc_gpio_get(struct gpio_chip *chip, unsigned int offset)
2092{
2093 u32 reg;
2094
2095 offset += 8;
2096
2097 reg = gpmc_read_reg(GPMC_STATUS) & BIT(offset);
2098
2099 return !!reg;
2100}
2101
2102static int gpmc_gpio_init(struct gpmc_device *gpmc)
2103{
2104 int ret;
2105
2106 gpmc->gpio_chip.parent = gpmc->dev;
2107 gpmc->gpio_chip.owner = THIS_MODULE;
2108 gpmc->gpio_chip.label = DEVICE_NAME;
2109 gpmc->gpio_chip.ngpio = gpmc_nr_waitpins;
2110 gpmc->gpio_chip.get_direction = gpmc_gpio_get_direction;
2111 gpmc->gpio_chip.direction_input = gpmc_gpio_direction_input;
2112 gpmc->gpio_chip.direction_output = gpmc_gpio_direction_output;
2113 gpmc->gpio_chip.set = gpmc_gpio_set;
2114 gpmc->gpio_chip.get = gpmc_gpio_get;
2115 gpmc->gpio_chip.base = -1;
2116
2117 ret = gpiochip_add(&gpmc->gpio_chip);
2118 if (ret < 0) {
2119 dev_err(gpmc->dev, "could not register gpio chip: %d\n", ret);
2120 return ret;
2121 }
2122
2123 return 0;
2124}
2125
2126static void gpmc_gpio_exit(struct gpmc_device *gpmc)
2127{
2128 gpiochip_remove(&gpmc->gpio_chip);
2129}
2130
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002131static int gpmc_probe_dt(struct platform_device *pdev)
2132{
2133 int ret;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002134 const struct of_device_id *of_id =
2135 of_match_device(gpmc_dt_ids, &pdev->dev);
2136
2137 if (!of_id)
2138 return 0;
2139
Gupta Pekonf34f3712013-05-31 17:31:30 +05302140 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
2141 &gpmc_cs_num);
2142 if (ret < 0) {
2143 pr_err("%s: number of chip-selects not defined\n", __func__);
2144 return ret;
2145 } else if (gpmc_cs_num < 1) {
2146 pr_err("%s: all chip-selects are disabled\n", __func__);
2147 return -EINVAL;
2148 } else if (gpmc_cs_num > GPMC_CS_NUM) {
2149 pr_err("%s: number of supported chip-selects cannot be > %d\n",
2150 __func__, GPMC_CS_NUM);
2151 return -EINVAL;
2152 }
2153
Jon Hunter9f833152013-02-20 15:53:38 -06002154 ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
2155 &gpmc_nr_waitpins);
2156 if (ret < 0) {
2157 pr_err("%s: number of wait pins not found!\n", __func__);
2158 return ret;
2159 }
2160
Roger Quadrosd2d00862016-03-07 12:18:43 +02002161 return 0;
2162}
2163
2164static int gpmc_probe_dt_children(struct platform_device *pdev)
2165{
2166 int ret;
2167 struct device_node *child;
2168
Guido Martínez68e2eb52014-07-02 10:35:18 -03002169 for_each_available_child_of_node(pdev->dev.of_node, child) {
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002170
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02002171 if (!child->name)
2172 continue;
Jon Huntercdd69282013-02-08 16:46:13 -06002173
Roger Quadrosc9711ec2014-05-21 07:29:03 +03002174 if (of_node_cmp(child->name, "onenand") == 0)
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02002175 ret = gpmc_probe_onenand_child(pdev, child);
Tony Lindgren28a7eed2015-06-01 15:00:44 -07002176 else
Javier Martinez Canillasf2b09f62013-04-17 22:34:11 +02002177 ret = gpmc_probe_generic_child(pdev, child);
Roger Quadrosd2d00862016-03-07 12:18:43 +02002178
2179 if (ret)
2180 return ret;
Javier Martinez Canillas5330dc12013-03-14 22:54:11 +01002181 }
2182
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002183 return 0;
2184}
2185#else
2186static int gpmc_probe_dt(struct platform_device *pdev)
2187{
2188 return 0;
2189}
Roger Quadrosd2d00862016-03-07 12:18:43 +02002190
2191static int gpmc_probe_dt_children(struct platform_device *pdev)
2192{
2193 return 0;
2194}
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002195#endif
2196
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002197static int gpmc_probe(struct platform_device *pdev)
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07002198{
Jon Hunter81190242012-10-17 09:41:25 -05002199 int rc;
Afzal Mohammed6b6c32f2012-08-30 12:53:23 -07002200 u32 l;
Afzal Mohammedda496872012-09-23 17:28:25 -06002201 struct resource *res;
Roger Quadros384258f2015-07-30 14:49:23 +03002202 struct gpmc_device *gpmc;
2203
2204 gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
2205 if (!gpmc)
2206 return -ENOMEM;
2207
2208 gpmc->dev = &pdev->dev;
2209 platform_set_drvdata(pdev, gpmc);
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07002210
Afzal Mohammedda496872012-09-23 17:28:25 -06002211 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2212 if (res == NULL)
2213 return -ENOENT;
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03002214
Afzal Mohammedda496872012-09-23 17:28:25 -06002215 phys_base = res->start;
2216 mem_size = resource_size(res);
Kevin Hilman8d084362010-01-29 14:20:06 -08002217
Thierry Reding5857bd92013-01-21 11:08:55 +01002218 gpmc_base = devm_ioremap_resource(&pdev->dev, res);
2219 if (IS_ERR(gpmc_base))
2220 return PTR_ERR(gpmc_base);
Afzal Mohammedda496872012-09-23 17:28:25 -06002221
2222 res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
Roger Quadros384258f2015-07-30 14:49:23 +03002223 if (!res) {
2224 dev_err(&pdev->dev, "Failed to get resource: irq\n");
2225 return -ENOENT;
2226 }
2227
2228 gpmc->irq = res->start;
Afzal Mohammedda496872012-09-23 17:28:25 -06002229
Roger Quadros8bf9be52014-09-01 15:18:56 +03002230 gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
Afzal Mohammedda496872012-09-23 17:28:25 -06002231 if (IS_ERR(gpmc_l3_clk)) {
Roger Quadros8bf9be52014-09-01 15:18:56 +03002232 dev_err(&pdev->dev, "Failed to get GPMC fck\n");
Afzal Mohammedda496872012-09-23 17:28:25 -06002233 return PTR_ERR(gpmc_l3_clk);
Paul Walmsleyfd1dc872008-10-06 15:49:17 +03002234 }
2235
Roger Quadros8bf9be52014-09-01 15:18:56 +03002236 if (!clk_get_rate(gpmc_l3_clk)) {
2237 dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
2238 return -EINVAL;
2239 }
2240
Roger Quadrosd2d00862016-03-07 12:18:43 +02002241 if (pdev->dev.of_node) {
2242 rc = gpmc_probe_dt(pdev);
2243 if (rc)
2244 return rc;
2245 } else {
2246 gpmc_cs_num = GPMC_CS_NUM;
2247 gpmc_nr_waitpins = GPMC_NR_WAITPINS;
2248 }
2249
avinash philipb3f55252013-06-12 16:30:56 +05302250 pm_runtime_enable(&pdev->dev);
2251 pm_runtime_get_sync(&pdev->dev);
Olof Johansson1daa8c12010-01-20 22:39:29 +00002252
Juha Yrjola4bbbc1a2006-06-26 16:16:16 -07002253 l = gpmc_read_reg(GPMC_REVISION);
Jon Hunteraa8d4762013-02-21 15:25:23 -06002254
2255 /*
2256 * FIXME: Once device-tree migration is complete the below flags
2257 * should be populated based upon the device-tree compatible
2258 * string. For now just use the IP revision. OMAP3+ devices have
2259 * the wr_access and wr_data_mux_bus register fields. OMAP4+
2260 * devices support the addr-addr-data multiplex protocol.
2261 *
2262 * GPMC IP revisions:
2263 * - OMAP24xx = 2.0
2264 * - OMAP3xxx = 5.0
2265 * - OMAP44xx/54xx/AM335x = 6.0
2266 */
Afzal Mohammedda496872012-09-23 17:28:25 -06002267 if (GPMC_REVISION_MAJOR(l) > 0x4)
2268 gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
Jon Hunteraa8d4762013-02-21 15:25:23 -06002269 if (GPMC_REVISION_MAJOR(l) > 0x5)
2270 gpmc_capability |= GPMC_HAS_MUX_AAD;
Roger Quadros384258f2015-07-30 14:49:23 +03002271 dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
Afzal Mohammedda496872012-09-23 17:28:25 -06002272 GPMC_REVISION_MINOR(l));
2273
Jon Hunter84b00f02013-03-06 14:36:47 -06002274 gpmc_mem_init();
Roger Quadrosd2d00862016-03-07 12:18:43 +02002275 rc = gpmc_gpio_init(gpmc);
2276 if (rc)
2277 goto gpio_init_failed;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05302278
Roger Quadros384258f2015-07-30 14:49:23 +03002279 rc = gpmc_setup_irq(gpmc);
2280 if (rc) {
2281 dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
Roger Quadrosd2d00862016-03-07 12:18:43 +02002282 goto setup_irq_failed;
Roger Quadros384258f2015-07-30 14:49:23 +03002283 }
Afzal Mohammedda496872012-09-23 17:28:25 -06002284
Roger Quadrosd2d00862016-03-07 12:18:43 +02002285 rc = gpmc_probe_dt_children(pdev);
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002286 if (rc < 0) {
Roger Quadrosd2d00862016-03-07 12:18:43 +02002287 dev_err(gpmc->dev, "failed to probe DT children\n");
2288 goto dt_children_failed;
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002289 }
2290
Afzal Mohammedda496872012-09-23 17:28:25 -06002291 return 0;
Roger Quadros384258f2015-07-30 14:49:23 +03002292
Roger Quadrosd2d00862016-03-07 12:18:43 +02002293dt_children_failed:
2294 gpmc_free_irq(gpmc);
2295setup_irq_failed:
2296 gpmc_gpio_exit(gpmc);
2297gpio_init_failed:
2298 gpmc_mem_exit();
Roger Quadros384258f2015-07-30 14:49:23 +03002299 pm_runtime_put_sync(&pdev->dev);
Roger Quadrosd2d00862016-03-07 12:18:43 +02002300 pm_runtime_disable(&pdev->dev);
2301
Roger Quadros384258f2015-07-30 14:49:23 +03002302 return rc;
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05302303}
Afzal Mohammedda496872012-09-23 17:28:25 -06002304
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002305static int gpmc_remove(struct platform_device *pdev)
Afzal Mohammedda496872012-09-23 17:28:25 -06002306{
Roger Quadros384258f2015-07-30 14:49:23 +03002307 struct gpmc_device *gpmc = platform_get_drvdata(pdev);
2308
2309 gpmc_free_irq(gpmc);
Roger Quadrosd2d00862016-03-07 12:18:43 +02002310 gpmc_gpio_exit(gpmc);
Afzal Mohammedda496872012-09-23 17:28:25 -06002311 gpmc_mem_exit();
avinash philipb3f55252013-06-12 16:30:56 +05302312 pm_runtime_put_sync(&pdev->dev);
2313 pm_runtime_disable(&pdev->dev);
Roger Quadros384258f2015-07-30 14:49:23 +03002314
Afzal Mohammedda496872012-09-23 17:28:25 -06002315 return 0;
2316}
2317
avinash philipb536dd42013-06-18 00:16:38 +05302318#ifdef CONFIG_PM_SLEEP
2319static int gpmc_suspend(struct device *dev)
2320{
2321 omap3_gpmc_save_context();
2322 pm_runtime_put_sync(dev);
2323 return 0;
2324}
2325
2326static int gpmc_resume(struct device *dev)
2327{
2328 pm_runtime_get_sync(dev);
2329 omap3_gpmc_restore_context();
2330 return 0;
2331}
2332#endif
2333
2334static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
2335
Afzal Mohammedda496872012-09-23 17:28:25 -06002336static struct platform_driver gpmc_driver = {
2337 .probe = gpmc_probe,
Greg Kroah-Hartman351a1022012-12-21 14:02:24 -08002338 .remove = gpmc_remove,
Afzal Mohammedda496872012-09-23 17:28:25 -06002339 .driver = {
2340 .name = DEVICE_NAME,
Daniel Mackbc6b1e72012-12-14 11:36:44 +01002341 .of_match_table = of_match_ptr(gpmc_dt_ids),
avinash philipb536dd42013-06-18 00:16:38 +05302342 .pm = &gpmc_pm_ops,
Afzal Mohammedda496872012-09-23 17:28:25 -06002343 },
2344};
2345
2346static __init int gpmc_init(void)
2347{
2348 return platform_driver_register(&gpmc_driver);
2349}
2350
2351static __exit void gpmc_exit(void)
2352{
2353 platform_driver_unregister(&gpmc_driver);
2354
2355}
2356
Tony Lindgrena8612802014-11-20 12:45:43 -08002357postcore_initcall(gpmc_init);
Afzal Mohammedda496872012-09-23 17:28:25 -06002358module_exit(gpmc_exit);
Sukumar Ghoraidb97eb7d2011-01-28 15:42:05 +05302359
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302360static struct omap3_gpmc_regs gpmc_context;
2361
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002362void omap3_gpmc_save_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302363{
2364 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002365
Tomeu Vizosoe984a1792015-08-05 14:24:15 +02002366 if (!gpmc_base)
2367 return;
2368
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302369 gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
2370 gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
2371 gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
2372 gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
2373 gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
2374 gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
2375 gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
Gupta Pekonf34f3712013-05-31 17:31:30 +05302376 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302377 gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
2378 if (gpmc_context.cs_context[i].is_valid) {
2379 gpmc_context.cs_context[i].config1 =
2380 gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
2381 gpmc_context.cs_context[i].config2 =
2382 gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
2383 gpmc_context.cs_context[i].config3 =
2384 gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
2385 gpmc_context.cs_context[i].config4 =
2386 gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
2387 gpmc_context.cs_context[i].config5 =
2388 gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
2389 gpmc_context.cs_context[i].config6 =
2390 gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
2391 gpmc_context.cs_context[i].config7 =
2392 gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
2393 }
2394 }
2395}
2396
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002397void omap3_gpmc_restore_context(void)
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302398{
2399 int i;
Felipe Balbib2fa3b72010-02-15 10:03:33 -08002400
Tomeu Vizosoe984a1792015-08-05 14:24:15 +02002401 if (!gpmc_base)
2402 return;
2403
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302404 gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
2405 gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
2406 gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
2407 gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
2408 gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
2409 gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
2410 gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
Gupta Pekonf34f3712013-05-31 17:31:30 +05302411 for (i = 0; i < gpmc_cs_num; i++) {
Rajendra Nayaka2d3e7b2008-09-26 17:47:33 +05302412 if (gpmc_context.cs_context[i].is_valid) {
2413 gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
2414 gpmc_context.cs_context[i].config1);
2415 gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
2416 gpmc_context.cs_context[i].config2);
2417 gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
2418 gpmc_context.cs_context[i].config3);
2419 gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
2420 gpmc_context.cs_context[i].config4);
2421 gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
2422 gpmc_context.cs_context[i].config5);
2423 gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
2424 gpmc_context.cs_context[i].config6);
2425 gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
2426 gpmc_context.cs_context[i].config7);
2427 }
2428 }
2429}