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