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
->map
->virt
= devm_platform_get_and_ioremap_resource(pdev
, 0, <q_mtd
->res
);
122 if (IS_ERR(ltq_mtd
->map
->virt
))
123 return PTR_ERR(ltq_mtd
->map
->virt
);
125 ltq_mtd
->map
= devm_kzalloc(&pdev
->dev
, sizeof(struct map_info
),
130 ltq_mtd
->map
->phys
= ltq_mtd
->res
->start
;
131 ltq_mtd
->map
->size
= resource_size(ltq_mtd
->res
);
133 ltq_mtd
->map
->name
= ltq_map_name
;
134 ltq_mtd
->map
->bankwidth
= 2;
135 ltq_mtd
->map
->read
= ltq_read16
;
136 ltq_mtd
->map
->write
= ltq_write16
;
137 ltq_mtd
->map
->copy_from
= ltq_copy_from
;
138 ltq_mtd
->map
->copy_to
= ltq_copy_to
;
140 ltq_mtd
->map
->map_priv_1
= LTQ_NOR_PROBING
;
141 ltq_mtd
->mtd
= do_map_probe("cfi_probe", ltq_mtd
->map
);
142 ltq_mtd
->map
->map_priv_1
= LTQ_NOR_NORMAL
;
145 dev_err(&pdev
->dev
, "probing failed\n");
149 ltq_mtd
->mtd
->dev
.parent
= &pdev
->dev
;
150 mtd_set_of_node(ltq_mtd
->mtd
, pdev
->dev
.of_node
);
152 cfi
= ltq_mtd
->map
->fldrv_priv
;
153 cfi
->addr_unlock1
^= 1;
154 cfi
->addr_unlock2
^= 1;
156 err
= mtd_device_register(ltq_mtd
->mtd
, NULL
, 0);
158 dev_err(&pdev
->dev
, "failed to add partitions\n");
165 map_destroy(ltq_mtd
->mtd
);
169 static void ltq_mtd_remove(struct platform_device
*pdev
)
171 struct ltq_mtd
*ltq_mtd
= platform_get_drvdata(pdev
);
173 if (ltq_mtd
&& ltq_mtd
->mtd
) {
174 mtd_device_unregister(ltq_mtd
->mtd
);
175 map_destroy(ltq_mtd
->mtd
);
179 static const struct of_device_id ltq_mtd_match
[] = {
180 { .compatible
= "lantiq,nor" },
183 MODULE_DEVICE_TABLE(of
, ltq_mtd_match
);
185 static struct platform_driver ltq_mtd_driver
= {
186 .probe
= ltq_mtd_probe
,
187 .remove
= ltq_mtd_remove
,
190 .of_match_table
= ltq_mtd_match
,
194 module_platform_driver(ltq_mtd_driver
);
196 MODULE_LICENSE("GPL");
197 MODULE_AUTHOR("John Crispin <john@phrozen.org>");
198 MODULE_DESCRIPTION("Lantiq SoC NOR");