Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2001 Maciej W. Rozycki
4 */
5
6#include <linux/init.h>
7#include <linux/ioport.h>
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/mtd/mtd.h>
11#include <linux/slab.h>
12#include <linux/types.h>
13
14#include <asm/addrspace.h>
15#include <asm/bootinfo.h>
16#include <asm/dec/ioasic_addrs.h>
17#include <asm/dec/kn02.h>
18#include <asm/dec/kn03.h>
19#include <asm/io.h>
20#include <asm/paccess.h>
21
22#include "ms02-nv.h"
23
24
25static char version[] __initdata =
26 "ms02-nv.c: v.1.0.0 13 Aug 2001 Maciej W. Rozycki.\n";
27
28MODULE_AUTHOR("Maciej W. Rozycki <macro@linux-mips.org>");
29MODULE_DESCRIPTION("DEC MS02-NV NVRAM module driver");
30MODULE_LICENSE("GPL");
31
32
33/*
34 * Addresses we probe for an MS02-NV at. Modules may be located
35 * at any 8MiB boundary within a 0MiB up to 112MiB range or at any 32MiB
36 * boundary within a 0MiB up to 448MiB range. We don't support a module
37 * at 0MiB, though.
38 */
39static ulong ms02nv_addrs[] __initdata = {
40 0x07000000, 0x06800000, 0x06000000, 0x05800000, 0x05000000,
41 0x04800000, 0x04000000, 0x03800000, 0x03000000, 0x02800000,
42 0x02000000, 0x01800000, 0x01000000, 0x00800000
43};
44
45static const char ms02nv_name[] = "DEC MS02-NV NVRAM";
46static const char ms02nv_res_diag_ram[] = "Diagnostic RAM";
47static const char ms02nv_res_user_ram[] = "General-purpose RAM";
48static const char ms02nv_res_csr[] = "Control and status register";
49
50static struct mtd_info *root_ms02nv_mtd;
51
52
53static int ms02nv_read(struct mtd_info *mtd, loff_t from,
54 size_t len, size_t *retlen, u_char *buf)
55{
56 struct ms02nv_private *mp = mtd->priv;
57
58 memcpy(buf, mp->uaddr + from, len);
59 *retlen = len;
60 return 0;
61}
62
63static int ms02nv_write(struct mtd_info *mtd, loff_t to,
64 size_t len, size_t *retlen, const u_char *buf)
65{
66 struct ms02nv_private *mp = mtd->priv;
67
68 memcpy(mp->uaddr + to, buf, len);
69 *retlen = len;
70 return 0;
71}
72
73
74static inline uint ms02nv_probe_one(ulong addr)
75{
76 ms02nv_uint *ms02nv_diagp;
77 ms02nv_uint *ms02nv_magicp;
78 uint ms02nv_diag;
79 uint ms02nv_magic;
80 size_t size;
81
82 int err;
83
84 /*
85 * The firmware writes MS02NV_ID at MS02NV_MAGIC and also
86 * a diagnostic status at MS02NV_DIAG.
87 */
88 ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG));
89 ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC));
90 err = get_dbe(ms02nv_magic, ms02nv_magicp);
91 if (err)
92 return 0;
93 if (ms02nv_magic != MS02NV_ID)
94 return 0;
95
96 ms02nv_diag = *ms02nv_diagp;
97 size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT;
98 if (size > MS02NV_CSR)
99 size = MS02NV_CSR;
100
101 return size;
102}
103
104static int __init ms02nv_init_one(ulong addr)
105{
106 struct mtd_info *mtd;
107 struct ms02nv_private *mp;
108 struct resource *mod_res;
109 struct resource *diag_res;
110 struct resource *user_res;
111 struct resource *csr_res;
112 ulong fixaddr;
113 size_t size, fixsize;
114
115 static int version_printed;
116
117 int ret = -ENODEV;
118
119 /* The module decodes 8MiB of address space. */
120 mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
121 if (!mod_res)
122 return -ENOMEM;
123
124 mod_res->name = ms02nv_name;
125 mod_res->start = addr;
126 mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
127 mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
128 if (request_resource(&iomem_resource, mod_res) < 0)
129 goto err_out_mod_res;
130
131 size = ms02nv_probe_one(addr);
132 if (!size)
133 goto err_out_mod_res_rel;
134
135 if (!version_printed) {
136 printk(KERN_INFO "%s", version);
137 version_printed = 1;
138 }
139
140 ret = -ENOMEM;
141 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
142 if (!mtd)
143 goto err_out_mod_res_rel;
144 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
145 if (!mp)
146 goto err_out_mtd;
147
148 mtd->priv = mp;
149 mp->resource.module = mod_res;
150
151 /* Firmware's diagnostic NVRAM area. */
152 diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
153 if (!diag_res)
154 goto err_out_mp;
155
156 diag_res->name = ms02nv_res_diag_ram;
157 diag_res->start = addr;
158 diag_res->end = addr + MS02NV_RAM - 1;
159 diag_res->flags = IORESOURCE_BUSY;
160 request_resource(mod_res, diag_res);
161
162 mp->resource.diag_ram = diag_res;
163
164 /* User-available general-purpose NVRAM area. */
165 user_res = kzalloc(sizeof(*user_res), GFP_KERNEL);
166 if (!user_res)
167 goto err_out_diag_res;
168
169 user_res->name = ms02nv_res_user_ram;
170 user_res->start = addr + MS02NV_RAM;
171 user_res->end = addr + size - 1;
172 user_res->flags = IORESOURCE_BUSY;
173 request_resource(mod_res, user_res);
174
175 mp->resource.user_ram = user_res;
176
177 /* Control and status register. */
178 csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL);
179 if (!csr_res)
180 goto err_out_user_res;
181
182 csr_res->name = ms02nv_res_csr;
183 csr_res->start = addr + MS02NV_CSR;
184 csr_res->end = addr + MS02NV_CSR + 3;
185 csr_res->flags = IORESOURCE_BUSY;
186 request_resource(mod_res, csr_res);
187
188 mp->resource.csr = csr_res;
189
190 mp->addr = phys_to_virt(addr);
191 mp->size = size;
192
193 /*
194 * Hide the firmware's diagnostic area. It may get destroyed
195 * upon a reboot. Take paging into account for mapping support.
196 */
197 fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
198 fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
199 mp->uaddr = phys_to_virt(fixaddr);
200
201 mtd->type = MTD_RAM;
202 mtd->flags = MTD_CAP_RAM;
203 mtd->size = fixsize;
204 mtd->name = ms02nv_name;
205 mtd->owner = THIS_MODULE;
206 mtd->_read = ms02nv_read;
207 mtd->_write = ms02nv_write;
208 mtd->writesize = 1;
209
210 ret = -EIO;
211 if (mtd_device_register(mtd, NULL, 0)) {
212 printk(KERN_ERR
213 "ms02-nv: Unable to register MTD device, aborting!\n");
214 goto err_out_csr_res;
215 }
216
217 printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n",
218 mtd->index, ms02nv_name, addr, size >> 20);
219
220 mp->next = root_ms02nv_mtd;
221 root_ms02nv_mtd = mtd;
222
223 return 0;
224
225
226err_out_csr_res:
227 release_resource(csr_res);
228 kfree(csr_res);
229err_out_user_res:
230 release_resource(user_res);
231 kfree(user_res);
232err_out_diag_res:
233 release_resource(diag_res);
234 kfree(diag_res);
235err_out_mp:
236 kfree(mp);
237err_out_mtd:
238 kfree(mtd);
239err_out_mod_res_rel:
240 release_resource(mod_res);
241err_out_mod_res:
242 kfree(mod_res);
243 return ret;
244}
245
246static void __exit ms02nv_remove_one(void)
247{
248 struct mtd_info *mtd = root_ms02nv_mtd;
249 struct ms02nv_private *mp = mtd->priv;
250
251 root_ms02nv_mtd = mp->next;
252
253 mtd_device_unregister(mtd);
254
255 release_resource(mp->resource.csr);
256 kfree(mp->resource.csr);
257 release_resource(mp->resource.user_ram);
258 kfree(mp->resource.user_ram);
259 release_resource(mp->resource.diag_ram);
260 kfree(mp->resource.diag_ram);
261 release_resource(mp->resource.module);
262 kfree(mp->resource.module);
263 kfree(mp);
264 kfree(mtd);
265}
266
267
268static int __init ms02nv_init(void)
269{
270 volatile u32 *csr;
271 uint stride = 0;
272 int count = 0;
273 int i;
274
275 switch (mips_machtype) {
276 case MACH_DS5000_200:
277 csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
278 if (*csr & KN02_CSR_BNK32M)
279 stride = 2;
280 break;
281 case MACH_DS5000_2X0:
282 case MACH_DS5900:
283 csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
284 if (*csr & KN03_MCR_BNK32M)
285 stride = 2;
286 break;
287 default:
288 return -ENODEV;
289 }
290
291 for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
292 if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
293 count++;
294
295 return (count > 0) ? 0 : -ENODEV;
296}
297
298static void __exit ms02nv_cleanup(void)
299{
300 while (root_ms02nv_mtd)
301 ms02nv_remove_one();
302}
303
304
305module_init(ms02nv_init);
306module_exit(ms02nv_cleanup);
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.
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
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 memcpy(buf, mp->uaddr + from, len);
63 *retlen = len;
64 return 0;
65}
66
67static int ms02nv_write(struct mtd_info *mtd, loff_t to,
68 size_t len, size_t *retlen, const u_char *buf)
69{
70 struct ms02nv_private *mp = mtd->priv;
71
72 memcpy(mp->uaddr + to, buf, len);
73 *retlen = len;
74 return 0;
75}
76
77
78static inline uint ms02nv_probe_one(ulong addr)
79{
80 ms02nv_uint *ms02nv_diagp;
81 ms02nv_uint *ms02nv_magicp;
82 uint ms02nv_diag;
83 uint ms02nv_magic;
84 size_t size;
85
86 int err;
87
88 /*
89 * The firmware writes MS02NV_ID at MS02NV_MAGIC and also
90 * a diagnostic status at MS02NV_DIAG.
91 */
92 ms02nv_diagp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_DIAG));
93 ms02nv_magicp = (ms02nv_uint *)(CKSEG1ADDR(addr + MS02NV_MAGIC));
94 err = get_dbe(ms02nv_magic, ms02nv_magicp);
95 if (err)
96 return 0;
97 if (ms02nv_magic != MS02NV_ID)
98 return 0;
99
100 ms02nv_diag = *ms02nv_diagp;
101 size = (ms02nv_diag & MS02NV_DIAG_SIZE_MASK) << MS02NV_DIAG_SIZE_SHIFT;
102 if (size > MS02NV_CSR)
103 size = MS02NV_CSR;
104
105 return size;
106}
107
108static int __init ms02nv_init_one(ulong addr)
109{
110 struct mtd_info *mtd;
111 struct ms02nv_private *mp;
112 struct resource *mod_res;
113 struct resource *diag_res;
114 struct resource *user_res;
115 struct resource *csr_res;
116 ulong fixaddr;
117 size_t size, fixsize;
118
119 static int version_printed;
120
121 int ret = -ENODEV;
122
123 /* The module decodes 8MiB of address space. */
124 mod_res = kzalloc(sizeof(*mod_res), GFP_KERNEL);
125 if (!mod_res)
126 return -ENOMEM;
127
128 mod_res->name = ms02nv_name;
129 mod_res->start = addr;
130 mod_res->end = addr + MS02NV_SLOT_SIZE - 1;
131 mod_res->flags = IORESOURCE_MEM | IORESOURCE_BUSY;
132 if (request_resource(&iomem_resource, mod_res) < 0)
133 goto err_out_mod_res;
134
135 size = ms02nv_probe_one(addr);
136 if (!size)
137 goto err_out_mod_res_rel;
138
139 if (!version_printed) {
140 printk(KERN_INFO "%s", version);
141 version_printed = 1;
142 }
143
144 ret = -ENOMEM;
145 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
146 if (!mtd)
147 goto err_out_mod_res_rel;
148 mp = kzalloc(sizeof(*mp), GFP_KERNEL);
149 if (!mp)
150 goto err_out_mtd;
151
152 mtd->priv = mp;
153 mp->resource.module = mod_res;
154
155 /* Firmware's diagnostic NVRAM area. */
156 diag_res = kzalloc(sizeof(*diag_res), GFP_KERNEL);
157 if (!diag_res)
158 goto err_out_mp;
159
160 diag_res->name = ms02nv_res_diag_ram;
161 diag_res->start = addr;
162 diag_res->end = addr + MS02NV_RAM - 1;
163 diag_res->flags = IORESOURCE_BUSY;
164 request_resource(mod_res, diag_res);
165
166 mp->resource.diag_ram = diag_res;
167
168 /* User-available general-purpose NVRAM area. */
169 user_res = kzalloc(sizeof(*user_res), GFP_KERNEL);
170 if (!user_res)
171 goto err_out_diag_res;
172
173 user_res->name = ms02nv_res_user_ram;
174 user_res->start = addr + MS02NV_RAM;
175 user_res->end = addr + size - 1;
176 user_res->flags = IORESOURCE_BUSY;
177 request_resource(mod_res, user_res);
178
179 mp->resource.user_ram = user_res;
180
181 /* Control and status register. */
182 csr_res = kzalloc(sizeof(*csr_res), GFP_KERNEL);
183 if (!csr_res)
184 goto err_out_user_res;
185
186 csr_res->name = ms02nv_res_csr;
187 csr_res->start = addr + MS02NV_CSR;
188 csr_res->end = addr + MS02NV_CSR + 3;
189 csr_res->flags = IORESOURCE_BUSY;
190 request_resource(mod_res, csr_res);
191
192 mp->resource.csr = csr_res;
193
194 mp->addr = phys_to_virt(addr);
195 mp->size = size;
196
197 /*
198 * Hide the firmware's diagnostic area. It may get destroyed
199 * upon a reboot. Take paging into account for mapping support.
200 */
201 fixaddr = (addr + MS02NV_RAM + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
202 fixsize = (size - (fixaddr - addr)) & ~(PAGE_SIZE - 1);
203 mp->uaddr = phys_to_virt(fixaddr);
204
205 mtd->type = MTD_RAM;
206 mtd->flags = MTD_CAP_RAM;
207 mtd->size = fixsize;
208 mtd->name = ms02nv_name;
209 mtd->owner = THIS_MODULE;
210 mtd->_read = ms02nv_read;
211 mtd->_write = ms02nv_write;
212 mtd->writesize = 1;
213
214 ret = -EIO;
215 if (mtd_device_register(mtd, NULL, 0)) {
216 printk(KERN_ERR
217 "ms02-nv: Unable to register MTD device, aborting!\n");
218 goto err_out_csr_res;
219 }
220
221 printk(KERN_INFO "mtd%d: %s at 0x%08lx, size %zuMiB.\n",
222 mtd->index, ms02nv_name, addr, size >> 20);
223
224 mp->next = root_ms02nv_mtd;
225 root_ms02nv_mtd = mtd;
226
227 return 0;
228
229
230err_out_csr_res:
231 release_resource(csr_res);
232 kfree(csr_res);
233err_out_user_res:
234 release_resource(user_res);
235 kfree(user_res);
236err_out_diag_res:
237 release_resource(diag_res);
238 kfree(diag_res);
239err_out_mp:
240 kfree(mp);
241err_out_mtd:
242 kfree(mtd);
243err_out_mod_res_rel:
244 release_resource(mod_res);
245err_out_mod_res:
246 kfree(mod_res);
247 return ret;
248}
249
250static void __exit ms02nv_remove_one(void)
251{
252 struct mtd_info *mtd = root_ms02nv_mtd;
253 struct ms02nv_private *mp = mtd->priv;
254
255 root_ms02nv_mtd = mp->next;
256
257 mtd_device_unregister(mtd);
258
259 release_resource(mp->resource.csr);
260 kfree(mp->resource.csr);
261 release_resource(mp->resource.user_ram);
262 kfree(mp->resource.user_ram);
263 release_resource(mp->resource.diag_ram);
264 kfree(mp->resource.diag_ram);
265 release_resource(mp->resource.module);
266 kfree(mp->resource.module);
267 kfree(mp);
268 kfree(mtd);
269}
270
271
272static int __init ms02nv_init(void)
273{
274 volatile u32 *csr;
275 uint stride = 0;
276 int count = 0;
277 int i;
278
279 switch (mips_machtype) {
280 case MACH_DS5000_200:
281 csr = (volatile u32 *)CKSEG1ADDR(KN02_SLOT_BASE + KN02_CSR);
282 if (*csr & KN02_CSR_BNK32M)
283 stride = 2;
284 break;
285 case MACH_DS5000_2X0:
286 case MACH_DS5900:
287 csr = (volatile u32 *)CKSEG1ADDR(KN03_SLOT_BASE + IOASIC_MCR);
288 if (*csr & KN03_MCR_BNK32M)
289 stride = 2;
290 break;
291 default:
292 return -ENODEV;
293 break;
294 }
295
296 for (i = 0; i < ARRAY_SIZE(ms02nv_addrs); i++)
297 if (!ms02nv_init_one(ms02nv_addrs[i] << stride))
298 count++;
299
300 return (count > 0) ? 0 : -ENODEV;
301}
302
303static void __exit ms02nv_cleanup(void)
304{
305 while (root_ms02nv_mtd)
306 ms02nv_remove_one();
307}
308
309
310module_init(ms02nv_init);
311module_exit(ms02nv_cleanup);