1 /*======================================================================
3 A PCMCIA ethernet driver for NS8390-based cards
5 This driver supports the D-Link DE-650 and Linksys EthernetCard
6 cards, the newer D-Link and Linksys combo cards, Accton EN2212
7 cards, the RPTI EP400, and the PreMax PE-200 in non-shared-memory
8 mode, and the IBM Credit Card Adapter, the NE4100, the Thomas
9 Conrad ethernet card, and the Kingston KNE-PCM/x in shared-memory
10 mode. It will also handle the Socket EA card in either mode.
12 Copyright (C) 1999 David A. Hinds -- dahinds@users.sourceforge.net
14 pcnet_cs.c 1.153 2003/11/09 18:53:09
16 The network driver code is based on Donald Becker's NE2000 code:
18 Written 1992,1993 by Donald Becker.
19 Copyright 1993 United States Government as represented by the
20 Director, National Security Agency. This software may be used and
21 distributed according to the terms of the GNU General Public License,
22 incorporated herein by reference.
23 Donald Becker may be reached at becker@scyld.com
25 Based also on Keith Moore's changes to Don Becker's code, for IBM
26 CCAE support. Drivers merged back together, and shared-memory
27 Socket EA support added, by Ken Raeburn, September 1995.
29 ======================================================================*/
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/ptrace.h>
35 #include <linux/slab.h>
36 #include <linux/string.h>
37 #include <linux/timer.h>
38 #include <linux/delay.h>
39 #include <linux/ethtool.h>
40 #include <linux/netdevice.h>
41 #include <../drivers/net/8390.h>
43 #include <pcmcia/version.h>
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/cisreg.h>
52 #include <asm/system.h>
53 #include <asm/byteorder.h>
54 #include <asm/uaccess.h>
56 #define PCNET_CMD 0x00
57 #define PCNET_DATAPORT 0x10 /* NatSemi-defined port window offset. */
58 #define PCNET_RESET 0x1f /* Issue a read to reset, a write to clear. */
59 #define PCNET_MISC 0x18 /* For IBM CCAE and Socket EA cards */
61 #define PCNET_START_PG 0x40 /* First page of TX buffer */
62 #define PCNET_STOP_PG 0x80 /* Last page +1 of RX ring */
64 /* Socket EA cards have a larger packet buffer */
65 #define SOCKET_START_PG 0x01
66 #define SOCKET_STOP_PG 0xff
68 #define PCNET_RDC_TIMEOUT (2*HZ/100) /* Max wait in jiffies for Tx RDC */
70 static char *if_names[] = { "auto", "10baseT", "10base2"};
73 static int pc_debug = PCMCIA_DEBUG;
74 MODULE_PARM(pc_debug, "i");
75 #define DEBUG(n, args...) if (pc_debug>(n)) printk(KERN_DEBUG args)
76 static char *version =
77 "pcnet_cs.c 1.153 2003/11/09 18:53:09 (David Hinds)";
79 #define DEBUG(n, args...)
82 /*====================================================================*/
84 /* Module parameters */
86 MODULE_AUTHOR("David Hinds <dahinds@users.sourceforge.net>");
87 MODULE_DESCRIPTION("NE2000 compatible PCMCIA ethernet driver");
88 MODULE_LICENSE("GPL");
90 #define INT_MODULE_PARM(n, v) static int n = v; MODULE_PARM(n, "i")
92 /* Bit map of interrupts to choose from */
93 INT_MODULE_PARM(irq_mask, 0xdeb8);
94 static int irq_list[4] = { -1 };
95 MODULE_PARM(irq_list, "1-4i");
97 INT_MODULE_PARM(if_port, 1); /* Transceiver type */
98 INT_MODULE_PARM(use_big_buf, 1); /* use 64K packet buffer? */
99 INT_MODULE_PARM(mem_speed, 0); /* shared mem speed, in ns */
100 INT_MODULE_PARM(delay_output, 0); /* pause after xmit? */
101 INT_MODULE_PARM(delay_time, 4); /* in usec */
102 INT_MODULE_PARM(use_shmem, -1); /* use shared memory? */
103 INT_MODULE_PARM(full_duplex, 0); /* full duplex? */
105 /* Ugh! Let the user hardwire the hardware address for queer cards */
106 static int hw_addr[6] = { 0, /* ... */ };
107 MODULE_PARM(hw_addr, "6i");
109 /*====================================================================*/
111 static void mii_phy_probe(struct net_device *dev);
112 static void pcnet_config(dev_link_t *link);
113 static void pcnet_release(dev_link_t *link);
114 static int pcnet_event(event_t event, int priority,
115 event_callback_args_t *args);
116 static int pcnet_open(struct net_device *dev);
117 static int pcnet_close(struct net_device *dev);
118 static int ei_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
119 static struct ethtool_ops netdev_ethtool_ops;
120 static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs);
121 static void ei_watchdog(u_long arg);
122 static void pcnet_reset_8390(struct net_device *dev);
123 static int set_config(struct net_device *dev, struct ifmap *map);
124 static int setup_shmem_window(dev_link_t *link, int start_pg,
125 int stop_pg, int cm_offset);
126 static int setup_dma_config(dev_link_t *link, int start_pg,
129 static dev_link_t *pcnet_attach(void);
130 static void pcnet_detach(dev_link_t *);
132 static dev_info_t dev_info = "pcnet_cs";
133 static dev_link_t *dev_list;
135 /*====================================================================*/
137 typedef struct hw_info_t {
143 #define DELAY_OUTPUT 0x01
144 #define HAS_MISC_REG 0x02
145 #define USE_BIG_BUF 0x04
146 #define HAS_IBM_MISC 0x08
147 #define IS_DL10019 0x10
148 #define IS_DL10022 0x20
150 #define USE_SHMEM 0x80 /* autodetected */
152 #define AM79C9XX_HOME_PHY 0x00006B90 /* HomePNA PHY */
153 #define AM79C9XX_ETH_PHY 0x00006B70 /* 10baseT PHY */
154 #define MII_PHYID_REV_MASK 0xfffffff0
155 #define MII_PHYID_REG1 0x02
156 #define MII_PHYID_REG2 0x03
158 static hw_info_t hw_info[] = {
159 { /* Accton EN2212 */ 0x0ff0, 0x00, 0x00, 0xe8, DELAY_OUTPUT },
160 { /* Allied Telesis LA-PCM */ 0x0ff0, 0x00, 0x00, 0xf4, 0 },
161 { /* APEX MultiCard */ 0x03f4, 0x00, 0x20, 0xe5, 0 },
162 { /* ASANTE FriendlyNet */ 0x4910, 0x00, 0x00, 0x94,
163 DELAY_OUTPUT | HAS_IBM_MISC },
164 { /* Danpex EN-6200P2 */ 0x0110, 0x00, 0x40, 0xc7, 0 },
165 { /* DataTrek NetCard */ 0x0ff0, 0x00, 0x20, 0xe8, 0 },
166 { /* Dayna CommuniCard E */ 0x0110, 0x00, 0x80, 0x19, 0 },
167 { /* D-Link DE-650 */ 0x0040, 0x00, 0x80, 0xc8, 0 },
168 { /* EP-210 Ethernet */ 0x0110, 0x00, 0x40, 0x33, 0 },
169 { /* EP4000 Ethernet */ 0x01c0, 0x00, 0x00, 0xb4, 0 },
170 { /* Epson EEN10B */ 0x0ff0, 0x00, 0x00, 0x48,
171 HAS_MISC_REG | HAS_IBM_MISC },
172 { /* ELECOM Laneed LD-CDWA */ 0xb8, 0x08, 0x00, 0x42, 0 },
173 { /* Hypertec Ethernet */ 0x01c0, 0x00, 0x40, 0x4c, 0 },
174 { /* IBM CCAE */ 0x0ff0, 0x08, 0x00, 0x5a,
175 HAS_MISC_REG | HAS_IBM_MISC },
176 { /* IBM CCAE */ 0x0ff0, 0x00, 0x04, 0xac,
177 HAS_MISC_REG | HAS_IBM_MISC },
178 { /* IBM CCAE */ 0x0ff0, 0x00, 0x06, 0x29,
179 HAS_MISC_REG | HAS_IBM_MISC },
180 { /* IBM FME */ 0x0374, 0x08, 0x00, 0x5a,
181 HAS_MISC_REG | HAS_IBM_MISC },
182 { /* IBM FME */ 0x0374, 0x00, 0x04, 0xac,
183 HAS_MISC_REG | HAS_IBM_MISC },
184 { /* Kansai KLA-PCM/T */ 0x0ff0, 0x00, 0x60, 0x87,
185 HAS_MISC_REG | HAS_IBM_MISC },
186 { /* NSC DP83903 */ 0x0374, 0x08, 0x00, 0x17,
187 HAS_MISC_REG | HAS_IBM_MISC },
188 { /* NSC DP83903 */ 0x0374, 0x00, 0xc0, 0xa8,
189 HAS_MISC_REG | HAS_IBM_MISC },
190 { /* NSC DP83903 */ 0x0374, 0x00, 0xa0, 0xb0,
191 HAS_MISC_REG | HAS_IBM_MISC },
192 { /* NSC DP83903 */ 0x0198, 0x00, 0x20, 0xe0,
193 HAS_MISC_REG | HAS_IBM_MISC },
194 { /* I-O DATA PCLA/T */ 0x0ff0, 0x00, 0xa0, 0xb0, 0 },
195 { /* Katron PE-520 */ 0x0110, 0x00, 0x40, 0xf6, 0 },
196 { /* Kingston KNE-PCM/x */ 0x0ff0, 0x00, 0xc0, 0xf0,
197 HAS_MISC_REG | HAS_IBM_MISC },
198 { /* Kingston KNE-PCM/x */ 0x0ff0, 0xe2, 0x0c, 0x0f,
199 HAS_MISC_REG | HAS_IBM_MISC },
200 { /* Kingston KNE-PC2 */ 0x0180, 0x00, 0xc0, 0xf0, 0 },
201 { /* Maxtech PCN2000 */ 0x5000, 0x00, 0x00, 0xe8, 0 },
202 { /* NDC Instant-Link */ 0x003a, 0x00, 0x80, 0xc6, 0 },
203 { /* NE2000 Compatible */ 0x0ff0, 0x00, 0xa0, 0x0c, 0 },
204 { /* Network General Sniffer */ 0x0ff0, 0x00, 0x00, 0x65,
205 HAS_MISC_REG | HAS_IBM_MISC },
206 { /* Panasonic VEL211 */ 0x0ff0, 0x00, 0x80, 0x45,
207 HAS_MISC_REG | HAS_IBM_MISC },
208 { /* PreMax PE-200 */ 0x07f0, 0x00, 0x20, 0xe0, 0 },
209 { /* RPTI EP400 */ 0x0110, 0x00, 0x40, 0x95, 0 },
210 { /* SCM Ethernet */ 0x0ff0, 0x00, 0x20, 0xcb, 0 },
211 { /* Socket EA */ 0x4000, 0x00, 0xc0, 0x1b,
212 DELAY_OUTPUT | HAS_MISC_REG | USE_BIG_BUF },
213 { /* Socket LP-E CF+ */ 0x01c0, 0x00, 0xc0, 0x1b, 0 },
214 { /* SuperSocket RE450T */ 0x0110, 0x00, 0xe0, 0x98, 0 },
215 { /* Volktek NPL-402CT */ 0x0060, 0x00, 0x40, 0x05, 0 },
216 { /* NEC PC-9801N-J12 */ 0x0ff0, 0x00, 0x00, 0x4c, 0 },
217 { /* PCMCIA Technology OEM */ 0x01c8, 0x00, 0xa0, 0x0c, 0 }
220 #define NR_INFO (sizeof(hw_info)/sizeof(hw_info_t))
222 static hw_info_t default_info = { 0, 0, 0, 0, 0 };
223 static hw_info_t dl10019_info = { 0, 0, 0, 0, IS_DL10019|HAS_MII };
224 static hw_info_t dl10022_info = { 0, 0, 0, 0, IS_DL10022|HAS_MII };
226 typedef struct pcnet_dev_t {
231 struct timer_list watchdog;
232 int stale, fast_poll;
234 u_char eth_phy, pna_phy;
239 static inline pcnet_dev_t *PRIV(struct net_device *dev)
241 char *p = netdev_priv(dev);
242 return (pcnet_dev_t *)(p + sizeof(struct ei_device));
245 /*======================================================================
247 pcnet_attach() creates an "instance" of the driver, allocating
248 local data structures for one device. The device is registered
251 ======================================================================*/
253 static dev_link_t *pcnet_attach(void)
257 struct net_device *dev;
258 client_reg_t client_reg;
261 DEBUG(0, "pcnet_attach()\n");
263 /* Create new ethernet device */
264 dev = __alloc_ei_netdev(sizeof(pcnet_dev_t));
265 if (!dev) return NULL;
270 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE;
271 link->irq.IRQInfo1 = IRQ_INFO2_VALID|IRQ_LEVEL_ID;
272 if (irq_list[0] == -1)
273 link->irq.IRQInfo2 = irq_mask;
275 for (i = 0; i < 4; i++)
276 link->irq.IRQInfo2 |= 1 << irq_list[i];
277 link->conf.Attributes = CONF_ENABLE_IRQ;
278 link->conf.IntType = INT_MEMORY_AND_IO;
280 SET_MODULE_OWNER(dev);
281 dev->open = &pcnet_open;
282 dev->stop = &pcnet_close;
283 dev->set_config = &set_config;
285 /* Register with Card Services */
286 link->next = dev_list;
288 client_reg.dev_info = &dev_info;
289 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
290 client_reg.EventMask =
291 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
292 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
293 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
294 client_reg.event_handler = &pcnet_event;
295 client_reg.Version = 0x0210;
296 client_reg.event_callback_args.client_data = link;
297 ret = pcmcia_register_client(&link->handle, &client_reg);
298 if (ret != CS_SUCCESS) {
299 cs_error(link->handle, RegisterClient, ret);
307 /*======================================================================
309 This deletes a driver "instance". The device is de-registered
310 with Card Services. If it has been released, all local data
311 structures are freed. Otherwise, the structures will be freed
312 when the device is released.
314 ======================================================================*/
316 static void pcnet_detach(dev_link_t *link)
318 struct net_device *dev = link->priv;
321 DEBUG(0, "pcnet_detach(0x%p)\n", link);
323 /* Locate device structure */
324 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
325 if (*linkp == link) break;
329 if (link->state & DEV_CONFIG)
333 pcmcia_deregister_client(link->handle);
335 /* Unlink device structure, free bits */
338 unregister_netdev(dev);
342 /*======================================================================
344 This probes for a card's hardware address, for card types that
345 encode this information in their CIS.
347 ======================================================================*/
349 static hw_info_t *get_hwinfo(dev_link_t *link)
351 struct net_device *dev = link->priv;
357 /* Allocate a small memory window */
358 req.Attributes = WIN_DATA_WIDTH_8|WIN_MEMORY_TYPE_AM|WIN_ENABLE;
359 req.Base = 0; req.Size = 0;
361 i = pcmcia_request_window(&link->handle, &req, &link->win);
362 if (i != CS_SUCCESS) {
363 cs_error(link->handle, RequestWindow, i);
367 virt = ioremap(req.Base, req.Size);
369 for (i = 0; i < NR_INFO; i++) {
370 mem.CardOffset = hw_info[i].offset & ~(req.Size-1);
371 pcmcia_map_mem_page(link->win, &mem);
372 base = &virt[hw_info[i].offset & (req.Size-1)];
373 if ((readb(base+0) == hw_info[i].a0) &&
374 (readb(base+2) == hw_info[i].a1) &&
375 (readb(base+4) == hw_info[i].a2))
379 for (j = 0; j < 6; j++)
380 dev->dev_addr[j] = readb(base + (j<<1));
384 j = pcmcia_release_window(link->win);
386 cs_error(link->handle, ReleaseWindow, j);
387 return (i < NR_INFO) ? hw_info+i : NULL;
390 /*======================================================================
392 This probes for a card's hardware address by reading the PROM.
393 It checks the address against a list of known types, then falls
394 back to a simple NE2000 clone signature check.
396 ======================================================================*/
398 static hw_info_t *get_prom(dev_link_t *link)
400 struct net_device *dev = link->priv;
401 ioaddr_t ioaddr = dev->base_addr;
405 /* This is lifted straight from drivers/net/ne.c */
407 u_char value, offset;
409 {E8390_NODMA+E8390_PAGE0+E8390_STOP, E8390_CMD}, /* Select page 0*/
410 {0x48, EN0_DCFG}, /* Set byte-wide (0x48) access. */
411 {0x00, EN0_RCNTLO}, /* Clear the count regs. */
413 {0x00, EN0_IMR}, /* Mask completion irq. */
415 {E8390_RXOFF, EN0_RXCR}, /* 0x20 Set to monitor */
416 {E8390_TXOFF, EN0_TXCR}, /* 0x02 and loopback mode. */
419 {0x00, EN0_RSARLO}, /* DMA starting at 0x0000. */
421 {E8390_RREAD+E8390_START, E8390_CMD},
424 pcnet_reset_8390(dev);
427 for (i = 0; i < sizeof(program_seq)/sizeof(program_seq[0]); i++)
428 outb_p(program_seq[i].value, ioaddr + program_seq[i].offset);
430 for (i = 0; i < 32; i++)
431 prom[i] = inb(ioaddr + PCNET_DATAPORT);
432 for (i = 0; i < NR_INFO; i++) {
433 if ((prom[0] == hw_info[i].a0) &&
434 (prom[2] == hw_info[i].a1) &&
435 (prom[4] == hw_info[i].a2))
438 if ((i < NR_INFO) || ((prom[28] == 0x57) && (prom[30] == 0x57))) {
439 for (j = 0; j < 6; j++)
440 dev->dev_addr[j] = prom[j<<1];
441 return (i < NR_INFO) ? hw_info+i : &default_info;
446 /*======================================================================
448 For DL10019 based cards, like the Linksys EtherFast
450 ======================================================================*/
452 static hw_info_t *get_dl10019(dev_link_t *link)
454 struct net_device *dev = link->priv;
458 for (sum = 0, i = 0x14; i < 0x1c; i++)
459 sum += inb_p(dev->base_addr + i);
462 for (i = 0; i < 6; i++)
463 dev->dev_addr[i] = inb_p(dev->base_addr + 0x14 + i);
464 i = inb(dev->base_addr + 0x1f);
465 return ((i == 0x91)||(i == 0x99)) ? &dl10022_info : &dl10019_info;
468 /*======================================================================
470 For Asix AX88190 based cards
472 ======================================================================*/
474 static hw_info_t *get_ax88190(dev_link_t *link)
476 struct net_device *dev = link->priv;
477 ioaddr_t ioaddr = dev->base_addr;
480 /* Not much of a test, but the alternatives are messy */
481 if (link->conf.ConfigBase != 0x03c0)
484 outb_p(0x01, ioaddr + EN0_DCFG); /* Set word-wide access. */
485 outb_p(0x00, ioaddr + EN0_RSARLO); /* DMA starting at 0x0400. */
486 outb_p(0x04, ioaddr + EN0_RSARHI);
487 outb_p(E8390_RREAD+E8390_START, ioaddr + E8390_CMD);
489 for (i = 0; i < 6; i += 2) {
490 j = inw(ioaddr + PCNET_DATAPORT);
491 dev->dev_addr[i] = j & 0xff;
492 dev->dev_addr[i+1] = j >> 8;
494 printk(KERN_NOTICE "pcnet_cs: this is an AX88190 card!\n");
495 printk(KERN_NOTICE "pcnet_cs: use axnet_cs instead.\n");
499 /*======================================================================
501 This should be totally unnecessary... but when we can't figure
502 out the hardware address any other way, we'll let the user hard
503 wire it when the module is initialized.
505 ======================================================================*/
507 static hw_info_t *get_hwired(dev_link_t *link)
509 struct net_device *dev = link->priv;
512 for (i = 0; i < 6; i++)
513 if (hw_addr[i] != 0) break;
517 for (i = 0; i < 6; i++)
518 dev->dev_addr[i] = hw_addr[i];
520 return &default_info;
523 /*======================================================================
525 pcnet_config() is scheduled to run after a CARD_INSERTION event
526 is received, to configure the PCMCIA socket, and to make the
527 ethernet device available to the system.
529 ======================================================================*/
531 #define CS_CHECK(fn, ret) \
532 do { last_fn = (fn); if ((last_ret = (ret)) != 0) goto cs_failed; } while (0)
534 static int try_io_port(dev_link_t *link)
537 if (link->io.NumPorts1 == 32) {
538 link->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
539 if (link->io.NumPorts2 > 0) {
540 /* for master/slave multifunction cards */
541 link->io.Attributes2 = IO_DATA_PATH_WIDTH_8;
542 link->irq.Attributes =
543 IRQ_TYPE_DYNAMIC_SHARING|IRQ_FIRST_SHARED;
546 /* This should be two 16-port windows */
547 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
548 link->io.Attributes2 = IO_DATA_PATH_WIDTH_16;
550 if (link->io.BasePort1 == 0) {
551 link->io.IOAddrLines = 16;
552 for (j = 0; j < 0x400; j += 0x20) {
553 link->io.BasePort1 = j ^ 0x300;
554 link->io.BasePort2 = (j ^ 0x300) + 0x10;
555 ret = pcmcia_request_io(link->handle, &link->io);
556 if (ret == CS_SUCCESS) return ret;
560 return pcmcia_request_io(link->handle, &link->io);
564 static void pcnet_config(dev_link_t *link)
566 client_handle_t handle = link->handle;
567 struct net_device *dev = link->priv;
568 pcnet_dev_t *info = PRIV(dev);
571 int i, last_ret, last_fn, start_pg, stop_pg, cm_offset;
572 int manfid = 0, prodid = 0, has_shmem = 0;
577 DEBUG(0, "pcnet_config(0x%p)\n", link);
579 tuple.Attributes = 0;
580 tuple.TupleData = (cisdata_t *)buf;
581 tuple.TupleDataMax = sizeof(buf);
582 tuple.TupleOffset = 0;
583 tuple.DesiredTuple = CISTPL_CONFIG;
584 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
585 CS_CHECK(GetTupleData, pcmcia_get_tuple_data(handle, &tuple));
586 CS_CHECK(ParseTuple, pcmcia_parse_tuple(handle, &tuple, &parse));
587 link->conf.ConfigBase = parse.config.base;
588 link->conf.Present = parse.config.rmask[0];
591 link->state |= DEV_CONFIG;
593 /* Look up current Vcc */
594 CS_CHECK(GetConfigurationInfo, pcmcia_get_configuration_info(handle, &conf));
595 link->conf.Vcc = conf.Vcc;
597 tuple.DesiredTuple = CISTPL_MANFID;
598 tuple.Attributes = TUPLE_RETURN_COMMON;
599 if ((pcmcia_get_first_tuple(handle, &tuple) == CS_SUCCESS) &&
600 (pcmcia_get_tuple_data(handle, &tuple) == CS_SUCCESS)) {
601 manfid = le16_to_cpu(buf[0]);
602 prodid = le16_to_cpu(buf[1]);
605 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
606 tuple.Attributes = 0;
607 CS_CHECK(GetFirstTuple, pcmcia_get_first_tuple(handle, &tuple));
608 while (last_ret == CS_SUCCESS) {
609 cistpl_cftable_entry_t *cfg = &(parse.cftable_entry);
610 cistpl_io_t *io = &(parse.cftable_entry.io);
612 if (pcmcia_get_tuple_data(handle, &tuple) != 0 ||
613 pcmcia_parse_tuple(handle, &tuple, &parse) != 0 ||
614 cfg->index == 0 || cfg->io.nwin == 0)
617 link->conf.ConfigIndex = cfg->index;
618 /* For multifunction cards, by convention, we configure the
619 network function with window 0, and serial with window 1 */
621 i = (io->win[1].len > io->win[0].len);
622 link->io.BasePort2 = io->win[1-i].base;
623 link->io.NumPorts2 = io->win[1-i].len;
625 i = link->io.NumPorts2 = 0;
627 has_shmem = ((cfg->mem.nwin == 1) &&
628 (cfg->mem.win[0].len >= 0x4000));
629 link->io.BasePort1 = io->win[i].base;
630 link->io.NumPorts1 = io->win[i].len;
631 link->io.IOAddrLines = io->flags & CISTPL_IO_LINES_MASK;
632 if (link->io.NumPorts1 + link->io.NumPorts2 >= 32) {
633 last_ret = try_io_port(link);
634 if (last_ret == CS_SUCCESS) break;
637 last_ret = pcmcia_get_next_tuple(handle, &tuple);
639 if (last_ret != CS_SUCCESS) {
640 cs_error(handle, RequestIO, last_ret);
644 CS_CHECK(RequestIRQ, pcmcia_request_irq(handle, &link->irq));
646 if (link->io.NumPorts2 == 8) {
647 link->conf.Attributes |= CONF_ENABLE_SPKR;
648 link->conf.Status = CCSR_AUDIO_ENA;
650 if ((manfid == MANFID_IBM) &&
651 (prodid == PRODID_IBM_HOME_AND_AWAY))
652 link->conf.ConfigIndex |= 0x10;
654 CS_CHECK(RequestConfiguration, pcmcia_request_configuration(handle, &link->conf));
655 dev->irq = link->irq.AssignedIRQ;
656 dev->base_addr = link->io.BasePort1;
657 if (info->flags & HAS_MISC_REG) {
658 if ((if_port == 1) || (if_port == 2))
659 dev->if_port = if_port;
661 printk(KERN_NOTICE "pcnet_cs: invalid if_port requested\n");
666 hw_info = get_hwinfo(link);
668 hw_info = get_prom(link);
670 hw_info = get_dl10019(link);
672 hw_info = get_ax88190(link);
674 hw_info = get_hwired(link);
676 if (hw_info == NULL) {
677 printk(KERN_NOTICE "pcnet_cs: unable to read hardware net"
678 " address for io base %#3lx\n", dev->base_addr);
682 info->flags = hw_info->flags;
683 /* Check for user overrides */
684 info->flags |= (delay_output) ? DELAY_OUTPUT : 0;
685 if ((manfid == MANFID_SOCKET) &&
686 ((prodid == PRODID_SOCKET_LPE) ||
687 (prodid == PRODID_SOCKET_LPE_CF) ||
688 (prodid == PRODID_SOCKET_EIO)))
689 info->flags &= ~USE_BIG_BUF;
691 info->flags &= ~USE_BIG_BUF;
693 if (info->flags & USE_BIG_BUF) {
694 start_pg = SOCKET_START_PG;
695 stop_pg = SOCKET_STOP_PG;
698 start_pg = PCNET_START_PG;
699 stop_pg = PCNET_STOP_PG;
703 /* has_shmem is ignored if use_shmem != -1 */
704 if ((use_shmem == 0) || (!has_shmem && (use_shmem == -1)) ||
705 (setup_shmem_window(link, start_pg, stop_pg, cm_offset) != 0))
706 setup_dma_config(link, start_pg, stop_pg);
708 ei_status.name = "NE2000";
709 ei_status.word16 = 1;
710 ei_status.reset_8390 = &pcnet_reset_8390;
712 SET_ETHTOOL_OPS(dev, &netdev_ethtool_ops);
714 if (info->flags & (IS_DL10019|IS_DL10022)) {
715 u_char id = inb(dev->base_addr + 0x1a);
716 dev->do_ioctl = &ei_ioctl;
718 if ((id == 0x30) && !info->pna_phy && (info->eth_phy == 4))
722 link->dev = &info->node;
723 link->state &= ~DEV_CONFIG_PENDING;
725 #ifdef CONFIG_NET_POLL_CONTROLLER
726 dev->poll_controller = ei_poll;
729 if (register_netdev(dev) != 0) {
730 printk(KERN_NOTICE "pcnet_cs: register_netdev() failed\n");
735 strcpy(info->node.dev_name, dev->name);
737 if (info->flags & (IS_DL10019|IS_DL10022)) {
738 u_char id = inb(dev->base_addr + 0x1a);
739 printk(KERN_INFO "%s: NE2000 (DL100%d rev %02x): ",
740 dev->name, ((info->flags & IS_DL10022) ? 22 : 19), id);
744 printk(KERN_INFO "%s: NE2000 Compatible: ", dev->name);
746 printk("io %#3lx, irq %d,", dev->base_addr, dev->irq);
747 if (info->flags & USE_SHMEM)
748 printk (" mem %#5lx,", dev->mem_start);
749 if (info->flags & HAS_MISC_REG)
750 printk(" %s xcvr,", if_names[dev->if_port]);
752 for (i = 0; i < 6; i++)
753 printk("%02X%s", dev->dev_addr[i], ((i<5) ? ":" : "\n"));
757 cs_error(link->handle, last_fn, last_ret);
760 link->state &= ~DEV_CONFIG_PENDING;
764 /*======================================================================
766 After a card is removed, pcnet_release() will unregister the net
767 device, and release the PCMCIA configuration. If the device is
768 still open, this will be postponed until it is closed.
770 ======================================================================*/
772 static void pcnet_release(dev_link_t *link)
774 pcnet_dev_t *info = PRIV(link->priv);
776 DEBUG(0, "pcnet_release(0x%p)\n", link);
778 if (info->flags & USE_SHMEM) {
780 pcmcia_release_window(link->win);
782 pcmcia_release_configuration(link->handle);
783 pcmcia_release_io(link->handle, &link->io);
784 pcmcia_release_irq(link->handle, &link->irq);
786 link->state &= ~DEV_CONFIG;
789 /*======================================================================
791 The card status event handler. Mostly, this schedules other
792 stuff to run after an event is received. A CARD_REMOVAL event
793 also sets some flags to discourage the net drivers from trying
794 to talk to the card any more.
796 ======================================================================*/
798 static int pcnet_event(event_t event, int priority,
799 event_callback_args_t *args)
801 dev_link_t *link = args->client_data;
802 struct net_device *dev = link->priv;
804 DEBUG(2, "pcnet_event(0x%06x)\n", event);
807 case CS_EVENT_CARD_REMOVAL:
808 link->state &= ~DEV_PRESENT;
809 if (link->state & DEV_CONFIG) {
810 netif_device_detach(dev);
814 case CS_EVENT_CARD_INSERTION:
815 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
818 case CS_EVENT_PM_SUSPEND:
819 link->state |= DEV_SUSPEND;
820 /* Fall through... */
821 case CS_EVENT_RESET_PHYSICAL:
822 if (link->state & DEV_CONFIG) {
824 netif_device_detach(dev);
825 pcmcia_release_configuration(link->handle);
828 case CS_EVENT_PM_RESUME:
829 link->state &= ~DEV_SUSPEND;
830 /* Fall through... */
831 case CS_EVENT_CARD_RESET:
832 if (link->state & DEV_CONFIG) {
833 pcmcia_request_configuration(link->handle, &link->conf);
835 pcnet_reset_8390(dev);
837 netif_device_attach(dev);
845 /*======================================================================
847 MII interface support for DL10019 and DL10022 based cards
849 On the DL10019, the MII IO direction bit is 0x10; on the DL10022
850 it is 0x20. Setting both bits seems to work on both card types.
852 ======================================================================*/
854 #define DLINK_GPIO 0x1c
855 #define DLINK_DIAG 0x1d
856 #define DLINK_EEPROM 0x1e
858 #define MDIO_SHIFT_CLK 0x80
859 #define MDIO_DATA_OUT 0x40
860 #define MDIO_DIR_WRITE 0x30
861 #define MDIO_DATA_WRITE0 (MDIO_DIR_WRITE)
862 #define MDIO_DATA_WRITE1 (MDIO_DIR_WRITE | MDIO_DATA_OUT)
863 #define MDIO_DATA_READ 0x10
864 #define MDIO_MASK 0x0f
866 static void mdio_sync(ioaddr_t addr)
868 int bits, mask = inb(addr) & MDIO_MASK;
869 for (bits = 0; bits < 32; bits++) {
870 outb(mask | MDIO_DATA_WRITE1, addr);
871 outb(mask | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, addr);
875 static int mdio_read(ioaddr_t addr, int phy_id, int loc)
877 u_int cmd = (0x06<<10)|(phy_id<<5)|loc;
878 int i, retval = 0, mask = inb(addr) & MDIO_MASK;
881 for (i = 13; i >= 0; i--) {
882 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
883 outb(mask | dat, addr);
884 outb(mask | dat | MDIO_SHIFT_CLK, addr);
886 for (i = 19; i > 0; i--) {
888 retval = (retval << 1) | ((inb(addr) & MDIO_DATA_READ) != 0);
889 outb(mask | MDIO_SHIFT_CLK, addr);
891 return (retval>>1) & 0xffff;
894 static void mdio_write(ioaddr_t addr, int phy_id, int loc, int value)
896 u_int cmd = (0x05<<28)|(phy_id<<23)|(loc<<18)|(1<<17)|value;
897 int i, mask = inb(addr) & MDIO_MASK;
900 for (i = 31; i >= 0; i--) {
901 int dat = (cmd&(1<<i)) ? MDIO_DATA_WRITE1 : MDIO_DATA_WRITE0;
902 outb(mask | dat, addr);
903 outb(mask | dat | MDIO_SHIFT_CLK, addr);
905 for (i = 1; i >= 0; i--) {
907 outb(mask | MDIO_SHIFT_CLK, addr);
911 static void mdio_reset(ioaddr_t addr, int phy_id)
920 /*======================================================================
922 EEPROM access routines for DL10019 and DL10022 based cards
924 ======================================================================*/
932 #define EE_ADOT 0x01 /* DataOut for ASIC */
933 #define EE_READ_CMD 0x06
935 #define DL19FDUPLX 0x0400 /* DL10019 Full duplex mode */
937 static int read_eeprom(ioaddr_t ioaddr, int location)
940 ioaddr_t ee_addr = ioaddr + DLINK_EEPROM;
941 int read_cmd = location | (EE_READ_CMD << 8);
944 outb(EE_EEP|EE_CS, ee_addr);
946 /* Shift the read command bits out. */
947 for (i = 10; i >= 0; i--) {
948 short dataval = (read_cmd & (1 << i)) ? EE_DO : 0;
949 outb_p(EE_EEP|EE_CS|dataval, ee_addr);
950 outb_p(EE_EEP|EE_CS|dataval|EE_CK, ee_addr);
952 outb(EE_EEP|EE_CS, ee_addr);
954 for (i = 16; i > 0; i--) {
955 outb_p(EE_EEP|EE_CS | EE_CK, ee_addr);
956 retval = (retval << 1) | ((inb(ee_addr) & EE_DI) ? 1 : 0);
957 outb_p(EE_EEP|EE_CS, ee_addr);
960 /* Terminate the EEPROM access. */
966 The internal ASIC registers can be changed by EEPROM READ access
967 with EE_ASIC bit set.
968 In ASIC mode, EE_ADOT is used to output the data to the ASIC.
971 static void write_asic(ioaddr_t ioaddr, int location, short asic_data)
974 ioaddr_t ee_addr = ioaddr + DLINK_EEPROM;
976 int read_cmd = location | (EE_READ_CMD << 8);
978 asic_data |= read_eeprom(ioaddr, location);
981 outb(EE_ASIC|EE_CS|EE_DI, ee_addr);
983 read_cmd = read_cmd >> 1;
985 /* Shift the read command bits out. */
986 for (i = 9; i >= 0; i--) {
987 dataval = (read_cmd & (1 << i)) ? EE_DO : 0;
988 outb_p(EE_ASIC|EE_CS|EE_DI|dataval, ee_addr);
989 outb_p(EE_ASIC|EE_CS|EE_DI|dataval|EE_CK, ee_addr);
990 outb_p(EE_ASIC|EE_CS|EE_DI|dataval, ee_addr);
993 outb(EE_ASIC|EE_CS, ee_addr);
994 outb(EE_ASIC|EE_CS|EE_CK, ee_addr);
995 outb(EE_ASIC|EE_CS, ee_addr);
997 for (i = 15; i >= 0; i--) {
998 dataval = (asic_data & (1 << i)) ? EE_ADOT : 0;
999 outb_p(EE_ASIC|EE_CS|dataval, ee_addr);
1000 outb_p(EE_ASIC|EE_CS|dataval|EE_CK, ee_addr);
1001 outb_p(EE_ASIC|EE_CS|dataval, ee_addr);
1004 /* Terminate the ASIC access. */
1005 outb(EE_ASIC|EE_DI, ee_addr);
1006 outb(EE_ASIC|EE_DI| EE_CK, ee_addr);
1007 outb(EE_ASIC|EE_DI, ee_addr);
1012 /*====================================================================*/
1014 static void set_misc_reg(struct net_device *dev)
1016 ioaddr_t nic_base = dev->base_addr;
1017 pcnet_dev_t *info = PRIV(dev);
1020 if (info->flags & HAS_MISC_REG) {
1021 tmp = inb_p(nic_base + PCNET_MISC) & ~3;
1022 if (dev->if_port == 2)
1024 if (info->flags & USE_BIG_BUF)
1026 if (info->flags & HAS_IBM_MISC)
1028 outb_p(tmp, nic_base + PCNET_MISC);
1030 if (info->flags & IS_DL10022) {
1031 if (info->flags & HAS_MII) {
1032 mdio_reset(nic_base + DLINK_GPIO, info->eth_phy);
1033 /* Restart MII autonegotiation */
1034 mdio_write(nic_base + DLINK_GPIO, info->eth_phy, 0, 0x0000);
1035 mdio_write(nic_base + DLINK_GPIO, info->eth_phy, 0, 0x1200);
1036 info->mii_reset = jiffies;
1038 outb(full_duplex ? 4 : 0, nic_base + DLINK_DIAG);
1043 /*====================================================================*/
1045 static void mii_phy_probe(struct net_device *dev)
1047 pcnet_dev_t *info = PRIV(dev);
1048 ioaddr_t mii_addr = dev->base_addr + DLINK_GPIO;
1052 for (i = 31; i >= 0; i--) {
1053 tmp = mdio_read(mii_addr, i, 1);
1054 if ((tmp == 0) || (tmp == 0xffff))
1056 tmp = mdio_read(mii_addr, i, MII_PHYID_REG1);
1058 phyid |= mdio_read(mii_addr, i, MII_PHYID_REG2);
1059 phyid &= MII_PHYID_REV_MASK;
1060 DEBUG(0, "%s: MII at %d is 0x%08x\n", dev->name, i, phyid);
1061 if (phyid == AM79C9XX_HOME_PHY) {
1063 } else if (phyid != AM79C9XX_ETH_PHY) {
1069 static int pcnet_open(struct net_device *dev)
1071 pcnet_dev_t *info = PRIV(dev);
1072 dev_link_t *link = &info->link;
1074 DEBUG(2, "pcnet_open('%s')\n", dev->name);
1082 request_irq(dev->irq, ei_irq_wrapper, SA_SHIRQ, dev_info, dev);
1084 info->phy_id = info->eth_phy;
1085 info->link_status = 0x00;
1086 init_timer(&info->watchdog);
1087 info->watchdog.function = &ei_watchdog;
1088 info->watchdog.data = (u_long)dev;
1089 info->watchdog.expires = jiffies + HZ;
1090 add_timer(&info->watchdog);
1092 return ei_open(dev);
1095 /*====================================================================*/
1097 static int pcnet_close(struct net_device *dev)
1099 pcnet_dev_t *info = PRIV(dev);
1100 dev_link_t *link = &info->link;
1102 DEBUG(2, "pcnet_close('%s')\n", dev->name);
1105 free_irq(dev->irq, dev);
1108 netif_stop_queue(dev);
1109 del_timer_sync(&info->watchdog);
1114 /*======================================================================
1116 Hard reset the card. This used to pause for the same period that
1117 a 8390 reset command required, but that shouldn't be necessary.
1119 ======================================================================*/
1121 static void pcnet_reset_8390(struct net_device *dev)
1123 ioaddr_t nic_base = dev->base_addr;
1126 ei_status.txing = ei_status.dmaing = 0;
1128 outb_p(E8390_NODMA+E8390_PAGE0+E8390_STOP, nic_base + E8390_CMD);
1130 outb(inb(nic_base + PCNET_RESET), nic_base + PCNET_RESET);
1132 for (i = 0; i < 100; i++) {
1133 if ((inb_p(nic_base+EN0_ISR) & ENISR_RESET) != 0)
1137 outb_p(ENISR_RESET, nic_base + EN0_ISR); /* Ack intr. */
1140 printk(KERN_ERR "%s: pcnet_reset_8390() did not complete.\n",
1144 } /* pcnet_reset_8390 */
1146 /*====================================================================*/
1148 static int set_config(struct net_device *dev, struct ifmap *map)
1150 pcnet_dev_t *info = PRIV(dev);
1151 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1152 if (!(info->flags & HAS_MISC_REG))
1154 else if ((map->port < 1) || (map->port > 2))
1156 dev->if_port = map->port;
1157 printk(KERN_INFO "%s: switched to %s port\n",
1158 dev->name, if_names[dev->if_port]);
1159 NS8390_init(dev, 1);
1164 /*====================================================================*/
1166 static irqreturn_t ei_irq_wrapper(int irq, void *dev_id, struct pt_regs *regs)
1168 struct net_device *dev = dev_id;
1169 pcnet_dev_t *info = PRIV(dev);
1170 irqreturn_t ret = ei_interrupt(irq, dev_id, regs);
1172 if (ret == IRQ_HANDLED)
1177 static void ei_watchdog(u_long arg)
1179 struct net_device *dev = (struct net_device *)arg;
1180 pcnet_dev_t *info = PRIV(dev);
1181 ioaddr_t nic_base = dev->base_addr;
1182 ioaddr_t mii_addr = nic_base + DLINK_GPIO;
1185 if (!netif_device_present(dev)) goto reschedule;
1187 /* Check for pending interrupt with expired latency timer: with
1188 this, we can limp along even if the interrupt is blocked */
1189 outb_p(E8390_NODMA+E8390_PAGE0, nic_base + E8390_CMD);
1190 if (info->stale++ && (inb_p(nic_base + EN0_ISR) & ENISR_ALL)) {
1191 if (!info->fast_poll)
1192 printk(KERN_INFO "%s: interrupt(s) dropped!\n", dev->name);
1193 ei_irq_wrapper(dev->irq, dev, NULL);
1194 info->fast_poll = HZ;
1196 if (info->fast_poll) {
1198 info->watchdog.expires = jiffies + 1;
1199 add_timer(&info->watchdog);
1203 if (!(info->flags & HAS_MII))
1206 mdio_read(mii_addr, info->phy_id, 1);
1207 link = mdio_read(mii_addr, info->phy_id, 1);
1208 if (!link || (link == 0xffff)) {
1209 if (info->eth_phy) {
1210 info->phy_id = info->eth_phy = 0;
1212 printk(KERN_INFO "%s: MII is missing!\n", dev->name);
1213 info->flags &= ~HAS_MII;
1219 if (link != info->link_status) {
1220 u_short p = mdio_read(mii_addr, info->phy_id, 5);
1221 printk(KERN_INFO "%s: %s link beat\n", dev->name,
1222 (link) ? "found" : "lost");
1223 if (link && (info->flags & IS_DL10022)) {
1224 /* Disable collision detection on full duplex links */
1225 outb((p & 0x0140) ? 4 : 0, nic_base + DLINK_DIAG);
1226 } else if (link && (info->flags & IS_DL10019)) {
1227 /* Disable collision detection on full duplex links */
1228 write_asic(dev->base_addr, 4, (p & 0x140) ? DL19FDUPLX : 0);
1231 if (info->phy_id == info->eth_phy) {
1233 printk(KERN_INFO "%s: autonegotiation complete: "
1234 "%sbaseT-%cD selected\n", dev->name,
1235 ((p & 0x0180) ? "100" : "10"),
1236 ((p & 0x0140) ? 'F' : 'H'));
1238 printk(KERN_INFO "%s: link partner did not "
1239 "autonegotiate\n", dev->name);
1241 NS8390_init(dev, 1);
1243 info->link_status = link;
1245 if (info->pna_phy && time_after(jiffies, info->mii_reset + 6*HZ)) {
1246 link = mdio_read(mii_addr, info->eth_phy, 1) & 0x0004;
1247 if (((info->phy_id == info->pna_phy) && link) ||
1248 ((info->phy_id != info->pna_phy) && !link)) {
1249 /* isolate this MII and try flipping to the other one */
1250 mdio_write(mii_addr, info->phy_id, 0, 0x0400);
1251 info->phy_id ^= info->pna_phy ^ info->eth_phy;
1252 printk(KERN_INFO "%s: switched to %s transceiver\n", dev->name,
1253 (info->phy_id == info->eth_phy) ? "ethernet" : "PNA");
1254 mdio_write(mii_addr, info->phy_id, 0,
1255 (info->phy_id == info->eth_phy) ? 0x1000 : 0);
1256 info->link_status = 0;
1257 info->mii_reset = jiffies;
1262 info->watchdog.expires = jiffies + HZ;
1263 add_timer(&info->watchdog);
1266 /*====================================================================*/
1268 static void netdev_get_drvinfo(struct net_device *dev,
1269 struct ethtool_drvinfo *info)
1271 strcpy(info->driver, "pcnet_cs");
1274 static struct ethtool_ops netdev_ethtool_ops = {
1275 .get_drvinfo = netdev_get_drvinfo,
1278 /*====================================================================*/
1281 static int ei_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1283 pcnet_dev_t *info = PRIV(dev);
1284 u16 *data = (u16 *)&rq->ifr_data;
1285 ioaddr_t mii_addr = dev->base_addr + DLINK_GPIO;
1288 data[0] = info->phy_id;
1289 case SIOCGMIIREG: /* Read MII PHY register. */
1290 data[3] = mdio_read(mii_addr, data[0], data[1] & 0x1f);
1292 case SIOCSMIIREG: /* Write MII PHY register. */
1293 if (!capable(CAP_NET_ADMIN))
1295 mdio_write(mii_addr, data[0], data[1] & 0x1f, data[2]);
1301 /*====================================================================*/
1303 static void dma_get_8390_hdr(struct net_device *dev,
1304 struct e8390_pkt_hdr *hdr,
1307 ioaddr_t nic_base = dev->base_addr;
1309 if (ei_status.dmaing) {
1310 printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
1311 "[DMAstat:%1x][irqlock:%1x]\n",
1312 dev->name, ei_status.dmaing, ei_status.irqlock);
1316 ei_status.dmaing |= 0x01;
1317 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
1318 outb_p(sizeof(struct e8390_pkt_hdr), nic_base + EN0_RCNTLO);
1319 outb_p(0, nic_base + EN0_RCNTHI);
1320 outb_p(0, nic_base + EN0_RSARLO); /* On page boundary */
1321 outb_p(ring_page, nic_base + EN0_RSARHI);
1322 outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);
1324 insw(nic_base + PCNET_DATAPORT, hdr,
1325 sizeof(struct e8390_pkt_hdr)>>1);
1326 /* Fix for big endian systems */
1327 hdr->count = le16_to_cpu(hdr->count);
1329 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
1330 ei_status.dmaing &= ~0x01;
1333 /*====================================================================*/
1335 static void dma_block_input(struct net_device *dev, int count,
1336 struct sk_buff *skb, int ring_offset)
1338 ioaddr_t nic_base = dev->base_addr;
1339 int xfer_count = count;
1340 char *buf = skb->data;
1343 if ((ei_debug > 4) && (count != 4))
1344 printk(KERN_DEBUG "%s: [bi=%d]\n", dev->name, count+4);
1346 if (ei_status.dmaing) {
1347 printk(KERN_NOTICE "%s: DMAing conflict in dma_block_input."
1348 "[DMAstat:%1x][irqlock:%1x]\n",
1349 dev->name, ei_status.dmaing, ei_status.irqlock);
1352 ei_status.dmaing |= 0x01;
1353 outb_p(E8390_NODMA+E8390_PAGE0+E8390_START, nic_base + PCNET_CMD);
1354 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
1355 outb_p(count >> 8, nic_base + EN0_RCNTHI);
1356 outb_p(ring_offset & 0xff, nic_base + EN0_RSARLO);
1357 outb_p(ring_offset >> 8, nic_base + EN0_RSARHI);
1358 outb_p(E8390_RREAD+E8390_START, nic_base + PCNET_CMD);
1360 insw(nic_base + PCNET_DATAPORT,buf,count>>1);
1362 buf[count-1] = inb(nic_base + PCNET_DATAPORT), xfer_count++;
1364 /* This was for the ALPHA version only, but enough people have
1365 encountering problems that it is still here. */
1367 if (ei_debug > 4) { /* DMA termination address check... */
1368 int addr, tries = 20;
1370 /* DON'T check for 'inb_p(EN0_ISR) & ENISR_RDC' here
1371 -- it's broken for Rx on some cards! */
1372 int high = inb_p(nic_base + EN0_RSARHI);
1373 int low = inb_p(nic_base + EN0_RSARLO);
1374 addr = (high << 8) + low;
1375 if (((ring_offset + xfer_count) & 0xff) == (addr & 0xff))
1377 } while (--tries > 0);
1379 printk(KERN_NOTICE "%s: RX transfer address mismatch,"
1380 "%#4.4x (expected) vs. %#4.4x (actual).\n",
1381 dev->name, ring_offset + xfer_count, addr);
1384 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
1385 ei_status.dmaing &= ~0x01;
1386 } /* dma_block_input */
1388 /*====================================================================*/
1390 static void dma_block_output(struct net_device *dev, int count,
1391 const u_char *buf, const int start_page)
1393 ioaddr_t nic_base = dev->base_addr;
1394 pcnet_dev_t *info = PRIV(dev);
1402 printk(KERN_DEBUG "%s: [bo=%d]\n", dev->name, count);
1405 /* Round the count up for word writes. Do we need to do this?
1406 What effect will an odd byte count have on the 8390?
1407 I should check someday. */
1410 if (ei_status.dmaing) {
1411 printk(KERN_NOTICE "%s: DMAing conflict in dma_block_output."
1412 "[DMAstat:%1x][irqlock:%1x]\n",
1413 dev->name, ei_status.dmaing, ei_status.irqlock);
1416 ei_status.dmaing |= 0x01;
1417 /* We should already be in page 0, but to be safe... */
1418 outb_p(E8390_PAGE0+E8390_START+E8390_NODMA, nic_base+PCNET_CMD);
1424 outb_p(ENISR_RDC, nic_base + EN0_ISR);
1426 /* Now the normal output. */
1427 outb_p(count & 0xff, nic_base + EN0_RCNTLO);
1428 outb_p(count >> 8, nic_base + EN0_RCNTHI);
1429 outb_p(0x00, nic_base + EN0_RSARLO);
1430 outb_p(start_page, nic_base + EN0_RSARHI);
1432 outb_p(E8390_RWRITE+E8390_START, nic_base + PCNET_CMD);
1433 outsw(nic_base + PCNET_DATAPORT, buf, count>>1);
1435 dma_start = jiffies;
1438 /* This was for the ALPHA version only, but enough people have
1439 encountering problems that it is still here. */
1440 if (ei_debug > 4) { /* DMA termination address check... */
1441 int addr, tries = 20;
1443 int high = inb_p(nic_base + EN0_RSARHI);
1444 int low = inb_p(nic_base + EN0_RSARLO);
1445 addr = (high << 8) + low;
1446 if ((start_page << 8) + count == addr)
1448 } while (--tries > 0);
1450 printk(KERN_NOTICE "%s: Tx packet transfer address mismatch,"
1451 "%#4.4x (expected) vs. %#4.4x (actual).\n",
1452 dev->name, (start_page << 8) + count, addr);
1459 while ((inb_p(nic_base + EN0_ISR) & ENISR_RDC) == 0)
1460 if (time_after(jiffies, dma_start + PCNET_RDC_TIMEOUT)) {
1461 printk(KERN_NOTICE "%s: timeout waiting for Tx RDC.\n",
1463 pcnet_reset_8390(dev);
1464 NS8390_init(dev, 1);
1468 outb_p(ENISR_RDC, nic_base + EN0_ISR); /* Ack intr. */
1469 if (info->flags & DELAY_OUTPUT)
1470 udelay((long)delay_time);
1471 ei_status.dmaing &= ~0x01;
1474 /*====================================================================*/
1476 static int setup_dma_config(dev_link_t *link, int start_pg,
1479 struct net_device *dev = link->priv;
1481 ei_status.tx_start_page = start_pg;
1482 ei_status.rx_start_page = start_pg + TX_PAGES;
1483 ei_status.stop_page = stop_pg;
1485 /* set up block i/o functions */
1486 ei_status.get_8390_hdr = &dma_get_8390_hdr;
1487 ei_status.block_input = &dma_block_input;
1488 ei_status.block_output = &dma_block_output;
1493 /*====================================================================*/
1495 static void copyin(u_char *dest, u_char *src, int c)
1497 u_short *d = (u_short *)dest, *s = (u_short *)src;
1502 odd = (c & 1); c >>= 1;
1505 do { *d++ = __raw_readw(s++); } while (--c);
1507 /* get last byte by fetching a word and masking */
1509 *((u_char *)d) = readw(s) & 0xff;
1512 static void copyout(u_char *dest, const u_char *src, int c)
1514 u_short *d = (u_short *)dest, *s = (u_short *)src;
1519 odd = (c & 1); c >>= 1;
1522 do { __raw_writew(*s++, d++); } while (--c);
1524 /* copy last byte doing a read-modify-write */
1526 writew((readw(d) & 0xff00) | *(u_char *)s, d);
1529 /*====================================================================*/
1531 static void shmem_get_8390_hdr(struct net_device *dev,
1532 struct e8390_pkt_hdr *hdr,
1535 void *xfer_start = (void *)(ei_status.rmem_start + (ring_page << 8)
1536 - (ei_status.rx_start_page << 8));
1538 copyin((void *)hdr, xfer_start, sizeof(struct e8390_pkt_hdr));
1539 /* Fix for big endian systems */
1540 hdr->count = le16_to_cpu(hdr->count);
1543 /*====================================================================*/
1545 static void shmem_block_input(struct net_device *dev, int count,
1546 struct sk_buff *skb, int ring_offset)
1548 void *xfer_start = (void *)(ei_status.rmem_start + ring_offset
1549 - (ei_status.rx_start_page << 8));
1550 char *buf = skb->data;
1552 if (xfer_start + count > (void *)ei_status.rmem_end) {
1553 /* We must wrap the input move. */
1554 int semi_count = (void*)ei_status.rmem_end - xfer_start;
1555 copyin(buf, xfer_start, semi_count);
1557 ring_offset = ei_status.rx_start_page << 8;
1558 xfer_start = (void *)ei_status.rmem_start;
1559 count -= semi_count;
1561 copyin(buf, xfer_start, count);
1564 /*====================================================================*/
1566 static void shmem_block_output(struct net_device *dev, int count,
1567 const u_char *buf, const int start_page)
1569 void *shmem = (void *)dev->mem_start + (start_page << 8);
1570 shmem -= ei_status.tx_start_page << 8;
1571 copyout(shmem, buf, count);
1574 /*====================================================================*/
1576 static int setup_shmem_window(dev_link_t *link, int start_pg,
1577 int stop_pg, int cm_offset)
1579 struct net_device *dev = link->priv;
1580 pcnet_dev_t *info = PRIV(dev);
1583 int i, window_size, offset, last_ret, last_fn;
1585 window_size = (stop_pg - start_pg) << 8;
1586 if (window_size > 32 * 1024)
1587 window_size = 32 * 1024;
1589 /* Make sure it's a power of two. */
1590 while ((window_size & (window_size - 1)) != 0)
1591 window_size += window_size & ~(window_size - 1);
1593 /* Allocate a memory window */
1594 req.Attributes = WIN_DATA_WIDTH_16|WIN_MEMORY_TYPE_CM|WIN_ENABLE;
1595 req.Attributes |= WIN_USE_WAIT;
1596 req.Base = 0; req.Size = window_size;
1597 req.AccessSpeed = mem_speed;
1598 CS_CHECK(RequestWindow, pcmcia_request_window(&link->handle, &req, &link->win));
1600 mem.CardOffset = (start_pg << 8) + cm_offset;
1601 offset = mem.CardOffset % window_size;
1602 mem.CardOffset -= offset;
1604 CS_CHECK(MapMemPage, pcmcia_map_mem_page(link->win, &mem));
1606 /* Try scribbling on the buffer */
1607 info->base = ioremap(req.Base, window_size);
1608 for (i = 0; i < (TX_PAGES<<8); i += 2)
1609 __raw_writew((i>>1), info->base+offset+i);
1611 for (i = 0; i < (TX_PAGES<<8); i += 2)
1612 if (__raw_readw(info->base+offset+i) != (i>>1)) break;
1613 pcnet_reset_8390(dev);
1614 if (i != (TX_PAGES<<8)) {
1615 iounmap(info->base);
1616 pcmcia_release_window(link->win);
1617 info->base = NULL; link->win = NULL;
1621 dev->mem_start = (u_long)info->base + offset;
1622 ei_status.rmem_start = dev->mem_start + (TX_PAGES<<8);
1623 dev->mem_end = ei_status.rmem_end = (u_long)info->base + req.Size;
1625 ei_status.tx_start_page = start_pg;
1626 ei_status.rx_start_page = start_pg + TX_PAGES;
1627 ei_status.stop_page = start_pg + ((req.Size - offset) >> 8);
1629 /* set up block i/o functions */
1630 ei_status.get_8390_hdr = &shmem_get_8390_hdr;
1631 ei_status.block_input = &shmem_block_input;
1632 ei_status.block_output = &shmem_block_output;
1634 info->flags |= USE_SHMEM;
1638 cs_error(link->handle, last_fn, last_ret);
1643 /*====================================================================*/
1645 static struct pcmcia_driver pcnet_driver = {
1649 .attach = pcnet_attach,
1650 .detach = pcnet_detach,
1651 .owner = THIS_MODULE,
1654 static int __init init_pcnet_cs(void)
1656 return pcmcia_register_driver(&pcnet_driver);
1659 static void __exit exit_pcnet_cs(void)
1661 DEBUG(0, "pcnet_cs: unloading\n");
1662 pcmcia_unregister_driver(&pcnet_driver);
1663 while (dev_list != NULL)
1664 pcnet_detach(dev_list);
1667 module_init(init_pcnet_cs);
1668 module_exit(exit_pcnet_cs);