Commit | Line | Data |
---|---|---|
1da177e4 LT |
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. | |
1da177e4 LT |
8 | */ |
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 | ||
29 | static char version[] __initdata = | |
30 | "ms02-nv.c: v.1.0.0 13 Aug 2001 Maciej W. Rozycki.\n"; | |
31 | ||
32 | MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>"); | |
33 | MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver"); | |
34 | MODULE_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 | */ | |
43 | static ulong ms02nv_addrs[] __initdata = { | |
44 | 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000, | |
45 | 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000, | |
46 | 0x02000000, 0x01800000, 0x01000000, 0x00800000 | |
47 | }; | |
48 | ||
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"; | |
53 | ||
54 | static struct mtd_info *root_ms02nv_mtd; | |
55 | ||
56 | ||
57 | static 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 | ||
71 | static 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 | ||
86 | static 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 | */ | |
af2c80e9 | 100 | ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG)); |
101 | ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC)); | |
1da177e4 LT |
102 | 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 | ||
116 | static 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. */ | |
95b93a0c | 132 | mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL); |
1da177e4 LT |
133 | if (!mod_res) |
134 | return -ENOMEM; | |
135 | ||
1da177e4 LT |
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; | |
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; | |
95b93a0c | 153 | mtd = kzalloc(sizeof(*mtd), GFP_KERNEL); |
1da177e4 LT |
154 | if (!mtd) |
155 | goto err_out_mod_res_rel; | |
95b93a0c | 156 | mp = kzalloc(sizeof(*mp), GFP_KERNEL); |
1da177e4 LT |
157 | if (!mp) |
158 | goto err_out_mtd; | |
1da177e4 LT |
159 | |
160 | mtd->priv = mp; | |
161 | mp->resource.module = mod_res; | |
162 | ||
163 | /* Firmware's diagnostic NVRAM area. */ | |
95b93a0c | 164 | diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL); |
1da177e4 LT |
165 | if (!diag_res) |
166 | goto err_out_mp; | |
167 | ||
1da177e4 LT |
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); | |
173 | ||
174 | mp->resource.diag_ram = diag_res; | |
175 | ||
176 | /* User-available general-purpose NVRAM area. */ | |
95b93a0c | 177 | user_res = kzalloc(sizeof(*user_res), GFP_KERNEL); |
1da177e4 LT |
178 | if (!user_res) |
179 | goto err_out_diag_res; | |
180 | ||
1da177e4 LT |
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); | |
186 | ||
187 | mp->resource.user_ram = user_res; | |
188 | ||
189 | /* Control and status register. */ | |
95b93a0c | 190 | csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL); |
1da177e4 LT |
191 | if (!csr_res) |
192 | goto err_out_user_res; | |
193 | ||
1da177e4 LT |
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); | |
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 | ||
21c8db9e | 213 | mtd->type = MTD_RAM; |
e1219724 | 214 | mtd->flags = MTD_CAP_RAM; |
1da177e4 LT |
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; | |
17ffc7ba | 220 | mtd->writesize = 1; |
1da177e4 LT |
221 | |
222 | ret = -EIO; | |
ee0e87b1 | 223 | if (mtd_device_register(mtd, NULL, 0)) { |
1da177e4 LT |
224 | printk(KERN_ERR |
225 | "ms02-nv: Unable to register MTD device, aborting!\n"); | |
226 | goto err_out_csr_res; | |
227 | } | |
228 | ||
af2c80e9 | 229 | printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n", |
1da177e4 LT |
230 | 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 | ||
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; | |
256 | } | |
257 | ||
258 | static 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 | ||
ee0e87b1 | 265 | mtd_device_unregister(mtd); |
1da177e4 LT |
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 | ||
280 | static 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: | |
3eb8ceac | 289 | csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR); |
1da177e4 LT |
290 | if (*csr & KN02_CSR_BNK32M) |
291 | stride = 2; | |
292 | break; | |
293 | case MACH_DS5000_2X0: | |
294 | case MACH_DS5900: | |
3eb8ceac | 295 | csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR); |
1da177e4 LT |
296 | if (*csr & KN03_MCR_BNK32M) |
297 | stride = 2; | |
298 | break; | |
299 | default: | |
300 | return -ENODEV; | |
301 | break; | |
302 | } | |
303 | ||
87d10f3c | 304 | for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++) |
1da177e4 LT |
305 | if (!ms02nv_init_one(ms02nv_addrs[i] << stride)) |
306 | count++; | |
307 | ||
308 | return (count > 0) ? 0 : -ENODEV; | |
309 | } | |
310 | ||
311 | static void __exit ms02nv_cleanup(void) | |
312 | { | |
313 | while (root_ms02nv_mtd) | |
314 | ms02nv_remove_one(); | |
315 | } | |
316 | ||
317 | ||
318 | module_init(ms02nv_init); | |
319 | module_exit(ms02nv_cleanup); |