Linux-libre 3.4.39-gnu1
[librecmc/linux-libre.git] / drivers / net / ethernet / 8390 / smc-ultra32.c
1 /*      smc-ultra32.c: An SMC Ultra32 EISA ethernet driver for linux.
2
3 Sources:
4
5         This driver is based on (cloned from) the ISA SMC Ultra driver
6         written by Donald Becker. Modifications to support the EISA
7         version of the card by Paul Gortmaker and Leonard N. Zubkoff.
8
9         This software may be used and distributed according to the terms
10         of the GNU General Public License, incorporated herein by reference.
11
12 Theory of Operation:
13
14         The SMC Ultra32C card uses the SMC 83c790 chip which is also
15         found on the ISA SMC Ultra cards. It has a shared memory mode of
16         operation that makes it similar to the ISA version of the card.
17         The main difference is that the EISA card has 32KB of RAM, but
18         only an 8KB window into that memory. The EISA card also can be
19         set for a bus-mastering mode of operation via the ECU, but that
20         is not (and probably will never be) supported by this driver.
21         The ECU should be run to enable shared memory and to disable the
22         bus-mastering feature for use with linux.
23
24         By programming the 8390 to use only 8KB RAM, the modifications
25         to the ISA driver can be limited to the probe and initialization
26         code. This allows easy integration of EISA support into the ISA
27         driver. However, the driver development kit from SMC provided the
28         register information for sliding the 8KB window, and hence the 8390
29         is programmed to use the full 32KB RAM.
30
31         Unfortunately this required code changes outside the probe/init
32         routines, and thus we decided to separate the EISA driver from
33         the ISA one. In this way, ISA users don't end up with a larger
34         driver due to the EISA code, and EISA users don't end up with a
35         larger driver due to the ISA EtherEZ PIO code. The driver is
36         similar to the 3c503/16 driver, in that the window must be set
37         back to the 1st 8KB of space for access to the two 8390 Tx slots.
38
39         In testing, using only 8KB RAM (3 Tx / 5 Rx) didn't appear to
40         be a limiting factor, since the EISA bus could get packets off
41         the card fast enough, but having the use of lots of RAM as Rx
42         space is extra insurance if interrupt latencies become excessive.
43
44 */
45
46 static const char *version = "smc-ultra32.c: 06/97 v1.00\n";
47
48
49 #include <linux/module.h>
50 #include <linux/eisa.h>
51 #include <linux/kernel.h>
52 #include <linux/errno.h>
53 #include <linux/string.h>
54 #include <linux/init.h>
55 #include <linux/interrupt.h>
56 #include <linux/netdevice.h>
57 #include <linux/etherdevice.h>
58
59 #include <asm/io.h>
60
61 #include "8390.h"
62
63 #define DRV_NAME "smc-ultra32"
64
65 static int ultra32_probe1(struct net_device *dev, int ioaddr);
66 static int ultra32_open(struct net_device *dev);
67 static void ultra32_reset_8390(struct net_device *dev);
68 static void ultra32_get_8390_hdr(struct net_device *dev, struct e8390_pkt_hdr *hdr,
69                                  int ring_page);
70 static void ultra32_block_input(struct net_device *dev, int count,
71                                 struct sk_buff *skb, int ring_offset);
72 static void ultra32_block_output(struct net_device *dev, int count,
73                                  const unsigned char *buf,
74                                  const int start_page);
75 static int ultra32_close(struct net_device *dev);
76
77 #define ULTRA32_CMDREG  0       /* Offset to ASIC command register. */
78 #define  ULTRA32_RESET  0x80    /* Board reset, in ULTRA32_CMDREG. */
79 #define  ULTRA32_MEMENB 0x40    /* Enable the shared memory. */
80 #define ULTRA32_NIC_OFFSET 16   /* NIC register offset from the base_addr. */
81 #define ULTRA32_IO_EXTENT 32
82 #define EN0_ERWCNT              0x08    /* Early receive warning count. */
83
84 /*
85  * Defines that apply only to the Ultra32 EISA card. Note that
86  * "smc" = 10011 01101 00011 = 0x4da3, and hence !smc8010.cfg translates
87  * into an EISA ID of 0x1080A34D
88  */
89 #define ULTRA32_BASE    0xca0
90 #define ULTRA32_ID      0x1080a34d
91 #define ULTRA32_IDPORT  (-0x20) /* 0xc80 */
92 /* Config regs 1->7 from the EISA !SMC8010.CFG file. */
93 #define ULTRA32_CFG1    0x04    /* 0xca4 */
94 #define ULTRA32_CFG2    0x05    /* 0xca5 */
95 #define ULTRA32_CFG3    (-0x18) /* 0xc88 */
96 #define ULTRA32_CFG4    (-0x17) /* 0xc89 */
97 #define ULTRA32_CFG5    (-0x16) /* 0xc8a */
98 #define ULTRA32_CFG6    (-0x15) /* 0xc8b */
99 #define ULTRA32_CFG7    0x0d    /* 0xcad */
100
101 static void cleanup_card(struct net_device *dev)
102 {
103         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET;
104         /* NB: ultra32_close_card() does free_irq */
105         release_region(ioaddr, ULTRA32_IO_EXTENT);
106         iounmap(ei_status.mem);
107 }
108
109 /*      Probe for the Ultra32.  This looks like a 8013 with the station
110         address PROM at I/O ports <base>+8 to <base>+13, with a checksum
111         following.
112 */
113
114 struct net_device * __init ultra32_probe(int unit)
115 {
116         struct net_device *dev;
117         int base;
118         int irq;
119         int err = -ENODEV;
120
121         if (!EISA_bus)
122                 return ERR_PTR(-ENODEV);
123
124         dev = alloc_ei_netdev();
125
126         if (!dev)
127                 return ERR_PTR(-ENOMEM);
128
129         if (unit >= 0) {
130                 sprintf(dev->name, "eth%d", unit);
131                 netdev_boot_setup_check(dev);
132         }
133
134         irq = dev->irq;
135
136         /* EISA spec allows for up to 16 slots, but 8 is typical. */
137         for (base = 0x1000 + ULTRA32_BASE; base < 0x9000; base += 0x1000) {
138                 if (ultra32_probe1(dev, base) == 0)
139                         break;
140                 dev->irq = irq;
141         }
142         if (base >= 0x9000)
143                 goto out;
144         err = register_netdev(dev);
145         if (err)
146                 goto out1;
147         return dev;
148 out1:
149         cleanup_card(dev);
150 out:
151         free_netdev(dev);
152         return ERR_PTR(err);
153 }
154
155
156 static const struct net_device_ops ultra32_netdev_ops = {
157         .ndo_open               = ultra32_open,
158         .ndo_stop               = ultra32_close,
159         .ndo_start_xmit         = ei_start_xmit,
160         .ndo_tx_timeout         = ei_tx_timeout,
161         .ndo_get_stats          = ei_get_stats,
162         .ndo_set_rx_mode        = ei_set_multicast_list,
163         .ndo_validate_addr      = eth_validate_addr,
164         .ndo_set_mac_address    = eth_mac_addr,
165         .ndo_change_mtu         = eth_change_mtu,
166 #ifdef CONFIG_NET_POLL_CONTROLLER
167         .ndo_poll_controller    = ei_poll,
168 #endif
169 };
170
171 static int __init ultra32_probe1(struct net_device *dev, int ioaddr)
172 {
173         int i, edge, media, retval;
174         int checksum = 0;
175         const char *model_name;
176         static unsigned version_printed;
177         /* Values from various config regs. */
178         unsigned char idreg;
179         unsigned char reg4;
180         const char *ifmap[] = {"UTP No Link", "", "UTP/AUI", "UTP/BNC"};
181
182         if (!request_region(ioaddr, ULTRA32_IO_EXTENT, DRV_NAME))
183                 return -EBUSY;
184
185         if (inb(ioaddr + ULTRA32_IDPORT) == 0xff ||
186             inl(ioaddr + ULTRA32_IDPORT) != ULTRA32_ID) {
187                 retval = -ENODEV;
188                 goto out;
189         }
190
191         media = inb(ioaddr + ULTRA32_CFG7) & 0x03;
192         edge = inb(ioaddr + ULTRA32_CFG5) & 0x08;
193         printk("SMC Ultra32 in EISA Slot %d, Media: %s, %s IRQs.\n",
194                 ioaddr >> 12, ifmap[media],
195                 (edge ? "Edge Triggered" : "Level Sensitive"));
196
197         idreg = inb(ioaddr + 7);
198         reg4 = inb(ioaddr + 4) & 0x7f;
199
200         /* Check the ID nibble. */
201         if ((idreg & 0xf0) != 0x20) {                   /* SMC Ultra */
202                 retval = -ENODEV;
203                 goto out;
204         }
205
206         /* Select the station address register set. */
207         outb(reg4, ioaddr + 4);
208
209         for (i = 0; i < 8; i++)
210                 checksum += inb(ioaddr + 8 + i);
211         if ((checksum & 0xff) != 0xff) {
212                 retval = -ENODEV;
213                 goto out;
214         }
215
216         if (ei_debug  &&  version_printed++ == 0)
217                 printk(version);
218
219         model_name = "SMC Ultra32";
220
221         for (i = 0; i < 6; i++)
222                 dev->dev_addr[i] = inb(ioaddr + 8 + i);
223
224         printk("%s: %s at 0x%X, %pM",
225                dev->name, model_name, ioaddr, dev->dev_addr);
226
227         /* Switch from the station address to the alternate register set and
228            read the useful registers there. */
229         outb(0x80 | reg4, ioaddr + 4);
230
231         /* Enable FINE16 mode to avoid BIOS ROM width mismatches @ reboot. */
232         outb(0x80 | inb(ioaddr + 0x0c), ioaddr + 0x0c);
233
234         /* Reset RAM addr. */
235         outb(0x00, ioaddr + 0x0b);
236
237         /* Switch back to the station address register set so that the
238            MS-DOS driver can find the card after a warm boot. */
239         outb(reg4, ioaddr + 4);
240
241         if ((inb(ioaddr + ULTRA32_CFG5) & 0x40) == 0) {
242                 printk("\nsmc-ultra32: Card RAM is disabled!  "
243                        "Run EISA config utility.\n");
244                 retval = -ENODEV;
245                 goto out;
246         }
247         if ((inb(ioaddr + ULTRA32_CFG2) & 0x04) == 0)
248                 printk("\nsmc-ultra32: Ignoring Bus-Master enable bit.  "
249                        "Run EISA config utility.\n");
250
251         if (dev->irq < 2) {
252                 unsigned char irqmap[] = {0, 9, 3, 5, 7, 10, 11, 15};
253                 int irq = irqmap[inb(ioaddr + ULTRA32_CFG5) & 0x07];
254                 if (irq == 0) {
255                         printk(", failed to detect IRQ line.\n");
256                         retval = -EAGAIN;
257                         goto out;
258                 }
259                 dev->irq = irq;
260         }
261
262         /* The 8390 isn't at the base address, so fake the offset */
263         dev->base_addr = ioaddr + ULTRA32_NIC_OFFSET;
264
265         /* Save RAM address in the unused reg0 to avoid excess inb's. */
266         ei_status.reg0 = inb(ioaddr + ULTRA32_CFG3) & 0xfc;
267
268         dev->mem_start =  0xc0000 + ((ei_status.reg0 & 0x7c) << 11);
269
270         ei_status.name = model_name;
271         ei_status.word16 = 1;
272         ei_status.tx_start_page = 0;
273         ei_status.rx_start_page = TX_PAGES;
274         /* All Ultra32 cards have 32KB memory with an 8KB window. */
275         ei_status.stop_page = 128;
276
277         ei_status.mem = ioremap(dev->mem_start, 0x2000);
278         if (!ei_status.mem) {
279                 printk(", failed to ioremap.\n");
280                 retval = -ENOMEM;
281                 goto out;
282         }
283         dev->mem_end = dev->mem_start + 0x1fff;
284
285         printk(", IRQ %d, 32KB memory, 8KB window at 0x%lx-0x%lx.\n",
286                dev->irq, dev->mem_start, dev->mem_end);
287         ei_status.block_input = &ultra32_block_input;
288         ei_status.block_output = &ultra32_block_output;
289         ei_status.get_8390_hdr = &ultra32_get_8390_hdr;
290         ei_status.reset_8390 = &ultra32_reset_8390;
291
292         dev->netdev_ops = &ultra32_netdev_ops;
293         NS8390_init(dev, 0);
294
295         return 0;
296 out:
297         release_region(ioaddr, ULTRA32_IO_EXTENT);
298         return retval;
299 }
300
301 static int ultra32_open(struct net_device *dev)
302 {
303         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET; /* ASIC addr */
304         int irq_flags = (inb(ioaddr + ULTRA32_CFG5) & 0x08) ? 0 : IRQF_SHARED;
305         int retval;
306
307         retval = request_irq(dev->irq, ei_interrupt, irq_flags, dev->name, dev);
308         if (retval)
309                 return retval;
310
311         outb(ULTRA32_MEMENB, ioaddr); /* Enable Shared Memory. */
312         outb(0x80, ioaddr + ULTRA32_CFG6); /* Enable Interrupts. */
313         outb(0x84, ioaddr + 5); /* Enable MEM16 & Disable Bus Master. */
314         outb(0x01, ioaddr + 6); /* Enable Interrupts. */
315         /* Set the early receive warning level in window 0 high enough not
316            to receive ERW interrupts. */
317         outb_p(E8390_NODMA+E8390_PAGE0, dev->base_addr);
318         outb(0xff, dev->base_addr + EN0_ERWCNT);
319         ei_open(dev);
320         return 0;
321 }
322
323 static int ultra32_close(struct net_device *dev)
324 {
325         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET; /* CMDREG */
326
327         netif_stop_queue(dev);
328
329         if (ei_debug > 1)
330                 printk("%s: Shutting down ethercard.\n", dev->name);
331
332         outb(0x00, ioaddr + ULTRA32_CFG6); /* Disable Interrupts. */
333         outb(0x00, ioaddr + 6);         /* Disable interrupts. */
334         free_irq(dev->irq, dev);
335
336         NS8390_init(dev, 0);
337
338         return 0;
339 }
340
341 static void ultra32_reset_8390(struct net_device *dev)
342 {
343         int ioaddr = dev->base_addr - ULTRA32_NIC_OFFSET; /* ASIC base addr */
344
345         outb(ULTRA32_RESET, ioaddr);
346         if (ei_debug > 1) printk("resetting Ultra32, t=%ld...", jiffies);
347         ei_status.txing = 0;
348
349         outb(ULTRA32_MEMENB, ioaddr); /* Enable Shared Memory. */
350         outb(0x80, ioaddr + ULTRA32_CFG6); /* Enable Interrupts. */
351         outb(0x84, ioaddr + 5); /* Enable MEM16 & Disable Bus Master. */
352         outb(0x01, ioaddr + 6); /* Enable Interrupts. */
353         if (ei_debug > 1) printk("reset done\n");
354 }
355
356 /* Grab the 8390 specific header. Similar to the block_input routine, but
357    we don't need to be concerned with ring wrap as the header will be at
358    the start of a page, so we optimize accordingly. */
359
360 static void ultra32_get_8390_hdr(struct net_device *dev,
361                                  struct e8390_pkt_hdr *hdr,
362                                  int ring_page)
363 {
364         void __iomem *hdr_start = ei_status.mem + ((ring_page & 0x1f) << 8);
365         unsigned int RamReg = dev->base_addr - ULTRA32_NIC_OFFSET + ULTRA32_CFG3;
366
367         /* Select correct 8KB Window. */
368         outb(ei_status.reg0 | ((ring_page & 0x60) >> 5), RamReg);
369
370 #ifdef __BIG_ENDIAN
371         /* Officially this is what we are doing, but the readl() is faster */
372         /* unfortunately it isn't endian aware of the struct               */
373         memcpy_fromio(hdr, hdr_start, sizeof(struct e8390_pkt_hdr));
374         hdr->count = le16_to_cpu(hdr->count);
375 #else
376         ((unsigned int*)hdr)[0] = readl(hdr_start);
377 #endif
378 }
379
380 /* Block input and output are easy on shared memory ethercards, the only
381    complication is when the ring buffer wraps, or in this case, when a
382    packet spans an 8KB boundary. Note that the current 8KB segment is
383    already set by the get_8390_hdr routine. */
384
385 static void ultra32_block_input(struct net_device *dev,
386                                 int count,
387                                 struct sk_buff *skb,
388                                 int ring_offset)
389 {
390         void __iomem *xfer_start = ei_status.mem + (ring_offset & 0x1fff);
391         unsigned int RamReg = dev->base_addr - ULTRA32_NIC_OFFSET + ULTRA32_CFG3;
392
393         if ((ring_offset & ~0x1fff) != ((ring_offset + count - 1) & ~0x1fff)) {
394                 int semi_count = 8192 - (ring_offset & 0x1FFF);
395                 memcpy_fromio(skb->data, xfer_start, semi_count);
396                 count -= semi_count;
397                 if (ring_offset < 96*256) {
398                         /* Select next 8KB Window. */
399                         ring_offset += semi_count;
400                         outb(ei_status.reg0 | ((ring_offset & 0x6000) >> 13), RamReg);
401                         memcpy_fromio(skb->data + semi_count, ei_status.mem, count);
402                 } else {
403                         /* Select first 8KB Window. */
404                         outb(ei_status.reg0, RamReg);
405                         memcpy_fromio(skb->data + semi_count, ei_status.mem + TX_PAGES * 256, count);
406                 }
407         } else {
408                 memcpy_fromio(skb->data, xfer_start, count);
409         }
410 }
411
412 static void ultra32_block_output(struct net_device *dev,
413                                  int count,
414                                  const unsigned char *buf,
415                                  int start_page)
416 {
417         void __iomem *xfer_start = ei_status.mem + (start_page<<8);
418         unsigned int RamReg = dev->base_addr - ULTRA32_NIC_OFFSET + ULTRA32_CFG3;
419
420         /* Select first 8KB Window. */
421         outb(ei_status.reg0, RamReg);
422
423         memcpy_toio(xfer_start, buf, count);
424 }
425
426 #ifdef MODULE
427 #define MAX_ULTRA32_CARDS   4   /* Max number of Ultra cards per module */
428 static struct net_device *dev_ultra[MAX_ULTRA32_CARDS];
429
430 MODULE_DESCRIPTION("SMC Ultra32 EISA ethernet driver");
431 MODULE_LICENSE("GPL");
432
433 int __init init_module(void)
434 {
435         int this_dev, found = 0;
436
437         for (this_dev = 0; this_dev < MAX_ULTRA32_CARDS; this_dev++) {
438                 struct net_device *dev = ultra32_probe(-1);
439                 if (IS_ERR(dev))
440                         break;
441                 dev_ultra[found++] = dev;
442         }
443         if (found)
444                 return 0;
445         printk(KERN_WARNING "smc-ultra32.c: No SMC Ultra32 found.\n");
446         return -ENXIO;
447 }
448
449 void __exit cleanup_module(void)
450 {
451         int this_dev;
452
453         for (this_dev = 0; this_dev < MAX_ULTRA32_CARDS; this_dev++) {
454                 struct net_device *dev = dev_ultra[this_dev];
455                 if (dev) {
456                         unregister_netdev(dev);
457                         cleanup_card(dev);
458                         free_netdev(dev);
459                 }
460         }
461 }
462 #endif /* MODULE */
463