blob: f56d0aa4404b05171fff005f2ad810df5d8b6aa7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/****************************************************************************/
2
3/*
4 * uclinux.c -- generic memory mapped MTD driver for uclinux
5 *
6 * (C) Copyright 2002, Greg Ungerer (gerg@snapgear.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 */
8
9/****************************************************************************/
10
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/types.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/fs.h>
Andrea Righi27ac7922008-07-23 21:28:13 -070016#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/mtd/mtd.h>
19#include <linux/mtd/map.h>
20#include <linux/mtd/partitions.h>
21#include <asm/io.h>
Geert Uytterhoeven363737d2012-05-31 22:39:21 +020022#include <asm/sections.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24/****************************************************************************/
25
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +010026#ifdef CONFIG_MTD_ROM
27#define MAP_NAME "rom"
28#else
29#define MAP_NAME "ram"
30#endif
31
Linus Torvalds1da177e2005-04-16 15:20:36 -070032struct map_info uclinux_ram_map = {
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +010033 .name = MAP_NAME,
Mike Frysingerfa254ec2009-05-26 19:33:16 -040034 .size = 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -070035};
36
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +010037static unsigned long physaddr = -1;
38module_param(physaddr, ulong, S_IRUGO);
39
Mike Frysinger9f31f4b2009-05-26 19:33:17 -040040static struct mtd_info *uclinux_ram_mtdinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42/****************************************************************************/
43
Mike Frysinger9f31f4b2009-05-26 19:33:17 -040044static struct mtd_partition uclinux_romfs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 { .name = "ROMfs" }
46};
47
Tobias Klauser87d10f32006-03-31 02:29:45 -080048#define NUM_PARTITIONS ARRAY_SIZE(uclinux_romfs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/****************************************************************************/
51
Mike Frysinger9f31f4b2009-05-26 19:33:17 -040052static int uclinux_point(struct mtd_info *mtd, loff_t from, size_t len,
Jared Hulberta98889f2008-04-29 23:26:49 -070053 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct map_info *map = mtd->priv;
Jared Hulberta98889f2008-04-29 23:26:49 -070056 *virt = map->virt + from;
57 if (phys)
58 *phys = map->phys + from;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *retlen = len;
60 return(0);
61}
62
63/****************************************************************************/
64
Dmitri Vorobiev769455e2008-11-25 02:55:09 +020065static int __init uclinux_mtd_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
67 struct mtd_info *mtd;
68 struct map_info *mapp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70 mapp = &uclinux_ram_map;
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +010071
72 if (physaddr == -1)
73 mapp->phys = (resource_size_t)__bss_stop;
74 else
75 mapp->phys = physaddr;
76
Mike Frysingerfa254ec2009-05-26 19:33:16 -040077 if (!mapp->size)
78 mapp->size = PAGE_ALIGN(ntohl(*((unsigned long *)(mapp->phys + 8))));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 mapp->bankwidth = 4;
80
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +010081 printk("uclinux[mtd]: probe address=0x%x size=0x%x\n",
Greg Ungererf6515db2005-09-12 11:18:10 +100082 (int) mapp->phys, (int) mapp->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Greg Ungerer08a3c4b2012-07-19 15:42:45 +100084 /*
85 * The filesystem is guaranteed to be in direct mapped memory. It is
86 * directly following the kernels own bss region. Following the same
87 * mechanism used by architectures setting up traditional initrds we
88 * use phys_to_virt to get the virtual address of its start.
89 */
90 mapp->virt = phys_to_virt(mapp->phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (mapp->virt == 0) {
Greg Ungerer08a3c4b2012-07-19 15:42:45 +100093 printk("uclinux[mtd]: no virtual mapping?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 return(-EIO);
95 }
96
97 simple_map_init(mapp);
98
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +010099 mtd = do_map_probe("map_" MAP_NAME, mapp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 if (!mtd) {
101 printk("uclinux[mtd]: failed to find a mapping?\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 return(-ENXIO);
103 }
Thomas Gleixner69f34c92005-11-07 11:15:40 +0000104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 mtd->owner = THIS_MODULE;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200106 mtd->_point = uclinux_point;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 mtd->priv = mapp;
108
109 uclinux_ram_mtdinfo = mtd;
Jamie Iles5e7e9682011-05-23 10:23:12 +0100110 mtd_device_register(mtd, uclinux_romfs, NUM_PARTITIONS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return(0);
113}
114
115/****************************************************************************/
116
Dmitri Vorobiev769455e2008-11-25 02:55:09 +0200117static void __exit uclinux_mtd_cleanup(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
119 if (uclinux_ram_mtdinfo) {
Jamie Iles5e7e9682011-05-23 10:23:12 +0100120 mtd_device_unregister(uclinux_ram_mtdinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 map_destroy(uclinux_ram_mtdinfo);
122 uclinux_ram_mtdinfo = NULL;
123 }
Greg Ungerer08a3c4b2012-07-19 15:42:45 +1000124 if (uclinux_ram_map.virt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 uclinux_ram_map.virt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128/****************************************************************************/
129
130module_init(uclinux_mtd_init);
131module_exit(uclinux_mtd_cleanup);
132
133MODULE_LICENSE("GPL");
134MODULE_AUTHOR("Greg Ungerer <gerg@snapgear.com>");
Uwe Kleine-König81f53ff2013-01-16 15:36:55 +0100135MODULE_DESCRIPTION("Generic MTD for uClinux");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137/****************************************************************************/