1 // SPDX-License-Identifier: GPL-2.0-only
3 * Flash memory access on SA11x0 based devices
5 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
7 #include <linux/module.h>
8 #include <linux/types.h>
9 #include <linux/ioport.h>
10 #include <linux/kernel.h>
11 #include <linux/init.h>
12 #include <linux/errno.h>
13 #include <linux/slab.h>
14 #include <linux/platform_device.h>
15 #include <linux/err.h>
18 #include <linux/mtd/mtd.h>
19 #include <linux/mtd/map.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/mtd/concat.h>
23 #include <mach/hardware.h>
24 #include <linux/sizes.h>
25 #include <asm/mach/flash.h>
27 struct sa_subdev_info
{
31 struct flash_platform_data
*plat
;
37 struct sa_subdev_info subdev
[];
40 static DEFINE_SPINLOCK(sa1100_vpp_lock
);
41 static int sa1100_vpp_refcnt
;
42 static void sa1100_set_vpp(struct map_info
*map
, int on
)
44 struct sa_subdev_info
*subdev
= container_of(map
, struct sa_subdev_info
, map
);
47 spin_lock_irqsave(&sa1100_vpp_lock
, flags
);
49 if (++sa1100_vpp_refcnt
== 1) /* first nested 'on' */
50 subdev
->plat
->set_vpp(1);
52 if (--sa1100_vpp_refcnt
== 0) /* last nested 'off' */
53 subdev
->plat
->set_vpp(0);
55 spin_unlock_irqrestore(&sa1100_vpp_lock
, flags
);
58 static void sa1100_destroy_subdev(struct sa_subdev_info
*subdev
)
61 map_destroy(subdev
->mtd
);
63 iounmap(subdev
->map
.virt
);
64 release_mem_region(subdev
->map
.phys
, subdev
->map
.size
);
67 static int sa1100_probe_subdev(struct sa_subdev_info
*subdev
, struct resource
*res
)
74 size
= res
->end
- phys
+ 1;
77 * Retrieve the bankwidth from the MSC registers.
78 * We currently only implement CS0 and CS1 here.
82 printk(KERN_WARNING
"SA1100 flash: unknown base address "
83 "0x%08lx, assuming CS0\n", phys
);
86 subdev
->map
.bankwidth
= (MSC0
& MSC_RBW
) ? 2 : 4;
90 subdev
->map
.bankwidth
= ((MSC0
>> 16) & MSC_RBW
) ? 2 : 4;
94 if (!request_mem_region(phys
, size
, subdev
->name
)) {
99 if (subdev
->plat
->set_vpp
)
100 subdev
->map
.set_vpp
= sa1100_set_vpp
;
102 subdev
->map
.phys
= phys
;
103 subdev
->map
.size
= size
;
104 subdev
->map
.virt
= ioremap(phys
, size
);
105 if (!subdev
->map
.virt
) {
110 simple_map_init(&subdev
->map
);
113 * Now let's probe for the actual flash. Do it here since
114 * specific machine settings might have been set above.
116 subdev
->mtd
= do_map_probe(subdev
->plat
->map_name
, &subdev
->map
);
117 if (subdev
->mtd
== NULL
) {
122 printk(KERN_INFO
"SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
123 phys
, (unsigned)(subdev
->mtd
->size
>> 20),
124 subdev
->map
.bankwidth
* 8);
129 sa1100_destroy_subdev(subdev
);
134 static void sa1100_destroy(struct sa_info
*info
, struct flash_platform_data
*plat
)
139 mtd_device_unregister(info
->mtd
);
140 if (info
->mtd
!= info
->subdev
[0].mtd
)
141 mtd_concat_destroy(info
->mtd
);
144 for (i
= info
->num_subdev
- 1; i
>= 0; i
--)
145 sa1100_destroy_subdev(&info
->subdev
[i
]);
152 static struct sa_info
*sa1100_setup_mtd(struct platform_device
*pdev
,
153 struct flash_platform_data
*plat
)
155 struct sa_info
*info
;
156 int nr
, size
, i
, ret
= 0;
159 * Count number of devices.
162 if (!platform_get_resource(pdev
, IORESOURCE_MEM
, nr
))
170 size
= sizeof(struct sa_info
) + sizeof(struct sa_subdev_info
) * nr
;
173 * Allocate the map_info structs in one go.
175 info
= kzalloc(size
, GFP_KERNEL
);
188 * Claim and then map the memory regions.
190 for (i
= 0; i
< nr
; i
++) {
191 struct sa_subdev_info
*subdev
= &info
->subdev
[i
];
192 struct resource
*res
;
194 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
198 subdev
->map
.name
= subdev
->name
;
199 sprintf(subdev
->name
, "%s-%d", plat
->name
, i
);
202 ret
= sa1100_probe_subdev(subdev
, res
);
207 info
->num_subdev
= i
;
210 * ENXIO is special. It means we didn't find a chip when we probed.
212 if (ret
!= 0 && !(ret
== -ENXIO
&& info
->num_subdev
> 0))
216 * If we found one device, don't bother with concat support. If
217 * we found multiple devices, use concat if we have it available,
218 * otherwise fail. Either way, it'll be called "sa1100".
220 if (info
->num_subdev
== 1) {
221 strcpy(info
->subdev
[0].name
, plat
->name
);
222 info
->mtd
= info
->subdev
[0].mtd
;
224 } else if (info
->num_subdev
> 1) {
225 struct mtd_info
**cdev
;
227 cdev
= kmalloc_array(nr
, sizeof(*cdev
), GFP_KERNEL
);
234 * We detected multiple devices. Concatenate them together.
236 for (i
= 0; i
< info
->num_subdev
; i
++)
237 cdev
[i
] = info
->subdev
[i
].mtd
;
239 info
->mtd
= mtd_concat_create(cdev
, info
->num_subdev
,
242 if (info
->mtd
== NULL
) {
247 info
->mtd
->dev
.parent
= &pdev
->dev
;
253 sa1100_destroy(info
, plat
);
258 static const char * const part_probes
[] = { "cmdlinepart", "RedBoot", NULL
};
260 static int sa1100_mtd_probe(struct platform_device
*pdev
)
262 struct flash_platform_data
*plat
= dev_get_platdata(&pdev
->dev
);
263 struct sa_info
*info
;
269 info
= sa1100_setup_mtd(pdev
, plat
);
276 * Partition selection stuff.
278 mtd_device_parse_register(info
->mtd
, part_probes
, NULL
, plat
->parts
,
281 platform_set_drvdata(pdev
, info
);
288 static int sa1100_mtd_remove(struct platform_device
*pdev
)
290 struct sa_info
*info
= platform_get_drvdata(pdev
);
291 struct flash_platform_data
*plat
= dev_get_platdata(&pdev
->dev
);
293 sa1100_destroy(info
, plat
);
298 static struct platform_driver sa1100_mtd_driver
= {
299 .probe
= sa1100_mtd_probe
,
300 .remove
= sa1100_mtd_remove
,
302 .name
= "sa1100-mtd",
306 module_platform_driver(sa1100_mtd_driver
);
308 MODULE_AUTHOR("Nicolas Pitre");
309 MODULE_DESCRIPTION("SA1100 CFI map driver");
310 MODULE_LICENSE("GPL");
311 MODULE_ALIAS("platform:sa1100-mtd");