1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
5 * Copyright (C) 2010 John Crispin <john@phrozen.org>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/mtd/map.h>
16 #include <linux/mtd/partitions.h>
17 #include <linux/mtd/cfi.h>
18 #include <linux/platform_device.h>
19 #include <linux/mtd/physmap.h>
22 #include <lantiq_soc.h>
25 * The NOR flash is connected to the same external bus unit (EBU) as PCI.
26 * To make PCI work we need to enable the endianness swapping for the address
27 * written to the EBU. This endianness swapping works for PCI correctly but
28 * fails for attached NOR devices. To workaround this we need to use a complex
29 * map. The workaround involves swapping all addresses whilst probing the chip.
30 * Once probing is complete we stop swapping the addresses but swizzle the
31 * unlock addresses to ensure that access to the NOR device works correctly.
45 static const char ltq_map_name
[] = "ltq_nor";
48 ltq_read16(struct map_info
*map
, unsigned long adr
)
53 if (map
->map_priv_1
== LTQ_NOR_PROBING
)
55 spin_lock_irqsave(&ebu_lock
, flags
);
56 temp
.x
[0] = *(u16
*)(map
->virt
+ adr
);
57 spin_unlock_irqrestore(&ebu_lock
, flags
);
62 ltq_write16(struct map_info
*map
, map_word d
, unsigned long adr
)
66 if (map
->map_priv_1
== LTQ_NOR_PROBING
)
68 spin_lock_irqsave(&ebu_lock
, flags
);
69 *(u16
*)(map
->virt
+ adr
) = d
.x
[0];
70 spin_unlock_irqrestore(&ebu_lock
, flags
);
74 * The following 2 functions copy data between iomem and a cached memory
75 * section. As memcpy() makes use of pre-fetching we cannot use it here.
76 * The normal alternative of using memcpy_{to,from}io also makes use of
77 * memcpy() on MIPS so it is not applicable either. We are therefore stuck
78 * with having to use our own loop.
81 ltq_copy_from(struct map_info
*map
, void *to
,
82 unsigned long from
, ssize_t len
)
84 unsigned char *f
= (unsigned char *)map
->virt
+ from
;
85 unsigned char *t
= (unsigned char *)to
;
88 spin_lock_irqsave(&ebu_lock
, flags
);
91 spin_unlock_irqrestore(&ebu_lock
, flags
);
95 ltq_copy_to(struct map_info
*map
, unsigned long to
,
96 const void *from
, ssize_t len
)
98 unsigned char *f
= (unsigned char *)from
;
99 unsigned char *t
= (unsigned char *)map
->virt
+ to
;
102 spin_lock_irqsave(&ebu_lock
, flags
);
105 spin_unlock_irqrestore(&ebu_lock
, flags
);
109 ltq_mtd_probe(struct platform_device
*pdev
)
111 struct ltq_mtd
*ltq_mtd
;
112 struct cfi_private
*cfi
;
115 ltq_mtd
= devm_kzalloc(&pdev
->dev
, sizeof(struct ltq_mtd
), GFP_KERNEL
);
119 platform_set_drvdata(pdev
, ltq_mtd
);
121 ltq_mtd
->res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
123 dev_err(&pdev
->dev
, "failed to get memory resource\n");
127 ltq_mtd
->map
= devm_kzalloc(&pdev
->dev
, sizeof(struct map_info
),
132 ltq_mtd
->map
->phys
= ltq_mtd
->res
->start
;
133 ltq_mtd
->map
->size
= resource_size(ltq_mtd
->res
);
134 ltq_mtd
->map
->virt
= devm_ioremap_resource(&pdev
->dev
, ltq_mtd
->res
);
135 if (IS_ERR(ltq_mtd
->map
->virt
))
136 return PTR_ERR(ltq_mtd
->map
->virt
);
138 ltq_mtd
->map
->name
= ltq_map_name
;
139 ltq_mtd
->map
->bankwidth
= 2;
140 ltq_mtd
->map
->read
= ltq_read16
;
141 ltq_mtd
->map
->write
= ltq_write16
;
142 ltq_mtd
->map
->copy_from
= ltq_copy_from
;
143 ltq_mtd
->map
->copy_to
= ltq_copy_to
;
145 ltq_mtd
->map
->map_priv_1
= LTQ_NOR_PROBING
;
146 ltq_mtd
->mtd
= do_map_probe("cfi_probe", ltq_mtd
->map
);
147 ltq_mtd
->map
->map_priv_1
= LTQ_NOR_NORMAL
;
150 dev_err(&pdev
->dev
, "probing failed\n");
154 ltq_mtd
->mtd
->dev
.parent
= &pdev
->dev
;
155 mtd_set_of_node(ltq_mtd
->mtd
, pdev
->dev
.of_node
);
157 cfi
= ltq_mtd
->map
->fldrv_priv
;
158 cfi
->addr_unlock1
^= 1;
159 cfi
->addr_unlock2
^= 1;
161 err
= mtd_device_register(ltq_mtd
->mtd
, NULL
, 0);
163 dev_err(&pdev
->dev
, "failed to add partitions\n");
170 map_destroy(ltq_mtd
->mtd
);
175 ltq_mtd_remove(struct platform_device
*pdev
)
177 struct ltq_mtd
*ltq_mtd
= platform_get_drvdata(pdev
);
179 if (ltq_mtd
&& ltq_mtd
->mtd
) {
180 mtd_device_unregister(ltq_mtd
->mtd
);
181 map_destroy(ltq_mtd
->mtd
);
186 static const struct of_device_id ltq_mtd_match
[] = {
187 { .compatible
= "lantiq,nor" },
190 MODULE_DEVICE_TABLE(of
, ltq_mtd_match
);
192 static struct platform_driver ltq_mtd_driver
= {
193 .probe
= ltq_mtd_probe
,
194 .remove
= ltq_mtd_remove
,
197 .of_match_table
= ltq_mtd_match
,
201 module_platform_driver(ltq_mtd_driver
);
203 MODULE_LICENSE("GPL");
204 MODULE_AUTHOR("John Crispin <john@phrozen.org>");
205 MODULE_DESCRIPTION("Lantiq SoC NOR");