blob: 75f6f441e876c69322db9ab806960f8b15c08c84 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Finn Thainefcce832005-08-20 15:53:22 +10002 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * (C) 1996,1998 by Thomas Bogendoerfer (tsbogend@alpha.franken.de)
Jeff Garzik6aa20a22006-09-13 13:24:59 -040010 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 * This driver is based on work from Andreas Busse, but most of
12 * the code is rewritten.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040013 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 * (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>
Russell Kingd052d1b2005-10-29 19:07:23 +010036#include <linux/platform_device.h>
Finn Thainefcce832005-08-20 15:53:22 +100037#include <linux/dma-mapping.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
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
47static char jazz_sonic_string[] = "jazzsonic";
48static struct platform_device *jazz_sonic_device;
49
50#define SONIC_MEM_SIZE 0x100
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include "sonic.h"
53
54/*
55 * Macros to access SONIC registers
56 */
Finn Thainefcce832005-08-20 15:53:22 +100057#define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#define SONIC_WRITE(reg,val) \
60do { \
Finn Thainefcce832005-08-20 15:53:22 +100061 *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070062} while (0)
63
64
Finn Thainefcce832005-08-20 15:53:22 +100065/* use 0 for production, 1 for verification, >1 for debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#ifdef SONIC_DEBUG
67static unsigned int sonic_debug = SONIC_DEBUG;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040068#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static unsigned int sonic_debug = 1;
70#endif
71
72/*
73 * Base address and interrupt of the SONIC controller on JAZZ boards
74 */
75static 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.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040083 * So we check for known Silicon Revision IDs instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85static unsigned short known_revisions[] =
86{
87 0x04, /* Mips Magnum 4000 */
88 0xffff /* end of list */
89};
90
Finn Thainf4d86752007-05-02 12:55:56 +100091static 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
100static 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
Finn Thainefcce832005-08-20 15:53:22 +1000108static int __init sonic_probe1(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 static unsigned version_printed;
111 unsigned int silicon_revision;
112 unsigned int val;
Finn Thainefcce832005-08-20 15:53:22 +1000113 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 int err = -ENODEV;
115 int i;
116
Finn Thainefcce832005-08-20 15:53:22 +1000117 if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return -EBUSY;
Finn Thainefcce832005-08-20 15:53:22 +1000119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /*
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 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (sonic_debug && version_printed++ == 0)
141 printk(version);
142
Finn Thainefcce832005-08-20 15:53:22 +1000143 printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", lp->device->bus_id, dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 err = -ENOMEM;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* Initialize the device structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
Finn Thainefcce832005-08-20 15:53:22 +1000161 lp->dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Finn Thainefcce832005-08-20 15:53:22 +1000163 /* 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 }
171
Finn Thainefcce832005-08-20 15:53:22 +1000172 /* 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
Finn Thainf4d86752007-05-02 12:55:56 +1000189 dev->open = jazzsonic_open;
190 dev->stop = jazzsonic_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 dev->hard_start_xmit = sonic_send_packet;
Finn Thainefcce832005-08-20 15:53:22 +1000192 dev->get_stats = sonic_get_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 dev->set_multicast_list = &sonic_multicast_list;
Finn Thainefcce832005-08-20 15:53:22 +1000194 dev->tx_timeout = sonic_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205out:
Finn Thainefcce832005-08-20 15:53:22 +1000206 release_region(dev->base_addr, SONIC_MEM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 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 */
Russell King3ae5eae2005-11-09 22:32:44 +0000214static int __init jazz_sonic_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
216 struct net_device *dev;
217 struct sonic_local *lp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 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
Finn Thainefcce832005-08-20 15:53:22 +1000227 dev = alloc_etherdev(sizeof(struct sonic_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (!dev)
229 return -ENOMEM;
230
Finn Thainefcce832005-08-20 15:53:22 +1000231 lp = netdev_priv(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000232 lp->device = &pdev->dev;
233 SET_NETDEV_DEV(dev, &pdev->dev);
Finn Thainefcce832005-08-20 15:53:22 +1000234 SET_MODULE_OWNER(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Finn Thainefcce832005-08-20 15:53:22 +1000236 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. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 err = -ENXIO;
242 } else {
243 for (i = 0; sonic_portlist[i].port; i++) {
Finn Thainefcce832005-08-20 15:53:22 +1000244 dev->base_addr = sonic_portlist[i].port;
245 dev->irq = sonic_portlist[i].irq;
246 if (sonic_probe1(dev) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 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
Finn Thainefcce832005-08-20 15:53:22 +1000258 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return 0;
267
268out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 release_region(dev->base_addr, SONIC_MEM_SIZE);
270out:
271 free_netdev(dev);
272
273 return err;
274}
275
Finn Thainefcce832005-08-20 15:53:22 +1000276MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
277module_param(sonic_debug, int, 0);
278MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280#include "sonic.c"
281
Russell King3ae5eae2005-11-09 22:32:44 +0000282static int __devexit jazz_sonic_device_remove (struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Russell King3ae5eae2005-11-09 22:32:44 +0000284 struct net_device *dev = platform_get_drvdata(pdev);
Finn Thainefcce832005-08-20 15:53:22 +1000285 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Finn Thaind74472f2007-05-01 22:33:02 +0200287 unregister_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000288 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
289 lp->descriptors, lp->descriptors_laddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 release_region (dev->base_addr, SONIC_MEM_SIZE);
Finn Thaind74472f2007-05-01 22:33:02 +0200291 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return 0;
294}
295
Russell King3ae5eae2005-11-09 22:32:44 +0000296static struct platform_driver jazz_sonic_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 .probe = jazz_sonic_probe,
298 .remove = __devexit_p(jazz_sonic_device_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000299 .driver = {
300 .name = jazz_sonic_string,
301 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
Finn Thainefcce832005-08-20 15:53:22 +1000303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static int __init jazz_sonic_init_module(void)
305{
Finn Thainefcce832005-08-20 15:53:22 +1000306 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Russell King3ae5eae2005-11-09 22:32:44 +0000308 if ((err = platform_driver_register(&jazz_sonic_driver))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 printk(KERN_ERR "Driver registration failed\n");
Finn Thainefcce832005-08-20 15:53:22 +1000310 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 }
312
Russell King95cb5d92005-11-05 21:20:47 +0000313 jazz_sonic_device = platform_device_alloc(jazz_sonic_string, 0);
Ralf Baechle23c2a7b2005-11-22 00:16:51 +0000314 if (!jazz_sonic_device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Russell King95cb5d92005-11-05 21:20:47 +0000317 if (platform_device_add(jazz_sonic_device)) {
318 platform_device_put(jazz_sonic_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 jazz_sonic_device = NULL;
320 }
321
322 return 0;
323
324out_unregister:
Ralf Baechleee7ebdf2005-11-22 00:19:44 +0000325 platform_driver_unregister(&jazz_sonic_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
327 return -ENOMEM;
328}
329
330static void __exit jazz_sonic_cleanup_module(void)
331{
Russell King3ae5eae2005-11-09 22:32:44 +0000332 platform_driver_unregister(&jazz_sonic_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 if (jazz_sonic_device) {
335 platform_device_unregister(jazz_sonic_device);
336 jazz_sonic_device = NULL;
337 }
338}
339
340module_init(jazz_sonic_init_module);
341module_exit(jazz_sonic_cleanup_module);