2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License version 2 as published
4 * by the Free Software Foundation.
6 * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
7 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
10 #include <linux/err.h>
11 #include <linux/module.h>
12 #include <linux/types.h>
13 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/map.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/mtd/cfi.h>
20 #include <linux/platform_device.h>
21 #include <linux/mtd/physmap.h>
24 #include <lantiq_soc.h>
27 * The NOR flash is connected to the same external bus unit (EBU) as PCI.
28 * To make PCI work we need to enable the endianness swapping for the address
29 * written to the EBU. This endianness swapping works for PCI correctly but
30 * fails for attached NOR devices. To workaround this we need to use a complex
31 * map. The workaround involves swapping all addresses whilst probing the chip.
32 * Once probing is complete we stop swapping the addresses but swizzle the
33 * unlock addresses to ensure that access to the NOR device works correctly.
47 static const char ltq_map_name
[] = "ltq_nor";
48 static const char * const ltq_probe_types
[] = { "cmdlinepart", "ofpart", NULL
};
51 ltq_read16(struct map_info
*map
, unsigned long adr
)
56 if (map
->map_priv_1
== LTQ_NOR_PROBING
)
58 spin_lock_irqsave(&ebu_lock
, flags
);
59 temp
.x
[0] = *(u16
*)(map
->virt
+ adr
);
60 spin_unlock_irqrestore(&ebu_lock
, flags
);
65 ltq_write16(struct map_info
*map
, map_word d
, unsigned long adr
)
69 if (map
->map_priv_1
== LTQ_NOR_PROBING
)
71 spin_lock_irqsave(&ebu_lock
, flags
);
72 *(u16
*)(map
->virt
+ adr
) = d
.x
[0];
73 spin_unlock_irqrestore(&ebu_lock
, flags
);
77 * The following 2 functions copy data between iomem and a cached memory
78 * section. As memcpy() makes use of pre-fetching we cannot use it here.
79 * The normal alternative of using memcpy_{to,from}io also makes use of
80 * memcpy() on MIPS so it is not applicable either. We are therefore stuck
81 * with having to use our own loop.
84 ltq_copy_from(struct map_info
*map
, void *to
,
85 unsigned long from
, ssize_t len
)
87 unsigned char *f
= (unsigned char *)map
->virt
+ from
;
88 unsigned char *t
= (unsigned char *)to
;
91 spin_lock_irqsave(&ebu_lock
, flags
);
94 spin_unlock_irqrestore(&ebu_lock
, flags
);
98 ltq_copy_to(struct map_info
*map
, unsigned long to
,
99 const void *from
, ssize_t len
)
101 unsigned char *f
= (unsigned char *)from
;
102 unsigned char *t
= (unsigned char *)map
->virt
+ to
;
105 spin_lock_irqsave(&ebu_lock
, flags
);
108 spin_unlock_irqrestore(&ebu_lock
, flags
);
112 ltq_mtd_probe(struct platform_device
*pdev
)
114 struct mtd_part_parser_data ppdata
;
115 struct ltq_mtd
*ltq_mtd
;
116 struct cfi_private
*cfi
;
119 if (of_machine_is_compatible("lantiq,falcon") &&
120 (ltq_boot_select() != BS_FLASH
)) {
121 dev_err(&pdev
->dev
, "invalid bootstrap options\n");
125 ltq_mtd
= devm_kzalloc(&pdev
->dev
, sizeof(struct ltq_mtd
), GFP_KERNEL
);
129 platform_set_drvdata(pdev
, ltq_mtd
);
131 ltq_mtd
->res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
133 dev_err(&pdev
->dev
, "failed to get memory resource\n");
137 ltq_mtd
->map
= devm_kzalloc(&pdev
->dev
, sizeof(struct map_info
),
142 ltq_mtd
->map
->phys
= ltq_mtd
->res
->start
;
143 ltq_mtd
->map
->size
= resource_size(ltq_mtd
->res
);
144 ltq_mtd
->map
->virt
= devm_ioremap_resource(&pdev
->dev
, ltq_mtd
->res
);
145 if (IS_ERR(ltq_mtd
->map
->virt
))
146 return PTR_ERR(ltq_mtd
->map
->virt
);
148 ltq_mtd
->map
->name
= ltq_map_name
;
149 ltq_mtd
->map
->bankwidth
= 2;
150 ltq_mtd
->map
->read
= ltq_read16
;
151 ltq_mtd
->map
->write
= ltq_write16
;
152 ltq_mtd
->map
->copy_from
= ltq_copy_from
;
153 ltq_mtd
->map
->copy_to
= ltq_copy_to
;
155 ltq_mtd
->map
->map_priv_1
= LTQ_NOR_PROBING
;
156 ltq_mtd
->mtd
= do_map_probe("cfi_probe", ltq_mtd
->map
);
157 ltq_mtd
->map
->map_priv_1
= LTQ_NOR_NORMAL
;
160 dev_err(&pdev
->dev
, "probing failed\n");
164 ltq_mtd
->mtd
->owner
= THIS_MODULE
;
166 cfi
= ltq_mtd
->map
->fldrv_priv
;
167 cfi
->addr_unlock1
^= 1;
168 cfi
->addr_unlock2
^= 1;
170 ppdata
.of_node
= pdev
->dev
.of_node
;
171 err
= mtd_device_parse_register(ltq_mtd
->mtd
, ltq_probe_types
,
174 dev_err(&pdev
->dev
, "failed to add partitions\n");
181 map_destroy(ltq_mtd
->mtd
);
186 ltq_mtd_remove(struct platform_device
*pdev
)
188 struct ltq_mtd
*ltq_mtd
= platform_get_drvdata(pdev
);
190 if (ltq_mtd
&& ltq_mtd
->mtd
) {
191 mtd_device_unregister(ltq_mtd
->mtd
);
192 map_destroy(ltq_mtd
->mtd
);
197 static const struct of_device_id ltq_mtd_match
[] = {
198 { .compatible
= "lantiq,nor" },
201 MODULE_DEVICE_TABLE(of
, ltq_mtd_match
);
203 static struct platform_driver ltq_mtd_driver
= {
204 .probe
= ltq_mtd_probe
,
205 .remove
= ltq_mtd_remove
,
208 .of_match_table
= ltq_mtd_match
,
212 module_platform_driver(ltq_mtd_driver
);
214 MODULE_LICENSE("GPL");
215 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
216 MODULE_DESCRIPTION("Lantiq SoC NOR");