MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / net / jazzsonic.c
blob8624bfc4905b196699c21873e619e6b0ae876498
1 /*
2 * sonic.c
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)
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)
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>
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>
40 #define DRV_NAME "jazzsonic"
42 #define SREGS_PAD(n) u16 n;
44 #include "sonic.h"
47 * Macros to access SONIC registers
49 #define SONIC_READ(reg) (*((volatile unsigned int *)base_addr+reg))
51 #define SONIC_WRITE(reg,val) \
52 do { \
53 *((volatile unsigned int *)base_addr+reg) = val; \
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
65 * Base address and interrupt of the SONIC controller on JAZZ boards
67 static struct {
68 unsigned int port;
69 unsigned int irq;
70 } sonic_portlist[] = { {JAZZ_ETHERNET_BASE, JAZZ_ETHERNET_IRQ}, {0, 0}};
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.
77 static unsigned short known_revisions[] =
79 0x04, /* Mips Magnum 4000 */
80 0xffff /* end of list */
83 /* Index to functions, as function prototypes. */
85 static int sonic_probe1(struct net_device *dev, unsigned int base_addr,
86 unsigned int irq);
90 * Probe for a SONIC ethernet controller on a Mips Jazz board.
91 * Actually probing is superfluous but we're paranoid.
93 struct net_device * __init sonic_probe(int unit)
95 struct net_device *dev;
96 struct sonic_local *lp;
97 unsigned int base_addr;
98 int err = 0;
99 int i;
102 * Don't probe if we're not running on a Jazz board.
104 if (mips_machgroup != MACH_GROUP_JAZZ)
105 return ERR_PTR(-ENODEV);
107 dev = alloc_etherdev(0);
108 if (!dev)
109 return ERR_PTR(-ENOMEM);
111 sprintf(dev->name, "eth%d", unit);
112 netdev_boot_setup_check(dev);
113 base_addr = dev->base_addr;
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;
125 if (!sonic_portlist[i].port)
126 err = -ENODEV;
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);
146 static int __init sonic_probe1(struct net_device *dev, unsigned int base_addr,
147 unsigned int irq)
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;
156 if (!request_region(base_addr, 0x100, DRV_NAME))
157 return -EBUSY;
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.
163 silicon_revision = SONIC_READ(SONIC_SR);
164 if (sonic_debug > 1)
165 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
167 i = 0;
168 while (known_revisions[i] != 0xffff
169 && known_revisions[i] != silicon_revision)
170 i++;
172 if (known_revisions[i] == 0xffff) {
173 printk("SONIC ethernet controller not found (0x%4x)\n",
174 silicon_revision);
175 goto out;
178 if (sonic_debug && version_printed++ == 0)
179 printk(version);
181 printk("%s: Sonic ethernet found at 0x%08lx, ", dev->name, base_addr);
183 /* Fill in the 'dev' fields. */
184 dev->base_addr = base_addr;
185 dev->irq = irq;
188 * Put the sonic into software reset, then
189 * retrieve and print the ethernet address.
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;
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(":");
206 printk(" IRQ %d\n", irq);
208 err = -ENOMEM;
210 /* Initialize the device structure. */
211 if (dev->priv == NULL) {
213 * the memory be located in the same 64kb segment
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;
225 } while (lp == NULL && i++ < 20);
227 if (lp == NULL) {
228 printk("%s: couldn't allocate memory for descriptors\n",
229 dev->name);
230 goto out;
233 memset(lp, 0, sizeof(struct sonic_local));
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;
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);
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;
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;
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);
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;
280 * clear tally counter
282 SONIC_WRITE(SONIC_CRCT,0xffff);
283 SONIC_WRITE(SONIC_FAET,0xffff);
284 SONIC_WRITE(SONIC_MPT,0xffff);
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;
299 * SONIC uses a normal IRQ
301 #define sonic_request_irq request_irq
302 #define sonic_free_irq free_irq
304 #define sonic_chiptomem(x) KSEG1ADDR(vdma_log2phys(x))
306 #include "sonic.c"