1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/moduleparam.h>
33#include <linux/ioport.h>
34#include <linux/delay.h>
35#include <linux/netdevice.h>
36#include <linux/bootmem.h>
37#include <linux/init.h>
38#include <linux/interrupt.h>
39#include <linux/io.h>
40
41#include "arcdevice.h"
42#include "com9026.h"
43
44
45
46static int arcrimi_probe(struct net_device *dev);
47static int arcrimi_found(struct net_device *dev);
48static void arcrimi_command(struct net_device *dev, int command);
49static int arcrimi_status(struct net_device *dev);
50static void arcrimi_setmask(struct net_device *dev, int mask);
51static int arcrimi_reset(struct net_device *dev, int really_reset);
52static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
53 void *buf, int count);
54static void arcrimi_copy_from_card(struct net_device *dev, int bufnum,
55 int offset, void *buf, int count);
56
57
58
59
60#define BUFFER_SIZE (512)
61#define MIRROR_SIZE (BUFFER_SIZE * 4)
62
63
64
65
66
67static int __init arcrimi_probe(struct net_device *dev)
68{
69 if (BUGLVL(D_NORMAL)) {
70 pr_info("%s\n", "RIM I (entirely mem-mapped) support");
71 pr_info("E-mail me if you actually test the RIM I driver, please!\n");
72 pr_info("Given: node %02Xh, shmem %lXh, irq %d\n",
73 dev->dev_addr[0], dev->mem_start, dev->irq);
74 }
75
76 if (dev->mem_start <= 0 || dev->irq <= 0) {
77 if (BUGLVL(D_NORMAL))
78 pr_err("No autoprobe for RIM I; you must specify the shmem and irq!\n");
79 return -ENODEV;
80 }
81 if (dev->dev_addr[0] == 0) {
82 if (BUGLVL(D_NORMAL))
83 pr_err("You need to specify your card's station ID!\n");
84 return -ENODEV;
85 }
86
87
88
89
90
91 if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) {
92 if (BUGLVL(D_NORMAL))
93 pr_notice("Card memory already allocated\n");
94 return -ENODEV;
95 }
96 return arcrimi_found(dev);
97}
98
99static int check_mirror(unsigned long addr, size_t size)
100{
101 void __iomem *p;
102 int res = -1;
103
104 if (!request_mem_region(addr, size, "arcnet (90xx)"))
105 return -1;
106
107 p = ioremap(addr, size);
108 if (p) {
109 if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue)
110 res = 1;
111 else
112 res = 0;
113 iounmap(p);
114 }
115
116 release_mem_region(addr, size);
117 return res;
118}
119
120
121
122
123static int __init arcrimi_found(struct net_device *dev)
124{
125 struct arcnet_local *lp;
126 unsigned long first_mirror, last_mirror, shmem;
127 void __iomem *p;
128 int mirror_size;
129 int err;
130
131 p = ioremap(dev->mem_start, MIRROR_SIZE);
132 if (!p) {
133 release_mem_region(dev->mem_start, MIRROR_SIZE);
134 arc_printk(D_NORMAL, dev, "Can't ioremap\n");
135 return -ENODEV;
136 }
137
138
139 if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) {
140 iounmap(p);
141 release_mem_region(dev->mem_start, MIRROR_SIZE);
142 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
143 return -ENODEV;
144 }
145
146 shmem = dev->mem_start;
147 arcnet_writeb(TESTvalue, p, COM9026_REG_W_INTMASK);
148 arcnet_writeb(TESTvalue, p, COM9026_REG_W_COMMAND);
149
150
151
152
153
154
155
156
157 mirror_size = MIRROR_SIZE;
158 if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue &&
159 check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 &&
160 check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1)
161 mirror_size = 2 * MIRROR_SIZE;
162
163 first_mirror = shmem - mirror_size;
164 while (check_mirror(first_mirror, mirror_size) == 1)
165 first_mirror -= mirror_size;
166 first_mirror += mirror_size;
167
168 last_mirror = shmem + mirror_size;
169 while (check_mirror(last_mirror, mirror_size) == 1)
170 last_mirror += mirror_size;
171 last_mirror -= mirror_size;
172
173 dev->mem_start = first_mirror;
174 dev->mem_end = last_mirror + MIRROR_SIZE - 1;
175
176
177
178 lp = netdev_priv(dev);
179 lp->card_name = "RIM I";
180 lp->hw.command = arcrimi_command;
181 lp->hw.status = arcrimi_status;
182 lp->hw.intmask = arcrimi_setmask;
183 lp->hw.reset = arcrimi_reset;
184 lp->hw.owner = THIS_MODULE;
185 lp->hw.copy_to_card = arcrimi_copy_to_card;
186 lp->hw.copy_from_card = arcrimi_copy_from_card;
187
188
189
190
191
192
193 iounmap(p);
194 release_mem_region(shmem, MIRROR_SIZE);
195 if (!request_mem_region(dev->mem_start,
196 dev->mem_end - dev->mem_start + 1,
197 "arcnet (90xx)")) {
198 arc_printk(D_NORMAL, dev, "Card memory already allocated\n");
199 goto err_free_irq;
200 }
201
202 lp->mem_start = ioremap(dev->mem_start,
203 dev->mem_end - dev->mem_start + 1);
204 if (!lp->mem_start) {
205 arc_printk(D_NORMAL, dev, "Can't remap device memory!\n");
206 goto err_release_mem;
207 }
208
209
210 dev->dev_addr[0] = arcnet_readb(lp->mem_start, COM9026_REG_R_STATION);
211
212 arc_printk(D_NORMAL, dev, "ARCnet RIM I: station %02Xh found at IRQ %d, ShMem %lXh (%ld*%d bytes)\n",
213 dev->dev_addr[0],
214 dev->irq, dev->mem_start,
215 (dev->mem_end - dev->mem_start + 1) / mirror_size,
216 mirror_size);
217
218 err = register_netdev(dev);
219 if (err)
220 goto err_unmap;
221
222 return 0;
223
224err_unmap:
225 iounmap(lp->mem_start);
226err_release_mem:
227 release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
228err_free_irq:
229 free_irq(dev->irq, dev);
230 return -EIO;
231}
232
233
234
235
236
237
238
239
240static int arcrimi_reset(struct net_device *dev, int really_reset)
241{
242 struct arcnet_local *lp = netdev_priv(dev);
243 void __iomem *ioaddr = lp->mem_start + 0x800;
244
245 arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
246 dev->name, arcnet_readb(ioaddr, COM9026_REG_R_STATUS));
247
248 if (really_reset) {
249 arcnet_writeb(TESTvalue, ioaddr, -0x800);
250 return 0;
251 }
252
253 arcnet_writeb(CFLAGScmd | RESETclear, ioaddr, COM9026_REG_W_COMMAND);
254 arcnet_writeb(CFLAGScmd | CONFIGclear, ioaddr, COM9026_REG_W_COMMAND);
255
256
257 arcnet_writeb(CONFIGcmd | EXTconf, ioaddr, COM9026_REG_W_COMMAND);
258
259
260 return 0;
261}
262
263static void arcrimi_setmask(struct net_device *dev, int mask)
264{
265 struct arcnet_local *lp = netdev_priv(dev);
266 void __iomem *ioaddr = lp->mem_start + 0x800;
267
268 arcnet_writeb(mask, ioaddr, COM9026_REG_W_INTMASK);
269}
270
271static int arcrimi_status(struct net_device *dev)
272{
273 struct arcnet_local *lp = netdev_priv(dev);
274 void __iomem *ioaddr = lp->mem_start + 0x800;
275
276 return arcnet_readb(ioaddr, COM9026_REG_R_STATUS);
277}
278
279static void arcrimi_command(struct net_device *dev, int cmd)
280{
281 struct arcnet_local *lp = netdev_priv(dev);
282 void __iomem *ioaddr = lp->mem_start + 0x800;
283
284 arcnet_writeb(cmd, ioaddr, COM9026_REG_W_COMMAND);
285}
286
287static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset,
288 void *buf, int count)
289{
290 struct arcnet_local *lp = netdev_priv(dev);
291 void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
292
293 TIME(dev, "memcpy_toio", count, memcpy_toio(memaddr, buf, count));
294}
295
296static void arcrimi_copy_from_card(struct net_device *dev, int bufnum,
297 int offset, void *buf, int count)
298{
299 struct arcnet_local *lp = netdev_priv(dev);
300 void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset;
301
302 TIME(dev, "memcpy_fromio", count, memcpy_fromio(buf, memaddr, count));
303}
304
305static int node;
306static int io;
307static int irq;
308static char device[9];
309
310module_param(node, int, 0);
311module_param(io, int, 0);
312module_param(irq, int, 0);
313module_param_string(device, device, sizeof(device), 0);
314MODULE_LICENSE("GPL");
315
316static struct net_device *my_dev;
317
318static int __init arc_rimi_init(void)
319{
320 struct net_device *dev;
321
322 dev = alloc_arcdev(device);
323 if (!dev)
324 return -ENOMEM;
325
326 if (node && node != 0xff)
327 dev->dev_addr[0] = node;
328
329 dev->mem_start = io;
330 dev->irq = irq;
331 if (dev->irq == 2)
332 dev->irq = 9;
333
334 if (arcrimi_probe(dev)) {
335 free_netdev(dev);
336 return -EIO;
337 }
338
339 my_dev = dev;
340 return 0;
341}
342
343static void __exit arc_rimi_exit(void)
344{
345 struct net_device *dev = my_dev;
346 struct arcnet_local *lp = netdev_priv(dev);
347
348 unregister_netdev(dev);
349 iounmap(lp->mem_start);
350 release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1);
351 free_irq(dev->irq, dev);
352 free_netdev(dev);
353}
354
355#ifndef MODULE
356static int __init arcrimi_setup(char *s)
357{
358 int ints[8];
359
360 s = get_options(s, 8, ints);
361 if (!ints[0])
362 return 1;
363 switch (ints[0]) {
364 default:
365 pr_err("Too many arguments\n");
366 case 3:
367 node = ints[3];
368 case 2:
369 irq = ints[2];
370 case 1:
371 io = ints[1];
372 }
373 if (*s)
374 snprintf(device, sizeof(device), "%s", s);
375 return 1;
376}
377__setup("arcrimi=", arcrimi_setup);
378#endif
379
380module_init(arc_rimi_init)
381module_exit(arc_rimi_exit)
382