]>
Commit | Line | Data |
---|---|---|
4bbbc1ad JY |
1 | /* |
2 | * GPMC support functions | |
3 | * | |
4 | * Copyright (C) 2005-2006 Nokia Corporation | |
5 | * | |
6 | * Author: Juha Yrjola | |
7 | * | |
44169075 SS |
8 | * Copyright (C) 2009 Texas Instruments |
9 | * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com> | |
10 | * | |
4bbbc1ad JY |
11 | * This program is free software; you can redistribute it and/or modify |
12 | * it under the terms of the GNU General Public License version 2 as | |
13 | * published by the Free Software Foundation. | |
14 | */ | |
db97eb7d | 15 | #include <linux/irq.h> |
4bbbc1ad JY |
16 | #include <linux/kernel.h> |
17 | #include <linux/init.h> | |
18 | #include <linux/err.h> | |
19 | #include <linux/clk.h> | |
f37e4580 ID |
20 | #include <linux/ioport.h> |
21 | #include <linux/spinlock.h> | |
fced80c7 | 22 | #include <linux/io.h> |
fd1dc87d | 23 | #include <linux/module.h> |
db97eb7d | 24 | #include <linux/interrupt.h> |
384258f2 | 25 | #include <linux/irqdomain.h> |
da496873 | 26 | #include <linux/platform_device.h> |
bc6b1e7b | 27 | #include <linux/of.h> |
cdd6928c | 28 | #include <linux/of_address.h> |
bc6b1e7b DM |
29 | #include <linux/of_mtd.h> |
30 | #include <linux/of_device.h> | |
b1dc1ca9 | 31 | #include <linux/of_platform.h> |
e639cd5b | 32 | #include <linux/omap-gpmc.h> |
b3f5525c | 33 | #include <linux/pm_runtime.h> |
4bbbc1ad | 34 | |
bc3668ea | 35 | #include <linux/platform_data/mtd-nand-omap2.h> |
e639cd5b | 36 | #include <linux/platform_data/mtd-onenand-omap2.h> |
4bbbc1ad | 37 | |
7f245162 | 38 | #include <asm/mach-types.h> |
72d0f1c3 | 39 | |
4be48fd5 AM |
40 | #define DEVICE_NAME "omap-gpmc" |
41 | ||
fd1dc87d | 42 | /* GPMC register offsets */ |
4bbbc1ad JY |
43 | #define GPMC_REVISION 0x00 |
44 | #define GPMC_SYSCONFIG 0x10 | |
45 | #define GPMC_SYSSTATUS 0x14 | |
46 | #define GPMC_IRQSTATUS 0x18 | |
47 | #define GPMC_IRQENABLE 0x1c | |
48 | #define GPMC_TIMEOUT_CONTROL 0x40 | |
49 | #define GPMC_ERR_ADDRESS 0x44 | |
50 | #define GPMC_ERR_TYPE 0x48 | |
51 | #define GPMC_CONFIG 0x50 | |
52 | #define GPMC_STATUS 0x54 | |
53 | #define GPMC_PREFETCH_CONFIG1 0x1e0 | |
54 | #define GPMC_PREFETCH_CONFIG2 0x1e4 | |
15e02a3b | 55 | #define GPMC_PREFETCH_CONTROL 0x1ec |
4bbbc1ad JY |
56 | #define GPMC_PREFETCH_STATUS 0x1f0 |
57 | #define GPMC_ECC_CONFIG 0x1f4 | |
58 | #define GPMC_ECC_CONTROL 0x1f8 | |
59 | #define GPMC_ECC_SIZE_CONFIG 0x1fc | |
948d38e7 | 60 | #define GPMC_ECC1_RESULT 0x200 |
8d602cf5 | 61 | #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */ |
2fdf0c98 AM |
62 | #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */ |
63 | #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */ | |
64 | #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */ | |
27c9fd60 | 65 | #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */ |
66 | #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */ | |
67 | #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */ | |
4bbbc1ad | 68 | |
2c65e744 YY |
69 | /* GPMC ECC control settings */ |
70 | #define GPMC_ECC_CTRL_ECCCLEAR 0x100 | |
71 | #define GPMC_ECC_CTRL_ECCDISABLE 0x000 | |
72 | #define GPMC_ECC_CTRL_ECCREG1 0x001 | |
73 | #define GPMC_ECC_CTRL_ECCREG2 0x002 | |
74 | #define GPMC_ECC_CTRL_ECCREG3 0x003 | |
75 | #define GPMC_ECC_CTRL_ECCREG4 0x004 | |
76 | #define GPMC_ECC_CTRL_ECCREG5 0x005 | |
77 | #define GPMC_ECC_CTRL_ECCREG6 0x006 | |
78 | #define GPMC_ECC_CTRL_ECCREG7 0x007 | |
79 | #define GPMC_ECC_CTRL_ECCREG8 0x008 | |
80 | #define GPMC_ECC_CTRL_ECCREG9 0x009 | |
81 | ||
e378d22b RQ |
82 | #define GPMC_CONFIG_LIMITEDADDRESS BIT(1) |
83 | ||
512d73d1 RQ |
84 | #define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS BIT(0) |
85 | ||
559d94b0 AM |
86 | #define GPMC_CONFIG2_CSEXTRADELAY BIT(7) |
87 | #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7) | |
88 | #define GPMC_CONFIG4_OEEXTRADELAY BIT(7) | |
89 | #define GPMC_CONFIG4_WEEXTRADELAY BIT(23) | |
90 | #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6) | |
91 | #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7) | |
92 | ||
948d38e7 | 93 | #define GPMC_CS0_OFFSET 0x60 |
4bbbc1ad | 94 | #define GPMC_CS_SIZE 0x30 |
2fdf0c98 | 95 | #define GPMC_BCH_SIZE 0x10 |
4bbbc1ad | 96 | |
f37e4580 | 97 | #define GPMC_MEM_END 0x3FFFFFFF |
f37e4580 ID |
98 | |
99 | #define GPMC_CHUNK_SHIFT 24 /* 16 MB */ | |
100 | #define GPMC_SECTION_SHIFT 28 /* 128 MB */ | |
101 | ||
59e9c5ae | 102 | #define CS_NUM_SHIFT 24 |
103 | #define ENABLE_PREFETCH (0x1 << 7) | |
104 | #define DMA_MPU_MODE 2 | |
105 | ||
da496873 AM |
106 | #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf) |
107 | #define GPMC_REVISION_MINOR(l) (l & 0xf) | |
108 | ||
109 | #define GPMC_HAS_WR_ACCESS 0x1 | |
110 | #define GPMC_HAS_WR_DATA_MUX_BUS 0x2 | |
aa8d4767 | 111 | #define GPMC_HAS_MUX_AAD 0x4 |
da496873 | 112 | |
9f833156 JH |
113 | #define GPMC_NR_WAITPINS 4 |
114 | ||
e639cd5b TL |
115 | #define GPMC_CS_CONFIG1 0x00 |
116 | #define GPMC_CS_CONFIG2 0x04 | |
117 | #define GPMC_CS_CONFIG3 0x08 | |
118 | #define GPMC_CS_CONFIG4 0x0c | |
119 | #define GPMC_CS_CONFIG5 0x10 | |
120 | #define GPMC_CS_CONFIG6 0x14 | |
121 | #define GPMC_CS_CONFIG7 0x18 | |
122 | #define GPMC_CS_NAND_COMMAND 0x1c | |
123 | #define GPMC_CS_NAND_ADDRESS 0x20 | |
124 | #define GPMC_CS_NAND_DATA 0x24 | |
125 | ||
126 | /* Control Commands */ | |
127 | #define GPMC_CONFIG_RDY_BSY 0x00000001 | |
128 | #define GPMC_CONFIG_DEV_SIZE 0x00000002 | |
129 | #define GPMC_CONFIG_DEV_TYPE 0x00000003 | |
e639cd5b TL |
130 | |
131 | #define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31) | |
132 | #define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30) | |
133 | #define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29) | |
134 | #define GPMC_CONFIG1_READTYPE_SYNC (1 << 29) | |
135 | #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28) | |
136 | #define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27) | |
137 | #define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27) | |
138 | #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25) | |
4b613e9b RA |
139 | /** CLKACTIVATIONTIME Max Ticks */ |
140 | #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2 | |
e639cd5b | 141 | #define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23) |
4b613e9b RA |
142 | /** ATTACHEDDEVICEPAGELENGTH Max Value */ |
143 | #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2 | |
e639cd5b TL |
144 | #define GPMC_CONFIG1_WAIT_READ_MON (1 << 22) |
145 | #define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21) | |
2e676901 RA |
146 | #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18) |
147 | /** WAITMONITORINGTIME Max Ticks */ | |
148 | #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2 | |
e639cd5b TL |
149 | #define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16) |
150 | #define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12) | |
151 | #define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1) | |
4b613e9b RA |
152 | /** DEVICESIZE Max Value */ |
153 | #define GPMC_CONFIG1_DEVICESIZE_MAX 1 | |
e639cd5b TL |
154 | #define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10) |
155 | #define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0) | |
156 | #define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8) | |
157 | #define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4) | |
158 | #define GPMC_CONFIG1_FCLK_DIV(val) (val & 3) | |
159 | #define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1)) | |
160 | #define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2)) | |
161 | #define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3)) | |
162 | #define GPMC_CONFIG7_CSVALID (1 << 6) | |
163 | ||
9c4f757e SP |
164 | #define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f |
165 | #define GPMC_CONFIG7_CSVALID_MASK BIT(6) | |
166 | #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8 | |
167 | #define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET) | |
168 | /* All CONFIG7 bits except reserved bits */ | |
169 | #define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \ | |
170 | GPMC_CONFIG7_CSVALID_MASK | \ | |
171 | GPMC_CONFIG7_MASKADDRESS_MASK) | |
172 | ||
e639cd5b TL |
173 | #define GPMC_DEVICETYPE_NOR 0 |
174 | #define GPMC_DEVICETYPE_NAND 2 | |
175 | #define GPMC_CONFIG_WRITEPROTECT 0x00000010 | |
176 | #define WR_RD_PIN_MONITORING 0x00600000 | |
177 | ||
e639cd5b TL |
178 | /* ECC commands */ |
179 | #define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */ | |
180 | #define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */ | |
181 | #define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */ | |
182 | ||
6b6c32fc AM |
183 | /* XXX: Only NAND irq has been considered,currently these are the only ones used |
184 | */ | |
185 | #define GPMC_NR_IRQ 2 | |
186 | ||
7f2e8c58 RA |
187 | enum gpmc_clk_domain { |
188 | GPMC_CD_FCLK, | |
189 | GPMC_CD_CLK | |
190 | }; | |
191 | ||
9ed7a776 TL |
192 | struct gpmc_cs_data { |
193 | const char *name; | |
194 | ||
195 | #define GPMC_CS_RESERVED (1 << 0) | |
196 | u32 flags; | |
197 | ||
198 | struct resource mem; | |
199 | }; | |
200 | ||
a2d3e7ba RN |
201 | /* Structure to save gpmc cs context */ |
202 | struct gpmc_cs_config { | |
203 | u32 config1; | |
204 | u32 config2; | |
205 | u32 config3; | |
206 | u32 config4; | |
207 | u32 config5; | |
208 | u32 config6; | |
209 | u32 config7; | |
210 | int is_valid; | |
211 | }; | |
212 | ||
213 | /* | |
214 | * Structure to save/restore gpmc context | |
215 | * to support core off on OMAP3 | |
216 | */ | |
217 | struct omap3_gpmc_regs { | |
218 | u32 sysconfig; | |
219 | u32 irqenable; | |
220 | u32 timeout_ctrl; | |
221 | u32 config; | |
222 | u32 prefetch_config1; | |
223 | u32 prefetch_config2; | |
224 | u32 prefetch_control; | |
225 | struct gpmc_cs_config cs_context[GPMC_CS_NUM]; | |
226 | }; | |
227 | ||
384258f2 RQ |
228 | struct gpmc_device { |
229 | struct device *dev; | |
230 | int irq; | |
231 | struct irq_chip irq_chip; | |
232 | }; | |
233 | ||
234 | static struct irq_domain *gpmc_irq_domain; | |
6b6c32fc | 235 | |
f37e4580 | 236 | static struct resource gpmc_mem_root; |
9ed7a776 | 237 | static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM]; |
87b247c4 | 238 | static DEFINE_SPINLOCK(gpmc_mem_lock); |
6797b4fe | 239 | /* Define chip-selects as reserved by default until probe completes */ |
f34f3716 | 240 | static unsigned int gpmc_cs_num = GPMC_CS_NUM; |
9f833156 | 241 | static unsigned int gpmc_nr_waitpins; |
da496873 AM |
242 | static resource_size_t phys_base, mem_size; |
243 | static unsigned gpmc_capability; | |
fd1dc87d | 244 | static void __iomem *gpmc_base; |
4bbbc1ad | 245 | |
fd1dc87d | 246 | static struct clk *gpmc_l3_clk; |
4bbbc1ad | 247 | |
db97eb7d SG |
248 | static irqreturn_t gpmc_handle_irq(int irq, void *dev); |
249 | ||
4bbbc1ad JY |
250 | static void gpmc_write_reg(int idx, u32 val) |
251 | { | |
edfaf05c | 252 | writel_relaxed(val, gpmc_base + idx); |
4bbbc1ad JY |
253 | } |
254 | ||
255 | static u32 gpmc_read_reg(int idx) | |
256 | { | |
edfaf05c | 257 | return readl_relaxed(gpmc_base + idx); |
4bbbc1ad JY |
258 | } |
259 | ||
260 | void gpmc_cs_write_reg(int cs, int idx, u32 val) | |
261 | { | |
262 | void __iomem *reg_addr; | |
263 | ||
948d38e7 | 264 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
edfaf05c | 265 | writel_relaxed(val, reg_addr); |
4bbbc1ad JY |
266 | } |
267 | ||
3fc089e7 | 268 | static u32 gpmc_cs_read_reg(int cs, int idx) |
4bbbc1ad | 269 | { |
fd1dc87d PW |
270 | void __iomem *reg_addr; |
271 | ||
948d38e7 | 272 | reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx; |
edfaf05c | 273 | return readl_relaxed(reg_addr); |
4bbbc1ad JY |
274 | } |
275 | ||
fd1dc87d | 276 | /* TODO: Add support for gpmc_fck to clock framework and use it */ |
3fc089e7 | 277 | static unsigned long gpmc_get_fclk_period(void) |
4bbbc1ad | 278 | { |
fd1dc87d PW |
279 | unsigned long rate = clk_get_rate(gpmc_l3_clk); |
280 | ||
fd1dc87d PW |
281 | rate /= 1000; |
282 | rate = 1000000000 / rate; /* In picoseconds */ | |
283 | ||
284 | return rate; | |
4bbbc1ad JY |
285 | } |
286 | ||
7f2e8c58 RA |
287 | /** |
288 | * gpmc_get_clk_period - get period of selected clock domain in ps | |
289 | * @cs Chip Select Region. | |
290 | * @cd Clock Domain. | |
291 | * | |
292 | * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup | |
293 | * prior to calling this function with GPMC_CD_CLK. | |
294 | */ | |
295 | static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd) | |
296 | { | |
297 | ||
298 | unsigned long tick_ps = gpmc_get_fclk_period(); | |
299 | u32 l; | |
300 | int div; | |
301 | ||
302 | switch (cd) { | |
303 | case GPMC_CD_CLK: | |
304 | /* get current clk divider */ | |
305 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); | |
306 | div = (l & 0x03) + 1; | |
307 | /* get GPMC_CLK period */ | |
308 | tick_ps *= div; | |
309 | break; | |
310 | case GPMC_CD_FCLK: | |
311 | /* FALL-THROUGH */ | |
312 | default: | |
313 | break; | |
314 | } | |
315 | ||
316 | return tick_ps; | |
317 | ||
318 | } | |
319 | ||
320 | static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs, | |
321 | enum gpmc_clk_domain cd) | |
4bbbc1ad JY |
322 | { |
323 | unsigned long tick_ps; | |
324 | ||
325 | /* Calculate in picosecs to yield more exact results */ | |
7f2e8c58 | 326 | tick_ps = gpmc_get_clk_period(cs, cd); |
4bbbc1ad JY |
327 | |
328 | return (time_ns * 1000 + tick_ps - 1) / tick_ps; | |
329 | } | |
330 | ||
7f2e8c58 RA |
331 | static unsigned int gpmc_ns_to_ticks(unsigned int time_ns) |
332 | { | |
333 | return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK); | |
334 | } | |
335 | ||
3fc089e7 | 336 | static unsigned int gpmc_ps_to_ticks(unsigned int time_ps) |
a3551f5b AH |
337 | { |
338 | unsigned long tick_ps; | |
339 | ||
340 | /* Calculate in picosecs to yield more exact results */ | |
341 | tick_ps = gpmc_get_fclk_period(); | |
342 | ||
343 | return (time_ps + tick_ps - 1) / tick_ps; | |
344 | } | |
345 | ||
7f2e8c58 RA |
346 | unsigned int gpmc_clk_ticks_to_ns(unsigned ticks, int cs, |
347 | enum gpmc_clk_domain cd) | |
348 | { | |
349 | return ticks * gpmc_get_clk_period(cs, cd) / 1000; | |
350 | } | |
351 | ||
fd1dc87d PW |
352 | unsigned int gpmc_ticks_to_ns(unsigned int ticks) |
353 | { | |
7f2e8c58 | 354 | return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK); |
fd1dc87d PW |
355 | } |
356 | ||
246da26d AM |
357 | static unsigned int gpmc_ticks_to_ps(unsigned int ticks) |
358 | { | |
359 | return ticks * gpmc_get_fclk_period(); | |
360 | } | |
361 | ||
362 | static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps) | |
363 | { | |
364 | unsigned long ticks = gpmc_ps_to_ticks(time_ps); | |
365 | ||
366 | return ticks * gpmc_get_fclk_period(); | |
367 | } | |
368 | ||
559d94b0 AM |
369 | static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value) |
370 | { | |
371 | u32 l; | |
372 | ||
373 | l = gpmc_cs_read_reg(cs, reg); | |
374 | if (value) | |
375 | l |= mask; | |
376 | else | |
377 | l &= ~mask; | |
378 | gpmc_cs_write_reg(cs, reg, l); | |
379 | } | |
380 | ||
381 | static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p) | |
382 | { | |
383 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1, | |
384 | GPMC_CONFIG1_TIME_PARA_GRAN, | |
385 | p->time_para_granularity); | |
386 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2, | |
387 | GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay); | |
388 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3, | |
389 | GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay); | |
390 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4, | |
391 | GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay); | |
392 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4, | |
393 | GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay); | |
394 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6, | |
395 | GPMC_CONFIG6_CYCLE2CYCLESAMECSEN, | |
396 | p->cycle2cyclesamecsen); | |
397 | gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6, | |
398 | GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN, | |
399 | p->cycle2cyclediffcsen); | |
400 | } | |
401 | ||
63aa945b | 402 | #ifdef CONFIG_OMAP_GPMC_DEBUG |
563dbb26 RA |
403 | /** |
404 | * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it. | |
405 | * @cs: Chip Select Region | |
406 | * @reg: GPMC_CS_CONFIGn register offset. | |
407 | * @st_bit: Start Bit | |
408 | * @end_bit: End Bit. Must be >= @st_bit. | |
4b613e9b RA |
409 | * @ma:x Maximum parameter value (before optional @shift). |
410 | * If 0, maximum is as high as @st_bit and @end_bit allow. | |
563dbb26 | 411 | * @name: DTS node name, w/o "gpmc," |
7f2e8c58 RA |
412 | * @cd: Clock Domain of timing parameter. |
413 | * @shift: Parameter value left shifts @shift, which is then printed instead of value. | |
563dbb26 RA |
414 | * @raw: Raw Format Option. |
415 | * raw format: gpmc,name = <value> | |
416 | * tick format: gpmc,name = <value> /‍* x ns -- y ns; x ticks *‍/ | |
417 | * Where x ns -- y ns result in the same tick value. | |
4b613e9b | 418 | * When @max is exceeded, "invalid" is printed inside comment. |
563dbb26 | 419 | * @noval: Parameter values equal to 0 are not printed. |
563dbb26 RA |
420 | * @return: Specified timing parameter (after optional @shift). |
421 | * | |
422 | */ | |
7f2e8c58 RA |
423 | static int get_gpmc_timing_reg( |
424 | /* timing specifiers */ | |
4b613e9b | 425 | int cs, int reg, int st_bit, int end_bit, int max, |
7f2e8c58 RA |
426 | const char *name, const enum gpmc_clk_domain cd, |
427 | /* value transform */ | |
428 | int shift, | |
429 | /* format specifiers */ | |
430 | bool raw, bool noval) | |
35ac051e TL |
431 | { |
432 | u32 l; | |
563dbb26 RA |
433 | int nr_bits; |
434 | int mask; | |
4b613e9b | 435 | bool invalid; |
35ac051e TL |
436 | |
437 | l = gpmc_cs_read_reg(cs, reg); | |
438 | nr_bits = end_bit - st_bit + 1; | |
563dbb26 RA |
439 | mask = (1 << nr_bits) - 1; |
440 | l = (l >> st_bit) & mask; | |
4b613e9b RA |
441 | if (!max) |
442 | max = mask; | |
443 | invalid = l > max; | |
35ac051e TL |
444 | if (shift) |
445 | l = (shift << l); | |
446 | if (noval && (l == 0)) | |
447 | return 0; | |
448 | if (!raw) { | |
563dbb26 RA |
449 | /* DTS tick format for timings in ns */ |
450 | unsigned int time_ns; | |
451 | unsigned int time_ns_min = 0; | |
35ac051e | 452 | |
563dbb26 | 453 | if (l) |
7f2e8c58 RA |
454 | time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1; |
455 | time_ns = gpmc_clk_ticks_to_ns(l, cs, cd); | |
4b613e9b RA |
456 | pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n", |
457 | name, time_ns, time_ns_min, time_ns, l, | |
458 | invalid ? "; invalid " : " "); | |
35ac051e | 459 | } else { |
563dbb26 | 460 | /* raw format */ |
4b613e9b RA |
461 | pr_info("gpmc,%s = <%u>%s\n", name, l, |
462 | invalid ? " /* invalid */" : ""); | |
35ac051e TL |
463 | } |
464 | ||
465 | return l; | |
466 | } | |
467 | ||
468 | #define GPMC_PRINT_CONFIG(cs, config) \ | |
469 | pr_info("cs%i %s: 0x%08x\n", cs, #config, \ | |
470 | gpmc_cs_read_reg(cs, config)) | |
471 | #define GPMC_GET_RAW(reg, st, end, field) \ | |
4b613e9b RA |
472 | get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0) |
473 | #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \ | |
474 | get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0) | |
35ac051e | 475 | #define GPMC_GET_RAW_BOOL(reg, st, end, field) \ |
4b613e9b RA |
476 | get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1) |
477 | #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \ | |
478 | get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1) | |
35ac051e | 479 | #define GPMC_GET_TICKS(reg, st, end, field) \ |
4b613e9b | 480 | get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0) |
7f2e8c58 | 481 | #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \ |
4b613e9b RA |
482 | get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0) |
483 | #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \ | |
484 | get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0) | |
35ac051e TL |
485 | |
486 | static void gpmc_show_regs(int cs, const char *desc) | |
487 | { | |
488 | pr_info("gpmc cs%i %s:\n", cs, desc); | |
489 | GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1); | |
490 | GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2); | |
491 | GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3); | |
492 | GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4); | |
493 | GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5); | |
494 | GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6); | |
495 | } | |
496 | ||
497 | /* | |
498 | * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available, | |
499 | * see commit c9fb809. | |
500 | */ | |
501 | static void gpmc_cs_show_timings(int cs, const char *desc) | |
502 | { | |
503 | gpmc_show_regs(cs, desc); | |
504 | ||
505 | pr_info("gpmc cs%i access configuration:\n", cs); | |
506 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity"); | |
507 | GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data"); | |
4b613e9b RA |
508 | GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13, |
509 | GPMC_CONFIG1_DEVICESIZE_MAX, "device-width"); | |
35ac051e TL |
510 | GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin"); |
511 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write"); | |
512 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read"); | |
4b613e9b RA |
513 | GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4, |
514 | GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX, | |
515 | "burst-length"); | |
35ac051e TL |
516 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write"); |
517 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write"); | |
518 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read"); | |
519 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read"); | |
520 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap"); | |
521 | ||
522 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay"); | |
523 | ||
524 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay"); | |
525 | ||
526 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay"); | |
527 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay"); | |
528 | ||
529 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen"); | |
530 | GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen"); | |
531 | ||
532 | pr_info("gpmc cs%i timings configuration:\n", cs); | |
533 | GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns"); | |
534 | GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns"); | |
535 | GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns"); | |
536 | ||
537 | GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns"); | |
538 | GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns"); | |
539 | GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns"); | |
2c92c04b NA |
540 | if (gpmc_capability & GPMC_HAS_MUX_AAD) { |
541 | GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns"); | |
542 | GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26, | |
543 | "adv-aad-mux-rd-off-ns"); | |
544 | GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30, | |
545 | "adv-aad-mux-wr-off-ns"); | |
546 | } | |
35ac051e TL |
547 | |
548 | GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns"); | |
549 | GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns"); | |
2c92c04b NA |
550 | if (gpmc_capability & GPMC_HAS_MUX_AAD) { |
551 | GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns"); | |
552 | GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns"); | |
553 | } | |
35ac051e TL |
554 | GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns"); |
555 | GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns"); | |
556 | ||
557 | GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns"); | |
558 | GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns"); | |
559 | GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns"); | |
560 | ||
561 | GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns"); | |
562 | ||
563 | GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns"); | |
564 | GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns"); | |
565 | ||
4b613e9b RA |
566 | GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19, |
567 | GPMC_CONFIG1_WAITMONITORINGTIME_MAX, | |
568 | "wait-monitoring-ns", GPMC_CD_CLK); | |
569 | GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26, | |
570 | GPMC_CONFIG1_CLKACTIVATIONTIME_MAX, | |
571 | "clk-activation-ns", GPMC_CD_FCLK); | |
35ac051e TL |
572 | |
573 | GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns"); | |
574 | GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns"); | |
575 | } | |
4bbbc1ad | 576 | #else |
35ac051e TL |
577 | static inline void gpmc_cs_show_timings(int cs, const char *desc) |
578 | { | |
579 | } | |
4bbbc1ad | 580 | #endif |
35ac051e | 581 | |
7f2e8c58 RA |
582 | /** |
583 | * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region. | |
584 | * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER | |
585 | * prior to calling this function with @cd equal to GPMC_CD_CLK. | |
586 | * | |
587 | * @cs: Chip Select Region. | |
588 | * @reg: GPMC_CS_CONFIGn register offset. | |
589 | * @st_bit: Start Bit | |
590 | * @end_bit: End Bit. Must be >= @st_bit. | |
4b613e9b RA |
591 | * @max: Maximum parameter value. |
592 | * If 0, maximum is as high as @st_bit and @end_bit allow. | |
7f2e8c58 RA |
593 | * @time: Timing parameter in ns. |
594 | * @cd: Timing parameter clock domain. | |
595 | * @name: Timing parameter name. | |
596 | * @return: 0 on success, -1 on error. | |
597 | */ | |
4b613e9b | 598 | static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max, |
7f2e8c58 | 599 | int time, enum gpmc_clk_domain cd, const char *name) |
4bbbc1ad JY |
600 | { |
601 | u32 l; | |
602 | int ticks, mask, nr_bits; | |
603 | ||
604 | if (time == 0) | |
605 | ticks = 0; | |
606 | else | |
7f2e8c58 | 607 | ticks = gpmc_ns_to_clk_ticks(time, cs, cd); |
4bbbc1ad | 608 | nr_bits = end_bit - st_bit + 1; |
80323742 RQ |
609 | mask = (1 << nr_bits) - 1; |
610 | ||
4b613e9b RA |
611 | if (!max) |
612 | max = mask; | |
613 | ||
614 | if (ticks > max) { | |
7f2e8c58 | 615 | pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n", |
4b613e9b | 616 | __func__, cs, name, time, ticks, max); |
80323742 | 617 | |
4bbbc1ad | 618 | return -1; |
1c22cc13 | 619 | } |
4bbbc1ad | 620 | |
4bbbc1ad | 621 | l = gpmc_cs_read_reg(cs, reg); |
63aa945b | 622 | #ifdef CONFIG_OMAP_GPMC_DEBUG |
f585070b | 623 | pr_info( |
2affc816 | 624 | "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n", |
7f2e8c58 | 625 | cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000, |
1c22cc13 | 626 | (l >> st_bit) & mask, time); |
4bbbc1ad JY |
627 | #endif |
628 | l &= ~(mask << st_bit); | |
629 | l |= ticks << st_bit; | |
630 | gpmc_cs_write_reg(cs, reg, l); | |
631 | ||
632 | return 0; | |
633 | } | |
634 | ||
4b613e9b RA |
635 | #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \ |
636 | if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \ | |
637 | t->field, (cd), #field) < 0) \ | |
4bbbc1ad | 638 | return -1 |
4bbbc1ad | 639 | |
7f2e8c58 | 640 | #define GPMC_SET_ONE(reg, st, end, field) \ |
4b613e9b | 641 | GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK) |
7f2e8c58 | 642 | |
2e676901 RA |
643 | /** |
644 | * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME | |
645 | * WAITMONITORINGTIME will be _at least_ as long as desired, i.e. | |
646 | * read --> don't sample bus too early | |
647 | * write --> data is longer on bus | |
648 | * | |
649 | * Formula: | |
650 | * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns) | |
651 | * / waitmonitoring_ticks) | |
652 | * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by | |
653 | * div <= 0 check. | |
654 | * | |
655 | * @wait_monitoring: WAITMONITORINGTIME in ns. | |
656 | * @return: -1 on failure to scale, else proper divider > 0. | |
657 | */ | |
658 | static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring) | |
659 | { | |
660 | ||
661 | int div = gpmc_ns_to_ticks(wait_monitoring); | |
662 | ||
663 | div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1; | |
664 | div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX; | |
665 | ||
666 | if (div > 4) | |
667 | return -1; | |
668 | if (div <= 0) | |
669 | div = 1; | |
670 | ||
671 | return div; | |
672 | ||
673 | } | |
674 | ||
675 | /** | |
676 | * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period. | |
677 | * @sync_clk: GPMC_CLK period in ps. | |
678 | * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK. | |
679 | * Else, returns -1. | |
680 | */ | |
1b47ca1a | 681 | int gpmc_calc_divider(unsigned int sync_clk) |
4bbbc1ad | 682 | { |
2e676901 | 683 | int div = gpmc_ps_to_ticks(sync_clk); |
4bbbc1ad | 684 | |
4bbbc1ad JY |
685 | if (div > 4) |
686 | return -1; | |
1c22cc13 | 687 | if (div <= 0) |
4bbbc1ad JY |
688 | div = 1; |
689 | ||
690 | return div; | |
691 | } | |
692 | ||
2e676901 RA |
693 | /** |
694 | * gpmc_cs_set_timings - program timing parameters for Chip Select Region. | |
695 | * @cs: Chip Select Region. | |
696 | * @t: GPMC timing parameters. | |
697 | * @s: GPMC timing settings. | |
698 | * @return: 0 on success, -1 on error. | |
699 | */ | |
700 | int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t, | |
701 | const struct gpmc_settings *s) | |
4bbbc1ad JY |
702 | { |
703 | int div; | |
704 | u32 l; | |
705 | ||
1b47ca1a | 706 | div = gpmc_calc_divider(t->sync_clk); |
4bbbc1ad | 707 | if (div < 0) |
a032d33b | 708 | return div; |
4bbbc1ad | 709 | |
2e676901 RA |
710 | /* |
711 | * See if we need to change the divider for waitmonitoringtime. | |
712 | * | |
713 | * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for | |
714 | * pure asynchronous accesses, i.e. both read and write asynchronous. | |
715 | * However, only do so if WAITMONITORINGTIME is actually used, i.e. | |
716 | * either WAITREADMONITORING or WAITWRITEMONITORING is set. | |
717 | * | |
718 | * This statement must not change div to scale async WAITMONITORINGTIME | |
719 | * to protect mixed synchronous and asynchronous accesses. | |
720 | * | |
721 | * We raise an error later if WAITMONITORINGTIME does not fit. | |
722 | */ | |
723 | if (!s->sync_read && !s->sync_write && | |
724 | (s->wait_on_read || s->wait_on_write) | |
725 | ) { | |
726 | ||
727 | div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring); | |
728 | if (div < 0) { | |
729 | pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n", | |
730 | __func__, | |
731 | t->wait_monitoring | |
732 | ); | |
733 | return -1; | |
734 | } | |
735 | } | |
736 | ||
4bbbc1ad JY |
737 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on); |
738 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off); | |
739 | GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off); | |
740 | ||
741 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on); | |
742 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off); | |
743 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off); | |
2c92c04b NA |
744 | if (gpmc_capability & GPMC_HAS_MUX_AAD) { |
745 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 4, 6, adv_aad_mux_on); | |
746 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off); | |
747 | GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off); | |
748 | } | |
4bbbc1ad JY |
749 | |
750 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on); | |
751 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off); | |
2c92c04b NA |
752 | if (gpmc_capability & GPMC_HAS_MUX_AAD) { |
753 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 4, 6, oe_aad_mux_on); | |
754 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off); | |
755 | } | |
4bbbc1ad JY |
756 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on); |
757 | GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off); | |
758 | ||
759 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle); | |
760 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle); | |
761 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access); | |
762 | ||
763 | GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access); | |
764 | ||
559d94b0 AM |
765 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround); |
766 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay); | |
767 | ||
da496873 | 768 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) |
cc26b3b0 | 769 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus); |
da496873 | 770 | if (gpmc_capability & GPMC_HAS_WR_ACCESS) |
cc26b3b0 | 771 | GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access); |
cc26b3b0 | 772 | |
1c22cc13 | 773 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1); |
7f2e8c58 RA |
774 | l &= ~0x03; |
775 | l |= (div - 1); | |
776 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l); | |
777 | ||
4b613e9b RA |
778 | GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19, |
779 | GPMC_CONFIG1_WAITMONITORINGTIME_MAX, | |
780 | wait_monitoring, GPMC_CD_CLK); | |
781 | GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26, | |
782 | GPMC_CONFIG1_CLKACTIVATIONTIME_MAX, | |
783 | clk_activation, GPMC_CD_FCLK); | |
7f2e8c58 | 784 | |
63aa945b | 785 | #ifdef CONFIG_OMAP_GPMC_DEBUG |
f585070b RA |
786 | pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n", |
787 | cs, (div * gpmc_get_fclk_period()) / 1000, div); | |
4bbbc1ad | 788 | #endif |
4bbbc1ad | 789 | |
559d94b0 | 790 | gpmc_cs_bool_timings(cs, &t->bool_timings); |
35ac051e | 791 | gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings"); |
559d94b0 | 792 | |
4bbbc1ad JY |
793 | return 0; |
794 | } | |
795 | ||
4cf27d2e | 796 | static int gpmc_cs_set_memconf(int cs, u32 base, u32 size) |
f37e4580 ID |
797 | { |
798 | u32 l; | |
799 | u32 mask; | |
800 | ||
c71f8e9b JH |
801 | /* |
802 | * Ensure that base address is aligned on a | |
803 | * boundary equal to or greater than size. | |
804 | */ | |
805 | if (base & (size - 1)) | |
806 | return -EINVAL; | |
807 | ||
9c4f757e | 808 | base >>= GPMC_CHUNK_SHIFT; |
f37e4580 | 809 | mask = (1 << GPMC_SECTION_SHIFT) - size; |
9c4f757e SP |
810 | mask >>= GPMC_CHUNK_SHIFT; |
811 | mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET; | |
812 | ||
f37e4580 | 813 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); |
9c4f757e SP |
814 | l &= ~GPMC_CONFIG7_MASK; |
815 | l |= base & GPMC_CONFIG7_BASEADDRESS_MASK; | |
816 | l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK; | |
a2d3e7ba | 817 | l |= GPMC_CONFIG7_CSVALID; |
f37e4580 | 818 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
c71f8e9b JH |
819 | |
820 | return 0; | |
f37e4580 ID |
821 | } |
822 | ||
4cf27d2e RQ |
823 | static void gpmc_cs_enable_mem(int cs) |
824 | { | |
825 | u32 l; | |
826 | ||
827 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); | |
828 | l |= GPMC_CONFIG7_CSVALID; | |
829 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); | |
830 | } | |
831 | ||
f37e4580 ID |
832 | static void gpmc_cs_disable_mem(int cs) |
833 | { | |
834 | u32 l; | |
835 | ||
836 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); | |
a2d3e7ba | 837 | l &= ~GPMC_CONFIG7_CSVALID; |
f37e4580 ID |
838 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l); |
839 | } | |
840 | ||
841 | static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size) | |
842 | { | |
843 | u32 l; | |
844 | u32 mask; | |
845 | ||
846 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); | |
847 | *base = (l & 0x3f) << GPMC_CHUNK_SHIFT; | |
848 | mask = (l >> 8) & 0x0f; | |
849 | *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT); | |
850 | } | |
851 | ||
852 | static int gpmc_cs_mem_enabled(int cs) | |
853 | { | |
854 | u32 l; | |
855 | ||
856 | l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7); | |
a2d3e7ba | 857 | return l & GPMC_CONFIG7_CSVALID; |
f37e4580 ID |
858 | } |
859 | ||
f5d8edaf | 860 | static void gpmc_cs_set_reserved(int cs, int reserved) |
4bbbc1ad | 861 | { |
9ed7a776 TL |
862 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; |
863 | ||
864 | gpmc->flags |= GPMC_CS_RESERVED; | |
f37e4580 ID |
865 | } |
866 | ||
ae9d908a | 867 | static bool gpmc_cs_reserved(int cs) |
f37e4580 | 868 | { |
9ed7a776 TL |
869 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; |
870 | ||
871 | return gpmc->flags & GPMC_CS_RESERVED; | |
872 | } | |
873 | ||
874 | static void gpmc_cs_set_name(int cs, const char *name) | |
875 | { | |
876 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; | |
877 | ||
878 | gpmc->name = name; | |
879 | } | |
880 | ||
2e25b0ec | 881 | static const char *gpmc_cs_get_name(int cs) |
9ed7a776 TL |
882 | { |
883 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; | |
884 | ||
885 | return gpmc->name; | |
f37e4580 ID |
886 | } |
887 | ||
888 | static unsigned long gpmc_mem_align(unsigned long size) | |
889 | { | |
890 | int order; | |
891 | ||
892 | size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1); | |
893 | order = GPMC_CHUNK_SHIFT - 1; | |
894 | do { | |
895 | size >>= 1; | |
896 | order++; | |
897 | } while (size); | |
898 | size = 1 << order; | |
899 | return size; | |
900 | } | |
901 | ||
902 | static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size) | |
903 | { | |
9ed7a776 TL |
904 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; |
905 | struct resource *res = &gpmc->mem; | |
f37e4580 ID |
906 | int r; |
907 | ||
908 | size = gpmc_mem_align(size); | |
909 | spin_lock(&gpmc_mem_lock); | |
910 | res->start = base; | |
911 | res->end = base + size - 1; | |
912 | r = request_resource(&gpmc_mem_root, res); | |
913 | spin_unlock(&gpmc_mem_lock); | |
914 | ||
915 | return r; | |
916 | } | |
917 | ||
da496873 AM |
918 | static int gpmc_cs_delete_mem(int cs) |
919 | { | |
9ed7a776 TL |
920 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; |
921 | struct resource *res = &gpmc->mem; | |
da496873 AM |
922 | int r; |
923 | ||
924 | spin_lock(&gpmc_mem_lock); | |
efe80723 | 925 | r = release_resource(res); |
da496873 AM |
926 | res->start = 0; |
927 | res->end = 0; | |
928 | spin_unlock(&gpmc_mem_lock); | |
929 | ||
930 | return r; | |
931 | } | |
932 | ||
cdd6928c JH |
933 | /** |
934 | * gpmc_cs_remap - remaps a chip-select physical base address | |
935 | * @cs: chip-select to remap | |
936 | * @base: physical base address to re-map chip-select to | |
937 | * | |
938 | * Re-maps a chip-select to a new physical base address specified by | |
939 | * "base". Returns 0 on success and appropriate negative error code | |
940 | * on failure. | |
941 | */ | |
942 | static int gpmc_cs_remap(int cs, u32 base) | |
943 | { | |
944 | int ret; | |
945 | u32 old_base, size; | |
946 | ||
f34f3716 GP |
947 | if (cs > gpmc_cs_num) { |
948 | pr_err("%s: requested chip-select is disabled\n", __func__); | |
cdd6928c | 949 | return -ENODEV; |
f34f3716 | 950 | } |
fb677ef7 TL |
951 | |
952 | /* | |
953 | * Make sure we ignore any device offsets from the GPMC partition | |
954 | * allocated for the chip select and that the new base confirms | |
955 | * to the GPMC 16MB minimum granularity. | |
956 | */ | |
957 | base &= ~(SZ_16M - 1); | |
958 | ||
cdd6928c JH |
959 | gpmc_cs_get_memconf(cs, &old_base, &size); |
960 | if (base == old_base) | |
961 | return 0; | |
4cf27d2e | 962 | |
cdd6928c JH |
963 | ret = gpmc_cs_delete_mem(cs); |
964 | if (ret < 0) | |
965 | return ret; | |
4cf27d2e | 966 | |
cdd6928c | 967 | ret = gpmc_cs_insert_mem(cs, base, size); |
c71f8e9b JH |
968 | if (ret < 0) |
969 | return ret; | |
cdd6928c | 970 | |
4cf27d2e RQ |
971 | ret = gpmc_cs_set_memconf(cs, base, size); |
972 | ||
973 | return ret; | |
cdd6928c JH |
974 | } |
975 | ||
f37e4580 ID |
976 | int gpmc_cs_request(int cs, unsigned long size, unsigned long *base) |
977 | { | |
9ed7a776 TL |
978 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; |
979 | struct resource *res = &gpmc->mem; | |
f37e4580 ID |
980 | int r = -1; |
981 | ||
f34f3716 GP |
982 | if (cs > gpmc_cs_num) { |
983 | pr_err("%s: requested chip-select is disabled\n", __func__); | |
f37e4580 | 984 | return -ENODEV; |
f34f3716 | 985 | } |
f37e4580 ID |
986 | size = gpmc_mem_align(size); |
987 | if (size > (1 << GPMC_SECTION_SHIFT)) | |
988 | return -ENOMEM; | |
989 | ||
990 | spin_lock(&gpmc_mem_lock); | |
991 | if (gpmc_cs_reserved(cs)) { | |
992 | r = -EBUSY; | |
993 | goto out; | |
994 | } | |
995 | if (gpmc_cs_mem_enabled(cs)) | |
996 | r = adjust_resource(res, res->start & ~(size - 1), size); | |
997 | if (r < 0) | |
998 | r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0, | |
999 | size, NULL, NULL); | |
1000 | if (r < 0) | |
1001 | goto out; | |
1002 | ||
4cf27d2e RQ |
1003 | /* Disable CS while changing base address and size mask */ |
1004 | gpmc_cs_disable_mem(cs); | |
1005 | ||
1006 | r = gpmc_cs_set_memconf(cs, res->start, resource_size(res)); | |
c71f8e9b JH |
1007 | if (r < 0) { |
1008 | release_resource(res); | |
1009 | goto out; | |
1010 | } | |
1011 | ||
4cf27d2e RQ |
1012 | /* Enable CS */ |
1013 | gpmc_cs_enable_mem(cs); | |
f37e4580 ID |
1014 | *base = res->start; |
1015 | gpmc_cs_set_reserved(cs, 1); | |
1016 | out: | |
1017 | spin_unlock(&gpmc_mem_lock); | |
1018 | return r; | |
1019 | } | |
fd1dc87d | 1020 | EXPORT_SYMBOL(gpmc_cs_request); |
f37e4580 ID |
1021 | |
1022 | void gpmc_cs_free(int cs) | |
1023 | { | |
9ed7a776 TL |
1024 | struct gpmc_cs_data *gpmc = &gpmc_cs[cs]; |
1025 | struct resource *res = &gpmc->mem; | |
efe80723 | 1026 | |
f37e4580 | 1027 | spin_lock(&gpmc_mem_lock); |
f34f3716 | 1028 | if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) { |
f37e4580 ID |
1029 | printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs); |
1030 | BUG(); | |
1031 | spin_unlock(&gpmc_mem_lock); | |
1032 | return; | |
1033 | } | |
1034 | gpmc_cs_disable_mem(cs); | |
efe80723 TL |
1035 | if (res->flags) |
1036 | release_resource(res); | |
f37e4580 ID |
1037 | gpmc_cs_set_reserved(cs, 0); |
1038 | spin_unlock(&gpmc_mem_lock); | |
1039 | } | |
fd1dc87d | 1040 | EXPORT_SYMBOL(gpmc_cs_free); |
f37e4580 | 1041 | |
948d38e7 | 1042 | /** |
3a544354 | 1043 | * gpmc_configure - write request to configure gpmc |
948d38e7 SG |
1044 | * @cmd: command type |
1045 | * @wval: value to write | |
1046 | * @return status of the operation | |
1047 | */ | |
3a544354 | 1048 | int gpmc_configure(int cmd, int wval) |
948d38e7 | 1049 | { |
3a544354 | 1050 | u32 regval; |
948d38e7 SG |
1051 | |
1052 | switch (cmd) { | |
948d38e7 SG |
1053 | case GPMC_CONFIG_WP: |
1054 | regval = gpmc_read_reg(GPMC_CONFIG); | |
1055 | if (wval) | |
1056 | regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */ | |
1057 | else | |
1058 | regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */ | |
1059 | gpmc_write_reg(GPMC_CONFIG, regval); | |
1060 | break; | |
1061 | ||
948d38e7 | 1062 | default: |
3a544354 JH |
1063 | pr_err("%s: command not supported\n", __func__); |
1064 | return -EINVAL; | |
948d38e7 SG |
1065 | } |
1066 | ||
3a544354 | 1067 | return 0; |
948d38e7 | 1068 | } |
3a544354 | 1069 | EXPORT_SYMBOL(gpmc_configure); |
948d38e7 | 1070 | |
52bd138d AM |
1071 | void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs) |
1072 | { | |
2fdf0c98 AM |
1073 | int i; |
1074 | ||
52bd138d AM |
1075 | reg->gpmc_status = gpmc_base + GPMC_STATUS; |
1076 | reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET + | |
1077 | GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs; | |
1078 | reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET + | |
1079 | GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs; | |
1080 | reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET + | |
1081 | GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs; | |
1082 | reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1; | |
1083 | reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2; | |
1084 | reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL; | |
1085 | reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS; | |
1086 | reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG; | |
1087 | reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL; | |
1088 | reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG; | |
1089 | reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT; | |
2fdf0c98 AM |
1090 | |
1091 | for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) { | |
1092 | reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 + | |
1093 | GPMC_BCH_SIZE * i; | |
1094 | reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 + | |
1095 | GPMC_BCH_SIZE * i; | |
1096 | reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 + | |
1097 | GPMC_BCH_SIZE * i; | |
1098 | reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 + | |
1099 | GPMC_BCH_SIZE * i; | |
27c9fd60 | 1100 | reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 + |
1101 | i * GPMC_BCH_SIZE; | |
1102 | reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 + | |
1103 | i * GPMC_BCH_SIZE; | |
1104 | reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 + | |
1105 | i * GPMC_BCH_SIZE; | |
2fdf0c98 | 1106 | } |
52bd138d AM |
1107 | } |
1108 | ||
512d73d1 RQ |
1109 | static bool gpmc_nand_writebuffer_empty(void) |
1110 | { | |
1111 | if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS) | |
1112 | return true; | |
1113 | ||
1114 | return false; | |
1115 | } | |
1116 | ||
1117 | static struct gpmc_nand_ops nand_ops = { | |
1118 | .nand_writebuffer_empty = gpmc_nand_writebuffer_empty, | |
1119 | }; | |
f47fcad6 RQ |
1120 | |
1121 | /** | |
1122 | * gpmc_omap_get_nand_ops - Get the GPMC NAND interface | |
1123 | * @regs: the GPMC NAND register map exclusive for NAND use. | |
1124 | * @cs: GPMC chip select number on which the NAND sits. The | |
1125 | * register map returned will be specific to this chip select. | |
1126 | * | |
1127 | * Returns NULL on error e.g. invalid cs. | |
1128 | */ | |
1129 | struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs) | |
1130 | { | |
1131 | if (cs >= gpmc_cs_num) | |
1132 | return NULL; | |
1133 | ||
1134 | gpmc_update_nand_reg(reg, cs); | |
1135 | ||
1136 | return &nand_ops; | |
1137 | } | |
1138 | EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops); | |
1139 | ||
6b6c32fc AM |
1140 | int gpmc_get_client_irq(unsigned irq_config) |
1141 | { | |
384258f2 RQ |
1142 | if (!gpmc_irq_domain) { |
1143 | pr_warn("%s called before GPMC IRQ domain available\n", | |
1144 | __func__); | |
6b6c32fc | 1145 | return 0; |
384258f2 | 1146 | } |
6b6c32fc | 1147 | |
384258f2 RQ |
1148 | if (irq_config >= GPMC_NR_IRQ) |
1149 | return 0; | |
6b6c32fc | 1150 | |
384258f2 | 1151 | return irq_create_mapping(gpmc_irq_domain, irq_config); |
6b6c32fc AM |
1152 | } |
1153 | ||
384258f2 | 1154 | static int gpmc_irq_endis(unsigned long hwirq, bool endis) |
6b6c32fc | 1155 | { |
6b6c32fc AM |
1156 | u32 regval; |
1157 | ||
384258f2 RQ |
1158 | regval = gpmc_read_reg(GPMC_IRQENABLE); |
1159 | if (endis) | |
1160 | regval |= BIT(hwirq); | |
1161 | else | |
1162 | regval &= ~BIT(hwirq); | |
1163 | gpmc_write_reg(GPMC_IRQENABLE, regval); | |
6b6c32fc AM |
1164 | |
1165 | return 0; | |
1166 | } | |
1167 | ||
1168 | static void gpmc_irq_disable(struct irq_data *p) | |
1169 | { | |
384258f2 | 1170 | gpmc_irq_endis(p->hwirq, false); |
6b6c32fc AM |
1171 | } |
1172 | ||
1173 | static void gpmc_irq_enable(struct irq_data *p) | |
1174 | { | |
384258f2 | 1175 | gpmc_irq_endis(p->hwirq, true); |
6b6c32fc AM |
1176 | } |
1177 | ||
1178 | static void gpmc_irq_noop(struct irq_data *data) { } | |
1179 | ||
1180 | static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; } | |
1181 | ||
384258f2 RQ |
1182 | static int gpmc_irq_map(struct irq_domain *d, unsigned int virq, |
1183 | irq_hw_number_t hw) | |
6b6c32fc | 1184 | { |
384258f2 RQ |
1185 | struct gpmc_device *gpmc = d->host_data; |
1186 | ||
1187 | irq_set_chip_data(virq, gpmc); | |
1188 | irq_set_chip_and_handler(virq, &gpmc->irq_chip, handle_simple_irq); | |
1189 | irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN); | |
1190 | ||
1191 | return 0; | |
1192 | } | |
1193 | ||
1194 | static const struct irq_domain_ops gpmc_irq_domain_ops = { | |
1195 | .map = gpmc_irq_map, | |
1196 | .xlate = irq_domain_xlate_twocell, | |
1197 | }; | |
1198 | ||
1199 | static irqreturn_t gpmc_handle_irq(int irq, void *data) | |
1200 | { | |
1201 | int hwirq, virq; | |
6b6c32fc | 1202 | u32 regval; |
384258f2 | 1203 | struct gpmc_device *gpmc = data; |
6b6c32fc | 1204 | |
384258f2 | 1205 | regval = gpmc_read_reg(GPMC_IRQSTATUS); |
6b6c32fc | 1206 | |
384258f2 RQ |
1207 | if (!regval) |
1208 | return IRQ_NONE; | |
6b6c32fc | 1209 | |
384258f2 RQ |
1210 | for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++) { |
1211 | if (regval & BIT(hwirq)) { | |
1212 | virq = irq_find_mapping(gpmc_irq_domain, hwirq); | |
1213 | if (!virq) { | |
1214 | dev_warn(gpmc->dev, | |
1215 | "spurious irq detected hwirq %d, virq %d\n", | |
1216 | hwirq, virq); | |
1217 | } | |
1218 | ||
1219 | generic_handle_irq(virq); | |
1220 | } | |
6b6c32fc AM |
1221 | } |
1222 | ||
384258f2 RQ |
1223 | gpmc_write_reg(GPMC_IRQSTATUS, regval); |
1224 | ||
1225 | return IRQ_HANDLED; | |
1226 | } | |
1227 | ||
1228 | static int gpmc_setup_irq(struct gpmc_device *gpmc) | |
1229 | { | |
1230 | u32 regval; | |
1231 | int rc; | |
1232 | ||
6b6c32fc AM |
1233 | /* Disable interrupts */ |
1234 | gpmc_write_reg(GPMC_IRQENABLE, 0); | |
1235 | ||
1236 | /* clear interrupts */ | |
1237 | regval = gpmc_read_reg(GPMC_IRQSTATUS); | |
1238 | gpmc_write_reg(GPMC_IRQSTATUS, regval); | |
1239 | ||
384258f2 RQ |
1240 | gpmc->irq_chip.name = "gpmc"; |
1241 | gpmc->irq_chip.irq_startup = gpmc_irq_noop_ret; | |
1242 | gpmc->irq_chip.irq_enable = gpmc_irq_enable; | |
1243 | gpmc->irq_chip.irq_disable = gpmc_irq_disable; | |
1244 | gpmc->irq_chip.irq_shutdown = gpmc_irq_noop; | |
1245 | gpmc->irq_chip.irq_ack = gpmc_irq_noop; | |
1246 | gpmc->irq_chip.irq_mask = gpmc_irq_noop; | |
1247 | gpmc->irq_chip.irq_unmask = gpmc_irq_noop; | |
1248 | ||
1249 | gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node, | |
1250 | GPMC_NR_IRQ, | |
1251 | &gpmc_irq_domain_ops, | |
1252 | gpmc); | |
1253 | if (!gpmc_irq_domain) { | |
1254 | dev_err(gpmc->dev, "IRQ domain add failed\n"); | |
1255 | return -ENODEV; | |
1256 | } | |
1257 | ||
1258 | rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc); | |
1259 | if (rc) { | |
1260 | dev_err(gpmc->dev, "failed to request irq %d: %d\n", | |
1261 | gpmc->irq, rc); | |
1262 | irq_domain_remove(gpmc_irq_domain); | |
1263 | gpmc_irq_domain = NULL; | |
1264 | } | |
1265 | ||
1266 | return rc; | |
6b6c32fc AM |
1267 | } |
1268 | ||
384258f2 | 1269 | static int gpmc_free_irq(struct gpmc_device *gpmc) |
da496873 | 1270 | { |
384258f2 | 1271 | int hwirq; |
da496873 | 1272 | |
384258f2 | 1273 | free_irq(gpmc->irq, gpmc); |
da496873 | 1274 | |
384258f2 RQ |
1275 | for (hwirq = 0; hwirq < GPMC_NR_IRQ; hwirq++) |
1276 | irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq)); | |
da496873 | 1277 | |
384258f2 RQ |
1278 | irq_domain_remove(gpmc_irq_domain); |
1279 | gpmc_irq_domain = NULL; | |
da496873 AM |
1280 | |
1281 | return 0; | |
1282 | } | |
1283 | ||
351a102d | 1284 | static void gpmc_mem_exit(void) |
da496873 AM |
1285 | { |
1286 | int cs; | |
1287 | ||
f34f3716 | 1288 | for (cs = 0; cs < gpmc_cs_num; cs++) { |
da496873 AM |
1289 | if (!gpmc_cs_mem_enabled(cs)) |
1290 | continue; | |
1291 | gpmc_cs_delete_mem(cs); | |
1292 | } | |
1293 | ||
1294 | } | |
1295 | ||
84b00f0e | 1296 | static void gpmc_mem_init(void) |
f37e4580 | 1297 | { |
84b00f0e | 1298 | int cs; |
f37e4580 | 1299 | |
bf234397 JH |
1300 | /* |
1301 | * The first 1MB of GPMC address space is typically mapped to | |
1302 | * the internal ROM. Never allocate the first page, to | |
1303 | * facilitate bug detection; even if we didn't boot from ROM. | |
7f245162 | 1304 | */ |
bf234397 | 1305 | gpmc_mem_root.start = SZ_1M; |
f37e4580 ID |
1306 | gpmc_mem_root.end = GPMC_MEM_END; |
1307 | ||
1308 | /* Reserve all regions that has been set up by bootloader */ | |
f34f3716 | 1309 | for (cs = 0; cs < gpmc_cs_num; cs++) { |
f37e4580 ID |
1310 | u32 base, size; |
1311 | ||
1312 | if (!gpmc_cs_mem_enabled(cs)) | |
1313 | continue; | |
1314 | gpmc_cs_get_memconf(cs, &base, &size); | |
84b00f0e JH |
1315 | if (gpmc_cs_insert_mem(cs, base, size)) { |
1316 | pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n", | |
1317 | __func__, cs, base, base + size); | |
1318 | gpmc_cs_disable_mem(cs); | |
8119024e | 1319 | } |
f37e4580 | 1320 | } |
4bbbc1ad JY |
1321 | } |
1322 | ||
246da26d AM |
1323 | static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk) |
1324 | { | |
1325 | u32 temp; | |
1326 | int div; | |
1327 | ||
1328 | div = gpmc_calc_divider(sync_clk); | |
1329 | temp = gpmc_ps_to_ticks(time_ps); | |
1330 | temp = (temp + div - 1) / div; | |
1331 | return gpmc_ticks_to_ps(temp * div); | |
1332 | } | |
1333 | ||
1334 | /* XXX: can the cycles be avoided ? */ | |
1335 | static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t, | |
c3be5b45 JH |
1336 | struct gpmc_device_timings *dev_t, |
1337 | bool mux) | |
246da26d | 1338 | { |
246da26d AM |
1339 | u32 temp; |
1340 | ||
1341 | /* adv_rd_off */ | |
1342 | temp = dev_t->t_avdp_r; | |
1343 | /* XXX: mux check required ? */ | |
1344 | if (mux) { | |
1345 | /* XXX: t_avdp not to be required for sync, only added for tusb | |
1346 | * this indirectly necessitates requirement of t_avdp_r and | |
1347 | * t_avdp_w instead of having a single t_avdp | |
1348 | */ | |
1349 | temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh); | |
1350 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); | |
1351 | } | |
1352 | gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp); | |
1353 | ||
1354 | /* oe_on */ | |
1355 | temp = dev_t->t_oeasu; /* XXX: remove this ? */ | |
1356 | if (mux) { | |
1357 | temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach); | |
1358 | temp = max_t(u32, temp, gpmc_t->adv_rd_off + | |
1359 | gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe)); | |
1360 | } | |
1361 | gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp); | |
1362 | ||
1363 | /* access */ | |
1364 | /* XXX: any scope for improvement ?, by combining oe_on | |
1365 | * and clk_activation, need to check whether | |
1366 | * access = clk_activation + round to sync clk ? | |
1367 | */ | |
1368 | temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk); | |
1369 | temp += gpmc_t->clk_activation; | |
1370 | if (dev_t->cyc_oe) | |
1371 | temp = max_t(u32, temp, gpmc_t->oe_on + | |
1372 | gpmc_ticks_to_ps(dev_t->cyc_oe)); | |
1373 | gpmc_t->access = gpmc_round_ps_to_ticks(temp); | |
1374 | ||
1375 | gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1); | |
1376 | gpmc_t->cs_rd_off = gpmc_t->oe_off; | |
1377 | ||
1378 | /* rd_cycle */ | |
1379 | temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez); | |
1380 | temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) + | |
1381 | gpmc_t->access; | |
1382 | /* XXX: barter t_ce_rdyz with t_cez_r ? */ | |
1383 | if (dev_t->t_ce_rdyz) | |
1384 | temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz); | |
1385 | gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp); | |
1386 | ||
1387 | return 0; | |
1388 | } | |
1389 | ||
1390 | static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t, | |
c3be5b45 JH |
1391 | struct gpmc_device_timings *dev_t, |
1392 | bool mux) | |
246da26d | 1393 | { |
246da26d AM |
1394 | u32 temp; |
1395 | ||
1396 | /* adv_wr_off */ | |
1397 | temp = dev_t->t_avdp_w; | |
1398 | if (mux) { | |
1399 | temp = max_t(u32, temp, | |
1400 | gpmc_t->clk_activation + dev_t->t_avdh); | |
1401 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); | |
1402 | } | |
1403 | gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp); | |
1404 | ||
1405 | /* wr_data_mux_bus */ | |
1406 | temp = max_t(u32, dev_t->t_weasu, | |
1407 | gpmc_t->clk_activation + dev_t->t_rdyo); | |
1408 | /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?, | |
1409 | * and in that case remember to handle we_on properly | |
1410 | */ | |
1411 | if (mux) { | |
1412 | temp = max_t(u32, temp, | |
1413 | gpmc_t->adv_wr_off + dev_t->t_aavdh); | |
1414 | temp = max_t(u32, temp, gpmc_t->adv_wr_off + | |
1415 | gpmc_ticks_to_ps(dev_t->cyc_aavdh_we)); | |
1416 | } | |
1417 | gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp); | |
1418 | ||
1419 | /* we_on */ | |
1420 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) | |
1421 | gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu); | |
1422 | else | |
1423 | gpmc_t->we_on = gpmc_t->wr_data_mux_bus; | |
1424 | ||
1425 | /* wr_access */ | |
1426 | /* XXX: gpmc_capability check reqd ? , even if not, will not harm */ | |
1427 | gpmc_t->wr_access = gpmc_t->access; | |
1428 | ||
1429 | /* we_off */ | |
1430 | temp = gpmc_t->we_on + dev_t->t_wpl; | |
1431 | temp = max_t(u32, temp, | |
1432 | gpmc_t->wr_access + gpmc_ticks_to_ps(1)); | |
1433 | temp = max_t(u32, temp, | |
1434 | gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl)); | |
1435 | gpmc_t->we_off = gpmc_round_ps_to_ticks(temp); | |
1436 | ||
1437 | gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off + | |
1438 | dev_t->t_wph); | |
1439 | ||
1440 | /* wr_cycle */ | |
1441 | temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk); | |
1442 | temp += gpmc_t->wr_access; | |
1443 | /* XXX: barter t_ce_rdyz with t_cez_w ? */ | |
1444 | if (dev_t->t_ce_rdyz) | |
1445 | temp = max_t(u32, temp, | |
1446 | gpmc_t->cs_wr_off + dev_t->t_ce_rdyz); | |
1447 | gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp); | |
1448 | ||
1449 | return 0; | |
1450 | } | |
1451 | ||
1452 | static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t, | |
c3be5b45 JH |
1453 | struct gpmc_device_timings *dev_t, |
1454 | bool mux) | |
246da26d | 1455 | { |
246da26d AM |
1456 | u32 temp; |
1457 | ||
1458 | /* adv_rd_off */ | |
1459 | temp = dev_t->t_avdp_r; | |
1460 | if (mux) | |
1461 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); | |
1462 | gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp); | |
1463 | ||
1464 | /* oe_on */ | |
1465 | temp = dev_t->t_oeasu; | |
1466 | if (mux) | |
1467 | temp = max_t(u32, temp, | |
1468 | gpmc_t->adv_rd_off + dev_t->t_aavdh); | |
1469 | gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp); | |
1470 | ||
1471 | /* access */ | |
1472 | temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */ | |
1473 | gpmc_t->oe_on + dev_t->t_oe); | |
1474 | temp = max_t(u32, temp, | |
1475 | gpmc_t->cs_on + dev_t->t_ce); | |
1476 | temp = max_t(u32, temp, | |
1477 | gpmc_t->adv_on + dev_t->t_aa); | |
1478 | gpmc_t->access = gpmc_round_ps_to_ticks(temp); | |
1479 | ||
1480 | gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1); | |
1481 | gpmc_t->cs_rd_off = gpmc_t->oe_off; | |
1482 | ||
1483 | /* rd_cycle */ | |
1484 | temp = max_t(u32, dev_t->t_rd_cycle, | |
1485 | gpmc_t->cs_rd_off + dev_t->t_cez_r); | |
1486 | temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez); | |
1487 | gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp); | |
1488 | ||
1489 | return 0; | |
1490 | } | |
1491 | ||
1492 | static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t, | |
c3be5b45 JH |
1493 | struct gpmc_device_timings *dev_t, |
1494 | bool mux) | |
246da26d | 1495 | { |
246da26d AM |
1496 | u32 temp; |
1497 | ||
1498 | /* adv_wr_off */ | |
1499 | temp = dev_t->t_avdp_w; | |
1500 | if (mux) | |
1501 | temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp); | |
1502 | gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp); | |
1503 | ||
1504 | /* wr_data_mux_bus */ | |
1505 | temp = dev_t->t_weasu; | |
1506 | if (mux) { | |
1507 | temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh); | |
1508 | temp = max_t(u32, temp, gpmc_t->adv_wr_off + | |
1509 | gpmc_ticks_to_ps(dev_t->cyc_aavdh_we)); | |
1510 | } | |
1511 | gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp); | |
1512 | ||
1513 | /* we_on */ | |
1514 | if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS) | |
1515 | gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu); | |
1516 | else | |
1517 | gpmc_t->we_on = gpmc_t->wr_data_mux_bus; | |
1518 | ||
1519 | /* we_off */ | |
1520 | temp = gpmc_t->we_on + dev_t->t_wpl; | |
1521 | gpmc_t->we_off = gpmc_round_ps_to_ticks(temp); | |
1522 | ||
1523 | gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off + | |
1524 | dev_t->t_wph); | |
1525 | ||
1526 | /* wr_cycle */ | |
1527 | temp = max_t(u32, dev_t->t_wr_cycle, | |
1528 | gpmc_t->cs_wr_off + dev_t->t_cez_w); | |
1529 | gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp); | |
1530 | ||
1531 | return 0; | |
1532 | } | |
1533 | ||
1534 | static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t, | |
1535 | struct gpmc_device_timings *dev_t) | |
1536 | { | |
1537 | u32 temp; | |
1538 | ||
1539 | gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) * | |
1540 | gpmc_get_fclk_period(); | |
1541 | ||
1542 | gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk( | |
1543 | dev_t->t_bacc, | |
1544 | gpmc_t->sync_clk); | |
1545 | ||
1546 | temp = max_t(u32, dev_t->t_ces, dev_t->t_avds); | |
1547 | gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp); | |
1548 | ||
1549 | if (gpmc_calc_divider(gpmc_t->sync_clk) != 1) | |
1550 | return 0; | |
1551 | ||
1552 | if (dev_t->ce_xdelay) | |
1553 | gpmc_t->bool_timings.cs_extra_delay = true; | |
1554 | if (dev_t->avd_xdelay) | |
1555 | gpmc_t->bool_timings.adv_extra_delay = true; | |
1556 | if (dev_t->oe_xdelay) | |
1557 | gpmc_t->bool_timings.oe_extra_delay = true; | |
1558 | if (dev_t->we_xdelay) | |
1559 | gpmc_t->bool_timings.we_extra_delay = true; | |
1560 | ||
1561 | return 0; | |
1562 | } | |
1563 | ||
1564 | static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t, | |
c3be5b45 JH |
1565 | struct gpmc_device_timings *dev_t, |
1566 | bool sync) | |
246da26d AM |
1567 | { |
1568 | u32 temp; | |
1569 | ||
1570 | /* cs_on */ | |
1571 | gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu); | |
1572 | ||
1573 | /* adv_on */ | |
1574 | temp = dev_t->t_avdasu; | |
1575 | if (dev_t->t_ce_avd) | |
1576 | temp = max_t(u32, temp, | |
1577 | gpmc_t->cs_on + dev_t->t_ce_avd); | |
1578 | gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp); | |
1579 | ||
c3be5b45 | 1580 | if (sync) |
246da26d AM |
1581 | gpmc_calc_sync_common_timings(gpmc_t, dev_t); |
1582 | ||
1583 | return 0; | |
1584 | } | |
1585 | ||
1586 | /* TODO: remove this function once all peripherals are confirmed to | |
1587 | * work with generic timing. Simultaneously gpmc_cs_set_timings() | |
1588 | * has to be modified to handle timings in ps instead of ns | |
1589 | */ | |
1590 | static void gpmc_convert_ps_to_ns(struct gpmc_timings *t) | |
1591 | { | |
1592 | t->cs_on /= 1000; | |
1593 | t->cs_rd_off /= 1000; | |
1594 | t->cs_wr_off /= 1000; | |
1595 | t->adv_on /= 1000; | |
1596 | t->adv_rd_off /= 1000; | |
1597 | t->adv_wr_off /= 1000; | |
1598 | t->we_on /= 1000; | |
1599 | t->we_off /= 1000; | |
1600 | t->oe_on /= 1000; | |
1601 | t->oe_off /= 1000; | |
1602 | t->page_burst_access /= 1000; | |
1603 | t->access /= 1000; | |
1604 | t->rd_cycle /= 1000; | |
1605 | t->wr_cycle /= 1000; | |
1606 | t->bus_turnaround /= 1000; | |
1607 | t->cycle2cycle_delay /= 1000; | |
1608 | t->wait_monitoring /= 1000; | |
1609 | t->clk_activation /= 1000; | |
1610 | t->wr_access /= 1000; | |
1611 | t->wr_data_mux_bus /= 1000; | |
1612 | } | |
1613 | ||
1614 | int gpmc_calc_timings(struct gpmc_timings *gpmc_t, | |
c3be5b45 JH |
1615 | struct gpmc_settings *gpmc_s, |
1616 | struct gpmc_device_timings *dev_t) | |
246da26d | 1617 | { |
c3be5b45 JH |
1618 | bool mux = false, sync = false; |
1619 | ||
1620 | if (gpmc_s) { | |
1621 | mux = gpmc_s->mux_add_data ? true : false; | |
1622 | sync = (gpmc_s->sync_read || gpmc_s->sync_write); | |
1623 | } | |
1624 | ||
246da26d AM |
1625 | memset(gpmc_t, 0, sizeof(*gpmc_t)); |
1626 | ||
c3be5b45 | 1627 | gpmc_calc_common_timings(gpmc_t, dev_t, sync); |
246da26d | 1628 | |
c3be5b45 JH |
1629 | if (gpmc_s && gpmc_s->sync_read) |
1630 | gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux); | |
246da26d | 1631 | else |
c3be5b45 | 1632 | gpmc_calc_async_read_timings(gpmc_t, dev_t, mux); |
246da26d | 1633 | |
c3be5b45 JH |
1634 | if (gpmc_s && gpmc_s->sync_write) |
1635 | gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux); | |
246da26d | 1636 | else |
c3be5b45 | 1637 | gpmc_calc_async_write_timings(gpmc_t, dev_t, mux); |
246da26d AM |
1638 | |
1639 | /* TODO: remove, see function definition */ | |
1640 | gpmc_convert_ps_to_ns(gpmc_t); | |
1641 | ||
1642 | return 0; | |
1643 | } | |
1644 | ||
aa8d4767 JH |
1645 | /** |
1646 | * gpmc_cs_program_settings - programs non-timing related settings | |
1647 | * @cs: GPMC chip-select to program | |
1648 | * @p: pointer to GPMC settings structure | |
1649 | * | |
1650 | * Programs non-timing related settings for a GPMC chip-select, such as | |
1651 | * bus-width, burst configuration, etc. Function should be called once | |
1652 | * for each chip-select that is being used and must be called before | |
1653 | * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1 | |
1654 | * register will be initialised to zero by this function. Returns 0 on | |
1655 | * success and appropriate negative error code on failure. | |
1656 | */ | |
1657 | int gpmc_cs_program_settings(int cs, struct gpmc_settings *p) | |
1658 | { | |
1659 | u32 config1; | |
1660 | ||
1661 | if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) { | |
1662 | pr_err("%s: invalid width %d!", __func__, p->device_width); | |
1663 | return -EINVAL; | |
1664 | } | |
1665 | ||
1666 | /* Address-data multiplexing not supported for NAND devices */ | |
1667 | if (p->device_nand && p->mux_add_data) { | |
1668 | pr_err("%s: invalid configuration!\n", __func__); | |
1669 | return -EINVAL; | |
1670 | } | |
1671 | ||
1672 | if ((p->mux_add_data > GPMC_MUX_AD) || | |
1673 | ((p->mux_add_data == GPMC_MUX_AAD) && | |
1674 | !(gpmc_capability & GPMC_HAS_MUX_AAD))) { | |
1675 | pr_err("%s: invalid multiplex configuration!\n", __func__); | |
1676 | return -EINVAL; | |
1677 | } | |
1678 | ||
1679 | /* Page/burst mode supports lengths of 4, 8 and 16 bytes */ | |
1680 | if (p->burst_read || p->burst_write) { | |
1681 | switch (p->burst_len) { | |
1682 | case GPMC_BURST_4: | |
1683 | case GPMC_BURST_8: | |
1684 | case GPMC_BURST_16: | |
1685 | break; | |
1686 | default: | |
1687 | pr_err("%s: invalid page/burst-length (%d)\n", | |
1688 | __func__, p->burst_len); | |
1689 | return -EINVAL; | |
1690 | } | |
1691 | } | |
1692 | ||
2b54057c | 1693 | if (p->wait_pin > gpmc_nr_waitpins) { |
aa8d4767 JH |
1694 | pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin); |
1695 | return -EINVAL; | |
1696 | } | |
1697 | ||
1698 | config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1)); | |
1699 | ||
1700 | if (p->sync_read) | |
1701 | config1 |= GPMC_CONFIG1_READTYPE_SYNC; | |
1702 | if (p->sync_write) | |
1703 | config1 |= GPMC_CONFIG1_WRITETYPE_SYNC; | |
1704 | if (p->wait_on_read) | |
1705 | config1 |= GPMC_CONFIG1_WAIT_READ_MON; | |
1706 | if (p->wait_on_write) | |
1707 | config1 |= GPMC_CONFIG1_WAIT_WRITE_MON; | |
1708 | if (p->wait_on_read || p->wait_on_write) | |
1709 | config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin); | |
1710 | if (p->device_nand) | |
1711 | config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND); | |
1712 | if (p->mux_add_data) | |
1713 | config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data); | |
1714 | if (p->burst_read) | |
1715 | config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP; | |
1716 | if (p->burst_write) | |
1717 | config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP; | |
1718 | if (p->burst_read || p->burst_write) { | |
1719 | config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3); | |
1720 | config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0; | |
1721 | } | |
1722 | ||
1723 | gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1); | |
1724 | ||
1725 | return 0; | |
1726 | } | |
1727 | ||
bc6b1e7b | 1728 | #ifdef CONFIG_OF |
31957609 | 1729 | static const struct of_device_id gpmc_dt_ids[] = { |
bc6b1e7b DM |
1730 | { .compatible = "ti,omap2420-gpmc" }, |
1731 | { .compatible = "ti,omap2430-gpmc" }, | |
1732 | { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */ | |
1733 | { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */ | |
1734 | { .compatible = "ti,am3352-gpmc" }, /* am335x devices */ | |
1735 | { } | |
1736 | }; | |
1737 | MODULE_DEVICE_TABLE(of, gpmc_dt_ids); | |
1738 | ||
8c8a7771 JH |
1739 | /** |
1740 | * gpmc_read_settings_dt - read gpmc settings from device-tree | |
1741 | * @np: pointer to device-tree node for a gpmc child device | |
1742 | * @p: pointer to gpmc settings structure | |
1743 | * | |
1744 | * Reads the GPMC settings for a GPMC child device from device-tree and | |
1745 | * stores them in the GPMC settings structure passed. The GPMC settings | |
1746 | * structure is initialised to zero by this function and so any | |
1747 | * previously stored settings will be cleared. | |
1748 | */ | |
1749 | void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p) | |
1750 | { | |
1751 | memset(p, 0, sizeof(struct gpmc_settings)); | |
1752 | ||
1753 | p->sync_read = of_property_read_bool(np, "gpmc,sync-read"); | |
1754 | p->sync_write = of_property_read_bool(np, "gpmc,sync-write"); | |
8c8a7771 JH |
1755 | of_property_read_u32(np, "gpmc,device-width", &p->device_width); |
1756 | of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data); | |
1757 | ||
1758 | if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) { | |
1759 | p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap"); | |
1760 | p->burst_read = of_property_read_bool(np, "gpmc,burst-read"); | |
1761 | p->burst_write = of_property_read_bool(np, "gpmc,burst-write"); | |
1762 | if (!p->burst_read && !p->burst_write) | |
1763 | pr_warn("%s: page/burst-length set but not used!\n", | |
1764 | __func__); | |
1765 | } | |
1766 | ||
1767 | if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) { | |
1768 | p->wait_on_read = of_property_read_bool(np, | |
1769 | "gpmc,wait-on-read"); | |
1770 | p->wait_on_write = of_property_read_bool(np, | |
1771 | "gpmc,wait-on-write"); | |
1772 | if (!p->wait_on_read && !p->wait_on_write) | |
2b54057c RQ |
1773 | pr_debug("%s: rd/wr wait monitoring not enabled!\n", |
1774 | __func__); | |
8c8a7771 JH |
1775 | } |
1776 | } | |
1777 | ||
bc6b1e7b DM |
1778 | static void __maybe_unused gpmc_read_timings_dt(struct device_node *np, |
1779 | struct gpmc_timings *gpmc_t) | |
1780 | { | |
d36b4cd4 JH |
1781 | struct gpmc_bool_timings *p; |
1782 | ||
1783 | if (!np || !gpmc_t) | |
1784 | return; | |
bc6b1e7b DM |
1785 | |
1786 | memset(gpmc_t, 0, sizeof(*gpmc_t)); | |
1787 | ||
1788 | /* minimum clock period for syncronous mode */ | |
d36b4cd4 | 1789 | of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk); |
bc6b1e7b DM |
1790 | |
1791 | /* chip select timtings */ | |
d36b4cd4 JH |
1792 | of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on); |
1793 | of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off); | |
1794 | of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off); | |
bc6b1e7b DM |
1795 | |
1796 | /* ADV signal timings */ | |
d36b4cd4 JH |
1797 | of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on); |
1798 | of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off); | |
1799 | of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off); | |
2c92c04b NA |
1800 | of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns", |
1801 | &gpmc_t->adv_aad_mux_on); | |
1802 | of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns", | |
1803 | &gpmc_t->adv_aad_mux_rd_off); | |
1804 | of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns", | |
1805 | &gpmc_t->adv_aad_mux_wr_off); | |
bc6b1e7b DM |
1806 | |
1807 | /* WE signal timings */ | |
d36b4cd4 JH |
1808 | of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on); |
1809 | of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off); | |
bc6b1e7b DM |
1810 | |
1811 | /* OE signal timings */ | |
d36b4cd4 JH |
1812 | of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on); |
1813 | of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off); | |
2c92c04b NA |
1814 | of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns", |
1815 | &gpmc_t->oe_aad_mux_on); | |
1816 | of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns", | |
1817 | &gpmc_t->oe_aad_mux_off); | |
bc6b1e7b DM |
1818 | |
1819 | /* access and cycle timings */ | |
d36b4cd4 JH |
1820 | of_property_read_u32(np, "gpmc,page-burst-access-ns", |
1821 | &gpmc_t->page_burst_access); | |
1822 | of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access); | |
1823 | of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle); | |
1824 | of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle); | |
1825 | of_property_read_u32(np, "gpmc,bus-turnaround-ns", | |
1826 | &gpmc_t->bus_turnaround); | |
1827 | of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns", | |
1828 | &gpmc_t->cycle2cycle_delay); | |
1829 | of_property_read_u32(np, "gpmc,wait-monitoring-ns", | |
1830 | &gpmc_t->wait_monitoring); | |
1831 | of_property_read_u32(np, "gpmc,clk-activation-ns", | |
1832 | &gpmc_t->clk_activation); | |
1833 | ||
1834 | /* only applicable to OMAP3+ */ | |
1835 | of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access); | |
1836 | of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns", | |
1837 | &gpmc_t->wr_data_mux_bus); | |
1838 | ||
1839 | /* bool timing parameters */ | |
1840 | p = &gpmc_t->bool_timings; | |
1841 | ||
1842 | p->cycle2cyclediffcsen = | |
1843 | of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen"); | |
1844 | p->cycle2cyclesamecsen = | |
1845 | of_property_read_bool(np, "gpmc,cycle2cycle-samecsen"); | |
1846 | p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay"); | |
1847 | p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay"); | |
1848 | p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay"); | |
1849 | p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay"); | |
1850 | p->time_para_granularity = | |
1851 | of_property_read_bool(np, "gpmc,time-para-granularity"); | |
bc6b1e7b DM |
1852 | } |
1853 | ||
980386d2 | 1854 | #if IS_ENABLED(CONFIG_MTD_ONENAND) |
75d3625e EG |
1855 | static int gpmc_probe_onenand_child(struct platform_device *pdev, |
1856 | struct device_node *child) | |
1857 | { | |
1858 | u32 val; | |
1859 | struct omap_onenand_platform_data *gpmc_onenand_data; | |
1860 | ||
1861 | if (of_property_read_u32(child, "reg", &val) < 0) { | |
1862 | dev_err(&pdev->dev, "%s has no 'reg' property\n", | |
1863 | child->full_name); | |
1864 | return -ENODEV; | |
1865 | } | |
1866 | ||
1867 | gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data), | |
1868 | GFP_KERNEL); | |
1869 | if (!gpmc_onenand_data) | |
1870 | return -ENOMEM; | |
1871 | ||
1872 | gpmc_onenand_data->cs = val; | |
1873 | gpmc_onenand_data->of_node = child; | |
1874 | gpmc_onenand_data->dma_channel = -1; | |
1875 | ||
1876 | if (!of_property_read_u32(child, "dma-channel", &val)) | |
1877 | gpmc_onenand_data->dma_channel = val; | |
1878 | ||
1879 | gpmc_onenand_init(gpmc_onenand_data); | |
1880 | ||
1881 | return 0; | |
1882 | } | |
1883 | #else | |
1884 | static int gpmc_probe_onenand_child(struct platform_device *pdev, | |
1885 | struct device_node *child) | |
1886 | { | |
1887 | return 0; | |
1888 | } | |
1889 | #endif | |
1890 | ||
cdd6928c | 1891 | /** |
3af91cf7 | 1892 | * gpmc_probe_generic_child - configures the gpmc for a child device |
cdd6928c | 1893 | * @pdev: pointer to gpmc platform device |
3af91cf7 | 1894 | * @child: pointer to device-tree node for child device |
cdd6928c | 1895 | * |
3af91cf7 | 1896 | * Allocates and configures a GPMC chip-select for a child device. |
cdd6928c JH |
1897 | * Returns 0 on success and appropriate negative error code on failure. |
1898 | */ | |
3af91cf7 | 1899 | static int gpmc_probe_generic_child(struct platform_device *pdev, |
cdd6928c JH |
1900 | struct device_node *child) |
1901 | { | |
1902 | struct gpmc_settings gpmc_s; | |
1903 | struct gpmc_timings gpmc_t; | |
1904 | struct resource res; | |
1905 | unsigned long base; | |
9ed7a776 | 1906 | const char *name; |
cdd6928c | 1907 | int ret, cs; |
e378d22b | 1908 | u32 val; |
cdd6928c JH |
1909 | |
1910 | if (of_property_read_u32(child, "reg", &cs) < 0) { | |
1911 | dev_err(&pdev->dev, "%s has no 'reg' property\n", | |
1912 | child->full_name); | |
1913 | return -ENODEV; | |
1914 | } | |
1915 | ||
1916 | if (of_address_to_resource(child, 0, &res) < 0) { | |
1917 | dev_err(&pdev->dev, "%s has malformed 'reg' property\n", | |
1918 | child->full_name); | |
1919 | return -ENODEV; | |
1920 | } | |
1921 | ||
9ed7a776 TL |
1922 | /* |
1923 | * Check if we have multiple instances of the same device | |
1924 | * on a single chip select. If so, use the already initialized | |
1925 | * timings. | |
1926 | */ | |
1927 | name = gpmc_cs_get_name(cs); | |
1928 | if (name && child->name && of_node_cmp(child->name, name) == 0) | |
1929 | goto no_timings; | |
1930 | ||
cdd6928c JH |
1931 | ret = gpmc_cs_request(cs, resource_size(&res), &base); |
1932 | if (ret < 0) { | |
1933 | dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs); | |
1934 | return ret; | |
1935 | } | |
9ed7a776 | 1936 | gpmc_cs_set_name(cs, child->name); |
cdd6928c | 1937 | |
35ac051e TL |
1938 | gpmc_read_settings_dt(child, &gpmc_s); |
1939 | gpmc_read_timings_dt(child, &gpmc_t); | |
cdd6928c | 1940 | |
fd4446f2 TL |
1941 | /* |
1942 | * For some GPMC devices we still need to rely on the bootloader | |
35ac051e TL |
1943 | * timings because the devices can be connected via FPGA. |
1944 | * REVISIT: Add timing support from slls644g.pdf. | |
fd4446f2 | 1945 | */ |
35ac051e TL |
1946 | if (!gpmc_t.cs_rd_off) { |
1947 | WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n", | |
1948 | cs); | |
1949 | gpmc_cs_show_timings(cs, | |
1950 | "please add GPMC bootloader timings to .dts"); | |
fd4446f2 TL |
1951 | goto no_timings; |
1952 | } | |
1953 | ||
4cf27d2e RQ |
1954 | /* CS must be disabled while making changes to gpmc configuration */ |
1955 | gpmc_cs_disable_mem(cs); | |
1956 | ||
cdd6928c JH |
1957 | /* |
1958 | * FIXME: gpmc_cs_request() will map the CS to an arbitary | |
1959 | * location in the gpmc address space. When booting with | |
1960 | * device-tree we want the NOR flash to be mapped to the | |
1961 | * location specified in the device-tree blob. So remap the | |
1962 | * CS to this location. Once DT migration is complete should | |
1963 | * just make gpmc_cs_request() map a specific address. | |
1964 | */ | |
1965 | ret = gpmc_cs_remap(cs, res.start); | |
1966 | if (ret < 0) { | |
f70bf2a3 FE |
1967 | dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n", |
1968 | cs, &res.start); | |
cdd6928c JH |
1969 | goto err; |
1970 | } | |
1971 | ||
c9711ec5 RQ |
1972 | if (of_node_cmp(child->name, "nand") == 0) { |
1973 | /* Warn about older DT blobs with no compatible property */ | |
1974 | if (!of_property_read_bool(child, "compatible")) { | |
1975 | dev_warn(&pdev->dev, | |
1976 | "Incompatible NAND node: missing compatible"); | |
1977 | ret = -EINVAL; | |
1978 | goto err; | |
1979 | } | |
1980 | } | |
1981 | ||
1982 | if (of_device_is_compatible(child, "ti,omap2-nand")) { | |
1983 | /* NAND specific setup */ | |
1984 | val = of_get_nand_bus_width(child); | |
1985 | switch (val) { | |
1986 | case 8: | |
1987 | gpmc_s.device_width = GPMC_DEVWIDTH_8BIT; | |
1988 | break; | |
1989 | case 16: | |
1990 | gpmc_s.device_width = GPMC_DEVWIDTH_16BIT; | |
1991 | break; | |
1992 | default: | |
1993 | dev_err(&pdev->dev, "%s: invalid 'nand-bus-width'\n", | |
1994 | child->name); | |
1995 | ret = -EINVAL; | |
1996 | goto err; | |
1997 | } | |
1998 | ||
1999 | /* disable write protect */ | |
2000 | gpmc_configure(GPMC_CONFIG_WP, 0); | |
2001 | gpmc_s.device_nand = true; | |
2002 | } else { | |
2003 | ret = of_property_read_u32(child, "bank-width", | |
2004 | &gpmc_s.device_width); | |
2005 | if (ret < 0) | |
2006 | goto err; | |
2007 | } | |
cdd6928c | 2008 | |
fd820a1e | 2009 | gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings"); |
cdd6928c JH |
2010 | ret = gpmc_cs_program_settings(cs, &gpmc_s); |
2011 | if (ret < 0) | |
2012 | goto err; | |
2013 | ||
2e676901 | 2014 | ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s); |
7604baf3 RQ |
2015 | if (ret) { |
2016 | dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n", | |
2017 | child->name); | |
2018 | goto err; | |
2019 | } | |
cdd6928c | 2020 | |
e378d22b RQ |
2021 | /* Clear limited address i.e. enable A26-A11 */ |
2022 | val = gpmc_read_reg(GPMC_CONFIG); | |
2023 | val &= ~GPMC_CONFIG_LIMITEDADDRESS; | |
2024 | gpmc_write_reg(GPMC_CONFIG, val); | |
2025 | ||
4cf27d2e RQ |
2026 | /* Enable CS region */ |
2027 | gpmc_cs_enable_mem(cs); | |
cdd6928c | 2028 | |
fd4446f2 | 2029 | no_timings: |
b1dc1ca9 RA |
2030 | |
2031 | /* create platform device, NULL on error or when disabled */ | |
2032 | if (!of_platform_device_create(child, NULL, &pdev->dev)) | |
2033 | goto err_child_fail; | |
2034 | ||
2035 | /* is child a common bus? */ | |
2036 | if (of_match_node(of_default_bus_match_table, child)) | |
2037 | /* create children and other common bus children */ | |
2038 | if (of_platform_populate(child, of_default_bus_match_table, | |
2039 | NULL, &pdev->dev)) | |
2040 | goto err_child_fail; | |
2041 | ||
2042 | return 0; | |
2043 | ||
2044 | err_child_fail: | |
cdd6928c JH |
2045 | |
2046 | dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name); | |
e8ffd6fd | 2047 | ret = -ENODEV; |
cdd6928c JH |
2048 | |
2049 | err: | |
2050 | gpmc_cs_free(cs); | |
2051 | ||
2052 | return ret; | |
2053 | } | |
2054 | ||
bc6b1e7b DM |
2055 | static int gpmc_probe_dt(struct platform_device *pdev) |
2056 | { | |
2057 | int ret; | |
2058 | struct device_node *child; | |
2059 | const struct of_device_id *of_id = | |
2060 | of_match_device(gpmc_dt_ids, &pdev->dev); | |
2061 | ||
2062 | if (!of_id) | |
2063 | return 0; | |
2064 | ||
f34f3716 GP |
2065 | ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs", |
2066 | &gpmc_cs_num); | |
2067 | if (ret < 0) { | |
2068 | pr_err("%s: number of chip-selects not defined\n", __func__); | |
2069 | return ret; | |
2070 | } else if (gpmc_cs_num < 1) { | |
2071 | pr_err("%s: all chip-selects are disabled\n", __func__); | |
2072 | return -EINVAL; | |
2073 | } else if (gpmc_cs_num > GPMC_CS_NUM) { | |
2074 | pr_err("%s: number of supported chip-selects cannot be > %d\n", | |
2075 | __func__, GPMC_CS_NUM); | |
2076 | return -EINVAL; | |
2077 | } | |
2078 | ||
9f833156 JH |
2079 | ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins", |
2080 | &gpmc_nr_waitpins); | |
2081 | if (ret < 0) { | |
2082 | pr_err("%s: number of wait pins not found!\n", __func__); | |
2083 | return ret; | |
2084 | } | |
2085 | ||
68e2eb53 | 2086 | for_each_available_child_of_node(pdev->dev.of_node, child) { |
bc6b1e7b | 2087 | |
f2b09f67 JMC |
2088 | if (!child->name) |
2089 | continue; | |
cdd6928c | 2090 | |
c9711ec5 | 2091 | if (of_node_cmp(child->name, "onenand") == 0) |
f2b09f67 | 2092 | ret = gpmc_probe_onenand_child(pdev, child); |
28a7eedd | 2093 | else |
f2b09f67 | 2094 | ret = gpmc_probe_generic_child(pdev, child); |
5330dc16 JMC |
2095 | } |
2096 | ||
bc6b1e7b DM |
2097 | return 0; |
2098 | } | |
2099 | #else | |
2100 | static int gpmc_probe_dt(struct platform_device *pdev) | |
2101 | { | |
2102 | return 0; | |
2103 | } | |
2104 | #endif | |
2105 | ||
351a102d | 2106 | static int gpmc_probe(struct platform_device *pdev) |
4bbbc1ad | 2107 | { |
8119024e | 2108 | int rc; |
6b6c32fc | 2109 | u32 l; |
da496873 | 2110 | struct resource *res; |
384258f2 RQ |
2111 | struct gpmc_device *gpmc; |
2112 | ||
2113 | gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL); | |
2114 | if (!gpmc) | |
2115 | return -ENOMEM; | |
2116 | ||
2117 | gpmc->dev = &pdev->dev; | |
2118 | platform_set_drvdata(pdev, gpmc); | |
4bbbc1ad | 2119 | |
da496873 AM |
2120 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
2121 | if (res == NULL) | |
2122 | return -ENOENT; | |
8d08436d | 2123 | |
da496873 AM |
2124 | phys_base = res->start; |
2125 | mem_size = resource_size(res); | |
fd1dc87d | 2126 | |
5857bd98 TR |
2127 | gpmc_base = devm_ioremap_resource(&pdev->dev, res); |
2128 | if (IS_ERR(gpmc_base)) | |
2129 | return PTR_ERR(gpmc_base); | |
da496873 AM |
2130 | |
2131 | res = platform_get_resource(pdev, IORESOURCE_IRQ, 0); | |
384258f2 RQ |
2132 | if (!res) { |
2133 | dev_err(&pdev->dev, "Failed to get resource: irq\n"); | |
2134 | return -ENOENT; | |
2135 | } | |
2136 | ||
2137 | gpmc->irq = res->start; | |
da496873 | 2138 | |
8bf9be56 | 2139 | gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck"); |
da496873 | 2140 | if (IS_ERR(gpmc_l3_clk)) { |
8bf9be56 | 2141 | dev_err(&pdev->dev, "Failed to get GPMC fck\n"); |
da496873 | 2142 | return PTR_ERR(gpmc_l3_clk); |
fd1dc87d PW |
2143 | } |
2144 | ||
8bf9be56 RQ |
2145 | if (!clk_get_rate(gpmc_l3_clk)) { |
2146 | dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n"); | |
2147 | return -EINVAL; | |
2148 | } | |
2149 | ||
b3f5525c | 2150 | pm_runtime_enable(&pdev->dev); |
2151 | pm_runtime_get_sync(&pdev->dev); | |
1daa8c1d | 2152 | |
4bbbc1ad | 2153 | l = gpmc_read_reg(GPMC_REVISION); |
aa8d4767 JH |
2154 | |
2155 | /* | |
2156 | * FIXME: Once device-tree migration is complete the below flags | |
2157 | * should be populated based upon the device-tree compatible | |
2158 | * string. For now just use the IP revision. OMAP3+ devices have | |
2159 | * the wr_access and wr_data_mux_bus register fields. OMAP4+ | |
2160 | * devices support the addr-addr-data multiplex protocol. | |
2161 | * | |
2162 | * GPMC IP revisions: | |
2163 | * - OMAP24xx = 2.0 | |
2164 | * - OMAP3xxx = 5.0 | |
2165 | * - OMAP44xx/54xx/AM335x = 6.0 | |
2166 | */ | |
da496873 AM |
2167 | if (GPMC_REVISION_MAJOR(l) > 0x4) |
2168 | gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS; | |
aa8d4767 JH |
2169 | if (GPMC_REVISION_MAJOR(l) > 0x5) |
2170 | gpmc_capability |= GPMC_HAS_MUX_AAD; | |
384258f2 | 2171 | dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l), |
da496873 AM |
2172 | GPMC_REVISION_MINOR(l)); |
2173 | ||
84b00f0e | 2174 | gpmc_mem_init(); |
db97eb7d | 2175 | |
384258f2 RQ |
2176 | rc = gpmc_setup_irq(gpmc); |
2177 | if (rc) { | |
2178 | dev_err(gpmc->dev, "gpmc_setup_irq failed\n"); | |
2179 | goto fail; | |
2180 | } | |
da496873 | 2181 | |
f34f3716 GP |
2182 | if (!pdev->dev.of_node) { |
2183 | gpmc_cs_num = GPMC_CS_NUM; | |
9f833156 | 2184 | gpmc_nr_waitpins = GPMC_NR_WAITPINS; |
f34f3716 | 2185 | } |
9f833156 | 2186 | |
bc6b1e7b DM |
2187 | rc = gpmc_probe_dt(pdev); |
2188 | if (rc < 0) { | |
384258f2 RQ |
2189 | dev_err(gpmc->dev, "failed to probe DT parameters\n"); |
2190 | gpmc_free_irq(gpmc); | |
2191 | goto fail; | |
bc6b1e7b DM |
2192 | } |
2193 | ||
da496873 | 2194 | return 0; |
384258f2 RQ |
2195 | |
2196 | fail: | |
2197 | pm_runtime_put_sync(&pdev->dev); | |
2198 | return rc; | |
da496873 AM |
2199 | } |
2200 | ||
351a102d | 2201 | static int gpmc_remove(struct platform_device *pdev) |
da496873 | 2202 | { |
384258f2 RQ |
2203 | struct gpmc_device *gpmc = platform_get_drvdata(pdev); |
2204 | ||
2205 | gpmc_free_irq(gpmc); | |
da496873 | 2206 | gpmc_mem_exit(); |
b3f5525c | 2207 | pm_runtime_put_sync(&pdev->dev); |
2208 | pm_runtime_disable(&pdev->dev); | |
384258f2 | 2209 | |
da496873 AM |
2210 | return 0; |
2211 | } | |
2212 | ||
b536dd41 | 2213 | #ifdef CONFIG_PM_SLEEP |
2214 | static int gpmc_suspend(struct device *dev) | |
2215 | { | |
2216 | omap3_gpmc_save_context(); | |
2217 | pm_runtime_put_sync(dev); | |
2218 | return 0; | |
2219 | } | |
2220 | ||
2221 | static int gpmc_resume(struct device *dev) | |
2222 | { | |
2223 | pm_runtime_get_sync(dev); | |
2224 | omap3_gpmc_restore_context(); | |
2225 | return 0; | |
2226 | } | |
2227 | #endif | |
2228 | ||
2229 | static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume); | |
2230 | ||
da496873 AM |
2231 | static struct platform_driver gpmc_driver = { |
2232 | .probe = gpmc_probe, | |
351a102d | 2233 | .remove = gpmc_remove, |
da496873 AM |
2234 | .driver = { |
2235 | .name = DEVICE_NAME, | |
bc6b1e7b | 2236 | .of_match_table = of_match_ptr(gpmc_dt_ids), |
b536dd41 | 2237 | .pm = &gpmc_pm_ops, |
da496873 AM |
2238 | }, |
2239 | }; | |
2240 | ||
2241 | static __init int gpmc_init(void) | |
2242 | { | |
2243 | return platform_driver_register(&gpmc_driver); | |
2244 | } | |
2245 | ||
2246 | static __exit void gpmc_exit(void) | |
2247 | { | |
2248 | platform_driver_unregister(&gpmc_driver); | |
2249 | ||
db97eb7d | 2250 | } |
da496873 | 2251 | |
a8612809 | 2252 | postcore_initcall(gpmc_init); |
da496873 | 2253 | module_exit(gpmc_exit); |
db97eb7d | 2254 | |
a2d3e7ba RN |
2255 | static struct omap3_gpmc_regs gpmc_context; |
2256 | ||
b2fa3b7c | 2257 | void omap3_gpmc_save_context(void) |
a2d3e7ba RN |
2258 | { |
2259 | int i; | |
b2fa3b7c | 2260 | |
e984a179 TV |
2261 | if (!gpmc_base) |
2262 | return; | |
2263 | ||
a2d3e7ba RN |
2264 | gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG); |
2265 | gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE); | |
2266 | gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL); | |
2267 | gpmc_context.config = gpmc_read_reg(GPMC_CONFIG); | |
2268 | gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1); | |
2269 | gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2); | |
2270 | gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL); | |
f34f3716 | 2271 | for (i = 0; i < gpmc_cs_num; i++) { |
a2d3e7ba RN |
2272 | gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i); |
2273 | if (gpmc_context.cs_context[i].is_valid) { | |
2274 | gpmc_context.cs_context[i].config1 = | |
2275 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG1); | |
2276 | gpmc_context.cs_context[i].config2 = | |
2277 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG2); | |
2278 | gpmc_context.cs_context[i].config3 = | |
2279 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG3); | |
2280 | gpmc_context.cs_context[i].config4 = | |
2281 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG4); | |
2282 | gpmc_context.cs_context[i].config5 = | |
2283 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG5); | |
2284 | gpmc_context.cs_context[i].config6 = | |
2285 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG6); | |
2286 | gpmc_context.cs_context[i].config7 = | |
2287 | gpmc_cs_read_reg(i, GPMC_CS_CONFIG7); | |
2288 | } | |
2289 | } | |
2290 | } | |
2291 | ||
b2fa3b7c | 2292 | void omap3_gpmc_restore_context(void) |
a2d3e7ba RN |
2293 | { |
2294 | int i; | |
b2fa3b7c | 2295 | |
e984a179 TV |
2296 | if (!gpmc_base) |
2297 | return; | |
2298 | ||
a2d3e7ba RN |
2299 | gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig); |
2300 | gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable); | |
2301 | gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl); | |
2302 | gpmc_write_reg(GPMC_CONFIG, gpmc_context.config); | |
2303 | gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1); | |
2304 | gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2); | |
2305 | gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control); | |
f34f3716 | 2306 | for (i = 0; i < gpmc_cs_num; i++) { |
a2d3e7ba RN |
2307 | if (gpmc_context.cs_context[i].is_valid) { |
2308 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG1, | |
2309 | gpmc_context.cs_context[i].config1); | |
2310 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG2, | |
2311 | gpmc_context.cs_context[i].config2); | |
2312 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG3, | |
2313 | gpmc_context.cs_context[i].config3); | |
2314 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG4, | |
2315 | gpmc_context.cs_context[i].config4); | |
2316 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG5, | |
2317 | gpmc_context.cs_context[i].config5); | |
2318 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG6, | |
2319 | gpmc_context.cs_context[i].config6); | |
2320 | gpmc_cs_write_reg(i, GPMC_CS_CONFIG7, | |
2321 | gpmc_context.cs_context[i].config7); | |
2322 | } | |
2323 | } | |
2324 | } |