- Alan Cox: synch. PA-RISC arch and bitops cleanups
[davej-history.git] / drivers / net / tokenring / abyss.c
bloba020b21ddc71362c119aec80c52e76a591acb078
1 /*
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 Public License, incorporated herein by reference.
9 * This driver module supports the following cards:
10 * - Madge Smart 16/4 PCI Mk2
12 * Maintainer(s):
13 * AF Adam Fritzler mid@auk.cx
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
21 * TODO:
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
24 * config registers)
28 #include <linux/module.h>
29 #include <linux/kernel.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/pci.h>
33 #include <linux/init.h>
35 #include <asm/system.h>
36 #include <asm/io.h>
37 #include <asm/irq.h>
39 #include <linux/netdevice.h>
40 #include <linux/trdevice.h>
41 #include "tms380tr.h"
42 #include "abyss.h" /* Madge-specific constants */
44 static char version[] __initdata =
45 "abyss.c: v1.02 23/11/2000 by Adam Fritzler\n";
47 #define ABYSS_IO_EXTENT 64
49 static struct pci_device_id abyss_pci_tbl[] __initdata = {
50 { PCI_VENDOR_ID_MADGE, PCI_DEVICE_ID_MADGE_MK2,
51 PCI_ANY_ID, PCI_ANY_ID, PCI_CLASS_NETWORK_TOKEN_RING << 8, 0x00ffffff, },
52 { } /* Terminating entry */
54 MODULE_DEVICE_TABLE(pci, abyss_pci_tbl);
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 int __init abyss_attach(struct pci_dev *pdev, const struct pci_device_id *ent)
96 static int versionprinted;
97 struct net_device *dev;
98 struct net_local *tp;
99 int i, ret, pci_irq_line;
100 unsigned long pci_ioaddr;
102 if (versionprinted++ == 0)
103 printk("%s", version);
105 if (pci_enable_device(pdev))
106 return -EIO;
108 /* Remove I/O space marker in bit 0. */
109 pci_irq_line = pdev->irq;
110 pci_ioaddr = pci_resource_start (pdev, 0);
112 /* At this point we have found a valid card. */
114 dev = init_trdev(NULL, 0);
115 if (!dev)
116 return -ENOMEM;
117 SET_MODULE_OWNER(dev);
119 if (!request_region(pci_ioaddr, ABYSS_IO_EXTENT, dev->name)) {
120 ret = -EBUSY;
121 goto err_out_trdev;
124 ret = request_irq(pdev->irq, tms380tr_interrupt, SA_SHIRQ,
125 dev->name, dev);
126 if (ret)
127 goto err_out_region;
129 dev->base_addr = pci_ioaddr;
130 dev->irq = pci_irq_line;
132 printk("%s: Madge Smart 16/4 PCI Mk2 (Abyss)\n", dev->name);
133 printk("%s: IO: %#4lx IRQ: %d\n",
134 dev->name, pci_ioaddr, dev->irq);
136 * The TMS SIF registers lay 0x10 above the card base address.
138 dev->base_addr += 0x10;
140 ret = tmsdev_init(dev);
141 if (ret) {
142 printk("%s: unable to get memory for dev->priv.\n",
143 dev->name);
144 goto err_out_irq;
147 abyss_read_eeprom(dev);
149 printk("%s: Ring Station Address: ", dev->name);
150 printk("%2.2x", dev->dev_addr[0]);
151 for (i = 1; i < 6; i++)
152 printk(":%2.2x", dev->dev_addr[i]);
153 printk("\n");
155 tp = dev->priv;
156 tp->dmalimit = 0; /* XXX: should be the max PCI32 DMA max */
157 tp->setnselout = abyss_setnselout_pins;
158 tp->sifreadb = abyss_sifreadb;
159 tp->sifreadw = abyss_sifreadw;
160 tp->sifwriteb = abyss_sifwriteb;
161 tp->sifwritew = abyss_sifwritew;
163 memcpy(tp->ProductID, "Madge PCI 16/4 Mk2", PROD_ID_SIZE + 1);
165 dev->open = abyss_open;
166 dev->stop = abyss_close;
168 ret = register_trdev(dev);
169 if (ret)
170 goto err_out_tmsdev;
172 pci_set_drvdata(pdev, dev);
173 return 0;
175 err_out_tmsdev:
176 kfree(dev->priv);
177 err_out_irq:
178 free_irq(pdev->irq, dev);
179 err_out_region:
180 release_region(pci_ioaddr, ABYSS_IO_EXTENT);
181 err_out_trdev:
182 unregister_netdev(dev);
183 kfree(dev);
184 return ret;
187 static unsigned short abyss_setnselout_pins(struct net_device *dev)
189 unsigned short val = 0;
190 struct net_local *tp = (struct net_local *)dev->priv;
192 if(tp->DataRate == SPEED_4)
193 val |= 0x01; /* Set 4Mbps */
194 else
195 val |= 0x00; /* Set 16Mbps */
197 return val;
201 * The following Madge boards should use this code:
202 * - Smart 16/4 PCI Mk2 (Abyss)
203 * - Smart 16/4 PCI Mk1 (PCI T)
204 * - Smart 16/4 Client Plus PnP (Big Apple)
205 * - Smart 16/4 Cardbus Mk2
207 * These access an Atmel AT24 SEEPROM using their glue chip registers.
210 static void at24_writedatabyte(unsigned long regaddr, unsigned char byte)
212 int i;
214 for (i = 0; i < 8; i++) {
215 at24_setlines(regaddr, 0, (byte >> (7-i))&0x01);
216 at24_setlines(regaddr, 1, (byte >> (7-i))&0x01);
217 at24_setlines(regaddr, 0, (byte >> (7-i))&0x01);
221 static int at24_sendfullcmd(unsigned long regaddr, unsigned char cmd, unsigned char addr)
223 if (at24_sendcmd(regaddr, cmd)) {
224 at24_writedatabyte(regaddr, addr);
225 return at24_waitforack(regaddr);
227 return 0;
230 static int at24_sendcmd(unsigned long regaddr, unsigned char cmd)
232 int i;
234 for (i = 0; i < 10; i++) {
235 at24_start(regaddr);
236 at24_writedatabyte(regaddr, cmd);
237 if (at24_waitforack(regaddr))
238 return 1;
240 return 0;
243 static unsigned char at24_readdatabit(unsigned long regaddr)
245 unsigned char val;
247 at24_setlines(regaddr, 0, 1);
248 at24_setlines(regaddr, 1, 1);
249 val = (inb(regaddr) & AT24_DATA)?1:0;
250 at24_setlines(regaddr, 1, 1);
251 at24_setlines(regaddr, 0, 1);
252 return val;
255 static unsigned char at24_readdatabyte(unsigned long regaddr)
257 unsigned char data = 0;
258 int i;
260 for (i = 0; i < 8; i++) {
261 data <<= 1;
262 data |= at24_readdatabit(regaddr);
265 return data;
268 static int at24_waitforack(unsigned long regaddr)
270 int i;
272 for (i = 0; i < 10; i++) {
273 if ((at24_readdatabit(regaddr) & 0x01) == 0x00)
274 return 1;
276 return 0;
279 static int at24_waitfornack(unsigned long regaddr)
281 int i;
282 for (i = 0; i < 10; i++) {
283 if ((at24_readdatabit(regaddr) & 0x01) == 0x01)
284 return 1;
286 return 0;
289 static void at24_setlines(unsigned long regaddr, unsigned char clock, unsigned char data)
291 unsigned char val = AT24_ENABLE;
292 if (clock)
293 val |= AT24_CLOCK;
294 if (data)
295 val |= AT24_DATA;
297 outb(val, regaddr);
298 tms380tr_wait(20); /* Very necessary. */
301 static void at24_start(unsigned long regaddr)
303 at24_setlines(regaddr, 0, 1);
304 at24_setlines(regaddr, 1, 1);
305 at24_setlines(regaddr, 1, 0);
306 at24_setlines(regaddr, 0, 1);
309 static unsigned char at24_readb(unsigned long regaddr, unsigned char addr)
311 unsigned char data = 0xff;
313 if (at24_sendfullcmd(regaddr, AT24_WRITE, addr)) {
314 if (at24_sendcmd(regaddr, AT24_READ)) {
315 data = at24_readdatabyte(regaddr);
316 if (!at24_waitfornack(regaddr))
317 data = 0xff;
320 return data;
325 * Enable basic functions of the Madge chipset needed
326 * for initialization.
328 static void abyss_enable(struct net_device *dev)
330 unsigned char reset_reg;
331 unsigned long ioaddr;
333 ioaddr = dev->base_addr;
334 reset_reg = inb(ioaddr + PCIBM2_RESET_REG);
335 reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
336 outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
337 tms380tr_wait(100);
341 * Enable the functions of the Madge chipset needed for
342 * full working order.
344 static int abyss_chipset_init(struct net_device *dev)
346 unsigned char reset_reg;
347 unsigned long ioaddr;
349 ioaddr = dev->base_addr;
351 reset_reg = inb(ioaddr + PCIBM2_RESET_REG);
353 reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
354 outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
356 reset_reg &= ~(PCIBM2_RESET_REG_CHIP_NRES |
357 PCIBM2_RESET_REG_FIFO_NRES |
358 PCIBM2_RESET_REG_SIF_NRES);
359 outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
361 tms380tr_wait(100);
363 reset_reg |= PCIBM2_RESET_REG_CHIP_NRES;
364 outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
366 reset_reg |= PCIBM2_RESET_REG_SIF_NRES;
367 outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
369 reset_reg |= PCIBM2_RESET_REG_FIFO_NRES;
370 outb(reset_reg, ioaddr + PCIBM2_RESET_REG);
372 outb(PCIBM2_INT_CONTROL_REG_SINTEN |
373 PCIBM2_INT_CONTROL_REG_PCI_ERR_ENABLE,
374 ioaddr + PCIBM2_INT_CONTROL_REG);
376 outb(30, ioaddr + PCIBM2_FIFO_THRESHOLD);
378 return 0;
381 static inline void abyss_chipset_close(struct net_device *dev)
383 unsigned long ioaddr;
385 ioaddr = dev->base_addr;
386 outb(0, ioaddr + PCIBM2_RESET_REG);
390 * Read configuration data from the AT24 SEEPROM on Madge cards.
393 static void abyss_read_eeprom(struct net_device *dev)
395 struct net_local *tp;
396 unsigned long ioaddr;
397 unsigned short val;
398 int i;
400 tp = (struct net_local *)dev->priv;
401 ioaddr = dev->base_addr;
403 /* Must enable glue chip first */
404 abyss_enable(dev);
406 val = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
407 PCIBM2_SEEPROM_RING_SPEED);
408 tp->DataRate = val?SPEED_4:SPEED_16; /* set open speed */
409 printk("%s: SEEPROM: ring speed: %dMb/sec\n", dev->name, tp->DataRate);
411 val = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
412 PCIBM2_SEEPROM_RAM_SIZE) * 128;
413 printk("%s: SEEPROM: adapter RAM: %dkb\n", dev->name, val);
415 dev->addr_len = 6;
416 for (i = 0; i < 6; i++)
417 dev->dev_addr[i] = at24_readb(ioaddr + PCIBM2_SEEPROM_REG,
418 PCIBM2_SEEPROM_BIA+i);
421 static int abyss_open(struct net_device *dev)
423 abyss_chipset_init(dev);
424 tms380tr_open(dev);
425 return 0;
428 static int abyss_close(struct net_device *dev)
430 tms380tr_close(dev);
431 abyss_chipset_close(dev);
432 return 0;
435 static void __exit abyss_detach (struct pci_dev *pdev)
437 struct net_device *dev = pci_get_drvdata(pdev);
439 if (!dev)
440 BUG();
441 unregister_netdev(dev);
442 release_region(dev->base_addr-0x10, ABYSS_IO_EXTENT);
443 free_irq(dev->irq, dev);
444 kfree(dev->priv);
445 kfree(dev);
446 pci_set_drvdata(pdev, NULL);
449 static struct pci_driver abyss_driver = {
450 name: "abyss",
451 id_table: abyss_pci_tbl,
452 probe: abyss_attach,
453 remove: abyss_detach,
456 static int __init abyss_init (void)
458 int rc = pci_register_driver (&abyss_driver);
459 if (rc < 0)
460 return rc;
461 if (rc == 0) {
462 pci_unregister_driver (&abyss_driver);
463 return -ENODEV;
465 return 0;
468 static void __exit abyss_rmmod (void)
470 pci_unregister_driver (&abyss_driver);
473 module_init(abyss_init);
474 module_exit(abyss_rmmod);
478 * Local variables:
479 * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c abyss.c"
480 * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c abyss.c"
481 * c-set-style "K&R"
482 * c-indent-level: 8
483 * c-basic-offset: 8
484 * tab-width: 8
485 * End: