blob: 380ff08d29e4a668e9a3f087586172259372b5da [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.
8 *
9 * $Id: ms02-nv.c,v 1.8 2005/01/05 18:05:12 dwmw2 Exp $
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
31static char version[] __initdata =
32 "ms02-nv.c: v.1.0.0 13 Aug 2001 Maciej W. Rozycki.\n";
33
34MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
35MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver");
36MODULE_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 */
45static ulong ms02nv_addrs[] __initdata = {
46 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000,
47 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000,
48 0x02000000, 0x01800000, 0x01000000, 0x00800000
49};
50
51static const char ms02nv_name[] = "DEC MS02-NV NVRAM";
52static const char ms02nv_res_diag_ram[] = "Diagnostic RAM";
53static const char ms02nv_res_user_ram[] = "General-purpose RAM";
54static const char ms02nv_res_csr[] = "Control and status register";
55
56static struct mtd_info *root_ms02nv_mtd;
57
58
59static 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
73static 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
88static 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 */
102 ms02nv_diagp = (ms02nv_uint *)(KSEG1ADDR(addr + MS02NV_DIAG));
103 ms02nv_magicp = (ms02nv_uint *)(KSEG1ADDR(addr + MS02NV_MAGIC));
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
118static 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. */
134 mod_res = kmalloc(sizeof(*mod_res), GFP_KERNEL);
135 if (!mod_res)
136 return -ENOMEM;
137
138 memset(mod_res, 0, sizeof(*mod_res));
139 mod_res->name = ms02nv_name;
140 mod_res->start = addr;
141 mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
142 mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
143 if (request_resource(&iomem_resource, mod_res) < 0)
144 goto err_out_mod_res;
145
146 size = ms02nv_probe_one(addr);
147 if (!size)
148 goto err_out_mod_res_rel;
149
150 if (!version_printed) {
151 printk(KERN_INFO "%s", version);
152 version_printed = 1;
153 }
154
155 ret = -ENOMEM;
156 mtd = kmalloc(sizeof(*mtd), GFP_KERNEL);
157 if (!mtd)
158 goto err_out_mod_res_rel;
159 memset(mtd, 0, sizeof(*mtd));
160 mp = kmalloc(sizeof(*mp), GFP_KERNEL);
161 if (!mp)
162 goto err_out_mtd;
163 memset(mp, 0, sizeof(*mp));
164
165 mtd->priv = mp;
166 mp->resource.module = mod_res;
167
168 /* Firmware's diagnostic NVRAM area. */
169 diag_res = kmalloc(sizeof(*diag_res), GFP_KERNEL);
170 if (!diag_res)
171 goto err_out_mp;
172
173 memset(diag_res, 0, sizeof(*diag_res));
174 diag_res->name = ms02nv_res_diag_ram;
175 diag_res->start = addr;
176 diag_res->end = addr + MS02NV_RAM - 1;
177 diag_res->flags = IORESOURCE_BUSY;
178 request_resource(mod_res, diag_res);
179
180 mp->resource.diag_ram = diag_res;
181
182 /* User-available general-purpose NVRAM area. */
183 user_res = kmalloc(sizeof(*user_res), GFP_KERNEL);
184 if (!user_res)
185 goto err_out_diag_res;
186
187 memset(user_res, 0, sizeof(*user_res));
188 user_res->name = ms02nv_res_user_ram;
189 user_res->start = addr + MS02NV_RAM;
190 user_res->end = addr + size - 1;
191 user_res->flags = IORESOURCE_BUSY;
192 request_resource(mod_res, user_res);
193
194 mp->resource.user_ram = user_res;
195
196 /* Control and status register. */
197 csr_res = kmalloc(sizeof(*csr_res), GFP_KERNEL);
198 if (!csr_res)
199 goto err_out_user_res;
200
201 memset(csr_res, 0, sizeof(*csr_res));
202 csr_res->name = ms02nv_res_csr;
203 csr_res->start = addr + MS02NV_CSR;
204 csr_res->end = addr + MS02NV_CSR + 3;
205 csr_res->flags = IORESOURCE_BUSY;
206 request_resource(mod_res, csr_res);
207
208 mp->resource.csr = csr_res;
209
210 mp->addr = phys_to_virt(addr);
211 mp->size = size;
212
213 /*
214 * Hide the firmware's diagnostic area. It may get destroyed
215 * upon a reboot. Take paging into account for mapping support.
216 */
217 fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
218 fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
219 mp->uaddr = phys_to_virt(fixaddr);
220
221 mtd->type = MTD_RAM;
222 mtd->flags = MTD_CAP_RAM | MTD_XIP;
223 mtd->size = fixsize;
224 mtd->name = (char *)ms02nv_name;
225 mtd->owner = THIS_MODULE;
226 mtd->read = ms02nv_read;
227 mtd->write = ms02nv_write;
228
229 ret = -EIO;
230 if (add_mtd_device(mtd)) {
231 printk(KERN_ERR
232 "ms02-nv: Unable to register MTD device, aborting!\n");
233 goto err_out_csr_res;
234 }
235
236 printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %uMiB.\n",
237 mtd->index, ms02nv_name, addr, size >> 20);
238
239 mp->next = root_ms02nv_mtd;
240 root_ms02nv_mtd = mtd;
241
242 return 0;
243
244
245err_out_csr_res:
246 release_resource(csr_res);
247 kfree(csr_res);
248err_out_user_res:
249 release_resource(user_res);
250 kfree(user_res);
251err_out_diag_res:
252 release_resource(diag_res);
253 kfree(diag_res);
254err_out_mp:
255 kfree(mp);
256err_out_mtd:
257 kfree(mtd);
258err_out_mod_res_rel:
259 release_resource(mod_res);
260err_out_mod_res:
261 kfree(mod_res);
262 return ret;
263}
264
265static void __exit ms02nv_remove_one(void)
266{
267 struct mtd_info *mtd = root_ms02nv_mtd;
268 struct ms02nv_private *mp = mtd->priv;
269
270 root_ms02nv_mtd = mp->next;
271
272 del_mtd_device(mtd);
273
274 release_resource(mp->resource.csr);
275 kfree(mp->resource.csr);
276 release_resource(mp->resource.user_ram);
277 kfree(mp->resource.user_ram);
278 release_resource(mp->resource.diag_ram);
279 kfree(mp->resource.diag_ram);
280 release_resource(mp->resource.module);
281 kfree(mp->resource.module);
282 kfree(mp);
283 kfree(mtd);
284}
285
286
287static int __init ms02nv_init(void)
288{
289 volatile u32 *csr;
290 uint stride = 0;
291 int count = 0;
292 int i;
293
294 switch (mips_machtype) {
295 case MACH_DS5000_200:
296 csr = (volatile u32 *)KN02_CSR_BASE;
297 if (*csr & KN02_CSR_BNK32M)
298 stride = 2;
299 break;
300 case MACH_DS5000_2X0:
301 case MACH_DS5900:
302 csr = (volatile u32 *)KN03_MCR_BASE;
303 if (*csr & KN03_MCR_BNK32M)
304 stride = 2;
305 break;
306 default:
307 return -ENODEV;
308 break;
309 }
310
311 for (i = 0; i < (sizeof(ms02nv_addrs) / sizeof(*ms02nv_addrs)); i++)
312 if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
313 count++;
314
315 return (count > 0) ? 0 : -ENODEV;
316}
317
318static void __exit ms02nv_cleanup(void)
319{
320 while (root_ms02nv_mtd)
321 ms02nv_remove_one();
322}
323
324
325module_init(ms02nv_init);
326module_exit(ms02nv_cleanup);