wl1251: use wiphy_dev instead of wl->spi->dev
[linux/fpc-iii.git] / arch / arm / mach-omap2 / gpmc.c
blobf91934b2b092dc7ca199f853de520637baa32d08
1 /*
2 * GPMC support functions
4 * Copyright (C) 2005-2006 Nokia Corporation
6 * Author: Juha Yrjola
8 * Copyright (C) 2009 Texas Instruments
9 * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
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.
15 #undef DEBUG
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/err.h>
20 #include <linux/clk.h>
21 #include <linux/ioport.h>
22 #include <linux/spinlock.h>
23 #include <linux/io.h>
24 #include <linux/module.h>
26 #include <asm/mach-types.h>
27 #include <mach/gpmc.h>
29 #include <mach/sdrc.h>
31 /* GPMC register offsets */
32 #define GPMC_REVISION 0x00
33 #define GPMC_SYSCONFIG 0x10
34 #define GPMC_SYSSTATUS 0x14
35 #define GPMC_IRQSTATUS 0x18
36 #define GPMC_IRQENABLE 0x1c
37 #define GPMC_TIMEOUT_CONTROL 0x40
38 #define GPMC_ERR_ADDRESS 0x44
39 #define GPMC_ERR_TYPE 0x48
40 #define GPMC_CONFIG 0x50
41 #define GPMC_STATUS 0x54
42 #define GPMC_PREFETCH_CONFIG1 0x1e0
43 #define GPMC_PREFETCH_CONFIG2 0x1e4
44 #define GPMC_PREFETCH_CONTROL 0x1ec
45 #define GPMC_PREFETCH_STATUS 0x1f0
46 #define GPMC_ECC_CONFIG 0x1f4
47 #define GPMC_ECC_CONTROL 0x1f8
48 #define GPMC_ECC_SIZE_CONFIG 0x1fc
50 #define GPMC_CS0 0x60
51 #define GPMC_CS_SIZE 0x30
53 #define GPMC_MEM_START 0x00000000
54 #define GPMC_MEM_END 0x3FFFFFFF
55 #define BOOT_ROM_SPACE 0x100000 /* 1MB */
57 #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
58 #define GPMC_SECTION_SHIFT 28 /* 128 MB */
60 static struct resource gpmc_mem_root;
61 static struct resource gpmc_cs_mem[GPMC_CS_NUM];
62 static DEFINE_SPINLOCK(gpmc_mem_lock);
63 static unsigned gpmc_cs_map;
65 static void __iomem *gpmc_base;
67 static struct clk *gpmc_l3_clk;
69 static void gpmc_write_reg(int idx, u32 val)
71 __raw_writel(val, gpmc_base + idx);
74 static u32 gpmc_read_reg(int idx)
76 return __raw_readl(gpmc_base + idx);
79 void gpmc_cs_write_reg(int cs, int idx, u32 val)
81 void __iomem *reg_addr;
83 reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
84 __raw_writel(val, reg_addr);
87 u32 gpmc_cs_read_reg(int cs, int idx)
89 void __iomem *reg_addr;
91 reg_addr = gpmc_base + GPMC_CS0 + (cs * GPMC_CS_SIZE) + idx;
92 return __raw_readl(reg_addr);
95 /* TODO: Add support for gpmc_fck to clock framework and use it */
96 unsigned long gpmc_get_fclk_period(void)
98 unsigned long rate = clk_get_rate(gpmc_l3_clk);
100 if (rate == 0) {
101 printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
102 return 0;
105 rate /= 1000;
106 rate = 1000000000 / rate; /* In picoseconds */
108 return rate;
111 unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
113 unsigned long tick_ps;
115 /* Calculate in picosecs to yield more exact results */
116 tick_ps = gpmc_get_fclk_period();
118 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
121 unsigned int gpmc_ticks_to_ns(unsigned int ticks)
123 return ticks * gpmc_get_fclk_period() / 1000;
126 unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
128 unsigned long ticks = gpmc_ns_to_ticks(time_ns);
130 return ticks * gpmc_get_fclk_period() / 1000;
133 #ifdef DEBUG
134 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
135 int time, const char *name)
136 #else
137 static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
138 int time)
139 #endif
141 u32 l;
142 int ticks, mask, nr_bits;
144 if (time == 0)
145 ticks = 0;
146 else
147 ticks = gpmc_ns_to_ticks(time);
148 nr_bits = end_bit - st_bit + 1;
149 if (ticks >= 1 << nr_bits) {
150 #ifdef DEBUG
151 printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
152 cs, name, time, ticks, 1 << nr_bits);
153 #endif
154 return -1;
157 mask = (1 << nr_bits) - 1;
158 l = gpmc_cs_read_reg(cs, reg);
159 #ifdef DEBUG
160 printk(KERN_INFO
161 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
162 cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
163 (l >> st_bit) & mask, time);
164 #endif
165 l &= ~(mask << st_bit);
166 l |= ticks << st_bit;
167 gpmc_cs_write_reg(cs, reg, l);
169 return 0;
172 #ifdef DEBUG
173 #define GPMC_SET_ONE(reg, st, end, field) \
174 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
175 t->field, #field) < 0) \
176 return -1
177 #else
178 #define GPMC_SET_ONE(reg, st, end, field) \
179 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
180 return -1
181 #endif
183 int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
185 int div;
186 u32 l;
188 l = sync_clk * 1000 + (gpmc_get_fclk_period() - 1);
189 div = l / gpmc_get_fclk_period();
190 if (div > 4)
191 return -1;
192 if (div <= 0)
193 div = 1;
195 return div;
198 int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
200 int div;
201 u32 l;
203 div = gpmc_cs_calc_divider(cs, t->sync_clk);
204 if (div < 0)
205 return -1;
207 GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
208 GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
209 GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
211 GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
212 GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
213 GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
215 GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
216 GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
217 GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
218 GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
220 GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
221 GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
222 GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
224 GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
226 if (cpu_is_omap34xx()) {
227 GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
228 GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
231 /* caller is expected to have initialized CONFIG1 to cover
232 * at least sync vs async
234 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
235 if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
236 #ifdef DEBUG
237 printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
238 cs, (div * gpmc_get_fclk_period()) / 1000, div);
239 #endif
240 l &= ~0x03;
241 l |= (div - 1);
242 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
245 return 0;
248 static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
250 u32 l;
251 u32 mask;
253 mask = (1 << GPMC_SECTION_SHIFT) - size;
254 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
255 l &= ~0x3f;
256 l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
257 l &= ~(0x0f << 8);
258 l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
259 l |= 1 << 6; /* CSVALID */
260 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
263 static void gpmc_cs_disable_mem(int cs)
265 u32 l;
267 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
268 l &= ~(1 << 6); /* CSVALID */
269 gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
272 static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
274 u32 l;
275 u32 mask;
277 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
278 *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
279 mask = (l >> 8) & 0x0f;
280 *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
283 static int gpmc_cs_mem_enabled(int cs)
285 u32 l;
287 l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
288 return l & (1 << 6);
291 int gpmc_cs_set_reserved(int cs, int reserved)
293 if (cs > GPMC_CS_NUM)
294 return -ENODEV;
296 gpmc_cs_map &= ~(1 << cs);
297 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
299 return 0;
302 int gpmc_cs_reserved(int cs)
304 if (cs > GPMC_CS_NUM)
305 return -ENODEV;
307 return gpmc_cs_map & (1 << cs);
310 static unsigned long gpmc_mem_align(unsigned long size)
312 int order;
314 size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
315 order = GPMC_CHUNK_SHIFT - 1;
316 do {
317 size >>= 1;
318 order++;
319 } while (size);
320 size = 1 << order;
321 return size;
324 static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
326 struct resource *res = &gpmc_cs_mem[cs];
327 int r;
329 size = gpmc_mem_align(size);
330 spin_lock(&gpmc_mem_lock);
331 res->start = base;
332 res->end = base + size - 1;
333 r = request_resource(&gpmc_mem_root, res);
334 spin_unlock(&gpmc_mem_lock);
336 return r;
339 int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
341 struct resource *res = &gpmc_cs_mem[cs];
342 int r = -1;
344 if (cs > GPMC_CS_NUM)
345 return -ENODEV;
347 size = gpmc_mem_align(size);
348 if (size > (1 << GPMC_SECTION_SHIFT))
349 return -ENOMEM;
351 spin_lock(&gpmc_mem_lock);
352 if (gpmc_cs_reserved(cs)) {
353 r = -EBUSY;
354 goto out;
356 if (gpmc_cs_mem_enabled(cs))
357 r = adjust_resource(res, res->start & ~(size - 1), size);
358 if (r < 0)
359 r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
360 size, NULL, NULL);
361 if (r < 0)
362 goto out;
364 gpmc_cs_enable_mem(cs, res->start, res->end - res->start + 1);
365 *base = res->start;
366 gpmc_cs_set_reserved(cs, 1);
367 out:
368 spin_unlock(&gpmc_mem_lock);
369 return r;
371 EXPORT_SYMBOL(gpmc_cs_request);
373 void gpmc_cs_free(int cs)
375 spin_lock(&gpmc_mem_lock);
376 if (cs >= GPMC_CS_NUM || !gpmc_cs_reserved(cs)) {
377 printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
378 BUG();
379 spin_unlock(&gpmc_mem_lock);
380 return;
382 gpmc_cs_disable_mem(cs);
383 release_resource(&gpmc_cs_mem[cs]);
384 gpmc_cs_set_reserved(cs, 0);
385 spin_unlock(&gpmc_mem_lock);
387 EXPORT_SYMBOL(gpmc_cs_free);
389 static void __init gpmc_mem_init(void)
391 int cs;
392 unsigned long boot_rom_space = 0;
394 /* never allocate the first page, to facilitate bug detection;
395 * even if we didn't boot from ROM.
397 boot_rom_space = BOOT_ROM_SPACE;
398 /* In apollon the CS0 is mapped as 0x0000 0000 */
399 if (machine_is_omap_apollon())
400 boot_rom_space = 0;
401 gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
402 gpmc_mem_root.end = GPMC_MEM_END;
404 /* Reserve all regions that has been set up by bootloader */
405 for (cs = 0; cs < GPMC_CS_NUM; cs++) {
406 u32 base, size;
408 if (!gpmc_cs_mem_enabled(cs))
409 continue;
410 gpmc_cs_get_memconf(cs, &base, &size);
411 if (gpmc_cs_insert_mem(cs, base, size) < 0)
412 BUG();
416 void __init gpmc_init(void)
418 u32 l;
419 char *ck;
421 if (cpu_is_omap24xx()) {
422 ck = "core_l3_ck";
423 if (cpu_is_omap2420())
424 l = OMAP2420_GPMC_BASE;
425 else
426 l = OMAP34XX_GPMC_BASE;
427 } else if (cpu_is_omap34xx()) {
428 ck = "gpmc_fck";
429 l = OMAP34XX_GPMC_BASE;
430 } else if (cpu_is_omap44xx()) {
431 ck = "gpmc_fck";
432 l = OMAP44XX_GPMC_BASE;
435 gpmc_l3_clk = clk_get(NULL, ck);
436 if (IS_ERR(gpmc_l3_clk)) {
437 printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
438 BUG();
441 gpmc_base = ioremap(l, SZ_4K);
442 if (!gpmc_base) {
443 clk_put(gpmc_l3_clk);
444 printk(KERN_ERR "Could not get GPMC register memory\n");
445 BUG();
448 l = gpmc_read_reg(GPMC_REVISION);
449 printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
450 /* Set smart idle mode and automatic L3 clock gating */
451 l = gpmc_read_reg(GPMC_SYSCONFIG);
452 l &= 0x03 << 3;
453 l |= (0x02 << 3) | (1 << 0);
454 gpmc_write_reg(GPMC_SYSCONFIG, l);
456 gpmc_mem_init();