| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* linux/drivers/mtd/maps/bast_flash.c | 
|  | 2 | * | 
| Ben Dooks | 6fc93d8 | 2005-01-18 11:13:50 +0000 | [diff] [blame] | 3 | * Copyright (c) 2004-2005 Simtec Electronics | 
|  | 4 | *	Ben Dooks <ben@simtec.co.uk> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * | 
|  | 6 | * Simtec Bast (EB2410ITX) NOR MTD Mapping driver | 
|  | 7 | * | 
|  | 8 | * Changelog: | 
|  | 9 | *	20-Sep-2004  BJD  Initial version | 
| Ben Dooks | 6fc93d8 | 2005-01-18 11:13:50 +0000 | [diff] [blame] | 10 | *	17-Jan-2005  BJD  Add whole device if no partitions found | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 12 | * $Id: bast-flash.c,v 1.5 2005/11/07 11:14:26 gleixner Exp $ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * | 
|  | 14 | * This program is free software; you can redistribute it and/or modify | 
|  | 15 | * it under the terms of the GNU General Public License as published by | 
|  | 16 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 17 | * (at your option) any later version. | 
|  | 18 | * | 
|  | 19 | * This program is distributed in the hope that it will be useful, | 
|  | 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 22 | * GNU General Public License for more details. | 
|  | 23 | * | 
|  | 24 | * You should have received a copy of the GNU General Public License | 
|  | 25 | * along with this program; if not, write to the Free Software | 
|  | 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|  | 27 | */ | 
|  | 28 |  | 
|  | 29 | #include <linux/module.h> | 
|  | 30 | #include <linux/types.h> | 
|  | 31 | #include <linux/init.h> | 
|  | 32 | #include <linux/kernel.h> | 
|  | 33 | #include <linux/string.h> | 
|  | 34 | #include <linux/ioport.h> | 
|  | 35 | #include <linux/device.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 36 | #include <linux/slab.h> | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 37 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/mtd/mtd.h> | 
|  | 39 | #include <linux/mtd/map.h> | 
|  | 40 | #include <linux/mtd/partitions.h> | 
|  | 41 |  | 
|  | 42 | #include <asm/io.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <asm/mach/flash.h> | 
|  | 44 |  | 
|  | 45 | #include <asm/arch/map.h> | 
|  | 46 | #include <asm/arch/bast-map.h> | 
|  | 47 | #include <asm/arch/bast-cpld.h> | 
|  | 48 |  | 
|  | 49 | #ifdef CONFIG_MTD_BAST_MAXSIZE | 
| Ben Dooks | 6fc93d8 | 2005-01-18 11:13:50 +0000 | [diff] [blame] | 50 | #define AREA_MAXSIZE (CONFIG_MTD_BAST_MAXSIZE * SZ_1M) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #else | 
| Ben Dooks | 6fc93d8 | 2005-01-18 11:13:50 +0000 | [diff] [blame] | 52 | #define AREA_MAXSIZE (32 * SZ_1M) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #endif | 
|  | 54 |  | 
|  | 55 | #define PFX "bast-flash: " | 
|  | 56 |  | 
|  | 57 | struct bast_flash_info { | 
|  | 58 | struct mtd_info		*mtd; | 
|  | 59 | struct map_info		 map; | 
|  | 60 | struct mtd_partition	*partitions; | 
|  | 61 | struct resource		*area; | 
|  | 62 | }; | 
|  | 63 |  | 
|  | 64 | static const char *probes[] = { "RedBoot", "cmdlinepart", NULL }; | 
|  | 65 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | static void bast_flash_setrw(int to) | 
|  | 67 | { | 
|  | 68 | unsigned int val; | 
|  | 69 | unsigned long flags; | 
|  | 70 |  | 
|  | 71 | local_irq_save(flags); | 
|  | 72 | val = __raw_readb(BAST_VA_CTRL3); | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 73 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | if (to) | 
|  | 75 | val |= BAST_CPLD_CTRL3_ROMWEN; | 
|  | 76 | else | 
|  | 77 | val &= ~BAST_CPLD_CTRL3_ROMWEN; | 
|  | 78 |  | 
|  | 79 | pr_debug("new cpld ctrl3=%02x\n", val); | 
|  | 80 |  | 
|  | 81 | __raw_writeb(val, BAST_VA_CTRL3); | 
|  | 82 | local_irq_restore(flags); | 
|  | 83 | } | 
|  | 84 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 85 | static int bast_flash_remove(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 87 | struct bast_flash_info *info = platform_get_drvdata(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 89 | platform_set_drvdata(pdev, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 |  | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 91 | if (info == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return 0; | 
|  | 93 |  | 
|  | 94 | if (info->map.virt != NULL) | 
|  | 95 | iounmap(info->map.virt); | 
|  | 96 |  | 
|  | 97 | if (info->mtd) { | 
|  | 98 | del_mtd_partitions(info->mtd); | 
|  | 99 | map_destroy(info->mtd); | 
|  | 100 | } | 
|  | 101 |  | 
| Jesper Juhl | fa67164 | 2005-11-07 01:01:27 -0800 | [diff] [blame] | 102 | kfree(info->partitions); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 |  | 
|  | 104 | if (info->area) { | 
|  | 105 | release_resource(info->area); | 
|  | 106 | kfree(info->area); | 
|  | 107 | } | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 108 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | kfree(info); | 
|  | 110 |  | 
|  | 111 | return 0; | 
|  | 112 | } | 
|  | 113 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 114 | static int bast_flash_probe(struct platform_device *pdev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | struct bast_flash_info *info; | 
|  | 117 | struct resource *res; | 
|  | 118 | int err = 0; | 
|  | 119 |  | 
|  | 120 | info = kmalloc(sizeof(*info), GFP_KERNEL); | 
|  | 121 | if (info == NULL) { | 
|  | 122 | printk(KERN_ERR PFX "no memory for flash info\n"); | 
|  | 123 | err = -ENOMEM; | 
|  | 124 | goto exit_error; | 
|  | 125 | } | 
|  | 126 |  | 
|  | 127 | memzero(info, sizeof(*info)); | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 128 | platform_set_drvdata(pdev, info); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 |  | 
|  | 130 | res = pdev->resource;  /* assume that the flash has one resource */ | 
|  | 131 |  | 
|  | 132 | info->map.phys = res->start; | 
|  | 133 | info->map.size = res->end - res->start + 1; | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 134 | info->map.name = pdev->dev.bus_id; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | info->map.bankwidth = 2; | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 136 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | if (info->map.size > AREA_MAXSIZE) | 
|  | 138 | info->map.size = AREA_MAXSIZE; | 
|  | 139 |  | 
|  | 140 | pr_debug("%s: area %08lx, size %ld\n", __FUNCTION__, | 
|  | 141 | info->map.phys, info->map.size); | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 142 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | info->area = request_mem_region(res->start, info->map.size, | 
|  | 144 | pdev->name); | 
|  | 145 | if (info->area == NULL) { | 
|  | 146 | printk(KERN_ERR PFX "cannot reserve flash memory region\n"); | 
|  | 147 | err = -ENOENT; | 
|  | 148 | goto exit_error; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | info->map.virt = ioremap(res->start, info->map.size); | 
|  | 152 | pr_debug("%s: virt at %08x\n", __FUNCTION__, (int)info->map.virt); | 
|  | 153 |  | 
|  | 154 | if (info->map.virt == 0) { | 
|  | 155 | printk(KERN_ERR PFX "failed to ioremap() region\n"); | 
|  | 156 | err = -EIO; | 
|  | 157 | goto exit_error; | 
|  | 158 | } | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 159 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | simple_map_init(&info->map); | 
|  | 161 |  | 
|  | 162 | /* enable the write to the flash area */ | 
|  | 163 |  | 
|  | 164 | bast_flash_setrw(1); | 
|  | 165 |  | 
|  | 166 | /* probe for the device(s) */ | 
|  | 167 |  | 
|  | 168 | info->mtd = do_map_probe("jedec_probe", &info->map); | 
|  | 169 | if (info->mtd == NULL) | 
|  | 170 | info->mtd = do_map_probe("cfi_probe", &info->map); | 
|  | 171 |  | 
|  | 172 | if (info->mtd == NULL) { | 
|  | 173 | printk(KERN_ERR PFX "map_probe() failed\n"); | 
|  | 174 | err = -ENXIO; | 
|  | 175 | goto exit_error; | 
|  | 176 | } | 
|  | 177 |  | 
|  | 178 | /* mark ourselves as the owner */ | 
|  | 179 | info->mtd->owner = THIS_MODULE; | 
|  | 180 |  | 
|  | 181 | err = parse_mtd_partitions(info->mtd, probes, &info->partitions, 0); | 
|  | 182 | if (err > 0) { | 
|  | 183 | err = add_mtd_partitions(info->mtd, info->partitions, err); | 
| Thomas Gleixner | 69f34c9 | 2005-11-07 11:15:40 +0000 | [diff] [blame] | 184 | if (err) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | printk(KERN_ERR PFX "cannot add/parse partitions\n"); | 
| Ben Dooks | 6fc93d8 | 2005-01-18 11:13:50 +0000 | [diff] [blame] | 186 | } else { | 
|  | 187 | err = add_mtd_device(info->mtd); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
|  | 190 | if (err == 0) | 
|  | 191 | return 0; | 
|  | 192 |  | 
|  | 193 | /* fall through to exit error */ | 
|  | 194 |  | 
|  | 195 | exit_error: | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 196 | bast_flash_remove(pdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return err; | 
|  | 198 | } | 
|  | 199 |  | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 200 | static struct platform_driver bast_flash_driver = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | .probe		= bast_flash_probe, | 
|  | 202 | .remove		= bast_flash_remove, | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 203 | .driver		= { | 
|  | 204 | .name	= "bast-nor", | 
|  | 205 | .owner	= THIS_MODULE, | 
|  | 206 | }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | }; | 
|  | 208 |  | 
|  | 209 | static int __init bast_flash_init(void) | 
|  | 210 | { | 
|  | 211 | printk("BAST NOR-Flash Driver, (c) 2004 Simtec Electronics\n"); | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 212 | return platform_driver_register(&bast_flash_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } | 
|  | 214 |  | 
|  | 215 | static void __exit bast_flash_exit(void) | 
|  | 216 | { | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 217 | platform_driver_unregister(&bast_flash_driver); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
|  | 220 | module_init(bast_flash_init); | 
|  | 221 | module_exit(bast_flash_exit); | 
|  | 222 |  | 
|  | 223 | MODULE_LICENSE("GPL"); | 
|  | 224 | MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>"); | 
|  | 225 | MODULE_DESCRIPTION("BAST MTD Map driver"); |