2 * abyss.c: Network driver for the Madge Smart 16/4 PCI Mk2 token ring card.
4 * Written 1999-2000 by Adam Fritzler
6 * This software may be used and distributed according to the terms
7 * of the GNU General Public License, incorporated herein by reference.
9 * This driver module supports the following cards:
10 * - Madge Smart 16/4 PCI Mk2
15 * Modification History:
16 * 30-Dec-99 AF Split off from the tms380tr driver.
17 * 22-Jan-00 AF Updated to use indirect read/writes
18 * 23-Nov-00 JG New PCI API, cleanups
22 * 1. See if we can use MMIO instead of inb/outb/inw/outw
23 * 2. Add support for Mk1 (has AT24 attached to the PCI
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/errno.h>
31 #include <linux/pci.h>
32 #include <linux/init.h>
33 #include <linux/netdevice.h>
34 #include <linux/trdevice.h>
40 #include "abyss.h" /* Madge-specific constants */
42 static char version
[] __devinitdata
=
43 "abyss.c: v1.02 23/11/2000 by Adam Fritzler\n";
45 #define ABYSS_IO_EXTENT 64
47 static DEFINE_PCI_DEVICE_TABLE(abyss_pci_tbl
) = {
48 { PCI_VENDOR_ID_MADGE
, PCI_DEVICE_ID_MADGE_MK2
,
49 PCI_ANY_ID
, PCI_ANY_ID
, PCI_CLASS_NETWORK_TOKEN_RING
<< 8, 0x00ffffff, },
50 { } /* Terminating entry */
52 MODULE_DEVICE_TABLE(pci
, abyss_pci_tbl
);
54 MODULE_LICENSE("GPL");
56 static int abyss_open(struct net_device
*dev
);
57 static int abyss_close(struct net_device
*dev
);
58 static void abyss_enable(struct net_device
*dev
);
59 static int abyss_chipset_init(struct net_device
*dev
);
60 static void abyss_read_eeprom(struct net_device
*dev
);
61 static unsigned short abyss_setnselout_pins(struct net_device
*dev
);
63 static void at24_writedatabyte(unsigned long regaddr
, unsigned char byte
);
64 static int at24_sendfullcmd(unsigned long regaddr
, unsigned char cmd
, unsigned char addr
);
65 static int at24_sendcmd(unsigned long regaddr
, unsigned char cmd
);
66 static unsigned char at24_readdatabit(unsigned long regaddr
);
67 static unsigned char at24_readdatabyte(unsigned long regaddr
);
68 static int at24_waitforack(unsigned long regaddr
);
69 static int at24_waitfornack(unsigned long regaddr
);
70 static void at24_setlines(unsigned long regaddr
, unsigned char clock
, unsigned char data
);
71 static void at24_start(unsigned long regaddr
);
72 static unsigned char at24_readb(unsigned long regaddr
, unsigned char addr
);
74 static unsigned short abyss_sifreadb(struct net_device
*dev
, unsigned short reg
)
76 return inb(dev
->base_addr
+ reg
);
79 static unsigned short abyss_sifreadw(struct net_device
*dev
, unsigned short reg
)
81 return inw(dev
->base_addr
+ reg
);
84 static void abyss_sifwriteb(struct net_device
*dev
, unsigned short val
, unsigned short reg
)
86 outb(val
, dev
->base_addr
+ reg
);
89 static void abyss_sifwritew(struct net_device
*dev
, unsigned short val
, unsigned short reg
)
91 outw(val
, dev
->base_addr
+ reg
);
94 static struct net_device_ops abyss_netdev_ops
;
96 static int __devinit
abyss_attach(struct pci_dev
*pdev
, const struct pci_device_id
*ent
)
98 static int versionprinted
;
99 struct net_device
*dev
;
100 struct net_local
*tp
;
101 int ret
, pci_irq_line
;
102 unsigned long pci_ioaddr
;
104 if (versionprinted
++ == 0)
105 printk("%s", version
);
107 if (pci_enable_device(pdev
))
110 /* Remove I/O space marker in bit 0. */
111 pci_irq_line
= pdev
->irq
;
112 pci_ioaddr
= pci_resource_start (pdev
, 0);
114 /* At this point we have found a valid card. */
116 dev
= alloc_trdev(sizeof(struct net_local
));
120 if (!request_region(pci_ioaddr
, ABYSS_IO_EXTENT
, dev
->name
)) {
125 ret
= request_irq(pdev
->irq
, tms380tr_interrupt
, IRQF_SHARED
,
130 dev
->base_addr
= pci_ioaddr
;
131 dev
->irq
= pci_irq_line
;
133 printk("%s: Madge Smart 16/4 PCI Mk2 (Abyss)\n", dev
->name
);
134 printk("%s: IO: %#4lx IRQ: %d\n",
135 dev
->name
, pci_ioaddr
, dev
->irq
);
137 * The TMS SIF registers lay 0x10 above the card base address.
139 dev
->base_addr
+= 0x10;
141 ret
= tmsdev_init(dev
, &pdev
->dev
);
143 printk("%s: unable to get memory for dev->priv.\n",
148 abyss_read_eeprom(dev
);
150 printk("%s: Ring Station Address: %pM\n", dev
->name
, dev
->dev_addr
);
152 tp
= netdev_priv(dev
);
153 tp
->setnselout
= abyss_setnselout_pins
;
154 tp
->sifreadb
= abyss_sifreadb
;
155 tp
->sifreadw
= abyss_sifreadw
;
156 tp
->sifwriteb
= abyss_sifwriteb
;
157 tp
->sifwritew
= abyss_sifwritew
;
159 memcpy(tp
->ProductID
, "Madge PCI 16/4 Mk2", PROD_ID_SIZE
+ 1);
161 dev
->netdev_ops
= &abyss_netdev_ops
;
163 pci_set_drvdata(pdev
, dev
);
164 SET_NETDEV_DEV(dev
, &pdev
->dev
);
166 ret
= register_netdev(dev
);
172 pci_set_drvdata(pdev
, NULL
);
175 free_irq(pdev
->irq
, dev
);
177 release_region(pci_ioaddr
, ABYSS_IO_EXTENT
);
183 static unsigned short abyss_setnselout_pins(struct net_device
*dev
)
185 unsigned short val
= 0;
186 struct net_local
*tp
= netdev_priv(dev
);
188 if(tp
->DataRate
== SPEED_4
)
189 val
|= 0x01; /* Set 4Mbps */
191 val
|= 0x00; /* Set 16Mbps */
197 * The following Madge boards should use this code:
198 * - Smart 16/4 PCI Mk2 (Abyss)
199 * - Smart 16/4 PCI Mk1 (PCI T)
200 * - Smart 16/4 Client Plus PnP (Big Apple)
201 * - Smart 16/4 Cardbus Mk2
203 * These access an Atmel AT24 SEEPROM using their glue chip registers.
206 static void at24_writedatabyte(unsigned long regaddr
, unsigned char byte
)
210 for (i
= 0; i
< 8; i
++) {
211 at24_setlines(regaddr
, 0, (byte
>> (7-i
))&0x01);
212 at24_setlines(regaddr
, 1, (byte
>> (7-i
))&0x01);
213 at24_setlines(regaddr
, 0, (byte
>> (7-i
))&0x01);
217 static int at24_sendfullcmd(unsigned long regaddr
, unsigned char cmd
, unsigned char addr
)
219 if (at24_sendcmd(regaddr
, cmd
)) {
220 at24_writedatabyte(regaddr
, addr
);
221 return at24_waitforack(regaddr
);
226 static int at24_sendcmd(unsigned long regaddr
, unsigned char cmd
)
230 for (i
= 0; i
< 10; i
++) {
232 at24_writedatabyte(regaddr
, cmd
);
233 if (at24_waitforack(regaddr
))
239 static unsigned char at24_readdatabit(unsigned long regaddr
)
243 at24_setlines(regaddr
, 0, 1);
244 at24_setlines(regaddr
, 1, 1);
245 val
= (inb(regaddr
) & AT24_DATA
)?1:0;
246 at24_setlines(regaddr
, 1, 1);
247 at24_setlines(regaddr
, 0, 1);
251 static unsigned char at24_readdatabyte(unsigned long regaddr
)
253 unsigned char data
= 0;
256 for (i
= 0; i
< 8; i
++) {
258 data
|= at24_readdatabit(regaddr
);
264 static int at24_waitforack(unsigned long regaddr
)
268 for (i
= 0; i
< 10; i
++) {
269 if ((at24_readdatabit(regaddr
) & 0x01) == 0x00)
275 static int at24_waitfornack(unsigned long regaddr
)
278 for (i
= 0; i
< 10; i
++) {
279 if ((at24_readdatabit(regaddr
) & 0x01) == 0x01)
285 static void at24_setlines(unsigned long regaddr
, unsigned char clock
, unsigned char data
)
287 unsigned char val
= AT24_ENABLE
;
294 tms380tr_wait(20); /* Very necessary. */
297 static void at24_start(unsigned long regaddr
)
299 at24_setlines(regaddr
, 0, 1);
300 at24_setlines(regaddr
, 1, 1);
301 at24_setlines(regaddr
, 1, 0);
302 at24_setlines(regaddr
, 0, 1);
305 static unsigned char at24_readb(unsigned long regaddr
, unsigned char addr
)
307 unsigned char data
= 0xff;
309 if (at24_sendfullcmd(regaddr
, AT24_WRITE
, addr
)) {
310 if (at24_sendcmd(regaddr
, AT24_READ
)) {
311 data
= at24_readdatabyte(regaddr
);
312 if (!at24_waitfornack(regaddr
))
321 * Enable basic functions of the Madge chipset needed
322 * for initialization.
324 static void abyss_enable(struct net_device
*dev
)
326 unsigned char reset_reg
;
327 unsigned long ioaddr
;
329 ioaddr
= dev
->base_addr
;
330 reset_reg
= inb(ioaddr
+ PCIBM2_RESET_REG
);
331 reset_reg
|= PCIBM2_RESET_REG_CHIP_NRES
;
332 outb(reset_reg
, ioaddr
+ PCIBM2_RESET_REG
);
337 * Enable the functions of the Madge chipset needed for
338 * full working order.
340 static int abyss_chipset_init(struct net_device
*dev
)
342 unsigned char reset_reg
;
343 unsigned long ioaddr
;
345 ioaddr
= dev
->base_addr
;
347 reset_reg
= inb(ioaddr
+ PCIBM2_RESET_REG
);
349 reset_reg
|= PCIBM2_RESET_REG_CHIP_NRES
;
350 outb(reset_reg
, ioaddr
+ PCIBM2_RESET_REG
);
352 reset_reg
&= ~(PCIBM2_RESET_REG_CHIP_NRES
|
353 PCIBM2_RESET_REG_FIFO_NRES
|
354 PCIBM2_RESET_REG_SIF_NRES
);
355 outb(reset_reg
, ioaddr
+ PCIBM2_RESET_REG
);
359 reset_reg
|= PCIBM2_RESET_REG_CHIP_NRES
;
360 outb(reset_reg
, ioaddr
+ PCIBM2_RESET_REG
);
362 reset_reg
|= PCIBM2_RESET_REG_SIF_NRES
;
363 outb(reset_reg
, ioaddr
+ PCIBM2_RESET_REG
);
365 reset_reg
|= PCIBM2_RESET_REG_FIFO_NRES
;
366 outb(reset_reg
, ioaddr
+ PCIBM2_RESET_REG
);
368 outb(PCIBM2_INT_CONTROL_REG_SINTEN
|
369 PCIBM2_INT_CONTROL_REG_PCI_ERR_ENABLE
,
370 ioaddr
+ PCIBM2_INT_CONTROL_REG
);
372 outb(30, ioaddr
+ PCIBM2_FIFO_THRESHOLD
);
377 static inline void abyss_chipset_close(struct net_device
*dev
)
379 unsigned long ioaddr
;
381 ioaddr
= dev
->base_addr
;
382 outb(0, ioaddr
+ PCIBM2_RESET_REG
);
386 * Read configuration data from the AT24 SEEPROM on Madge cards.
389 static void abyss_read_eeprom(struct net_device
*dev
)
391 struct net_local
*tp
;
392 unsigned long ioaddr
;
396 tp
= netdev_priv(dev
);
397 ioaddr
= dev
->base_addr
;
399 /* Must enable glue chip first */
402 val
= at24_readb(ioaddr
+ PCIBM2_SEEPROM_REG
,
403 PCIBM2_SEEPROM_RING_SPEED
);
404 tp
->DataRate
= val
?SPEED_4
:SPEED_16
; /* set open speed */
405 printk("%s: SEEPROM: ring speed: %dMb/sec\n", dev
->name
, tp
->DataRate
);
407 val
= at24_readb(ioaddr
+ PCIBM2_SEEPROM_REG
,
408 PCIBM2_SEEPROM_RAM_SIZE
) * 128;
409 printk("%s: SEEPROM: adapter RAM: %dkb\n", dev
->name
, val
);
412 for (i
= 0; i
< 6; i
++)
413 dev
->dev_addr
[i
] = at24_readb(ioaddr
+ PCIBM2_SEEPROM_REG
,
414 PCIBM2_SEEPROM_BIA
+i
);
417 static int abyss_open(struct net_device
*dev
)
419 abyss_chipset_init(dev
);
424 static int abyss_close(struct net_device
*dev
)
427 abyss_chipset_close(dev
);
431 static void __devexit
abyss_detach (struct pci_dev
*pdev
)
433 struct net_device
*dev
= pci_get_drvdata(pdev
);
436 unregister_netdev(dev
);
437 release_region(dev
->base_addr
-0x10, ABYSS_IO_EXTENT
);
438 free_irq(dev
->irq
, dev
);
441 pci_set_drvdata(pdev
, NULL
);
444 static struct pci_driver abyss_driver
= {
446 .id_table
= abyss_pci_tbl
,
447 .probe
= abyss_attach
,
448 .remove
= __devexit_p(abyss_detach
),
451 static int __init
abyss_init (void)
453 abyss_netdev_ops
= tms380tr_netdev_ops
;
455 abyss_netdev_ops
.ndo_open
= abyss_open
;
456 abyss_netdev_ops
.ndo_stop
= abyss_close
;
458 return pci_register_driver(&abyss_driver
);
461 static void __exit
abyss_rmmod (void)
463 pci_unregister_driver (&abyss_driver
);
466 module_init(abyss_init
);
467 module_exit(abyss_rmmod
);