| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | *	Copyright (c) 2001 Maciej W. Rozycki | 
|  | 3 | * | 
|  | 4 | *	This program is free software; you can redistribute it and/or | 
|  | 5 | *	modify it under the terms of the GNU General Public License | 
|  | 6 | *	as published by the Free Software Foundation; either version | 
|  | 7 | *	2 of the License, or (at your option) any later version. | 
|  | 8 | * | 
| Maciej W. Rozycki | 3eb8cea | 2005-11-14 13:41:51 +0000 | [diff] [blame] | 9 | *	$Id: ms02-nv.c,v 1.11 2005/11/14 13:41:47 macro Exp $ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ | 
|  | 11 |  | 
|  | 12 | #include <linux/init.h> | 
|  | 13 | #include <linux/ioport.h> | 
|  | 14 | #include <linux/kernel.h> | 
|  | 15 | #include <linux/module.h> | 
|  | 16 | #include <linux/mtd/mtd.h> | 
|  | 17 | #include <linux/slab.h> | 
|  | 18 | #include <linux/types.h> | 
|  | 19 |  | 
|  | 20 | #include <asm/addrspace.h> | 
|  | 21 | #include <asm/bootinfo.h> | 
|  | 22 | #include <asm/dec/ioasic_addrs.h> | 
|  | 23 | #include <asm/dec/kn02.h> | 
|  | 24 | #include <asm/dec/kn03.h> | 
|  | 25 | #include <asm/io.h> | 
|  | 26 | #include <asm/paccess.h> | 
|  | 27 |  | 
|  | 28 | #include "ms02-nv.h" | 
|  | 29 |  | 
|  | 30 |  | 
|  | 31 | static char version[] __initdata = | 
|  | 32 | "ms02-nv.c: v.1.0.0  13 Aug 2001  Maciej W. Rozycki.\n"; | 
|  | 33 |  | 
|  | 34 | MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>"); | 
|  | 35 | MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver"); | 
|  | 36 | MODULE_LICENSE("GPL"); | 
|  | 37 |  | 
|  | 38 |  | 
|  | 39 | /* | 
|  | 40 | * Addresses we probe for an MS02-NV at.  Modules may be located | 
|  | 41 | * at any 8MiB boundary within a 0MiB up to 112MiB range or at any 32MiB | 
|  | 42 | * boundary within a 0MiB up to 448MiB range.  We don't support a module | 
|  | 43 | * at 0MiB, though. | 
|  | 44 | */ | 
|  | 45 | static ulong ms02nv_addrs[] __initdata = { | 
|  | 46 | 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000, | 
|  | 47 | 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000, | 
|  | 48 | 0x02000000, 0x01800000, 0x01000000, 0x00800000 | 
|  | 49 | }; | 
|  | 50 |  | 
|  | 51 | static const char ms02nv_name[] = "DEC MS02-NV NVRAM"; | 
|  | 52 | static const char ms02nv_res_diag_ram[] = "Diagnostic RAM"; | 
|  | 53 | static const char ms02nv_res_user_ram[] = "General-purpose RAM"; | 
|  | 54 | static const char ms02nv_res_csr[] = "Control and status register"; | 
|  | 55 |  | 
|  | 56 | static struct mtd_info *root_ms02nv_mtd; | 
|  | 57 |  | 
|  | 58 |  | 
|  | 59 | static int ms02nv_read(struct mtd_info *mtd, loff_t from, | 
|  | 60 | size_t len, size_t *retlen, u_char *buf) | 
|  | 61 | { | 
|  | 62 | struct ms02nv_private *mp = mtd->priv; | 
|  | 63 |  | 
|  | 64 | if (from + len > mtd->size) | 
|  | 65 | return -EINVAL; | 
|  | 66 |  | 
|  | 67 | memcpy(buf, mp->uaddr + from, len); | 
|  | 68 | *retlen = len; | 
|  | 69 |  | 
|  | 70 | return 0; | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | static int ms02nv_write(struct mtd_info *mtd, loff_t to, | 
|  | 74 | size_t len, size_t *retlen, const u_char *buf) | 
|  | 75 | { | 
|  | 76 | struct ms02nv_private *mp = mtd->priv; | 
|  | 77 |  | 
|  | 78 | if (to + len > mtd->size) | 
|  | 79 | return -EINVAL; | 
|  | 80 |  | 
|  | 81 | memcpy(mp->uaddr + to, buf, len); | 
|  | 82 | *retlen = len; | 
|  | 83 |  | 
|  | 84 | return 0; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 |  | 
|  | 88 | static inline uint ms02nv_probe_one(ulong addr) | 
|  | 89 | { | 
|  | 90 | ms02nv_uint *ms02nv_diagp; | 
|  | 91 | ms02nv_uint *ms02nv_magicp; | 
|  | 92 | uint ms02nv_diag; | 
|  | 93 | uint ms02nv_magic; | 
|  | 94 | size_t size; | 
|  | 95 |  | 
|  | 96 | int err; | 
|  | 97 |  | 
|  | 98 | /* | 
|  | 99 | * The firmware writes MS02NV_ID at MS02NV_MAGIC and also | 
|  | 100 | * a diagnostic status at MS02NV_DIAG. | 
|  | 101 | */ | 
| ? | af2c80e | 2005-06-20 13:22:55 +0100 | [diff] [blame] | 102 | ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG)); | 
|  | 103 | ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC)); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | err = get_dbe(ms02nv_magic, ms02nv_magicp); | 
|  | 105 | if (err) | 
|  | 106 | return 0; | 
|  | 107 | if (ms02nv_magic != MS02NV_ID) | 
|  | 108 | return 0; | 
|  | 109 |  | 
|  | 110 | ms02nv_diag = *ms02nv_diagp; | 
|  | 111 | size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT; | 
|  | 112 | if (size > MS02NV_CSR) | 
|  | 113 | size = MS02NV_CSR; | 
|  | 114 |  | 
|  | 115 | return size; | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | static int __init ms02nv_init_one(ulong addr) | 
|  | 119 | { | 
|  | 120 | struct mtd_info *mtd; | 
|  | 121 | struct ms02nv_private *mp; | 
|  | 122 | struct resource *mod_res; | 
|  | 123 | struct resource *diag_res; | 
|  | 124 | struct resource *user_res; | 
|  | 125 | struct resource *csr_res; | 
|  | 126 | ulong fixaddr; | 
|  | 127 | size_t size, fixsize; | 
|  | 128 |  | 
|  | 129 | static int version_printed; | 
|  | 130 |  | 
|  | 131 | int ret = -ENODEV; | 
|  | 132 |  | 
|  | 133 | /* The module decodes 8MiB of address space. */ | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 134 | mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if (!mod_res) | 
|  | 136 | return -ENOMEM; | 
|  | 137 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | mod_res->name = ms02nv_name; | 
|  | 139 | mod_res->start = addr; | 
|  | 140 | mod_res->end = addr + MS02NV_SLOT_SIZE - 1; | 
|  | 141 | mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY; | 
|  | 142 | if (request_resource(&iomem_resource, mod_res) < 0) | 
|  | 143 | goto err_out_mod_res; | 
|  | 144 |  | 
|  | 145 | size = ms02nv_probe_one(addr); | 
|  | 146 | if (!size) | 
|  | 147 | goto err_out_mod_res_rel; | 
|  | 148 |  | 
|  | 149 | if (!version_printed) { | 
|  | 150 | printk(KERN_INFO "%s", version); | 
|  | 151 | version_printed = 1; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | ret = -ENOMEM; | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 155 | mtd = kzalloc(sizeof(*mtd), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | if (!mtd) | 
|  | 157 | goto err_out_mod_res_rel; | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 158 | mp = kzalloc(sizeof(*mp), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | if (!mp) | 
|  | 160 | goto err_out_mtd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 |  | 
|  | 162 | mtd->priv = mp; | 
|  | 163 | mp->resource.module = mod_res; | 
|  | 164 |  | 
|  | 165 | /* Firmware's diagnostic NVRAM area. */ | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 166 | diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | if (!diag_res) | 
|  | 168 | goto err_out_mp; | 
|  | 169 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | diag_res->name = ms02nv_res_diag_ram; | 
|  | 171 | diag_res->start = addr; | 
|  | 172 | diag_res->end = addr + MS02NV_RAM - 1; | 
|  | 173 | diag_res->flags = IORESOURCE_BUSY; | 
|  | 174 | request_resource(mod_res, diag_res); | 
|  | 175 |  | 
|  | 176 | mp->resource.diag_ram = diag_res; | 
|  | 177 |  | 
|  | 178 | /* User-available general-purpose NVRAM area. */ | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 179 | user_res = kzalloc(sizeof(*user_res), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | if (!user_res) | 
|  | 181 | goto err_out_diag_res; | 
|  | 182 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | user_res->name = ms02nv_res_user_ram; | 
|  | 184 | user_res->start = addr + MS02NV_RAM; | 
|  | 185 | user_res->end = addr + size - 1; | 
|  | 186 | user_res->flags = IORESOURCE_BUSY; | 
|  | 187 | request_resource(mod_res, user_res); | 
|  | 188 |  | 
|  | 189 | mp->resource.user_ram = user_res; | 
|  | 190 |  | 
|  | 191 | /* Control and status register. */ | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 192 | csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | if (!csr_res) | 
|  | 194 | goto err_out_user_res; | 
|  | 195 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | csr_res->name = ms02nv_res_csr; | 
|  | 197 | csr_res->start = addr + MS02NV_CSR; | 
|  | 198 | csr_res->end = addr + MS02NV_CSR + 3; | 
|  | 199 | csr_res->flags = IORESOURCE_BUSY; | 
|  | 200 | request_resource(mod_res, csr_res); | 
|  | 201 |  | 
|  | 202 | mp->resource.csr = csr_res; | 
|  | 203 |  | 
|  | 204 | mp->addr = phys_to_virt(addr); | 
|  | 205 | mp->size = size; | 
|  | 206 |  | 
|  | 207 | /* | 
|  | 208 | * Hide the firmware's diagnostic area.  It may get destroyed | 
|  | 209 | * upon a reboot.  Take paging into account for mapping support. | 
|  | 210 | */ | 
|  | 211 | fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1); | 
|  | 212 | fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1); | 
|  | 213 | mp->uaddr = phys_to_virt(fixaddr); | 
|  | 214 |  | 
| David Woodhouse | 21c8db9 | 2006-06-14 21:39:48 +0100 | [diff] [blame] | 215 | mtd->type = MTD_RAM; | 
| Joern Engel | e121972 | 2006-05-30 14:25:05 +0200 | [diff] [blame] | 216 | mtd->flags = MTD_CAP_RAM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | mtd->size = fixsize; | 
|  | 218 | mtd->name = (char *)ms02nv_name; | 
|  | 219 | mtd->owner = THIS_MODULE; | 
|  | 220 | mtd->read = ms02nv_read; | 
|  | 221 | mtd->write = ms02nv_write; | 
| Artem B. Bityutskiy | 17ffc7b | 2006-06-22 18:15:48 +0400 | [diff] [blame] | 222 | mtd->writesize = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 |  | 
|  | 224 | ret = -EIO; | 
|  | 225 | if (add_mtd_device(mtd)) { | 
|  | 226 | printk(KERN_ERR | 
|  | 227 | "ms02-nv: Unable to register MTD device, aborting!\n"); | 
|  | 228 | goto err_out_csr_res; | 
|  | 229 | } | 
|  | 230 |  | 
| ? | af2c80e | 2005-06-20 13:22:55 +0100 | [diff] [blame] | 231 | printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | mtd->index, ms02nv_name, addr, size >> 20); | 
|  | 233 |  | 
|  | 234 | mp->next = root_ms02nv_mtd; | 
|  | 235 | root_ms02nv_mtd = mtd; | 
|  | 236 |  | 
|  | 237 | return 0; | 
|  | 238 |  | 
|  | 239 |  | 
|  | 240 | err_out_csr_res: | 
|  | 241 | release_resource(csr_res); | 
|  | 242 | kfree(csr_res); | 
|  | 243 | err_out_user_res: | 
|  | 244 | release_resource(user_res); | 
|  | 245 | kfree(user_res); | 
|  | 246 | err_out_diag_res: | 
|  | 247 | release_resource(diag_res); | 
|  | 248 | kfree(diag_res); | 
|  | 249 | err_out_mp: | 
|  | 250 | kfree(mp); | 
|  | 251 | err_out_mtd: | 
|  | 252 | kfree(mtd); | 
|  | 253 | err_out_mod_res_rel: | 
|  | 254 | release_resource(mod_res); | 
|  | 255 | err_out_mod_res: | 
|  | 256 | kfree(mod_res); | 
|  | 257 | return ret; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 | static void __exit ms02nv_remove_one(void) | 
|  | 261 | { | 
|  | 262 | struct mtd_info *mtd = root_ms02nv_mtd; | 
|  | 263 | struct ms02nv_private *mp = mtd->priv; | 
|  | 264 |  | 
|  | 265 | root_ms02nv_mtd = mp->next; | 
|  | 266 |  | 
|  | 267 | del_mtd_device(mtd); | 
|  | 268 |  | 
|  | 269 | release_resource(mp->resource.csr); | 
|  | 270 | kfree(mp->resource.csr); | 
|  | 271 | release_resource(mp->resource.user_ram); | 
|  | 272 | kfree(mp->resource.user_ram); | 
|  | 273 | release_resource(mp->resource.diag_ram); | 
|  | 274 | kfree(mp->resource.diag_ram); | 
|  | 275 | release_resource(mp->resource.module); | 
|  | 276 | kfree(mp->resource.module); | 
|  | 277 | kfree(mp); | 
|  | 278 | kfree(mtd); | 
|  | 279 | } | 
|  | 280 |  | 
|  | 281 |  | 
|  | 282 | static int __init ms02nv_init(void) | 
|  | 283 | { | 
|  | 284 | volatile u32 *csr; | 
|  | 285 | uint stride = 0; | 
|  | 286 | int count = 0; | 
|  | 287 | int i; | 
|  | 288 |  | 
|  | 289 | switch (mips_machtype) { | 
|  | 290 | case MACH_DS5000_200: | 
| Maciej W. Rozycki | 3eb8cea | 2005-11-14 13:41:51 +0000 | [diff] [blame] | 291 | csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | if (*csr & KN02_CSR_BNK32M) | 
|  | 293 | stride = 2; | 
|  | 294 | break; | 
|  | 295 | case MACH_DS5000_2X0: | 
|  | 296 | case MACH_DS5900: | 
| Maciej W. Rozycki | 3eb8cea | 2005-11-14 13:41:51 +0000 | [diff] [blame] | 297 | csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | if (*csr & KN03_MCR_BNK32M) | 
|  | 299 | stride = 2; | 
|  | 300 | break; | 
|  | 301 | default: | 
|  | 302 | return -ENODEV; | 
|  | 303 | break; | 
|  | 304 | } | 
|  | 305 |  | 
| Tobias Klauser | 87d10f3 | 2006-03-31 02:29:45 -0800 | [diff] [blame] | 306 | for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | if (!ms02nv_init_one(ms02nv_addrs[i] << stride)) | 
|  | 308 | count++; | 
|  | 309 |  | 
|  | 310 | return (count > 0) ? 0 : -ENODEV; | 
|  | 311 | } | 
|  | 312 |  | 
|  | 313 | static void __exit ms02nv_cleanup(void) | 
|  | 314 | { | 
|  | 315 | while (root_ms02nv_mtd) | 
|  | 316 | ms02nv_remove_one(); | 
|  | 317 | } | 
|  | 318 |  | 
|  | 319 |  | 
|  | 320 | module_init(ms02nv_init); | 
|  | 321 | module_exit(ms02nv_cleanup); |