yuezonghe | 824eb0c | 2024-06-27 02:32:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NV-RAM memory access on autcpu12 |
| 3 | * (C) 2002 Thomas Gleixner (gleixner@autronix.de) |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/types.h> |
| 23 | #include <linux/kernel.h> |
| 24 | #include <linux/ioport.h> |
| 25 | #include <linux/init.h> |
| 26 | #include <asm/io.h> |
| 27 | #include <asm/sizes.h> |
| 28 | #include <mach/hardware.h> |
| 29 | #include <mach/autcpu12.h> |
| 30 | #include <linux/mtd/mtd.h> |
| 31 | #include <linux/mtd/map.h> |
| 32 | #include <linux/mtd/partitions.h> |
| 33 | |
| 34 | |
| 35 | static struct mtd_info *sram_mtd; |
| 36 | |
| 37 | struct map_info autcpu12_sram_map = { |
| 38 | .name = "SRAM", |
| 39 | .size = 32768, |
| 40 | .bankwidth = 4, |
| 41 | .phys = 0x12000000, |
| 42 | }; |
| 43 | |
| 44 | static int __init init_autcpu12_sram (void) |
| 45 | { |
| 46 | map_word tmp, save0, save1; |
| 47 | int err; |
| 48 | |
| 49 | autcpu12_sram_map.virt = ioremap(0x12000000, SZ_128K); |
| 50 | if (!autcpu12_sram_map.virt) { |
| 51 | printk("Failed to ioremap autcpu12 NV-RAM space\n"); |
| 52 | err = -EIO; |
| 53 | goto out; |
| 54 | } |
| 55 | simple_map_init(&autcpu12_sram_map); |
| 56 | |
| 57 | /* |
| 58 | * Check for 32K/128K |
| 59 | * read ofs 0 |
| 60 | * read ofs 0x10000 |
| 61 | * Write complement to ofs 0x100000 |
| 62 | * Read and check result on ofs 0x0 |
| 63 | * Restore contents |
| 64 | */ |
| 65 | save0 = map_read(&autcpu12_sram_map, 0); |
| 66 | save1 = map_read(&autcpu12_sram_map, 0x10000); |
| 67 | tmp.x[0] = ~save0.x[0]; |
| 68 | map_write(&autcpu12_sram_map, tmp, 0x10000); |
| 69 | /* if we find this pattern on 0x0, we have 32K size |
| 70 | * restore contents and exit |
| 71 | */ |
| 72 | tmp = map_read(&autcpu12_sram_map, 0); |
| 73 | if (!map_word_equal(&autcpu12_sram_map, tmp, save0)) { |
| 74 | map_write(&autcpu12_sram_map, save0, 0x0); |
| 75 | goto map; |
| 76 | } |
| 77 | /* We have a 128K found, restore 0x10000 and set size |
| 78 | * to 128K |
| 79 | */ |
| 80 | map_write(&autcpu12_sram_map, save1, 0x10000); |
| 81 | autcpu12_sram_map.size = SZ_128K; |
| 82 | |
| 83 | map: |
| 84 | sram_mtd = do_map_probe("map_ram", &autcpu12_sram_map); |
| 85 | if (!sram_mtd) { |
| 86 | printk("NV-RAM probe failed\n"); |
| 87 | err = -ENXIO; |
| 88 | goto out_ioremap; |
| 89 | } |
| 90 | |
| 91 | sram_mtd->owner = THIS_MODULE; |
| 92 | sram_mtd->erasesize = 16; |
| 93 | |
| 94 | if (mtd_device_register(sram_mtd, NULL, 0)) { |
| 95 | printk("NV-RAM device addition failed\n"); |
| 96 | err = -ENOMEM; |
| 97 | goto out_probe; |
| 98 | } |
| 99 | |
| 100 | printk("NV-RAM device size %ldKiB registered on AUTCPU12\n",autcpu12_sram_map.size/SZ_1K); |
| 101 | |
| 102 | return 0; |
| 103 | |
| 104 | out_probe: |
| 105 | map_destroy(sram_mtd); |
| 106 | sram_mtd = 0; |
| 107 | |
| 108 | out_ioremap: |
| 109 | iounmap((void *)autcpu12_sram_map.virt); |
| 110 | out: |
| 111 | return err; |
| 112 | } |
| 113 | |
| 114 | static void __exit cleanup_autcpu12_maps(void) |
| 115 | { |
| 116 | if (sram_mtd) { |
| 117 | mtd_device_unregister(sram_mtd); |
| 118 | map_destroy(sram_mtd); |
| 119 | iounmap((void *)autcpu12_sram_map.virt); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | module_init(init_autcpu12_sram); |
| 124 | module_exit(cleanup_autcpu12_maps); |
| 125 | |
| 126 | MODULE_AUTHOR("Thomas Gleixner"); |
| 127 | MODULE_DESCRIPTION("autcpu12 NV-RAM map driver"); |
| 128 | MODULE_LICENSE("GPL"); |