Merge branch 'juju' of git://git.kernel.org/pub/scm/linux/kernel/git/ieee1394/linux13...
[linux-2.6] / drivers / net / jazzsonic.c
1 /*
2  * jazzsonic.c
3  *
4  * (C) 2005 Finn Thain
5  *
6  * Converted to DMA API, and (from the mac68k project) introduced
7  * dhd's support for 16-bit cards.
8  *
9  * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
10  *
11  * This driver is based on work from Andreas Busse, but most of
12  * the code is rewritten.
13  *
14  * (C) 1995 by Andreas Busse (andy@waldorf-gmbh.de)
15  *
16  * A driver for the onboard Sonic ethernet controller on Mips Jazz
17  * systems (Acer Pica-61, Mips Magnum 4000, Olivetti M700 and
18  * perhaps others, too)
19  */
20
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/fcntl.h>
25 #include <linux/interrupt.h>
26 #include <linux/init.h>
27 #include <linux/ioport.h>
28 #include <linux/in.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/delay.h>
32 #include <linux/errno.h>
33 #include <linux/netdevice.h>
34 #include <linux/etherdevice.h>
35 #include <linux/skbuff.h>
36 #include <linux/platform_device.h>
37 #include <linux/dma-mapping.h>
38
39 #include <asm/bootinfo.h>
40 #include <asm/system.h>
41 #include <asm/pgtable.h>
42 #include <asm/io.h>
43 #include <asm/dma.h>
44 #include <asm/jazz.h>
45 #include <asm/jazzdma.h>
46
47 static char jazz_sonic_string[] = "jazzsonic";
48 static struct platform_device *jazz_sonic_device;
49
50 #define SONIC_MEM_SIZE  0x100
51
52 #include "sonic.h"
53
54 /*
55  * Macros to access SONIC registers
56  */
57 #define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
58
59 #define SONIC_WRITE(reg,val)                                            \
60 do {                                                                    \
61         *((volatile unsigned int *)dev->base_addr+(reg)) = (val);               \
62 } while (0)
63
64
65 /* use 0 for production, 1 for verification, >1 for debug */
66 #ifdef SONIC_DEBUG
67 static unsigned int sonic_debug = SONIC_DEBUG;
68 #else
69 static unsigned int sonic_debug = 1;
70 #endif
71
72 /*
73  * Base address and interrupt of the SONIC controller on JAZZ boards
74  */
75 static struct {
76         unsigned int port;
77         unsigned int irq;
78 } sonic_portlist[] = { {JAZZ_ETHERNET_BASE, JAZZ_ETHERNET_IRQ}, {0, 0}};
79
80 /*
81  * We cannot use station (ethernet) address prefixes to detect the
82  * sonic controller since these are board manufacturer depended.
83  * So we check for known Silicon Revision IDs instead.
84  */
85 static unsigned short known_revisions[] =
86 {
87         0x04,                   /* Mips Magnum 4000 */
88         0xffff                  /* end of list */
89 };
90
91 static int jazzsonic_open(struct net_device* dev)
92 {
93         if (request_irq(dev->irq, &sonic_interrupt, IRQF_DISABLED, "sonic", dev)) {
94                 printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq);
95                 return -EAGAIN;
96         }
97         return sonic_open(dev);
98 }
99
100 static int jazzsonic_close(struct net_device* dev)
101 {
102         int err;
103         err = sonic_close(dev);
104         free_irq(dev->irq, dev);
105         return err;
106 }
107
108 static int __init sonic_probe1(struct net_device *dev)
109 {
110         static unsigned version_printed;
111         unsigned int silicon_revision;
112         unsigned int val;
113         struct sonic_local *lp = netdev_priv(dev);
114         int err = -ENODEV;
115         int i;
116
117         if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
118                 return -EBUSY;
119
120         /*
121          * get the Silicon Revision ID. If this is one of the known
122          * one assume that we found a SONIC ethernet controller at
123          * the expected location.
124          */
125         silicon_revision = SONIC_READ(SONIC_SR);
126         if (sonic_debug > 1)
127                 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
128
129         i = 0;
130         while (known_revisions[i] != 0xffff
131                && known_revisions[i] != silicon_revision)
132                 i++;
133
134         if (known_revisions[i] == 0xffff) {
135                 printk("SONIC ethernet controller not found (0x%4x)\n",
136                        silicon_revision);
137                 goto out;
138         }
139
140         if (sonic_debug  &&  version_printed++ == 0)
141                 printk(version);
142
143         printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", lp->device->bus_id, dev->base_addr);
144
145         /*
146          * Put the sonic into software reset, then
147          * retrieve and print the ethernet address.
148          */
149         SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
150         SONIC_WRITE(SONIC_CEP,0);
151         for (i=0; i<3; i++) {
152                 val = SONIC_READ(SONIC_CAP0-i);
153                 dev->dev_addr[i*2] = val;
154                 dev->dev_addr[i*2+1] = val >> 8;
155         }
156
157         err = -ENOMEM;
158
159         /* Initialize the device structure. */
160
161         lp->dma_bitmode = SONIC_BITMODE32;
162
163         /* Allocate the entire chunk of memory for the descriptors.
164            Note that this cannot cross a 64K boundary. */
165         if ((lp->descriptors = dma_alloc_coherent(lp->device,
166                                 SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
167                                 &lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
168                 printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", lp->device->bus_id);
169                 goto out;
170         }
171
172         /* Now set up the pointers to point to the appropriate places */
173         lp->cda = lp->descriptors;
174         lp->tda = lp->cda + (SIZEOF_SONIC_CDA
175                              * SONIC_BUS_SCALE(lp->dma_bitmode));
176         lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
177                              * SONIC_BUS_SCALE(lp->dma_bitmode));
178         lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
179                              * SONIC_BUS_SCALE(lp->dma_bitmode));
180
181         lp->cda_laddr = lp->descriptors_laddr;
182         lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
183                              * SONIC_BUS_SCALE(lp->dma_bitmode));
184         lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
185                              * SONIC_BUS_SCALE(lp->dma_bitmode));
186         lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
187                              * SONIC_BUS_SCALE(lp->dma_bitmode));
188
189         dev->open = jazzsonic_open;
190         dev->stop = jazzsonic_close;
191         dev->hard_start_xmit = sonic_send_packet;
192         dev->get_stats = sonic_get_stats;
193         dev->set_multicast_list = &sonic_multicast_list;
194         dev->tx_timeout = sonic_tx_timeout;
195         dev->watchdog_timeo = TX_TIMEOUT;
196
197         /*
198          * clear tally counter
199          */
200         SONIC_WRITE(SONIC_CRCT,0xffff);
201         SONIC_WRITE(SONIC_FAET,0xffff);
202         SONIC_WRITE(SONIC_MPT,0xffff);
203
204         return 0;
205 out:
206         release_region(dev->base_addr, SONIC_MEM_SIZE);
207         return err;
208 }
209
210 /*
211  * Probe for a SONIC ethernet controller on a Mips Jazz board.
212  * Actually probing is superfluous but we're paranoid.
213  */
214 static int __init jazz_sonic_probe(struct platform_device *pdev)
215 {
216         struct net_device *dev;
217         struct sonic_local *lp;
218         int err = 0;
219         int i;
220
221         /*
222          * Don't probe if we're not running on a Jazz board.
223          */
224         if (mips_machgroup != MACH_GROUP_JAZZ)
225                 return -ENODEV;
226
227         dev = alloc_etherdev(sizeof(struct sonic_local));
228         if (!dev)
229                 return -ENOMEM;
230
231         lp = netdev_priv(dev);
232         lp->device = &pdev->dev;
233         SET_NETDEV_DEV(dev, &pdev->dev);
234         SET_MODULE_OWNER(dev);
235
236         netdev_boot_setup_check(dev);
237
238         if (dev->base_addr >= KSEG0) { /* Check a single specified location. */
239                 err = sonic_probe1(dev);
240         } else if (dev->base_addr != 0) { /* Don't probe at all. */
241                 err = -ENXIO;
242         } else {
243                 for (i = 0; sonic_portlist[i].port; i++) {
244                         dev->base_addr = sonic_portlist[i].port;
245                         dev->irq = sonic_portlist[i].irq;
246                         if (sonic_probe1(dev) == 0)
247                                 break;
248                 }
249                 if (!sonic_portlist[i].port)
250                         err = -ENODEV;
251         }
252         if (err)
253                 goto out;
254         err = register_netdev(dev);
255         if (err)
256                 goto out1;
257
258         printk("%s: MAC ", dev->name);
259         for (i = 0; i < 6; i++) {
260                 printk("%2.2x", dev->dev_addr[i]);
261                 if (i < 5)
262                         printk(":");
263         }
264         printk(" IRQ %d\n", dev->irq);
265
266         return 0;
267
268 out1:
269         release_region(dev->base_addr, SONIC_MEM_SIZE);
270 out:
271         free_netdev(dev);
272
273         return err;
274 }
275
276 MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
277 module_param(sonic_debug, int, 0);
278 MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
279
280 #include "sonic.c"
281
282 static int __devexit jazz_sonic_device_remove (struct platform_device *pdev)
283 {
284         struct net_device *dev = platform_get_drvdata(pdev);
285         struct sonic_local* lp = netdev_priv(dev);
286
287         unregister_netdev(dev);
288         dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
289                           lp->descriptors, lp->descriptors_laddr);
290         release_region (dev->base_addr, SONIC_MEM_SIZE);
291         free_netdev(dev);
292
293         return 0;
294 }
295
296 static struct platform_driver jazz_sonic_driver = {
297         .probe  = jazz_sonic_probe,
298         .remove = __devexit_p(jazz_sonic_device_remove),
299         .driver = {
300                 .name   = jazz_sonic_string,
301         },
302 };
303
304 static int __init jazz_sonic_init_module(void)
305 {
306         int err;
307
308         if ((err = platform_driver_register(&jazz_sonic_driver))) {
309                 printk(KERN_ERR "Driver registration failed\n");
310                 return err;
311         }
312
313         jazz_sonic_device = platform_device_alloc(jazz_sonic_string, 0);
314         if (!jazz_sonic_device)
315                 goto out_unregister;
316
317         if (platform_device_add(jazz_sonic_device)) {
318                 platform_device_put(jazz_sonic_device);
319                 jazz_sonic_device = NULL;
320         }
321
322         return 0;
323
324 out_unregister:
325         platform_driver_unregister(&jazz_sonic_driver);
326
327         return -ENOMEM;
328 }
329
330 static void __exit jazz_sonic_cleanup_module(void)
331 {
332         platform_driver_unregister(&jazz_sonic_driver);
333
334         if (jazz_sonic_device) {
335                 platform_device_unregister(jazz_sonic_device);
336                 jazz_sonic_device = NULL;
337         }
338 }
339
340 module_init(jazz_sonic_init_module);
341 module_exit(jazz_sonic_cleanup_module);