2 * Copyright (c) 2001 Maciej W. Rozycki
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.
10 #include <linux/init.h>
11 #include <linux/ioport.h>
12 #include <linux/kernel.h>
13 #include <linux/module.h>
14 #include <linux/mtd/mtd.h>
15 #include <linux/slab.h>
16 #include <linux/types.h>
18 #include <asm/addrspace.h>
19 #include <asm/bootinfo.h>
20 #include <asm/dec/ioasic_addrs.h>
21 #include <asm/dec/kn02.h>
22 #include <asm/dec/kn03.h>
24 #include <asm/paccess.h>
29 static char version[] __initdata =
30 "ms02-nv.c: v.1.0.0 13 Aug 2001 Maciej W. Rozycki.\n";
32 MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
33 MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver");
34 MODULE_LICENSE("GPL");
38 * Addresses we probe for an MS02-NV at. Modules may be located
39 * at any 8MiB boundary within a 0MiB up to 112MiB range or at any 32MiB
40 * boundary within a 0MiB up to 448MiB range. We don't support a module
43 static ulong ms02nv_addrs[] __initdata = {
44 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000,
45 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000,
46 0x02000000, 0x01800000, 0x01000000, 0x00800000
49 static const char ms02nv_name[] = "DEC MS02-NV NVRAM";
50 static const char ms02nv_res_diag_ram[] = "Diagnostic RAM";
51 static const char ms02nv_res_user_ram[] = "General-purpose RAM";
52 static const char ms02nv_res_csr[] = "Control and status register";
54 static struct mtd_info *root_ms02nv_mtd;
57 static int ms02nv_read(struct mtd_info *mtd, loff_t from,
58 size_t len, size_t *retlen, u_char *buf)
60 struct ms02nv_private *mp = mtd->priv;
62 if (from + len > mtd->size)
65 memcpy(buf, mp->uaddr + from, len);
71 static int ms02nv_write(struct mtd_info *mtd, loff_t to,
72 size_t len, size_t *retlen, const u_char *buf)
74 struct ms02nv_private *mp = mtd->priv;
76 if (to + len > mtd->size)
79 memcpy(mp->uaddr + to, buf, len);
86 static inline uint ms02nv_probe_one(ulong addr)
88 ms02nv_uint *ms02nv_diagp;
89 ms02nv_uint *ms02nv_magicp;
97 * The firmware writes MS02NV_ID at MS02NV_MAGIC and also
98 * a diagnostic status at MS02NV_DIAG.
100 ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG));
101 ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC));
102 err = get_dbe(ms02nv_magic, ms02nv_magicp);
105 if (ms02nv_magic != MS02NV_ID)
108 ms02nv_diag = *ms02nv_diagp;
109 size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT;
110 if (size > MS02NV_CSR)
116 static int __init ms02nv_init_one(ulong addr)
118 struct mtd_info *mtd;
119 struct ms02nv_private *mp;
120 struct resource *mod_res;
121 struct resource *diag_res;
122 struct resource *user_res;
123 struct resource *csr_res;
125 size_t size, fixsize;
127 static int version_printed;
131 /* The module decodes 8MiB of address space. */
132 mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
136 mod_res->name = ms02nv_name;
137 mod_res->start = addr;
138 mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
139 mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
140 if (request_resource(&iomem_resource, mod_res) < 0)
141 goto err_out_mod_res;
143 size = ms02nv_probe_one(addr);
145 goto err_out_mod_res_rel;
147 if (!version_printed) {
148 printk(KERN_INFO "%s", version);
153 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
155 goto err_out_mod_res_rel;
156 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
161 mp->resource.module = mod_res;
163 /* Firmware's diagnostic NVRAM area. */
164 diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
168 diag_res->name = ms02nv_res_diag_ram;
169 diag_res->start = addr;
170 diag_res->end = addr + MS02NV_RAM - 1;
171 diag_res->flags = IORESOURCE_BUSY;
172 request_resource(mod_res, diag_res);
174 mp->resource.diag_ram = diag_res;
176 /* User-available general-purpose NVRAM area. */
177 user_res = kzalloc(sizeof(*user_res), GFP_KERNEL);
179 goto err_out_diag_res;
181 user_res->name = ms02nv_res_user_ram;
182 user_res->start = addr + MS02NV_RAM;
183 user_res->end = addr + size - 1;
184 user_res->flags = IORESOURCE_BUSY;
185 request_resource(mod_res, user_res);
187 mp->resource.user_ram = user_res;
189 /* Control and status register. */
190 csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL);
192 goto err_out_user_res;
194 csr_res->name = ms02nv_res_csr;
195 csr_res->start = addr + MS02NV_CSR;
196 csr_res->end = addr + MS02NV_CSR + 3;
197 csr_res->flags = IORESOURCE_BUSY;
198 request_resource(mod_res, csr_res);
200 mp->resource.csr = csr_res;
202 mp->addr = phys_to_virt(addr);
206 * Hide the firmware's diagnostic area. It may get destroyed
207 * upon a reboot. Take paging into account for mapping support.
209 fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
210 fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
211 mp->uaddr = phys_to_virt(fixaddr);
214 mtd->flags = MTD_CAP_RAM;
216 mtd->name = (char *)ms02nv_name;
217 mtd->owner = THIS_MODULE;
218 mtd->read = ms02nv_read;
219 mtd->write = ms02nv_write;
223 if (add_mtd_device(mtd)) {
225 "ms02-nv: Unable to register MTD device, aborting!\n");
226 goto err_out_csr_res;
229 printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n",
230 mtd->index, ms02nv_name, addr, size >> 20);
232 mp->next = root_ms02nv_mtd;
233 root_ms02nv_mtd = mtd;
239 release_resource(csr_res);
242 release_resource(user_res);
245 release_resource(diag_res);
252 release_resource(mod_res);
258 static void __exit ms02nv_remove_one(void)
260 struct mtd_info *mtd = root_ms02nv_mtd;
261 struct ms02nv_private *mp = mtd->priv;
263 root_ms02nv_mtd = mp->next;
267 release_resource(mp->resource.csr);
268 kfree(mp->resource.csr);
269 release_resource(mp->resource.user_ram);
270 kfree(mp->resource.user_ram);
271 release_resource(mp->resource.diag_ram);
272 kfree(mp->resource.diag_ram);
273 release_resource(mp->resource.module);
274 kfree(mp->resource.module);
280 static int __init ms02nv_init(void)
287 switch (mips_machtype) {
288 case MACH_DS5000_200:
289 csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
290 if (*csr & KN02_CSR_BNK32M)
293 case MACH_DS5000_2X0:
295 csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
296 if (*csr & KN03_MCR_BNK32M)
304 for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
305 if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
308 return (count > 0) ? 0 : -ENODEV;
311 static void __exit ms02nv_cleanup(void)
313 while (root_ms02nv_mtd)
318 module_init(ms02nv_init);
319 module_exit(ms02nv_cleanup);