Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Linux ARCnet driver - COM20020 chipset support | |
cb334648 | 3 | * |
1da177e4 LT |
4 | * Written 1997 by David Woodhouse. |
5 | * Written 1994-1999 by Avery Pennarun. | |
6 | * Written 1999 by Martin Mares <mj@ucw.cz>. | |
7 | * Derived from skeleton.c by Donald Becker. | |
8 | * | |
9 | * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com) | |
10 | * for sponsoring the further development of this driver. | |
11 | * | |
12 | * ********************** | |
13 | * | |
14 | * The original copyright of skeleton.c was as follows: | |
15 | * | |
16 | * skeleton.c Written 1993 by Donald Becker. | |
17 | * Copyright 1993 United States Government as represented by the | |
18 | * Director, National Security Agency. This software may only be used | |
19 | * and distributed according to the terms of the GNU General Public License as | |
20 | * modified by SRC, incorporated herein by reference. | |
21 | * | |
22 | * ********************** | |
23 | * | |
24 | * For more details, see drivers/net/arcnet.c | |
25 | * | |
26 | * ********************** | |
27 | */ | |
05a24b23 JP |
28 | |
29 | #define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt | |
30 | ||
1da177e4 LT |
31 | #include <linux/module.h> |
32 | #include <linux/kernel.h> | |
33 | #include <linux/types.h> | |
34 | #include <linux/ioport.h> | |
1da177e4 LT |
35 | #include <linux/errno.h> |
36 | #include <linux/delay.h> | |
37 | #include <linux/netdevice.h> | |
38 | #include <linux/init.h> | |
a6b7a407 | 39 | #include <linux/interrupt.h> |
5e7ef913 | 40 | #include <linux/io.h> |
1da177e4 | 41 | |
26c6d281 JP |
42 | #include "arcdevice.h" |
43 | #include "com20020.h" | |
44 | ||
4e299b92 JP |
45 | static const char * const clockrates[] = { |
46 | "XXXXXXX", "XXXXXXXX", "XXXXXX", "2.5 Mb/s", | |
47 | "1.25Mb/s", "625 Kb/s", "312.5 Kb/s", "156.25 Kb/s", | |
48 | "Reserved", "Reserved", "Reserved" | |
7f5e760c | 49 | }; |
1da177e4 LT |
50 | |
51 | static void com20020_command(struct net_device *dev, int command); | |
52 | static int com20020_status(struct net_device *dev); | |
53 | static void com20020_setmask(struct net_device *dev, int mask); | |
54 | static int com20020_reset(struct net_device *dev, int really_reset); | |
55 | static void com20020_copy_to_card(struct net_device *dev, int bufnum, | |
56 | int offset, void *buf, int count); | |
57 | static void com20020_copy_from_card(struct net_device *dev, int bufnum, | |
58 | int offset, void *buf, int count); | |
59 | static void com20020_set_mc_list(struct net_device *dev); | |
60 | static void com20020_close(struct net_device *); | |
61 | ||
62 | static void com20020_copy_from_card(struct net_device *dev, int bufnum, | |
63 | int offset, void *buf, int count) | |
64 | { | |
65 | int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset; | |
66 | ||
67 | /* set up the address register */ | |
0fec6513 JP |
68 | arcnet_outb((ofs >> 8) | RDDATAflag | AUTOINCflag, |
69 | ioaddr, COM20020_REG_W_ADDR_HI); | |
70 | arcnet_outb(ofs & 0xff, ioaddr, COM20020_REG_W_ADDR_LO); | |
1da177e4 LT |
71 | |
72 | /* copy the data */ | |
0fec6513 JP |
73 | TIME(dev, "insb", count, |
74 | arcnet_insb(ioaddr, COM20020_REG_RW_MEMDATA, buf, count)); | |
1da177e4 LT |
75 | } |
76 | ||
1da177e4 LT |
77 | static void com20020_copy_to_card(struct net_device *dev, int bufnum, |
78 | int offset, void *buf, int count) | |
79 | { | |
80 | int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset; | |
81 | ||
82 | /* set up the address register */ | |
0fec6513 JP |
83 | arcnet_outb((ofs >> 8) | AUTOINCflag, ioaddr, COM20020_REG_W_ADDR_HI); |
84 | arcnet_outb(ofs & 0xff, ioaddr, COM20020_REG_W_ADDR_LO); | |
1da177e4 LT |
85 | |
86 | /* copy the data */ | |
0fec6513 JP |
87 | TIME(dev, "outsb", count, |
88 | arcnet_outsb(ioaddr, COM20020_REG_RW_MEMDATA, buf, count)); | |
1da177e4 LT |
89 | } |
90 | ||
1da177e4 LT |
91 | /* Reset the card and check some basic stuff during the detection stage. */ |
92 | int com20020_check(struct net_device *dev) | |
93 | { | |
94 | int ioaddr = dev->base_addr, status; | |
454d7c9b | 95 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 | 96 | |
0fec6513 JP |
97 | arcnet_outb(0x18 | 0x80, ioaddr, COM20020_REG_W_CONFIG); |
98 | udelay(5); | |
99 | arcnet_outb(0x18 , ioaddr, COM20020_REG_W_CONFIG); | |
1da177e4 LT |
100 | mdelay(RESETtime); |
101 | ||
102 | lp->setup = lp->clockm ? 0 : (lp->clockp << 1); | |
103 | lp->setup2 = (lp->clockm << 4) | 8; | |
104 | ||
105 | /* CHECK: should we do this for SOHARD cards ? */ | |
106 | /* Enable P1Mode for backplane mode */ | |
107 | lp->setup = lp->setup | P1MODE; | |
108 | ||
0fec6513 JP |
109 | com20020_set_subaddress(lp, ioaddr, SUB_SETUP1); |
110 | arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG); | |
1da177e4 | 111 | |
7f5e760c | 112 | if (lp->clockm != 0) { |
0fec6513 JP |
113 | com20020_set_subaddress(lp, ioaddr, SUB_SETUP2); |
114 | arcnet_outb(lp->setup2, ioaddr, COM20020_REG_W_XREG); | |
cb334648 | 115 | |
1da177e4 LT |
116 | /* must now write the magic "restart operation" command */ |
117 | mdelay(1); | |
0fec6513 | 118 | arcnet_outb(0x18, ioaddr, COM20020_REG_W_COMMAND); |
1da177e4 LT |
119 | } |
120 | ||
121 | lp->config = 0x21 | (lp->timeout << 3) | (lp->backplane << 2); | |
122 | /* set node ID to 0x42 (but transmitter is disabled, so it's okay) */ | |
0fec6513 JP |
123 | arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG); |
124 | arcnet_outb(0x42, ioaddr, COM20020_REG_W_XREG); | |
1da177e4 | 125 | |
0fec6513 | 126 | status = arcnet_inb(ioaddr, COM20020_REG_R_STATUS); |
1da177e4 LT |
127 | |
128 | if ((status & 0x99) != (NORXflag | TXFREEflag | RESETflag)) { | |
a34c0932 | 129 | arc_printk(D_NORMAL, dev, "status invalid (%Xh).\n", status); |
1da177e4 LT |
130 | return -ENODEV; |
131 | } | |
a34c0932 | 132 | arc_printk(D_INIT_REASONS, dev, "status after reset: %X\n", status); |
1da177e4 LT |
133 | |
134 | /* Enable TX */ | |
0fec6513 JP |
135 | arcnet_outb(0x39, ioaddr, COM20020_REG_W_CONFIG); |
136 | arcnet_outb(arcnet_inb(ioaddr, 8), ioaddr, COM20020_REG_W_XREG); | |
1da177e4 | 137 | |
0fec6513 JP |
138 | arcnet_outb(CFLAGScmd | RESETclear | CONFIGclear, |
139 | ioaddr, COM20020_REG_W_COMMAND); | |
140 | status = arcnet_inb(ioaddr, COM20020_REG_R_STATUS); | |
a34c0932 JP |
141 | arc_printk(D_INIT_REASONS, dev, "status after reset acknowledged: %X\n", |
142 | status); | |
1da177e4 LT |
143 | |
144 | /* Read first location of memory */ | |
0fec6513 JP |
145 | arcnet_outb(0 | RDDATAflag | AUTOINCflag, |
146 | ioaddr, COM20020_REG_W_ADDR_HI); | |
147 | arcnet_outb(0, ioaddr, COM20020_REG_W_ADDR_LO); | |
1da177e4 | 148 | |
0fec6513 | 149 | status = arcnet_inb(ioaddr, COM20020_REG_RW_MEMDATA); |
97464edd | 150 | if (status != TESTvalue) { |
a34c0932 JP |
151 | arc_printk(D_NORMAL, dev, "Signature byte not found (%02Xh != D1h).\n", |
152 | status); | |
1da177e4 LT |
153 | return -ENODEV; |
154 | } | |
155 | return 0; | |
156 | } | |
157 | ||
a0d2e513 MG |
158 | static int com20020_set_hwaddr(struct net_device *dev, void *addr) |
159 | { | |
160 | int ioaddr = dev->base_addr; | |
161 | struct arcnet_local *lp = netdev_priv(dev); | |
162 | struct sockaddr *hwaddr = addr; | |
163 | ||
164 | memcpy(dev->dev_addr, hwaddr->sa_data, 1); | |
0fec6513 JP |
165 | com20020_set_subaddress(lp, ioaddr, SUB_NODE); |
166 | arcnet_outb(dev->dev_addr[0], ioaddr, COM20020_REG_W_XREG); | |
a0d2e513 MG |
167 | |
168 | return 0; | |
169 | } | |
170 | ||
a1799af4 SH |
171 | const struct net_device_ops com20020_netdev_ops = { |
172 | .ndo_open = arcnet_open, | |
173 | .ndo_stop = arcnet_close, | |
174 | .ndo_start_xmit = arcnet_send_packet, | |
175 | .ndo_tx_timeout = arcnet_timeout, | |
a0d2e513 | 176 | .ndo_set_mac_address = com20020_set_hwaddr, |
afc4b13d | 177 | .ndo_set_rx_mode = com20020_set_mc_list, |
a1799af4 SH |
178 | }; |
179 | ||
1da177e4 LT |
180 | /* Set up the struct net_device associated with this card. Called after |
181 | * probing succeeds. | |
182 | */ | |
183 | int com20020_found(struct net_device *dev, int shared) | |
184 | { | |
185 | struct arcnet_local *lp; | |
186 | int ioaddr = dev->base_addr; | |
187 | ||
188 | /* Initialize the rest of the device structure. */ | |
189 | ||
454d7c9b | 190 | lp = netdev_priv(dev); |
1da177e4 LT |
191 | |
192 | lp->hw.owner = THIS_MODULE; | |
193 | lp->hw.command = com20020_command; | |
194 | lp->hw.status = com20020_status; | |
195 | lp->hw.intmask = com20020_setmask; | |
196 | lp->hw.reset = com20020_reset; | |
197 | lp->hw.copy_to_card = com20020_copy_to_card; | |
198 | lp->hw.copy_from_card = com20020_copy_from_card; | |
199 | lp->hw.close = com20020_close; | |
200 | ||
d6d7d3ed | 201 | /* FIXME: do this some other way! */ |
1da177e4 | 202 | if (!dev->dev_addr[0]) |
0fec6513 | 203 | dev->dev_addr[0] = arcnet_inb(ioaddr, 8); |
1da177e4 | 204 | |
0fec6513 JP |
205 | com20020_set_subaddress(lp, ioaddr, SUB_SETUP1); |
206 | arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG); | |
1da177e4 | 207 | |
7f5e760c | 208 | if (lp->card_flags & ARC_CAN_10MBIT) { |
0fec6513 JP |
209 | com20020_set_subaddress(lp, ioaddr, SUB_SETUP2); |
210 | arcnet_outb(lp->setup2, ioaddr, COM20020_REG_W_XREG); | |
cb334648 | 211 | |
1da177e4 LT |
212 | /* must now write the magic "restart operation" command */ |
213 | mdelay(1); | |
0fec6513 | 214 | arcnet_outb(0x18, ioaddr, COM20020_REG_W_COMMAND); |
1da177e4 LT |
215 | } |
216 | ||
217 | lp->config = 0x20 | (lp->timeout << 3) | (lp->backplane << 2) | 1; | |
218 | /* Default 0x38 + register: Node ID */ | |
0fec6513 JP |
219 | arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG); |
220 | arcnet_outb(dev->dev_addr[0], ioaddr, COM20020_REG_W_XREG); | |
1da177e4 LT |
221 | |
222 | /* reserve the irq */ | |
a0607fd3 | 223 | if (request_irq(dev->irq, arcnet_interrupt, shared, |
1da177e4 | 224 | "arcnet (COM20020)", dev)) { |
a34c0932 | 225 | arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq); |
1da177e4 LT |
226 | return -ENODEV; |
227 | } | |
228 | ||
229 | dev->base_addr = ioaddr; | |
230 | ||
a34c0932 JP |
231 | arc_printk(D_NORMAL, dev, "%s: station %02Xh found at %03lXh, IRQ %d.\n", |
232 | lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq); | |
1da177e4 LT |
233 | |
234 | if (lp->backplane) | |
a34c0932 | 235 | arc_printk(D_NORMAL, dev, "Using backplane mode.\n"); |
1da177e4 LT |
236 | |
237 | if (lp->timeout != 3) | |
a34c0932 JP |
238 | arc_printk(D_NORMAL, dev, "Using extended timeout value of %d\n", |
239 | lp->timeout); | |
1da177e4 | 240 | |
a34c0932 JP |
241 | arc_printk(D_NORMAL, dev, "Using CKP %d - data rate %s\n", |
242 | lp->setup >> 1, | |
4e299b92 JP |
243 | clockrates[3 - |
244 | ((lp->setup2 & 0xF0) >> 4) + | |
245 | ((lp->setup & 0x0F) >> 1)]); | |
246 | /* The clockrates array index looks very fragile. | |
247 | * It seems like it could have negative indexing. | |
248 | */ | |
1da177e4 LT |
249 | |
250 | if (register_netdev(dev)) { | |
251 | free_irq(dev->irq, dev); | |
252 | return -EIO; | |
253 | } | |
254 | return 0; | |
255 | } | |
256 | ||
f2f0a16b | 257 | /* Do a hardware reset on the card, and set up necessary registers. |
cb334648 | 258 | * |
1da177e4 LT |
259 | * This should be called as little as possible, because it disrupts the |
260 | * token on the network (causes a RECON) and requires a significant delay. | |
261 | * | |
262 | * However, it does make sure the card is in a defined state. | |
263 | */ | |
264 | static int com20020_reset(struct net_device *dev, int really_reset) | |
265 | { | |
454d7c9b | 266 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
267 | u_int ioaddr = dev->base_addr; |
268 | u_char inbyte; | |
269 | ||
a34c0932 JP |
270 | arc_printk(D_DEBUG, dev, "%s: %d: %s: dev: %p, lp: %p, dev->name: %s\n", |
271 | __FILE__, __LINE__, __func__, dev, lp, dev->name); | |
272 | arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n", | |
0fec6513 | 273 | dev->name, arcnet_inb(ioaddr, COM20020_REG_R_STATUS)); |
1da177e4 | 274 | |
a34c0932 | 275 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); |
1da177e4 LT |
276 | lp->config = TXENcfg | (lp->timeout << 3) | (lp->backplane << 2); |
277 | /* power-up defaults */ | |
0fec6513 | 278 | arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG); |
a34c0932 | 279 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); |
1da177e4 LT |
280 | |
281 | if (really_reset) { | |
282 | /* reset the card */ | |
0fec6513 JP |
283 | arcnet_outb(lp->config | 0x80, ioaddr, COM20020_REG_W_CONFIG); |
284 | udelay(5); | |
285 | arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG); | |
d6d7d3ed JP |
286 | mdelay(RESETtime * 2); |
287 | /* COM20020 seems to be slower sometimes */ | |
1da177e4 LT |
288 | } |
289 | /* clear flags & end reset */ | |
a34c0932 | 290 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); |
0fec6513 JP |
291 | arcnet_outb(CFLAGScmd | RESETclear | CONFIGclear, |
292 | ioaddr, COM20020_REG_W_COMMAND); | |
1da177e4 LT |
293 | |
294 | /* verify that the ARCnet signature byte is present */ | |
a34c0932 | 295 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); |
1da177e4 LT |
296 | |
297 | com20020_copy_from_card(dev, 0, 0, &inbyte, 1); | |
a34c0932 | 298 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); |
1da177e4 | 299 | if (inbyte != TESTvalue) { |
a34c0932 JP |
300 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", |
301 | __FILE__, __LINE__, __func__); | |
302 | arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n"); | |
1da177e4 LT |
303 | return 1; |
304 | } | |
305 | /* enable extended (512-byte) packets */ | |
0fec6513 JP |
306 | arcnet_outb(CONFIGcmd | EXTconf, ioaddr, COM20020_REG_W_COMMAND); |
307 | ||
a34c0932 | 308 | arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__); |
1da177e4 LT |
309 | |
310 | /* done! return success. */ | |
311 | return 0; | |
312 | } | |
313 | ||
1da177e4 LT |
314 | static void com20020_setmask(struct net_device *dev, int mask) |
315 | { | |
316 | u_int ioaddr = dev->base_addr; | |
01a1d5ac | 317 | |
a34c0932 | 318 | arc_printk(D_DURING, dev, "Setting mask to %x at %x\n", mask, ioaddr); |
0fec6513 | 319 | arcnet_outb(mask, ioaddr, COM20020_REG_W_INTMASK); |
1da177e4 LT |
320 | } |
321 | ||
1da177e4 LT |
322 | static void com20020_command(struct net_device *dev, int cmd) |
323 | { | |
324 | u_int ioaddr = dev->base_addr; | |
01a1d5ac | 325 | |
0fec6513 | 326 | arcnet_outb(cmd, ioaddr, COM20020_REG_W_COMMAND); |
1da177e4 LT |
327 | } |
328 | ||
1da177e4 LT |
329 | static int com20020_status(struct net_device *dev) |
330 | { | |
331 | u_int ioaddr = dev->base_addr; | |
332 | ||
0fec6513 JP |
333 | return arcnet_inb(ioaddr, COM20020_REG_R_STATUS) + |
334 | (arcnet_inb(ioaddr, COM20020_REG_R_DIAGSTAT) << 8); | |
1da177e4 LT |
335 | } |
336 | ||
337 | static void com20020_close(struct net_device *dev) | |
338 | { | |
454d7c9b | 339 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
340 | int ioaddr = dev->base_addr; |
341 | ||
342 | /* disable transmitter */ | |
343 | lp->config &= ~TXENcfg; | |
0fec6513 | 344 | arcnet_outb(lp->config, ioaddr, COM20020_REG_W_CONFIG); |
1da177e4 LT |
345 | } |
346 | ||
347 | /* Set or clear the multicast filter for this adaptor. | |
348 | * num_addrs == -1 Promiscuous mode, receive all packets | |
349 | * num_addrs == 0 Normal mode, clear multicast list | |
350 | * num_addrs > 0 Multicast mode, receive normal and MC packets, and do | |
351 | * best-effort filtering. | |
352 | * FIXME - do multicast stuff, not just promiscuous. | |
353 | */ | |
354 | static void com20020_set_mc_list(struct net_device *dev) | |
355 | { | |
454d7c9b | 356 | struct arcnet_local *lp = netdev_priv(dev); |
1da177e4 LT |
357 | int ioaddr = dev->base_addr; |
358 | ||
d6d7d3ed JP |
359 | if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) { |
360 | /* Enable promiscuous mode */ | |
1da177e4 | 361 | if (!(lp->setup & PROMISCset)) |
a34c0932 | 362 | arc_printk(D_NORMAL, dev, "Setting promiscuous flag...\n"); |
0fec6513 | 363 | com20020_set_subaddress(lp, ioaddr, SUB_SETUP1); |
1da177e4 | 364 | lp->setup |= PROMISCset; |
0fec6513 | 365 | arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG); |
d6d7d3ed | 366 | } else { |
1da177e4 | 367 | /* Disable promiscuous mode, use normal mode */ |
1da177e4 | 368 | if ((lp->setup & PROMISCset)) |
a34c0932 | 369 | arc_printk(D_NORMAL, dev, "Resetting promiscuous flag...\n"); |
0fec6513 | 370 | com20020_set_subaddress(lp, ioaddr, SUB_SETUP1); |
1da177e4 | 371 | lp->setup &= ~PROMISCset; |
0fec6513 | 372 | arcnet_outb(lp->setup, ioaddr, COM20020_REG_W_XREG); |
1da177e4 LT |
373 | } |
374 | } | |
375 | ||
ace48ffb | 376 | #if defined(CONFIG_ARCNET_COM20020_PCI_MODULE) || \ |
2a10387e RD |
377 | defined(CONFIG_ARCNET_COM20020_ISA_MODULE) || \ |
378 | defined(CONFIG_ARCNET_COM20020_CS_MODULE) | |
1da177e4 LT |
379 | EXPORT_SYMBOL(com20020_check); |
380 | EXPORT_SYMBOL(com20020_found); | |
dd0a251c | 381 | EXPORT_SYMBOL(com20020_netdev_ops); |
ace48ffb | 382 | #endif |
1da177e4 LT |
383 | |
384 | MODULE_LICENSE("GPL"); | |
385 | ||
ace48ffb RD |
386 | #ifdef MODULE |
387 | ||
0ebd1c1d | 388 | static int __init com20020_module_init(void) |
1da177e4 | 389 | { |
72aeea48 | 390 | if (BUGLVL(D_NORMAL)) |
d6d7d3ed | 391 | pr_info("%s\n", "COM20020 chipset support (by David Woodhouse et al.)"); |
1da177e4 LT |
392 | return 0; |
393 | } | |
394 | ||
0ebd1c1d | 395 | static void __exit com20020_module_exit(void) |
1da177e4 LT |
396 | { |
397 | } | |
0ebd1c1d JS |
398 | module_init(com20020_module_init); |
399 | module_exit(com20020_module_exit); | |
1da177e4 | 400 | #endif /* MODULE */ |