xref: /linux/drivers/mtd/maps/lantiq-flash.c (revision d642ef71)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
5  *  Copyright (C) 2010 John Crispin <john@phrozen.org>
6  */
7 
8 #include <linux/err.h>
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/io.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>
20 #include <linux/of.h>
21 
22 #include <lantiq_soc.h>
23 
24 /*
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.
32  */
33 
34 enum {
35 	LTQ_NOR_PROBING,
36 	LTQ_NOR_NORMAL
37 };
38 
39 struct ltq_mtd {
40 	struct resource *res;
41 	struct mtd_info *mtd;
42 	struct map_info *map;
43 };
44 
45 static const char ltq_map_name[] = "ltq_nor";
46 
47 static map_word
48 ltq_read16(struct map_info *map, unsigned long adr)
49 {
50 	unsigned long flags;
51 	map_word temp;
52 
53 	if (map->map_priv_1 == LTQ_NOR_PROBING)
54 		adr ^= 2;
55 	spin_lock_irqsave(&ebu_lock, flags);
56 	temp.x[0] = *(u16 *)(map->virt + adr);
57 	spin_unlock_irqrestore(&ebu_lock, flags);
58 	return temp;
59 }
60 
61 static void
62 ltq_write16(struct map_info *map, map_word d, unsigned long adr)
63 {
64 	unsigned long flags;
65 
66 	if (map->map_priv_1 == LTQ_NOR_PROBING)
67 		adr ^= 2;
68 	spin_lock_irqsave(&ebu_lock, flags);
69 	*(u16 *)(map->virt + adr) = d.x[0];
70 	spin_unlock_irqrestore(&ebu_lock, flags);
71 }
72 
73 /*
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.
79  */
80 static void
81 ltq_copy_from(struct map_info *map, void *to,
82 	unsigned long from, ssize_t len)
83 {
84 	unsigned char *f = (unsigned char *)map->virt + from;
85 	unsigned char *t = (unsigned char *)to;
86 	unsigned long flags;
87 
88 	spin_lock_irqsave(&ebu_lock, flags);
89 	while (len--)
90 		*t++ = *f++;
91 	spin_unlock_irqrestore(&ebu_lock, flags);
92 }
93 
94 static void
95 ltq_copy_to(struct map_info *map, unsigned long to,
96 	const void *from, ssize_t len)
97 {
98 	unsigned char *f = (unsigned char *)from;
99 	unsigned char *t = (unsigned char *)map->virt + to;
100 	unsigned long flags;
101 
102 	spin_lock_irqsave(&ebu_lock, flags);
103 	while (len--)
104 		*t++ = *f++;
105 	spin_unlock_irqrestore(&ebu_lock, flags);
106 }
107 
108 static int
109 ltq_mtd_probe(struct platform_device *pdev)
110 {
111 	struct ltq_mtd *ltq_mtd;
112 	struct cfi_private *cfi;
113 	int err;
114 
115 	ltq_mtd = devm_kzalloc(&pdev->dev, sizeof(struct ltq_mtd), GFP_KERNEL);
116 	if (!ltq_mtd)
117 		return -ENOMEM;
118 
119 	platform_set_drvdata(pdev, ltq_mtd);
120 
121 	ltq_mtd->map->virt = devm_platform_get_and_ioremap_resource(pdev, 0, &ltq_mtd->res);
122 	if (IS_ERR(ltq_mtd->map->virt))
123 		return PTR_ERR(ltq_mtd->map->virt);
124 
125 	ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
126 				    GFP_KERNEL);
127 	if (!ltq_mtd->map)
128 		return -ENOMEM;
129 
130 	ltq_mtd->map->phys = ltq_mtd->res->start;
131 	ltq_mtd->map->size = resource_size(ltq_mtd->res);
132 
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;
139 
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;
143 
144 	if (!ltq_mtd->mtd) {
145 		dev_err(&pdev->dev, "probing failed\n");
146 		return -ENXIO;
147 	}
148 
149 	ltq_mtd->mtd->dev.parent = &pdev->dev;
150 	mtd_set_of_node(ltq_mtd->mtd, pdev->dev.of_node);
151 
152 	cfi = ltq_mtd->map->fldrv_priv;
153 	cfi->addr_unlock1 ^= 1;
154 	cfi->addr_unlock2 ^= 1;
155 
156 	err = mtd_device_register(ltq_mtd->mtd, NULL, 0);
157 	if (err) {
158 		dev_err(&pdev->dev, "failed to add partitions\n");
159 		goto err_destroy;
160 	}
161 
162 	return 0;
163 
164 err_destroy:
165 	map_destroy(ltq_mtd->mtd);
166 	return err;
167 }
168 
169 static void ltq_mtd_remove(struct platform_device *pdev)
170 {
171 	struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
172 
173 	if (ltq_mtd && ltq_mtd->mtd) {
174 		mtd_device_unregister(ltq_mtd->mtd);
175 		map_destroy(ltq_mtd->mtd);
176 	}
177 }
178 
179 static const struct of_device_id ltq_mtd_match[] = {
180 	{ .compatible = "lantiq,nor" },
181 	{},
182 };
183 MODULE_DEVICE_TABLE(of, ltq_mtd_match);
184 
185 static struct platform_driver ltq_mtd_driver = {
186 	.probe = ltq_mtd_probe,
187 	.remove_new = ltq_mtd_remove,
188 	.driver = {
189 		.name = "ltq-nor",
190 		.of_match_table = ltq_mtd_match,
191 	},
192 };
193 
194 module_platform_driver(ltq_mtd_driver);
195 
196 MODULE_LICENSE("GPL");
197 MODULE_AUTHOR("John Crispin <john@phrozen.org>");
198 MODULE_DESCRIPTION("Lantiq SoC NOR");
199