2 * Flash memory access on SA11x0 based devices
4 * (C) 2000 Nicolas Pitre <nico@fluxnic.net>
6 #include <linux/module.h>
7 #include <linux/types.h>
8 #include <linux/ioport.h>
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/errno.h>
12 #include <linux/slab.h>
13 #include <linux/platform_device.h>
14 #include <linux/err.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/map.h>
19 #include <linux/mtd/partitions.h>
20 #include <linux/mtd/concat.h>
22 #include <mach/hardware.h>
23 #include <asm/sizes.h>
24 #include <asm/mach/flash.h>
26 struct sa_subdev_info
{
30 struct flash_platform_data
*plat
;
36 struct sa_subdev_info subdev
[0];
39 static DEFINE_SPINLOCK(sa1100_vpp_lock
);
40 static int sa1100_vpp_refcnt
;
41 static void sa1100_set_vpp(struct map_info
*map
, int on
)
43 struct sa_subdev_info
*subdev
= container_of(map
, struct sa_subdev_info
, map
);
46 spin_lock_irqsave(&sa1100_vpp_lock
, flags
);
48 if (++sa1100_vpp_refcnt
== 1) /* first nested 'on' */
49 subdev
->plat
->set_vpp(1);
51 if (--sa1100_vpp_refcnt
== 0) /* last nested 'off' */
52 subdev
->plat
->set_vpp(0);
54 spin_unlock_irqrestore(&sa1100_vpp_lock
, flags
);
57 static void sa1100_destroy_subdev(struct sa_subdev_info
*subdev
)
60 map_destroy(subdev
->mtd
);
62 iounmap(subdev
->map
.virt
);
63 release_mem_region(subdev
->map
.phys
, subdev
->map
.size
);
66 static int sa1100_probe_subdev(struct sa_subdev_info
*subdev
, struct resource
*res
)
73 size
= res
->end
- phys
+ 1;
76 * Retrieve the bankwidth from the MSC registers.
77 * We currently only implement CS0 and CS1 here.
81 printk(KERN_WARNING
"SA1100 flash: unknown base address "
82 "0x%08lx, assuming CS0\n", phys
);
85 subdev
->map
.bankwidth
= (MSC0
& MSC_RBW
) ? 2 : 4;
89 subdev
->map
.bankwidth
= ((MSC0
>> 16) & MSC_RBW
) ? 2 : 4;
93 if (!request_mem_region(phys
, size
, subdev
->name
)) {
98 if (subdev
->plat
->set_vpp
)
99 subdev
->map
.set_vpp
= sa1100_set_vpp
;
101 subdev
->map
.phys
= phys
;
102 subdev
->map
.size
= size
;
103 subdev
->map
.virt
= ioremap(phys
, size
);
104 if (!subdev
->map
.virt
) {
109 simple_map_init(&subdev
->map
);
112 * Now let's probe for the actual flash. Do it here since
113 * specific machine settings might have been set above.
115 subdev
->mtd
= do_map_probe(subdev
->plat
->map_name
, &subdev
->map
);
116 if (subdev
->mtd
== NULL
) {
121 printk(KERN_INFO
"SA1100 flash: CFI device at 0x%08lx, %uMiB, %d-bit\n",
122 phys
, (unsigned)(subdev
->mtd
->size
>> 20),
123 subdev
->map
.bankwidth
* 8);
128 sa1100_destroy_subdev(subdev
);
133 static void sa1100_destroy(struct sa_info
*info
, struct flash_platform_data
*plat
)
138 mtd_device_unregister(info
->mtd
);
139 if (info
->mtd
!= info
->subdev
[0].mtd
)
140 mtd_concat_destroy(info
->mtd
);
143 for (i
= info
->num_subdev
- 1; i
>= 0; i
--)
144 sa1100_destroy_subdev(&info
->subdev
[i
]);
151 static struct sa_info
*sa1100_setup_mtd(struct platform_device
*pdev
,
152 struct flash_platform_data
*plat
)
154 struct sa_info
*info
;
155 int nr
, size
, i
, ret
= 0;
158 * Count number of devices.
161 if (!platform_get_resource(pdev
, IORESOURCE_MEM
, nr
))
169 size
= sizeof(struct sa_info
) + sizeof(struct sa_subdev_info
) * nr
;
172 * Allocate the map_info structs in one go.
174 info
= kzalloc(size
, GFP_KERNEL
);
187 * Claim and then map the memory regions.
189 for (i
= 0; i
< nr
; i
++) {
190 struct sa_subdev_info
*subdev
= &info
->subdev
[i
];
191 struct resource
*res
;
193 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, i
);
197 subdev
->map
.name
= subdev
->name
;
198 sprintf(subdev
->name
, "%s-%d", plat
->name
, i
);
201 ret
= sa1100_probe_subdev(subdev
, res
);
206 info
->num_subdev
= i
;
209 * ENXIO is special. It means we didn't find a chip when we probed.
211 if (ret
!= 0 && !(ret
== -ENXIO
&& info
->num_subdev
> 0))
215 * If we found one device, don't bother with concat support. If
216 * we found multiple devices, use concat if we have it available,
217 * otherwise fail. Either way, it'll be called "sa1100".
219 if (info
->num_subdev
== 1) {
220 strcpy(info
->subdev
[0].name
, plat
->name
);
221 info
->mtd
= info
->subdev
[0].mtd
;
223 } else if (info
->num_subdev
> 1) {
224 struct mtd_info
*cdev
[nr
];
226 * We detected multiple devices. Concatenate them together.
228 for (i
= 0; i
< info
->num_subdev
; i
++)
229 cdev
[i
] = info
->subdev
[i
].mtd
;
231 info
->mtd
= mtd_concat_create(cdev
, info
->num_subdev
,
233 if (info
->mtd
== NULL
) {
238 info
->mtd
->dev
.parent
= &pdev
->dev
;
244 sa1100_destroy(info
, plat
);
249 static const char * const part_probes
[] = { "cmdlinepart", "RedBoot", NULL
};
251 static int sa1100_mtd_probe(struct platform_device
*pdev
)
253 struct flash_platform_data
*plat
= dev_get_platdata(&pdev
->dev
);
254 struct sa_info
*info
;
260 info
= sa1100_setup_mtd(pdev
, plat
);
267 * Partition selection stuff.
269 mtd_device_parse_register(info
->mtd
, part_probes
, NULL
, plat
->parts
,
272 platform_set_drvdata(pdev
, info
);
279 static int sa1100_mtd_remove(struct platform_device
*pdev
)
281 struct sa_info
*info
= platform_get_drvdata(pdev
);
282 struct flash_platform_data
*plat
= dev_get_platdata(&pdev
->dev
);
284 sa1100_destroy(info
, plat
);
289 static struct platform_driver sa1100_mtd_driver
= {
290 .probe
= sa1100_mtd_probe
,
291 .remove
= sa1100_mtd_remove
,
293 .name
= "sa1100-mtd",
297 module_platform_driver(sa1100_mtd_driver
);
299 MODULE_AUTHOR("Nicolas Pitre");
300 MODULE_DESCRIPTION("SA1100 CFI map driver");
301 MODULE_LICENSE("GPL");
302 MODULE_ALIAS("platform:sa1100-mtd");