18 #include <linux/kernel.h>
25 #include <linux/module.h>
40 #define DEVICE_NAME "omap-gpmc"
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
55 #define GPMC_PREFETCH_CONTROL 0x1ec
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
60 #define GPMC_ECC1_RESULT 0x200
61 #define GPMC_ECC_BCH_RESULT_0 0x240
64 #define GPMC_ECC_CTRL_ECCCLEAR 0x100
65 #define GPMC_ECC_CTRL_ECCDISABLE 0x000
66 #define GPMC_ECC_CTRL_ECCREG1 0x001
67 #define GPMC_ECC_CTRL_ECCREG2 0x002
68 #define GPMC_ECC_CTRL_ECCREG3 0x003
69 #define GPMC_ECC_CTRL_ECCREG4 0x004
70 #define GPMC_ECC_CTRL_ECCREG5 0x005
71 #define GPMC_ECC_CTRL_ECCREG6 0x006
72 #define GPMC_ECC_CTRL_ECCREG7 0x007
73 #define GPMC_ECC_CTRL_ECCREG8 0x008
74 #define GPMC_ECC_CTRL_ECCREG9 0x009
76 #define GPMC_CS0_OFFSET 0x60
77 #define GPMC_CS_SIZE 0x30
79 #define GPMC_MEM_START 0x00000000
80 #define GPMC_MEM_END 0x3FFFFFFF
81 #define BOOT_ROM_SPACE 0x100000
83 #define GPMC_CHUNK_SHIFT 24
84 #define GPMC_SECTION_SHIFT 28
86 #define CS_NUM_SHIFT 24
87 #define ENABLE_PREFETCH (0x1 << 7)
88 #define DMA_MPU_MODE 2
90 #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
91 #define GPMC_REVISION_MINOR(l) (l & 0xf)
93 #define GPMC_HAS_WR_ACCESS 0x1
94 #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
133 static struct irq_chip gpmc_irq_chip;
134 static unsigned gpmc_irq_start;
136 static struct resource gpmc_mem_root;
139 static unsigned int gpmc_cs_map;
140 static int gpmc_ecc_used = -
EINVAL;
141 static struct device *gpmc_dev;
144 static unsigned gpmc_capability;
145 static void __iomem *gpmc_base;
147 static struct clk *gpmc_l3_clk;
151 static void gpmc_write_reg(
int idx,
u32 val)
156 static u32 gpmc_read_reg(
int idx)
161 static void gpmc_cs_write_byte(
int cs,
int idx,
u8 val)
169 static u8 gpmc_cs_read_byte(
int cs,
int idx)
204 rate = 1000000000 /
rate;
211 unsigned long tick_ps;
216 return (time_ns * 1000 + tick_ps - 1) / tick_ps;
221 unsigned long tick_ps;
226 return (time_ps + tick_ps - 1) / tick_ps;
242 static int set_gpmc_timing_reg(
int cs,
int reg,
int st_bit,
int end_bit,
245 static int set_gpmc_timing_reg(
int cs,
int reg,
int st_bit,
int end_bit,
256 nr_bits = end_bit - st_bit + 1;
257 if (ticks >= 1 << nr_bits) {
260 cs, name, time, ticks, 1 << nr_bits);
265 mask = (1 << nr_bits) - 1;
269 "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
271 (l >> st_bit) & mask, time);
273 l &= ~(mask << st_bit);
274 l |= ticks << st_bit;
281 #define GPMC_SET_ONE(reg, st, end, field) \
282 if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
283 t->field, #field) < 0) \
286 #define GPMC_SET_ONE(reg, st, end, field) \
287 if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
356 static void gpmc_cs_enable_mem(
int cs,
u32 base,
u32 size)
371 static void gpmc_cs_disable_mem(
int cs)
380 static void gpmc_cs_get_memconf(
int cs,
u32 *base,
u32 *
size)
387 mask = (l >> 8) & 0x0f;
391 static int gpmc_cs_mem_enabled(
int cs)
404 gpmc_cs_map &= ~(1 <<
cs);
405 gpmc_cs_map |= (reserved ? 1 : 0) << cs;
415 return gpmc_cs_map & (1 <<
cs);
418 static unsigned long gpmc_mem_align(
unsigned long size)
432 static int gpmc_cs_insert_mem(
int cs,
unsigned long base,
unsigned long size)
437 size = gpmc_mem_align(size);
438 spin_lock(&gpmc_mem_lock);
440 res->
end = base + size - 1;
442 spin_unlock(&gpmc_mem_lock);
447 static int gpmc_cs_delete_mem(
int cs)
452 spin_lock(&gpmc_mem_lock);
456 spin_unlock(&gpmc_mem_lock);
469 size = gpmc_mem_align(size);
473 spin_lock(&gpmc_mem_lock);
478 if (gpmc_cs_mem_enabled(cs))
486 gpmc_cs_enable_mem(cs, res->
start, resource_size(res));
490 spin_unlock(&gpmc_mem_lock);
497 spin_lock(&gpmc_mem_lock);
501 spin_unlock(&gpmc_mem_lock);
504 gpmc_cs_disable_mem(cs);
507 spin_unlock(&gpmc_mem_lock);
679 unsigned int u32_count,
int is_write)
683 pr_err(
"gpmc: fifo threshold is not supported\n");
764 static int gpmc_irq_endis(
unsigned irq,
bool endis)
783 static void gpmc_irq_disable(
struct irq_data *
p)
785 gpmc_irq_endis(p->
irq,
false);
788 static void gpmc_irq_enable(
struct irq_data *
p)
790 gpmc_irq_endis(p->
irq,
true);
795 static unsigned int gpmc_irq_noop_ret(
struct irq_data *
data) {
return 0; }
797 static int gpmc_setup_irq(
void)
805 gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
807 pr_err(
"irq_alloc_descs failed\n");
808 return gpmc_irq_start;
811 gpmc_irq_chip.name =
"gpmc";
812 gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
813 gpmc_irq_chip.irq_enable = gpmc_irq_enable;
814 gpmc_irq_chip.irq_disable = gpmc_irq_disable;
815 gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
816 gpmc_irq_chip.irq_ack = gpmc_irq_noop;
817 gpmc_irq_chip.irq_mask = gpmc_irq_noop;
818 gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
859 static void __devexit gpmc_mem_exit(
void)
864 if (!gpmc_cs_mem_enabled(cs))
866 gpmc_cs_delete_mem(cs);
874 unsigned long boot_rom_space = 0;
881 if (machine_is_omap_apollon())
890 if (!gpmc_cs_mem_enabled(cs))
892 gpmc_cs_get_memconf(cs, &base, &size);
893 rc = gpmc_cs_insert_mem(cs, base, size);
896 if (gpmc_cs_mem_enabled(cs))
897 gpmc_cs_delete_mem(cs);
915 phys_base = res->
start;
920 dev_err(&pdev->
dev,
"error: request memory / ioremap\n");
926 dev_warn(&pdev->
dev,
"Failed to get resource: irq\n");
928 gpmc_irq = res->
start;
931 if (IS_ERR(gpmc_l3_clk)) {
934 return PTR_ERR(gpmc_l3_clk);
937 clk_prepare_enable(gpmc_l3_clk);
939 gpmc_dev = &pdev->
dev;
947 rc = gpmc_mem_init();
949 clk_disable_unprepare(gpmc_l3_clk);
951 dev_err(gpmc_dev,
"failed to reserve memory\n");
956 dev_warn(gpmc_dev,
"gpmc_setup_irq failed\n");
978 static __init int gpmc_init(
void)
983 static __exit void gpmc_exit(
void)
992 static int __init omap_gpmc_init(
void)
996 char *oh_name =
"gpmc";
1000 pr_err(
"Could not look up %s\n", oh_name);
1005 WARN(IS_ERR(pdev),
"could not build omap_device for %s\n", oh_name);
1007 return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
1030 #ifdef CONFIG_ARCH_OMAP3
1045 gpmc_context.cs_context[
i].is_valid = gpmc_cs_mem_enabled(i);
1046 if (gpmc_context.cs_context[i].is_valid) {
1047 gpmc_context.cs_context[
i].config1 =
1049 gpmc_context.cs_context[
i].config2 =
1051 gpmc_context.cs_context[
i].config3 =
1053 gpmc_context.cs_context[
i].config4 =
1055 gpmc_context.cs_context[
i].config5 =
1057 gpmc_context.cs_context[
i].config6 =
1059 gpmc_context.cs_context[
i].config7 =
1077 if (gpmc_context.cs_context[i].is_valid) {
1079 gpmc_context.cs_context[i].config1);
1081 gpmc_context.cs_context[i].config2);
1083 gpmc_context.cs_context[i].config3);
1085 gpmc_context.cs_context[i].config4);
1087 gpmc_context.cs_context[i].config5);
1089 gpmc_context.cs_context[i].config6);
1091 gpmc_context.cs_context[i].config7);
1109 if (gpmc_ecc_used != -
EINVAL)
1120 val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
1141 val = (dev_width << 7) | (cs << 1) | (0x1);
1161 unsigned int val = 0x0;
1163 if (gpmc_ecc_used != cs)
1169 *ecc_code++ = val >> 16;
1171 *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
1178 #ifdef CONFIG_ARCH_OMAP3
1188 int gpmc_init_hwecc_bch(
int cs,
int nsectors,
int nerrors)
1191 if (gpmc_ecc_used != -
EINVAL)
1204 if ((nerrors == 4) &&
1206 printk(
KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
1229 int gpmc_enable_hwecc_bch(
int cs,
int mode,
int dev_width,
int nsectors,
1235 if (gpmc_ecc_used != -
EINVAL)
1253 (((nerrors == 8) ? 1 : 0) << 12) |
1256 (((nsectors-1) & 0x7) << 4) |
1275 unsigned long nsectors,
reg, val1, val2;
1277 if (gpmc_ecc_used != cs)
1282 for (i = 0; i < nsectors; i++) {
1287 val1 = gpmc_read_reg(reg + 0);
1288 val2 = gpmc_read_reg(reg + 4);
1295 *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
1296 *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
1297 *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
1298 *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
1299 *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
1300 *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
1301 *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
1315 int gpmc_calculate_ecc_bch8(
int cs,
const u_char *dat,
u_char *ecc)
1318 unsigned long nsectors,
reg, val1, val2, val3, val4;
1320 if (gpmc_ecc_used != cs)
1325 for (i = 0; i < nsectors; i++) {
1330 val1 = gpmc_read_reg(reg + 0);
1331 val2 = gpmc_read_reg(reg + 4);
1332 val3 = gpmc_read_reg(reg + 8);
1333 val4 = gpmc_read_reg(reg + 12);
1339 *ecc++ = 0xef ^ (val4 & 0xFF);
1340 *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
1341 *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
1342 *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
1343 *ecc++ = 0xed ^ (val3 & 0xFF);
1344 *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
1345 *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
1346 *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
1347 *ecc++ = 0x97 ^ (val2 & 0xFF);
1348 *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
1349 *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
1350 *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
1351 *ecc++ = 0xb5 ^ (val1 & 0xFF);