File: /usr/src/linux/drivers/net/tokenring/tmspci.c
1 /*
2 * tmspci.c: A generic network driver for TMS380-based PCI token ring cards.
3 *
4 * Written 1999 by Adam Fritzler
5 *
6 * This software may be used and distributed according to the terms
7 * of the GNU General Public License, incorporated herein by reference.
8 *
9 * This driver module supports the following cards:
10 * - SysKonnect TR4/16(+) PCI (SK-4590)
11 * - SysKonnect TR4/16 PCI (SK-4591)
12 * - Compaq TR 4/16 PCI
13 * - Thomas-Conrad TC4048 4/16 PCI
14 * - 3Com 3C339 Token Link Velocity
15 *
16 * Maintainer(s):
17 * AF Adam Fritzler mid@auk.cx
18 *
19 * Modification History:
20 * 30-Dec-99 AF Split off from the tms380tr driver.
21 * 22-Jan-00 AF Updated to use indirect read/writes
22 * 23-Nov-00 JG New PCI API, cleanups
23 *
24 * TODO:
25 * 1. See if we can use MMIO instead of port accesses
26 *
27 */
28
29 #include <linux/module.h>
30 #include <linux/kernel.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/pci.h>
34 #include <linux/init.h>
35
36 #include <asm/system.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39
40 #include <linux/netdevice.h>
41 #include <linux/trdevice.h>
42 #include "tms380tr.h"
43
44 static char version[] __initdata =
45 "tmspci.c: v1.02 23/11/2000 by Adam Fritzler\n";
46
47 #define TMS_PCI_IO_EXTENT 32
48
49 struct card_info {
50 unsigned char nselout[2]; /* NSELOUT vals for 4mb([0]) and 16mb([1]) */
51 char *name;
52 };
53
54 static struct card_info card_info_table[] = {
55 { {0x03, 0x01}, "Compaq 4/16 TR PCI"},
56 { {0x03, 0x01}, "SK NET TR 4/16 PCI"},
57 { {0x03, 0x01}, "Thomas-Conrad TC4048 PCI 4/16"},
58 { {0x03, 0x01}, "3Com Token Link Velocity"},
59 };
60
61 static struct pci_device_id tmspci_pci_tbl[] __initdata = {
62 { PCI_VENDOR_ID_COMPAQ, PCI_DEVICE_ID_COMPAQ_TOKENRING, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
63 { PCI_VENDOR_ID_SYSKONNECT, PCI_DEVICE_ID_SYSKONNECT_TR, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
64 { PCI_VENDOR_ID_TCONRAD, PCI_DEVICE_ID_TCONRAD_TOKENRING, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
65 { PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C339, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
66 { } /* Terminating entry */
67 };
68 MODULE_DEVICE_TABLE(pci, tmspci_pci_tbl);
69
70 MODULE_LICENSE("GPL");
71
72 static void tms_pci_read_eeprom(struct net_device *dev);
73 static unsigned short tms_pci_setnselout_pins(struct net_device *dev);
74
75 static unsigned short tms_pci_sifreadb(struct net_device *dev, unsigned short reg)
76 {
77 return inb(dev->base_addr + reg);
78 }
79
80 static unsigned short tms_pci_sifreadw(struct net_device *dev, unsigned short reg)
81 {
82 return inw(dev->base_addr + reg);
83 }
84
85 static void tms_pci_sifwriteb(struct net_device *dev, unsigned short val, unsigned short reg)
86 {
87 outb(val, dev->base_addr + reg);
88 }
89
90 static void tms_pci_sifwritew(struct net_device *dev, unsigned short val, unsigned short reg)
91 {
92 outw(val, dev->base_addr + reg);
93 }
94
95 static int __init tms_pci_attach(struct pci_dev *pdev, const struct pci_device_id *ent)
96 {
97 static int versionprinted;
98 struct net_device *dev;
99 struct net_local *tp;
100 int i, ret;
101 unsigned int pci_irq_line;
102 unsigned long pci_ioaddr;
103 struct card_info *cardinfo = &card_info_table[ent->driver_data];
104
105 if (versionprinted++ == 0)
106 printk("%s", version);
107
108 if (pci_enable_device(pdev))
109 return -EIO;
110
111 /* Remove I/O space marker in bit 0. */
112 pci_irq_line = pdev->irq;
113 pci_ioaddr = pci_resource_start (pdev, 0);
114
115 /* At this point we have found a valid card. */
116 dev = init_trdev(NULL, 0);
117 if (!dev)
118 return -ENOMEM;
119 SET_MODULE_OWNER(dev);
120
121 if (!request_region(pci_ioaddr, TMS_PCI_IO_EXTENT, dev->name)) {
122 ret = -EBUSY;
123 goto err_out_trdev;
124 }
125
126 ret = request_irq(pdev->irq, tms380tr_interrupt, SA_SHIRQ,
127 dev->name, dev);
128 if (ret)
129 goto err_out_region;
130
131 dev->base_addr = pci_ioaddr;
132 dev->irq = pci_irq_line;
133 dev->dma = 0;
134
135 printk("%s: %s\n", dev->name, cardinfo->name);
136 printk("%s: IO: %#4lx IRQ: %d\n",
137 dev->name, dev->base_addr, dev->irq);
138
139 tms_pci_read_eeprom(dev);
140
141 printk("%s: Ring Station Address: ", dev->name);
142 printk("%2.2x", dev->dev_addr[0]);
143 for (i = 1; i < 6; i++)
144 printk(":%2.2x", dev->dev_addr[i]);
145 printk("\n");
146
147 ret = tmsdev_init(dev, PCI_MAX_ADDRESS, pdev);
148 if (ret) {
149 printk("%s: unable to get memory for dev->priv.\n", dev->name);
150 goto err_out_irq;
151 }
152
153 tp = dev->priv;
154 tp->setnselout = tms_pci_setnselout_pins;
155
156 tp->sifreadb = tms_pci_sifreadb;
157 tp->sifreadw = tms_pci_sifreadw;
158 tp->sifwriteb = tms_pci_sifwriteb;
159 tp->sifwritew = tms_pci_sifwritew;
160
161 memcpy(tp->ProductID, cardinfo->name, PROD_ID_SIZE + 1);
162
163 tp->tmspriv = cardinfo;
164
165 dev->open = tms380tr_open;
166 dev->stop = tms380tr_close;
167
168 ret = register_trdev(dev);
169 if (ret)
170 goto err_out_tmsdev;
171
172 pci_set_drvdata(pdev, dev);
173 return 0;
174
175 err_out_tmsdev:
176 tmsdev_term(dev);
177 err_out_irq:
178 free_irq(pdev->irq, dev);
179 err_out_region:
180 release_region(pci_ioaddr, TMS_PCI_IO_EXTENT);
181 err_out_trdev:
182 unregister_netdev(dev);
183 kfree(dev);
184 return ret;
185 }
186
187 /*
188 * Reads MAC address from adapter RAM, which should've read it from
189 * the onboard ROM.
190 *
191 * Calling this on a board that does not support it can be a very
192 * dangerous thing. The Madge board, for instance, will lock your
193 * machine hard when this is called. Luckily, its supported in a
194 * seperate driver. --ASF
195 */
196 static void tms_pci_read_eeprom(struct net_device *dev)
197 {
198 int i;
199
200 /* Address: 0000:0000 */
201 tms_pci_sifwritew(dev, 0, SIFADX);
202 tms_pci_sifwritew(dev, 0, SIFADR);
203
204 /* Read six byte MAC address data */
205 dev->addr_len = 6;
206 for(i = 0; i < 6; i++)
207 dev->dev_addr[i] = tms_pci_sifreadw(dev, SIFINC) >> 8;
208 }
209
210 static unsigned short tms_pci_setnselout_pins(struct net_device *dev)
211 {
212 unsigned short val = 0;
213 struct net_local *tp = dev->priv;
214 struct card_info *cardinfo = tp->tmspriv;
215
216 if(tp->DataRate == SPEED_4)
217 val |= cardinfo->nselout[0]; /* Set 4Mbps */
218 else
219 val |= cardinfo->nselout[1]; /* Set 16Mbps */
220 return val;
221 }
222
223 static void __exit tms_pci_detach (struct pci_dev *pdev)
224 {
225 struct net_device *dev = pci_get_drvdata(pdev);
226
227 if (!dev)
228 BUG();
229 unregister_netdev(dev);
230 release_region(dev->base_addr, TMS_PCI_IO_EXTENT);
231 free_irq(dev->irq, dev);
232 tmsdev_term(dev);
233 kfree(dev);
234 pci_set_drvdata(pdev, NULL);
235 }
236
237 static struct pci_driver tms_pci_driver = {
238 name: "tmspci",
239 id_table: tmspci_pci_tbl,
240 probe: tms_pci_attach,
241 remove: tms_pci_detach,
242 };
243
244 static int __init tms_pci_init (void)
245 {
246 int rc = pci_register_driver (&tms_pci_driver);
247 if (rc < 0)
248 return rc;
249 if (rc == 0) {
250 pci_unregister_driver (&tms_pci_driver);
251 return -ENODEV;
252 }
253 return 0;
254 }
255
256 static void __exit tms_pci_rmmod (void)
257 {
258 pci_unregister_driver (&tms_pci_driver);
259 }
260
261 module_init(tms_pci_init);
262 module_exit(tms_pci_rmmod);
263
264
265 /*
266 * Local variables:
267 * compile-command: "gcc -DMODVERSIONS -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c tmspci.c"
268 * alt-compile-command: "gcc -DMODULE -D__KERNEL__ -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer -I/usr/src/linux/drivers/net/tokenring/ -c tmspci.c"
269 * c-set-style "K&R"
270 * c-indent-level: 8
271 * c-basic-offset: 8
272 * tab-width: 8
273 * End:
274 */
275