blob: 6a9a24a80a6d206a573fea039a8a0274b389db4d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
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>
17
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>
25
26#include "ms02-nv.h"
27
28
29static char version[] __initdata =
30 "ms02-nv.c: v.1.0.0 13 Aug 2001 Maciej W. Rozycki.\n";
31
32MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
33MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver");
34MODULE_LICENSE("GPL");
35
36
37/*
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.
42 */
43static ulong ms02nv_addrs[] __initdata = {
44 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000,
45 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000,
46 0x02000000, 0x01800000, 0x01000000, 0x00800000
47};
48
49static const char ms02nv_name[] = "DEC MS02-NV NVRAM";
50static const char ms02nv_res_diag_ram[] = "Diagnostic RAM";
51static const char ms02nv_res_user_ram[] = "General-purpose RAM";
52static const char ms02nv_res_csr[] = "Control and status register";
53
54static struct mtd_info *root_ms02nv_mtd;
55
56
57static int ms02nv_read(struct mtd_info *mtd, loff_t from,
58 size_t len, size_t *retlen, u_char *buf)
59{
60 struct ms02nv_private *mp = mtd->priv;
61
62 if (from + len > mtd->size)
63 return -EINVAL;
64
65 memcpy(buf, mp->uaddr + from, len);
66 *retlen = len;
67
68 return 0;
69}
70
71static int ms02nv_write(struct mtd_info *mtd, loff_t to,
72 size_t len, size_t *retlen, const u_char *buf)
73{
74 struct ms02nv_private *mp = mtd->priv;
75
76 if (to + len > mtd->size)
77 return -EINVAL;
78
79 memcpy(mp->uaddr + to, buf, len);
80 *retlen = len;
81
82 return 0;
83}
84
85
86static inline uint ms02nv_probe_one(ulong addr)
87{
88 ms02nv_uint *ms02nv_diagp;
89 ms02nv_uint *ms02nv_magicp;
90 uint ms02nv_diag;
91 uint ms02nv_magic;
92 size_t size;
93
94 int err;
95
96 /*
97 * The firmware writes MS02NV_ID at MS02NV_MAGIC and also
98 * a diagnostic status at MS02NV_DIAG.
99 */
?af2c80e2005-06-20 13:22:55 +0100100 ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG));
101 ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 err = get_dbe(ms02nv_magic, ms02nv_magicp);
103 if (err)
104 return 0;
105 if (ms02nv_magic != MS02NV_ID)
106 return 0;
107
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;
112
113 return size;
114}
115
116static int __init ms02nv_init_one(ulong addr)
117{
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;
126
127 static int version_printed;
128
129 int ret = -ENODEV;
130
131 /* The module decodes 8MiB of address space. */
Burman Yan95b93a02006-11-15 21:10:29 +0200132 mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (!mod_res)
134 return -ENOMEM;
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 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;
142
143 size = ms02nv_probe_one(addr);
144 if (!size)
145 goto err_out_mod_res_rel;
146
147 if (!version_printed) {
148 printk(KERN_INFO "%s", version);
149 version_printed = 1;
150 }
151
152 ret = -ENOMEM;
Burman Yan95b93a02006-11-15 21:10:29 +0200153 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (!mtd)
155 goto err_out_mod_res_rel;
Burman Yan95b93a02006-11-15 21:10:29 +0200156 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (!mp)
158 goto err_out_mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160 mtd->priv = mp;
161 mp->resource.module = mod_res;
162
163 /* Firmware's diagnostic NVRAM area. */
Burman Yan95b93a02006-11-15 21:10:29 +0200164 diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (!diag_res)
166 goto err_out_mp;
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 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);
173
174 mp->resource.diag_ram = diag_res;
175
176 /* User-available general-purpose NVRAM area. */
Burman Yan95b93a02006-11-15 21:10:29 +0200177 user_res = kzalloc(sizeof(*user_res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!user_res)
179 goto err_out_diag_res;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 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);
186
187 mp->resource.user_ram = user_res;
188
189 /* Control and status register. */
Burman Yan95b93a02006-11-15 21:10:29 +0200190 csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (!csr_res)
192 goto err_out_user_res;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 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);
199
200 mp->resource.csr = csr_res;
201
202 mp->addr = phys_to_virt(addr);
203 mp->size = size;
204
205 /*
206 * Hide the firmware's diagnostic area. It may get destroyed
207 * upon a reboot. Take paging into account for mapping support.
208 */
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);
212
David Woodhouse21c8db92006-06-14 21:39:48 +0100213 mtd->type = MTD_RAM;
Joern Engele1219722006-05-30 14:25:05 +0200214 mtd->flags = MTD_CAP_RAM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 mtd->size = fixsize;
216 mtd->name = (char *)ms02nv_name;
217 mtd->owner = THIS_MODULE;
218 mtd->read = ms02nv_read;
219 mtd->write = ms02nv_write;
Artem B. Bityutskiy17ffc7b2006-06-22 18:15:48 +0400220 mtd->writesize = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
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;
227 }
228
?af2c80e2005-06-20 13:22:55 +0100229 printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 mtd->index, ms02nv_name, addr, size >> 20);
231
232 mp->next = root_ms02nv_mtd;
233 root_ms02nv_mtd = mtd;
234
235 return 0;
236
237
238err_out_csr_res:
239 release_resource(csr_res);
240 kfree(csr_res);
241err_out_user_res:
242 release_resource(user_res);
243 kfree(user_res);
244err_out_diag_res:
245 release_resource(diag_res);
246 kfree(diag_res);
247err_out_mp:
248 kfree(mp);
249err_out_mtd:
250 kfree(mtd);
251err_out_mod_res_rel:
252 release_resource(mod_res);
253err_out_mod_res:
254 kfree(mod_res);
255 return ret;
256}
257
258static void __exit ms02nv_remove_one(void)
259{
260 struct mtd_info *mtd = root_ms02nv_mtd;
261 struct ms02nv_private *mp = mtd->priv;
262
263 root_ms02nv_mtd = mp->next;
264
265 del_mtd_device(mtd);
266
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);
277}
278
279
280static int __init ms02nv_init(void)
281{
282 volatile u32 *csr;
283 uint stride = 0;
284 int count = 0;
285 int i;
286
287 switch (mips_machtype) {
288 case MACH_DS5000_200:
Maciej W. Rozycki3eb8cea2005-11-14 13:41:51 +0000289 csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (*csr & KN02_CSR_BNK32M)
291 stride = 2;
292 break;
293 case MACH_DS5000_2X0:
294 case MACH_DS5900:
Maciej W. Rozycki3eb8cea2005-11-14 13:41:51 +0000295 csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (*csr & KN03_MCR_BNK32M)
297 stride = 2;
298 break;
299 default:
300 return -ENODEV;
301 break;
302 }
303
Tobias Klauser87d10f32006-03-31 02:29:45 -0800304 for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
306 count++;
307
308 return (count > 0) ? 0 : -ENODEV;
309}
310
311static void __exit ms02nv_cleanup(void)
312{
313 while (root_ms02nv_mtd)
314 ms02nv_remove_one();
315}
316
317
318module_init(ms02nv_init);
319module_exit(ms02nv_cleanup);