RT-AC56 3.0.0.4.374.37 core
[tomato.git] / release / src-rt-6.x.4708 / linux / linux-2.6.36 / drivers / mtd / devices / ms02-nv.c
blob6a9a24a80a6d206a573fea039a8a0274b389db4d
1 /*
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.
8 */
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>
23 #include <asm/io.h>
24 #include <asm/paccess.h>
26 #include "ms02-nv.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
41 * at 0MiB, though.
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)
63 return -EINVAL;
65 memcpy(buf, mp->uaddr + from, len);
66 *retlen = len;
68 return 0;
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)
77 return -EINVAL;
79 memcpy(mp->uaddr + to, buf, len);
80 *retlen = len;
82 return 0;
86 static inline uint ms02nv_probe_one(ulong addr)
88 ms02nv_uint *ms02nv_diagp;
89 ms02nv_uint *ms02nv_magicp;
90 uint ms02nv_diag;
91 uint ms02nv_magic;
92 size_t size;
94 int err;
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);
103 if (err)
104 return 0;
105 if (ms02nv_magic != MS02NV_ID)
106 return 0;
108 ms02nv_diag = *ms02nv_diagp;
109 size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT;
110 if (size > MS02NV_CSR)
111 size = MS02NV_CSR;
113 return size;
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;
124 ulong fixaddr;
125 size_t size, fixsize;
127 static int version_printed;
129 int ret = -ENODEV;
131 /* The module decodes 8MiB of address space. */
132 mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
133 if (!mod_res)
134 return -ENOMEM;
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);
144 if (!size)
145 goto err_out_mod_res_rel;
147 if (!version_printed) {
148 printk(KERN_INFO "%s", version);
149 version_printed = 1;
152 ret = -ENOMEM;
153 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
154 if (!mtd)
155 goto err_out_mod_res_rel;
156 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
157 if (!mp)
158 goto err_out_mtd;
160 mtd->priv = mp;
161 mp->resource.module = mod_res;
163 /* Firmware's diagnostic NVRAM area. */
164 diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
165 if (!diag_res)
166 goto err_out_mp;
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);
178 if (!user_res)
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);
191 if (!csr_res)
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);
203 mp->size = size;
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);
213 mtd->type = MTD_RAM;
214 mtd->flags = MTD_CAP_RAM;
215 mtd->size = fixsize;
216 mtd->name = (char *)ms02nv_name;
217 mtd->owner = THIS_MODULE;
218 mtd->read = ms02nv_read;
219 mtd->write = ms02nv_write;
220 mtd->writesize = 1;
222 ret = -EIO;
223 if (add_mtd_device(mtd)) {
224 printk(KERN_ERR
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;
235 return 0;
238 err_out_csr_res:
239 release_resource(csr_res);
240 kfree(csr_res);
241 err_out_user_res:
242 release_resource(user_res);
243 kfree(user_res);
244 err_out_diag_res:
245 release_resource(diag_res);
246 kfree(diag_res);
247 err_out_mp:
248 kfree(mp);
249 err_out_mtd:
250 kfree(mtd);
251 err_out_mod_res_rel:
252 release_resource(mod_res);
253 err_out_mod_res:
254 kfree(mod_res);
255 return ret;
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;
265 del_mtd_device(mtd);
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);
275 kfree(mp);
276 kfree(mtd);
280 static int __init ms02nv_init(void)
282 volatile u32 *csr;
283 uint stride = 0;
284 int count = 0;
285 int i;
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)
291 stride = 2;
292 break;
293 case MACH_DS5000_2X0:
294 case MACH_DS5900:
295 csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
296 if (*csr & KN03_MCR_BNK32M)
297 stride = 2;
298 break;
299 default:
300 return -ENODEV;
301 break;
304 for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
305 if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
306 count++;
308 return (count > 0) ? 0 : -ENODEV;
311 static void __exit ms02nv_cleanup(void)
313 while (root_ms02nv_mtd)
314 ms02nv_remove_one();
318 module_init(ms02nv_init);
319 module_exit(ms02nv_cleanup);