Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Linux ARCnet driver - "RIM I" (entirely mem-mapped) cards | |
cb334648 | 3 | * |
1da177e4 LT |
4 | * Written 1994-1999 by Avery Pennarun. |
5 | * Written 1999-2000 by Martin Mares <mj@ucw.cz>. | |
6 | * Derived from skeleton.c by Donald Becker. | |
7 | * | |
8 | * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com) | |
9 | * for sponsoring the further development of this driver. | |
10 | * | |
11 | * ********************** | |
12 | * | |
13 | * The original copyright of skeleton.c was as follows: | |
14 | * | |
15 | * skeleton.c Written 1993 by Donald Becker. | |
16 | * Copyright 1993 United States Government as represented by the | |
17 | * Director, National Security Agency. This software may only be used | |
18 | * and distributed according to the terms of the GNU General Public License as | |
19 | * modified by SRC, incorporated herein by reference. | |
20 | * | |
21 | * ********************** | |
22 | * | |
23 | * For more details, see drivers/net/arcnet.c | |
24 | * | |
25 | * ********************** | |
26 | */ | |
05a24b23 JP |
27 | |
28 | #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt | |
29 | ||
1da177e4 LT |
30 | #include <linux/kernel.h> |
31 | #include <linux/module.h> | |
32 | #include <linux/moduleparam.h> | |
33 | #include <linux/ioport.h> | |
1da177e4 LT |
34 | #include <linux/delay.h> |
35 | #include <linux/netdevice.h> | |
36 | #include <linux/bootmem.h> | |
37 | #include <linux/init.h> | |
a6b7a407 | 38 | #include <linux/interrupt.h> |
5e7ef913 | 39 | #include <linux/io.h> |
26c6d281 JP |
40 | |
41 | #include "arcdevice.h" | |
1da177e4 | 42 | |
1da177e4 LT |
43 | /* Internal function declarations */ |
44 | ||
45 | static int arcrimi_probe(struct net_device *dev); | |
46 | static int arcrimi_found(struct net_device *dev); | |
47 | static void arcrimi_command(struct net_device *dev, int command); | |
48 | static int arcrimi_status(struct net_device *dev); | |
49 | static void arcrimi_setmask(struct net_device *dev, int mask); | |
50 | static int arcrimi_reset(struct net_device *dev, int really_reset); | |
51 | static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset, | |
52 | void *buf, int count); | |
d6d7d3ed JP |
53 | static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, |
54 | int offset, void *buf, int count); | |
1da177e4 LT |
55 | |
56 | /* Handy defines for ARCnet specific stuff */ | |
57 | ||
58 | /* Amount of I/O memory used by the card */ | |
cb334648 JP |
59 | #define BUFFER_SIZE (512) |
60 | #define MIRROR_SIZE (BUFFER_SIZE * 4) | |
1da177e4 LT |
61 | |
62 | /* COM 9026 controller chip --> ARCnet register addresses */ | |
4e2f9f1b JP |
63 | #define COM9026_REG_W_INTMASK 0 /* writable */ |
64 | #define COM9026_REG_R_STATUS 0 /* readable */ | |
65 | #define COM9026_REG_W_COMMAND 1 /* writable, returns random vals on read (?) */ | |
66 | #define COM9026_REG_RW_CONFIG 2 /* Configuration register */ | |
67 | #define COM9026_REG_R_RESET 8 /* software reset (on read) */ | |
68 | #define COM9026_REG_RW_MEMDATA 12 /* Data port for IO-mapped memory */ | |
69 | #define COM9026_REG_W_ADDR_LO 14 /* Control registers for said */ | |
70 | #define COM9026_REG_W_ADDR_HI 15 | |
71 | ||
72 | #define COM9026_REG_R_STATION 1 /* Station ID */ | |
1da177e4 | 73 | |
f2f0a16b | 74 | /* We cannot probe for a RIM I card; one reason is I don't know how to reset |
1da177e4 LT |
75 | * them. In fact, we can't even get their node ID automatically. So, we |
76 | * need to be passed a specific shmem address, IRQ, and node ID. | |
77 | */ | |
78 | static int __init arcrimi_probe(struct net_device *dev) | |
79 | { | |
72aeea48 | 80 | if (BUGLVL(D_NORMAL)) { |
05a24b23 JP |
81 | pr_info("%s\n", "RIM I (entirely mem-mapped) support"); |
82 | pr_info("E-mail me if you actually test the RIM I driver, please!\n"); | |
83 | pr_info("Given: node %02Xh, shmem %lXh, irq %d\n", | |
84 | dev->dev_addr[0], dev->mem_start, dev->irq); | |
72aeea48 | 85 | } |
1da177e4 LT |
86 | |
87 | if (dev->mem_start <= 0 || dev->irq <= 0) { | |
72aeea48 | 88 | if (BUGLVL(D_NORMAL)) |
05a24b23 | 89 | pr_err("No autoprobe for RIM I; you must specify the shmem and irq!\n"); |
1da177e4 LT |
90 | return -ENODEV; |
91 | } | |
d0f6ecad | 92 | if (dev->dev_addr[0] == 0) { |
72aeea48 | 93 | if (BUGLVL(D_NORMAL)) |
05a24b23 | 94 | pr_err("You need to specify your card's station ID!\n"); |
d0f6ecad AV |
95 | return -ENODEV; |
96 | } | |
f2f0a16b | 97 | /* Grab the memory region at mem_start for MIRROR_SIZE bytes. |
1da177e4 LT |
98 | * Later in arcrimi_found() the real size will be determined |
99 | * and this reserve will be released and the correct size | |
100 | * will be taken. | |
101 | */ | |
d0f6ecad | 102 | if (!request_mem_region(dev->mem_start, MIRROR_SIZE, "arcnet (90xx)")) { |
72aeea48 | 103 | if (BUGLVL(D_NORMAL)) |
05a24b23 | 104 | pr_notice("Card memory already allocated\n"); |
1da177e4 LT |
105 | return -ENODEV; |
106 | } | |
1da177e4 LT |
107 | return arcrimi_found(dev); |
108 | } | |
109 | ||
d0f6ecad AV |
110 | static int check_mirror(unsigned long addr, size_t size) |
111 | { | |
112 | void __iomem *p; | |
113 | int res = -1; | |
114 | ||
115 | if (!request_mem_region(addr, size, "arcnet (90xx)")) | |
116 | return -1; | |
117 | ||
118 | p = ioremap(addr, size); | |
119 | if (p) { | |
4e2f9f1b | 120 | if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue) |
d0f6ecad AV |
121 | res = 1; |
122 | else | |
123 | res = 0; | |
124 | iounmap(p); | |
125 | } | |
126 | ||
127 | release_mem_region(addr, size); | |
128 | return res; | |
129 | } | |
1da177e4 | 130 | |
f2f0a16b JP |
131 | /* Set up the struct net_device associated with this card. |
132 | * Called after probing succeeds. | |
1da177e4 LT |
133 | */ |
134 | static int __init arcrimi_found(struct net_device *dev) | |
135 | { | |
136 | struct arcnet_local *lp; | |
137 | unsigned long first_mirror, last_mirror, shmem; | |
d0f6ecad | 138 | void __iomem *p; |
1da177e4 LT |
139 | int mirror_size; |
140 | int err; | |
141 | ||
d0f6ecad AV |
142 | p = ioremap(dev->mem_start, MIRROR_SIZE); |
143 | if (!p) { | |
144 | release_mem_region(dev->mem_start, MIRROR_SIZE); | |
a34c0932 | 145 | arc_printk(D_NORMAL, dev, "Can't ioremap\n"); |
d0f6ecad AV |
146 | return -ENODEV; |
147 | } | |
148 | ||
1da177e4 | 149 | /* reserve the irq */ |
a0607fd3 | 150 | if (request_irq(dev->irq, arcnet_interrupt, 0, "arcnet (RIM I)", dev)) { |
d0f6ecad AV |
151 | iounmap(p); |
152 | release_mem_region(dev->mem_start, MIRROR_SIZE); | |
a34c0932 | 153 | arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq); |
1da177e4 LT |
154 | return -ENODEV; |
155 | } | |
156 | ||
157 | shmem = dev->mem_start; | |
4e2f9f1b JP |
158 | arcnet_writeb(TESTvalue, p, COM9026_REG_W_INTMASK); |
159 | arcnet_writeb(TESTvalue, p, COM9026_REG_W_COMMAND); | |
160 | /* actually the station/node ID */ | |
1da177e4 LT |
161 | |
162 | /* find the real shared memory start/end points, including mirrors */ | |
163 | ||
164 | /* guess the actual size of one "memory mirror" - the number of | |
165 | * bytes between copies of the shared memory. On most cards, it's | |
166 | * 2k (or there are no mirrors at all) but on some, it's 4k. | |
167 | */ | |
168 | mirror_size = MIRROR_SIZE; | |
4e2f9f1b | 169 | if (arcnet_readb(p, COM9026_REG_R_STATUS) == TESTvalue && |
8e95a202 JP |
170 | check_mirror(shmem - MIRROR_SIZE, MIRROR_SIZE) == 0 && |
171 | check_mirror(shmem - 2 * MIRROR_SIZE, MIRROR_SIZE) == 1) | |
d0f6ecad | 172 | mirror_size = 2 * MIRROR_SIZE; |
1da177e4 | 173 | |
d0f6ecad AV |
174 | first_mirror = shmem - mirror_size; |
175 | while (check_mirror(first_mirror, mirror_size) == 1) | |
1da177e4 LT |
176 | first_mirror -= mirror_size; |
177 | first_mirror += mirror_size; | |
178 | ||
d0f6ecad AV |
179 | last_mirror = shmem + mirror_size; |
180 | while (check_mirror(last_mirror, mirror_size) == 1) | |
1da177e4 LT |
181 | last_mirror += mirror_size; |
182 | last_mirror -= mirror_size; | |
183 | ||
184 | dev->mem_start = first_mirror; | |
185 | dev->mem_end = last_mirror + MIRROR_SIZE - 1; | |
186 | ||
187 | /* initialize the rest of the device structure. */ | |
188 | ||
454d7c9b | 189 | lp = netdev_priv(dev); |
1da177e4 LT |
190 | lp->card_name = "RIM I"; |
191 | lp->hw.command = arcrimi_command; | |
192 | lp->hw.status = arcrimi_status; | |
193 | lp->hw.intmask = arcrimi_setmask; | |
194 | lp->hw.reset = arcrimi_reset; | |
195 | lp->hw.owner = THIS_MODULE; | |
196 | lp->hw.copy_to_card = arcrimi_copy_to_card; | |
197 | lp->hw.copy_from_card = arcrimi_copy_from_card; | |
198 | ||
f2f0a16b | 199 | /* re-reserve the memory region - arcrimi_probe() alloced this reqion |
1da177e4 LT |
200 | * but didn't know the real size. Free that region and then re-get |
201 | * with the correct size. There is a VERY slim chance this could | |
202 | * fail. | |
203 | */ | |
d0f6ecad AV |
204 | iounmap(p); |
205 | release_mem_region(shmem, MIRROR_SIZE); | |
1da177e4 LT |
206 | if (!request_mem_region(dev->mem_start, |
207 | dev->mem_end - dev->mem_start + 1, | |
208 | "arcnet (90xx)")) { | |
a34c0932 | 209 | arc_printk(D_NORMAL, dev, "Card memory already allocated\n"); |
1da177e4 LT |
210 | goto err_free_irq; |
211 | } | |
212 | ||
d6d7d3ed JP |
213 | lp->mem_start = ioremap(dev->mem_start, |
214 | dev->mem_end - dev->mem_start + 1); | |
1da177e4 | 215 | if (!lp->mem_start) { |
a34c0932 | 216 | arc_printk(D_NORMAL, dev, "Can't remap device memory!\n"); |
1da177e4 LT |
217 | goto err_release_mem; |
218 | } | |
219 | ||
220 | /* get and check the station ID from offset 1 in shmem */ | |
4e2f9f1b | 221 | dev->dev_addr[0] = arcnet_readb(lp->mem_start, COM9026_REG_R_STATION); |
1da177e4 | 222 | |
a34c0932 JP |
223 | arc_printk(D_NORMAL, dev, "ARCnet RIM I: station %02Xh found at IRQ %d, ShMem %lXh (%ld*%d bytes)\n", |
224 | dev->dev_addr[0], | |
225 | dev->irq, dev->mem_start, | |
226 | (dev->mem_end - dev->mem_start + 1) / mirror_size, | |
227 | mirror_size); | |
1da177e4 LT |
228 | |
229 | err = register_netdev(dev); | |
230 | if (err) | |
231 | goto err_unmap; | |
232 | ||
233 | return 0; | |
234 | ||
235 | err_unmap: | |
236 | iounmap(lp->mem_start); | |
237 | err_release_mem: | |
238 | release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1); | |
239 | err_free_irq: | |
240 | free_irq(dev->irq, dev); | |
241 | return -EIO; | |
242 | } | |
243 | ||
f2f0a16b | 244 | /* Do a hardware reset on the card, and set up necessary registers. |
1da177e4 LT |
245 | * |
246 | * This should be called as little as possible, because it disrupts the | |
247 | * token on the network (causes a RECON) and requires a significant delay. | |
248 | * | |
249 | * However, it does make sure the card is in a defined state. | |
250 | */ | |
251 | static int arcrimi_reset(struct net_device *dev, int really_reset) | |
252 | { | |
454d7c9b | 253 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
254 | void __iomem *ioaddr = lp->mem_start + 0x800; |
255 | ||
a34c0932 | 256 | arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n", |
4e2f9f1b | 257 | dev->name, arcnet_readb(ioaddr, COM9026_REG_R_STATUS)); |
1da177e4 LT |
258 | |
259 | if (really_reset) { | |
4e2f9f1b | 260 | arcnet_writeb(TESTvalue, ioaddr, -0x800); /* fake reset */ |
1da177e4 LT |
261 | return 0; |
262 | } | |
4e2f9f1b JP |
263 | /* clear flags & end reset */ |
264 | arcnet_writeb(CFLAGScmd | RESETclear, ioaddr, COM9026_REG_W_COMMAND); | |
265 | arcnet_writeb(CFLAGScmd | CONFIGclear, ioaddr, COM9026_REG_W_COMMAND); | |
1da177e4 LT |
266 | |
267 | /* enable extended (512-byte) packets */ | |
4e2f9f1b | 268 | arcnet_writeb(CONFIGcmd | EXTconf, ioaddr, COM9026_REG_W_COMMAND); |
1da177e4 LT |
269 | |
270 | /* done! return success. */ | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static void arcrimi_setmask(struct net_device *dev, int mask) | |
275 | { | |
454d7c9b | 276 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
277 | void __iomem *ioaddr = lp->mem_start + 0x800; |
278 | ||
4e2f9f1b | 279 | arcnet_writeb(mask, ioaddr, COM9026_REG_W_INTMASK); |
1da177e4 LT |
280 | } |
281 | ||
282 | static int arcrimi_status(struct net_device *dev) | |
283 | { | |
454d7c9b | 284 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
285 | void __iomem *ioaddr = lp->mem_start + 0x800; |
286 | ||
4e2f9f1b | 287 | return arcnet_readb(ioaddr, COM9026_REG_R_STATUS); |
1da177e4 LT |
288 | } |
289 | ||
290 | static void arcrimi_command(struct net_device *dev, int cmd) | |
291 | { | |
454d7c9b | 292 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
293 | void __iomem *ioaddr = lp->mem_start + 0x800; |
294 | ||
4e2f9f1b | 295 | arcnet_writeb(cmd, ioaddr, COM9026_REG_W_COMMAND); |
1da177e4 LT |
296 | } |
297 | ||
298 | static void arcrimi_copy_to_card(struct net_device *dev, int bufnum, int offset, | |
299 | void *buf, int count) | |
300 | { | |
454d7c9b | 301 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 | 302 | void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset; |
01a1d5ac | 303 | |
a34c0932 | 304 | TIME(dev, "memcpy_toio", count, memcpy_toio(memaddr, buf, count)); |
1da177e4 LT |
305 | } |
306 | ||
d6d7d3ed JP |
307 | static void arcrimi_copy_from_card(struct net_device *dev, int bufnum, |
308 | int offset, void *buf, int count) | |
1da177e4 | 309 | { |
454d7c9b | 310 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 | 311 | void __iomem *memaddr = lp->mem_start + 0x800 + bufnum * 512 + offset; |
01a1d5ac | 312 | |
a34c0932 | 313 | TIME(dev, "memcpy_fromio", count, memcpy_fromio(buf, memaddr, count)); |
1da177e4 LT |
314 | } |
315 | ||
316 | static int node; | |
317 | static int io; /* use the insmod io= irq= node= options */ | |
318 | static int irq; | |
319 | static char device[9]; /* use eg. device=arc1 to change name */ | |
320 | ||
321 | module_param(node, int, 0); | |
322 | module_param(io, int, 0); | |
323 | module_param(irq, int, 0); | |
324 | module_param_string(device, device, sizeof(device), 0); | |
325 | MODULE_LICENSE("GPL"); | |
326 | ||
327 | static struct net_device *my_dev; | |
328 | ||
329 | static int __init arc_rimi_init(void) | |
330 | { | |
331 | struct net_device *dev; | |
332 | ||
333 | dev = alloc_arcdev(device); | |
334 | if (!dev) | |
335 | return -ENOMEM; | |
336 | ||
337 | if (node && node != 0xff) | |
338 | dev->dev_addr[0] = node; | |
339 | ||
340 | dev->mem_start = io; | |
341 | dev->irq = irq; | |
342 | if (dev->irq == 2) | |
343 | dev->irq = 9; | |
344 | ||
345 | if (arcrimi_probe(dev)) { | |
346 | free_netdev(dev); | |
347 | return -EIO; | |
348 | } | |
349 | ||
350 | my_dev = dev; | |
351 | return 0; | |
352 | } | |
353 | ||
354 | static void __exit arc_rimi_exit(void) | |
355 | { | |
356 | struct net_device *dev = my_dev; | |
454d7c9b | 357 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
358 | |
359 | unregister_netdev(dev); | |
360 | iounmap(lp->mem_start); | |
361 | release_mem_region(dev->mem_start, dev->mem_end - dev->mem_start + 1); | |
362 | free_irq(dev->irq, dev); | |
363 | free_netdev(dev); | |
364 | } | |
365 | ||
366 | #ifndef MODULE | |
367 | static int __init arcrimi_setup(char *s) | |
368 | { | |
369 | int ints[8]; | |
01a1d5ac | 370 | |
1da177e4 LT |
371 | s = get_options(s, 8, ints); |
372 | if (!ints[0]) | |
373 | return 1; | |
374 | switch (ints[0]) { | |
375 | default: /* ERROR */ | |
05a24b23 | 376 | pr_err("Too many arguments\n"); |
1da177e4 LT |
377 | case 3: /* Node ID */ |
378 | node = ints[3]; | |
379 | case 2: /* IRQ */ | |
380 | irq = ints[2]; | |
381 | case 1: /* IO address */ | |
382 | io = ints[1]; | |
383 | } | |
384 | if (*s) | |
385 | snprintf(device, sizeof(device), "%s", s); | |
386 | return 1; | |
387 | } | |
388 | __setup("arcrimi=", arcrimi_setup); | |
389 | #endif /* MODULE */ | |
390 | ||
391 | module_init(arc_rimi_init) | |
392 | module_exit(arc_rimi_exit) |