blob: e813fc619ac90bc121b204f62ba670a91f31b3e6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - COM20020 chipset support
Joe Perchescb334642015-05-05 10:05:47 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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 */
Joe Perches05a24b22015-05-05 10:05:56 -070028
29#define pr_fmt(fmt) "arcnet:" KBUILD_MODNAME ": " fmt
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
32#include <linux/kernel.h>
33#include <linux/types.h>
34#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/errno.h>
36#include <linux/delay.h>
37#include <linux/netdevice.h>
38#include <linux/init.h>
Alexey Dobriyana6b7a402011-06-06 10:43:46 +000039#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/arcdevice.h>
41#include <linux/com20020.h>
42
Joe Perches5e7ef912015-05-05 10:05:51 -070043#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Joe Perches7f5e7602015-05-05 10:05:49 -070045static char *clockrates[] = {
46 "XXXXXXX", "XXXXXXXX", "XXXXXX",
47 "2.5 Mb/s", "1.25Mb/s", "625 Kb/s", "312.5 Kb/s",
48 "156.25 Kb/s", "Reserved", "Reserved", "Reserved"
49};
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51static void com20020_command(struct net_device *dev, int command);
52static int com20020_status(struct net_device *dev);
53static void com20020_setmask(struct net_device *dev, int mask);
54static int com20020_reset(struct net_device *dev, int really_reset);
55static void com20020_copy_to_card(struct net_device *dev, int bufnum,
56 int offset, void *buf, int count);
57static void com20020_copy_from_card(struct net_device *dev, int bufnum,
58 int offset, void *buf, int count);
59static void com20020_set_mc_list(struct net_device *dev);
60static void com20020_close(struct net_device *);
61
62static 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 */
68 outb((ofs >> 8) | RDDATAflag | AUTOINCflag, _ADDR_HI);
69 outb(ofs & 0xff, _ADDR_LO);
70
71 /* copy the data */
Joe Perchesa34c0932015-05-05 10:05:55 -070072 TIME(dev, "insb", count, insb(_MEMDATA, buf, count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static void com20020_copy_to_card(struct net_device *dev, int bufnum,
76 int offset, void *buf, int count)
77{
78 int ioaddr = dev->base_addr, ofs = 512 * bufnum + offset;
79
80 /* set up the address register */
81 outb((ofs >> 8) | AUTOINCflag, _ADDR_HI);
82 outb(ofs & 0xff, _ADDR_LO);
83
84 /* copy the data */
Joe Perchesa34c0932015-05-05 10:05:55 -070085 TIME(dev, "outsb", count, outsb(_MEMDATA, buf, count));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Reset the card and check some basic stuff during the detection stage. */
89int com20020_check(struct net_device *dev)
90{
91 int ioaddr = dev->base_addr, status;
Wang Chen454d7c92008-11-12 23:37:49 -080092 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 ARCRESET0;
95 mdelay(RESETtime);
96
97 lp->setup = lp->clockm ? 0 : (lp->clockp << 1);
98 lp->setup2 = (lp->clockm << 4) | 8;
99
100 /* CHECK: should we do this for SOHARD cards ? */
101 /* Enable P1Mode for backplane mode */
102 lp->setup = lp->setup | P1MODE;
103
104 SET_SUBADR(SUB_SETUP1);
105 outb(lp->setup, _XREG);
106
Joe Perches7f5e7602015-05-05 10:05:49 -0700107 if (lp->clockm != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 SET_SUBADR(SUB_SETUP2);
109 outb(lp->setup2, _XREG);
Joe Perchescb334642015-05-05 10:05:47 -0700110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /* must now write the magic "restart operation" command */
112 mdelay(1);
113 outb(0x18, _COMMAND);
114 }
115
116 lp->config = 0x21 | (lp->timeout << 3) | (lp->backplane << 2);
117 /* set node ID to 0x42 (but transmitter is disabled, so it's okay) */
118 SETCONF;
Joe Perchescb334642015-05-05 10:05:47 -0700119 outb(0x42, ioaddr + BUS_ALIGN * 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 status = ASTATUS();
122
123 if ((status & 0x99) != (NORXflag | TXFREEflag | RESETflag)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700124 arc_printk(D_NORMAL, dev, "status invalid (%Xh).\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -ENODEV;
126 }
Joe Perchesa34c0932015-05-05 10:05:55 -0700127 arc_printk(D_INIT_REASONS, dev, "status after reset: %X\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Enable TX */
130 outb(0x39, _CONFIG);
Joe Perchescb334642015-05-05 10:05:47 -0700131 outb(inb(ioaddr + BUS_ALIGN * 8), ioaddr + BUS_ALIGN * 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
133 ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
134
135 status = ASTATUS();
Joe Perchesa34c0932015-05-05 10:05:55 -0700136 arc_printk(D_INIT_REASONS, dev, "status after reset acknowledged: %X\n",
137 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* Read first location of memory */
140 outb(0 | RDDATAflag | AUTOINCflag, _ADDR_HI);
141 outb(0, _ADDR_LO);
142
143 if ((status = inb(_MEMDATA)) != TESTvalue) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700144 arc_printk(D_NORMAL, dev, "Signature byte not found (%02Xh != D1h).\n",
145 status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -ENODEV;
147 }
148 return 0;
149}
150
Michael Grzeschika0d2e512014-09-29 11:55:35 +0200151static int com20020_set_hwaddr(struct net_device *dev, void *addr)
152{
153 int ioaddr = dev->base_addr;
154 struct arcnet_local *lp = netdev_priv(dev);
155 struct sockaddr *hwaddr = addr;
156
157 memcpy(dev->dev_addr, hwaddr->sa_data, 1);
158 SET_SUBADR(SUB_NODE);
159 outb(dev->dev_addr[0], _XREG);
160
161 return 0;
162}
163
Stephen Hemmingera1799af2009-01-09 13:01:10 +0000164const struct net_device_ops com20020_netdev_ops = {
165 .ndo_open = arcnet_open,
166 .ndo_stop = arcnet_close,
167 .ndo_start_xmit = arcnet_send_packet,
168 .ndo_tx_timeout = arcnet_timeout,
Michael Grzeschika0d2e512014-09-29 11:55:35 +0200169 .ndo_set_mac_address = com20020_set_hwaddr,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000170 .ndo_set_rx_mode = com20020_set_mc_list,
Stephen Hemmingera1799af2009-01-09 13:01:10 +0000171};
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/* Set up the struct net_device associated with this card. Called after
174 * probing succeeds.
175 */
176int com20020_found(struct net_device *dev, int shared)
177{
178 struct arcnet_local *lp;
179 int ioaddr = dev->base_addr;
180
181 /* Initialize the rest of the device structure. */
182
Wang Chen454d7c92008-11-12 23:37:49 -0800183 lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
185 lp->hw.owner = THIS_MODULE;
186 lp->hw.command = com20020_command;
187 lp->hw.status = com20020_status;
188 lp->hw.intmask = com20020_setmask;
189 lp->hw.reset = com20020_reset;
190 lp->hw.copy_to_card = com20020_copy_to_card;
191 lp->hw.copy_from_card = com20020_copy_from_card;
192 lp->hw.close = com20020_close;
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 if (!dev->dev_addr[0])
Joe Perchescb334642015-05-05 10:05:47 -0700195 dev->dev_addr[0] = inb(ioaddr + BUS_ALIGN * 8); /* FIXME: do this some other way! */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 SET_SUBADR(SUB_SETUP1);
198 outb(lp->setup, _XREG);
199
Joe Perches7f5e7602015-05-05 10:05:49 -0700200 if (lp->card_flags & ARC_CAN_10MBIT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 SET_SUBADR(SUB_SETUP2);
202 outb(lp->setup2, _XREG);
Joe Perchescb334642015-05-05 10:05:47 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* must now write the magic "restart operation" command */
205 mdelay(1);
206 outb(0x18, _COMMAND);
207 }
208
209 lp->config = 0x20 | (lp->timeout << 3) | (lp->backplane << 2) | 1;
210 /* Default 0x38 + register: Node ID */
211 SETCONF;
212 outb(dev->dev_addr[0], _XREG);
213
214 /* reserve the irq */
Joe Perchesa0607fd2009-11-18 23:29:17 -0800215 if (request_irq(dev->irq, arcnet_interrupt, shared,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 "arcnet (COM20020)", dev)) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700217 arc_printk(D_NORMAL, dev, "Can't get IRQ %d!\n", dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return -ENODEV;
219 }
220
221 dev->base_addr = ioaddr;
222
Joe Perchesa34c0932015-05-05 10:05:55 -0700223 arc_printk(D_NORMAL, dev, "%s: station %02Xh found at %03lXh, IRQ %d.\n",
224 lp->card_name, dev->dev_addr[0], dev->base_addr, dev->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
226 if (lp->backplane)
Joe Perchesa34c0932015-05-05 10:05:55 -0700227 arc_printk(D_NORMAL, dev, "Using backplane mode.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 if (lp->timeout != 3)
Joe Perchesa34c0932015-05-05 10:05:55 -0700230 arc_printk(D_NORMAL, dev, "Using extended timeout value of %d\n",
231 lp->timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Joe Perchesa34c0932015-05-05 10:05:55 -0700233 arc_printk(D_NORMAL, dev, "Using CKP %d - data rate %s\n",
234 lp->setup >> 1,
235 clockrates[3 - ((lp->setup2 & 0xF0) >> 4) + ((lp->setup & 0x0F) >> 1)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (register_netdev(dev)) {
238 free_irq(dev->irq, dev);
239 return -EIO;
240 }
241 return 0;
242}
243
Joe Perchesf2f0a162015-05-05 10:05:52 -0700244/* Do a hardware reset on the card, and set up necessary registers.
Joe Perchescb334642015-05-05 10:05:47 -0700245 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 * 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 */
251static int com20020_reset(struct net_device *dev, int really_reset)
252{
Wang Chen454d7c92008-11-12 23:37:49 -0800253 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 u_int ioaddr = dev->base_addr;
255 u_char inbyte;
256
Joe Perchesa34c0932015-05-05 10:05:55 -0700257 arc_printk(D_DEBUG, dev, "%s: %d: %s: dev: %p, lp: %p, dev->name: %s\n",
258 __FILE__, __LINE__, __func__, dev, lp, dev->name);
259 arc_printk(D_INIT, dev, "Resetting %s (status=%02Xh)\n",
260 dev->name, ASTATUS());
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Joe Perchesa34c0932015-05-05 10:05:55 -0700262 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 lp->config = TXENcfg | (lp->timeout << 3) | (lp->backplane << 2);
264 /* power-up defaults */
265 SETCONF;
Joe Perchesa34c0932015-05-05 10:05:55 -0700266 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 if (really_reset) {
269 /* reset the card */
270 ARCRESET;
271 mdelay(RESETtime * 2); /* COM20020 seems to be slower sometimes */
272 }
273 /* clear flags & end reset */
Joe Perchesa34c0932015-05-05 10:05:55 -0700274 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ACOMMAND(CFLAGScmd | RESETclear | CONFIGclear);
276
277 /* verify that the ARCnet signature byte is present */
Joe Perchesa34c0932015-05-05 10:05:55 -0700278 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
280 com20020_copy_from_card(dev, 0, 0, &inbyte, 1);
Joe Perchesa34c0932015-05-05 10:05:55 -0700281 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (inbyte != TESTvalue) {
Joe Perchesa34c0932015-05-05 10:05:55 -0700283 arc_printk(D_DEBUG, dev, "%s: %d: %s\n",
284 __FILE__, __LINE__, __func__);
285 arc_printk(D_NORMAL, dev, "reset failed: TESTvalue not present.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 return 1;
287 }
288 /* enable extended (512-byte) packets */
289 ACOMMAND(CONFIGcmd | EXTconf);
Joe Perchesa34c0932015-05-05 10:05:55 -0700290 arc_printk(D_DEBUG, dev, "%s: %d: %s\n", __FILE__, __LINE__, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 /* done! return success. */
293 return 0;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static void com20020_setmask(struct net_device *dev, int mask)
297{
298 u_int ioaddr = dev->base_addr;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700299
Joe Perchesa34c0932015-05-05 10:05:55 -0700300 arc_printk(D_DURING, dev, "Setting mask to %x at %x\n", mask, ioaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 AINTMASK(mask);
302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static void com20020_command(struct net_device *dev, int cmd)
305{
306 u_int ioaddr = dev->base_addr;
Joe Perches01a1d5a2015-05-05 10:05:48 -0700307
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 ACOMMAND(cmd);
309}
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311static int com20020_status(struct net_device *dev)
312{
313 u_int ioaddr = dev->base_addr;
314
Joe Perchescb334642015-05-05 10:05:47 -0700315 return ASTATUS() + (ADIAGSTATUS() << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
318static void com20020_close(struct net_device *dev)
319{
Wang Chen454d7c92008-11-12 23:37:49 -0800320 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 int ioaddr = dev->base_addr;
322
323 /* disable transmitter */
324 lp->config &= ~TXENcfg;
325 SETCONF;
326}
327
328/* Set or clear the multicast filter for this adaptor.
329 * num_addrs == -1 Promiscuous mode, receive all packets
330 * num_addrs == 0 Normal mode, clear multicast list
331 * num_addrs > 0 Multicast mode, receive normal and MC packets, and do
332 * best-effort filtering.
333 * FIXME - do multicast stuff, not just promiscuous.
334 */
335static void com20020_set_mc_list(struct net_device *dev)
336{
Wang Chen454d7c92008-11-12 23:37:49 -0800337 struct arcnet_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 int ioaddr = dev->base_addr;
339
340 if ((dev->flags & IFF_PROMISC) && (dev->flags & IFF_UP)) { /* Enable promiscuous mode */
341 if (!(lp->setup & PROMISCset))
Joe Perchesa34c0932015-05-05 10:05:55 -0700342 arc_printk(D_NORMAL, dev, "Setting promiscuous flag...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 SET_SUBADR(SUB_SETUP1);
344 lp->setup |= PROMISCset;
345 outb(lp->setup, _XREG);
346 } else
347 /* Disable promiscuous mode, use normal mode */
348 {
349 if ((lp->setup & PROMISCset))
Joe Perchesa34c0932015-05-05 10:05:55 -0700350 arc_printk(D_NORMAL, dev, "Resetting promiscuous flag...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 SET_SUBADR(SUB_SETUP1);
352 lp->setup &= ~PROMISCset;
353 outb(lp->setup, _XREG);
354 }
355}
356
Randy Dunlapace48ff2006-11-08 19:51:03 -0800357#if defined(CONFIG_ARCNET_COM20020_PCI_MODULE) || \
Randy Dunlap2a103872007-02-12 00:52:22 -0800358 defined(CONFIG_ARCNET_COM20020_ISA_MODULE) || \
359 defined(CONFIG_ARCNET_COM20020_CS_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360EXPORT_SYMBOL(com20020_check);
361EXPORT_SYMBOL(com20020_found);
David S. Millerdd0a2512009-01-25 21:17:25 -0800362EXPORT_SYMBOL(com20020_netdev_ops);
Randy Dunlapace48ff2006-11-08 19:51:03 -0800363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365MODULE_LICENSE("GPL");
366
Randy Dunlapace48ff2006-11-08 19:51:03 -0800367#ifdef MODULE
368
Jon Schindler0ebd1c12008-02-28 01:31:08 -0600369static int __init com20020_module_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Joe Perches72aeea42015-05-05 10:05:54 -0700371 if (BUGLVL(D_NORMAL))
Joe Perches05a24b22015-05-05 10:05:56 -0700372 pr_info("%s\n", "COM20020 chipset support (by David Woodhouse et al.)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 return 0;
374}
375
Jon Schindler0ebd1c12008-02-28 01:31:08 -0600376static void __exit com20020_module_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378}
Jon Schindler0ebd1c12008-02-28 01:31:08 -0600379module_init(com20020_module_init);
380module_exit(com20020_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381#endif /* MODULE */