blob: b71e6633f42d5f8481f3edd7b9061a2a10e889e7 [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";
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define SONIC_MEM_SIZE 0x100
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include "sonic.h"
52
53/*
54 * Macros to access SONIC registers
55 */
Finn Thainefcce832005-08-20 15:53:22 +100056#define SONIC_READ(reg) (*((volatile unsigned int *)dev->base_addr+reg))
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
58#define SONIC_WRITE(reg,val) \
59do { \
Finn Thainefcce832005-08-20 15:53:22 +100060 *((volatile unsigned int *)dev->base_addr+(reg)) = (val); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070061} while (0)
62
63
Finn Thainefcce832005-08-20 15:53:22 +100064/* use 0 for production, 1 for verification, >1 for debug */
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#ifdef SONIC_DEBUG
66static unsigned int sonic_debug = SONIC_DEBUG;
Jeff Garzik6aa20a22006-09-13 13:24:59 -040067#else
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static unsigned int sonic_debug = 1;
69#endif
70
71/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * We cannot use station (ethernet) address prefixes to detect the
73 * sonic controller since these are board manufacturer depended.
Jeff Garzik6aa20a22006-09-13 13:24:59 -040074 * So we check for known Silicon Revision IDs instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 */
76static unsigned short known_revisions[] =
77{
78 0x04, /* Mips Magnum 4000 */
79 0xffff /* end of list */
80};
81
Finn Thainf4d86752007-05-02 12:55:56 +100082static int jazzsonic_open(struct net_device* dev)
83{
84 if (request_irq(dev->irq, &sonic_interrupt, IRQF_DISABLED, "sonic", dev)) {
85 printk(KERN_ERR "%s: unable to get IRQ %d.\n", dev->name, dev->irq);
86 return -EAGAIN;
87 }
88 return sonic_open(dev);
89}
90
91static int jazzsonic_close(struct net_device* dev)
92{
93 int err;
94 err = sonic_close(dev);
95 free_irq(dev->irq, dev);
96 return err;
97}
98
Finn Thainefcce832005-08-20 15:53:22 +100099static int __init sonic_probe1(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
101 static unsigned version_printed;
102 unsigned int silicon_revision;
103 unsigned int val;
Finn Thainefcce832005-08-20 15:53:22 +1000104 struct sonic_local *lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int err = -ENODEV;
106 int i;
107
Finn Thainefcce832005-08-20 15:53:22 +1000108 if (!request_mem_region(dev->base_addr, SONIC_MEM_SIZE, jazz_sonic_string))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 return -EBUSY;
Finn Thainefcce832005-08-20 15:53:22 +1000110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 /*
112 * get the Silicon Revision ID. If this is one of the known
113 * one assume that we found a SONIC ethernet controller at
114 * the expected location.
115 */
116 silicon_revision = SONIC_READ(SONIC_SR);
117 if (sonic_debug > 1)
118 printk("SONIC Silicon Revision = 0x%04x\n",silicon_revision);
119
120 i = 0;
121 while (known_revisions[i] != 0xffff
122 && known_revisions[i] != silicon_revision)
123 i++;
124
125 if (known_revisions[i] == 0xffff) {
126 printk("SONIC ethernet controller not found (0x%4x)\n",
127 silicon_revision);
128 goto out;
129 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 if (sonic_debug && version_printed++ == 0)
132 printk(version);
133
Finn Thainefcce832005-08-20 15:53:22 +1000134 printk(KERN_INFO "%s: Sonic ethernet found at 0x%08lx, ", lp->device->bus_id, dev->base_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136 /*
137 * Put the sonic into software reset, then
138 * retrieve and print the ethernet address.
139 */
140 SONIC_WRITE(SONIC_CMD,SONIC_CR_RST);
141 SONIC_WRITE(SONIC_CEP,0);
142 for (i=0; i<3; i++) {
143 val = SONIC_READ(SONIC_CAP0-i);
144 dev->dev_addr[i*2] = val;
145 dev->dev_addr[i*2+1] = val >> 8;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 err = -ENOMEM;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 /* Initialize the device structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Finn Thainefcce832005-08-20 15:53:22 +1000152 lp->dma_bitmode = SONIC_BITMODE32;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Finn Thainefcce832005-08-20 15:53:22 +1000154 /* Allocate the entire chunk of memory for the descriptors.
155 Note that this cannot cross a 64K boundary. */
156 if ((lp->descriptors = dma_alloc_coherent(lp->device,
157 SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
158 &lp->descriptors_laddr, GFP_KERNEL)) == NULL) {
159 printk(KERN_ERR "%s: couldn't alloc DMA memory for descriptors.\n", lp->device->bus_id);
160 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
Finn Thainefcce832005-08-20 15:53:22 +1000163 /* Now set up the pointers to point to the appropriate places */
164 lp->cda = lp->descriptors;
165 lp->tda = lp->cda + (SIZEOF_SONIC_CDA
166 * SONIC_BUS_SCALE(lp->dma_bitmode));
167 lp->rda = lp->tda + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
168 * SONIC_BUS_SCALE(lp->dma_bitmode));
169 lp->rra = lp->rda + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
170 * SONIC_BUS_SCALE(lp->dma_bitmode));
171
172 lp->cda_laddr = lp->descriptors_laddr;
173 lp->tda_laddr = lp->cda_laddr + (SIZEOF_SONIC_CDA
174 * SONIC_BUS_SCALE(lp->dma_bitmode));
175 lp->rda_laddr = lp->tda_laddr + (SIZEOF_SONIC_TD * SONIC_NUM_TDS
176 * SONIC_BUS_SCALE(lp->dma_bitmode));
177 lp->rra_laddr = lp->rda_laddr + (SIZEOF_SONIC_RD * SONIC_NUM_RDS
178 * SONIC_BUS_SCALE(lp->dma_bitmode));
179
Finn Thainf4d86752007-05-02 12:55:56 +1000180 dev->open = jazzsonic_open;
181 dev->stop = jazzsonic_close;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 dev->hard_start_xmit = sonic_send_packet;
Finn Thainefcce832005-08-20 15:53:22 +1000183 dev->get_stats = sonic_get_stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 dev->set_multicast_list = &sonic_multicast_list;
Finn Thainefcce832005-08-20 15:53:22 +1000185 dev->tx_timeout = sonic_tx_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 dev->watchdog_timeo = TX_TIMEOUT;
187
188 /*
189 * clear tally counter
190 */
191 SONIC_WRITE(SONIC_CRCT,0xffff);
192 SONIC_WRITE(SONIC_FAET,0xffff);
193 SONIC_WRITE(SONIC_MPT,0xffff);
194
195 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196out:
Finn Thainefcce832005-08-20 15:53:22 +1000197 release_region(dev->base_addr, SONIC_MEM_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return err;
199}
200
201/*
202 * Probe for a SONIC ethernet controller on a Mips Jazz board.
203 * Actually probing is superfluous but we're paranoid.
204 */
Russell King3ae5eae2005-11-09 22:32:44 +0000205static int __init jazz_sonic_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 struct net_device *dev;
208 struct sonic_local *lp;
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200209 struct resource *res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 int err = 0;
211 int i;
212
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200213 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
214 if (!res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 return -ENODEV;
216
Finn Thainefcce832005-08-20 15:53:22 +1000217 dev = alloc_etherdev(sizeof(struct sonic_local));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (!dev)
219 return -ENOMEM;
220
Finn Thainefcce832005-08-20 15:53:22 +1000221 lp = netdev_priv(dev);
Russell King3ae5eae2005-11-09 22:32:44 +0000222 lp->device = &pdev->dev;
223 SET_NETDEV_DEV(dev, &pdev->dev);
Finn Thainefcce832005-08-20 15:53:22 +1000224 SET_MODULE_OWNER(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Finn Thainefcce832005-08-20 15:53:22 +1000226 netdev_boot_setup_check(dev);
227
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200228 dev->base_addr = res->start;
229 dev->irq = platform_get_irq(pdev, 0);
230 err = sonic_probe1(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (err)
232 goto out;
233 err = register_netdev(dev);
234 if (err)
235 goto out1;
236
Finn Thainefcce832005-08-20 15:53:22 +1000237 printk("%s: MAC ", dev->name);
238 for (i = 0; i < 6; i++) {
239 printk("%2.2x", dev->dev_addr[i]);
240 if (i < 5)
241 printk(":");
242 }
243 printk(" IRQ %d\n", dev->irq);
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return 0;
246
247out1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 release_region(dev->base_addr, SONIC_MEM_SIZE);
249out:
250 free_netdev(dev);
251
252 return err;
253}
254
Finn Thainefcce832005-08-20 15:53:22 +1000255MODULE_DESCRIPTION("Jazz SONIC ethernet driver");
256module_param(sonic_debug, int, 0);
257MODULE_PARM_DESC(sonic_debug, "jazzsonic debug level (1-4)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#include "sonic.c"
260
Russell King3ae5eae2005-11-09 22:32:44 +0000261static int __devexit jazz_sonic_device_remove (struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Russell King3ae5eae2005-11-09 22:32:44 +0000263 struct net_device *dev = platform_get_drvdata(pdev);
Finn Thainefcce832005-08-20 15:53:22 +1000264 struct sonic_local* lp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Finn Thaind74472f2007-05-01 22:33:02 +0200266 unregister_netdev(dev);
Finn Thainefcce832005-08-20 15:53:22 +1000267 dma_free_coherent(lp->device, SIZEOF_SONIC_DESC * SONIC_BUS_SCALE(lp->dma_bitmode),
268 lp->descriptors, lp->descriptors_laddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 release_region (dev->base_addr, SONIC_MEM_SIZE);
Finn Thaind74472f2007-05-01 22:33:02 +0200270 free_netdev(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 return 0;
273}
274
Russell King3ae5eae2005-11-09 22:32:44 +0000275static struct platform_driver jazz_sonic_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .probe = jazz_sonic_probe,
277 .remove = __devexit_p(jazz_sonic_device_remove),
Russell King3ae5eae2005-11-09 22:32:44 +0000278 .driver = {
279 .name = jazz_sonic_string,
280 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281};
Finn Thainefcce832005-08-20 15:53:22 +1000282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283static int __init jazz_sonic_init_module(void)
284{
Thomas Bogendoerfered9f0e02007-09-08 21:46:49 +0200285 return platform_driver_register(&jazz_sonic_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static void __exit jazz_sonic_cleanup_module(void)
289{
Russell King3ae5eae2005-11-09 22:32:44 +0000290 platform_driver_unregister(&jazz_sonic_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293module_init(jazz_sonic_init_module);
294module_exit(jazz_sonic_cleanup_module);