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