vserver 1.9.5.x5
[linux-2.6.git] / drivers / net / jazzsonic.c
1 /*
2  * sonic.c
3  *
4  * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
5  * 
6  * This driver is based on work from Andreas Busse, but most of
7  * the code is rewritten.
8  * 
9  * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
10  *
11  * A driver for the onboard Sonic ethernet controller on Mips Jazz
12  * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
13  * perhaps others, too)
14  */
15
16 #include <linux/kernel.h>
17 #include <linux/types.h>
18 #include <linux/fcntl.h>
19 #include <linux/interrupt.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/in.h>
23 #include <linux/slab.h>
24 #include <linux/string.h>
25 #include <linux/delay.h>
26 #include <linux/errno.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/bitops.h>
31
32 #include <asm/bootinfo.h>
33 #include <asm/system.h>
34 #include <asm/pgtable.h>
35 #include <asm/io.h>
36 #include <asm/dma.h>
37 #include <asm/jazz.h>
38 #include <asm/jazzdma.h>
39
40 #define DRV_NAME "jazzsonic"
41
42 #define SREGS_PAD(n)    u16 n;
43
44 #include "sonic.h"
45
46 /*
47  * Macros to access SONIC registers
48  */
49 #define SONIC_READ(reg) (*((volatile unsigned int *)base_addr+reg))
50
51 #define SONIC_WRITE(reg,val)                                            \
52 do {                                                                    \
53         *((volatile unsigned int *)base_addr+reg) = val;                \
54 }
55
56
57 /* use 0 for production, 1 for verification, >2 for debug */
58 #ifdef SONIC_DEBUG
59 static unsigned int sonic_debug = SONIC_DEBUG;
60 #else 
61 static unsigned int sonic_debug = 1;
62 #endif
63
64 /*
65  * Base address and interrupt of the SONIC controller on JAZZ boards
66  */
67 static struct {
68         unsigned int port;
69         unsigned int irq;
70 } sonic_portlist[] = { {JAZZ_ETHERNET_BASE, JAZZ_ETHERNET_IRQ}, {0, 0}};
71
72 /*
73  * We cannot use station (ethernet) address prefixes to detect the
74  * sonic controller since these are board manufacturer depended.
75  * So we check for known Silicon Revision IDs instead. 
76  */
77 static unsigned short known_revisions[] =
78 {
79         0x04,                   /* Mips Magnum 4000 */
80         0xffff                  /* end of list */
81 };
82
83 /* Index to functions, as function prototypes. */
84
85 static int sonic_probe1(struct net_device *dev, unsigned int base_addr,
86                         unsigned int irq);
87
88
89 /*
90  * Probe for a SONIC ethernet controller on a Mips Jazz board.
91  * Actually probing is superfluous but we're paranoid.
92  */
93 struct net_device * __init sonic_probe(int unit)
94 {
95         struct net_device *dev;
96         struct sonic_local *lp;
97         unsigned int base_addr;
98         int err = 0;
99         int i;
100
101         /*
102          * Don't probe if we're not running on a Jazz board.
103          */
104         if (mips_machgroup != MACH_GROUP_JAZZ)
105                 return ERR_PTR(-ENODEV);
106
107         dev = alloc_etherdev(0);
108         if (!dev)
109                 return ERR_PTR(-ENOMEM);
110
111         sprintf(dev->name, "eth%d", unit);
112         netdev_boot_setup_check(dev);
113         base_addr = dev->base_addr;
114
115         if (base_addr >= KSEG0) { /* Check a single specified location. */
116                 err = sonic_probe1(dev, base_addr, dev->irq);
117         } else if (base_addr != 0) { /* Don't probe at all. */
118                 err = -ENXIO;
119         } else {
120                 for (i = 0; sonic_portlist[i].port; i++) {
121                         int io = sonic_portlist[i].port;
122                         if (sonic_probe1(dev, io, sonic_portlist[i].irq) == 0)
123                                 break;
124                 }
125                 if (!sonic_portlist[i].port)
126                         err = -ENODEV;
127         }
128         if (err)
129                 goto out;
130         err = register_netdev(dev);
131         if (err)
132                 goto out1;
133         return dev;
134 out1:
135         lp = dev->priv;
136         vdma_free(lp->rba_laddr);
137         kfree(lp->rba);
138         vdma_free(lp->cda_laddr);
139         kfree(lp);
140         release_region(dev->base_addr, 0x100);
141 out:
142         free_netdev(dev);
143         return ERR_PTR(err);
144 }
145
146 static int __init sonic_probe1(struct net_device *dev, unsigned int base_addr,
147                                unsigned int irq)
148 {
149         static unsigned version_printed;
150         unsigned int silicon_revision;
151         unsigned int val;
152         struct sonic_local *lp;
153         int err = -ENODEV;
154         int i;
155
156         if (!request_region(base_addr, 0x100, DRV_NAME))
157                 return -EBUSY;
158         /*
159          * get the Silicon Revision ID. If this is one of the known
160          * one assume that we found a SONIC ethernet controller at
161          * the expected location.
162          */
163         silicon_revision = SONIC_READ(SONIC_SR);
164         if (sonic_debug > 1)
165                 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
166
167         i = 0;
168         while (known_revisions[i] != 0xffff
169                && known_revisions[i] != silicon_revision)
170                 i++;
171
172         if (known_revisions[i] == 0xffff) {
173                 printk("SONIC ethernet controller not found (0x%4x)\n",
174                        silicon_revision);
175                 goto out;
176         }
177     
178         if (sonic_debug  &&  version_printed++ == 0)
179                 printk(version);
180
181         printk("%s: Sonic ethernet found at 0x%08lx, ", dev->name, base_addr);
182
183         /* Fill in the 'dev' fields. */
184         dev->base_addr = base_addr;
185         dev->irq = irq;
186
187         /*
188          * Put the sonic into software reset, then
189          * retrieve and print the ethernet address.
190          */
191         SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
192         SONIC_WRITE(SONIC_CEP,0);
193         for (i=0; i<3; i++) {
194                 val = SONIC_READ(SONIC_CAP0-i);
195                 dev->dev_addr[i*2] = val;
196                 dev->dev_addr[i*2+1] = val >> 8;
197         }
198
199         printk("HW Address ");
200         for (i = 0; i < 6; i++) {
201                 printk("%2.2x", dev->dev_addr[i]);
202                 if (i<5)
203                         printk(":");
204         }
205
206         printk(" IRQ %d\n", irq);
207
208         err = -ENOMEM;
209     
210         /* Initialize the device structure. */
211         if (dev->priv == NULL) {
212                 /*
213                  * the memory be located in the same 64kb segment
214                  */
215                 lp = NULL;
216                 i = 0;
217                 do {
218                         lp = kmalloc(sizeof(*lp), GFP_KERNEL);
219                         if ((unsigned long) lp >> 16
220                             != ((unsigned long)lp + sizeof(*lp) ) >> 16) {
221                                 /* FIXME, free the memory later */
222                                 kfree(lp);
223                                 lp = NULL;
224                         }
225                 } while (lp == NULL && i++ < 20);
226
227                 if (lp == NULL) {
228                         printk("%s: couldn't allocate memory for descriptors\n",
229                                dev->name);
230                         goto out;
231                 }
232
233                 memset(lp, 0, sizeof(struct sonic_local));
234
235                 /* get the virtual dma address */
236                 lp->cda_laddr = vdma_alloc(PHYSADDR(lp),sizeof(*lp));
237                 if (lp->cda_laddr == ~0UL) {
238                         printk("%s: couldn't get DMA page entry for "
239                                "descriptors\n", dev->name);
240                         goto out1;
241                 }
242
243                 lp->tda_laddr = lp->cda_laddr + sizeof (lp->cda);
244                 lp->rra_laddr = lp->tda_laddr + sizeof (lp->tda);
245                 lp->rda_laddr = lp->rra_laddr + sizeof (lp->rra);
246         
247                 /* allocate receive buffer area */
248                 /* FIXME, maybe we should use skbs */
249                 lp->rba = kmalloc(SONIC_NUM_RRS * SONIC_RBSIZE, GFP_KERNEL);
250                 if (!lp->rba) {
251                         printk("%s: couldn't allocate receive buffers\n",
252                                dev->name);
253                         goto out2;
254                 }
255
256                 /* get virtual dma address */
257                 lp->rba_laddr = vdma_alloc(PHYSADDR(lp->rba),
258                                            SONIC_NUM_RRS * SONIC_RBSIZE);
259                 if (lp->rba_laddr == ~0UL) {
260                         printk("%s: couldn't get DMA page entry for receive "
261                                "buffers\n",dev->name);
262                         goto out3;
263                 }
264
265                 /* now convert pointer to KSEG1 pointer */
266                 lp->rba = (char *)KSEG1ADDR(lp->rba);
267                 flush_cache_all();
268                 dev->priv = (struct sonic_local *)KSEG1ADDR(lp);
269         }
270
271         lp = (struct sonic_local *)dev->priv;
272         dev->open = sonic_open;
273         dev->stop = sonic_close;
274         dev->hard_start_xmit = sonic_send_packet;
275         dev->get_stats  = sonic_get_stats;
276         dev->set_multicast_list = &sonic_multicast_list;
277         dev->watchdog_timeo = TX_TIMEOUT;
278
279         /*
280          * clear tally counter
281          */
282         SONIC_WRITE(SONIC_CRCT,0xffff);
283         SONIC_WRITE(SONIC_FAET,0xffff);
284         SONIC_WRITE(SONIC_MPT,0xffff);
285
286         return 0;
287 out3:
288         kfree(lp->rba);
289 out2:
290         vdma_free(lp->cda_laddr);
291 out1:
292         kfree(lp);
293 out:
294         release_region(base_addr, 0x100);
295         return err;
296 }
297
298 /*
299  *      SONIC uses a normal IRQ
300  */
301 #define sonic_request_irq       request_irq
302 #define sonic_free_irq          free_irq
303
304 #define sonic_chiptomem(x)      KSEG1ADDR(vdma_log2phys(x))
305
306 #include "sonic.c"